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