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