-sub retrieve_assemblies {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
-
- # connect to database
- my $dbh = $form->dbconnect($myconfig);
-
- my $where = '1 = 1';
-
- if ($form->{partnumber}) {
- my $partnumber = $form->like(lc $form->{partnumber});
- $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
- }
-
- if ($form->{description}) {
- my $description = $form->like(lc $form->{description});
- $where .= " AND lower(p.description) LIKE '$description'";
- }
- $where .= " AND NOT p.obsolete = '1'";
-
- # retrieve assembly items
- my $query = qq|SELECT p.id, p.partnumber, p.description,
- p.bin, p.onhand, p.rop,
- (SELECT sum(p2.inventory_accno_id)
- FROM parts p2, assembly a
- WHERE p2.id = a.parts_id
- AND a.id = p.id) AS inventory
- FROM parts p
- WHERE $where
- AND assembly = '1'|;
-
- my $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
- push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
- }
- $sth->finish;
-
- $dbh->disconnect;
-
- $main::lxdebug->leave_sub();
-}
-
-sub restock_assemblies {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
-
- # connect to database
- my $dbh = $form->dbconnect_noauto($myconfig);
-
- for my $i (1 .. $form->{rowcount}) {
-
- $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
-
- if ($form->{"qty_$i"} != 0) {
- &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
- }
-
- }
-
- my $rc = $dbh->commit;
- $dbh->disconnect;
-
- $main::lxdebug->leave_sub();
-
- return $rc;
-}
-
-sub adjust_inventory {
- $main::lxdebug->enter_sub();
-
- my ($dbh, $form, $id, $qty) = @_;
-
- my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
- FROM parts p, assembly a
- WHERE a.parts_id = p.id
- AND a.id = $id|;
- my $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-
- my $allocate = $qty * $ref->{qty};
-
- # is it a service item, then loop
- $ref->{inventory_accno_id} *= 1;
- next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
-
- # adjust parts onhand
- $form->update_balance($dbh, "parts", "onhand",
- qq|id = $ref->{id}|,
- $allocate * -1);
- }
-
- $sth->finish;
-
- # update assembly
- my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
-
- $main::lxdebug->leave_sub();
-
- return $rc;
-}
-
-sub delete {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
-
- # connect to database, turn off AutoCommit
- my $dbh = $form->dbconnect_noauto($myconfig);
-
- # first delete prices of pricegroup
- my $query = qq|DELETE FROM prices
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- my $query = qq|DELETE FROM parts
- WHERE id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- $query = qq|DELETE FROM partstax
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- # check if it is a part, assembly or service
- if ($form->{item} ne 'service') {
- $query = qq|DELETE FROM makemodel
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
- }
-
- if ($form->{item} eq 'assembly') {
-
- # delete inventory
- $query = qq|DELETE FROM inventory
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- $query = qq|DELETE FROM assembly
- WHERE id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
- }
-
- if ($form->{item} eq 'alternate') {
- $query = qq|DELETE FROM alternate
- WHERE id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
- }
-
- # commit
- my $rc = $dbh->commit;
- $dbh->disconnect;
-
- $main::lxdebug->leave_sub();
-
- return $rc;
-}
-
-sub assembly_item {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
-
- my $i = $form->{assembly_rows};
- my $var;
- my $where = "1 = 1";
-
- if ($form->{"partnumber_$i"}) {
- $var = $form->like(lc $form->{"partnumber_$i"});
- $where .= " AND lower(p.partnumber) LIKE '$var'";
- }
- if ($form->{"description_$i"}) {
- $var = $form->like(lc $form->{"description_$i"});
- $where .= " AND lower(p.description) LIKE '$var'";
- }
- if ($form->{"partsgroup_$i"}) {
- $var = $form->like(lc $form->{"partsgroup_$i"});
- $where .= " AND lower(pg.partsgroup) LIKE '$var'";
- }
-
- if ($form->{id}) {
- $where .= " AND NOT p.id = $form->{id}";
- }
-
- if ($partnumber) {
- $where .= " ORDER BY p.partnumber";
- } else {
- $where .= " ORDER BY p.description";
- }
-
- # connect to database
- my $dbh = $form->dbconnect($myconfig);
-
- my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
- p.weight, p.onhand, p.unit,
- pg.partsgroup
- FROM parts p
- LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
- WHERE $where|;
- my $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
- push @{ $form->{item_list} }, $ref;
- }
-
- $sth->finish;
- $dbh->disconnect;
-
- $main::lxdebug->leave_sub();
-}
-
-sub all_parts {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
-
- my $where = '1 = 1';
- my $var;
-
- my $group;
- my $limit;
-
- foreach my $item (qw(partnumber drawing microfiche)) {
- if ($form->{$item}) {
- $var = $form->like(lc $form->{$item});
- $where .= " AND lower(p.$item) LIKE '$var'";
- }
- }
-
- # special case for description
- if ($form->{description}) {
- unless ( $form->{bought}
- || $form->{sold}
- || $form->{onorder}
- || $form->{ordered}
- || $form->{rfq}
- || $form->{quoted}) {
- $var = $form->like(lc $form->{description});
- $where .= " AND lower(p.description) LIKE '$var'";
- }
- }
-
- # special case for serialnumber
- if ($form->{l_serialnumber}) {
- if ($form->{serialnumber}) {
- $var = $form->like(lc $form->{serialnumber});
- $where .= " AND lower(serialnumber) LIKE '$var'";
- }
- }
-
- if ($form->{searchitems} eq 'part') {
- $where .= " AND p.inventory_accno_id > 0";
- }
- if ($form->{searchitems} eq 'assembly') {
- $form->{bought} = "";
- $where .= " AND p.assembly = '1'";
- }
- if ($form->{searchitems} eq 'service') {
- $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
-
- # irrelevant for services
- $form->{make} = $form->{model} = "";
- }
-
- # items which were never bought, sold or on an order
- if ($form->{itemstatus} eq 'orphaned') {
- $form->{onhand} = $form->{short} = 0;
- $form->{bought} = $form->{sold} = 0;
- $form->{onorder} = $form->{ordered} = 0;
- $form->{rfq} = $form->{quoted} = 0;
-
- $form->{transdatefrom} = $form->{transdateto} = "";
-
- $where .= " AND p.onhand = 0
- AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
- WHERE p.id = i.parts_id)
- AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
- WHERE p.id = a.parts_id)
- AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
- WHERE p.id = o.parts_id)";
- }
-
- if ($form->{itemstatus} eq 'active') {
- $where .= " AND p.obsolete = '0'";
- }
- if ($form->{itemstatus} eq 'obsolete') {
- $where .= " AND p.obsolete = '1'";
- $form->{onhand} = $form->{short} = 0;
- }
- if ($form->{itemstatus} eq 'onhand') {
- $where .= " AND p.onhand > 0";
- }
- if ($form->{itemstatus} eq 'short') {
- $where .= " AND p.onhand < p.rop";
- }
- if ($form->{make}) {
- $var = $form->like(lc $form->{make});
- $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
- FROM makemodel m WHERE lower(m.make) LIKE '$var')";
- }
- if ($form->{model}) {
- $var = $form->like(lc $form->{model});
- $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
- FROM makemodel m WHERE lower(m.model) LIKE '$var')";
- }
- if ($form->{partsgroup}) {
- $var = $form->like(lc $form->{partsgroup});
- $where .= " AND lower(pg.partsgroup) LIKE '$var'";
- }
- if ($form->{l_soldtotal}) {
- $where .= " AND p.id=i.parts_id AND i.qty >= 0";
- $group =
- " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
- }
- if ($form->{top100}) {
- $limit = " LIMIT 100";
- }
-
- # tables revers?
- if ($form->{revers} == 1) {
- $form->{desc} = " DESC";
- } else {
- $form->{desc} = "";
- }
-
- # connect to database
- my $dbh = $form->dbconnect($myconfig);
-
- my $sortorder = $form->{sort};
- $sortorder .= $form->{desc};
- $sortorder = $form->{sort} if $form->{sort};
-
- my $query = "";
-
- if ($form->{l_soldtotal}) {
- $form->{soldtotal} = 'soldtotal';
- $query =
- qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
- p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
- p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
- WHERE $where
- $group
- ORDER BY $sortorder
- $limit|;
- } else {
- $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
- p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
- p.priceupdate, p.image, p.drawing, p.microfiche,
- pg.partsgroup
- FROM parts p
- LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
- WHERE $where
- $group
- ORDER BY $sortorder|;
- }
-
- # rebuild query for bought and sold items
- if ( $form->{bought}
- || $form->{sold}
- || $form->{onorder}
- || $form->{ordered}
- || $form->{rfq}
- || $form->{quoted}) {
-
- my @a = qw(partnumber description bin priceupdate name);
-
- push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
- push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
- push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
-
- my $union = "";
- $query = "";
-
- if ($form->{bought} || $form->{sold}) {
-
- my $invwhere = "$where";
- $invwhere .= " AND i.assemblyitem = '0'";
- $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
- if $form->{transdatefrom};
- $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
- if $form->{transdateto};
-
- if ($form->{description}) {
- $var = $form->like(lc $form->{description});
- $invwhere .= " AND lower(i.description) LIKE '$var'";
- }
-
- my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
- i.qty AS onhand, i.unit, p.bin, i.sellprice,
- p.listprice, p.lastcost, p.rop, p.weight,
- p.priceupdate, p.image, p.drawing, p.microfiche,
- pg.partsgroup,
- a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
- ct.name, i.deliverydate|;
-
- if ($form->{bought}) {
- $query = qq|
- SELECT $flds, 'ir' AS module, '' AS type,
- 1 AS exchangerate
- FROM invoice i
- JOIN parts p ON (p.id = i.parts_id)
- JOIN ap a ON (a.id = i.trans_id)
- JOIN vendor ct ON (a.vendor_id = ct.id)
- LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
- WHERE $invwhere|;
- $union = "
- UNION";
- }
-
- if ($form->{sold}) {
- $query .= qq|$union
- SELECT $flds, 'is' AS module, '' AS type,
- 1 As exchangerate
- FROM invoice i
- JOIN parts p ON (p.id = i.parts_id)
- JOIN ar a ON (a.id = i.trans_id)
- JOIN customer ct ON (a.customer_id = ct.id)
- LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
- WHERE $invwhere|;
- $union = "
- UNION";
- }
- }
-
- if ($form->{onorder} || $form->{ordered}) {
- my $ordwhere = "$where
- AND o.quotation = '0'";
- $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
- if $form->{transdatefrom};
- $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
- if $form->{transdateto};
-
- if ($form->{description}) {
- $var = $form->like(lc $form->{description});
- $ordwhere .= " AND lower(oi.description) LIKE '$var'";
- }
-
- $flds =
- qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
- oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
- p.listprice, p.lastcost, p.rop, p.weight,
- p.priceupdate, p.image, p.drawing, p.microfiche,
- pg.partsgroup,
- '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
- ct.name|;
-
- if ($form->{ordered}) {
- $query .= qq|$union
- SELECT $flds, 'oe' AS module, 'sales_order' AS type,
- (SELECT buy FROM exchangerate ex
- WHERE ex.curr = o.curr
- AND ex.transdate = o.transdate) AS exchangerate
- FROM orderitems oi
- JOIN parts p ON (oi.parts_id = p.id)
- JOIN oe o ON (oi.trans_id = o.id)
- JOIN customer ct ON (o.customer_id = ct.id)
- LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
- WHERE $ordwhere
- AND o.customer_id > 0|;
- $union = "
- UNION";
- }
-
- if ($form->{onorder}) {
- $flds =
- qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
- oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
- p.listprice, p.lastcost, p.rop, p.weight,
- p.priceupdate, p.image, p.drawing, p.microfiche,
- pg.partsgroup,
- '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
- ct.name|;
-
- $query .= qq|$union
- SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
- (SELECT sell FROM exchangerate ex
- WHERE ex.curr = o.curr
- AND ex.transdate = o.transdate) AS exchangerate
- FROM orderitems oi
- JOIN parts p ON (oi.parts_id = p.id)
- JOIN oe o ON (oi.trans_id = o.id)
- JOIN vendor ct ON (o.vendor_id = ct.id)
- LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
- WHERE $ordwhere
- AND o.vendor_id > 0|;
- }