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