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