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