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