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