'templates' nicht aus %::myconfig, sondern aus Defaults-Tabelle holen
[kivitendo-erp.git] / SL / DO.pm
index 4fbe802..fb8672c 100644 (file)
--- a/SL/DO.pm
+++ b/SL/DO.pm
@@ -61,7 +61,8 @@ sub transactions {
   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
 
   my $query =
-    qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.transdate,
+    qq|SELECT dord.id, dord.donumber, dord.ordnumber,
+         dord.transdate, dord.reqdate,
          ct.${vc}number, ct.name, dord.${vc}_id, dord.globalproject_id,
          dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
          dord.transaction_description,
@@ -143,6 +144,7 @@ sub transactions {
 
   my %allowed_sort_columns = (
     "transdate"               => "dord.transdate",
+    "reqdate"                 => "dord.reqdate",
     "id"                      => "dord.id",
     "donumber"                => "dord.donumber",
     "ordnumber"               => "dord.ordnumber",
@@ -227,7 +229,7 @@ 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}));
   }
 
@@ -293,6 +295,13 @@ sub save {
     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"},
@@ -305,7 +314,7 @@ sub save {
                $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"});
@@ -341,7 +350,7 @@ 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},
@@ -355,7 +364,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);
 
@@ -609,7 +618,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)
@@ -631,9 +640,6 @@ sub retrieve {
   }
   $sth->finish();
 
-  # remove any trailing whitespace
-  $form->{currency} =~ s/\s*$//;
-
   $form->{donumber_array} =~ s/\s*$//g;
 
   $form->{saved_donumber} = $form->{donumber};
@@ -666,16 +672,13 @@ sub retrieve {
     delete $form->{id};
   }
 
-  my %oid = ('Pg'     => 'oid',
-             'Oracle' => 'rowid');
-
   # retrieve individual items
   # this query looks up all information about the items
   # stuff different from the whole will not be overwritten, but saved with a suffix.
   $query =
     qq|SELECT doi.id AS delivery_order_items_id,
          p.partnumber, p.assembly, p.listprice, doi.description, doi.qty,
-         doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.bin, p.notes AS partnotes,
+         doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.notes AS partnotes,
          doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
          doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
          doi.price_factor_id, doi.price_factor, doi.marge_price_factor, doi.pricegroup_id,
@@ -687,7 +690,7 @@ sub retrieve {
        LEFT JOIN project pr ON (doi.project_id = pr.id)
        LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
        WHERE doi.delivery_order_id IN ($do_ids_placeholders)
-       ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
+       ORDER BY doi.oid|;
 
   $form->{form_details} = selectall_hashref_query($form, $dbh, $query, @do_ids);
 
@@ -733,10 +736,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);
@@ -749,9 +749,6 @@ sub order_details {
   my $partsgroup;
   my $position = 0;
 
-  my %oid = ('Pg'     => 'oid',
-             'Oracle' => 'rowid');
-
   my (@project_ids, %projectnumbers, %projectdescriptions);
 
   push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
@@ -801,13 +798,13 @@ 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
        partnotes serialnumber reqdate projectnumber projectdescription
        si_runningnumber si_number si_description
-       si_warehouse si_bin si_chargenumber si_bestbefore si_qty si_unit);
+       si_warehouse si_bin si_chargenumber si_bestbefore si_qty si_unit weight lineweight);
 
   map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays);
 
@@ -816,6 +813,7 @@ sub order_details {
   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
   my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
 
+  my $totalweight = 0;
   my $sameitem = "";
   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
     $i = $item->[0];
@@ -851,6 +849,13 @@ sub order_details {
     push @{ $form->{TEMPLATE_ARRAYS}{projectdescription} },
       $projectdescriptions{$form->{"project_id_$i"}};
 
+    my $lineweight = $form->{"qty_$i"} * $form->{"weight_$i"};
+    $totalweight += $lineweight;
+    push @{ $form->{TEMPLATE_ARRAYS}->{weight} },            $form->format_amount($myconfig, $form->{"weight_$i"}, 3);
+    push @{ $form->{TEMPLATE_ARRAYS}->{weight_nofmt} },      $form->{"weight_$i"};
+    push @{ $form->{TEMPLATE_ARRAYS}->{lineweight} },        $form->format_amount($myconfig, $lineweight, 3);
+    push @{ $form->{TEMPLATE_ARRAYS}->{lineweight_nofmt} },  $lineweight;
+
     if ($form->{"assembly_$i"}) {
       $sameitem = "";
 
@@ -858,22 +863,22 @@ sub order_details {
       my $sortorder = "";
       if ($form->{groupitems}) {
         $sortorder =
-          qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
+          qq|ORDER BY pg.partsgroup, a.oid|;
       } else {
-        $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
+        $sortorder = qq|ORDER BY a.oid|;
       }
 
       do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
 
       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));
       }
     }
 
@@ -904,6 +909,11 @@ sub order_details {
         for @{ $ic_cvar_configs };
   }
 
+  $form->{totalweight}       = $form->format_amount($myconfig, $totalweight, 3);
+  $form->{totalweight_nofmt} = $totalweight;
+  my $defaults = AM->get_defaults();
+  $form->{weightunit}        = $defaults->{weightunit};
+
   $h_pg->finish();
   $h_bin_wh->finish();