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