]> wagnertech.de Git - mfinanz.git/blobdiff - SL/OE.pm
Lieferdatum in Rechnungen.
[mfinanz.git] / SL / OE.pm
index ecd46470a575e8719973809b18a865bef7d5524e..093e77033cdf070e258bc6ea8adf4b460743aa55 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -125,7 +125,7 @@ sub transactions {
 
   my $sortorder = join ', ',
     ("o.id", $form->sort_columns(transdate, $ordnumber, name));
-  $sortorder = $form->{sort} unless $sortorder;
+  $sortorder = $form->{sort} if $form->{sort};
 
   $query .= " AND lower($ordnumber) LIKE '$number'" if $form->{$ordnumber};
   $query .= " AND o.transdate >= '$form->{transdatefrom}'"
@@ -324,7 +324,8 @@ sub save {
       $query = qq|INSERT INTO orderitems (|;
       $query .= "id, " if $form->{"orderitems_id_$i"};
       $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
-                  unit, reqdate, project_id, serialnumber, ship, pricegroup_id)
+                  unit, reqdate, project_id, serialnumber, ship, pricegroup_id,
+                  ordnumber, transdate, cusordnumber)
                    VALUES (|;
       $query .= qq|$form->{"orderitems_id_$i"},|
         if $form->{"orderitems_id_$i"};
@@ -332,8 +333,8 @@ sub save {
                   '$form->{"description_$i"}', $form->{"qty_$i"},
                   $fxsellprice, $form->{"discount_$i"},
                   '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
-                  '$form->{"serialnumber_$i"}', $form->{"ship_$i"},
-       '$pricegroup_id')|;
+                  '$form->{"serialnumber_$i"}', $form->{"ship_$i"}, '$pricegroup_id',
+                  '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}')|;
       $dbh->do($query) || $form->dberror($query);
 
       $form->{"sellprice_$i"} = $fxsellprice;
@@ -483,30 +484,12 @@ sub close_orders {
 
     if ($form->{"qty_$i"}) {
 
-      map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
-        qw(partnumber description unit);
-
       # set values to 0 if nothing entered
       $form->{"discount_$i"} =
-        $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
+        $form->parse_amount($myconfig, $form->{"discount_$i"});
 
       $form->{"sellprice_$i"} =
         $form->parse_amount($myconfig, $form->{"sellprice_$i"});
-      $fxsellprice = $form->{"sellprice_$i"};
-
-      my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
-      $dec = length $dec;
-      my $decimalplaces = ($dec > 2) ? $dec : 2;
-
-      $discount =
-        $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
-                            $decimalplaces);
-      $form->{"sellprice_$i"} =
-        $form->round_amount($form->{"sellprice_$i"} - $discount,
-                            $decimalplaces);
-
-      $form->{"inventory_accno_$i"} *= 1;
-      $form->{"expense_accno_$i"}   *= 1;
     }
   }
   # get ids from $form
@@ -600,10 +583,10 @@ sub retrieve {
   # connect to database
   my $dbh = $form->dbconnect_noauto($myconfig);
 
-  my $query;
+  my $query, @ids;
 
   # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
-  map { push @ids, $form->{"trans_id_$_"} if ($form->{"id_$_"}) } (1 .. $form->{"rowcount"});
+  map { push @ids, $form->{"trans_id_$_"} if ($form->{"id_$_"} and $form->{"trans_id_$_"}) } (1 .. $form->{"rowcount"});
 
   # if called in multi id mode, and still only got one id, switch back to single id 
   if ($form->{"rowcount"} and $#ids == 0) {
@@ -650,6 +633,10 @@ sub retrieve {
 
   ($form->{currency}) = split /:/, $form->{currencies};
 
+  # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure 
+  # we come from invoices, feel free.
+  $form->{reqdate} = $form->{deliverydate} if ($form->{deliverydate} and $form->{callback} =~ /action=ar_transactions/);
+
   if ($form->{id} or @ids) {
 
     # retrieve order for single id
@@ -679,9 +666,9 @@ sub retrieve {
     $ref = $sth->fetchrow_hashref(NAME_lc);
     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
-    # destroy all entries for multiple ids that yield different information
+    # set all entries for multiple ids blank that yield different information
     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
-      map { undef $form->{$_} if ($ref->{$_} ne $form->{$_}) } keys %$ref;
+      map { $form->{$_} = '' if ($ref->{$_} ne $form->{$_}) } keys %$ref;
     }
 
     # if not given, fill transdate with current_date
@@ -726,10 +713,11 @@ sub retrieve {
                 c1.accno AS inventory_accno,
                 c2.accno AS income_accno,
                c3.accno AS expense_accno,
-               oe.ordnumber, oe.transdate, oe.cusordnumber
+               oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe
                 p.partnumber, p.assembly, o.description, o.qty,
                o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
                 o.reqdate, o.project_id, o.serialnumber, o.ship,
+               o.ordnumber, o.transdate, o.cusordnumber,
                pr.projectnumber,
                pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
                FROM orderitems o
@@ -750,6 +738,14 @@ sub retrieve {
     $sth->execute || $form->dberror($query);
 
     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
+     
+      # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
+      #   unless already present there 
+      # remove _oe entries afterwards
+      map { $ref->{$_} = $ref->{"${_}_oe"} if ($ref->{$_} eq '') }
+        qw|ordnumber transdate cusordnumber| if (@ids);
+      map{ delete $ref->{$_} } 
+        qw|ordnumber_oe transdate_oe cusordnumber_oe|;
 
       #set expense_accno=inventory_accno if they are different => bilanz
       $vendor_accno =
@@ -900,14 +896,14 @@ sub order_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 - $discount;
+      $form->{"netprice_$i"} = $sellprice - $i_discount;
 
       my $linetotal =
         $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
@@ -927,6 +923,7 @@ sub order_details {
       $linetotal = ($linetotal != 0) ? $linetotal : " ";
 
       push(@{ $form->{discount} }, $discount);
+      push(@{ $form->{p_discount} }, $form->{"discount_$i"});
 
       $form->{ordtotal} += $linetotal;