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