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