Oracle Support entfernt.
[kivitendo-erp.git] / SL / OE.pm
index d3b1313..739d8a0 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -37,6 +37,7 @@ package OE;
 use List::Util qw(max first);
 use SL::AM;
 use SL::Common;
+use SL::CVar;
 use SL::DBUtils;
 use SL::IC;
 
@@ -72,7 +73,8 @@ sub transactions {
     qq|  o.marge_total, o.marge_percent, | .
     qq|  ex.$rate AS exchangerate, | .
     qq|  pr.projectnumber AS globalprojectnumber, | .
-    qq|  e.name AS employee, s.name AS salesman | .
+    qq|  e.name AS employee, s.name AS salesman, | .
+    qq|  ct.country, ct.ustid  | .
     qq|FROM oe o | .
     qq|JOIN $vc ct ON (o.${vc}_id = ct.id) | .
     qq|LEFT JOIN employee e ON (o.employee_id = e.id) | .
@@ -83,7 +85,8 @@ sub transactions {
     qq|WHERE (o.quotation = ?) |;
   push(@values, $quotation);
 
-  my ($null, $department_id) = split /--/, $form->{department};
+  my ($null, $split_department_id) = split /--/, $form->{department};
+  my $department_id = $form->{department_id} || $split_department_id;
   if ($department_id) {
     $query .= qq| AND o.department_id = ?|;
     push(@values, $department_id);
@@ -247,6 +250,9 @@ sub save {
   my $all_units = AM->retrieve_units($myconfig, $form);
   $form->{all_units} = $all_units;
 
+  my $ic_cvar_configs = CVar->get_configs(module => 'IC',
+                                          dbh    => $dbh);
+
   $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
   unless ($form->{employee_id}) {
     $form->get_employee($dbh);
@@ -255,6 +261,11 @@ sub save {
   my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
 
   if ($form->{id}) {
+    $query = qq|DELETE FROM custom_variables
+                WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'IC'))
+                  AND (sub_module = 'orderitems')
+                  AND (trans_id IN (SELECT id FROM orderitems WHERE trans_id = ?))|;
+    do_query($form, $dbh, $query, $form->{id});
 
     $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
     do_query($form, $dbh, $query, $form->{id});
@@ -378,24 +389,19 @@ sub save {
       $pricegroup_id *= 1;
 
       # save detail record in orderitems table
+      my $orderitems_id = $form->{"orderitems_id_$i"};
+      ($orderitems_id)  = selectfirst_array_query($form, $dbh, qq|SELECT nextval('orderitemsid')|) if (!$orderitems_id);
+
       @values = ();
-      $query = qq|INSERT INTO orderitems (|;
-      if ($form->{"orderitems_id_$i"}) {
-        $query .= "id, ";
-      }
-      $query .= qq|trans_id, parts_id, description, longdescription, qty, base_qty, | .
-                qq|sellprice, discount, unit, reqdate, project_id, serialnumber, ship, | .
-                qq|pricegroup_id, ordnumber, transdate, cusordnumber, subtotal, | .
-                qq|marge_percent, marge_total, lastcost, price_factor_id, price_factor, marge_price_factor) | .
-                qq|VALUES (|;
-      if($form->{"orderitems_id_$i"}) {
-        $query .= qq|?,|;
-        push(@values, $form->{"orderitems_id_$i"});
-      }
-      $query .= qq|?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
-                   (SELECT factor FROM price_factors WHERE id = ?), ?)|;
+      $query = qq|INSERT INTO orderitems (
+                    id, trans_id, parts_id, description, longdescription, qty, base_qty,
+                    sellprice, discount, unit, reqdate, project_id, serialnumber, ship,
+                    pricegroup_id, ordnumber, transdate, cusordnumber, subtotal,
+                    marge_percent, marge_total, lastcost, price_factor_id, price_factor, marge_price_factor)
+                  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
+                          (SELECT factor FROM price_factors WHERE id = ?), ?)|;
       push(@values,
-           conv_i($form->{id}), conv_i($form->{"id_$i"}),
+           conv_i($orderitems_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
            $form->{"description_$i"}, $form->{"longdescription_$i"},
            $form->{"qty_$i"}, $baseqty,
            $fxsellprice, $form->{"discount_$i"},
@@ -411,6 +417,15 @@ sub save {
 
       $form->{"sellprice_$i"} = $fxsellprice;
       $form->{"discount_$i"} *= 100;
+
+      CVar->save_custom_variables(module       => 'IC',
+                                  sub_module   => 'orderitems',
+                                  trans_id     => $orderitems_id,
+                                  configs      => $ic_cvar_configs,
+                                  variables    => $form,
+                                  name_prefix  => 'ic_',
+                                  name_postfix => "_$i",
+                                  dbh          => $dbh);
     }
   }
 
@@ -636,6 +651,9 @@ sub retrieve {
 
   my ($query, $query_add, @values, @ids, $sth);
 
+  my $ic_cvar_configs = CVar->get_configs(module => 'IC',
+                                          dbh    => $dbh);
+
   # 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_$_"}
@@ -797,6 +815,15 @@ sub retrieve {
     $sth = prepare_execute_query($form, $dbh, $query, @values);
 
     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
+      # Retrieve custom variables.
+      my $cvars = CVar->get_custom_variables(dbh        => $dbh,
+                                             module     => 'IC',
+                                             sub_module => 'orderitems',
+                                             trans_id   => $ref->{orderitems_id},
+                                            );
+      map { $ref->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
+
+      # Handle accounts.
       if (!$ref->{"part_inventory_accno_id"}) {
         map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
       }
@@ -873,6 +900,7 @@ sub retrieve {
       }
 
       chop $ref->{taxaccounts};
+
       push @{ $form->{form_details} }, $ref;
       $stw->finish;
     }
@@ -968,8 +996,11 @@ sub order_details {
 
   $form->{discount} = [];
 
+  $form->{TEMPLATE_ARRAYS} = { };
   IC->prepare_parts_for_printing();
 
+  my $ic_cvar_configs = CVar->get_configs(module => 'IC');
+
   my @arrays =
     qw(runningnumber number description longdescription qty ship unit bin
        partnotes serialnumber reqdate sellprice listprice netprice
@@ -977,9 +1008,11 @@ sub order_details {
        linetotal  nodiscount_linetotal tax_rate projectnumber
        price_factor price_factor_name partsgroup);
 
+  push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
+
   my @tax_arrays = qw(taxbase tax taxdescription taxrate taxnumber);
 
-  $form->{TEMPLATE_ARRAYS} = { map { $_ => [] } (@arrays, @tax_arrays) };
+  map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays, @tax_arrays);
 
   my $sameitem = "";
   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
@@ -1141,6 +1174,7 @@ sub order_details {
         $sth->finish;
       }
 
+      map { push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} }, $form->{"ic_cvar_$_->{name}_$i"} } @{ $ic_cvar_configs };
     }
   }