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