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