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