Bug 1756 - Ertrag in Bruttorechnung gefixed
[kivitendo-erp.git] / bin / mozilla / io.pl
index c569ef8..1805843 100644 (file)
@@ -38,7 +38,6 @@
 
 use Carp;
 use CGI;
-use CGI::Ajax;
 use List::Util qw(min max first);
 
 use SL::CVar;
@@ -47,6 +46,9 @@ use SL::CT;
 use SL::IC;
 use SL::IO;
 
+use SL::DB::Language;
+use SL::DB::Printer;
+
 require "bin/mozilla/common.pl";
 
 use strict;
@@ -117,7 +119,7 @@ sub display_row {
 
   my $numrows = shift;
 
-  my ($readonly, $stock_in_out, $stock_in_out_title);
+  my ($stock_in_out, $stock_in_out_title);
 
   my $is_purchase        = (first { $_ eq $form->{type} } qw(request_quotation purchase_order purchase_delivery_order)) || ($form->{script} eq 'ir.pl');
   my $show_min_order_qty =  first { $_ eq $form->{type} } qw(request_quotation purchase_order);
@@ -125,8 +127,6 @@ sub display_row {
   my $is_s_p_order       = (first { $_ eq $form->{type} } qw(sales_order purchase_order));
 
   if ($is_delivery_order) {
-    $readonly             = ' readonly' if ($form->{closed});
-
     if ($form->{type} eq 'sales_delivery_order') {
       $stock_in_out_title = $locale->text('Release From Stock');
       $stock_in_out       = 'out';
@@ -278,12 +278,16 @@ sub display_row {
     }
 
     # build in drop down list for pricesgroups
+    # $sellprice_value setzt den Wert etwas unabhängiger von der Darstellung.
+    # Hintergrund: Preisgruppen werden hier überprüft und neu berechnet.
+    # Vorher wurde der ganze cgi->textfield Block zweimal identisch eingebaut, dass passiert
+    # jetzt nach der Abfrage.
+    my $sellprice_value;
     if ($form->{"prices_$i"}) {
       $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i" style="width: 8em">$form->{"prices_$i"}</select>|;
-      $column_data{sellprice}    = $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => 'check_right_number_format(this)', -value =>
-                                   (($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
+      $sellprice_value           =($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
                                       ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
-                                      : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces)));
+                                      : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
     } else {
       # for last row and report
       # set pricegroup drop down list from report menu
@@ -296,11 +300,22 @@ sub display_row {
       } else {
         $column_data{sellprice_pg} = qq|&nbsp;|;
       }
-      $column_data{sellprice} = $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value =>
-                                                $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces));
+      $sellprice_value = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
     }
-    $column_data{discount}    = $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
+    # Falls der Benutzer die Preise nicht anpassen sollte, wird das entsprechende
+    # Textfield auf readonly gesetzt. Anm. von Sven: Manipulation der Preise ist
+    # immer noch möglich, konsequenterweise sollten diese NUR aus der Datenbank
+    # geholt werden.
+    my $edit_prices = $main::auth->assert('edit_prices', 1);
+    $column_data{sellprice} = (!$edit_prices)
+                                ? $cgi->textfield(-readonly => "readonly",
+                                                  -name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value)
+                                : $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value);
+    $column_data{discount}    = (!$edit_prices)
+                                  ? $cgi->textfield(-readonly => "readonly",
+                                                    -name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}))
+                                  : $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
     $column_data{linetotal}   = $form->format_amount(\%myconfig, $linetotal, 2);
     $column_data{bin}         = $form->{"bin_$i"};
 
@@ -326,7 +341,19 @@ sub display_row {
     $form->{"marge_percent_$i"} = 0;
 
     my $marge_color;
-    my $real_sellprice           = $linetotal;
+    my $real_sellprice;
+    if ( $form->{taxincluded} and $form->{"qty_$i"} * 1  and $form->{$form->{"taxaccounts_$i"} . "_rate"} * 1) {
+      # if we use taxincluded we need to calculate the marge from the net_value
+      # all the marge calculations are based on linetotal which we need to
+      # convert to net first
+
+      # there is no direct form value for the tax_rate of the item, but
+      # form->{taxaccounts_$i} gives the tax account (e.g. 3806) and 3806_rate
+      # gives the tax percentage (e.g. 0.19)
+      $real_sellprice = $linetotal / (1 + $form->{$form->{"taxaccounts_$i"} . "_rate"});
+    } else {
+      $real_sellprice            = $linetotal;
+    };
     my $real_lastcost            = $form->{"lastcost_$i"} * $form->{"qty_$i"} / ( $form->{"marge_price_factor_$i"} || 1 );
     my $marge_percent_warn       = $myconfig{marge_percent_warn} * 1 || 15;
     my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
@@ -528,6 +555,11 @@ sub item_selected {
       $form->{"sellprice_$i"} =
         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
     }
+
+    # tradediscount
+    if ($::form->{tradediscount}) {
+      $::form->{"sellprice_$i"} *= 1 - $::form->{tradediscount};
+    }
   }
 
   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
@@ -952,7 +984,7 @@ sub edit_e_mail {
                                      subject       => $subject,
                                      print_options => print_options('inline' => 1),
                                      HIDDEN        => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
-                                     SHOW_BCC      => $myconfig{role} eq 'admin' });
+                                     SHOW_BCC      => $::auth->assert('email_bcc', 'may fail') });
 
   $main::lxdebug->leave_sub();
 }
@@ -1313,12 +1345,14 @@ sub print_form {
   my $salesman_id_saved = $form->{salesman_id};
   my $cp_id_saved = $form->{cp_id};
   my $taxzone_id_saved = $form->{taxzone_id};
+  my $currency_saved = $form->{currency};
 
   call_sub("$form->{vc}_details") if ($form->{vc});
 
   $form->{language_id} = $language_saved;
   $form->{payment_id} = $payment_id_saved;
   $form->{taxzone_id} = $taxzone_id_saved;
+  $form->{currency} = $currency_saved;
 
   $form->{"email"} = $saved_email if ($saved_email);
   $form->{"cc"}    = $saved_cc    if ($saved_cc);
@@ -1461,17 +1495,33 @@ sub print_form {
     $extension            = 'xls';
   }
 
-  my $email_extension = (($form->{media} eq 'email') && (-f "$myconfig{templates}/$form->{formname}_email$form->{language}${printer_code}.${extension}")) ? '_email' : '';
+  # search for the template
+  my @template_files;
+  push @template_files, "$form->{formname}_email$form->{language}$printer_code.$extension" if $form->{media} eq 'email';
+  push @template_files, "$form->{formname}$form->{language}$printer_code.$extension";
+  push @template_files, "$form->{formname}.$extension";
+  push @template_files, "default.$extension";
 
-  $form->{IN}         = "$form->{formname}${email_extension}$form->{language}${printer_code}.${extension}";
+  $form->{IN} = undef;
+  for my $filename (@template_files) {
+    if (-f "$myconfig{templates}/$filename") {
+      $form->{IN} = $filename;
+      last;
+    }
+  }
+
+  if (!defined $form->{IN}) {
+    $::form->error($::locale->text('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files));
+  }
 
   delete $form->{OUT};
 
   if ($form->{media} eq 'printer') {
     #$form->{OUT} = "| $form->{printer_command} &>/dev/null";
-    $form->{OUT} = "| $form->{printer_command} ";
+    $form->{OUT}      = $form->{printer_command};
+    $form->{OUT_MODE} = '|-';
     $form->{printed} .= " $form->{formname}";
-    $form->{printed} =~ s/^ //;
+    $form->{printed}  =~ s/^ //;
   }
   my $printed = $form->{printed};
 
@@ -1498,7 +1548,8 @@ sub print_form {
     }
 
     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
-    $form->{OUT} = ">" . $::lx_office_conf{paths}->{spool} . "/$filename";
+    $form->{OUT} = $::lx_office_conf{paths}->{spool} . "/$filename";
+    $form->{OUT_MODE} = '>';
 
     # add type
     $form->{queued} .= " $form->{formname} $filename";
@@ -1526,6 +1577,16 @@ sub print_form {
   }
   # /saving the history
 
+  # prepare meta information for template introspection
+  $form->{template_meta} = {
+    formname  => $form->{formname},
+    language  => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id}),
+    format    => $form->{format},
+    media     => $form->{media},
+    extension => $extension,
+    printer   => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id}),
+  };
+
   $form->parse_template(\%myconfig);
 
   $form->{callback} = "";