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