Aufrufe von 'exit' durch eigene Funktion '::end_of_request()' ersetzt.
[kivitendo-erp.git] / bin / mozilla / io.pl
index 2047ceb..cd01926 100644 (file)
@@ -159,6 +159,7 @@ sub display_row {
   );
   my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
 
+
   # cache units
   my $all_units       = AM->retrieve_units(\%myconfig, $form);
 
@@ -209,7 +210,9 @@ sub display_row {
     my %column_data = ();
 
     # undo formatting
-    map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(qty discount sellprice price_new price_old) unless ($form->{simple_save});
+    map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } 
+      qw(qty discount sellprice lastcost price_new price_old) 
+        unless ($form->{simple_save});
 
 # unit begin
     $form->{"unit_old_$i"}      ||= $form->{"unit_$i"};
@@ -294,6 +297,7 @@ sub display_row {
       }
       $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));
+
     }
     $column_data{discount}    = $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
     $column_data{linetotal}   = $form->format_amount(\%myconfig, $linetotal, 2);
@@ -317,28 +321,31 @@ sub display_row {
                    $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
 
 # begin marge calculations
-    my $marge_color;
-    my $real_sellprice     = ($form->{"sellprice_$i"} - $discount) / $price_factor;
-    my $marge_price_factor = $form->{"marge_price_factor_$i"} * 1 || 1;
-
-    $form->{"lastcost_$i"} *= 1;
+    $form->{"lastcost_$i"}     *= 1;
     $form->{"marge_percent_$i"} = 0;
 
-    if ($real_sellprice && ($form->{"qty_$i"} * 1)) {
-      $form->{"marge_percent_$i"}     = ($real_sellprice - $form->{"lastcost_$i"} / $marge_price_factor) * 100 / $real_sellprice;
-      $myconfig{marge_percent_warn} ||= 15;
-      $marge_color                    = 'color="#ff0000"' if $form->{"id_$i"} && ($form->{"marge_percent_$i"} < (1 * $myconfig{marge_percent_warn}));
+    my $marge_color;
+    my $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;
+
+    if ($real_sellprice * 1 && ($form->{"qty_$i"} * 1)) {
+      $form->{"marge_percent_$i"} = ($real_sellprice - $real_lastcost) * 100 / $real_sellprice;
+      $marge_color                = 'color="#ff0000"' if $form->{"id_$i"} && $form->{"marge_percent_$i"} < $marge_percent_warn;
     }
 
-    my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
-    $form->{"marge_absolut_$i"}  = ($real_sellprice - $form->{"lastcost_$i"} / $marge_price_factor) * $form->{"qty_$i"} * $marge_adjust_credit_note;
+    $form->{"marge_absolut_$i"}  = ($real_sellprice - $real_lastcost) * $marge_adjust_credit_note;
     $form->{"marge_total"}      += $form->{"marge_absolut_$i"};
-    $form->{"lastcost_total"}   += $form->{"lastcost_$i"} * $form->{"qty_$i"} / $marge_price_factor;
-    $form->{"sellprice_total"}  += $real_sellprice * $form->{"qty_$i"};
+    $form->{"lastcost_total"}   += $real_lastcost;
+    $form->{"sellprice_total"}  += $real_sellprice;
 
     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
 
-    push @ROW2, { value => sprintf qq|<font %s><b>%s</b> %s &nbsp;%s%% </font> &nbsp;<b>%s</b> %s &nbsp;<b>%s</b> %s|,
+    push @ROW2, { value => sprintf qq|
+         <font %s><b>%s</b> %s &nbsp;%s%% </font> 
+        &nbsp;<b>%s</b> %s 
+        &nbsp;<b>%s</b> <input size="5" name="lastcost_$i" value="%s">|,
                    $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
                    $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
                    $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, 2) }
@@ -361,8 +368,8 @@ sub display_row {
     my @hidden_vars;
 
     if ($is_delivery_order) {
-      map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount);
-      push @hidden_vars, qw(sellprice discount price_factor_id);
+      map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
+      push @hidden_vars, qw(sellprice discount price_factor_id lastcost);
       push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
     }
 
@@ -372,7 +379,7 @@ sub display_row {
           map { ($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } map { $_."_$i" }
             (qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
                 income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
-                longdescription basefactor marge_absolut marge_percent marge_price_factor lastcost), @hidden_vars)
+                longdescription basefactor marge_absolut marge_percent marge_price_factor), @hidden_vars)
     );
 
     map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
@@ -668,7 +675,7 @@ sub item_selected {
   map {
     $form->{"${_}_$i"} =
       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
-  } qw(sellprice listprice) if $form->{item} ne 'assembly';
+  } qw(sellprice listprice lastcost) if $form->{item} ne 'assembly';
 
   # get pricegroups for parts
   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
@@ -750,7 +757,7 @@ sub check_form {
         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
 
         # fuer assemblies auskommentiert. siehe oben
-       #    $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"} / ($form->{"price_factor_$i"} || 1));
+        #    $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"} / ($form->{"price_factor_$i"} || 1));
         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"} / ($form->{"price_factor_$i"} || 1));
         $count++;
       }
@@ -765,44 +772,9 @@ sub check_form {
     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
 
   } else {
-    my @flds = qw(id partnumber description qty ship sellprice unit
-                  discount inventory_accno income_accno expense_accno listprice
-                  taxaccounts bin assembly weight projectnumber project_id
-                  oldprojectnumber runningnumber serialnumber partsgroup payment_id
-                  not_discountable shop ve gv buchungsgruppen_id language_values
-                  sellprice_pg pricegroup_old price_old price_new unit_old ordnumber
-                  transdate longdescription basefactor marge_total marge_percent
-                  marge_price_factor lastcost price_factor_id partnotes
-                  stock_out stock_in has_sernumber);
-
-    my $ic_cvar_configs = CVar->get_configs(module => 'IC');
-    push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
-
-    # this section applies to invoices and orders
-    # remove any empty numbers
-    if ($form->{rowcount}) {
-      for my $i (1 .. $form->{rowcount} - 1) {
-        if ($form->{"partnumber_$i"}) {
-          push @a, {};
-          my $j = $#a;
-
-          map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
-          $count++;
-          if ($main::lizenzen) {
-            if ($form->{"licensenumber_$i"} == -1) {
-              &new_license($i);
-              exit;
-            }
-          }
-        }
-      }
+    remove_emptied_rows(1);
 
-      $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
-      $form->{rowcount} = $count;
-
-      $form->{creditremaining} -= &invoicetotal;
-
-    }
+    $form->{creditremaining} -= &invoicetotal;
   }
 
   #sk
@@ -825,6 +797,37 @@ sub check_form {
   $main::lxdebug->leave_sub();
 }
 
+sub remove_emptied_rows {
+  my $dont_add_empty = shift;
+  my $form           = $::form;
+
+  return unless $form->{rowcount};
+
+  my @flds = qw(id partnumber description qty ship sellprice unit
+                discount inventory_accno income_accno expense_accno listprice
+                taxaccounts bin assembly weight projectnumber project_id
+                oldprojectnumber runningnumber serialnumber partsgroup payment_id
+                not_discountable shop ve gv buchungsgruppen_id language_values
+                sellprice_pg pricegroup_old price_old price_new unit_old ordnumber
+                transdate longdescription basefactor marge_total marge_percent
+                marge_price_factor lastcost price_factor_id partnotes
+                stock_out stock_in has_sernumber);
+
+  my $ic_cvar_configs = CVar->get_configs(module => 'IC');
+  push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
+
+  my @new_rows;
+  for my $i (1 .. $form->{rowcount} - 1) {
+    next unless $form->{"partnumber_$i"};
+
+    push @new_rows, { map { $_ => $form->{"${_}_$i" } } @flds };
+  }
+
+  my $new_rowcount = scalar @new_rows;
+  $form->redo_rows(\@flds, \@new_rows, $new_rowcount, $form->{rowcount});
+  $form->{rowcount} = $new_rowcount + ($dont_add_empty ? 0 : 1);
+}
+
 sub invoicetotal {
   $main::lxdebug->enter_sub();
 
@@ -879,7 +882,7 @@ sub validate_items {
   # check if items are valid
   if ($form->{rowcount} == 1) {
     &update;
-    exit;
+    ::end_of_request();
   }
 
   for my $i (1 .. $form->{rowcount} - 1) {
@@ -1181,7 +1184,9 @@ sub print_options {
     (!$options{no_html}) ?
       opthash("html", $form->{DF}{html}, "HTML") : undef,
     ($main::opendocument_templates && !$options{no_opendocument}) ?
-      opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef;
+      opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef,
+    ($main::excel_templates && !$options{no_excel}) ?
+      opthash("excel",               $form->{DF}{excel},               $locale->text("Excel")) : undef;
 
   push @LANGUAGE_ID,
     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
@@ -1208,7 +1213,7 @@ sub print_options {
     );
 
   my %template_vars = (
-    display_copies       => scalar @{ $form->{printers} } && $main::latex_templates && $form->{media} ne 'email',
+    display_copies       => scalar @{ $form->{printers} || [] } && $main::latex_templates && $form->{media} ne 'email',
     display_remove_draft => (!$form->{id} && $form->{draft_id}),
     display_groupitems   => !$dont_display_groupitems{$form->{type}},
     groupitems_checked   => $form->{groupitems} ? "checked" : '',
@@ -1260,7 +1265,7 @@ sub print {
     &save();
     $form->{formname} = $formname;
     &edit();
-    exit;
+    ::end_of_request();
   }
 
   &print_form($old_form);
@@ -1421,13 +1426,13 @@ sub print_form {
 
       call_sub($display_form);
       # saving the history
-         if(!exists $form->{addition}) {
+      if(!exists $form->{addition}) {
         $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
-           $form->{addition} = "PRINTED";
-           $form->save_history($form->dbconnect(\%myconfig));
+        $form->{addition} = "PRINTED";
+        $form->save_history($form->dbconnect(\%myconfig));
       }
       # /saving the history
-      exit;
+      ::end_of_request();
     }
   }
 
@@ -1589,6 +1594,9 @@ sub print_form {
   } elsif ($form->{"format"} =~ /opendocument/) {
     $form->{opendocument} = 1;
     $extension            = 'odt';
+  } elsif ($form->{"format"} =~ /excel/) {
+    $form->{excel} = 1;
+    $extension            = 'xls';
   }
 
   my $email_extension = '_email' if (($form->{media} eq 'email') && (-f "$myconfig{templates}/$form->{formname}_email$form->{language}${printer_code}.${extension}"));
@@ -1641,16 +1649,16 @@ sub print_form {
   if(!exists $form->{addition}) {
     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
     if($form->{media} =~ /printer/) {
-       $form->{addition} = "PRINTED";
+      $form->{addition} = "PRINTED";
     }
     elsif($form->{media} =~ /email/) {
-       $form->{addition} = "MAILED";
+      $form->{addition} = "MAILED";
     }
     elsif($form->{media} =~ /queue/) {
-       $form->{addition} = "QUEUED";
+      $form->{addition} = "QUEUED";
     }
     elsif($form->{media} =~ /screen/) {
-       $form->{addition} = "SCREENED";
+      $form->{addition} = "SCREENED";
     }
     $form->save_history($form->dbconnect(\%myconfig));
   }
@@ -1697,7 +1705,7 @@ sub print_form {
       }
 
       call_sub($display_form);
-      exit;
+      ::end_of_request();
     }
 
     my $msg =
@@ -1708,7 +1716,7 @@ sub print_form {
   }
   if ($form->{printing}) {
    call_sub($display_form);
-   exit;
+   ::end_of_request();
   }
 
   $main::lxdebug->leave_sub();
@@ -1809,72 +1817,72 @@ sub ship_to {
   <tr>
     <td>
       <table>
-       <tr class="listheading">
-         <th class="listheading" colspan="2" width="50%">|
+        <tr class="listheading">
+          <th class="listheading" colspan="2" width="50%">|
     . $locale->text('Billing Address') . qq|</th>
-         <th class="listheading" width="50%">|
+          <th class="listheading" width="50%">|
     . $locale->text('Shipping Address') . qq|</th>
-       </tr>
-       <tr height="5"></tr>
-       <tr>
-         <th align="right" nowrap>$number</th>
-         <td>$form->{"$form->{vc}number"}</td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('Company Name') . qq|</th>
-         <td>$form->{name}</td>
-         <td><input name="shiptoname" size="35" value="$form->{shiptoname}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
-         <td>$form->{department_1}</td>
-         <td><input name="shiptodepartment_1" size="35" value="$form->{shiptodepartment_1}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>&nbsp;</th>
-         <td>$form->{department_2}</td>
-         <td><input name="shiptodepartment_2" size="35" value="$form->{shiptodepartment_2}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('Street') . qq|</th>
-         <td>$form->{street}</td>
-         <td><input name="shiptostreet" size="35" value="$form->{shiptostreet}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('Zipcode') . qq|</th>
-         <td>$form->{zipcode}</td>
-         <td><input name="shiptozipcode" size="35" value="$form->{shiptozipcode}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('City') . qq|</th>
-         <td>$form->{city}</td>
-         <td><input name="shiptocity" size="35" value="$form->{shiptocity}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('Country') . qq|</th>
-         <td>$form->{country}</td>
-         <td><input name="shiptocountry" size="35" value="$form->{shiptocountry}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('Contact') . qq|</th>
-         <td>$form->{contact}</td>
-         <td><input name="shiptocontact" size="35" value="$form->{shiptocontact}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('Phone') . qq|</th>
-         <td>$form->{phone}</td>
-         <td><input name="shiptophone" size="20" value="$form->{shiptophone}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('Fax') . qq|</th>
-         <td>$form->{fax}</td>
-         <td><input name="shiptofax" size="20" value="$form->{shiptofax}"></td>
-       </tr>
-       <tr>
-         <th align="right" nowrap>| . $locale->text('E-mail') . qq|</th>
-         <td>$form->{email}</td>
-         <td><input name="shiptoemail" size="35" value="$form->{shiptoemail}"></td>
-       </tr>
+        </tr>
+        <tr height="5"></tr>
+        <tr>
+          <th align="right" nowrap>$number</th>
+          <td>$form->{"$form->{vc}number"}</td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('Company Name') . qq|</th>
+          <td>$form->{name}</td>
+          <td><input name="shiptoname" size="35" value="$form->{shiptoname}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
+          <td>$form->{department_1}</td>
+          <td><input name="shiptodepartment_1" size="35" value="$form->{shiptodepartment_1}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>&nbsp;</th>
+          <td>$form->{department_2}</td>
+          <td><input name="shiptodepartment_2" size="35" value="$form->{shiptodepartment_2}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('Street') . qq|</th>
+          <td>$form->{street}</td>
+          <td><input name="shiptostreet" size="35" value="$form->{shiptostreet}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('Zipcode') . qq|</th>
+          <td>$form->{zipcode}</td>
+          <td><input name="shiptozipcode" size="35" value="$form->{shiptozipcode}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('City') . qq|</th>
+          <td>$form->{city}</td>
+          <td><input name="shiptocity" size="35" value="$form->{shiptocity}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('Country') . qq|</th>
+          <td>$form->{country}</td>
+          <td><input name="shiptocountry" size="35" value="$form->{shiptocountry}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('Contact') . qq|</th>
+          <td>$form->{contact}</td>
+          <td><input name="shiptocontact" size="35" value="$form->{shiptocontact}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('Phone') . qq|</th>
+          <td>$form->{phone}</td>
+          <td><input name="shiptophone" size="20" value="$form->{shiptophone}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('Fax') . qq|</th>
+          <td>$form->{fax}</td>
+          <td><input name="shiptofax" size="20" value="$form->{shiptofax}"></td>
+        </tr>
+        <tr>
+          <th align="right" nowrap>| . $locale->text('E-mail') . qq|</th>
+          <td>$form->{email}</td>
+          <td><input name="shiptoemail" size="35" value="$form->{shiptoemail}"></td>
+        </tr>
       </table>
     </td>
   </tr>
@@ -1885,7 +1893,7 @@ sub ship_to {
 
 
   # delete shipto
-  map({ delete $form->{$_} } (@shipto_vars, qw(header)));
+  map({ delete $form->{$_} } (@shipto_vars, qw(header shipto_id)));
   $form->{title} = $title;
 
   foreach my $key (keys %$form) {
@@ -2077,7 +2085,7 @@ sub _update_custom_variables {
 }
 
 sub _render_custom_variables_inputs {
-  $main::lxdebug->enter_sub();
+  $main::lxdebug->enter_sub(2);
 
   my $form     = $main::form;
 
@@ -2114,5 +2122,5 @@ sub _render_custom_variables_inputs {
                              };
   }
 
-  $main::lxdebug->leave_sub();
+  $main::lxdebug->leave_sub(2);
 }