summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: 44d3dcd45b331e627e95656b9c92545c97b49fff (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 (30%) whitespace cleanup To line 638
  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. if ($form->{customer_id}) {
  824. $query = qq|SELECT p.id AS parts_id, 0 AS customer_id, 0 AS pricegroup_id,
  825. 0 AS pricebreak, p.sellprice, NULL AS validfrom, NULL AS validto,
  826. '$form->{defaultcurrency}' AS curr, '' AS pricegroup
  827. FROM parts p
  828. WHERE p.id = ?
  829. UNION
  830. SELECT p.*, g.pricegroup
  831. FROM partscustomer p
  832. LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
  833. WHERE p.parts_id = ?
  834. AND p.customer_id = $form->{customer_id}
  835. UNION
  836. SELECT p.*, g.pricegroup
  837. FROM partscustomer p
  838. LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
  839. JOIN customer c ON (c.pricegroup_id = g.id)
  840. WHERE p.parts_id = ?
  841. AND c.id = $form->{customer_id}
  842. UNION
  843. SELECT p.*, '' AS pricegroup
  844. FROM partscustomer p
  845. WHERE p.customer_id = 0
  846. AND p.pricegroup_id = 0
  847. AND p.parts_id = ?
  848. ORDER BY customer_id DESC, pricegroup_id DESC, pricebreak
  849. |;
  850. $sth = $dbh->prepare($query) || $form->dberror($query);
  851. }
  852. if ($form->{vendor_id}) {
  853. # price matrix and vendor's partnumber
  854. $query = qq|SELECT partnumber
  855. FROM partsvendor
  856. WHERE parts_id = ?
  857. AND vendor_id = $form->{vendor_id}|;
  858. $sth = $dbh->prepare($query) || $form->dberror($query);
  859. }
  860. $sth;
  861. }
  862. sub price_matrix {
  863. my ($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig) = @_;
  864. $ref->{pricematrix} = "";
  865. my $customerprice;
  866. my $pricegroupprice;
  867. my $sellprice;
  868. my $mref;
  869. my %p = ();
  870. # depends if this is a customer or vendor
  871. if ($form->{customer_id}) {
  872. $pmh->execute($ref->{id}, $ref->{id}, $ref->{id}, $ref->{id});
  873. while ($mref = $pmh->fetchrow_hashref(NAME_lc)) {
  874. # check date
  875. if ($mref->{validfrom}) {
  876. next if $transdate < $form->datetonum($myconfig, $mref->{validfrom});
  877. }
  878. if ($mref->{validto}) {
  879. next if $transdate > $form->datetonum($myconfig, $mref->{validto});
  880. }
  881. # convert price
  882. $sellprice = $form->round_amount($mref->{sellprice} * $form->{$mref->{curr}}, $decimalplaces);
  883. if ($mref->{customer_id}) {
  884. $ref->{sellprice} = $sellprice if !$mref->{pricebreak};
  885. $p{$mref->{pricebreak}} = $sellprice;
  886. $customerprice = 1;
  887. }
  888. if ($mref->{pricegroup_id}) {
  889. if (! $customerprice) {
  890. $ref->{sellprice} = $sellprice if !$mref->{pricebreak};
  891. $p{$mref->{pricebreak}} = $sellprice;
  892. }
  893. $pricegroupprice = 1;
  894. }
  895. if (!$customerprice && !$pricegroupprice) {
  896. $p{$mref->{pricebreak}} = $sellprice;
  897. }
  898. }
  899. $pmh->finish;
  900. if (%p) {
  901. if ($ref->{sellprice}) {
  902. $p{0} = $ref->{sellprice};
  903. }
  904. for (sort { $a <=> $b } keys %p) { $ref->{pricematrix} .= "${_}:$p{$_} " }
  905. } else {
  906. if ($init) {
  907. $ref->{sellprice} = $form->round_amount($ref->{sellprice}, $decimalplaces);
  908. } else {
  909. $ref->{sellprice} = $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), $decimalplaces);
  910. }
  911. $ref->{pricematrix} = "0:$ref->{sellprice} " if $ref->{sellprice};
  912. }
  913. chop $ref->{pricematrix};
  914. }
  915. if ($form->{vendor_id}) {
  916. $pmh->execute($ref->{id});
  917. $mref = $pmh->fetchrow_hashref(NAME_lc);
  918. if ($mref->{partnumber} ne "") {
  919. $ref->{partnumber} = $mref->{partnumber};
  920. }
  921. if ($mref->{lastcost}) {
  922. # do a conversion
  923. $ref->{sellprice} = $form->round_amount($mref->{lastcost} * $form->{$mref->{curr}}, $decimalplaces);
  924. }
  925. $pmh->finish;
  926. $ref->{sellprice} *= 1;
  927. # add 0:price to matrix
  928. $ref->{pricematrix} = "0:$ref->{sellprice}";
  929. }
  930. }
  931. sub exchangerate_defaults {
  932. my ($dbh, $form) = @_;
  933. my $var;
  934. my $buysell = ($form->{vc} eq "customer") ? "buy" : "sell";
  935. # get default currencies
  936. my $query = qq|SELECT substr(curr,1,3), curr FROM defaults|;
  937. ($form->{defaultcurrency}, $form->{currencies}) = $dbh->selectrow_array($query);
  938. $query = qq|SELECT $buysell
  939. FROM exchangerate
  940. WHERE curr = ?
  941. AND transdate = ?|;
  942. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  943. $query = qq~SELECT max(transdate || ' ' || $buysell || ' ' || curr)
  944. FROM exchangerate
  945. WHERE curr = ?~;
  946. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  947. # get exchange rates for transdate or max
  948. foreach $var (split /:/, substr($form->{currencies},4)) {
  949. $eth1->execute($var, $form->{transdate});
  950. ($form->{$var}) = $eth1->fetchrow_array;
  951. if (! $form->{$var} ) {
  952. $eth2->execute($var);
  953. ($form->{$var}) = $eth2->fetchrow_array;
  954. ($null, $form->{$var}) = split / /, $form->{$var};
  955. $form->{$var} = 1 unless $form->{$var};
  956. $eth2->finish;
  957. }
  958. $eth1->finish;
  959. }
  960. $form->{$form->{currency}} = $form->{exchangerate} if $form->{exchangerate};
  961. $form->{$form->{currency}} ||= 1;
  962. $form->{$form->{defaultcurrency}} = 1;
  963. }
  964. sub order_details {
  965. my ($self, $myconfig, $form) = @_;
  966. # connect to database
  967. my $dbh = $form->dbconnect($myconfig);
  968. my $query;
  969. my $sth;
  970. my $item;
  971. my $i;
  972. my @sortlist = ();
  973. my $projectnumber;
  974. my $projectdescription;
  975. my $projectnumber_id;
  976. my $translation;
  977. my $partsgroup;
  978. my %oid = ( 'Pg' => 'TRUE',
  979. 'PgPP' => 'TRUE',
  980. 'Oracle' => 'rowid',
  981. 'DB2' => '1=1'
  982. );
  983. my @taxaccounts;
  984. my %taxaccounts;
  985. my $tax;
  986. my $taxrate;
  987. my $taxamount;
  988. my %translations;
  989. $query = qq|SELECT p.description, t.description
  990. FROM project p
  991. LEFT JOIN translation t ON (t.trans_id = p.id AND t.language_code = '$form->{language_code}')
  992. WHERE id = ?|;
  993. my $prh = $dbh->prepare($query) || $form->dberror($query);
  994. $query = qq|SELECT inventory_accno_id, income_accno_id,
  995. expense_accno_id, assembly FROM parts
  996. WHERE id = ?|;
  997. my $pth = $dbh->prepare($query) || $form->dberror($query);
  998. my $sortby;
  999. # sort items by project and partsgroup
  1000. for $i (1 .. $form->{rowcount}) {
  1001. if ($form->{"id_$i"}) {
  1002. # account numbers
  1003. $pth->execute($form->{"id_$i"});
  1004. $ref = $pth->fetchrow_hashref(NAME_lc);
  1005. for (keys %$ref) { $form->{"${_}_$i"} = $ref->{$_} }
  1006. $pth->finish;
  1007. $projectnumber_id = 0;
  1008. $projectnumber = "";
  1009. $form->{partsgroup} = "";
  1010. $form->{projectnumber} = "";
  1011. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  1012. $inventory_accno_id = ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) ? "1" : "";
  1013. if ($form->{groupprojectnumber}) {
  1014. ($projectnumber, $projectnumber_id) = split /--/, $form->{"projectnumber_$i"};
  1015. }
  1016. if ($form->{grouppartsgroup}) {
  1017. ($form->{partsgroup}) = split /--/, $form->{"partsgroup_$i"};
  1018. }
  1019. if ($projectnumber_id && $form->{groupprojectnumber}) {
  1020. if ($translation{$projectnumber_id}) {
  1021. $form->{projectnumber} = $translation{$projectnumber_id};
  1022. } else {
  1023. # get project description
  1024. $prh->execute($projectnumber_id);
  1025. ($projectdescription, $translation) = $prh->fetchrow_array;
  1026. $prh->finish;
  1027. $form->{projectnumber} = ($translation) ? "$projectnumber, $translation" : "$projectnumber, $projectdescription";
  1028. $translation{$projectnumber_id} = $form->{projectnumber};
  1029. }
  1030. }
  1031. if ($form->{grouppartsgroup} && $form->{partsgroup}) {
  1032. $form->{projectnumber} .= " / " if $projectnumber_id;
  1033. $form->{projectnumber} .= $form->{partsgroup};
  1034. }
  1035. $form->format_string(projectnumber);
  1036. }
  1037. $sortby = qq|$projectnumber$form->{partsgroup}|;
  1038. if ($form->{sortby} ne 'runningnumber') {
  1039. for (qw(partnumber description bin)) {
  1040. $sortby .= $form->{"${_}_$i"} if $form->{sortby} eq $_;
  1041. }
  1042. }
  1043. push @sortlist, [ $i, qq|$projectnumber$form->{partsgroup}$inventory_accno_id|, $form->{projectnumber}, $projectnumber_id, $form->{partsgroup}, $sortby ];
  1044. }
  1045. }
  1046. delete $form->{projectnumber};
  1047. # sort the whole thing by project and group
  1048. @sortlist = sort { $a->[5] cmp $b->[5] } @sortlist;
  1049. # if there is a warehouse limit picking
  1050. if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
  1051. # run query to check for inventory
  1052. $query = qq|SELECT sum(qty) AS qty
  1053. FROM inventory
  1054. WHERE parts_id = ?
  1055. AND warehouse_id = ?|;
  1056. $sth = $dbh->prepare($query) || $form->dberror($query);
  1057. for $i (1 .. $form->{rowcount}) {
  1058. $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
  1059. ($qty) = $sth->fetchrow_array;
  1060. $sth->finish;
  1061. $form->{"qty_$i"} = 0 if $qty == 0;
  1062. if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
  1063. $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
  1064. }
  1065. }
  1066. }
  1067. my $runningnumber = 1;
  1068. my $sameitem = "";
  1069. my $subtotal;
  1070. my $k = scalar @sortlist;
  1071. my $j = 0;
  1072. foreach $item (@sortlist) {
  1073. $i = $item->[0];
  1074. $j++;
  1075. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  1076. if ($item->[1] ne $sameitem) {
  1077. $sameitem = $item->[1];
  1078. $ok = 0;
  1079. if ($form->{groupprojectnumber}) {
  1080. $ok = $form->{"projectnumber_$i"};
  1081. }
  1082. if ($form->{grouppartsgroup}) {
  1083. $ok = $form->{"partsgroup_$i"} unless $ok;
  1084. }
  1085. if ($ok) {
  1086. if ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) {
  1087. push(@{ $form->{part} }, "");
  1088. push(@{ $form->{service} }, NULL);
  1089. } else {
  1090. push(@{ $form->{part} }, NULL);
  1091. push(@{ $form->{service} }, "");
  1092. }
  1093. push(@{ $form->{description} }, $item->[2]);
  1094. for (qw(taxrates runningnumber number sku qty ship unit bin serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate linetotal weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1095. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  1096. }
  1097. }
  1098. }
  1099. $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
  1100. $form->{"ship_$i"} = $form->parse_amount($myconfig, $form->{"ship_$i"});
  1101. if ($form->{"qty_$i"}) {
  1102. $form->{totalqty} += $form->{"qty_$i"};
  1103. $form->{totalship} += $form->{"ship_$i"};
  1104. $form->{totalweight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
  1105. $form->{totalweightship} += ($form->{"weight_$i"} * $form->{"ship_$i"});
  1106. # add number, description and qty to $form->{number}, ....
  1107. push(@{ $form->{runningnumber} }, $runningnumber++);
  1108. push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
  1109. push(@{ $form->{sku} }, qq|$form->{"sku_$i"}|);
  1110. push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
  1111. push(@{ $form->{itemnotes} }, $form->{"notes_$i"});
  1112. push(@{ $form->{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"}));
  1113. push(@{ $form->{ship} }, $form->format_amount($myconfig, $form->{"ship_$i"}));
  1114. push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
  1115. push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
  1116. push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
  1117. push(@{ $form->{requiredate} }, qq|$form->{"reqdate_$i"}|);
  1118. push(@{ $form->{projectnumber} }, qq|$form->{"projectnumber_$i"}|);
  1119. push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
  1120. push(@{ $form->{listprice} }, $form->{"listprice_$i"});
  1121. push(@{ $form->{weight} }, $form->format_amount($myconfig, $form->{"weight_$i"} * $form->{"ship_$i"}));
  1122. my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
  1123. my ($dec) = ($sellprice =~ /\.(\d+)/);
  1124. $dec = length $dec;
  1125. my $decimalplaces = ($dec > 2) ? $dec : 2;
  1126. my $discount = $form->round_amount($sellprice * $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100, $decimalplaces);
  1127. # keep a netprice as well, (sellprice - discount)
  1128. $form->{"netprice_$i"} = $sellprice - $discount;
  1129. my $linetotal = $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
  1130. if ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) {
  1131. push(@{ $form->{part} }, $form->{"sku_$i"});
  1132. push(@{ $form->{service} }, NULL);
  1133. $form->{totalparts} += $linetotal;
  1134. } else {
  1135. push(@{ $form->{service} }, $form->{"sku_$i"});
  1136. push(@{ $form->{part} }, NULL);
  1137. $form->{totalservices} += $linetotal;
  1138. }
  1139. push(@{ $form->{netprice} }, ($form->{"netprice_$i"}) ? $form->format_amount($myconfig, $form->{"netprice_$i"}, $decimalplaces) : " ");
  1140. $discount = ($discount) ? $form->format_amount($myconfig, $discount * -1, $decimalplaces) : " ";
  1141. push(@{ $form->{discount} }, $discount);
  1142. push(@{ $form->{discountrate} }, $form->format_amount($myconfig, $form->{"discount_$i"}));
  1143. $form->{ordtotal} += $linetotal;
  1144. # this is for the subtotals for grouping
  1145. $subtotal += $linetotal;
  1146. $form->{"linetotal_$i"} = $form->format_amount($myconfig, $linetotal, 2);
  1147. push(@{ $form->{linetotal} }, $form->{"linetotal_$i"});
  1148. @taxaccounts = split / /, $form->{"taxaccounts_$i"};
  1149. my $ml = 1;
  1150. my @taxrates = ();
  1151. $tax = 0;
  1152. for (0 .. 1) {
  1153. $taxrate = 0;
  1154. for (@taxaccounts) { $taxrate += $form->{"${_}_rate"} if ($form->{"${_}_rate"} * $ml) > 0 }
  1155. $taxrate *= $ml;
  1156. $taxamount = $linetotal * $taxrate / (1 + $taxrate);
  1157. $taxbase = ($linetotal - $taxamount);
  1158. foreach $item (@taxaccounts) {
  1159. if (($form->{"${item}_rate"} * $ml) > 0) {
  1160. push @taxrates, $form->{"${item}_rate"} * 100;
  1161. if ($form->{taxincluded}) {
  1162. $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"} / (1 + $taxrate);
  1163. $taxbase{$item} += $taxbase;
  1164. } else {
  1165. $taxbase{$item} += $linetotal;
  1166. $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
  1167. }
  1168. }
  1169. }
  1170. if ($form->{taxincluded}) {
  1171. $tax += $linetotal * ($taxrate / (1 + ($taxrate * $ml)));
  1172. } else {
  1173. $tax += $linetotal * $taxrate;
  1174. }
  1175. $ml *= -1;
  1176. }
  1177. push(@{ $form->{lineitems} }, { amount => $linetotal, tax => $form->round_amount($tax, 2) });
  1178. push(@{ $form->{taxrates} }, join ' ', sort { $a <=> $b } @taxrates);
  1179. if ($form->{"assembly_$i"}) {
  1180. $form->{stagger} = -1;
  1181. &assembly_details($myconfig, $form, $dbh, $form->{"id_$i"}, $oid{$myconfig->{dbdriver}}, $form->{"qty_$i"});
  1182. }
  1183. }
  1184. # add subtotal
  1185. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  1186. if ($subtotal) {
  1187. if ($j < $k) {
  1188. # look at next item
  1189. if ($sortlist[$j]->[1] ne $sameitem) {
  1190. if ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) {
  1191. push(@{ $form->{part} }, "");
  1192. push(@{ $form->{service} }, NULL);
  1193. } else {
  1194. push(@{ $form->{service} }, "");
  1195. push(@{ $form->{part} }, NULL);
  1196. }
  1197. for (qw(taxrates runningnumber number sku qty ship unit bin serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1198. push(@{ $form->{description} }, $form->{groupsubtotaldescription});
  1199. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  1200. if ($form->{groupsubtotaldescription} ne "") {
  1201. push(@{ $form->{linetotal} }, $form->format_amount($myconfig, $subtotal, 2));
  1202. } else {
  1203. push(@{ $form->{linetotal} }, "");
  1204. }
  1205. $subtotal = 0;
  1206. }
  1207. } else {
  1208. # got last item
  1209. if ($form->{groupsubtotaldescription} ne "") {
  1210. if ($form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"}) {
  1211. push(@{ $form->{part} }, "");
  1212. push(@{ $form->{service} }, NULL);
  1213. } else {
  1214. push(@{ $form->{service} }, "");
  1215. push(@{ $form->{part} }, NULL);
  1216. }
  1217. for (qw(taxrates runningnumber number sku qty ship unit bin serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1218. push(@{ $form->{description} }, $form->{groupsubtotaldescription});
  1219. push(@{ $form->{linetotal} }, $form->format_amount($myconfig, $subtotal, 2));
  1220. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  1221. }
  1222. }
  1223. }
  1224. }
  1225. }
  1226. $tax = 0;
  1227. foreach $item (sort keys %taxaccounts) {
  1228. if ($form->round_amount($taxaccounts{$item}, 2)) {
  1229. $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
  1230. push(@{ $form->{taxbaseinclusive} }, $form->{"${item}_taxbaseinclusive"} = $form->round_amount($taxbase{$item} + $tax, 2));
  1231. push(@{ $form->{taxbase} }, $form->{"${item}_taxbase"} = $form->format_amount($myconfig, $taxbase{$item}, 2));
  1232. push(@{ $form->{tax} }, $form->{"${item}_tax"} = $form->format_amount($myconfig, $taxamount, 2));
  1233. push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
  1234. $form->{"${item}_taxrate"} = $form->format_amount($myconfig, $form->{"${item}_rate"} * 100);
  1235. push(@{ $form->{taxrate} }, $form->{"${item}_taxrate"});
  1236. push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
  1237. }
  1238. }
  1239. # adjust taxes for lineitems
  1240. my $total = 0;
  1241. for (@{ $form->{lineitems} }) {
  1242. $total += $_->{tax};
  1243. }
  1244. if ($form->round_amount($total,2) != $form->round_amount($tax,2)) {
  1245. # get largest amount
  1246. for (reverse sort { $a->{tax} <=> $b->{tax} } @{ $form->{lineitems} }) {
  1247. $_->{tax} -= $total - $tax;
  1248. last;
  1249. }
  1250. }
  1251. $i = 1;
  1252. for (@{ $form->{lineitems} }) {
  1253. push(@{ $form->{linetax} }, $form->format_amount($myconfig, $_->{tax}, 2, ""));
  1254. }
  1255. for (qw(totalparts totalservices)) { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) }
  1256. for (qw(totalqty totalship totalweight)) { $form->{$_} = $form->format_amount($myconfig, $form->{$_}) }
  1257. $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
  1258. $form->{ordtotal} = ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
  1259. use LedgerSMB::CP;
  1260. my $c;
  1261. if ($form->{language_code} ne "") {
  1262. $c = new CP $form->{language_code};
  1263. } else {
  1264. $c = new CP $myconfig->{countrycode};
  1265. }
  1266. $c->init;
  1267. my $whole;
  1268. ($whole, $form->{decimal}) = split /\./, $form->{ordtotal};
  1269. $form->{decimal} .= "00";
  1270. $form->{decimal} = substr($form->{decimal}, 0, 2);
  1271. $form->{text_decimal} = $c->num2text($form->{decimal} * 1);
  1272. $form->{text_amount} = $c->num2text($whole);
  1273. $form->{integer_amount} = $form->format_amount($myconfig, $whole);
  1274. # format amounts
  1275. $form->{quototal} = $form->{ordtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
  1276. $form->format_string(qw(text_amount text_decimal));
  1277. $query = qq|SELECT weightunit FROM defaults|;
  1278. ($form->{weightunit}) = $dbh->selectrow_array($query);
  1279. $dbh->disconnect;
  1280. }
  1281. sub assembly_details {
  1282. my ($myconfig, $form, $dbh, $id, $oid, $qty) = @_;
  1283. my $sm = "";
  1284. my $spacer;
  1285. $form->{stagger}++;
  1286. if ($form->{format} eq 'html') {
  1287. $spacer = "&nbsp;" x (3 * ($form->{stagger} - 1)) if $form->{stagger} > 1;
  1288. }
  1289. if ($form->{format} =~ /(postscript|pdf)/) {
  1290. if ($form->{stagger} > 1) {
  1291. $spacer = ($form->{stagger} - 1) * 3;
  1292. $spacer = '\rule{'.$spacer.'mm}{0mm}';
  1293. }
  1294. }
  1295. # get parts and push them onto the stack
  1296. my $sortorder = "";
  1297. if ($form->{grouppartsgroup}) {
  1298. $sortorder = qq|ORDER BY pg.partsgroup, a.$oid|;
  1299. } else {
  1300. $sortorder = qq|ORDER BY a.$oid|;
  1301. }
  1302. my $where = ($form->{formname} eq 'work_order') ? "1 = 1" : "a.bom = '1'";
  1303. my $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
  1304. pg.partsgroup, p.partnumber AS sku, p.assembly, p.id, p.bin
  1305. FROM assembly a
  1306. JOIN parts p ON (a.parts_id = p.id)
  1307. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1308. WHERE $where
  1309. AND a.id = '$id'
  1310. $sortorder|;
  1311. my $sth = $dbh->prepare($query);
  1312. $sth->execute || $form->dberror($query);
  1313. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1314. for (qw(partnumber description partsgroup)) {
  1315. $form->{"a_$_"} = $ref->{$_};
  1316. $form->format_string("a_$_");
  1317. }
  1318. if ($form->{grouppartsgroup} && $ref->{partsgroup} ne $sm) {
  1319. for (qw(taxrates number sku unit qty runningnumber ship bin serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate linetotal weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1320. $sm = ($form->{"a_partsgroup"}) ? $form->{"a_partsgroup"} : "";
  1321. push(@{ $form->{description} }, "$spacer$sm");
  1322. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  1323. }
  1324. if ($form->{stagger}) {
  1325. push(@{ $form->{description} }, qq|$spacer$form->{"a_partnumber"}, $form->{"a_description"}|);
  1326. for (qw(taxrates number sku runningnumber ship serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate linetotal weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1327. } else {
  1328. push(@{ $form->{description} }, qq|$form->{"a_description"}|);
  1329. push(@{ $form->{sku} }, $form->{"a_partnumber"});
  1330. push(@{ $form->{number} }, $form->{"a_partnumber"});
  1331. for (qw(taxrates runningnumber ship serialnumber requiredate projectnumber sellprice listprice netprice discount discountrate linetotal weight itemnotes)) { push(@{ $form->{$_} }, "") }
  1332. }
  1333. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  1334. push(@{ $form->{qty} }, $form->format_amount($myconfig, $ref->{qty} * $qty));
  1335. for (qw(unit bin)) {
  1336. $form->{"a_$_"} = $ref->{$_};
  1337. $form->format_string("a_$_");
  1338. push(@{ $form->{$_} }, $form->{"a_$_"});
  1339. }
  1340. if ($ref->{assembly} && $form->{formname} eq 'work_order') {
  1341. &assembly_details($myconfig, $form, $dbh, $ref->{id}, $oid, $ref->{qty} * $qty);
  1342. }
  1343. }
  1344. $sth->finish;
  1345. $form->{stagger}--;
  1346. }
  1347. sub project_description {
  1348. my ($self, $dbh, $id) = @_;
  1349. my $query = qq|SELECT description
  1350. FROM project
  1351. WHERE id = $id|;
  1352. ($_) = $dbh->selectrow_array($query);
  1353. $_;
  1354. }
  1355. sub get_warehouses {
  1356. my ($self, $myconfig, $form) = @_;
  1357. my $dbh = $form->dbconnect($myconfig);
  1358. # setup warehouses
  1359. my $query = qq|SELECT id, description
  1360. FROM warehouse
  1361. ORDER BY 2|;
  1362. my $sth = $dbh->prepare($query);
  1363. $sth->execute || $form->dberror($query);
  1364. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1365. push @{ $form->{all_warehouse} }, $ref;
  1366. }
  1367. $sth->finish;
  1368. $dbh->disconnect;
  1369. }
  1370. sub save_inventory {
  1371. my ($self, $myconfig, $form) = @_;
  1372. my ($null, $warehouse_id) = split /--/, $form->{warehouse};
  1373. $warehouse_id *= 1;
  1374. my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
  1375. my $dbh = $form->dbconnect_noauto($myconfig);
  1376. my $sth;
  1377. my $wth;
  1378. my $serialnumber;
  1379. my $ship;
  1380. my ($null, $employee_id) = split /--/, $form->{employee};
  1381. ($null, $employee_id) = $form->get_employee($dbh) if ! $employee_id;
  1382. $query = qq|SELECT serialnumber, ship
  1383. FROM orderitems
  1384. WHERE trans_id = ?
  1385. AND id = ?
  1386. FOR UPDATE|;
  1387. $sth = $dbh->prepare($query) || $form->dberror($query);
  1388. $query = qq|SELECT sum(qty)
  1389. FROM inventory
  1390. WHERE parts_id = ?
  1391. AND warehouse_id = ?|;
  1392. $wth = $dbh->prepare($query) || $form->dberror($query);
  1393. for my $i (1 .. $form->{rowcount}) {
  1394. $ship = (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"})) ? $form->{"qty_$i"} : $form->{"ship_$i"};
  1395. if ($warehouse_id && $form->{type} eq 'ship_order') {
  1396. $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
  1397. ($qty) = $wth->fetchrow_array;
  1398. $wth->finish;
  1399. if ($ship > $qty) {
  1400. $ship = $qty;
  1401. }
  1402. }
  1403. if ($ship) {
  1404. $ship *= $ml;
  1405. $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
  1406. qty, trans_id, orderitems_id, shippingdate, employee_id)
  1407. VALUES ($form->{"id_$i"}, $warehouse_id,
  1408. $ship, $form->{"id"},
  1409. $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
  1410. $employee_id)|;
  1411. $dbh->do($query) || $form->dberror($query);
  1412. # add serialnumber, ship to orderitems
  1413. $sth->execute($form->{id}, $form->{"orderitems_id_$i"}) || $form->dberror;
  1414. ($serialnumber, $ship) = $sth->fetchrow_array;
  1415. $sth->finish;
  1416. $serialnumber .= " " if $serialnumber;
  1417. $serialnumber .= qq|$form->{"serialnumber_$i"}|;
  1418. $ship += $form->{"ship_$i"};
  1419. $query = qq|UPDATE orderitems SET
  1420. serialnumber = '$serialnumber',
  1421. ship = $ship,
  1422. reqdate = '$form->{shippingdate}'
  1423. WHERE trans_id = $form->{id}
  1424. AND id = $form->{"orderitems_id_$i"}|;
  1425. $dbh->do($query) || $form->dberror($query);
  1426. # update order with ship via
  1427. $query = qq|UPDATE oe SET
  1428. shippingpoint = |.$dbh->quote($form->{shippingpoint}).qq|,
  1429. shipvia = |.$dbh->quote($form->{shipvia}).qq|
  1430. WHERE id = $form->{id}|;
  1431. $dbh->do($query) || $form->dberror($query);
  1432. # update onhand for parts
  1433. $form->update_balance($dbh,
  1434. "parts",
  1435. "onhand",
  1436. qq|id = $form->{"id_$i"}|,
  1437. $form->{"ship_$i"} * $ml);
  1438. }
  1439. }
  1440. my $rc = $dbh->commit;
  1441. $dbh->disconnect;
  1442. $rc;
  1443. }
  1444. sub adj_onhand {
  1445. my ($dbh, $form, $ml) = @_;
  1446. my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
  1447. FROM orderitems oi
  1448. JOIN parts p ON (p.id = oi.parts_id)
  1449. WHERE oi.trans_id = $form->{id}|;
  1450. my $sth = $dbh->prepare($query);
  1451. $sth->execute || $form->dberror($query);
  1452. $query = qq|SELECT sum(p.inventory_accno_id), p.assembly
  1453. FROM parts p
  1454. JOIN assembly a ON (a.parts_id = p.id)
  1455. WHERE a.id = ?
  1456. GROUP BY p.assembly|;
  1457. my $ath = $dbh->prepare($query) || $form->dberror($query);
  1458. my $ref;
  1459. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1460. if ($ref->{inventory_accno_id} || $ref->{assembly}) {
  1461. # do not update if assembly consists of all services
  1462. if ($ref->{assembly}) {
  1463. $ath->execute($ref->{parts_id}) || $form->dberror($query);
  1464. my ($inv, $assembly) = $ath->fetchrow_array;
  1465. $ath->finish;
  1466. next unless ($inv || $assembly);
  1467. }
  1468. # adjust onhand in parts table
  1469. $form->update_balance($dbh,
  1470. "parts",
  1471. "onhand",
  1472. qq|id = $ref->{parts_id}|,
  1473. $ref->{ship} * $ml);
  1474. }
  1475. }
  1476. $sth->finish;
  1477. }
  1478. sub adj_inventory {
  1479. my ($dbh, $myconfig, $form) = @_;
  1480. my %oid = ( 'Pg' => 'id',
  1481. 'PgPP' => 'id',
  1482. 'Oracle' => 'rowid',
  1483. 'DB2' => '1=1'
  1484. );
  1485. # increase/reduce qty in inventory table
  1486. my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
  1487. FROM orderitems oi
  1488. WHERE oi.trans_id = $form->{id}|;
  1489. my $sth = $dbh->prepare($query);
  1490. $sth->execute || $form->dberror($query);
  1491. $query = qq|SELECT qty,
  1492. (SELECT SUM(qty) FROM inventory
  1493. WHERE trans_id = $form->{id}
  1494. AND orderitems_id = ?) AS total
  1495. FROM inventory
  1496. WHERE trans_id = $form->{id}
  1497. AND orderitems_id = ?|;
  1498. my $ith = $dbh->prepare($query) || $form->dberror($query);
  1499. my $qty;
  1500. my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
  1501. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1502. $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
  1503. my $ship = $ref->{ship};
  1504. while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
  1505. if (($qty = (($inv->{total} * $ml) - $ship)) >= 0) {
  1506. $qty = $inv->{qty} * $ml if ($qty > ($inv->{qty} * $ml));
  1507. $form->update_balance($dbh,
  1508. "inventory",
  1509. "qty",
  1510. qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
  1511. $qty * -1 * $ml);
  1512. $ship -= $qty;
  1513. }
  1514. }
  1515. $ith->finish;
  1516. }
  1517. $sth->finish;
  1518. # delete inventory entries if qty = 0
  1519. $query = qq|DELETE FROM inventory
  1520. WHERE trans_id = $form->{id}
  1521. AND qty = 0|;
  1522. $dbh->do($query) || $form->dberror($query);
  1523. }
  1524. sub get_inventory {
  1525. my ($self, $myconfig, $form) = @_;
  1526. my $where;
  1527. my $query;
  1528. my $null;
  1529. my $fromwarehouse_id;
  1530. my $towarehouse_id;
  1531. my $var;
  1532. my $dbh = $form->dbconnect($myconfig);
  1533. if ($form->{partnumber} ne "") {
  1534. $var = $form->like(lc $form->{partnumber});
  1535. $where .= "
  1536. AND lower(p.partnumber) LIKE '$var'";
  1537. }
  1538. if ($form->{description} ne "") {
  1539. $var = $form->like(lc $form->{description});
  1540. $where .= "
  1541. AND lower(p.description) LIKE '$var'";
  1542. }
  1543. if ($form->{partsgroup} ne "") {
  1544. ($null, $var) = split /--/, $form->{partsgroup};
  1545. $where .= "
  1546. AND pg.id = $var";
  1547. }
  1548. ($null, $fromwarehouse_id) = split /--/, $form->{fromwarehouse};
  1549. $fromwarehouse_id *= 1;
  1550. ($null, $towarehouse_id) = split /--/, $form->{towarehouse};
  1551. $towarehouse_id *= 1;
  1552. my %ordinal = ( partnumber => 2,
  1553. description => 3,
  1554. partsgroup => 5,
  1555. warehouse => 6,
  1556. );
  1557. my @a = (partnumber, warehouse);
  1558. my $sortorder = $form->sort_order(\@a, \%ordinal);
  1559. if ($fromwarehouse_id) {
  1560. if ($towarehouse_id) {
  1561. $where .= "
  1562. AND NOT i.warehouse_id = $towarehouse_id";
  1563. }
  1564. $query = qq|SELECT p.id, p.partnumber, p.description,
  1565. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1566. pg.partsgroup, w.description AS warehouse, i.warehouse_id
  1567. FROM inventory i
  1568. JOIN parts p ON (p.id = i.parts_id)
  1569. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1570. JOIN warehouse w ON (w.id = i.warehouse_id)
  1571. WHERE i.warehouse_id = $fromwarehouse_id
  1572. $where
  1573. GROUP BY p.id, p.partnumber, p.description, pg.partsgroup, w.description, i.warehouse_id
  1574. ORDER BY $sortorder|;
  1575. } else {
  1576. if ($towarehouse_id) {
  1577. $query = qq|
  1578. SELECT p.id, p.partnumber, p.description,
  1579. p.onhand, (SELECT SUM(qty) FROM inventory i WHERE i.parts_id = p.id) AS qty,
  1580. pg.partsgroup, '' AS warehouse, 0 AS warehouse_id
  1581. FROM parts p
  1582. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1583. WHERE p.onhand > 0
  1584. $where
  1585. UNION|;
  1586. }
  1587. $query .= qq|
  1588. SELECT p.id, p.partnumber, p.description,
  1589. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1590. pg.partsgroup, w.description AS warehouse, i.warehouse_id
  1591. FROM inventory i
  1592. JOIN parts p ON (p.id = i.parts_id)
  1593. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1594. JOIN warehouse w ON (w.id = i.warehouse_id)
  1595. WHERE i.warehouse_id != $towarehouse_id
  1596. $where
  1597. GROUP BY p.id, p.partnumber, p.description, pg.partsgroup, w.description, i.warehouse_id
  1598. ORDER BY $sortorder|;
  1599. }
  1600. my $sth = $dbh->prepare($query);
  1601. $sth->execute || $form->dberror($query);
  1602. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1603. $ref->{qty} = $ref->{onhand} - $ref->{qty};
  1604. push @{ $form->{all_inventory} }, $ref if $ref->{qty} > 0;
  1605. }
  1606. $sth->finish;
  1607. $dbh->disconnect;
  1608. }
  1609. sub transfer {
  1610. my ($self, $myconfig, $form) = @_;
  1611. my $dbh = $form->dbconnect_noauto($myconfig);
  1612. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
  1613. my @a = localtime;
  1614. $a[5] += 1900;
  1615. $a[4]++;
  1616. $a[4] = substr("0$a[4]", -2);
  1617. $a[3] = substr("0$a[3]", -2);
  1618. $shippingdate = "$a[5]$a[4]$a[3]";
  1619. my %total = ();
  1620. my $query = qq|INSERT INTO inventory
  1621. (warehouse_id, parts_id, qty, shippingdate, employee_id)
  1622. VALUES (?, ?, ?, '$shippingdate', $form->{employee_id})|;
  1623. $sth = $dbh->prepare($query) || $form->dberror($query);
  1624. my $qty;
  1625. for my $i (1 .. $form->{rowcount}) {
  1626. $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
  1627. $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
  1628. if ($qty > 0) {
  1629. # to warehouse
  1630. if ($form->{warehouse_id}) {
  1631. $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty) || $form->dberror;
  1632. $sth->finish;
  1633. }
  1634. # from warehouse
  1635. if ($form->{"warehouse_id_$i"}) {
  1636. $sth->execute($form->{"warehouse_id_$i"}, $form->{"id_$i"}, $qty * -1) || $form->dberror;
  1637. $sth->finish;
  1638. }
  1639. }
  1640. }
  1641. my $rc = $dbh->commit;
  1642. $dbh->disconnect;
  1643. $rc;
  1644. }
  1645. sub get_soparts {
  1646. my ($self, $myconfig, $form) = @_;
  1647. # connect to database
  1648. my $dbh = $form->dbconnect($myconfig);
  1649. my $id;
  1650. my $ref;
  1651. # store required items from selected sales orders
  1652. my $query = qq|SELECT p.id, oi.qty - oi.ship AS required,
  1653. p.assembly
  1654. FROM orderitems oi
  1655. JOIN parts p ON (p.id = oi.parts_id)
  1656. WHERE oi.trans_id = ?|;
  1657. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1658. for (my $i = 1; $i <= $form->{rowcount}; $i++) {
  1659. if ($form->{"ndx_$i"}) {
  1660. $sth->execute($form->{"ndx_$i"});
  1661. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1662. &add_items_required("", $dbh, $form, $ref->{id}, $ref->{required}, $ref->{assembly});
  1663. }
  1664. $sth->finish;
  1665. }
  1666. }
  1667. $query = qq|SELECT current_date FROM defaults|;
  1668. ($form->{transdate}) = $dbh->selectrow_array($query);
  1669. # foreign exchange rates
  1670. &exchangerate_defaults($dbh, $form);
  1671. $dbh->disconnect;
  1672. }
  1673. sub add_items_required {
  1674. my ($self, $dbh, $form, $parts_id, $required, $assembly) = @_;
  1675. my $query;
  1676. my $sth;
  1677. my $ref;
  1678. if ($assembly) {
  1679. $query = qq|SELECT p.id, a.qty, p.assembly
  1680. FROM assembly a
  1681. JOIN parts p ON (p.id = a.parts_id)
  1682. WHERE a.id = $parts_id|;
  1683. $sth = $dbh->prepare($query);
  1684. $sth->execute || $form->dberror($query);
  1685. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1686. &add_items_required("", $dbh, $form, $ref->{id}, $required * $ref->{qty}, $ref->{assembly});
  1687. }
  1688. $sth->finish;
  1689. } else {
  1690. $query = qq|SELECT partnumber, description, lastcost
  1691. FROM parts
  1692. WHERE id = $parts_id|;
  1693. $sth = $dbh->prepare($query);
  1694. $sth->execute || $form->dberror($query);
  1695. $ref = $sth->fetchrow_hashref(NAME_lc);
  1696. for (keys %$ref) { $form->{orderitems}{$parts_id}{$_} = $ref->{$_} }
  1697. $sth->finish;
  1698. $form->{orderitems}{$parts_id}{required} += $required;
  1699. $query = qq|SELECT pv.partnumber, pv.leadtime, pv.lastcost, pv.curr,
  1700. pv.vendor_id, v.name
  1701. FROM partsvendor pv
  1702. JOIN vendor v ON (v.id = pv.vendor_id)
  1703. WHERE pv.parts_id = ?|;
  1704. $sth = $dbh->prepare($query) || $form->dberror($query);
  1705. # get cost and vendor
  1706. $sth->execute($parts_id);
  1707. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1708. for (keys %$ref) { $form->{orderitems}{$parts_id}{partsvendor}{$ref->{vendor_id}}{$_} = $ref->{$_} }
  1709. }
  1710. $sth->finish;
  1711. }
  1712. }
  1713. sub generate_orders {
  1714. my ($self, $myconfig, $form) = @_;
  1715. my $i;
  1716. my %a;
  1717. my $query;
  1718. my $sth;
  1719. for ($i = 1; $i <= $form->{rowcount}; $i++) {
  1720. for (qw(qty lastcost)) { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) }
  1721. if ($form->{"qty_$i"}) {
  1722. ($vendor, $vendor_id) = split /--/, $form->{"vendor_$i"};
  1723. if ($vendor_id) {
  1724. $a{$vendor_id}{$form->{"id_$i"}}{qty} += $form->{"qty_$i"};
  1725. for (qw(curr lastcost)) { $a{$vendor_id}{$form->{"id_$i"}}{$_} = $form->{"${_}_$i"} }
  1726. }
  1727. }
  1728. }
  1729. # connect to database
  1730. my $dbh = $form->dbconnect_noauto($myconfig);
  1731. # foreign exchange rates
  1732. &exchangerate_defaults($dbh, $form);
  1733. my $amount;
  1734. my $netamount;
  1735. my $curr = "";
  1736. my %tax;
  1737. my $taxincluded = 0;
  1738. my $vendor_id;
  1739. my $description;
  1740. my $unit;
  1741. my $sellprice;
  1742. foreach $vendor_id (keys %a) {
  1743. %tax = ();
  1744. $query = qq|SELECT v.curr, v.taxincluded, t.rate, c.accno
  1745. FROM vendor v
  1746. LEFT JOIN vendortax vt ON (v.id = vt.vendor_id)
  1747. LEFT JOIN tax t ON (t.chart_id = vt.chart_id)
  1748. LEFT JOIN chart c ON (c.id = t.chart_id)
  1749. WHERE v.id = $vendor_id|;
  1750. $sth = $dbh->prepare($query);
  1751. $sth->execute || $form->dberror($query);
  1752. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1753. $curr = $ref->{curr};
  1754. $taxincluded = $ref->{taxincluded};
  1755. $tax{$ref->{accno}} = $ref->{rate};
  1756. }
  1757. $sth->finish;
  1758. $curr ||= $form->{defaultcurrency};
  1759. $taxincluded *= 1;
  1760. my $uid = localtime;
  1761. $uid .= "$$";
  1762. $query = qq|INSERT INTO oe (ordnumber)
  1763. VALUES ('$uid')|;
  1764. $dbh->do($query) || $form->dberror($query);
  1765. $query = qq|SELECT id FROM oe
  1766. WHERE ordnumber = '$uid'|;
  1767. $sth = $dbh->prepare($query);
  1768. $sth->execute || $form->dberror($query);
  1769. my ($id) = $sth->fetchrow_array;
  1770. $sth->finish;
  1771. $amount = 0;
  1772. $netamount = 0;
  1773. foreach my $parts_id (keys %{ $a{$vendor_id} }) {
  1774. if (($form->{$curr} * $form->{$a{$vendor_id}{$parts_id}{curr}}) > 0) {
  1775. $sellprice = $a{$vendor_id}{$parts_id}{lastcost} / $form->{$curr} * $form->{$a{$vendor_id}{$parts_id}{curr}};
  1776. } else {
  1777. $sellprice = $a{$vendor_id}{$parts_id}{lastcost};
  1778. }
  1779. $sellprice = $form->round_amount($sellprice, 2);
  1780. my $linetotal = $form->round_amount($sellprice * $a{$vendor_id}{$parts_id}{qty}, 2);
  1781. $query = qq|SELECT p.description, p.unit, c.accno FROM parts p
  1782. LEFT JOIN partstax pt ON (p.id = pt.parts_id)
  1783. LEFT JOIN chart c ON (c.id = pt.chart_id)
  1784. WHERE p.id = $parts_id|;
  1785. $sth = $dbh->prepare($query);
  1786. $sth->execute || $form->dberror($query);
  1787. my $rate = 0;
  1788. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1789. $description = $ref->{description};
  1790. $unit = $ref->{unit};
  1791. $rate += $tax{$ref->{accno}};
  1792. }
  1793. $sth->finish;
  1794. $netamount += $linetotal;
  1795. if ($taxincluded) {
  1796. $amount += $linetotal;
  1797. } else {
  1798. $amount += $form->round_amount($linetotal * (1 + $rate), 2);
  1799. }
  1800. $description = $dbh->quote($description);
  1801. $unit = $dbh->quote($unit);
  1802. $query = qq|INSERT INTO orderitems (trans_id, parts_id, description,
  1803. qty, ship, sellprice, unit) VALUES
  1804. ($id, $parts_id, $description,
  1805. $a{$vendor_id}{$parts_id}{qty}, 0, $sellprice, $unit)|;
  1806. $dbh->do($query) || $form->dberror($query);
  1807. }
  1808. my $ordnumber = $form->update_defaults($myconfig, 'ponumber');
  1809. my $null;
  1810. my $employee_id;
  1811. my $department_id;
  1812. ($null, $employee_id) = $form->get_employee($dbh);
  1813. ($null, $department_id) = split /--/, $form->{department};
  1814. $department_id *= 1;
  1815. $query = qq|UPDATE oe SET
  1816. ordnumber = |.$dbh->quote($ordnumber).qq|,
  1817. transdate = current_date,
  1818. vendor_id = $vendor_id,
  1819. customer_id = 0,
  1820. amount = $amount,
  1821. netamount = $netamount,
  1822. taxincluded = '$taxincluded',
  1823. curr = '$curr',
  1824. employee_id = $employee_id,
  1825. department_id = '$department_id',
  1826. ponumber = |.$dbh->quote($form->{ponumber}).qq|
  1827. WHERE id = $id|;
  1828. $dbh->do($query) || $form->dberror($query);
  1829. }
  1830. my $rc = $dbh->commit;
  1831. $dbh->disconnect;
  1832. $rc;
  1833. }
  1834. sub consolidate_orders {
  1835. my ($self, $myconfig, $form) = @_;
  1836. # connect to database
  1837. my $dbh = $form->dbconnect_noauto($myconfig);
  1838. my $i;
  1839. my $id;
  1840. my $ref;
  1841. my %oe = ();
  1842. my $query = qq|SELECT * FROM oe
  1843. WHERE id = ?|;
  1844. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1845. for ($i = 1; $i <= $form->{rowcount}; $i++) {
  1846. # retrieve order
  1847. if ($form->{"ndx_$i"}) {
  1848. $sth->execute($form->{"ndx_$i"});
  1849. $ref = $sth->fetchrow_hashref(NAME_lc);
  1850. $ref->{ndx} = $i;
  1851. $oe{oe}{$ref->{curr}}{$ref->{id}} = $ref;
  1852. $oe{vc}{$ref->{curr}}{$ref->{"$form->{vc}_id"}}++;
  1853. $sth->finish;
  1854. }
  1855. }
  1856. $query = qq|SELECT * FROM orderitems
  1857. WHERE trans_id = ?|;
  1858. $sth = $dbh->prepare($query) || $form->dberror($query);
  1859. foreach $curr (keys %{ $oe{oe} }) {
  1860. foreach $id (sort { $oe{oe}{$curr}{$a}->{ndx} <=> $oe{oe}{$curr}{$b}->{ndx} } keys %{ $oe{oe}{$curr} }) {
  1861. # retrieve order
  1862. $vc_id = $oe{oe}{$curr}{$id}->{"$form->{vc}_id"};
  1863. if ($oe{vc}{$oe{oe}{$curr}{$id}->{curr}}{$vc_id} > 1) {
  1864. push @{ $oe{orders}{$curr}{$vc_id} }, $id;
  1865. $sth->execute($id);
  1866. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1867. push @{ $oe{orderitems}{$curr}{$id} }, $ref;
  1868. }
  1869. $sth->finish;
  1870. }
  1871. }
  1872. }
  1873. my $ordnumber = $form->{ordnumber};
  1874. my $numberfld = ($form->{vc} eq 'customer') ? 'sonumber' : 'ponumber';
  1875. my ($department, $department_id) = $form->{department};
  1876. $department_id *= 1;
  1877. my $uid = localtime;
  1878. $uid .= "$$";
  1879. my @orderitems = ();
  1880. foreach $curr (keys %{ $oe{orders} }) {
  1881. foreach $vc_id (sort { $a <=> $b } keys %{ $oe{orders}{$curr} }) {
  1882. # the orders
  1883. @orderitems = ();
  1884. $form->{customer_id} = $form->{vendor_id} = 0;
  1885. $form->{"$form->{vc}_id"} = $vc_id;
  1886. $amount = 0;
  1887. $netamount = 0;
  1888. foreach $id (@{ $oe{orders}{$curr}{$vc_id} }) {
  1889. # header
  1890. $ref = $oe{oe}{$curr}{$id};
  1891. $amount += $ref->{amount};
  1892. $netamount += $ref->{netamount};
  1893. foreach $item (@{ $oe{orderitems}{$curr}{$id} }) {
  1894. push @orderitems, $item;
  1895. }
  1896. # close order
  1897. $query = qq|UPDATE oe SET
  1898. closed = '1'
  1899. WHERE id = $id|;
  1900. $dbh->do($query) || $form->dberror($query);
  1901. # reset shipped
  1902. $query = qq|UPDATE orderitems SET
  1903. ship = 0
  1904. WHERE trans_id = $id|;
  1905. $dbh->do($query) || $form->dberror($query);
  1906. }
  1907. $ordnumber ||= $form->update_defaults($myconfig, $numberfld, $dbh);
  1908. $query = qq|INSERT INTO oe (ordnumber)
  1909. VALUES ($uid)|;
  1910. $dbh->do($query) || $form->dberror($query);
  1911. $query = qq|SELECT id
  1912. FROM oe
  1913. WHERE ordnumber = '$uid'|;
  1914. ($id) = $dbh->selectrow_array($query);
  1915. $ref->{employee_id} *= 1;
  1916. $query = qq|UPDATE oe SET
  1917. ordnumber = |.$dbh->quote($ordnumber).qq|,
  1918. transdate = current_date,
  1919. vendor_id = $form->{vendor_id},
  1920. customer_id = $form->{customer_id},
  1921. amount = $amount,
  1922. netamount = $netamount,
  1923. reqdate = |.$form->dbquote($ref->{reqdate}, SQL_DATE).qq|,
  1924. taxincluded = '$ref->{taxincluded}',
  1925. shippingpoint = |.$dbh->quote($ref->{shippingpoint}).qq|,
  1926. notes = |.$dbh->quote($ref->{notes}).qq|,
  1927. curr = '$curr',
  1928. employee_id = $ref->{employee_id},
  1929. intnotes = |.$dbh->quote($ref->{intnotes}).qq|,
  1930. shipvia = |.$dbh->quote($ref->{shipvia}).qq|,
  1931. language_code = '$ref->{language_code}',
  1932. ponumber = |.$dbh->quote($form->{ponumber}).qq|,
  1933. department_id = $department_id
  1934. WHERE id = $id|;
  1935. $dbh->do($query) || $form->dberror($query);
  1936. # add items
  1937. foreach $item (@orderitems) {
  1938. for (qw(qty sellprice discount project_id ship)) { $item->{$_} *= 1 }
  1939. $query = qq|INSERT INTO orderitems (
  1940. trans_id, parts_id, description,
  1941. qty, sellprice, discount,
  1942. unit, reqdate, project_id,
  1943. ship, serialnumber, notes)
  1944. VALUES (
  1945. $id, $item->{parts_id}, |.$dbh->quote($item->{description}).qq|,
  1946. $item->{qty}, $item->{sellprice}, $item->{discount},
  1947. |.$dbh->quote($item->{unit}).qq|, |.$form->dbquote($item->{reqdate}, SQL_DATE).qq|, $item->{project_id},
  1948. $item->{ship}, |.$dbh->quote($item->{serialnumber}).qq|, |.$dbh->quote($item->{notes}).qq|)|;
  1949. $dbh->do($query) || $form->dberror($query);
  1950. }
  1951. }
  1952. }
  1953. $rc = $dbh->commit;
  1954. $dbh->disconnect;
  1955. $rc;
  1956. }
  1957. 1;