Auth: DBUtils nicht den namespace vollmüllen lassen
[kivitendo-erp.git] / SL / Controller / Inventory.pm
index 4336c71..ceebf68 100644 (file)
@@ -11,13 +11,14 @@ use SL::DB::Warehouse;
 use SL::DB::Unit;
 use SL::WH;
 use SL::Locale::String qw(t8);
-use SL::ClientJS;
 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 js p) ],
+  'scalar --get_set_init' => [ qw(warehouses units p) ],
   'scalar'                => [ qw(warehouse bin unit part) ],
 );
 
@@ -28,6 +29,7 @@ __PACKAGE__->run_before('load_unit_from_form',   only => [ qw(stock_in part_chan
 __PACKAGE__->run_before('load_wh_from_form',     only => [ qw(stock_in warehouse_changed stock) ]);
 __PACKAGE__->run_before('load_bin_from_form',    only => [ qw(stock_in stock) ]);
 __PACKAGE__->run_before('set_target_from_part',  only => [ qw(part_changed) ]);
+__PACKAGE__->run_before('mini_stock',            only => [ qw(stock_in mini_stock) ]);
 __PACKAGE__->run_before('sanitize_target',       only => [ qw(stock_in warehouse_changed part_changed) ]);
 __PACKAGE__->run_before('set_layout');
 
@@ -43,26 +45,50 @@ sub action_stock_in {
 sub action_stock {
   my ($self) = @_;
 
-  # do stock
-  WH->transfer({
-    parts         => $self->part,
-    dst_bin       => $self->bin,
-    dst_wh        => $self->warehouse,
-    qty           => $::form->format_amount(\%::myconfig, $::form->{qty}),
-    unit          => $self->unit,
-    transfer_type => 'stock',
-    chargenumber  => $::form->{chargenumber},
-    ean           => $::form->{ean},
-    comment       => $::form->{comment},
-  });
-
-  if ($::form->{write_default_bin}) {
-    $self->part->bin($self->bin);
-    $self->part->warehouse($self->warehouse);
-    $self->part->save;
+  my $transfer_error;
+  my $qty = $::form->parse_amount(\%::myconfig, $::form->{qty});
+  if (!$qty) {
+    $transfer_error = t8('Cannot stock without amount');
+  } elsif ($qty < 0) {
+    $transfer_error = t8('Cannot stock negative amounts');
+  } else {
+    # do stock
+    $::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 (!$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
   $self->redirect_to(
@@ -70,6 +96,8 @@ sub action_stock {
     part_id      => $self->part->id,
     bin_id       => $self->bin->id,
     warehouse_id => $self->warehouse->id,
+    unit_id      => $self->unit->id,
+    %additional_redirect_params,
   );
 }
 
@@ -89,7 +117,7 @@ sub action_part_changed {
     ->replaceWith('#bin_id', $self->build_bin_select)
     ->replaceWith('#unit_id', $self->build_unit_select)
     ->focus('#warehouse_id')
-    ->render($self);
+    ->render;
 }
 
 sub action_warehouse_changed {
@@ -98,26 +126,15 @@ sub action_warehouse_changed {
   $self->js
     ->replaceWith('#bin_id', $self->build_bin_select)
     ->focus('#bin_id')
-    ->render($self);
+    ->render;
 }
 
 sub action_mini_stock {
   my ($self) = @_;
 
-  my $stock        = $self->part->get_simple_stock;
-  my $stock_by_bin = { map { $_->{bin_id} => $_ } @$stock };
-  my $stock_empty  = ! grep { $_->{sum} * 1 } @$stock;
-
   $self->js
-    ->html('#stock', $self->render('inventory/_stock', { output => 0 }, stock => $stock_by_bin, stock_empty => $stock_empty ))
-    ->render($self);
-}
-
-sub action_last_journal {
-  my ($self) = @_;
-
-#  my $jounal = $self->journal;
-
+    ->html('#stock', $self->render('inventory/_stock', { output => 0 }))
+    ->render;
 }
 
 #================================================================
@@ -131,17 +148,13 @@ sub _check_warehouses {
 }
 
 sub init_warehouses {
-  SL::DB::Manager::Warehouse->get_all;
+  SL::DB::Manager::Warehouse->get_all(query => [ or => [ invalid => 0, invalid => undef ]]);
 }
 
 sub init_units {
   SL::DB::Manager::Unit->get_all;
 }
 
-sub init_js {
-  SL::ClientJS->new;
-}
-
 sub init_p {
   SL::Presenter->get;
 }
@@ -158,8 +171,8 @@ sub set_target_from_part {
 sub sanitize_target {
   my ($self) = @_;
 
-  $self->warehouse(SL::DB::Manager::Warehouse->get_first) if !$self->warehouse || !$self->warehouse->id;
-  $self->bin      ($self->warehouse->bins->[0])           if !$self->bin       || !$self->bin->id;
+  $self->warehouse($self->warehouses->[0])       if !$self->warehouse || !$self->warehouse->id;
+  $self->bin      ($self->warehouse->bins->[0])  if !$self->bin       || !$self->bin->id;
 }
 
 sub load_part_from_form {
@@ -216,7 +229,7 @@ sub mini_journal {
   my @ids = selectall_array_query($::form, $::form->get_standard_dbh, $query);
 
   my $objs;
-  $obj = SL::DB::Manager::Inventory->get_all(query => [ trans_id => \@ids ]) if @ids;
+  $objs = SL::DB::Manager::Inventory->get_all(query => [ trans_id => \@ids ]) if @ids;
 
   # at most 2 of them belong to a transaction and the qty determins in or out.
   # sort them for display
@@ -231,12 +244,20 @@ sub mini_journal {
   return \@sorted;
 }
 
-sub show_no_warehouse_error {
+sub mini_stock {
+  my ($self) = @_;
+
+  my $stock             = $self->part->get_simple_stock;
+  $self->{stock_by_bin} = { map { $_->{bin_id} => $_ } @$stock };
+  $self->{stock_empty}  = ! grep { $_->{sum} * 1 } @$stock;
+}
+
+sub show_no_warehouses_error {
   my ($self) = @_;
 
   my $msg = t8('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
 
-  if ($::auth->check_right($::form->{login}, 'config')) { # TODO wut?
+  if ($::auth->check_right($::myconfig{login}, 'config')) { # TODO wut?
     $msg .= t8('You can create warehouses and bins via the menu "System -> Warehouses".');
   } else {
     $msg .= t8('Please ask your administrator to create warehouses and bins.');