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