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