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