Preisgruppen verbessert
[kivitendo-erp.git] / bin / mozilla / oe.pl
index e6ec1f9..0e469d8 100644 (file)
@@ -50,8 +50,7 @@ require "bin/mozilla/reportgenerator.pl";
 
 use strict;
 
-my $print_post;
-my %TMPL_VAR;
+our %TMPL_VAR;
 
 1;
 
@@ -283,6 +282,7 @@ sub prepare_order {
     $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
     $form->{"discount_$i"}  = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($format_discounts ? 100 : 1));
     $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
+    $form->{"lastcost_$i"}  = $form->format_amount(\%myconfig, $form->{"lastcost_$i"});
     $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"});
   }
 
@@ -300,8 +300,9 @@ sub form_header {
 
   check_oe_access();
 
-  # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
-  our %TMPL_VAR = ();
+  # Container for template variables. Unfortunately this has to be
+  # visible in form_footer too, so package local level and not my here.
+  %TMPL_VAR = ();
 
   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
@@ -518,12 +519,8 @@ sub update {
   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
   $form->{update} = 1;
 
-  my $payment_id = $form->{payment_id} if $form->{payment_id};
-
   &check_name($form->{vc});
 
-  $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
-
   my $buysell           = 'buy';
   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
@@ -557,13 +554,15 @@ sub update {
     # select discount as customer_discount from customer
     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"$form->{vc}_discount"} * 100);
 
+    $form->{"lastcost_$i"} = $form->parse_amount(\%myconfig, $form->{"lastcost_$i"});
+
     if ($rows) {
-      $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
+      $form->{"qty_$i"} = 1 unless ($form->parse_amount(\%myconfig, $form->{"qty_$i"}));
 
       if ($rows > 1) {
 
         &select_item;
-        exit;
+        ::end_of_request();
 
       } else {
 
@@ -602,6 +601,7 @@ sub update {
         $form->{creditremaining} -= $amount;
 
         $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
+        $form->{"lastcost_$i"}  = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
         $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
         # get pricegroups for parts
@@ -689,7 +689,10 @@ sub search {
 
   $form->header();
 
-  print $form->parse_html_template('oe/search', { %myconfig });
+  print $form->parse_html_template('oe/search', {
+    %myconfig,
+    is_order => $form->{type} =~ /_order/,
+  });
 
   $main::lxdebug->leave_sub();
 }
@@ -790,7 +793,7 @@ sub orders {
   my %column_defs = (
     'ids'                     => { 'text' => '', },
     'transdate'               => { 'text' => $locale->text('Date'), },
-    'reqdate'                 => { 'text' => $locale->text('Required by'), },
+    'reqdate'                 => { 'text' => $form->{type} =~ /_order/ ? $locale->text('Required by') : $locale->text('Valid until') },
     'id'                      => { 'text' => $locale->text('ID'), },
     'ordnumber'               => { 'text' => $locale->text('Order'), },
     'quonumber'               => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
@@ -861,6 +864,7 @@ sub orders {
                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
     );
   $report->set_options_from_form();
+  $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
   # add sort and escape callback, this one we use for the add sub
   $form->{callback} = $href .= "&sort=$form->{sort}";
@@ -1006,7 +1010,7 @@ sub save_and_close {
       $form->{payment_id} = $payment_id;
     }
     &update;
-    exit;
+    ::end_of_request();
   }
 
   $form->{id} = 0 if $form->{saveasnew};
@@ -1060,7 +1064,7 @@ sub save_and_close {
   if(!exists $form->{addition}) {
     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
     $form->{addition} = "SAVED";
-    $form->save_history($form->dbconnect(\%myconfig));
+    $form->save_history;
   }
   # /saving the history
 
@@ -1101,6 +1105,7 @@ sub save {
   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
     if ($form->{currency} ne $form->{defaultcurrency});
 
+  remove_emptied_rows();
   &validate_items;
 
   my $payment_id;
@@ -1114,7 +1119,7 @@ sub save {
       $form->{payment_id} = $payment_id;
     }
     &update;
-    exit;
+    ::end_of_request();
   }
 
   $form->{id} = 0 if $form->{saveasnew};
@@ -1167,7 +1172,7 @@ sub save {
   if(!exists $form->{addition}) {
     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
     $form->{addition} = "SAVED";
-    $form->save_history($form->dbconnect(\%myconfig));
+    $form->save_history;
   }
   # /saving the history
 
@@ -1175,7 +1180,7 @@ sub save {
   if(!$form->{print_and_save}) {
     delete @{$form}{ary_diff([keys %{ $form }], [qw(login stylesheet id script type cursor_fokus)])};
     edit();
-    exit;
+    ::end_of_request();
   }
   $main::lxdebug->leave_sub();
 }
@@ -1255,11 +1260,11 @@ sub delete_order_quotation {
     if(!exists $form->{addition}) {
       $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
       $form->{addition} = "DELETED";
-      $form->save_history($form->dbconnect(\%myconfig));
+      $form->save_history;
     }
     # /saving the history
     $form->info($msg);
-    exit();
+    ::end_of_request();
   }
   $form->error($err);
 
@@ -1305,7 +1310,7 @@ sub invoice {
   if (&check_name($form->{vc})) {
     $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
     &update;
-    exit;
+    ::end_of_request();
   }
 
   $form->{cp_id} *= 1;
@@ -1328,7 +1333,7 @@ sub invoice {
 
     if (!$exchangerate) {
       &backorder_exchangerate($orddate, $buysell);
-      exit;
+      ::end_of_request();
     }
   }
 
@@ -1414,6 +1419,10 @@ sub invoice {
 
   }
 
+  #  show pricegroup in newly loaded invoice when creating invoice from quotation/order
+  IS->get_pricegroups_for_parts(\%myconfig, \%$form);
+  set_pricegroup($_) for 1 .. $form->{rowcount};
+
   &display_form;
 
   $main::lxdebug->leave_sub();
@@ -1676,7 +1685,7 @@ sub check_for_direct_delivery {
 
   $main::lxdebug->leave_sub();
 
-  exit 0;
+  ::end_of_request();
 }
 
 sub purchase_order {
@@ -1760,7 +1769,7 @@ sub poso {
   # reset
   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered ordnumber);
 
-  # if purchase_order was generated from sales_order, use lastcost_$i as sellprice_$i
+  # if purchase_order was generated from sales_order, use  lastcost_$i as sellprice_$i
   if ( $form->{sales_order_to_purchase_order} ) {
     for my $i (1 .. $form->{rowcount}) {
       $form->{"sellprice_${i}"} = $form->format_amount(\%myconfig,$form->{"lastcost_${i}"});
@@ -1787,12 +1796,6 @@ sub poso {
   };
 
   &prepare_order;
-
-  # format amounts
-  for my $i (1 .. $form->{rowcount} - 1) {
-    map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
-  }
-
   &update;
 
   $main::lxdebug->leave_sub();
@@ -1834,10 +1837,10 @@ sub delivery_order {
   delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
 
   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);
+    map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice lastcost basefactor discount);
   }
 
-  my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
+  my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor shipto_id);
 
   order_links();
 
@@ -1859,8 +1862,6 @@ sub e_mail {
 
   $form->{print_and_save} = 1;
 
-  $print_post = 1;
-
   my $saved_form = save_form();
 
   save();
@@ -1900,9 +1901,7 @@ sub display_form {
   }
   $form->{"taxaccounts"} = "";
 
-  for my $i (1 .. $form->{"rowcount"}) {
-    IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
-  }
+  IC->retrieve_accounts(\%myconfig, $form, map { $_ => $form->{"id_$_"} } 1 .. $form->{rowcount});
 
   $form->{rowcount}++;
   $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
@@ -1941,3 +1940,17 @@ sub report_for_todo_list {
   return $content;
 }
 
+sub dispatcher {
+  my $form     = $main::form;
+  my $locale   = $main::locale;
+
+  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)) {
+    if ($form->{"action_${action}"}) {
+      call_sub($action);
+      return;
+    }
+  }
+
+  $form->error($locale->text('No action defined.'));
+}