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