summaryrefslogtreecommitdiff
path: root/LedgerSMB/IR.pm
blob: d9da72a96edf09310f0156861e3171025d12013d (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2000
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors: Jim Rawlings <jim@your-dba.com>
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # Inventory received module
  31. #
  32. #======================================================================
  33. package IR;
  34. use LedgerSMB::Tax;
  35. use LedgerSMB::PriceMatrix;
  36. use LedgerSMB::Sysconfig;
  37. use Math::BigFloat;
  38. sub post_invoice {
  39. my ( $self, $myconfig, $form ) = @_;
  40. my $dbh = $form->{dbh};
  41. $form->{invnumber} = $form->update_defaults( $myconfig, "vinumber", $dbh )
  42. unless $form->{invnumber};
  43. for ( 1 .. $form->{rowcount} ) {
  44. unless ( $form->{"deliverydate_$_"} ) {
  45. $form->{"deliverydate_$_"} = $form->{transdate};
  46. }
  47. }
  48. my $query;
  49. my $sth;
  50. my $ref;
  51. my $null;
  52. my $project_id;
  53. my $exchangerate = 0;
  54. my $allocated;
  55. my $taxrate;
  56. my $taxamount;
  57. my $diff = 0;
  58. my $item;
  59. my $invoice_id;
  60. my $keepcleared;
  61. ( $null, $form->{employee_id} ) = split /--/, $form->{employee};
  62. unless ( $form->{employee_id} ) {
  63. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  64. }
  65. ( $null, $form->{department_id} ) = split( /--/, $form->{department} );
  66. $form->{department_id} *= 1;
  67. $query = qq|
  68. SELECT (SELECT value FROM defaults
  69. WHERE setting_key = 'fxgain_accno_id')
  70. AS fxgain_accno_id,
  71. (SELECT value FROM defaults
  72. WHERE setting_key = 'fxloss_accno_id')
  73. AS fxloss_accno_id|;
  74. my ( $fxgain_accno_id, $fxloss_accno_id ) = $dbh->selectrow_array($query);
  75. $query = qq|
  76. SELECT inventory_accno_id, income_accno_id, expense_accno_id
  77. FROM parts
  78. WHERE id = ?|;
  79. my $pth = $dbh->prepare($query) || $form->dberror($query);
  80. my %updparts = ();
  81. if ( $form->{id} ) {
  82. my $sth;
  83. $keepcleared = 1;
  84. $query = qq|SELECT id FROM ap WHERE id = ?|;
  85. $sth = $dbh->prepare($query);
  86. $sth->execute( $form->{id} );
  87. if ( $sth->fetchrow_array ) {
  88. $query = qq|
  89. SELECT p.id, p.inventory_accno_id,
  90. p.income_accno_id
  91. FROM invoice i
  92. JOIN parts p ON (p.id = i.parts_id)
  93. WHERE i.trans_id = ?|;
  94. $sth = $dbh->prepare($query);
  95. $sth->execute( $form->{id} ) || $form->dberror($query);
  96. while ( $ref = $sth->fetchrow_hashref ) {
  97. if ( $ref->{inventory_accno_id}
  98. && $ref->{income_accno_id} )
  99. {
  100. $updparts{ $ref->{id} } = 1;
  101. }
  102. }
  103. $sth->finish;
  104. &reverse_invoice( $dbh, $form );
  105. }
  106. else {
  107. $query = qq|INSERT INTO ap (id) VALUES (?)|;
  108. $sth = $dbh->prepare($query);
  109. $sth->execute( $form->{id} ) || $form->dberror($query);
  110. }
  111. }
  112. my $uid = localtime;
  113. $uid .= "$$";
  114. if ( !$form->{id} ) {
  115. $query = qq|
  116. INSERT INTO ap (invnumber, employee_id)
  117. VALUES ('$uid', (SELECT id FROM employee
  118. WHERE login = ?))|;
  119. $sth = $dbh->prepare($query);
  120. $sth->execute( $form->{login} ) || $form->dberror($query);
  121. $query = qq|SELECT id FROM ap WHERE invnumber = '$uid'|;
  122. $sth = $dbh->prepare($query);
  123. $sth->execute || $form->dberror($query);
  124. ( $form->{id} ) = $sth->fetchrow_array;
  125. $sth->finish;
  126. }
  127. my $amount;
  128. my $grossamount;
  129. my $allocated;
  130. my $invamount = 0;
  131. my $invnetamount = 0;
  132. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  133. $form->{exchangerate} = 1;
  134. }
  135. else {
  136. $exchangerate =
  137. $form->check_exchangerate( $myconfig, $form->{currency},
  138. $form->{transdate}, 'sell' );
  139. }
  140. $form->{exchangerate} =
  141. ($exchangerate)
  142. ? $exchangerate
  143. : $form->parse_amount( $myconfig, $form->{exchangerate} );
  144. for my $i ( 1 .. $form->{rowcount} ) {
  145. $form->{"qty_$i"} = $form->parse_amount( $myconfig, $form->{"qty_$i"} );
  146. if ( $form->{"qty_$i"} ) {
  147. $pth->execute( $form->{"id_$i"} );
  148. $ref = $pth->fetchrow_hashref(NAME_lc);
  149. for ( keys %$ref ) {
  150. $form->{"${_}_$i"} = $ref->{$_};
  151. }
  152. $pth->finish;
  153. # project
  154. if ( $form->{"projectnumber_$i"} ne "" ) {
  155. ( $null, $project_id ) =
  156. split /--/, $form->{"projectnumber_$i"};
  157. }
  158. # undo discount formatting
  159. $form->{"discount_$i"} =
  160. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) / 100;
  161. # keep entered selling price
  162. my $fxsellprice =
  163. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  164. my ($dec) = ( $fxsellprice =~ /\.(\d+)/ );
  165. $dec = length $dec;
  166. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  167. # deduct discount
  168. $form->{"sellprice_$i"} = $fxsellprice -
  169. $form->round_amount( $fxsellprice * $form->{"discount_$i"},
  170. $decimalplaces );
  171. # linetotal
  172. my $fxlinetotal =
  173. $form->round_amount( $form->{"sellprice_$i"} * $form->{"qty_$i"},
  174. 2 );
  175. $amount = $fxlinetotal * $form->{exchangerate};
  176. my $linetotal = $form->round_amount( $amount, 2 );
  177. $fxdiff += $amount - $linetotal;
  178. @taxaccounts = Tax::init_taxes(
  179. $form,
  180. $form->{"taxaccounts_$i"},
  181. $form->{'taxaccounts'}
  182. );
  183. $tax = Math::BigFloat->bzero();
  184. $fxtax = Math::BigFloat->bzero();
  185. if ( $form->{taxincluded} ) {
  186. $tax += $amount =
  187. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  188. $form->{"sellprice_$i"} -= $amount / $form->{"qty_$i"};
  189. }
  190. else {
  191. $tax += $amount =
  192. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  193. $fxtax +=
  194. Tax::calculate_taxes( \@taxaccounts, $form, $fxlinetotal, 0 );
  195. }
  196. for (@taxaccounts) {
  197. $form->{acc_trans}{ $form->{id} }{ $_->account }{amount} +=
  198. $_->value;
  199. }
  200. $grossamount = $form->round_amount( $linetotal, 2 );
  201. if ( $form->{taxincluded} ) {
  202. $amount = $form->round_amount( $tax, 2 );
  203. $linetotal -= $form->round_amount( $tax - $diff, 2 );
  204. $diff = ( $amount - $tax );
  205. }
  206. $amount = $form->round_amount( $linetotal, 2 );
  207. $allocated = 0;
  208. # adjust and round sellprice
  209. $form->{"sellprice_$i"} =
  210. $form->round_amount(
  211. $form->{"sellprice_$i"} * $form->{exchangerate},
  212. $decimalplaces );
  213. # save detail record in invoice table
  214. $query = qq|
  215. INSERT INTO invoice (description)
  216. VALUES ('$uid')|;
  217. $dbh->do($query) || $form->dberror($query);
  218. $query = qq|
  219. SELECT id FROM invoice
  220. WHERE description = '$uid'|;
  221. ($invoice_id) = $dbh->selectrow_array($query);
  222. if ( $form->{"inventory_accno_id_$i"} ) {
  223. my $totalqty = $form->{"qty_$i"};
  224. # if this is a exit for the product
  225. if($form->{"qty_$i"}<0) {
  226. $query = qq|
  227. SELECT i.id, i.qty, i.allocated, a.transdate
  228. FROM invoice i
  229. JOIN parts p ON (p.id = i.parts_id)
  230. JOIN ap a ON (a.id = i.trans_id)
  231. WHERE i.parts_id = ? AND (i.qty + i.allocated) < 0 AND i.sellprice = ?
  232. ORDER BY transdate
  233. |;
  234. $sth = $dbh->prepare($query);
  235. $sth->execute( $form->{"id_$i"}, $form->{"sellprice_$i"}) || $form->dberror($query);
  236. my $totalqty = $form->{"qty_$i"};
  237. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  238. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  239. my $qty = $ref->{qty} + $ref->{allocated};
  240. if ( ( $qty - $totalqty ) < 0 ) { $qty = $totalqty; }
  241. # update allocated for sold item
  242. $form->update_balance( $dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1 );
  243. $allocated += $qty;
  244. last if ( ( $totalqty -= $qty ) >= 0 );
  245. }
  246. } else {
  247. # start patch bug 1755355 ###############################################################################
  248. # check for unallocated entries atthe same price to match our entry
  249. $query = qq|
  250. SELECT i.id, i.qty, i.allocated, a.transdate
  251. FROM invoice i
  252. JOIN parts p ON (p.id = i.parts_id)
  253. JOIN ap a ON (a.id = i.trans_id)
  254. WHERE i.parts_id = ? AND (i.qty + i.allocated) > 0 AND i.sellprice = ?
  255. ORDER BY transdate|;
  256. $sth = $dbh->prepare($query);
  257. $sth->execute( $form->{"id_$i"}, $form->{"sellprice_$i"}) || $form->dberror($query);
  258. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  259. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  260. my $qty = $ref->{qty} + $ref->{allocated};
  261. if ( ( $qty - $totalqty ) > 0 ) { $qty = $totalqty; }
  262. # update allocated for sold item
  263. $form->update_balance( $dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1 );
  264. $allocated += $qty;
  265. last if ( ( $totalqty -= $qty ) <= 0 );
  266. }
  267. } else {
  268. # start patch bug 1755355 ###############################################################################
  269. # check for unallocated entries atthe same price to match our entry
  270. $query = qq|
  271. SELECT i.id, i.qty, i.allocated, a.transdate
  272. FROM invoice i
  273. JOIN parts p ON (p.id = i.parts_id)
  274. JOIN ap a ON (a.id = i.trans_id)
  275. WHERE i.parts_id = ? AND (i.qty + i.allocated) > 0 AND i.sellprice = ?
  276. ORDER BY transdate|;
  277. $sth = $dbh->prepare($query);
  278. $sth->execute( $form->{"id_$i"}, $form->{"sellprice_$i"}) || $form->dberror($query);
  279. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  280. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  281. my $qty = $ref->{qty} + $ref->{allocated};
  282. if ( ( $qty - $totalqty ) > 0 ) { $qty = $totalqty; }
  283. # update allocated for sold item
  284. $form->update_balance( $dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1 );
  285. $allocated += $qty;
  286. last if ( ( $totalqty -= $qty ) <= 0 );
  287. }
  288. }
  289. # add purchase to inventory
  290. push @{ $form->{acc_trans}{lineitems} },
  291. {
  292. chart_id => $form->{"inventory_accno_id_$i"},
  293. amount => $amount,
  294. fxgrossamount => $fxlinetotal +
  295. $form->round_amount( $fxtax, 2 ),
  296. grossamount => $grossamount,
  297. project_id => $project_id,
  298. invoice_id => $invoice_id
  299. };
  300. $updparts{ $form->{"id_$i"} } = 1;
  301. # update parts table
  302. $form->update_balance( $dbh, "parts", "onhand",
  303. qq|id = $form->{"id_$i"}|,
  304. $form->{"qty_$i"} )
  305. unless $form->{shipped};
  306. # check if we sold the item
  307. $query = qq|
  308. SELECT i.id, i.qty, i.allocated,
  309. i.trans_id, i.project_id,
  310. p.inventory_accno_id,
  311. p.expense_accno_id, a.transdate
  312. FROM invoice i
  313. JOIN parts p ON (p.id = i.parts_id)
  314. JOIN ar a ON (a.id = i.trans_id)
  315. WHERE i.parts_id = ?
  316. AND (i.qty + i.allocated) > 0
  317. ORDER BY transdate|;
  318. my $sth = $dbh->prepare($query);
  319. $sth->execute( $form->{"id_$i"} )
  320. || $form->dberror($query);
  321. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  322. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  323. my $qty = $ref->{qty} + $ref->{allocated};
  324. if ( ( $qty - $totalqty ) > 0 ) {
  325. $qty = $totalqty;
  326. }
  327. $linetotal =
  328. $form->round_amount( $form->{"sellprice_$i"} * $qty, 2 );
  329. if ($linetotal) {
  330. $query = qq|
  331. INSERT INTO acc_trans
  332. (trans_id,
  333. chart_id,
  334. amount,
  335. transdate,
  336. project_id,
  337. invoice_id)
  338. VALUES (?, ?, ?, ?,
  339. ?, ?)|;
  340. my $sth = $dbh->prepare($query);
  341. $sth->execute(
  342. $ref->{trans_id}, $ref->{inventory_accno_id},
  343. $linetotal, $ref->{transdate},
  344. $ref->{project_id}, $invoice_id
  345. ) || $form->dberror($query);
  346. # add expense
  347. $query = qq|
  348. INSERT INTO acc_trans
  349. (trans_id,
  350. chart_id,
  351. amount,
  352. transdate,
  353. project_id,
  354. invoice_id)
  355. VALUES (?, ?, ?, ?,
  356. ?, ?)|;
  357. $sth = $dbh->prepare($query);
  358. $sth->execute(
  359. $ref->{trans_id}, $ref->{expense_accno_id},
  360. $linetotal * -1, $ref->{transdate},
  361. $ref->{project_id}, $invoice_id
  362. ) || $form->dberror($query);
  363. }
  364. # update allocated for sold item
  365. $form->update_balance( $dbh, "invoice", "allocated",
  366. qq|id = $ref->{id}|,
  367. $qty * -1 );
  368. $allocated += $qty;
  369. last if ( ( $totalqty -= $qty ) <= 0 );
  370. }
  371. $sth->finish;
  372. }
  373. else {
  374. # add purchase to expense
  375. push @{ $form->{acc_trans}{lineitems} },
  376. {
  377. chart_id => $form->{"expense_accno_id_$i"},
  378. amount => $amount,
  379. fxgrossamount => $fxlinetotal +
  380. $form->round_amount( $fxtax, 2 ),
  381. grossamount => $grossamount,
  382. project_id => $project_id,
  383. invoice_id => $invoice_id
  384. };
  385. }
  386. $query = qq|
  387. UPDATE invoice
  388. SET trans_id = ?,
  389. parts_id = ?,
  390. description = ?,
  391. qty = ?,
  392. sellprice = ?,
  393. fxsellprice = ?,
  394. discount = ?,
  395. allocated = ?,
  396. unit = ?,
  397. deliverydate = ?,
  398. project_id = ?,
  399. serialnumber = ?,
  400. notes = ?
  401. WHERE id = ?|;
  402. $sth = $dbh->prepare($query);
  403. $sth->execute(
  404. $form->{id}, $form->{"id_$i"},
  405. $form->{"description_$i"}, $form->{"qty_$i"} * -1,
  406. $form->{"sellprice_$i"}, $fxsellprice,
  407. $form->{"discount_$i"}, $allocated,
  408. $form->{"unit_$i"}, $form->{"deliverydate_$i"},
  409. $project_id, $form->{"serialnumber_$i"},
  410. $form->{"notes_$i"}, $invoice_id
  411. ) || $form->dberror($query);
  412. }
  413. }
  414. $form->{paid} = 0;
  415. for $i ( 1 .. $form->{paidaccounts} ) {
  416. $form->{"paid_$i"} =
  417. $form->parse_amount( $myconfig, $form->{"paid_$i"} );
  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. my $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. FROM ap a
  924. WHERE id = ?|;
  925. $sth = $dbh->prepare($query);
  926. $sth->execute( $form->{id} ) || $form->dberror($query);
  927. $ref = $sth->fetchrow_hashref(NAME_lc);
  928. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  929. for ( keys %$ref ) {
  930. $form->{$_} = $ref->{$_};
  931. }
  932. $sth->finish;
  933. # get shipto
  934. $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
  935. $sth = $dbh->prepare($query);
  936. $sth->execute( $form->{id} ) || $form->dberror($query);
  937. $ref = $sth->fetchrow_hashref(NAME_lc);
  938. for ( keys %$ref ) {
  939. $form->{$_} = $ref->{$_};
  940. }
  941. $sth->finish;
  942. # retrieve individual items
  943. $query = qq|
  944. SELECT p.partnumber, i.description, i.qty,
  945. i.fxsellprice, i.sellprice,
  946. i.parts_id AS id, i.unit, p.bin,
  947. i.deliverydate,
  948. pr.projectnumber, i.project_id,
  949. i.serialnumber,
  950. i.discount, i.notes, pg.partsgroup,
  951. p.partsgroup_id, p.partnumber AS sku,
  952. p.weight, p.onhand, p.inventory_accno_id,
  953. p.income_accno_id, p.expense_accno_id,
  954. t.description AS partsgrouptranslation
  955. FROM invoice i
  956. JOIN parts p ON (i.parts_id = p.id)
  957. LEFT JOIN project pr ON (i.project_id = pr.id)
  958. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  959. LEFT JOIN translation t
  960. ON (t.trans_id = p.partsgroup_id
  961. AND t.language_code = ?)
  962. WHERE i.trans_id = ?
  963. ORDER BY i.id|;
  964. $sth = $dbh->prepare($query);
  965. $sth->execute( $form->{language_code}, $form->{id} )
  966. || $form->dberror($query);
  967. # exchangerate defaults
  968. &exchangerate_defaults( $dbh, $form );
  969. # price matrix and vendor partnumber
  970. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  971. # tax rates for part
  972. $query = qq|
  973. SELECT c.accno
  974. FROM chart c
  975. JOIN partstax pt ON (pt.chart_id = c.id)
  976. WHERE pt.parts_id = ?|;
  977. my $tth = $dbh->prepare($query);
  978. my $ptref;
  979. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  980. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  981. my ($dec) = ( $ref->{fxsellprice} =~ /\.(\d+)/ );
  982. $dec = length $dec;
  983. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  984. $tth->execute( $ref->{id} );
  985. $ref->{taxaccounts} = "";
  986. my $taxrate = 0;
  987. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  988. $form->db_parse_numeric(sth => $tth, hashref => $ptref);
  989. $ref->{taxaccounts} .= "$ptref->{accno} ";
  990. $taxrate += $form->{"$ptref->{accno}_rate"};
  991. }
  992. $tth->finish;
  993. chop $ref->{taxaccounts};
  994. # price matrix
  995. $ref->{sellprice} =
  996. $form->round_amount(
  997. $ref->{fxsellprice} * $form->{ $form->{currency} },
  998. $decimalplaces );
  999. PriceMatrix::price_matrix( $pmh, $ref, $decimalplaces, $form,
  1000. $myconfig );
  1001. $ref->{sellprice} = $ref->{fxsellprice};
  1002. $ref->{qty} *= -1;
  1003. $ref->{partsgroup} = $ref->{partsgrouptranslation}
  1004. if $ref->{partsgrouptranslation};
  1005. push @{ $form->{invoice_details} }, $ref;
  1006. }
  1007. $sth->finish;
  1008. }
  1009. my $rc = $dbh->commit;
  1010. $rc;
  1011. }
  1012. sub retrieve_item {
  1013. my ( $self, $myconfig, $form ) = @_;
  1014. $dbh = $form->{dbh};
  1015. my $i = $form->{rowcount};
  1016. my $null;
  1017. my $var;
  1018. # don't include assemblies or obsolete parts
  1019. my $where = "WHERE p.assembly = '0' AND p.obsolete = '0'";
  1020. if ( $form->{"partnumber_$i"} ne "" ) {
  1021. $var = $dbh->quote( $form->like( lc $form->{"partnumber_$i"} ) );
  1022. $where .= " AND lower(p.partnumber) LIKE $var";
  1023. }
  1024. if ( $form->{"description_$i"} ne "" ) {
  1025. $var = $dbh->quote( $form->like( lc $form->{"description_$i"} ) );
  1026. if ( $form->{language_code} ne "" ) {
  1027. $where .= " AND lower(t1.description) LIKE $var";
  1028. }
  1029. else {
  1030. $where .= " AND lower(p.description) LIKE $var";
  1031. }
  1032. }
  1033. if ( $form->{"partsgroup_$i"} ne "" ) {
  1034. ( $null, $var ) = split /--/, $form->{"partsgroup_$i"};
  1035. $var = $dbh->quote($var);
  1036. $where .= qq| AND p.partsgroup_id = $var|;
  1037. }
  1038. if ( $form->{"description_$i"} ne "" ) {
  1039. $where .= " ORDER BY 3";
  1040. }
  1041. else {
  1042. $where .= " ORDER BY 2";
  1043. }
  1044. my $query = qq|
  1045. SELECT p.id, p.partnumber, p.description,
  1046. pg.partsgroup, p.partsgroup_id,
  1047. p.lastcost AS sellprice, p.unit, p.bin, p.onhand,
  1048. p.notes, p.inventory_accno_id, p.income_accno_id,
  1049. p.expense_accno_id, p.partnumber AS sku, p.weight,
  1050. t1.description AS translation,
  1051. t2.description AS grouptranslation
  1052. FROM parts p
  1053. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  1054. LEFT JOIN translation t1
  1055. ON (t1.trans_id = p.id AND t1.language_code = ?)
  1056. LEFT JOIN translation t2
  1057. ON (t2.trans_id = p.partsgroup_id
  1058. AND t2.language_code = ?)
  1059. $where|;
  1060. my $sth = $dbh->prepare($query);
  1061. $sth->execute( $form->{language_code}, $form->{language_code} )
  1062. || $form->dberror($query);
  1063. # foreign currency
  1064. &exchangerate_defaults( $dbh, $form );
  1065. # taxes
  1066. $query = qq|
  1067. SELECT c.accno
  1068. FROM chart c
  1069. JOIN partstax pt ON (pt.chart_id = c.id)
  1070. WHERE pt.parts_id = ?|;
  1071. my $tth = $dbh->prepare($query) || $form->dberror($query);
  1072. # price matrix
  1073. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  1074. my $ref;
  1075. my $ptref;
  1076. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1077. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1078. my ($dec) = ( $ref->{sellprice} =~ /\.(\d+)/ );
  1079. $dec = length $dec;
  1080. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  1081. # get taxes for part
  1082. $tth->execute( $ref->{id} );
  1083. $ref->{taxaccounts} = "";
  1084. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  1085. $ref->{taxaccounts} .= "$ptref->{accno} ";
  1086. }
  1087. $tth->finish;
  1088. chop $ref->{taxaccounts};
  1089. # get vendor price and partnumber
  1090. PriceMatrix::price_matrix( $pmh, $ref, $decimalplaces, $form,
  1091. $myconfig );
  1092. $ref->{description} = $ref->{translation}
  1093. if $ref->{translation};
  1094. $ref->{partsgroup} = $ref->{grouptranslation}
  1095. if $ref->{grouptranslation};
  1096. push @{ $form->{item_list} }, $ref;
  1097. }
  1098. $sth->finish;
  1099. }
  1100. sub exchangerate_defaults {
  1101. my ( $dbh, $form ) = @_;
  1102. my $var;
  1103. # get default currencies
  1104. my $query = qq|
  1105. SELECT substr(value,1,3), value FROM defaults
  1106. WHERE setting_key = 'curr'|;
  1107. my $eth = $dbh->prepare($query) || $form->dberror($query);
  1108. $eth->execute;
  1109. ( $form->{defaultcurrency}, $form->{currencies} ) = $eth->fetchrow_array;
  1110. $eth->finish;
  1111. $query = qq|
  1112. SELECT sell
  1113. FROM exchangerate
  1114. WHERE curr = ?
  1115. AND transdate = ?|;
  1116. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  1117. $query = qq/
  1118. SELECT max(transdate || ' ' || sell || ' ' || curr)
  1119. FROM exchangerate
  1120. WHERE curr = ?/;
  1121. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  1122. # get exchange rates for transdate or max
  1123. foreach $var ( split /:/, substr( $form->{currencies}, 4 ) ) {
  1124. $eth1->execute( $var, $form->{transdate} );
  1125. @array = $eth1->fetchrow_array;
  1126. $form->db_parse_numeric(sth=> $eth1, arrayref=>\@array);
  1127. $form->{$var} = shift @array;
  1128. if ( !$form->{$var} ) {
  1129. $eth2->execute($var);
  1130. ( $form->{$var} ) = $eth2->fetchrow_array;
  1131. ( $null, $form->{$var} ) = split / /, $form->{$var};
  1132. $form->{$var} = 1 unless $form->{$var};
  1133. $eth2->finish;
  1134. }
  1135. $eth1->finish;
  1136. }
  1137. $form->{ $form->{currency} } = $form->{exchangerate}
  1138. if $form->{exchangerate};
  1139. $form->{ $form->{currency} } ||= 1;
  1140. $form->{ $form->{defaultcurrency} } = 1;
  1141. }
  1142. sub vendor_details {
  1143. my ( $self, $myconfig, $form ) = @_;
  1144. # connect to database
  1145. my $dbh = $form->{dbh};
  1146. # get rest for the vendor
  1147. my $query = qq|
  1148. SELECT vendornumber, name, address1, address2, city, state,
  1149. zipcode, country, contact, phone as vendorphone,
  1150. fax as vendorfax, vendornumber,
  1151. taxnumber AS vendortaxnumber, sic_code AS sic, iban, bic,
  1152. gifi_accno AS gifi, startdate, enddate
  1153. FROM vendor
  1154. WHERE id = ?|;
  1155. my $sth = $dbh->prepare($query);
  1156. $sth->execute( $form->{vendor_id} ) || $form->dberror($query);
  1157. $ref = $sth->fetchrow_hashref(NAME_lc);
  1158. for ( keys %$ref ) {
  1159. $form->{$_} = $ref->{$_};
  1160. }
  1161. $sth->finish;
  1162. }
  1163. sub item_links {
  1164. my ( $self, $myconfig, $form ) = @_;
  1165. # connect to database
  1166. my $dbh = $form->{dbh};
  1167. my $query = qq|
  1168. SELECT accno, description, link
  1169. FROM chart
  1170. WHERE link LIKE '%IC%'
  1171. ORDER BY accno|;
  1172. my $sth = $dbh->prepare($query);
  1173. $sth->execute || $form->dberror($query);
  1174. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1175. foreach my $key ( split( /:/, $ref->{link} ) ) {
  1176. if ( $key =~ /IC/ ) {
  1177. push @{ $form->{IC_links}{$key} },
  1178. {
  1179. accno => $ref->{accno},
  1180. description => $ref->{description}
  1181. };
  1182. }
  1183. }
  1184. }
  1185. $sth->finish;
  1186. }
  1187. 1;