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