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