summaryrefslogtreecommitdiff
path: root/LedgerSMB/IR.pm
blob: 4020eb361fa1da4ea719822ea7725fc971d67c5c (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (C) 2000
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors: Jim Rawlings <jim@your-dba.com>
  22. #
  23. #======================================================================
  24. #
  25. # This file has NOT undergone whitespace cleanup.
  26. #
  27. #======================================================================
  28. #
  29. # Inventory received module
  30. #
  31. #======================================================================
  32. package IR;
  33. sub post_invoice {
  34. my ($self, $myconfig, $form) = @_;
  35. # connect to database, turn off autocommit
  36. my $dbh = $form->dbconnect_noauto($myconfig);
  37. my $query;
  38. my $sth;
  39. my $ref;
  40. my $null;
  41. my $project_id;
  42. my $exchangerate = 0;
  43. my $allocated;
  44. my $taxrate;
  45. my $taxamount;
  46. my $diff = 0;
  47. my $item;
  48. my $invoice_id;
  49. my $keepcleared;
  50. ($null, $form->{employee_id}) = split /--/, $form->{employee};
  51. unless ($form->{employee_id}) {
  52. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
  53. }
  54. ($null, $form->{department_id}) = split(/--/, $form->{department});
  55. $form->{department_id} *= 1;
  56. $query = qq|SELECT fxgain_accno_id, fxloss_accno_id
  57. FROM defaults d|;
  58. my ($fxgain_accno_id, $fxloss_accno_id) = $dbh->selectrow_array($query);
  59. $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
  60. FROM parts
  61. WHERE id = ?|;
  62. my $pth = $dbh->prepare($query) || $form->dberror($query);
  63. my %updparts = ();
  64. if ($form->{id}) {
  65. $keepcleared = 1;
  66. $query = qq|SELECT id FROM ap
  67. WHERE id = $form->{id}|;
  68. if ($dbh->selectrow_array($query)) {
  69. $query = qq|SELECT p.id, p.inventory_accno_id, p.income_accno_id
  70. FROM invoice i
  71. JOIN parts p ON (p.id = i.parts_id)
  72. WHERE i.trans_id = $form->{id}|;
  73. $sth = $dbh->prepare($query);
  74. $sth->execute || $form->dberror($query);
  75. while ($ref = $sth->fetchrow_hashref) {
  76. if ($ref->{inventory_accno_id} && $ref->{income_accno_id}) {
  77. $updparts{$ref->{id}} = 1;
  78. }
  79. }
  80. $sth->finish;
  81. &reverse_invoice($dbh, $form);
  82. } else {
  83. $query = qq|INSERT INTO ap (id)
  84. VALUES ($form->{id})|;
  85. $dbh->do($query) || $form->dberror($query);
  86. }
  87. }
  88. my $uid = localtime;
  89. $uid .= "$$";
  90. if (! $form->{id}) {
  91. $query = qq|INSERT INTO ap (invnumber, employee_id)
  92. VALUES ('$uid', (SELECT id FROM employee
  93. WHERE login = '$form->{login}'))|;
  94. $dbh->do($query) || $form->dberror($query);
  95. $query = qq|SELECT id FROM ap
  96. WHERE invnumber = '$uid'|;
  97. $sth = $dbh->prepare($query);
  98. $sth->execute || $form->dberror($query);
  99. ($form->{id}) = $sth->fetchrow_array;
  100. $sth->finish;
  101. }
  102. my $amount;
  103. my $grossamount;
  104. my $allocated;
  105. my $invamount = 0;
  106. my $invnetamount = 0;
  107. if ($form->{currency} eq $form->{defaultcurrency}) {
  108. $form->{exchangerate} = 1;
  109. } else {
  110. $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell');
  111. }
  112. $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
  113. for my $i (1 .. $form->{rowcount}) {
  114. $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
  115. if ($form->{"qty_$i"}) {
  116. $pth->execute($form->{"id_$i"});
  117. $ref = $pth->fetchrow_hashref(NAME_lc);
  118. for (keys %$ref) {
  119. $form->{"${_}_$i"} = $ref->{$_};
  120. }
  121. $pth->finish;
  122. # project
  123. $project_id = 'NULL';
  124. if ($form->{"projectnumber_$i"} ne "") {
  125. ($null, $project_id) = split /--/, $form->{"projectnumber_$i"};
  126. }
  127. # undo discount formatting
  128. $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
  129. # keep entered selling price
  130. my $fxsellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
  131. my ($dec) = ($fxsellprice =~ /\.(\d+)/);
  132. $dec = length $dec;
  133. my $decimalplaces = ($dec > 2) ? $dec : 2;
  134. # deduct discount
  135. $form->{"sellprice_$i"} = $fxsellprice - $form->round_amount($fxsellprice * $form->{"discount_$i"}, $decimalplaces);
  136. # linetotal
  137. my $fxlinetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
  138. $amount = $fxlinetotal * $form->{exchangerate};
  139. my $linetotal = $form->round_amount($amount, 2);
  140. $fxdiff += $amount - $linetotal;
  141. @taxaccounts = split / /, $form->{"taxaccounts_$i"};
  142. $ml = 1;
  143. $tax = 0;
  144. $fxtax = 0;
  145. for (0 .. 1) {
  146. $taxrate = 0;
  147. # add tax rates
  148. for (@taxaccounts) {
  149. $taxrate += $form->{"${_}_rate"} if ($form->{"${_}_rate"} * $ml) > 0;
  150. }
  151. if ($form->{taxincluded}) {
  152. $tax += $amount = $linetotal * ($taxrate / (1 + ($taxrate * $ml)));
  153. $form->{"sellprice_$i"} -= $amount / $form->{"qty_$i"};
  154. } else {
  155. $tax += $amount = $linetotal * $taxrate;
  156. $fxtax += $fxlinetotal * $taxrate;
  157. }
  158. for (@taxaccounts) {
  159. $form->{acc_trans}{$form->{id}}{$_}{amount} += $amount * $form->{"${_}_rate"} / $taxrate if ($form->{"${_}_rate"} * $ml) > 0;
  160. }
  161. $ml = -1;
  162. }
  163. $grossamount = $form->round_amount($linetotal, 2);
  164. if ($form->{taxincluded}) {
  165. $amount = $form->round_amount($tax, 2);
  166. $linetotal -= $form->round_amount($tax - $diff, 2);
  167. $diff = ($amount - $tax);
  168. }
  169. $amount = $form->round_amount($linetotal, 2);
  170. $allocated = 0;
  171. # adjust and round sellprice
  172. $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
  173. # save detail record in invoice table
  174. $query = qq|INSERT INTO invoice (description)
  175. VALUES ('$uid')|;
  176. $dbh->do($query) || $form->dberror($query);
  177. $query = qq|SELECT id FROM invoice
  178. WHERE description = '$uid'|;
  179. ($invoice_id) = $dbh->selectrow_array($query);
  180. $query = qq|UPDATE invoice SET
  181. trans_id = $form->{id},
  182. parts_id = $form->{"id_$i"},
  183. description = |.$dbh->quote($form->{"description_$i"}).qq|,
  184. qty = $form->{"qty_$i"} * -1,
  185. sellprice = $form->{"sellprice_$i"},
  186. fxsellprice = $fxsellprice,
  187. discount = $form->{"discount_$i"},
  188. allocated = $allocated,
  189. unit = |.$dbh->quote($form->{"unit_$i"}).qq|,
  190. deliverydate = |.$form->dbquote($form->{"deliverydate_$i"}, SQL_DATE).qq|,
  191. project_id = $project_id,
  192. serialnumber = |.$dbh->quote($form->{"serialnumber_$i"}).qq|,
  193. notes = |.$dbh->quote($form->{"notes_$i"}).qq|
  194. WHERE id = $invoice_id|;
  195. $dbh->do($query) || $form->dberror($query);
  196. if ($form->{"inventory_accno_id_$i"}) {
  197. # add purchase to inventory
  198. push @{ $form->{acc_trans}{lineitems} }, {
  199. chart_id => $form->{"inventory_accno_id_$i"},
  200. amount => $amount,
  201. fxgrossamount => $fxlinetotal + $form->round_amount($fxtax, 2),
  202. grossamount => $grossamount,
  203. project_id => $project_id,
  204. invoice_id => $invoice_id };
  205. $updparts{$form->{"id_$i"}} = 1;
  206. # update parts table
  207. $form->update_balance($dbh,
  208. "parts",
  209. "onhand",
  210. qq|id = $form->{"id_$i"}|,
  211. $form->{"qty_$i"}) unless $form->{shipped};
  212. # check if we sold the item
  213. $query = qq|SELECT i.id, i.qty, i.allocated, i.trans_id, i.project_id,
  214. p.inventory_accno_id, p.expense_accno_id, a.transdate
  215. FROM invoice i
  216. JOIN parts p ON (p.id = i.parts_id)
  217. JOIN ar a ON (a.id = i.trans_id)
  218. WHERE i.parts_id = $form->{"id_$i"}
  219. AND (i.qty + i.allocated) > 0
  220. ORDER BY transdate|;
  221. $sth = $dbh->prepare($query);
  222. $sth->execute || $form->dberror($query);
  223. my $totalqty = $form->{"qty_$i"};
  224. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  225. my $qty = $ref->{qty} + $ref->{allocated};
  226. if (($qty - $totalqty) > 0) {
  227. $qty = $totalqty;
  228. }
  229. $linetotal = $form->round_amount($form->{"sellprice_$i"} * $qty, 2);
  230. $ref->{project_id} ||= 'NULL';
  231. # add entry for inventory, this one is for the sold item
  232. if ($linetotal) {
  233. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  234. transdate, project_id, invoice_id)
  235. VALUES ($ref->{trans_id}, $ref->{inventory_accno_id},
  236. $linetotal, '$ref->{transdate}', $ref->{project_id},
  237. $invoice_id)|;
  238. $dbh->do($query) || $form->dberror($query);
  239. # add expense
  240. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  241. transdate, project_id, invoice_id)
  242. VALUES ($ref->{trans_id}, $ref->{expense_accno_id},
  243. |. ($linetotal * -1) .qq|, '$ref->{transdate}',
  244. $ref->{project_id}, $invoice_id)|;
  245. $dbh->do($query) || $form->dberror($query);
  246. }
  247. # update allocated for sold item
  248. $form->update_balance($dbh,
  249. "invoice",
  250. "allocated",
  251. qq|id = $ref->{id}|,
  252. $qty * -1);
  253. $allocated += $qty;
  254. last if (($totalqty -= $qty) <= 0);
  255. }
  256. $sth->finish;
  257. } else {
  258. # add purchase to expense
  259. push @{ $form->{acc_trans}{lineitems} }, {
  260. chart_id => $form->{"expense_accno_id_$i"},
  261. amount => $amount,
  262. fxgrossamount => $fxlinetotal + $form->round_amount($fxtax, 2),
  263. grossamount => $grossamount,
  264. project_id => $project_id,
  265. invoice_id => $invoice_id };
  266. }
  267. }
  268. }
  269. $form->{paid} = 0;
  270. for $i (1 .. $form->{paidaccounts}) {
  271. $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
  272. $form->{paid} += $form->{"paid_$i"};
  273. $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
  274. }
  275. # add lineitems + tax
  276. $amount = 0;
  277. $grossamount = 0;
  278. $fxgrossamount = 0;
  279. for (@{ $form->{acc_trans}{lineitems} }) {
  280. $amount += $_->{amount};
  281. $grossamount += $_->{grossamount};
  282. $fxgrossamount += $_->{fxgrossamount};
  283. }
  284. $invnetamount = $amount;
  285. $amount = 0;
  286. for (split / /, $form->{taxaccounts}) {
  287. $amount += $form->{acc_trans}{$form->{id}}{$_}{amount} = $form->round_amount($form->{acc_trans}{$form->{id}}{$_}{amount}, 2);
  288. $form->{acc_trans}{$form->{id}}{$_}{amount} *= -1;
  289. }
  290. $invamount = $invnetamount + $amount;
  291. $diff = 0;
  292. if ($form->{taxincluded}) {
  293. $diff = $form->round_amount($grossamount - $invamount, 2);
  294. $invamount += $diff;
  295. }
  296. $fxdiff = $form->round_amount($fxdiff,2);
  297. $invnetamount += $fxdiff;
  298. $invamount += $fxdiff;
  299. if ($form->round_amount($form->{paid} - $fxgrossamount,2) == 0) {
  300. $form->{paid} = $invamount;
  301. } else {
  302. $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
  303. }
  304. foreach $ref (sort { $b->{amount} <=> $a->{amount} } @ { $form->{acc_trans}{lineitems} }) {
  305. $amount = $ref->{amount} + $diff + $fxdiff;
  306. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  307. transdate, project_id, invoice_id)
  308. VALUES ($form->{id}, $ref->{chart_id}, $amount * -1,
  309. '$form->{transdate}', $ref->{project_id}, $ref->{invoice_id})|;
  310. $dbh->do($query) || $form->dberror($query);
  311. $diff = 0;
  312. $fxdiff = 0;
  313. }
  314. $form->{payables} = $invamount;
  315. delete $form->{acc_trans}{lineitems};
  316. # update exchangerate
  317. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  318. $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, $form->{exchangerate});
  319. }
  320. # record payable
  321. if ($form->{payables}) {
  322. ($accno) = split /--/, $form->{AP};
  323. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  324. transdate)
  325. VALUES ($form->{id},
  326. (SELECT id FROM chart
  327. WHERE accno = '$accno'),
  328. $form->{payables}, '$form->{transdate}')|;
  329. $dbh->do($query) || $form->dberror($query);
  330. }
  331. foreach my $trans_id (keys %{$form->{acc_trans}}) {
  332. foreach my $accno (keys %{ $form->{acc_trans}{$trans_id} }) {
  333. $amount = $form->round_amount($form->{acc_trans}{$trans_id}{$accno}{amount}, 2);
  334. if ($amount) {
  335. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  336. transdate)
  337. VALUES ($trans_id, (SELECT id FROM chart
  338. WHERE accno = '$accno'),
  339. $amount, '$form->{transdate}')|;
  340. $dbh->do($query) || $form->dberror($query);
  341. }
  342. }
  343. }
  344. # if there is no amount but a payment record payable
  345. if ($invamount == 0) {
  346. $form->{payables} = 1;
  347. }
  348. my $cleared = 0;
  349. # record payments and offsetting AP
  350. for my $i (1 .. $form->{paidaccounts}) {
  351. if ($form->{"paid_$i"}) {
  352. my ($accno) = split /--/, $form->{"AP_paid_$i"};
  353. $form->{"datepaid_$i"} = $form->{transdate} unless ($form->{"datepaid_$i"});
  354. $form->{datepaid} = $form->{"datepaid_$i"};
  355. $exchangerate = 0;
  356. if ($form->{currency} eq $form->{defaultcurrency}) {
  357. $form->{"exchangerate_$i"} = 1;
  358. } else {
  359. $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
  360. $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
  361. }
  362. # record AP
  363. $amount = ($form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2)) * -1;
  364. if ($form->{payables}) {
  365. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  366. transdate)
  367. VALUES ($form->{id}, (SELECT id FROM chart
  368. WHERE accno = '$form->{AP}'),
  369. $amount, '$form->{"datepaid_$i"}')|;
  370. $dbh->do($query) || $form->dberror($query);
  371. }
  372. if ($keepcleared) {
  373. $cleared = ($form->{"cleared_$i"}) ? 1 : 0;
  374. }
  375. # record payment
  376. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
  377. source, memo, cleared)
  378. VALUES ($form->{id}, (SELECT id FROM chart
  379. WHERE accno = '$accno'),
  380. $form->{"paid_$i"}, '$form->{"datepaid_$i"}', |
  381. .$dbh->quote($form->{"source_$i"}).qq|, |
  382. .$dbh->quote($form->{"memo_$i"}).qq|, '$cleared')|;
  383. $dbh->do($query) || $form->dberror($query);
  384. # exchangerate difference
  385. $amount = $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate_$i"} - $form->{"paid_$i"}, 2);
  386. if ($amount) {
  387. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  388. transdate, source, fx_transaction, cleared)
  389. VALUES ($form->{id}, (SELECT id FROM chart
  390. WHERE accno = '$accno'),
  391. $amount, '$form->{"datepaid_$i"}', |
  392. .$dbh->quote($form->{"source_$i"}).qq|, '1', '$cleared')|;
  393. $dbh->do($query) || $form->dberror($query);
  394. }
  395. # gain/loss
  396. $amount = $form->round_amount($form->round_amount($form->{"paid_$i"} * $form->{exchangerate},2) - $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate_$i"},2), 2);
  397. if ($amount) {
  398. my $accno_id = ($amount > 0) ? $fxgain_accno_id : $fxloss_accno_id;
  399. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  400. transdate, fx_transaction, cleared)
  401. VALUES ($form->{id}, $accno_id,
  402. $amount, '$form->{"datepaid_$i"}', '1', '$cleared')|;
  403. $dbh->do($query) || $form->dberror($query);
  404. }
  405. # update exchange rate
  406. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  407. $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, 0, $form->{"exchangerate_$i"});
  408. }
  409. }
  410. }
  411. # set values which could be empty
  412. $form->{taxincluded} *= 1;
  413. $form->{invnumber} = $form->update_defaults($myconfig, "vinumber", $dbh) unless $form->{invnumber};
  414. # save AP record
  415. $query = qq|UPDATE ap set
  416. invnumber = |.$dbh->quote($form->{invnumber}).qq|,
  417. ordnumber = |.$dbh->quote($form->{ordnumber}).qq|,
  418. quonumber = |.$dbh->quote($form->{quonumber}).qq|,
  419. transdate = '$form->{transdate}',
  420. vendor_id = $form->{vendor_id},
  421. amount = $invamount,
  422. netamount = $invnetamount,
  423. paid = $form->{paid},
  424. datepaid = |.$form->dbquote($form->{datepaid}, SQL_DATE).qq|,
  425. duedate = |.$form->dbquote($form->{duedate}, SQL_DATE).qq|,
  426. invoice = '1',
  427. shippingpoint = |.$dbh->quote($form->{shippingpoint}).qq|,
  428. shipvia = |.$dbh->quote($form->{shipvia}).qq|,
  429. taxincluded = '$form->{taxincluded}',
  430. notes = |.$dbh->quote($form->{notes}).qq|,
  431. intnotes = |.$dbh->quote($form->{intnotes}).qq|,
  432. curr = '$form->{currency}',
  433. department_id = $form->{department_id},
  434. employee_id = $form->{employee_id},
  435. language_code = '$form->{language_code}',
  436. ponumber = |.$dbh->quote($form->{ponumber}).qq|
  437. WHERE id = $form->{id}|;
  438. $dbh->do($query) || $form->dberror($query);
  439. # add shipto
  440. $form->{name} = $form->{vendor};
  441. $form->{name} =~ s/--$form->{vendor_id}//;
  442. $form->add_shipto($dbh, $form->{id});
  443. my %audittrail = ( tablename => 'ap',
  444. reference => $form->{invnumber},
  445. formname => $form->{type},
  446. action => 'posted',
  447. id => $form->{id} );
  448. $form->audittrail($dbh, "", \%audittrail);
  449. my $rc = $dbh->commit;
  450. foreach $item (keys %updparts) {
  451. $query = qq|UPDATE parts SET
  452. avgcost = avgcost($item),
  453. lastcost = lastcost($item)
  454. WHERE id = $item|;
  455. $dbh->do($query) || $form->dberror($query);
  456. $dbh->commit;
  457. }
  458. $dbh->disconnect;
  459. $rc;
  460. }
  461. sub reverse_invoice {
  462. my ($dbh, $form) = @_;
  463. my $query = qq|SELECT id FROM ap
  464. WHERE id = $form->{id}|;
  465. my ($id) = $dbh->selectrow_array($query);
  466. return unless $id;
  467. # reverse inventory items
  468. $query = qq|SELECT i.parts_id, p.inventory_accno_id, p.expense_accno_id,
  469. i.qty, i.allocated, i.sellprice, i.project_id
  470. FROM invoice i, parts p
  471. WHERE i.parts_id = p.id
  472. AND i.trans_id = $form->{id}|;
  473. my $sth = $dbh->prepare($query);
  474. $sth->execute || $form->dberror($query);
  475. my $netamount = 0;
  476. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  477. $netamount += $form->round_amount($ref->{sellprice} * $ref->{qty} * -1, 2);
  478. if ($ref->{inventory_accno_id}) {
  479. # update onhand
  480. $form->update_balance($dbh,
  481. "parts",
  482. "onhand",
  483. qq|id = $ref->{parts_id}|,
  484. $ref->{qty});
  485. # if $ref->{allocated} > 0 than we sold that many items
  486. if ($ref->{allocated} > 0) {
  487. # get references for sold items
  488. $query = qq|SELECT i.id, i.trans_id, i.allocated, a.transdate
  489. FROM invoice i, ar a
  490. WHERE i.parts_id = $ref->{parts_id}
  491. AND i.allocated < 0
  492. AND i.trans_id = a.id
  493. ORDER BY transdate DESC|;
  494. my $sth = $dbh->prepare($query);
  495. $sth->execute || $form->dberror($query);
  496. while (my $pthref = $sth->fetchrow_hashref(NAME_lc)) {
  497. my $qty = $ref->{allocated};
  498. if (($ref->{allocated} + $pthref->{allocated}) > 0) {
  499. $qty = $pthref->{allocated} * -1;
  500. }
  501. my $amount = $form->round_amount($ref->{sellprice} * $qty, 2);
  502. #adjust allocated
  503. $form->update_balance($dbh,
  504. "invoice",
  505. "allocated",
  506. qq|id = $pthref->{id}|,
  507. $qty);
  508. # add reversal for sale
  509. $ref->{project_id} *= 1;
  510. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  511. transdate, project_id)
  512. VALUES ($pthref->{trans_id}, $ref->{expense_accno_id},
  513. $amount, '$form->{transdate}', $ref->{project_id})|;
  514. $dbh->do($query) || $form->dberror($query);
  515. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  516. transdate, project_id)
  517. VALUES ($pthref->{trans_id}, $ref->{inventory_accno_id},
  518. $amount * -1, '$form->{transdate}', $ref->{project_id})|;
  519. $dbh->do($query) || $form->dberror($query);
  520. last if (($ref->{allocated} -= $qty) <= 0);
  521. }
  522. $sth->finish;
  523. }
  524. }
  525. }
  526. $sth->finish;
  527. # delete acc_trans
  528. $query = qq|DELETE FROM acc_trans
  529. WHERE trans_id = $form->{id}|;
  530. $dbh->do($query) || $form->dberror($query);
  531. # delete invoice entries
  532. $query = qq|DELETE FROM invoice
  533. WHERE trans_id = $form->{id}|;
  534. $dbh->do($query) || $form->dberror($query);
  535. $query = qq|DELETE FROM shipto
  536. WHERE trans_id = $form->{id}|;
  537. $dbh->do($query) || $form->dberror($query);
  538. $dbh->commit;
  539. }
  540. sub delete_invoice {
  541. my ($self, $myconfig, $form) = @_;
  542. # connect to database
  543. my $dbh = $form->dbconnect_noauto($myconfig);
  544. my %audittrail = ( tablename => 'ap',
  545. reference => $form->{invnumber},
  546. formname => $form->{type},
  547. action => 'deleted',
  548. id => $form->{id} );
  549. $form->audittrail($dbh, "", \%audittrail);
  550. my $query = qq|SELECT parts_id FROM invoice
  551. WHERE trans_id = $form->{id}|;
  552. my $sth = $dbh->prepare($query);
  553. $sth->execute || $form->dberror($query);
  554. my $item;
  555. my %updparts = ();
  556. while (($item) = $sth->fetchrow_array) {
  557. $updparts{$item} = 1;
  558. }
  559. $sth->finish;
  560. &reverse_invoice($dbh, $form);
  561. # delete AP record
  562. $query = qq|DELETE FROM ap
  563. WHERE id = $form->{id}|;
  564. $dbh->do($query) || $form->dberror($query);
  565. # delete spool files
  566. $query = qq|SELECT spoolfile FROM status
  567. WHERE trans_id = $form->{id}
  568. AND spoolfile IS NOT NULL|;
  569. my $sth = $dbh->prepare($query);
  570. $sth->execute || $form->dberror($query);
  571. my $spoolfile;
  572. my @spoolfiles = ();
  573. while (($spoolfile) = $sth->fetchrow_array) {
  574. push @spoolfiles, $spoolfile;
  575. }
  576. $sth->finish;
  577. # delete status entries
  578. $query = qq|DELETE FROM status
  579. WHERE trans_id = $form->{id}|;
  580. $dbh->do($query) || $form->dberror($query);
  581. my $rc = $dbh->commit;
  582. if ($rc) {
  583. foreach $item (keys %updparts) {
  584. $query = qq|UPDATE parts SET
  585. avgcost = avgcost($item),
  586. lastcost = lastcost($item)
  587. WHERE id = $item|;
  588. $dbh->do($query) || $form->dberror($query);
  589. $dbh->commit;
  590. }
  591. foreach $spoolfile (@spoolfiles) {
  592. unlink "$spool/$spoolfile" if $spoolfile;
  593. }
  594. }
  595. $dbh->disconnect;
  596. $rc;
  597. }
  598. sub retrieve_invoice {
  599. my ($self, $myconfig, $form) = @_;
  600. # connect to database
  601. my $dbh = $form->dbconnect_noauto($myconfig);
  602. my $query;
  603. if ($form->{id}) {
  604. # get default accounts and last invoice number
  605. $query = qq|SELECT (SELECT c.accno FROM chart c
  606. WHERE d.inventory_accno_id = c.id) AS inventory_accno,
  607. (SELECT c.accno FROM chart c
  608. WHERE d.income_accno_id = c.id) AS income_accno,
  609. (SELECT c.accno FROM chart c
  610. WHERE d.expense_accno_id = c.id) AS expense_accno,
  611. (SELECT c.accno FROM chart c
  612. WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
  613. (SELECT c.accno FROM chart c
  614. WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
  615. d.curr AS currencies
  616. FROM defaults d|;
  617. } else {
  618. $query = qq|SELECT (SELECT c.accno FROM chart c
  619. WHERE d.inventory_accno_id = c.id) AS inventory_accno,
  620. (SELECT c.accno FROM chart c
  621. WHERE d.income_accno_id = c.id) AS income_accno,
  622. (SELECT c.accno FROM chart c
  623. WHERE d.expense_accno_id = c.id) AS expense_accno,
  624. (SELECT c.accno FROM chart c
  625. WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
  626. (SELECT c.accno FROM chart c
  627. WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
  628. d.curr AS currencies,
  629. current_date AS transdate
  630. FROM defaults d|;
  631. }
  632. my $sth = $dbh->prepare($query);
  633. $sth->execute || $form->dberror($query);
  634. my $ref = $sth->fetchrow_hashref(NAME_lc);
  635. for (keys %$ref) {
  636. $form->{$_} = $ref->{$_};
  637. }
  638. $sth->finish;
  639. if ($form->{id}) {
  640. # retrieve invoice
  641. $query = qq|SELECT a.invnumber, a.transdate, a.duedate,
  642. a.ordnumber, a.quonumber, a.paid, a.taxincluded, a.notes,
  643. a.intnotes, a.curr AS currency, a.vendor_id, a.language_code,
  644. a.ponumber
  645. FROM ap a
  646. WHERE id = $form->{id}|;
  647. $sth = $dbh->prepare($query);
  648. $sth->execute || $form->dberror($query);
  649. $ref = $sth->fetchrow_hashref(NAME_lc);
  650. for (keys %$ref) {
  651. $form->{$_} = $ref->{$_};
  652. }
  653. $sth->finish;
  654. # get shipto
  655. $query = qq|SELECT * FROM shipto
  656. WHERE trans_id = $form->{id}|;
  657. $sth = $dbh->prepare($query);
  658. $sth->execute || $form->dberror($query);
  659. $ref = $sth->fetchrow_hashref(NAME_lc);
  660. for (keys %$ref) {
  661. $form->{$_} = $ref->{$_};
  662. }
  663. $sth->finish;
  664. # retrieve individual items
  665. $query = qq|SELECT
  666. p.partnumber, i.description, i.qty, i.fxsellprice, i.sellprice,
  667. i.parts_id AS id, i.unit, p.bin, i.deliverydate,
  668. pr.projectnumber,
  669. i.project_id, i.serialnumber, i.discount, i.notes,
  670. pg.partsgroup, p.partsgroup_id, p.partnumber AS sku,
  671. p.weight, p.onhand,
  672. p.inventory_accno_id, p.income_accno_id, p.expense_accno_id,
  673. t.description AS partsgrouptranslation
  674. FROM invoice i
  675. JOIN parts p ON (i.parts_id = p.id)
  676. LEFT JOIN project pr ON (i.project_id = pr.id)
  677. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  678. LEFT JOIN translation t ON (t.trans_id = p.partsgroup_id AND t.language_code = '$form->{language_code}')
  679. WHERE i.trans_id = $form->{id}
  680. ORDER BY i.id|;
  681. $sth = $dbh->prepare($query);
  682. $sth->execute || $form->dberror($query);
  683. # exchangerate defaults
  684. &exchangerate_defaults($dbh, $form);
  685. # price matrix and vendor partnumber
  686. $query = qq|SELECT partnumber
  687. FROM partsvendor
  688. WHERE parts_id = ?
  689. AND vendor_id = $form->{vendor_id}|;
  690. my $pmh = $dbh->prepare($query) || $form->dberror($query);
  691. # tax rates for part
  692. $query = qq|SELECT c.accno
  693. FROM chart c
  694. JOIN partstax pt ON (pt.chart_id = c.id)
  695. WHERE pt.parts_id = ?|;
  696. my $tth = $dbh->prepare($query);
  697. my $ptref;
  698. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  699. my ($dec) = ($ref->{fxsellprice} =~ /\.(\d+)/);
  700. $dec = length $dec;
  701. my $decimalplaces = ($dec > 2) ? $dec : 2;
  702. $tth->execute($ref->{id});
  703. $ref->{taxaccounts} = "";
  704. my $taxrate = 0;
  705. while ($ptref = $tth->fetchrow_hashref(NAME_lc)) {
  706. $ref->{taxaccounts} .= "$ptref->{accno} ";
  707. $taxrate += $form->{"$ptref->{accno}_rate"};
  708. }
  709. $tth->finish;
  710. chop $ref->{taxaccounts};
  711. # price matrix
  712. $ref->{sellprice} = $form->round_amount($ref->{fxsellprice} * $form->{$form->{currency}}, $decimalplaces);
  713. &price_matrix($pmh, $ref, $decimalplaces, $form);
  714. $ref->{sellprice} = $ref->{fxsellprice};
  715. $ref->{qty} *= -1;
  716. $ref->{partsgroup} = $ref->{partsgrouptranslation} if $ref->{partsgrouptranslation};
  717. push @{ $form->{invoice_details} }, $ref;
  718. }
  719. $sth->finish;
  720. }
  721. my $rc = $dbh->commit;
  722. $dbh->disconnect;
  723. $rc;
  724. }
  725. sub retrieve_item {
  726. my ($self, $myconfig, $form) = @_;
  727. my $i = $form->{rowcount};
  728. my $null;
  729. my $var;
  730. # don't include assemblies or obsolete parts
  731. my $where = "WHERE p.assembly = '0' AND p.obsolete = '0'";
  732. if ($form->{"partnumber_$i"} ne "") {
  733. $var = $form->like(lc $form->{"partnumber_$i"});
  734. $where .= " AND lower(p.partnumber) LIKE '$var'";
  735. }
  736. if ($form->{"description_$i"} ne "") {
  737. $var = $form->like(lc $form->{"description_$i"});
  738. if ($form->{language_code} ne "") {
  739. $where .= " AND lower(t1.description) LIKE '$var'";
  740. } else {
  741. $where .= " AND lower(p.description) LIKE '$var'";
  742. }
  743. }
  744. if ($form->{"partsgroup_$i"} ne "") {
  745. ($null, $var) = split /--/, $form->{"partsgroup_$i"};
  746. $var *= 1;
  747. $where .= qq| AND p.partsgroup_id = $var|;
  748. }
  749. if ($form->{"description_$i"} ne "") {
  750. $where .= " ORDER BY 3";
  751. } else {
  752. $where .= " ORDER BY 2";
  753. }
  754. # connect to database
  755. my $dbh = $form->dbconnect($myconfig);
  756. my $query = qq|SELECT p.id, p.partnumber, p.description,
  757. pg.partsgroup, p.partsgroup_id,
  758. p.lastcost AS sellprice, p.unit, p.bin, p.onhand, p.notes,
  759. p.inventory_accno_id, p.income_accno_id, p.expense_accno_id,
  760. p.partnumber AS sku, p.weight,
  761. t1.description AS translation,
  762. t2.description AS grouptranslation
  763. FROM parts p
  764. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  765. LEFT JOIN translation t1 ON (t1.trans_id = p.id AND t1.language_code = '$form->{language_code}')
  766. LEFT JOIN translation t2 ON (t2.trans_id = p.partsgroup_id AND t2.language_code = '$form->{language_code}')
  767. $where|;
  768. my $sth = $dbh->prepare($query);
  769. $sth->execute || $form->dberror($query);
  770. # foreign currency
  771. &exchangerate_defaults($dbh, $form);
  772. # taxes
  773. $query = qq|SELECT c.accno
  774. FROM chart c
  775. JOIN partstax pt ON (pt.chart_id = c.id)
  776. WHERE pt.parts_id = ?|;
  777. my $tth = $dbh->prepare($query) || $form->dberror($query);
  778. # price matrix
  779. $query = qq|SELECT p.*
  780. FROM partsvendor p
  781. WHERE p.parts_id = ?
  782. AND vendor_id = $form->{vendor_id}|;
  783. my $pmh = $dbh->prepare($query) || $form->dberror($query);
  784. my $ref;
  785. my $ptref;
  786. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  787. my ($dec) = ($ref->{sellprice} =~ /\.(\d+)/);
  788. $dec = length $dec;
  789. my $decimalplaces = ($dec > 2) ? $dec : 2;
  790. # get taxes for part
  791. $tth->execute($ref->{id});
  792. $ref->{taxaccounts} = "";
  793. while ($ptref = $tth->fetchrow_hashref(NAME_lc)) {
  794. $ref->{taxaccounts} .= "$ptref->{accno} ";
  795. }
  796. $tth->finish;
  797. chop $ref->{taxaccounts};
  798. # get vendor price and partnumber
  799. &price_matrix($pmh, $ref, $decimalplaces, $form);
  800. $ref->{description} = $ref->{translation} if $ref->{translation};
  801. $ref->{partsgroup} = $ref->{grouptranslation} if $ref->{grouptranslation};
  802. push @{ $form->{item_list} }, $ref;
  803. }
  804. $sth->finish;
  805. $dbh->disconnect;
  806. }
  807. sub exchangerate_defaults {
  808. my ($dbh, $form) = @_;
  809. my $var;
  810. # get default currencies
  811. my $query = qq|SELECT substr(curr,1,3), curr FROM defaults|;
  812. my $eth = $dbh->prepare($query) || $form->dberror($query);
  813. $eth->execute;
  814. ($form->{defaultcurrency}, $form->{currencies}) = $eth->fetchrow_array;
  815. $eth->finish;
  816. $query = qq|SELECT sell
  817. FROM exchangerate
  818. WHERE curr = ?
  819. AND transdate = ?|;
  820. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  821. $query = qq~SELECT max(transdate || ' ' || sell || ' ' || curr)
  822. FROM exchangerate
  823. WHERE curr = ?~;
  824. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  825. # get exchange rates for transdate or max
  826. foreach $var (split /:/, substr($form->{currencies},4)) {
  827. $eth1->execute($var, $form->{transdate});
  828. ($form->{$var}) = $eth1->fetchrow_array;
  829. if (! $form->{$var} ) {
  830. $eth2->execute($var);
  831. ($form->{$var}) = $eth2->fetchrow_array;
  832. ($null, $form->{$var}) = split / /, $form->{$var};
  833. $form->{$var} = 1 unless $form->{$var};
  834. $eth2->finish;
  835. }
  836. $eth1->finish;
  837. }
  838. $form->{$form->{currency}} = $form->{exchangerate} if $form->{exchangerate};
  839. $form->{$form->{currency}} ||= 1;
  840. $form->{$form->{defaultcurrency}} = 1;
  841. }
  842. sub price_matrix {
  843. my ($pmh, $ref, $decimalplaces, $form) = @_;
  844. $pmh->execute($ref->{id});
  845. my $mref = $pmh->fetchrow_hashref(NAME_lc);
  846. if ($mref->{partnumber} ne "") {
  847. $ref->{partnumber} = $mref->{partnumber};
  848. }
  849. if ($mref->{lastcost}) {
  850. # do a conversion
  851. $ref->{sellprice} = $form->round_amount($mref->{lastcost} * $form->{$mref->{curr}}, $decimalplaces);
  852. }
  853. $pmh->finish;
  854. $ref->{sellprice} *= 1;
  855. # add 0:price to matrix
  856. $ref->{pricematrix} = "0:$ref->{sellprice}";
  857. }
  858. sub vendor_details {
  859. my ($self, $myconfig, $form) = @_;
  860. # connect to database
  861. my $dbh = $form->dbconnect($myconfig);
  862. # get rest for the vendor
  863. my $query = qq|SELECT vendornumber, name, address1, address2, city, state,
  864. zipcode, country,
  865. contact, phone as vendorphone, fax as vendorfax, vendornumber,
  866. taxnumber AS vendortaxnumber, sic_code AS sic, iban, bic,
  867. gifi_accno AS gifi, startdate, enddate
  868. FROM vendor
  869. WHERE id = $form->{vendor_id}|;
  870. my $sth = $dbh->prepare($query);
  871. $sth->execute || $form->dberror($query);
  872. $ref = $sth->fetchrow_hashref(NAME_lc);
  873. for (keys %$ref) {
  874. $form->{$_} = $ref->{$_};
  875. }
  876. $sth->finish;
  877. $dbh->disconnect;
  878. }
  879. sub item_links {
  880. my ($self, $myconfig, $form) = @_;
  881. # connect to database
  882. my $dbh = $form->dbconnect($myconfig);
  883. my $query = qq|SELECT accno, description, link
  884. FROM chart
  885. WHERE link LIKE '%IC%'
  886. ORDER BY accno|;
  887. my $sth = $dbh->prepare($query);
  888. $sth->execute || $form->dberror($query);
  889. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  890. foreach my $key (split(/:/, $ref->{link})) {
  891. if ($key =~ /IC/) {
  892. push @{ $form->{IC_links}{$key} }, { accno => $ref->{accno},
  893. description => $ref->{description} };
  894. }
  895. }
  896. }
  897. $sth->finish;
  898. }
  899. 1;