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