]> wagnertech.de Git - mfinanz.git/blobdiff - bin/mozilla/is.pl
Zahlungsbedingungen: Flag »Fälligkeitsdatum automatisch berechnen«
[mfinanz.git] / bin / mozilla / is.pl
index 1f9dd084315a0ea980cc9e30b5e447eacdf07658..c330ec2f5220d0edfce9a6cd7def07715db2a060 100644 (file)
@@ -43,6 +43,7 @@ use English qw(-no_match_vars);
 
 use SL::DB::Default;
 use SL::DB::Customer;
+use SL::DB::PaymentTerm;
 
 require "bin/mozilla/io.pl";
 require "bin/mozilla/invoice_io.pl";
@@ -387,7 +388,10 @@ sub form_header {
   ), @custom_hiddens,
   map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
 
-  $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part));
+  $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part client_js));
+
+  $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
+  $form->{duedate}             = $TMPL_VAR{payment_terms_obj}->calc_date(reference_date => $form->{invdate}, due_date => $form->{due_due})->to_kivitendo if $TMPL_VAR{payment_terms_obj};
 
   $form->header();
 
@@ -554,11 +558,8 @@ sub update {
 
     my $rows = scalar @{ $form->{item_list} };
 
-    # Falls kein Kundenrabatt vorhanden ist, den aktuellen Rabatt nicht mit 0% überschreiben,
-    # da hier der Anwender schon manual einen Wert eingetragen haben könnte (analog zu qty) Bugfix: 1412
-    if ($form->{customer_discount}){
-      $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
-    }
+    $form->{"discount_$i"}   = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
+    $form->{"discount_$i"} ||= $form->{customer_discount};
 
     if ($rows) {
       $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
@@ -609,7 +610,7 @@ sub update {
 
         $form->{"listprice_$i"} /= $exchangerate;
 
-        my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
+        my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
@@ -618,7 +619,8 @@ sub update {
 
         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost);
 
-        $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
+        $form->{"qty_$i"}      = $form->format_amount(\%myconfig, $form->{"qty_$i"});
+        $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
       }
 
       &display_form;
@@ -780,6 +782,9 @@ sub post {
 
   relink_accounts();
 
+  my $terms        = get_payment_terms_for_invoice();
+  $form->{duedate} = $terms->calc_date(reference_date => $form->{invdate}, due_date => $form->{due_due})->to_kivitendo if $terms;
+
   # If transfer_out is requested, get rose db handle and do post and
   # transfer out in one transaction. Otherwise just post the invoice.
   if ($::instance_conf->get_is_transfer_out && $form->{type} ne 'credit_note' && !$form->{storno}) {
@@ -844,7 +849,7 @@ sub print_and_post {
 
   $main::auth->assert('invoice_edit');
 
-  my $old_form                    = new Form;
+  my $old_form                    = Form->new;
   $form->{no_redirect_after_post} = 1;
   $form->{print_and_post}         = 1;
   &post();
@@ -866,7 +871,8 @@ sub use_as_new {
   $form->{rowcount}--;
   $form->{paidaccounts} = 1;
   $form->{invdate}      = $form->current_date(\%myconfig);
-  $form->{duedate}      = $form->get_duedate(\%myconfig, $form->{invdate}) || $form->{invdate};
+  my $terms             = get_payment_terms_for_invoice();
+  $form->{duedate}      = $terms ? $terms->calc_date(reference_date => $form->{invdate})->to_kivitendo : $form->{invdate};
   $form->{employee_id}  = SL::DB::Manager::Employee->current->id;
   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
   $form->{exchangerate} = $form->{forex} if $form->{forex};
@@ -935,7 +941,7 @@ sub preview {
   $main::auth->assert('invoice_edit');
 
   $form->{preview} = 1;
-  my $old_form = new Form;
+  my $old_form = Form->new;
   for (keys %$form) { $old_form->{$_} = $form->{$_} }
 
   &print_form($old_form);
@@ -1021,11 +1027,6 @@ sub credit_note {
 #  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 #    qw(creditlimit creditremaining);
 
-  for my $i (1 .. $form->{rowcount}) {
-    for (qw(listprice)) {
-      $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
-    }
-  }
   # set new persistent ids for credit note and link previous invoice id
   $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};