Merge von 845 aus unstable: Lieferdatum in Rechnungen
[kivitendo-erp.git] / SL / IS.pm
index 8664b99..890bdcc 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -107,9 +107,11 @@ sub invoice_details {
       push(@{ $form->{qty} },
            $form->format_amount($myconfig, $form->{"qty_$i"}));
       push(@{ $form->{unit} },         qq|$form->{"unit_$i"}|);
-      push(@{ $form->{deliverydate} }, qq|$form->{"deliverydate_$i"}|);
+      push(@{ $form->{deliverydate_oe} }, qq|$form->{"deliverydate_$i"}|);
 
       push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
+      push(@{ $form->{ordnumber_oe} }, qq|$form->{"ordnumber_$i"}|);
+      push(@{ $form->{transdate_oe} }, qq|$form->{"transdate_$i"}|);
 
       if ($form->{lizenzen}) {
         if ($form->{"licensenumber_$i"}) {
@@ -139,14 +141,14 @@ sub invoice_details {
       $dec = length $dec;
       my $decimalplaces = ($dec > 2) ? $dec : 2;
 
-      my $discount =
-        $form->round_amount(
-                            $sellprice * $form->parse_amount($myconfig,
-                                                 $form->{"discount_$i"}) / 100,
-                            $decimalplaces);
+      my $i_discount = $form->round_amount($sellprice * 
+                                           $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100, $decimalplaces);
+
+      my $discount = $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
 
       # keep a netprice as well, (sellprice - discount)
-      $form->{"netprice_$i"} = $sellprice - $discount;
+      $form->{"netprice_$i"} = $sellprice - $i_discount;
+
       push(@{ $form->{netprice} },
            ($form->{"netprice_$i"} != 0)
            ? $form->format_amount(
@@ -165,6 +167,7 @@ sub invoice_details {
       $linetotal = ($linetotal != 0) ? $linetotal : " ";
 
       push(@{ $form->{discount} }, $discount);
+      push(@{ $form->{p_discount} }, $form->{"discount_$i"});
 
       $form->{total} += $linetotal;
 
@@ -214,7 +217,8 @@ sub invoice_details {
           $taxbase{$item} += $taxbase;
         }
       }
-
+      $tax_rate = $taxrate * 100;
+      push(@{ $form->{tax_rate} }, qq|$tax_rate|);
       if ($form->{"assembly_$i"}) {
         $sameitem = "";
 
@@ -344,8 +348,6 @@ sub customer_details {
     $contact = "and cp.cp_id = $form->{cp_id}";
   }
 
-  $taxincluded = $form->{taxincluded};
-
   # get rest for the customer
   my $query = qq|SELECT ct.*, cp.*, ct.notes as customernotes
                  FROM customer ct
@@ -355,9 +357,10 @@ sub customer_details {
   $sth->execute || $form->dberror($query);
 
   $ref = $sth->fetchrow_hashref(NAME_lc);
-  map { $form->{$_} = $ref->{$_} } keys %$ref;
 
-  $form->{taxincluded} = $taxincluded;
+  # remove id and taxincluded before copy back
+  delete @$ref{qw(id taxincluded)};
+  map { $form->{$_} = $ref->{$_} } keys %$ref;
 
   $sth->finish;
   $dbh->disconnect;
@@ -367,7 +370,7 @@ sub customer_details {
 
 sub post_invoice {
   $main::lxdebug->enter_sub();
-print STDERR "IS.pm-post_invoice\n";
+
   my ($self, $myconfig, $form) = @_;
 
   # connect to database, turn off autocommit
@@ -551,7 +554,6 @@ print STDERR "IS.pm-post_invoice\n";
         ? qq|'$form->{"deliverydate_$i"}'|
         : "NULL";
 
-
       # get pricegroup_id and save ist
       ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
       $pricegroup_id *= 1;
@@ -559,14 +561,15 @@ print STDERR "IS.pm-post_invoice\n";
       # save detail record in invoice table
       $query = qq|INSERT INTO invoice (trans_id, parts_id, description, qty,
                   sellprice, fxsellprice, discount, allocated, assemblyitem,
-                 unit, deliverydate, project_id, serialnumber, pricegroup_id)
+                 unit, deliverydate, project_id, serialnumber, pricegroup_id,
+                 ordnumber, transdate, cusordnumber)
                  VALUES ($form->{id}, $form->{"id_$i"},
                  '$form->{"description_$i"}', $form->{"qty_$i"},
                  $form->{"sellprice_$i"}, $fxsellprice,
                  $form->{"discount_$i"}, $allocated, 'f',
                  '$form->{"unit_$i"}', $deliverydate, (SELECT id from project where projectnumber = '$project_id'),
-                 '$form->{"serialnumber_$i"}',
-      '$pricegroup_id')|;
+                 '$form->{"serialnumber_$i"}', '$pricegroup_id',
+                 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}')|;
       $dbh->do($query) || $form->dberror($query);
 
       if ($form->{lizenzen}) {
@@ -802,8 +805,9 @@ print STDERR "IS.pm-post_invoice\n";
   # set values which could be empty to 0
   $form->{terms}       *= 1;
   $form->{taxincluded} *= 1;
-  my $datepaid = ($form->{paid})    ? qq|'$form->{datepaid}'| : "NULL";
-  my $duedate  = ($form->{duedate}) ? qq|'$form->{duedate}'|  : "NULL";
+  my $datepaid     = ($form->{paid})         ? qq|'$form->{datepaid}'| : "NULL";
+  my $duedate      = ($form->{duedate})      ? qq|'$form->{duedate}'|  : "NULL";
+  my $deliverydate = ($form->{deliverydate}) ? qq|'$form->{deliverydate}'| : "NULL";
 
   # fill in subject if there is none
   $form->{subject} = qq|$form->{label} $form->{invnumber}|
@@ -836,6 +840,7 @@ Message: $form->{message}\r| if $form->{message};
               paid = $form->{paid},
              datepaid = $datepaid,
              duedate = $duedate,
+              deliverydate = $deliverydate,
              invoice = '1',
              shippingpoint = '$form->{shippingpoint}',
              shipvia = '$form->{shipvia}',
@@ -1113,7 +1118,7 @@ sub delete_invoice {
 
 sub retrieve_invoice {
   $main::lxdebug->enter_sub();
-print STDERR "IS.pm-retrieve_invoice\n";
+
   my ($self, $myconfig, $form) = @_;
 
   # connect to database
@@ -1161,7 +1166,7 @@ print STDERR "IS.pm-retrieve_invoice\n";
 
     # retrieve invoice
     $query = qq|SELECT a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
-                a.transdate AS invdate, a.paid,
+                a.transdate AS invdate, a.deliverydate, a.paid,
                 a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes,
                a.duedate, a.taxincluded, a.curr AS currency,
                a.employee_id, e.name AS employee
@@ -1219,7 +1224,8 @@ print STDERR "IS.pm-retrieve_invoice\n";
                i.discount, i.parts_id AS id, i.unit, i.deliverydate,
                i.project_id, pr.projectnumber, i.serialnumber,
                p.partnumber, p.assembly, p.bin, p.notes AS partnotes, i.id AS invoice_pos,
-               pg.partsgroup, i.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=i.pricegroup_id) as pricegroup
+               pg.partsgroup, i.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=i.pricegroup_id) as pricegroup,
+               i.ordnumber, i.transdate, i.cusordnumber
                FROM invoice i
                JOIN parts p ON (i.parts_id = p.id)
                LEFT JOIN project pr ON (i.project_id = pr.id)
@@ -1531,11 +1537,11 @@ sub retrieve_item {
 ##########################
 # get pricegroups from database
 # build up selected pricegroup
-# if an exchange rate - change price 
+# if an exchange rate - change price
 # for each part
 #
 sub get_pricegroups_for_parts {
-print STDERR "IS.pm - get_pricegroups_for_parts\n";
+
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
@@ -1554,111 +1560,85 @@ print STDERR "IS.pm - get_pricegroups_for_parts\n";
       $id = $form->{"new_id_$i"};
     }
 
-    ($price, $selectedpricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
-#  print (STDERR "sellprice_drag_$i", Dumper($form->{"sellprice_drag_$i"}));
+    ($price, $selectedpricegroup_id) = split /--/,
+      $form->{"sellprice_drag_$i"};
 
     $pricegroup_old = $form->{"pricegroup_old_$i"};
-#  print (STDERR "pricegroup_old_i-$i", Dumper($pricegroup_old));
 
     $price_new = $form->{"price_new_$i"};
 
     $price_old = $form->{"price_old_$i"};
 
-    $query = qq|SELECT pricegroup_id, (SELECT p.sellprice from parts p where p.id = $id) as default_sellprice,(SELECT pg.pricegroup FROM pricegroup pg WHERE id=pricegroup_id) AS pricegroup, price, '' AS selected FROM prices WHERE parts_id = $id UNION SELECT 0 as pricegroup_id,(SELECT sellprice FROM parts WHERE id=$id) as default_sellprice,'' as pricegroup, (SELECT DISTINCT sellprice from parts where id=$id) as price, 'selected' AS selected from prices ORDER BY pricegroup|;
+    $query =
+      qq|SELECT pricegroup_id, (SELECT p.sellprice from parts p where p.id = $id) as default_sellprice,(SELECT pg.pricegroup FROM pricegroup pg WHERE id=pricegroup_id) AS pricegroup, price, '' AS selected FROM prices WHERE parts_id = $id UNION SELECT 0 as pricegroup_id,(SELECT sellprice FROM parts WHERE id=$id) as default_sellprice,'' as pricegroup, (SELECT DISTINCT sellprice from parts where id=$id) as price, 'selected' AS selected from prices ORDER BY pricegroup|;
 
     $pkq = $dbh->prepare($query);
     $pkq->execute || $form->dberror($query);
     while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
-#       push @{ $form->{PRICES}{$id} }, $pkr;
-        push @{ $form->{PRICES}{$i} }, $pkr;
-        $pkr->{id} = $id;
-        $pkr->{selected}  = '';
 
-    # if there is an exchange rate change price
+      #       push @{ $form->{PRICES}{$id} }, $pkr;
+      push @{ $form->{PRICES}{$i} }, $pkr;
+      $pkr->{id}       = $id;
+      $pkr->{selected} = '';
+
+      # if there is an exchange rate change price
       if (($form->{exchangerate} * 1) != 0) {
-# print STDERR "WECHSELKURS?-$form->{exchangerate}\n";
+
         $pkr->{price} /= $form->{exchangerate};
       }
-      $pkr->{price} = $form->format_amount($myconfig,$pkr->{price},5);
-
+      $pkr->{price} = $form->format_amount($myconfig, $pkr->{price}, 5);
 
       if ($selectedpricegroup_id eq undef) {
         if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
-print STDERR "   INIT ROW \n";
-#print (STDERR "   PREIS", Dumper($pkr->{price}));
+
           $pkr->{selected}  = ' selected';
           $last->{selected} = '';
-# print (STDERR "   SELLPRICE", Dumper($form->{"sellprice_$i"}));
 
-        # no customer pricesgroup set 
+          # no customer pricesgroup set
           if ($pkr->{price} == $pkr->{default_sellprice}) {
-print (STDERR "   PREIS IST DEFAULT-SELLPRICE", Dumper($form->{"sellprice_$i"}));
-           $pkr->{price} = $form->{"sellprice_$i"};
 
-#  if ($form->{tradediscount}){
-#  print (STDERR "TRADE--", Dumper($pkr->{price}));
-#               $pkr->{price} =$pkr->{price} * (1 - $form->{tradediscount});
-#               $pkr->{price} = $form->format_amount($myconfig,$pkr->{price},5);
-#  print (STDERR "TRADE--", Dumper($pkr->{price}));
-#  }
+            $pkr->{price} = $form->{"sellprice_$i"};
 
           } else {
-print STDERR "   PREIS IST NICHT NULL\n";
-             $form->{"sellprice_$i"} = $pkr->{price};
+
+            $form->{"sellprice_$i"} = $pkr->{price};
           }
-# print (STDERR "           PRICE", Dumper($pkr->{price}));
 
         } else {
-print STDERR "   INIT ROW but what\n";
-print (STDERR "   PREIS -", Dumper($pkr->{price}), "Default", Dumper($pkr->{default_sellprice}));
           if ($pkr->{price} == $pkr->{default_sellprice}) {
-print (STDERR "   PREIS IST DEFAULT-", Dumper($form->{"sellprice_$i"}));
-            $pkr->{price} = $form->{"sellprice_$i"};
-            $pkr->{selected}                    = ' selected';
+
+            $pkr->{price}    = $form->{"sellprice_$i"};
+            $pkr->{selected} = ' selected';
           }
         }
       }
-      if ($selectedpricegroup_id or $selectedpricegroup_id == 0){
+      if ($selectedpricegroup_id or $selectedpricegroup_id == 0) {
         if ($selectedpricegroup_id ne $pricegroup_old) {
           if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
             if ($price_new != $form->{"sellprice_$i"}) {
-print STDERR "   MANUELLEN PREIS WÄHLEN\n";
             } else {
-print STDERR "   UPDATE CHANGE PRICEGROUP\n";
-              $pkr->{selected}                    = ' selected';
-              $last->{selected}                   = '';
-#$form->{"pricegroup_old_$i"} = $pkr->{$pricegroup_id};
+              $pkr->{selected}  = ' selected';
+              $last->{selected} = '';
             }
           }
-        } else { 
+        } else {
           if (($price_new != $form->{"sellprice_$i"}) and ($price_new ne 0)) {
             if ($pkr->{pricegroup_id} == 0) {
-print STDERR "   UPDATE CHANGE PRICEGROUP with price manuelly\n";
-print (STDERR "  SELLPRICE??? ---", Dumper($form->{"sellprice_$i"}));
-print (STDERR "  NEWPRICE??? ---", Dumper($price_new));
-              $pkr->{price} = $form->{"sellprice_$i"};
-              $pkr->{selected}                    = ' selected';
-              $last->{selected}                   = '';
-       #$form->{"sellprice_$i"} = $form->format_amount($myconfig, $price_new, 2);
-# print (STDERR "----5555---", Dumper($pkr));
+              $pkr->{price}     = $form->{"sellprice_$i"};
+              $pkr->{selected}  = ' selected';
+              $last->{selected} = '';
             }
           } else {
             if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
-print STDERR "   UPDATE NO CHANGE\n";
-              $pkr->{selected}                    = ' selected';
-              $last->{selected}                   = '';
-print STDERR "  DEFAULTPRICE??? ---$pkr->{default_sellprice}\n";
-print (STDERR "  SELLPRICE??? ---", Dumper($form->{"sellprice_$i"}));
-# print (STDERR "  HIER DER SELLPRICE DEFAULT??? ---", Dumper($form));
-print STDERR "  NEWPRICE??? ---$price_new_\n";
-              if (($pkr->{pricegroup_id} == 0) and ($pkr->{price} == $form->{"sellprice_$i"})) {
-print (STDERR "  UPDATE NO CHANGE BUT PRICE MANUELLY SET", Dumper($pkr->{price}));
+              $pkr->{selected}  = ' selected';
+              $last->{selected} = '';
+              if (    ($pkr->{pricegroup_id} == 0)
+                  and ($pkr->{price} == $form->{"sellprice_$i"})) {
+
                 # $pkr->{price}                         = $form->{"sellprice_$i"};
-              } else {
+                  } else {
                 $pkr->{price} = $form->{"sellprice_$i"};
               }
-#print (STDERR "   FEHLER", Dumper($form->{"sellprice_$i"}));
             }
           }
         }
@@ -1671,8 +1651,6 @@ print (STDERR "  UPDATE NO CHANGE BUT PRICE MANUELLY SET", Dumper($pkr->{price})
 
   $dbh->disconnect;
 
-#        print (STDERR "TEST", Dumper($form->{PRICES}));
-# print (STDERR "TEST id_$i", Dumper($form->{"id_$i"}));
   $main::lxdebug->leave_sub();
 }