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