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