- $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 = $linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"}));
- $totaltax += $taxamount;
- $form->{amount}{$form->{id}}{$item} -= $taxamount;
- }
- } else {
- map { $form->{amount}{$form->{id}}{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
- }
- } else {
- map { $form->{amount}{$form->{id}}{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
- }
-
-
- $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 expense
- $expensediff += ($amount - $linetotal);
-
- # add amount to expense
- $form->{amount}{$form->{id}}{$form->{"expense_accno_$i"}} -= $linetotal;
-
- $lastexpenseaccno = $form->{"expense_accno_$i"};
-
- # adjust and round sellprice
- $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
-
- # update lastcost
- $query = qq|UPDATE parts SET
- lastcost = $form->{"sellprice_$i"}
- WHERE id = $form->{"id_$i"}|;
-
- $dbh->do($query) || $form->dberror($query);
+ 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} / $price_factor;
+ $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 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);
+
+ $lastinventoryaccno = $form->{"inventory_accno_$i"};
+
+ next if $payments_only;
+
+ # update parts table by setting lastcost to current price, don't allow negative values by using abs
+ $query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
+ @values = (abs($fxsellprice * $form->{exchangerate} / $basefactor), conv_i($form->{"id_$i"}));
+ do_query($form, $dbh, $query, @values);
+
+ # check if we sold the item already and
+ # make an entry for the expense and inventory
+ my $taxzone = $form->{taxzone_id} * 1;
+ $query =
+ qq|SELECT i.id, i.qty, i.allocated, i.trans_id, i.base_qty,
+ bg.inventory_accno_id, tc.expense_accno_id AS expense_accno_id, a.transdate
+ FROM invoice i, ar a, parts p, buchungsgruppen bg, taxzone_charts tc
+ WHERE (i.parts_id = p.id)
+ AND (i.parts_id = ?)
+ AND ((i.base_qty + i.allocated) > 0)
+ AND (i.trans_id = a.id)
+ AND (p.buchungsgruppen_id = bg.id)
+ AND (tc.buchungsgruppen_id = p.buchungsgruppen_id)
+ AND (tc.taxzone_id = ${taxzone})
+ ORDER BY transdate|;
+ # ORDER BY transdate guarantees FIFO
+
+ # sold two items without having bought them yet, example result of query:
+ # id | qty | allocated | trans_id | inventory_accno_id | expense_accno_id | transdate
+ # ---+-----+-----------+----------+--------------------+------------------+------------
+ # 9 | 2 | 0 | 9 | 15 | 151 | 2011-01-05
+
+ # base_qty + allocated > 0 if article has already been sold but not bought yet
+
+ # select qty,allocated,base_qty,sellprice from invoice where trans_id = 9;
+ # qty | allocated | base_qty | sellprice
+ # -----+-----------+----------+------------
+ # 2 | 0 | 2 | 1000.00000
+
+ $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
+
+ my $totalqty = $baseqty;
+
+ while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
+ my $qty = min $totalqty, ($ref->{base_qty} + $ref->{allocated});
+ $linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 2);
+
+ if ( $::instance_conf->get_inventory_system eq 'perpetual' ) {
+ # Warenbestandsbuchungen nur bei Bestandsmethode
+
+ 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);
+
+ } elsif ($linetotal != 0) {
+
+ # allocated >= 0
+ # add entry for inventory, this one is for the sold item
+ $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) VALUES (?, ?, ?, ?,
+ (SELECT taxkey_id
+ FROM taxkeys
+ WHERE chart_id= ?
+ AND startdate <= ?
+ ORDER BY startdate DESC LIMIT 1),
+ (SELECT tax_id
+ FROM taxkeys
+ WHERE chart_id= ?
+ AND startdate <= ?
+ ORDER BY startdate DESC LIMIT 1),
+ (SELECT link FROM chart WHERE id = ?))|;
+ @values = ($ref->{trans_id}, $ref->{inventory_accno_id}, $linetotal, $ref->{transdate}, $ref->{inventory_accno_id}, $ref->{transdate}, $ref->{inventory_accno_id}, $ref->{transdate},
+ $ref->{inventory_accno_id});
+ do_query($form, $dbh, $query, @values);
+
+ # add expense
+ $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) VALUES (?, ?, ?, ?,
+ (SELECT taxkey_id
+ FROM taxkeys
+ WHERE chart_id= ?
+ AND startdate <= ?
+ ORDER BY startdate DESC LIMIT 1),
+ (SELECT tax_id
+ FROM taxkeys
+ WHERE chart_id= ?
+ AND startdate <= ?
+ ORDER BY startdate DESC LIMIT 1),
+ (SELECT link FROM chart WHERE id = ?))|;
+ @values = ($ref->{trans_id}, $ref->{expense_accno_id}, ($linetotal * -1), $ref->{transdate}, $ref->{expense_accno_id}, $ref->{transdate}, $ref->{expense_accno_id}, $ref->{transdate},
+ $ref->{expense_accno_id});
+ do_query($form, $dbh, $query, @values);
+ }
+ };
+
+ # 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();
+
+ } else { # if ($form->{"inventory_accno_id_$i"})
+ # part doesn't have an inventory_accno_id
+ # lastcost of the part is updated at the end