summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: 1a51a6cfde77668a85f38c8d20b2f397817bb84f (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->db_prepare_vars("orderitems_id_$i", "id_$i",
  324. "description_$i", "project_id_$i", "ship_$i");
  325. for (qw(qty ship)) {
  326. $form->{"${_}_$i"} = $form->parse_amount(
  327. $myconfig, $form->{"${_}_$i"}
  328. );
  329. }
  330. $form->{"discount_$i"} = $form->parse_amount(
  331. $myconfig, $form->{"discount_$i"}
  332. ) / 100;
  333. $form->{"sellprice_$i"} = $form->parse_amount(
  334. $myconfig, $form->{"sellprice_$i"}
  335. );
  336. if ($form->{"qty_$i"}) {
  337. $pth->execute($form->{"id_$i"});
  338. $ref = $pth->fetchrow_hashref(NAME_lc);
  339. for (keys %$ref) { $form->{"${_}_$i"} = $ref->{$_} }
  340. $pth->finish;
  341. $fxsellprice = $form->{"sellprice_$i"};
  342. my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  343. $dec = length $dec;
  344. my $decimalplaces = ($dec > 2) ? $dec : 2;
  345. $discount = $form->round_amount(
  346. $form->{"sellprice_$i"} *
  347. $form->{"discount_$i"},
  348. $decimalplaces
  349. );
  350. $form->{"sellprice_$i"} = $form->round_amount(
  351. $form->{"sellprice_$i"} - $discount,
  352. $decimalplaces
  353. );
  354. $linetotal = $form->round_amount(
  355. $form->{"sellprice_$i"} * $form->{"qty_$i"}, 2
  356. );
  357. @taxaccounts = Tax::init_taxes($form,
  358. $form->{"taxaccounts_$i"});
  359. if ($form->{taxincluded}) {
  360. $taxamount = Tax::calculate_taxes(\@taxaccounts,
  361. $form, $linetotal, 1);
  362. $form->{"sellprice_$i"} = Tax::extract_taxes(\@taxaccounts,
  363. $form, $form->{"sellprice_$i"});
  364. $taxbase = Tax::extract_taxes(\@taxaccounts,
  365. $form, $linetotal);
  366. } else {
  367. $taxamount = Tax::apply_taxes(\@taxaccounts,
  368. $form, $linetotal);
  369. $taxbase = $linetotal;
  370. }
  371. if (@taxaccounts && $form->round_amount($taxamount, 2)
  372. == 0) {
  373. if ($form->{taxincluded}) {
  374. foreach $item (@taxaccounts) {
  375. $taxamount =
  376. $form->round_amount(
  377. $item->value, 2);
  378. $taxaccounts{$item->account} +=
  379. $taxamount;
  380. $taxdiff += $taxamount;
  381. $taxbase{$item->account} +=
  382. $taxbase;
  383. }
  384. $taxaccounts{$taxaccounts[0]->account}
  385. += $taxdiff;
  386. } else {
  387. foreach $item (@taxaccounts) {
  388. $taxaccounts{$item->account} +=
  389. $item->value;
  390. $taxbase{$item->account} +=
  391. $taxbase;
  392. }
  393. }
  394. } else {
  395. foreach $item (@taxaccounts) {
  396. $taxaccounts{$item->account} +=
  397. $item->value;
  398. $taxbase{$item->account} += $taxbase;
  399. }
  400. }
  401. $netamount += $form->{"sellprice_$i"}
  402. * $form->{"qty_$i"};
  403. if ($form->{"projectnumber_$i"} ne "") {
  404. ($null, $project_id)
  405. = split /--/,
  406. $form->{"projectnumber_$i"};
  407. }
  408. $project_id = $form->{"project_id_$i"}
  409. if $form->{"project_id_$i"};
  410. if (!$form->{"reqdate_$i"}){
  411. $form->{"reqdate_$i"} = undef;
  412. }
  413. @queryargs = ();
  414. # save detail record in orderitems table
  415. $query = qq|INSERT INTO orderitems (|;
  416. if ($form->{"orderitems_id_$i"}){
  417. $query .= "id, ";
  418. }
  419. $query .= qq|
  420. trans_id, parts_id, description, qty, sellprice,
  421. discount, unit, reqdate, project_id, ship,
  422. serialnumber, notes)
  423. VALUES (|;
  424. if ($form->{"orderitems_id_$i"}){
  425. $query .= "?, ";
  426. push @queryargs, $form->{"orderitems_id_$i"};
  427. }
  428. $query .= qq| ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
  429. $sth = $dbh->prepare($query);
  430. push (@queryargs,
  431. $form->{id}, $form->{"id_$i"},
  432. $form->{"description_$i"}, $form->{"qty_$i"},
  433. $fxsellprice, $form->{"discount_$i"},
  434. $form->{"unit_$i"}, $form->{"reqdate_$i"},
  435. $project_id, $form->{"ship_$i"},
  436. $form->{"serialnumber_$i"},
  437. $form->{"notes_$i"});
  438. $sth->execute(@queryargs) || $form->dberror($query);
  439. $form->{"sellprice_$i"} = $fxsellprice;
  440. }
  441. $form->{"discount_$i"} *= 100;
  442. }
  443. # set values which could be empty
  444. for (qw(vendor_id customer_id taxincluded closed quotation))
  445. { $form->{$_} *= 1 }
  446. # add up the tax
  447. my $tax = 0;
  448. for (keys %taxaccounts) { $tax += $taxaccounts{$_} }
  449. $amount = $form->round_amount($netamount + $tax, 2);
  450. $netamount = $form->round_amount($netamount, 2);
  451. if ($form->{currency} eq $form->{defaultcurrency}) {
  452. $form->{exchangerate} = 1;
  453. } else {
  454. $exchangerate = $form->check_exchangerate(
  455. $myconfig, $form->{currency}, $form->{transdate},
  456. ($form->{vc} eq 'customer') ? 'buy' : 'sell');
  457. }
  458. $form->{exchangerate} = ($exchangerate) ? $exchangerate :
  459. $form->parse_amount($myconfig, $form->{exchangerate});
  460. ($null, $form->{department_id}) = split(/--/, $form->{department});
  461. for (qw(department_id terms)) { $form->{$_} *= 1 }
  462. if ($did_insert){
  463. $query = qq|
  464. UPDATE oe SET
  465. amount = ?,
  466. netamount = ?,
  467. taxincluded = ?
  468. WHERE id = ?|;
  469. @queryargs = ($amount, $netamount, $form->{taxincluded},
  470. $form->{id});
  471. } else {
  472. # save OE record
  473. $query = qq|
  474. UPDATE oe set
  475. ordnumber = ?,
  476. quonumber = ?,
  477. transdate = ?,
  478. vendor_id = ?,
  479. customer_id = ?,
  480. amount = ?,
  481. netamount = ?,
  482. reqdate = ?,
  483. taxincluded = ?,
  484. shippingpoint = ?,
  485. shipvia = ?,
  486. notes = ?,
  487. intnotes = ?,
  488. curr = ?,
  489. closed = ?,
  490. quotation = ?,
  491. department_id = ?,
  492. employee_id = ?,
  493. language_code = ?,
  494. ponumber = ?,
  495. terms = ?
  496. WHERE id = ?|;
  497. if (!$form->{reqdate}){
  498. $form->{reqdate} = undef;
  499. }
  500. @queryargs = ($form->{ordnumber},
  501. $form->{quonumber},
  502. $form->{transdate},
  503. $form->{vendor_id},
  504. $form->{customer_id},
  505. $amount,
  506. $netamount,
  507. $form->{reqdate},
  508. $form->{taxincluded},
  509. $form->{shippingpoint},
  510. $form->{shipvia},
  511. $form->{notes},
  512. $form->{intnotes},
  513. $form->{currency},
  514. $form->{closed},
  515. $quotation,
  516. $form->{department_id},
  517. $form->{employee_id},
  518. $form->{language_code},
  519. $form->{ponumber},
  520. $form->{terms},
  521. $form->{id});
  522. }
  523. $sth = $dbh->prepare($query);
  524. $sth->execute(@queryargs) || $form->dberror($query);
  525. if (!$did_insert){
  526. @queries = $form->run_custom_queries('oe', 'UPDATE');
  527. }
  528. $form->{ordtotal} = $amount;
  529. # add shipto
  530. $form->{name} = $form->{$form->{vc}};
  531. $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
  532. $form->add_shipto($dbh, $form->{id});
  533. # save printed, emailed, queued
  534. $form->save_status($dbh);
  535. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  536. if ($form->{vc} eq 'customer') {
  537. $form->update_exchangerate(
  538. $dbh,
  539. $form->{currency},
  540. $form->{transdate},
  541. $form->{exchangerate},
  542. 0);
  543. }
  544. if ($form->{vc} eq 'vendor') {
  545. $form->update_exchangerate(
  546. $dbh,
  547. $form->{currency},
  548. $form->{transdate},
  549. 0,
  550. $form->{exchangerate});
  551. }
  552. }
  553. if ($form->{type} =~ /_order$/) {
  554. # adjust onhand
  555. &adj_onhand($dbh, $form, $ml * -1);
  556. &adj_inventory($dbh, $myconfig, $form);
  557. }
  558. my %audittrail = (
  559. tablename => 'oe',
  560. reference => ($form->{type} =~ /_order$/)
  561. ? $form->{ordnumber} : $form->{quonumber},
  562. formname => $form->{type},
  563. action => 'saved',
  564. id => $form->{id} );
  565. $form->audittrail($dbh, "", \%audittrail);
  566. $form->save_recurring($dbh, $myconfig);
  567. my $rc = $dbh->commit;
  568. $rc;
  569. }
  570. sub delete {
  571. my ($self, $myconfig, $form) = @_;
  572. # connect to database
  573. my $dbh = $form->{dbh};
  574. # delete spool files
  575. my $query = qq|
  576. SELECT spoolfile FROM status
  577. WHERE trans_id = ?
  578. AND spoolfile IS NOT NULL|;
  579. $sth = $dbh->prepare($query);
  580. $sth->execute($form->{id}) || $form->dberror($query);
  581. my $spoolfile;
  582. my @spoolfiles = ();
  583. while (($spoolfile) = $sth->fetchrow_array) {
  584. push @spoolfiles, $spoolfile;
  585. }
  586. $sth->finish;
  587. $query = qq|
  588. SELECT o.parts_id, o.ship, p.inventory_accno_id, p.assembly
  589. FROM orderitems o
  590. JOIN parts p ON (p.id = o.parts_id)
  591. WHERE trans_id = ?|;
  592. $sth = $dbh->prepare($query);
  593. $sth->execute($form->{id}) || $form->dberror($query);
  594. if ($form->{type} =~ /_order$/) {
  595. $ml = ($form->{type} eq 'purchase_order') ? -1 : 1;
  596. while (my ($id, $ship, $inv, $assembly)
  597. = $sth->fetchrow_array) {
  598. $form->update_balance(
  599. $dbh,
  600. "parts",
  601. "onhand",
  602. "id = $id",
  603. $ship * $ml)
  604. if ($inv || $assembly);
  605. }
  606. }
  607. $sth->finish;
  608. # delete inventory
  609. $query = qq|DELETE FROM inventory WHERE trans_id = ?|;
  610. $sth = $dbh->prepare($query);
  611. $sth->execute($form->{id}) || $form->dberror($query);
  612. $sth->finish;
  613. # delete status entries
  614. $query = qq|DELETE FROM status WHERE trans_id = ?|;
  615. $sth = $dbh->prepare($query);
  616. $sth->execute($form->{id}) || $form->dberror($query);
  617. $sth->finish;
  618. # delete OE record
  619. $query = qq|DELETE FROM oe WHERE id = ?|;
  620. $sth = $dbh->prepare($query);
  621. $sth->execute($form->{id}) || $form->dberror($query);
  622. $sth->finish;
  623. # delete individual entries
  624. $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
  625. $sth->finish;
  626. $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
  627. $sth = $dbh->prepare($query);
  628. $sth->execute($form->{id}) || $form->dberror($query);
  629. $sth->finish;
  630. my %audittrail = (
  631. tablename => 'oe',
  632. reference => ($form->{type} =~ /_order$/)
  633. ? $form->{ordnumber} : $form->{quonumber},
  634. formname => $form->{type},
  635. action => 'deleted',
  636. id => $form->{id} );
  637. $form->audittrail($dbh, "", \%audittrail);
  638. my $rc = $dbh->commit;
  639. $dbh->disconnect;
  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|SELECT curr, current_date FROM defaults|;
  657. ($form->{currencies}, $form->{transdate}) =
  658. $dbh->selectrow_array($query);
  659. if ($form->{id}) {
  660. # retrieve order
  661. $query = qq|
  662. SELECT o.ordnumber, o.transdate, o.reqdate, o.terms,
  663. o.taxincluded, o.shippingpoint, o.shipvia,
  664. o.notes, o.intnotes, o.curr AS currency,
  665. e.name AS employee, o.employee_id,
  666. o.$form->{vc}_id, vc.name AS $form->{vc},
  667. o.amount AS invtotal, o.closed, o.reqdate,
  668. o.quonumber, o.department_id,
  669. d.description AS department, o.language_code,
  670. o.ponumber
  671. FROM oe o
  672. JOIN $form->{vc} vc ON (o.$form->{vc}_id = vc.id)
  673. LEFT JOIN employee e ON (o.employee_id = e.id)
  674. LEFT JOIN department d ON (o.department_id = d.id)
  675. WHERE o.id = ?|;
  676. $sth = $dbh->prepare($query);
  677. $sth->execute($form->{id}) || $form->dberror($query);
  678. $ref = $sth->fetchrow_hashref(NAME_lc);
  679. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  680. $sth->finish;
  681. $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
  682. $sth = $dbh->prepare($query);
  683. $sth->execute($form->{id}) || $form->dberror($query);
  684. $ref = $sth->fetchrow_hashref(NAME_lc);
  685. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  686. $sth->finish;
  687. # get printed, emailed and queued
  688. $query = qq|
  689. SELECT s.printed, s.emailed, s.spoolfile, s.formname
  690. FROM status s
  691. WHERE s.trans_id = ?|;
  692. $sth = $dbh->prepare($query);
  693. $sth->execute($form->{id}) || $form->dberror($query);
  694. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  695. $form->{printed} .= "$ref->{formname} "
  696. if $ref->{printed};
  697. $form->{emailed} .= "$ref->{formname} "
  698. if $ref->{emailed};
  699. $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
  700. if $ref->{spoolfile};
  701. }
  702. $sth->finish;
  703. for (qw(printed emailed queued)) { $form->{$_} =~ s/ +$//g }
  704. # retrieve individual items
  705. $query = qq|
  706. SELECT o.id AS orderitems_id, p.partnumber, p.assembly,
  707. o.description, o.qty, o.sellprice,
  708. o.parts_id AS id, o.unit, o.discount, p.bin,
  709. o.reqdate, o.project_id, o.ship, o.serialnumber,
  710. o.notes, pr.projectnumber, pg.partsgroup,
  711. p.partsgroup_id, p.partnumber AS sku,
  712. p.listprice, p.lastcost, p.weight, p.onhand,
  713. p.inventory_accno_id, p.income_accno_id,
  714. p.expense_accno_id, t.description
  715. AS partsgrouptranslation
  716. FROM orderitems o
  717. JOIN parts p ON (o.parts_id = p.id)
  718. LEFT JOIN project pr ON (o.project_id = pr.id)
  719. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  720. LEFT JOIN translation t
  721. ON (t.trans_id = p.partsgroup_id
  722. AND t.language_code = ?)
  723. WHERE o.trans_id = ?
  724. ORDER BY o.id|;
  725. $sth = $dbh->prepare($query);
  726. $sth->execute($form->{language_code}, $form->{id})
  727. || $form->dberror($query);
  728. # foreign exchange rates
  729. &exchangerate_defaults($dbh, $form);
  730. # query for price matrix
  731. my $pmh = PriceMatrix::price_matrix_query($dbh, $form);
  732. # taxes
  733. $query = qq|
  734. SELECT c.accno FROM chart c
  735. JOIN partstax pt ON (pt.chart_id = c.id)
  736. WHERE pt.parts_id = ?|;
  737. my $tth = $dbh->prepare($query) || $form->dberror($query);
  738. my $taxrate;
  739. my $ptref;
  740. my $sellprice;
  741. my $listprice;
  742. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  743. ($decimalplaces) = ($ref->{sellprice} =~ /\.(\d+)/);
  744. $decimalplaces = length $decimalplaces;
  745. $decimalplaces = ($decimalplaces > 2) ?
  746. $decimalplaces : 2;
  747. $tth->execute($ref->{id});
  748. $ref->{taxaccounts} = "";
  749. $taxrate = 0;
  750. while ($ptref = $tth->fetchrow_hashref(NAME_lc)) {
  751. $ref->{taxaccounts} .= "$ptref->{accno} ";
  752. $taxrate += $form->{"$ptref->{accno}_rate"};
  753. }
  754. $tth->finish;
  755. chop $ref->{taxaccounts};
  756. # preserve price
  757. $sellprice = $ref->{sellprice};
  758. # multiply by exchangerate
  759. $ref->{sellprice} = $form->round_amount(
  760. $ref->{sellprice} * $form->{$form->{currency}},
  761. $decimalplaces
  762. );
  763. for (qw(listprice lastcost)) {
  764. $ref->{$_} = $form->round_amount(
  765. $ref->{$_} / $form->{$form->{currency}},
  766. $decimalplaces
  767. );
  768. }
  769. # partnumber and price matrix
  770. PriceMatrix::price_matrix(
  771. $pmh, $ref, $form->{transdate}, $decimalplaces,
  772. $form, $myconfig);
  773. $ref->{sellprice} = $sellprice;
  774. $ref->{partsgroup} = $ref->{partsgrouptranslation}
  775. if $ref->{partsgrouptranslation};
  776. push @{ $form->{form_details} }, $ref;
  777. }
  778. $sth->finish;
  779. # get recurring transaction
  780. $form->get_recurring;
  781. @queries = $form->run_custom_queries('oe', 'SELECT');
  782. $form->{dbh}->commit;
  783. } else {
  784. # get last name used
  785. $form->lastname_used($myconfig, $dbh, $form->{vc})
  786. unless $form->{"$form->{vc}_id"};
  787. delete $form->{notes};
  788. }
  789. $dbh->commit;
  790. }
  791. sub exchangerate_defaults {
  792. my ($dbh2, $form) = @_;
  793. $dbh = $form->{dbh};
  794. my $var;
  795. my $buysell = ($form->{vc} eq "customer") ? "buy" : "sell";
  796. # get default currencies
  797. my $query = qq|SELECT substr(curr,1,3), curr FROM defaults|;
  798. ($form->{defaultcurrency}, $form->{currencies})
  799. = $dbh->selectrow_array($query);
  800. $query = qq|
  801. SELECT $buysell
  802. FROM exchangerate
  803. WHERE curr = ?
  804. AND transdate = ?|;
  805. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  806. $query = qq~
  807. SELECT max(transdate || ' ' || $buysell || ' ' || curr)
  808. FROM exchangerate
  809. WHERE curr = ?~;
  810. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  811. # get exchange rates for transdate or max
  812. foreach $var (split /:/, substr($form->{currencies},4)) {
  813. $eth1->execute($var, $form->{transdate});
  814. ($form->{$var}) = $eth1->fetchrow_array;
  815. if (! $form->{$var} ) {
  816. $eth2->execute($var);
  817. ($form->{$var}) = $eth2->fetchrow_array;
  818. ($null, $form->{$var}) = split / /, $form->{$var};
  819. $form->{$var} = 1 unless $form->{$var};
  820. $eth2->finish;
  821. }
  822. $eth1->finish;
  823. }
  824. $form->{$form->{currency}} = $form->{exchangerate}
  825. if $form->{exchangerate};
  826. $form->{$form->{currency}} ||= 1;
  827. $form->{$form->{defaultcurrency}} = 1;
  828. }
  829. sub order_details {
  830. use LedgerSMB::CP;
  831. my ($self, $myconfig, $form) = @_;
  832. # connect to database
  833. my $dbh = $form->dbconnect($myconfig);
  834. my $query;
  835. my $sth;
  836. my $item;
  837. my $i;
  838. my @sortlist = ();
  839. my $projectnumber;
  840. my $projectdescription;
  841. my $projectnumber_id;
  842. my $translation;
  843. my $partsgroup;
  844. my @queryargs;
  845. my @taxaccounts;
  846. my %taxaccounts; # I don't think this works.
  847. my $tax;
  848. my $taxrate;
  849. my $taxamount;
  850. my %translations;
  851. my $language_code = $form->{dbh}->quote($form->{language_code});
  852. $query = qq|
  853. SELECT p.description, t.description
  854. FROM project p
  855. LEFT JOIN translation t ON (t.trans_id = p.id AND
  856. t.language_code = $language_code)
  857. WHERE id = ?|;
  858. my $prh = $dbh->prepare($query) || $form->dberror($query);
  859. $query = qq|
  860. SELECT inventory_accno_id, income_accno_id,
  861. expense_accno_id, assembly FROM parts
  862. WHERE id = ?|;
  863. my $pth = $dbh->prepare($query) || $form->dberror($query);
  864. my $sortby;
  865. # sort items by project and partsgroup
  866. for $i (1 .. $form->{rowcount}) {
  867. if ($form->{"id_$i"}) {
  868. # account numbers
  869. $pth->execute($form->{"id_$i"});
  870. $ref = $pth->fetchrow_hashref(NAME_lc);
  871. for (keys %$ref) { $form->{"${_}_$i"} = $ref->{$_} }
  872. $pth->finish;
  873. $projectnumber_id = 0;
  874. $projectnumber = "";
  875. $form->{partsgroup} = "";
  876. $form->{projectnumber} = "";
  877. if ($form->{groupprojectnumber}
  878. || $form->{grouppartsgroup}) {
  879. $inventory_accno_id =
  880. ($form->{"inventory_accno_id_$i"} ||
  881. $form->{"assembly_$i"}) ? "1" : "";
  882. if ($form->{groupprojectnumber}) {
  883. ($projectnumber, $projectnumber_id) =
  884. split /--/,
  885. $form->{"projectnumber_$i"};
  886. }
  887. if ($form->{grouppartsgroup}) {
  888. ($form->{partsgroup}) = split /--/,
  889. $form->{"partsgroup_$i"};
  890. }
  891. if ($projectnumber_id &&
  892. $form->{groupprojectnumber}) {
  893. if ($translation{$projectnumber_id}) {
  894. $form->{projectnumber} =
  895. $translation{$projectnumber_id};
  896. } else {
  897. # get project description
  898. $prh->execute(
  899. $projectnumber_id);
  900. ($projectdescription,
  901. $translation) =
  902. $prh->fetchrow_array;
  903. $prh->finish;
  904. $form->{projectnumber} =
  905. ($translation) ?
  906. "$projectnumber, \n" .
  907. "$translation"
  908. : "$projectnumber, \n" .
  909. "$projectdescription";
  910. $translation{$projectnumber_id}
  911. = $form->{projectnumber};
  912. }
  913. }
  914. if ($form->{grouppartsgroup}
  915. && $form->{partsgroup}) {
  916. $form->{projectnumber} .= " / "
  917. if $projectnumber_id;
  918. $form->{projectnumber} .=
  919. $form->{partsgroup};
  920. }
  921. $form->format_string(projectnumber);
  922. }
  923. $sortby = qq|$projectnumber$form->{partsgroup}|;
  924. if ($form->{sortby} ne 'runningnumber') {
  925. for (qw(partnumber description bin)) {
  926. $sortby .= $form->{"${_}_$i"}
  927. if $form->{sortby} eq $_;
  928. }
  929. }
  930. push @sortlist, [ $i,
  931. "$projectnumber$form->{partsgroup}".
  932. "$inventory_accno_id",
  933. $form->{projectnumber}, $projectnumber_id,
  934. $form->{partsgroup}, $sortby ];
  935. }
  936. }
  937. delete $form->{projectnumber};
  938. # sort the whole thing by project and group
  939. @sortlist = sort { $a->[5] cmp $b->[5] } @sortlist;
  940. # if there is a warehouse limit picking
  941. if ($form->{warehouse_id} && $form->{formname} =~
  942. /(pick|packing)_list/) {
  943. # run query to check for inventory
  944. $query = qq|
  945. SELECT sum(qty) AS qty FROM inventory
  946. WHERE parts_id = ? AND warehouse_id = ?|;
  947. $sth = $dbh->prepare($query) || $form->dberror($query);
  948. for $i (1 .. $form->{rowcount}) {
  949. $sth->execute($form->{"id_$i"}, $form->{warehouse_id})
  950. || $form->dberror;
  951. ($qty) = $sth->fetchrow_array;
  952. $sth->finish;
  953. $form->{"qty_$i"} = 0 if $qty == 0;
  954. if ($form->parse_amount($myconfig, $form->{"ship_$i"})
  955. > $qty) {
  956. $form->{"ship_$i"} =
  957. $form->format_amount($myconfig, $qty);
  958. }
  959. }
  960. }
  961. my $runningnumber = 1;
  962. my $sameitem = "";
  963. my $subtotal;
  964. my $k = scalar @sortlist;
  965. my $j = 0;
  966. foreach $item (@sortlist) {
  967. $i = $item->[0];
  968. $j++;
  969. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  970. if ($item->[1] ne $sameitem) {
  971. $sameitem = $item->[1];
  972. $ok = 0;
  973. if ($form->{groupprojectnumber}) {
  974. $ok = $form->{"projectnumber_$i"};
  975. }
  976. if ($form->{grouppartsgroup}) {
  977. $ok = $form->{"partsgroup_$i"}
  978. unless $ok;
  979. }
  980. if ($ok) {
  981. if ($form->{"inventory_accno_id_$i"}
  982. || $form->{"assembly_$i"}) {
  983. push(@{ $form->{part} }, "");
  984. push(@{ $form->{service} },
  985. NULL);
  986. } else {
  987. push(@{ $form->{part} }, NULL);
  988. push(@{ $form->{service} }, "");
  989. }
  990. push(@{ $form->{description} },
  991. $item->[2]);
  992. for (
  993. qw(taxrates runningnumber
  994. number sku qty ship unit bin
  995. serialnumber requiredate
  996. projectnumber sellprice
  997. listprice netprice discount
  998. discountrate linetotal weight
  999. itemnotes)
  1000. ) {
  1001. push(@{ $form->{$_} }, "");
  1002. }
  1003. push(@{ $form->{lineitems} },
  1004. { amount => 0, tax => 0 });
  1005. }
  1006. }
  1007. }
  1008. $form->{"qty_$i"} = $form->parse_amount(
  1009. $myconfig, $form->{"qty_$i"});
  1010. $form->{"ship_$i"} = $form->parse_amount(
  1011. $myconfig, $form->{"ship_$i"});
  1012. if ($form->{"qty_$i"}) {
  1013. $form->{totalqty} += $form->{"qty_$i"};
  1014. $form->{totalship} += $form->{"ship_$i"};
  1015. $form->{totalweight} += ($form->{"weight_$i"}
  1016. * $form->{"qty_$i"});
  1017. $form->{totalweightship} += ($form->{"weight_$i"}
  1018. * $form->{"ship_$i"});
  1019. # add number, description and qty to $form->{number}
  1020. push(@{ $form->{runningnumber} }, $runningnumber++);
  1021. push(@{ $form->{number} },
  1022. qq|$form->{"partnumber_$i"}|);
  1023. push(@{ $form->{sku} }, qq|$form->{"sku_$i"}|);
  1024. push(@{ $form->{description} },
  1025. qq|$form->{"description_$i"}|);
  1026. push(@{ $form->{itemnotes} }, $form->{"notes_$i"});
  1027. push(@{ $form->{qty} }, $form->format_amount(
  1028. $myconfig, $form->{"qty_$i"}));
  1029. push(@{ $form->{ship} }, $form->format_amount(
  1030. $myconfig, $form->{"ship_$i"}));
  1031. push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
  1032. push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
  1033. push(@{ $form->{serialnumber} },
  1034. qq|$form->{"serialnumber_$i"}|);
  1035. push(@{ $form->{requiredate} },
  1036. qq|$form->{"reqdate_$i"}|);
  1037. push(@{ $form->{projectnumber} },
  1038. qq|$form->{"projectnumber_$i"}|);
  1039. push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
  1040. push(@{ $form->{listprice} }, $form->{"listprice_$i"});
  1041. push(@{ $form->{weight} }, $form->format_amount(
  1042. $myconfig,
  1043. $form->{"weight_$i"} * $form->{"ship_$i"}));
  1044. my $sellprice = $form->parse_amount(
  1045. $myconfig, $form->{"sellprice_$i"});
  1046. my ($dec) = ($sellprice =~ /\.(\d+)/);
  1047. $dec = length $dec;
  1048. my $decimalplaces = ($dec > 2) ? $dec : 2;
  1049. my $discount = $form->round_amount(
  1050. $sellprice * $form->parse_amount(
  1051. $myconfig,
  1052. $form->{"discount_$i"}) / 100,
  1053. $decimalplaces);
  1054. # keep a netprice as well, (sellprice - discount)
  1055. $form->{"netprice_$i"} = $sellprice - $discount;
  1056. my $linetotal = $form->round_amount(
  1057. $form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
  1058. if ($form->{"inventory_accno_id_$i"}
  1059. || $form->{"assembly_$i"}) {
  1060. push(@{ $form->{part} }, $form->{"sku_$i"});
  1061. push(@{ $form->{service} }, NULL);
  1062. $form->{totalparts} += $linetotal;
  1063. } else {
  1064. push(@{ $form->{service} }, $form->{"sku_$i"});
  1065. push(@{ $form->{part} }, NULL);
  1066. $form->{totalservices} += $linetotal;
  1067. }
  1068. push(@{ $form->{netprice} },
  1069. ($form->{"netprice_$i"})
  1070. ? $form->format_amount(
  1071. $myconfig,
  1072. $form->{"netprice_$i"},
  1073. $decimalplaces)
  1074. : " ");
  1075. $discount = ($discount)
  1076. ? $form->format_amount(
  1077. $myconfig,
  1078. $discount * -1,
  1079. $decimalplaces)
  1080. : " ";
  1081. push(@{ $form->{discount} }, $discount);
  1082. push(@{ $form->{discountrate} },
  1083. $form->format_amount($myconfig,
  1084. $form->{"discount_$i"}));
  1085. $form->{ordtotal} += $linetotal;
  1086. # this is for the subtotals for grouping
  1087. $subtotal += $linetotal;
  1088. $form->{"linetotal_$i"} = $form->format_amount(
  1089. $myconfig, $linetotal, 2);
  1090. push(@{ $form->{linetotal} }, $form->{"linetotal_$i"});
  1091. @taxaccounts = Tax::init_taxes($form,
  1092. $form->{"taxaccounts_$i"});
  1093. my $ml = 1;
  1094. my @taxrates = ();
  1095. $tax = 0;
  1096. $taxamount = Tax::calculate_taxes(\@taxaccounts,
  1097. $form, $linetotal, 1);
  1098. $taxbase = Tax::extract_taxes(\@taxaccounts,
  1099. $form, $linetotal);
  1100. foreach $item (@taxaccounts) {
  1101. push @taxrates, Math::BigFloat->new(100) *
  1102. $item->rate;
  1103. if ($form->{taxincluded}) {
  1104. $taxaccounts{$item->account} +=
  1105. $item->value;
  1106. $taxbase{$item->account} += $taxbase;
  1107. } else {
  1108. Tax::apply_taxes(\@taxaccounts, $form,
  1109. $linetotal);
  1110. $taxbase{$item->account} += $linetotal;
  1111. $taxaccounts{$item->account} +=
  1112. $item->value;
  1113. }
  1114. }
  1115. if ($form->{taxincluded}) {
  1116. $tax += Tax::calculate_taxes(\@taxaccounts,
  1117. $form, $linetotal, 1);
  1118. } else {
  1119. $tax += Tax::calculate_taxes(\@taxaccounts,
  1120. $form, $linetotal, 0);
  1121. }
  1122. push(@{ $form->{lineitems} },
  1123. { amount => $linetotal,
  1124. tax => $form->round_amount($tax, 2) });
  1125. push(@{ $form->{taxrates} },
  1126. join ' ', sort { $a <=> $b } @taxrates);
  1127. if ($form->{"assembly_$i"}) {
  1128. $form->{stagger} = -1;
  1129. &assembly_details($myconfig,
  1130. $form, $dbh, $form->{"id_$i"},
  1131. $oid{$myconfig->{dbdriver}},
  1132. $form->{"qty_$i"});
  1133. }
  1134. }
  1135. # add subtotal
  1136. if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
  1137. if ($subtotal) {
  1138. if ($j < $k) {
  1139. # look at next item
  1140. if ($sortlist[$j]->[1] ne $sameitem) {
  1141. if ($form->{"inventory_accno_id_$i"}
  1142. || $form->{"assembly_$i"}) {
  1143. push(@{ $form->{part} },
  1144. "");
  1145. push(@{
  1146. $form->{service}
  1147. }, NULL);
  1148. } else {
  1149. push(@{
  1150. $form->{service}
  1151. }, "");
  1152. push(@{ $form->{part} },
  1153. NULL);
  1154. }
  1155. for (qw(
  1156. taxrates runningnumber
  1157. number sku qty ship unit
  1158. bin serialnumber
  1159. requiredate
  1160. projectnumber sellprice
  1161. listprice netprice
  1162. discount discountrate
  1163. weight itemnotes)
  1164. ) {
  1165. push(@{ $form->{$_} },
  1166. "");
  1167. }
  1168. push(@{ $form->{description} },
  1169. $form->{groupsubtotaldescription});
  1170. push(@{ $form->{lineitems} },
  1171. { amount => 0,
  1172. tax => 0 });
  1173. if ($form->{groupsubtotaldescription}
  1174. ne "") {
  1175. push(@{
  1176. $form->{linetotal}
  1177. },
  1178. $form->format_amount($myconfig, $subtotal, 2));
  1179. } else {
  1180. push(@{
  1181. $form->{linetotal}
  1182. }, "");
  1183. }
  1184. $subtotal = 0;
  1185. }
  1186. } else {
  1187. # got last item
  1188. if ($form->{groupsubtotaldescription}
  1189. ne "") {
  1190. if ($form->{"inventory_accno_id_$i"}
  1191. || $form->{"assembly_$i"}) {
  1192. push(@{ $form->{part} },
  1193. "");
  1194. push(@{
  1195. $form->{service}
  1196. }, NULL);
  1197. } else {
  1198. push(@{
  1199. $form->{service}
  1200. }, "");
  1201. push(@{ $form->{part} },
  1202. NULL);
  1203. }
  1204. for (qw(
  1205. taxrates runningnumber
  1206. number sku qty ship unit
  1207. bin serialnumber
  1208. requiredate
  1209. projectnumber sellprice
  1210. listprice netprice
  1211. discount discountrate
  1212. weight itemnotes)
  1213. ) {
  1214. push(@{ $form->{$_} },
  1215. "");
  1216. }
  1217. push(@{ $form->{description} },
  1218. $form->{groupsubtotaldescription});
  1219. push(@{ $form->{linetotal} },
  1220. $form->format_amount(
  1221. $myconfig,
  1222. $subtotal,
  1223. 2));
  1224. push(@{ $form->{lineitems} },
  1225. { amount => 0,
  1226. tax => 0 });
  1227. }
  1228. }
  1229. }
  1230. }
  1231. }
  1232. $tax = 0;
  1233. foreach $item (sort keys %taxaccounts) {
  1234. if ($form->round_amount($taxaccounts{$item}, 2)) {
  1235. $tax += $taxamount = $form->round_amount(
  1236. $taxaccounts{$item}, 2);
  1237. push(@{ $form->{taxbaseinclusive} },
  1238. $form->{"${item}_taxbaseinclusive"}
  1239. = $form->round_amount(
  1240. $taxbase{$item} + $tax, 2));
  1241. push(@{ $form->{taxbase} },
  1242. $form->{"${item}_taxbase"}
  1243. = $form->format_amount($myconfig,
  1244. $taxbase{$item}, 2));
  1245. push(@{ $form->{tax} },
  1246. $form->{"${item}_tax"}
  1247. = $form->format_amount($myconfig,
  1248. $taxamount, 2));
  1249. push(@{ $form->{taxdescription} },
  1250. $form->{"${item}_description"});
  1251. $form->{"${item}_taxrate"} =
  1252. $form->format_amount($myconfig,
  1253. $form->{"${item}_rate"} * 100);
  1254. push(@{ $form->{taxrate} }, $form->{"${item}_taxrate"});
  1255. push(@{ $form->{taxnumber} },
  1256. $form->{"${item}_taxnumber"});
  1257. }
  1258. }
  1259. # adjust taxes for lineitems
  1260. my $total = 0;
  1261. for (@{ $form->{lineitems} }) {
  1262. $total += $_->{tax};
  1263. }
  1264. if ($form->round_amount($total,2) != $form->round_amount($tax,2)) {
  1265. # get largest amount
  1266. for (reverse sort { $a->{tax} <=> $b->{tax} }
  1267. @{ $form->{lineitems} }) {
  1268. $_->{tax} -= $total - $tax;
  1269. last;
  1270. }
  1271. }
  1272. $i = 1;
  1273. for (@{ $form->{lineitems} }) {
  1274. push(@{ $form->{linetax} },
  1275. $form->format_amount($myconfig, $_->{tax}, 2, ""));
  1276. }
  1277. for (qw(totalparts totalservices)) {
  1278. $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2);
  1279. }
  1280. for (qw(totalqty totalship totalweight)) {
  1281. $form->{$_} = $form->format_amount($myconfig, $form->{$_});
  1282. }
  1283. $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal},
  1284. 2);
  1285. $form->{ordtotal} = ($form->{taxincluded})
  1286. ? $form->{ordtotal}
  1287. : $form->{ordtotal} + $tax;
  1288. my $c;
  1289. if ($form->{language_code} ne "") {
  1290. $c = new CP $form->{language_code};
  1291. } else {
  1292. $c = new CP $myconfig->{countrycode};
  1293. }
  1294. $c->init;
  1295. my $whole;
  1296. ($whole, $form->{decimal}) = split /\./, $form->{ordtotal};
  1297. $form->{decimal} .= "00";
  1298. $form->{decimal} = substr($form->{decimal}, 0, 2);
  1299. $form->{text_decimal} = $c->num2text($form->{decimal} * 1);
  1300. $form->{text_amount} = $c->num2text($whole);
  1301. $form->{integer_amount} = $form->format_amount($myconfig, $whole);
  1302. # format amounts
  1303. $form->{quototal} = $form->{ordtotal} =
  1304. $form->format_amount($myconfig, $form->{ordtotal}, 2);
  1305. $form->format_string(qw(text_amount text_decimal));
  1306. $query = qq|SELECT weightunit FROM defaults|;
  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->dbconnect($myconfig);
  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->disconnect;
  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->dbconnect_noauto($myconfig);
  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 = '$serialnumber',
  1506. ship = $ship,
  1507. reqdate = '$form->{shippingdate}'
  1508. WHERE trans_id = $form->{id}
  1509. AND id = $form->{"orderitems_id_$i"}|;
  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->dbconnect_noauto($myconfig);
  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 FROM defaults|;
  1789. ($form->{transdate}) = $dbh->selectrow_array($query);
  1790. # foreign exchange rates
  1791. &exchangerate_defaults($dbh, $form);
  1792. $dbh->disconnect;
  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->dbconnect_noauto($myconfig);
  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->quore($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 (
  2114. $department_id, $id
  2115. ) || $form->dberror($query);
  2116. # add items
  2117. foreach $item (@orderitems) {
  2118. for (qw(
  2119. qty sellprice discount project_id ship)
  2120. ) {
  2121. $item->{$_} *= 1;
  2122. }
  2123. $query = qq|
  2124. INSERT INTO orderitems
  2125. (trans_id, parts_id, description,
  2126. qty, sellprice, discount, unit, reqdate,
  2127. project_id, ship, serialnumber, notes)
  2128. VALUES
  2129. (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
  2130. $sth = $dbh->prepare($query);
  2131. $sth->execute(
  2132. $id, $item->{parts_id}, $item->{description},
  2133. $item->{qty}, $item->{sellprice},
  2134. $item->{discount}, $item->{unit},
  2135. $form->{reqdate}, $item->{project_id},
  2136. $item->{ship}, $item->{serialnumber},
  2137. $item->{notes}
  2138. ) || $form->dberror($query);
  2139. }
  2140. }
  2141. }
  2142. $rc = $dbh->commit;
  2143. $rc;
  2144. }
  2145. 1;