WH::transfer_assembly gegen S/H/Inventory::produce_assembly getauscht
[kivitendo-erp.git] / bin / mozilla / wh.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #############################################################################
7 # SQL-Ledger, Accounting
8 # Copyright (c) 1998-2002
9 #
10 #  Author: Dieter Simader
11 #   Email: dsimader@sql-ledger.org
12 #     Web: http://www.sql-ledger.org
13 #
14 #
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 # MA 02110-1335, USA.
28 #
29 #######################################################################
30 #
31 # warehouse and packinglist
32 #
33 #######################################################################
34
35 use List::Util qw(min max first);
36 use POSIX qw(strftime);
37
38 use SL::Form;
39 use SL::User;
40
41 use SL::AM;
42 use SL::CVar;
43 use SL::CT;
44 use SL::Helper::Flash qw(flash_later);
45 use SL::IC;
46 use SL::WH;
47 use SL::OE;
48 use SL::Helper::Inventory qw(produce_assembly);
49 use SL::Locale::String qw(t8);
50 use SL::ReportGenerator;
51 use SL::Presenter::Tag qw(checkbox_tag);
52 use SL::Presenter::Part;
53
54 use SL::DB::Part;
55
56 use Data::Dumper;
57
58 require "bin/mozilla/common.pl";
59 require "bin/mozilla/reportgenerator.pl";
60
61 use strict;
62
63 # parserhappy(R):
64
65 # contents of the "transfer_type" table:
66 #  $locale->text('back')
67 #  $locale->text('correction')
68 #  $locale->text('disposed')
69 #  $locale->text('found')
70 #  $locale->text('missing')
71 #  $locale->text('stock')
72 #  $locale->text('shipped')
73 #  $locale->text('transfer')
74 #  $locale->text('used')
75 #  $locale->text('return_material')
76 #  $locale->text('release_material')
77 #  $locale->text('assembled')
78 #  $locale->text('stocktaking')
79
80 # --------------------------------------------------------------------
81 # Transfer
82 # --------------------------------------------------------------------
83
84 sub transfer_warehouse_selection {
85   $main::lxdebug->enter_sub();
86
87   $main::auth->assert('warehouse_management');
88
89   my $form     = $main::form;
90   my %myconfig = %main::myconfig;
91   my $locale   = $main::locale;
92
93   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
94                                      'bins'   => 'BINS', });
95
96   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
97
98   my $units      = AM->retrieve_units(\%myconfig, $form);
99
100   my $part = 0;
101   if ( $form->{parts_id} ) {
102     $part = SL::DB::Part->new();
103     $part->id($form->{parts_id});
104     $part->load();
105   }
106
107   # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
108   $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $part ? $part->unit : 0);
109
110   if (scalar @{ $form->{WAREHOUSES} }) {
111     $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
112     $form->{bin_id}       ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
113   }
114
115   my $content;
116
117   if ($form->{trans_type} eq 'removal') {
118     setup_wh_transfer_warehouse_selection_action_bar("removal_parts_selection");
119     $form->{title}   = $locale->text('Removal from Warehouse');
120     $content         = $form->parse_html_template('wh/warehouse_selection');
121
122   } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
123     setup_wh_transfer_warehouse_selection_action_bar("transfer_parts_selection");
124     $form->{title}   = $locale->text('Transfer');
125     $content         = $form->parse_html_template('wh/warehouse_selection');
126
127   } elsif ($form->{trans_type} eq 'assembly') {
128     setup_wh_transfer_warehouse_selection_assembly_action_bar();
129     $form->{title} = $locale->text('Produce Assembly');
130     $content       = $form->parse_html_template('wh/warehouse_selection_assembly');
131   }
132
133   $form->header();
134   print $content;
135
136   $main::lxdebug->leave_sub();
137 }
138
139 sub transfer_parts_selection {
140   $main::lxdebug->enter_sub();
141
142   $main::auth->assert('warehouse_management');
143
144   my $form     = $main::form;
145   my %myconfig = %main::myconfig;
146   my $locale   = $main::locale;
147
148   transfer_or_removal_prepare_contents('direction' => 'transfer');
149
150   setup_wh_transfer_parts_action_bar();
151
152   $form->{title} = $locale->text('Transfer');
153   $form->header();
154   print $form->parse_html_template("wh/transfer_parts_selection");
155
156   $main::lxdebug->leave_sub();
157 }
158
159 sub transfer_or_removal_prepare_contents {
160   $main::lxdebug->enter_sub();
161
162   $main::auth->assert('warehouse_management');
163
164   my %args = @_;
165
166   my $form     = $main::form;
167   my %myconfig = %main::myconfig;
168   my $locale   = $main::locale;
169
170   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
171                                      'bins'   => 'BINS', });
172
173   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
174   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
175
176   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
177
178   $form->{initial_warehouse_idx} = $warehouse_idx;
179   $form->{warehouse_description} = $warehouse->{description};
180   $warehouse->{selected}         = 1;
181
182   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
183
184   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit ean);
185
186   $form->{sort} = 'bindescription';
187   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
188                                            "bin_id"       => $form->{bin_id},
189                                            "chargenumber" => $form->{chargenumber},
190                                            "bestbefore"   => $form->{bestbefore},
191                                            "partsid"      => $form->{part_id},
192                                            "ean"          => $form->{ean});
193
194   if (0 == scalar(@contents)) {
195     $form->show_generic_error($locale->text("The selected warehouse is empty, or no stocked items where found that match the filter settings."));
196   }
197
198   my $all_units = AM->retrieve_units(\%myconfig, $form);
199
200   foreach (@contents) {
201     $_->{qty} = $form->format_amount_units('amount'     => $_->{qty},
202                                            'part_unit'  => $_->{partunit},
203                                            'conv_units' => 'convertible');
204     my $this_unit = $_->{partunit};
205
206     if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
207       $this_unit = "kg";
208     }
209
210     $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
211   }
212
213   my $transfer_types = WH->retrieve_transfer_types($args{direction});
214   map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
215
216   $form->{CONTENTS}       = \@contents;
217   $form->{TRANSFER_TYPES} = $transfer_types;
218
219   $main::lxdebug->leave_sub();
220 }
221
222
223 sub transfer_parts {
224   $main::lxdebug->enter_sub();
225
226   $main::auth->assert('warehouse_management');
227
228   my $form     = $main::form;
229   my %myconfig = %main::myconfig;
230   my $locale   = $main::locale;
231
232   $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
233
234   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
235   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
236
237   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
238
239   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
240
241   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
242
243   $form->{sort} = 'bindescription';
244   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
245   my $all_units = AM->retrieve_units(\%myconfig, $form);
246
247   my @transfers;
248
249   foreach my $row (1 .. $form->{rowcount}) {
250     $form->{"qty_$row"} =~ s/^\s*//;
251     $form->{"qty_$row"} =~ s/\s*$//;
252     next if (!$form->{"qty_$row"});
253
254     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
255     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
256     my $bin     = $warehouse->{BINS}->[$bin_idx];
257
258     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
259
260     my $transfer = {
261       'src_warehouse_id' => $form->{warehouse_id},
262       'transfer_type_id' => $form->{transfer_type_id},
263     };
264
265     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
266
267     my $entry;
268
269     foreach (@contents) {
270       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
271         $entry = $_;
272         last;
273       }
274     }
275
276     if (!$entry) {
277       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
278                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
279     }
280
281     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
282     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
283
284     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
285       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
286                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
287     }
288
289     $transfer->{comment} = $form->{comment};
290     $transfer->{change_default_bin} = $form->{change_default_bin};
291
292     push @transfers, $transfer;
293
294     $entry->{qty} -= $transfer->{qty};
295   }
296
297   if (!scalar @transfers) {
298     $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
299     $::dispatcher->end_request;
300   }
301
302   WH->transfer(@transfers);
303
304   $form->{trans_type}    = 'transfer';
305   $form->{saved_message} = $locale->text('The parts have been transferred.');
306
307   transfer_warehouse_selection();
308
309   $main::lxdebug->leave_sub();
310 }
311
312 # --------------------------------------------------------------------
313 # Transfer: stock
314 # --------------------------------------------------------------------
315
316 # --------------------------------------------------------------------
317 # Transfer: assemblies
318 # Dies ist die Auswahlmaske für ein assembly.
319 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
320 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
321 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
322 # --------------------------------------------------------------------
323
324 sub transfer_assembly_update_part {
325   my $form     = $main::form;
326   my %myconfig = %main::myconfig;
327   my $locale   = $main::locale;
328
329   $form->{trans_type} = 'assembly';
330   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
331
332   if (!$form->{parts_id}) {
333     delete $form->{partunit};
334     transfer_warehouse_selection();
335     return;
336
337   }
338
339   my $part = SL::DB::Part->new(id => $::form->{parts_id})->load;
340   @{$form}{qw(parts_id partnumber description)} = ($part->id, $part->partnumber, $part->description);
341
342   transfer_stock_get_partunit();
343   transfer_warehouse_selection();
344 }
345
346 sub transfer_stock_part_selected {
347   $main::lxdebug->enter_sub();
348
349   my $part = shift;
350
351   my $form     = $main::form;
352
353   @{$form}{qw(parts_id partnumber description ean warehouse_id bin_id)} = @{$part}{qw(id partnumber description ean warehouse_id bin_id)};
354
355   transfer_stock_get_partunit();
356   transfer_warehouse_selection();
357
358   $main::lxdebug->leave_sub();
359 }
360
361 sub transfer_stock_get_partunit {
362   $main::lxdebug->enter_sub();
363
364   my $form     = $main::form;
365
366   if ($form->{parts_id}) {
367     my $part_info     = IC->get_basic_part_info('id' => $form->{parts_id});
368     $form->{partunit} = $part_info->{unit};
369   }
370
371   $main::lxdebug->leave_sub();
372 }
373
374 sub create_assembly {
375   $main::lxdebug->enter_sub();
376
377   my $form     = $main::form;
378   my %myconfig = %main::myconfig;
379   my $locale   = $main::locale;
380
381   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
382   if ($form->{qty} <= 0) {
383     $form->show_generic_error($locale->text('Invalid quantity.'));
384   }
385   if (!$form->{warehouse_id} || !$form->{bin_id}) {
386     $form->error($locale->text('The warehouse or the bin is missing.'));
387   }
388   # need part and bin object
389   my ($bin, $assembly);
390   $assembly = SL::DB::Manager::Part->find_by(id => $form->{parts_id}, part_type => 'assembly');
391   $form->show_generic_error($locale->text('Invalid assembly')) unless ref $assembly eq 'SL::DB::Part';
392
393   $bin = SL::DB::Manager::Bin->find_by(id => $form->{bin_id});
394   $form->show_generic_error($locale->text('Invalid bin')) unless ref $bin eq 'SL::DB::Bin';
395
396   if (!$::instance_conf->get_show_bestbefore) {
397     $form->{bestbefore} = '';
398   }
399
400   produce_assembly(
401               part           => $assembly,               # target assembly
402               qty            => $form->{qty},            # qty
403               auto_allocate  => 1,
404               bin            => $bin,                    # needed unless a global standard target is configured
405               chargenumber   => $form->{chargenumber},   # optional
406               bestbefore     => $form->{bestbefore},
407               comment        => $form->{comment},        # optional
408   );
409
410   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
411
412   $form->{saved_message} = $locale->text('The assembly has been created.');
413   $form->{trans_type}    = 'assembly';
414
415   transfer_warehouse_selection();
416
417   $main::lxdebug->leave_sub();
418 }
419
420 # --------------------------------------------------------------------
421 # Transfer: removal
422 # --------------------------------------------------------------------
423
424 sub removal_parts_selection {
425   $main::lxdebug->enter_sub();
426
427   $main::auth->assert('warehouse_management');
428
429   my $form     = $main::form;
430   my %myconfig = %main::myconfig;
431   my $locale   = $main::locale;
432
433   transfer_or_removal_prepare_contents('direction' => 'out');
434
435   setup_wh_removal_parts_selection_action_bar();
436
437   $form->{title} = $locale->text('Removal');
438   $form->header();
439   print $form->parse_html_template("wh/removal_parts_selection");
440
441   $main::lxdebug->leave_sub();
442 }
443
444 sub remove_parts {
445   $main::lxdebug->enter_sub();
446
447   $main::auth->assert('warehouse_management');
448
449   my $form     = $main::form;
450   my %myconfig = %main::myconfig;
451   my $locale   = $main::locale;
452
453   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
454                                      'bins'   => 'BINS', });
455
456   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
457   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
458
459   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
460
461   $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
462
463   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
464
465   $form->{sort} = 'bindescription';
466   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
467   my $all_units = AM->retrieve_units(\%myconfig, $form);
468
469   my @transfers;
470
471   foreach my $row (1 .. $form->{rowcount}) {
472     $form->{"qty_$row"} =~ s/^\s*//;
473     $form->{"qty_$row"} =~ s/\s*$//;
474     next if (!$form->{"qty_$row"});
475
476     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
477     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
478     my $bin     = $warehouse->{BINS}->[$bin_idx];
479
480     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
481
482     my $transfer = {
483       'src_warehouse_id' => $form->{warehouse_id},
484       'transfer_type_id' => $form->{transfer_type_id},
485     };
486
487     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
488
489     my $entry;
490
491     foreach (@contents) {
492       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
493         $entry = $_;
494         last;
495       }
496     }
497
498     if (!$entry) {
499       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
500                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
501     }
502
503     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
504     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
505
506     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
507       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
508                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
509     }
510
511     $transfer->{comment} = $form->{comment};
512
513     push @transfers, $transfer;
514
515     $entry->{qty} -= $transfer->{qty};
516   }
517
518   if (!scalar @transfers) {
519     $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
520     $::dispatcher->end_request;
521   }
522
523   WH->transfer(@transfers);
524
525   $form->{trans_type}    = 'removal';
526   $form->{saved_message} = $locale->text('The parts have been removed.');
527
528   transfer_warehouse_selection();
529
530   $main::lxdebug->leave_sub();
531 }
532
533 sub disassemble_assembly {
534   $main::lxdebug->enter_sub();
535
536   $main::auth->assert('warehouse_management');
537
538   my $form = $main::form;
539
540   croak("No assembly ids") unless scalar @{ $form->{ids}} > 0;
541
542   # everything in one transaction
543   my $db = SL::DB::Inventory->new->db;
544   $db->with_transaction(sub {
545
546     foreach my $trans_id (@{ $::form->{ids}} )  {
547       SL::DB::Manager::Inventory->delete_all(where => [ trans_id => $trans_id ]);
548       flash_later('info', t8("Disassembly successful for trans_id #1",  $trans_id));
549     }
550
551     1;
552   }) || die t8('error while disassembling for trans_ids #1 : #2', $form->{ids})  . $db->error . "\n";
553
554   $main::lxdebug->leave_sub();
555   $form->redirect;
556 }
557
558 # --------------------------------------------------------------------
559 # Journal
560 # --------------------------------------------------------------------
561
562 sub journal {
563   $main::lxdebug->enter_sub();
564
565   $main::auth->assert('warehouse_management');
566
567   my $form     = $main::form;
568   my %myconfig = %main::myconfig;
569   my $locale   = $main::locale;
570
571   $form->{title} = $locale->text('Report about warehouse transactions');
572   $form->get_lists('warehouses' => { 'key'  => 'WAREHOUSES',
573                                      'bins' => 'BINS', });
574
575   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
576
577   setup_wh_journal_action_bar();
578
579   $form->header();
580   print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
581
582   $main::lxdebug->leave_sub();
583 }
584
585 sub generate_journal {
586   $main::lxdebug->enter_sub();
587
588   $main::auth->assert('warehouse_management');
589
590   my $form     = $main::form;
591   my %myconfig = %main::myconfig;
592   my $locale   = $main::locale;
593
594   setup_wh_journal_list_all_action_bar();
595   $form->{title}   = $locale->text("WHJournal");
596   $form->{sort}  ||= 'date';
597
598   $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
599
600   my %filter;
601   my @columns = qw(ids trans_id date warehouse_from bin_from warehouse_to bin_to partnumber type_and_classific partdescription chargenumber bestbefore trans_type comment qty unit partunit employee oe_id projectnumber);
602
603   # filter stuff
604   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore transtype_id transtype_ids comment projectnumber);
605
606   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
607   if ($filter{qty_op}) {
608     $form->isblank("qty",      $locale->text('Quantity missing.'));
609     $form->isblank("qty_unit", $locale->text('Unit missing.'));
610
611     $filter{qty}      = $form->{qty};
612     $filter{qty_unit} = $form->{qty_unit};
613   }
614   # /filter stuff
615
616   my $allrows        = !!($form->{report_generator_output_format} ne 'HTML') ;
617
618   # manual paginating
619   my $pages          = {};
620   my $page           = $::form->{page} || 1;
621   $pages->{per_page} = $::form->{per_page} || 15;
622   my $first_nr       = ($page - 1) * $pages->{per_page};
623   my $last_nr        = $first_nr + $pages->{per_page};
624
625   # no optimisation if qty op
626   if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
627     $filter{limit}  = $pages->{per_page};
628     $filter{offset} = ($page - 1) * $pages->{per_page};
629     $first_nr       = 0;
630     $last_nr        = $pages->{per_page};
631   }
632
633   my $old_l_trans_id = $form->{l_trans_id};
634   my @contents  = WH->get_warehouse_journal(%filter);
635   $form->{l_trans_id} = $old_l_trans_id;
636
637   # get maxcount
638   if (!$form->{maxrows}) {
639     $form->{maxrows} = scalar @contents ;
640   }
641
642   my $report = SL::ReportGenerator->new(\%myconfig, $form);
643
644   my @hidden_variables = map { "l_${_}" } @columns;
645   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber bestbefore qty_op qty qty_unit unit partunit fromdate todate transtype_ids comment projectnumber);
646   push @hidden_variables, qw(classification_id);
647
648   my %column_defs = (
649     'ids'             => { raw_header_data => checkbox_tag("", id => "check_all", checkall  => "[data-checkall=1]") },
650     'date'            => { 'text' => $locale->text('Date'), },
651     'trans_id'        => { 'text' => $locale->text('Trans Id'), },
652     'trans_type'      => { 'text' => $locale->text('Trans Type'), },
653     'comment'         => { 'text' => $locale->text('Comment'), },
654     'warehouse_from'  => { 'text' => $locale->text('Warehouse From'), },
655     'warehouse_to'    => { 'text' => $locale->text('Warehouse To'), },
656     'bin_from'        => { 'text' => $locale->text('Bin From'), },
657     'bin_to'          => { 'text' => $locale->text('Bin To'), },
658     'partnumber'      => { 'text' => $locale->text('Part Number'), },
659     'type_and_classific'
660                       => { 'text' => $locale->text('Type'), },
661     'partdescription' => { 'text' => $locale->text('Part Description'), },
662     'chargenumber'    => { 'text' => $locale->text('Charge Number'), },
663     'bestbefore'      => { 'text' => $locale->text('Best Before'), },
664     'qty'             => { 'text' => $locale->text('Qty'), },
665     'unit'            => { 'text' => $locale->text('Part Unit'), },
666     'partunit'        => { 'text' => $locale->text('Unit'), },
667     'employee'        => { 'text' => $locale->text('Employee'), },
668     'projectnumber'   => { 'text' => $locale->text('Project Number'), },
669     'oe_id'           => { 'text' => $locale->text('Document'), },
670   );
671
672   if ($form->{transtype_ids} && 'ARRAY' eq ref $form->{transtype_ids}) {
673     for (my $i = 0; $i < scalar(@{ $form->{transtype_ids} }); $i++) {
674       delete $form->{transtype_ids}[$i] if $form->{transtype_ids}[$i] eq '';
675     }
676     $form->{transtype_ids} = join(",", @{ $form->{transtype_ids} });
677   }
678
679   my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
680   $href .= "&maxrows=".$form->{maxrows};
681
682   map { $column_defs{$_}->{link} = $href ."&page=".$page. "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
683
684   my %column_alignment = map { $_ => 'right' } qw(qty);
685
686   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
687   $column_defs{partunit}->{visible} = 1;
688   $column_defs{type_and_classific}->{visible} = 1;
689   $column_defs{type_and_classific}->{link} ='';
690   $column_defs{ids}->{visible} = 1;
691
692   $report->set_columns(%column_defs);
693   $report->set_column_order(@columns);
694
695   $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
696
697   $report->set_sort_indicator($form->{sort}, $form->{order});
698
699   $report->set_options('output_format'        => 'HTML',
700                        'title'                => $form->{title},
701                        'attachment_basename'  => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
702   $report->set_options_from_form();
703   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
704
705   my $all_units = AM->retrieve_units(\%myconfig, $form);
706
707   my %doc_types = ( 'sales_quotation'         => { script => 'oe', title => $locale->text('Sales quotation') },
708                     'sales_order'             => { script => 'oe', title => $locale->text('Sales Order') },
709                     'request_quotation'       => { script => 'oe', title => $locale->text('Request quotation') },
710                     'purchase_order'          => { script => 'oe', title => $locale->text('Purchase Order') },
711                     'sales_delivery_order'    => { script => 'do', title => $locale->text('Sales delivery order') },
712                     'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
713                     'sales_invoice'           => { script => 'is', title => $locale->text('Sales Invoice') },
714                     'purchase_invoice'        => { script => 'ir', title => $locale->text('Purchase Invoice') },
715                   );
716
717   my $idx       = 0;
718   my $undo_date  = DateTime->today->subtract(days => $::instance_conf->get_undo_transfer_interval);
719   foreach my $entry (@contents) {
720     $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}) .
721                                    SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
722     $entry->{qty}        = $form->format_amount(\%myconfig, $entry->{qty});
723     $entry->{assembled} = $entry->{trans_type} eq 'assembled' ? 1 : '';
724     $entry->{trans_type} = $locale->text($entry->{trans_type});
725     my $row = { };
726
727     foreach my $column (@columns) {
728       $row->{$column} = {
729         'data'  => $entry->{$column},
730         'align' => $column_alignment{$column},
731       };
732     }
733
734     if ($entry->{assembled}) {
735       my $insertdate = DateTime->from_kivitendo($entry->{shippingdate});
736       if (ref $undo_date eq 'DateTime' && ref $insertdate eq 'DateTime' && $insertdate > $undo_date) {
737         $row->{ids}->{raw_data} = checkbox_tag("ids[]", value => $entry->{trans_id}, "data-checkall" => 1);
738       }
739     }
740     $row->{trans_type}->{raw_data} = $entry->{trans_type};
741     if ($form->{l_oe_id}) {
742       $row->{oe_id}->{data} = '';
743       my $info              = $entry->{oe_id_info};
744
745       if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
746         $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
747                           link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
748       }
749     }
750
751     if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
752       $report->add_data($row);
753     }
754     $idx++;
755   }
756
757
758     $report->set_options(
759       raw_top_info_text     => $form->parse_html_template('wh/report_top'),
760       raw_bottom_info_text  => $form->parse_html_template('wh/report_bottom', { callback => $href }),
761     );
762   if ( ! $allrows ) {
763       $pages->{max}  = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
764       $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
765       $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
766
767       $report->set_options('raw_bottom_info_text' =>  $form->parse_html_template('wh/report_bottom', { callback => $href }) . $form->parse_html_template('common/paginate',
768                                                             { 'pages' => $pages , 'base_url' => $href.'&sort='.$form->{sort}.'&order='.$form->{order}}) );
769   }
770   $report->generate_with_headers();
771
772   $main::lxdebug->leave_sub();
773 }
774
775 # --------------------------------------------------------------------
776 # Report
777 # --------------------------------------------------------------------
778
779 sub report {
780   $main::lxdebug->enter_sub();
781
782   $main::auth->assert('warehouse_contents | warehouse_management');
783
784   my $form     = $main::form;
785   my %myconfig = %main::myconfig;
786   my $locale   = $main::locale;
787
788   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
789                                      'bins'   => 'BINS', });
790
791   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
792
793   my $CVAR_CONFIGS = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => 'IC' ]);
794   my $INCLUDABLE_CVAR_CONFIGS = [ grep { $_->includeable } @{ $CVAR_CONFIGS } ];
795
796   $form->{title}   = $locale->text("Report about warehouse contents");
797
798   setup_wh_report_action_bar();
799
800   $form->header();
801   print $form->parse_html_template("wh/report_filter",
802                                    { "WAREHOUSES" => $form->{WAREHOUSES},
803                                      "UNITS"      => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)),
804                                      # "CVAR_CONFIGS"            => $CVAR_CONFIGS, # nyi searchable cvars
805                                      "INCLUDABLE_CVAR_CONFIGS" => $INCLUDABLE_CVAR_CONFIGS,
806                                    });
807
808   $main::lxdebug->leave_sub();
809 }
810
811 sub generate_report {
812   $main::lxdebug->enter_sub();
813
814   $main::auth->assert('warehouse_contents | warehouse_management');
815
816   my $form     = $main::form;
817   my %myconfig = %main::myconfig;
818   my $locale   = $main::locale;
819
820   my $cvar_configs = CVar->get_configs('module' => 'IC');
821
822   $form->{title}   = $locale->text("Report about warehouse contents");
823   $form->{sort}  ||= 'partnumber';
824   my $sort_col     = $form->{sort};
825
826   my %filter;
827   my @columns = qw(warehousedescription bindescription partnumber type_and_classific partdescription chargenumber bestbefore comment qty partunit list_price purchase_price stock_value);
828
829   # filter stuff
830   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore date include_invalid_warehouses);
831
832   # show filter stuff also in report
833   my @options;
834   my $currentdate = $form->current_date(\%myconfig);
835   push @options, $locale->text('Printdate') . " : ".$locale->date(\%myconfig, $currentdate, 1);
836
837   # dispatch all options
838   my $dispatch_options = {
839    warehouse_id   => sub { push @options, $locale->text('Warehouse') . " : " .
840                                             SL::DB::Manager::Warehouse->find_by(id => $form->{warehouse_id})->description},
841    bin_id         => sub { push @options, $locale->text('Bin') . " : " .
842                                             SL::DB::Manager::Bin->find_by(id => $form->{bin_id})->description},
843    partnumber     => sub { push @options, $locale->text('Partnumber')     . " : $form->{partnumber}"},
844    classification_id => sub { push @options, $locale->text('Parts Classification'). " : ".
845                                                SL::DB::Manager::PartClassification->get_first(where => [ id => $form->{classification_id} ] )->description; },
846    description    => sub { push @options, $locale->text('Description')    . " : $form->{description}"},
847    chargenumber   => sub { push @options, $locale->text('Charge Number')  . " : $form->{chargenumber}"},
848    bestbefore     => sub { push @options, $locale->text('Best Before')    . " : $form->{bestbefore}"},
849    include_invalid_warehouses    => sub { push @options, $locale->text('Include invalid warehouses ')},
850   };
851   foreach (keys %filter) {
852    $dispatch_options->{$_}->() if $dispatch_options->{$_};
853   }
854   push @options, $locale->text('Stock Qty for Date') . " " . $locale->date(\%myconfig, $form->{date}?$form->{date}:$currentdate, 1);
855
856   # / end show filter stuff also in report
857
858   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
859   if ($filter{qty_op}) {
860     $form->isblank("qty",      $locale->text('Quantity missing.'));
861     $form->isblank("qty_unit", $locale->text('Unit missing.'));
862
863     $filter{qty}      = $form->{qty};
864     $filter{qty_unit} = $form->{qty_unit};
865   }
866   # /filter stuff
867
868   $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
869
870   # manual paginating
871   my $allrows        = $form->{report_generator_output_format} eq 'HTML' ? $form->{allrows} : 1;
872   my $page           = $::form->{page} || 1;
873   my $pages          = {};
874   $pages->{per_page} = $::form->{per_page} || 20;
875   my $first_nr       = ($page - 1) * $pages->{per_page};
876   my $last_nr        = $first_nr + $pages->{per_page};
877
878   # no optimisation if qty op
879   if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
880     $filter{limit}  = $pages->{per_page};
881     $filter{offset} = ($page - 1) * $pages->{per_page};
882     $first_nr       = 0;
883     $last_nr        = $pages->{per_page};
884   }
885
886   my @contents  = WH->get_warehouse_report(%filter);
887
888   # get maxcount
889   if (!$form->{maxrows}) {
890     $form->{maxrows} = scalar @contents ;
891   }
892
893   $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
894
895   my $report = SL::ReportGenerator->new(\%myconfig, $form);
896
897   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
898   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
899
900   my @hidden_variables = map { "l_${_}" } @columns;
901   push @hidden_variables, qw(warehouse_id bin_id partnumber partstypes_id description chargenumber bestbefore qty_op qty qty_unit partunit l_warehousedescription l_bindescription);
902   push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
903   push @hidden_variables, qw(classification_id stock_value_basis allrows);
904
905   my %column_defs = (
906     'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
907     'bindescription'       => { 'text' => $locale->text('Bin'), },
908     'partnumber'           => { 'text' => $locale->text('Part Number'), },
909     'type_and_classific'   => { 'text' => $locale->text('Type'), },
910     'partdescription'      => { 'text' => $locale->text('Part Description'), },
911     'chargenumber'         => { 'text' => $locale->text('Charge Number'), },
912     'bestbefore'           => { 'text' => $locale->text('Best Before'), },
913     'qty'                  => { 'text' => $locale->text('Qty'), },
914     'partunit'             => { 'text' => $locale->text('Unit'), },
915     'stock_value'          => { 'text' => $locale->text('Stock value'), },
916     'purchase_price'       => { 'text' => $locale->text('Purchase price'), },
917     'list_price'           => { 'text' => $locale->text('List Price'), },
918   );
919
920   my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
921   $href .= "&maxrows=".$form->{maxrows};
922   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
923   %column_defs = (%column_defs, %column_defs_cvars);
924
925   map { $column_defs{$_}->{link} = $href . "&page=".$page."&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
926
927   my %column_alignment = map { $_ => 'right' } qw(qty list_price purchase_price stock_value);
928
929   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
930
931   $column_defs{partunit}->{visible}           = 1;
932   $column_defs{type_and_classific}->{visible} = 1;
933   $column_defs{type_and_classific}->{link} ='';
934
935   $report->set_columns(%column_defs);
936   $report->set_column_order(@columns);
937
938   $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
939
940   $report->set_sort_indicator($sort_col, $form->{order});
941
942   $report->set_options('top_info_text'        => join("\n", @options),
943                        'output_format'        => 'HTML',
944                        'title'                => $form->{title},
945                        'attachment_basename'  => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
946   $report->set_options_from_form();
947   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
948   CVar->add_custom_variables_to_report('module'         => 'IC',
949                                        'trans_id_field' => 'parts_id',
950                                        'configs'        => $cvar_configs,
951                                        'column_defs'    => \%column_defs,
952                                        'data'           => \@contents);
953
954   my $all_units = AM->retrieve_units(\%myconfig, $form);
955   my $idx       = 0;
956
957   my @subtotals_columns = qw(qty stock_value);
958   my %subtotals         = map { $_ => 0 } @subtotals_columns;
959
960   my $total_stock_value = 0;
961
962   foreach my $entry (@contents) {
963
964     $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}).
965                                    SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
966     map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
967     $total_stock_value   += $entry->{stock_value} * 1;
968     $entry->{qty}         = $form->format_amount(\%myconfig, $entry->{qty});
969 #    $entry->{qty}         = $form->format_amount_units('amount'     => $entry->{qty},
970 #                                                       'part_unit'  => $entry->{partunit},
971 #                                                       'conv_units' => 'convertible');
972     $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
973     $entry->{purchase_price} = $form->format_amount(\%myconfig, $entry->{purchase_price} * 1, 2);
974     $entry->{list_price}     = $form->format_amount(\%myconfig, $entry->{list_price}     * 1, 2);
975
976     my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
977
978     if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
979         && (($idx == (scalar @contents - 1))
980             || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
981
982       my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
983       $row->{qty}->{data}         = $form->format_amount(\%myconfig, $subtotals{qty});
984 #      $row->{qty}->{data}         = $form->format_amount_units('amount'     => $subtotals{qty} * 1,
985 #                                                               'part_unit'  => $entry->{partunit},
986 #                                                               'conv_units' => 'convertible');
987       $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
988       $row->{purchase_price}->{data} = $form->format_amount(\%myconfig, $subtotals{purchase_price} * 1, 2);
989       $row->{list_price}->{data}     = $form->format_amount(\%myconfig, $subtotals{list_price}     * 1, 2);
990
991       %subtotals                  = map { $_ => 0 } @subtotals_columns;
992
993       push @{ $row_set }, $row;
994     }
995
996     if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
997         $report->add_data($row_set);
998     }
999     $idx++;
1000   }
1001
1002   if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
1003     $report->add_separator();
1004
1005     my $row                      = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
1006
1007     my $left_col                 = first { $column_defs{$_}->{visible} } @columns;
1008
1009     $row->{$left_col}->{data}    = $locale->text('Total stock value');
1010     $row->{stock_value}->{data}  = $form->format_amount(\%myconfig, $total_stock_value, 2);
1011     $row->{stock_value}->{align} = 'right';
1012
1013     $report->add_data($row);
1014   }
1015   if ( ! $allrows ) {
1016     $pages->{max}  = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
1017     $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
1018     $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
1019
1020     $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
1021                                                                               {'pages' => $pages , 'base_url' => $href}) );
1022   }
1023
1024   $report->generate_with_headers();
1025
1026   $main::lxdebug->leave_sub();
1027 }
1028
1029 # --------------------------------------------------------------------
1030 # Utility functions
1031 # --------------------------------------------------------------------
1032
1033 sub show_no_warehouses_error {
1034   $main::lxdebug->enter_sub();
1035
1036   my $form     = $main::form;
1037   my %myconfig = %main::myconfig;
1038   my $locale   = $main::locale;
1039
1040   my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
1041
1042   if ($main::auth->check_right($::myconfig{login}, 'config')) {
1043     $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
1044   } else {
1045     $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
1046   }
1047
1048   $form->show_generic_error($msg);
1049
1050   $main::lxdebug->leave_sub();
1051 }
1052
1053 sub get_warehouse_idx {
1054   my ($warehouse_id) = @_;
1055
1056   my $form     = $main::form;
1057
1058   for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
1059     return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
1060   }
1061
1062   return -1;
1063 }
1064
1065 sub get_bin_idx {
1066   my ($warehouse_index, $bin_id) = @_;
1067
1068   my $form     = $main::form;
1069
1070   my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
1071
1072   return -1 if (!$warehouse);
1073
1074   for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
1075     return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
1076   }
1077
1078   return -1;
1079 }
1080
1081 sub new_item {
1082   $main::lxdebug->enter_sub();
1083   my %params = @_;
1084
1085   my $form     = $main::form;
1086
1087   # change callback
1088   $form->{old_callback} = $form->escape($form->{callback}, 1);
1089   $form->{callback}     = $form->escape("$form->{script}?action=$params{action}", 1);
1090
1091   # save all form variables except action in a previousform variable
1092   my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
1093   my @HIDDENS = ();
1094
1095 #  push @HIDDENS,      { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
1096   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
1097   push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
1098   push @HIDDENS,      { 'name' => 'notes',        'value' => $form->{longdescription} };
1099
1100   $form->header();
1101   print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
1102
1103   $main::lxdebug->leave_sub();
1104 }
1105
1106 sub update {
1107   my $form     = $main::form;
1108   call_sub($form->{update_nextsub} || $form->{nextsub});
1109 }
1110
1111 sub continue {
1112   my $form     = $main::form;
1113   call_sub($form->{continue_nextsub} || $form->{nextsub});
1114 }
1115
1116 sub stock {
1117   my $form     = $main::form;
1118   call_sub($form->{stock_nextsub} || $form->{nextsub});
1119 }
1120
1121 sub setup_wh_transfer_warehouse_selection_action_bar {
1122   my ($action) = @_;
1123
1124   for my $bar ($::request->layout->get('actionbar')) {
1125     $bar->add(
1126       action => [
1127         t8('Update'),
1128         submit    => [ '#form', { action => $action } ],
1129         accesskey => 'enter',
1130       ],
1131     );
1132   }
1133 }
1134
1135 sub setup_wh_transfer_warehouse_selection_assembly_action_bar {
1136   my ($action) = @_;
1137
1138   for my $bar ($::request->layout->get('actionbar')) {
1139     $bar->add(
1140       action => [
1141         t8('Update'),
1142         submit    => [ '#form', { action => 'transfer_assembly_update_part' } ],
1143         accesskey => 'enter',
1144       ],
1145       action => [
1146         t8('Produce'),
1147         submit   => [ '#form', { action => 'create_assembly' } ],
1148         disabled => $::form->{parts_id} ? undef : $::locale->text('No assembly has been selected yet.'),
1149       ],
1150     );
1151   }
1152 }
1153
1154 sub setup_wh_transfer_parts_action_bar {
1155   my ($action) = @_;
1156
1157   for my $bar ($::request->layout->get('actionbar')) {
1158     $bar->add(
1159       action => [
1160         t8('Transfer'),
1161         submit    => [ '#form', { action => 'transfer_parts' } ],
1162         accesskey => 'enter',
1163       ],
1164       action => [
1165         t8('Back'),
1166         call => [ 'kivi.history_back' ],
1167       ],
1168     );
1169   }
1170 }
1171
1172 sub setup_wh_removal_parts_selection_action_bar {
1173   my ($action) = @_;
1174
1175   for my $bar ($::request->layout->get('actionbar')) {
1176     $bar->add(
1177       action => [
1178         t8('Transfer out'),
1179         submit    => [ '#form', { action => 'remove_parts' } ],
1180         accesskey => 'enter',
1181       ],
1182       action => [
1183         t8('Back'),
1184         call => [ 'kivi.history_back' ],
1185       ],
1186     );
1187   }
1188 }
1189
1190 sub setup_wh_report_action_bar {
1191   my ($action) = @_;
1192
1193   for my $bar ($::request->layout->get('actionbar')) {
1194     $bar->add(
1195       action => [
1196         t8('Show'),
1197         submit    => [ '#form', { action => 'generate_report' } ],
1198         accesskey => 'enter',
1199       ],
1200     );
1201   }
1202 }
1203
1204 sub setup_wh_journal_action_bar {
1205   my ($action) = @_;
1206
1207   for my $bar ($::request->layout->get('actionbar')) {
1208     $bar->add(
1209       action => [
1210         t8('Show'),
1211         submit    => [ '#form', { action => 'generate_journal' } ],
1212         accesskey => 'enter',
1213       ],
1214     );
1215   }
1216 }
1217 sub setup_wh_journal_list_all_action_bar {
1218   my ($action) = @_;
1219   for my $bar ($::request->layout->get('actionbar')) {
1220     $bar->add(
1221       combobox => [
1222         action => [ t8('Actions') ],
1223         action => [
1224           t8('Disassemble Assembly'),
1225             submit => [ '#form', { action => 'disassemble_assembly' } ],
1226             checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
1227           ],
1228         ],
1229     );
1230   }
1231 }
1232
1233
1234 1;
1235
1236 __END__
1237
1238 =head1 NAME
1239
1240 bin/mozilla/wh.pl - Warehouse frontend.
1241
1242 =head1 FUNCTIONS
1243
1244 =over 4
1245
1246 =item new_item
1247
1248 call new item dialogue from warehouse masks.
1249
1250 PARAMS:
1251   action  => name of sub to be called when new item is done
1252
1253 =back
1254
1255 =cut