7456cd5f92ce99bfc8a64ada9eb7d13ec94217c0
[kivitendo-erp.git] / bin / mozilla / ar.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) 2001
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., 51 Franklin Street, Fifth Floor, Boston,
28 # MA 02110-1335, USA.
29 #======================================================================
30 #
31 # Accounts Receivables
32 #
33 #======================================================================
34
35 use POSIX qw(strftime);
36 use List::Util qw(sum first max);
37 use List::UtilsBy qw(sort_by);
38
39 use SL::AR;
40 use SL::FU;
41 use SL::IS;
42 use SL::DB::Default;
43 use SL::DB::Invoice;
44 use SL::ReportGenerator;
45
46 require "bin/mozilla/arap.pl";
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
49
50 use strict;
51 #use warnings;
52
53 # this is for our long dates
54 # $locale->text('January')
55 # $locale->text('February')
56 # $locale->text('March')
57 # $locale->text('April')
58 # $locale->text('May ')
59 # $locale->text('June')
60 # $locale->text('July')
61 # $locale->text('August')
62 # $locale->text('September')
63 # $locale->text('October')
64 # $locale->text('November')
65 # $locale->text('December')
66
67 # this is for our short month
68 # $locale->text('Jan')
69 # $locale->text('Feb')
70 # $locale->text('Mar')
71 # $locale->text('Apr')
72 # $locale->text('May')
73 # $locale->text('Jun')
74 # $locale->text('Jul')
75 # $locale->text('Aug')
76 # $locale->text('Sep')
77 # $locale->text('Oct')
78 # $locale->text('Nov')
79 # $locale->text('Dec')
80
81 sub add {
82   $main::lxdebug->enter_sub();
83
84   $main::auth->assert('ar_transactions');
85
86   my $form     = $main::form;
87   my %myconfig = %main::myconfig;
88
89   # saving the history
90   if(!exists $form->{addition} && ($form->{id} ne "")) {
91     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
92     $form->{addition} = "ADDED";
93     $form->save_history;
94   }
95   # /saving the history
96
97   $form->{title}    = "Add";
98   $form->{callback} = "ar.pl?action=add" unless $form->{callback};
99
100   AR->get_transdate(\%myconfig, $form);
101   $form->{initial_transdate} = $form->{transdate};
102   create_links(dont_save => 1);
103   $form->{transdate} = $form->{initial_transdate};
104   &display_form;
105   $main::lxdebug->leave_sub();
106 }
107
108 sub edit {
109   $main::lxdebug->enter_sub();
110
111   $main::auth->assert('ar_transactions');
112
113   my $form     = $main::form;
114
115   # show history button
116   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
117   #/show hhistory button
118   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
119   $form->{title} = "Edit";
120
121   create_links();
122   &display_form;
123
124   $main::lxdebug->leave_sub();
125 }
126
127 sub display_form {
128   $main::lxdebug->enter_sub();
129
130   $main::auth->assert('ar_transactions');
131
132   my $form     = $main::form;
133
134   &form_header;
135   &form_footer;
136
137   $main::lxdebug->leave_sub();
138 }
139
140 sub _retrieve_invoice_object {
141   return undef if !$::form->{id};
142   return $::form->{invoice_obj} if $::form->{invoice_obj} && $::form->{invoice_obj}->id == $::form->{id};
143   return SL::DB::Invoice->new(id => $::form->{id})->load;
144 }
145
146 sub create_links {
147   $main::lxdebug->enter_sub();
148
149   $main::auth->assert('ar_transactions');
150
151   my %params   = @_;
152   my $form     = $main::form;
153   my %myconfig = %main::myconfig;
154
155   $form->create_links("AR", \%myconfig, "customer");
156   $form->{invoice_obj} = _retrieve_invoice_object();
157
158   my %saved;
159   if (!$params{dont_save}) {
160     %saved = map { ($_ => $form->{$_}) } qw(direct_debit id taxincluded);
161     $saved{duedate} = $form->{duedate} if $form->{duedate};
162     $saved{currency} = $form->{currency} if $form->{currency};
163   }
164
165   IS->get_customer(\%myconfig, \%$form);
166
167   $form->{$_}          = $saved{$_} for keys %saved;
168   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
169   $form->{rowcount}    = 1;
170
171   # currencies
172   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
173
174   $form->{selectcurrency} = "";
175   map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
176
177   # customers
178   if (@{ $form->{all_customer} || [] }) {
179     $form->{customer} = "$form->{customer}--$form->{customer_id}";
180     map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
181       (@{ $form->{all_customer} });
182   }
183
184   $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
185
186   $form->{employee} = "$form->{employee}--$form->{employee_id}";
187
188   # sales staff
189   if (@{ $form->{all_employees} || [] }) {
190     $form->{selectemployee} = "";
191     map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
192       (@{ $form->{all_employees} || [] });
193   }
194
195   # build the popup menus
196   $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
197
198   AR->setup_form($form);
199
200   $form->{locked} =
201     ($form->datetonum($form->{transdate}, \%myconfig) <=
202      $form->datetonum($form->{closedto}, \%myconfig));
203
204   $main::lxdebug->leave_sub();
205 }
206
207 sub form_header {
208   $main::lxdebug->enter_sub();
209
210   $main::auth->assert('ar_transactions');
211
212   my $form     = $main::form;
213   my %myconfig = %main::myconfig;
214   my $locale   = $main::locale;
215   my $cgi      = $::request->{cgi};
216
217   $form->{invoice_obj} = _retrieve_invoice_object();
218
219   my ($title, $readonly, $exchangerate, $rows);
220   my ($notes, $customer, $employee, $amount, $project);
221   my ($ARselected);
222
223
224   $title = $form->{title};
225   # $locale->text('Add Accounts Receivables Transaction')
226   # $locale->text('Edit Accounts Receivables Transaction')
227   $form->{title} = $locale->text("$title Accounts Receivables Transaction");
228
229   $form->{javascript} = qq|<script type="text/javascript">
230   <!--
231   function setTaxkey(accno, row) {
232     var taxkey = accno.options[accno.selectedIndex].value;
233     var reg = /--([0-9]*)/;
234     var found = reg.exec(taxkey);
235     var index = found[1];
236     index = parseInt(index);
237     var tax = 'taxchart_' + row;
238     for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
239       var reg2 = new RegExp("^"+ index, "");
240       if (reg2.exec(document.getElementById(tax).options[i].value)) {
241         document.getElementById(tax).options[i].selected = true;
242         break;
243       }
244     }
245   };
246   //-->
247   </script>|;
248   # show history button js
249   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
250   #/show history button js
251   $readonly = ($form->{id}) ? "readonly" : "";
252
253   $form->{radier} = ($::instance_conf->get_ar_changeable == 2)
254                       ? ($form->current_date(\%myconfig) eq $form->{gldate})
255                       : ($::instance_conf->get_ar_changeable == 1);
256   $readonly = ($form->{radier}) ? "" : $readonly;
257
258   # set option selected
259   foreach my $item (qw(customer currency employee)) {
260     $form->{"select$item"} =~ s/ selected//;
261     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
262   }
263
264   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
265   $form->{exchangerate} = $form->{forex} if $form->{forex};
266
267   # format exchangerate
268   $form->{exchangerate}    = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
269
270   $rows = max 2, $form->numtextrows($form->{notes}, 50);
271
272   my @old_project_ids = grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{rowcount};
273
274   $form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
275                                     "all"       => 0,
276                                     "old_id"    => \@old_project_ids },
277                    "charts"    => { "key"       => "ALL_CHARTS",
278                                     "transdate" => $form->{transdate} },
279                    "taxcharts" => { "key"       => "ALL_TAXCHARTS",
280                                     "module"    => "AR" },);
281
282   $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
283
284   $_->{link_split} = { map { $_ => 1 } split/:/, $_->{link} } for @{ $form->{ALL_CHARTS} };
285
286   my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} };
287
288   my (@AR_amount_values);
289   my (@AR_values);
290   my (@AR_paid_values);
291   my %chart_labels;
292   my %charts;
293   my $taxchart_init;
294
295   foreach my $item (@{ $form->{ALL_CHARTS} }) {
296     if ($item->{link_split}{AR_amount}) {
297       $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
298       my $key = "$item->{accno}--$item->{tax_id}";
299       push(@AR_amount_values, $key);
300     } elsif ($item->{link_split}{AR}) {
301       push(@AR_values, $item->{accno});
302     } elsif ($item->{link_split}{AR_paid}) {
303       push(@AR_paid_values, $item->{accno});
304     }
305
306     # weirdness for AR_amount
307     $chart_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
308     $chart_labels{"$item->{accno}--$item->{tax_id}"} = "$item->{accno}--$item->{description}";
309
310     $charts{$item->{accno}} = $item;
311   }
312
313   my %taxchart_labels = ();
314   my @taxchart_values = ();
315   my %taxcharts = ();
316   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
317     my $key = "$item->{id}--$item->{rate}";
318     $taxchart_init = $key if ($taxchart_init eq $item->{id});
319     push(@taxchart_values, $key);
320     $taxchart_labels{$key} = "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
321     $taxcharts{$item->{id}} = $item;
322   }
323
324   my $follow_up_vc         =  $form->{customer};
325   $follow_up_vc            =~ s/--.*?//;
326   my $follow_up_trans_info =  "$form->{invnumber} ($follow_up_vc)";
327
328   $form->{javascript} .=
329     qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
330     qq|<script type="text/javascript" src="js/follow_up.js"></script>| .
331     qq|<script type="text/javascript" src="js/kivi.Draft.js"></script>|;
332
333 #  $amount  = $locale->text('Amount');
334 #  $project = $locale->text('Project');
335
336   my @transactions;
337   for my $i (1 .. $form->{rowcount}) {
338     my $transaction = {
339       amount     => $form->{"amount_$i"},
340       tax        => $form->{"tax_$i"},
341       project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"},
342     };
343
344     my $selected_accno_full;
345     my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
346     my $item = $charts{$accno_row};
347     $selected_accno_full = "$item->{accno}--$item->{tax_id}";
348
349     my $selected_taxchart = $form->{"taxchart_$i"};
350     my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
351     my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
352
353     if ($previous_accno &&
354         ($previous_accno eq $selected_accno) &&
355         ($previous_tax_id ne $selected_tax_id)) {
356       my $item = $taxcharts{$selected_tax_id};
357       $selected_taxchart = "$item->{id}--$item->{rate}";
358     }
359
360     if (!$form->{"taxchart_$i"}) {
361       if ($form->{"AR_amount_$i"} =~ m/.--./) {
362         $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
363       } else {
364         $selected_taxchart = $taxchart_init;
365       }
366     }
367
368     $transaction->{selectAR_amount} =
369       NTI($cgi->popup_menu('-name' => "AR_amount_$i",
370                            '-id' => "AR_amount_$i",
371                            '-style' => 'width:400px',
372                            '-onChange' => "setTaxkey(this, $i)",
373                            '-values' => \@AR_amount_values,
374                            '-labels' => \%chart_labels,
375                            '-default' => $selected_accno_full))
376       . $cgi->hidden('-name' => "previous_AR_amount_$i",
377                      '-default' => $selected_accno_full);
378
379     $transaction->{taxchart} =
380       NTI($cgi->popup_menu('-name' => "taxchart_$i",
381                            '-id' => "taxchart_$i",
382                            '-style' => 'width:200px',
383                            '-values' => \@taxchart_values,
384                            '-labels' => \%taxchart_labels,
385                            '-default' => $selected_taxchart));
386
387     push @transactions, $transaction;
388   }
389
390   $form->{invtotal_unformatted} = $form->{invtotal};
391
392   $ARselected =
393     NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
394                          '-style' => 'width:400px',
395                          '-values' => \@AR_values, '-labels' => \%chart_labels,
396                          '-default' => $form->{ARselected}));
397
398
399   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
400
401   my $now = $form->current_date(\%myconfig);
402
403   my @payments;
404   for my $i (1 .. $form->{paidaccounts}) {
405     my $payment = {
406       paid             => $form->{"paid_$i"},
407       exchangerate     => $form->{"exchangerate_$i"} || '',
408       gldate           => $form->{"gldate_$i"},
409       acc_trans_id     => $form->{"acc_trans_id_$i"},
410       source           => $form->{"source_$i"},
411       memo             => $form->{"memo_$i"},
412       AR_paid          => $form->{"AR_paid_$i"},
413       forex            => $form->{"forex_$i"},
414       datepaid         => $form->{"datepaid_$i"},
415       paid_project_id  => $form->{"paid_project_id_$i"},
416       gldate           => $form->{"gldate_$i"},
417     };
418
419     # default account for current assets (i.e. 1801 - SKR04) if no account is selected
420     $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
421
422     $payment->{selectAR_paid} =
423       NTI($cgi->popup_menu('-name' => "AR_paid_$i",
424                            '-id' => "AR_paid_$i",
425                            '-values' => \@AR_paid_values,
426                            '-labels' => \%chart_labels,
427                            '-default' => $payment->{AR_paid} || $form->{accno_arap}));
428
429
430
431     $payment->{changeable} =
432         SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
433       : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
434       :                                                           1;
435
436     #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
437     if ($form->date_closed($payment->{"gldate_$i"})) {
438         $payment->{changeable} = 0;
439     }
440
441     push @payments, $payment;
442   }
443
444   my @empty = grep { $_->{paid} eq '' } @payments;
445   @payments = (
446     (sort_by { DateTime->from_kivitendo($_->{datepaid}) } grep { $_->{paid} ne '' } @payments),
447     @empty,
448   );
449
450   $form->{totalpaid} = sum map { $_->{paid} } @payments;
451
452   $form->header;
453   print $::form->parse_html_template('ar/form_header', {
454     paid_missing         => $::form->{invtotal} - $::form->{totalpaid},
455     show_exch            => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
456     payments             => \@payments,
457     transactions         => \@transactions,
458     project_labels       => \%project_labels,
459     rows                 => $rows,
460     ARselected           => $ARselected,
461     title_str            => $title,
462     follow_up_trans_info => $follow_up_trans_info,
463     today                => DateTime->today,
464   });
465
466   $main::lxdebug->leave_sub();
467 }
468
469 sub form_footer {
470   $main::lxdebug->enter_sub();
471
472   $main::auth->assert('ar_transactions');
473
474   my $form     = $main::form;
475   my %myconfig = %main::myconfig;
476   my $locale   = $main::locale;
477   my $cgi      = $::request->{cgi};
478
479   if ( $form->{id} ) {
480     my $follow_ups = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1);
481     if ( @{ $follow_ups} ) {
482       $form->{follow_up_length} = scalar(@{$follow_ups});
483       $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
484     }
485   }
486
487   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
488   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
489
490   $form->{is_closed} = $transdate <= $closedto;
491
492   # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
493   $form->{show_storno_button} =
494     $form->{id} &&
495     !IS->has_storno(\%myconfig, $form, 'ar') &&
496     !IS->is_storno(\%myconfig, $form, 'ar') &&
497     ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
498
499   $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
500
501   print $::form->parse_html_template('ar/form_footer');
502
503   $main::lxdebug->leave_sub();
504 }
505
506 sub mark_as_paid {
507   $main::lxdebug->enter_sub();
508
509   $main::auth->assert('ar_transactions');
510
511   my $form     = $main::form;
512   my %myconfig = %main::myconfig;
513
514   &mark_as_paid_common(\%myconfig,"ar");
515
516   $main::lxdebug->leave_sub();
517 }
518
519 sub show_draft {
520   $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
521   $::form->{gldate}    = $::form->{transdate} if !$::form->{gldate};
522   update();
523 }
524
525 sub update {
526   $main::lxdebug->enter_sub();
527
528   $main::auth->assert('ar_transactions');
529
530   my $form     = $main::form;
531   my %myconfig = %main::myconfig;
532
533   my $display = shift;
534
535   my ($totaltax, $exchangerate);
536
537   $form->{invtotal} = 0;
538
539   delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
540
541   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
542     qw(exchangerate creditlimit creditremaining);
543
544   my @flds  = qw(amount AR_amount projectnumber oldprojectnumber project_id);
545   my $count = 0;
546   my @a     = ();
547
548   for my $i (1 .. $form->{rowcount}) {
549     $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
550     if ($form->{"amount_$i"}) {
551       push @a, {};
552       my $j = $#a;
553       my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
554
555       my $tmpnetamount;
556       ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
557
558       $totaltax += $form->{"tax_$i"};
559       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
560       $count++;
561     }
562   }
563
564   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
565   $form->{rowcount} = $count + 1;
566   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
567
568   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
569   $form->{exchangerate} = $form->{forex} if $form->{forex};
570
571   $form->{invdate} = $form->{transdate};
572
573   my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1 customer_id notes);
574
575   &check_name("customer");
576
577   $form->{AR} = $saved_variables{AR};
578   if ($saved_variables{AR_amount_1} =~ m/.--./) {
579     map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1);
580   } else {
581     delete $form->{taxchart_1};
582   }
583
584   $form->{invtotal} =
585     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
586
587   for my $i (1 .. $form->{paidaccounts}) {
588     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
589       map {
590         $form->{"${_}_$i"} =
591           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
592       } qw(paid exchangerate);
593
594       $form->{totalpaid} += $form->{"paid_$i"};
595
596       $form->{"forex_$i"}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
597       $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
598     }
599   }
600
601   $form->{creditremaining} -=
602     ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
603      $form->{oldinvtotal});
604   $form->{oldinvtotal}  = $form->{invtotal};
605   $form->{oldtotalpaid} = $form->{totalpaid};
606
607   &display_form;
608
609   $main::lxdebug->leave_sub();
610 }
611
612 #
613 # ToDO: fix $closedto and $invdate
614 #
615 sub post_payment {
616   $main::lxdebug->enter_sub();
617
618   $main::auth->assert('ar_transactions');
619
620   my $form     = $main::form;
621   my %myconfig = %main::myconfig;
622   my $locale   = $main::locale;
623
624   $form->mtime_ischanged('ar');
625   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
626
627   my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
628
629   for my $i (1 .. $form->{paidaccounts}) {
630
631     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
632       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
633
634       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
635
636       $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
637         if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
638
639       #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
640       # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
641       $form->error($locale->text('Cannot post payment for a closed period!'))
642         if ($form->date_closed($form->{"datepaid_$i"})  && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
643
644       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
645 #        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
646         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
647       }
648     }
649   }
650
651   ($form->{AR})      = split /--/, $form->{AR};
652   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
653   if (AR->post_payment(\%myconfig, \%$form)) {
654     $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
655     $form->{what_done} = 'invoice';
656     $form->{addition}  = "PAYMENT POSTED";
657     $form->save_history;
658     $form->redirect($locale->text('Payment posted!'))
659   } else {
660     $form->error($locale->text('Cannot post payment!'));
661   };
662
663   $main::lxdebug->leave_sub();
664 }
665
666 sub _post {
667
668   $main::auth->assert('ar_transactions');
669
670   my $form     = $main::form;
671
672   # inline post
673   post(1);
674 }
675
676 sub post {
677   $main::lxdebug->enter_sub();
678
679   $main::auth->assert('ar_transactions');
680
681   my $form     = $main::form;
682   my %myconfig = %main::myconfig;
683   my $locale   = $main::locale;
684
685   my ($inline) = @_;
686
687   $form->mtime_ischanged('ar');
688
689   my ($datepaid);
690
691   # check if there is an invoice number, invoice and due date
692   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
693   $form->isblank("duedate",   $locale->text('Due Date missing!'));
694   $form->isblank("customer",  $locale->text('Customer missing!'));
695
696   if ($myconfig{mandatory_departments} && !$form->{department_id}) {
697     $form->{saved_message} = $::locale->text('You have to specify a department.');
698     update();
699     exit;
700   }
701
702   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
703   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
704
705   $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
706     if ($form->date_max_future($transdate, \%myconfig));
707
708   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
709
710   $form->error($locale->text('Zero amount posting!'))
711     unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
712
713   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
714     if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
715
716   delete($form->{AR});
717
718   for my $i (1 .. $form->{paidaccounts}) {
719     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
720       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
721
722       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
723
724       $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
725         if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
726
727       #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
728       # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
729       $form->error($locale->text('Cannot post payment for a closed period!'))
730         if ($form->date_closed($form->{"datepaid_$i"})  && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
731
732       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
733         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
734         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
735       }
736     }
737   }
738
739   # if oldcustomer ne customer redo form
740   my ($customer) = split /--/, $form->{customer};
741   if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
742     update();
743     $::dispatcher->end_request;
744   }
745
746   $form->{AR}{receivables} = $form->{ARselected};
747   $form->{storno}          = 0;
748
749   $form->{id} = 0 if $form->{postasnew};
750   $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
751
752   # saving the history
753   if(!exists $form->{addition} && $form->{id} ne "") {
754     $form->{snumbers}  = "invnumber_$form->{invnumber}";
755     $form->{what_done} = "invoice";
756     $form->{addition}  = "POSTED";
757     $form->save_history;
758   }
759   # /saving the history
760
761   $form->redirect($locale->text('Transaction posted!')) unless $inline;
762
763   $main::lxdebug->leave_sub();
764 }
765
766 sub post_as_new {
767   $main::lxdebug->enter_sub();
768
769   $main::auth->assert('ar_transactions');
770
771   my $form     = $main::form;
772   my %myconfig = %main::myconfig;
773
774   $form->{postasnew} = 1;
775   # saving the history
776   if(!exists $form->{addition} && $form->{id} ne "") {
777     $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
778     $form->{what_done} = "invoice";
779     $form->{addition}  = "POSTED AS NEW";
780     $form->save_history;
781   }
782   # /saving the history
783   &post;
784
785   $main::lxdebug->leave_sub();
786 }
787
788 sub use_as_new {
789   $main::lxdebug->enter_sub();
790
791   $main::auth->assert('ar_transactions');
792
793   my $form     = $main::form;
794   my %myconfig = %main::myconfig;
795
796   map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
797   $form->{paidaccounts} = 1;
798   $form->{rowcount}--;
799   $form->{invdate} = $form->current_date(\%myconfig);
800   &update;
801
802   $main::lxdebug->leave_sub();
803 }
804
805 sub delete {
806   $main::lxdebug->enter_sub();
807
808   $main::auth->assert('ar_transactions');
809
810   my $form     = $main::form;
811   my $locale   = $main::locale;
812
813   $form->{title} = $locale->text('Confirm!');
814
815   $form->header;
816
817   delete $form->{header};
818
819   print qq|
820 <form method=post action=$form->{script}>
821 |;
822
823   foreach my $key (keys %$form) {
824     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
825     $form->{$key} =~ s/\"/&quot;/g;
826     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
827   }
828
829   print qq|
830 <h2 class=confirm>$form->{title}</h2>
831
832 <h4>|
833     . $locale->text('Are you sure you want to delete Transaction')
834     . qq| $form->{invnumber}</h4>
835
836 <input name=action class=submit type=submit value="|
837     . $locale->text('Yes') . qq|">
838 </form>
839 |;
840
841   $main::lxdebug->leave_sub();
842 }
843
844 sub yes {
845   $main::lxdebug->enter_sub();
846
847   $main::auth->assert('ar_transactions');
848
849   my $form     = $main::form;
850   my %myconfig = %main::myconfig;
851   my $locale   = $main::locale;
852
853   if (AR->delete_transaction(\%myconfig, \%$form)) {
854     # saving the history
855     if(!exists $form->{addition}) {
856       $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
857       $form->{what_done} = "invoice";
858       $form->{addition}  = "DELETED";
859       $form->save_history;
860     }
861     # /saving the history
862     $form->redirect($locale->text('Transaction deleted!'));
863   }
864   $form->error($locale->text('Cannot delete transaction!'));
865
866   $main::lxdebug->leave_sub();
867 }
868
869 sub search {
870   $main::lxdebug->enter_sub();
871
872   $main::auth->assert('invoice_edit');
873
874   my $form     = $main::form;
875   my %myconfig = %main::myconfig;
876   my $locale   = $main::locale;
877   my $cgi      = $::request->{cgi};
878
879   # setup customer selection
880   $form->all_vc(\%myconfig, "customer", "AR");
881
882   $form->{title}    = $locale->text('AR Transactions');
883
884   # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
885   $form->get_lists("projects"       => { "key" => "ALL_PROJECTS", "all" => 1 },
886                    "customers"      => "ALL_VC",
887                    "business_types" => "ALL_BUSINESS_TYPES");
888   $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
889   $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
890   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
891
892   $form->{CT_CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
893   ($form->{CT_CUSTOM_VARIABLES_FILTER_CODE},
894    $form->{CT_CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CT_CUSTOM_VARIABLES},
895                                                                               'include_prefix' => 'l_',
896                                                                               'include_value'  => 'Y');
897
898   # constants and subs for template
899   $form->{vc_keys}   = sub { "$_[0]->{name}--$_[0]->{id}" };
900
901   $form->header;
902   print $form->parse_html_template('ar/search', { %myconfig });
903
904   $main::lxdebug->leave_sub();
905 }
906
907 sub create_subtotal_row {
908   $main::lxdebug->enter_sub();
909
910   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
911
912   my $form     = $main::form;
913   my %myconfig = %main::myconfig;
914
915   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
916
917   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
918
919   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
920
921   map { $totals->{$_} = 0 } @{ $subtotal_columns };
922
923   $main::lxdebug->leave_sub();
924
925   return $row;
926 }
927
928 sub ar_transactions {
929   $main::lxdebug->enter_sub();
930
931   $main::auth->assert('invoice_edit');
932
933   my $form     = $main::form;
934   my %myconfig = %main::myconfig;
935   my $locale   = $main::locale;
936
937   my ($callback, $href, @columns);
938
939   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
940
941   report_generator_set_default_sort('transdate', 1);
942
943   AR->ar_transactions(\%myconfig, \%$form);
944
945   $form->{title} = $locale->text('AR Transactions');
946
947   my $report = SL::ReportGenerator->new(\%myconfig, $form);
948
949   @columns =
950     qw(ids transdate id type invnumber ordnumber cusordnumber name netamount tax amount paid
951        datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
952        marge_total marge_percent globalprojectnumber customernumber country ustid taxzone
953        payment_terms charts customertype direct_debit dunning_description department);
954
955   my $ct_cvar_configs                 = CVar->get_configs('module' => 'CT');
956   my @ct_includeable_custom_variables = grep { $_->{includeable} } @{ $ct_cvar_configs };
957   my @ct_searchable_custom_variables  = grep { $_->{searchable} }  @{ $ct_cvar_configs };
958
959   my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @ct_includeable_custom_variables;
960   push @columns, map { "cvar_$_->{name}" } @ct_includeable_custom_variables;
961
962   my @hidden_variables = map { "l_${_}" } @columns;
963   push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber cusordnumber transaction_description notes project_id transdatefrom transdateto duedatefrom duedateto
964                                            employee_id salesman_id business_id parts_partnumber parts_description department_id);
965   push @hidden_variables, map { "cvar_$_->{name}" } @ct_searchable_custom_variables;
966
967   $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
968
969   my %column_defs = (
970     'ids'                     => { raw_header_data => $::request->presenter->checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]"), align => 'center' },
971     'transdate'               => { 'text' => $locale->text('Date'), },
972     'id'                      => { 'text' => $locale->text('ID'), },
973     'type'                    => { 'text' => $locale->text('Type'), },
974     'invnumber'               => { 'text' => $locale->text('Invoice'), },
975     'ordnumber'               => { 'text' => $locale->text('Order'), },
976     'cusordnumber'            => { 'text' => $locale->text('Customer Order Number'), },
977     'name'                    => { 'text' => $locale->text('Customer'), },
978     'netamount'               => { 'text' => $locale->text('Amount'), },
979     'tax'                     => { 'text' => $locale->text('Tax'), },
980     'amount'                  => { 'text' => $locale->text('Total'), },
981     'paid'                    => { 'text' => $locale->text('Paid'), },
982     'datepaid'                => { 'text' => $locale->text('Date Paid'), },
983     'due'                     => { 'text' => $locale->text('Amount Due'), },
984     'duedate'                 => { 'text' => $locale->text('Due Date'), },
985     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
986     'notes'                   => { 'text' => $locale->text('Notes'), },
987     'salesman'                => { 'text' => $locale->text('Salesperson'), },
988     'employee'                => { 'text' => $locale->text('Employee'), },
989     'shippingpoint'           => { 'text' => $locale->text('Shipping Point'), },
990     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
991     'globalprojectnumber'     => { 'text' => $locale->text('Document Project Number'), },
992     'marge_total'             => { 'text' => $locale->text('Ertrag'), },
993     'marge_percent'           => { 'text' => $locale->text('Ertrag prozentual'), },
994     'customernumber'          => { 'text' => $locale->text('Customer Number'), },
995     'country'                 => { 'text' => $locale->text('Country'), },
996     'ustid'                   => { 'text' => $locale->text('USt-IdNr.'), },
997     'taxzone'                 => { 'text' => $locale->text('Steuersatz'), },
998     'payment_terms'           => { 'text' => $locale->text('Payment Terms'), },
999     'charts'                  => { 'text' => $locale->text('Buchungskonto'), },
1000     'customertype'            => { 'text' => $locale->text('Customer type'), },
1001     'direct_debit'            => { 'text' => $locale->text('direct debit'), },
1002     'department'              => { 'text' => $locale->text('Department'), },
1003     dunning_description       => { 'text' => $locale->text('Dunning level'), },
1004     %column_defs_cvars,
1005   );
1006
1007   foreach my $name (qw(id transdate duedate invnumber ordnumber cusordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
1008     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1009     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
1010   }
1011
1012   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1013
1014   $form->{"l_type"} = "Y";
1015   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1016
1017   $column_defs{ids}->{visible} = 'HTML';
1018
1019   $report->set_columns(%column_defs);
1020   $report->set_column_order(@columns);
1021
1022   $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
1023
1024   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1025
1026   CVar->add_custom_variables_to_report('module'         => 'CT',
1027                                        'trans_id_field' => 'customer_id',
1028                                        'configs'        => $ct_cvar_configs,
1029                                        'column_defs'    => \%column_defs,
1030                                        'data'           => $form->{AR});
1031
1032   my @options;
1033   if ($form->{customer}) {
1034     push @options, $locale->text('Customer') . " : $form->{customer}";
1035   }
1036   if ($form->{cp_name}) {
1037     push @options, $locale->text('Contact Person') . " : $form->{cp_name}";
1038   }
1039
1040   if ($form->{department_id}) {
1041     my $department = SL::DB::Manager::Department->find_by( id => $form->{department_id} );
1042     push @options, $locale->text('Department') . " : " . $department->description;
1043   }
1044   if ($form->{invnumber}) {
1045     push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
1046   }
1047   if ($form->{ordnumber}) {
1048     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1049   }
1050   if ($form->{cusordnumber}) {
1051     push @options, $locale->text('Customer Order Number') . " : $form->{cusordnumber}";
1052   }
1053   if ($form->{notes}) {
1054     push @options, $locale->text('Notes') . " : $form->{notes}";
1055   }
1056   if ($form->{transaction_description}) {
1057     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1058   }
1059   if ($form->{parts_partnumber}) {
1060     push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}";
1061   }
1062   if ($form->{parts_description}) {
1063     push @options, $locale->text('Part Description') . " : $form->{parts_description}";
1064   }
1065   if ($form->{transdatefrom}) {
1066     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1067   }
1068   if ($form->{transdateto}) {
1069     push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1070   }
1071   if ($form->{open}) {
1072     push @options, $locale->text('Open');
1073   }
1074   if ($form->{employee_id}) {
1075     my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
1076     push @options, $locale->text('Employee') . ' : ' . $employee->name;
1077   }
1078   if ($form->{salesman_id}) {
1079     my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
1080     push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1081   }
1082   if ($form->{closed}) {
1083     push @options, $locale->text('Closed');
1084   }
1085
1086   $form->{ALL_PRINTERS} = SL::DB::Manager::Printer->get_all_sorted;
1087
1088   $report->set_options('top_info_text'        => join("\n", @options),
1089                        'raw_top_info_text'    => $form->parse_html_template('ar/ar_transactions_header'),
1090                        'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1091                        'output_format'        => 'HTML',
1092                        'title'                => $form->{title},
1093                        'attachment_basename'  => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1094     );
1095   $report->set_options_from_form();
1096   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1097
1098   # add sort and escape callback, this one we use for the add sub
1099   $form->{callback} = $href .= "&sort=$form->{sort}";
1100
1101   # escape callback for href
1102   $callback = $form->escape($href);
1103
1104   my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1105
1106   my %totals    = map { $_ => 0 } @subtotal_columns;
1107   my %subtotals = map { $_ => 0 } @subtotal_columns;
1108
1109   my $idx = 0;
1110
1111   foreach my $ar (@{ $form->{AR} }) {
1112     $ar->{tax} = $ar->{amount} - $ar->{netamount};
1113     $ar->{due} = $ar->{amount} - $ar->{paid};
1114
1115     map { $subtotals{$_} += $ar->{$_};
1116           $totals{$_}    += $ar->{$_} } @subtotal_columns;
1117
1118     $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1119     $totals{marge_percent}    = $totals{netamount}    ? ($totals{marge_total}    * 100 / $totals{netamount}   ) : 0;
1120
1121     my $is_storno  = $ar->{storno} &&  $ar->{storno_id};
1122     my $has_storno = $ar->{storno} && !$ar->{storno_id};
1123
1124     $ar->{type} =
1125       $has_storno       ? $locale->text("Invoice with Storno (abbreviation)") :
1126       $is_storno        ? $locale->text("Storno (one letter abbreviation)") :
1127       $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1128       $ar->{invoice}    ? $locale->text("Invoice (one letter abbreviation)") :
1129                           $locale->text("AR Transaction (abbreviation)");
1130
1131     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1132
1133     $ar->{direct_debit} = $ar->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1134
1135     my $row = { };
1136
1137     foreach my $column (@columns) {
1138       $row->{$column} = {
1139         'data'  => $ar->{$column},
1140         'align' => $column_alignment{$column},
1141       };
1142     }
1143
1144     $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1145       . "&id=" . E($ar->{id}) . "&callback=${callback}";
1146
1147     $row->{ids} = {
1148       raw_data =>  $::request->presenter->checkbox_tag("id[]", value => $ar->{id}, "data-checkall" => 1),
1149       valign   => 'center',
1150       align    => 'center',
1151     };
1152
1153     my $row_set = [ $row ];
1154
1155     if (($form->{l_subtotal} eq 'Y')
1156         && (($idx == (scalar @{ $form->{AR} } - 1))
1157             || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1158       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1159     }
1160
1161     $report->add_data($row_set);
1162
1163     $idx++;
1164   }
1165
1166   $report->add_separator();
1167   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1168
1169   $report->generate_with_headers();
1170
1171   $main::lxdebug->leave_sub();
1172 }
1173
1174 sub storno {
1175   $main::lxdebug->enter_sub();
1176
1177   $main::auth->assert('ar_transactions');
1178
1179   my $form     = $main::form;
1180   my %myconfig = %main::myconfig;
1181   my $locale   = $main::locale;
1182
1183   # don't cancel cancelled transactions
1184   if (IS->has_storno(\%myconfig, $form, 'ar')) {
1185     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1186     $form->error($locale->text("Transaction has already been cancelled!"));
1187   }
1188
1189   AR->storno($form, \%myconfig, $form->{id});
1190
1191   # saving the history
1192   if(!exists $form->{addition} && $form->{id} ne "") {
1193     $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
1194     $form->{addition}  = "STORNO";
1195     $form->{what_done} = "invoice";
1196     $form->save_history;
1197   }
1198   # /saving the history
1199
1200   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1201
1202   $main::lxdebug->leave_sub();
1203 }
1204
1205 1;