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