- $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
-
- if ($form->{"qty_$i"} != 0) {
-
- map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(partnumber description unit);
-
- @taxaccounts = split / /, $form->{"taxaccounts_$i"};
- $taxdiff = 0;
- $allocated = 0;
- $taxrate = 0;
-
- $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
- my $fxsellprice = $form->{"sellprice_$i"};
-
- my ($dec) = ($fxsellprice =~ /\.(\d+)/);
- $dec = length $dec;
- my $decimalplaces = ($dec > 2) ? $dec : 2;
-
-
- map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
-
- if ($form->{"inventory_accno_$i"}) {
-
- $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
-
- if ($form->{taxincluded}) {
- $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
- $form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
- } else {
- $taxamount = $linetotal * $taxrate;
- }
-
- $netamount += $linetotal;
-
- if ($form->round_amount($taxrate, 7) == 0) {
- if ($form->{taxincluded}) {
- foreach $item (@taxaccounts) {
- $taxamount = $form->round_amount($linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})), 2);
- $taxdiff += $taxamount;
- $form->{amount}{$form->{id}}{$item} -= $taxamount;
- }
- $form->{amount}{$form->{id}}{$taxaccounts[0]} += $taxdiff;
- } else {
- map { $form->{amount}{$form->{id}}{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
- }
- } else {
- map { $form->{amount}{$form->{id}}{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
- }
-
-
- # add purchase to inventory, this one is without the tax!
- $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
- $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) * $form->{exchangerate};
- $linetotal = $form->round_amount($linetotal, 2);
-
- # this is the difference for the inventory
- $invoicediff += ($amount - $linetotal);
-
- $form->{amount}{$form->{id}}{$form->{"inventory_accno_$i"}} -= $linetotal;
-
- # adjust and round sellprice
- $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
-
-
- # update parts table
- $query = qq|UPDATE parts SET
- lastcost = $form->{"sellprice_$i"}
- WHERE id = $form->{"id_$i"}|;
-
- $dbh->do($query) || $form->dberror($query);
-
- $form->update_balance($dbh,
- "parts",
- "onhand",
- qq|id = $form->{"id_$i"}|,
- $form->{"qty_$i"}) unless $form->{shipped};
-
-
- # check if we sold the item already and
- # make an entry for the expense and inventory
- $query = qq|SELECT i.id, i.qty, i.allocated, i.trans_id,
- p.inventory_accno_id, p.expense_accno_id, a.transdate
- FROM invoice i, ar a, parts p
- WHERE i.parts_id = p.id
- AND i.parts_id = $form->{"id_$i"}
- AND (i.qty + i.allocated) > 0
- AND i.trans_id = a.id
- ORDER BY transdate|;
- $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
-
- my $totalqty = $form->{"qty_$i"};
-
- while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-
- my $qty = $ref->{qty} + $ref->{allocated};
-
- if (($qty - $totalqty) > 0) {
- $qty = $totalqty;
- }
-
-
- $linetotal = $form->round_amount($form->{"sellprice_$i"} * $qty, 2);
-
- if ($ref->{allocated} < 0) {
- # we have an entry for it already, adjust amount
- $form->update_balance($dbh,
- "acc_trans",
- "amount",
- qq|trans_id = $ref->{trans_id} AND chart_id = $ref->{inventory_accno_id} AND transdate = '$ref->{transdate}'|,
- $linetotal);
-
- $form->update_balance($dbh,
- "acc_trans",
- "amount",
- qq|trans_id = $ref->{trans_id} AND chart_id = $ref->{expense_accno_id} AND transdate = '$ref->{transdate}'|,
- $linetotal * -1);
-
- } else {
- # add entry for inventory, this one is for the sold item
- if ($linetotal != 0) {
- $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
- transdate)
- VALUES ($ref->{trans_id}, $ref->{inventory_accno_id},
- $linetotal, '$ref->{transdate}')|;
- $dbh->do($query) || $form->dberror($query);
-
- # add expense
- $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
- transdate, taxkey)
- VALUES ($ref->{trans_id}, $ref->{expense_accno_id},
- |. ($linetotal * -1) .qq|, '$ref->{transdate}',
- (SELECT taxkey from tax WHERE chart_id = $ref->{expense_accno_id}))|;
- $dbh->do($query) || $form->dberror($query);
- }
- }
-
- # update allocated for sold item
- $form->update_balance($dbh,
- "invoice",
- "allocated",
- qq|id = $ref->{id}|,
- $qty * -1);
-
- $allocated += $qty;
-
- last if (($totalqty -= $qty) <= 0);
- }
-
- $sth->finish;
-
- $lastinventoryaccno = $form->{"inventory_accno_$i"};
-
+ next unless $form->{"id_$i"};
+
+ $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
+ $form->{"qty_$i"} *= -1 if $form->{storno};
+
+ $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"} if $main::eur;
+
+ # get item baseunit
+ if (!$item_units{$form->{"id_$i"}}) {
+ do_statement($form, $h_item_unit, $q_item_unit, $form->{"id_$i"});
+ ($item_units{$form->{"id_$i"}}) = $h_item_unit->fetchrow_array();
+ }
+
+ my $item_unit = $item_units{$form->{"id_$i"}};
+
+ if (defined($all_units->{$item_unit}->{factor})
+ && ($all_units->{$item_unit}->{factor} ne '')
+ && ($all_units->{$item_unit}->{factor} * 1 != 0)) {
+ $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
+ } else {
+ $basefactor = 1;
+ }
+ $baseqty = $form->{"qty_$i"} * $basefactor;
+
+ @taxaccounts = split / /, $form->{"taxaccounts_$i"};
+ $taxdiff = 0;
+ $allocated = 0;
+ $taxrate = 0;
+
+ $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
+ (my $fxsellprice = $form->{"sellprice_$i"}) =~ /\.(\d+)/;
+ my $dec = length $1;
+ my $decimalplaces = ($dec > 2) ? $dec : 2;
+
+ map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
+
+ $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
+ #####################################################################
+ # das ist aus IS.pm kopiert. schlimm. jb 7.10.2009
+ # ich würde mir wünschen, dass diese vier stellen zusammengefasst werden
+ # ... vier stellen = (einkauf + verkauf) * (maske + backend)
+ # ansonsten stolpert man immer wieder viermal statt einmal heftig
+ # und auch das undo discount formatting ist nicht besonders wartungsfreundlich
+
+ # keep entered selling price
+ my $fxsellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
+
+ # keine ahnung wofür das in IS.pm gemacht wird:
+ # my ($dec) = ($fxsellprice =~ /\.(\d+)/);
+ # $dec = length $dec;
+ # my $decimalplaces = ($dec > 2) ? $dec : 2;
+
+ # undo discount formatting
+ $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
+ # deduct discount
+ $form->{"sellprice_$i"} = $fxsellprice * (1 - $form->{"discount_$i"});
+
+ ######################################################################
+ if ($form->{"inventory_accno_$i"}) {
+
+ $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
+
+ if ($form->{taxincluded}) {
+ $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
+ $form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));