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