Einlagern: Bei einem Fehler die eingebenen Werte in der Maske wieder herstellen
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Mon, 30 Nov 2015 16:27:22 +0000 (17:27 +0100)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Mon, 30 Nov 2015 16:27:22 +0000 (17:27 +0100)
Zwei Dinge:
Zum einen einen $::form->error abfangen und Fehler auch mit flash ausgeben,
wenn das Speichern der Lagerbewegung fehl schlägt (passiert z.B. wenn man ein
falsches Datumsformat beim Mindesthaltbarkeitsdatum eingibt).

Zum anderen bei einem Fehler dem redirect die ursprünglich eingegebenen Werte
wieder mitgeben, damit man den Fehler korrigieren und dann einlagern kann.

SL/Controller/Inventory.pm
templates/webpages/inventory/warehouse_selection_stock.html

index a0764ab..ceebf68 100644 (file)
@@ -15,6 +15,8 @@ use SL::Presenter;
 use SL::DBUtils;
 use SL::Helper::Flash;
 
+use English qw(-no_match_vars);
+
 use Rose::Object::MakeMethods::Generic (
   'scalar --get_set_init' => [ qw(warehouses units p) ],
   'scalar'                => [ qw(warehouse bin unit part) ],
@@ -43,34 +45,49 @@ sub action_stock_in {
 sub action_stock {
   my ($self) = @_;
 
+  my $transfer_error;
   my $qty = $::form->parse_amount(\%::myconfig, $::form->{qty});
   if (!$qty) {
-    flash_later('error', t8('Cannot stock without amount'));
+    $transfer_error = t8('Cannot stock without amount');
   } elsif ($qty < 0) {
-    flash_later('error', t8('Cannot stock negative amounts'));
+    $transfer_error = t8('Cannot stock negative amounts');
   } else {
     # do stock
-    WH->transfer({
-      parts         => $self->part,
-      dst_bin       => $self->bin,
-      dst_wh        => $self->warehouse,
-      qty           => $qty,
-      unit          => $self->unit,
-      transfer_type => 'stock',
-      chargenumber  => $::form->{chargenumber},
-      bestbefore    => $::form->{bestbefore},
-      ean           => $::form->{ean},
-      comment       => $::form->{comment},
+    $::form->throw_on_error(sub {
+      eval {
+        WH->transfer({
+          parts         => $self->part,
+          dst_bin       => $self->bin,
+          dst_wh        => $self->warehouse,
+          qty           => $qty,
+          unit          => $self->unit,
+          transfer_type => 'stock',
+          chargenumber  => $::form->{chargenumber},
+          bestbefore    => $::form->{bestbefore},
+          ean           => $::form->{ean},
+          comment       => $::form->{comment},
+        });
+        1;
+      } or do { $transfer_error = $EVAL_ERROR->getMessage; }
     });
 
-    if ($::form->{write_default_bin}) {
-      $self->part->load;   # onhand is calculated in between. don't mess that up
-      $self->part->bin($self->bin);
-      $self->part->warehouse($self->warehouse);
-      $self->part->save;
+    if (!$transfer_error) {
+      if ($::form->{write_default_bin}) {
+        $self->part->load;   # onhand is calculated in between. don't mess that up
+        $self->part->bin($self->bin);
+        $self->part->warehouse($self->warehouse);
+        $self->part->save;
+      }
+
+      flash_later('info', t8('Transfer successful'));
     }
+  }
 
-    flash_later('info', t8('Transfer successful'));
+  my %additional_redirect_params = ();
+  if ($transfer_error) {
+    flash_later('error', $transfer_error);
+    $additional_redirect_params{$_}  = $::form->{$_} for qw(qty chargenumber bestbefore ean comment);
+    $additional_redirect_params{qty} = $qty;
   }
 
   # redirect
@@ -80,6 +97,7 @@ sub action_stock {
     bin_id       => $self->bin->id,
     warehouse_id => $self->warehouse->id,
     unit_id      => $self->unit->id,
+    %additional_redirect_params,
   );
 }
 
index 5901f6d..e311991 100644 (file)
 
   <tr>
    <th align="right" nowrap>[% 'Charge number' | $T8 %]</th>
-   <td>[% L.input_tag('chargenumber', SELF.chargenumber, size=30) %]</td>
+   <td>[% L.input_tag('chargenumber', FORM.chargenumber, size=30) %]</td>
   </tr>
 
 [% IF INSTANCE_CONF.get_show_bestbefore %]
   <tr>
    <th align="right" nowrap>[% 'Best Before' | $T8 %]</th>
-   <td>[% L.date_tag('bestbefore', SELF.bestbefore) %]</td>
+   <td>[% L.date_tag('bestbefore', FORM.bestbefore) %]</td>
   </tr>
 [%- END %]
 
   <tr>
    <th align="right" nowrap>[% 'EAN' | $T8 %]</th>
-   <td><input name="ean" size="30" value="[% HTML.escape(ean) %]"></td>
+   <td>[% L.input_tag('ean', FORM.ean, size=30) %]</td>
   </tr>
 
   <tr>
    <th align="right" nowrap>[% 'Quantity' | $T8 %]</th>
    <td>
-    <input name="qty" size="10" value="[% HTML.escape(LxERP.format_amount(qty)) %]">
+    [% L.input_tag('qty', LxERP.format_amount(FORM.qty), size=10) %]
 [%- IF SELF.part.unit %]
     [% L.select_tag('unit_id', SELF.part.available_units, title_key='name', default=SELF.unit.id) %]
 [%- ELSE %]
@@ -58,7 +58,7 @@
 
   <tr>
    <th align="right" nowrap>[% 'Optional comment' | $T8 %]</th>
-   <td><input name="comment" size="60" value="[% HTML.escape(comment) %]"></td>
+   <td>[% L.input_tag('comment', FORM.comment, size=60) %]</td>
   </tr>
  </table>