summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: 0c1b1d2c57ed5d58d4534a93ac4be6166f814803 (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) 2001
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors:
  22. #
  23. #======================================================================
  24. #
  25. # This file has NOT undergone whitespace cleanup.
  26. #
  27. #======================================================================
  28. #
  29. # Order entry module
  30. # Quotation
  31. #
  32. #======================================================================
  33. package OE;
  34. sub transactions {
  35. my ($self, $myconfig, $form) = @_;
  36. # connect to database
  37. my $dbh = $form->dbconnect($myconfig);
  38. my $query;
  39. my $null;
  40. my $var;
  41. my $ordnumber = 'ordnumber';
  42. my $quotation = '0';
  43. my $department;
  44. my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
  45. ($form->{transdatefrom}, $form->{transdateto}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  46. if ($form->{type} =~ /_quotation$/) {
  47. $quotation = '1';
  48. $ordnumber = 'quonumber';
  49. }
  50. my $number = $form->like(lc $form->{$ordnumber});
  51. my $name = $form->like(lc $form->{$form->{vc}});
  52. for (qw(department employee)) {
  53. if ($form->{$_}) {
  54. ($null, $var) = split /--/, $form->{$_};
  55. $department .= " AND o.${_}_id = $var";
  56. }
  57. }
  58. my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
  59. o.amount, ct.name, o.netamount, o.$form->{vc}_id,
  60. ex.$rate AS exchangerate,
  61. o.closed, o.quonumber, o.shippingpoint, o.shipvia,
  62. e.name AS employee, m.name AS manager, o.curr, o.ponumber
  63. FROM oe o
  64. JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
  65. LEFT JOIN employee e ON (o.employee_id = e.id)
  66. LEFT JOIN employee m ON (e.managerid = m.id)
  67. LEFT JOIN exchangerate ex ON (ex.curr = o.curr
  68. AND ex.transdate = o.transdate)
  69. WHERE o.quotation = '$quotation'
  70. $department|;
  71. my %ordinal = ( id => 1,
  72. ordnumber => 2,
  73. transdate => 3,
  74. reqdate => 4,
  75. name => 6,
  76. quonumber => 11,
  77. shipvia => 13,
  78. employee => 14,
  79. manager => 15,
  80. curr => 16,
  81. ponumber => 17
  82. );
  83. my @a = (transdate, $ordnumber, name);
  84. push @a, "employee" if $form->{l_employee};
  85. if ($form->{type} !~ /(ship|receive)_order/) {
  86. push @a, "manager" if $form->{l_manager};
  87. }
  88. my $sortorder = $form->sort_order(\@a, \%ordinal);
  89. # build query if type eq (ship|receive)_order
  90. if ($form->{type} =~ /(ship|receive)_order/) {
  91. my ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
  92. $query = qq|SELECT DISTINCT o.id, o.ordnumber, o.transdate,
  93. o.reqdate, o.amount, ct.name, o.netamount, o.$form->{vc}_id,
  94. ex.$rate AS exchangerate,
  95. o.closed, o.quonumber, o.shippingpoint, o.shipvia,
  96. e.name AS employee, o.curr, o.ponumber
  97. FROM oe o
  98. JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
  99. JOIN orderitems oi ON (oi.trans_id = o.id)
  100. JOIN parts p ON (p.id = oi.parts_id)|;
  101. if ($warehouse_id && $form->{type} eq 'ship_order') {
  102. $query .= qq|
  103. JOIN inventory i ON (oi.parts_id = i.parts_id)
  104. |;
  105. }
  106. $query .= qq|
  107. LEFT JOIN employee e ON (o.employee_id = e.id)
  108. LEFT JOIN exchangerate ex ON (ex.curr = o.curr
  109. AND ex.transdate = o.transdate)
  110. WHERE o.quotation = '0'
  111. AND (p.inventory_accno_id > 0 OR p.assembly = '1')
  112. AND oi.qty != oi.ship
  113. $department|;
  114. if ($warehouse_id && $form->{type} eq 'ship_order') {
  115. $query .= qq|
  116. AND i.warehouse_id = $warehouse_id
  117. AND ( SELECT SUM(i.qty)
  118. FROM inventory i
  119. WHERE oi.parts_id = i.parts_id
  120. AND i.warehouse_id = $warehouse_id ) > 0
  121. |;
  122. }
  123. }
  124. if ($form->{"$form->{vc}_id"}) {
  125. $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  126. } else {
  127. if ($form->{$form->{vc}} ne "") {
  128. $query .= " AND lower(ct.name) LIKE '$name'";
  129. }
  130. }
  131. if ($form->{$ordnumber} ne "") {
  132. $query .= " AND lower($ordnumber) LIKE '$number'";
  133. $form->{open} = 1;
  134. $form->{closed} = 1;
  135. }
  136. if ($form->{ponumber} ne "") {
  137. $query .= " AND lower(ponumber) LIKE '$form->{ponumber}'";
  138. }
  139. if (!$form->{open} && !$form->{closed}) {
  140. $query .= " AND o.id = 0";
  141. } elsif (!($form->{open} && $form->{closed})) {
  142. $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
  143. }
  144. if ($form->{shipvia} ne "") {
  145. $var = $form->like(lc $form->{shipvia});
  146. $query .= " AND lower(o.shipvia) LIKE '$var'";
  147. }
  148. if ($form->{description} ne "") {
  149. $var = $form->like(lc $form->{description});
  150. $query .= " AND o.id IN (SELECT DISTINCT trans_id
  151. FROM orderitems
  152. WHERE lower(description) LIKE '$var')";
  153. }
  154. if ($form->{transdatefrom}) {
  155. $query .= " AND o.transdate >= '$form->{transdatefrom}'";
  156. }
  157. if ($form->{transdateto}) {
  158. $query .= " AND o.transdate <= '$form->{transdateto}'";
  159. }
  160. $query .= " ORDER by $sortorder";
  161. my $sth = $dbh->prepare($query);
  162. $sth->execute || $form->dberror($query);
  163. my %oid = ();
  164. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  165. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  166. if ($ref->{id} != $oid{id}{$ref->{id}}) {
  167. push @{ $form->{OE} }, $ref;
  168. $oid{vc}{$ref->{curr}}{$ref->{"$form->{vc}_id"}}++;
  169. }
  170. $oid{id}{$ref->{id}} = $ref->{id};
  171. }
  172. $sth->finish;
  173. $dbh->disconnect;
  174. if ($form->{type} =~ /^consolidate_/) {
  175. @a = ();
  176. foreach $ref (@{ $form->{OE} }) { push @a, $ref if $oid{vc}{$ref->{curr}}{$ref->{"$form->{vc}_id"}} > 1 }
  177. @{ $form->{OE} } = @a;
  178. }
  179. }
  180. sub save {
  181. my ($self, $myconfig, $form) = @_;
  182. # connect to database, turn off autocommit
  183. my $dbh = $form->dbconnect_noauto($myconfig);
  184. my $query;
  185. my $sth;
  186. my $null;
  187. my $exchangerate = 0;
  188. ($null, $form->{employee_id}) = split /--/, $form->{employee};
  189. if (! $form->{employee_id}) {
  190. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
  191. $form->{employee} = "$form->{employee}--$form->{employee_id}";
  192. }
  193. my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
  194. $query = qq|SELECT p.assembly, p.project_id
  195. FROM parts p
  196. WHERE p.id = ?|;
  197. my $pth = $dbh->prepare($query) || $form->dberror($query);
  198. if ($form->{id}) {
  199. $query = qq|SELECT id FROM oe
  200. WHERE id = $form->{id}|;
  201. if ($dbh->selectrow_array($query)) {
  202. &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
  203. $query = qq|DELETE FROM orderitems
  204. WHERE trans_id = $form->{id}|;
  205. $dbh->do($query) || $form->dberror($query);
  206. $query = qq|DELETE FROM shipto
  207. WHERE trans_id = $form->{id}|;
  208. $dbh->do($query) || $form->dberror($query);
  209. } else {
  210. $query = qq|INSERT INTO oe (id)
  211. VALUES ($form->{id})|;
  212. $dbh->do($query) || $form->dberror($query);
  213. }
  214. }
  215. if (! $form->{id}) {
  216. my $uid = localtime;
  217. $uid .= "$$";
  218. $query = qq|INSERT INTO oe (ordnumber, employee_id)
  219. VALUES ('$uid', $form->{employee_id})|;
  220. $dbh->do($query) || $form->dberror($query);
  221. $query = qq|SELECT id FROM oe
  222. WHERE ordnumber = '$uid'|;
  223. $sth = $dbh->prepare($query);
  224. $sth->execute || $form->dberror($query);
  225. ($form->{id}) = $sth->fetchrow_array;
  226. $sth->finish;
  227. }
  228. my $amount;
  229. my $linetotal;
  230. my $discount;
  231. my $project_id;
  232. my $taxrate;
  233. my $taxamount;
  234. my $fxsellprice;
  235. my %taxbase;
  236. my @taxaccounts;
  237. my %taxaccounts;
  238. my $netamount = 0;
  239. for my $i (1 .. $form->{rowcount}) {
  240. for (qw(qty ship)) { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) }
  241. $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
  242. $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
  243. if ($form->{"qty_$i"}) {
  244. $pth->execute($form->{"id_$i"});
  245. $ref = $pth->fetchrow_hashref(NAME_lc);
  246. for (keys %$ref) { $form->{"${_}_$i"} = $ref->{$_} }
  247. $pth->finish;
  248. $fxsellprice = $form->{"sellprice_$i"};
  249. my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  250. $dec = length $dec;
  251. my $decimalplaces = ($dec > 2) ? $dec : 2;
  252. $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}, $decimalplaces);
  253. $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
  254. $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
  255. @taxaccounts = split / /, $form->{"taxaccounts_$i"};
  256. $taxrate = 0;
  257. $taxdiff = 0;
  258. for (@taxaccounts) { $taxrate += $form->{"${_}_rate"} }
  259. if ($form->{taxincluded}) {
  260. $taxamount = $linetotal * $taxrate / (1 + $taxrate);
  261. $taxbase = $linetotal - $taxamount;
  262. # we are not keeping a natural price, do not round
  263. $form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
  264. } else {
  265. $taxamount = $linetotal * $taxrate;
  266. $taxbase = $linetotal;
  267. }
  268. if (@taxaccounts && $form->round_amount($taxamount, 2) == 0) {
  269. if ($form->{taxincluded}) {
  270. foreach $item (@taxaccounts) {
  271. $taxamount = $form->round_amount($linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})), 2);
  272. $taxaccounts{$item} += $taxamount;
  273. $taxdiff += $taxamount;
  274. $taxbase{$item} += $taxbase;
  275. }
  276. $taxaccounts{$taxaccounts[0]} += $taxdiff;
  277. } else {
  278. foreach $item (@taxaccounts) {
  279. $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
  280. $taxbase{$item} += $taxbase;
  281. }
  282. }
  283. } else {
  284. foreach $item (@taxaccounts) {
  285. $taxaccounts{$item} += $taxamount * $form->{"${item}_rate"} / $taxrate;
  286. $taxbase{$item} += $taxbase;
  287. }
  288. }
  289. $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
  290. $project_id = 'NULL';
  291. if ($form->{"projectnumber_$i"} ne "") {
  292. ($null, $project_id) = split /--/, $form->{"projectnumber_$i"};
  293. }
  294. $project_id = $form->{"project_id_$i"} if $form->{"project_id_$i"};
  295. # save detail record in orderitems table
  296. $query = qq|INSERT INTO orderitems (|;
  297. $query .= "id, " if $form->{"orderitems_id_$i"};
  298. $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
  299. unit, reqdate, project_id, ship, serialnumber, notes)
  300. VALUES (|;
  301. $query .= qq|$form->{"orderitems_id_$i"},| if $form->{"orderitems_id_$i"};
  302. $query .= qq|$form->{id}, $form->{"id_$i"}, |
  303. .$dbh->quote($form->{"description_$i"}).qq|,
  304. $form->{"qty_$i"}, $fxsellprice, $form->{"discount_$i"}, |
  305. .$dbh->quote($form->{"unit_$i"}).qq|, |
  306. .$form->dbquote($form->{"reqdate_$i"}, SQL_DATE).qq|,
  307. $project_id, $form->{"ship_$i"}, |
  308. .$dbh->quote($form->{"serialnumber_$i"}).qq|, |
  309. .$dbh->quote($form->{"notes_$i"}).qq|)|;
  310. $dbh->do($query) || $form->dberror($query);
  311. $form->{"sellprice_$i"} = $fxsellprice;
  312. }
  313. $form->{"discount_$i"} *= 100;
  314. }
  315. # set values which could be empty
  316. for (qw(vendor_id customer_id taxincluded closed quotation)) { $form->{$_} *= 1 }
  317. # add up the tax
  318. my $tax = 0;
  319. for (keys %taxaccounts) { $tax += $taxaccounts{$_} }
  320. $amount = $form->round_amount($netamount + $tax, 2);
  321. $netamount = $form->round_amount($netamount, 2);
  322. if ($form->{currency} eq $form->{defaultcurrency}) {
  323. $form->{exchangerate} = 1;
  324. } else {
  325. $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, ($form->{vc} eq 'customer') ? 'buy' : 'sell');
  326. }
  327. $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
  328. my $quotation;
  329. my $ordnumber;
  330. my $numberfld;
  331. if ($form->{type} =~ /_order$/) {
  332. $quotation = "0";
  333. $ordnumber = "ordnumber";
  334. $numberfld = ($form->{vc} eq 'customer') ? "sonumber" : "ponumber";
  335. } else {
  336. $quotation = "1";
  337. $ordnumber = "quonumber";
  338. $numberfld = ($form->{vc} eq 'customer') ? "sqnumber" : "rfqnumber";
  339. }
  340. $form->{$ordnumber} = $form->update_defaults($myconfig, $numberfld, $dbh) unless $form->{$ordnumber};
  341. ($null, $form->{department_id}) = split(/--/, $form->{department});
  342. for (qw(department_id terms)) { $form->{$_} *= 1 }
  343. # save OE record
  344. $query = qq|UPDATE oe set
  345. ordnumber = |.$dbh->quote($form->{ordnumber}).qq|,
  346. quonumber = |.$dbh->quote($form->{quonumber}).qq|,
  347. transdate = '$form->{transdate}',
  348. vendor_id = $form->{vendor_id},
  349. customer_id = $form->{customer_id},
  350. amount = $amount,
  351. netamount = $netamount,
  352. reqdate = |.$form->dbquote($form->{reqdate}, SQL_DATE).qq|,
  353. taxincluded = '$form->{taxincluded}',
  354. shippingpoint = |.$dbh->quote($form->{shippingpoint}).qq|,
  355. shipvia = |.$dbh->quote($form->{shipvia}).qq|,
  356. notes = |.$dbh->quote($form->{notes}).qq|,
  357. intnotes = |.$dbh->quote($form->{intnotes}).qq|,
  358. curr = '$form->{currency}',
  359. closed = '$form->{closed}',
  360. quotation = '$quotation',
  361. department_id = $form->{department_id},
  362. employee_id = $form->{employee_id},
  363. language_code = '$form->{language_code}',
  364. ponumber = |.$dbh->quote($form->{ponumber}).qq|,
  365. terms = $form->{terms}
  366. WHERE id = $form->{id}|;
  367. $dbh->do($query) || $form->dberror($query);
  368. $form->{ordtotal} = $amount;
  369. # add shipto
  370. $form->{name} = $form->{$form->{vc}};
  371. $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
  372. $form->add_shipto($dbh, $form->{id});
  373. # save printed, emailed, queued
  374. $form->save_status($dbh);
  375. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  376. if ($form->{vc} eq 'customer') {
  377. $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0);
  378. }
  379. if ($form->{vc} eq 'vendor') {
  380. $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, $form->{exchangerate});
  381. }
  382. }
  383. if ($form->{type} =~ /_order$/) {
  384. # adjust onhand
  385. &adj_onhand($dbh, $form, $ml * -1);
  386. &adj_inventory($dbh, $myconfig, $form);
  387. }
  388. my %audittrail = ( tablename => 'oe',
  389. reference => ($form->{type} =~ /_order$/) ? $form->{ordnumber} : $form->{quonumber},
  390. formname => $form->{type},
  391. action => 'saved',
  392. id => $form->{id} );
  393. $form->audittrail($dbh, "", \%audittrail);
  394. $form->save_recurring($dbh, $myconfig);
  395. my $rc = $dbh->commit;
  396. $dbh->disconnect;
  397. $rc;
  398. }
  399. sub delete {
  400. my ($self, $myconfig, $form, $spool) = @_;
  401. # connect to database
  402. my $dbh = $form->dbconnect_noauto($myconfig);
  403. # delete spool files
  404. my $query = qq|SELECT spoolfile FROM status
  405. WHERE trans_id = $form->{id}
  406. AND spoolfile IS NOT NULL|;
  407. $sth = $dbh->prepare($query);
  408. $sth->execute || $form->dberror($query);
  409. my $spoolfile;
  410. my @spoolfiles = ();
  411. while (($spoolfile) = $sth->fetchrow_array) {
  412. push @spoolfiles, $spoolfile;
  413. }
  414. $sth->finish;
  415. $query = qq|SELECT o.parts_id, o.ship, p.inventory_accno_id, p.assembly
  416. FROM orderitems o
  417. JOIN parts p ON (p.id = o.parts_id)
  418. WHERE trans_id = $form->{id}|;
  419. $sth = $dbh->prepare($query);
  420. $sth->execute || $form->dberror($query);
  421. if ($form->{type} =~ /_order$/) {
  422. $ml = ($form->{type} eq 'purchase_order') ? -1 : 1;
  423. while (my ($id, $ship, $inv, $assembly) = $sth->fetchrow_array) {
  424. $form->update_balance($dbh,
  425. "parts",
  426. "onhand",
  427. qq|id = $id|,
  428. $ship * $ml) if ($inv || $assembly);
  429. }
  430. }
  431. $sth->finish;
  432. # delete inventory
  433. $query = qq|DELETE FROM inventory
  434. WHERE trans_id = $form->{id}|;
  435. $dbh->do($query) || $form->dberror($query);
  436. # delete status entries
  437. $query = qq|DELETE FROM status
  438. WHERE trans_id = $form->{id}|;
  439. $dbh->do($query) || $form->dberror($query);
  440. # delete OE record
  441. $query = qq|DELETE FROM oe
  442. WHERE id = $form->{id}|;
  443. $dbh->do($query) || $form->dberror($query);
  444. # delete individual entries
  445. $query = qq|DELETE FROM orderitems
  446. WHERE trans_id = $form->{id}|;
  447. $dbh->do($query) || $form->dberror($query);
  448. $query = qq|DELETE FROM shipto
  449. WHERE trans_id = $form->{id}|;
  450. $dbh->do($query) || $form->dberror($query);
  451. my %audittrail = ( tablename => 'oe',
  452. reference => ($form->{type} =~ /_order$/) ? $form->{ordnumber} : $form->{quonumber},
  453. formname => $form->{type},
  454. action => 'deleted',
  455. id => $form->{id} );
  456. $form->audittrail($dbh, "", \%audittrail);
  457. my $rc = $dbh->commit;
  458. $dbh->disconnect;
  459. if ($rc) {
  460. foreach $spoolfile (@spoolfiles) {
  461. unlink "$spool/$spoolfile" if $spoolfile;
  462. }
  463. }
  464. $rc;
  465. }
  466. sub retrieve {
  467. my ($self, $myconfig, $form) = @_;
  468. # connect to database
  469. my $dbh = $form->dbconnect($myconfig);
  470. my $query;
  471. my $sth;
  472. my $var;
  473. my $ref;
  474. $query = qq|SELECT curr, current_date
  475. FROM defaults|;
  476. ($form->{currencies}, $form->{transdate}) = $dbh->selectrow_array($query);
  477. if ($form->{id}) {
  478. # retrieve order
  479. $query = qq|SELECT o.ordnumber, o.transdate, o.reqdate, o.terms,
  480. o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
  481. o.curr AS currency, e.name AS employee, o.employee_id,
  482. o.$form->{vc}_id, vc.name AS $form->{vc}, o.amount AS invtotal,
  483. o.closed, o.reqdate, o.quonumber, o.department_id,
  484. d.description AS department, o.language_code, o.ponumber
  485. FROM oe o
  486. JOIN $form->{vc} vc ON (o.$form->{vc}_id = vc.id)
  487. LEFT JOIN employee e ON (o.employee_id = e.id)
  488. LEFT JOIN department d ON (o.department_id = d.id)
  489. WHERE o.id = $form->{id}|;
  490. $sth = $dbh->prepare($query);
  491. $sth->execute || $form->dberror($query);
  492. $ref = $sth->fetchrow_hashref(NAME_lc);
  493. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  494. $sth->finish;
  495. $query = qq|SELECT * FROM shipto
  496. WHERE trans_id = $form->{id}|;
  497. $sth = $dbh->prepare($query);
  498. $sth->execute || $form->dberror($query);
  499. $ref = $sth->fetchrow_hashref(NAME_lc);
  500. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  501. $sth->finish;
  502. # get printed, emailed and queued
  503. $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
  504. FROM status s
  505. WHERE s.trans_id = $form->{id}|;
  506. $sth = $dbh->prepare($query);
  507. $sth->execute || $form->dberror($query);
  508. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  509. $form->{printed} .= "$ref->{formname} " if $ref->{printed};
  510. $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
  511. $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
  512. }
  513. $sth->finish;
  514. for (qw(printed emailed queued)) { $form->{$_} =~ s/ +$//g }
  515. my %oid = ( 'Pg' => 'id',
  516. 'Oracle' => 'rowid',
  517. 'DB2' => '1=1'
  518. );
  519. # retrieve individual items
  520. $query = qq|SELECT o.id AS orderitems_id,
  521. p.partnumber, p.assembly, o.description, o.qty,
  522. o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin,
  523. o.reqdate, o.project_id, o.ship, o.serialnumber, o.notes,
  524. pr.projectnumber,
  525. pg.partsgroup, p.partsgroup_id, p.partnumber AS sku,
  526. p.listprice, p.lastcost, p.weight, p.onhand,
  527. p.inventory_accno_id, p.income_accno_id, p.expense_accno_id,
  528. t.description AS partsgrouptranslation
  529. FROM orderitems o
  530. JOIN parts p ON (o.parts_id = p.id)
  531. LEFT JOIN project pr ON (o.project_id = pr.id)
  532. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  533. LEFT JOIN translation t ON (t.trans_id = p.partsgroup_id AND t.language_code = '$form->{language_code}')
  534. WHERE o.trans_id = $form->{id}
  535. ORDER BY o.$oid{$myconfig->{dbdriver}}|;
  536. $sth = $dbh->prepare($query);
  537. $sth->execute || $form->dberror($query);
  538. # foreign exchange rates
  539. &exchangerate_defaults($dbh, $form);
  540. # query for price matrix
  541. my $pmh = &price_matrix_query($dbh, $form);
  542. # taxes
  543. $query = qq|SELECT c.accno
  544. FROM chart c
  545. JOIN partstax pt ON (pt.chart_id = c.id)
  546. WHERE pt.parts_id = ?|;
  547. my $tth = $dbh->prepare($query) || $form->dberror($query);
  548. my $taxrate;
  549. my $ptref;
  550. my $sellprice;
  551. my $listprice;
  552. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  553. ($decimalplaces) = ($ref->{sellprice} =~ /\.(\d+)/);
  554. $decimalplaces = length $decimalplaces;
  555. $decimalplaces = ($decimalplaces > 2) ? $decimalplaces : 2;
  556. $tth->execute($ref->{id});
  557. $ref->{taxaccounts} = "";
  558. $taxrate = 0;
  559. while ($ptref = $tth->fetchrow_hashref(NAME_lc)) {
  560. $ref->{taxaccounts} .= "$ptref->{accno} ";
  561. $taxrate += $form->{"$ptref->{accno}_rate"};
  562. }
  563. $tth->finish;
  564. chop $ref->{taxaccounts};
  565. # preserve price
  566. $sellprice = $ref->{sellprice};
  567. # multiply by exchangerate
  568. $ref->{sellprice} = $form->round_amount($ref->{sellprice} * $form->{$form->{currency}}, $decimalplaces);
  569. for (qw(listprice lastcost)) { $ref->{$_} = $form->round_amount($ref->{$_} / $form->{$form->{currency}}, $decimalplaces) }
  570. # partnumber and price matrix
  571. &price_matrix($pmh, $ref, $form->{transdate}, $decimalplaces, $form, $myconfig);
  572. $ref->{sellprice} = $sellprice;
  573. $ref->{partsgroup} = $ref->{partsgrouptranslation} if $ref->{partsgrouptranslation};
  574. push @{ $form->{form_details} }, $ref;
  575. }
  576. $sth->finish;
  577. # get recurring transaction
  578. $form->get_recurring($dbh);
  579. } else {
  580. # get last name used
  581. $form->lastname_used($myconfig, $dbh, $form->{vc}) unless $form->{"$form->{vc}_id"};
  582. delete $form->{notes};
  583. }
  584. $dbh->disconnect;
  585. }
  586. sub price_matrix_query {
  587. my ($dbh, $form) = @_;
  588. my $query;
  589. my $sth;
  590. if ($form->{customer_id}) {
  591. $query = qq|SELECT p.id AS parts_id, 0 AS customer_id, 0 AS pricegroup_id,
  592. 0 AS pricebreak, p.sellprice, NULL AS validfrom, NULL AS validto,
  593. '$form->{defaultcurrency}' AS curr, '' AS pricegroup
  594. FROM parts p
  595. WHERE p.id = ?
  596. UNION
  597. SELECT p.*, g.pricegroup
  598. FROM partscustomer p
  599. LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
  600. WHERE p.parts_id = ?
  601. AND p.customer_id = $form->{customer_id}
  602. UNION
  603. SELECT p.*, g.pricegroup
  604. FROM partscustomer p
  605. LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
  606. JOIN customer c ON (c.pricegroup_id = g.id)
  607. WHERE p.parts_id = ?
  608. AND c.id = $form->{customer_id}
  609. UNION
  610. SELECT p.*, '' AS pricegroup
  611. FROM partscustomer p
  612. WHERE p.customer_id = 0
  613. AND p.pricegroup_id = 0
  614. AND p.parts_id = ?
  615. ORDER BY customer_id DESC, pricegroup_id DESC, pricebreak
  616. |;
  617. $sth = $dbh->prepare($query) || $form->dberror($query);
  618. }
  619. if ($form->{vendor_id}) {
  620. # price matrix and vendor's partnumber
  621. $query = qq|SELECT partnumber
  622. FROM partsvendor
  623. WHERE parts_id = ?
  624. AND vendor_id = $form->{vendor_id}|;
  625. $sth = $dbh->prepare($query) || $form->dberror($query);
  626. }
  627. $sth;
  628. }
  629. sub price_matrix {
  630. my ($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig) = @_;
  631. $ref->{pricematrix} = "";
  632. my $customerprice;
  633. my $pricegroupprice;
  634. my $sellprice;
  635. my $mref;
  636. my %p = ();
  637. # depends if this is a customer or vendor
  638. if ($form->{customer_id}) {
  639. $pmh->execute($ref->{id}, $ref->{id}, $ref->{id}, $ref->{id});
  640. while ($mref = $pmh->fetchrow_hashref(NAME_lc)) {
  641. # check date
  642. if ($mref->{validfrom}) {
  643. next if $transdate < $form->datetonum($myconfig, $mref->{validfrom});
  644. }
  645. if ($mref->{validto}) {
  646. next if $transdate > $form->datetonum($myconfig, $mref->{validto});
  647. }
  648. # convert price
  649. $sellprice = $form->round_amount($mref->{sellprice} * $form->{$mref->{curr}}, $decimalplaces);
  650. if ($mref->{customer_id}) {
  651. $ref->{sellprice} = $sellprice if !$mref->{pricebreak};
  652. $p{$mref->{pricebreak}} = $sellprice;
  653. $customerprice = 1;
  654. }
  655. if ($mref->{pricegroup_id}) {
  656. if (! $customerprice) {
  657. $ref->{sellprice} = $sellprice if !$mref->{pricebreak};
  658. $p{$mref->{pricebreak}} = $sellprice;
  659. }
  660. $pricegroupprice = 1;
  661. }
  662. if (!$customerprice && !$pricegroupprice) {
  663. $p{$mref->{pricebreak}} = $sellprice;
  664. }
  665. }
  666. $pmh->finish;
  667. if (%p) {
  668. if ($ref->{sellprice}) {
  669. $p{0} = $ref->{sellprice};
  670. }
  671. for (sort { $a <=> $b } keys %p) { $ref->{pricematrix} .= "${_}:$p{$_} " }
  672. } else {
  673. if ($init) {
  674. $ref->{sellprice} = $form->round_amount($ref->{sellprice}, $decimalplaces);
  675. } else {
  676. $ref->{sellprice} = $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), $decimalplaces);
  677. }
  678. $ref->{pricematrix} = "0:$ref->{sellprice} " if $ref->{sellprice};
  679. }
  680. chop $ref->{pricematrix};
  681. }
  682. if ($form->{vendor_id}) {
  683. $pmh->execute($ref->{id});
  684. $mref = $pmh->fetchrow_hashref(NAME_lc);
  685. if ($mref->{partnumber} ne "") {
  686. $ref->{partnumber} = $mref->{partnumber};
  687. }
  688. if ($mref->{lastcost}) {
  689. # do a conversion
  690. $ref->{sellprice} = $form->round_amount($mref->{lastcost} * $form->{$mref->{curr}}, $decimalplaces);
  691. }
  692. $pmh->finish;
  693. $ref->{sellprice} *= 1;
  694. # add 0:price to matrix
  695. $ref->{pricematrix} = "0:$ref->{sellprice}";
  696. }
  697. }
  698. sub exchangerate_defaults {
  699. my ($dbh, $form) = @_;
  700. my $var;
  701. my $buysell = ($form->{vc} eq "customer") ? "buy" : "sell";
  702. # get default currencies
  703. my $query = qq|SELECT substr(curr,1,3), curr FROM defaults|;
  704. ($form->{defaultcurrency}, $form->{currencies}) = $dbh->selectrow_array($query);
  705. $query = qq|SELECT $buysell
  706. FROM exchangerate
  707. WHERE curr = ?
  708. AND transdate = ?|;
  709. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  710. $query = qq~SELECT max(transdate || ' ' || $buysell || ' ' || curr)
  711. FROM exchangerate
  712. WHERE curr = ?~;
  713. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  714. # get exchange rates for transdate or max
  715. foreach $var (split /:/, substr($form->{currencies},4)) {
  716. $eth1->execute($var, $form->{transdate});
  717. ($form->{$var}) = $eth1->fetchrow_array;
  718. if (! $form->{$var} ) {
  719. $eth2->execute($var);
  720. ($form->{$var}) = $eth2->fetchrow_array;
  721. ($null, $form->{$var}) = split / /, $form->{$var};
  722. $form->{$var} = 1 unless $form->{$var};
  723. $eth2->finish;
  724. }
  725. $eth1->finish;
  726. }
  727. $form->{$form->{currency}} = $form->{exchangerate} if $form->{exchangerate};
  728. $form->{$form->{currency}} ||= 1;
  729. $form->{$form->{defaultcurrency}} = 1;
  730. }
  731. sub order_details {
  732. my ($self, $myconfig, $form) = @_;
  733. # connect to database
  734. my $dbh = $form->dbconnect($myconfig);
  735. my $query;
  736. my $sth;
  737. my $item;
  738. my $i;
  739. my @sortlist = ();
  740. my $projectnumber;
  741. my $projectdescription;
  742. my $projectnumber_id;
  743. my $translation;
  744. my $partsgroup;
  745. my %oid = ( 'Pg' => 'TRUE',
  746. 'PgPP' => 'TRUE',
  747. 'Oracle' => 'rowid',
  748. 'DB2' => '1=1'
  749. );
  750. my @taxaccounts;
  751. my %taxaccounts;
  752. my $tax;
  753. my $taxrate;
  754. my $taxamount;
  755. my %translations;
  756. $query = qq|SELECT p.description, t.description
  757. FROM project p
  758. LEFT JOIN translation t ON (t.trans_id = p.id AND t.language_code = '$form->{language_code}')
  759. WHERE id = ?|;
  760. my $prh = $dbh->prepare($query) || $form->dberror($query);
  761. $query = qq|SELECT inventory_accno_id, income_accno_id,
  762. expense_accno_id, assembly FROM parts
  763. WHERE id = ?|;
  764. my $pth = $dbh->prepare($query) || $form->dberror($query);
  765. my $sortby;
  766. # sort items by project and partsgroup
  767. for $i (1 .. $form->{rowcount}) {
  768. if ($form->{"id_$i"}) {
  769. # account numbers
  770. $pth->execute($form->{"id_$i"});
  771. $ref = $pth->fetchrow_hashref(NAME_lc);
  772. for (keys %$ref) { $form->{"${_}_$i"} = $ref->{$_} }
  773. $pth->finish;
  774. $projectnumber_id = 0;
  775. $projectnumber = "";
  776. $form->{partsgroup} = "";
  777. $form->{projectnumber} = "";
  778. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  779. $inventory_accno_id = ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) ? "1" : "";
  780. if ($form->{groupprojectnumber}) {
  781. ($projectnumber, $projectnumber_id) = split /--/, $form->{"projectnumber_$i"};
  782. }
  783. if ($form->{grouppartsgroup}) {
  784. ($form->{partsgroup}) = split /--/, $form->{"partsgroup_$i"};
  785. }
  786. if ($projectnumber_id && $form->{groupprojectnumber}) {
  787. if ($translation{$projectnumber_id}) {
  788. $form->{projectnumber} = $translation{$projectnumber_id};
  789. } else {
  790. # get project description
  791. $prh->execute($projectnumber_id);
  792. ($projectdescription, $translation) = $prh->fetchrow_array;
  793. $prh->finish;
  794. $form->{projectnumber} = ($translation) ? "$projectnumber, $translation" : "$projectnumber, $projectdescription";
  795. $translation{$projectnumber_id} = $form->{projectnumber};
  796. }
  797. }
  798. if ($form->{grouppartsgroup} && $form->{partsgroup}) {
  799. $form->{projectnumber} .= " / " if $projectnumber_id;
  800. $form->{projectnumber} .= $form->{partsgroup};
  801. }
  802. $form->format_string(projectnumber);
  803. }
  804. $sortby = qq|$projectnumber$form->{partsgroup}|;
  805. if ($form->{sortby} ne 'runningnumber') {
  806. for (qw(partnumber description bin)) {
  807. $sortby .= $form->{"${_}_$i"} if $form->{sortby} eq $_;
  808. }
  809. }
  810. push @sortlist, [ $i, qq|$projectnumber$form->{partsgroup}$inventory_accno_id|, $form->{projectnumber}, $projectnumber_id, $form->{partsgroup}, $sortby ];
  811. }
  812. }
  813. delete $form->{projectnumber};
  814. # sort the whole thing by project and group
  815. @sortlist = sort { $a->[5] cmp $b->[5] } @sortlist;
  816. # if there is a warehouse limit picking
  817. if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
  818. # run query to check for inventory
  819. $query = qq|SELECT sum(qty) AS qty
  820. FROM inventory
  821. WHERE parts_id = ?
  822. AND warehouse_id = ?|;
  823. $sth = $dbh->prepare($query) || $form->dberror($query);
  824. for $i (1 .. $form->{rowcount}) {
  825. $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
  826. ($qty) = $sth->fetchrow_array;
  827. $sth->finish;
  828. $form->{"qty_$i"} = 0 if $qty == 0;
  829. if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
  830. $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
  831. }
  832. }
  833. }
  834. my $runningnumber = 1;
  835. my $sameitem = "";
  836. my $subtotal;
  837. my $k = scalar @sortlist;
  838. my $j = 0;
  839. foreach $item (@sortlist) {
  840. $i = $item->[0];
  841. $j++;
  842. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  843. if ($item->[1] ne $sameitem) {
  844. $sameitem = $item->[1];
  845. $ok = 0;
  846. if ($form->{groupprojectnumber}) {
  847. $ok = $form->{"projectnumber_$i"};
  848. }
  849. if ($form->{grouppartsgroup}) {
  850. $ok = $form->{"partsgroup_$i"} unless $ok;
  851. }
  852. if ($ok) {
  853. if ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) {
  854. push(@{ $form->{part} }, "");
  855. push(@{ $form->{service} }, NULL);
  856. } else {
  857. push(@{ $form->{part} }, NULL);
  858. push(@{ $form->{service} }, "");
  859. }
  860. push(@{ $form->{description} }, $item->[2]);
  861. for (qw(taxrates runningnumber number sku qty ship unit bin serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate linetotal weight itemnotes)) { push(@{ $form->{$_} }, "") }
  862. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  863. }
  864. }
  865. }
  866. $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
  867. $form->{"ship_$i"} = $form->parse_amount($myconfig, $form->{"ship_$i"});
  868. if ($form->{"qty_$i"}) {
  869. $form->{totalqty} += $form->{"qty_$i"};
  870. $form->{totalship} += $form->{"ship_$i"};
  871. $form->{totalweight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
  872. $form->{totalweightship} += ($form->{"weight_$i"} * $form->{"ship_$i"});
  873. # add number, description and qty to $form->{number}, ....
  874. push(@{ $form->{runningnumber} }, $runningnumber++);
  875. push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
  876. push(@{ $form->{sku} }, qq|$form->{"sku_$i"}|);
  877. push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
  878. push(@{ $form->{itemnotes} }, $form->{"notes_$i"});
  879. push(@{ $form->{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"}));
  880. push(@{ $form->{ship} }, $form->format_amount($myconfig, $form->{"ship_$i"}));
  881. push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
  882. push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
  883. push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
  884. push(@{ $form->{requiredate} }, qq|$form->{"reqdate_$i"}|);
  885. push(@{ $form->{projectnumber} }, qq|$form->{"projectnumber_$i"}|);
  886. push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
  887. push(@{ $form->{listprice} }, $form->{"listprice_$i"});
  888. push(@{ $form->{weight} }, $form->format_amount($myconfig, $form->{"weight_$i"} * $form->{"ship_$i"}));
  889. my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
  890. my ($dec) = ($sellprice =~ /\.(\d+)/);
  891. $dec = length $dec;
  892. my $decimalplaces = ($dec > 2) ? $dec : 2;
  893. my $discount = $form->round_amount($sellprice * $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100, $decimalplaces);
  894. # keep a netprice as well, (sellprice - discount)
  895. $form->{"netprice_$i"} = $sellprice - $discount;
  896. my $linetotal = $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
  897. if ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) {
  898. push(@{ $form->{part} }, $form->{"sku_$i"});
  899. push(@{ $form->{service} }, NULL);
  900. $form->{totalparts} += $linetotal;
  901. } else {
  902. push(@{ $form->{service} }, $form->{"sku_$i"});
  903. push(@{ $form->{part} }, NULL);
  904. $form->{totalservices} += $linetotal;
  905. }
  906. push(@{ $form->{netprice} }, ($form->{"netprice_$i"}) ? $form->format_amount($myconfig, $form->{"netprice_$i"}, $decimalplaces) : " ");
  907. $discount = ($discount) ? $form->format_amount($myconfig, $discount * -1, $decimalplaces) : " ";
  908. push(@{ $form->{discount} }, $discount);
  909. push(@{ $form->{discountrate} }, $form->format_amount($myconfig, $form->{"discount_$i"}));
  910. $form->{ordtotal} += $linetotal;
  911. # this is for the subtotals for grouping
  912. $subtotal += $linetotal;
  913. $form->{"linetotal_$i"} = $form->format_amount($myconfig, $linetotal, 2);
  914. push(@{ $form->{linetotal} }, $form->{"linetotal_$i"});
  915. @taxaccounts = split / /, $form->{"taxaccounts_$i"};
  916. my $ml = 1;
  917. my @taxrates = ();
  918. $tax = 0;
  919. for (0 .. 1) {
  920. $taxrate = 0;
  921. for (@taxaccounts) { $taxrate += $form->{"${_}_rate"} if ($form->{"${_}_rate"} * $ml) > 0 }
  922. $taxrate *= $ml;
  923. $taxamount = $linetotal * $taxrate / (1 + $taxrate);
  924. $taxbase = ($linetotal - $taxamount);
  925. foreach $item (@taxaccounts) {
  926. if (($form->{"${item}_rate"} * $ml) > 0) {
  927. push @taxrates, $form->{"${item}_rate"} * 100;
  928. if ($form->{taxincluded}) {
  929. $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"} / (1 + $taxrate);
  930. $taxbase{$item} += $taxbase;
  931. } else {
  932. $taxbase{$item} += $linetotal;
  933. $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
  934. }
  935. }
  936. }
  937. if ($form->{taxincluded}) {
  938. $tax += $linetotal * ($taxrate / (1 + ($taxrate * $ml)));
  939. } else {
  940. $tax += $linetotal * $taxrate;
  941. }
  942. $ml *= -1;
  943. }
  944. push(@{ $form->{lineitems} }, { amount => $linetotal, tax => $form->round_amount($tax, 2) });
  945. push(@{ $form->{taxrates} }, join ' ', sort { $a <=> $b } @taxrates);
  946. if ($form->{"assembly_$i"}) {
  947. $form->{stagger} = -1;
  948. &assembly_details($myconfig, $form, $dbh, $form->{"id_$i"}, $oid{$myconfig->{dbdriver}}, $form->{"qty_$i"});
  949. }
  950. }
  951. # add subtotal
  952. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  953. if ($subtotal) {
  954. if ($j < $k) {
  955. # look at next item
  956. if ($sortlist[$j]->[1] ne $sameitem) {
  957. if ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) {
  958. push(@{ $form->{part} }, "");
  959. push(@{ $form->{service} }, NULL);
  960. } else {
  961. push(@{ $form->{service} }, "");
  962. push(@{ $form->{part} }, NULL);
  963. }
  964. for (qw(taxrates runningnumber number sku qty ship unit bin serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate weight itemnotes)) { push(@{ $form->{$_} }, "") }
  965. push(@{ $form->{description} }, $form->{groupsubtotaldescription});
  966. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  967. if ($form->{groupsubtotaldescription} ne "") {
  968. push(@{ $form->{linetotal} }, $form->format_amount($myconfig, $subtotal, 2));
  969. } else {
  970. push(@{ $form->{linetotal} }, "");
  971. }
  972. $subtotal = 0;
  973. }
  974. } else {
  975. # got last item
  976. if ($form->{groupsubtotaldescription} ne "") {
  977. if ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) {
  978. push(@{ $form->{part} }, "");
  979. push(@{ $form->{service} }, NULL);
  980. } else {
  981. push(@{ $form->{service} }, "");
  982. push(@{ $form->{part} }, NULL);
  983. }
  984. for (qw(taxrates runningnumber number sku qty ship unit bin serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate weight itemnotes)) { push(@{ $form->{$_} }, "") }
  985. push(@{ $form->{description} }, $form->{groupsubtotaldescription});
  986. push(@{ $form->{linetotal} }, $form->format_amount($myconfig, $subtotal, 2));
  987. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  988. }
  989. }
  990. }
  991. }
  992. }
  993. $tax = 0;
  994. foreach $item (sort keys %taxaccounts) {
  995. if ($form->round_amount($taxaccounts{$item}, 2)) {
  996. $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
  997. push(@{ $form->{taxbaseinclusive} }, $form->{"${item}_taxbaseinclusive"} = $form->round_amount($taxbase{$item} + $tax, 2));
  998. push(@{ $form->{taxbase} }, $form->{"${item}_taxbase"} = $form->format_amount($myconfig, $taxbase{$item}, 2));
  999. push(@{ $form->{tax} }, $form->{"${item}_tax"} = $form->format_amount($myconfig, $taxamount, 2));
  1000. push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
  1001. $form->{"${item}_taxrate"} = $form->format_amount($myconfig, $form->{"${item}_rate"} * 100);
  1002. push(@{ $form->{taxrate} }, $form->{"${item}_taxrate"});
  1003. push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
  1004. }
  1005. }
  1006. # adjust taxes for lineitems
  1007. my $total = 0;
  1008. for (@{ $form->{lineitems} }) {
  1009. $total += $_->{tax};
  1010. }
  1011. if ($form->round_amount($total,2) != $form->round_amount($tax,2)) {
  1012. # get largest amount
  1013. for (reverse sort { $a->{tax} <=> $b->{tax} } @{ $form->{lineitems} }) {
  1014. $_->{tax} -= $total - $tax;
  1015. last;
  1016. }
  1017. }
  1018. $i = 1;
  1019. for (@{ $form->{lineitems} }) {
  1020. push(@{ $form->{linetax} }, $form->format_amount($myconfig, $_->{tax}, 2, ""));
  1021. }
  1022. for (qw(totalparts totalservices)) { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) }
  1023. for (qw(totalqty totalship totalweight)) { $form->{$_} = $form->format_amount($myconfig, $form->{$_}) }
  1024. $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
  1025. $form->{ordtotal} = ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
  1026. use LedgerSMB::CP;
  1027. my $c;
  1028. if ($form->{language_code} ne "") {
  1029. $c = new CP $form->{language_code};
  1030. } else {
  1031. $c = new CP $myconfig->{countrycode};
  1032. }
  1033. $c->init;
  1034. my $whole;
  1035. ($whole, $form->{decimal}) = split /\./, $form->{ordtotal};
  1036. $form->{decimal} .= "00";
  1037. $form->{decimal} = substr($form->{decimal}, 0, 2);
  1038. $form->{text_decimal} = $c->num2text($form->{decimal} * 1);
  1039. $form->{text_amount} = $c->num2text($whole);
  1040. $form->{integer_amount} = $form->format_amount($myconfig, $whole);
  1041. # format amounts
  1042. $form->{quototal} = $form->{ordtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
  1043. $form->format_string(qw(text_amount text_decimal));
  1044. $query = qq|SELECT weightunit FROM defaults|;
  1045. ($form->{weightunit}) = $dbh->selectrow_array($query);
  1046. $dbh->disconnect;
  1047. }
  1048. sub assembly_details {
  1049. my ($myconfig, $form, $dbh, $id, $oid, $qty) = @_;
  1050. my $sm = "";
  1051. my $spacer;
  1052. $form->{stagger}++;
  1053. if ($form->{format} eq 'html') {
  1054. $spacer = "&nbsp;" x (3 * ($form->{stagger} - 1)) if $form->{stagger} > 1;
  1055. }
  1056. if ($form->{format} =~ /(postscript|pdf)/) {
  1057. if ($form->{stagger} > 1) {
  1058. $spacer = ($form->{stagger} - 1) * 3;
  1059. $spacer = '\rule{'.$spacer.'mm}{0mm}';
  1060. }
  1061. }
  1062. # get parts and push them onto the stack
  1063. my $sortorder = "";
  1064. if ($form->{grouppartsgroup}) {
  1065. $sortorder = qq|ORDER BY pg.partsgroup, a.$oid|;
  1066. } else {
  1067. $sortorder = qq|ORDER BY a.$oid|;
  1068. }
  1069. my $where = ($form->{formname} eq 'work_order') ? "1 = 1" : "a.bom = '1'";
  1070. my $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
  1071. pg.partsgroup, p.partnumber AS sku, p.assembly, p.id, p.bin
  1072. FROM assembly a
  1073. JOIN parts p ON (a.parts_id = p.id)
  1074. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1075. WHERE $where
  1076. AND a.id = '$id'
  1077. $sortorder|;
  1078. my $sth = $dbh->prepare($query);
  1079. $sth->execute || $form->dberror($query);
  1080. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1081. for (qw(partnumber description partsgroup)) {
  1082. $form->{"a_$_"} = $ref->{$_};
  1083. $form->format_string("a_$_");
  1084. }
  1085. if ($form->{grouppartsgroup} && $ref->{partsgroup} ne $sm) {
  1086. for (qw(taxrates number sku unit qty runningnumber ship bin serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate linetotal weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1087. $sm = ($form->{"a_partsgroup"}) ? $form->{"a_partsgroup"} : "";
  1088. push(@{ $form->{description} }, "$spacer$sm");
  1089. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  1090. }
  1091. if ($form->{stagger}) {
  1092. push(@{ $form->{description} }, qq|$spacer$form->{"a_partnumber"}, $form->{"a_description"}|);
  1093. for (qw(taxrates number sku runningnumber ship serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate linetotal weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1094. } else {
  1095. push(@{ $form->{description} }, qq|$form->{"a_description"}|);
  1096. push(@{ $form->{sku} }, $form->{"a_partnumber"});
  1097. push(@{ $form->{number} }, $form->{"a_partnumber"});
  1098. for (qw(taxrates runningnumber ship serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate linetotal weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1099. }
  1100. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  1101. push(@{ $form->{qty} }, $form->format_amount($myconfig, $ref->{qty} * $qty));
  1102. for (qw(unit bin)) {
  1103. $form->{"a_$_"} = $ref->{$_};
  1104. $form->format_string("a_$_");
  1105. push(@{ $form->{$_} }, $form->{"a_$_"});
  1106. }
  1107. if ($ref->{assembly} && $form->{formname} eq 'work_order') {
  1108. &assembly_details($myconfig, $form, $dbh, $ref->{id}, $oid, $ref->{qty} * $qty);
  1109. }
  1110. }
  1111. $sth->finish;
  1112. $form->{stagger}--;
  1113. }
  1114. sub project_description {
  1115. my ($self, $dbh, $id) = @_;
  1116. my $query = qq|SELECT description
  1117. FROM project
  1118. WHERE id = $id|;
  1119. ($_) = $dbh->selectrow_array($query);
  1120. $_;
  1121. }
  1122. sub get_warehouses {
  1123. my ($self, $myconfig, $form) = @_;
  1124. my $dbh = $form->dbconnect($myconfig);
  1125. # setup warehouses
  1126. my $query = qq|SELECT id, description
  1127. FROM warehouse
  1128. ORDER BY 2|;
  1129. my $sth = $dbh->prepare($query);
  1130. $sth->execute || $form->dberror($query);
  1131. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1132. push @{ $form->{all_warehouse} }, $ref;
  1133. }
  1134. $sth->finish;
  1135. $dbh->disconnect;
  1136. }
  1137. sub save_inventory {
  1138. my ($self, $myconfig, $form) = @_;
  1139. my ($null, $warehouse_id) = split /--/, $form->{warehouse};
  1140. $warehouse_id *= 1;
  1141. my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
  1142. my $dbh = $form->dbconnect_noauto($myconfig);
  1143. my $sth;
  1144. my $wth;
  1145. my $serialnumber;
  1146. my $ship;
  1147. my ($null, $employee_id) = split /--/, $form->{employee};
  1148. ($null, $employee_id) = $form->get_employee($dbh) if ! $employee_id;
  1149. $query = qq|SELECT serialnumber, ship
  1150. FROM orderitems
  1151. WHERE trans_id = ?
  1152. AND id = ?
  1153. FOR UPDATE|;
  1154. $sth = $dbh->prepare($query) || $form->dberror($query);
  1155. $query = qq|SELECT sum(qty)
  1156. FROM inventory
  1157. WHERE parts_id = ?
  1158. AND warehouse_id = ?|;
  1159. $wth = $dbh->prepare($query) || $form->dberror($query);
  1160. for my $i (1 .. $form->{rowcount}) {
  1161. $ship = (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"})) ? $form->{"qty_$i"} : $form->{"ship_$i"};
  1162. if ($warehouse_id && $form->{type} eq 'ship_order') {
  1163. $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
  1164. ($qty) = $wth->fetchrow_array;
  1165. $wth->finish;
  1166. if ($ship > $qty) {
  1167. $ship = $qty;
  1168. }
  1169. }
  1170. if ($ship) {
  1171. $ship *= $ml;
  1172. $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
  1173. qty, trans_id, orderitems_id, shippingdate, employee_id)
  1174. VALUES ($form->{"id_$i"}, $warehouse_id,
  1175. $ship, $form->{"id"},
  1176. $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
  1177. $employee_id)|;
  1178. $dbh->do($query) || $form->dberror($query);
  1179. # add serialnumber, ship to orderitems
  1180. $sth->execute($form->{id}, $form->{"orderitems_id_$i"}) || $form->dberror;
  1181. ($serialnumber, $ship) = $sth->fetchrow_array;
  1182. $sth->finish;
  1183. $serialnumber .= " " if $serialnumber;
  1184. $serialnumber .= qq|$form->{"serialnumber_$i"}|;
  1185. $ship += $form->{"ship_$i"};
  1186. $query = qq|UPDATE orderitems SET
  1187. serialnumber = '$serialnumber',
  1188. ship = $ship,
  1189. reqdate = '$form->{shippingdate}'
  1190. WHERE trans_id = $form->{id}
  1191. AND id = $form->{"orderitems_id_$i"}|;
  1192. $dbh->do($query) || $form->dberror($query);
  1193. # update order with ship via
  1194. $query = qq|UPDATE oe SET
  1195. shippingpoint = |.$dbh->quote($form->{shippingpoint}).qq|,
  1196. shipvia = |.$dbh->quote($form->{shipvia}).qq|
  1197. WHERE id = $form->{id}|;
  1198. $dbh->do($query) || $form->dberror($query);
  1199. # update onhand for parts
  1200. $form->update_balance($dbh,
  1201. "parts",
  1202. "onhand",
  1203. qq|id = $form->{"id_$i"}|,
  1204. $form->{"ship_$i"} * $ml);
  1205. }
  1206. }
  1207. my $rc = $dbh->commit;
  1208. $dbh->disconnect;
  1209. $rc;
  1210. }
  1211. sub adj_onhand {
  1212. my ($dbh, $form, $ml) = @_;
  1213. my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
  1214. FROM orderitems oi
  1215. JOIN parts p ON (p.id = oi.parts_id)
  1216. WHERE oi.trans_id = $form->{id}|;
  1217. my $sth = $dbh->prepare($query);
  1218. $sth->execute || $form->dberror($query);
  1219. $query = qq|SELECT sum(p.inventory_accno_id), p.assembly
  1220. FROM parts p
  1221. JOIN assembly a ON (a.parts_id = p.id)
  1222. WHERE a.id = ?
  1223. GROUP BY p.assembly|;
  1224. my $ath = $dbh->prepare($query) || $form->dberror($query);
  1225. my $ref;
  1226. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1227. if ($ref->{inventory_accno_id} || $ref->{assembly}) {
  1228. # do not update if assembly consists of all services
  1229. if ($ref->{assembly}) {
  1230. $ath->execute($ref->{parts_id}) || $form->dberror($query);
  1231. my ($inv, $assembly) = $ath->fetchrow_array;
  1232. $ath->finish;
  1233. next unless ($inv || $assembly);
  1234. }
  1235. # adjust onhand in parts table
  1236. $form->update_balance($dbh,
  1237. "parts",
  1238. "onhand",
  1239. qq|id = $ref->{parts_id}|,
  1240. $ref->{ship} * $ml);
  1241. }
  1242. }
  1243. $sth->finish;
  1244. }
  1245. sub adj_inventory {
  1246. my ($dbh, $myconfig, $form) = @_;
  1247. my %oid = ( 'Pg' => 'id',
  1248. 'PgPP' => 'id',
  1249. 'Oracle' => 'rowid',
  1250. 'DB2' => '1=1'
  1251. );
  1252. # increase/reduce qty in inventory table
  1253. my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
  1254. FROM orderitems oi
  1255. WHERE oi.trans_id = $form->{id}|;
  1256. my $sth = $dbh->prepare($query);
  1257. $sth->execute || $form->dberror($query);
  1258. $query = qq|SELECT qty,
  1259. (SELECT SUM(qty) FROM inventory
  1260. WHERE trans_id = $form->{id}
  1261. AND orderitems_id = ?) AS total
  1262. FROM inventory
  1263. WHERE trans_id = $form->{id}
  1264. AND orderitems_id = ?|;
  1265. my $ith = $dbh->prepare($query) || $form->dberror($query);
  1266. my $qty;
  1267. my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
  1268. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1269. $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
  1270. my $ship = $ref->{ship};
  1271. while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
  1272. if (($qty = (($inv->{total} * $ml) - $ship)) >= 0) {
  1273. $qty = $inv->{qty} * $ml if ($qty > ($inv->{qty} * $ml));
  1274. $form->update_balance($dbh,
  1275. "inventory",
  1276. "qty",
  1277. qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
  1278. $qty * -1 * $ml);
  1279. $ship -= $qty;
  1280. }
  1281. }
  1282. $ith->finish;
  1283. }
  1284. $sth->finish;
  1285. # delete inventory entries if qty = 0
  1286. $query = qq|DELETE FROM inventory
  1287. WHERE trans_id = $form->{id}
  1288. AND qty = 0|;
  1289. $dbh->do($query) || $form->dberror($query);
  1290. }
  1291. sub get_inventory {
  1292. my ($self, $myconfig, $form) = @_;
  1293. my $where;
  1294. my $query;
  1295. my $null;
  1296. my $fromwarehouse_id;
  1297. my $towarehouse_id;
  1298. my $var;
  1299. my $dbh = $form->dbconnect($myconfig);
  1300. if ($form->{partnumber} ne "") {
  1301. $var = $form->like(lc $form->{partnumber});
  1302. $where .= "
  1303. AND lower(p.partnumber) LIKE '$var'";
  1304. }
  1305. if ($form->{description} ne "") {
  1306. $var = $form->like(lc $form->{description});
  1307. $where .= "
  1308. AND lower(p.description) LIKE '$var'";
  1309. }
  1310. if ($form->{partsgroup} ne "") {
  1311. ($null, $var) = split /--/, $form->{partsgroup};
  1312. $where .= "
  1313. AND pg.id = $var";
  1314. }
  1315. ($null, $fromwarehouse_id) = split /--/, $form->{fromwarehouse};
  1316. $fromwarehouse_id *= 1;
  1317. ($null, $towarehouse_id) = split /--/, $form->{towarehouse};
  1318. $towarehouse_id *= 1;
  1319. my %ordinal = ( partnumber => 2,
  1320. description => 3,
  1321. partsgroup => 5,
  1322. warehouse => 6,
  1323. );
  1324. my @a = (partnumber, warehouse);
  1325. my $sortorder = $form->sort_order(\@a, \%ordinal);
  1326. if ($fromwarehouse_id) {
  1327. if ($towarehouse_id) {
  1328. $where .= "
  1329. AND NOT i.warehouse_id = $towarehouse_id";
  1330. }
  1331. $query = qq|SELECT p.id, p.partnumber, p.description,
  1332. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1333. pg.partsgroup, w.description AS warehouse, i.warehouse_id
  1334. FROM inventory i
  1335. JOIN parts p ON (p.id = i.parts_id)
  1336. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1337. JOIN warehouse w ON (w.id = i.warehouse_id)
  1338. WHERE i.warehouse_id = $fromwarehouse_id
  1339. $where
  1340. GROUP BY p.id, p.partnumber, p.description, pg.partsgroup, w.description, i.warehouse_id
  1341. ORDER BY $sortorder|;
  1342. } else {
  1343. if ($towarehouse_id) {
  1344. $query = qq|
  1345. SELECT p.id, p.partnumber, p.description,
  1346. p.onhand, (SELECT SUM(qty) FROM inventory i WHERE i.parts_id = p.id) AS qty,
  1347. pg.partsgroup, '' AS warehouse, 0 AS warehouse_id
  1348. FROM parts p
  1349. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1350. WHERE p.onhand > 0
  1351. $where
  1352. UNION|;
  1353. }
  1354. $query .= qq|
  1355. SELECT p.id, p.partnumber, p.description,
  1356. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1357. pg.partsgroup, w.description AS warehouse, i.warehouse_id
  1358. FROM inventory i
  1359. JOIN parts p ON (p.id = i.parts_id)
  1360. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1361. JOIN warehouse w ON (w.id = i.warehouse_id)
  1362. WHERE i.warehouse_id != $towarehouse_id
  1363. $where
  1364. GROUP BY p.id, p.partnumber, p.description, pg.partsgroup, w.description, i.warehouse_id
  1365. ORDER BY $sortorder|;
  1366. }
  1367. my $sth = $dbh->prepare($query);
  1368. $sth->execute || $form->dberror($query);
  1369. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1370. $ref->{qty} = $ref->{onhand} - $ref->{qty};
  1371. push @{ $form->{all_inventory} }, $ref if $ref->{qty} > 0;
  1372. }
  1373. $sth->finish;
  1374. $dbh->disconnect;
  1375. }
  1376. sub transfer {
  1377. my ($self, $myconfig, $form) = @_;
  1378. my $dbh = $form->dbconnect_noauto($myconfig);
  1379. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
  1380. my @a = localtime;
  1381. $a[5] += 1900;
  1382. $a[4]++;
  1383. $a[4] = substr("0$a[4]", -2);
  1384. $a[3] = substr("0$a[3]", -2);
  1385. $shippingdate = "$a[5]$a[4]$a[3]";
  1386. my %total = ();
  1387. my $query = qq|INSERT INTO inventory
  1388. (warehouse_id, parts_id, qty, shippingdate, employee_id)
  1389. VALUES (?, ?, ?, '$shippingdate', $form->{employee_id})|;
  1390. $sth = $dbh->prepare($query) || $form->dberror($query);
  1391. my $qty;
  1392. for my $i (1 .. $form->{rowcount}) {
  1393. $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
  1394. $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
  1395. if ($qty > 0) {
  1396. # to warehouse
  1397. if ($form->{warehouse_id}) {
  1398. $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty) || $form->dberror;
  1399. $sth->finish;
  1400. }
  1401. # from warehouse
  1402. if ($form->{"warehouse_id_$i"}) {
  1403. $sth->execute($form->{"warehouse_id_$i"}, $form->{"id_$i"}, $qty * -1) || $form->dberror;
  1404. $sth->finish;
  1405. }
  1406. }
  1407. }
  1408. my $rc = $dbh->commit;
  1409. $dbh->disconnect;
  1410. $rc;
  1411. }
  1412. sub get_soparts {
  1413. my ($self, $myconfig, $form) = @_;
  1414. # connect to database
  1415. my $dbh = $form->dbconnect($myconfig);
  1416. my $id;
  1417. my $ref;
  1418. # store required items from selected sales orders
  1419. my $query = qq|SELECT p.id, oi.qty - oi.ship AS required,
  1420. p.assembly
  1421. FROM orderitems oi
  1422. JOIN parts p ON (p.id = oi.parts_id)
  1423. WHERE oi.trans_id = ?|;
  1424. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1425. for (my $i = 1; $i <= $form->{rowcount}; $i++) {
  1426. if ($form->{"ndx_$i"}) {
  1427. $sth->execute($form->{"ndx_$i"});
  1428. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1429. &add_items_required("", $dbh, $form, $ref->{id}, $ref->{required}, $ref->{assembly});
  1430. }
  1431. $sth->finish;
  1432. }
  1433. }
  1434. $query = qq|SELECT current_date FROM defaults|;
  1435. ($form->{transdate}) = $dbh->selectrow_array($query);
  1436. # foreign exchange rates
  1437. &exchangerate_defaults($dbh, $form);
  1438. $dbh->disconnect;
  1439. }
  1440. sub add_items_required {
  1441. my ($self, $dbh, $form, $parts_id, $required, $assembly) = @_;
  1442. my $query;
  1443. my $sth;
  1444. my $ref;
  1445. if ($assembly) {
  1446. $query = qq|SELECT p.id, a.qty, p.assembly
  1447. FROM assembly a
  1448. JOIN parts p ON (p.id = a.parts_id)
  1449. WHERE a.id = $parts_id|;
  1450. $sth = $dbh->prepare($query);
  1451. $sth->execute || $form->dberror($query);
  1452. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1453. &add_items_required("", $dbh, $form, $ref->{id}, $required * $ref->{qty}, $ref->{assembly});
  1454. }
  1455. $sth->finish;
  1456. } else {
  1457. $query = qq|SELECT partnumber, description, lastcost
  1458. FROM parts
  1459. WHERE id = $parts_id|;
  1460. $sth = $dbh->prepare($query);
  1461. $sth->execute || $form->dberror($query);
  1462. $ref = $sth->fetchrow_hashref(NAME_lc);
  1463. for (keys %$ref) { $form->{orderitems}{$parts_id}{$_} = $ref->{$_} }
  1464. $sth->finish;
  1465. $form->{orderitems}{$parts_id}{required} += $required;
  1466. $query = qq|SELECT pv.partnumber, pv.leadtime, pv.lastcost, pv.curr,
  1467. pv.vendor_id, v.name
  1468. FROM partsvendor pv
  1469. JOIN vendor v ON (v.id = pv.vendor_id)
  1470. WHERE pv.parts_id = ?|;
  1471. $sth = $dbh->prepare($query) || $form->dberror($query);
  1472. # get cost and vendor
  1473. $sth->execute($parts_id);
  1474. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1475. for (keys %$ref) { $form->{orderitems}{$parts_id}{partsvendor}{$ref->{vendor_id}}{$_} = $ref->{$_} }
  1476. }
  1477. $sth->finish;
  1478. }
  1479. }
  1480. sub generate_orders {
  1481. my ($self, $myconfig, $form) = @_;
  1482. my $i;
  1483. my %a;
  1484. my $query;
  1485. my $sth;
  1486. for ($i = 1; $i <= $form->{rowcount}; $i++) {
  1487. for (qw(qty lastcost)) { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) }
  1488. if ($form->{"qty_$i"}) {
  1489. ($vendor, $vendor_id) = split /--/, $form->{"vendor_$i"};
  1490. if ($vendor_id) {
  1491. $a{$vendor_id}{$form->{"id_$i"}}{qty} += $form->{"qty_$i"};
  1492. for (qw(curr lastcost)) { $a{$vendor_id}{$form->{"id_$i"}}{$_} = $form->{"${_}_$i"} }
  1493. }
  1494. }
  1495. }
  1496. # connect to database
  1497. my $dbh = $form->dbconnect_noauto($myconfig);
  1498. # foreign exchange rates
  1499. &exchangerate_defaults($dbh, $form);
  1500. my $amount;
  1501. my $netamount;
  1502. my $curr = "";
  1503. my %tax;
  1504. my $taxincluded = 0;
  1505. my $vendor_id;
  1506. my $description;
  1507. my $unit;
  1508. my $sellprice;
  1509. foreach $vendor_id (keys %a) {
  1510. %tax = ();
  1511. $query = qq|SELECT v.curr, v.taxincluded, t.rate, c.accno
  1512. FROM vendor v
  1513. LEFT JOIN vendortax vt ON (v.id = vt.vendor_id)
  1514. LEFT JOIN tax t ON (t.chart_id = vt.chart_id)
  1515. LEFT JOIN chart c ON (c.id = t.chart_id)
  1516. WHERE v.id = $vendor_id|;
  1517. $sth = $dbh->prepare($query);
  1518. $sth->execute || $form->dberror($query);
  1519. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1520. $curr = $ref->{curr};
  1521. $taxincluded = $ref->{taxincluded};
  1522. $tax{$ref->{accno}} = $ref->{rate};
  1523. }
  1524. $sth->finish;
  1525. $curr ||= $form->{defaultcurrency};
  1526. $taxincluded *= 1;
  1527. my $uid = localtime;
  1528. $uid .= "$$";
  1529. $query = qq|INSERT INTO oe (ordnumber)
  1530. VALUES ('$uid')|;
  1531. $dbh->do($query) || $form->dberror($query);
  1532. $query = qq|SELECT id FROM oe
  1533. WHERE ordnumber = '$uid'|;
  1534. $sth = $dbh->prepare($query);
  1535. $sth->execute || $form->dberror($query);
  1536. my ($id) = $sth->fetchrow_array;
  1537. $sth->finish;
  1538. $amount = 0;
  1539. $netamount = 0;
  1540. foreach my $parts_id (keys %{ $a{$vendor_id} }) {
  1541. if (($form->{$curr} * $form->{$a{$vendor_id}{$parts_id}{curr}}) > 0) {
  1542. $sellprice = $a{$vendor_id}{$parts_id}{lastcost} / $form->{$curr} * $form->{$a{$vendor_id}{$parts_id}{curr}};
  1543. } else {
  1544. $sellprice = $a{$vendor_id}{$parts_id}{lastcost};
  1545. }
  1546. $sellprice = $form->round_amount($sellprice, 2);
  1547. my $linetotal = $form->round_amount($sellprice * $a{$vendor_id}{$parts_id}{qty}, 2);
  1548. $query = qq|SELECT p.description, p.unit, c.accno FROM parts p
  1549. LEFT JOIN partstax pt ON (p.id = pt.parts_id)
  1550. LEFT JOIN chart c ON (c.id = pt.chart_id)
  1551. WHERE p.id = $parts_id|;
  1552. $sth = $dbh->prepare($query);
  1553. $sth->execute || $form->dberror($query);
  1554. my $rate = 0;
  1555. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1556. $description = $ref->{description};
  1557. $unit = $ref->{unit};
  1558. $rate += $tax{$ref->{accno}};
  1559. }
  1560. $sth->finish;
  1561. $netamount += $linetotal;
  1562. if ($taxincluded) {
  1563. $amount += $linetotal;
  1564. } else {
  1565. $amount += $form->round_amount($linetotal * (1 + $rate), 2);
  1566. }
  1567. $description = $dbh->quote($description);
  1568. $unit = $dbh->quote($unit);
  1569. $query = qq|INSERT INTO orderitems (trans_id, parts_id, description,
  1570. qty, ship, sellprice, unit) VALUES
  1571. ($id, $parts_id, $description,
  1572. $a{$vendor_id}{$parts_id}{qty}, 0, $sellprice, $unit)|;
  1573. $dbh->do($query) || $form->dberror($query);
  1574. }
  1575. my $ordnumber = $form->update_defaults($myconfig, 'ponumber');
  1576. my $null;
  1577. my $employee_id;
  1578. my $department_id;
  1579. ($null, $employee_id) = $form->get_employee($dbh);
  1580. ($null, $department_id) = split /--/, $form->{department};
  1581. $department_id *= 1;
  1582. $query = qq|UPDATE oe SET
  1583. ordnumber = |.$dbh->quote($ordnumber).qq|,
  1584. transdate = current_date,
  1585. vendor_id = $vendor_id,
  1586. customer_id = 0,
  1587. amount = $amount,
  1588. netamount = $netamount,
  1589. taxincluded = '$taxincluded',
  1590. curr = '$curr',
  1591. employee_id = $employee_id,
  1592. department_id = '$department_id',
  1593. ponumber = |.$dbh->quote($form->{ponumber}).qq|
  1594. WHERE id = $id|;
  1595. $dbh->do($query) || $form->dberror($query);
  1596. }
  1597. my $rc = $dbh->commit;
  1598. $dbh->disconnect;
  1599. $rc;
  1600. }
  1601. sub consolidate_orders {
  1602. my ($self, $myconfig, $form) = @_;
  1603. # connect to database
  1604. my $dbh = $form->dbconnect_noauto($myconfig);
  1605. my $i;
  1606. my $id;
  1607. my $ref;
  1608. my %oe = ();
  1609. my $query = qq|SELECT * FROM oe
  1610. WHERE id = ?|;
  1611. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1612. for ($i = 1; $i <= $form->{rowcount}; $i++) {
  1613. # retrieve order
  1614. if ($form->{"ndx_$i"}) {
  1615. $sth->execute($form->{"ndx_$i"});
  1616. $ref = $sth->fetchrow_hashref(NAME_lc);
  1617. $ref->{ndx} = $i;
  1618. $oe{oe}{$ref->{curr}}{$ref->{id}} = $ref;
  1619. $oe{vc}{$ref->{curr}}{$ref->{"$form->{vc}_id"}}++;
  1620. $sth->finish;
  1621. }
  1622. }
  1623. $query = qq|SELECT * FROM orderitems
  1624. WHERE trans_id = ?|;
  1625. $sth = $dbh->prepare($query) || $form->dberror($query);
  1626. foreach $curr (keys %{ $oe{oe} }) {
  1627. foreach $id (sort { $oe{oe}{$curr}{$a}->{ndx} <=> $oe{oe}{$curr}{$b}->{ndx} } keys %{ $oe{oe}{$curr} }) {
  1628. # retrieve order
  1629. $vc_id = $oe{oe}{$curr}{$id}->{"$form->{vc}_id"};
  1630. if ($oe{vc}{$oe{oe}{$curr}{$id}->{curr}}{$vc_id} > 1) {
  1631. push @{ $oe{orders}{$curr}{$vc_id} }, $id;
  1632. $sth->execute($id);
  1633. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1634. push @{ $oe{orderitems}{$curr}{$id} }, $ref;
  1635. }
  1636. $sth->finish;
  1637. }
  1638. }
  1639. }
  1640. my $ordnumber = $form->{ordnumber};
  1641. my $numberfld = ($form->{vc} eq 'customer') ? 'sonumber' : 'ponumber';
  1642. my ($department, $department_id) = $form->{department};
  1643. $department_id *= 1;
  1644. my $uid = localtime;
  1645. $uid .= "$$";
  1646. my @orderitems = ();
  1647. foreach $curr (keys %{ $oe{orders} }) {
  1648. foreach $vc_id (sort { $a <=> $b } keys %{ $oe{orders}{$curr} }) {
  1649. # the orders
  1650. @orderitems = ();
  1651. $form->{customer_id} = $form->{vendor_id} = 0;
  1652. $form->{"$form->{vc}_id"} = $vc_id;
  1653. $amount = 0;
  1654. $netamount = 0;
  1655. foreach $id (@{ $oe{orders}{$curr}{$vc_id} }) {
  1656. # header
  1657. $ref = $oe{oe}{$curr}{$id};
  1658. $amount += $ref->{amount};
  1659. $netamount += $ref->{netamount};
  1660. foreach $item (@{ $oe{orderitems}{$curr}{$id} }) {
  1661. push @orderitems, $item;
  1662. }
  1663. # close order
  1664. $query = qq|UPDATE oe SET
  1665. closed = '1'
  1666. WHERE id = $id|;
  1667. $dbh->do($query) || $form->dberror($query);
  1668. # reset shipped
  1669. $query = qq|UPDATE orderitems SET
  1670. ship = 0
  1671. WHERE trans_id = $id|;
  1672. $dbh->do($query) || $form->dberror($query);
  1673. }
  1674. $ordnumber ||= $form->update_defaults($myconfig, $numberfld, $dbh);
  1675. $query = qq|INSERT INTO oe (ordnumber)
  1676. VALUES ($uid)|;
  1677. $dbh->do($query) || $form->dberror($query);
  1678. $query = qq|SELECT id
  1679. FROM oe
  1680. WHERE ordnumber = '$uid'|;
  1681. ($id) = $dbh->selectrow_array($query);
  1682. $ref->{employee_id} *= 1;
  1683. $query = qq|UPDATE oe SET
  1684. ordnumber = |.$dbh->quote($ordnumber).qq|,
  1685. transdate = current_date,
  1686. vendor_id = $form->{vendor_id},
  1687. customer_id = $form->{customer_id},
  1688. amount = $amount,
  1689. netamount = $netamount,
  1690. reqdate = |.$form->dbquote($ref->{reqdate}, SQL_DATE).qq|,
  1691. taxincluded = '$ref->{taxincluded}',
  1692. shippingpoint = |.$dbh->quote($ref->{shippingpoint}).qq|,
  1693. notes = |.$dbh->quote($ref->{notes}).qq|,
  1694. curr = '$curr',
  1695. employee_id = $ref->{employee_id},
  1696. intnotes = |.$dbh->quote($ref->{intnotes}).qq|,
  1697. shipvia = |.$dbh->quote($ref->{shipvia}).qq|,
  1698. language_code = '$ref->{language_code}',
  1699. ponumber = |.$dbh->quote($form->{ponumber}).qq|,
  1700. department_id = $department_id
  1701. WHERE id = $id|;
  1702. $dbh->do($query) || $form->dberror($query);
  1703. # add items
  1704. foreach $item (@orderitems) {
  1705. for (qw(qty sellprice discount project_id ship)) { $item->{$_} *= 1 }
  1706. $query = qq|INSERT INTO orderitems (
  1707. trans_id, parts_id, description,
  1708. qty, sellprice, discount,
  1709. unit, reqdate, project_id,
  1710. ship, serialnumber, notes)
  1711. VALUES (
  1712. $id, $item->{parts_id}, |.$dbh->quote($item->{description}).qq|,
  1713. $item->{qty}, $item->{sellprice}, $item->{discount},
  1714. |.$dbh->quote($item->{unit}).qq|, |.$form->dbquote($item->{reqdate}, SQL_DATE).qq|, $item->{project_id},
  1715. $item->{ship}, |.$dbh->quote($item->{serialnumber}).qq|, |.$dbh->quote($item->{notes}).qq|)|;
  1716. $dbh->do($query) || $form->dberror($query);
  1717. }
  1718. }
  1719. }
  1720. $rc = $dbh->commit;
  1721. $dbh->disconnect;
  1722. $rc;
  1723. }
  1724. 1;