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