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