]> wagnertech.de Git - kivitendo-erp.git/blobdiff - SL/IC.pm
List::MoreUtils für kompakteren Code benutzen.
[kivitendo-erp.git] / SL / IC.pm
index f9315f42acb23a5b310d08b369c9eb414b779338..5314d4b8e694ca4333e53a27c5b31e9f537745d0 100644 (file)
--- a/SL/IC.pm
+++ b/SL/IC.pm
@@ -35,6 +35,7 @@
 package IC;
 
 use Data::Dumper;
+use List::MoreUtils qw(all);
 use YAML;
 
 use SL::DBUtils;
@@ -80,10 +81,11 @@ sub get_part {
     $query =
       qq|SELECT p.id, p.partnumber, p.description,
            p.sellprice, p.lastcost, p.weight, a.qty, a.bom, p.unit,
-           pg.partsgroup
+           pg.partsgroup, p.price_factor_id, pfac.factor AS price_factor
          FROM parts p
          JOIN assembly a ON (a.parts_id = p.id)
          LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
+         LEFT JOIN price_factors pfac ON pfac.id = p.price_factor_id
          WHERE (a.id = ?)
          ORDER BY $oid{$myconfig->{dbdriver}}|;
     $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
@@ -307,6 +309,8 @@ sub save {
 
   my ($query, $sth);
 
+  my $priceupdate = ', priceupdate = current_date';
+
   if ($form->{id}) {
 
     # get old price
@@ -337,6 +341,11 @@ sub save {
     # delete translations
     do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
 
+    # Check whether or not the prices have changed. If they haven't
+    # then 'priceupdate' should not be updated.
+    my $previous_values = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM parts WHERE id = ?|, conv_i($form->{id})) || {};
+    $priceupdate        = '' if (all { $previous_values->{$_} == $form->{$_} } qw(sellprice lastcost listprice));
+
   } else {
     my ($count) = selectrow_query($form, $dbh, qq|SELECT COUNT(*) FROM parts WHERE partnumber = ?|, $form->{partnumber});
     if ($count) {
@@ -392,7 +401,6 @@ sub save {
          sellprice = ?,
          lastcost = ?,
          weight = ?,
-         priceupdate = ?,
          unit = ?,
          notes = ?,
          formel = ?,
@@ -414,6 +422,7 @@ sub save {
          microfiche = ?,
          partsgroup_id = ?,
          price_factor_id = ?
+         $priceupdate
        WHERE id = ?|;
   @values = ($form->{partnumber},
              $form->{description},
@@ -423,7 +432,6 @@ sub save {
              $form->{sellprice},
              $form->{lastcost},
              $form->{weight},
-             conv_date($form->{priceupdate}),
              $form->{unit},
              $form->{notes},
              $form->{formel},
@@ -690,9 +698,11 @@ sub assembly_item {
   my $dbh = $form->dbconnect($myconfig);
 
   my $query =
-    qq|SELECT p.id, p.partnumber, p.description, p.sellprice, p.weight, p.onhand, p.unit, pg.partsgroup
+    qq|SELECT p.id, p.partnumber, p.description, p.sellprice, p.weight, p.onhand, p.unit, pg.partsgroup,
+              p.price_factor_id, pfac.factor AS price_factor
        FROM parts p
        LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
+       LEFT JOIN price_factors pfac ON pfac.id = p.price_factor_id
        WHERE $where|;
   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
@@ -793,6 +803,10 @@ sub all_parts {
   my @join_order = qw(partsgroup makemodel invoice_oi apoe cv pfac);
   my %joins_needed;
 
+  if (($form->{searchitems} eq 'assembly') && $form->{l_lastcost}) {
+    @simple_l_switches = grep { $_ ne 'lastcost' } @simple_l_switches;
+  }
+
   #===== switches and simple filters ========#
 
   my @select_tokens = qw(id factor);
@@ -845,6 +859,12 @@ sub all_parts {
     push @where_tokens, 'p.onhand < p.rop',            if /short/;
   }
 
+  my $q_assembly_lastcost =
+    qq|(SELECT SUM(a_lc.qty * p_lc.lastcost / COALESCE(pfac_lc.factor, 1))
+        FROM assembly a_lc
+        LEFT JOIN parts p_lc            ON (a_lc.parts_id        = p_lc.id)
+        LEFT JOIN price_factors pfac_lc ON (p_lc.price_factor_id = pfac_lc.id)
+        WHERE (a_lc.id = p.id)) AS lastcost|;
 
   my @sort_cols = (@simple_filters, qw(id bin priceupdate onhand invnumber ordnumber quonumber name serialnumber soldtotal deliverydate));
   $form->{sort} = 'id' unless grep { $form->{"l_$_"} } grep { $form->{sort} eq $_ } @sort_cols;
@@ -871,6 +891,7 @@ sub all_parts {
   my @bsooqr;
   push @select_tokens, @qsooqr_flags                                          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};
   push @bsooqr_tokens, q|module = 'is' AND NOT ioi.assemblyitem|              if $form->{sold};
   push @bsooqr_tokens, q|module = 'oe' AND NOT quotation AND cv = 'customer'| if $form->{ordered};
@@ -903,6 +924,7 @@ sub all_parts {
   if ($form->{l_soldtotal}) {
     push @where_tokens, 'ioi.qty >= 0';
     push @group_tokens, @select_tokens;
+     map { s/.*\sAS\s+//si } @group_tokens;
     push @select_tokens, 'SUM(ioi.qty)';
   }
 
@@ -916,10 +938,13 @@ sub all_parts {
      ordnumber    => 'apoe.', make         => 'mm.',
      quonumber    => 'apoe.', model        => 'mm.',
      invnumber    => 'apoe.', partsgroup   => 'pg.',
+     lastcost     => ' ',
      factor       => 'pfac.',
      'SUM(ioi.qty)' => ' ',
   );
 
+  $table_prefix{$q_assembly_lastcost} = ' ';
+
   my %renamed_columns = (
     'factor'       => 'price_factor',
     'SUM(ioi.qty)' => 'soldtotal',