summaryrefslogtreecommitdiff
path: root/LedgerSMB/AM.pm
blob: 4da9e6c973d76286f9b20e6983c1ca39b8f39a80 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2000
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors: Jim Rawlings <jim@your-dba.com>
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # Administration module
  31. # Chart of Accounts
  32. # template routines
  33. # preferences
  34. #
  35. #======================================================================
  36. package AM;
  37. use LedgerSMB::Tax;
  38. use LedgerSMB::Sysconfig;
  39. sub get_account {
  40. my ($self, $myconfig, $form) = @_;
  41. my $dbh = $form->{dbh};
  42. my $query = qq|
  43. SELECT accno, description, charttype, gifi_accno,
  44. category, link, contra
  45. FROM chart
  46. WHERE id = ?|;
  47. my $sth = $dbh->prepare($query);
  48. $sth->execute($form->{id}) || $form->dberror($query);
  49. my $ref = $sth->fetchrow_hashref(NAME_lc);
  50. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  51. $sth->finish;
  52. # get default accounts
  53. $query = qq|
  54. SELECT inventory_accno_id, income_accno_id, expense_accno_id,
  55. fxgain_accno_id, fxloss_accno_id
  56. FROM defaults|;
  57. $sth = $dbh->prepare($query);
  58. $sth->execute || $form->dberror($query);
  59. $ref = $sth->fetchrow_hashref(NAME_lc);
  60. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  61. $sth->finish;
  62. # check if we have any transactions
  63. $query = qq|
  64. SELECT trans_id
  65. FROM acc_trans
  66. WHERE chart_id = ?
  67. LIMIT 1|;
  68. $sth = $dbh->prepare($query);
  69. $sth->execute($form->{id});
  70. ($form->{orphaned}) = $sth->fetchrow_array();
  71. $form->{orphaned} = !$form->{orphaned};
  72. $dbh->commit;
  73. }
  74. sub save_account {
  75. my ($self, $myconfig, $form) = @_;
  76. # connect to database, turn off AutoCommit
  77. my $dbh = $form->{dbh};
  78. $form->{link} = "";
  79. foreach my $item ($form->{AR},
  80. $form->{AR_amount},
  81. $form->{AR_tax},
  82. $form->{AR_paid},
  83. $form->{AP},
  84. $form->{AP_amount},
  85. $form->{AP_tax},
  86. $form->{AP_paid},
  87. $form->{IC},
  88. $form->{IC_income},
  89. $form->{IC_sale},
  90. $form->{IC_expense},
  91. $form->{IC_cogs},
  92. $form->{IC_taxpart},
  93. $form->{IC_taxservice}) {
  94. $form->{link} .= "${item}:" if ($item);
  95. }
  96. chop $form->{link};
  97. # strip blanks from accno
  98. for (qw(accno gifi_accno)) { $form->{$_} =~ s/( |')//g }
  99. foreach my $item (qw(accno gifi_accno description)) {
  100. $form->{$item} =~ s/-(-+)/-/g;
  101. $form->{$item} =~ s/ ( )+/ /g;
  102. }
  103. my $query;
  104. my $sth;
  105. $form->{contra} *= 1;
  106. my @queryargs;
  107. @queryargs = ($form->{accno}, $form->{description},
  108. $form->{charttype}, $form->{gifi_accno},
  109. $form->{category}, $form->{"link"},
  110. $form->{contra});
  111. # if we have an id then replace the old record
  112. if ($form->{id}) {
  113. $query = qq|
  114. UPDATE chart SET accno = ?,
  115. description = ?,
  116. charttype = ?,
  117. gifi_accno = ?,
  118. category = ?,
  119. link = ?,
  120. contra = ?
  121. WHERE id = ?|;
  122. push @queryargs, $form->{id};
  123. } else {
  124. $query = qq|
  125. INSERT INTO chart
  126. (accno, description, charttype,
  127. gifi_accno, category, link, contra)
  128. VALUES (?, ?, ?, ?, ?, ?, ?)|;
  129. }
  130. $sth = $dbh->prepare($query);
  131. $sth->execute(@queryargs) || $form->dberror($query);
  132. $sth->finish;
  133. $chart_id = $dbh->quote($form->{id});
  134. if (! $form->{id}) {
  135. # get id from chart
  136. $query = qq|
  137. SELECT id
  138. FROM chart
  139. WHERE accno = ?|;
  140. $sth = $dbh->prepare($query);
  141. $sth->execute($form->{accno});
  142. ($chart_id) = $sth->fetchrow_array();
  143. $sth->finish;
  144. }
  145. if ($form->{IC_taxpart} || $form->{IC_taxservice} || $form->{AR_tax} || $form->{AP_tax}) {
  146. # add account if it doesn't exist in tax
  147. $query = qq|SELECT chart_id
  148. FROM tax
  149. WHERE chart_id = $chart_id|;
  150. my ($tax_id) = $dbh->selectrow_array($query);
  151. # add tax if it doesn't exist
  152. unless ($tax_id) {
  153. $query = qq|INSERT INTO tax (chart_id, rate)
  154. VALUES ($chart_id, 0)|;
  155. $dbh->do($query) || $form->dberror($query);
  156. }
  157. } else {
  158. # remove tax
  159. if ($form->{id}) {
  160. $query = qq|DELETE FROM tax
  161. WHERE chart_id = $form->{id}|;
  162. $dbh->do($query) || $form->dberror($query);
  163. }
  164. }
  165. # commit
  166. my $rc = $dbh->commit;
  167. $rc;
  168. }
  169. sub delete_account {
  170. my ($self, $myconfig, $form) = @_;
  171. # connect to database, turn off AutoCommit
  172. my $dbh = $form->{dbh};
  173. my $sth;
  174. my $query = qq|
  175. SELECT count(*)
  176. FROM acc_trans
  177. WHERE chart_id = ?|;
  178. $sth = $dbh->prepare($query);
  179. $sth->execute($form->{id});
  180. my ($rowcount) = $sth->fetchrow_array();
  181. if ($dbh->selectrow_array($query)) {
  182. $form->error(
  183. "Cannot delete accounts with associated transactions!"
  184. );
  185. }
  186. # delete chart of account record
  187. $query = qq|
  188. DELETE FROM chart
  189. WHERE id = ?|;
  190. $sth = $dbh->prepare($query);
  191. $sth->execute($form->{id}) || $form->dberror($query);
  192. # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
  193. $query = qq|
  194. UPDATE parts
  195. SET inventory_accno_id = (SELECT inventory_accno_id
  196. FROM defaults)
  197. WHERE inventory_accno_id = ?|;
  198. $sth = $dbh->prepare($query);
  199. $dbh->execute($form->{id}) || $form->dberror($query);
  200. for (qw(income_accno_id expense_accno_id)){
  201. $query = qq|
  202. UPDATE parts
  203. SET $_ = (SELECT $_
  204. FROM defaults)
  205. WHERE $_ = ?|;
  206. $sth = $dbh->prepare($query);
  207. $sth->execute($form->{id}) || $form->dberror($query);
  208. $sth->finish;
  209. }
  210. foreach my $table (qw(partstax customertax vendortax tax)) {
  211. $query = qq|
  212. DELETE FROM $table
  213. WHERE chart_id = ?|;
  214. $sth = $dbh->prepare($query);
  215. $sth->execute($form->{id}) || $form->dberror($query);
  216. $sth->finish;
  217. }
  218. # commit and redirect
  219. my $rc = $dbh->commit;
  220. $rc;
  221. }
  222. sub gifi_accounts {
  223. my ($self, $myconfig, $form) = @_;
  224. # connect to database
  225. my $dbh = $form->{dbh};
  226. my $query = qq|
  227. SELECT accno, description
  228. FROM gifi
  229. ORDER BY accno|;
  230. $sth = $dbh->prepare($query);
  231. $sth->execute || $form->dberror($query);
  232. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  233. push @{ $form->{ALL} }, $ref;
  234. }
  235. $sth->finish;
  236. $dbh->commit;
  237. }
  238. sub get_gifi {
  239. my ($self, $myconfig, $form) = @_;
  240. # connect to database
  241. my $dbh = $form->{dbh};
  242. my $sth;
  243. my $query = qq|
  244. SELECT accno, description
  245. FROM gifi
  246. WHERE accno = '$form->{accno}'|;
  247. $sth = $dbh->prepare($query);
  248. $sth->execute($form->{accno}) || $form->dberror($query);
  249. ($form->{accno}, $form->{description}) = $sth->fetchrow_array();
  250. $sth->finish;
  251. # check for transactions
  252. $query = qq|
  253. SELECT count(*)
  254. FROM acc_trans a
  255. JOIN chart c ON (a.chart_id = c.id)
  256. JOIN gifi g ON (c.gifi_accno = g.accno)
  257. WHERE g.accno = ?|;
  258. $sth = $dbh->prepare($query);
  259. $sth->execute($form->{accno}) || $form->dberror($query);
  260. ($numrows) = $dbh->selectrow_array($query);
  261. if (($numrows * 1) == 0){
  262. $form->{orphaned} = 1;
  263. } else {
  264. $form->{orphaned} = 0;
  265. }
  266. $dbh->commit;
  267. }
  268. sub save_gifi {
  269. my ($self, $myconfig, $form) = @_;
  270. my $dbh = $form->{dbh};
  271. $form->{accno} =~ s/( |')//g;
  272. foreach my $item (qw(accno description)) {
  273. $form->{$item} =~ s/-(-+)/-/g;
  274. $form->{$item} =~ s/ ( )+/ /g;
  275. }
  276. my @queryargs = ($form->{accno}, $form->{description});
  277. # id is the old account number!
  278. if ($form->{id}) {
  279. $query = qq|
  280. UPDATE gifi
  281. SET accno = ?,
  282. description = ?
  283. WHERE accno = ?|;
  284. push @queryargs, $form->{id};
  285. } else {
  286. $query = qq|
  287. INSERT INTO gifi (accno, description)
  288. VALUES (?, ?)|;
  289. }
  290. $sth = $dbh->prepare($query);
  291. $sth->execute(@queryargs) || $form->dberror;
  292. $sth->finish;
  293. $dbh->commit;
  294. }
  295. sub delete_gifi {
  296. my ($self, $myconfig, $form) = @_;
  297. # connect to database
  298. my $dbh = $form->{dbh};
  299. # id is the old account number!
  300. $query = qq|
  301. DELETE FROM gifi
  302. WHERE accno = ?|;
  303. $sth = $dbh->prepare($query);
  304. $sth->execute($form->{id}) || $form->dberror($query);
  305. $sth->finish;
  306. $dbh->commit;
  307. }
  308. sub warehouses {
  309. my ($self, $myconfig, $form) = @_;
  310. # connect to database
  311. my $dbh = $form->{dbh};
  312. $form->sort_order();
  313. my $query = qq|
  314. SELECT id, description
  315. FROM warehouse
  316. ORDER BY description $form->{direction}|;
  317. $sth = $dbh->prepare($query);
  318. $sth->execute || $form->dberror($query);
  319. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  320. push @{ $form->{ALL} }, $ref;
  321. }
  322. $sth->finish;
  323. $dbh->commit;
  324. }
  325. sub get_warehouse {
  326. my ($self, $myconfig, $form) = @_;
  327. # connect to database
  328. my $dbh = $form->{dbh};
  329. my $sth;
  330. my $query = qq|
  331. SELECT description
  332. FROM warehouse
  333. WHERE id = ?|;
  334. $sth = $dbh->prepare($query);
  335. $sth->execute($form->{id}) || $form->dberror($query);
  336. ($form->{description}) = $sth->fetchrow_array($query);
  337. $sth->finish;
  338. # see if it is in use
  339. $query = qq|
  340. SELECT count(*)
  341. FROM inventory
  342. WHERE warehouse_id = ?|;
  343. $sth = $dbh->prepare($query);
  344. $sth->execute($form->{id});
  345. ($form->{orphaned}) = $sth->fetchrow_array($query);
  346. if (($form->{orphaned} * 1) == 0){
  347. $form->{orphaned} = 1;
  348. } else {
  349. $form->{orphaned} = 0;
  350. }
  351. $dbh->commit;
  352. }
  353. sub save_warehouse {
  354. my ($self, $myconfig, $form) = @_;
  355. # connect to database
  356. my $dbh = $form->{dbh};
  357. my $sth;
  358. my @queryargs = ($form->{description});
  359. $form->{description} =~ s/-(-)+/-/g;
  360. $form->{description} =~ s/ ( )+/ /g;
  361. if ($form->{id}) {
  362. $query = qq|
  363. UPDATE warehouse
  364. SET description = ?
  365. WHERE id = ?|;
  366. push @queryargs, $form->{id};
  367. } else {
  368. $query = qq|
  369. INSERT INTO warehouse (description)
  370. VALUES (?)|;
  371. }
  372. $sth = $dbh->prepare($query);
  373. $sth->execute(@queryargs) || $form->dberror($query);
  374. $sth->finish;
  375. $dbh->commit;
  376. }
  377. sub delete_warehouse {
  378. my ($self, $myconfig, $form) = @_;
  379. # connect to database
  380. my $dbh = $form->{dbh};
  381. $query = qq|
  382. DELETE FROM warehouse
  383. WHERE id = ?|;
  384. $dbh->prepare($query)->execute($form->{id}) || $form->dberror($query);
  385. $dbh->commit;
  386. }
  387. sub departments {
  388. my ($self, $myconfig, $form) = @_;
  389. # connect to database
  390. my $dbh = $form->{dbh};
  391. $form->sort_order();
  392. my $query = qq|SELECT id, description, role
  393. FROM department
  394. ORDER BY description $form->{direction}|;
  395. $sth = $dbh->prepare($query);
  396. $sth->execute || $form->dberror($query);
  397. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  398. push @{ $form->{ALL} }, $ref;
  399. }
  400. $sth->finish;
  401. $dbh->commit;
  402. }
  403. sub get_department {
  404. my ($self, $myconfig, $form) = @_;
  405. # connect to database
  406. my $dbh = $form->{dbh};
  407. my $sth;
  408. my $query = qq|
  409. SELECT description, role
  410. FROM department
  411. WHERE id = ?|;
  412. $sth = $dbh->prepare($query);
  413. $sth->execute($form->{id});
  414. ($form->{description}, $form->{role}) = $sth->fetchrow_array($query);
  415. $sth->finish;
  416. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  417. # see if it is in use
  418. $query = qq|
  419. SELECT count(*)
  420. FROM dpt_trans
  421. WHERE department_id = ? |;
  422. $sth = $dbh->prepare($query);
  423. $sth->execute($form->{id});
  424. ($form->{orphaned}) = $sth->fetchrow_array($query);
  425. if (($form->{orphaned} * 1) == 0){
  426. $form->{orphaned} = 1;
  427. } else {
  428. $form->{orphaned} = 0;
  429. }
  430. $dbh->commit;
  431. }
  432. sub save_department {
  433. my ($self, $myconfig, $form) = @_;
  434. # connect to database
  435. my $dbh = $form->{dbh};
  436. $form->{description} =~ s/-(-)+/-/g;
  437. $form->{description} =~ s/ ( )+/ /g;
  438. my $sth;
  439. my @queryargs = ($form->{description}, $form->{role});
  440. if ($form->{id}) {
  441. $query = qq|
  442. UPDATE department
  443. SET description = ?,
  444. role = ?
  445. WHERE id = ?|;
  446. push @queryargs, $form->{id};
  447. } else {
  448. $query = qq|
  449. INSERT INTO department (description, role)
  450. VALUES (?, ?)|;
  451. }
  452. $sth = $dbh->prepare($query);
  453. $sth->execute(@queryargs) || $form->dberror($query);
  454. $dbh->commit;
  455. }
  456. sub delete_department {
  457. my ($self, $myconfig, $form) = @_;
  458. # connect to database
  459. my $dbh = $form->{dbh};
  460. $query = qq|
  461. DELETE FROM department
  462. WHERE id = ?|;
  463. $dbh->prepare($query)->execute($form->{id});
  464. $dbh->commit;
  465. }
  466. sub business {
  467. my ($self, $myconfig, $form) = @_;
  468. # connect to database
  469. my $dbh = $form->{dbh};
  470. $form->sort_order();
  471. my $query = qq|
  472. SELECT id, description, discount
  473. FROM business
  474. ORDER BY description $form->{direction}|;
  475. $sth = $dbh->prepare($query);
  476. $sth->execute || $form->dberror($query);
  477. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  478. push @{ $form->{ALL} }, $ref;
  479. }
  480. $sth->finish;
  481. $dbh->commit;
  482. }
  483. sub get_business {
  484. my ($self, $myconfig, $form) = @_;
  485. # connect to database
  486. my $dbh = $form->dbconnect($myconfig);
  487. my $query = qq|
  488. SELECT description, discount
  489. FROM business
  490. WHERE id = ?|;
  491. $sth = $dbh->prepare($query);
  492. $sth->execute($form->{id});
  493. ($form->{description}, $form->{discount}) = $sth->fetchrow_array();
  494. $dbh->commit;
  495. }
  496. sub save_business {
  497. my ($self, $myconfig, $form) = @_;
  498. # connect to database
  499. my $dbh = $form->dbconnect($myconfig);
  500. $form->{description} =~ s/-(-)+/-/g;
  501. $form->{description} =~ s/ ( )+/ /g;
  502. $form->{discount} /= 100;
  503. my $sth;
  504. my @queryargs = ($form->{description}, $form->{discount});
  505. if ($form->{id}) {
  506. $query = qq|
  507. UPDATE business
  508. SET description = ?,
  509. discount = ?
  510. WHERE id = ?|;
  511. push @queryargs, $form->{id};
  512. } else {
  513. $query = qq|INSERT INTO business (description, discount)
  514. VALUES (?, ?)|;
  515. }
  516. $dbh->prepare($query)->execute(@queryargs) || $form->dberror($query);
  517. $dbh->commit;
  518. }
  519. sub delete_business {
  520. my ($self, $myconfig, $form) = @_;
  521. # connect to database
  522. my $dbh = $form->{dbh};
  523. $query = qq|
  524. DELETE FROM business
  525. WHERE id = ?|;
  526. $dbh->prepare($query)->execute($form->{id}) || $form->dberror($query);
  527. $dbh->commit;
  528. }
  529. sub sic {
  530. my ($self, $myconfig, $form) = @_;
  531. # connect to database
  532. my $dbh = $form->{dbh};
  533. $form->{sort} = "code" unless $form->{sort};
  534. my @a = qw(code description);
  535. my %ordinal = ( code => 1,
  536. description => 3 );
  537. my $sortorder = $form->sort_order(\@a, \%ordinal);
  538. my $query = qq|SELECT code, sictype, description
  539. FROM sic
  540. ORDER BY $sortorder|;
  541. $sth = $dbh->prepare($query);
  542. $sth->execute || $form->dberror($query);
  543. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  544. push @{ $form->{ALL} }, $ref;
  545. }
  546. $sth->finish;
  547. $dbh->commit;
  548. }
  549. sub get_sic {
  550. my ($self, $myconfig, $form) = @_;
  551. # connect to database
  552. my $dbh = $form->{dbh};
  553. my $query = qq|
  554. SELECT code, sictype, description
  555. FROM sic
  556. WHERE code = |.$dbh->quote($form->{code});
  557. my $sth = $dbh->prepare($query);
  558. $sth->execute || $form->dberror($query);
  559. my $ref = $sth->fetchrow_hashref(NAME_lc);
  560. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  561. $sth->finish;
  562. $dbh->commit;
  563. }
  564. sub save_sic {
  565. my ($self, $myconfig, $form) = @_;
  566. # connect to database
  567. my $dbh = $form->{dbh};
  568. foreach my $item (qw(code description)) {
  569. $form->{$item} =~ s/-(-)+/-/g;
  570. }
  571. my $sth;
  572. @queryargs = ($form->{code}, $form->{sictype}, $form->{description});
  573. # if there is an id
  574. if ($form->{id}) {
  575. $query = qq|
  576. UPDATE sic
  577. SET code = ?,
  578. sictype = ?,
  579. description = ?
  580. WHERE code = ?)|;
  581. push @queryargs, $form->{id};
  582. } else {
  583. $query = qq|
  584. INSERT INTO sic (code, sictype, description)
  585. VALUES (?, ?, ?)|;
  586. }
  587. $dbh->prepare($query)->execute(@queryargs) || $form->dberror($query);
  588. $dbh->commit;
  589. }
  590. sub delete_sic {
  591. my ($self, $myconfig, $form) = @_;
  592. # connect to database
  593. my $dbh = $form->{dbh};
  594. $query = qq|
  595. DELETE FROM sic
  596. WHERE code = ?|;
  597. $dbh->prepare($query)->execute($form->{code});
  598. $dbh->commit;
  599. }
  600. sub language {
  601. my ($self, $myconfig, $form) = @_;
  602. # connect to database
  603. my $dbh = $form->{dbh};
  604. $form->{sort} = "code" unless $form->{sort};
  605. my @a = qw(code description);
  606. my %ordinal = ( code => 1,
  607. description => 2 );
  608. my $sortorder = $form->sort_order(\@a, \%ordinal);
  609. my $query = qq|
  610. SELECT code, description
  611. FROM language
  612. ORDER BY $sortorder|;
  613. $sth = $dbh->prepare($query);
  614. $sth->execute || $form->dberror($query);
  615. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  616. push @{ $form->{ALL} }, $ref;
  617. }
  618. $sth->finish;
  619. $dbh->commit;
  620. }
  621. sub get_language {
  622. my ($self, $myconfig, $form) = @_;
  623. # connect to database
  624. my $dbh = $form->{dbh};
  625. ## needs fixing (SELECT *...)
  626. my $query = qq|
  627. SELECT *
  628. FROM language
  629. WHERE code = ?|;
  630. my $sth = $dbh->prepare($query);
  631. $sth->execute($form->{code}) || $form->dberror($query);
  632. my $ref = $sth->fetchrow_hashref(NAME_lc);
  633. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  634. $sth->finish;
  635. $dbh->commit;
  636. }
  637. sub save_language {
  638. my ($self, $myconfig, $form) = @_;
  639. # connect to database
  640. my $dbh = $form->{dbh};
  641. $form->{code} =~ s/ //g;
  642. foreach my $item (qw(code description)) {
  643. $form->{$item} =~ s/-(-)+/-/g;
  644. $form->{$item} =~ s/ ( )+/-/g;
  645. }
  646. my $sth;
  647. my @queryargs = ($form->{code}, $form->{description});
  648. # if there is an id
  649. if ($form->{id}) {
  650. $query = qq|
  651. UPDATE language
  652. SET code = ?,
  653. description = ?
  654. WHERE code = ?|;
  655. push @queryargs, $form->{id};
  656. } else {
  657. $query = qq|
  658. INSERT INTO language (code, description)
  659. VALUES (?, ?)|;
  660. }
  661. $dbh->prepare($query)->execute(@queryargs) || $form->dberror($query);
  662. $dbh->commit;
  663. }
  664. sub delete_language {
  665. my ($self, $myconfig, $form) = @_;
  666. # connect to database
  667. my $dbh = $form->{dbh};
  668. $query = qq|
  669. DELETE FROM language
  670. WHERE code = |.$dbh->quote($form->{code});
  671. $dbh->do($query) || $form->dberror($query);
  672. $dbh->{dbh};
  673. }
  674. sub recurring_transactions {
  675. my ($self, $myconfig, $form) = @_;
  676. my $dbh = $form->{dbh};
  677. my $query = qq|SELECT curr FROM defaults|;
  678. my ($defaultcurrency) = $dbh->selectrow_array($query);
  679. $defaultcurrency = $dbh->quote($defaultcurrency =~ s/:.*//g);
  680. $form->{sort} ||= "nextdate";
  681. my @a = ($form->{sort});
  682. my $sortorder = $form->sort_order(\@a);
  683. $query = qq|
  684. SELECT 'ar' AS module, 'ar' AS transaction, a.invoice,
  685. n.name AS description, a.amount,
  686. s.*, se.formname AS recurringemail,
  687. sp.formname AS recurringprint,
  688. s.nextdate - current_date AS overdue,
  689. 'customer' AS vc,
  690. ex.buy AS exchangerate, a.curr,
  691. (s.nextdate IS NULL OR s.nextdate > s.enddate)
  692. AS expired
  693. FROM recurring s
  694. JOIN ar a ON (a.id = s.id)
  695. JOIN customer n ON (n.id = a.customer_id)
  696. LEFT JOIN recurringemail se ON (se.id = s.id)
  697. LEFT JOIN recurringprint sp ON (sp.id = s.id)
  698. LEFT JOIN exchangerate ex
  699. ON (ex.curr = a.curr AND a.transdate = ex.transdate)
  700. UNION
  701. SELECT 'ap' AS module, 'ap' AS transaction, a.invoice,
  702. n.name AS description, a.amount,
  703. s.*, se.formname AS recurringemail,
  704. sp.formname AS recurringprint,
  705. s.nextdate - current_date AS overdue, 'vendor' AS vc,
  706. ex.sell AS exchangerate, a.curr,
  707. (s.nextdate IS NULL OR s.nextdate > s.enddate)
  708. AS expired
  709. FROM recurring s
  710. JOIN ap a ON (a.id = s.id)
  711. JOIN vendor n ON (n.id = a.vendor_id)
  712. LEFT JOIN recurringemail se ON (se.id = s.id)
  713. LEFT JOIN recurringprint sp ON (sp.id = s.id)
  714. LEFT JOIN exchangerate ex ON
  715. (ex.curr = a.curr AND a.transdate = ex.transdate)
  716. UNION
  717. SELECT 'gl' AS module, 'gl' AS transaction, FALSE AS invoice,
  718. a.description, (SELECT SUM(ac.amount)
  719. FROM acc_trans ac
  720. WHERE ac.trans_id = a.id
  721. AND ac.amount > 0) AS amount,
  722. s.*, se.formname AS recurringemail,
  723. sp.formname AS recurringprint,
  724. s.nextdate - current_date AS overdue, '' AS vc,
  725. '1' AS exchangerate, $defaultcurrency AS curr,
  726. (s.nextdate IS NULL OR s.nextdate > s.enddate)
  727. AS expired
  728. FROM recurring s
  729. JOIN gl a ON (a.id = s.id)
  730. LEFT JOIN recurringemail se ON (se.id = s.id)
  731. LEFT JOIN recurringprint sp ON (sp.id = s.id)
  732. UNION
  733. SELECT 'oe' AS module, 'so' AS transaction, FALSE AS invoice,
  734. n.name AS description, a.amount,
  735. s.*, se.formname AS recurringemail,
  736. sp.formname AS recurringprint,
  737. s.nextdate - current_date AS overdue,
  738. 'customer' AS vc,
  739. ex.buy AS exchangerate, a.curr,
  740. (s.nextdate IS NULL OR s.nextdate > s.enddate)
  741. AS expired
  742. FROM recurring s
  743. JOIN oe a ON (a.id = s.id)
  744. JOIN customer n ON (n.id = a.customer_id)
  745. LEFT JOIN recurringemail se ON (se.id = s.id)
  746. LEFT JOIN recurringprint sp ON (sp.id = s.id)
  747. LEFT JOIN exchangerate ex ON
  748. (ex.curr = a.curr AND a.transdate = ex.transdate)
  749. WHERE a.quotation = '0'
  750. UNION
  751. SELECT 'oe' AS module, 'po' AS transaction, FALSE AS invoice,
  752. n.name AS description, a.amount,
  753. s.*, se.formname AS recurringemail,
  754. sp.formname AS recurringprint,
  755. s.nextdate - current_date AS overdue, 'vendor' AS vc,
  756. ex.sell AS exchangerate, a.curr,
  757. (s.nextdate IS NULL OR s.nextdate > s.enddate)
  758. AS expired
  759. FROM recurring s
  760. JOIN oe a ON (a.id = s.id)
  761. JOIN vendor n ON (n.id = a.vendor_id)
  762. LEFT JOIN recurringemail se ON (se.id = s.id)
  763. LEFT JOIN recurringprint sp ON (sp.id = s.id)
  764. LEFT JOIN exchangerate ex ON
  765. (ex.curr = a.curr AND a.transdate = ex.transdate)
  766. WHERE a.quotation = '0'
  767. ORDER BY $sortorder|;
  768. my $sth = $dbh->prepare($query);
  769. $sth->execute || $form->dberror($query);
  770. my $id;
  771. my $transaction;
  772. my %e = ();
  773. my %p = ();
  774. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  775. $ref->{exchangerate} ||= 1;
  776. if ($ref->{id} != $id) {
  777. if (%e) {
  778. $form->{transactions}{$transaction}->[$i]->{recurringemail} = "";
  779. for (keys %e) {
  780. $form->{transactions}{$transaction}->[$i]->{recurringemail} .= "${_}:";
  781. }
  782. chop $form->{transactions}{$transaction}->[$i]->{recurringemail};
  783. }
  784. if (%p) {
  785. $form->{transactions}{$transaction}->[$i]->{recurringprint} = "";
  786. for (keys %p) { $form->{transactions}{$transaction}->[$i]->{recurringprint} .= "${_}:" }
  787. chop $form->{transactions}{$transaction}->[$i]->{recurringprint};
  788. }
  789. %e = ();
  790. %p = ();
  791. push @{ $form->{transactions}{$ref->{transaction}} }, $ref;
  792. $id = $ref->{id};
  793. $i = $#{ $form->{transactions}{$ref->{transaction}} };
  794. }
  795. $transaction = $ref->{transaction};
  796. $e{$ref->{recurringemail}} = 1 if $ref->{recurringemail};
  797. $p{$ref->{recurringprint}} = 1 if $ref->{recurringprint};
  798. }
  799. $sth->finish;
  800. # this is for the last row
  801. if (%e) {
  802. $form->{transactions}{$transaction}->[$i]->{recurringemail} = "";
  803. for (keys %e) { $form->{transactions}{$transaction}->[$i]->{recurringemail} .= "${_}:" }
  804. chop $form->{transactions}{$transaction}->[$i]->{recurringemail};
  805. }
  806. if (%p) {
  807. $form->{transactions}{$transaction}->[$i]->{recurringprint} = "";
  808. for (keys %p) { $form->{transactions}{$transaction}->[$i]->{recurringprint} .= "${_}:" }
  809. chop $form->{transactions}{$transaction}->[$i]->{recurringprint};
  810. }
  811. $dbh->commit;
  812. }
  813. sub recurring_details {
  814. my ($self, $myconfig, $form, $id) = @_;
  815. my $dbh = $form->{dbh};
  816. my $query = qq|
  817. SELECT s.*, ar.id AS arid, ar.invoice AS arinvoice,
  818. ap.id AS apid, ap.invoice AS apinvoice,
  819. ar.duedate - ar.transdate AS overdue,
  820. ar.datepaid - ar.transdate AS paid,
  821. oe.reqdate - oe.transdate AS req,
  822. oe.id AS oeid, oe.customer_id, oe.vendor_id
  823. FROM recurring s
  824. LEFT JOIN ar ON (ar.id = s.id)
  825. LEFT JOIN ap ON (ap.id = s.id)
  826. LEFT JOIN oe ON (oe.id = s.id)
  827. WHERE s.id = ?|;
  828. my $sth = $dbh->prepare($query);
  829. $sth->execute($id) || $form->dberror($query);
  830. my $ref = $sth->fetchrow_hashref(NAME_lc);
  831. $form->{vc} = "customer" if $ref->{customer_id};
  832. $form->{vc} = "vendor" if $ref->{vendor_id};
  833. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  834. $sth->finish;
  835. $form->{invoice} = ($form->{arid} && $form->{arinvoice});
  836. $form->{invoice} = ($form->{apid} && $form->{apinvoice}) unless $form->{invoice};
  837. $query = qq|
  838. SELECT *
  839. FROM recurringemail
  840. WHERE id = ?|;
  841. $sth = $dbh->prepare($query);
  842. $sth->execute($id) || $form->dberror($query);
  843. $form->{recurringemail} = "";
  844. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  845. $form->{recurringemail} .= "$ref->{formname}:$ref->{format}:";
  846. $form->{message} = $ref->{message};
  847. }
  848. $sth->finish;
  849. $query = qq|
  850. SELECT *
  851. FROM recurringprint
  852. WHERE id = ?|;
  853. $sth = $dbh->prepare($query);
  854. $sth->execute($id) || $form->dberror($query);
  855. $form->{recurringprint} = "";
  856. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  857. $form->{recurringprint} .=
  858. "$ref->{formname}:$ref->{format}:$ref->{printer}:";
  859. }
  860. $sth->finish;
  861. chop $form->{recurringemail};
  862. chop $form->{recurringprint};
  863. for (qw(arinvoice apinvoice)) { delete $form->{$_} }
  864. $dbh->commit;
  865. }
  866. sub update_recurring {
  867. my ($self, $myconfig, $form, $id) = @_;
  868. my $dbh = $form->{dbh};
  869. $id = $dbh->quote($id);
  870. my $query = qq|
  871. SELECT nextdate, repeat, unit
  872. FROM recurring
  873. WHERE id = $id|;
  874. my ($nextdate, $repeat, $unit) = $dbh->selectrow_array($query);
  875. $nextdate = $dbh->quote($nextdate);
  876. my $interval = $dbh->quote("$repeat $unit");
  877. # check if it is the last date
  878. $query = qq|
  879. SELECT (date $nextdate + interval $interval) > enddate
  880. FROM recurring
  881. WHERE id = $id|;
  882. my ($last_repeat) = $dbh->selectrow_array($query);
  883. if ($last_repeat) {
  884. $advance{$myconfig->{dbdriver}} = "NULL";
  885. }
  886. $query = qq|
  887. UPDATE recurring
  888. SET nextdate = (date $nextdate + interval $interval)
  889. WHERE id = $id|;
  890. $dbh->do($query) || $form->dberror($query);
  891. $dbh->commit;
  892. }
  893. sub check_template_name {
  894. my ($self, $myconfig, $form) = @_;
  895. my @allowedsuff = qw(css tex txt html xml);
  896. if ($form->{file} =~ /^(.:)*?\/|\.\.\/|^\//){
  897. $form->error("Directory transversal not allowed.");
  898. }
  899. my ${LedgerSMB::Sysconfig::userspath} = ${main::userspath};
  900. if ($form->{file} =~ /^${LedgerSMB::Sysconfig::userspath}\//){
  901. $form->error("Not allowed to access ${LedgerSMB::Sysconfig::userspath}/ with this method");
  902. }
  903. my $whitelisted = 0;
  904. for (@allowedsuff){
  905. if ($form->{file} =~ /$_$/){
  906. $whitelisted = 1;
  907. }
  908. }
  909. if (!$whitelisted){
  910. $form->error("Error: File is of type that is not allowed.");
  911. }
  912. if ($form->{file} !~ /^$myconfig->{templates}\//){
  913. $form->error("Not in a whitelisted directory: $form->{file}") unless $form->{file} =~ /^css\//;
  914. }
  915. }
  916. sub load_template {
  917. my ($self, $myconfig, $form) = @_;
  918. $self->check_template_name(\%$myconfig, \%$form);
  919. open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
  920. while (<TEMPLATE>) {
  921. $form->{body} .= $_;
  922. }
  923. close(TEMPLATE);
  924. }
  925. sub save_template {
  926. my ($self, $myconfig, $form) = @_;
  927. $self->check_template_name(\%$myconfig, \%$form);
  928. open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
  929. # strip
  930. $form->{body} =~ s/\r//g;
  931. print TEMPLATE $form->{body};
  932. close(TEMPLATE);
  933. }
  934. sub save_preferences {
  935. my ($self, $myconfig, $form, ${LedgerSMB::Sysconfig::memberfile}, ${LedgerSMB::Sysconfig::userspath}) = @_;
  936. # connect to database
  937. my $dbh = $form->{dbh};
  938. my @queryargs;
  939. # update name
  940. my $query = qq|
  941. UPDATE employee
  942. SET name = ?,
  943. role = ?
  944. WHERE login = ?|;
  945. @queryargs = ($form->{name}, $form->{role}, $form->{login});
  946. $dbh->prepare($query)->execute(@queryargs) || $form->dberror($query);
  947. # get default currency
  948. $query = qq|SELECT curr, businessnumber
  949. FROM defaults|;
  950. ($form->{currency}, $form->{businessnumber}) =
  951. $dbh->selectrow_array($query);
  952. $form->{currency} =~ s/:.*//;
  953. $dbh->commit;
  954. my $myconfig = new User "${LedgerSMB::Sysconfig::memberfile}", "$form->{login}";
  955. foreach my $item (keys %$form) {
  956. $myconfig->{$item} = $form->{$item};
  957. }
  958. $myconfig->{password} = $form->{new_password} if ($form->{old_password} ne $form->{new_password});
  959. $myconfig->save_member(${LedgerSMB::Sysconfig::memberfile}, ${LedgerSMB::Sysconfig::userspath});
  960. 1;
  961. }
  962. sub save_defaults {
  963. my ($self, $myconfig, $form) = @_;
  964. for (qw(IC IC_income IC_expense FX_gain FX_loss)) { ($form->{$_}) = split /--/, $form->{$_} }
  965. my @a;
  966. $form->{curr} =~ s/ //g;
  967. for (split /:/, $form->{curr}) { push(@a, uc pack "A3", $_) if $_ }
  968. $form->{curr} = join ':', @a;
  969. # connect to database
  970. my $dbh = $form->{dbh};
  971. # save defaults
  972. my $query = qq|
  973. UPDATE defaults
  974. SET inventory_accno_id = (SELECT id
  975. FROM chart
  976. WHERE accno = ?),
  977. income_accno_id = (SELECT id
  978. FROM chart
  979. WHERE accno = ?),
  980. expense_accno_id = (SELECT id
  981. FROM chart
  982. WHERE accno = ?),
  983. fxgain_accno_id = (SELECT id
  984. FROM chart
  985. WHERE accno = ?),
  986. fxloss_accno_id = (SELECT id
  987. FROM chart
  988. WHERE accno = ?),
  989. glnumber = ?,
  990. sinumber = ?,
  991. vinumber = ?,
  992. sonumber = ?,
  993. ponumber = ?,
  994. sqnumber = ?,
  995. rfqnumber = ?,
  996. partnumber = ?,
  997. employeenumber = ?,
  998. customernumber = ?,
  999. vendornumber = ?,
  1000. projectnumber = ?,
  1001. yearend = ?,
  1002. curr = ?,
  1003. weightunit = ?,
  1004. businessnumber = ?|;
  1005. my @queryargs = (
  1006. $form->{IC}, $form->{IC_income}, $form->{IC_expense},
  1007. $form->{FX_gain}, $form->{FX_loss}, $form->{glnumber},
  1008. $form->{sinumber}, $form->{vinumber}, $form->{sonumber},
  1009. $form->{ponumber}, $form->{sqnumber}, $form->{rfqnumber},
  1010. $form->{partnumber}, $form->{employeenumber},
  1011. $form->{customernumber}, $form->{vendornumber},
  1012. $form->{projectnumber}, $form->{yearend}, $form->{curr},
  1013. $form->{weightunit}, $form->{businessnumber});
  1014. $dbh->prepare($query)->execute(@queryargs) || $form->dberror($query);
  1015. my $rc = $dbh->commit;
  1016. $rc;
  1017. }
  1018. sub defaultaccounts {
  1019. my ($self, $myconfig, $form) = @_;
  1020. # connect to database
  1021. my $dbh = $form->{dbh};
  1022. # get defaults from defaults table
  1023. my $query = qq|SELECT * FROM defaults|;
  1024. my $sth = $dbh->prepare($query);
  1025. $sth->execute || $form->dberror($query);
  1026. my $ref = $sth->fetchrow_hashref(NAME_lc);
  1027. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  1028. $form->{defaults}{IC} = $form->{inventory_accno_id};
  1029. $form->{defaults}{IC_income} = $form->{income_accno_id};
  1030. $form->{defaults}{IC_sale} = $form->{income_accno_id};
  1031. $form->{defaults}{IC_expense} = $form->{expense_accno_id};
  1032. $form->{defaults}{IC_cogs} = $form->{expense_accno_id};
  1033. $form->{defaults}{FX_gain} = $form->{fxgain_accno_id};
  1034. $form->{defaults}{FX_loss} = $form->{fxloss_accno_id};
  1035. $sth->finish;
  1036. $query = qq|
  1037. SELECT id, accno, description, link
  1038. FROM chart
  1039. WHERE link LIKE '%IC%'
  1040. ORDER BY accno|;
  1041. $sth = $dbh->prepare($query);
  1042. $sth->execute || $form->dberror($query);
  1043. my $nkey;
  1044. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1045. foreach my $key (split(/:/, $ref->{link})) {
  1046. if ($key =~ /IC/) {
  1047. $nkey = $key;
  1048. if ($key =~ /cogs/) {
  1049. $nkey = "IC_expense";
  1050. }
  1051. if ($key =~ /sale/) {
  1052. $nkey = "IC_income";
  1053. }
  1054. %{ $form->{accno}{$nkey}{$ref->{accno}} } = ( id => $ref->{id},
  1055. description => $ref->{description} );
  1056. }
  1057. }
  1058. }
  1059. $sth->finish;
  1060. $query = qq|
  1061. SELECT id, accno, description
  1062. FROM chart
  1063. WHERE (category = 'I' OR category = 'E')
  1064. AND charttype = 'A'
  1065. ORDER BY accno|;
  1066. $sth = $dbh->prepare($query);
  1067. $sth->execute || $form->dberror($query);
  1068. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1069. %{ $form->{accno}{FX_gain}{$ref->{accno}} } = ( id => $ref->{id},
  1070. description => $ref->{description} );
  1071. %{ $form->{accno}{FX_loss}{$ref->{accno}} } = ( id => $ref->{id},
  1072. description => $ref->{description} );
  1073. }
  1074. $sth->finish;
  1075. $dbh->commit;
  1076. }
  1077. sub taxes {
  1078. my ($self, $myconfig, $form) = @_;
  1079. my $taxaccounts = '';
  1080. # connect to database
  1081. my $dbh = $form->{dbh};
  1082. my $query = qq|
  1083. SELECT c.id, c.accno, c.description,
  1084. t.rate * 100 AS rate, t.taxnumber, t.validto,
  1085. t.pass, m.taxmodulename
  1086. FROM chart c
  1087. JOIN tax t ON (c.id = t.chart_id)
  1088. JOIN taxmodule m ON (t.taxmodule_id = m.taxmodule_id)
  1089. ORDER BY 3, 6|;
  1090. my $sth = $dbh->prepare($query);
  1091. $sth->execute || $form->dberror($query);
  1092. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1093. push @{ $form->{taxrates} }, $ref;
  1094. $taxaccounts .= " " . $ref{accno};
  1095. }
  1096. $sth->finish;
  1097. $query = qq|
  1098. SELECT taxmodule_id, taxmodulename FROM taxmodule
  1099. ORDER BY 2|;
  1100. $sth = $dbh->prepare($query);
  1101. $sth->execute || $form->dberror($query);
  1102. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1103. $form->{"taxmodule_".$ref->{taxmodule_id}} =
  1104. $ref->{taxmodulename};
  1105. }
  1106. $sth->finish;
  1107. $dbh->commit;
  1108. }
  1109. sub save_taxes {
  1110. my ($self, $myconfig, $form) = @_;
  1111. # connect to database
  1112. my $dbh = $form->{dbh};
  1113. my $query = qq|DELETE FROM tax|;
  1114. $dbh->do($query) || $form->dberror($query);
  1115. $query = qq|
  1116. INSERT INTO tax (chart_id, rate, taxnumber, validto,
  1117. pass, taxmodule_id)
  1118. VALUES (?, ?, ?, ?, ?, ?)|;
  1119. my $sth = $dbh->prepare($query);
  1120. foreach my $item (split / /, $form->{taxaccounts}) {
  1121. my ($chart_id, $i) = split /_/, $item;
  1122. my $rate = $form->parse_amount(
  1123. $myconfig, $form->{"taxrate_$i"}) / 100;
  1124. my $validto = $form->{"validto_$i"};
  1125. $validto = undef if not $validto;
  1126. my @queryargs = ($chart_id, $rate, $form->{"taxnumber_$i"},
  1127. $validto, $form->{"pass_$i"},
  1128. $form->{"taxmodule_id_$i"});
  1129. $sth->execute(@queryargs) || $form->dberror($query);
  1130. }
  1131. my $rc = $dbh->commit;
  1132. $rc;
  1133. }
  1134. sub backup {
  1135. my ($self, $myconfig, $form, ${LedgerSMB::Sysconfig::userspath}, ${LedgerSMB::Sysconfig::gzip}) = @_;
  1136. my $mail;
  1137. my $err;
  1138. my @t = localtime(time);
  1139. $t[4]++;
  1140. $t[5] += 1900;
  1141. $t[3] = substr("0$t[3]", -2);
  1142. $t[4] = substr("0$t[4]", -2);
  1143. my $boundary = time;
  1144. my $tmpfile = "${LedgerSMB::Sysconfig::userspath}/$boundary.$myconfig->{dbname}-$form->{dbversion}-$t[5]$t[4]$t[3].sql";
  1145. my $out = $form->{OUT};
  1146. $form->{OUT} = ">$tmpfile";
  1147. open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
  1148. # get sequences, functions and triggers
  1149. my $today = scalar localtime;
  1150. $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
  1151. $ENV{PGPASSWD} = $myconfig->{dbpasswd};
  1152. # drop tables and sequences
  1153. # compress backup if gzip defined
  1154. my $suffix = "";
  1155. if ($form->{media} eq 'email') {
  1156. if (${LedgerSMB::Sysconfig::gzip}){
  1157. print OUT `pg_dump -U $myconfig->{dbuser} -h $myconfig->{dbhost} $myconfig->{dbname} | ${LedgerSMB::Sysconfig::gzip}`;
  1158. } else {
  1159. print OUT `pg_dump -U $myconfig->{dbuser} -h $myconfig->{dbhost} $myconfig->{dbname}`;
  1160. }
  1161. close OUT;
  1162. use LedgerSMB::Mailer;
  1163. $mail = new Mailer;
  1164. $mail->{to} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
  1165. $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
  1166. $mail->{subject} = "LedgerSMB Backup / $myconfig->{dbname}-$form->{dbversion}-$t[5]$t[4]$t[3].sql$suffix";
  1167. @{ $mail->{attachments} } = ($tmpfile);
  1168. $mail->{version} = $form->{version};
  1169. $mail->{fileid} = "$boundary.";
  1170. $myconfig->{signature} =~ s/\\n/\n/g;
  1171. $mail->{message} = "-- \n$myconfig->{signature}";
  1172. $err = $mail->send($out);
  1173. }
  1174. if ($form->{media} eq 'file') {
  1175. open(IN, "$tmpfile") or $form->error("$tmpfile : $!");
  1176. open(OUT, ">-") or $form->error("STDOUT : $!");
  1177. print OUT qq|Content-Type: application/file;\n| .
  1178. qq|Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}-$t[5]$t[4]$t[3].sql$suffix"\n\n|;
  1179. if (${LedgerSMB::Sysconfig::gzip}){
  1180. print OUT `pg_dump -U $myconfig->{dbuser} -h $myconfig->{dbhost} $myconfig->{dbname} | ${LedgerSMB::Sysconfig::gzip}`;
  1181. } else {
  1182. print OUT `pg_dump -U $myconfig->{dbuser} -h $myconfig->{dbhost} $myconfig->{dbname}`;
  1183. }
  1184. }
  1185. unlink "$tmpfile";
  1186. }
  1187. sub closedto {
  1188. my ($self, $myconfig, $form) = @_;
  1189. my $dbh = $form->{dbh};
  1190. my $query = qq|
  1191. SELECT closedto, revtrans, audittrail
  1192. FROM defaults|;
  1193. ($form->{closedto}, $form->{revtrans}, $form->{audittrail}) = $dbh->selectrow_array($query);
  1194. $dbh->commit;
  1195. }
  1196. sub closebooks {
  1197. my ($self, $myconfig, $form) = @_;
  1198. my $dbh = $form->{dbh};
  1199. my $query = qq|UPDATE defaults SET|;
  1200. if ($form->{revtrans}) {
  1201. $query .= qq| revtrans = '1'|;
  1202. } else {
  1203. $query .= qq| revtrans = '0'|;
  1204. }
  1205. $query .= qq|, closedto = |.$dbh->quote($form->{closedto});
  1206. if ($form->{audittrail}) {
  1207. $query .= qq|, audittrail = '1'|;
  1208. } else {
  1209. $query .= qq|, audittrail = '0'|;
  1210. }
  1211. # set close in defaults
  1212. $dbh->do($query) || $form->dberror($query);
  1213. if ($form->{removeaudittrail}) {
  1214. $query = qq|
  1215. DELETE FROM audittrail
  1216. WHERE transdate < | .
  1217. $dbh->quote($form->{removeaudittrail});
  1218. $dbh->do($query) || $form->dberror($query);
  1219. }
  1220. $dbh->commit;
  1221. }
  1222. sub earningsaccounts {
  1223. my ($self, $myconfig, $form) = @_;
  1224. my ($query, $sth, $ref);
  1225. # connect to database
  1226. my $dbh = $form->{dbh};
  1227. # get chart of accounts
  1228. $query = qq|
  1229. SELECT accno,description
  1230. FROM chart
  1231. WHERE charttype = 'A'
  1232. AND category = 'Q'
  1233. ORDER BY accno|;
  1234. $sth = $dbh->prepare($query);
  1235. $sth->execute || $form->dberror($query);
  1236. $form->{chart} = "";
  1237. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1238. push @{ $form->{chart} }, $ref;
  1239. }
  1240. $sth->finish;
  1241. $dbh->commit;
  1242. }
  1243. sub post_yearend {
  1244. my ($self, $myconfig, $form) = @_;
  1245. # connect to database, turn off AutoCommit
  1246. my $dbh = $form->{dbh};
  1247. my $query;
  1248. my @queryargs;
  1249. my $uid = localtime;
  1250. $uid .= "$$";
  1251. $query = qq|
  1252. INSERT INTO gl (reference, employee_id)
  1253. VALUES (?, (SELECT id FROM employee
  1254. WHERE login = ?))|;
  1255. $dbh->prepare($query)->execute($uid, $form->{login})
  1256. || $form->dberror($query);
  1257. $query = qq|
  1258. SELECT id
  1259. FROM gl
  1260. WHERE reference = ?|;
  1261. ($form->{id}) = $dbh->prepare($query)->fetchrow_array($uid);
  1262. $query = qq|
  1263. UPDATE gl
  1264. SET reference = ?,
  1265. description = ?,
  1266. notes = ?,
  1267. transdate = ?,
  1268. department_id = 0
  1269. WHERE id = ?|;
  1270. @queryargs = ($form->{reference}, $form->{description}, $form->{notes},
  1271. $form->{transdate}, $form->{id});
  1272. $dbh->prepare($query)->execute(@queryargs) || $form->dberror($query);
  1273. my $amount;
  1274. my $accno;
  1275. $query = qq|
  1276. INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
  1277. source)
  1278. VALUES (?, (SELECT id
  1279. FROM chart
  1280. WHERE accno = ?),
  1281. ?, ?, ?)|;
  1282. # insert acc_trans transactions
  1283. for my $i (1 .. $form->{rowcount}) {
  1284. # extract accno
  1285. ($accno) = split(/--/, $form->{"accno_$i"});
  1286. $amount = 0;
  1287. if ($form->{"credit_$i"}) {
  1288. $amount = $form->{"credit_$i"};
  1289. }
  1290. if ($form->{"debit_$i"}) {
  1291. $amount = $form->{"debit_$i"} * -1;
  1292. }
  1293. # if there is an amount, add the record
  1294. if ($amount) {
  1295. my @args = ($form->{id}, $accno, $amount,
  1296. $form->{transdate}, $form->{reference});
  1297. $dbh->prepare($query)->execute(@args)
  1298. || $form->dberror($query);
  1299. }
  1300. }
  1301. $query = qq|
  1302. INSERT INTO yearend (trans_id, transdate)
  1303. VALUES (?, ?)|;
  1304. $dbh->prepare($query)->execute($form->{id}, $form->{transdate})
  1305. || $form->dberror($query);
  1306. my %audittrail = (
  1307. tablename => 'gl',
  1308. reference => $form->{reference},
  1309. formname => 'yearend',
  1310. action => 'posted',
  1311. id => $form->{id} );
  1312. $form->audittrail($dbh, "", \%audittrail);
  1313. # commit and redirect
  1314. my $rc = $dbh->commit;
  1315. $rc;
  1316. }
  1317. 1;