Artikel-Klassifizierung
[kivitendo-erp.git] / SL / Controller / Inventory.pm
1 package SL::Controller::Inventory;
2
3 use strict;
4 use warnings;
5 use POSIX qw(strftime);
6
7 use parent qw(SL::Controller::Base);
8
9 use SL::DB::Inventory;
10 use SL::DB::Part;
11 use SL::DB::Warehouse;
12 use SL::DB::Unit;
13 use SL::WH;
14 use SL::ReportGenerator;
15 use SL::Locale::String qw(t8);
16 use SL::Presenter;
17 use SL::DBUtils;
18 use SL::Helper::Flash;
19 use SL::Controller::Helper::ReportGenerator;
20
21 use English qw(-no_match_vars);
22
23 use Rose::Object::MakeMethods::Generic (
24   'scalar --get_set_init' => [ qw(warehouses units p) ],
25   'scalar'                => [ qw(warehouse bin unit part) ],
26 );
27
28 __PACKAGE__->run_before('_check_auth');
29 __PACKAGE__->run_before('_check_warehouses');
30 __PACKAGE__->run_before('load_part_from_form',   only => [ qw(stock_in part_changed mini_stock stock) ]);
31 __PACKAGE__->run_before('load_unit_from_form',   only => [ qw(stock_in part_changed mini_stock stock) ]);
32 __PACKAGE__->run_before('load_wh_from_form',     only => [ qw(stock_in warehouse_changed stock) ]);
33 __PACKAGE__->run_before('load_bin_from_form',    only => [ qw(stock_in stock) ]);
34 __PACKAGE__->run_before('set_target_from_part',  only => [ qw(part_changed) ]);
35 __PACKAGE__->run_before('mini_stock',            only => [ qw(stock_in mini_stock) ]);
36 __PACKAGE__->run_before('sanitize_target',       only => [ qw(stock_usage stock_in warehouse_changed part_changed) ]);
37 __PACKAGE__->run_before('set_layout');
38
39 sub action_stock_in {
40   my ($self) = @_;
41
42   $::form->{title}   = t8('Stock');
43
44   $::request->layout->focus('#part_id_name');
45   my $transfer_types = WH->retrieve_transfer_types('in');
46   map { $_->{description} = $main::locale->text($_->{description}) } @{ $transfer_types };
47   $self->render('inventory/warehouse_selection_stock', title => $::form->{title}, TRANSFER_TYPES => $transfer_types );
48 }
49
50 sub action_stock_usage {
51   my ($self) = @_;
52
53   $::form->{title}   = t8('UsageE');
54
55   $::form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
56                                        'bins'   => 'BINS', });
57   $::request->layout->use_javascript("${_}.js") for qw(kivi.PartsWarehouse);
58
59   $self->render('inventory/warehouse_usage',
60                 title => $::form->{title},
61                 year => DateTime->today->year,
62                 WAREHOUSES => $::form->{WAREHOUSES},
63                 WAREHOUSE_FILTER => 1,
64                 warehouse_id => 0,
65                 bin_id => 0
66       );
67
68 }
69
70 sub getnumcolumns {
71   my ($self) = @_;
72   return qw(stock incorrection found insum back outcorrection disposed
73                      missing shipped used outsum consumed averconsumed);
74 }
75
76 sub action_usage {
77   my ($self) = @_;
78
79   $main::lxdebug->enter_sub();
80
81   my $form     = $main::form;
82   my %myconfig = %main::myconfig;
83   my $locale   = $main::locale;
84
85   $form->{title}   = t8('UsageE');
86   $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
87
88   my $report = SL::ReportGenerator->new(\%myconfig, $form);
89
90   my @columns = qw(partnumber partdescription);
91
92   push @columns , qw(ptype unit) if $form->{report_generator_output_format} eq 'HTML';
93
94   my @numcolumns = qw(stock incorrection found insum back outcorrection disposed
95                      missing shipped used outsum consumed averconsumed);
96
97   push @columns , $self->getnumcolumns();
98
99   my @hidden_variables = qw(reporttype year duetyp fromdate todate
100                             warehouse_id bin_id partnumber description bestbefore chargenumber partstypes_id);
101   my %column_defs = (
102     'partnumber'      => { 'text' => $locale->text('Part Number'), },
103     'partdescription' => { 'text' => $locale->text('Part_br_Description'), },
104     'unit'            => { 'text' => $locale->text('Unit'), },
105     'stock'           => { 'text' => $locale->text('stock_br'), },
106     'incorrection'    => { 'text' => $locale->text('correction_br'), },
107     'found'           => { 'text' => $locale->text('found_br'), },
108     'insum'           => { 'text' => $locale->text('sum'), },
109     'back'            => { 'text' => $locale->text('back_br'), },
110     'outcorrection'   => { 'text' => $locale->text('correction_br'), },
111     'disposed'        => { 'text' => $locale->text('disposed_br'), },
112     'missing'         => { 'text' => $locale->text('missing_br'), },
113     'shipped'         => { 'text' => $locale->text('shipped_br'), },
114     'used'            => { 'text' => $locale->text('used_br'), },
115     'outsum'          => { 'text' => $locale->text('sum'), },
116     'consumed'        => { 'text' => $locale->text('consumed'), },
117     'averconsumed'    => { 'text' => $locale->text('averconsumed_br'), },
118   );
119
120
121   map { $column_defs{$_}->{visible} = 1 } @columns;
122   #map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
123   map { $column_defs{$_}->{align} = 'right' } @numcolumns;
124
125   my @custom_headers = ();
126   # Zeile 1:
127   push @custom_headers, [
128       { 'text' => $locale->text('Part'),
129         'colspan' => ($form->{report_generator_output_format} eq 'HTML'?4:2), 'align' => 'center'},
130       { 'text' => $locale->text('Into bin'), 'colspan' => 4, 'align' => 'center'},
131       { 'text' => $locale->text('From bin'), 'colspan' => 7, 'align' => 'center'},
132       { 'text' => $locale->text('UsageWithout'),    'colspan' => 2, 'align' => 'center'},
133   ];
134
135   # Zeile 2:
136   my @line_2 = ();
137   map { push @line_2 , $column_defs{$_} } @columns;
138   push @custom_headers, [ @line_2 ];
139
140   $report->set_custom_headers(@custom_headers);
141   $report->set_columns( %column_defs );
142   $report->set_column_order(@columns);
143
144   $report->set_export_options('usage', @hidden_variables );
145
146   $report->set_sort_indicator($form->{sort}, $form->{order});
147   $report->set_options('output_format'        => 'HTML',
148                        'controller_class'     => 'Inventory',
149                        'title'                => $form->{title},
150 #                      'html_template'        => 'inventory/usage_report',
151                        'attachment_basename'  => strftime($locale->text('warehouse_usage_list') . '_%Y%m%d', localtime time));
152   $report->set_options_from_form;
153
154   my %searchparams ;
155 # form vars
156 #   reporttype = custom
157 #   year = 2014
158 #   duetyp = 7
159
160   my $start       = DateTime->now_local;
161   my $end         = DateTime->now_local;
162   my $actualepoch = $end->epoch();
163   my $days = 365;
164   my $mdays=30;
165   $searchparams{reporttype} = $form->{reporttype};
166   if ($form->{reporttype} eq "custom") {
167     my $smon = 1;
168     my $emon = 12;
169     my $sday = 1;
170     my $eday = 31;
171     #forgotten the year --> thisyear
172     if ($form->{year} !~ m/^\d\d\d\d$/) {
173       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
174         /(\d\d\d\d)/;
175       $form->{year} = $1;
176     }
177     my $leapday = ($form->{year} % 4 == 0) ? 1:0;
178     #yearly report
179     if ($form->{duetyp} eq "13") {
180         $days += $leapday;
181     }
182
183     #Quater reports
184     if ($form->{duetyp} eq "A") {
185       $emon = 3;
186       $days = 90 + $leapday;
187     }
188     if ($form->{duetyp} eq "B") {
189       $smon = 4;
190       $emon = 6;
191       $eday = 30;
192       $days = 91;
193     }
194     if ($form->{duetyp} eq "C") {
195       $smon = 7;
196       $emon = 9;
197       $eday = 30;
198       $days = 92;
199     }
200     if ($form->{duetyp} eq "D") {
201       $smon = 10;
202       $days = 92;
203     }
204     #Monthly reports
205     if ($form->{duetyp} eq "1" || $form->{duetyp} eq "3" || $form->{duetyp} eq "5" ||
206         $form->{duetyp} eq "7" || $form->{duetyp} eq "8" || $form->{duetyp} eq "10" ||
207         $form->{duetyp} eq "12") {
208         $smon = $emon = $form->{duetyp}*1;
209         $mdays=$days = 31;
210     }
211     if ($form->{duetyp} eq "2" || $form->{duetyp} eq "4" || $form->{duetyp} eq "6" ||
212         $form->{duetyp} eq "9" || $form->{duetyp} eq "11" ) {
213         $smon = $emon = $form->{duetyp}*1;
214         $eday = 30;
215         if ($form->{duetyp} eq "2" ) {
216             #this works from 1901 to 2099, 1900 and 2100 fail.
217             $eday = ($form->{year} % 4 == 0) ? 29 : 28;
218         }
219         $mdays=$days = $eday;
220     }
221     $searchparams{year} = $form->{year};
222     $searchparams{duetyp} = $form->{duetyp};
223     $start->set_month($smon);
224     $start->set_day($sday);
225     $start->set_year($form->{year}*1);
226     $end->set_month($emon);
227     $end->set_day($eday);
228     $end->set_year($form->{year}*1);
229   }  else {
230     $searchparams{fromdate} = $form->{fromdate};
231     $searchparams{todate} = $form->{todate};
232 #   reporttype = free
233 #   fromdate = 01.01.2014
234 #   todate = 31.05.2014
235     my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig,$form->{fromdate});
236     $start->set_year($yy);
237     $start->set_month($mm);
238     $start->set_day($dd);
239     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig,$form->{todate});
240     $end->set_year($yy);
241     $end->set_month($mm);
242     $end->set_day($dd);
243     my $dur = $start->delta_md($end);
244     $days = $dur->delta_months()*30 + $dur->delta_days() ;
245   }
246   $start->set_second(0);
247   $start->set_minute(0);
248   $start->set_hour(0);
249   $end->set_second(59);
250   $end->set_minute(59);
251   $end->set_hour(23);
252   if ( $end->epoch() > $actualepoch ) {
253       $end = DateTime->now_local;
254       my $dur = $start->delta_md($end);
255       $days = $dur->delta_months()*30 + $dur->delta_days() ;
256   }
257   if ( $start->epoch() > $end->epoch() ) { $start = $end;$days = 1;}
258   $days = $mdays if $days < $mdays;
259   #$main::lxdebug->message(LXDebug->DEBUG2(), "start=".$start->epoch());
260   #$main::lxdebug->message(LXDebug->DEBUG2(), "  end=".$end->epoch());
261   #$main::lxdebug->message(LXDebug->DEBUG2(), " days=".$days);
262   my @andfilter = (shippingdate => { ge => $start }, shippingdate => { le => $end } );
263   if ( $form->{warehouse_id} ) {
264       push @andfilter , ( warehouse_id => $form->{warehouse_id});
265       $searchparams{warehouse_id} = $form->{warehouse_id};
266       if ( $form->{bin_id} ) {
267           push @andfilter , ( bin_id => $form->{bin_id});
268           $searchparams{bin_id} = $form->{bin_id};
269       }
270   }
271   # alias class t2 entspricht parts
272   if ( $form->{partnumber} ) {
273       push @andfilter , ( 't2.partnumber' => { ilike => '%'. $form->{partnumber} .'%' });
274       $searchparams{partnumber} = $form->{partnumber};
275   }
276   if ( $form->{description} ) {
277       push @andfilter , ( 't2.description' => { ilike => '%'. $form->{description} .'%'  });
278       $searchparams{description} = $form->{description};
279   }
280   if ( $form->{bestbefore} ) {
281     push @andfilter , ( bestbefore => { eq => $form->{bestbefore} });
282       $searchparams{bestbefore} = $form->{bestbefore};
283   }
284   if ( $form->{chargenumber} ) {
285       push @andfilter , ( chargenumber => { ilike => '%'.$form->{chargenumber}.'%' });
286       $searchparams{chargenumber} = $form->{chargenumber};
287   }
288   if ( $form->{partstypes_id} ) {
289       push @andfilter , ( 't2.partstypes_id' => $form->{partstypes_id} );
290       $searchparams{partstypes_id} = $form->{partstypes_id};
291   }
292
293   my @filter = (and => [ @andfilter ] );
294
295   my $objs = SL::DB::Manager::Inventory->get_all(with_objects => ['parts'], where => [ @filter ] , sort_by => 'parts.partnumber ASC');
296   #my $objs = SL::DB::Inventory->_get_manager_class->get_all(...);
297
298   # manual paginating, yuck
299   my $page = $::form->{page} || 1;
300   my $pages = {};
301   $pages->{per_page}        = $::form->{per_page} || 20;
302   my $first_nr = ($page - 1) * $pages->{per_page};
303   my $last_nr  = $first_nr + $pages->{per_page};
304
305   my $last_partid = 0;
306   my $last_row = { };
307   my $row_ind = 0;
308   my $allrows = 0;
309   $allrows = 1 if $form->{report_generator_output_format} ne 'HTML' ;
310   #$main::lxdebug->message(LXDebug->DEBUG2(), "first_nr=".$first_nr." last_nr=".$last_nr);
311   foreach my $entry (@{ $objs } ) {
312       if ( $entry->parts_id != $last_partid ) {
313           if ( $last_partid > 0 ) {
314               if ( $allrows || ($row_ind >= $first_nr && $row_ind < $last_nr )) {
315                   $self->make_row_result($last_row,$days,$last_partid);
316                   $report->add_data($last_row);
317               }
318               $row_ind++ ;
319           }
320           $last_partid = $entry->parts_id;
321           $last_row = { };
322           $last_row->{partnumber}->{data} = $entry->part->partnumber;
323           $last_row->{partdescription}->{data} = $entry->part->description;
324           $last_row->{unit}->{data} = $entry->part->unit;
325           $last_row->{stock}->{data} = 0;
326           $last_row->{incorrection}->{data} = 0;
327           $last_row->{found}->{data} = 0;
328           $last_row->{back}->{data} = 0;
329           $last_row->{outcorrection}->{data} = 0;
330           $last_row->{disposed}->{data} = 0;
331           $last_row->{missing}->{data} = 0;
332           $last_row->{shipped}->{data} = 0;
333           $last_row->{used}->{data} = 0;
334           $last_row->{insum}->{data} = 0;
335           $last_row->{outsum}->{data} = 0;
336           $last_row->{consumed}->{data} = 0;
337           $last_row->{averconsumed}->{data} = 0;
338       }
339       if ( !$allrows && $row_ind >= $last_nr ) {
340           next;
341       }
342       my $prefix='';
343       if ( $entry->trans_type->description eq 'correction' ) {
344           $prefix = $entry->trans_type->direction;
345       }
346       $last_row->{$prefix.$entry->trans_type->description}->{data} +=
347           ( $entry->trans_type->direction eq 'out' ? -$entry->qty : $entry->qty );
348   }
349   if ( $last_partid > 0 && ( $allrows || ($row_ind >= $first_nr && $row_ind < $last_nr ))) {
350       $self->make_row_result($last_row,$days,$last_partid);
351       $report->add_data($last_row);
352       $row_ind++ ;
353   }
354   my $num_rows = @{ $report->{data} } ;
355   #$main::lxdebug->message(LXDebug->DEBUG2(), "count=".$row_ind." rows=".$num_rows);
356
357   if ( ! $allrows ) {
358       $pages->{max}  = SL::DB::Helper::Paginated::ceil($row_ind, $pages->{per_page}) || 1;
359       $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
360       $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
361       $self->{pages} = $pages;
362       $searchparams{action} = "usage";
363       $self->{base_url} = $self->url_for(\%searchparams );
364       #$main::lxdebug->message(LXDebug->DEBUG2(), "page=".$pages->{page}." url=".$self->{base_url});
365
366       $report->set_options('raw_bottom_info_text' => $self->render('inventory/report_bottom', { output => 0 }) );
367   }
368   $report->generate_with_headers();
369
370   $main::lxdebug->leave_sub();
371
372 }
373
374 sub make_row_result {
375   my ($self,$row,$days,$partid) = @_;
376   my $form     = $main::form;
377   my $myconfig = \%main::myconfig;
378
379   $row->{insum}->{data}  = $row->{stock}->{data} + $row->{incorrection}->{data} + $row->{found}->{data};
380   $row->{outsum}->{data} = $row->{back}->{data} + $row->{outcorrection}->{data} + $row->{disposed}->{data} +
381        $row->{missing}->{data} + $row->{shipped}->{data} + $row->{used}->{data};
382   $row->{consumed}->{data} = $row->{outsum}->{data} -
383        $row->{outcorrection}->{data} - $row->{incorrection}->{data};
384   $row->{averconsumed}->{data} = $row->{consumed}->{data}*30/$days ;
385   map { $row->{$_}->{data} = $form->format_amount($myconfig,$row->{$_}->{data},2); } $self->getnumcolumns();
386   $row->{partnumber}->{link} = 'controller.pl?action=Part/edit&part.id' . $partid;
387 #  $row->{partdescription}->{link} = 'ic.pl?action=edit&id='.$partid;
388 }
389
390 sub action_stock {
391   my ($self) = @_;
392
393   my $transfer_error;
394   my $qty = $::form->parse_amount(\%::myconfig, $::form->{qty});
395   if (!$qty) {
396     $transfer_error = t8('Cannot stock without amount');
397   } elsif ($qty < 0) {
398     $transfer_error = t8('Cannot stock negative amounts');
399   } else {
400     # do stock
401     $::form->throw_on_error(sub {
402       eval {
403         WH->transfer({
404           parts         => $self->part,
405           dst_bin       => $self->bin,
406           dst_wh        => $self->warehouse,
407           qty           => $qty,
408           unit          => $self->unit,
409           transfer_type => 'stock',
410           chargenumber  => $::form->{chargenumber},
411           bestbefore    => $::form->{bestbefore},
412           ean           => $::form->{ean},
413           comment       => $::form->{comment},
414         });
415         1;
416       } or do { $transfer_error = $EVAL_ERROR->getMessage; }
417     });
418
419     if (!$transfer_error) {
420       if ($::form->{write_default_bin}) {
421         $self->part->load;   # onhand is calculated in between. don't mess that up
422         $self->part->bin($self->bin);
423         $self->part->warehouse($self->warehouse);
424         $self->part->save;
425       }
426
427       flash_later('info', t8('Transfer successful'));
428     }
429   }
430
431   my %additional_redirect_params = ();
432   if ($transfer_error) {
433     flash_later('error', $transfer_error);
434     $additional_redirect_params{$_}  = $::form->{$_} for qw(qty chargenumber bestbefore ean comment);
435     $additional_redirect_params{qty} = $qty;
436   }
437
438   # redirect
439   $self->redirect_to(
440     action       => 'stock_in',
441     part_id      => $self->part->id,
442     bin_id       => $self->bin->id,
443     warehouse_id => $self->warehouse->id,
444     unit_id      => $self->unit->id,
445     %additional_redirect_params,
446   );
447 }
448
449 sub action_part_changed {
450   my ($self) = @_;
451
452   # no standard? ask user if he wants to write it
453   if ($self->part->id && !$self->part->bin_id && !$self->part->warehouse_id) {
454     $self->js->show('#write_default_bin_span');
455   } else {
456     $self->js->hide('#write_default_bin_span')
457              ->removeAttr('#write_default_bin', 'checked');
458   }
459
460   $self->js
461     ->replaceWith('#warehouse_id', $self->build_warehouse_select)
462     ->replaceWith('#bin_id', $self->build_bin_select)
463     ->replaceWith('#unit_id', $self->build_unit_select)
464     ->focus('#warehouse_id')
465     ->render;
466 }
467
468 sub action_warehouse_changed {
469   my ($self) = @_;
470
471   $self->js
472     ->replaceWith('#bin_id', $self->build_bin_select)
473     ->focus('#bin_id')
474     ->render;
475 }
476
477 sub action_mini_stock {
478   my ($self) = @_;
479
480   $self->js
481     ->html('#stock', $self->render('inventory/_stock', { output => 0 }))
482     ->render;
483 }
484
485 #================================================================
486
487 sub _check_auth {
488   $main::auth->assert('warehouse_management');
489 }
490
491 sub _check_warehouses {
492   $_[0]->show_no_warehouses_error if !@{ $_[0]->warehouses };
493 }
494
495 sub init_warehouses {
496   SL::DB::Manager::Warehouse->get_all(query => [ or => [ invalid => 0, invalid => undef ]]);
497 }
498
499 #sub init_bins {
500 #  SL::DB::Manager::Bin->get_all();
501 #}
502
503 sub init_units {
504   SL::DB::Manager::Unit->get_all;
505 }
506
507 sub init_p {
508   SL::Presenter->get;
509 }
510
511 sub set_target_from_part {
512   my ($self) = @_;
513
514   return if !$self->part;
515
516   $self->warehouse($self->part->warehouse) if $self->part->warehouse;
517   $self->bin(      $self->part->bin)       if $self->part->bin;
518 }
519
520 sub sanitize_target {
521   my ($self) = @_;
522
523   $self->warehouse($self->warehouses->[0])       if !$self->warehouse || !$self->warehouse->id;
524   $self->bin      ($self->warehouse->bins->[0])  if !$self->bin       || !$self->bin->id;
525 #  foreach my $warehouse ( $self->warehouses ) {
526 #      $warehouse->{BINS} = [];
527 #      foreach my $bin ( $self->bins ) {
528 #         if ( $bin->warehouse_id == $warehouse->id ) {
529 #             push @{ $warehouse->{BINS} }, $bin;
530 #         }
531 #      }
532 #  }
533 }
534
535 sub load_part_from_form {
536   $_[0]->part(SL::DB::Manager::Part->find_by_or_create(id => $::form->{part_id}));
537 }
538
539 sub load_unit_from_form {
540   $_[0]->unit(SL::DB::Manager::Unit->find_by_or_create(id => $::form->{unit_id}));
541 }
542
543 sub load_wh_from_form {
544   $_[0]->warehouse(SL::DB::Manager::Warehouse->find_by_or_create(id => $::form->{warehouse_id}));
545 }
546
547 sub load_bin_from_form {
548   $_[0]->bin(SL::DB::Manager::Bin->find_by_or_create(id => $::form->{bin_id}));
549 }
550
551 sub set_layout {
552   $::request->layout->add_javascripts('client_js.js');
553 }
554
555 sub build_warehouse_select {
556  $_[0]->p->select_tag('warehouse_id', $_[0]->warehouses,
557    title_key => 'description',
558    default   => $_[0]->warehouse->id,
559    onchange  => 'reload_bin_selection()',
560   )
561 }
562
563 sub build_bin_select {
564   $_[0]->p->select_tag('bin_id', [ $_[0]->warehouse->bins ],
565     title_key => 'description',
566     default   => $_[0]->bin->id,
567   );
568 }
569
570 sub build_unit_select {
571   $_[0]->part->id
572     ? $_[0]->p->select_tag('unit_id', $_[0]->part->available_units,
573         title_key => 'name',
574         default   => $_[0]->part->unit_obj->id,
575       )
576     : $_[0]->p->select_tag('unit_id', $_[0]->units,
577         title_key => 'name',
578       )
579 }
580
581 sub mini_journal {
582   my ($self) = @_;
583
584   # get last 10 transaction ids
585   my $query = 'SELECT trans_id, max(itime) FROM inventory GROUP BY trans_id ORDER BY max(itime) DESC LIMIT 10';
586   my @ids = selectall_array_query($::form, $::form->get_standard_dbh, $query);
587
588   my $objs;
589   $objs = SL::DB::Manager::Inventory->get_all(query => [ trans_id => \@ids ]) if @ids;
590
591   # at most 2 of them belong to a transaction and the qty determins in or out.
592   # sort them for display
593   my %transactions;
594   for (@$objs) {
595     $transactions{ $_->trans_id }{ $_->qty > 0 ? 'in' : 'out' } = $_;
596     $transactions{ $_->trans_id }{base} = $_;
597   }
598   # and get them into order again
599   my @sorted = map { $transactions{$_} } @ids;
600
601   return \@sorted;
602 }
603
604 sub mini_stock {
605   my ($self) = @_;
606
607   my $stock             = $self->part->get_simple_stock;
608   $self->{stock_by_bin} = { map { $_->{bin_id} => $_ } @$stock };
609   $self->{stock_empty}  = ! grep { $_->{sum} * 1 } @$stock;
610 }
611
612 sub show_no_warehouses_error {
613   my ($self) = @_;
614
615   my $msg = t8('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
616
617   if ($::auth->check_right($::myconfig{login}, 'config')) { # TODO wut?
618     $msg .= t8('You can create warehouses and bins via the menu "System -> Warehouses".');
619   } else {
620     $msg .= t8('Please ask your administrator to create warehouses and bins.');
621   }
622   $::form->show_generic_error($msg);
623 }
624
625 1;
626 __END__
627
628 =encoding utf-8
629
630 =head1 NAME
631
632 SL::Controller::Inventory - Report Controller for inventory
633
634 =head1 DESCRIPTION
635
636 This controller makes three reports about inventory in warehouses/stocks
637
638 - warehouse content
639
640 - warehouse journal
641
642 - warehouse withdrawal
643
644 =head1 FUNCTIONS
645
646 =over 4
647
648 =item C<action_stock_usage>
649
650 Create a search form for stock withdrawal.
651 The search parameter for report are made like the reports in bin/mozilla/rp.pl
652
653 =item C<action_usage>
654
655 Make a report about stock withdrawal.
656
657 The manual pagination is implemented like the pagination in SL::Controller::CsvImport.
658
659 =back
660
661 =head1 SPECIAL CASES
662
663 Because of the PFD-Table Formatter some parameters for PDF must be different to the HTML parameters.
664 So in german language there are some tries to use a HTML Break in the second heading line
665 to produce two line heading inside table. The actual version has some abbreviations for the header texts.
666
667 =head1 BUGS
668
669 The PDF-Table library has some limits (doesn't display all if the line is to large) so
670 the format is adapted to this
671
672
673 =head1 AUTHOR
674
675 only for C<action_stock_usage> and C<action_usage>:
676
677 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
678
679
680 =cut
681