Bei Eingangs-, Ausgangs-, Kreditoren- und Debitorenrechnungen unterhalb der Zahlungse...
[kivitendo-erp.git] / bin / mozilla / ap.pl
index 9c71519..24c9f59 100644 (file)
@@ -33,6 +33,7 @@
 
 use SL::AP;
 use SL::IR;
+use SL::IS;
 use SL::PE;
 
 require "bin/mozilla/arap.pl";
@@ -671,6 +672,7 @@ $jsscript
     ? $locale->text('Tax Included')
     : $locale->text('Tax');
 
+  $form->{invtotal_unformatted} = $form->{invtotal};
   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
 
   $APselected =
@@ -740,7 +742,9 @@ $jsscript
         </tr>
 ";
 
-  my @triggers = ();
+  my @triggers  = ();
+  my $totalpaid = 0;
+
   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
   for $i (1 .. $form->{paidaccounts}) {
     print "
@@ -754,6 +758,8 @@ $jsscript
                            '-labels' => \%AP_paid_labels,
                            '-default' => $form->{"AP_paid_$i"}));
 
+    $totalpaid += $form->{"paid_$i"};
+
     # format amounts
     if ($form->{"paid_$i"}) {
       $form->{"paid_$i"} =
@@ -804,7 +810,23 @@ $jsscript
 ";
     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
   }
-  print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
+
+  my $paid_missing = $form->{invtotal_unformatted} - $totalpaid;
+
+  print qq|
+        <tr>
+          <td></td>
+          <td></td>
+          <td align="center">| . $locale->text('Total') . qq|</td>
+          <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
+        </tr>
+        <tr>
+          <td></td>
+          <td></td>
+          <td align="center">| . $locale->text('Missing amount') . qq|</td>
+          <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
+        </tr>
+| . $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
     qq|
     <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
 
@@ -850,43 +872,30 @@ sub form_footer {
   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
-  print qq|<input class="submit" type="submit" name="action" id="update_button" value="|
-    . $locale->text('Update') . qq|">|;
+  # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
+  print qq|<input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|">|
+    if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ap') && !IS->is_storno(\%myconfig, $form, 'ap') && !$form->{paid_1});
 
-  if ($form->{id}) {
+  print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">|;
 
-    #     print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
-    # |;
+  if ($form->{id}) {
     if ($form->{radier}) {
-      print qq|
-       <input class=submit type=submit name=action value="|
-          . $locale->text('Post') . qq|">
-       <input class=submit type=submit name=action value="|
-          . $locale->text('Delete') . qq|">
+      print qq| <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|">
+                <input class=submit type=submit name=action value="| . $locale->text('Delete') . qq|">
 |;
     }
 
-    print qq|
-<input class=submit type=submit name=action value="|
-        . $locale->text('Use As Template') . qq|">
-<input class=submit type=submit name=action value="|
-        . $locale->text('Post Payment') . qq|">
+    print qq| <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|">
+              <input class=submit type=submit name=action value="| . $locale->text('Post Payment') . qq|">
 |;
   } elsif (($transdate > $closedto) && !$form->{id}) {
     print qq|
-      <input class=submit type=submit name=action value="|
-      . $locale->text('Post') . qq|"> | .
-      NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
-                       '-class' => 'submit'));
+      <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|"> | .
+      NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
   }
   # button for saving history
   if($form->{id} ne "") {
-    print qq|
-         <input type="button" class="submit" onclick="set_history_window(|
-         . $form->{id} 
-         . qq|);" name="history" id="history" value="|
-         . $locale->text('history') 
-         . qq|">|;
+    print qq| <input type="button" class="submit" onclick="set_history_window($form->{id});" name="history" id="history" value="| . $locale->text('history') . qq|">|;
   }
   # /button for saving history
   print "
@@ -1012,7 +1021,7 @@ sub post_payment {
 
   ($form->{AP})      = split /--/, $form->{AP};
   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
-  $form->redirect($locale->text(' Payment posted!'))
+  $form->redirect($locale->text('Payment posted!'))
       if (AP->post_payment(\%myconfig, \%$form));
     $form->error($locale->text('Cannot post payment!'));
 
@@ -1745,3 +1754,43 @@ sub ap_subtotal {
 
   $lxdebug->leave_sub();
 }
+
+sub storno {
+  $lxdebug->enter_sub();
+
+  if (IS->has_storno(\%myconfig, $form, 'ap')) {
+    $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
+    $form->error($locale->text("Transaction has already been cancelled!"));
+  }
+
+  # negate amount/taxes
+  for my $i (1 .. $form->{rowcount}) {
+    $form->{"amount_$i"} *= -1;
+    $form->{"tax_$i"}    *= -1; 
+  }
+
+  # format things
+  for my $i (1 .. $form->{rowcount}) {
+    for (qw(amount tax)) {
+      $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) if $form->{"${_}_$i"};
+    }
+  }
+
+  $form->{storno}      = 1;
+  $form->{storno_id}   = $form->{id};
+  $form->{id}          = 0;
+
+  $form->{invnumber}   = "Storno-" . $form->{invnumber};
+
+  post();
+
+  # saving the history
+  if(!exists $form->{addition} && $form->{id} ne "") {
+    $form->{snumbers} = "ordnumber_$form->{ordnumber}";
+    $form->{addition} = "STORNO";
+    $form->save_history($form->dbconnect(\%myconfig));
+  }
+  # /saving the history 
+
+  $lxdebug->leave_sub();
+}