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