arap strict
[kivitendo-erp.git] / bin / mozilla / do.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 #=====================================================================
8 # SQL-Ledger, Accounting
9 # Copyright (c) 1998-2003
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Delivery orders
31 #======================================================================
32
33 use List::Util qw(max sum);
34 use POSIX qw(strftime);
35 use YAML;
36
37 use SL::DO;
38 use SL::IR;
39 use SL::IS;
40 use SL::ReportGenerator;
41 use SL::WH;
42
43 require "bin/mozilla/arap.pl";
44 require "bin/mozilla/common.pl";
45 require "bin/mozilla/invoice_io.pl";
46 require "bin/mozilla/io.pl";
47 require "bin/mozilla/reportgenerator.pl";
48
49 use strict;
50
51 my $print_post;
52
53 1;
54
55 # end of main
56
57 sub check_do_access {
58   $main::auth->assert($main::form->{type} . '_edit');
59 }
60
61 sub set_headings {
62   $main::lxdebug->enter_sub();
63
64   check_do_access();
65
66   my ($action) = @_;
67
68   my $form     = $main::form;
69   my $locale   = $main::locale;
70
71   if ($form->{type} eq 'purchase_delivery_order') {
72     $form->{vc}    = 'vendor';
73     $form->{title} = $action eq "edit" ? $locale->text('Edit Purchase Delivery Order') : $locale->text('Add Purchase Delivery Order');
74   } else {
75     $form->{vc}    = 'customer';
76     $form->{title} = $action eq "edit" ? $locale->text('Edit Sales Delivery Order') : $locale->text('Add Sales Delivery Order');
77   }
78
79   $form->{heading} = $locale->text('Delivery Order');
80
81   $main::lxdebug->leave_sub();
82 }
83
84 sub add {
85   $main::lxdebug->enter_sub();
86
87   check_do_access();
88
89   my $form     = $main::form;
90
91   set_headings("add");
92
93   $form->{callback} = build_std_url('action=add', 'type', 'vc') unless ($form->{callback});
94
95   order_links();
96   prepare_order();
97   display_form();
98
99   $main::lxdebug->leave_sub();
100 }
101
102 sub edit {
103   $main::lxdebug->enter_sub();
104
105   check_do_access();
106
107   my $form     = $main::form;
108
109   # show history button
110   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
111   #/show hhistory button
112
113   $form->{simple_save} = 0;
114
115   set_headings("edit");
116
117   # editing without stuff to edit? try adding it first
118   if ($form->{rowcount} && !$form->{print_and_save}) {
119 #     map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
120 #     if (!$id) {
121
122       # reset rowcount
123       undef $form->{rowcount};
124       add();
125       $main::lxdebug->leave_sub();
126       return;
127 #     }
128   } elsif (!$form->{id}) {
129     add();
130     $main::lxdebug->leave_sub();
131     return;
132   }
133
134   my ($language_id, $printer_id);
135   if ($form->{print_and_save}) {
136     $form->{action}   = "dispatcher";
137     $form->{action_print} = "1";
138     $form->{resubmit} = 1;
139     $language_id      = $form->{language_id};
140     $printer_id       = $form->{printer_id};
141   }
142
143   set_headings("edit");
144
145   order_links();
146   prepare_order();
147
148   if ($form->{print_and_save}) {
149     $form->{language_id} = $language_id;
150     $form->{printer_id}  = $printer_id;
151   }
152
153   display_form();
154
155   $main::lxdebug->leave_sub();
156 }
157
158 sub order_links {
159   $main::lxdebug->enter_sub();
160
161   check_do_access();
162
163   my $form     = $main::form;
164   my %myconfig = %main::myconfig;
165
166   # get customer/vendor
167   $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
168
169   # retrieve order/quotation
170   $form->{webdav}   = $main::webdav;
171   $form->{jsscript} = 1;
172
173   my $editing = $form->{id};
174
175   DO->retrieve('vc'  => $form->{vc},
176                'ids' => $form->{id});
177
178   $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
179   $form->{shipto} = 1 if $form->{id};
180
181   # get customer / vendor
182   if ($form->{vc} eq 'vendor') {
183     IR->get_vendor(\%myconfig, \%$form);
184   } else {
185     IS->get_customer(\%myconfig, \%$form);
186   }
187
188   $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
189   $form->restore_vars(qw(taxincluded)) if $form->{id};
190   $form->restore_vars(qw(salesman_id)) if $editing;
191
192   if ($form->{"all_$form->{vc}"}) {
193     unless ($form->{"$form->{vc}_id"}) {
194       $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
195     }
196   }
197
198   ($form->{ $form->{vc} })  = split /--/, $form->{ $form->{vc} };
199   $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
200
201   $form->{employee} = "$form->{employee}--$form->{employee_id}";
202
203   $main::lxdebug->leave_sub();
204 }
205
206 sub prepare_order {
207   $main::lxdebug->enter_sub();
208
209   check_do_access();
210
211   my $form     = $main::form;
212   my %myconfig = %main::myconfig;
213
214   $form->{formname} = $form->{type} unless $form->{formname};
215
216   my $i = 0;
217   foreach my $ref (@{ $form->{form_details} }) {
218     $form->{rowcount} = ++$i;
219
220     map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
221   }
222   for my $i (1 .. $form->{rowcount}) {
223     if ($form->{id}) {
224       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
225     } else {
226       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
227     }
228     my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
229     $dec           = length $dec;
230     my $decimalplaces = ($dec > 2) ? $dec : 2;
231
232     # copy reqdate from deliverydate for invoice -> order conversion
233     $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
234
235     $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
236
237     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
238     $dec_qty = length $dec_qty;
239     $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
240   }
241
242   $main::lxdebug->leave_sub();
243 }
244
245 sub form_header {
246   $main::lxdebug->enter_sub();
247
248   check_do_access();
249
250   my $form     = $main::form;
251   my %myconfig = %main::myconfig;
252
253   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
254   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
255
256   # use JavaScript Calendar or not
257   $form->{jsscript} = 1;
258
259   #write Trigger
260   my $jsscript = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
261
262   my @old_project_ids = ($form->{"globalproject_id"});
263   map({ push(@old_project_ids, $form->{"project_id_$_"})
264           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
265
266   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
267   $form->get_lists("contacts"       => "ALL_CONTACTS",
268                    "shipto"         => "ALL_SHIPTO",
269                    "projects"       => {
270                      "key"          => "ALL_PROJECTS",
271                      "all"          => 0,
272                      "old_id"       => \@old_project_ids
273                    },
274                    "employees"      => "ALL_EMPLOYEES",
275                    "salesmen"       => "ALL_SALESMEN",
276                    $vc              => "ALL_VC",
277                    "price_factors"  => "ALL_PRICE_FACTORS",
278                    "departments"    => "ALL_DEPARTMENTS",
279                    "business_types" => "ALL_BUSINESS_TYPES",
280     );
281
282   map { $_->{value} = "$_->{description}--$_->{id}" } @{ $form->{ALL_DEPARTMENTS} };
283   map { $_->{value} = "$_->{name}--$_->{id}"        } @{ $form->{ALL_VC} };
284
285   $form->{SHOW_VC_DROP_DOWN} =  $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
286
287   $form->{oldvcname}         =  $form->{"old$form->{vc}"};
288   $form->{oldvcname}         =~ s/--.*//;
289
290   $form->{onload} = "";
291   if ($form->{resubmit}) {
292     if ($form->{format} eq "html") {
293       $form->{onload} = "window.open('about:blank','Beleg'); document.do.target = 'Beleg';";
294     }
295     # emulate click for resubmitting actions
296     $form->{onload} .= "document.do.${_}.click(); " for grep { /^action_/ } keys %$form;
297     $form->{onload} .= "document.do.submit();"
298   }
299
300   $form->header();
301   # Fix für Bug 1082 Erwartet wird: 'abteilungsNAME--abteilungsID'
302   $form->{department} .= '--' . $form->{department_id};
303
304   print $form->parse_html_template('do/form_header');
305
306   $main::lxdebug->leave_sub();
307 }
308
309 sub form_footer {
310   $main::lxdebug->enter_sub();
311
312   check_do_access();
313
314   my $form     = $main::form;
315
316   $form->{PRINT_OPTIONS} = print_options('inline' => 1);
317
318   print $form->parse_html_template('do/form_footer');
319
320   $main::lxdebug->leave_sub();
321 }
322
323 sub update_delivery_order {
324   $main::lxdebug->enter_sub();
325
326   check_do_access();
327
328   my $form     = $main::form;
329   my %myconfig = %main::myconfig;
330
331   set_headings($form->{"id"} ? "edit" : "add");
332
333   $form->{update} = 1;
334
335   my $payment_id;
336   $payment_id = $form->{payment_id} if $form->{payment_id};
337
338   check_name($form->{vc});
339
340   $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
341
342   # for pricegroups
343   my $i = $form->{rowcount};
344
345   if (   ($form->{"partnumber_$i"} eq "")
346       && ($form->{"description_$i"} eq "")
347       && ($form->{"partsgroup_$i"}  eq "")) {
348
349     check_form();
350
351   } else {
352
353     if ($form->{type} eq 'purchase_delivery_order') {
354       IR->retrieve_item(\%myconfig, $form);
355     } else {
356       IS->retrieve_item(\%myconfig, $form);
357     }
358
359     my $rows = scalar @{ $form->{item_list} };
360
361     if ($rows) {
362       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
363
364       if ($rows > 1) {
365
366         select_item();
367         exit;
368
369       } else {
370
371         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
372
373         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
374         $form->{"sellprice_$i"}          = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
375         $form->{"qty_$i"}                = $form->format_amount(\%myconfig, $form->{"qty_$i"});
376       }
377
378       display_form();
379
380     } else {
381
382       # ok, so this is a new part
383       # ask if it is a part or service item
384
385       if (   $form->{"partsgroup_$i"}
386           && ($form->{"partsnumber_$i"} eq "")
387           && ($form->{"description_$i"} eq "")) {
388         $form->{rowcount}--;
389         $form->{"discount_$i"} = "";
390         display_form();
391
392       } else {
393         $form->{"id_$i"}   = 0;
394         new_item();
395       }
396     }
397   }
398
399   $main::lxdebug->leave_sub();
400 }
401
402 sub search {
403   $main::lxdebug->enter_sub();
404
405   check_do_access();
406
407   my $form     = $main::form;
408   my %myconfig = %main::myconfig;
409   my $locale   = $main::locale;
410
411   $form->{vc} = $form->{type} eq 'purchase_delivery_order' ? 'vendor' : 'customer';
412
413   $form->get_lists("projects"     => { "key" => "ALL_PROJECTS",
414                                        "all" => 1 },
415                    "employees"    => "ALL_EMPLOYEES",
416                    "salesmen"     => "ALL_SALESMEN",
417                    "$form->{vc}s" => "ALL_VC");
418
419   $form->{SHOW_VC_DROP_DOWN} =  $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
420   $form->{jsscript}          = 1;
421   $form->{title}             = $locale->text('Delivery Orders');
422
423   $form->header();
424
425   print $form->parse_html_template('do/search');
426
427   $main::lxdebug->leave_sub();
428 }
429
430 sub orders {
431   $main::lxdebug->enter_sub();
432
433   check_do_access();
434
435   my $form     = $main::form;
436   my %myconfig = %main::myconfig;
437   my $locale   = $main::locale;
438   my $cgi      = $main::cgi;
439
440   ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) = split(/--/, $form->{ $form->{vc} });
441
442   report_generator_set_default_sort('transdate', 1);
443
444   DO->transactions();
445
446   $form->{rowcount} = scalar @{ $form->{DO} };
447
448   my @columns = qw(
449     ids                     transdate
450     id                      donumber
451     ordnumber
452     name                    employee
453     shipvia                 globalprojectnumber
454     transaction_description
455     open                    delivered
456   );
457
458   $form->{l_open}      = $form->{l_closed} = "Y" if ($form->{open}      && $form->{closed});
459   $form->{l_delivered} = "Y"                     if ($form->{delivered} && $form->{notdelivered});
460
461   $form->{title}       = $locale->text('Delivery Orders');
462
463   my $attachment_basename = $form->{vc} eq 'vendor' ? $locale->text('purchase_delivery_order_list') : $locale->text('sales_delivery_order_list');
464
465   my $report = SL::ReportGenerator->new(\%myconfig, $form);
466
467   my @hidden_variables = map { "l_${_}" } @columns;
468   push @hidden_variables, $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered donumber ordnumber
469                                           transaction_description transdatefrom transdateto type vc employee_id salesman_id);
470
471   my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
472
473   my %column_defs = (
474     'ids'                     => { 'text' => '', },
475     'transdate'               => { 'text' => $locale->text('Date'), },
476     'id'                      => { 'text' => $locale->text('ID'), },
477     'donumber'                => { 'text' => $locale->text('Delivery Order'), },
478     'ordnumber'               => { 'text' => $locale->text('Order'), },
479     'name'                    => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
480     'employee'                => { 'text' => $locale->text('Salesperson'), },
481     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
482     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
483     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
484     'open'                    => { 'text' => $locale->text('Open'), },
485     'delivered'               => { 'text' => $locale->text('Delivered'), },
486   );
487
488   foreach my $name (qw(id transdate donumber ordnumber name employee shipvia transaction_description)) {
489     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
490     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
491   }
492
493   $form->{"l_type"} = "Y";
494   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
495
496   $column_defs{ids}->{visible} = 'HTML';
497
498   $report->set_columns(%column_defs);
499   $report->set_column_order(@columns);
500
501   $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
502
503   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
504
505   my @options;
506   if ($form->{customer}) {
507     push @options, $locale->text('Customer') . " : $form->{customer}";
508   }
509   if ($form->{vendor}) {
510     push @options, $locale->text('Vendor') . " : $form->{vendor}";
511   }
512   if ($form->{department}) {
513     my ($department) = split /--/, $form->{department};
514     push @options, $locale->text('Department') . " : $department";
515   }
516   if ($form->{donumber}) {
517     push @options, $locale->text('Delivery Order Number') . " : $form->{donumber}";
518   }
519   if ($form->{ordnumber}) {
520     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
521   }
522   if ($form->{transaction_description}) {
523     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
524   }
525   if ($form->{transdatefrom}) {
526     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
527   }
528   if ($form->{transdateto}) {
529     push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
530   }
531   if ($form->{open}) {
532     push @options, $locale->text('Open');
533   }
534   if ($form->{closed}) {
535     push @options, $locale->text('Closed');
536   }
537   if ($form->{delivered}) {
538     push @options, $locale->text('Delivered');
539   }
540   if ($form->{notdelivered}) {
541     push @options, $locale->text('Not delivered');
542   }
543
544   $report->set_options('top_info_text'        => join("\n", @options),
545                        'raw_top_info_text'    => $form->parse_html_template('do/orders_top'),
546                        'raw_bottom_info_text' => $form->parse_html_template('do/orders_bottom'),
547                        'output_format'        => 'HTML',
548                        'title'                => $form->{title},
549                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
550     );
551   $report->set_options_from_form();
552
553   # add sort and escape callback, this one we use for the add sub
554   $form->{callback} = $href .= "&sort=$form->{sort}";
555
556   # escape callback for href
557   my $callback = $form->escape($href);
558
559   my $edit_url       = build_std_url('action=edit', 'type', 'vc');
560   my $edit_order_url = build_std_url('script=oe.pl', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'), 'action=edit');
561
562   my $idx            = 1;
563
564   foreach my $dord (@{ $form->{DO} }) {
565     $dord->{open}      = $dord->{closed}    ? $locale->text('No')  : $locale->text('Yes');
566     $dord->{delivered} = $dord->{delivered} ? $locale->text('Yes') : $locale->text('No');
567
568     my $row = { map { $_ => { 'data' => $dord->{$_} } } @columns };
569
570     $row->{ids}  = {
571       'raw_data' =>   $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $dord->{id})
572                     . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
573       'valign'   => 'center',
574       'align'    => 'center',
575     };
576
577     $row->{donumber}->{link}  = $edit_url       . "&id=" . E($dord->{id})      . "&callback=${callback}";
578     $row->{ordnumber}->{link} = $edit_order_url . "&id=" . E($dord->{oe_id})   . "&callback=${callback}";
579
580     $report->add_data($row);
581
582     $idx++;
583   }
584
585   $report->generate_with_headers();
586
587   $main::lxdebug->leave_sub();
588 }
589
590 sub save {
591   $main::lxdebug->enter_sub();
592
593   check_do_access();
594
595   my $form     = $main::form;
596   my %myconfig = %main::myconfig;
597   my $locale   = $main::locale;
598
599   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
600
601   $form->isblank("transdate", $locale->text('Delivery Order Date missing!'));
602
603   $form->{donumber} =~ s/^\s*//g;
604   $form->{donumber} =~ s/\s*$//g;
605
606   my $msg = ucfirst $form->{vc};
607   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
608
609   # $locale->text('Customer missing!');
610   # $locale->text('Vendor missing!');
611
612   validate_items();
613
614   # if the name changed get new values
615   if (check_name($form->{vc})) {
616     update();
617     exit;
618   }
619
620   $form->{id} = 0 if $form->{saveasnew};
621
622   DO->save();
623
624   # saving the history
625   if(!exists $form->{addition}) {
626     $form->{snumbers} = qq|donumber_| . $form->{donumber};
627         $form->{addition} = "SAVED";
628         $form->save_history($form->dbconnect(\%myconfig));
629   }
630   # /saving the history
631
632   $form->{simple_save} = 1;
633   if(!$form->{print_and_save}) {
634     set_headings("edit");
635     update();
636     exit;
637   }
638   $main::lxdebug->leave_sub();
639 }
640
641 sub delete {
642   $main::lxdebug->enter_sub();
643
644   check_do_access();
645
646   my $form     = $main::form;
647   my $locale   = $main::locale;
648
649   map { delete $form->{$_} } qw(action header login password);
650   my @variables = map { { 'key' => $_, 'value' => $form->{$_} } } grep { '' eq ref $form->{$_} } keys %{ $form };
651
652   $form->{title} = $locale->text('Delete delivery order');
653   $form->header();
654
655   print $form->parse_html_template('do/delete', { 'VARIABLES' => \@variables });
656
657   $main::lxdebug->leave_sub();
658 }
659
660 sub delete_delivery_order {
661   $main::lxdebug->enter_sub();
662
663   check_do_access();
664
665   my $form     = $main::form;
666   my %myconfig = %main::myconfig;
667   my $locale   = $main::locale;
668
669   if (DO->delete()) {
670     # saving the history
671     if(!exists $form->{addition}) {
672       $form->{snumbers} = qq|donumber_| . $form->{donumber};
673           $form->{addition} = "DELETED";
674           $form->save_history($form->dbconnect(\%myconfig));
675     }
676     # /saving the history
677
678     $form->info($locale->text('Delivery Order deleted!'));
679     exit();
680   }
681
682   $form->error($locale->text('Cannot delete delivery order!'));
683
684   $main::lxdebug->leave_sub();
685 }
686
687 sub invoice {
688   $main::lxdebug->enter_sub();
689
690   my $form     = $main::form;
691   my %myconfig = %main::myconfig;
692   my $locale   = $main::locale;
693
694   check_do_access();
695   $main::auth->assert($form->{type} eq 'purchase_delivery_order' ? 'vendor_invoice_edit' : 'invoice_edit');
696
697   $form->{convert_from_do_ids} = $form->{id};
698   $form->{deliverydate}        = $form->{transdate};
699   $form->{transdate}           = $form->{invdate} = $form->current_date(\%myconfig);
700   $form->{duedate}             = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
701   $form->{defaultcurrency}     = $form->get_default_currency(\%myconfig);
702
703   $form->{rowcount}--;
704
705   delete @{$form}{qw(id closed delivered)};
706
707   my ($script, $buysell);
708   if ($form->{type} eq 'purchase_delivery_order') {
709     $form->{title}  = $locale->text('Add Vendor Invoice');
710     $form->{script} = 'ir.pl';
711     $script         = "ir";
712     $buysell        = 'sell';
713
714   } else {
715     $form->{title}  = $locale->text('Add Sales Invoice');
716     $form->{script} = 'is.pl';
717     $script         = "is";
718     $buysell        = 'buy';
719   }
720
721   for my $i (1 .. $form->{rowcount}) {
722     map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"} } qw(ship qty sellprice listprice basefactor);
723   }
724
725   $form->{type} = "invoice";
726
727   # locale messages
728   $locale = new Locale "$myconfig{countrycode}", "$script";
729
730   require "bin/mozilla/$form->{script}";
731
732   my $currency = $form->{currency};
733   invoice_links();
734
735   $form->{currency}     = $currency;
736   $form->{exchangerate} = "";
737   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell);
738   $form->{exchangerate} = $form->{forex} if ($form->{forex});
739
740   prepare_invoice();
741
742   # format amounts
743   for my $i (1 .. $form->{rowcount}) {
744     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
745
746     my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
747     $dec           = length $dec;
748     my $decimalplaces = ($dec > 2) ? $dec : 2;
749
750     # copy delivery date from reqdate for order -> invoice conversion
751     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
752       unless $form->{"deliverydate_$i"};
753
754     $form->{"sellprice_$i"} =
755       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
756                            $decimalplaces);
757
758     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
759     $dec_qty = length $dec_qty;
760     $form->{"qty_$i"} =
761       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
762
763   }
764
765   display_form();
766
767   $main::lxdebug->leave_sub();
768 }
769
770 sub invoice_multi {
771   $main::lxdebug->enter_sub();
772
773   my $form     = $main::form;
774   my %myconfig = %main::myconfig;
775   my $locale   = $main::locale;
776
777   check_do_access();
778   $main::auth->assert($form->{type} eq 'sales_delivery_order' ? 'invoice_edit' : 'vendor_invoice_edit');
779
780   my @do_ids = map { $form->{"trans_id_$_"} } grep { $form->{"multi_id_$_"} } (1..$form->{rowcount});
781
782   if (!scalar @do_ids) {
783     $form->show_generic_error($locale->text('You have not selected any delivery order.'), 'back_button' => 1);
784   }
785
786   map { delete $form->{$_} } grep { m/^(?:trans|multi)_id_\d+/ } keys %{ $form };
787
788   if (!DO->retrieve('vc' => $form->{vc}, 'ids' => \@do_ids)) {
789     $form->show_generic_error($form->{vc} eq 'customer' ?
790                               $locale->text('You cannot create an invoice for delivery orders for different customers.') :
791                               $locale->text('You cannot create an invoice for delivery orders from different vendors.'),
792                               'back_button' => 1);
793   }
794
795   my $source_type              = $form->{type};
796   $form->{convert_from_do_ids} = join ' ', @do_ids;
797   # bei der auswahl von mehreren Lieferscheinen fuer eine Rechnung, die einfach in donumber_array
798   # zwischenspeichern (DO.pm) und als ' '-separierte Liste wieder zurueckschreiben
799   # Hinweis: delete gibt den wert zurueck und loescht danach das element (nett und einfach)
800   # $shell: perldoc perlunc; /delete EXPR
801   $form->{donumber}            = delete $form->{donumber_array};
802   $form->{deliverydate}        = $form->{transdate};
803   $form->{transdate}           = $form->current_date(\%myconfig);
804   $form->{duedate}             = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
805   $form->{type}                = "invoice";
806   $form->{closed}              = 0;
807   $form->{defaultcurrency}     = $form->get_default_currency(\%myconfig);
808
809   my ($script, $buysell);
810   if ($source_type eq 'purchase_delivery_order') {
811     $form->{title}  = $locale->text('Add Vendor Invoice');
812     $form->{script} = 'ir.pl';
813     $script         = "ir";
814     $buysell        = 'sell';
815
816   } else {
817     $form->{title}  = $locale->text('Add Sales Invoice');
818     $form->{script} = 'is.pl';
819     $script         = "is";
820     $buysell        = 'buy';
821   }
822
823   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
824
825   $form->{rowcount} = 0;
826   foreach my $ref (@{ $form->{form_details} }) {
827     $form->{rowcount}++;
828     $ref->{reqdate} ||= $ref->{dord_transdate}; # copy transdates into each invoice row
829     map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{ $ref };
830     map { $form->{"${_}_$form->{rowcount}"} = $form->format_amount(\%myconfig, $ref->{$_}) } qw(qty sellprice discount lastcost);
831                 $form->{"discount_$form->{rowcount}"}   = $form->{"discount_$form->{rowcount}"}  * 100; #s.a. Bug 1151
832                 # Anm.: Eine Ã„nderung des discounts in der SL/DO.pm->retrieve (select (doi.discount * 100) as discount) ergibt in psql einen
833                 # Wert von 10.0000001490116. Ferner ist der Rabatt in der Rechnung dann bei 1.0 (?). Deswegen lasse ich das hier. jb 10.10.09
834   }
835   delete $form->{form_details};
836
837   $locale = new Locale "$myconfig{countrycode}", "$script";
838
839   require "bin/mozilla/$form->{script}";
840
841   invoice_links();
842   prepare_invoice();
843   display_form();
844
845   $main::lxdebug->leave_sub();
846 }
847
848 sub save_as_new {
849   $main::lxdebug->enter_sub();
850
851   check_do_access();
852
853   my $form     = $main::form;
854
855   $form->{saveasnew} = 1;
856   $form->{closed}    = 0;
857   $form->{delivered} = 0;
858   map { delete $form->{$_} } qw(printed emailed queued);
859
860   # Let Lx-Office assign a new order number if the user hasn't changed the
861   # previous one. If it has been changed manually then use it as-is.
862   $form->{donumber} =~ s/^\s*//g;
863   $form->{donumber} =~ s/\s*$//g;
864   if ($form->{saved_donumber} && ($form->{saved_donumber} eq $form->{donumber})) {
865     delete($form->{donumber});
866   }
867
868   save();
869
870   $main::lxdebug->leave_sub();
871 }
872
873 sub e_mail {
874   $main::lxdebug->enter_sub();
875
876   check_do_access();
877
878   my $form     = $main::form;
879
880   $form->{print_and_save} = 1;
881
882   $print_post = 1;
883
884   my $saved_form = save_form();
885
886   save();
887
888   restore_form($saved_form, 0, qw(id ordnumber quonumber));
889
890   edit_e_mail();
891
892   $main::lxdebug->leave_sub();
893 }
894
895 sub calculate_stock_in_out {
896   $main::lxdebug->enter_sub();
897
898   my $form     = $main::form;
899
900   my $i = shift;
901
902   if (!$form->{"id_${i}"}) {
903     $main::lxdebug->leave_sub();
904     return '';
905   }
906
907   my $all_units = AM->retrieve_all_units();
908
909   my $in_out   = $form->{type} =~ /^sales/ ? 'out' : 'in';
910   my $sinfo    = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_${i}"});
911
912   my $sum      = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $sinfo });
913
914   my $content  = $form->format_amount_units('amount'      => $sum * 1,
915                                             'part_unit'   => $form->{"partunit_$i"},
916                                             'amount_unit' => $all_units->{$form->{"partunit_$i"}}->{base_unit},
917                                             'conv_units'  => 'convertible_not_smaller',
918                                             'max_places'  => 2);
919   $content    .= qq| <input type="button" onclick="open_stock_in_out_window('${in_out}', $i);" value="?">|;
920
921   $main::lxdebug->leave_sub();
922
923   return $content;
924 }
925
926 sub get_basic_bin_wh_info {
927   $main::lxdebug->enter_sub();
928
929   my $stock_info = shift;
930
931   my $form     = $main::form;
932
933   foreach my $sinfo (@{ $stock_info }) {
934     next unless ($sinfo->{bin_id});
935
936     my $bin_info = WH->get_basic_bin_info('id' => $sinfo->{bin_id});
937     map { $sinfo->{"${_}_description"} = $sinfo->{"${_}description"} = $bin_info->{"${_}_description"} } qw(bin warehouse);
938   }
939
940   $main::lxdebug->leave_sub();
941 }
942
943 sub stock_in_out_form {
944   $main::lxdebug->enter_sub();
945
946   my $form     = $main::form;
947
948   if ($form->{in_out} eq 'out') {
949     stock_out_form();
950   } else {
951     stock_in_form();
952   }
953
954   $main::lxdebug->leave_sub();
955 }
956
957 sub redo_stock_info {
958   $main::lxdebug->enter_sub();
959
960   my %params    = @_;
961
962   my $form     = $main::form;
963
964   my @non_empty = grep { $_->{qty} } @{ $params{stock_info} };
965
966   if ($params{add_empty_row}) {
967     push @non_empty, {
968       'warehouse_id' => scalar(@non_empty) ? $non_empty[-1]->{warehouse_id} : undef,
969       'bin_id'       => scalar(@non_empty) ? $non_empty[-1]->{bin_id}       : undef,
970     };
971   }
972
973   @{ $params{stock_info} } = @non_empty;
974
975   $main::lxdebug->leave_sub();
976 }
977
978 sub update_stock_in {
979   $main::lxdebug->enter_sub();
980
981   my $form     = $main::form;
982   my %myconfig = %main::myconfig;
983
984   my $stock_info = [];
985
986   foreach my $i (1..$form->{rowcount}) {
987     $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
988     push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
989   }
990
991   display_stock_in_form($stock_info);
992
993   $main::lxdebug->leave_sub();
994 }
995
996 sub stock_in_form {
997   $main::lxdebug->enter_sub();
998
999   my $form     = $main::form;
1000
1001   my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
1002
1003   display_stock_in_form($stock_info);
1004
1005   $main::lxdebug->leave_sub();
1006 }
1007
1008 sub display_stock_in_form {
1009   $main::lxdebug->enter_sub();
1010
1011   my $stock_info = shift;
1012
1013   my $form     = $main::form;
1014   my %myconfig = %main::myconfig;
1015   my $locale   = $main::locale;
1016
1017   $form->{title} = $locale->text('Stock');
1018
1019   my $part_info  = IC->get_basic_part_info('id' => $form->{parts_id});
1020
1021   my $units      = AM->retrieve_units(\%myconfig, $form);
1022   my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
1023
1024   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
1025                                      'bins'   => 'BINS' });
1026
1027   redo_stock_info('stock_info' => $stock_info, 'add_empty_row' => !$form->{delivered});
1028
1029   get_basic_bin_wh_info($stock_info);
1030
1031   $form->header();
1032   print $form->parse_html_template('do/stock_in_form', { 'UNITS'      => $units_data,
1033                                                          'STOCK_INFO' => $stock_info,
1034                                                          'PART_INFO'  => $part_info, });
1035
1036   $main::lxdebug->leave_sub();
1037 }
1038
1039 sub set_stock_in {
1040   $main::lxdebug->enter_sub();
1041
1042   my $form     = $main::form;
1043   my %myconfig = %main::myconfig;
1044
1045   my $stock_info = [];
1046
1047   foreach my $i (1..$form->{rowcount}) {
1048     $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1049
1050     next if ($form->{"qty_$i"} <= 0);
1051
1052     push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
1053   }
1054
1055   $form->{stock} = YAML::Dump($stock_info);
1056
1057   $form->header();
1058   print $form->parse_html_template('do/set_stock_in_out');
1059
1060   $main::lxdebug->leave_sub();
1061 }
1062
1063 sub stock_out_form {
1064   $main::lxdebug->enter_sub();
1065
1066   my $form     = $main::form;
1067   my %myconfig = %main::myconfig;
1068   my $locale   = $main::locale;
1069
1070   $form->{title} = $locale->text('Release From Stock');
1071
1072   my $part_info  = IC->get_basic_part_info('id' => $form->{parts_id});
1073
1074   my $units      = AM->retrieve_units(\%myconfig, $form);
1075   my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
1076
1077   my @contents   = DO->get_item_availability('parts_id' => $form->{parts_id});
1078
1079   my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
1080
1081   if (!$form->{delivered}) {
1082     foreach my $row (@contents) {
1083       $row->{available_qty} = $form->format_amount_units('amount'      => $row->{qty} * 1,
1084                                                          'part_unit'   => $part_info->{unit},
1085                                                          'conv_units'  => 'convertible_not_smaller',
1086                                                          'max_places'  => 2);
1087
1088       foreach my $sinfo (@{ $stock_info }) {
1089         next if (($row->{bin_id}       != $sinfo->{bin_id}) ||
1090                  ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1091                  ($row->{chargenumber} ne $sinfo->{chargenumber}));
1092
1093         map { $row->{"stock_$_"} = $sinfo->{$_} } qw(qty unit error);
1094       }
1095     }
1096
1097   } else {
1098     get_basic_bin_wh_info($stock_info);
1099
1100     foreach my $sinfo (@{ $stock_info }) {
1101       map { $sinfo->{"stock_$_"} = $sinfo->{$_} } qw(qty unit);
1102     }
1103   }
1104
1105   $form->header();
1106   print $form->parse_html_template('do/stock_out_form', { 'UNITS'      => $units_data,
1107                                                           'WHCONTENTS' => $form->{delivered} ? $stock_info : \@contents,
1108                                                           'PART_INFO'  => $part_info, });
1109
1110   $main::lxdebug->leave_sub();
1111 }
1112
1113 sub set_stock_out {
1114   $main::lxdebug->enter_sub();
1115
1116   my $form     = $main::form;
1117   my %myconfig = %main::myconfig;
1118   my $locale   = $main::locale;
1119
1120   my $stock_info = [];
1121
1122   foreach my $i (1 .. $form->{rowcount}) {
1123     $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1124
1125     next if ($form->{"qty_$i"} <= 0);
1126
1127     push @{ $stock_info }, {
1128       'warehouse_id' => $form->{"warehouse_id_$i"},
1129       'bin_id'       => $form->{"bin_id_$i"},
1130       'chargenumber' => $form->{"chargenumber_$i"},
1131       'qty'          => $form->{"qty_$i"},
1132       'unit'         => $form->{"unit_$i"},
1133       'row'          => $i,
1134     };
1135   }
1136
1137   my @errors     = DO->check_stock_availability('requests' => $stock_info,
1138                                                 'parts_id' => $form->{parts_id});
1139
1140   $form->{stock} = YAML::Dump($stock_info);
1141
1142   if (@errors) {
1143     $form->{ERRORS} = [];
1144     map { push @{ $form->{ERRORS} }, $locale->text('Error in row #1: The quantity you entered is bigger than the stocked quantity.', $_->{row}); } @errors;
1145     stock_in_out_form();
1146
1147   } else {
1148     $form->header();
1149     print $form->parse_html_template('do/set_stock_in_out');
1150   }
1151
1152   $main::lxdebug->leave_sub();
1153 }
1154
1155 sub transfer_in {
1156   $main::lxdebug->enter_sub();
1157
1158   my $form     = $main::form;
1159   my %myconfig = %main::myconfig;
1160   my $locale   = $main::locale;
1161
1162   if (DO->is_marked_as_delivered('id' => $form->{id})) {
1163     $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred in.'), 'back_button' => 1);
1164   }
1165
1166   my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_in_${_}"} } (1 .. $form->{rowcount});
1167   my @all_requests;
1168
1169   if (@part_ids) {
1170     my $units         = AM->retrieve_units(\%myconfig, $form);
1171     my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1172     my %request_map;
1173
1174     $form->{ERRORS}   = [];
1175
1176     foreach my $i (1 .. $form->{rowcount}) {
1177       next unless ($form->{"id_$i"} && $form->{"stock_in_$i"});
1178
1179       my $row_sum_base_qty = 0;
1180       my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1181
1182       foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_in_$i"}) }) {
1183         $request->{parts_id}  = $form->{"id_$i"};
1184         $row_sum_base_qty    += $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1185
1186         push @all_requests, $request;
1187       }
1188
1189       next if (0 == $row_sum_base_qty);
1190
1191       my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1192
1193       if ($do_base_qty != $row_sum_base_qty) {
1194         push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no stock at all or the full quantity of #2 #3.',
1195                                                  $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1196       }
1197     }
1198
1199     if (@{ $form->{ERRORS} }) {
1200       push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1201
1202       update();
1203       $main::lxdebug->leave_sub();
1204
1205       exit 0;
1206     }
1207   }
1208
1209   DO->transfer_in_out('direction' => 'in',
1210                       'requests'  => \@all_requests);
1211
1212   $form->{delivered} = 1;
1213
1214   save();
1215
1216   $main::lxdebug->leave_sub();
1217 }
1218
1219 sub transfer_out {
1220   $main::lxdebug->enter_sub();
1221
1222   my $form     = $main::form;
1223   my %myconfig = %main::myconfig;
1224   my $locale   = $main::locale;
1225
1226   if (DO->is_marked_as_delivered('id' => $form->{id})) {
1227     $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred out.'), 'back_button' => 1);
1228   }
1229
1230   my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_out_${_}"} } (1 .. $form->{rowcount});
1231   my @all_requests;
1232
1233   if (@part_ids) {
1234     my $units         = AM->retrieve_units(\%myconfig, $form);
1235     my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1236     my %request_map;
1237
1238     $form->{ERRORS}   = [];
1239
1240     foreach my $i (1 .. $form->{rowcount}) {
1241       next unless ($form->{"id_$i"} && $form->{"stock_out_$i"});
1242
1243       my $row_sum_base_qty = 0;
1244       my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1245
1246       foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_out_$i"}) }) {
1247         $request->{parts_id} = $form->{"id_$i"};
1248         $request->{base_qty} = $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1249
1250         my $map_key          = join '--', ($form->{"id_$i"}, @{$request}{qw(warehouse_id bin_id chargenumber)});
1251
1252         $request_map{$map_key}                 ||= $request;
1253         $request_map{$map_key}->{sum_base_qty} ||= 0;
1254         $request_map{$map_key}->{sum_base_qty}  += $request->{base_qty};
1255         $row_sum_base_qty                       += $request->{base_qty};
1256
1257         push @all_requests, $request;
1258       }
1259
1260       next if (0 == $row_sum_base_qty);
1261
1262       my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1263
1264       if ($do_base_qty != $row_sum_base_qty) {
1265         push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no transfer at all or the full quantity of #2 #3.',
1266                                                  $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1267       }
1268     }
1269
1270     if (%request_map) {
1271       my @bin_ids      = map { $_->{bin_id} } values %request_map;
1272       my %bin_info_map = WH->get_basic_bin_info('id' => \@bin_ids);
1273       my @contents     = DO->get_item_availability('parts_id' => \@part_ids);
1274
1275       foreach my $inv (@contents) {
1276         my $map_key = join '--', @{$inv}{qw(parts_id warehouse_id bin_id chargenumber)};
1277
1278         next unless ($request_map{$map_key});
1279
1280         my $request    = $request_map{$map_key};
1281         $request->{ok} = $request->{sum_base_qty} <= $inv->{qty};
1282       }
1283
1284       foreach my $request (values %request_map) {
1285         next if ($request->{ok});
1286
1287         my $pinfo = $part_info_map{$request->{parts_id}};
1288         my $binfo = $bin_info_map{$request->{bin_id}};
1289
1290         push @{ $form->{ERRORS} }, $locale->text("There is not enough available of '#1' at warehouse '#2', bin '#3', #4, for the transfer of #5.",
1291                                                  $pinfo->{description}, $binfo->{warehouse_description}, $binfo->{bin_description},
1292                                                  $request->{chargenumber} ? $locale->text('chargenumber #1', $request->{chargenumber}) : $locale->text('no chargenumber'),
1293                                                  $form->format_amount_units('amount'      => $request->{sum_base_qty},
1294                                                                             'part_unit'   => $pinfo->{unit},
1295                                                                             'conv_units'  => 'convertible_not_smaller'));
1296       }
1297     }
1298
1299     if (@{ $form->{ERRORS} }) {
1300       push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1301
1302       update();
1303       $main::lxdebug->leave_sub();
1304
1305       exit 0;
1306     }
1307   }
1308
1309   DO->transfer_in_out('direction' => 'out',
1310                       'requests'  => \@all_requests);
1311
1312   $form->{delivered} = 1;
1313
1314   save();
1315
1316   $main::lxdebug->leave_sub();
1317 }
1318
1319 sub mark_closed {
1320   $main::lxdebug->enter_sub();
1321
1322   my $form     = $main::form;
1323
1324   DO->close_orders('ids' => [ $form->{id} ]);
1325
1326   $form->{closed} = 1;
1327
1328   update();
1329
1330   $main::lxdebug->leave_sub();
1331 }
1332
1333
1334 sub yes {
1335   call_sub($main::form->{yes_nextsub});
1336 }
1337
1338 sub no {
1339   call_sub($main::form->{no_nextsub});
1340 }
1341
1342 sub update {
1343   call_sub($main::form->{update_nextsub} || $main::form->{nextsub} || 'update_delivery_order');
1344 }
1345
1346 sub dispatcher {
1347   my $form     = $main::form;
1348   my $locale   = $main::locale;
1349
1350   foreach my $action (qw(update ship_to print e_mail save transfer_out transfer_in mark_closed save_as_new invoice delete)) {
1351     if ($form->{"action_${action}"}) {
1352       call_sub($action);
1353       return;
1354     }
1355   }
1356
1357   $form->error($locale->text('No action defined.'));
1358 }