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