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