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