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