Falschen Spaltennamen korrigiert.
[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
36 use SL::AR;
37 use SL::IS;
38 use SL::PE;
39 use SL::ReportGenerator;
40
41 require "bin/mozilla/arap.pl";
42 require "bin/mozilla/common.pl";
43 require "bin/mozilla/drafts.pl";
44 require "bin/mozilla/report_generator.pl";
45
46 1;
47
48 # end of main
49
50 # this is for our long dates
51 # $locale->text('January')
52 # $locale->text('February')
53 # $locale->text('March')
54 # $locale->text('April')
55 # $locale->text('May ')
56 # $locale->text('June')
57 # $locale->text('July')
58 # $locale->text('August')
59 # $locale->text('September')
60 # $locale->text('October')
61 # $locale->text('November')
62 # $locale->text('December')
63
64 # this is for our short month
65 # $locale->text('Jan')
66 # $locale->text('Feb')
67 # $locale->text('Mar')
68 # $locale->text('Apr')
69 # $locale->text('May')
70 # $locale->text('Jun')
71 # $locale->text('Jul')
72 # $locale->text('Aug')
73 # $locale->text('Sep')
74 # $locale->text('Oct')
75 # $locale->text('Nov')
76 # $locale->text('Dec')
77
78 sub add {
79   $lxdebug->enter_sub();
80
81   return $lxdebug->leave_sub() if (load_draft_maybe());
82
83   # saving the history
84   if(!exists $form->{addition} && ($form->{id} ne "")) {
85     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
86         $form->{addition} = "ADDED";
87         $form->save_history($form->dbconnect(\%myconfig));
88   }
89   # /saving the history 
90   
91   $form->{title}    = "Add";
92   $form->{callback} =
93     "$form->{script}?action=add&login=$form->{login}&password=$form->{password}"
94     unless $form->{callback};
95
96   AR->get_transdate(\%myconfig, $form);
97   $form->{initial_transdate} = $form->{transdate};
98   &create_links;
99   $form->{transdate} = $form->{initial_transdate};
100   &display_form;
101   $lxdebug->leave_sub();
102 }
103
104 sub edit {
105   $lxdebug->enter_sub();
106   # show history button
107   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
108   #/show hhistory button
109   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
110   $form->{title} = "Edit";
111
112   &create_links;
113   &display_form;
114
115   $lxdebug->leave_sub();
116 }
117
118 sub display_form {
119   $lxdebug->enter_sub();
120
121   &form_header;
122   &form_footer;
123
124   $lxdebug->leave_sub();
125 }
126
127 sub create_links {
128   $lxdebug->enter_sub();
129
130   $form->create_links("AR", \%myconfig, "customer");
131   $duedate = $form->{duedate};
132
133   $taxincluded = $form->{taxincluded};
134   my $id = $form->{id};
135   IS->get_customer(\%myconfig, \%$form);
136   $form->{taxincluded} = $taxincluded;
137   $form->{id} = $id;
138
139   $form->{duedate}     = $duedate if $duedate;
140   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
141   $form->{rowcount}    = 1;
142
143   # notes
144   $form->{notes} = $form->{intnotes} unless $form->{notes};
145
146   # currencies
147   @curr = split(/:/, $form->{currencies});
148   chomp $curr[0];
149   $form->{defaultcurrency} = $curr[0];
150
151   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
152
153   # customers
154   if (@{ $form->{all_customer} }) {
155     $form->{customer} = "$form->{customer}--$form->{customer_id}";
156     map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
157       (@{ $form->{all_customer} });
158   }
159
160   # departments
161   if (@{ $form->{all_departments} }) {
162     $form->{selectdepartment} = "<option>\n";
163     $form->{department}       = "$form->{department}--$form->{department_id}";
164
165     map {
166       $form->{selectdepartment} .=
167         "<option>$_->{description}--$_->{id}\n"
168     } (@{ $form->{all_departments} });
169   }
170
171   $form->{employee} = "$form->{employee}--$form->{employee_id}";
172
173   # sales staff
174   if (@{ $form->{all_employees} }) {
175     $form->{selectemployee} = "";
176     map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
177       (@{ $form->{all_employees} });
178   }
179
180   # build the popup menus
181   $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
182
183   AR->setup_form($form);
184
185   $form->{locked} =
186     ($form->datetonum($form->{transdate}, \%myconfig) <=
187      $form->datetonum($form->{closedto}, \%myconfig));
188
189   $lxdebug->leave_sub();
190 }
191
192 sub form_header {
193   $lxdebug->enter_sub();
194
195   $title = $form->{title};
196   $form->{title} = $locale->text("$title Accounts Receivables Transaction");
197
198   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
199
200   # $locale->text('Add Accounts Receivables Transaction')
201   # $locale->text('Edit Accounts Receivables Transaction')
202   $form->{javascript} = qq|<script type="text/javascript">
203   <!--
204   function setTaxkey(accno, row) {
205     var taxkey = accno.options[accno.selectedIndex].value;
206     var reg = /--([0-9]*)/;
207     var found = reg.exec(taxkey);
208     var index = found[1];
209     index = parseInt(index);
210     var tax = 'taxchart_' + row;
211     for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
212       var reg2 = new RegExp("^"+ index, "");
213       if (reg2.exec(document.getElementById(tax).options[i].value)) {
214         document.getElementById(tax).options[i].selected = true;
215         break;
216       }
217     }
218   };
219   //-->
220   </script>|;
221   # show history button js
222   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
223   #/show history button js
224   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
225   $readonly = ($form->{id}) ? "readonly" : "";
226
227   $form->{radier} =
228     ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
229   $readonly = ($form->{radier}) ? "" : $readonly;
230
231   # set option selected
232   foreach $item (qw(customer currency department employee)) {
233     $form->{"select$item"} =~ s/ selected//;
234     $form->{"select$item"} =~
235       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
236   }
237
238   $form->{exchangerate} = $exchangerate
239     if (
240         $form->{forex} = (
241                      $exchangerate =
242                        $form->check_exchangerate(
243                        \%myconfig, $form->{currency}, $form->{transdate}, 'buy'
244                        )));
245
246   # format amounts
247   $form->{exchangerate} =
248     $form->format_amount(\%myconfig, $form->{exchangerate});
249
250   if ($form->{exchangerate} == 0) {
251     $form->{exchangerate} = "";
252   }
253
254   $form->{creditlimit} =
255     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
256   $form->{creditremaining} =
257     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
258
259   $exchangerate = qq|
260 <input type=hidden name=forex value=$form->{forex}>
261 |;
262   if ($form->{currency} ne $form->{defaultcurrency}) {
263     if ($form->{forex}) {
264       $exchangerate .= qq|
265         <th align=right>| . $locale->text('Exchangerate') . qq|</th>
266         <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
267 |;
268     } else {
269       $exchangerate .= qq|
270         <th align=right>| . $locale->text('Exchangerate') . qq|</th>
271         <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
272 |;
273     }
274   }
275
276   $taxincluded = qq|
277               <tr>
278                 <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
279                 <th align=left nowrap>| . $locale->text('Tax Included') . qq|</th>
280               </tr>
281 |;
282
283   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
284     $rows = 2;
285   }
286   $notes =
287     qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
288
289   $department = qq|
290               <tr>
291                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
292                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
293                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
294                 </td>
295               </tr>
296 | if $form->{selectdepartment};
297
298   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
299
300   $customer =
301     ($form->{selectcustomer})
302     ? qq|<select name="customer"
303 onchange="document.getElementById('update_button').click();">$form->{
304 selectcustomer}</select>|
305     : qq|<input name=customer value="$form->{customer}" size=35>|;
306
307   $employee = qq|
308                 <input type=hidden name=employee value="$form->{employee}">
309 |;
310
311   if ($form->{selectemployee}) {
312     $employee = qq|
313               <tr>
314                 <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
315                 <td  colspan=2><select name=employee>$form->{selectemployee}</select></td>
316                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
317               </tr>
318 |;
319   }
320
321   my @old_project_ids = ();
322   map({ push(@old_project_ids, $form->{"project_id_$_"})
323           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
324
325   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
326                                    "all" => 0,
327                                    "old_id" => \@old_project_ids },
328                    "charts" => { "key" => "ALL_CHARTS",
329                                  "transdate" => $form->{transdate} },
330                    "taxcharts" => "ALL_TAXCHARTS");
331
332   map({ $_->{link_split} = [ split(/:/, $_->{link}) ]; }
333       @{ $form->{ALL_CHARTS} });
334
335   my %project_labels = ();
336   my @project_values = ("");
337   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
338     push(@project_values, $item->{"id"});
339     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
340   }
341
342   my (%AR_amount_labels, @AR_amount_values);
343   my (%AR_labels, @AR_values);
344   my (%AR_paid_labels, @AR_paid_values);
345   my %charts;
346   my $taxchart_init;
347
348   foreach my $item (@{ $form->{ALL_CHARTS} }) {
349     if (grep({ $_ eq "AR_amount" } @{ $item->{link_split} })) {
350       $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
351       my $key = "$item->{accno}--$item->{tax_id}";
352       push(@AR_amount_values, $key);
353       $AR_amount_labels{$key} =
354         "$item->{accno}--$item->{description}";
355
356     } elsif (grep({ $_ eq "AR" } @{ $item->{link_split} })) {
357       push(@AR_values, $item->{accno});
358       $AR_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
359
360     } elsif (grep({ $_ eq "AR_paid" } @{ $item->{link_split} })) {
361       push(@AR_paid_values, $item->{accno});
362       $AR_paid_labels{$item->{accno}} =
363         "$item->{accno}--$item->{description}";
364     }
365
366     $charts{$item->{accno}} = $item;
367   }
368
369   my %taxchart_labels = ();
370   my @taxchart_values = ();
371   my %taxcharts = ();
372   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
373     my $key = "$item->{id}--$item->{rate}";
374     $taxchart_init = $key if ($taxchart_init eq $item->{id});
375     push(@taxchart_values, $key);
376     $taxchart_labels{$key} =
377       "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
378     $taxcharts{$item->{id}} = $item;
379   }
380
381   $form->{fokus} = "arledger.customer";
382
383   # use JavaScript Calendar or not
384   $form->{jsscript} = 1;
385   $jsscript = "";
386   if ($form->{jsscript}) {
387
388     # with JavaScript Calendar
389     $button1 = qq|
390        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>
391        <td><input type=button name=transdate id="trigger1" value=|
392       . $locale->text('button') . qq|></td>
393        |;
394     $button2 = qq|
395        <td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\"></td>
396        <td><input type=button name=duedate id="trigger2" value=|
397       . $locale->text('button') . qq|></td></td>
398      |;
399
400     #write Trigger
401     $jsscript =
402       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
403                           "duedate", "BL", "trigger2");
404   } else {
405
406     # without JavaScript Calendar
407     $button1 =
408       qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>|;
409     $button2 =
410       qq|<td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\"></td>|;
411   }
412
413   $form->{javascript} .=
414     qq|<script type="text/javascript" src="js/common.js"></script>| .
415     qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
416
417   $form->header;
418   $onload = qq|focus()|;
419   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
420   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
421   print qq|
422 <body onLoad="$onload">
423
424 <form method=post name="arledger" action=$form->{script}>
425
426 <input type=hidden name=id value=$form->{id}>
427 <input type=hidden name=sort value=$form->{sort}>
428 <input type=hidden name=closedto value=$form->{closedto}>
429 <input type=hidden name=locked value=$form->{locked}>
430 <input type=hidden name=title value="$title">
431
432 | . ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") . qq|
433
434 <table width=100%>
435   <tr class=listtop>
436     <th class=listtop>$form->{title}</th>
437   </tr>
438   <tr height="5"></tr>
439   <tr valign=top>
440     <td>
441       <table width=100%>
442         <tr valign=top>
443           <td>
444             <table>
445               <tr>
446                 <th align="right" nowrap>| . $locale->text('Customer') . qq|</th>
447                 <td colspan=3>$customer <input type="button" value="?" onclick="show_vc_details('customer')"></td>
448                 <input type=hidden name=selectcustomer value="$form->{selectcustomer}">
449                 <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
450                 <input type=hidden name=customer_id value="$form->{customer_id}">
451                 <input type=hidden name=terms value=$form->{terms}>
452               </tr>
453               <tr>
454                 <td></td>
455                 <td colspan=3>
456                   <table width=100%>
457                     <tr>
458                       <th align=left nowrap>| . $locale->text('Credit Limit') . qq|</th>
459                       <td>$form->{creditlimit}</td>
460                       <th align=left nowrap>| . $locale->text('Remaining') . qq|</th>
461                       <td class="plus$n">$form->{creditremaining}</td>
462                       <input type=hidden name=creditlimit value=$form->{creditlimit}>
463                       <input type=hidden name=creditremaining value=$form->{creditremaining}>
464                     </tr>
465                   </table>
466                 </td>
467               </tr>
468               <tr>
469                 <th align=right>| . $locale->text('Currency') . qq|</th>
470                 <td><select name=currency>$form->{selectcurrency}</select></td>
471                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
472                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
473                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
474                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
475                 $exchangerate
476               </tr>
477               $department
478               $taxincluded
479             </table>
480           </td>
481           <td align=right>
482             <table>
483               $employee
484               <tr>
485                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
486                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
487               </tr>
488               <tr>
489                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
490                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
491               </tr>
492               <tr>
493                 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
494                 $button1
495               </tr>
496               <tr>
497                 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
498                 $button2
499               </tr>
500      </table>
501           </td>
502         </tr>
503       </table>
504     </td>
505   </tr>
506
507 $jsscript
508   <input type=hidden name=rowcount value=$form->{rowcount}>
509   <tr>
510       <td>
511           <table width=100%>
512            <tr class=listheading>
513           <th class=listheading style="width:15%">|
514     . $locale->text('Account') . qq|</th>
515           <th class=listheading style="width:10%">|
516     . $locale->text('Amount') . qq|</th>
517           <th class=listheading style="width:10%">|
518     . $locale->text('Tax') . qq|</th>
519           <th class=listheading style="width:5%">|
520     . $locale->text('Korrektur') . qq|</th>
521           <th class=listheading style="width:10%">|
522     . $locale->text('Taxkey') . qq|</th>
523           <th class=listheading style="width:10%">|
524     . $locale->text('Project') . qq|</th>
525         </tr>
526 |;
527
528   $amount  = $locale->text('Amount');
529   $project = $locale->text('Project');
530
531   for $i (1 .. $form->{rowcount}) {
532
533     # format amounts
534     $form->{"amount_$i"} =
535       $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
536     $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
537
538     my $selected_accno_full;
539     my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
540     my $item = $charts{$accno_row};
541     $selected_accno_full = "$item->{accno}--$item->{tax_id}";
542
543     my $selected_taxchart = $form->{"taxchart_$i"};
544     my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
545     my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
546
547     if ($previous_accno &&
548         ($previous_accno eq $selected_accno) &&
549         ($previous_tax_id ne $selected_tax_id)) {
550       my $item = $taxcharts{$selected_tax_id};
551       $selected_taxchart = "$item->{id}--$item->{rate}";
552     }
553
554     $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
555
556     $selectAR_amount =
557       NTI($cgi->popup_menu('-name' => "AR_amount_$i",
558                            '-id' => "AR_amount_$i",
559                            '-style' => 'width:400px',
560                            '-onChange' => "setTaxkey(this, $i)",
561                            '-values' => \@AR_amount_values,
562                            '-labels' => \%AR_amount_labels,
563                            '-default' => $selected_accno_full))
564       . $cgi->hidden('-name' => "previous_AR_amount_$i",
565                      '-default' => $selected_accno_full);
566
567     $tax = qq|<td>| .
568       NTI($cgi->popup_menu('-name' => "taxchart_$i",
569                            '-id' => "taxchart_$i",
570                            '-style' => 'width:200px',
571                            '-values' => \@taxchart_values,
572                            '-labels' => \%taxchart_labels,
573                            '-default' => $selected_taxchart))
574       . qq|</td>|;
575
576     $korrektur_checked = ($form->{"korrektur_$i"} ? 'checked' : '');
577
578     my $projectnumber =
579       NTI($cgi->popup_menu('-name' => "project_id_$i",
580                            '-values' => \@project_values,
581                            '-labels' => \%project_labels,
582                            '-default' => $form->{"project_id_$i"} ));
583
584     print qq|
585         <tr>
586           <td>$selectAR_amount</td>
587           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"}></td>
588           <td><input name="tax_$i" size=10 value=$form->{"tax_$i"}></td>
589           <td><input type="checkbox" name="korrektur_$i" value="1" $korrektur_checked></td>
590           $tax
591           <td>$projectnumber</td>
592         </tr>
593 |;
594     $amount  = "";
595     $project = "";
596   }
597
598   $form->{invtotal_unformatted} = $form->{invtotal};
599   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
600
601   $ARselected =
602     NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
603                          '-style' => 'width:400px',
604                          '-values' => \@AR_values, '-labels' => \%AR_labels,
605                          '-default' => $form->{ARselected}));
606
607   print qq|
608         <tr>
609           <td colspan=6>
610             <hr noshade>
611           </td>
612         </tr>
613         <tr>
614           <td>${ARselected}</td>
615           <th align=left>$form->{invtotal}</th>
616
617           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
618           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
619
620           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
621
622           <td colspan=4></td>
623
624
625         </tr>
626         </table>
627         </td>
628     </tr>
629     <tr>
630       <td>
631         <table width=100%>
632         <tr>
633           <th align=left width=1%>| . $locale->text('Notes') . qq|</th>
634           <td align=left>$notes</td>
635         </tr>
636       </table>
637     </td>
638   </tr>
639   <tr>
640     <td>
641       <table width=100%>
642         <tr class=listheading>
643           <th colspan=7 class=listheading>|
644     . $locale->text('Incoming Payments') . qq|</th>
645         </tr>
646 |;
647
648   if ($form->{currency} eq $form->{defaultcurrency}) {
649     @column_index = qw(datepaid source memo paid AR_paid paid_project_id);
650   } else {
651     @column_index = qw(datepaid source memo paid exchangerate AR_paid paid_project_id);
652   }
653
654   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
655   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
656   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
657   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
658   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
659   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>"; 
660   $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>"; 
661
662   print "
663         <tr>
664 ";
665   map { print "$column_data{$_}\n" } @column_index;
666   print "
667         </tr>
668 ";
669
670   my @triggers  = ();
671   my $totalpaid = 0;
672
673   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
674   for $i (1 .. $form->{paidaccounts}) {
675     print "
676         <tr>
677 ";
678
679     $selectAR_paid =
680       NTI($cgi->popup_menu('-name' => "AR_paid_$i",
681                            '-id' => "AR_paid_$i",
682                            '-values' => \@AR_paid_values,
683                            '-labels' => \%AR_paid_labels,
684                            '-default' => $form->{"AR_paid_$i"}));
685
686     $totalpaid += $form->{"paid_$i"};
687
688     # format amounts
689     if ($form->{"paid_$i"}) {
690       $form->{"paid_$i"} =
691         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
692     }
693     $form->{"exchangerate_$i"} =
694       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
695
696     if ($form->{"exchangerate_$i"} == 0) {
697       $form->{"exchangerate_$i"} = "";
698     }
699
700     $exchangerate = qq|&nbsp;|;
701     if ($form->{currency} ne $form->{defaultcurrency}) {
702       if ($form->{"forex_$i"}) {
703         $exchangerate =
704           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
705       } else {
706         $exchangerate =
707           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
708       }
709     }
710
711     $exchangerate .= qq|
712 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
713 |;
714
715     $column_data{paid} =
716       qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
717     $column_data{AR_paid} =
718       qq|<td align=center>${selectAR_paid}</td>|;
719     $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
720     $column_data{datepaid}     =
721       qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
722          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
723     $column_data{source} =
724       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
725     $column_data{memo} =
726       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
727
728     $column_data{paid_project_id} =
729       qq|<td>|
730       . NTI($cgi->popup_menu('-name' => "paid_project_id_$i",
731                              '-values' => \@project_values,
732                              '-labels' => \%project_labels,
733                              '-default' => $form->{"paid_project_id_$i"} ))
734       . qq|</td>|;
735
736     map { print qq|$column_data{$_}\n| } @column_index;
737
738     print "
739         </tr>
740 ";
741     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
742   }
743
744   my $paid_missing = $form->{invtotal_unformatted} - $totalpaid;
745
746   print qq|
747         <tr>
748           <td></td>
749           <td></td>
750           <td align="center">| . $locale->text('Total') . qq|</td>
751           <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
752         </tr>
753         <tr>
754           <td></td>
755           <td></td>
756           <td align="center">| . $locale->text('Missing amount') . qq|</td>
757           <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
758         </tr>
759 | . $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
760     qq|
761 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
762
763       </table>
764     </td>
765   </tr>
766   <tr>
767     <td><hr size=3 noshade></td>
768   </tr>
769 </table>
770 |;
771
772   $lxdebug->leave_sub();
773 }
774
775 sub form_footer {
776   $lxdebug->enter_sub();
777
778   print qq|
779
780 <input name=gldate type=hidden value="| . Q($form->{gldate}) . qq|">
781
782 <input name=callback type=hidden value="$form->{callback}">
783
784 <input type=hidden name=login value=$form->{login}>
785 <input type=hidden name=password value=$form->{password}>
786 |
787 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
788 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
789 . qq|
790
791 <br>
792 |;
793
794   if (!$form->{id} && $form->{draft_id}) {
795     print(NTI($cgi->checkbox('-name' => 'remove_draft', '-id' => 'remove_draft',
796                              '-value' => 1, '-checked' => $form->{remove_draft},
797                              '-label' => '')) .
798           qq|&nbsp;<label for="remove_draft">| .
799           $locale->text("Remove draft when posting") .
800           qq|</label><br>|);
801   }
802
803   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
804   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
805
806   print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">\n|;
807
808   # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
809   print qq|<input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|">|
810     if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ar') && !IS->is_storno(\%myconfig, $form, 'ar') && !$form->{paid_1});
811
812   if ($form->{id}) {
813     if ($form->{radier}) {
814       print qq|
815         <input class=submit type=submit name=action value="| . $locale->text('Post') .            qq|">
816         <input class=submit type=submit name=action value="| . $locale->text('Delete') .          qq|"> |;
817     }
818     if ($transdate > $closedto) {
819       print qq|
820         <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|"> |;
821     }
822     print qq|
823         <input class=submit type=submit name=action value="| . $locale->text('Post Payment') .    qq|"> |;
824
825   } else {
826     if ($transdate > $closedto) {
827       print qq|<input class=submit type=submit name=action value="| . $locale->text('Post') .     qq|"> | .
828         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
829     }
830   }
831
832   if ($form->{menubar}) {
833     require "bin/mozilla/menu.pl";
834     &menubar;
835   }
836   # button for saving history
837   if($form->{id} ne "") {
838     print qq|<input type=button class=submit onclick=set_history_window($form->{id}); name=history id=history value=| . $locale->text('history') . qq|>|;
839   }
840   # /button for saving history
841   # mark_as_paid button 
842   if($form->{id} ne "") {  
843     print qq|<input type="submit" class="submit" name="action" value="| 
844           . $locale->text('mark as paid') . qq|">|;
845   }
846   # /mark_as_paid button
847
848   print "
849 </form>
850
851 </body>
852 </html>
853 ";
854
855   $lxdebug->leave_sub();
856 }
857
858 sub mark_as_paid {
859   $lxdebug->enter_sub();
860   &mark_as_paid_common(\%myconfig,"ar");  
861   $lxdebug->leave_sub();
862 }
863
864 sub update {
865   $lxdebug->enter_sub();
866
867   my $display = shift;
868
869   $form->{invtotal} = 0;
870
871   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
872     qw(exchangerate creditlimit creditremaining);
873
874   @flds  = qw(amount AR_amount projectnumber oldprojectnumber project_id);
875   $count = 0;
876   @a     = ();
877
878   for $i (1 .. $form->{rowcount}) {
879     $form->{"amount_$i"} =
880       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
881     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
882     if ($form->{"amount_$i"}) {
883       push @a, {};
884       $j = $#a;
885       if (!$form->{"korrektur_$i"}) {
886         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
887         if ($taxkey > 1) {
888           if ($form->{taxincluded}) {
889             $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
890           } else {
891             $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
892           }
893         } else {
894           $form->{"tax_$i"} = 0;
895         }
896       }
897       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
898
899       $totaltax += $form->{"tax_$i"};
900       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
901       $count++;
902     }
903   }
904
905   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
906   $form->{rowcount} = $count + 1;
907   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
908
909   $form->{exchangerate} = $exchangerate
910     if (
911         $form->{forex} = (
912                      $exchangerate =
913                        $form->check_exchangerate(
914                        \%myconfig, $form->{currency}, $form->{transdate}, 'buy'
915                        )));
916
917   $form->{invdate} = $form->{transdate};
918   $save_AR = $form->{AR};
919   &check_name(customer);
920   $form->{AR} = $save_AR;
921
922   $form->{invtotal} =
923     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
924
925   for $i (1 .. $form->{paidaccounts}) {
926     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
927       map {
928         $form->{"${_}_$i"} =
929           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
930       } qw(paid exchangerate);
931
932       $totalpaid += $form->{"paid_$i"};
933
934       $form->{"exchangerate_$i"} = $exchangerate
935         if (
936             $form->{"forex_$i"} = (
937                  $exchangerate =
938                    $form->check_exchangerate(
939                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
940                    )));
941     }
942   }
943
944   $form->{creditremaining} -=
945     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
946      $form->{oldinvtotal});
947   $form->{oldinvtotal}  = $form->{invtotal};
948   $form->{oldtotalpaid} = $totalpaid;
949
950   &display_form;
951
952   $lxdebug->leave_sub();
953 }
954
955 sub post_payment {
956   $lxdebug->enter_sub();
957
958   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
959
960   for $i (1 .. $form->{paidaccounts}) {
961     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
962       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
963
964       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
965
966       $form->error($locale->text('Cannot post payment for a closed period!'))
967         if ($datepaid <= $closedto);
968
969       if ($form->{currency} ne $form->{defaultcurrency}) {
970         $form->{"exchangerate_$i"} = $form->{exchangerate}
971           if ($invdate == $datepaid);
972         $form->isblank("exchangerate_$i",
973                        $locale->text('Exchangerate for payment missing!'));
974       }
975     }
976   }
977
978   ($form->{AR})      = split /--/, $form->{AR};
979   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
980   $form->redirect($locale->text('Payment posted!'))
981       if (AR->post_payment(\%myconfig, \%$form));
982     $form->error($locale->text('Cannot post payment!'));
983
984
985   $lxdebug->leave_sub();
986 }
987
988 sub post {
989   $lxdebug->enter_sub();
990
991   # check if there is an invoice number, invoice and due date
992   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
993   $form->isblank("duedate",   $locale->text('Due Date missing!'));
994   $form->isblank("customer",  $locale->text('Customer missing!'));
995
996   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
997   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
998   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
999
1000   $form->error($locale->text('Zero amount posting!')) 
1001     unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
1002
1003   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1004     if ($form->{currency} ne $form->{defaultcurrency});
1005
1006   delete($form->{AR});
1007
1008   for $i (1 .. $form->{paidaccounts}) {
1009     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1010       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1011
1012       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1013
1014       $form->error($locale->text('Cannot post payment for a closed period!'))
1015         if ($datepaid <= $closedto);
1016
1017       if ($form->{currency} ne $form->{defaultcurrency}) {
1018         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
1019         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
1020       }
1021     }
1022   }
1023
1024   # if oldcustomer ne customer redo form
1025   ($customer) = split /--/, $form->{customer};
1026   if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
1027     &update;
1028     exit;
1029   }
1030
1031   $form->{AR}{receivables} = $form->{ARselected};
1032   $form->{storno}          = 0;
1033
1034   $form->{id} = 0 if $form->{postasnew};
1035
1036
1037   if (AR->post_transaction(\%myconfig, \%$form)) {
1038     # saving the history
1039     if(!exists $form->{addition} && $form->{id} ne "") {
1040       $form->{snumbers} = "invnumber_$form->{invnumber}";
1041       $form->{addition} = "POSTED";
1042       $form->save_history($form->dbconnect(\%myconfig));
1043     }
1044     # /saving the history 
1045     remove_draft() if $form->{remove_draft};
1046     $form->redirect($locale->text('Transaction posted!'));
1047   }
1048   $form->error($locale->text('Cannot post transaction!'));
1049
1050   $lxdebug->leave_sub();
1051 }
1052
1053 sub post_as_new {
1054   $lxdebug->enter_sub();
1055
1056   $form->{postasnew} = 1;
1057   # saving the history
1058   if(!exists $form->{addition} && $form->{id} ne "") {
1059     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1060         $form->{addition} = "POSTED AS NEW";
1061         $form->save_history($form->dbconnect(\%myconfig));
1062   }
1063   # /saving the history 
1064   &post;
1065
1066   $lxdebug->leave_sub();
1067 }
1068
1069 sub use_as_template {
1070   $lxdebug->enter_sub();
1071
1072   map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
1073   $form->{paidaccounts} = 1;
1074   $form->{rowcount}--;
1075   $form->{invdate} = $form->current_date(\%myconfig);
1076   &update;
1077
1078   $lxdebug->leave_sub();
1079 }
1080
1081 sub delete {
1082   $lxdebug->enter_sub();
1083
1084   $form->{title} = $locale->text('Confirm!');
1085
1086   $form->header;
1087
1088   delete $form->{header};
1089
1090   print qq|
1091 <body>
1092
1093 <form method=post action=$form->{script}>
1094 |;
1095
1096   foreach $key (keys %$form) {
1097     $form->{$key} =~ s/\"/&quot;/g;
1098     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1099   }
1100
1101   print qq|
1102 <h2 class=confirm>$form->{title}</h2>
1103
1104 <h4>|
1105     . $locale->text('Are you sure you want to delete Transaction')
1106     . qq| $form->{invnumber}</h4>
1107
1108 <input name=action class=submit type=submit value="|
1109     . $locale->text('Yes') . qq|">
1110 </form>
1111
1112 </body>
1113 </html>
1114 |;
1115
1116   $lxdebug->leave_sub();
1117 }
1118
1119 sub yes {
1120   $lxdebug->enter_sub();
1121   if (AR->delete_transaction(\%myconfig, \%$form, $spool)) {
1122     # saving the history
1123     if(!exists $form->{addition}) {
1124       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1125           $form->{addition} = "DELETED";
1126           $form->save_history($form->dbconnect(\%myconfig));
1127     }
1128     # /saving the history 
1129     $form->redirect($locale->text('Transaction deleted!'));
1130   }
1131   $form->error($locale->text('Cannot delete transaction!'));
1132
1133   $lxdebug->leave_sub();
1134 }
1135
1136 sub search {
1137   $lxdebug->enter_sub();
1138
1139   # setup customer selection
1140   $form->all_vc(\%myconfig, "customer", "AR");
1141
1142   if (@{ $form->{all_customer} }) {
1143     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
1144       @{ $form->{all_customer} };
1145     $customer = qq|<select name=customer><option>\n$customer</select>|;
1146   } else {
1147     $customer = qq|<input name=customer size=35>|;
1148   }
1149
1150   # departments
1151   if (@{ $form->{all_departments} }) {
1152     $form->{selectdepartment} = "<option>\n";
1153
1154     map {
1155       $form->{selectdepartment} .=
1156         "<option>$_->{description}--$_->{id}\n"
1157     } (@{ $form->{all_departments} });
1158   }
1159
1160   $department = qq|
1161         <tr>
1162           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1163           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1164         </tr>
1165 | if $form->{selectdepartment};
1166
1167   $form->{title} = $locale->text('AR Transactions');
1168   
1169   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
1170   
1171   # use JavaScript Calendar or not
1172   $form->{jsscript} = 1;
1173   $jsscript = "";
1174   if ($form->{jsscript}) {
1175
1176     # with JavaScript Calendar
1177     $button1 = qq|
1178        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1179        <input type=button name=transdatefrom id="trigger1" value=|
1180       . $locale->text('button') . qq|></td>
1181       |;
1182     $button2 = qq|
1183        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1184        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1185       . $locale->text('button') . qq|></td>
1186      |;
1187
1188     #write Trigger
1189     $jsscript =
1190       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1191                           "transdateto", "BL", "trigger2");
1192   } else {
1193
1194     # without JavaScript Calendar
1195     $button1 = qq|
1196                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1197     $button2 = qq|
1198                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1199   }
1200
1201   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1202                                    "all" => 1 });
1203
1204   my %labels = ();
1205   my @values = ("");
1206   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1207     push(@values, $item->{"id"});
1208     $labels{$item->{"id"}} = $item->{"projectnumber"};
1209   }
1210   my $projectnumber =
1211     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1212                          '-labels' => \%labels));
1213
1214   $form->{fokus} = "search.customer";
1215   $form->header;
1216   $onload = qq|focus()|;
1217   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
1218   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
1219   print qq|
1220 <body onLoad="$onload">
1221
1222 <form method=post name="search" action=$form->{script}>
1223
1224 <table width=100%>
1225   <tr><th class=listtop>$form->{title}</th></tr>
1226   <tr height="5"></tr>
1227   <tr>
1228     <td>
1229       <table>
1230         <tr>
1231           <th align=right>| . $locale->text('Customer') . qq|</th>
1232           <td colspan=3>$customer</td>
1233         </tr>
1234         $department
1235         <tr>
1236           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1237           <td colspan=3><input name=invnumber size=20></td>
1238         </tr>
1239         <tr>
1240           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1241           <td colspan=3><input name=ordnumber size=20></td>
1242         </tr>
1243        <tr>
1244          <th align=right nowrap>| . $locale->text('Transaction description') . qq|</th>
1245          <td colspan=3><input name=transaction_description size=40></td>
1246        </tr>
1247         <tr>
1248           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1249           <td colspan=3><input name=notes size=40></td>
1250         </tr>
1251         <tr>
1252           <th align="right">| . $locale->text("Project Number") . qq|</th>
1253           <td colspan="3">$projectnumber</td>
1254         </tr>
1255         <tr>
1256           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1257           $button1
1258           <th align=right>| . $locale->text('Bis') . qq|</th>
1259           $button2
1260         </tr>
1261         <input type=hidden name=sort value=transdate>
1262       </table>
1263     </td>
1264   </tr>
1265   <tr>
1266     <td>
1267       <table>
1268         <tr>
1269           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1270           <td>
1271             <table width=100%>
1272               <tr>
1273                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1274                 <td nowrap>| . $locale->text('Open') . qq|</td>
1275                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1276                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1277               </tr>
1278               <tr>
1279                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1280                 <td nowrap>| . $locale->text('ID') . qq|</td>
1281                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1282                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1283                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1284                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1285                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1286                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1287               </tr>
1288               <tr>
1289                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1290                 <td nowrap>| . $locale->text('Customer') . qq|</td>
1291                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1292                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1293                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1294                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1295                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1296                 <td nowrap>| . $locale->text('Total') . qq|</td>
1297               </tr>
1298               <tr>
1299                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1300                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1301                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1302                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1303                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1304                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1305                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1306                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1307               </tr>
1308               <tr>
1309                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1310                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1311                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1312                 <td nowrap>| . $locale->text('Salesperson') . qq|</td>
1313                 <td align=right><input name="l_shippingpoint" class=checkbox type=checkbox value=Y></td>
1314                 <td nowrap>| . $locale->text('Shipping Point') . qq|</td>
1315                 <td align=right><input name="l_shipvia" class=checkbox type=checkbox value=Y></td>
1316                 <td nowrap>| . $locale->text('Ship via') . qq|</td>
1317               </tr>
1318               <tr>
1319                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1320                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1321                 <td align=right><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y></td>
1322                 <td nowrap>| . $locale->text('Project Number') . qq|</td>
1323                 <td align=right><input name="l_transaction_description" class=checkbox type=checkbox value=Y></td>
1324                 <td nowrap>| . $locale->text('Transaction description') . qq|</td>
1325               </tr>
1326             </table>
1327           </td>
1328         </tr>
1329       </table>
1330     </td>
1331   </tr>
1332   <tr>
1333     <td><hr size=3 noshade></td>
1334   </tr>
1335 </table>
1336
1337 <input type=hidden name=nextsub value=$form->{nextsub}>
1338
1339 <input type=hidden name=login value=$form->{login}>
1340 <input type=hidden name=password value=$form->{password}>
1341
1342 <br>
1343 <input class=submit type=submit name=action value="|
1344     . $locale->text('Continue') . qq|">
1345
1346 </form>
1347
1348 </body>
1349
1350 $jsscript
1351
1352 </html>
1353 |;
1354
1355   $lxdebug->leave_sub();
1356 }
1357
1358 sub create_subtotal_row {
1359   $lxdebug->enter_sub();
1360
1361   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
1362
1363   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
1364
1365   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
1366
1367   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
1368
1369   map { $totals->{$_} = 0 } @{ $subtotal_columns };
1370
1371   $lxdebug->leave_sub();
1372
1373   return $row;
1374 }
1375
1376 sub ar_transactions {
1377   $lxdebug->enter_sub();
1378
1379   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
1380
1381   $form->{sort} ||= 'transdate';
1382
1383   AR->ar_transactions(\%myconfig, \%$form);
1384
1385   $form->{title} = $locale->text('AR Transactions');
1386
1387   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1388
1389   my @columns =
1390     qw(transdate id type invnumber ordnumber name netamount tax amount paid
1391        datepaid due duedate transaction_description notes employee shippingpoint shipvia
1392        globalprojectnumber);
1393
1394   my @hidden_variables = map { "l_${_}" } @columns;
1395   push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto);
1396
1397   my $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
1398
1399   my %column_defs = (
1400     'transdate'               => { 'text' => $locale->text('Date'), },
1401     'id'                      => { 'text' => $locale->text('ID'), },
1402     'type'                    => { 'text' => $locale->text('Type'), },
1403     'invnumber'               => { 'text' => $locale->text('Invoice'), },
1404     'ordnumber'               => { 'text' => $locale->text('Order'), },
1405     'name'                    => { 'text' => $locale->text('Customer'), },
1406     'netamount'               => { 'text' => $locale->text('Amount'), },
1407     'tax'                     => { 'text' => $locale->text('Tax'), },
1408     'amount'                  => { 'text' => $locale->text('Total'), },
1409     'paid'                    => { 'text' => $locale->text('Paid'), },
1410     'datepaid'                => { 'text' => $locale->text('Date Paid'), },
1411     'due'                     => { 'text' => $locale->text('Amount Due'), },
1412     'duedate'                 => { 'text' => $locale->text('Due Date'), },
1413     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1414     'notes'                   => { 'text' => $locale->text('Notes'), },
1415     'employee'                => { 'text' => $locale->text('Salesperson'), },
1416     'shippingpoint'           => { 'text' => $locale->text('Shipping Point'), },
1417     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
1418     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
1419   );
1420
1421   foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid
1422                        employee shippingpoint shipvia)) {
1423     $column_defs{$name}->{link} = $href . "&sort=$name";
1424   }
1425
1426   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1427
1428   $form->{"l_type"} = "Y";
1429   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1430
1431   $report->set_columns(%column_defs);
1432   $report->set_column_order(@columns);
1433
1434   $report->set_export_options('ar_transactions', @hidden_variables);
1435
1436   my @options;
1437   if ($form->{customer}) {
1438     push @options, $locale->text('Customer') . " : $form->{customer}";
1439   }
1440   if ($form->{department}) {
1441     ($department) = split /--/, $form->{department};
1442     push @options, $locale->text('Department') . " : $department";
1443   }
1444   if ($form->{invnumber}) {
1445     push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
1446   }
1447   if ($form->{ordnumber}) {
1448     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1449   }
1450   if ($form->{notes}) {
1451     push @options, $locale->text('Notes') . " : $form->{notes}";
1452   }
1453   if ($form->{transaction_description}) {
1454     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1455   }
1456   if ($form->{transdatefrom}) {
1457     push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1458   }
1459   if ($form->{transdateto}) {
1460     push @options, $locale->text('Bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdateto}, 1);
1461   }
1462   if ($form->{open}) {
1463     push @options, $locale->text('Open');
1464   }
1465   if ($form->{closed}) {
1466     push @options, $locale->text('Closed');
1467   }
1468
1469   $report->set_options('top_info_text'        => join("\n", @options),
1470                        'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1471                        'output_format'        => 'HTML',
1472                        'title'                => $form->{title},
1473                        'attachment_basename'  => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1474     );
1475   $report->set_options_from_form();
1476
1477   # add sort and escape callback, this one we use for the add sub
1478   $form->{callback} = $href .= "&sort=$form->{sort}";
1479
1480   # escape callback for href
1481   $callback = $form->escape($href);
1482
1483   my @subtotal_columns = qw(netamount amount paid due);
1484
1485   my %totals    = map { $_ => 0 } @subtotal_columns;
1486   my %subtotals = map { $_ => 0 } @subtotal_columns;
1487
1488   my $idx = 0;
1489
1490   foreach $ar (@{ $form->{AR} }) {
1491     $ar->{tax} = $ar->{amount} - $ar->{netamount};
1492     $ar->{due} = $ar->{amount} - $ar->{paid};
1493
1494     map { $subtotals{$_} += $ar->{$_};
1495           $totals{$_}    += $ar->{$_} } @subtotal_columns;
1496
1497     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due);
1498
1499     my $is_storno  = $ar->{storno} && IS->is_storno(\%myconfig, $form, 'ar');
1500     my $has_storno = $ar->{storno} && !$is_storno;
1501
1502     $ar->{type} =
1503       $has_storno       ? $locale->text("Invoice with Storno (abbreviation)") :
1504       $is_storno        ? $locale->text("Storno (one letter abbreviation)") :
1505       $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1506       $ar->{invoice}    ? $locale->text("Invoice (one letter abbreviation)") :
1507                           $locale->text("AR Transaction (abbreviation)");
1508
1509     my $row = { };
1510
1511     foreach my $column (@columns) {
1512       $row->{$column} = {
1513         'data'  => $ar->{$column},
1514         'align' => $column_alignment{$column},
1515       };
1516     }
1517
1518     $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1519       . "&id=" . E($ar->{id}) . "&callback=${callback}";
1520
1521     my $row_set = [ $row ];
1522
1523     if (($form->{l_subtotal} eq 'Y')
1524         && (($idx == (scalar @{ $form->{AR} } - 1))
1525             || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1526       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1527     }
1528
1529     $report->add_data($row_set);
1530
1531     $idx++;
1532   }
1533
1534   $report->add_separator();
1535   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1536
1537   $report->generate_with_headers();
1538
1539   $lxdebug->leave_sub();
1540 }
1541
1542 sub storno {
1543   $lxdebug->enter_sub();
1544
1545   if (IS->has_storno(\%myconfig, $form, 'ar')) {
1546     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1547     $form->error($locale->text("Transaction has already been cancelled!"));
1548   }
1549
1550   # negate amount/taxes
1551   for my $i (1 .. $form->{rowcount}) {
1552     $form->{"amount_$i"} *= -1;
1553     $form->{"tax_$i"}    *= -1; 
1554   }
1555
1556   # format things
1557   for my $i (1 .. $form->{rowcount}) {
1558     for (qw(amount tax)) {
1559       $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) if $form->{"${_}_$i"};
1560     }
1561   }
1562
1563   $form->{storno}      = 1;
1564   $form->{storno_id}   = $form->{id};
1565   $form->{id}          = 0;
1566
1567   $form->{invnumber}   = "Storno-" . $form->{invnumber};
1568
1569   post();
1570
1571   # saving the history
1572   if(!exists $form->{addition} && $form->{id} ne "") {
1573     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1574     $form->{addition} = "STORNO";
1575     $form->save_history($form->dbconnect(\%myconfig));
1576   }
1577   # /saving the history 
1578
1579   $lxdebug->leave_sub();
1580 }