Mahnwesen: Die Tabelle dunning so umgebaut, dass gemeinsam gestartete Mahnungen auch...
[kivitendo-erp.git] / SL / IS.pm
index dd3eaf4..310dcbb 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -382,8 +382,12 @@ sub invoice_details {
       $form->{paid} += $form->parse_amount($myconfig, $form->{"paid_$i"});
     }
   }
-
-  $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
+  if($form->{taxincluded}) {
+    $form->{subtotal} = $form->format_amount($myconfig, $form->{total} - $tax, 2);
+  }
+  else {
+    $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
+  }
   $yesdiscount = $form->{nodiscount_total} - $nodiscount;
   $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
   $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
@@ -920,23 +924,25 @@ sub post_invoice {
   
         if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
-                      transdate, project_id)
+                      transdate, taxkey, project_id)
                       VALUES ($form->{id}, (SELECT c.id FROM chart c
-                                          WHERE c.accno = '$form->{AR}'),
-                      $amount, '$form->{"datepaid_$i"}', ?)|;
-          do_query($form, $dbh, $query, $project_id);
+                                          WHERE c.accno = ?),
+                      $amount, '$form->{"datepaid_$i"}',
+                      (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
+          do_query($form, $dbh, $query, $form->{AR}, $form->{AR}, $project_id);
         }
   
         # record payment
         $form->{"paid_$i"} *= -1;
   
         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
-                    source, memo, project_id)
+                    source, memo, taxkey, project_id)
                     VALUES ($form->{id}, (SELECT c.id FROM chart c
-                                        WHERE c.accno = '$accno'),
+                                        WHERE c.accno = ?),
                     $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
-                    '$form->{"source_$i"}', '$form->{"memo_$i"}', ?)|;
-        do_query($form, $dbh, $query, $project_id);
+                    '$form->{"source_$i"}', '$form->{"memo_$i"}',
+                    (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
+        do_query($form, $dbh, $query, $accno, $accno, $project_id);
   
         # exchangerate difference
         $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
@@ -976,12 +982,13 @@ sub post_invoice {
         ) {
 
         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
-                   transdate, cleared, fx_transaction, project_id)
+                   transdate, cleared, fx_transaction, taxkey, project_id)
                    VALUES ($form->{id},
                           (SELECT c.id FROM chart c
-                           WHERE c.accno = '$accno'),
-                   $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1', ?)|;
-        do_query($form, $dbh, $query, $project_id);
+                           WHERE c.accno = ?),
+                   $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1',
+                    (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
+        do_query($form, $dbh, $query, $accno, $accno, $project_id);
       }
     }
   }
@@ -1047,6 +1054,7 @@ Message: $form->{message}\r| if $form->{message};
               delivery_customer_id = $form->{delivery_customer_id},
               delivery_vendor_id = $form->{delivery_vendor_id},
               employee_id = $form->{employee_id},
+              salesman_id = | . conv_i($form->{salesman_id}, 'NULL') . qq|,
               storno = '$form->{storno}',
               globalproject_id = | . conv_i($form->{"globalproject_id"}, 'NULL') . qq|,
               cp_id = | . conv_i($form->{"cp_id"}, 'NULL') . qq|
@@ -1479,7 +1487,7 @@ sub retrieve_invoice {
                 a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate,
                 a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id,
                a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id,
-               a.employee_id, e.name AS employee, a.payment_id, a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type
+               a.employee_id, e.name AS employee, a.salesman_id, a.payment_id, a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type
                FROM ar a
                LEFT JOIN employee e ON (e.id = a.employee_id)
                WHERE a.id = $form->{id}|;
@@ -1682,49 +1690,29 @@ sub get_customer {
                  c.email, c.cc, c.bcc, c.language_id, c.payment_id AS customer_payment_id,
                 c.street, c.zipcode, c.city, c.country,
                 $duedate + COALESCE(pt.terms_netto, 0) AS duedate, c.notes AS intnotes,
-                b.discount AS tradediscount, b.description AS business, c.klass as customer_klass, c.taxzone_id
+                b.discount AS tradediscount, b.description AS business, c.klass as customer_klass, c.taxzone_id,
+                 c.salesman_id
                  FROM customer c
                 LEFT JOIN business b ON (b.id = c.business_id)
                  LEFT JOIN payment_terms pt ON c.payment_id = pt.id
-                WHERE c.id = $form->{customer_id}|;
-  $query =~ s/[\n\t]/ /g;
-  $query =~ s/ +/ /g;
-  $main::lxdebug->message(1, "qq $query");
-  my $sth = $dbh->prepare($query);
-  $sth->execute || $form->dberror($query);
-
-  $ref = $sth->fetchrow_hashref(NAME_lc);
-
+                WHERE c.id = ?|;
+  $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
   map { $form->{$_} = $ref->{$_} } keys %$ref;
-  $sth->finish;
-
-  my $query = qq|SELECT sum(a.amount-a.paid) AS dunning_amount FROM ar a WHERE a.paid < a.amount AND a.customer_id=$form->{customer_id} AND a.dunning_id IS NOT NULL|;
-  my $sth = $dbh->prepare($query);
-
-  $sth->execute || $form->dberror($query);
-  
-  $ref = $sth->fetchrow_hashref(NAME_lc);
 
+  my $query = qq|SELECT sum(a.amount - a.paid) AS dunning_amount FROM ar a 
+                 WHERE a.paid < a.amount AND a.customer_id = ? AND a.dunning_config_id IS NOT NULL|;
+  $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
   map { $form->{$_} = $ref->{$_} } keys %$ref;
-  $sth->finish;
-
-  #print(STDERR "DUNNING AMOUTN $form->{dunning_amount}\n");
-
-  my $query = qq|SELECT dnn.dunning_description AS max_dunning_level FROM dunning_config dnn WHERE id in (select dunning_id from ar WHERE paid < amount AND customer_id=$form->{customer_id} AND dunning_id IS NOT NULL) ORDER BY dunning_level DESC LIMIT 1|;
-  my $sth = $dbh->prepare($query);
-
-  $sth->execute || $form->dberror($query);
-
-  $ref = $sth->fetchrow_hashref(NAME_lc);
 
+  my $query = qq|SELECT dnn.dunning_description AS max_dunning_level FROM dunning_config dnn 
+                 WHERE id in (SELECT dunning_config_id from ar WHERE paid < amount AND customer_id = ? AND dunning_id IS NOT NULL)
+                 ORDER BY dunning_level DESC LIMIT 1|;
+  $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
   map { $form->{$_} = $ref->{$_} } keys %$ref;
-  $sth->finish;
-  #print(STDERR "LEVEL $form->{max_dunning_level}\n");
-
 
   #check whether payment_terms are better than old payment_terms
   if (($form->{payment_id} ne "") && ($form->{customer_payment_id} ne "")) {
-    my $query = qq|select (select ranking from payment_terms WHERE id = $form->{payment_id}), (select ranking from payment_terms WHERE id = $form->{customer_payment_id})|;
+    my $query = qq|SELECT (SELECT ranking from payment_terms WHERE id = $form->{payment_id}), (SELECT ranking FROM payment_terms WHERE id = $form->{customer_payment_id})|;
     my $stw = $dbh->prepare($query);
     $stw->execute || $form->dberror($query);
     ($old_ranking, $new_ranking) = $stw->fetchrow_array;