summaryrefslogtreecommitdiff
path: root/LedgerSMB/IR.pm
blob: 3f3b0aeda5d2533d63de3af6eecebda959239a11 (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. # Inventory received module
  31. #
  32. #======================================================================
  33. package IR;
  34. use LedgerSMB::Tax;
  35. use LedgerSMB::PriceMatrix;
  36. use LedgerSMB::Sysconfig;
  37. use Math::BigFloat;
  38. sub post_invoice {
  39. my ( $self, $myconfig, $form ) = @_;
  40. my $dbh = $form->{dbh};
  41. $form->{invnumber} = $form->update_defaults( $myconfig, "vinumber", $dbh )
  42. unless $form->{invnumber};
  43. for ( 1 .. $form->{rowcount} ) {
  44. unless ( $form->{"deliverydate_$_"} ) {
  45. $form->{"deliverydate_$_"} = $form->{transdate};
  46. }
  47. }
  48. my $query;
  49. my $sth;
  50. my $ref;
  51. my $null;
  52. my $project_id;
  53. my $exchangerate = 0;
  54. my $allocated;
  55. my $taxrate;
  56. my $taxamount;
  57. my $diff = 0;
  58. my $item;
  59. my $invoice_id;
  60. my $keepcleared;
  61. ( $null, $form->{employee_id} ) = split /--/, $form->{employee};
  62. unless ( $form->{employee_id} ) {
  63. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  64. }
  65. ( $null, $form->{department_id} ) = split( /--/, $form->{department} );
  66. $form->{department_id} *= 1;
  67. $query = qq|
  68. SELECT (SELECT value FROM defaults
  69. WHERE setting_key = 'fxgain_accno_id')
  70. AS fxgain_accno_id,
  71. (SELECT value FROM defaults
  72. WHERE setting_key = 'fxloss_accno_id')
  73. AS fxloss_accno_id|;
  74. my ( $fxgain_accno_id, $fxloss_accno_id ) = $dbh->selectrow_array($query);
  75. $query = qq|
  76. SELECT inventory_accno_id, income_accno_id, expense_accno_id
  77. FROM parts
  78. WHERE id = ?|;
  79. my $pth = $dbh->prepare($query) || $form->dberror($query);
  80. my %updparts = ();
  81. if ( $form->{id} ) {
  82. my $sth;
  83. $keepcleared = 1;
  84. $query = qq|SELECT id FROM ap WHERE id = ?|;
  85. $sth = $dbh->prepare($query);
  86. $sth->execute( $form->{id} );
  87. if ( $sth->fetchrow_array ) {
  88. $query = qq|
  89. SELECT p.id, p.inventory_accno_id,
  90. p.income_accno_id
  91. FROM invoice i
  92. JOIN parts p ON (p.id = i.parts_id)
  93. WHERE i.trans_id = ?|;
  94. $sth = $dbh->prepare($query);
  95. $sth->execute( $form->{id} ) || $form->dberror($query);
  96. while ( $ref = $sth->fetchrow_hashref ) {
  97. if ( $ref->{inventory_accno_id}
  98. && $ref->{income_accno_id} )
  99. {
  100. $updparts{ $ref->{id} } = 1;
  101. }
  102. }
  103. $sth->finish;
  104. &reverse_invoice( $dbh, $form );
  105. }
  106. else {
  107. $query = qq|INSERT INTO ap (id, vendor_id) VALUES (?, ?)|;
  108. $sth = $dbh->prepare($query);
  109. $sth->execute( $form->{id}, $form->{vendor_id} )
  110. || $form->dberror($query);
  111. }
  112. }
  113. my $uid = localtime;
  114. $uid .= "$$";
  115. if ( !$form->{id} ) {
  116. $query = qq|
  117. INSERT INTO ap (invnumber, vendor_id, employee_id)
  118. VALUES ('$uid', ?, (SELECT id FROM employee
  119. WHERE login = ?))|;
  120. $sth = $dbh->prepare($query);
  121. $sth->execute( $form->{vendor_id}, $form->{login})
  122. || $form->dberror($query);
  123. $query = qq|SELECT id FROM ap WHERE invnumber = '$uid'|;
  124. $sth = $dbh->prepare($query);
  125. $sth->execute || $form->dberror($query);
  126. ( $form->{id} ) = $sth->fetchrow_array;
  127. $sth->finish;
  128. }
  129. my $amount;
  130. my $grossamount;
  131. my $allocated;
  132. my $invamount = 0;
  133. my $invnetamount = 0;
  134. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  135. $form->{exchangerate} = 1;
  136. }
  137. else {
  138. $exchangerate =
  139. $form->check_exchangerate( $myconfig, $form->{currency},
  140. $form->{transdate}, 'sell' );
  141. }
  142. $form->{exchangerate} =
  143. ($exchangerate)
  144. ? $exchangerate
  145. : $form->parse_amount( $myconfig, $form->{exchangerate} );
  146. for my $i ( 1 .. $form->{rowcount} ) {
  147. $form->{"qty_$i"} = $form->parse_amount( $myconfig, $form->{"qty_$i"} );
  148. if ( $form->{"qty_$i"} ) {
  149. $pth->execute( $form->{"id_$i"} );
  150. $ref = $pth->fetchrow_hashref(NAME_lc);
  151. for ( keys %$ref ) {
  152. $form->{"${_}_$i"} = $ref->{$_};
  153. }
  154. $pth->finish;
  155. # project
  156. if ( $form->{"projectnumber_$i"} ne "" ) {
  157. ( $null, $project_id ) =
  158. split /--/, $form->{"projectnumber_$i"};
  159. }
  160. # undo discount formatting
  161. $form->{"discount_$i"} =
  162. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) / 100;
  163. # keep entered selling price
  164. my $fxsellprice =
  165. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  166. my ($dec) = ( $fxsellprice =~ /\.(\d+)/ );
  167. $dec = length $dec;
  168. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  169. # deduct discount
  170. $form->{"sellprice_$i"} = $fxsellprice -
  171. $form->round_amount( $fxsellprice * $form->{"discount_$i"},
  172. $decimalplaces );
  173. # linetotal
  174. my $fxlinetotal =
  175. $form->round_amount( $form->{"sellprice_$i"} * $form->{"qty_$i"},
  176. 2 );
  177. $amount = $fxlinetotal * $form->{exchangerate};
  178. my $linetotal = $form->round_amount( $amount, 2 );
  179. $fxdiff += $amount - $linetotal;
  180. @taxaccounts = Tax::init_taxes(
  181. $form,
  182. $form->{"taxaccounts_$i"},
  183. $form->{'taxaccounts'}
  184. );
  185. $tax = Math::BigFloat->bzero();
  186. $fxtax = Math::BigFloat->bzero();
  187. if ( $form->{taxincluded} ) {
  188. $tax += $amount =
  189. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  190. $form->{"sellprice_$i"} -= $amount / $form->{"qty_$i"};
  191. }
  192. else {
  193. $tax += $amount =
  194. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  195. $fxtax +=
  196. Tax::calculate_taxes( \@taxaccounts, $form, $fxlinetotal, 0 );
  197. }
  198. for (@taxaccounts) {
  199. $form->{acc_trans}{ $form->{id} }{ $_->account }{amount} +=
  200. $_->value;
  201. }
  202. $grossamount = $form->round_amount( $linetotal, 2 );
  203. if ( $form->{taxincluded} ) {
  204. $amount = $form->round_amount( $tax, 2 );
  205. $linetotal -= $form->round_amount( $tax - $diff, 2 );
  206. $diff = ( $amount - $tax );
  207. }
  208. $amount = $form->round_amount( $linetotal, 2 );
  209. $allocated = 0;
  210. # adjust and round sellprice
  211. $form->{"sellprice_$i"} =
  212. $form->round_amount(
  213. $form->{"sellprice_$i"} * $form->{exchangerate},
  214. $decimalplaces );
  215. # save detail record in invoice table
  216. $query = qq|
  217. INSERT INTO invoice (description)
  218. VALUES ('$uid')|;
  219. $dbh->do($query) || $form->dberror($query);
  220. $query = qq|
  221. SELECT id FROM invoice
  222. WHERE description = '$uid'|;
  223. ($invoice_id) = $dbh->selectrow_array($query);
  224. if ( $form->{"inventory_accno_id_$i"} ) {
  225. my $totalqty = $form->{"qty_$i"};
  226. # if this is a exit for the product
  227. if($form->{"qty_$i"}<0) {
  228. $query = qq|
  229. SELECT i.id, i.qty, i.allocated, a.transdate
  230. FROM invoice i
  231. JOIN parts p ON (p.id = i.parts_id)
  232. JOIN ap a ON (a.id = i.trans_id)
  233. WHERE i.parts_id = ? AND (i.qty + i.allocated) < 0 AND i.sellprice = ?
  234. ORDER BY transdate
  235. |;
  236. $sth = $dbh->prepare($query);
  237. $sth->execute( $form->{"id_$i"}, $form->{"sellprice_$i"}) || $form->dberror($query);
  238. my $totalqty = $form->{"qty_$i"};
  239. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  240. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  241. my $qty = $ref->{qty} + $ref->{allocated};
  242. if ( ( $qty - $totalqty ) < 0 ) { $qty = $totalqty; }
  243. # update allocated for sold item
  244. $form->update_balance( $dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1 );
  245. $allocated += $qty;
  246. last if ( ( $totalqty -= $qty ) >= 0 );
  247. }
  248. } else {
  249. # check for unallocated entries atthe same price to match our entry
  250. $query = qq|
  251. SELECT i.id, i.qty, i.allocated, a.transdate
  252. FROM invoice i
  253. JOIN parts p ON (p.id = i.parts_id)
  254. JOIN ap a ON (a.id = i.trans_id)
  255. WHERE i.parts_id = ? AND (i.qty + i.allocated) > 0 AND i.sellprice = ?
  256. ORDER BY transdate|;
  257. $sth = $dbh->prepare($query);
  258. $sth->execute( $form->{"id_$i"}, $form->{"sellprice_$i"}) || $form->dberror($query);
  259. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  260. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  261. my $qty = $ref->{qty} + $ref->{allocated};
  262. if ( ( $qty - $totalqty ) > 0 ) { $qty = $totalqty; }
  263. # update allocated for sold item
  264. $form->update_balance( $dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1 );
  265. $allocated += $qty;
  266. last if ( ( $totalqty -= $qty ) <= 0 );
  267. }
  268. }
  269. # add purchase to inventory
  270. push @{ $form->{acc_trans}{lineitems} },
  271. {
  272. chart_id => $form->{"inventory_accno_id_$i"},
  273. amount => $amount,
  274. fxgrossamount => $fxlinetotal +
  275. $form->round_amount( $fxtax, 2 ),
  276. grossamount => $grossamount,
  277. project_id => $project_id,
  278. invoice_id => $invoice_id
  279. };
  280. $updparts{ $form->{"id_$i"} } = 1;
  281. # update parts table
  282. $form->update_balance( $dbh, "parts", "onhand",
  283. qq|id = $form->{"id_$i"}|,
  284. $form->{"qty_$i"} )
  285. unless $form->{shipped};
  286. # check if we sold the item
  287. $query = qq|
  288. SELECT i.id, i.qty, i.allocated,
  289. i.trans_id, i.project_id,
  290. p.inventory_accno_id,
  291. p.expense_accno_id, a.transdate
  292. FROM invoice i
  293. JOIN parts p ON (p.id = i.parts_id)
  294. JOIN ar a ON (a.id = i.trans_id)
  295. WHERE i.parts_id = ?
  296. AND (i.qty + i.allocated) > 0
  297. ORDER BY transdate|;
  298. my $sth = $dbh->prepare($query);
  299. $sth->execute( $form->{"id_$i"} )
  300. || $form->dberror($query);
  301. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  302. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  303. my $qty = $ref->{qty} + $ref->{allocated};
  304. if ( ( $qty - $totalqty ) > 0 ) {
  305. $qty = $totalqty;
  306. }
  307. $linetotal =
  308. $form->round_amount( $form->{"sellprice_$i"} * $qty, 2 );
  309. if ($linetotal) {
  310. $query = qq|
  311. INSERT INTO acc_trans
  312. (trans_id,
  313. chart_id,
  314. amount,
  315. transdate,
  316. project_id,
  317. invoice_id)
  318. VALUES (?, ?, ?, ?,
  319. ?, ?)|;
  320. my $sth = $dbh->prepare($query);
  321. $sth->execute(
  322. $ref->{trans_id}, $ref->{inventory_accno_id},
  323. $linetotal, $ref->{transdate},
  324. $ref->{project_id}, $invoice_id
  325. ) || $form->dberror($query);
  326. # add expense
  327. $query = qq|
  328. INSERT INTO acc_trans
  329. (trans_id,
  330. chart_id,
  331. amount,
  332. transdate,
  333. project_id,
  334. invoice_id)
  335. VALUES (?, ?, ?, ?,
  336. ?, ?)|;
  337. $sth = $dbh->prepare($query);
  338. $sth->execute(
  339. $ref->{trans_id}, $ref->{expense_accno_id},
  340. $linetotal * -1, $ref->{transdate},
  341. $ref->{project_id}, $invoice_id
  342. ) || $form->dberror($query);
  343. }
  344. # update allocated for sold item
  345. $form->update_balance( $dbh, "invoice", "allocated",
  346. qq|id = $ref->{id}|,
  347. $qty * -1 );
  348. $allocated += $qty;
  349. last if ( ( $totalqty -= $qty ) <= 0 );
  350. }
  351. $sth->finish;
  352. }
  353. else {
  354. # add purchase to expense
  355. push @{ $form->{acc_trans}{lineitems} },
  356. {
  357. chart_id => $form->{"expense_accno_id_$i"},
  358. amount => $amount,
  359. fxgrossamount => $fxlinetotal +
  360. $form->round_amount( $fxtax, 2 ),
  361. grossamount => $grossamount,
  362. project_id => $project_id,
  363. invoice_id => $invoice_id
  364. };
  365. }
  366. $query = qq|
  367. UPDATE invoice
  368. SET trans_id = ?,
  369. parts_id = ?,
  370. description = ?,
  371. qty = ?,
  372. sellprice = ?,
  373. fxsellprice = ?,
  374. discount = ?,
  375. allocated = ?,
  376. unit = ?,
  377. deliverydate = ?,
  378. project_id = ?,
  379. serialnumber = ?,
  380. notes = ?
  381. WHERE id = ?|;
  382. $sth = $dbh->prepare($query);
  383. $sth->execute(
  384. $form->{id}, $form->{"id_$i"},
  385. $form->{"description_$i"}, $form->{"qty_$i"} * -1,
  386. $form->{"sellprice_$i"}, $fxsellprice,
  387. $form->{"discount_$i"}, $allocated,
  388. $form->{"unit_$i"}, $form->{"deliverydate_$i"},
  389. $project_id, $form->{"serialnumber_$i"},
  390. $form->{"notes_$i"}, $invoice_id
  391. ) || $form->dberror($query);
  392. }
  393. }
  394. $form->{paid} = 0;
  395. for $i ( 1 .. $form->{paidaccounts} ) {
  396. $form->{"paid_$i"} =
  397. $form->parse_amount( $myconfig, $form->{"paid_$i"} );
  398. $form->{paid} += $form->{"paid_$i"};
  399. $form->{datepaid} = $form->{"datepaid_$i"}
  400. if ( $form->{"datepaid_$i"} );
  401. }
  402. # add lineitems + tax
  403. $amount = 0;
  404. $grossamount = 0;
  405. $fxgrossamount = 0;
  406. for ( @{ $form->{acc_trans}{lineitems} } ) {
  407. $amount += $_->{amount};
  408. $grossamount += $_->{grossamount};
  409. $fxgrossamount += $_->{fxgrossamount};
  410. }
  411. $invnetamount = $amount;
  412. $amount = 0;
  413. for ( split / /, $form->{taxaccounts} ) {
  414. $amount += $form->{acc_trans}{ $form->{id} }{$_}{amount} =
  415. $form->round_amount( $form->{acc_trans}{ $form->{id} }{$_}{amount},
  416. 2 );
  417. $form->{acc_trans}{ $form->{id} }{$_}{amount} *= -1;
  418. }
  419. $invamount = $invnetamount + $amount;
  420. $diff = 0;
  421. if ( $form->{taxincluded} ) {
  422. $diff = $form->round_amount( $grossamount - $invamount, 2 );
  423. $invamount += $diff;
  424. }
  425. $fxdiff = $form->round_amount( $fxdiff, 2 );
  426. $invnetamount += $fxdiff;
  427. $invamount += $fxdiff;
  428. if ( $form->round_amount( $form->{paid} - $fxgrossamount, 2 ) == 0 ) {
  429. $form->{paid} = $invamount;
  430. }
  431. else {
  432. $form->{paid} =
  433. $form->round_amount( $form->{paid} * $form->{exchangerate}, 2 );
  434. }
  435. foreach $ref ( sort { $b->{amount} <=> $a->{amount} }
  436. @{ $form->{acc_trans}{lineitems} } )
  437. {
  438. $amount = $ref->{amount} + $diff + $fxdiff;
  439. $query = qq|
  440. INSERT INTO acc_trans (trans_id, chart_id, amount,
  441. transdate, project_id, invoice_id)
  442. VALUES (?, ?, ?, ?, ?, ?)|;
  443. $sth = $dbh->prepare($query);
  444. $sth->execute(
  445. $form->{id}, $ref->{chart_id}, $amount * -1,
  446. $form->{transdate}, $ref->{project_id}, $ref->{invoice_id}
  447. ) || $form->dberror($query);
  448. $diff = 0;
  449. $fxdiff = 0;
  450. }
  451. $form->{payables} = $invamount;
  452. delete $form->{acc_trans}{lineitems};
  453. # update exchangerate
  454. if ( ( $form->{currency} ne $form->{defaultcurrency} ) && !$exchangerate ) {
  455. $form->update_exchangerate( $dbh, $form->{currency}, $form->{transdate},
  456. 0, $form->{exchangerate} );
  457. }
  458. # record payable
  459. if ( $form->{payables} ) {
  460. ($accno) = split /--/, $form->{AP};
  461. $query = qq|
  462. INSERT INTO acc_trans (trans_id, chart_id, amount,
  463. transdate)
  464. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  465. ?, ?)|;
  466. $sth = $dbh->prepare($query);
  467. $sth->execute( $form->{id}, $accno, $form->{payables},
  468. $form->{transdate} )
  469. || $form->dberror($query);
  470. }
  471. foreach my $trans_id ( keys %{ $form->{acc_trans} } ) {
  472. foreach my $accno ( keys %{ $form->{acc_trans}{$trans_id} } ) {
  473. $amount =
  474. $form->round_amount(
  475. $form->{acc_trans}{$trans_id}{$accno}{amount}, 2 );
  476. if ($amount) {
  477. $query = qq|
  478. INSERT INTO acc_trans
  479. (trans_id, chart_id, amount,
  480. transdate)
  481. VALUES (?, (SELECT id FROM chart
  482. WHERE accno = ?),
  483. ?, ?)|;
  484. $sth = $dbh->prepare($query);
  485. $sth->execute( $trans_id, $accno, $amount, $form->{transdate} )
  486. || $form->dberror($query);
  487. }
  488. }
  489. }
  490. # if there is no amount but a payment record payable
  491. if ( $invamount == 0 ) {
  492. $form->{payables} = 1;
  493. }
  494. my $cleared = 0;
  495. # record payments and offsetting AP
  496. for my $i ( 1 .. $form->{paidaccounts} ) {
  497. if ( $form->{"paid_$i"} ) {
  498. my ($accno) = split /--/, $form->{"AP_paid_$i"};
  499. $form->{"datepaid_$i"} = $form->{transdate}
  500. unless ( $form->{"datepaid_$i"} );
  501. $form->{datepaid} = $form->{"datepaid_$i"};
  502. $exchangerate = 0;
  503. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  504. $form->{"exchangerate_$i"} = 1;
  505. }
  506. else {
  507. $exchangerate =
  508. $form->check_exchangerate( $myconfig, $form->{currency},
  509. $form->{"datepaid_$i"}, 'sell' );
  510. $form->{"exchangerate_$i"} =
  511. ($exchangerate)
  512. ? $exchangerate
  513. : $form->parse_amount( $myconfig,
  514. $form->{"exchangerate_$i"} );
  515. }
  516. # record AP
  517. $amount = (
  518. $form->round_amount(
  519. $form->{"paid_$i"} * $form->{exchangerate}, 2
  520. )
  521. ) * -1;
  522. if ( $form->{payables} ) {
  523. $query = qq|
  524. INSERT INTO acc_trans
  525. (trans_id, chart_id, amount,
  526. transdate)
  527. VALUES (?, (SELECT id FROM chart
  528. WHERE accno = ?),
  529. ?, ?)|;
  530. $sth = $dbh->prepare($query);
  531. $sth->execute( $form->{id}, $form->{AP}, $amount,
  532. $form->{"datepaid_$i"} )
  533. || $form->dberror($query);
  534. }
  535. if ($keepcleared) {
  536. $cleared = ( $form->{"cleared_$i"} ) ? 1 : 0;
  537. }
  538. # record payment
  539. $query = qq|
  540. INSERT INTO acc_trans
  541. (trans_id, chart_id, amount,
  542. transdate, source, memo, cleared)
  543. VALUES (?, (SELECT id FROM chart
  544. WHERE accno = ?),
  545. ?, ?, ?, ?, ?)|;
  546. $sth = $dbh->prepare($query);
  547. $sth->execute( $form->{id}, $accno, $form->{"paid_$i"},
  548. $form->{"datepaid_$i"},
  549. $form->{"source_$i"}, $form->{"memo_$i"}, $cleared )
  550. || $form->dberror($query);
  551. # exchangerate difference
  552. $amount = $form->round_amount(
  553. $form->{"paid_$i"} * $form->{"exchangerate_$i"} -
  554. $form->{"paid_$i"},
  555. 2
  556. );
  557. if ($amount) {
  558. $query = qq|
  559. INSERT INTO acc_trans
  560. (trans_id, chart_id, amount,
  561. transdate, source,
  562. fx_transaction, cleared)
  563. VALUES (?, (SELECT id FROM chart
  564. WHERE accno = ?),
  565. ?, ?, ?, '1', ?)|;
  566. $sth = $dbh->prepare($query);
  567. $sth->execute( $form->{id}, $accno, $amount,
  568. $form->{"datepaid_$i"},
  569. $form->{"source_$i"}, $cleared )
  570. || $form->dberror($query);
  571. }
  572. # gain/loss
  573. $amount = $form->round_amount(
  574. $form->round_amount( $form->{"paid_$i"} * $form->{exchangerate},
  575. 2 ) - $form->round_amount(
  576. $form->{"paid_$i"} * $form->{"exchangerate_$i"}, 2
  577. ),
  578. 2
  579. );
  580. if ($amount) {
  581. my $accno_id =
  582. ( $amount > 0 )
  583. ? $fxgain_accno_id
  584. : $fxloss_accno_id;
  585. $query = qq|
  586. INSERT INTO acc_trans
  587. (trans_id, chart_id, amount,
  588. transdate, fx_transaction,
  589. cleared)
  590. VALUES (?, ?, ?, ?, '1', ?)|;
  591. $sth = $dbh->prepare($query);
  592. $sth->execute( $form->{id}, $accno_id, $amount,
  593. $form->{"datepaid_$i"}, $cleared )
  594. || $form->dberror($query);
  595. }
  596. # update exchange rate
  597. if ( ( $form->{currency} ne $form->{defaultcurrency} )
  598. && !$exchangerate )
  599. {
  600. $form->update_exchangerate( $dbh, $form->{currency},
  601. $form->{"datepaid_$i"},
  602. 0, $form->{"exchangerate_$i"} );
  603. }
  604. }
  605. }
  606. # set values which could be empty
  607. $form->{taxincluded} *= 1;
  608. # save AP record
  609. $query = qq|
  610. UPDATE ap
  611. SET invnumber = ?,
  612. ordnumber = ?,
  613. quonumber = ?,
  614. transdate = ?,
  615. vendor_id = ?,
  616. amount = ?,
  617. netamount = ?,
  618. paid = ?,
  619. datepaid = ?,
  620. duedate = ?,
  621. invoice = '1',
  622. shippingpoint = ?,
  623. shipvia = ?,
  624. taxincluded = ?,
  625. notes = ?,
  626. intnotes = ?,
  627. curr = ?,
  628. department_id = ?,
  629. employee_id = ?,
  630. language_code = ?,
  631. ponumber = ?
  632. WHERE id = ?|;
  633. $sth = $dbh->prepare($query);
  634. $sth->execute(
  635. $form->{invnumber}, $form->{ordnumber}, $form->{quonumber},
  636. $form->{transdate}, $form->{vendor_id}, $invamount,
  637. $invnetamount, $form->{paid}, $form->{datepaid},
  638. $form->{duedate}, $form->{shippingpoint}, $form->{shipvia},
  639. $form->{taxincluded}, $form->{notes}, $form->{intnotes},
  640. $form->{currency}, $form->{department_id}, $form->{employee_id},
  641. $form->{language_code}, $form->{ponumber}, $form->{id}
  642. ) || $form->dberror($query);
  643. # add shipto
  644. $form->{name} = $form->{vendor};
  645. $form->{name} =~ s/--$form->{vendor_id}//;
  646. $form->add_shipto( $dbh, $form->{id} );
  647. my %audittrail = (
  648. tablename => 'ap',
  649. reference => $form->{invnumber},
  650. formname => $form->{type},
  651. action => 'posted',
  652. id => $form->{id}
  653. );
  654. $form->audittrail( $dbh, "", \%audittrail );
  655. foreach $item ( keys %updparts ) {
  656. $item = $dbh->quote($item);
  657. $query = qq|
  658. UPDATE parts
  659. SET avgcost = avgcost($item),
  660. lastcost = lastcost($item)
  661. WHERE id = $item|;
  662. $dbh->prepare($query) || $form->dberror($query);
  663. }
  664. my $rc = $dbh->commit;
  665. $rc;
  666. }
  667. sub reverse_invoice {
  668. my ( $dbh, $form ) = @_;
  669. my $query = qq|SELECT id FROM ap
  670. WHERE id = $form->{id}|;
  671. my ($id) = $dbh->selectrow_array($query);
  672. return unless $id;
  673. # reverse inventory items
  674. $query = qq|
  675. SELECT i.parts_id, p.inventory_accno_id, p.expense_accno_id,
  676. i.qty, i.allocated, i.sellprice, i.project_id
  677. FROM invoice i, parts p
  678. WHERE i.parts_id = p.id
  679. AND i.trans_id = ?|;
  680. my $sth = $dbh->prepare($query);
  681. $sth->execute( $form->{id} ) || $form->dberror($query);
  682. my $netamount = 0;
  683. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  684. $netamount +=
  685. $form->round_amount( $ref->{sellprice} * $ref->{qty} * -1, 2 );
  686. if ( $ref->{inventory_accno_id} ) {
  687. # update onhand
  688. $form->update_balance( $dbh, "parts", "onhand",
  689. qq|id = $ref->{parts_id}|,
  690. $ref->{qty} );
  691. # if $ref->{allocated} > 0 than we sold that many items
  692. if ( $ref->{allocated} > 0 ) {
  693. # get references for sold items
  694. $query = qq|
  695. SELECT i.id, i.trans_id, i.allocated,
  696. a.transdate
  697. FROM invoice i, ar a
  698. WHERE i.parts_id = ?
  699. AND i.allocated < 0
  700. AND i.trans_id = a.id
  701. ORDER BY transdate DESC|;
  702. my $sth = $dbh->prepare($query);
  703. $sth->execute( $ref->{parts_id} )
  704. || $form->dberror($query);
  705. while ( my $pthref = $sth->fetchrow_hashref(NAME_lc) ) {
  706. my $qty = $ref->{allocated};
  707. if ( ( $ref->{allocated} + $pthref->{allocated} ) > 0 ) {
  708. $qty = $pthref->{allocated} * -1;
  709. }
  710. my $amount =
  711. $form->round_amount( $ref->{sellprice} * $qty, 2 );
  712. #adjust allocated
  713. $form->update_balance( $dbh, "invoice", "allocated",
  714. qq|id = $pthref->{id}|, $qty );
  715. # add reversal for sale
  716. $ref->{project_id} *= 1;
  717. $query = qq|
  718. INSERT INTO acc_trans
  719. (trans_id,
  720. chart_id, amount,
  721. transdate,
  722. project_id)
  723. VALUES (?, ?, ?, ?, ?)|;
  724. my $sth = $dbh->prepare($query);
  725. $sth->execute( $pthref->{trans_id},
  726. $ref->{expense_accno_id},
  727. $amount, $form->{transdate}, $ref->{project_id} )
  728. || $form->dberror($query);
  729. $query = qq|
  730. INSERT INTO acc_trans
  731. (trans_id, chart_id,
  732. amount, transdate,
  733. project_id)
  734. VALUES (?, ?, ?, ?, ?)|;
  735. $sth = $dbh->do($query);
  736. $sth->execute(
  737. $pthref->{trans_id},
  738. $ref->{inventory_accno_id},
  739. $amount * -1,
  740. $form->{transdate}, $ref->{project_id}
  741. ) || $form->dberror($query);
  742. last if ( ( $ref->{allocated} -= $qty ) <= 0 );
  743. }
  744. $sth->finish;
  745. }
  746. }
  747. }
  748. $sth->finish;
  749. # delete acc_trans
  750. $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
  751. $dbh->prepare($query);
  752. $sth->execute( $form->{id} ) || $form->dberror($query);
  753. # delete invoice entries
  754. $query = qq|DELETE FROM invoice WHERE trans_id = ?|;
  755. $dbh->prepare($query);
  756. $sth->execute( $form->{id} ) || $form->dberror($query);
  757. $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
  758. $dbh->prepare($query);
  759. $sth->execute( $form->{id} ) || $form->dberror($query);
  760. $dbh->commit;
  761. }
  762. sub delete_invoice {
  763. my ( $self, $myconfig, $form ) = @_;
  764. # connect to database
  765. my $dbh = $form->{dbh};
  766. my %audittrail = (
  767. tablename => 'ap',
  768. reference => $form->{invnumber},
  769. formname => $form->{type},
  770. action => 'deleted',
  771. id => $form->{id}
  772. );
  773. $form->audittrail( $dbh, "", \%audittrail );
  774. my $query = qq|SELECT parts_id FROM invoice WHERE trans_id = ?|;
  775. my $sth = $dbh->prepare($query);
  776. $sth->execute( $form->{id} ) || $form->dberror($query);
  777. my $item;
  778. my %updparts = ();
  779. while ( ($item) = $sth->fetchrow_array ) {
  780. $updparts{$item} = 1;
  781. }
  782. $sth->finish;
  783. &reverse_invoice( $dbh, $form );
  784. # delete AP record
  785. $query = qq|DELETE FROM ap WHERE id = ?|;
  786. my $sth = $dbh->prepare($query);
  787. $sth->execute( $form->{id} ) || $form->dberror($query);
  788. # delete spool files
  789. $query = qq|
  790. SELECT spoolfile FROM status
  791. WHERE trans_id = ?
  792. AND spoolfile IS NOT NULL|;
  793. my $sth = $dbh->prepare($query);
  794. $sth->execute( $form->{id} ) || $form->dberror($query);
  795. my $spoolfile;
  796. my @spoolfiles = ();
  797. while ( ($spoolfile) = $sth->fetchrow_array ) {
  798. push @spoolfiles, $spoolfile;
  799. }
  800. $sth->finish;
  801. # delete status entries
  802. $query = qq|DELETE FROM status WHERE trans_id = ?|;
  803. my $sth = $dbh->prepare($query);
  804. $sth->execute( $form->{id} ) || $form->dberror($query);
  805. if ($rc) {
  806. foreach $item ( keys %updparts ) {
  807. $item = $dbh->quote($item);
  808. $query = qq|
  809. UPDATE parts
  810. SET avgcost = avgcost($item),
  811. lastcost = lastcost($item)
  812. WHERE id = $item|;
  813. $dbh->do($query) || $form->dberror($query);
  814. }
  815. foreach $spoolfile (@spoolfiles) {
  816. unlink "${LedgerSMB::Sysconfig::spool}/$spoolfile"
  817. if $spoolfile;
  818. }
  819. }
  820. my $rc = $dbh->commit;
  821. $rc;
  822. }
  823. sub retrieve_invoice {
  824. my ( $self, $myconfig, $form ) = @_;
  825. # connect to database
  826. my $dbh = $form->{dbh};
  827. my $query;
  828. if ( $form->{id} ) {
  829. # get default accounts and last invoice number
  830. $query = qq|
  831. SELECT (select c.accno FROM chart c
  832. WHERE c.id = (SELECT value FROM defaults
  833. WHERE setting_key =
  834. 'inventory_accno_id'))
  835. AS inventory_accno,
  836. (SELECT c.accno FROM chart c
  837. WHERE c.id = (SELECT value FROM defaults
  838. WHERE setting_key =
  839. 'income_accno_id'))
  840. AS income_accno,
  841. (SELECT c.accno FROM chart c
  842. WHERE c.id = (SELECT value FROM defaults
  843. WHERE setting_key =
  844. 'expense_accno_id'))
  845. AS expense_accno,
  846. (SELECT c.accno FROM chart c
  847. WHERE c.id = (SELECT value FROM defaults
  848. WHERE setting_key =
  849. 'fxgain_accno_id'))
  850. AS fxgain_accno,
  851. (SELECT c.accno FROM chart c
  852. WHERE c.id = (SELECT value FROM defaults
  853. WHERE setting_key =
  854. 'fxloss_accno_id'))
  855. AS fxloss_accno,
  856. (SELECT value FROM defaults
  857. WHERE setting_key = 'curr') AS currencies|;
  858. }
  859. else {
  860. $query = qq|
  861. SELECT (select c.accno FROM chart c
  862. WHERE c.id = (SELECT value FROM defaults
  863. WHERE setting_key =
  864. 'inventory_accno_id'))
  865. AS inventory_accno,
  866. (SELECT c.accno FROM chart c
  867. WHERE c.id = (SELECT value FROM defaults
  868. WHERE setting_key =
  869. 'income_accno_id'))
  870. AS income_accno,
  871. (SELECT c.accno FROM chart c
  872. WHERE c.id = (SELECT value FROM defaults
  873. WHERE setting_key =
  874. 'expense_accno_id'))
  875. AS expense_accno,
  876. (SELECT c.accno FROM chart c
  877. WHERE c.id = (SELECT value FROM defaults
  878. WHERE setting_key =
  879. 'fxgain_accno_id'))
  880. AS fxgain_accno,
  881. (SELECT c.accno FROM chart c
  882. WHERE c.id = (SELECT value FROM defaults
  883. WHERE setting_key =
  884. 'fxloss_accno_id'))
  885. AS fxloss_accno,
  886. (SELECT value FROM defaults
  887. WHERE setting_key = 'curr') AS currencies,
  888. current_date AS transdate|;
  889. }
  890. my $sth = $dbh->prepare($query);
  891. $sth->execute || $form->dberror($query);
  892. my $ref = $sth->fetchrow_hashref(NAME_lc);
  893. for ( keys %$ref ) {
  894. $form->{$_} = $ref->{$_};
  895. }
  896. $sth->finish;
  897. if ( $form->{id} ) {
  898. $query = qq|
  899. SELECT a.invnumber, a.transdate, a.duedate,
  900. a.ordnumber, a.quonumber, a.paid, a.taxincluded,
  901. a.notes, a.intnotes, a.curr AS currency,
  902. a.vendor_id, a.language_code, a.ponumber
  903. FROM ap a
  904. WHERE id = ?|;
  905. $sth = $dbh->prepare($query);
  906. $sth->execute( $form->{id} ) || $form->dberror($query);
  907. $ref = $sth->fetchrow_hashref(NAME_lc);
  908. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  909. for ( keys %$ref ) {
  910. $form->{$_} = $ref->{$_};
  911. }
  912. $sth->finish;
  913. # get shipto
  914. $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
  915. $sth = $dbh->prepare($query);
  916. $sth->execute( $form->{id} ) || $form->dberror($query);
  917. $ref = $sth->fetchrow_hashref(NAME_lc);
  918. for ( keys %$ref ) {
  919. $form->{$_} = $ref->{$_};
  920. }
  921. $sth->finish;
  922. # retrieve individual items
  923. $query = qq|
  924. SELECT p.partnumber, i.description, i.qty,
  925. i.fxsellprice, i.sellprice,
  926. i.parts_id AS id, i.unit, p.bin,
  927. i.deliverydate,
  928. pr.projectnumber, i.project_id,
  929. i.serialnumber,
  930. i.discount, i.notes, pg.partsgroup,
  931. p.partsgroup_id, p.partnumber AS sku,
  932. p.weight, p.onhand, p.inventory_accno_id,
  933. p.income_accno_id, p.expense_accno_id,
  934. t.description AS partsgrouptranslation
  935. FROM invoice i
  936. JOIN parts p ON (i.parts_id = p.id)
  937. LEFT JOIN project pr ON (i.project_id = pr.id)
  938. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  939. LEFT JOIN translation t
  940. ON (t.trans_id = p.partsgroup_id
  941. AND t.language_code = ?)
  942. WHERE i.trans_id = ?
  943. ORDER BY i.id|;
  944. $sth = $dbh->prepare($query);
  945. $sth->execute( $form->{language_code}, $form->{id} )
  946. || $form->dberror($query);
  947. # exchangerate defaults
  948. &exchangerate_defaults( $dbh, $form );
  949. # price matrix and vendor partnumber
  950. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  951. # tax rates for part
  952. $query = qq|
  953. SELECT c.accno
  954. FROM chart c
  955. JOIN partstax pt ON (pt.chart_id = c.id)
  956. WHERE pt.parts_id = ?|;
  957. my $tth = $dbh->prepare($query);
  958. my $ptref;
  959. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  960. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  961. my ($dec) = ( $ref->{fxsellprice} =~ /\.(\d+)/ );
  962. $dec = length $dec;
  963. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  964. $tth->execute( $ref->{id} );
  965. $ref->{taxaccounts} = "";
  966. my $taxrate = 0;
  967. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  968. $form->db_parse_numeric(sth => $tth, hashref => $ptref);
  969. $ref->{taxaccounts} .= "$ptref->{accno} ";
  970. $taxrate += $form->{"$ptref->{accno}_rate"};
  971. }
  972. $tth->finish;
  973. chop $ref->{taxaccounts};
  974. # price matrix
  975. $ref->{sellprice} =
  976. $form->round_amount(
  977. $ref->{fxsellprice} * $form->{ $form->{currency} },
  978. $decimalplaces );
  979. PriceMatrix::price_matrix( $pmh, $ref, $decimalplaces, $form,
  980. $myconfig );
  981. $ref->{sellprice} = $ref->{fxsellprice};
  982. $ref->{qty} *= -1;
  983. $ref->{partsgroup} = $ref->{partsgrouptranslation}
  984. if $ref->{partsgrouptranslation};
  985. push @{ $form->{invoice_details} }, $ref;
  986. }
  987. $sth->finish;
  988. }
  989. my $rc = $dbh->commit;
  990. $rc;
  991. }
  992. sub retrieve_item {
  993. my ( $self, $myconfig, $form ) = @_;
  994. $dbh = $form->{dbh};
  995. my $i = $form->{rowcount};
  996. my $null;
  997. my $var;
  998. # don't include assemblies or obsolete parts
  999. my $where = "WHERE p.assembly = '0' AND p.obsolete = '0'";
  1000. if ( $form->{"partnumber_$i"} ne "" ) {
  1001. $var = $dbh->quote( $form->like( lc $form->{"partnumber_$i"} ) );
  1002. $where .= " AND lower(p.partnumber) LIKE $var";
  1003. }
  1004. if ( $form->{"description_$i"} ne "" ) {
  1005. $var = $dbh->quote( $form->like( lc $form->{"description_$i"} ) );
  1006. if ( $form->{language_code} ne "" ) {
  1007. $where .= " AND lower(t1.description) LIKE $var";
  1008. }
  1009. else {
  1010. $where .= " AND lower(p.description) LIKE $var";
  1011. }
  1012. }
  1013. if ( $form->{"partsgroup_$i"} ne "" ) {
  1014. ( $null, $var ) = split /--/, $form->{"partsgroup_$i"};
  1015. $var = $dbh->quote($var);
  1016. $where .= qq| AND p.partsgroup_id = $var|;
  1017. }
  1018. if ( $form->{"description_$i"} ne "" ) {
  1019. $where .= " ORDER BY 3";
  1020. }
  1021. else {
  1022. $where .= " ORDER BY 2";
  1023. }
  1024. my $query = qq|
  1025. SELECT p.id, p.partnumber, p.description,
  1026. pg.partsgroup, p.partsgroup_id,
  1027. p.lastcost AS sellprice, p.unit, p.bin, p.onhand,
  1028. p.notes, p.inventory_accno_id, p.income_accno_id,
  1029. p.expense_accno_id, p.partnumber AS sku, p.weight,
  1030. t1.description AS translation,
  1031. t2.description AS grouptranslation
  1032. FROM parts p
  1033. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  1034. LEFT JOIN translation t1
  1035. ON (t1.trans_id = p.id AND t1.language_code = ?)
  1036. LEFT JOIN translation t2
  1037. ON (t2.trans_id = p.partsgroup_id
  1038. AND t2.language_code = ?)
  1039. $where|;
  1040. my $sth = $dbh->prepare($query);
  1041. $sth->execute( $form->{language_code}, $form->{language_code} )
  1042. || $form->dberror($query);
  1043. # foreign currency
  1044. &exchangerate_defaults( $dbh, $form );
  1045. # taxes
  1046. $query = qq|
  1047. SELECT c.accno
  1048. FROM chart c
  1049. JOIN partstax pt ON (pt.chart_id = c.id)
  1050. WHERE pt.parts_id = ?|;
  1051. my $tth = $dbh->prepare($query) || $form->dberror($query);
  1052. # price matrix
  1053. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  1054. my $ref;
  1055. my $ptref;
  1056. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1057. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1058. my ($dec) = ( $ref->{sellprice} =~ /\.(\d+)/ );
  1059. $dec = length $dec;
  1060. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  1061. # get taxes for part
  1062. $tth->execute( $ref->{id} );
  1063. $ref->{taxaccounts} = "";
  1064. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  1065. $ref->{taxaccounts} .= "$ptref->{accno} ";
  1066. }
  1067. $tth->finish;
  1068. chop $ref->{taxaccounts};
  1069. # get vendor price and partnumber
  1070. PriceMatrix::price_matrix( $pmh, $ref, $decimalplaces, $form,
  1071. $myconfig );
  1072. $ref->{description} = $ref->{translation}
  1073. if $ref->{translation};
  1074. $ref->{partsgroup} = $ref->{grouptranslation}
  1075. if $ref->{grouptranslation};
  1076. push @{ $form->{item_list} }, $ref;
  1077. }
  1078. $sth->finish;
  1079. }
  1080. sub exchangerate_defaults {
  1081. my ( $dbh, $form ) = @_;
  1082. my $var;
  1083. # get default currencies
  1084. my $query = qq|
  1085. SELECT substr(value,1,3), value FROM defaults
  1086. WHERE setting_key = 'curr'|;
  1087. my $eth = $dbh->prepare($query) || $form->dberror($query);
  1088. $eth->execute;
  1089. ( $form->{defaultcurrency}, $form->{currencies} ) = $eth->fetchrow_array;
  1090. $eth->finish;
  1091. $query = qq|
  1092. SELECT sell
  1093. FROM exchangerate
  1094. WHERE curr = ?
  1095. AND transdate = ?|;
  1096. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  1097. $query = qq/
  1098. SELECT max(transdate || ' ' || sell || ' ' || curr)
  1099. FROM exchangerate
  1100. WHERE curr = ?/;
  1101. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  1102. # get exchange rates for transdate or max
  1103. foreach $var ( split /:/, substr( $form->{currencies}, 4 ) ) {
  1104. $eth1->execute( $var, $form->{transdate} );
  1105. @array = $eth1->fetchrow_array;
  1106. $form->db_parse_numeric(sth=> $eth1, arrayref=>\@array);
  1107. $form->{$var} = shift @array;
  1108. if ( !$form->{$var} ) {
  1109. $eth2->execute($var);
  1110. ( $form->{$var} ) = $eth2->fetchrow_array;
  1111. ( $null, $form->{$var} ) = split / /, $form->{$var};
  1112. $form->{$var} = 1 unless $form->{$var};
  1113. $eth2->finish;
  1114. }
  1115. $eth1->finish;
  1116. }
  1117. $form->{ $form->{currency} } = $form->{exchangerate}
  1118. if $form->{exchangerate};
  1119. $form->{ $form->{currency} } ||= 1;
  1120. $form->{ $form->{defaultcurrency} } = 1;
  1121. }
  1122. sub vendor_details {
  1123. my ( $self, $myconfig, $form ) = @_;
  1124. # connect to database
  1125. my $dbh = $form->{dbh};
  1126. # get rest for the vendor
  1127. my $query = qq|
  1128. SELECT vendornumber, name, address1, address2, city, state,
  1129. zipcode, country, contact, phone as vendorphone,
  1130. fax as vendorfax, vendornumber,
  1131. taxnumber AS vendortaxnumber, sic_code AS sic, iban, bic,
  1132. gifi_accno AS gifi, startdate, enddate
  1133. FROM vendor
  1134. WHERE id = ?|;
  1135. my $sth = $dbh->prepare($query);
  1136. $sth->execute( $form->{vendor_id} ) || $form->dberror($query);
  1137. $ref = $sth->fetchrow_hashref(NAME_lc);
  1138. for ( keys %$ref ) {
  1139. $form->{$_} = $ref->{$_};
  1140. }
  1141. $sth->finish;
  1142. }
  1143. sub item_links {
  1144. my ( $self, $myconfig, $form ) = @_;
  1145. # connect to database
  1146. my $dbh = $form->{dbh};
  1147. my $query = qq|
  1148. SELECT accno, description, link
  1149. FROM chart
  1150. WHERE link LIKE '%IC%'
  1151. ORDER BY accno|;
  1152. my $sth = $dbh->prepare($query);
  1153. $sth->execute || $form->dberror($query);
  1154. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1155. foreach my $key ( split( /:/, $ref->{link} ) ) {
  1156. if ( $key =~ /IC/ ) {
  1157. push @{ $form->{IC_links}{$key} },
  1158. {
  1159. accno => $ref->{accno},
  1160. description => $ref->{description}
  1161. };
  1162. }
  1163. }
  1164. }
  1165. $sth->finish;
  1166. }
  1167. 1;