Anpassung Rose-Funktionen auf Umstellung currencies
[kivitendo-erp.git] / SL / DO.pm
index 8d61b0f..5d406dc 100644 (file)
--- a/SL/DO.pm
+++ b/SL/DO.pm
@@ -62,17 +62,20 @@ sub transactions {
 
   my $query =
     qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.transdate,
-         ct.name, dord.${vc}_id, dord.globalproject_id,
+         ct.${vc}number, ct.name, dord.${vc}_id, dord.globalproject_id,
          dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
          dord.transaction_description,
          pr.projectnumber AS globalprojectnumber,
+         dep.description AS department,
          e.name AS employee,
          sm.name AS salesman
        FROM delivery_orders dord
        LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
        LEFT JOIN employee e ON (dord.employee_id = e.id)
        LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
-       LEFT JOIN project pr ON (dord.globalproject_id = pr.id)|;
+       LEFT JOIN project pr ON (dord.globalproject_id = pr.id)
+       LEFT JOIN department dep ON (dord.department_id = dep.id)
+|;
 
   push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
 
@@ -147,7 +150,8 @@ sub transactions {
     "employee"                => "e.name",
     "salesman"                => "sm.name",
     "shipvia"                 => "dord.shipvia",
-    "transaction_description" => "dord.transaction_description"
+    "transaction_description" => "dord.transaction_description",
+    "department"              => "lower(dep.description)",
   );
 
   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
@@ -223,12 +227,12 @@ sub save {
     $query = qq|SELECT nextval('id')|;
     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
-    $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id) VALUES (?, '', ?)|;
+    $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id, currency_id) VALUES (?, '', ?, (SELECT currency_id FROM defaults LIMIT 1))|;
     do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
   }
 
   my $project_id;
-  my $reqdate;
+  my $items_reqdate;
 
   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
   my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
@@ -284,24 +288,31 @@ sub save {
     $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
     my $linetotal    = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
 
-    $reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
+    $items_reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
 
     do_statement($form, $h_item_id, $q_item_id);
     my ($item_id) = $h_item_id->fetchrow_array();
 
+    # Get pricegroup_id and save it. Unfortunately the interface
+    # also uses ID "0" for signalling that none is selected, but "0"
+    # must not be stored in the database. Therefore we cannot simply
+    # use conv_i().
+    my $pricegroup_id = $form->{"pricegroup_id_$i"} * 1;
+    $pricegroup_id    = undef if !$pricegroup_id;
+
     # save detail record in delivery_order_items table
     @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
                $form->{"description_$i"}, $form->{"longdescription_$i"},
                $form->{"qty_$i"}, $baseqty,
                $form->{"sellprice_$i"}, $form->{"discount_$i"} / 100,
-               $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}),
+               $form->{"unit_$i"}, conv_date($items_reqdate), conv_i($form->{"project_id_$i"}),
                $form->{"serialnumber_$i"},
                $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
                $form->{"cusordnumber_$i"},
                $form->{"lastcost_$i"},
                conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
                conv_i($form->{"marge_price_factor_$i"}),
-               conv_i($form->{"pricegroup_id_$i"}));
+               $pricegroup_id);
     do_statement($form, $h_item, $q_item, @values);
 
     my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
@@ -327,6 +338,8 @@ sub save {
   $h_item_stock->finish();
 
 
+  # reqdate is last items reqdate (?: old behaviour) if not already set
+  $form->{reqdate} ||= $items_reqdate;
   # save DO record
   $query =
     qq|UPDATE delivery_orders SET
@@ -335,13 +348,13 @@ sub save {
          shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
          delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
          globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
-         is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, curr = ?
+         is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, currency_id = (SELECT id FROM currencies WHERE name = ?)
        WHERE id = ?|;
 
   @values = ($form->{donumber}, $form->{ordnumber},
              $form->{cusordnumber}, conv_date($form->{transdate}),
              conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
-             conv_date($reqdate), $form->{shippingpoint}, $form->{shipvia},
+             conv_date($form->{reqdate}), $form->{shippingpoint}, $form->{shipvia},
              $form->{notes}, $form->{intnotes},
              $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
              conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
@@ -349,7 +362,7 @@ sub save {
              conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
              $form->{transaction_description},
              $form->{type} =~ /^sales/ ? 't' : 'f',
-             conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), substr($form->{currency}, 0, 3),
+             conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), $form->{currency},
              conv_i($form->{id}));
   do_query($form, $dbh, $query, @values);
 
@@ -573,9 +586,12 @@ sub retrieve {
   my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
 
   if ($mode eq 'default') {
-    $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate, current_date AS reqdate|);
+    $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate|);
     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
+    # if reqdate is not set from oe-workflow, set it to transdate (which is current date)
+    $form->{reqdate} ||= $form->{transdate};
+
     # get last name used
     $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
 
@@ -600,7 +616,7 @@ sub retrieve {
          d.description AS department, dord.language_id,
          dord.shipto_id,
          dord.globalproject_id, dord.delivered, dord.transaction_description,
-         dord.taxzone_id, dord.taxincluded, dord.terms, dord.curr AS currency
+         dord.taxzone_id, dord.taxincluded, dord.terms, (SELECT cu.name FROM currencies cu WHERE cu.id=dord.currency_id) AS currency
        FROM delivery_orders dord
        JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
        LEFT JOIN employee e ON (dord.employee_id = e.id)
@@ -622,9 +638,6 @@ sub retrieve {
   }
   $sth->finish();
 
-  # remove any trailing whitespace
-  $form->{currency} =~ s/\s*$//;
-
   $form->{donumber_array} =~ s/\s*$//g;
 
   $form->{saved_donumber} = $form->{donumber};
@@ -724,10 +737,7 @@ sub retrieve {
 sub order_details {
   $main::lxdebug->enter_sub();
 
-  my ($self)   = @_;
-
-  my $myconfig = \%main::myconfig;
-  my $form     = $main::form;
+  my ($self, $myconfig, $form) = @_;
 
   # connect to database
   my $dbh = $form->get_standard_dbh($myconfig);
@@ -792,7 +802,7 @@ sub order_details {
   my $ic_cvar_configs = CVar->get_configs(module => 'IC');
 
   $form->{TEMPLATE_ARRAYS} = { };
-  IC->prepare_parts_for_printing();
+  IC->prepare_parts_for_printing(myconfig => $myconfig, form => $form);
 
   my @arrays =
     qw(runningnumber number description longdescription qty unit
@@ -858,13 +868,13 @@ sub order_details {
 
       while (my $ref = $h_pg->fetchrow_hashref("NAME_lc")) {
         if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
-          map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
+          map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
           $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
-          push(@{ $form->{description} }, $sameitem);
+          push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, $sameitem);
         }
-        push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|);
+        push(@{ $form->{TEMPLATE_ARRAYS}->{"description"} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq| -- $ref->{partnumber}, $ref->{description}|);
 
-        map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
+        map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
       }
     }