todo strict
[kivitendo-erp.git] / SL / IC.pm
index ab0c791..0bf015f 100644 (file)
--- a/SL/IC.pm
+++ b/SL/IC.pm
@@ -41,6 +41,8 @@ use YAML;
 use SL::CVar;
 use SL::DBUtils;
 
+use strict;
+
 sub get_part {
   $main::lxdebug->enter_sub();
 
@@ -412,6 +414,7 @@ sub save {
          ve = ?,
          gv = ?,
          ean = ?,
+         has_sernumber = ?,
          not_discountable = ?,
          microfiche = ?,
          partsgroup_id = ?,
@@ -441,6 +444,7 @@ sub save {
              conv_i($form->{ve}),
              conv_i($form->{gv}),
              $form->{ean},
+             $form->{has_sernumber} ? 't' : 'f',
              $form->{not_discountable} ? 't' : 'f',
              $form->{microfiche},
              conv_i($partsgroup_id),
@@ -790,8 +794,8 @@ sub all_parts {
     pfac       => 'LEFT JOIN price_factors pfac ON (pfac.id     = p.price_factor_id)',
     invoice_oi =>
       q|LEFT JOIN (
-         SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty,          assemblyitem,         deliverydate, 'invoice'    AS ioi FROM invoice UNION
-         SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty, FALSE AS assemblyitem, NULL AS deliverydate, 'orderitems' AS ioi FROM orderitems
+         SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty,          assemblyitem,         deliverydate, 'invoice'    AS ioi, id FROM invoice UNION
+         SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty, FALSE AS assemblyitem, NULL AS deliverydate, 'orderitems' AS ioi, id FROM orderitems
        ) AS ioi ON ioi.parts_id = p.id|,
     apoe       =>
       q|LEFT JOIN (
@@ -819,8 +823,11 @@ sub all_parts {
      'SUM(ioi.qty)' => ' ',
      description  => 'p.',
      qty          => 'ioi.',
-     unit         => 'ioi.',
      serialnumber => 'ioi.',
+     quotation    => 'apoe.',
+     cv           => 'cv.',
+     "ioi.id"     => ' ',
+     "ioi.ioi"    => ' ',
   );
 
   # if the join condition in these blocks are met, the column
@@ -830,13 +837,17 @@ sub all_parts {
     #  column name,   prefix,  joins_needed
     [ 'description',  'ioi.',  'invoice_oi'  ],
     [ 'deliverydate', 'ioi.',  'invoice_oi'  ],
-    [ 'transdate' ,   'apoe.', 'apoe'  ],
+    [ 'transdate',    'apoe.', 'apoe'        ],
+    [ 'unit',         'ioi.',  'invoice_oi'  ],
+    [ 'sellprice',    'ioi.',  'invoice_oi'  ],
   );
 
   # careful with renames. these are HARD, and any filters done on the original column will break
   my %renamed_columns = (
     'factor'       => 'price_factor',
     'SUM(ioi.qty)' => 'soldtotal',
+    'ioi.id'       => 'ioi_id',
+    'ioi.ioi'      => 'ioi',
   );
 
   if (($form->{searchitems} eq 'assembly') && $form->{l_lastcost}) {
@@ -846,22 +857,19 @@ sub all_parts {
   my $make_token_builder = sub {
     my $joins_needed = shift;
     sub {
-      my ($col, $group) = @_;
-      $renamed_columns{$col} ||= $col;
-
+      my ($col, $alias) = @_;
       my @coalesce_tokens =
-        map { ($_->[1] || 'p.') . $_->[0] }
+        map  { ($_->[1] || 'p.') . $_->[0] }
         grep { !$_->[2] || $joins_needed->{$_->[2]} }
-        grep { $_->[0] eq $col }
-        @column_override,
-        [ $col, $table_prefix{$col} ];
+        grep {  $_->[0] eq $col }
+        @column_override, [ $col, $table_prefix{$col} ];
 
-      my $coalesce   = scalar @coalesce_tokens > 1;
+      my $coalesce = scalar @coalesce_tokens > 1;
       return ($coalesce
         ? sprintf 'COALESCE(%s)', join ', ', @coalesce_tokens
         : shift                              @coalesce_tokens)
-        . ($group && $coalesce
-        ?  " AS $renamed_columns{$col}"
+        . ($alias && ($coalesce || $renamed_columns{$col})
+        ?  " AS " . ($renamed_columns{$col} || $col)
         : '');
     }
   };
@@ -936,7 +944,7 @@ sub all_parts {
   my $bsooqr        = any { $form->{$_} } @oe_flags;
   my @bsooqr_tokens = ();
 
-  push @select_tokens, @qsooqr_flags                                          if $bsooqr;
+  push @select_tokens, @qsooqr_flags, 'quotation', 'cv', 'ioi.id', 'ioi.ioi'  if $bsooqr;
   push @select_tokens, @deliverydate_flags                                    if $bsooqr && $form->{l_deliverydate};
   push @select_tokens, $q_assembly_lastcost                                   if ($form->{searchitems} eq 'assembly') && $form->{l_lastcost};
   push @bsooqr_tokens, q|module = 'ir' AND NOT ioi.assemblyitem|              if $form->{bought};
@@ -947,9 +955,6 @@ sub all_parts {
   push @bsooqr_tokens, q|module = 'oe' AND     quotation AND cv = 'vendor'|   if $form->{rfq};
   push @where_tokens, join ' OR ', map { "($_)" } @bsooqr_tokens              if $bsooqr;
 
-  $renamed_columns{onhand} = 'onhand_before_bsooqr';
-  $renamed_columns{qty}    = 'onhand';
-
   $joins_needed{partsgroup}  = 1;
   $joins_needed{pfac}        = 1;
   $joins_needed{makemodel}   = 1 if grep { $form->{$_} || $form->{"l_$_"} } @makemodel_filters;
@@ -957,6 +962,12 @@ sub all_parts {
   $joins_needed{apoe}        = 1 if $joins_needed{cv}   || grep { $form->{$_} || $form->{"l_$_"} } @apoe_filters;
   $joins_needed{invoice_oi}  = 1 if $joins_needed{apoe} || grep { $form->{$_} || $form->{"l_$_"} } @invoice_oi_filters;
 
+  # in bsoorq, use qtys instead of onhand
+  if ($joins_needed{invoice_oi}) {
+    $renamed_columns{onhand} = 'onhand_before_bsooqr';
+    $renamed_columns{qty}    = 'onhand';
+  }
+
   # special case for description search.
   # up in the simple filter section the description filter got interpreted as something like: WHERE description ILIKE '%$form->{description}%'
   # now we'd like to search also for the masked description entered in orderitems and invoice, so...
@@ -1001,7 +1012,15 @@ sub all_parts {
     push @bind_vars, @cvar_values;
   }
 
-  my $query = qq|SELECT DISTINCT $select_clause FROM parts p $join_clause WHERE $where_clause $group_clause $order_clause $limit_clause|;
+  my $query = <<"  SQL";
+    SELECT DISTINCT $select_clause
+    FROM parts p
+    $join_clause
+    WHERE $where_clause
+    $group_clause
+    $order_clause
+    $limit_clause
+  SQL
 
   $form->{parts} = selectall_hashref_query($form, $dbh, $query, @bind_vars);
 
@@ -1559,7 +1578,7 @@ sub get_basic_part_info {
 
   my $dbh      = $form->get_standard_dbh($myconfig);
 
-  my $query    = qq|SELECT id, partnumber, description, unit FROM parts WHERE id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
+  my $query    = qq|SELECT * FROM parts WHERE id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
 
   my $info     = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);