kivitendo/SL/Controller/Inventory.pm @ 79b7fc43
666d4cad | Sven Schöling | package SL::Controller::Inventory;
|
||
use strict;
|
||||
use warnings;
|
||||
use parent qw(SL::Controller::Base);
|
||||
use SL::DB::Inventory;
|
||||
use SL::DB::Part;
|
||||
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 Rose::Object::MakeMethods::Generic (
|
||||
'scalar --get_set_init' => [ qw(warehouses units js p) ],
|
||||
'scalar' => [ qw(warehouse bin unit part) ],
|
||||
);
|
||||
__PACKAGE__->run_before('_check_auth');
|
||||
__PACKAGE__->run_before('_check_warehouses');
|
||||
__PACKAGE__->run_before('load_part_from_form', only => [ qw(stock_in part_changed mini_stock stock) ]);
|
||||
__PACKAGE__->run_before('load_unit_from_form', only => [ qw(stock_in part_changed mini_stock stock) ]);
|
||||
__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) ]);
|
||||
5f543a5e | Sven Schöling | __PACKAGE__->run_before('mini_stock', only => [ qw(stock_in mini_stock) ]);
|
||
666d4cad | Sven Schöling | __PACKAGE__->run_before('sanitize_target', only => [ qw(stock_in warehouse_changed part_changed) ]);
|
||
__PACKAGE__->run_before('set_layout');
|
||||
sub action_stock_in {
|
||||
my ($self) = @_;
|
||||
$::form->{title} = t8('Stock');
|
||||
$::request->layout->focus('#part_id_name');
|
||||
$_[0]->render('inventory/warehouse_selection_stock', title => $::form->{title});
|
||||
}
|
||||
sub action_stock {
|
||||
my ($self) = @_;
|
||||
8209ac91 | Sven Schöling | my $qty = $::form->parse_amount(\%::myconfig, $::form->{qty});
|
||
74a70e2a | Sven Schöling | if (!$qty) {
|
||
flash_later('error', t8('Cannot stock without amount'));
|
||||
} elsif ($qty < 0) {
|
||||
8209ac91 | Sven Schöling | flash_later('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},
|
||||
ean => $::form->{ean},
|
||||
comment => $::form->{comment},
|
||||
});
|
||||
666d4cad | Sven Schöling | |||
8209ac91 | Sven Schöling | 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'));
|
||||
}
|
||||
666d4cad | Sven Schöling | |||
# redirect
|
||||
$self->redirect_to(
|
||||
action => 'stock_in',
|
||||
part_id => $self->part->id,
|
||||
bin_id => $self->bin->id,
|
||||
warehouse_id => $self->warehouse->id,
|
||||
5491c513 | Sven Schöling | unit_id => $self->unit->id,
|
||
666d4cad | Sven Schöling | );
|
||
}
|
||||
sub action_part_changed {
|
||||
my ($self) = @_;
|
||||
# no standard? ask user if he wants to write it
|
||||
if ($self->part->id && !$self->part->bin_id && !$self->part->warehouse_id) {
|
||||
$self->js->show('#write_default_bin_span');
|
||||
} else {
|
||||
$self->js->hide('#write_default_bin_span')
|
||||
->removeAttr('#write_default_bin', 'checked');
|
||||
}
|
||||
$self->js
|
||||
->replaceWith('#warehouse_id', $self->build_warehouse_select)
|
||||
->replaceWith('#bin_id', $self->build_bin_select)
|
||||
->replaceWith('#unit_id', $self->build_unit_select)
|
||||
->focus('#warehouse_id')
|
||||
->render($self);
|
||||
}
|
||||
sub action_warehouse_changed {
|
||||
my ($self) = @_;
|
||||
$self->js
|
||||
->replaceWith('#bin_id', $self->build_bin_select)
|
||||
->focus('#bin_id')
|
||||
->render($self);
|
||||
}
|
||||
sub action_mini_stock {
|
||||
my ($self) = @_;
|
||||
$self->js
|
||||
5f543a5e | Sven Schöling | ->html('#stock', $self->render('inventory/_stock', { output => 0 }))
|
||
666d4cad | Sven Schöling | ->render($self);
|
||
}
|
||||
#================================================================
|
||||
sub _check_auth {
|
||||
$main::auth->assert('warehouse_management');
|
||||
}
|
||||
sub _check_warehouses {
|
||||
$_[0]->show_no_warehouses_error if !@{ $_[0]->warehouses };
|
||||
}
|
||||
sub init_warehouses {
|
||||
44655653 | Sven Schöling | SL::DB::Manager::Warehouse->get_all(query => [ or => [ invalid => 0, invalid => undef ]]);
|
||
666d4cad | Sven Schöling | }
|
||
sub init_units {
|
||||
SL::DB::Manager::Unit->get_all;
|
||||
}
|
||||
sub init_js {
|
||||
SL::ClientJS->new;
|
||||
}
|
||||
sub init_p {
|
||||
SL::Presenter->get;
|
||||
}
|
||||
sub set_target_from_part {
|
||||
my ($self) = @_;
|
||||
return if !$self->part;
|
||||
$self->warehouse($self->part->warehouse) if $self->part->warehouse;
|
||||
$self->bin( $self->part->bin) if $self->part->bin;
|
||||
}
|
||||
sub sanitize_target {
|
||||
my ($self) = @_;
|
||||
44655653 | Sven Schöling | $self->warehouse($self->warehouses->[0]) if !$self->warehouse || !$self->warehouse->id;
|
||
$self->bin ($self->warehouse->bins->[0]) if !$self->bin || !$self->bin->id;
|
||||
666d4cad | Sven Schöling | }
|
||
sub load_part_from_form {
|
||||
$_[0]->part(SL::DB::Manager::Part->find_by_or_create(id => $::form->{part_id}));
|
||||
}
|
||||
sub load_unit_from_form {
|
||||
$_[0]->unit(SL::DB::Manager::Unit->find_by_or_create(id => $::form->{unit_id}));
|
||||
}
|
||||
sub load_wh_from_form {
|
||||
$_[0]->warehouse(SL::DB::Manager::Warehouse->find_by_or_create(id => $::form->{warehouse_id}));
|
||||
}
|
||||
sub load_bin_from_form {
|
||||
$_[0]->bin(SL::DB::Manager::Bin->find_by_or_create(id => $::form->{bin_id}));
|
||||
}
|
||||
sub set_layout {
|
||||
$::request->layout->add_javascripts('client_js.js');
|
||||
}
|
||||
sub build_warehouse_select {
|
||||
$_[0]->p->select_tag('warehouse_id', $_[0]->warehouses,
|
||||
title_key => 'description',
|
||||
default => $_[0]->warehouse->id,
|
||||
onchange => 'reload_bin_selection()',
|
||||
)
|
||||
}
|
||||
sub build_bin_select {
|
||||
$_[0]->p->select_tag('bin_id', [ $_[0]->warehouse->bins ],
|
||||
title_key => 'description',
|
||||
default => $_[0]->bin->id,
|
||||
);
|
||||
}
|
||||
sub build_unit_select {
|
||||
$_[0]->part->id
|
||||
? $_[0]->p->select_tag('unit_id', $_[0]->part->available_units,
|
||||
title_key => 'name',
|
||||
default => $_[0]->part->unit_obj->id,
|
||||
)
|
||||
: $_[0]->p->select_tag('unit_id', $_[0]->units,
|
||||
title_key => 'name',
|
||||
)
|
||||
}
|
||||
sub mini_journal {
|
||||
my ($self) = @_;
|
||||
# get last 10 transaction ids
|
||||
my $query = 'SELECT trans_id, max(itime) FROM inventory GROUP BY trans_id ORDER BY max(itime) DESC LIMIT 10';
|
||||
my @ids = selectall_array_query($::form, $::form->get_standard_dbh, $query);
|
||||
6f3f13dd | Sven Schöling | my $objs;
|
||
ce3d9171 | Sven Schöling | $objs = SL::DB::Manager::Inventory->get_all(query => [ trans_id => \@ids ]) if @ids;
|
||
666d4cad | Sven Schöling | |||
# at most 2 of them belong to a transaction and the qty determins in or out.
|
||||
# sort them for display
|
||||
my %transactions;
|
||||
for (@$objs) {
|
||||
$transactions{ $_->trans_id }{ $_->qty > 0 ? 'in' : 'out' } = $_;
|
||||
$transactions{ $_->trans_id }{base} = $_;
|
||||
}
|
||||
# and get them into order again
|
||||
my @sorted = map { $transactions{$_} } @ids;
|
||||
return \@sorted;
|
||||
}
|
||||
5f543a5e | Sven Schöling | 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;
|
||||
}
|
||||
039226c3 | Sven Schöling | sub show_no_warehouses_error {
|
||
666d4cad | Sven Schöling | my ($self) = @_;
|
||
my $msg = t8('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
|
||||
4bd1e2f8 | Sven Schöling | if ($::auth->check_right($::myconfig{login}, 'config')) { # TODO wut?
|
||
666d4cad | Sven Schöling | $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.');
|
||||
}
|
||||
$::form->show_generic_error($msg);
|
||||
}
|
||||
1;
|