fbdbbc084f3b0456ce8d7f1d8c102aba1b613fed
[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   for $i (1 .. $form->{paidaccounts}) {
955     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
956       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
957
958       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
959
960       $form->error($locale->text('Cannot post payment for a closed period!'))
961         if ($datepaid <= $closedto);
962
963       if ($form->{currency} ne $form->{defaultcurrency}) {
964         $form->{"exchangerate_$i"} = $form->{exchangerate}
965           if ($invdate == $datepaid);
966         $form->isblank("exchangerate_$i",
967                        $locale->text('Exchangerate for payment missing!'));
968       }
969     }
970   }
971
972   ($form->{AR})      = split /--/, $form->{AR};
973   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
974   $form->redirect($locale->text('Payment posted!'))
975       if (AR->post_payment(\%myconfig, \%$form));
976     $form->error($locale->text('Cannot post payment!'));
977
978
979   $lxdebug->leave_sub();
980 }
981
982 sub post {
983   $lxdebug->enter_sub();
984
985   # check if there is an invoice number, invoice and due date
986   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
987   $form->isblank("duedate",   $locale->text('Due Date missing!'));
988   $form->isblank("customer",  $locale->text('Customer missing!'));
989
990   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
991   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
992   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
993
994   $form->error($locale->text('Zero amount posting!')) 
995     unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
996
997   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
998     if ($form->{currency} ne $form->{defaultcurrency});
999
1000   delete($form->{AR});
1001
1002   for $i (1 .. $form->{paidaccounts}) {
1003     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1004       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1005
1006       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1007
1008       $form->error($locale->text('Cannot post payment for a closed period!'))
1009         if ($datepaid <= $closedto);
1010
1011       if ($form->{currency} ne $form->{defaultcurrency}) {
1012         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
1013         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
1014       }
1015     }
1016   }
1017
1018   # if oldcustomer ne customer redo form
1019   ($customer) = split /--/, $form->{customer};
1020   if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
1021     &update;
1022     exit;
1023   }
1024
1025   $form->{AR}{receivables} = $form->{ARselected};
1026   $form->{storno}          = 0;
1027
1028   $form->{id} = 0 if $form->{postasnew};
1029
1030
1031   if (AR->post_transaction(\%myconfig, \%$form)) {
1032     # saving the history
1033     if(!exists $form->{addition} && $form->{id} ne "") {
1034       $form->{snumbers} = "invnumber_$form->{invnumber}";
1035       $form->{addition} = "POSTED";
1036       $form->save_history($form->dbconnect(\%myconfig));
1037     }
1038     # /saving the history 
1039     remove_draft() if $form->{remove_draft};
1040     $form->redirect($locale->text('Transaction posted!'));
1041   }
1042   $form->error($locale->text('Cannot post transaction!'));
1043
1044   $lxdebug->leave_sub();
1045 }
1046
1047 sub post_as_new {
1048   $lxdebug->enter_sub();
1049
1050   $form->{postasnew} = 1;
1051   # saving the history
1052   if(!exists $form->{addition} && $form->{id} ne "") {
1053     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1054         $form->{addition} = "POSTED AS NEW";
1055         $form->save_history($form->dbconnect(\%myconfig));
1056   }
1057   # /saving the history 
1058   &post;
1059
1060   $lxdebug->leave_sub();
1061 }
1062
1063 sub use_as_template {
1064   $lxdebug->enter_sub();
1065
1066   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);
1067   $form->{paidaccounts} = 1;
1068   $form->{rowcount}--;
1069   $form->{invdate} = $form->current_date(\%myconfig);
1070   &update;
1071
1072   $lxdebug->leave_sub();
1073 }
1074
1075 sub delete {
1076   $lxdebug->enter_sub();
1077
1078   $form->{title} = $locale->text('Confirm!');
1079
1080   $form->header;
1081
1082   delete $form->{header};
1083
1084   print qq|
1085 <body>
1086
1087 <form method=post action=$form->{script}>
1088 |;
1089
1090   foreach $key (keys %$form) {
1091     $form->{$key} =~ s/\"/&quot;/g;
1092     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1093   }
1094
1095   print qq|
1096 <h2 class=confirm>$form->{title}</h2>
1097
1098 <h4>|
1099     . $locale->text('Are you sure you want to delete Transaction')
1100     . qq| $form->{invnumber}</h4>
1101
1102 <input name=action class=submit type=submit value="|
1103     . $locale->text('Yes') . qq|">
1104 </form>
1105
1106 </body>
1107 </html>
1108 |;
1109
1110   $lxdebug->leave_sub();
1111 }
1112
1113 sub yes {
1114   $lxdebug->enter_sub();
1115   if (AR->delete_transaction(\%myconfig, \%$form, $spool)) {
1116     # saving the history
1117     if(!exists $form->{addition}) {
1118       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1119           $form->{addition} = "DELETED";
1120           $form->save_history($form->dbconnect(\%myconfig));
1121     }
1122     # /saving the history 
1123     $form->redirect($locale->text('Transaction deleted!'));
1124   }
1125   $form->error($locale->text('Cannot delete transaction!'));
1126
1127   $lxdebug->leave_sub();
1128 }
1129
1130 sub search {
1131   $lxdebug->enter_sub();
1132
1133   # setup customer selection
1134   $form->all_vc(\%myconfig, "customer", "AR");
1135
1136   if (@{ $form->{all_customer} }) {
1137     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
1138       @{ $form->{all_customer} };
1139     $customer = qq|<select name=customer><option>\n$customer</select>|;
1140   } else {
1141     $customer = qq|<input name=customer size=35>|;
1142   }
1143
1144   # departments
1145   if (@{ $form->{all_departments} }) {
1146     $form->{selectdepartment} = "<option>\n";
1147
1148     map {
1149       $form->{selectdepartment} .=
1150         "<option>$_->{description}--$_->{id}\n"
1151     } (@{ $form->{all_departments} });
1152   }
1153
1154   $department = qq|
1155         <tr>
1156           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1157           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1158         </tr>
1159 | if $form->{selectdepartment};
1160
1161   $form->{title} = $locale->text('AR Transactions');
1162   
1163   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
1164   
1165   # use JavaScript Calendar or not
1166   $form->{jsscript} = 1;
1167   $jsscript = "";
1168   if ($form->{jsscript}) {
1169
1170     # with JavaScript Calendar
1171     $button1 = qq|
1172        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1173        <input type=button name=transdatefrom id="trigger1" value=|
1174       . $locale->text('button') . qq|></td>
1175       |;
1176     $button2 = qq|
1177        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1178        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1179       . $locale->text('button') . qq|></td>
1180      |;
1181
1182     #write Trigger
1183     $jsscript =
1184       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1185                           "transdateto", "BL", "trigger2");
1186   } else {
1187
1188     # without JavaScript Calendar
1189     $button1 = qq|
1190                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1191     $button2 = qq|
1192                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1193   }
1194
1195   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1196                                    "all" => 1 });
1197
1198   my %labels = ();
1199   my @values = ("");
1200   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1201     push(@values, $item->{"id"});
1202     $labels{$item->{"id"}} = $item->{"projectnumber"};
1203   }
1204   my $projectnumber =
1205     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1206                          '-labels' => \%labels));
1207
1208   $form->{fokus} = "search.customer";
1209   $form->header;
1210   $onload = qq|focus()|;
1211   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
1212   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
1213   print qq|
1214 <body onLoad="$onload">
1215
1216 <form method=post name="search" action=$form->{script}>
1217
1218 <table width=100%>
1219   <tr><th class=listtop>$form->{title}</th></tr>
1220   <tr height="5"></tr>
1221   <tr>
1222     <td>
1223       <table>
1224         <tr>
1225           <th align=right>| . $locale->text('Customer') . qq|</th>
1226           <td colspan=3>$customer</td>
1227         </tr>
1228         $department
1229         <tr>
1230           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1231           <td colspan=3><input name=invnumber size=20></td>
1232         </tr>
1233         <tr>
1234           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1235           <td colspan=3><input name=ordnumber size=20></td>
1236         </tr>
1237        <tr>
1238          <th align=right nowrap>| . $locale->text('Transaction description') . qq|</th>
1239          <td colspan=3><input name=transaction_description size=40></td>
1240        </tr>
1241         <tr>
1242           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1243           <td colspan=3><input name=notes size=40></td>
1244         </tr>
1245         <tr>
1246           <th align="right">| . $locale->text("Project Number") . qq|</th>
1247           <td colspan="3">$projectnumber</td>
1248         </tr>
1249         <tr>
1250           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1251           $button1
1252           <th align=right>| . $locale->text('Bis') . qq|</th>
1253           $button2
1254         </tr>
1255         <input type=hidden name=sort value=transdate>
1256       </table>
1257     </td>
1258   </tr>
1259   <tr>
1260     <td>
1261       <table>
1262         <tr>
1263           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1264           <td>
1265             <table width=100%>
1266               <tr>
1267                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1268                 <td nowrap>| . $locale->text('Open') . qq|</td>
1269                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1270                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1271               </tr>
1272               <tr>
1273                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1274                 <td nowrap>| . $locale->text('ID') . qq|</td>
1275                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1276                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1277                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1278                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1279                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1280                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1281               </tr>
1282               <tr>
1283                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1284                 <td nowrap>| . $locale->text('Customer') . qq|</td>
1285                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1286                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1287                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1288                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1289                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1290                 <td nowrap>| . $locale->text('Total') . qq|</td>
1291               </tr>
1292               <tr>
1293                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1294                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1295                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1296                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1297                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1298                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1299                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1300                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1301               </tr>
1302               <tr>
1303                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1304                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1305                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1306                 <td nowrap>| . $locale->text('Salesperson') . qq|</td>
1307                 <td align=right><input name="l_shippingpoint" class=checkbox type=checkbox value=Y></td>
1308                 <td nowrap>| . $locale->text('Shipping Point') . qq|</td>
1309                 <td align=right><input name="l_shipvia" class=checkbox type=checkbox value=Y></td>
1310                 <td nowrap>| . $locale->text('Ship via') . qq|</td>
1311               </tr>
1312               <tr>
1313                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1314                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1315                 <td align=right><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y></td>
1316                 <td nowrap>| . $locale->text('Project Number') . qq|</td>
1317                 <td align=right><input name="l_transaction_description" class=checkbox type=checkbox value=Y></td>
1318                 <td nowrap>| . $locale->text('Transaction description') . qq|</td>
1319               </tr>
1320             </table>
1321           </td>
1322         </tr>
1323       </table>
1324     </td>
1325   </tr>
1326   <tr>
1327     <td><hr size=3 noshade></td>
1328   </tr>
1329 </table>
1330
1331 <input type=hidden name=nextsub value=$form->{nextsub}>
1332
1333 <input type=hidden name=login value=$form->{login}>
1334 <input type=hidden name=password value=$form->{password}>
1335
1336 <br>
1337 <input class=submit type=submit name=action value="|
1338     . $locale->text('Continue') . qq|">
1339
1340 </form>
1341
1342 </body>
1343
1344 $jsscript
1345
1346 </html>
1347 |;
1348
1349   $lxdebug->leave_sub();
1350 }
1351
1352 sub ar_transactions {
1353   $lxdebug->enter_sub();
1354
1355   $form->{customer} = $form->unescape($form->{customer});
1356   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
1357
1358   AR->ar_transactions(\%myconfig, \%$form);
1359
1360   $callback =
1361     "$form->{script}?action=ar_transactions&login=$form->{login}&password=$form->{password}";
1362   $href = $callback;
1363
1364   if ($form->{customer}) {
1365     $callback .= "&customer=" . $form->escape($form->{customer}, 1);
1366     $href .= "&customer=" . $form->escape($form->{customer});
1367     $option = $locale->text('Customer') . " : $form->{customer}";
1368   }
1369   if ($form->{department}) {
1370     $callback .= "&department=" . $form->escape($form->{department}, 1);
1371     $href .= "&department=" . $form->escape($form->{department});
1372     ($department) = split /--/, $form->{department};
1373     $option .= "\n<br>" if ($option);
1374     $option .= $locale->text('Department') . " : $department";
1375   }
1376   if ($form->{invnumber}) {
1377     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
1378     $href .= "&invnumber=" . $form->escape($form->{invnumber});
1379     $option .= "\n<br>" if ($option);
1380     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
1381   }
1382   if ($form->{ordnumber}) {
1383     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
1384     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
1385     $option .= "\n<br>" if ($option);
1386     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
1387   }
1388   if ($form->{notes}) {
1389     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
1390     $href .= "&notes=" . $form->escape($form->{notes});
1391     $option .= "\n<br>" if $option;
1392     $option .= $locale->text('Notes') . " : $form->{notes}";
1393   }
1394   if ($form->{transaction_description}) {
1395     $callback .= "&transaction_description=" . $form->escape($form->{transaction_description}, 1);
1396     $href .= "&transaction_description=" . $form->escape($form->{transaction_description});
1397     $option .= "\n<br>" if $option;
1398     $option .= $locale->text('Transaction description') . " : $form->{transaction_description}";
1399   }
1400
1401   if ($form->{transdatefrom}) {
1402     $callback .= "&transdatefrom=$form->{transdatefrom}";
1403     $href     .= "&transdatefrom=$form->{transdatefrom}";
1404     $option   .= "\n<br>" if ($option);
1405     $option   .=
1406         $locale->text('From') . "&nbsp;"
1407       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1408   }
1409   if ($form->{transdateto}) {
1410     $callback .= "&transdateto=$form->{transdateto}";
1411     $href     .= "&transdateto=$form->{transdateto}";
1412     $option   .= "\n<br>" if ($option);
1413     $option   .=
1414         $locale->text('Bis') . "&nbsp;"
1415       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1416   }
1417   if ($form->{open}) {
1418     $callback .= "&open=$form->{open}";
1419     $href     .= "&open=$form->{open}";
1420     $option   .= "\n<br>" if ($option);
1421     $option   .= $locale->text('Open');
1422   }
1423   if ($form->{closed}) {
1424     $callback .= "&closed=$form->{closed}";
1425     $href     .= "&closed=$form->{closed}";
1426     $option   .= "\n<br>" if ($option);
1427     $option   .= $locale->text('Closed');
1428   }
1429   if ($form->{globalproject_id}) {
1430     $callback .= "&globalproject_id=" . E($form->{globalproject_id});
1431     $href     .= "&globalproject_id=" . E($form->{globalproject_id});
1432   }
1433
1434   @columns =
1435     qw(transdate id type invnumber ordnumber name netamount tax amount paid
1436        datepaid due duedate transaction_description notes employee shippingpoint shipvia
1437        globalprojectnumber);
1438
1439   $form->{"l_type"} = "Y";
1440
1441   foreach $item (@columns) {
1442     if ($form->{"l_$item"} eq "Y") {
1443       push @column_index, $item;
1444
1445       # add column to href and callback
1446       $callback .= "&l_$item=Y";
1447       $href     .= "&l_$item=Y";
1448     }
1449   }
1450
1451   if ($form->{l_subtotal} eq 'Y') {
1452     $callback .= "&l_subtotal=Y";
1453     $href     .= "&l_subtotal=Y";
1454   }
1455
1456   $column_header{id} =
1457       "<th><a class=listheading href=$href&sort=id>"
1458     . $locale->text('ID')
1459     . "</a></th>";
1460   $column_header{transdate} =
1461       "<th><a class=listheading href=$href&sort=transdate>"
1462     . $locale->text('Date')
1463     . "</a></th>";
1464   $column_header{duedate} =
1465       "<th><a class=listheading href=$href&sort=duedate>"
1466     . $locale->text('Due Date')
1467     . "</a></th>";
1468   $column_header{type} =
1469       "<th class=\"listheading\">" . $locale->text('Type') . "</th>";
1470   $column_header{invnumber} =
1471       "<th><a class=listheading href=$href&sort=invnumber>"
1472     . $locale->text('Invoice')
1473     . "</a></th>";
1474   $column_header{ordnumber} =
1475       "<th><a class=listheading href=$href&sort=ordnumber>"
1476     . $locale->text('Order')
1477     . "</a></th>";
1478   $column_header{name} =
1479       "<th><a class=listheading href=$href&sort=name>"
1480     . $locale->text('Customer')
1481     . "</a></th>";
1482   $column_header{netamount} =
1483     "<th class=listheading>" . $locale->text('Amount') . "</th>";
1484   $column_header{tax} =
1485     "<th class=listheading>" . $locale->text('Tax') . "</th>";
1486   $column_header{amount} =
1487     "<th class=listheading>" . $locale->text('Total') . "</th>";
1488   $column_header{paid} =
1489     "<th class=listheading>" . $locale->text('Paid') . "</th>";
1490   $column_header{datepaid} =
1491       "<th><a class=listheading href=$href&sort=datepaid>"
1492     . $locale->text('Date Paid')
1493     . "</a></th>";
1494   $column_header{due} =
1495     "<th class=listheading>" . $locale->text('Amount Due') . "</th>";
1496   $column_header{notes} =
1497     "<th class=listheading>" . $locale->text('Notes') . "</th>";
1498   $column_header{employee} =
1499     "<th><a class=listheading href=$href&sort=employee>"
1500     . $locale->text('Salesperson') . "</th>";
1501
1502   $column_header{shippingpoint} =
1503       "<th><a class=listheading href=$href&sort=shippingpoint>"
1504     . $locale->text('Shipping Point')
1505     . "</a></th>";
1506   $column_header{shipvia} =
1507       "<th><a class=listheading href=$href&sort=shipvia>"
1508     . $locale->text('Ship via')
1509     . "</a></th>";
1510   $column_header{globalprojectnumber} =
1511     qq|<th class="listheading">| . $locale->text('Project Number') . qq|</th>|;
1512   $column_header{transaction_description} =
1513     "<th class=listheading>" . $locale->text('Transaction description') . "</th>";
1514
1515   $form->{title} = $locale->text('AR Transactions');
1516
1517   $form->header;
1518
1519   print qq|
1520 <body>
1521
1522 <table width=100%>
1523   <tr>
1524     <th class=listtop>$form->{title}</th>
1525   </tr>
1526   <tr height="5"></tr>
1527   <tr>
1528     <td>$option</td>
1529   </tr>
1530   <tr>
1531     <td>
1532       <table width=100%>
1533         <tr class=listheading>
1534 |;
1535
1536   map { print "\n$column_header{$_}" } @column_index;
1537
1538   print qq|
1539         </tr>
1540 |;
1541
1542   # add sort and escape callback, this one we use for the add sub
1543   $form->{callback} = $callback .= "&sort=$form->{sort}";
1544
1545   # escape callback for href
1546   $callback = $form->escape($callback);
1547
1548   if (@{ $form->{AR} }) {
1549     $sameitem = $form->{AR}->[0]->{ $form->{sort} };
1550   }
1551
1552   # sums and tax on reports by Antonio Gallardo
1553   #
1554   foreach $ar (@{ $form->{AR} }) {
1555
1556     if ($form->{l_subtotal} eq 'Y') {
1557       if ($sameitem ne $ar->{ $form->{sort} }) {
1558         &ar_subtotal;
1559       }
1560     }
1561
1562     $column_data{netamount} =
1563         "<td align=right>"
1564       . $form->format_amount(\%myconfig, $ar->{netamount}, 2, "&nbsp;")
1565       . "</td>";
1566     $column_data{tax} = "<td align=right>"
1567       . $form->format_amount(\%myconfig, $ar->{amount} - $ar->{netamount},
1568                              2, "&nbsp;")
1569       . "</td>";
1570     $column_data{amount} =
1571       "<td align=right>"
1572       . $form->format_amount(\%myconfig, $ar->{amount}, 2, "&nbsp;") . "</td>";
1573     $column_data{paid} =
1574       "<td align=right>"
1575       . $form->format_amount(\%myconfig, $ar->{paid}, 2, "&nbsp;") . "</td>";
1576     $column_data{due} = "<td align=right>"
1577       . $form->format_amount(\%myconfig, $ar->{amount} - $ar->{paid},
1578                              2, "&nbsp;")
1579       . "</td>";
1580
1581     $subtotalnetamount += $ar->{netamount};
1582     $subtotalamount    += $ar->{amount};
1583     $subtotalpaid      += $ar->{paid};
1584     $subtotaldue       += $ar->{amount} - $ar->{paid};
1585
1586     $totalnetamount += $ar->{netamount};
1587     $totalamount    += $ar->{amount};
1588     $totalpaid      += $ar->{paid};
1589     $totaldue       += ($ar->{amount} - $ar->{paid});
1590
1591     $column_data{transdate} = "<td>$ar->{transdate}&nbsp;</td>";
1592     $column_data{id}        = "<td>$ar->{id}</td>";
1593     $column_data{datepaid}  = "<td>$ar->{datepaid}&nbsp;</td>";
1594     $column_data{duedate}   = "<td>$ar->{duedate}&nbsp;</td>";
1595
1596     $module = ($ar->{invoice}) ? "is.pl" : $form->{script};
1597
1598     $column_data{invnumber} =
1599       "<td><a href=$module?action=edit&id=$ar->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ar->{invnumber}</a></td>";
1600
1601     my $is_storno  = $ar->{storno} && IS->is_storno(\%myconfig, $form, 'ar');
1602     my $has_storno = $ar->{storno} && !$is_storno;
1603
1604     $column_data{type} = "<td>" .
1605       ($has_storno       ? $locale->text("Invoice with Storno (abbreviation)") :
1606        $is_storno        ? $locale->text("Storno (one letter abbreviation)") :
1607        $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1608        $ar->{invoice}    ? $locale->text("Invoice (one letter abbreviation)") :
1609                            $locale->text("AR Transaction (abbreviation)"))
1610         . "</td>";
1611     $column_data{ordnumber} = "<td>$ar->{ordnumber}&nbsp;</td>";
1612     $column_data{name}      = "<td>$ar->{name}</td>";
1613     $ar->{notes} =~ s/\r\n/<br>/g;
1614     $column_data{notes}         = "<td>$ar->{notes}&nbsp;</td>";
1615     $column_data{shippingpoint} = "<td>$ar->{shippingpoint}&nbsp;</td>";
1616     $column_data{shipvia}       = "<td>$ar->{shipvia}&nbsp;</td>";
1617     $column_data{employee}      = "<td>$ar->{employee}&nbsp;</td>";
1618     $column_data{globalprojectnumber}  =
1619       "<td>" . H($ar->{globalprojectnumber}) . "</td>";
1620     $column_data{transaction_description}  =
1621       "<td>" . H($ar->{transaction_description}) . "</td>";
1622
1623     $i++;
1624     $i %= 2;
1625     print "
1626         <tr class=listrow$i>
1627 ";
1628
1629     map { print "\n$column_data{$_}" } @column_index;
1630
1631     print qq|
1632         </tr>
1633 |;
1634
1635   }
1636
1637   if ($form->{l_subtotal} eq 'Y') {
1638     &ar_subtotal;
1639   }
1640
1641   # print totals
1642   print qq|
1643         <tr class=listtotal>
1644 |;
1645
1646   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1647
1648   $column_data{netamount} =
1649     "<th class=listtotal align=right>"
1650     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1651   $column_data{tax} = "<th class=listtotal align=right>"
1652     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1653                            2, "&nbsp;")
1654     . "</th>";
1655   $column_data{amount} =
1656     "<th class=listtotal align=right>"
1657     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1658   $column_data{paid} =
1659     "<th class=listtotal align=right>"
1660     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;") . "</th>";
1661   $column_data{due} =
1662     "<th class=listtotal align=right>"
1663     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;") . "</th>";
1664
1665   map { print "\n$column_data{$_}" } @column_index;
1666
1667   print qq|
1668         </tr>
1669       </table>
1670     </td>
1671   </tr>
1672   <tr>
1673     <td><hr size=3 noshade></td>
1674   </tr>
1675 </table>
1676
1677 <br>
1678 <form method=post action=$form->{script}>
1679
1680 <input name=callback type=hidden value="$form->{callback}">
1681
1682 <input type=hidden name=login value=$form->{login}>
1683 <input type=hidden name=password value=$form->{password}>
1684
1685 <input class=submit type=submit name=action value="|
1686     . $locale->text('AR Transaction') . qq|">
1687 <input class=submit type=submit name=action value="|
1688     . $locale->text('Sales Invoice') . qq|">
1689
1690 </form>
1691
1692 </body>
1693 </html>
1694 |;
1695
1696   $lxdebug->leave_sub();
1697 }
1698
1699 sub ar_subtotal {
1700   $lxdebug->enter_sub();
1701
1702   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1703
1704   $column_data{tax} = "<th class=listsubtotal align=right>"
1705     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1706                            2, "&nbsp;")
1707     . "</th>";
1708   $column_data{amount} =
1709     "<th class=listsubtotal align=right>"
1710     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1711   $column_data{paid} =
1712     "<th class=listsubtotal align=right>"
1713     . $form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;") . "</th>";
1714   $column_data{due} =
1715     "<th class=listsubtotal align=right>"
1716     . $form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;") . "</th>";
1717
1718   $subtotalnetamount = 0;
1719   $subtotalamount    = 0;
1720   $subtotalpaid      = 0;
1721   $subtotaldue       = 0;
1722
1723   $sameitem = $ar->{ $form->{sort} };
1724
1725   print "<tr class=listsubtotal>";
1726
1727   map { print "\n$column_data{$_}" } @column_index;
1728
1729   print "
1730 </tr>
1731 ";
1732
1733   $lxdebug->leave_sub();
1734 }
1735
1736
1737 sub storno {
1738   $lxdebug->enter_sub();
1739
1740   if (IS->has_storno(\%myconfig, $form, 'ar')) {
1741     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1742     $form->error($locale->text("Transaction has already been cancelled!"));
1743   }
1744
1745   # negate amount/taxes
1746   for my $i (1 .. $form->{rowcount}) {
1747     $form->{"amount_$i"} *= -1;
1748     $form->{"tax_$i"}    *= -1; 
1749   }
1750
1751   # format things
1752   for my $i (1 .. $form->{rowcount}) {
1753     for (qw(amount tax)) {
1754       $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) if $form->{"${_}_$i"};
1755     }
1756   }
1757
1758   $form->{storno}      = 1;
1759   $form->{storno_id}   = $form->{id};
1760   $form->{id}          = 0;
1761
1762   $form->{invnumber}   = "Storno-" . $form->{invnumber};
1763
1764   post();
1765
1766   # saving the history
1767   if(!exists $form->{addition} && $form->{id} ne "") {
1768     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1769     $form->{addition} = "STORNO";
1770     $form->save_history($form->dbconnect(\%myconfig));
1771   }
1772   # /saving the history 
1773
1774   $lxdebug->leave_sub();
1775 }