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