Listenpreis in Belegen u. Ausdruck richtig formatieren und nicht mehr parsen.
[kivitendo-erp.git] / bin / mozilla / io.pl
index 99ba934..c511893 100644 (file)
@@ -65,8 +65,8 @@ use strict;
 if (-f "bin/mozilla/custom_io.pl") {
   eval { require "bin/mozilla/custom_io.pl"; };
 }
-if (-f "bin/mozilla/$::form->{login}_io.pl") {
-  eval { require "bin/mozilla/$::form->{login}_io.pl"; };
+if (-f "bin/mozilla/$::myconfig{login}_io.pl") {
+  eval { require "bin/mozilla/$::myconfig{login}_io.pl"; };
 }
 
 1;
@@ -139,6 +139,7 @@ sub display_row {
   my $is_quotation       = $form->{type} =~ /_quotation$/;
   my $is_invoice         = $form->{type} =~ /invoice/;
   my $is_s_p_order       = (first { $_ eq $form->{type} } qw(sales_order purchase_order));
+  my $show_ship_missing  = $is_s_p_order && $::instance_conf->get_sales_purchase_order_ship_missing_column;
 
   if ($is_delivery_order) {
     if ($form->{type} eq 'sales_delivery_order') {
@@ -159,6 +160,7 @@ sub display_row {
     {  id => 'partnumber',    width => 8,     value => $locale->text('Number'),               display => 1, },
     {  id => 'description',   width => 30,    value => $locale->text('Part Description'),     display => 1, },
     {  id => 'ship',          width => 5,     value => $locale->text('Delivered'),            display => $is_s_p_order, },
+    {  id => 'ship_missing',  width => 5,     value => $locale->text('Not delivered'),        display => $show_ship_missing, },
     {  id => 'qty',           width => 5,     value => $locale->text('Qty'),                  display => 1, },
     {  id => 'price_factor',  width => 5,     value => $locale->text('Price Factor'),         display => !$is_delivery_order, },
     {  id => 'unit',          width => 5,     value => $locale->text('Unit'),                 display => 1, },
@@ -200,7 +202,7 @@ sub display_row {
   my $deliverydate  = $locale->text('Required by');
 
   # special alignings
-  my %align  = map { $_ => 'right' } qw(qty ship right discount linetotal stock_in_out weight);
+  my %align  = map { $_ => 'right' } qw(qty ship right discount linetotal stock_in_out weight ship_missing);
   my %nowrap = map { $_ => 1 }       qw(description unit);
 
   $form->{marge_total}           = 0;
@@ -309,6 +311,11 @@ sub display_row {
       $ship_qty          /= ( $all_units->{$form->{"unit_$i"}}->{factor} || 1 );
 
       $column_data{ship}  = $form->format_amount(\%myconfig, $form->round_amount($ship_qty, 2) * 1) . ' ' . $form->{"unit_$i"};
+
+      my $ship_missing_qty    = $form->{"qty_$i"} - $ship_qty;
+      my $ship_missing_amount = $form->round_amount($ship_missing_qty * $form->{"sellprice_$i"} * (100 - $form->{"discount_$i"}) / 100 / $price_factor, 2);
+
+      $column_data{ship_missing} = $form->format_amount(\%myconfig, $ship_missing_qty) . ' ' . $form->{"unit_$i"} . '; ' . $form->format_amount(\%myconfig, $ship_missing_amount, $decimalplaces);
     }
 
     my $sellprice_value = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
@@ -413,8 +420,6 @@ sub display_row {
                    $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces) }
       if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ ||  $form->{type} =~ /invoice/ || $form->{type} =~ /^credit_note$/ ) && !$is_delivery_order;
 
-    $form->{"listprice_$i"} = $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2)
-      if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ ||  $form->{type} =~ /invoice/) ;
 # / marge calculations ending
 
 # Calculate total weight
@@ -442,7 +447,7 @@ sub display_row {
       push @hidden_vars, qw(orderitems_id converted_from_orderitems_id converted_from_invoice_id);
     }
     if ($is_invoice) {
-      push @hidden_vars, qw(invoice_id converted_from_orderitems_id converted_from_delivery_order_items_id);
+      push @hidden_vars, qw(invoice_id converted_from_orderitems_id converted_from_delivery_order_items_id converted_from_invoice_id);
     }
     if ($::form->{type} =~ /credit_note/) {
       push @hidden_vars, qw(invoice_id converted_from_invoice_id);
@@ -451,7 +456,7 @@ sub display_row {
       map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
       push @hidden_vars, grep { defined $form->{"${_}_${i}"} } qw(sellprice discount not_discountable price_factor_id lastcost);
       push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
-      push @hidden_vars, qw(delivery_order_items_id converted_from_orderitems_id);
+      push @hidden_vars, qw(delivery_order_items_id converted_from_orderitems_id converted_from_delivery_order_items_id);
     }
 
     my @HIDDENS = map { value => $_}, (
@@ -490,8 +495,8 @@ sub select_item {
   $main::lxdebug->enter_sub();
 
   my %params = @_;
-  my $mode   = $params{mode} || croak "Missing parameter 'mode'";
-
+  my $mode            = $params{mode}            || croak "Missing parameter 'mode'";
+  my $pre_entered_qty = $params{pre_entered_qty} || 1;
   _check_io_auth();
 
   my $previous_form = $::auth->save_form_in_session(form => $::form);
@@ -506,15 +511,21 @@ sub select_item {
   # delete action variable
   delete @{$::form}{qw(action item_list)};
 
-  print $::form->parse_html_template('io/select_item', { PREVIOUS_FORM => $previous_form,
-                                                         MODE          => $mode,
-                                                         ITEM_LIST     => \@item_list,
-                                                         IS_PURCHASE   => $mode eq 'IS' });
+  print $::form->parse_html_template('io/select_item', { PREVIOUS_FORM   => $previous_form,
+                                                         MODE            => $mode,
+                                                         ITEM_LIST       => \@item_list,
+                                                         IS_ASSEMBLY     => $mode eq 'IC',
+                                                         IS_PURCHASE     => $mode eq 'IS',
+                                                         PRE_ENTERED_QTY => $pre_entered_qty, });
 
   $main::lxdebug->leave_sub();
 }
 
 sub item_selected {
+
+  # this function is used for adding parts to records (mode = IR/IS)
+  # and to assemblies (mode = IC)
+
   $main::lxdebug->enter_sub();
 
   my $form     = $main::form;
@@ -524,106 +535,142 @@ sub item_selected {
 
   $::auth->restore_form_from_session($form->{select_item_previous_form} || croak('Missing previous form ID'), form => $form);
 
-  my $mode = delete($form->{select_item_mode}) || croak 'Missing item selection mode';
-  my $id   = delete($form->{select_item_id})   || croak 'Missing item selection ID';
-  my $i    = $form->{ $mode eq 'IC' ? 'assembly_rows' : 'rowcount' };
+  my $mode     = delete($form->{select_item_mode}) || croak 'Missing item selection mode';
+  my $row_key  = $mode eq 'IC' ? 'assembly_rows' : 'rowcount';
+  my $curr_row = $form->{ $row_key };
 
-  $form->{"id_${i}"} = $id;
+  my $row = $curr_row;
 
-  if ($mode eq 'IS') {
-    IS->retrieve_item(\%myconfig, \%$form);
-  } elsif ($mode eq 'IR') {
-    IR->retrieve_item(\%myconfig, \%$form);
-  } elsif ($mode eq 'IC') {
-    IC->assembly_item(\%myconfig, \%$form);
+  if ($myconfig{item_multiselect}) {
+    foreach (grep(/^select_qty_/, keys(%{ $form }))) {
+      next unless $form->{$_};
+      $_ =~ /^select_qty_(\d+)/;
+      $form->{"id_${row}"}  = $1;
+      $form->{"qty_${row}"} = $form->{$_};
+      $row++;
+    }
   } else {
-    croak "Invalid item selection mode '${mode}'";
+    $form->{"id_${row}"} = delete($form->{select_item_id}) || croak 'Missing item selection ID';
+    $row++;
   }
 
-  my $new_item = $form->{item_list}->[0] || croak "No item found for mode '${mode}' and ID '${id}'";
+  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
+    qw(sellprice weight);
 
-  # if there was a price entered, override it
-  my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
+  if ( $mode eq 'IC' ) {
+    # assembly mode:
+    # the qty variables of the existing assembly items are all still formatted, so we parse them here
+    # including the qty of the just added part
+    $form->{"qty_$_"} = $form->parse_amount(\%myconfig, $form->{"qty_$_"}) for (1 .. $row - 1);
+  } else {
+    if ($myconfig{item_multiselect}) {
+      # other modes and multiselection:
+      # parse all newly entered qtys
+      $form->{"qty_$_"} = $form->parse_amount(\%myconfig, $form->{"qty_$_"}) for ($curr_row .. $row - 1);
+    }
+  }
 
-  my @new_fields =
-    qw(id partnumber description sellprice listprice inventory_accno
-       income_accno expense_accno bin unit weight assembly taxaccounts
-       partsgroup formel longdescription not_discountable partnotes lastcost
-       price_factor_id price_factor);
+  for my $i ($curr_row .. $row - 1) {
+    $form->{ $row_key } = $i;
 
-  my $ic_cvar_configs = CVar->get_configs(module => 'IC');
-  push @new_fields, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
+    my $id = $form->{"id_${i}"};
 
-  map { $form->{"${_}_$i"} = $new_item->{$_} } @new_fields;
+    delete $form->{item_list};
 
-  my $record       = _make_record();
-  my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
-  my $best_price   = $price_source->best_price;
+    if ($mode eq 'IS') {
+      IS->retrieve_item(\%myconfig, \%$form);
+    } elsif ($mode eq 'IR') {
+      IR->retrieve_item(\%myconfig, \%$form);
+    } elsif ($mode eq 'IC') {
+      IC->assembly_item(\%myconfig, \%$form);
+    } else {
+      croak "Invalid item selection mode '${mode}'";
+    }
 
-  if ($best_price) {
-    $::form->{"sellprice_$i"}           = $best_price->price;
-    $::form->{"active_price_source_$i"} = $best_price->source;
-  }
+    my $new_item = $form->{item_list}->[0] || croak "No item found for mode '${mode}' and ID '${id}'";
 
-  my $best_discount = $price_source->best_discount;
+    # if there was a price entered, override it
+    my $sellprice;
+    unless ( $mode eq 'IC' ) {
+      $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
+    };
 
-  if ($best_discount) {
-    $::form->{"discount_$i"}               = $best_discount->discount;
-    $::form->{"active_discount_source_$i"} = $best_discount->source;
-  }
+    my @new_fields =
+        qw(id partnumber description sellprice listprice inventory_accno
+           income_accno expense_accno bin unit weight assembly taxaccounts
+           partsgroup formel longdescription not_discountable partnotes lastcost
+           price_factor_id price_factor);
 
+    my $ic_cvar_configs = CVar->get_configs(module => 'IC');
+    push @new_fields, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
 
-  $form->{"marge_price_factor_$i"} = $new_item->{price_factor};
+    map { $form->{"${_}_$i"} = $new_item->{$_} } @new_fields;
 
-  if ($form->{"part_payment_id_$i"} ne "") {
-    $form->{payment_id} = $form->{"part_payment_id_$i"};
-  }
+    if (my $record = _make_record()) {
+      my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
+      my $best_price   = $price_source->best_price;
 
-  my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
-  $dec           = length $dec;
-  my $decimalplaces = ($dec > 2) ? $dec : 2;
+      if ($best_price) {
+        $::form->{"sellprice_$i"}           = $best_price->price;
+        $::form->{"active_price_source_$i"} = $best_price->source;
+      }
 
-  if ($sellprice) {
-    $form->{"sellprice_$i"} = $sellprice;
-  } else {
+      my $best_discount = $price_source->best_discount;
 
-    # if there is an exchange rate adjust sellprice
-    if (($form->{exchangerate} * 1) != 0) {
-      $form->{"sellprice_$i"} /= $form->{exchangerate};
-      $form->{"sellprice_$i"} =
-        $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
+      if ($best_discount) {
+        $::form->{"discount_$i"}               = $best_discount->discount;
+        $::form->{"active_discount_source_$i"} = $best_discount->source;
+      }
     }
-  }
 
-  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
-    qw(sellprice listprice weight);
+    $form->{"marge_price_factor_$i"} = $new_item->{price_factor};
+
+    if ($form->{"part_payment_id_$i"} ne "") {
+      $form->{payment_id} = $form->{"part_payment_id_$i"};
+    }
 
-  $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
+    my ($dec)         = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
+    $dec              = length $dec;
+    my $decimalplaces = ($dec > 2) ? $dec : 2;
 
-  if ($form->{"not_discountable_$i"}) {
-    $form->{"discount_$i"} = 0;
-  }
+    if ($sellprice) {
+      $form->{"sellprice_$i"} = $sellprice;
+    } else {
 
-  my $amount =
-    $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
-    $form->{"qty_$i"};
-  map { $form->{"${_}_base"} += $amount }
-    (split / /, $form->{"taxaccounts_$i"});
-  map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
-    $form->{"taxaccounts_$i"}
-    if !$form->{taxincluded};
+      # if there is an exchange rate adjust sellprice
+      if (($form->{exchangerate} * 1) != 0) {
+        $form->{"sellprice_$i"} /= $form->{exchangerate};
+        $form->{"sellprice_$i"} =
+            $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
+      }
+    }
+
+    # at this stage qty of newly added part needs to be have been parsed
+    $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
+
+    if ($form->{"not_discountable_$i"}) {
+      $form->{"discount_$i"} = 0;
+    }
+
+    my $amount =
+        $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"}) * $form->{"qty_$i"};
+    map { $form->{"${_}_base"} += $amount }                         (split / /, $form->{"taxaccounts_$i"});
+    map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
 
-  $form->{creditremaining} -= $amount;
+    $form->{creditremaining} -= $amount;
 
-  $form->{"runningnumber_$i"} = $i;
+    $form->{"runningnumber_$i"} = $i;
 
-  delete $form->{nextsub};
+    # format amounts
+    map {
+      $form->{"${_}_$i"} =
+          $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
+    } qw(sellprice lastcost qty) if $form->{item} ne 'assembly';
+    $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0) if $form->{item} ne 'assembly';
 
-  # format amounts
-  map {
-    $form->{"${_}_$i"} =
-      $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
-  } qw(sellprice listprice lastcost) if $form->{item} ne 'assembly';
+    delete $form->{nextsub};
+
+  }
 
   &display_form;
 
@@ -679,7 +726,7 @@ sub check_form {
     #$form->{sellprice} = 0;
     $form->{weight}    = 0;
     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
-      qw(listprice sellprice rop stock);
+      qw(sellprice rop stock);
 
     my @flds = qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup lastcost);
 
@@ -885,7 +932,7 @@ sub order {
   for my $i (1 .. $form->{rowcount}) {
     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"})
             if ($form->{"${_}_${i}"}) }
-        qw(ship qty sellprice listprice basefactor discount));
+        qw(ship qty sellprice basefactor discount));
     $form->{"converted_from_invoice_id_$i"} = delete $form->{"invoice_id_$i"};
   }
 
@@ -953,7 +1000,7 @@ sub quotation {
     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
                                                      $form->{"${_}_${i}"})
             if ($form->{"${_}_${i}"}) }
-        qw(ship qty sellprice listprice basefactor discount lastcost));
+        qw(ship qty sellprice basefactor discount lastcost));
   }
 
   &prepare_order;
@@ -1329,12 +1376,18 @@ sub print_form {
     $numberfld            = $form->{type} =~ /^sales/ ? 'sdonumber' : 'pdonumber';
     $form->{label}        = $form->{formname} eq 'pick_list' ? $locale->text('Pick List') : $locale->text('Delivery Order');
   }
+  if ($form->{type} =~ /letter/) {
+    undef $due;
+    undef $inv;
+    $form->{label}        = $locale->text('Letter');
+  }
 
   $form->{TEMPLATE_DRIVER_OPTIONS} = { };
-  if (any { $form->{type} eq $_ } qw(sales_quotation sales_order sales_delivery_order invoice request_quotation purchase_order purchase_delivery_order)) {
+  if (any { $form->{type} eq $_ } qw(sales_quotation sales_order sales_delivery_order invoice request_quotation purchase_order purchase_delivery_order credit_note)) {
     $form->{TEMPLATE_DRIVER_OPTIONS}->{variable_content_types} = {
       longdescription => 'html',
       partnotes       => 'html',
+      notes           => 'html',
     };
   }
 
@@ -1408,6 +1461,9 @@ sub print_form {
     DO->order_details(\%myconfig, \%$form);
   } elsif ($order) {
     OE->order_details(\%myconfig, \%$form);
+  } elsif ($form->{type} eq 'letter') {
+    # right now, no details are needed
+    # but i do not want to break the bad default (invoice)
   } else {
     IS->invoice_details(\%myconfig, \%$form, $locale);
   }
@@ -1769,8 +1825,7 @@ sub _update_part_information {
 
   my $form     = $main::form;
 
-  my %part_information = IC->get_basic_part_info('id'        => [ grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount}) ],
-                                                 'vendor_id' => $form->{vendor_id});
+  my %part_information = IC->get_basic_part_info('id' => [ grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount}) ]);
 
   $form->{PART_INFORMATION} = \%part_information;
 
@@ -2048,4 +2103,3 @@ sub _make_record {
 
   return $obj;
 }
-