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