X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FIR.pm;h=909a3b07dcb8ccd6867d9e5f255f6a5c50752393;hb=5074cc502811728e2bbaf42cfd5ea39974ba6e2a;hp=83921779abe595da25d65f06a8fa04c208b2e834;hpb=b3caa17585a69e0c5092483f709dd1227b9acaaf;p=kivitendo-erp.git diff --git a/SL/IR.pm b/SL/IR.pm index 83921779a..909a3b07d 100644 --- a/SL/IR.pm +++ b/SL/IR.pm @@ -103,7 +103,11 @@ sub post_invoice { $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 $::lx_office_conf{system}->{eur}; + if ( $::instance_conf->get_inventory_system eq 'periodic') { + # inventory account number is overwritten with expense account number, so + # never book incoming to inventory account but always to expense account + $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"} + }; # get item baseunit if (!$item_units{$form->{"id_$i"}}) { @@ -158,8 +162,10 @@ sub post_invoice { $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)); + } else { $taxamount = $linetotal * $taxrate; } @@ -209,7 +215,7 @@ sub post_invoice { # 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, + qq|SELECT i.id, i.qty, i.allocated, i.trans_id, i.base_qty, p.inventory_accno_id, p.expense_accno_id, a.transdate FROM invoice i, ar a, parts p WHERE (i.parts_id = p.id) @@ -217,6 +223,20 @@ sub post_invoice { AND ((i.base_qty + i.allocated) > 0) AND (i.trans_id = a.id) 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; @@ -225,32 +245,38 @@ sub post_invoice { my $qty = min $totalqty, ($ref->{base_qty} + $ref->{allocated}); $linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 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); - - } elsif ($linetotal != 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) VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?))|; - @values = ($ref->{trans_id}, $ref->{inventory_accno_id}, $linetotal, $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) VALUES (?, ?, ?, ?, (SELECT taxkey from tax WHERE chart_id = ?))|; - @values = ($ref->{trans_id}, $ref->{expense_accno_id}, ($linetotal * -1), $ref->{transdate}, $ref->{expense_accno_id}); - do_query($form, $dbh, $query, @values); - } + 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) VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?))|; + @values = ($ref->{trans_id}, $ref->{inventory_accno_id}, $linetotal, $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) VALUES (?, ?, ?, ?, (SELECT taxkey from tax WHERE chart_id = ?))|; + @values = ($ref->{trans_id}, $ref->{expense_accno_id}, ($linetotal * -1), $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); @@ -263,6 +289,8 @@ sub post_invoice { $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 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2); @@ -380,11 +408,41 @@ sub post_invoice { $invoicediff += $paiddiff; $expensediff += $paiddiff; - ######## this only applies to tax included +######## this only applies to tax included + + # in the sales invoice case rounding errors only have to be corrected for + # income accounts, it is enough to add the total rounding error to one of + # the income accounts, with the one assigned to the last row being used + # (lastinventoryaccno) + + # in the purchase invoice case rounding errors may be split between + # inventory accounts and expense accounts. After rounding, an error of 1 + # cent is introduced if the total rounding error exceeds 0.005. The total + # error is made up of $invoicediff and $expensediff, however, so if both + # values are below 0.005, but add up to a total >= 0.005, correcting + # lastinventoryaccno and lastexpenseaccno separately has no effect after + # rounding. This caused bug 1579. Therefore when the combined total exceeds + # 0.005, but neither do individually, the account with the larger value + # shall receive the total rounding error, and the next time it is rounded + # the 1 cent correction will be introduced. $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff if $lastinventoryaccno; $form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff if $lastexpenseaccno; + if ( (abs($expensediff)+abs($invoicediff)) >= 0.005 and abs($expensediff) < 0.005 and abs($invoicediff) < 0.005 ) { + + # in total the rounding error adds up to 1 cent effectively, correct the + # larger of the two numbers + + if ( abs($form->{amount}{ $form->{id} }{$lastinventoryaccno}) > abs($form->{amount}{ $form->{id} }{$lastexpenseaccno}) ) { + # $invoicediff has already been deducted, now also deduct expensediff + $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $expensediff; + } else { + # expensediff has already been deducted, now also deduct invoicediff + $form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $invoicediff; + }; + }; + } else { $amount = $form->round_amount($netamount * $form->{exchangerate}, 2); $paiddiff = $amount - $netamount * $form->{exchangerate}; @@ -405,21 +463,22 @@ sub post_invoice { $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0; - # update exchangerate +# update exchangerate $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0, $form->{exchangerate}) if ($form->{currency} ne $defaultcurrency) && !$exchangerate; - # record acc_trans transactions +# record acc_trans transactions foreach my $trans_id (keys %{ $form->{amount} }) { foreach my $accno (keys %{ $form->{amount}{$trans_id} }) { $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2); + next if $payments_only || !$form->{amount}{$trans_id}{$accno}; $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id) VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, - (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|; + (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|; @values = ($trans_id, $accno, $form->{amount}{$trans_id}{$accno}, conv_date($form->{invdate}), $accno, $project_id); do_query($form, $dbh, $query, @values); @@ -1034,6 +1093,12 @@ sub retrieve_item { push @values, $form->{"partnumber_$i"}; } + # Search for part ID overrides all other criteria. + if ($form->{"id_${i}"}) { + $where = qq|p.id = ?|; + @values = ($form->{"id_${i}"}); + } + if ($form->{"description_$i"}) { $where .= " ORDER BY p.description"; } else { @@ -1091,6 +1156,19 @@ sub retrieve_item { WHERE $where|; my $sth = prepare_execute_query($form, $dbh, $query, @values); + my @translation_queries = ( [ qq|SELECT tr.translation, tr.longdescription + FROM translation tr + WHERE tr.language_id = ? AND tr.parts_id = ?| ], + [ qq|SELECT tr.translation, tr.longdescription + FROM translation tr + WHERE tr.language_id IN + (SELECT id + FROM language + WHERE article_code = (SELECT article_code FROM language WHERE id = ?)) + AND tr.parts_id = ? + LIMIT 1| ] ); + map { push @{ $_ }, prepare_query($form, $dbh, $_->[0]) } @translation_queries; + $form->{item_list} = []; while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { @@ -1140,6 +1218,16 @@ sub retrieve_item { $form->{taxaccounts} .= "$ptr->{accno} "; } + if ($form->{language_id}) { + for my $spec (@translation_queries) { + do_statement($form, $spec->[1], $spec->[0], conv_i($form->{language_id}), conv_i($ref->{id})); + my ($translation, $longdescription) = $spec->[1]->fetchrow_array; + next unless $translation; + $ref->{description} = $translation; + $ref->{longdescription} = $longdescription; + last; + } + } } $stw->finish(); @@ -1152,6 +1240,7 @@ sub retrieve_item { } $sth->finish(); + $_->[1]->finish for @translation_queries; foreach my $item (@{ $form->{item_list} }) { my $custom_variables = CVar->get_custom_variables(module => 'IC',