X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FOE.pm;h=524af26effd98915a2dcc007efc55642bff0278e;hb=67978a2d5a19e3d2af10a476fb9e0306dda118fc;hp=3d92746b55db2bfcf5beb45bc65a4dd2bc5eb427;hpb=b832f3446db5b6132bdf8e96ddeb11ff5edd681d;p=kivitendo-erp.git diff --git a/SL/OE.pm b/SL/OE.pm index 3d92746b5..524af26ef 100644 --- a/SL/OE.pm +++ b/SL/OE.pm @@ -45,6 +45,7 @@ use SL::DB::PeriodicInvoicesConfig; use SL::DB::Status; use SL::DB::Tax; use SL::DBUtils; +use SL::HTML::Restrict; use SL::IC; use SL::TransNumber; @@ -80,10 +81,32 @@ sub transactions { my $vc = $form->{vc} eq "customer" ? "customer" : "vendor"; + my %billed_amount; + my %billed_netamount; + if ($form->{l_remaining_amount} || $form->{l_remaining_netamount}) { + $query = <<''; + SELECT from_id, ar.amount, ar.netamount FROM ( + SELECT from_id, to_id + FROM record_links + WHERE from_table = 'oe' AND to_table = 'ar' + UNION + SELECT rl1.from_id, rl2.to_id + FROM record_links rl1 + LEFT JOIN record_links rl2 ON (rl1.to_table = rl2.from_table AND rl1.to_id = rl2.from_id) + WHERE rl1.from_table = 'oe' AND rl2.to_table = 'ar' + ) rl + LEFT JOIN ar ON ar.id = rl.to_id + + for my $ref (@{ selectall_hashref_query($form, $dbh, $query) }) { + $billed_amount{ $ref->{from_id}} += $ref->{amount}; + $billed_netamount{$ref->{from_id}} += $ref->{netamount}; + } + } + $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate, | . qq| o.amount, ct.${vc}number, ct.name, o.netamount, o.${vc}_id, o.globalproject_id, | . - qq| o.closed, o.delivered, o.quonumber, o.shippingpoint, o.shipvia, | . + qq| o.closed, o.delivered, o.quonumber, o.cusordnumber, o.shippingpoint, o.shipvia, | . qq| o.transaction_description, | . qq| o.marge_total, o.marge_percent, | . qq| ex.$rate AS exchangerate, | . @@ -175,6 +198,11 @@ SQL push(@values, '%' . $form->{$ordnumber} . '%'); } + if ($form->{cusordnumber}) { + $query .= qq| AND o.cusordnumber ILIKE ?|; + push(@values, '%' . $form->{cusordnumber} . '%'); + } + if($form->{transdatefrom}) { $query .= qq| AND o.transdate >= ?|; push(@values, conv_date($form->{transdatefrom})); @@ -222,6 +250,7 @@ SQL "reqdate" => "o.reqdate", "id" => "o.id", "ordnumber" => "o.ordnumber", + "cusordnumber" => "o.cusordnumber", "quonumber" => "o.quonumber", "name" => "ct.name", "employee" => "e.name", @@ -243,6 +272,10 @@ SQL my %id = (); $form->{OE} = []; while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { + $ref->{billed_amount} = $billed_amount{$ref->{id}}; + $ref->{billed_netamount} = $billed_netamount{$ref->{id}}; + $ref->{remaining_amount} = $ref->{amount} - $ref->{billed_amount}; + $ref->{remaining_netamount} = $ref->{netamount} - $ref->{billed_netamount}; $ref->{exchangerate} = 1 unless $ref->{exchangerate}; push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} }; $id{ $ref->{id} } = $ref->{id}; @@ -298,6 +331,7 @@ sub save { # connect to database, turn off autocommit my $dbh = $form->get_standard_dbh; + my $restricter = SL::HTML::Restrict->create; my ($query, @values, $sth, $null); my $exchangerate = 0; @@ -383,25 +417,27 @@ sub save { $form->{"lastcost_$i"} = $form->parse_amount($myconfig, $form->{"lastcost_$i"}); - # set values to 0 if nothing entered - $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100; - - $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"}); - $fxsellprice = $form->{"sellprice_$i"}; + # keep entered selling price + my $fxsellprice = + $form->parse_amount($myconfig, $form->{"sellprice_$i"}); - my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/); - $dec = length($dec); + my ($dec) = ($fxsellprice =~ /\.(\d+)/); + $dec = length $dec; my $decimalplaces = ($dec > 2) ? $dec : 2; - $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}, $decimalplaces); - $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces); + # undo discount formatting + $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100; - $form->{"inventory_accno_$i"} *= 1; - $form->{"expense_accno_$i"} *= 1; + # deduct discount + $form->{"sellprice_$i"} = $fxsellprice * (1 - $form->{"discount_$i"}); + # round linetotal at least to 2 decimal places $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1; $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2); + $form->{"inventory_accno_$i"} *= 1; + $form->{"expense_accno_$i"} *= 1; + @taxaccounts = split(/ /, $form->{"taxaccounts_$i"}); $taxrate = 0; $taxdiff = 0; @@ -468,7 +504,7 @@ sub save { (SELECT factor FROM price_factors WHERE id = ?), ?)|; push(@values, conv_i($orderitems_id), conv_i($form->{id}), conv_i($form->{"id_$i"}), - $form->{"description_$i"}, $form->{"longdescription_$i"}, + $form->{"description_$i"}, $restricter->process($form->{"longdescription_$i"}), $form->{"qty_$i"}, $baseqty, $fxsellprice, $form->{"discount_$i"}, $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}), @@ -630,7 +666,7 @@ sub load_periodic_invoice_config { my $config_obj = SL::DB::Manager::PeriodicInvoicesConfig->find_by(oe_id => $form->{id}); if ($config_obj) { - my $config = { map { $_ => $config_obj->$_ } qw(active terminated periodicity start_date_as_date end_date_as_date extend_automatically_by ar_chart_id + my $config = { map { $_ => $config_obj->$_ } qw(active terminated periodicity start_date_as_date end_date_as_date first_billing_date_as_date extend_automatically_by ar_chart_id print printer_id copies) }; $form->{periodic_invoices_config} = YAML::Dump($config); }