-sub save {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
- my @values;
- # 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
-
- # undo amount formatting
- map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
- qw(rop weight listprice sellprice gv lastcost stock);
-
- my $makemodel = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
-
- $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
-
- my ($query, $sth);
-
- if ($form->{id}) {
-
- # get old price
- $query = qq|SELECT sellprice, weight FROM parts WHERE id = ?|;
- my ($sellprice, $weight) = selectrow_query($form, $dbh, $query, conv_i($form->{id}));
-
- # if item is part of an assembly adjust all assemblies
- $query = qq|SELECT id, qty FROM assembly WHERE parts_id = ?|;
- $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
- 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
- do_query($form, $dbh, qq|DELETE FROM makemodel WHERE parts_id = ?|, conv_i($form->{id}));
- }
-
- if ($form->{item} eq 'assembly') {
- if ($form->{onhand} != 0) {
- &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
- }
-
- # delete assembly records
- do_query($form, $dbh, qq|DELETE FROM assembly WHERE id = ?|, conv_i($form->{id}));
-
- $form->{onhand} += $form->{stock};
- }
-
- # delete tax records
- do_query($form, $dbh, qq|DELETE FROM partstax WHERE parts_id = ?|, conv_i($form->{id}));
-
- # delete translations
- do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
-
- } else {
- my ($count) = selectrow_array($form, $dbh, qq|SELECT COUNT(*) FROM parts WHERE partnumber = ?|, $form->{partnumber});
- if ($count) {
- $main::lxdebug->leave_sub();
- return 3;
- }
-
- ($form->{id}) = selectrow_array($form, $dbh, qq|SELECT nextval('id')|);
- do_query($form, $dbh, qq|INSERT INTO parts (id, partnumber) VALUES (?, '')|, $form->{id});
-
- $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
- FROM buchungsgruppen bg
- WHERE bg.id = | . conv_i($form->{"buchungsgruppen_id"}, 'NULL') . qq|)|;
- } else {
- $subq_inventory = "NULL";
- }
-
- if ($form->{"item"} ne "assembly") {
- $subq_expense =
- qq|(SELECT bg.expense_accno_id_0
- FROM buchungsgruppen bg
- WHERE bg.id = | . conv_i($form->{"buchungsgruppen_id"}, 'NULL') . qq|)|;
- } else {
- $subq_expense = "NULL";
- }
-
- $query =
- qq|UPDATE parts SET
- partnumber = ?,
- description = ?,
- makemodel = ?,
- alternate = 'f',
- assembly = ?,
- listprice = ?,
- sellprice = ?,
- lastcost = ?,
- weight = ?,
- priceupdate = ?,
- unit = ?,
- notes = ?,
- formel = ?,
- rop = ?,
- bin = ?,
- buchungsgruppen_id = ?,
- payment_id = ?,
- inventory_accno_id = $subq_inventory,
- income_accno_id = (SELECT bg.income_accno_id_0 FROM buchungsgruppen bg WHERE bg.id = ?),
- expense_accno_id = $subq_expense,
- obsolete = ?,
- image = ?,
- drawing = ?,
- shop = ?,
- ve = ?,
- gv = ?,
- ean = ?,
- not_discountable = ?,
- microfiche = ?,
- partsgroup_id = ?
- WHERE id = ?|;
- @values = ($form->{partnumber},
- $form->{description},
- $makemodel ? 't' : 'f',
- $form->{assembly} ? 't' : 'f',
- $form->{listprice},
- $form->{sellprice},
- $form->{lastcost},
- $form->{weight},
- conv_date($form->{priceupdate}),
- $form->{unit},
- $form->{notes},
- $form->{formel},
- $form->{rop},
- $form->{bin},
- conv_i($form->{buchungsgruppen_id}),
- conv_i($form->{payment_id}),
- conv_i($form->{buchungsgruppen_id}),
- $form->{obsolete} ? 't' : 'f',
- $form->{image},
- $form->{drawing},
- $form->{shop} ? 't' : 'f',
- conv_i($form->{ve}),
- conv_i($form->{gv}),
- $form->{ean},
- $form->{not_discountable} ? 't' : 'f',
- $form->{microfiche},
- conv_i($partsgroup_id),
- conv_i($form->{id})
- );
- do_query($form, $dbh, $query, @values);
-
- # delete translation records
- do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
-
- if ($form->{language_values} ne "") {
- foreach $item (split(/---\+\+\+---/, $form->{language_values})) {
- my ($language_id, $translation, $longdescription) = split(/--\+\+--/, $item);
- if ($translation ne "") {
- $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription)
- VALUES ( ?, ?, ?, ? )|;
- @values = (conv_i($form->{id}), conv_i($language_id), $translation, $longdescription);
- do_query($form, $dbh, $query, @values);
- }
- }
- }
-
- # delete price records
- do_query($form, $dbh, qq|DELETE FROM prices WHERE parts_id = ?|, conv_i($form->{id}));
-
- # 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) | .
- qq|VALUES(?, ?, ?)|;
- @values = (conv_i($form->{id}), conv_i($pricegroup_id), $price);
- do_query($form, $dbh, $query, @values);
- }
- }
-
- # 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) | .
- qq|VALUES (?, ?, ?)|;
- @values = (conv_i($form->{id}), $form->{"make_$i"}, $form->{"model_$i"});
- do_query($form, $dbh, $query, @values);
- }
- }
- }
-
- # insert taxes
- foreach $item (split(/ /, $form->{taxaccounts})) {
- if ($form->{"IC_tax_$item"}) {
- $query =
- qq|INSERT INTO partstax (parts_id, chart_id)
- VALUES (?, (SELECT id FROM chart WHERE accno = ?))|;
- @values = (conv_i($form->{id}), $item);
- do_query($form, $dbh, $query, @values);
- }
- }
-
- # 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) | .
- qq|VALUES (?, ?, ?, ?)|;
- @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}), conv_i($form->{"qty_$i"}), $form->{"bom_$i"} ? 't' : 'f');
- do_query($form, $dbh, $query, @values);
- }
- }
-
- # 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, ?, ?, '$shippingdate', ?)|;
- @values = (conv_i($form->{id}), $form->{stock}, conv_i($form->{employee_id}));
- do_query($form, $dbh, $query, @values);
-
- }
-
- #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 = ?))
- ORDER BY c.accno|;
- $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
-
- $form->{taxaccount} = "";
- while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
- $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 id, qty FROM assembly WHERE parts_id = ?|;
- my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
-
- 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 + ?, weight = weight + ?
- WHERE id = ?|;
- @values = ($qty * ($form->{sellprice} - $sellprice),
- $qty * ($form->{weight} - $weight), conv_i($id));
- do_query($form, $dbh, $query, @values);
-
- $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 = qq|NOT p.obsolete|;
- my @values;
-
- if ($form->{partnumber}) {
- $where .= qq| AND (p.partnumber ILIKE ?)|;
- push(@values, '%' . $form->{partnumber} . '%');
- }
-
- if ($form->{description}) {
- $where .= qq| AND (p.description ILIKE ?)|;
- push(@values, '%' . $form->{description} . '%');
- }
-
- # 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 NOT p.obsolete AND p.assembly $where|;
-
- $form->{assembly_items} = selectall_hashref_query($form, $dbh, $query, @values);
-
- $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 = ?)|;
- my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
-
- 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);
-
- $main::lxdebug->leave_sub();
-
- return $rc;
-}
-
-sub delete {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
- my @values = (conv_i($form->{id}));
- # connect to database, turn off AutoCommit
- my $dbh = $form->dbconnect_noauto($myconfig);
-
- my %columns = ( "assembly" => "id", "alternate" => "id", "parts" => "id" );
-
- for my $table (qw(prices partstax makemodel inventory assembly parts)) {
- my $column = defined($columns{$table}) ? $columns{$table} : "parts_id";
- do_query($form, $dbh, qq|DELETE FROM $table WHERE $column = ?|, @values);
- }
-
- # commit
- my $rc = $dbh->commit;
- $dbh->disconnect;
-
- $main::lxdebug->leave_sub();
-
- return $rc;
-}