6392ecc0c0c4cee947321bbc1cadfda98f8701c6
[kivitendo-erp.git] / SL / Controller / Inventory.pm
1 package SL::Controller::Inventory;
2
3 use strict;
4 use warnings;
5
6 use parent qw(SL::Controller::Base);
7
8 use SL::DB::Inventory;
9 use SL::DB::Part;
10 use SL::DB::Warehouse;
11 use SL::DB::Unit;
12 use SL::WH;
13 use SL::Locale::String qw(t8);
14 use SL::Presenter;
15 use SL::DBUtils;
16 use SL::Helper::Flash;
17
18 use English qw(-no_match_vars);
19
20 use Rose::Object::MakeMethods::Generic (
21   'scalar --get_set_init' => [ qw(warehouses units p) ],
22   'scalar'                => [ qw(warehouse bin unit part) ],
23 );
24
25 __PACKAGE__->run_before('_check_auth');
26 __PACKAGE__->run_before('_check_warehouses');
27 __PACKAGE__->run_before('load_part_from_form',   only => [ qw(stock_in part_changed mini_stock stock) ]);
28 __PACKAGE__->run_before('load_unit_from_form',   only => [ qw(stock_in part_changed mini_stock stock) ]);
29 __PACKAGE__->run_before('load_wh_from_form',     only => [ qw(stock_in warehouse_changed stock) ]);
30 __PACKAGE__->run_before('load_bin_from_form',    only => [ qw(stock_in stock) ]);
31 __PACKAGE__->run_before('set_target_from_part',  only => [ qw(part_changed) ]);
32 __PACKAGE__->run_before('mini_stock',            only => [ qw(stock_in mini_stock) ]);
33 __PACKAGE__->run_before('sanitize_target',       only => [ qw(stock_in warehouse_changed part_changed) ]);
34 __PACKAGE__->run_before('set_layout');
35
36 sub action_stock_in {
37   my ($self) = @_;
38
39   $::form->{title}   = t8('Stock');
40
41   $::request->layout->focus('#part_id_name');
42   my $transfer_types = WH->retrieve_transfer_types('in');
43   map { $_->{description} = $main::locale->text($_->{description}) } @{ $transfer_types };
44   $self->render('inventory/warehouse_selection_stock', title => $::form->{title}, TRANSFER_TYPES => $transfer_types );
45 }
46
47 sub action_stock {
48   my ($self) = @_;
49
50   my $transfer_error;
51   my $qty = $::form->parse_amount(\%::myconfig, $::form->{qty});
52   if (!$qty) {
53     $transfer_error = t8('Cannot stock without amount');
54   } elsif ($qty < 0) {
55     $transfer_error = t8('Cannot stock negative amounts');
56   } else {
57     # do stock
58     $::form->throw_on_error(sub {
59       eval {
60         WH->transfer({
61           parts         => $self->part,
62           dst_bin       => $self->bin,
63           dst_wh        => $self->warehouse,
64           qty           => $qty,
65           unit          => $self->unit,
66           transfer_type => 'stock',
67           chargenumber  => $::form->{chargenumber},
68           bestbefore    => $::form->{bestbefore},
69           ean           => $::form->{ean},
70           comment       => $::form->{comment},
71         });
72         1;
73       } or do { $transfer_error = $EVAL_ERROR->getMessage; }
74     });
75
76     if (!$transfer_error) {
77       if ($::form->{write_default_bin}) {
78         $self->part->load;   # onhand is calculated in between. don't mess that up
79         $self->part->bin($self->bin);
80         $self->part->warehouse($self->warehouse);
81         $self->part->save;
82       }
83
84       flash_later('info', t8('Transfer successful'));
85     }
86   }
87
88   my %additional_redirect_params = ();
89   if ($transfer_error) {
90     flash_later('error', $transfer_error);
91     $additional_redirect_params{$_}  = $::form->{$_} for qw(qty chargenumber bestbefore ean comment);
92     $additional_redirect_params{qty} = $qty;
93   }
94
95   # redirect
96   $self->redirect_to(
97     action       => 'stock_in',
98     part_id      => $self->part->id,
99     bin_id       => $self->bin->id,
100     warehouse_id => $self->warehouse->id,
101     unit_id      => $self->unit->id,
102     %additional_redirect_params,
103   );
104 }
105
106 sub action_part_changed {
107   my ($self) = @_;
108
109   # no standard? ask user if he wants to write it
110   if ($self->part->id && !$self->part->bin_id && !$self->part->warehouse_id) {
111     $self->js->show('#write_default_bin_span');
112   } else {
113     $self->js->hide('#write_default_bin_span')
114              ->removeAttr('#write_default_bin', 'checked');
115   }
116
117   $self->js
118     ->replaceWith('#warehouse_id', $self->build_warehouse_select)
119     ->replaceWith('#bin_id', $self->build_bin_select)
120     ->replaceWith('#unit_id', $self->build_unit_select)
121     ->focus('#warehouse_id')
122     ->render;
123 }
124
125 sub action_warehouse_changed {
126   my ($self) = @_;
127
128   $self->js
129     ->replaceWith('#bin_id', $self->build_bin_select)
130     ->focus('#bin_id')
131     ->render;
132 }
133
134 sub action_mini_stock {
135   my ($self) = @_;
136
137   $self->js
138     ->html('#stock', $self->render('inventory/_stock', { output => 0 }))
139     ->render;
140 }
141
142 #================================================================
143
144 sub _check_auth {
145   $main::auth->assert('warehouse_management');
146 }
147
148 sub _check_warehouses {
149   $_[0]->show_no_warehouses_error if !@{ $_[0]->warehouses };
150 }
151
152 sub init_warehouses {
153   SL::DB::Manager::Warehouse->get_all(query => [ or => [ invalid => 0, invalid => undef ]]);
154 }
155
156 sub init_units {
157   SL::DB::Manager::Unit->get_all;
158 }
159
160 sub init_p {
161   SL::Presenter->get;
162 }
163
164 sub set_target_from_part {
165   my ($self) = @_;
166
167   return if !$self->part;
168
169   $self->warehouse($self->part->warehouse) if $self->part->warehouse;
170   $self->bin(      $self->part->bin)       if $self->part->bin;
171 }
172
173 sub sanitize_target {
174   my ($self) = @_;
175
176   $self->warehouse($self->warehouses->[0])       if !$self->warehouse || !$self->warehouse->id;
177   $self->bin      ($self->warehouse->bins->[0])  if !$self->bin       || !$self->bin->id;
178 }
179
180 sub load_part_from_form {
181   $_[0]->part(SL::DB::Manager::Part->find_by_or_create(id => $::form->{part_id}));
182 }
183
184 sub load_unit_from_form {
185   $_[0]->unit(SL::DB::Manager::Unit->find_by_or_create(id => $::form->{unit_id}));
186 }
187
188 sub load_wh_from_form {
189   $_[0]->warehouse(SL::DB::Manager::Warehouse->find_by_or_create(id => $::form->{warehouse_id}));
190 }
191
192 sub load_bin_from_form {
193   $_[0]->bin(SL::DB::Manager::Bin->find_by_or_create(id => $::form->{bin_id}));
194 }
195
196 sub set_layout {
197   $::request->layout->add_javascripts('client_js.js');
198 }
199
200 sub build_warehouse_select {
201  $_[0]->p->select_tag('warehouse_id', $_[0]->warehouses,
202    title_key => 'description',
203    default   => $_[0]->warehouse->id,
204    onchange  => 'reload_bin_selection()',
205   )
206 }
207
208 sub build_bin_select {
209   $_[0]->p->select_tag('bin_id', [ $_[0]->warehouse->bins ],
210     title_key => 'description',
211     default   => $_[0]->bin->id,
212   );
213 }
214
215 sub build_unit_select {
216   $_[0]->part->id
217     ? $_[0]->p->select_tag('unit_id', $_[0]->part->available_units,
218         title_key => 'name',
219         default   => $_[0]->part->unit_obj->id,
220       )
221     : $_[0]->p->select_tag('unit_id', $_[0]->units,
222         title_key => 'name',
223       )
224 }
225
226 sub mini_journal {
227   my ($self) = @_;
228
229   # get last 10 transaction ids
230   my $query = 'SELECT trans_id, max(itime) FROM inventory GROUP BY trans_id ORDER BY max(itime) DESC LIMIT 10';
231   my @ids = selectall_array_query($::form, $::form->get_standard_dbh, $query);
232
233   my $objs;
234   $objs = SL::DB::Manager::Inventory->get_all(query => [ trans_id => \@ids ]) if @ids;
235
236   # at most 2 of them belong to a transaction and the qty determins in or out.
237   # sort them for display
238   my %transactions;
239   for (@$objs) {
240     $transactions{ $_->trans_id }{ $_->qty > 0 ? 'in' : 'out' } = $_;
241     $transactions{ $_->trans_id }{base} = $_;
242   }
243   # and get them into order again
244   my @sorted = map { $transactions{$_} } @ids;
245
246   return \@sorted;
247 }
248
249 sub mini_stock {
250   my ($self) = @_;
251
252   my $stock             = $self->part->get_simple_stock;
253   $self->{stock_by_bin} = { map { $_->{bin_id} => $_ } @$stock };
254   $self->{stock_empty}  = ! grep { $_->{sum} * 1 } @$stock;
255 }
256
257 sub show_no_warehouses_error {
258   my ($self) = @_;
259
260   my $msg = t8('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
261
262   if ($::auth->check_right($::myconfig{login}, 'config')) { # TODO wut?
263     $msg .= t8('You can create warehouses and bins via the menu "System -> Warehouses".');
264   } else {
265     $msg .= t8('Please ask your administrator to create warehouses and bins.');
266   }
267   $::form->show_generic_error($msg);
268 }
269
270 1;