Persistente delivery_order_items_(stocK)_id
authorJan Büren <jan@kivitendo-premium.de>
Mon, 5 Jan 2015 12:54:43 +0000 (13:54 +0100)
committerJan Büren <jan@kivitendo-premium.de>
Mon, 5 Jan 2015 12:54:43 +0000 (13:54 +0100)
id in tabelle delivery_order_items und
id in tabelle delivery_order_items_stock persistent gemacht.

Testszenarien:

delivery_order_items:
Einkauf / Verkauf-LS -> als neu speichern.  => i.O.
Einlagern                                   => i.O.
Mittlere Position löschen                   => i.O.

delivery_order_items_stock:
Mittlere Position löschen                   => n.i.O.
als neu Speichern                           => i.O.
Einlagern                                   => i.O. (Lieferschein kann standardmässig nicht mehr verändert werden)

SL/DO.pm
bin/mozilla/do.pl
bin/mozilla/io.pl
templates/webpages/do/stock_in_form.html
templates/webpages/do/stock_out_form.html

index 478a125..57e971e 100644 (file)
--- a/SL/DO.pm
+++ b/SL/DO.pm
@@ -240,14 +240,11 @@ sub save {
 
   my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
 
-  if ($form->{id}) {
-
-    $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
-    do_query($form, $dbh, $query, conv_i($form->{id}));
+  my (@processed_doi, @processed_dois);
 
-    $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
-    do_query($form, $dbh, $query, conv_i($form->{id}));
+  if ($form->{id}) {
 
+    # only delete shipto complete
     $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
     do_query($form, $dbh, $query, conv_i($form->{id}));
 
@@ -275,24 +272,23 @@ sub save {
     $query         = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
     %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
   }
-
-  my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
-  my $h_item_id = prepare_query($form, $dbh, $q_item_id);
-
-  my $q_item =
-    qq|INSERT INTO delivery_order_items (
-         id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
-         sellprice, discount, unit, reqdate, project_id, serialnumber,
-         ordnumber, transdate, cusordnumber,
-         lastcost, price_factor_id, price_factor, marge_price_factor, pricegroup_id,
-         active_price_source, active_discount_source)
-       VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
-         (SELECT factor FROM price_factors WHERE id = ?), ?, ?, ?, ?)|;
+  my $q_item = <<SQL;
+    UPDATE delivery_order_items SET
+       delivery_order_id = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?, base_qty = ?,
+       sellprice = ?, discount = ?, unit = ?, reqdate = ?, project_id = ?, serialnumber = ?,
+       ordnumber = ?, transdate = ?, cusordnumber = ?,
+       lastcost = ? , price_factor_id = ?, price_factor = (SELECT factor FROM price_factors where id = ?),
+       marge_price_factor = ?, pricegroup_id = ?, active_price_source = ?, active_discount_source = ?
+    WHERE id = ?
+SQL
   my $h_item = prepare_query($form, $dbh, $q_item);
 
-  my $q_item_stock =
-    qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber, bestbefore)
-       VALUES (?, ?, ?, ?, ?, ?, ?)|;
+  my $q_item_stock = <<SQL;
+    UPDATE delivery_order_items_stock SET
+      delivery_order_item_id = ?, qty = ?,  unit = ?,  warehouse_id = ?,
+      bin_id = ?, chargenumber = ?, bestbefore = ?
+    WHERE id = ?
+SQL
   my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
 
   my $in_out       = $form->{type} =~ /^sales/ ? 'out' : 'in';
@@ -300,6 +296,17 @@ sub save {
   for my $i (1 .. $form->{rowcount}) {
     next if (!$form->{"id_$i"});
 
+    if (!$form->{"delivery_order_items_id_$i"}) {
+      # there is no persistent id, therefore create one with all necessary constraints
+      my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
+      my $h_item_id = prepare_query($form, $dbh, $q_item_id);
+      do_statement($form, $h_item_id, $q_item_id);
+      $form->{"delivery_order_items_id_$i"}  = $h_item_id->fetchrow_array();
+      $query = qq|INSERT INTO delivery_order_items (id, delivery_order_id, parts_id) VALUES (?, ?, ?)|;
+      do_query($form, $dbh, $query, conv_i($form->{"delivery_order_items_id_$i"}),
+                conv_i($form->{"id"}), conv_i($form->{"id_$i"}));
+      $h_item_id->finish();
+    }
     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
     my $item_unit = $part_unit_map{$form->{"id_$i"}};
@@ -320,8 +327,6 @@ sub save {
 
     $items_reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
 
-    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"
@@ -331,7 +336,7 @@ sub save {
     $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"}),
+    @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}),
                $form->{"description_$i"}, $restricter->process($form->{"longdescription_$i"}),
                $form->{"qty_$i"}, $baseqty,
                $form->{"sellprice_$i"}, $form->{"discount_$i"} / 100,
@@ -343,20 +348,36 @@ sub save {
                conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
                conv_i($form->{"marge_price_factor_$i"}),
                $pricegroup_id,
-               $form->{"active_price_source_$i"}, $form->{"active_discount_source_$i"});
+               $form->{"active_price_source_$i"}, $form->{"active_discount_source_$i"},
+               conv_i($form->{"delivery_order_items_id_$i"}));
     do_statement($form, $h_item, $q_item, @values);
+    push @processed_doi, $form->{"delivery_order_items_id_$i"}; # transaction safe?
 
     my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
 
     foreach my $sinfo (@{ $stock_info }) {
-      @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
-                 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber}, conv_date($sinfo->{bestbefore}));
+      # if we have stock_info, we have to check for persistents entries
+      if (!$sinfo->{"delivery_order_items_stock_id"}) {
+        my $q_item_stock_id = qq|SELECT nextval('id')|;
+        my $h_item_stock_id = prepare_query($form, $dbh, $q_item_stock_id);
+        do_statement($form, $h_item_stock_id, $q_item_stock_id);
+        $sinfo->{"delivery_order_items_stock_id"} = $h_item_stock_id->fetchrow_array();
+        $query = qq|INSERT INTO delivery_order_items_stock (id, delivery_order_item_id, qty, unit, warehouse_id, bin_id)
+                    VALUES (?, ?, ?, ?, ?, ?)|;
+        do_query($form, $dbh, $query, conv_i($sinfo->{"delivery_order_items_stock_id"}),
+                  conv_i($form->{"delivery_order_items_id_$i"}), $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
+                  conv_i($sinfo->{bin_id}));
+       $h_item_stock_id->finish();
+      }
+      @values = ($form->{"delivery_order_items_id_$i"}, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
+                 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber}, conv_date($sinfo->{bestbefore}),
+                 conv_i($sinfo->{"delivery_order_items_stock_id"}));
       do_statement($form, $h_item_stock, $q_item_stock, @values);
     }
 
     CVar->save_custom_variables(module       => 'IC',
                                 sub_module   => 'delivery_order_items',
-                                trans_id     => $item_id,
+                                trans_id     => $form->{"delivery_order_items_id_$i"},
                                 configs      => $ic_cvar_configs,
                                 variables    => $form,
                                 name_prefix  => 'ic_',
@@ -364,7 +385,15 @@ sub save {
                                 dbh          => $dbh);
   }
 
-  $h_item_id->finish();
+  # search for orphaned doi
+  $query  = sprintf 'SELECT id FROM delivery_order_items WHERE delivery_order_id = ? AND NOT id IN (%s)', join ', ', ("?") x scalar @processed_doi;
+  @values = (conv_i($form->{id}), map { conv_i($_) } @processed_doi);
+  my @orphaned_ids = map { $_->{id} } selectall_hashref_query($form, $dbh, $query, @values);
+  if (scalar @orphaned_ids) {
+    # clean up delivery_order_items
+    $query  = sprintf 'DELETE FROM delivery_order_items WHERE id IN (%s)', join ', ', ("?") x scalar @orphaned_ids;
+    do_query($form, $dbh, $query, @orphaned_ids);
+  }
   $h_item->finish();
   $h_item_stock->finish();
 
@@ -717,7 +746,8 @@ sub retrieve {
     my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
 
     $query =
-      qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber, bestbefore
+      qq|SELECT id as delivery_order_items_stock_id, qty, unit, bin_id,
+                warehouse_id, chargenumber, bestbefore
          FROM delivery_order_items_stock
          WHERE delivery_order_item_id = ?|;
     my $sth = prepare_query($form, $dbh, $query);
index 36e5154..69854e2 100644 (file)
@@ -960,7 +960,7 @@ sub save_as_new {
   $form->{delivered} = 0;
   map { delete $form->{$_} } qw(printed emailed queued);
   delete @{ $form }{ grep { m/^stock_(?:in|out)_\d+/ } keys %{ $form } };
-
+  delete $form->{"delivery_order_items_id_$_"} for 1 .. $form->{"rowcount"};
   # Let kivitendo assign a new order number if the user hasn't changed the
   # previous one. If it has been changed manually then use it as-is.
   $form->{donumber} =~ s/^\s*//g;
@@ -1172,7 +1172,7 @@ sub set_stock_in {
 
     next if ($form->{"qty_$i"} <= 0);
 
-    push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber bestbefore qty unit) };
+    push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(delivery_order_items_stock_id warehouse_id bin_id chargenumber bestbefore qty unit) };
   }
 
   $form->{stock} = YAML::Dump($stock_info);
@@ -1221,7 +1221,7 @@ sub stock_out_form {
                  ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
                  ($row->{bestbefore}   ne $sinfo->{bestbefore}));
 
-        map { $row->{"stock_$_"} = $sinfo->{$_} } qw(qty unit error);
+        map { $row->{"stock_$_"} = $sinfo->{$_} } qw(qty unit error delivery_order_items_stock_id);
       }
     }
 
@@ -1263,6 +1263,7 @@ sub set_stock_out {
       'qty'          => $form->{"qty_$i"},
       'unit'         => $form->{"unit_$i"},
       'row'          => $i,
+      'delivery_order_items_stock_id'  => $form->{"delivery_order_items_stock_id_$i"},
     };
   }
 
index 849e8df..eb5c60a 100644 (file)
@@ -437,6 +437,7 @@ sub display_row {
       map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
       push @hidden_vars, grep { defined $form->{"${_}_${i}"} } qw(sellprice discount not_discountable price_factor_id lastcost);
       push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
+      push @hidden_vars, qw(delivery_order_items_id);
     }
 
     my @HIDDENS = map { value => $_}, (
@@ -726,7 +727,7 @@ sub remove_emptied_rows {
                 transdate longdescription basefactor marge_total marge_percent
                 marge_price_factor lastcost price_factor_id partnotes
                 stock_out stock_in has_sernumber reqdate orderitems_id
-                active_price_source active_discount_source);
+                active_price_source active_discount_source delivery_order_items_id);
 
   my $ic_cvar_configs = CVar->get_configs(module => 'IC');
   push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
index 5a8b18d..08384f7 100644 (file)
        [%- END %]
       </select>
      </td>
-
+     <input type="hidden" name="delivery_order_items_stock_id_[% loop.count %]" value="[% HTML.escape(row.delivery_order_items_stock_id) %]">
      [%- END %]
     </tr>
 
index 3aab098..0e33bd1 100644 (file)
     <input type="hidden" name="warehouse_id_[% loop.count %]" value="[% HTML.escape(row.warehouse_id) %]">
     <input type="hidden" name="bin_id_[% loop.count %]"       value="[% HTML.escape(row.bin_id) %]">
     <input type="hidden" name="chargenumber_[% loop.count %]" value="[% HTML.escape(row.chargenumber) %]">
+    <input type="hidden" name="delivery_order_items_stock_id_[% loop.count %]" value="[% HTML.escape(row.stock_delivery_order_items_stock_id) %]">
     [% IF INSTANCE_CONF.get_show_bestbefore %]
     <input type="hidden" name="bestbefore_[% loop.count %]" value="[% HTML.escape(row.bestbefore) %]">
     [% END %]