X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FIR.pm;h=72a36ae5710732a6f660e2cb13f029f119da68f9;hb=2868feee8fb33457e7562f02778186b5b5c1a2b6;hp=875059bfacf397090bbb1244042671abd08481b2;hpb=d94820d09f4848d06bf3b74bb85c2c52820fbc48;p=kivitendo-erp.git diff --git a/SL/IR.pm b/SL/IR.pm index 875059bfa..72a36ae57 100644 --- a/SL/IR.pm +++ b/SL/IR.pm @@ -35,9 +35,12 @@ package IR; use SL::AM; +use SL::ARAP; use SL::Common; use SL::DBUtils; +use SL::DO; use SL::MoreCommon; +use List::Util qw(min); sub post_invoice { $main::lxdebug->enter_sub(); @@ -46,6 +49,7 @@ sub post_invoice { # connect to database, turn off autocommit my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig); + $form->{defaultcurrency} = $form->get_default_currency($myconfig); my ($query, $sth, @values, $project_id); my ($allocated, $taxrate, $taxamount, $taxdiff, $item); @@ -58,10 +62,8 @@ sub post_invoice { if (!$payments_only) { if ($form->{id}) { &reverse_invoice($dbh, $form); - } else { ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|); - do_query($form, $dbh, qq|INSERT INTO ap (id, invnumber) VALUES (?, '')|, $form->{id}); } } @@ -72,34 +74,27 @@ sub post_invoice { if ($form->{currency} eq $defaultcurrency) { $form->{exchangerate} = 1; } else { - $exchangerate = - $form->check_exchangerate($myconfig, $form->{currency}, - $form->{transdate}, 'sell'); + $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell'); } - $form->{exchangerate} = - ($exchangerate) - ? $exchangerate - : $form->parse_amount($myconfig, $form->{exchangerate}); - + $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate}); $form->{exchangerate} = 1 unless ($form->{exchangerate} * 1); my %item_units; my $q_item_unit = qq|SELECT unit FROM parts WHERE id = ?|; my $h_item_unit = prepare_query($form, $dbh, $q_item_unit); + $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS'); + my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} }; + my $price_factor; + for my $i (1 .. $form->{rowcount}) { next unless $form->{"id_$i"}; - $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}); - - if ($form->{storno}) { - $form->{"qty_$i"} *= -1; - } + $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}); + $form->{"qty_$i"} *= -1 if $form->{storno}; - if ($main::eur) { - $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"}; - } + $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"} if $main::eur; # get item baseunit if (!$item_units{$form->{"id_$i"}}) { @@ -110,8 +105,8 @@ sub post_invoice { 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)) { + && ($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; @@ -124,17 +119,17 @@ sub post_invoice { $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 $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; + if ($form->{"inventory_accno_$i"}) { - $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2); + $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2); if ($form->{taxincluded}) { $taxamount = $linetotal * ($taxrate / (1 + $taxrate)); @@ -164,8 +159,8 @@ sub post_invoice { } # 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}; + $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 @@ -174,8 +169,7 @@ sub post_invoice { $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); + $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces); $lastinventoryaccno = $form->{"inventory_accno_$i"}; @@ -186,10 +180,6 @@ sub post_invoice { @values = ($form->{"sellprice_$i"}, conv_i($form->{"id_$i"})); do_query($form, $dbh, $query, @values); - if (!$form->{shipped}) { - $form->update_balance($dbh, "parts", "onhand", qq|id = ?|, $baseqty, $form->{"id_$i"}) - } - # check if we sold the item already and # make an entry for the expense and inventory $query = @@ -206,13 +196,7 @@ sub post_invoice { my $totalqty = $base_qty; while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - - my $qty = $ref->{base_qty} + $ref->{allocated}; - - if (($qty - $totalqty) > 0) { - $qty = $totalqty; - } - + my $qty = min $totalqty, ($ref->{base_qty} + $ref->{allocated}); $linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 2); if ($ref->{allocated} < 0) { @@ -232,17 +216,13 @@ sub post_invoice { } 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}); + $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}); + $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); } @@ -251,14 +231,14 @@ sub post_invoice { $allocated += $qty; - last if (($totalqty -= $qty) <= 0); + last if ($totalqty -= $qty) <= 0; } $sth->finish(); - } else { + } else { # if ($form->{"inventory_accno_id_$i"}) - $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2); + $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2); if ($form->{taxincluded}) { $taxamount = $linetotal * ($taxrate / (1 + $taxrate)); @@ -273,23 +253,19 @@ sub post_invoice { if ($form->round_amount($taxrate, 7) == 0) { if ($form->{taxincluded}) { foreach $item (@taxaccounts) { - $taxamount = - $linetotal * $form->{"${item}_rate"} - / (1 + abs($form->{"${item}_rate"})); + $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}; + $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 expense @@ -316,19 +292,20 @@ sub post_invoice { $query = qq|INSERT INTO invoice (trans_id, parts_id, description, qty, base_qty, sellprice, fxsellprice, allocated, unit, deliverydate, - project_id, serialnumber) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|; + project_id, serialnumber, price_factor_id, price_factor, marge_price_factor) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT factor FROM price_factors WHERE id = ?), ?)|; @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}), $form->{"description_$i"}, $form->{"qty_$i"} * -1, $baseqty * -1, $form->{"sellprice_$i"}, $fxsellprice, $allocated, $form->{"unit_$i"}, conv_date($form->{deliverydate}), - conv_i($form->{"project_id_$i"}), $form->{"serialnumber_$i"}); + conv_i($form->{"project_id_$i"}), $form->{"serialnumber_$i"}, + conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"marge_price_factor_$i"})); do_query($form, $dbh, $query, @values); } $h_item_unit->finish(); - my $project_id = conv_i($form->{"globalproject_id"}); + $project_id = conv_i($form->{"globalproject_id"}); $form->{datepaid} = $form->{invdate}; @@ -340,7 +317,7 @@ sub post_invoice { for my $i (1 .. $form->{paidaccounts}) { $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}); $form->{paid} += $form->{"paid_$i"}; - $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"}); + $form->{datepaid} = $form->{"datepaid_$i"} if $form->{"datepaid_$i"}; } my ($tax, $paiddiff) = (0, 0); @@ -367,12 +344,9 @@ sub post_invoice { $expensediff += $paiddiff; ######## this only applies to tax included - if ($lastinventoryaccno) { - $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff; - } - if ($lastexpenseaccno) { - $form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff; - } + + $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff if $lastinventoryaccno; + $form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff if $lastexpenseaccno; } else { $amount = $form->round_amount($netamount * $form->{exchangerate}, 2); @@ -391,21 +365,20 @@ sub post_invoice { $form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax; - if ($form->{paid} != 0) { - $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2); - } + + $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0; # update exchangerate - if (($form->{currency} ne $defaultcurrency) && !$exchangerate) { - $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0, $form->{exchangerate}); - } + + $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0, $form->{exchangerate}) + if ($form->{currency} ne $defaultcurrency) && !$exchangerate; # 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}); + 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 = ?), ?, ?, @@ -425,13 +398,12 @@ sub post_invoice { } # force AP entry if 0 - if ($form->{amount}{ $form->{id} }{ $form->{AP} } == 0) { - $form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid}; - } + + $form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid} if $form->{amount}{$form->{id}}{$form->{AP}} == 0; # record payments and offsetting AP for my $i (1 .. $form->{paidaccounts}) { - next if ($form->{"paid_$i"} == 0); + next if $form->{"paid_$i"} == 0; my ($accno) = split /--/, $form->{"AP_paid_$i"}; $form->{"datepaid_$i"} = $form->{invdate} unless ($form->{"datepaid_$i"}); @@ -462,17 +434,12 @@ sub post_invoice { if ($form->{currency} eq $defaultcurrency) { $form->{"exchangerate_$i"} = 1; } else { - $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'); - - $form->{"exchangerate_$i"} = - ($exchangerate) - ? $exchangerate - : $form->parse_amount($myconfig, $form->{"exchangerate_$i"}); + $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'); + $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"}); } # exchangerate difference - $form->{fx}{$accno}{ $form->{"datepaid_$i"} } += - $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff; + $form->{fx}{$accno}{ $form->{"datepaid_$i"} } += $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff; # gain/loss $amount = @@ -480,7 +447,6 @@ sub post_invoice { ($form->{"paid_$i"} * $form->{"exchangerate_$i"}); if ($amount > 0) { $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } += $amount; - } else { $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } += $amount; } @@ -488,11 +454,8 @@ sub post_invoice { $paiddiff = 0; # update exchange rate - if (($form->{currency} ne $defaultcurrency) && !$exchangerate) { - $form->update_exchangerate($dbh, $form->{currency}, - $form->{"datepaid_$i"}, - 0, $form->{"exchangerate_$i"}); - } + $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, 0, $form->{"exchangerate_$i"}) + if ($form->{currency} ne $defaultcurrency) && !$exchangerate; } # record exchange rate differences and gains/losses @@ -528,45 +491,28 @@ sub post_invoice { $form->{department_id} = (split /--/, $form->{department})[1]; $form->{invnumber} = $form->{id} unless $form->{invnumber}; - $taxzone_id = 0 if ((3 < $taxzone_id) || (0 > $taxzone_id)); + $taxzone_id = 0 if (3 < $taxzone_id) || (0 > $taxzone_id); # save AP record $query = qq|UPDATE ap SET - invnumber = ?, - ordnumber = ?, - quonumber = ?, - transdate = ?, - orddate = ?, - quodate = ?, - vendor_id = ?, - amount = ?, - netamount = ?, - paid = ?, - datepaid = ?, - duedate = ?, - invoice = '1', - taxzone_id = ?, - taxincluded = ?, - notes = ?, - intnotes = ?, - curr = ?, - department_id = ?, - storno = ?, - globalproject_id = ?, - cp_id = ?, - employee_id = ? + invnumber = ?, ordnumber = ?, quonumber = ?, transdate = ?, + orddate = ?, quodate = ?, vendor_id = ?, amount = ?, + netamount = ?, paid = ?, duedate = ?, datepaid = ?, + invoice = ?, taxzone_id = ?, notes = ?, taxincluded = ?, + intnotes = ?, curr = ?, storno_id = ?, storno = ?, + cp_id = ?, employee_id = ?, department_id = ?, + globalproject_id = ? WHERE id = ?|; - @values = ($form->{invnumber}, $form->{ordnumber}, $form->{quonumber}, - conv_date($form->{invdate}), conv_date($form->{orddate}), conv_date($form->{quodate}), - conv_i($form->{vendor_id}), $amount, $netamount, $form->{paid}, - $form->{paid} ? conv_date($form->{datepaid}) : undef, - conv_date($form->{duedate}), $taxzone_id, - $form->{taxincluded} ? 't' : 'f', - $form->{notes}, $form->{intnotes}, $form->{currency}, conv_i($form->{department_id}), - $form->{storno} ? 't' : 'f', - conv_i($form->{globalproject_id}), conv_i($form->{cp_id}), - conv_i($form->{employee_id}), - conv_i($form->{id})); + @values = ( + $form->{invnumber}, $form->{ordnumber}, $form->{quonumber}, conv_date($form->{invdate}), + conv_date($form->{orddate}), conv_date($form->{quodate}), conv_i($form->{vendor_id}), $amount, + $netamount, $form->{paid}, conv_date($form->{duedate}), $form->{paid} ? conv_date($form->{datepaid}) : undef, + '1', $taxzone_id, $form->{notes}, $form->{taxincluded} ? 't' : 'f', + $form->{intnotes}, $form->{currency}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f', + conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}), + conv_i($form->{globalproject_id}), + conv_i($form->{id}) + ); do_query($form, $dbh, $query, @values); if ($form->{storno}) { @@ -586,7 +532,7 @@ sub post_invoice { # add shipto $form->{name} = $form->{vendor}; - $form->{name} =~ s/--$form->{vendor_id}//; + $form->{name} =~ s/--\Q$form->{vendor_id}\E//; $form->add_shipto($dbh, $form->{id}, "AP"); # delete zero entries @@ -594,8 +540,36 @@ sub post_invoice { Common::webdav_folder($form) if ($main::webdav); - my $rc = 1; + # Link this record to the records it was created from. + RecordLinks->create_links('dbh' => $dbh, + 'mode' => 'ids', + 'from_table' => 'oe', + 'from_ids' => $form->{convert_from_oe_ids}, + 'to_table' => 'ap', + 'to_id' => $form->{id}, + ); + delete $form->{convert_from_oe_ids}; + + my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids}; + if (scalar @convert_from_do_ids) { + DO->close_orders('dbh' => $dbh, + 'ids' => \@convert_from_do_ids); + + RecordLinks->create_links('dbh' => $dbh, + 'mode' => 'ids', + 'from_table' => 'delivery_orders', + 'from_ids' => \@convert_from_do_ids, + 'to_table' => 'ap', + 'to_id' => $form->{id}, + ); + } + delete $form->{convert_from_do_ids}; + + ARAP->close_orders_if_billed('dbh' => $dbh, + 'arap_id' => $form->{id}, + 'table' => 'ap',); + my $rc = 1; if (!$provided_dbh) { $rc = $dbh->commit(); $dbh->disconnect(); @@ -626,9 +600,6 @@ sub reverse_invoice { next unless $ref->{inventory_accno_id}; - # update onhand - $form->update_balance($dbh, "parts", "onhand", qq|id = $ref->{parts_id}|, $ref->{qty}); - # if $ref->{allocated} > 0 than we sold that many items next if ($ref->{allocated} <= 0); @@ -691,18 +662,18 @@ sub delete_invoice { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + my $query; # connect to database my $dbh = $form->dbconnect_noauto($myconfig); &reverse_invoice($dbh, $form); # delete zero entries - my $query = qq|DELETE FROM acc_trans WHERE amount = 0|; + $query = qq|DELETE FROM acc_trans WHERE amount = 0|; do_query($form, $dbh, $query); # delete AP record - my $query = qq|DELETE FROM ap WHERE id = ?|; + $query = qq|DELETE FROM ap WHERE id = ?|; do_query($form, $dbh, $query, conv_i($form->{id})); my $rc = $dbh->commit; @@ -723,20 +694,31 @@ sub retrieve_invoice { my ($query, $sth, $ref, $q_invdate); - $q_invdate = ", current_date AS invdate" unless $form->{id}; + if (!$form->{id}) { + $q_invdate = qq|, COALESCE((SELECT transdate FROM ar WHERE id = (SELECT MAX(id) FROM ar)), current_date) AS invdate|; + if ($form->{vendor_id}) { + my $vendor_id = $dbh->quote($form->{vendor_id} * 1); + $q_invdate .= + qq|, COALESCE((SELECT transdate FROM ar WHERE id = (SELECT MAX(id) FROM ar)), current_date) + + COALESCE((SELECT pt.terms_netto + FROM vendor v + LEFT JOIN payment_terms pt ON (v.payment_id = pt.id) + WHERE v.id = $vendor_id), + 0) AS duedate|; + } + } # get default accounts and last invoice number - $query= - qq|SELECT - (SELECT c.accno FROM chart c WHERE d.inventory_accno_id = c.id) AS inventory_accno, - (SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id) AS income_accno, - (SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id) AS expense_accno, - (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, - (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, - d.curr AS currencies - $q_invdate - FROM defaults d|; + $query = qq|SELECT + (SELECT c.accno FROM chart c WHERE d.inventory_accno_id = c.id) AS inventory_accno, + (SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id) AS income_accno, + (SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id) AS expense_accno, + (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, + (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, + d.curr AS currencies + $q_invdate + FROM defaults d|; $ref = selectfirst_hashref_query($form, $dbh, $query); map { $form->{$_} = $ref->{$_} } keys %$ref; @@ -748,17 +730,16 @@ sub retrieve_invoice { } # retrieve invoice - $query = - qq|SELECT cp_id, invnumber, transdate AS invdate, duedate, - orddate, quodate, globalproject_id, - ordnumber, quonumber, paid, taxincluded, notes, taxzone_id, storno, gldate, - intnotes, curr AS currency - FROM ap - WHERE id = ?|; + $query = qq|SELECT cp_id, invnumber, transdate AS invdate, duedate, + orddate, quodate, globalproject_id, + ordnumber, quonumber, paid, taxincluded, notes, taxzone_id, storno, gldate, + intnotes, curr AS currency + FROM ap + WHERE id = ?|; $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id})); map { $form->{$_} = $ref->{$_} } keys %$ref; - $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "sell"); + $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "sell"); # get shipto $query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module = 'AP')|; @@ -774,39 +755,19 @@ sub retrieve_invoice { # retrieve individual items $query = qq|SELECT - c1.accno AS inventory_accno, - c1.new_chart_id AS inventory_new_chart, - date($transdate) - c1.valid_from AS inventory_valid, - - c2.accno AS income_accno, - c2.new_chart_id AS income_new_chart, - date($transdate) - c2.valid_from AS income_valid, - - c3.accno AS expense_accno, - c3.new_chart_id AS expense_new_chart, - date($transdate) - c3.valid_from AS expense_valid, + c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from AS inventory_valid, + c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from AS income_valid, + c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from AS expense_valid, - i.description, i.qty, i.fxsellprice AS sellprice, - i.parts_id AS id, i.unit, i.deliverydate, i.project_id, i.serialnumber, - - p.partnumber, p.inventory_accno_id AS part_inventory_accno_id, p.bin, - pr.projectnumber, - pg.partsgroup + i.description, i.qty, i.fxsellprice AS sellprice, i.parts_id AS id, i.unit, i.deliverydate, i.project_id, i.serialnumber, + i.price_factor_id, i.price_factor, i.marge_price_factor, + p.partnumber, p.inventory_accno_id AS part_inventory_accno_id, p.bin, pr.projectnumber, pg.partsgroup FROM invoice i JOIN parts p ON (i.parts_id = p.id) - LEFT JOIN chart c1 ON - ((SELECT inventory_accno_id - FROM buchungsgruppen - WHERE id = p.buchungsgruppen_id) = c1.id) - LEFT JOIN chart c2 ON - ((SELECT income_accno_id_${taxzone_id} - FROM buchungsgruppen - WHERE id = p.buchungsgruppen_id) = c2.id) - LEFT JOIN chart c3 ON - ((SELECT expense_accno_id_${taxzone_id} - FROM buchungsgruppen - WHERE id = p.buchungsgruppen_id) = c3.id) + LEFT JOIN chart c1 ON ((SELECT inventory_accno_id FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c1.id) + LEFT JOIN chart c2 ON ((SELECT income_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c2.id) + LEFT JOIN chart c3 ON ((SELECT expense_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c3.id) LEFT JOIN project pr ON (i.project_id = pr.id) LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id) @@ -816,37 +777,24 @@ sub retrieve_invoice { $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id})); while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - if (!$ref->{"part_inventory_accno_id"}) { - map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)); - } + map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_inventory_accno_id"}; delete($ref->{"part_inventory_accno_id"}); foreach my $type (qw(inventory income expense)) { while ($ref->{"${type}_new_chart"} && ($ref->{"${type}_valid"} >=0)) { - my $query = - qq|SELECT accno, new_chart_id, date($transdate) - valid_from - FROM chart - WHERE id = ?|; - ($ref->{"${type}_accno"}, - $ref->{"${type}_new_chart"}, - $ref->{"${type}_valid"}) - = selectrow_query($form, $dbh, $query, $ref->{"${type}_new_chart"}); + my $query = qq|SELECT accno, new_chart_id, date($transdate) - valid_from FROM chart WHERE id = ?|; + @$ref{ map $type.$_, qw(_accno _new_chart _valid) } = selectrow_query($form, $dbh, $query, $ref->{"${type}_new_chart"}); } } # get tax rates and description my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno}; $query = - qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber - FROM tax t + qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber FROM tax t LEFT JOIN chart c ON (c.id = t.chart_id) WHERE t.id in - (SELECT tk.tax_id - FROM taxkeys tk - WHERE tk.chart_id = - (SELECT id - FROM chart - WHERE accno = ?) + (SELECT tk.tax_id FROM taxkeys tk + WHERE tk.chart_id = (SELECT id FROM chart WHERE accno = ?) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1) @@ -863,7 +811,7 @@ sub retrieve_invoice { $ref->{taxaccounts} .= "$ptr->{accno} "; - if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) { + if (!($form->{taxaccounts} =~ /\Q$ptr->{accno}\E/)) { $form->{"$ptr->{accno}_rate"} = $ptr->{rate}; $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription}; $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber}; @@ -888,7 +836,9 @@ sub retrieve_invoice { sub get_vendor { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; + my ($self, $myconfig, $form, $params) = @_; + + $params = $form unless defined $params && ref $params eq "HASH"; # connect to database my $dbh = $form->dbconnect($myconfig); @@ -896,17 +846,27 @@ sub get_vendor { my $dateformat = $myconfig->{dateformat}; $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/; - my $vid = conv_i($form->{vendor_id}); + my $vid = conv_i($params->{vendor_id}); + my $vnr = conv_i($params->{vendornumber}); my $duedate = - ($form->{invdate}) - ? "to_date(" . $dbh->quote($form->{invdate}) . ", '$dateformat')" + ($params->{invdate}) + ? "to_date(" . $dbh->quote($params->{invdate}) . ", '$dateformat')" : "current_date"; # get vendor + @values = (); + if ($vid) { + $where .= 'AND v.id = ?'; + push @values, $vid; + } + if ($vnr) { + $where .= 'AND v.vendornumber = ?'; + push @values, $vnr; + } my $query = qq|SELECT - v.name AS vendor, v.creditlimit, v.terms, v.notes AS intnotes, + v.id AS vendor_id, v.name AS vendor, v.creditlimit, v.terms, v.notes AS intnotes, v.email, v.cc, v.bcc, v.language_id, v.payment_id, v.street, v.zipcode, v.city, v.country, v.taxzone_id, $duedate + COALESCE(pt.terms_netto, 0) AS duedate, @@ -914,15 +874,15 @@ sub get_vendor { FROM vendor v LEFT JOIN business b ON (b.id = v.business_id) LEFT JOIN payment_terms pt ON (v.payment_id = pt.id) - WHERE v.id = ?|; - $ref = selectfirst_hashref_query($form, $dbh, $query, $vid); - map { $form->{$_} = $ref->{$_} } keys %$ref; + WHERE 1=1 $where|; + $ref = selectfirst_hashref_query($form, $dbh, $query, @values); + map { $params->{$_} = $ref->{$_} } keys %$ref; - $form->{creditremaining} = $form->{creditlimit}; + $params->{creditremaining} = $params->{creditlimit}; $query = qq|SELECT SUM(amount - paid) FROM ap WHERE vendor_id = ?|; my ($unpaid_invoices) = selectfirst_array_query($form, $dbh, $query, $vid); - $form->{creditremaining} -= $unpaid_invoices; + $params->{creditremaining} -= $unpaid_invoices; $query = qq|SELECT o.amount, (SELECT e.sell @@ -934,21 +894,21 @@ sub get_vendor { my $sth = prepare_execute_query($form, $dbh, $query, $vid); while (my ($amount, $exch) = $sth->fetchrow_array()) { $exch = 1 unless $exch; - $form->{creditremaining} -= $amount * $exch; + $params->{creditremaining} -= $amount * $exch; } $sth->finish(); # get shipto if we do not convert an order or invoice - if (!$form->{shipto}) { - delete @{$form}{qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail)}; + if (!$params->{shipto}) { + delete @{$params}{qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail)}; $query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module= 'CT')|; $ref = selectfirst_hashref_query($form, $dbh, $query, $vid); - @{$form}{keys %$ref} = @{$ref}{keys %$ref}; - map { $form->{$_} = $ref->{$_} } keys %$ref; + @{$params}{keys %$ref} = @{$ref}{keys %$ref}; + map { $params->{$_} = $ref->{$_} } keys %$ref; } - if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) { + if (!$params->{id} && $params->{type} !~ /_(order|quotation)/) { # setup last accounts used $query = qq|SELECT c.id, c.accno, c.description, c.link, c.category @@ -965,24 +925,24 @@ sub get_vendor { if ($ref->{category} eq 'E') { $i++; - if ($form->{initial_transdate}) { + if ($params->{initial_transdate}) { my $tax_query = qq|SELECT tk.tax_id, t.rate FROM taxkeys tk LEFT JOIN tax t ON (tk.tax_id = t.id) WHERE (tk.chart_id = ?) AND (startdate <= ?) ORDER BY tk.startdate DESC LIMIT 1|; - my ($tax_id, $rate) = selectrow_query($form, $dbh, $tax_query, $ref->{id}, $form->{initial_transdate}); - $form->{"taxchart_$i"} = "${tax_id}--${rate}"; + my ($tax_id, $rate) = selectrow_query($form, $dbh, $tax_query, $ref->{id}, $params->{initial_transdate}); + $params->{"taxchart_$i"} = "${tax_id}--${rate}"; } - $form->{"AP_amount_$i"} = "$ref->{accno}--$tax_id"; + $params->{"AP_amount_$i"} = "$ref->{accno}--$tax_id"; } if ($ref->{category} eq 'L') { - $form->{APselected} = $form->{AP_1} = $ref->{accno}; + $params->{APselected} = $params->{AP_1} = $ref->{accno}; } } - $form->{rowcount} = $i if ($i && !$form->{type}); + $params->{rowcount} = $i if ($i && !$params->{type}); } $dbh->disconnect(); @@ -1007,7 +967,7 @@ sub retrieve_item { foreach my $table_column (qw(p.partnumber p.description pg.partsgroup)) { my $field = (split m{\.}, $table_column)[1]; next unless $form->{"${field}_${i}"}; - $where .= " AND lower(${table_column}) LIKE ?"; + $where .= " AND lower(${table_column}) LIKE lower(?)"; push @values, '%' . $form->{"${field}_${i}"} . '%'; } @@ -1032,7 +992,9 @@ sub retrieve_item { p.id, p.partnumber, p.description, p.lastcost AS sellprice, p.listprice, p.unit, p.assembly, p.bin, p.onhand, p.formel, p.notes AS partnotes, p.notes AS longdescription, p.not_discountable, - p.inventory_accno_id, + p.inventory_accno_id, p.price_factor_id, + + pfac.factor AS price_factor, c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, @@ -1062,6 +1024,7 @@ sub retrieve_item { FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c3.id) LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id) + LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id) WHERE $where|; my $sth = prepare_execute_query($form, $dbh, $query, @values); @@ -1107,7 +1070,7 @@ sub retrieve_item { $ref->{taxaccounts} .= "$ptr->{accno} "; - if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) { + if (!($form->{taxaccounts} =~ /\Q$ptr->{accno}\E/)) { $form->{"$ptr->{accno}_rate"} = $ptr->{rate}; $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription}; $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber}; @@ -1119,6 +1082,8 @@ sub retrieve_item { $stw->finish(); chop $ref->{taxaccounts}; + $ref->{onhand} *= 1; + push @{ $form->{item_list} }, $ref; } @@ -1140,7 +1105,7 @@ sub vendor_details { my @values; # get contact id, set it if nessessary - $form->{cp_id} = (split /--/, $form->{contact})[1]; + $form->{cp_id} *= 1; my $contact = ""; if ($form->{cp_id}) { $contact = "AND cp.cp_id = ?"; @@ -1170,6 +1135,11 @@ sub vendor_details { map { $form->{$_} = $ref->{$_} } keys %$ref; + my $custom_variables = CVar->get_custom_variables('dbh' => $dbh, + 'module' => 'CT', + 'trans_id' => $form->{vendor_id}); + map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables }; + $dbh->disconnect(); $main::lxdebug->leave_sub(); @@ -1313,4 +1283,42 @@ sub post_payment { return $rc; } +sub get_duedate { + $main::lxdebug->enter_sub(); + + my $self = shift; + my %params = @_; + + if (!$params{vendor_id} || !$params{invdate}) { + $main::lxdebug->leave_sub(); + return $params{default}; + } + + my $myconfig = \%main::myconfig; + my $form = $main::form; + + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + + my $query = qq|SELECT ?::date + pt.terms_netto + FROM vendor v + LEFT JOIN payment_terms pt ON (pt.id = v.payment_id) + WHERE v.id = ?|; + + my ($sth, $duedate); + + if (($sth = $dbh->prepare($query)) && $sth->execute($params{invdate}, conv_i($params{vendor_id}))) { + ($duedate) = $sth->fetchrow_array(); + $sth->finish(); + } else { + $dbh->rollback(); + } + + $duedate ||= $params{default}; + + $main::lxdebug->leave_sub(); + + return $duedate; +} + + 1;