Aufrufe von 'exit' durch eigene Funktion '::end_of_request()' ersetzt.
[kivitendo-erp.git] / SL / IC.pm
index e3879a4..0256e3d 100644 (file)
--- a/SL/IC.pm
+++ b/SL/IC.pm
 package IC;
 
 use Data::Dumper;
-use List::MoreUtils qw(all any);
+use List::MoreUtils qw(all any uniq);
 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),
@@ -493,8 +497,8 @@ sub save {
       if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
 
         $query = qq|INSERT INTO makemodel (parts_id, make, model) | .
-                            qq|VALUES (?, ?, ?)|;
-                   @values = (conv_i($form->{id}), conv_i($form->{"make_$i"}), $form->{"model_$i"});
+                 qq|VALUES (?, ?, ?)|;
+        @values = (conv_i($form->{id}), conv_i($form->{"make_$i"}), $form->{"model_$i"});
 
         do_query($form, $dbh, $query, @values);
       }
@@ -507,7 +511,7 @@ sub save {
       $query =
         qq|INSERT INTO partstax (parts_id, chart_id)
            VALUES (?, (SELECT id FROM chart WHERE accno = ?))|;
-                       @values = (conv_i($form->{id}), $item);
+      @values = (conv_i($form->{id}), $item);
       do_query($form, $dbh, $query, @values);
     }
   }
@@ -521,8 +525,8 @@ sub save {
       if ($form->{"qty_$i"} != 0) {
         $form->{"bom_$i"} *= 1;
         $query = qq|INSERT INTO assembly (id, parts_id, qty, bom) | .
-                            qq|VALUES (?, ?, ?, ?)|;
-                   @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}), conv_i($form->{"qty_$i"}), $form->{"bom_$i"} ? 't' : 'f');
+                 qq|VALUES (?, ?, ?, ?)|;
+        @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}), conv_i($form->{"qty_$i"}), $form->{"bom_$i"} ? 't' : 'f');
         do_query($form, $dbh, $query, @values);
       }
     }
@@ -790,14 +794,14 @@ 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 (
-         SELECT id, transdate, 'ir' AS module, ordnumber, quonumber,         invnumber, FALSE AS quotation, NULL AS customer_id,         vendor_id, NULL AS deliverydate, 'invoice'    AS ioi FROM ap UNION
-         SELECT id, transdate, 'is' AS module, ordnumber, quonumber,         invnumber, FALSE AS quotation,         customer_id, NULL AS vendor_id,         deliverydate, 'invoice'    AS ioi FROM ar UNION
-         SELECT id, transdate, 'oe' AS module, ordnumber, quonumber, NULL AS invnumber,          quotation,         customer_id,         vendor_id, NULL AS deliverydate, 'orderitems' AS ioi FROM oe
+         SELECT id, transdate, 'ir' AS module, ordnumber, quonumber,         invnumber, FALSE AS quotation, NULL AS customer_id,         vendor_id,    NULL AS deliverydate, 'invoice'    AS ioi FROM ap UNION
+         SELECT id, transdate, 'is' AS module, ordnumber, quonumber,         invnumber, FALSE AS quotation,         customer_id, NULL AS vendor_id,            deliverydate, 'invoice'    AS ioi FROM ar UNION
+         SELECT id, transdate, 'oe' AS module, ordnumber, quonumber, NULL AS invnumber,          quotation,         customer_id,         vendor_id, reqdate AS deliverydate, 'orderitems' AS ioi FROM oe
        ) AS apoe ON ((ioi.trans_id = apoe.id) AND (ioi.ioi = apoe.ioi))|,
     cv         =>
       q|LEFT JOIN (
@@ -814,7 +818,7 @@ sub all_parts {
      ordnumber    => 'apoe.', make         => 'mm.',
      quonumber    => 'apoe.', model        => 'mm.',
      invnumber    => 'apoe.', partsgroup   => 'pg.',
-     lastcost     => ' ',
+     lastcost     => ' ',   , soldtotal    => ' ',
      factor       => 'pfac.',
      'SUM(ioi.qty)' => ' ',
      description  => 'p.',
@@ -822,6 +826,8 @@ sub all_parts {
      serialnumber => 'ioi.',
      quotation    => 'apoe.',
      cv           => 'cv.',
+     "ioi.id"     => ' ',
+     "ioi.ioi"    => ' ',
   );
 
   # if the join condition in these blocks are met, the column
@@ -833,12 +839,15 @@ sub all_parts {
     [ 'deliverydate', 'ioi.',  'invoice_oi'  ],
     [ '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}) {
@@ -928,14 +937,16 @@ sub all_parts {
 
   #my $order_clause = " ORDER BY $form->{sort} $sort_order";
 
-  my $limit_clause = " LIMIT 100" if $form->{top100};
+  my $limit_clause;
+  $limit_clause = " LIMIT 100"                   if $form->{top100};
+  $limit_clause = " LIMIT " . $form->{limit} * 1 if $form->{limit} * 1;
 
   #=== joins and complicated filters ========#
 
   my $bsooqr        = any { $form->{$_} } @oe_flags;
   my @bsooqr_tokens = ();
 
-  push @select_tokens, @qsooqr_flags, 'quotation', 'cv'                       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};
@@ -974,7 +985,7 @@ sub all_parts {
 
   # now the master trick: soldtotal.
   if ($form->{l_soldtotal}) {
-    push @where_tokens, 'ioi.qty >= 0';
+    push @where_tokens, 'NOT ioi.qty = 0';
     push @group_tokens, @select_tokens;
      map { s/.*\sAS\s+//si } @group_tokens;
     push @select_tokens, 'SUM(ioi.qty)';
@@ -994,16 +1005,36 @@ sub all_parts {
   my $where_clause  = join ' AND ', map { "($_)" } @where_tokens;
   my $group_clause  = ' GROUP BY ' . join ', ',    map { $token_builder->($_) } @group_tokens if scalar @group_tokens;
 
-  my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'IC',
-                                                            'trans_id_field' => 'p.id',
-                                                            'filter'         => $form);
+  my %oe_flag_to_cvar = (
+    bought   => 'invoice',
+    sold     => 'invoice',
+    onorder  => 'orderitems',
+    ordered  => 'orderitems',
+    rfq      => 'orderitems',
+    quoted   => 'orderitems',
+  );
+
+  my ($cvar_where, @cvar_values) = CVar->build_filter_query(
+    module         => 'IC',
+    trans_id_field => $bsooqr ? 'ioi.id': 'p.id',
+    filter         => $form,
+    sub_module     => $bsooqr ? [ uniq grep { $oe_flag_to_cvar{$form->{$_}} } @oe_flags ] : undef,
+  );
 
   if ($cvar_where) {
     $where_clause .= qq| AND ($cvar_where)|;
     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);
 
@@ -1043,6 +1074,8 @@ sub all_parts {
   }
 
   $main::lxdebug->leave_sub();
+
+  return wantarray ? @{ $form->{parts} } : $form->{parts};
 }
 
 sub _create_filter_for_priceupdate {
@@ -1425,7 +1458,7 @@ sub follow_account_chain {
     qq|  cnew.accno | .
     qq|FROM chart c | .
     qq|LEFT JOIN chart cnew ON c.new_chart_id = cnew.id | .
-    qq|WHERE (c.id = ?) AND NOT c.new_chart_id ISNULL AND (c.new_chart_id > 0)|;
+    qq|WHERE (c.id = ?) AND NOT c.new_chart_id IS NULL AND (c.new_chart_id > 0)|;
   $sth = prepare_query($form, $dbh, $query);
 
   while (1) {
@@ -1561,7 +1594,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);