$main::lxdebug->leave_sub(2);
}
+
+sub _remove_billed_or_delivered_rows {
+ my (%params) = @_;
+
+ croak "Missing parameter 'quantities'" if !$params{quantities};
+
+ my @fields = map { s/_1$//; $_ } grep { m/_1$/ } keys %{ $::form };
+ my @new_rows;
+
+ my $removed_rows = 0;
+ my $row = 0;
+ while ($row < $::form->{rowcount}) {
+ $row++;
+ next unless $::form->{"id_$row"};
+
+ my $parts_id = $::form->{"id_$row"};
+ my $base_qty = $::form->parse_amount(\%::myconfig, $::form->{"qty_$row"}) * SL::DB::Manager::Unit->find_by(name => $::form->{"unit_$row"})->base_factor;
+
+ my $sub_qty = min($base_qty, $params{quantities}->{$parts_id});
+ $params{quantities}->{$parts_id} -= $sub_qty;
+
+ if (!$sub_qty || ($sub_qty != $base_qty)) {
+ $::form->{"qty_${row}"} = $::form->format_amount(\%::myconfig, ($base_qty - $sub_qty) / SL::DB::Manager::Unit->find_by(name => $::form->{"unit_$row"})->base_factor);
+ push @new_rows, { map { $_ => $::form->{"${_}_${row}"} } @fields };
+
+ } else {
+ $removed_rows++;
+ }
+ }
+
+ $::form->redo_rows(\@fields, \@new_rows, scalar(@new_rows), $::form->{rowcount});
+ $::form->{rowcount} -= $removed_rows;
+}
# Quotation module
#======================================================================
+
+use Carp;
use POSIX qw(strftime);
use SL::DO;
use SL::PE;
use SL::ReportGenerator;
use List::MoreUtils qw(any none);
-use List::Util qw(max reduce sum);
+use List::Util qw(min max reduce sum);
use Data::Dumper;
require "bin/mozilla/io.pl";
::end_of_request();
}
+ _oe_remove_delivered_or_billed_rows(id => $form->{id}, type => 'billed');
+
$form->{cp_id} *= 1;
for my $i (1 .. $form->{rowcount}) {
$form->{old_employee_id} = $form->{employee_id};
$form->{old_salesman_id} = $form->{salesman_id};
+ _oe_remove_delivered_or_billed_rows(id => $form->{id}, type => 'delivered');
+
# reset
delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
$::lxdebug->leave_sub();
}
+sub _oe_remove_delivered_or_billed_rows {
+ my (%params) = @_;
+
+ return if !$params{id} || !$params{type};
+
+ my $ord_quot = SL::DB::Order->new(id => $params{id})->load;
+ return if !$ord_quot;
+
+ my %args = (
+ direction => 'to',
+ to => $params{type} eq 'delivered' ? 'DeliveryOrder' : 'Invoice',
+ via => [ $params{type} eq 'delivered' ? qw(Order) : qw(Order DeliveryOrder) ],
+ );
+
+ my %handled_base_qtys;
+ foreach my $record (@{ $ord_quot->linked_records(%args) }) {
+ next if $ord_quot->is_sales != $record->is_sales;
+
+ foreach my $item (@{ $record->items }) {
+ $handled_base_qtys{ $item->parts_id } += $item->qty * $item->unit_obj->base_factor;
+ }
+ }
+
+ _remove_billed_or_delivered_rows(quantities => \%handled_base_qtys);
+}
+
sub dispatcher {
foreach my $action (qw(delete delivery_order e_mail invoice print purchase_order purchase_order quotation
request_for_quotation sales_order sales_order save save_and_close save_as_new ship_to update)) {