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