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