1 package SL::Controller::Inventory;
6 use parent qw(SL::Controller::Base);
10 use SL::DB::Warehouse;
13 use SL::Locale::String qw(t8);
17 use SL::Helper::Flash;
19 use Rose::Object::MakeMethods::Generic (
20 'scalar --get_set_init' => [ qw(warehouses units js p) ],
21 'scalar' => [ qw(warehouse bin unit part) ],
24 __PACKAGE__->run_before('_check_auth');
25 __PACKAGE__->run_before('_check_warehouses');
26 __PACKAGE__->run_before('load_part_from_form', only => [ qw(stock_in part_changed mini_stock stock) ]);
27 __PACKAGE__->run_before('load_unit_from_form', only => [ qw(stock_in part_changed mini_stock stock) ]);
28 __PACKAGE__->run_before('load_wh_from_form', only => [ qw(stock_in warehouse_changed stock) ]);
29 __PACKAGE__->run_before('load_bin_from_form', only => [ qw(stock_in stock) ]);
30 __PACKAGE__->run_before('set_target_from_part', only => [ qw(part_changed) ]);
31 __PACKAGE__->run_before('sanitize_target', only => [ qw(stock_in warehouse_changed part_changed) ]);
32 __PACKAGE__->run_before('set_layout');
37 $::form->{title} = t8('Stock');
39 $::request->layout->focus('#part_id_name');
40 $_[0]->render('inventory/warehouse_selection_stock', title => $::form->{title});
49 dst_bin => $self->bin,
50 dst_wh => $self->warehouse,
51 qty => $::form->format_amount(\%::myconfig, $::form->{qty}),
53 transfer_type => 'stock',
54 chargenumber => $::form->{chargenumber},
55 ean => $::form->{ean},
56 comment => $::form->{comment},
59 if ($::form->{write_default_bin}) {
60 $self->part->bin($self->bin);
61 $self->part->warehouse($self->warehouse);
65 flash_later('info', t8('Transfer successful'));
70 part_id => $self->part->id,
71 bin_id => $self->bin->id,
72 warehouse_id => $self->warehouse->id,
76 sub action_part_changed {
79 # no standard? ask user if he wants to write it
80 if ($self->part->id && !$self->part->bin_id && !$self->part->warehouse_id) {
81 $self->js->show('#write_default_bin_span');
83 $self->js->hide('#write_default_bin_span')
84 ->removeAttr('#write_default_bin', 'checked');
88 ->replaceWith('#warehouse_id', $self->build_warehouse_select)
89 ->replaceWith('#bin_id', $self->build_bin_select)
90 ->replaceWith('#unit_id', $self->build_unit_select)
91 ->focus('#warehouse_id')
95 sub action_warehouse_changed {
99 ->replaceWith('#bin_id', $self->build_bin_select)
104 sub action_mini_stock {
107 my $stock = $self->part->get_simple_stock;
108 my $stock_by_bin = { map { $_->{bin_id} => $_ } @$stock };
109 my $stock_empty = ! grep { $_->{sum} * 1 } @$stock;
112 ->html('#stock', $self->render('inventory/_stock', { output => 0 }, stock => $stock_by_bin, stock_empty => $stock_empty ))
116 #================================================================
119 $main::auth->assert('warehouse_management');
122 sub _check_warehouses {
123 $_[0]->show_no_warehouses_error if !@{ $_[0]->warehouses };
126 sub init_warehouses {
127 SL::DB::Manager::Warehouse->get_all;
131 SL::DB::Manager::Unit->get_all;
142 sub set_target_from_part {
145 return if !$self->part;
147 $self->warehouse($self->part->warehouse) if $self->part->warehouse;
148 $self->bin( $self->part->bin) if $self->part->bin;
151 sub sanitize_target {
154 $self->warehouse(SL::DB::Manager::Warehouse->get_first) if !$self->warehouse || !$self->warehouse->id;
155 $self->bin ($self->warehouse->bins->[0]) if !$self->bin || !$self->bin->id;
158 sub load_part_from_form {
159 $_[0]->part(SL::DB::Manager::Part->find_by_or_create(id => $::form->{part_id}));
162 sub load_unit_from_form {
163 $_[0]->unit(SL::DB::Manager::Unit->find_by_or_create(id => $::form->{unit_id}));
166 sub load_wh_from_form {
167 $_[0]->warehouse(SL::DB::Manager::Warehouse->find_by_or_create(id => $::form->{warehouse_id}));
170 sub load_bin_from_form {
171 $_[0]->bin(SL::DB::Manager::Bin->find_by_or_create(id => $::form->{bin_id}));
175 $::request->layout->add_javascripts('client_js.js');
178 sub build_warehouse_select {
179 $_[0]->p->select_tag('warehouse_id', $_[0]->warehouses,
180 title_key => 'description',
181 default => $_[0]->warehouse->id,
182 onchange => 'reload_bin_selection()',
186 sub build_bin_select {
187 $_[0]->p->select_tag('bin_id', [ $_[0]->warehouse->bins ],
188 title_key => 'description',
189 default => $_[0]->bin->id,
193 sub build_unit_select {
195 ? $_[0]->p->select_tag('unit_id', $_[0]->part->available_units,
197 default => $_[0]->part->unit_obj->id,
199 : $_[0]->p->select_tag('unit_id', $_[0]->units,
207 # get last 10 transaction ids
208 my $query = 'SELECT trans_id, max(itime) FROM inventory GROUP BY trans_id ORDER BY max(itime) DESC LIMIT 10';
209 my @ids = selectall_array_query($::form, $::form->get_standard_dbh, $query);
212 $objs = SL::DB::Manager::Inventory->get_all(query => [ trans_id => \@ids ]) if @ids;
214 # at most 2 of them belong to a transaction and the qty determins in or out.
215 # sort them for display
218 $transactions{ $_->trans_id }{ $_->qty > 0 ? 'in' : 'out' } = $_;
219 $transactions{ $_->trans_id }{base} = $_;
221 # and get them into order again
222 my @sorted = map { $transactions{$_} } @ids;
227 sub show_no_warehouse_error {
230 my $msg = t8('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
232 if ($::auth->check_right($::form->{login}, 'config')) { # TODO wut?
233 $msg .= t8('You can create warehouses and bins via the menu "System -> Warehouses".');
235 $msg .= t8('Please ask your administrator to create warehouses and bins.');
237 $::form->show_generic_error($msg);