summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: 9e87055b33440b85cfeb7482ab052e3395c34e57 (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 whitespace cleanup
  27. #
  28. #======================================================================
  29. #
  30. # Order entry module
  31. # Quotation
  32. #
  33. #======================================================================
  34. package OE;
  35. sub transactions {
  36. my ($self, $myconfig, $form) = @_;
  37. # connect to database
  38. my $dbh = $form->{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. $form->{"qty_$i"} = $form->parse_amount(
  1170. $myconfig, $form->{"qty_$i"});
  1171. $form->{"ship_$i"} = $form->parse_amount(
  1172. $myconfig, $form->{"ship_$i"});
  1173. if ($form->{"qty_$i"}) {
  1174. $form->{totalqty} += $form->{"qty_$i"};
  1175. $form->{totalship} += $form->{"ship_$i"};
  1176. $form->{totalweight} += ($form->{"weight_$i"}
  1177. * $form->{"qty_$i"});
  1178. $form->{totalweightship} += ($form->{"weight_$i"}
  1179. * $form->{"ship_$i"});
  1180. # add number, description and qty to $form->{number}
  1181. push(@{ $form->{runningnumber} }, $runningnumber++);
  1182. push(@{ $form->{number} },
  1183. qq|$form->{"partnumber_$i"}|);
  1184. push(@{ $form->{sku} }, qq|$form->{"sku_$i"}|);
  1185. push(@{ $form->{description} },
  1186. qq|$form->{"description_$i"}|);
  1187. push(@{ $form->{itemnotes} }, $form->{"notes_$i"});
  1188. push(@{ $form->{qty} }, $form->format_amount(
  1189. $myconfig, $form->{"qty_$i"}));
  1190. push(@{ $form->{ship} }, $form->format_amount(
  1191. $myconfig, $form->{"ship_$i"}));
  1192. push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
  1193. push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
  1194. push(@{ $form->{serialnumber} },
  1195. qq|$form->{"serialnumber_$i"}|);
  1196. push(@{ $form->{requiredate} },
  1197. qq|$form->{"reqdate_$i"}|);
  1198. push(@{ $form->{projectnumber} },
  1199. qq|$form->{"projectnumber_$i"}|);
  1200. push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
  1201. push(@{ $form->{listprice} }, $form->{"listprice_$i"});
  1202. push(@{ $form->{weight} }, $form->format_amount(
  1203. $myconfig,
  1204. $form->{"weight_$i"} * $form->{"ship_$i"}));
  1205. my $sellprice = $form->parse_amount(
  1206. $myconfig, $form->{"sellprice_$i"});
  1207. my ($dec) = ($sellprice =~ /\.(\d+)/);
  1208. $dec = length $dec;
  1209. my $decimalplaces = ($dec > 2) ? $dec : 2;
  1210. my $discount = $form->round_amount(
  1211. $sellprice * $form->parse_amount(
  1212. $myconfig,
  1213. $form->{"discount_$i"}) / 100,
  1214. $decimalplaces);
  1215. # keep a netprice as well, (sellprice - discount)
  1216. $form->{"netprice_$i"} = $sellprice - $discount;
  1217. my $linetotal = $form->round_amount(
  1218. $form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
  1219. if ($form->{"inventory_accno_id_$i"}
  1220. || $form->{"assembly_$i"}) {
  1221. push(@{ $form->{part} }, $form->{"sku_$i"});
  1222. push(@{ $form->{service} }, NULL);
  1223. $form->{totalparts} += $linetotal;
  1224. } else {
  1225. push(@{ $form->{service} }, $form->{"sku_$i"});
  1226. push(@{ $form->{part} }, NULL);
  1227. $form->{totalservices} += $linetotal;
  1228. }
  1229. push(@{ $form->{netprice} },
  1230. ($form->{"netprice_$i"})
  1231. ? $form->format_amount(
  1232. $myconfig,
  1233. $form->{"netprice_$i"},
  1234. $decimalplaces)
  1235. : " ");
  1236. $discount = ($discount)
  1237. ? $form->format_amount(
  1238. $myconfig,
  1239. $discount * -1,
  1240. $decimalplaces)
  1241. : " ";
  1242. push(@{ $form->{discount} }, $discount);
  1243. push(@{ $form->{discountrate} },
  1244. $form->format_amount($myconfig,
  1245. $form->{"discount_$i"}));
  1246. $form->{ordtotal} += $linetotal;
  1247. # this is for the subtotals for grouping
  1248. $subtotal += $linetotal;
  1249. $form->{"linetotal_$i"} = $form->format_amount(
  1250. $myconfig, $linetotal, 2);
  1251. push(@{ $form->{linetotal} }, $form->{"linetotal_$i"});
  1252. @taxaccounts = split / /, $form->{"taxaccounts_$i"};
  1253. my $ml = 1;
  1254. my @taxrates = ();
  1255. $tax = 0;
  1256. for (0 .. 1) {
  1257. $taxrate = 0;
  1258. for (@taxaccounts) {
  1259. $taxrate += $form->{"${_}_rate"}
  1260. if ($form->{"${_}_rate"} * $ml)
  1261. > 0;
  1262. }
  1263. $taxrate *= $ml;
  1264. $taxamount = $linetotal * $taxrate
  1265. / (1 + $taxrate);
  1266. $taxbase = ($linetotal - $taxamount);
  1267. foreach $item (@taxaccounts) {
  1268. if (($form->{"${item}_rate"} * $ml)
  1269. > 0) {
  1270. push @taxrates,
  1271. $form->{"${item}_rate"}
  1272. * 100;
  1273. if ($form->{taxincluded}) {
  1274. $taxaccounts{$item}
  1275. += $linetotal
  1276. * $form->{"${item}_rate"}
  1277. /
  1278. (1 + $taxrate);
  1279. $taxbase{$item}
  1280. += $taxbase;
  1281. } else {
  1282. $taxbase{$item}
  1283. += $linetotal;
  1284. $taxaccounts{$item}
  1285. += $linetotal
  1286. * $form->{"${item}_rate"};
  1287. }
  1288. }
  1289. }
  1290. if ($form->{taxincluded}) {
  1291. $tax += $linetotal
  1292. * ($taxrate
  1293. / (1 + ($taxrate * $ml)));
  1294. } else {
  1295. $tax += $linetotal * $taxrate;
  1296. }
  1297. $ml *= -1;
  1298. }
  1299. push(@{ $form->{lineitems} },
  1300. { amount => $linetotal,
  1301. tax => $form->round_amount($tax, 2) });
  1302. push(@{ $form->{taxrates} },
  1303. join ' ', sort { $a <=> $b } @taxrates);
  1304. if ($form->{"assembly_$i"}) {
  1305. $form->{stagger} = -1;
  1306. &assembly_details($myconfig,
  1307. $form, $dbh, $form->{"id_$i"},
  1308. $oid{$myconfig->{dbdriver}},
  1309. $form->{"qty_$i"});
  1310. }
  1311. }
  1312. # add subtotal
  1313. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  1314. if ($subtotal) {
  1315. if ($j < $k) {
  1316. # look at next item
  1317. if ($sortlist[$j]->[1] ne $sameitem) {
  1318. if ($form->{"inventory_accno_id_$i"}
  1319. || $form->{"assembly_$i"}) {
  1320. push(@{ $form->{part} },
  1321. "");
  1322. push(@{
  1323. $form->{service}
  1324. }, NULL);
  1325. } else {
  1326. push(@{
  1327. $form->{service}
  1328. }, "");
  1329. push(@{ $form->{part} },
  1330. NULL);
  1331. }
  1332. for (qw(
  1333. taxrates runningnumber
  1334. number sku qty ship unit
  1335. bin serialnumber
  1336. requiredate
  1337. projectnumber sellprice
  1338. listprice netprice
  1339. discount discountrate
  1340. weight itemnotes)
  1341. ) {
  1342. push(@{ $form->{$_} },
  1343. "");
  1344. }
  1345. push(@{ $form->{description} },
  1346. $form->{groupsubtotaldescription});
  1347. push(@{ $form->{lineitems} },
  1348. { amount => 0,
  1349. tax => 0 });
  1350. if ($form->{groupsubtotaldescription}
  1351. ne "") {
  1352. push(@{
  1353. $form->{linetotal}
  1354. },
  1355. $form->format_amount($myconfig, $subtotal, 2));
  1356. } else {
  1357. push(@{
  1358. $form->{linetotal}
  1359. }, "");
  1360. }
  1361. $subtotal = 0;
  1362. }
  1363. } else {
  1364. # got last item
  1365. if ($form->{groupsubtotaldescription}
  1366. ne "") {
  1367. if ($form->{"inventory_accno_id_$i"}
  1368. || $form->{"assembly_$i"}) {
  1369. push(@{ $form->{part} },
  1370. "");
  1371. push(@{
  1372. $form->{service}
  1373. }, NULL);
  1374. } else {
  1375. push(@{
  1376. $form->{service}
  1377. }, "");
  1378. push(@{ $form->{part} },
  1379. NULL);
  1380. }
  1381. for (qw(
  1382. taxrates runningnumber
  1383. number sku qty ship unit
  1384. bin serialnumber
  1385. requiredate
  1386. projectnumber sellprice
  1387. listprice netprice
  1388. discount discountrate
  1389. weight itemnotes)
  1390. ) {
  1391. push(@{ $form->{$_} },
  1392. "");
  1393. }
  1394. push(@{ $form->{description} },
  1395. $form->{groupsubtotaldescription});
  1396. push(@{ $form->{linetotal} },
  1397. $form->format_amount(
  1398. $myconfig,
  1399. $subtotal,
  1400. 2));
  1401. push(@{ $form->{lineitems} },
  1402. { amount => 0,
  1403. tax => 0 });
  1404. }
  1405. }
  1406. }
  1407. }
  1408. }
  1409. $tax = 0;
  1410. foreach $item (sort keys %taxaccounts) {
  1411. if ($form->round_amount($taxaccounts{$item}, 2)) {
  1412. $tax += $taxamount = $form->round_amount(
  1413. $taxaccounts{$item}, 2);
  1414. push(@{ $form->{taxbaseinclusive} },
  1415. $form->{"${item}_taxbaseinclusive"}
  1416. = $form->round_amount(
  1417. $taxbase{$item} + $tax, 2));
  1418. push(@{ $form->{taxbase} },
  1419. $form->{"${item}_taxbase"}
  1420. = $form->format_amount($myconfig,
  1421. $taxbase{$item}, 2));
  1422. push(@{ $form->{tax} },
  1423. $form->{"${item}_tax"}
  1424. = $form->format_amount($myconfig,
  1425. $taxamount, 2));
  1426. push(@{ $form->{taxdescription} },
  1427. $form->{"${item}_description"});
  1428. $form->{"${item}_taxrate"} =
  1429. $form->format_amount($myconfig,
  1430. $form->{"${item}_rate"} * 100);
  1431. push(@{ $form->{taxrate} }, $form->{"${item}_taxrate"});
  1432. push(@{ $form->{taxnumber} },
  1433. $form->{"${item}_taxnumber"});
  1434. }
  1435. }
  1436. # adjust taxes for lineitems
  1437. my $total = 0;
  1438. for (@{ $form->{lineitems} }) {
  1439. $total += $_->{tax};
  1440. }
  1441. if ($form->round_amount($total,2) != $form->round_amount($tax,2)) {
  1442. # get largest amount
  1443. for (reverse sort { $a->{tax} <=> $b->{tax} }
  1444. @{ $form->{lineitems} }) {
  1445. $_->{tax} -= $total - $tax;
  1446. last;
  1447. }
  1448. }
  1449. $i = 1;
  1450. for (@{ $form->{lineitems} }) {
  1451. push(@{ $form->{linetax} },
  1452. $form->format_amount($myconfig, $_->{tax}, 2, ""));
  1453. }
  1454. for (qw(totalparts totalservices)) {
  1455. $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2);
  1456. }
  1457. for (qw(totalqty totalship totalweight)) {
  1458. $form->{$_} = $form->format_amount($myconfig, $form->{$_});
  1459. }
  1460. $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal},
  1461. 2);
  1462. $form->{ordtotal} = ($form->{taxincluded})
  1463. ? $form->{ordtotal}
  1464. : $form->{ordtotal} + $tax;
  1465. use LedgerSMB::CP;
  1466. my $c;
  1467. if ($form->{language_code} ne "") {
  1468. $c = new CP $form->{language_code};
  1469. } else {
  1470. $c = new CP $myconfig->{countrycode};
  1471. }
  1472. $c->init;
  1473. my $whole;
  1474. ($whole, $form->{decimal}) = split /\./, $form->{ordtotal};
  1475. $form->{decimal} .= "00";
  1476. $form->{decimal} = substr($form->{decimal}, 0, 2);
  1477. $form->{text_decimal} = $c->num2text($form->{decimal} * 1);
  1478. $form->{text_amount} = $c->num2text($whole);
  1479. $form->{integer_amount} = $form->format_amount($myconfig, $whole);
  1480. # format amounts
  1481. $form->{quototal} = $form->{ordtotal} =
  1482. $form->format_amount($myconfig, $form->{ordtotal}, 2);
  1483. $form->format_string(qw(text_amount text_decimal));
  1484. $query = qq|SELECT weightunit FROM defaults|;
  1485. ($form->{weightunit}) = $dbh->selectrow_array($query);
  1486. $dbh->commit;
  1487. }
  1488. sub assembly_details {
  1489. my ($myconfig, $form, $dbh, $id, $oid, $qty) = @_;
  1490. my $sm = "";
  1491. my $spacer;
  1492. $form->{stagger}++;
  1493. if ($form->{format} eq 'html') {
  1494. $spacer = "&nbsp;" x (3 * ($form->{stagger} - 1))
  1495. if $form->{stagger} > 1;
  1496. }
  1497. if ($form->{format} =~ /(postscript|pdf)/) {
  1498. if ($form->{stagger} > 1) {
  1499. $spacer = ($form->{stagger} - 1) * 3;
  1500. $spacer = '\rule{'.$spacer.'mm}{0mm}';
  1501. }
  1502. }
  1503. # get parts and push them onto the stack
  1504. my $sortorder = "";
  1505. if ($form->{grouppartsgroup}) {
  1506. $sortorder = qq|ORDER BY pg.partsgroup, a.id|;
  1507. } else {
  1508. $sortorder = qq|ORDER BY a.id|;
  1509. }
  1510. my $where = ($form->{formname} eq 'work_order')
  1511. ? "1 = 1"
  1512. : "a.bom = '1'";
  1513. my $query = qq|
  1514. SELECT p.partnumber, p.description, p.unit, a.qty,
  1515. pg.partsgroup, p.partnumber AS sku, p.assembly, p.id,
  1516. p.bin
  1517. FROM assembly a
  1518. JOIN parts p ON (a.parts_id = p.id)
  1519. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1520. WHERE $where
  1521. AND a.id = ?
  1522. $sortorder|;
  1523. my $sth = $dbh->prepare($query);
  1524. $sth->execute($id) || $form->dberror($query);
  1525. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1526. for (qw(partnumber description partsgroup)) {
  1527. $form->{"a_$_"} = $ref->{$_};
  1528. $form->format_string("a_$_");
  1529. }
  1530. if ($form->{grouppartsgroup} && $ref->{partsgroup} ne $sm) {
  1531. for (qw(
  1532. taxrates number sku unit qty runningnumber ship
  1533. bin serialnumber requiredate projectnumber
  1534. sellprice listprice netprice discount
  1535. discountrate linetotal weight itemnotes)
  1536. ) {
  1537. push(@{ $form->{$_} }, "");
  1538. }
  1539. $sm = ($form->{"a_partsgroup"})
  1540. ? $form->{"a_partsgroup"} : "";
  1541. push(@{ $form->{description} }, "$spacer$sm");
  1542. push(@{ $form->{lineitems} },
  1543. { amount => 0, tax => 0 });
  1544. }
  1545. if ($form->{stagger}) {
  1546. push(@{ $form->{description} },
  1547. qq|$spacer$form->{"a_partnumber"}, |.
  1548. qq|$form->{"a_description"}|);
  1549. for (qw(
  1550. taxrates number sku runningnumber ship
  1551. serialnumber requiredate projectnumber
  1552. sellprice listprice netprice discount
  1553. discountrate linetotal weight itemnotes)
  1554. ) {
  1555. push(@{ $form->{$_} }, "")
  1556. }
  1557. } else {
  1558. push(@{ $form->{description} },
  1559. qq|$form->{"a_description"}|);
  1560. push(@{ $form->{sku} }, $form->{"a_partnumber"});
  1561. push(@{ $form->{number} }, $form->{"a_partnumber"});
  1562. for (qw(
  1563. taxrates runningnumber ship serialnumber
  1564. requiredate projectnumber sellprice listprice
  1565. netprice discount discountrate linetotal weight
  1566. itemnotes)
  1567. ) {
  1568. push(@{ $form->{$_} }, "")
  1569. }
  1570. }
  1571. push(@{ $form->{lineitems} }, { amount => 0, tax => 0 });
  1572. push(@{ $form->{qty} }, $form->format_amount(
  1573. $myconfig, $ref->{qty} * $qty));
  1574. for (qw(unit bin)) {
  1575. $form->{"a_$_"} = $ref->{$_};
  1576. $form->format_string("a_$_");
  1577. push(@{ $form->{$_} }, $form->{"a_$_"});
  1578. }
  1579. if ($ref->{assembly} && $form->{formname} eq 'work_order') {
  1580. &assembly_details(
  1581. $myconfig, $form, $dbh, $ref->{id}, $oid,
  1582. $ref->{qty} * $qty);
  1583. }
  1584. }
  1585. $sth->finish;
  1586. $form->{stagger}--;
  1587. }
  1588. sub project_description {
  1589. my ($self, $dbh, $id) = @_;
  1590. my $query = qq|
  1591. SELECT description
  1592. FROM project
  1593. WHERE id = $id|;
  1594. ($_) = $dbh->selectrow_array($query);
  1595. $_;
  1596. }
  1597. sub get_warehouses {
  1598. my ($self, $myconfig, $form) = @_;
  1599. my $dbh = $form->dbconnect($myconfig);
  1600. # setup warehouses
  1601. my $query = qq|
  1602. SELECT id, description
  1603. FROM warehouse
  1604. ORDER BY 2|;
  1605. my $sth = $dbh->prepare($query);
  1606. $sth->execute || $form->dberror($query);
  1607. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1608. push @{ $form->{all_warehouse} }, $ref;
  1609. }
  1610. $sth->finish;
  1611. $dbh->disconnect;
  1612. }
  1613. sub save_inventory {
  1614. my ($self, $myconfig, $form) = @_;
  1615. my ($null, $warehouse_id) = split /--/, $form->{warehouse};
  1616. $warehouse_id *= 1;
  1617. my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
  1618. my $dbh = $form->dbconnect_noauto($myconfig);
  1619. my $sth;
  1620. my $wth;
  1621. my $serialnumber;
  1622. my $ship;
  1623. my ($null, $employee_id) = split /--/, $form->{employee};
  1624. ($null, $employee_id) = $form->get_employee($dbh) if ! $employee_id;
  1625. $query = qq|
  1626. SELECT serialnumber, ship
  1627. FROM orderitems
  1628. WHERE trans_id = ?
  1629. AND id = ?
  1630. FOR UPDATE|;
  1631. $sth = $dbh->prepare($query) || $form->dberror($query);
  1632. $query = qq|
  1633. SELECT sum(qty)
  1634. FROM inventory
  1635. WHERE parts_id = ?
  1636. AND warehouse_id = ?|;
  1637. $wth = $dbh->prepare($query) || $form->dberror($query);
  1638. for my $i (1 .. $form->{rowcount}) {
  1639. $ship = (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
  1640. ? $form->{"qty_$i"}
  1641. : $form->{"ship_$i"};
  1642. if ($warehouse_id && $form->{type} eq 'ship_order') {
  1643. $wth->execute($form->{"id_$i"}, $warehouse_id)
  1644. || $form->dberror;
  1645. ($qty) = $wth->fetchrow_array;
  1646. $wth->finish;
  1647. if ($ship > $qty) {
  1648. $ship = $qty;
  1649. }
  1650. }
  1651. if ($ship) {
  1652. if (!$form->{shippingdate}){
  1653. $form->{shippingdate} = undef;
  1654. }
  1655. $ship *= $ml;
  1656. $query = qq|
  1657. INSERT INTO inventory
  1658. (parts_id, warehouse_id, qty, trans_id,
  1659. orderitems_id, shippingdate,
  1660. employee_id)
  1661. VALUES
  1662. (?, ?, ?, ?, ?, ?, ?)|;
  1663. $sth2 = $dbh->prepare($query);
  1664. $sth2->execute(
  1665. $form->{"id_$i"}, $warehouse_id,
  1666. $ship, $form->{"id"},
  1667. $form->{"orderitems_id_$i"},
  1668. $form->{shippingdate},
  1669. $employee_id
  1670. ) || $form->dberror($query);
  1671. $sth2->finish;
  1672. # add serialnumber, ship to orderitems
  1673. $sth->execute(
  1674. $form->{id}, $form->{"orderitems_id_$i"})
  1675. || $form->dberror;
  1676. ($serialnumber, $ship) = $sth->fetchrow_array;
  1677. $sth->finish;
  1678. $serialnumber .= " " if $serialnumber;
  1679. $serialnumber .= qq|$form->{"serialnumber_$i"}|;
  1680. $ship += $form->{"ship_$i"};
  1681. $query = qq|
  1682. UPDATE orderitems SET
  1683. serialnumber = '$serialnumber',
  1684. ship = $ship,
  1685. reqdate = '$form->{shippingdate}'
  1686. WHERE trans_id = $form->{id}
  1687. AND id = $form->{"orderitems_id_$i"}|;
  1688. $sth2 = $dbh->prepare($query);
  1689. $sth2->execute(
  1690. $serialnumber, $ship,
  1691. $form->{shippingdate}, $form->{id},
  1692. $form->{"orderitems_id_$i"}
  1693. ) || $form->dberror($query);
  1694. $sth2->finish;
  1695. # update order with ship via
  1696. $query = qq|
  1697. UPDATE oe SET
  1698. shippingpoint = ?,
  1699. shipvia = ?
  1700. WHERE id = ?|;
  1701. $sth2 = $dbh->prepare($query);
  1702. $sth2->execute(
  1703. $form->{shippingpoint}, $form->{shipvia},
  1704. $form->{id}
  1705. ) || $form->dberror($query);
  1706. $sth2->finish;
  1707. # update onhand for parts
  1708. $form->update_balance($dbh,
  1709. "parts",
  1710. "onhand",
  1711. qq|id = $form->{"id_$i"}|,
  1712. $form->{"ship_$i"} * $ml);
  1713. }
  1714. }
  1715. my $rc = $dbh->commit;
  1716. $rc;
  1717. }
  1718. sub adj_onhand {
  1719. my ($dbh, $form, $ml) = @_;
  1720. my $query = qq|
  1721. SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
  1722. FROM orderitems oi
  1723. JOIN parts p ON (p.id = oi.parts_id)
  1724. WHERE oi.trans_id = ?|;
  1725. my $sth = $dbh->prepare($query);
  1726. $sth->execute($form->{id}) || $form->dberror($query);
  1727. $query = qq|
  1728. SELECT sum(p.inventory_accno_id), p.assembly
  1729. FROM parts p
  1730. JOIN assembly a ON (a.parts_id = p.id)
  1731. WHERE a.id = ?
  1732. GROUP BY p.assembly|;
  1733. my $ath = $dbh->prepare($query) || $form->dberror($query);
  1734. my $ref;
  1735. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1736. if ($ref->{inventory_accno_id} || $ref->{assembly}) {
  1737. # do not update if assembly consists of all services
  1738. if ($ref->{assembly}) {
  1739. $ath->execute($ref->{parts_id})
  1740. || $form->dberror($query);
  1741. my ($inv, $assembly) = $ath->fetchrow_array;
  1742. $ath->finish;
  1743. next unless ($inv || $assembly);
  1744. }
  1745. # adjust onhand in parts table
  1746. $form->update_balance($dbh,
  1747. "parts",
  1748. "onhand",
  1749. qq|id = $ref->{parts_id}|,
  1750. $ref->{ship} * $ml);
  1751. }
  1752. }
  1753. $sth->finish;
  1754. }
  1755. sub adj_inventory {
  1756. my ($dbh, $myconfig, $form) = @_;
  1757. # increase/reduce qty in inventory table
  1758. my $query = qq|
  1759. SELECT oi.id, oi.parts_id, oi.ship
  1760. FROM orderitems oi
  1761. WHERE oi.trans_id = ?|;
  1762. my $sth = $dbh->prepare($query);
  1763. $sth->execute($form->{id}) || $form->dberror($query);
  1764. my $id = $dbh->quote($form->{id});
  1765. $query = qq|
  1766. SELECT qty,
  1767. (SELECT SUM(qty) FROM inventory
  1768. WHERE trans_id = $id
  1769. AND orderitems_id = ?) AS total
  1770. FROM inventory
  1771. WHERE trans_id = $id
  1772. AND orderitems_id = ?|;
  1773. my $ith = $dbh->prepare($query) || $form->dberror($query);
  1774. my $qty;
  1775. my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
  1776. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1777. $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
  1778. my $ship = $ref->{ship};
  1779. while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
  1780. if (($qty = (($inv->{total} * $ml) - $ship)) >= 0) {
  1781. $qty = $inv->{qty} * $ml
  1782. if ($qty > ($inv->{qty} * $ml));
  1783. $form->update_balance($dbh,
  1784. "inventory",
  1785. "qty",
  1786. qq|$oid{$myconfig->{dbdriver}} |.
  1787. qq|= $inv->{oid}|,
  1788. $qty * -1 * $ml);
  1789. $ship -= $qty;
  1790. }
  1791. }
  1792. $ith->finish;
  1793. }
  1794. $sth->finish;
  1795. # delete inventory entries if qty = 0
  1796. $query = qq|
  1797. DELETE FROM inventory
  1798. WHERE trans_id = ?
  1799. AND qty = 0|;
  1800. $sth = $dbh->prepare($query);
  1801. $sth->execute($form->{id}) || $form->dberror($query);
  1802. }
  1803. sub get_inventory {
  1804. my ($self, $myconfig, $form) = @_;
  1805. my $where;
  1806. my $query;
  1807. my $null;
  1808. my $fromwarehouse_id;
  1809. my $towarehouse_id;
  1810. my $var;
  1811. my $dbh = $form->{dbh};
  1812. if ($form->{partnumber} ne "") {
  1813. $var = $dbh->quote($form->like(lc $form->{partnumber}));
  1814. $where .= "
  1815. AND lower(p.partnumber) LIKE '$var'";
  1816. }
  1817. if ($form->{description} ne "") {
  1818. $var = $dbh->quote($form->like(lc $form->{description}));
  1819. $where .= "
  1820. AND lower(p.description) LIKE '$var'";
  1821. }
  1822. if ($form->{partsgroup} ne "") {
  1823. ($null, $var) = split /--/, $form->{partsgroup};
  1824. $var = $dbh->quote($var);
  1825. $where .= "
  1826. AND pg.id = $var";
  1827. }
  1828. ($null, $fromwarehouse_id) = split /--/, $form->{fromwarehouse};
  1829. $fromwarehouse_id = $dbh->quote($fromwarehouse_id);
  1830. ($null, $towarehouse_id) = split /--/, $form->{towarehouse};
  1831. $towarehouse_id = $dbh->quote($towarehouse_id);
  1832. my %ordinal = (
  1833. partnumber => 2,
  1834. description => 3,
  1835. partsgroup => 5,
  1836. warehouse => 6,
  1837. );
  1838. my @a = (partnumber, warehouse);
  1839. my $sortorder = $form->sort_order(\@a, \%ordinal);
  1840. if ($fromwarehouse_id) {
  1841. if ($towarehouse_id) {
  1842. $where .= "
  1843. AND NOT i.warehouse_id = $towarehouse_id";
  1844. }
  1845. $query = qq|
  1846. SELECT p.id, p.partnumber, p.description,
  1847. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1848. pg.partsgroup, w.description AS warehouse,
  1849. i.warehouse_id
  1850. FROM inventory i
  1851. JOIN parts p ON (p.id = i.parts_id)
  1852. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1853. JOIN warehouse w ON (w.id = i.warehouse_id)
  1854. WHERE i.warehouse_id = $fromwarehouse_id
  1855. $where
  1856. GROUP BY p.id, p.partnumber, p.description,
  1857. pg.partsgroup, w.description, i.warehouse_id
  1858. ORDER BY $sortorder|;
  1859. } else {
  1860. if ($towarehouse_id) {
  1861. $query = qq|
  1862. SELECT p.id, p.partnumber, p.description,
  1863. p.onhand,
  1864. (SELECT SUM(qty)
  1865. FROM inventory i
  1866. WHERE i.parts_id = p.id) AS qty,
  1867. pg.partsgroup, '' AS warehouse,
  1868. 0 AS warehouse_id
  1869. FROM parts p
  1870. LEFT JOIN partsgroup pg
  1871. ON (p.partsgroup_id = pg.id)
  1872. WHERE p.onhand > 0
  1873. $where
  1874. UNION|;
  1875. }
  1876. $query .= qq|
  1877. SELECT p.id, p.partnumber, p.description,
  1878. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1879. pg.partsgroup, w.description AS warehouse,
  1880. i.warehouse_id
  1881. FROM inventory i
  1882. JOIN parts p ON (p.id = i.parts_id)
  1883. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1884. JOIN warehouse w ON (w.id = i.warehouse_id)
  1885. WHERE i.warehouse_id != $towarehouse_id
  1886. $where
  1887. GROUP BY p.id, p.partnumber, p.description,
  1888. pg.partsgroup, w.description, i.warehouse_id
  1889. ORDER BY $sortorder|;
  1890. }
  1891. my $sth = $dbh->prepare($query);
  1892. $sth->execute || $form->dberror($query);
  1893. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1894. $ref->{qty} = $ref->{onhand} - $ref->{qty};
  1895. push @{ $form->{all_inventory} }, $ref if $ref->{qty} > 0;
  1896. }
  1897. $sth->finish;
  1898. $dbh->commit;
  1899. }
  1900. sub transfer {
  1901. my ($self, $myconfig, $form) = @_;
  1902. my $dbh = $form->dbconnect_noauto($myconfig);
  1903. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
  1904. my @a = localtime;
  1905. $a[5] += 1900;
  1906. $a[4]++;
  1907. $a[4] = substr("0$a[4]", -2);
  1908. $a[3] = substr("0$a[3]", -2);
  1909. $shippingdate = "$a[5]$a[4]$a[3]";
  1910. my %total = ();
  1911. my $query = qq|
  1912. INSERT INTO inventory
  1913. (warehouse_id, parts_id, qty, shippingdate, employee_id)
  1914. VALUES (?, ?, ?, '$shippingdate', $form->{employee_id})|;
  1915. $sth = $dbh->prepare($query) || $form->dberror($query);
  1916. my $qty;
  1917. for my $i (1 .. $form->{rowcount}) {
  1918. $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
  1919. $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
  1920. if ($qty > 0) {
  1921. # to warehouse
  1922. if ($form->{warehouse_id}) {
  1923. $sth->execute($form->{warehouse_id},
  1924. $form->{"id_$i"}, $qty,
  1925. $shippingdate, $form->{employee_id})
  1926. || $form->dberror;
  1927. $sth->finish;
  1928. }
  1929. # from warehouse
  1930. if ($form->{"warehouse_id_$i"}) {
  1931. $sth->execute($form->{"warehouse_id_$i"},
  1932. $form->{"id_$i"}, $qty * -1)
  1933. || $form->dberror;
  1934. $sth->finish;
  1935. }
  1936. }
  1937. }
  1938. my $rc = $dbh->commit;
  1939. $dbh->commit;
  1940. $rc;
  1941. }
  1942. sub get_soparts {
  1943. my ($self, $myconfig, $form) = @_;
  1944. # connect to database
  1945. my $dbh = $form->{dbh};
  1946. my $id;
  1947. my $ref;
  1948. # store required items from selected sales orders
  1949. my $query = qq|
  1950. SELECT p.id, oi.qty - oi.ship AS required, p.assembly
  1951. FROM orderitems oi
  1952. JOIN parts p ON (p.id = oi.parts_id)
  1953. WHERE oi.trans_id = ?|;
  1954. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1955. for (my $i = 1; $i <= $form->{rowcount}; $i++) {
  1956. if ($form->{"ndx_$i"}) {
  1957. $sth->execute($form->{"ndx_$i"});
  1958. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1959. &add_items_required(
  1960. "", $dbh, $form, $ref->{id},
  1961. $ref->{required}, $ref->{assembly});
  1962. }
  1963. $sth->finish;
  1964. }
  1965. }
  1966. $query = qq|SELECT current_date FROM defaults|;
  1967. ($form->{transdate}) = $dbh->selectrow_array($query);
  1968. # foreign exchange rates
  1969. &exchangerate_defaults($dbh, $form);
  1970. $dbh->disconnect;
  1971. }
  1972. sub add_items_required {
  1973. my ($self, $dbh, $form, $parts_id, $required, $assembly) = @_;
  1974. my $query;
  1975. my $sth;
  1976. my $ref;
  1977. if ($assembly) {
  1978. $query = qq|
  1979. SELECT p.id, a.qty, p.assembly
  1980. FROM assembly a
  1981. JOIN parts p ON (p.id = a.parts_id)
  1982. WHERE a.id = ?|;
  1983. $sth = $dbh->prepare($query);
  1984. $sth->execute || $form->dberror($query);
  1985. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1986. &add_items_required(
  1987. "", $dbh, $form, $ref->{id},
  1988. $required * $ref->{qty}, $ref->{assembly});
  1989. }
  1990. $sth->finish;
  1991. } else {
  1992. $query = qq|
  1993. SELECT partnumber, description, lastcost
  1994. FROM parts
  1995. WHERE id = ?|;
  1996. $sth = $dbh->prepare($query);
  1997. $sth->execute($parts_id) || $form->dberror($query);
  1998. $ref = $sth->fetchrow_hashref(NAME_lc);
  1999. for (keys %$ref) {
  2000. $form->{orderitems}{$parts_id}{$_} = $ref->{$_};
  2001. }
  2002. $sth->finish;
  2003. $form->{orderitems}{$parts_id}{required} += $required;
  2004. $query = qq|
  2005. SELECT pv.partnumber, pv.leadtime, pv.lastcost, pv.curr,
  2006. pv.vendor_id, v.name
  2007. FROM partsvendor pv
  2008. JOIN vendor v ON (v.id = pv.vendor_id)
  2009. WHERE pv.parts_id = ?|;
  2010. $sth = $dbh->prepare($query) || $form->dberror($query);
  2011. # get cost and vendor
  2012. $sth->execute($parts_id);
  2013. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  2014. for (keys %$ref) {
  2015. $form->{orderitems}{$parts_id}{partsvendor}
  2016. {$ref->{vendor_id}}{$_} = $ref->{$_};
  2017. }
  2018. }
  2019. $sth->finish;
  2020. }
  2021. }
  2022. sub generate_orders {
  2023. my ($self, $myconfig, $form) = @_;
  2024. my $i;
  2025. my %a;
  2026. my $query;
  2027. my $sth;
  2028. for ($i = 1; $i <= $form->{rowcount}; $i++) {
  2029. for (qw(qty lastcost)) {
  2030. $form->{"${_}_$i"} = $form->parse_amount(
  2031. $myconfig, $form->{"${_}_$i"});
  2032. }
  2033. if ($form->{"qty_$i"}) {
  2034. ($vendor, $vendor_id) =
  2035. split /--/, $form->{"vendor_$i"};
  2036. if ($vendor_id) {
  2037. $a{$vendor_id}{$form->{"id_$i"}}{qty}
  2038. += $form->{"qty_$i"};
  2039. for (qw(curr lastcost)) {
  2040. $a{$vendor_id}{$form->{"id_$i"}}{$_}
  2041. = $form->{"${_}_$i"};
  2042. }
  2043. }
  2044. }
  2045. }
  2046. # connect to database
  2047. my $dbh = $form->dbconnect_noauto($myconfig);
  2048. # foreign exchange rates
  2049. &exchangerate_defaults($dbh, $form);
  2050. my $amount;
  2051. my $netamount;
  2052. my $curr = "";
  2053. my %tax;
  2054. my $taxincluded = 0;
  2055. my $vendor_id;
  2056. my $description;
  2057. my $unit;
  2058. my $sellprice;
  2059. foreach $vendor_id (keys %a) {
  2060. %tax = ();
  2061. $query = qq|
  2062. SELECT v.curr, v.taxincluded, t.rate, c.accno
  2063. FROM vendor v
  2064. LEFT JOIN vendortax vt ON (v.id = vt.vendor_id)
  2065. LEFT JOIN tax t ON (t.chart_id = vt.chart_id)
  2066. LEFT JOIN chart c ON (c.id = t.chart_id)
  2067. WHERE v.id = ?|;
  2068. $sth = $dbh->prepare($query);
  2069. $sth->execute($vendor_id) || $form->dberror($query);
  2070. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  2071. $curr = $ref->{curr};
  2072. $taxincluded = $ref->{taxincluded};
  2073. $tax{$ref->{accno}} = $ref->{rate};
  2074. }
  2075. $sth->finish;
  2076. $curr ||= $form->{defaultcurrency};
  2077. $taxincluded *= 1;
  2078. my $uid = localtime;
  2079. $uid .= "$$";
  2080. # TODO: Make this function insert as much as possible
  2081. $query = qq|
  2082. INSERT INTO oe (ordnumber)
  2083. VALUES ('$uid')|;
  2084. $dbh->do($query) || $form->dberror($query);
  2085. $query = qq|SELECT id FROM oe WHERE ordnumber = '$uid'|;
  2086. $sth = $dbh->prepare($query);
  2087. $sth->execute || $form->dberror($query);
  2088. my ($id) = $sth->fetchrow_array;
  2089. $sth->finish;
  2090. $amount = 0;
  2091. $netamount = 0;
  2092. foreach my $parts_id (keys %{ $a{$vendor_id} }) {
  2093. if (($form->{$curr}
  2094. * $form->{$a{$vendor_id}{$parts_id}{curr}}) > 0) {
  2095. $sellprice = $a{$vendor_id}{$parts_id}{lastcost}
  2096. / $form->{$curr}
  2097. * $form->{$a{$vendor_id}{$parts_id}
  2098. {curr}};
  2099. } else {
  2100. $sellprice =
  2101. $a{$vendor_id}{$parts_id}{lastcost};
  2102. }
  2103. $sellprice = $form->round_amount($sellprice, 2);
  2104. my $linetotal = $form->round_amount(
  2105. $sellprice * $a{$vendor_id}{$parts_id}{qty}, 2);
  2106. $query = qq|
  2107. SELECT p.description, p.unit, c.accno
  2108. FROM parts p
  2109. LEFT JOIN partstax pt ON (p.id = pt.parts_id)
  2110. LEFT JOIN chart c ON (c.id = pt.chart_id)
  2111. WHERE p.id = ?|;
  2112. $sth = $dbh->prepare($query);
  2113. $sth->execute($parts_id) || $form->dberror($query);
  2114. my $rate = 0;
  2115. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  2116. $description = $ref->{description};
  2117. $unit = $ref->{unit};
  2118. $rate += $tax{$ref->{accno}};
  2119. }
  2120. $sth->finish;
  2121. $netamount += $linetotal;
  2122. if ($taxincluded) {
  2123. $amount += $linetotal;
  2124. } else {
  2125. $amount += $form->round_amount(
  2126. $linetotal * (1 + $rate), 2);
  2127. }
  2128. $query = qq|
  2129. INSERT INTO orderitems
  2130. (trans_id, parts_id, description,
  2131. qty, ship, sellprice, unit)
  2132. VALUES
  2133. (?, ?, ?, ?, 0, ?, ?)|;
  2134. $sth = $dbh->prepare($query);
  2135. $sth->execute(
  2136. $id, $parts_id, $description,
  2137. $a{vendor_id}{parts_id}{qty}, $sellprice,
  2138. $unit
  2139. ) || $form->dberror($query);
  2140. }
  2141. my $ordnumber = $form->update_defaults($myconfig, 'ponumber');
  2142. my $null;
  2143. my $employee_id;
  2144. my $department_id;
  2145. ($null, $employee_id) = $form->get_employee($dbh);
  2146. ($null, $department_id) = split /--/, $form->{department};
  2147. $department_id *= 1;
  2148. $query = qq|
  2149. UPDATE oe SET
  2150. ordnumber = ?,
  2151. transdate = current_date,
  2152. vendor_id = ?,
  2153. customer_id = 0,
  2154. amount = ?,
  2155. netamount = ?,
  2156. taxincluded = ?,
  2157. curr = ?,
  2158. employee_id = ?,
  2159. department_id = ?,
  2160. ponumber = ?
  2161. WHERE id = ?|;
  2162. $sth = $dbh->prepare($query);
  2163. $sth->execute(
  2164. $ordnumber, $vendor_id, $amount, $netamount,
  2165. $taxincluded, $curr, $employee_id,
  2166. $department_id, $form->{ponumber}, $id
  2167. ) || $form->dberror($query);
  2168. }
  2169. my $rc = $dbh->commit;
  2170. $rc;
  2171. }
  2172. sub consolidate_orders {
  2173. my ($self, $myconfig, $form) = @_;
  2174. # connect to database
  2175. my $dbh = $form->{dbh};
  2176. my $i;
  2177. my $id;
  2178. my $ref;
  2179. my %oe = ();
  2180. my $query = qq|SELECT * FROM oe WHERE id = ?|;
  2181. my $sth = $dbh->prepare($query) || $form->dberror($query);
  2182. for ($i = 1; $i <= $form->{rowcount}; $i++) {
  2183. # retrieve order
  2184. if ($form->{"ndx_$i"}) {
  2185. $sth->execute($form->{"ndx_$i"});
  2186. $ref = $sth->fetchrow_hashref(NAME_lc);
  2187. $ref->{ndx} = $i;
  2188. $oe{oe}{$ref->{curr}}{$ref->{id}} = $ref;
  2189. $oe{vc}{$ref->{curr}}{$ref->{"$form->{vc}_id"}}++;
  2190. $sth->finish;
  2191. }
  2192. }
  2193. $query = qq|SELECT * FROM orderitems WHERE trans_id = ?|;
  2194. $sth = $dbh->prepare($query) || $form->dberror($query);
  2195. foreach $curr (keys %{ $oe{oe} }) {
  2196. foreach $id (sort {
  2197. $oe{oe}{$curr}{$a}->{ndx}
  2198. <=> $oe{oe}{$curr}{$b}->{ndx} }
  2199. keys %{ $oe{oe}{$curr} }) {
  2200. # retrieve order
  2201. $vc_id = $oe{oe}{$curr}{$id}->{"$form->{vc}_id"};
  2202. if ($oe{vc}{$oe{oe}{$curr}{$id}->{curr}}{$vc_id} > 1) {
  2203. push @{ $oe{orders}{$curr}{$vc_id} }, $id;
  2204. $sth->execute($id);
  2205. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  2206. push @{ $oe{orderitems}{$curr}{$id} },
  2207. $ref;
  2208. }
  2209. $sth->finish;
  2210. }
  2211. }
  2212. }
  2213. my $ordnumber = $form->{ordnumber};
  2214. my $numberfld = ($form->{vc} eq 'customer') ? 'sonumber' : 'ponumber';
  2215. my ($department, $department_id) = $form->{department};
  2216. $department_id *= 1;
  2217. my $uid = localtime;
  2218. $uid .= "$$";
  2219. my @orderitems = ();
  2220. foreach $curr (keys %{ $oe{orders} }) {
  2221. foreach $vc_id (sort {
  2222. $a <=> $b
  2223. } keys %{ $oe{orders}{$curr} }) {
  2224. # the orders
  2225. @orderitems = ();
  2226. $form->{customer_id} = $form->{vendor_id} = 0;
  2227. $form->{"$form->{vc}_id"} = $vc_id;
  2228. $amount = 0;
  2229. $netamount = 0;
  2230. foreach $id (@{ $oe{orders}{$curr}{$vc_id} }) {
  2231. # header
  2232. $ref = $oe{oe}{$curr}{$id};
  2233. $amount += $ref->{amount};
  2234. $netamount += $ref->{netamount};
  2235. $id = $dbh->quore($id);
  2236. foreach $item
  2237. (@{ $oe{orderitems}{$curr}{$id} }) {
  2238. push @orderitems, $item;
  2239. }
  2240. # close order
  2241. $query = qq|
  2242. UPDATE oe SET
  2243. closed = '1'
  2244. WHERE id = $id|;
  2245. $dbh->do($query) || $form->dberror($query);
  2246. # reset shipped
  2247. $query = qq|
  2248. UPDATE orderitems SET
  2249. ship = 0
  2250. WHERE trans_id = $id|;
  2251. $dbh->do($query) || $form->dberror($query);
  2252. }
  2253. $ordnumber ||= $form->update_defaults(
  2254. $myconfig, $numberfld, $dbh);
  2255. #fixme: Change this
  2256. $query = qq|
  2257. INSERT INTO oe (ordnumber) VALUES ($uid)|;
  2258. $dbh->do($query) || $form->dberror($query);
  2259. $query = qq|
  2260. SELECT id
  2261. FROM oe
  2262. WHERE ordnumber = '$uid'|;
  2263. ($id) = $dbh->selectrow_array($query);
  2264. $ref->{employee_id} *= 1;
  2265. $query = qq|
  2266. UPDATE oe SET
  2267. ordnumber = |.$dbh->quote($ordnumber).qq|,
  2268. transdate = current_date,
  2269. vendor_id = $form->{vendor_id},
  2270. customer_id = $form->{customer_id},
  2271. amount = $amount,
  2272. netamount = $netamount,
  2273. reqdate = |.$form->dbquote($ref->{reqdate}, SQL_DATE).qq|,
  2274. taxincluded = '$ref->{taxincluded}',
  2275. shippingpoint = |.$dbh->quote($ref->{shippingpoint}).qq|,
  2276. notes = |.$dbh->quote($ref->{notes}).qq|,
  2277. curr = '$curr',
  2278. employee_id = $ref->{employee_id},
  2279. intnotes = |.$dbh->quote($ref->{intnotes}).qq|,
  2280. shipvia = |.$dbh->quote($ref->{shipvia}).qq|,
  2281. language_code = '$ref->{language_code}',
  2282. ponumber = |.$dbh->quote($form->{ponumber}).qq|,
  2283. department_id = $department_id
  2284. WHERE id = $id|;
  2285. $sth = $dbh->prepare($query);
  2286. $sth->execute (
  2287. $department_id, $id
  2288. ) || $form->dberror($query);
  2289. # add items
  2290. foreach $item (@orderitems) {
  2291. for (qw(
  2292. qty sellprice discount project_id ship)
  2293. ) {
  2294. $item->{$_} *= 1;
  2295. }
  2296. $query = qq|
  2297. INSERT INTO orderitems
  2298. (trans_id, parts_id, description,
  2299. qty, sellprice, discount, unit, reqdate,
  2300. project_id, ship, serialnumber, notes)
  2301. VALUES
  2302. (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
  2303. $sth = $dbh->prepare($query);
  2304. $sth->execute(
  2305. $id, $item->{parts_id}, $item->{description},
  2306. $item->{qty}, $item->{sellprice},
  2307. $item->{discount}, $item->{unit},
  2308. $form->{reqdate}, $item->{project_id},
  2309. $item->{ship}, $item->{serialnumber},
  2310. $item->{notes}
  2311. ) || $form->dberror($query);
  2312. }
  2313. }
  2314. }
  2315. $rc = $dbh->commit;
  2316. $rc;
  2317. }
  2318. 1;