summaryrefslogtreecommitdiff
path: root/LedgerSMB/IC.pm
blob: d4eb2a6e29c4584c9235ae6014312495268c0087 (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) 2000
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has NOT undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # Inventory Control backend
  31. #
  32. #======================================================================
  33. package IC;
  34. sub get_part {
  35. my ($self, $myconfig, $form) = @_;
  36. # connect to db
  37. my $dbh = $form->{dbh};
  38. my $i;
  39. my $query = qq|
  40. SELECT p.*, c1.accno AS inventory_accno,
  41. c1.description AS inventory_description,
  42. c2.accno AS income_accno,
  43. c2.description AS income_description,
  44. c3.accno AS expense_accno,
  45. c3.description AS expense_description, pg.partsgroup
  46. FROM parts p
  47. LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
  48. LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
  49. LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
  50. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  51. WHERE p.id = ?|;
  52. my $sth = $dbh->prepare($query);
  53. $sth->execute($form->{id}) || $form->dberror($query);
  54. my $ref = $sth->fetchrow_hashref(NAME_lc);
  55. # copy to $form variables
  56. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  57. $sth->finish;
  58. # part, service item or labor
  59. $form->{item} = ($form->{inventory_accno_id}) ? 'part' : 'service';
  60. $form->{item} = 'labor' if ! $form->{income_accno_id};
  61. if ($form->{assembly}) {
  62. $form->{item} = 'assembly';
  63. # retrieve assembly items
  64. $query = qq|
  65. SELECT p.id, p.partnumber, p.description,
  66. p.sellprice, p.weight, a.qty, a.bom, a.adj,
  67. p.unit, p.lastcost, p.listprice,
  68. pg.partsgroup, p.assembly, p.partsgroup_id
  69. FROM parts p
  70. JOIN assembly a ON (a.parts_id = p.id)
  71. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  72. WHERE a.id = ?|;
  73. $sth = $dbh->prepare($query);
  74. $sth->execute($form->{id}) || $form->dberror($query);
  75. $form->{assembly_rows} = 0;
  76. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  77. $form->{assembly_rows}++;
  78. foreach my $key ( keys %{ $ref } ) {
  79. $form->{"${key}_$form->{assembly_rows}"}
  80. = $ref->{$key};
  81. }
  82. }
  83. $sth->finish;
  84. }
  85. # setup accno hash for <option checked>
  86. # {amount} is used in create_links
  87. for (qw(inventory income expense)) {
  88. $form->{amount}{"IC_$_"}
  89. = {
  90. accno => $form->{"${_}_accno"},
  91. description => $form->{"${_}_description"}
  92. };
  93. };
  94. if ($form->{item} =~ /(part|assembly)/) {
  95. if ($form->{makemodel} ne "") {
  96. $query = qq|
  97. SELECT make, model
  98. FROM makemodel
  99. WHERE parts_id = ?|;
  100. $sth = $dbh->prepare($query);
  101. $sth->execute($form->{id}) || $form->dberror($query);
  102. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  103. push @{ $form->{makemodels} }, $ref;
  104. }
  105. $sth->finish;
  106. }
  107. }
  108. # now get accno for taxes
  109. $query = qq|
  110. SELECT c.accno FROM chart c, partstax pt
  111. WHERE pt.chart_id = c.id AND pt.parts_id = ?|;
  112. $sth = $dbh->prepare($query);
  113. $sth->execute($form->{id}) || $form->dberror($query);
  114. while (($key) = $sth->fetchrow_array) {
  115. $form->{amount}{$key} = $key;
  116. }
  117. $sth->finish;
  118. my $id = $dbh->quote($form->{id});
  119. # is it an orphan
  120. $query = qq|
  121. SELECT parts_id FROM invoice WHERE parts_id = $id
  122. UNION
  123. SELECT parts_id FROM orderitems WHERE parts_id = $id
  124. UNION
  125. SELECT parts_id FROM assembly WHERE parts_id = $id
  126. UNION
  127. SELECT parts_id FROM jcitems WHERE parts_id = $id|;
  128. ($form->{orphaned}) = $dbh->selectrow_array($query);
  129. $form->{orphaned} = !$form->{orphaned};
  130. $form->{orphaned} = 0 if $form->{project_id};
  131. if ($form->{item} eq 'assembly') {
  132. if ($form->{orphaned}) {
  133. $form->{orphaned} = !$form->{onhand};
  134. }
  135. }
  136. if ($form->{item} =~ /(part|service)/) {
  137. # get vendors
  138. $query = qq|
  139. SELECT v.id, v.name, pv.partnumber,
  140. pv.lastcost, pv.leadtime,
  141. pv.curr AS vendorcurr
  142. FROM partsvendor pv
  143. JOIN vendor v ON (v.id = pv.vendor_id)
  144. WHERE pv.parts_id = ?
  145. ORDER BY 2|;
  146. $sth = $dbh->prepare($query);
  147. $sth->execute($form->{id}) || $form->dberror($query);
  148. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  149. push @{ $form->{vendormatrix} }, $ref;
  150. }
  151. $sth->finish;
  152. }
  153. # get matrix
  154. if ($form->{item} ne 'labor') {
  155. $query = qq|
  156. SELECT pc.pricebreak, pc.sellprice AS customerprice,
  157. pc.curr AS customercurr, pc.validfrom,
  158. pc.validto, c.name, c.id AS cid,
  159. g.pricegroup, g.id AS gid
  160. FROM partscustomer pc
  161. LEFT JOIN customer c ON (c.id = pc.customer_id)
  162. LEFT JOIN pricegroup g ON (g.id = pc.pricegroup_id)
  163. WHERE pc.parts_id = ?
  164. ORDER BY c.name, g.pricegroup, pc.pricebreak|;
  165. $sth = $dbh->prepare($query);
  166. $sth->execute($form->{id}) || $form->dberror($query);
  167. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  168. push @{ $form->{customermatrix} }, $ref;
  169. }
  170. $sth->finish;
  171. }
  172. $form->run_custom_queries('parts', 'SELECT');
  173. }
  174. sub save {
  175. my ($self, $myconfig, $form) = @_;
  176. ($form->{inventory_accno}) = split(/--/, $form->{IC_inventory});
  177. ($form->{expense_accno}) = split(/--/, $form->{IC_expense});
  178. ($form->{income_accno}) = split(/--/, $form->{IC_income});
  179. my $dbh = $form->{dbh};
  180. # undo amount formatting
  181. for (qw(rop weight listprice sellprice lastcost stock)) {
  182. $form->{$_} = $form->parse_amount($myconfig, $form->{$_});
  183. }
  184. $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
  185. $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
  186. for (qw(alternate obsolete onhand)) { $form->{$_} *= 1 }
  187. my $query;
  188. my $sth;
  189. my $i;
  190. my $null;
  191. my $vendor_id;
  192. my $customer_id;
  193. if ($form->{id}) {
  194. # get old price
  195. $query = qq|
  196. SELECT id, listprice, sellprice, lastcost, weight,
  197. project_id
  198. FROM parts
  199. WHERE id = ?|;
  200. my $sth = $dbh->prepare($query);
  201. $sth->execute($form->{id});
  202. my ($id, $listprice, $sellprice, $lastcost, $weight,
  203. $project_id)
  204. = $sth->fetchrow_array();
  205. if ($id) {
  206. if (!$project_id) {
  207. # if item is part of an assembly
  208. # adjust all assemblies
  209. $query = qq|
  210. SELECT id, qty, adj
  211. FROM assembly
  212. WHERE parts_id = ?|;
  213. $sth = $dbh->prepare($query);
  214. $sth->execute($form->{id}) ||
  215. $form->dberror($query);
  216. while (my ($id, $qty, $adj)
  217. = $sth->fetchrow_array) {
  218. &update_assembly(
  219. $dbh, $form, $id, $qty, $adj,
  220. $listprice * 1, $sellprice * 1,
  221. $lastcost * 1, $weight * 1);
  222. }
  223. $sth->finish;
  224. }
  225. if ($form->{item} =~ /(part|service)/) {
  226. # delete partsvendor records
  227. $query = qq|
  228. DELETE FROM partsvendor
  229. WHERE parts_id = ?|;
  230. $sth = $dbh->prepare($query);
  231. $sth->execute($form->{id})
  232. || $form->dberror($query);
  233. }
  234. if ($form->{item} !~ /(service|labor)/) {
  235. # delete makemodel records
  236. $query = qq|
  237. DELETE FROM makemodel
  238. WHERE parts_id = ?|;
  239. $sth = $dbh->prepare($query);
  240. $sth->execute($form->{id})
  241. || $form->dberror($query);
  242. }
  243. if ($form->{item} eq 'assembly') {
  244. if ($form->{onhand}) {
  245. &adjust_inventory(
  246. $dbh, $form, $form->{id},
  247. $form->{onhand} * -1);
  248. }
  249. if ($form->{orphaned}) {
  250. # delete assembly records
  251. $query = qq|
  252. DELETE FROM assembly
  253. WHERE id = ?|;
  254. $sth = $dbh->prepare($query);
  255. $sth->execute($form->{id})
  256. || $form->dberror($query);
  257. } else {
  258. for $i (1 ..
  259. $form->{assembly_rows} - 1) {
  260. # update BOM, A only
  261. for (qw(bom adj)) {
  262. $form->{"${_}_$i"}
  263. *= 1;
  264. }
  265. $query = qq|
  266. UPDATE assembly
  267. SET bom = ?,
  268. adj = ?
  269. WHERE id = ?
  270. AND parts_id = ?|;
  271. $sth = $dbh->prepare($query);
  272. $sth->execute(
  273. $form->{"bom_$i"},
  274. $form->{"adj_$i"},
  275. $form->{id},
  276. $form->{"id_$i"}
  277. )|| $form->dberror(
  278. $query);
  279. }
  280. }
  281. $form->{onhand} += $form->{stock};
  282. }
  283. # delete tax records
  284. $query = qq|DELETE FROM partstax WHERE parts_id = ?|;
  285. $sth = $dbh->prepare($query);
  286. $sth->execute($form->{id})|| $form->dberror($query);
  287. # delete matrix
  288. $query = qq|
  289. DELETE FROM partscustomer
  290. WHERE parts_id = ?|;
  291. $sth = $dbh->prepare($query);
  292. $sth->execute($form->{id})|| $form->dberror($query);
  293. } else {
  294. $query = qq|INSERT INTO parts (id) VALUES (?)|;
  295. $sth = $dbh->prepare($query);
  296. $sth->execute($form->{id})|| $form->dberror($query);
  297. }
  298. }
  299. if (!$form->{id}) {
  300. my $uid = localtime;
  301. $uid .= "$$";
  302. $query = qq|INSERT INTO parts (partnumber) VALUES ('$uid')|;
  303. $dbh->do($query) || $form->dberror($query);
  304. $query = qq|SELECT id FROM parts WHERE partnumber = '$uid'|;
  305. $sth = $dbh->prepare($query);
  306. $sth->execute || $form->dberror($query);
  307. ($form->{id}) = $sth->fetchrow_array;
  308. $sth->finish;
  309. $form->{orphaned} = 1;
  310. $form->{onhand} = ($form->{stock} * 1)
  311. if $form->{item} eq 'assembly';
  312. }
  313. my $partsgroup_id;
  314. ($null, $partsgroup_id) = split /--/, $form->{partsgroup};
  315. $partsgroup_id *= 1;
  316. $form->{partnumber} = $form->update_defaults(
  317. $myconfig, "partnumber", $dbh) if ! $form->{partnumber};
  318. if (!$form->{priceupdate}){
  319. $form->{priceupdate} = 'now';
  320. }
  321. $query = qq|
  322. UPDATE parts
  323. SET partnumber = ?,
  324. description = ?,
  325. makemodel = ?,
  326. alternate = ?,
  327. assembly = ?,
  328. listprice = ?,
  329. sellprice = ?,
  330. lastcost = ?,
  331. weight = ?,
  332. priceupdate = ?,
  333. unit = ?,
  334. notes = ?,
  335. rop = ?,
  336. bin = ?,
  337. inventory_accno_id = (SELECT id FROM chart
  338. WHERE accno = ?),
  339. income_accno_id = (SELECT id FROM chart
  340. WHERE accno = ?),
  341. expense_accno_id = (SELECT id FROM chart
  342. WHERE accno = ?),
  343. obsolete = ?,
  344. image = ?,
  345. drawing = ?,
  346. microfiche = ?,
  347. partsgroup_id = ?
  348. WHERE id = ?|;
  349. $sth = $dbh->prepare($query);
  350. $sth->execute(
  351. $form->{partnumber}, $form->{description}, $form->{makemodel},
  352. $form->{alternate}, $form->{assembly}, $form->{listprice},
  353. $form->{sellprice}, $form->{lastcost}, $form->{weight},
  354. $form->{priceupdate}, $form->{unit}, $form->{notes},
  355. $form->{rop}, $form->{bin}, $form->{inventory_accno},
  356. $form->{income_accno}, $form->{expense_accno},
  357. $form->{obsolete}, $form->{image}, $form->{drawing},
  358. $form->{microfiche}, $partsgroup_id, $form->{id}
  359. ) || $form->dberror($query);
  360. # insert makemodel records
  361. if ($form->{item} =~ /(part|assembly)/) {
  362. $query = qq|
  363. INSERT INTO makemodel (parts_id, make, model)
  364. VALUES (?, ?, ?)|;
  365. $sth = $dbh->prepare($query) || $form->dberror($query);
  366. for $i (1 .. $form->{makemodel_rows}) {
  367. if (($form->{"make_$i"} ne "")
  368. || ($form->{"model_$i"} ne "")) {
  369. $sth->execute(
  370. $form->{id}, $form->{"make_$i"},
  371. $form->{"model_$i"}
  372. ) || $form->dberror($query);
  373. }
  374. }
  375. }
  376. # insert taxes
  377. $query = qq|
  378. INSERT INTO partstax (parts_id, chart_id)
  379. VALUES (?, (SELECT id FROM chart WHERE accno = ?))|;
  380. $sth = $dbh->prepare($query);
  381. for (split / /, $form->{taxaccounts}) {
  382. if ($form->{"IC_tax_$_"}) {
  383. $sth->execute($form->{id}, $_)
  384. || $form->dberror($query);
  385. }
  386. }
  387. @a = localtime;
  388. $a[5] += 1900;
  389. $a[4]++;
  390. $a[4] = substr("0$a[4]", -2);
  391. $a[3] = substr("0$a[3]", -2);
  392. my $shippingdate = "$a[5]$a[4]$a[3]";
  393. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
  394. # add assembly records
  395. if ($form->{item} eq 'assembly' && !$project_id) {
  396. if ($form->{orphaned}) {
  397. $query = qq|
  398. INSERT INTO assembly
  399. (id, parts_id, qty, bom, adj)
  400. VALUES (?, ?, ?, ?, ?)|;
  401. $sth = $dbh->prepare($query);
  402. for $i (1 .. $form->{assembly_rows}) {
  403. $form->{"qty_$i"} = $form->parse_amount(
  404. $myconfig, $form->{"qty_$i"});
  405. $sth->execute(
  406. $form->{id}, $form->{"id_$i"},
  407. $form->{"qty_$i"}, $form->{"bom_$i"},
  408. $form->{"adj_$i"}
  409. ) || $form->dberror($query);
  410. }
  411. }
  412. # adjust onhand for the parts
  413. if ($form->{onhand}) {
  414. &adjust_inventory(
  415. $dbh, $form, $form->{id}, $form->{onhand});
  416. }
  417. }
  418. # add vendors
  419. if ($form->{item} ne 'assembly') {
  420. $updparts{$form->{id}} = 1;
  421. for $i (1 .. $form->{vendor_rows}) {
  422. if (($form->{"vendor_$i"} ne "")
  423. && $form->{"lastcost_$i"}) {
  424. ($null, $vendor_id)
  425. = split /--/, $form->{"vendor_$i"};
  426. for (qw(lastcost leadtime)) {
  427. $form->{"${_}_$i"}
  428. = $form->parse_amount(
  429. $myconfig,
  430. $form->{"${_}_$i"});
  431. }
  432. $query = qq|
  433. INSERT INTO partsvendor
  434. (vendor_id, parts_id,
  435. partnumber, lastcost,
  436. leadtime, curr)
  437. VALUES (?, ?, ?, ?, ?, ?)|;
  438. $sth = $dbh->prepare($query);
  439. $sth->execute(
  440. $vendor_id, $form->{id},
  441. $form->{"partnumber_$i"},
  442. $form->{"lastcost_$i"},
  443. $form->{"leadtime_$i"},
  444. $form->{"vendorcurr_$i"}
  445. )|| $form->dberror($query);
  446. }
  447. }
  448. }
  449. # add pricematrix
  450. for $i (1 .. $form->{customer_rows}) {
  451. for (qw(pricebreak customerprice)) {
  452. $form->{"${_}_$i"} = $form->parse_amount(
  453. $myconfig, $form->{"${_}_$i"});
  454. }
  455. if ($form->{"customerprice_$i"}) {
  456. ($null, $customer_id)
  457. = split /--/, $form->{"customer_$i"};
  458. $customer_id *= 1;
  459. ($null, $pricegroup_id)
  460. = split /--/, $form->{"pricegroup_$i"};
  461. my $validfrom;
  462. my $validto;
  463. $validfrom = $form->{"validfrom_$i"} if $form->{"validfrom_$i"};
  464. $validto = $form->{"validto_$i"} if $form->{"validto_$i"};
  465. $query = qq|
  466. INSERT INTO partscustomer
  467. (parts_id, customer_id,
  468. pricegroup_id, pricebreak,
  469. sellprice, curr,
  470. validfrom, validto)
  471. VALUES (?, ?, ?, ?, ?, ?, ?, ?)|;
  472. $sth = $dbh->prepare($query);
  473. $sth->execute(
  474. $form->{id}, $customer_id, $pricegroup_id,
  475. $form->{"pricebreak_$i"},
  476. $form->{"customerprice_$i"},
  477. $form->{"customercurr_$i"},
  478. $validfrom, $validto
  479. )|| $form->dberror($query);
  480. }
  481. }
  482. my $rc = $dbh->commit;
  483. $form->run_custom_queries('parts', 'UPDATE');
  484. $rc;
  485. }
  486. sub update_assembly {
  487. my ($dbh, $form, $id, $qty, $adj, $listprice, $sellprice, $lastcost,
  488. $weight) = @_;
  489. my $formlistprice = $form->{listprice};
  490. my $formsellprice = $form->{sellprice};
  491. if (!$adj) {
  492. $formlistprice = $listprice;
  493. $formsellprice = $sellprice;
  494. }
  495. my $query = qq|SELECT id, qty, adj FROM assembly WHERE parts_id = ?|;
  496. my $sth = $dbh->prepare($query);
  497. $sth->execute($id) || $form->dberror($query);
  498. $form->{$id} = 1; # Not sure what this is for...
  499. # In fact, we don't seem to use it... Chris T
  500. while (my ($pid, $aqty, $aadj) = $sth->fetchrow_array) {
  501. &update_assembly($dbh, $form, $pid, $aqty * $qty, $aadj,
  502. $listprice, $sellprice, $lastcost, $weight)
  503. if !$form->{$pid};
  504. }
  505. $sth->finish;
  506. $qty = $dbh->quote($qty);
  507. $formlistprice = $dbh->quote($formlistprice );
  508. $listprice = $dbh->quote($listprice );
  509. $formsellprice = $dbh->quote($formsellprice );
  510. $formlastcost = $dbh->quote($form->{lastcost});
  511. $lastcost = $dbh->quote($lastcost);
  512. $weight = $dbh->quote($weight);
  513. $id = $dbh->quote($id);
  514. $query = qq|
  515. UPDATE parts
  516. SET listprice = listprice +
  517. $qty * ($formlistprice - $listprice),
  518. sellprice = sellprice +
  519. $qty * ($formsellprice - $sellprice),
  520. lastcost = lastcost +
  521. $qty * ($form->{lastcost} - $lastcost),
  522. weight = weight +
  523. $qty * ($form->{weight} - $weight)
  524. WHERE id = $id|;
  525. $dbh->do($query) || $form->dberror($query);
  526. delete $form->{$id};
  527. }
  528. sub retrieve_assemblies {
  529. my ($self, $myconfig, $form) = @_;
  530. # connect to database
  531. my $dbh = $form->{dbh};
  532. my $where = '1 = 1';
  533. if ($form->{partnumber} ne "") {
  534. my $partnumber = $dbh->quote($form->like(
  535. lc $form->{partnumber}));
  536. $where .= " AND lower(p.partnumber) LIKE $partnumber";
  537. }
  538. if ($form->{description} ne "") {
  539. my $description = $dbh->($form->like(lc $form->{description}));
  540. $where .= " AND lower(p.description) LIKE $description";
  541. }
  542. $where .= qq| AND p.obsolete = '0'
  543. AND p.project_id IS NULL|;
  544. my %ordinal = (
  545. 'partnumber' => 2,
  546. 'description' => 3,
  547. 'bin' => 4
  548. );
  549. my @a = qw(partnumber description bin);
  550. my $sortorder = $form->sort_order(\@a, \%ordinal);
  551. # retrieve assembly items
  552. my $query = qq|
  553. SELECT p.id, p.partnumber, p.description, p.bin, p.onhand,
  554. p.rop
  555. FROM parts p
  556. WHERE $where
  557. AND p.assembly = '1'
  558. ORDER BY $sortorder|;
  559. my $sth = $dbh->prepare($query);
  560. $sth->execute || $form->dberror($query);
  561. $query = qq|
  562. SELECT sum(p.inventory_accno_id), p.assembly
  563. FROM parts p
  564. JOIN assembly a ON (a.parts_id = p.id)
  565. WHERE a.id = ?
  566. GROUP BY p.assembly|;
  567. my $svh = $dbh->prepare($query) || $form->dberror($query);
  568. my $inh;
  569. if ($form->{checkinventory}) {
  570. $query = qq|
  571. SELECT p.id, p.onhand, a.qty
  572. FROM parts p
  573. JOIN assembly a ON (a.parts_id = p.id)
  574. WHERE (p.inventory_accno_id > 0 OR p.assembly)
  575. AND p.income_accno_id > 0 AND a.id = ?|;
  576. $inh = $dbh->prepare($query) || $form->dberror($query);
  577. }
  578. my %available = ();
  579. my %required;
  580. my $ref;
  581. my $aref;
  582. my $stock;
  583. my $howmany;
  584. my $ok;
  585. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  586. $svh->execute($ref->{id});
  587. ($ref->{inventory}, $ref->{assembly}) = $svh->fetchrow_array;
  588. $svh->finish;
  589. if ($ref->{inventory} || $ref->{assembly}) {
  590. $ok = 1;
  591. if ($form->{checkinventory}) {
  592. $inh->execute($ref->{id})
  593. || $form->dberror($query);;
  594. $ok = 0;
  595. %required = ();
  596. while ($aref
  597. = $inh->fetchrow_hashref(NAME_lc)) {
  598. $available{$aref->{id}} =
  599. (exists $available{$aref->{id}})
  600. ? $available{$aref->{id}}
  601. : $aref->{onhand};
  602. $required{$aref->{id}} = $aref->{qty};
  603. if ($available{$aref->{id}}
  604. >= $aref->{qty}) {
  605. $howmany =
  606. ($aref->{qty})
  607. ? int $available{
  608. $aref->{id}}
  609. /$aref->{qty}
  610. : 1;
  611. if ($stock) {
  612. $stock =
  613. ($stock
  614. > $howmany)
  615. ? $howmany
  616. : $stock;
  617. } else {
  618. $stock = $howmany;
  619. }
  620. $ok = 1;
  621. $available{$aref->{id}}
  622. -= $aref->{qty}
  623. * $stock;
  624. } else {
  625. $ok = 0;
  626. for (keys %required) {
  627. $available{$_} +=
  628. $required{$_}
  629. * $stock;
  630. }
  631. $stock = 0;
  632. last;
  633. }
  634. }
  635. $inh->finish;
  636. $ref->{stock} = $stock;
  637. }
  638. push @{ $form->{assembly_items} }, $ref if $ok;
  639. }
  640. }
  641. $sth->finish;
  642. $dbh->commit;
  643. }
  644. sub restock_assemblies {
  645. my ($self, $myconfig, $form) = @_;
  646. # connect to database
  647. my $dbh = $form->{dbh};
  648. for my $i (1 .. $form->{rowcount}) {
  649. $form->{"qty_$i"} = $form->parse_amount(
  650. $myconfig, $form->{"qty_$i"});
  651. if ($form->{"qty_$i"}) {
  652. &adjust_inventory(
  653. $dbh, $form, $form->{"id_$i"},
  654. $form->{"qty_$i"});
  655. }
  656. }
  657. my $rc = $dbh->commit;
  658. $rc;
  659. }
  660. sub adjust_inventory {
  661. # Private method. Do not commit transaction at end of function...
  662. my ($dbh, $form, $id, $qty) = @_;
  663. my $query = qq|
  664. SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
  665. FROM parts p
  666. JOIN assembly a ON (a.parts_id = p.id)
  667. WHERE a.id = ?|;
  668. my $sth = $dbh->prepare($query);
  669. $sth->execute($id) || $form->dberror($query);
  670. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  671. # is it a service item then loop
  672. if (! $ref->{inventory_accno_id}) {
  673. next if ! $ref->{assembly};
  674. }
  675. # adjust parts onhand
  676. $form->update_balance(
  677. $dbh, "parts", "onhand", qq|id = $ref->{id}|,
  678. $qty * $ref->{qty} * -1);
  679. }
  680. $sth->finish;
  681. # update assembly
  682. $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
  683. }
  684. sub delete {
  685. my ($self, $myconfig, $form) = @_;
  686. my $dbh = $form->{dbh};
  687. my $query;
  688. $query = qq|DELETE FROM parts WHERE id = ?|;
  689. $sth = $dbh->prepare($query);
  690. $sth->execute($form->{id}) || $form->dberror($query);
  691. $query = qq|DELETE FROM partstax WHERE parts_id = ?|;
  692. $sth = $dbh->prepare($query);
  693. $sth->execute($form->{id}) || $form->dberror($query);
  694. if ($form->{item} ne 'assembly') {
  695. $query = qq|DELETE FROM partsvendor WHERE parts_id = ?|;
  696. $sth = $dbh->prepare($query);
  697. $sth->execute($form->{id}) || $form->dberror($query);
  698. }
  699. # check if it is a part, assembly or service
  700. if ($form->{item} ne 'service') {
  701. $query = qq|DELETE FROM makemodel WHERE parts_id = ?|;
  702. $sth = $dbh->prepare($query);
  703. $sth->execute($form->{id}) || $form->dberror($query);
  704. }
  705. if ($form->{item} eq 'assembly') {
  706. $query = qq|DELETE FROM assembly WHERE id = ?|;
  707. $sth = $dbh->prepare($query);
  708. $sth->execute($form->{id}) || $form->dberror($query);
  709. }
  710. $query = qq|DELETE FROM inventory WHERE parts_id = ?|;
  711. $sth = $dbh->prepare($query);
  712. $sth->execute($form->{id}) || $form->dberror($query);
  713. $query = qq|DELETE FROM partscustomer WHERE parts_id = ?|;
  714. $sth = $dbh->prepare($query);
  715. $sth->execute($form->{id}) || $form->dberror($query);
  716. $query = qq|DELETE FROM translation WHERE trans_id = ?|;
  717. $sth = $dbh->prepare($query);
  718. $sth->execute($form->{id}) || $form->dberror($query);
  719. # commit
  720. my $rc = $dbh->commit;
  721. $rc;
  722. }
  723. sub assembly_item {
  724. my ($self, $myconfig, $form) = @_;
  725. my $dbh = $form->{dbh};
  726. my $i = $form->{assembly_rows};
  727. my $var;
  728. my $null;
  729. my $where = "p.obsolete = '0'";
  730. if ($form->{"partnumber_$i"} ne "") {
  731. $var = $dbh->quote($form->like(lc $form->{"partnumber_$i"}));
  732. $where .= " AND lower(p.partnumber) LIKE $var";
  733. }
  734. if ($form->{"description_$i"} ne "") {
  735. $var = $dbh->quote($form->like(lc $form->{"description_$i"}));
  736. $where .= " AND lower(p.description) LIKE $var";
  737. }
  738. if ($form->{"partsgroup_$i"} ne "") {
  739. ($null, $var) = split /--/, $form->{"partsgroup_$i"};
  740. $var = $dbh->quote($var);
  741. $where .= qq| AND p.partsgroup_id = $var|;
  742. }
  743. if ($form->{id}) {
  744. $where .= " AND p.id != ".$dbh->quote($form->{id});
  745. }
  746. if ($form->{"description_$i"} ne "") {
  747. $where .= " ORDER BY p.description";
  748. } else {
  749. $where .= " ORDER BY p.partnumber";
  750. }
  751. my $query = qq|
  752. SELECT p.id, p.partnumber, p.description, p.sellprice,
  753. p.weight, p.onhand, p.unit, p.lastcost,
  754. pg.partsgroup, p.partsgroup_id
  755. FROM parts p
  756. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  757. WHERE $where|;
  758. my $sth = $dbh->prepare($query);
  759. $sth->execute || $form->dberror($query);
  760. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  761. push @{ $form->{item_list} }, $ref;
  762. }
  763. $sth->finish;
  764. }
  765. sub all_parts {
  766. my ($self, $myconfig, $form) = @_;
  767. $dbh = $form->{dbh};
  768. my $where = '1 = 1';
  769. my $null;
  770. my $var;
  771. my $ref;
  772. for (qw(partnumber drawing microfiche)) {
  773. if ($form->{$_} ne "") {
  774. $var = $dbh->quote($form->like(lc $form->{$_}));
  775. $where .= " AND lower(p.$_) LIKE $var";
  776. }
  777. }
  778. # special case for description
  779. if ($form->{description} ne "") {
  780. unless ($form->{bought} || $form->{sold} || $form->{onorder}
  781. || $form->{ordered} || $form->{rfq} || $form->{quoted}
  782. ) {
  783. $var = $dbh->quote($form->like(
  784. lc $form->{description}));
  785. $where .= " AND lower(p.description) LIKE $var";
  786. }
  787. }
  788. # assembly components
  789. my $assemblyflds;
  790. if ($form->{searchitems} eq 'component') {
  791. $assemblyflds = qq|, p1.partnumber AS assemblypartnumber,
  792. a.id AS assembly_id|;
  793. }
  794. # special case for serialnumber
  795. if ($form->{l_serialnumber}) {
  796. if ($form->{serialnumber} ne "") {
  797. $var = $dbh->quote(
  798. $form->like(lc $form->{serialnumber}));
  799. $where .= " AND lower(i.serialnumber) LIKE $var";
  800. }
  801. }
  802. if (($form->{warehouse} ne "") || $form->{l_warehouse}) {
  803. $form->{l_warehouse} = 1;
  804. }
  805. if ($form->{searchitems} eq 'part') {
  806. $where .= " AND p.inventory_accno_id > 0 AND p.income_accno_id > 0";
  807. }
  808. if ($form->{searchitems} eq 'assembly') {
  809. $form->{bought} = "";
  810. $where .= " AND p.assembly = '1'";
  811. }
  812. if ($form->{searchitems} eq 'service') {
  813. $where .= " AND p.assembly = '0' AND p.inventory_accno_id IS NULL";
  814. }
  815. if ($form->{searchitems} eq 'labor') {
  816. $where .= " AND p.inventory_accno_id > 0 AND p.income_accno_id IS NULL";
  817. }
  818. # items which were never bought, sold or on an order
  819. if ($form->{itemstatus} eq 'orphaned') {
  820. $where .= qq|
  821. AND p.onhand = 0
  822. AND p.id NOT IN (SELECT p.id FROM parts p
  823. JOIN invoice i
  824. ON (p.id = i.parts_id))
  825. AND p.id NOT IN (SELECT p.id FROM parts p
  826. JOIN assembly a
  827. ON (p.id = a.parts_id))
  828. AND p.id NOT IN (SELECT p.id FROM parts p
  829. JOIN orderitems o
  830. ON (p.id = o.parts_id))
  831. AND p.id NOT IN (SELECT p.id FROM parts p
  832. JOIN jcitems j
  833. ON (p.id = j.parts_id))|;
  834. }
  835. if ($form->{itemstatus} eq 'active') {
  836. $where .= " AND p.obsolete = '0'";
  837. }
  838. if ($form->{itemstatus} eq 'obsolete') {
  839. $where .= " AND p.obsolete = '1'";
  840. }
  841. if ($form->{itemstatus} eq 'onhand') {
  842. $where .= " AND p.onhand > 0";
  843. }
  844. if ($form->{itemstatus} eq 'short') {
  845. $where .= " AND p.onhand < p.rop";
  846. }
  847. my $makemodelflds = qq|, '', ''|;;
  848. my $makemodeljoin;
  849. if (($form->{make} ne "") || $form->{l_make} || ($form->{model} ne "")
  850. || $form->{l_model}) {
  851. $makemodelflds = qq|, m.make, m.model|;
  852. $makemodeljoin = qq|LEFT JOIN makemodel m ON (m.parts_id = p.id)|;
  853. if ($form->{make} ne "") {
  854. $var = $dbh->quote($form->like(lc $form->{make}));
  855. $where .= " AND lower(m.make) LIKE $var";
  856. }
  857. if ($form->{model} ne "") {
  858. $var = $dbh->quote($form->like(lc $form->{model}));
  859. $where .= " AND lower(m.model) LIKE $var";
  860. }
  861. }
  862. if ($form->{partsgroup} ne "") {
  863. ($null, $var) = split /--/, $form->{partsgroup};
  864. $where .= qq| AND p.partsgroup_id = | . $dbh->quote($var);
  865. }
  866. my %ordinal = (
  867. 'partnumber' => 2,
  868. 'description' => 3,
  869. 'bin' => 6,
  870. 'priceupdate' => 13,
  871. 'drawing' => 15,
  872. 'microfiche' => 16,
  873. 'partsgroup' => 18,
  874. 'make' => 21,
  875. 'model' => 22,
  876. 'assemblypartnumber' => 23
  877. );
  878. my @a = qw(partnumber description);
  879. my $sortorder = $form->sort_order(\@a, \%ordinal);
  880. my $query = qq|SELECT value FROM defaults WHERE setting_key = 'curr'|;
  881. my ($curr) = $dbh->selectrow_array($query);
  882. $curr =~ s/:.*//;
  883. $curr = $dbh->quote($curr);
  884. my $flds = qq|
  885. p.id, p.partnumber, p.description, p.onhand, p.unit,
  886. p.bin, p.sellprice, p.listprice, p.lastcost, p.rop,
  887. p.avgcost,
  888. p.weight, p.priceupdate, p.image, p.drawing, p.microfiche,
  889. p.assembly, pg.partsgroup, $curr AS curr,
  890. c1.accno AS inventory, c2.accno AS income, c3.accno AS expense,
  891. p.notes
  892. $makemodelflds $assemblyflds
  893. |;
  894. $query = qq|
  895. SELECT $flds
  896. FROM parts p
  897. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  898. LEFT JOIN chart c1 ON (c1.id = p.inventory_accno_id)
  899. LEFT JOIN chart c2 ON (c2.id = p.income_accno_id)
  900. LEFT JOIN chart c3 ON (c3.id = p.expense_accno_id)
  901. $makemodeljoin
  902. WHERE $where
  903. ORDER BY $sortorder|;
  904. # redo query for components report
  905. if ($form->{searchitems} eq 'component') {
  906. $flds =~ s/p.onhand/a.qty AS onhand/;
  907. $query = qq|
  908. SELECT $flds
  909. FROM assembly a
  910. JOIN parts p ON (a.parts_id = p.id)
  911. JOIN parts p1 ON (a.id = p1.id)
  912. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  913. LEFT JOIN chart c1 ON (c1.id = p.inventory_accno_id)
  914. LEFT JOIN chart c2 ON (c2.id = p.income_accno_id)
  915. LEFT JOIN chart c3 ON (c3.id = p.expense_accno_id)
  916. $makemodeljoin
  917. WHERE $where
  918. ORDER BY $sortorder|;
  919. }
  920. # rebuild query for bought and sold items
  921. if ($form->{bought} || $form->{sold} || $form->{onorder}
  922. || $form->{ordered} || $form->{rfq} || $form->{quoted}
  923. ) {
  924. $form->sort_order();
  925. @a = qw(partnumber description curr employee name
  926. serialnumber id);
  927. push @a, "invnumber" if ($form->{bought} || $form->{sold});
  928. push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
  929. push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
  930. %ordinal = (
  931. 'partnumber' => 2,
  932. 'description' => 3,
  933. 'serialnumber' => 4,
  934. 'bin' => 7,
  935. 'priceupdate' => 14,
  936. 'partsgroup' => 19,
  937. 'invnumber' => 20,
  938. 'ordnumber' => 21,
  939. 'quonumber' => 22,
  940. 'name' => 24,
  941. 'employee' => 25,
  942. 'curr' => 26,
  943. 'make' => 29,
  944. 'model' => 30
  945. );
  946. $sortorder = $form->sort_order(\@a, \%ordinal);
  947. my $union = "";
  948. $query = "";
  949. if ($form->{bought} || $form->{sold}) {
  950. my $invwhere = "$where";
  951. my $transdate =
  952. ($form->{method} eq 'accrual')
  953. ? "transdate"
  954. : "datepaid";
  955. $invwhere .= " AND i.assemblyitem = '0'";
  956. $invwhere .= " AND a.$transdate >= " .
  957. $dbh->quote($form->{transdatefrom})
  958. if $form->{transdatefrom};
  959. $invwhere .= " AND a.$transdate <= " .
  960. $dbh->quote($form->{transdateto})
  961. if $form->{transdateto};
  962. if ($form->{description} ne "") {
  963. $var = dbh->quote(
  964. $form->like(lc $form->{description}));
  965. $invwhere .=
  966. " AND lower(i.description) LIKE $var";
  967. }
  968. if ($form->{open} || $form->{closed}) {
  969. if ($form->{open} && $form->{closed}) {
  970. if ($form->{method} eq 'cash') {
  971. $invwhere .=
  972. " AND a.amount = a.paid";
  973. }
  974. } else {
  975. if ($form->{open}) {
  976. if ($form->{method} eq 'cash') {
  977. $invwhere .=
  978. " AND a.id = 0";
  979. } else {
  980. $invwhere .=
  981. " AND NOT a.amount = a.paid";
  982. }
  983. } else {
  984. $invwhere .=
  985. " AND a.amount = a.paid";
  986. }
  987. }
  988. } else {
  989. $invwhere .= " AND a.id = 0";
  990. }
  991. my $flds = qq|
  992. p.id, p.partnumber, i.description,
  993. i.serialnumber, i.qty AS onhand, i.unit, p.bin,
  994. i.sellprice, p.listprice, p.lastcost, p.rop,
  995. p.weight, p.avgcost, p.priceupdate, p.image,
  996. p.drawing, p.microfiche, p.assembly,
  997. pg.partsgroup, a.invnumber, a.ordnumber,
  998. a.quonumber, i.trans_id, ct.name,
  999. e.name AS employee, a.curr, a.till, p.notes
  1000. $makemodelfld|;
  1001. if ($form->{bought}) {
  1002. my $rflds = $flds;
  1003. $rflds =~
  1004. s/i.qty AS onhand/i.qty * -1 AS onhand/;
  1005. $query = qq|
  1006. SELECT $rflds, 'ir' AS module,
  1007. '' AS type,
  1008. (SELECT sell
  1009. FROM exchangerate ex
  1010. WHERE ex.curr = a.curr
  1011. AND ex.transdate
  1012. = a.$transdate)
  1013. AS exchangerate, i.discount
  1014. FROM invoice i
  1015. JOIN parts p
  1016. ON (p.id = i.parts_id)
  1017. JOIN ap a ON (a.id = i.trans_id)
  1018. JOIN vendor ct
  1019. ON (a.vendor_id = ct.id)
  1020. LEFT JOIN partsgroup pg
  1021. ON (p.partsgroup_id = pg.id)
  1022. LEFT JOIN employee e
  1023. ON (a.employee_id = e.id)
  1024. $makemodeljoin
  1025. WHERE $invwhere|;
  1026. $union = "
  1027. UNION ALL";
  1028. }
  1029. if ($form->{sold}) {
  1030. $query .= qq|
  1031. $union
  1032. SELECT $flds, 'is' AS module,
  1033. '' AS type,
  1034. (SELECT buy
  1035. FROM exchangerate ex
  1036. WHERE ex.curr = a.curr
  1037. AND ex.transdate
  1038. = a.$transdate)
  1039. AS exchangerate, i.discount
  1040. FROM invoice i
  1041. JOIN parts p
  1042. ON (p.id = i.parts_id)
  1043. JOIN ar a ON (a.id = i.trans_id)
  1044. JOIN customer ct
  1045. ON (a.customer_id = ct.id)
  1046. LEFT JOIN partsgroup pg
  1047. ON (p.partsgroup_id = pg.id)
  1048. LEFT JOIN employee e
  1049. ON (a.employee_id = e.id)
  1050. $makemodeljoin
  1051. WHERE $invwhere|;
  1052. $union = "
  1053. UNION ALL";
  1054. }
  1055. }
  1056. if ($form->{onorder} || $form->{ordered}) {
  1057. my $ordwhere = "$where
  1058. AND a.quotation = '0'";
  1059. $ordwhere .= " AND a.transdate >= ".
  1060. $dbh->quote($form->{transdatefrom})
  1061. if $form->{transdatefrom};
  1062. $ordwhere .= " AND a.transdate <= ".
  1063. $dbh->quote($form->{transdateto})
  1064. if $form->{transdateto};
  1065. if ($form->{description} ne "") {
  1066. $var = $dbh->quote(
  1067. $form->like(lc $form->{description}));
  1068. $ordwhere .= " AND lower(i.description) LIKE $var";
  1069. }
  1070. if ($form->{open} || $form->{closed}) {
  1071. unless ($form->{open} && $form->{closed}) {
  1072. $ordwhere .= " AND a.closed = '0'"
  1073. if $form->{open};
  1074. $ordwhere .= " AND a.closed = '1'"
  1075. if $form->{closed};
  1076. }
  1077. } else {
  1078. $ordwhere .= " AND a.id = 0";
  1079. }
  1080. $flds = qq|
  1081. p.id, p.partnumber, i.description,
  1082. i.serialnumber, i.qty AS onhand, i.unit, p.bin,
  1083. i.sellprice, p.listprice, p.lastcost, p.rop,
  1084. p.weight, p.avgcost, p.priceupdate, p.image,
  1085. p.drawing, p.microfiche, p.assembly,
  1086. pg.partsgroup, '' AS invnumber, a.ordnumber,
  1087. a.quonumber, i.trans_id, ct.name,
  1088. e.name AS employee, a.curr, '0' AS till,
  1089. p.notes
  1090. $makemodelfld|;
  1091. if ($form->{ordered}) {
  1092. $query .= qq|
  1093. $union
  1094. SELECT $flds, 'oe' AS module,
  1095. 'sales_order' AS type,
  1096. (SELECT buy
  1097. FROM exchangerate ex
  1098. WHERE ex.curr = a.curr
  1099. AND ex.transdate
  1100. = a.transdate)
  1101. AS exchangerate, i.discount
  1102. FROM orderitems i
  1103. JOIN parts p ON (i.parts_id = p.id)
  1104. JOIN oe a ON (i.trans_id = a.id)
  1105. JOIN customer ct
  1106. ON (a.customer_id = ct.id)
  1107. LEFT JOIN partsgroup pg
  1108. ON (p.partsgroup_id = pg.id)
  1109. LEFT JOIN employee e
  1110. ON (a.employee_id = e.id)
  1111. $makemodeljoin
  1112. WHERE $ordwhere
  1113. AND a.customer_id > 0|;
  1114. $union = "
  1115. UNION ALL";
  1116. }
  1117. if ($form->{onorder}) {
  1118. $flds = qq|
  1119. p.id, p.partnumber, i.description,
  1120. i.serialnumber, i.qty AS onhand, i.unit,
  1121. p.bin, i.sellprice, p.listprice,
  1122. p.lastcost, p.rop, p.weight, p.avgcost,
  1123. p.priceupdate, p.image, p.drawing,
  1124. p.microfiche, p.assembly,
  1125. pg.partsgroup, '' AS invnumber,
  1126. a.ordnumber, a.quonumber,
  1127. i.trans_id, ct.name,e.name AS employee,
  1128. a.curr, '0' AS till, p.notes
  1129. $makemodelfld|;
  1130. $query .= qq|
  1131. $union
  1132. SELECT $flds, 'oe' AS module,
  1133. 'purchase_order' AS type,
  1134. (SELECT sell
  1135. FROM exchangerate ex
  1136. WHERE ex.curr = a.curr
  1137. AND ex.transdate
  1138. = a.transdate)
  1139. AS exchangerate, i.discount
  1140. FROM orderitems i
  1141. JOIN parts p ON (i.parts_id = p.id)
  1142. JOIN oe a ON (i.trans_id = a.id)
  1143. JOIN vendor ct
  1144. ON (a.vendor_id = ct.id)
  1145. LEFT JOIN partsgroup pg
  1146. ON (p.partsgroup_id = pg.id)
  1147. LEFT JOIN employee e
  1148. ON (a.employee_id = e.id)
  1149. $makemodeljoin
  1150. WHERE $ordwhere
  1151. AND a.vendor_id > 0|;
  1152. }
  1153. }
  1154. if ($form->{rfq} || $form->{quoted}) {
  1155. my $quowhere = "$where
  1156. AND a.quotation = '1'";
  1157. $quowhere .= " AND a.transdate >= ".
  1158. $dbh->quote($form->{transdatefrom})
  1159. if $form->{transdatefrom};
  1160. $quowhere .= " AND a.transdate <= ".
  1161. $dbh->quote($form->{transdateto})
  1162. if $form->{transdateto};
  1163. if ($form->{description} ne "") {
  1164. $var = $dbh->quote(
  1165. $form->like(lc $form->{description}));
  1166. $quowhere .= " AND lower(i.description) LIKE $var";
  1167. }
  1168. if ($form->{open} || $form->{closed}) {
  1169. unless ($form->{open} && $form->{closed}) {
  1170. $ordwhere .= " AND a.closed = '0'"
  1171. if $form->{open};
  1172. $ordwhere .= " AND a.closed = '1'"
  1173. if $form->{closed};
  1174. }
  1175. } else {
  1176. $ordwhere .= " AND a.id = 0";
  1177. }
  1178. $flds = qq|
  1179. p.id, p.partnumber, i.description,
  1180. i.serialnumber, i.qty AS onhand, i.unit, p.bin,
  1181. i.sellprice, p.listprice, p.lastcost, p.rop,
  1182. p.weight, p.avgcost, p.priceupdate, p.image,
  1183. p.drawing, p.microfiche, p.assembly,
  1184. pg.partsgroup, '' AS invnumber, a.ordnumber,
  1185. a.quonumber, i.trans_id, ct.name,
  1186. e.name AS employee, a.curr, '0' AS till, p.notes
  1187. $makemodelfld|;
  1188. if ($form->{quoted}) {
  1189. $query .= qq|
  1190. $union
  1191. SELECT $flds, 'oe' AS module,
  1192. 'sales_quotation' AS type,
  1193. (SELECT buy
  1194. FROM exchangerate ex
  1195. WHERE ex.curr = a.curr
  1196. AND ex.transdate
  1197. = a.transdate)
  1198. AS exchangerate,
  1199. i.discount
  1200. FROM orderitems i
  1201. JOIN parts p ON (i.parts_id = p.id)
  1202. JOIN oe a ON (i.trans_id = a.id)
  1203. JOIN customer ct
  1204. ON (a.customer_id = ct.id)
  1205. LEFT JOIN partsgroup pg
  1206. ON (p.partsgroup_id = pg.id)
  1207. LEFT JOIN employee e
  1208. ON (a.employee_id = e.id)
  1209. $makemodeljoin
  1210. WHERE $quowhere
  1211. AND a.customer_id > 0|;
  1212. $union = "
  1213. UNION ALL";
  1214. }
  1215. if ($form->{rfq}) {
  1216. $flds = qq|
  1217. p.id, p.partnumber, i.description,
  1218. i.serialnumber, i.qty AS onhand,
  1219. i.unit, p.bin, i.sellprice, p.listprice,
  1220. p.lastcost, p.rop, p.weight, p.avgcost,
  1221. p.priceupdate, p.image, p.drawing,
  1222. p.microfiche, p.assembly,
  1223. pg.partsgroup, '' AS invnumber,
  1224. a.ordnumber, a.quonumber,
  1225. i.trans_id, ct.name, e.name AS employee,
  1226. a.curr, '0' AS till, p.notes
  1227. $makemodelfld|;
  1228. $query .= qq|
  1229. $union
  1230. SELECT $flds, 'oe' AS module,
  1231. 'request_quotation' AS type,
  1232. (SELECT sell
  1233. FROM exchangerate ex
  1234. WHERE ex.curr = a.curr
  1235. AND ex.transdate
  1236. = a.transdate)
  1237. AS exchangerate, i.discount
  1238. FROM orderitems i
  1239. JOIN parts p ON (i.parts_id = p.id)
  1240. JOIN oe a ON (i.trans_id = a.id)
  1241. JOIN vendor ct
  1242. ON (a.vendor_id = ct.id)
  1243. LEFT JOIN partsgroup pg
  1244. ON (p.partsgroup_id = pg.id)
  1245. LEFT JOIN employee e
  1246. ON (a.employee_id = e.id)
  1247. $makemodeljoin
  1248. WHERE $quowhere
  1249. AND a.vendor_id > 0|;
  1250. }
  1251. }
  1252. $query .= qq|
  1253. ORDER BY $sortorder|;
  1254. }
  1255. my $sth = $dbh->prepare($query);
  1256. $sth->execute || $form->dberror($query);
  1257. $query = qq|
  1258. SELECT c.accno
  1259. FROM chart c
  1260. JOIN partstax pt ON (pt.chart_id = c.id)
  1261. WHERE pt.parts_id = ?
  1262. ORDER BY accno|;
  1263. my $pth = $dbh->prepare($query) || $form->dberror($query);
  1264. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1265. $pth->execute($ref->{id});
  1266. while (($accno) = $pth->fetchrow_array) {
  1267. $ref->{tax} .= "$accno ";
  1268. }
  1269. $pth->finish;
  1270. push @{ $form->{parts} }, $ref;
  1271. }
  1272. $sth->finish;
  1273. @a = ();
  1274. # include individual items for assembly
  1275. if (($form->{searchitems} eq 'assembly') && $form->{individual}) {
  1276. if ($form->{sold} || $form->{ordered} || $form->{quoted}) {
  1277. $flds = qq|
  1278. p.id, p.partnumber, p.description,
  1279. p.onhand AS perassembly, p.unit, p.bin,
  1280. p.sellprice, p.listprice, p.lastcost, p.rop,
  1281. p.avgcost, p.weight, p.priceupdate, p.image,
  1282. p.drawing, p.microfiche, p.assembly,
  1283. pg.partsgroup, p.notes
  1284. $makemodelflds $assemblyflds |;
  1285. } else {
  1286. # replace p.onhand with a.qty AS onhand
  1287. $flds =~ s/p\.onhand/a.qty AS perassembly/;
  1288. }
  1289. for (@{ $form->{parts} }) {
  1290. push @a, $_;
  1291. $_->{perassembly} = 1;
  1292. $flds =~ s/p\.onhand*AS perassembly/p\.onhand, a\.qty AS perassembly/;
  1293. push @a, &include_assembly(
  1294. $dbh, $myconfig, $form, $_->{id}, $flds,
  1295. $makemodeljoin);
  1296. push @a, {id => $_->{id}, assemblyitem => 1};
  1297. }
  1298. # copy assemblies to $form->{parts}
  1299. @{ $form->{parts} } = @a;
  1300. }
  1301. @a = ();
  1302. if (($form->{warehouse} ne "") || $form->{l_warehouse}) {
  1303. if ($form->{warehouse} ne "") {
  1304. my ($desc, $var) = split /--/, $form->{warehouse};
  1305. $var = $dbh->quote($var);
  1306. $desc = $dbh->quote($desc);
  1307. $query = qq|
  1308. SELECT SUM(qty) AS onhand,
  1309. $desc AS description
  1310. FROM inventory
  1311. WHERE warehouse_id = $var
  1312. AND parts_id = ?|;
  1313. } else {
  1314. $query = qq|
  1315. SELECT SUM(i.qty) AS onhand,
  1316. w.description AS warehouse
  1317. FROM inventory i
  1318. JOIN warehouse w ON (w.id = i.warehouse_id)
  1319. WHERE i.parts_id = ?
  1320. GROUP BY w.description|;
  1321. }
  1322. $sth = $dbh->prepare($query) || $form->dberror($query);
  1323. for (@{ $form->{parts} }) {
  1324. $sth->execute($_->{id}) || $form->dberror($query);
  1325. if ($form->{warehouse} ne "") {
  1326. $ref = $sth->fetchrow_hashref(NAME_lc);
  1327. if ($ref->{onhand} != 0) {
  1328. $_->{onhand} = $ref->{onhand};
  1329. push @a, $_;
  1330. }
  1331. } else {
  1332. push @a, $_;
  1333. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1334. if ($ref->{onhand} > 0) {
  1335. push @a, $ref;
  1336. }
  1337. }
  1338. }
  1339. $sth->finish;
  1340. }
  1341. @{ $form->{parts} } = @a;
  1342. }
  1343. $dbh->commit;
  1344. }
  1345. sub include_assembly {
  1346. my ($dbh, $myconfig, $form, $id, $flds, $makemodeljoin) = @_;
  1347. $form->{stagger}++;
  1348. if ($form->{stagger} > $form->{pncol}) {
  1349. $form->{pncol} = $form->{stagger};
  1350. }
  1351. $form->{$id} = 1;
  1352. my @a = qw(partnumber description bin);
  1353. if ($form->{sort} eq 'partnumber') {
  1354. $sortorder = "TRUE";
  1355. } else {
  1356. @a = grep !/$form->{sort}/, @a;
  1357. $sortorder = "$form->{sort} $form->{direction}, ". join ',', @a;
  1358. }
  1359. @a = ();
  1360. my $query = qq|
  1361. SELECT $flds
  1362. FROM parts p
  1363. JOIN assembly a ON (a.parts_id = p.id)
  1364. LEFT JOIN partsgroup pg ON (pg.id = p.id)
  1365. LEFT JOIN chart c1 ON (c1.id = p.inventory_accno_id)
  1366. LEFT JOIN chart c2 ON (c2.id = p.income_accno_id)
  1367. LEFT JOIN chart c3 ON (c3.id = p.expense_accno_id)
  1368. $makemodeljoin
  1369. WHERE a.id = ?
  1370. ORDER BY $sortorder|;
  1371. my $sth = $dbh->prepare($query);
  1372. $sth->execute($id) || $form->dberror($query);
  1373. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1374. $ref->{assemblyitem} = 1;
  1375. $ref->{stagger} = $form->{stagger};
  1376. push @a, $ref;
  1377. if ($ref->{assembly} && !$form->{$ref->{id}}) {
  1378. push @a, &include_assembly(
  1379. $dbh, $myconfig, $form, $ref->{id}, $flds,
  1380. $makemodeljoin);
  1381. if ($form->{stagger} > $form->{pncol}) {
  1382. $form->{pncol} = $form->{stagger};
  1383. }
  1384. }
  1385. }
  1386. $sth->finish;
  1387. $form->{$id} = 0;
  1388. $form->{stagger}--;
  1389. @a;
  1390. }
  1391. sub requirements {
  1392. my ($self, $myconfig, $form) = @_;
  1393. my $dbh = $form->{dbh};
  1394. my $null;
  1395. my $var;
  1396. my $ref;
  1397. my $where = qq|p.obsolete = '0'|;
  1398. my $dwhere;
  1399. for (qw(partnumber description)) {
  1400. if ($form->{$_} ne "") {
  1401. $var = $dbh->quote($form->like(lc $form->{$_}));
  1402. $where .= qq| AND lower(p.$_) LIKE $var|;
  1403. }
  1404. }
  1405. if ($form->{partsgroup} ne "") {
  1406. ($null, $var) = split /--/, $form->{partsgroup};
  1407. $var = $dbh->quote($var);
  1408. $where .= qq| AND p.partsgroup_id = $var|;
  1409. }
  1410. # connect to database
  1411. my ($transdatefrom, $transdateto);
  1412. if ($form->{year}) {
  1413. ($transdatefrom, $transdateto)
  1414. = $form->from_to($form->{year}, '01', 12);
  1415. $dwhere = qq| AND a.transdate >= '$transdatefrom'
  1416. AND a.transdate <= '$transdateto'|;
  1417. }
  1418. $query = qq|
  1419. SELECT p.id, p.partnumber, p.description, sum(i.qty) AS qty,
  1420. p.onhand, extract(MONTH FROM a.transdate) AS month,
  1421. '0' AS so, '0' AS po
  1422. FROM invoice i
  1423. JOIN parts p ON (p.id = i.parts_id)
  1424. JOIN ar a ON (a.id = i.trans_id)
  1425. WHERE $where $dwhere AND p.inventory_accno_id > 0
  1426. GROUP BY p.id, p.partnumber, p.description, p.onhand,
  1427. extract(MONTH FROM a.transdate)|;
  1428. my $sth = $dbh->prepare($query);
  1429. $sth->execute || $form->dberror($query);
  1430. my %parts;
  1431. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1432. $parts{$ref->{id}} = $ref;
  1433. }
  1434. $sth->finish;
  1435. my %ofld = ( customer => so, vendor => po );
  1436. for (qw(customer vendor)) {
  1437. $query = qq|
  1438. SELECT p.id, p.partnumber, p.description,
  1439. sum(qty) - sum(ship) AS $ofld{$_}, p.onhand,
  1440. 0 AS month
  1441. FROM orderitems i
  1442. JOIN parts p ON (p.id = i.parts_id)
  1443. JOIN oe a ON (a.id = i.trans_id)
  1444. WHERE $where AND p.inventory_accno_id > 0
  1445. AND p.assembly = '0' AND a.closed = '0'
  1446. AND a.${_}_id > 0
  1447. GROUP BY p.id, p.partnumber, p.description, p.onhand,
  1448. month|;
  1449. $sth = $dbh->prepare($query);
  1450. $sth->execute || $form->dberror($query);
  1451. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1452. if (exists $parts{$ref->{id}}->{$ofld{$_}}) {
  1453. $parts{$ref->{id}}->{$ofld{$_}}
  1454. += $ref->{$ofld{$_}};
  1455. } else {
  1456. $parts{$ref->{id}} = $ref;
  1457. }
  1458. }
  1459. $sth->finish;
  1460. }
  1461. # add assemblies from open sales orders
  1462. $query = qq|
  1463. SELECT DISTINCT a.id AS orderid, b.id, i.qty - i.ship AS qty
  1464. FROM parts p
  1465. JOIN assembly b ON (b.parts_id = p.id)
  1466. JOIN orderitems i ON (i.parts_id = b.id)
  1467. JOIN oe a ON (a.id = i.trans_id)
  1468. WHERE $where
  1469. AND (p.inventory_accno_id > 0 OR p.assembly = '1')
  1470. AND a.closed = '0'|;
  1471. $sth = $dbh->prepare($query);
  1472. $sth->execute || $form->dberror($query);
  1473. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1474. &requirements_assembly(
  1475. $dbh, $form, \%parts, $ref->{id}, $ref->{qty}, $where)
  1476. if $ref->{qty};
  1477. }
  1478. $sth->finish;
  1479. $dbh->commit;
  1480. for (sort { $parts{$a}->{$form->{sort}} cmp $parts{$b}->{$form->{sort}}} keys %parts) {
  1481. push @{ $form->{parts} }, $parts{$_};
  1482. }
  1483. }
  1484. sub requirements_assembly {
  1485. my ($dbh, $form, $parts, $id, $qty, $where) = @_;
  1486. # assemblies
  1487. my $query = qq|
  1488. SELECT p.id, p.partnumber, p.description, a.qty * ? AS so,
  1489. p.onhand, p.assembly, p.partsgroup_id
  1490. FROM assembly a
  1491. JOIN parts p ON (p.id = a.parts_id)
  1492. WHERE $where AND a.id = ? AND p.inventory_accno_id > 0
  1493. UNION
  1494. SELECT p.id, p.partnumber, p.description, a.qty * ? AS so,
  1495. p.onhand, p.assembly, p.partsgroup_id
  1496. FROM assembly a
  1497. JOIN parts p ON (p.id = a.parts_id)
  1498. WHERE a.id = ? AND p.assembly = '1'|;
  1499. my $sth = $dbh->prepare($query);
  1500. $sth->execute($qty, $id, $qty, $id) || $form->dberror($query);
  1501. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1502. if ($ref->{assembly}) {
  1503. &requirements_assembly(
  1504. $dbh, $form, $parts, $ref->{id}, $ref->{so},
  1505. $where);
  1506. next;
  1507. }
  1508. if (exists $parts->{$ref->{id}}{so}) {
  1509. $parts->{$ref->{id}}{so} += $ref->{so};
  1510. } else {
  1511. $parts->{$ref->{id}} = $ref;
  1512. }
  1513. }
  1514. $sth->finish;
  1515. }
  1516. sub create_links {
  1517. my ($self, $module, $myconfig, $form) = @_;
  1518. # connect to database
  1519. my $dbh = $form->{dbh};
  1520. my $ref;
  1521. my $query = qq|
  1522. SELECT accno, description, link
  1523. FROM chart
  1524. WHERE link LIKE ?
  1525. ORDER BY accno|;
  1526. my $sth = $dbh->prepare($query);
  1527. $sth->execute("%$module%") || $form->dberror($query);
  1528. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1529. foreach my $key (split /:/, $ref->{link}) {
  1530. if ($key =~ /$module/) {
  1531. push @{ $form->{"${module}_links"}{$key} },
  1532. { accno => $ref->{accno},
  1533. description => $ref->{description} };
  1534. }
  1535. }
  1536. }
  1537. $sth->finish;
  1538. if ($form->{item} ne 'assembly') {
  1539. $query = qq|SELECT count(*) FROM vendor|;
  1540. my ($count) = $dbh->selectrow_array($query);
  1541. if ($count < $myconfig->{vclimit}) {
  1542. $query = qq|SELECT id, name FROM vendor ORDER BY name|;
  1543. $sth = $dbh->prepare($query);
  1544. $sth->execute || $form->dberror($query);
  1545. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1546. push @{ $form->{all_vendor} }, $ref;
  1547. }
  1548. $sth->finish;
  1549. }
  1550. }
  1551. # pricegroups, customers
  1552. $query = qq|SELECT count(*) FROM customer|;
  1553. ($count) = $dbh->selectrow_array($query);
  1554. if ($count < $myconfig->{vclimit}) {
  1555. $query = qq|SELECT id, name FROM customer ORDER BY name|;
  1556. $sth = $dbh->prepare($query);
  1557. $sth->execute || $form->dberror($query);
  1558. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1559. push @{ $form->{all_customer} }, $ref;
  1560. }
  1561. $sth->finish;
  1562. }
  1563. $query = qq|SELECT id, pricegroup FROM pricegroup ORDER BY pricegroup|;
  1564. $sth = $dbh->prepare($query);
  1565. $sth->execute || $form->dberror($query);
  1566. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  1567. push @{ $form->{all_pricegroup} }, $ref;
  1568. }
  1569. $sth->finish;
  1570. if ($form->{id}) {
  1571. $query = qq|
  1572. SELECT value FROM defaults
  1573. WHERE setting_key = 'weightunit'|;
  1574. ($form->{weightunit}) = $dbh->selectrow_array($query);
  1575. $query = qq|
  1576. SELECT value FROM defaults
  1577. WHERE setting_key = 'curr'|;
  1578. ($form->{currencies}) = $dbh->selectrow_array($query);
  1579. } else {
  1580. # Dieter: FIXME left joins not working
  1581. $query = qq|
  1582. SELECT (SELECT value FROM defaults
  1583. WHERE setting_key = 'weightunit')
  1584. AS weightunit, current_date AS priceupdate,
  1585. (SELECT value FROM defaults
  1586. WHERE setting_key = 'curr') AS currencies,
  1587. c1.accno AS inventory_accno,
  1588. c1.description AS inventory_description,
  1589. c2.accno AS income_accno,
  1590. c2.description AS income_description,
  1591. c3.accno AS expense_accno,
  1592. c3.description AS expense_description
  1593. FROM chart c1, chart c2, chart c3
  1594. WHERE c1.id IN (SELECT value FROM defaults
  1595. WHERE setting_key = 'inventory_accno_id')
  1596. AND c2.id IN (SELECT value FROM defaults
  1597. WHERE setting_key = 'income_accno_id')
  1598. AND c3.id IN (SELECT value FROM defaults
  1599. WHERE setting_key
  1600. = 'expense_accno_id')|;
  1601. $sth = $dbh->prepare($query);
  1602. $sth->execute || $form->dberror($query);
  1603. $ref = $sth->fetchrow_hashref(NAME_lc);
  1604. for (qw(weightunit priceupdate currencies)) {
  1605. $form->{$_} = $ref->{$_};
  1606. }
  1607. # setup accno hash, {amount} is used in create_links
  1608. for (qw(inventory income expense)) {
  1609. $form->{amount}{"IC_$_"}
  1610. = {
  1611. accno => $ref->{"${_}_accno"},
  1612. description => $ref->{"${_}_description"}
  1613. };
  1614. }
  1615. $sth->finish;
  1616. }
  1617. $dbh->commit;
  1618. }
  1619. sub get_warehouses {
  1620. my ($self, $myconfig, $form) = @_;
  1621. my $dbh = $form->{dbh};
  1622. my $query = qq|SELECT id, description FROM warehouse|;
  1623. my $sth = $dbh->prepare($query);
  1624. $sth->execute || $form->dberror($query);
  1625. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  1626. push @{ $form->{all_warehouse} }, $ref;
  1627. }
  1628. $sth->finish;
  1629. $dbh->commit;
  1630. }
  1631. 1;