- $query = qq|SELECT id, description
- FROM buchungsgruppen
- ORDER BY sortkey|;
- $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- $form->{BUCHUNGSGRUPPEN} = [];
- while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
- push(@{ $form->{BUCHUNGSGRUPPEN} }, $ref);
- }
- $sth->finish;
-
- $main::lxdebug->leave_sub();
-}
-
-sub save {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
-
- # connect to database, turn off AutoCommit
- my $dbh = $form->dbconnect_noauto($myconfig);
-
- # save the part
- # make up a unique handle and store in partnumber field
- # then retrieve the record based on the unique handle to get the id
- # replace the partnumber field with the actual variable
- # add records for makemodel
-
- # if there is a $form->{id} then replace the old entry
- # delete all makemodel entries and add the new ones
-
- # escape '
- map { $form->{$_} =~ s/\'/\'\'/g } qw(partnumber description notes unit);
-
- # undo amount formatting
- map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
- qw(rop weight listprice sellprice gv lastcost stock);
-
- # set date to NULL if nothing entered
- $form->{priceupdate} =
- ($form->{priceupdate}) ? qq|'$form->{priceupdate}'| : "NULL";
-
- $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
-
- $form->{alternate} = 0;
- $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
- $form->{obsolete} *= 1;
- $form->{shop} *= 1;
- $form->{onhand} *= 1;
- $form->{ve} *= 1;
- $form->{ge} *= 1;
- $form->{buchungsgruppen_id} *= 1;
- $form->{not_discountable} *= 1;
- $form->{payment_id} *= 1;
-
- my ($query, $sth);
-
- if ($form->{id}) {
-
- # get old price
- $query = qq|SELECT p.sellprice, p.weight
- FROM parts p
- WHERE p.id = $form->{id}|;
- $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
- my ($sellprice, $weight) = $sth->fetchrow_array;
- $sth->finish;
-
- # if item is part of an assembly adjust all assemblies
- $query = qq|SELECT a.id, a.qty
- FROM assembly a
- WHERE a.parts_id = $form->{id}|;
- $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
- while (my ($id, $qty) = $sth->fetchrow_array) {
- &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
- }
- $sth->finish;
-
- if ($form->{item} ne 'service') {
-
- # delete makemodel records
- $query = qq|DELETE FROM makemodel
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
- }
-
- if ($form->{item} eq 'assembly') {
- if ($form->{onhand} != 0) {
- &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
- }
-
- # delete assembly records
- $query = qq|DELETE FROM assembly
- WHERE id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- $form->{onhand} += $form->{stock};
- }
-
- # delete tax records
- $query = qq|DELETE FROM partstax
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- # delete translations
- $query = qq|DELETE FROM translation
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- } else {
- my $uid = rand() . time;
- $uid .= $form->{login};
-
- $query = qq|SELECT p.id FROM parts p
- WHERE p.partnumber = '$form->{partnumber}'|;
- $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
- ($form->{id}) = $sth->fetchrow_array;
- $sth->finish;
-
- if ($form->{id} ne "") {
- $main::lxdebug->leave_sub();
- return 3;
- }
- $query = qq|INSERT INTO parts (partnumber, description)
- VALUES ('$uid', 'dummy')|;
- $dbh->do($query) || $form->dberror($query);
-
- $query = qq|SELECT p.id FROM parts p
- WHERE p.partnumber = '$uid'|;
- $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- ($form->{id}) = $sth->fetchrow_array;
- $sth->finish;
-
- $form->{orphaned} = 1;
- $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
- if ($form->{partnumber} eq "" && $form->{"item"} eq "service") {
- $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
- }
- if ($form->{partnumber} eq "" && $form->{"item"} ne "service") {
- $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
- }
-
- }
- my $partsgroup_id = 0;
-
- if ($form->{partsgroup}) {
- ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
- }
-
- my ($subq_inventory, $subq_expense, $subq_income);
- if ($form->{"item"} eq "part") {
- $subq_inventory =
- qq|(SELECT bg.inventory_accno_id | .
- qq| FROM buchungsgruppen bg | .
- qq| WHERE bg.id = | . $dbh->quote($form->{"buchungsgruppen_id"}) . qq|)|;
- } else {
- $subq_inventory = "NULL";
- }
-
- if ($form->{"item"} ne "assembly") {
- $subq_expense =
- qq|(SELECT bg.expense_accno_id_0 | .
- qq| FROM buchungsgruppen bg | .
- qq| WHERE bg.id = | . $dbh->quote($form->{"buchungsgruppen_id"}) . qq|)|;
- } else {
- $subq_expense = "NULL";
- }
-
- $subq_income =
- qq|(SELECT bg.income_accno_id_0 | .
- qq| FROM buchungsgruppen bg | .
- qq| WHERE bg.id = | . $dbh->quote($form->{"buchungsgruppen_id"}) . qq|)|;
-
- $query = qq|UPDATE parts SET
- partnumber = '$form->{partnumber}',
- description = '$form->{description}',
- makemodel = '$form->{makemodel}',
- alternate = '$form->{alternate}',
- assembly = '$form->{assembly}',
- listprice = $form->{listprice},
- sellprice = $form->{sellprice},
- lastcost = $form->{lastcost},
- weight = $form->{weight},
- priceupdate = $form->{priceupdate},
- unit = '$form->{unit}',
- notes = '$form->{notes}',
- formel = '$form->{formel}',
- rop = $form->{rop},
- bin = '$form->{bin}',
- buchungsgruppen_id = '$form->{buchungsgruppen_id}',
- payment_id = '$form->{payment_id}',
- inventory_accno_id = $subq_inventory,
- income_accno_id = $subq_income,
- expense_accno_id = $subq_expense,
- obsolete = '$form->{obsolete}',
- image = '$form->{image}',
- drawing = '$form->{drawing}',
- shop = '$form->{shop}',
- ve = '$form->{ve}',
- gv = '$form->{gv}',
- ean = '$form->{ean}',
- not_discountable = '$form->{not_discountable}',
- microfiche = '$form->{microfiche}',
- partsgroup_id = $partsgroup_id
- WHERE id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- # delete translation records
- $query = qq|DELETE FROM translation
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
-
- if ($form->{language_values} ne "") {
- split /---\+\+\+---/,$form->{language_values};
- foreach $item (@_) {
- my ($language_id, $translation, $longdescription) = split /--\+\+--/, $item;
- if ($translation ne "") {
- $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription) VALUES
- ($form->{id}, $language_id, | . $dbh->quote($translation) . qq|, | . $dbh->quote($longdescription) . qq| )|;
- $dbh->do($query) || $form->dberror($query);
- }
- }
- }
- # delete price records
- $query = qq|DELETE FROM prices
- WHERE parts_id = $form->{id}|;
- $dbh->do($query) || $form->dberror($query);
- # insert price records only if different to sellprice
- for my $i (1 .. $form->{price_rows}) {
- if ($form->{"price_$i"} eq "0") {
- $form->{"price_$i"} = $form->{sellprice};
- }
- if (
- ( $form->{"price_$i"}
- || $form->{"klass_$i"}
- || $form->{"pricegroup_id_$i"})
- and $form->{"price_$i"} != $form->{sellprice}
- ) {
- $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
- $price = $form->parse_amount($myconfig, $form->{"price_$i"});
- $pricegroup_id =
- $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
- $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
- VALUES($form->{id},$pricegroup_id,$price)|;
- $dbh->do($query) || $form->dberror($query);
- }
- }
-
- # insert makemodel records
- unless ($form->{item} eq 'service') {
- for my $i (1 .. $form->{makemodel_rows}) {
- if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
- map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
-
- $query = qq|INSERT INTO makemodel (parts_id, make, model)
- VALUES ($form->{id},
- '$form->{"make_$i"}', '$form->{"model_$i"}')|;
- $dbh->do($query) || $form->dberror($query);
- }
- }
- }
-
- # insert taxes
- foreach $item (split / /, $form->{taxaccounts}) {
- if ($form->{"IC_tax_$item"}) {
- $query = qq|INSERT INTO partstax (parts_id, chart_id)
- VALUES ($form->{id},
- (SELECT c.id
- FROM chart c
- WHERE c.accno = '$item'))|;
- $dbh->do($query) || $form->dberror($query);
- }
- }
-
- # add assembly records
- if ($form->{item} eq 'assembly') {
-
- for my $i (1 .. $form->{assembly_rows}) {
- $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
-
- if ($form->{"qty_$i"} != 0) {
- $form->{"bom_$i"} *= 1;
- $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
- VALUES ($form->{id}, $form->{"id_$i"},
- $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
- $dbh->do($query) || $form->dberror($query);
- }
- }
-
- # adjust onhand for the parts
- if ($form->{onhand} != 0) {
- &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
- }
-
- @a = localtime;
- $a[5] += 1900;
- $a[4]++;
- my $shippingdate = "$a[5]-$a[4]-$a[3]";
-
- $form->get_employee($dbh);
-
- # add inventory record
- $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
- shippingdate, employee_id) VALUES (
- 0, $form->{id}, $form->{stock}, '$shippingdate',
- $form->{employee_id})|;
- $dbh->do($query) || $form->dberror($query);
-
- }
-
- #set expense_accno=inventory_accno if they are different => bilanz
- $vendor_accno =
- ($form->{expense_accno} != $form->{inventory_accno})
- ? $form->{inventory_accno}
- : $form->{expense_accno};
-
- # get tax rates and description
- $accno_id =
- ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
- $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
- FROM chart c, tax t
- WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
- ORDER BY c.accno|;
- $stw = $dbh->prepare($query);
-
- $stw->execute || $form->dberror($query);
-
- $form->{taxaccount} = "";
- while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
-
- # if ($customertax{$ref->{accno}}) {
- $form->{taxaccount} .= "$ptr->{accno} ";
- if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
- $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
- $form->{"$ptr->{accno}_description"} = $ptr->{description};
- $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
- $form->{taxaccount2} .= " $ptr->{accno} ";
- }
-
- }
-
- # commit
- my $rc = $dbh->commit;
- $dbh->disconnect;
-
- $main::lxdebug->leave_sub();
-
- return $rc;
-}
-
-sub update_assembly {
- $main::lxdebug->enter_sub();
-
- my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
-
- my $query = qq|SELECT a.id, a.qty
- FROM assembly a
- WHERE a.parts_id = $id|;
- my $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- while (my ($pid, $aqty) = $sth->fetchrow_array) {
- &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
- }
- $sth->finish;
-
- $query = qq|UPDATE parts
- SET sellprice = sellprice +
- $qty * ($form->{sellprice} - $sellprice),
- weight = weight +
- $qty * ($form->{weight} - $weight)
- WHERE id = $id|;
- $dbh->do($query) || $form->dberror($query);
-
- $main::lxdebug->leave_sub();
-}
-
-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 = ?|, $qty, $id);