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