0b2676f0d99eb1011c4a7e643a63f014cd168e4a
[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} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
676
677   $APselected =
678     NTI($cgi->popup_menu('-name' => "APselected", '-id' => "APselected",
679                          '-style' => 'width:400px',
680                          '-values' => \@AP_values, '-labels' => \%AP_labels,
681                          '-default' => $form->{APselected}));
682   print qq|
683         <tr>
684           <td colspan=6>
685             <hr noshade>
686           </td>
687         </tr>
688         <tr>
689           <td>${APselected}</td>
690           <th align=left>$form->{invtotal}</th>
691
692           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
693           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
694
695           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
696
697           <td colspan=4></td>
698
699
700         </tr>
701         </table>
702         </td>
703     </tr>
704     <tr>
705       <td>
706         <table width=100%>
707         <tr>
708           <th align=left width=1%>| . $locale->text('Notes') . qq|</th>
709           <td align=left>$notes</td>
710         </tr>
711       </table>
712     </td>
713   </tr>
714   <tr>
715     <td>
716       <table width=100%>
717         <tr class=listheading>
718           <th class=listheading colspan=7>| . $locale->text('Payments') . qq|</th>
719         </tr>
720 |;
721
722   if ($form->{currency} eq $form->{defaultcurrency}) {
723     @column_index = qw(datepaid source memo paid AP_paid paid_project_id);
724   } else {
725     @column_index = qw(datepaid source memo paid exchangerate AP_paid paid_project_id);
726   }
727
728   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
729   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
730   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
731   $column_data{AP_paid}      = "<th>" . $locale->text('Account') . "</th>";
732   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
733   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
734   $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>"; 
735
736   print "
737         <tr>
738 ";
739   map { print "$column_data{$_}\n" } @column_index;
740   print "
741         </tr>
742 ";
743
744   my @triggers = ();
745   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
746   for $i (1 .. $form->{paidaccounts}) {
747     print "
748         <tr>
749 ";
750
751     $selectAP_paid =
752       NTI($cgi->popup_menu('-name' => "AP_paid_$i",
753                            '-id' => "AP_paid_$i",
754                            '-values' => \@AP_paid_values,
755                            '-labels' => \%AP_paid_labels,
756                            '-default' => $form->{"AP_paid_$i"}));
757
758     # format amounts
759     if ($form->{"paid_$i"}) {
760       $form->{"paid_$i"} =
761       $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
762     }
763     $form->{"exchangerate_$i"} =
764       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
765
766     $exchangerate = qq|&nbsp;|;
767     if ($form->{currency} ne $form->{defaultcurrency}) {
768       if ($form->{"forex_$i"}) {
769         $exchangerate =
770           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
771       } else {
772         $exchangerate =
773           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
774       }
775     }
776
777     $exchangerate .= qq|
778 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
779 |;
780
781     $column_data{"paid_$i"} =
782       qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
783     $column_data{"AP_paid_$i"} =
784       qq|<td align=center>${selectAP_paid}</td>|;
785     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
786     $column_data{"datepaid_$i"}     =
787       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)\">
788          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
789     $column_data{"source_$i"} =
790       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
791     $column_data{"memo_$i"} =
792       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
793     $column_data{"paid_project_id_$i"} =
794       qq|<td>|
795       . NTI($cgi->popup_menu('-name' => "paid_project_id_$i",
796                              '-values' => \@project_values,
797                              '-labels' => \%project_labels,
798                              '-default' => $form->{"paid_project_id_$i"} ))
799       . qq|</td>|;
800
801     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
802
803     print "
804         </tr>
805 ";
806     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
807   }
808   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
809     qq|
810     <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
811
812       </table>
813     </td>
814   </tr>
815   <tr>
816     <td><hr size=3 noshade></td>
817   </tr>
818 </table>
819 |;
820
821   $lxdebug->leave_sub();
822 }
823
824 sub form_footer {
825   $lxdebug->enter_sub();
826
827   print qq|
828
829 <input name=callback type=hidden value="$form->{callback}">
830 <input name="gldate" type="hidden" value="| . Q($form->{gldate}) . qq|">
831
832 <input type=hidden name=login value=$form->{login}>
833 <input type=hidden name=password value=$form->{password}>
834 |
835 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
836 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
837 . qq|
838
839 <br>
840 |;
841
842   if (!$form->{id} && $form->{draft_id}) {
843     print(NTI($cgi->checkbox('-name' => 'remove_draft', '-id' => 'remove_draft',
844                              '-value' => 1, '-checked' => $form->{remove_draft},
845                              '-label' => '')) .
846           qq|&nbsp;<label for="remove_draft">| .
847           $locale->text("Remove draft when posting") .
848           qq|</label><br>|);
849   }
850
851   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
852   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
853
854   # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
855   print qq|<input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|">|
856     if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ap') && !IS->is_storno(\%myconfig, $form, 'ap') && !$form->{paid_1});
857
858   print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">|;
859
860   if ($form->{id}) {
861     if ($form->{radier}) {
862       print qq| <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|">
863                 <input class=submit type=submit name=action value="| . $locale->text('Delete') . qq|">
864 |;
865     }
866
867     print qq| <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|">
868               <input class=submit type=submit name=action value="| . $locale->text('Post Payment') . qq|">
869 |;
870   } elsif (($transdate > $closedto) && !$form->{id}) {
871     print qq|
872       <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|"> | .
873       NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
874   }
875   # button for saving history
876   if($form->{id} ne "") {
877     print qq| <input type="button" class="submit" onclick="set_history_window($form->{id});" name="history" id="history" value="| . $locale->text('history') . qq|">|;
878   }
879   # /button for saving history
880   print "
881 </form>
882
883 </body>
884 </html>
885 ";
886
887   $lxdebug->leave_sub();
888 }
889
890 sub update {
891   $lxdebug->enter_sub();
892
893   my $display = shift;
894
895   $form->{invtotal} = 0;
896
897   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
898     qw(exchangerate creditlimit creditremaining);
899
900   @flds  = qw(amount AP_amount projectnumber oldprojectnumber project_id);
901   $count = 0;
902   for $i (1 .. $form->{rowcount}) {
903     $form->{"amount_$i"} =
904       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
905     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
906     if ($form->{"amount_$i"}) {
907       push @a, {};
908       $j = $#a;
909       if (!$form->{"korrektur_$i"}) {
910         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
911         if ($taxkey > 1) {
912           if ($form->{taxincluded}) {
913             $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
914           } else {
915             $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
916           }
917         } else {
918           $form->{"tax_$i"} = 0;
919         }
920       }
921       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
922
923       $totaltax += $form->{"tax_$i"};
924       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
925       $count++;
926     }
927   }
928   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
929
930   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
931
932   $form->{exchangerate} = $exchangerate
933     if (
934         $form->{forex} = (
935                     $exchangerate =
936                       $form->check_exchangerate(
937                       \%myconfig, $form->{currency}, $form->{transdate}, 'sell'
938                       )));
939
940   $form->{invdate} = $form->{transdate};
941   $save_AP = $form->{AP};
942   &check_name(vendor);
943   $form->{AP} = $save_AP;
944
945   $form->{rowcount} = $count + 1;
946
947   $form->{invtotal} =
948     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
949
950   for $i (1 .. $form->{paidaccounts}) {
951     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
952       map {
953         $form->{"${_}_$i"} =
954           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
955       } qw(paid exchangerate);
956
957       $totalpaid += $form->{"paid_$i"};
958
959       $form->{"exchangerate_$i"} = $exchangerate
960         if (
961             $form->{"forex_$i"} = (
962                 $exchangerate =
963                   $form->check_exchangerate(
964                   \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'
965                   )));
966     }
967   }
968
969   $form->{creditremaining} -=
970     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
971      $form->{oldinvtotal});
972   $form->{oldinvtotal}  = $form->{invtotal};
973   $form->{oldtotalpaid} = $totalpaid;
974
975   &display_form;
976
977   $lxdebug->leave_sub();
978 }
979
980
981 sub post_payment {
982   $lxdebug->enter_sub();
983   for $i (1 .. $form->{paidaccounts}) {
984     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
985       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
986
987       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
988
989       $form->error($locale->text('Cannot post payment for a closed period!'))
990         if ($datepaid <= $closedto);
991
992       if ($form->{currency} ne $form->{defaultcurrency}) {
993         $form->{"exchangerate_$i"} = $form->{exchangerate}
994           if ($invdate == $datepaid);
995         $form->isblank("exchangerate_$i",
996                        $locale->text('Exchangerate for payment missing!'));
997       }
998     }
999   }
1000
1001   ($form->{AP})      = split /--/, $form->{AP};
1002   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1003   $form->redirect($locale->text(' Payment posted!'))
1004       if (AP->post_payment(\%myconfig, \%$form));
1005     $form->error($locale->text('Cannot post payment!'));
1006
1007
1008   $lxdebug->leave_sub();
1009 }
1010
1011
1012 sub post {
1013   $lxdebug->enter_sub();
1014
1015   # check if there is a vendor, invoice and due date
1016   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
1017   $form->isblank("duedate",   $locale->text("Due Date missing!"));
1018   $form->isblank("vendor",    $locale->text('Vendor missing!'));
1019
1020   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
1021   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1022   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
1023
1024   my $zero_amount_posting = 1;
1025   for $i (1 .. $form->{rowcount}) {
1026     if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
1027       $zero_amount_posting = 0;
1028       last;
1029     }
1030   }
1031
1032   $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
1033
1034   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1035     if ($form->{currency} ne $form->{defaultcurrency});
1036   delete($form->{AP});
1037
1038   for $i (1 .. $form->{paidaccounts}) {
1039     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1040       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1041
1042       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1043
1044       $form->error($locale->text('Cannot post payment for a closed period!'))
1045         if ($datepaid <= $closedto);
1046
1047       if ($form->{currency} ne $form->{defaultcurrency}) {
1048         $form->{"exchangerate_$i"} = $form->{exchangerate}
1049           if ($transdate == $datepaid);
1050         $form->isblank("exchangerate_$i",
1051                        $locale->text('Exchangerate for payment missing!'));
1052       }
1053
1054     }
1055   }
1056
1057   # if old vendor ne vendor redo form
1058   ($vendor) = split /--/, $form->{vendor};
1059   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
1060     &update;
1061     exit;
1062   }
1063   ($debitaccno,    $debittaxkey)    = split /--/, $form->{AP_amountselected};
1064   ($taxkey,        $NULL)           = split /--/, $form->{taxchartselected};
1065   ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
1066   $form->{AP}{amount_1} = $debitaccno;
1067   $form->{AP}{payables} = $payablesaccno;
1068   $form->{taxkey}       = $taxkey;
1069
1070   $form->{id} = 0 if $form->{postasnew};
1071
1072   if (AP->post_transaction(\%myconfig, \%$form)) {
1073     # saving the history
1074     if(!exists $form->{addition} && $form->{id} ne "") {
1075       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1076       $form->{addition} = "POSTED";
1077       $form->save_history($form->dbconnect(\%myconfig));
1078     }
1079     # /saving the history 
1080     remove_draft() if $form->{remove_draft};
1081     $form->redirect($locale->text('Transaction posted!'));
1082   }
1083   $form->error($locale->text('Cannot post transaction!'));
1084
1085   $lxdebug->leave_sub();
1086 }
1087
1088 sub post_as_new {
1089   $lxdebug->enter_sub();
1090
1091   $form->{postasnew} = 1;
1092   # saving the history
1093   if(!exists $form->{addition} && $form->{id} ne "") {
1094     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1095         $form->{addition} = "POSTED AS NEW";
1096         $form->save_history($form->dbconnect(\%myconfig));
1097   }
1098   # /saving the history 
1099   &post;
1100
1101   $lxdebug->leave_sub();
1102 }
1103
1104 sub use_as_template {
1105   $lxdebug->enter_sub();
1106
1107   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);
1108   $form->{paidaccounts} = 1;
1109   $form->{rowcount}--;
1110   $form->{invdate} = $form->current_date(\%myconfig);
1111   &update;
1112
1113   $lxdebug->leave_sub();
1114 }
1115
1116 sub delete {
1117   $lxdebug->enter_sub();
1118
1119   $form->{title} = $locale->text('Confirm!');
1120
1121   $form->header;
1122
1123   delete $form->{header};
1124
1125   print qq|
1126 <body>
1127
1128 <form method=post action=$form->{script}>
1129 |;
1130
1131   foreach $key (keys %$form) {
1132     $form->{$key} =~ s/\"/&quot;/g;
1133     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1134   }
1135
1136   print qq|
1137 <h2 class=confirm>$form->{title}</h2>
1138
1139 <h4>|
1140     . $locale->text('Are you sure you want to delete Transaction')
1141     . qq| $form->{invnumber}</h4>
1142
1143 <input name=action class=submit type=submit value="|
1144     . $locale->text('Yes') . qq|">
1145 </form>
1146
1147 </body>
1148 </html>
1149 |;
1150
1151   $lxdebug->leave_sub();
1152 }
1153
1154 sub yes {
1155   $lxdebug->enter_sub();
1156   if (AP->delete_transaction(\%myconfig, \%$form, $spool)) {
1157     # saving the history
1158     if(!exists $form->{addition}) {
1159       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1160           $form->{addition} = "DELETED";
1161       $form->save_history($form->dbconnect(\%myconfig));
1162     }
1163     # /saving the history 
1164     $form->redirect($locale->text('Transaction deleted!'));
1165   }
1166   $form->error($locale->text('Cannot delete transaction!'));
1167
1168   $lxdebug->leave_sub();
1169 }
1170
1171 sub search {
1172   $lxdebug->enter_sub();
1173
1174   # setup vendor selection
1175   $form->all_vc(\%myconfig, "vendor", "AP");
1176
1177   if (@{ $form->{all_vendor} }) {
1178     map { $vendor .= "<option>$_->{name}--$_->{id}\n" }
1179       @{ $form->{all_vendor} };
1180     $vendor = qq|<select name=vendor><option>\n$vendor\n</select>|;
1181   } else {
1182     $vendor = qq|<input name=vendor size=35>|;
1183   }
1184
1185   # departments
1186   if (@{ $form->{all_departments} }) {
1187     $form->{selectdepartment} = "<option>\n";
1188
1189     map {
1190       $form->{selectdepartment} .=
1191         "<option>$_->{description}--$_->{id}\n"
1192     } (@{ $form->{all_departments} });
1193   }
1194
1195   $department = qq|
1196         <tr>
1197           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1198           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1199         </tr>
1200 | if $form->{selectdepartment};
1201
1202   $form->{title} = $locale->text('AP Transactions');
1203
1204   # use JavaScript Calendar or not
1205   $form->{jsscript} = 1;
1206   $jsscript = "";
1207   if ($form->{jsscript}) {
1208
1209     # with JavaScript Calendar
1210     $button1 = qq|
1211        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1212        <input type=button name=transdatefrom id="trigger1" value=|
1213       . $locale->text('button') . qq|></td>
1214       |;
1215     $button2 = qq|
1216        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1217        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1218       . $locale->text('button') . qq|></td>
1219      |;
1220
1221     #write Trigger
1222     $jsscript =
1223       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1224                           "transdateto", "BL", "trigger2");
1225   } else {
1226
1227     # without JavaScript Calendar
1228     $button1 = qq|
1229                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1230     $button2 = qq|
1231                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1232   }
1233
1234   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1235                                    "all" => 1 });
1236
1237   my %labels = ();
1238   my @values = ("");
1239   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1240     push(@values, $item->{"id"});
1241     $labels{$item->{"id"}} = $item->{"projectnumber"};
1242   }
1243   my $projectnumber =
1244     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1245                          '-labels' => \%labels));
1246   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
1247   $form->header;
1248   $onload = qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
1249   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
1250   print qq|
1251 <body onLoad="$onload">
1252
1253 <form method=post action=$form->{script}>
1254
1255 <table width=100%>
1256   <tr>
1257     <th class=listtop>$form->{title}</th>
1258   </tr>
1259   <tr height="5"></tr>
1260   <tr>
1261     <td>
1262       <table>
1263         <tr>
1264           <th align=right>| . $locale->text('Vendor') . qq|</th>
1265           <td colspan=3>$vendor</td>
1266         </tr>
1267         $department
1268         <tr>
1269           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1270           <td colspan=3><input name=invnumber size=20></td>
1271         </tr>
1272         <tr>
1273           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1274           <td colspan=3><input name=ordnumber size=20></td>
1275         </tr>
1276         <tr>
1277           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1278           <td colspan=3><input name=notes size=40></td>
1279         </tr>
1280         <tr>
1281           <th align="right">| . $locale->text("Project Number") . qq|</th>
1282           <td colspan="3">$projectnumber</td>
1283         </tr>
1284         <tr>
1285           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1286           $button1
1287           <th align=right>| . $locale->text('Bis') . qq|</th>
1288           $button2
1289         </tr>
1290         <input type=hidden name=sort value=transdate>
1291       </table>
1292     </td>
1293   </tr>
1294   <tr>
1295     <td>
1296       <table>
1297         <tr>
1298           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1299           <td>
1300             <table width=100%>
1301               <tr>
1302                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1303                 <td nowrap>| . $locale->text('Open') . qq|</td>
1304                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1305                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1306               </tr>
1307               <tr>
1308                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1309                 <td nowrap>| . $locale->text('ID') . qq|</td>
1310                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1311                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1312                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1313                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1314               </tr>
1315               <tr>
1316                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1317                 <td nowrap>| . $locale->text('Vendor') . qq|</td>
1318                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1319                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1320                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1321                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1322               </tr>
1323               <tr>
1324                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1325                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1326                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1327                 <td nowrap>| . $locale->text('Total') . qq|</td>
1328                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1329                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1330               </tr>
1331               <tr>
1332                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1333                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1334                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1335                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1336                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1337                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1338               </tr>
1339               <tr>
1340                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1341                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1342                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1343                 <td nowrap>| . $locale->text('Employee') . qq|</td>
1344               </tr>
1345               <tr>
1346                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1347                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1348                 <td align=right><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y></td>
1349                 <td nowrap>| . $locale->text('Project Number') . qq|</td>
1350               </tr>
1351             </table>
1352           </td>
1353         </tr>
1354       </table>
1355     </td>
1356   </tr>
1357   <tr>
1358     <td><hr size=3 noshade></td>
1359   </tr>
1360 </table>
1361
1362 $jsscript
1363
1364 <br>
1365 <input type=hidden name=nextsub value=$form->{nextsub}>
1366 <input type=hidden name=login value=$form->{login}>
1367 <input type=hidden name=password value=$form->{password}>
1368
1369 <input class=submit type=submit name=action value="|
1370     . $locale->text('Continue') . qq|">
1371 </form>
1372
1373 </body>
1374 </html>
1375 |;
1376
1377   $lxdebug->leave_sub();
1378 }
1379
1380 sub ap_transactions {
1381   $lxdebug->enter_sub();
1382
1383   $form->{vendor} = $form->unescape($form->{vendor});
1384   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
1385
1386   AP->ap_transactions(\%myconfig, \%$form);
1387
1388   $callback =
1389     "$form->{script}?action=ap_transactions&login=$form->{login}&password=$form->{password}";
1390   $href = $callback;
1391
1392   if ($form->{vendor}) {
1393     $callback .= "&vendor=" . $form->escape($form->{vendor}, 1);
1394     $href .= "&vendor=" . $form->escape($form->{vendor});
1395     $option .= $locale->text('Vendor') . " : $form->{vendor}";
1396   }
1397   if ($form->{department}) {
1398     $callback .= "&department=" . $form->escape($form->{department}, 1);
1399     $href .= "&department=" . $form->escape($form->{department});
1400     ($department) = split /--/, $form->{department};
1401     $option .= "\n<br>" if ($option);
1402     $option .= $locale->text('Department') . " : $department";
1403   }
1404   if ($form->{invnumber}) {
1405     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
1406     $href .= "&invnumber=" . $form->escape($form->{invnumber});
1407     $option .= "\n<br>" if ($option);
1408     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
1409   }
1410   if ($form->{ordnumber}) {
1411     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
1412     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
1413     $option .= "\n<br>" if ($option);
1414     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
1415   }
1416   if ($form->{notes}) {
1417     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
1418     $href .= "&notes=" . $form->escape($form->{notes});
1419     $option .= "\n<br>" if $option;
1420     $option .= $locale->text('Notes') . " : $form->{notes}";
1421   }
1422
1423   if ($form->{transdatefrom}) {
1424     $callback .= "&transdatefrom=$form->{transdatefrom}";
1425     $href     .= "&transdatefrom=$form->{transdatefrom}";
1426     $option   .= "\n<br>" if ($option);
1427     $option   .=
1428         $locale->text('From') . " "
1429       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1430   }
1431   if ($form->{transdateto}) {
1432     $callback .= "&transdateto=$form->{transdateto}";
1433     $href     .= "&transdateto=$form->{transdateto}";
1434     $option   .= "\n<br>" if ($option);
1435     $option   .=
1436         $locale->text('Bis') . " "
1437       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1438   }
1439   if ($form->{open}) {
1440     $callback .= "&open=$form->{open}";
1441     $href     .= "&open=$form->{open}";
1442     $option   .= "\n<br>" if ($option);
1443     $option   .= $locale->text('Open');
1444   }
1445   if ($form->{closed}) {
1446     $callback .= "&closed=$form->{closed}";
1447     $href     .= "&closed=$form->{closed}";
1448     $option   .= "\n<br>" if ($option);
1449     $option   .= $locale->text('Closed');
1450   }
1451   if ($form->{globalproject_id}) {
1452     $callback .= "&globalproject_id=" . E($form->{globalproject_id});
1453     $href     .= "&globalproject_id=" . E($form->{globalproject_id});
1454   }
1455
1456   @columns =
1457     qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
1458        due duedate notes employee globalprojectnumber);
1459
1460   $form->{"l_type"} = "Y";
1461
1462   foreach $item (@columns) {
1463     if ($form->{"l_$item"} eq "Y") {
1464       push @column_index, $item;
1465
1466       # add column to href and callback
1467       $callback .= "&l_$item=Y";
1468       $href     .= "&l_$item=Y";
1469     }
1470   }
1471
1472   if ($form->{l_subtotal} eq 'Y') {
1473     $callback .= "&l_subtotal=Y";
1474     $href     .= "&l_subtotal=Y";
1475   }
1476
1477   $column_header{id} =
1478       qq|<th><a class=listheading href=$href&sort=id>|
1479     . $locale->text('ID')
1480     . qq|</a></th>|;
1481   $column_header{transdate} =
1482       qq|<th><a class=listheading href=$href&sort=transdate>|
1483     . $locale->text('Date')
1484     . qq|</a></th>|;
1485   $column_header{type} =
1486       "<th class=\"listheading\">" . $locale->text('Type') . "</th>";
1487   $column_header{duedate} =
1488       qq|<th><a class=listheading href=$href&sort=duedate>|
1489     . $locale->text('Due Date')
1490     . qq|</a></th>|;
1491   $column_header{due} =
1492     qq|<th class=listheading>| . $locale->text('Amount Due') . qq|</th>|;
1493   $column_header{invnumber} =
1494       qq|<th><a class=listheading href=$href&sort=invnumber>|
1495     . $locale->text('Invoice')
1496     . qq|</a></th>|;
1497   $column_header{ordnumber} =
1498       qq|<th><a class=listheading href=$href&sort=ordnumber>|
1499     . $locale->text('Order')
1500     . qq|</a></th>|;
1501   $column_header{name} =
1502       qq|<th><a class=listheading href=$href&sort=name>|
1503     . $locale->text('Vendor')
1504     . qq|</a></th>|;
1505   $column_header{netamount} =
1506     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1507   $column_header{tax} =
1508     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1509   $column_header{amount} =
1510     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1511   $column_header{paid} =
1512     qq|<th class=listheading>| . $locale->text('Paid') . qq|</th>|;
1513   $column_header{datepaid} =
1514       qq|<th><a class=listheading href=$href&sort=datepaid>|
1515     . $locale->text('Date Paid')
1516     . qq|</a></th>|;
1517   $column_header{notes} =
1518     qq|<th class=listheading>| . $locale->text('Notes') . qq|</th>|;
1519   $column_header{employee} =
1520     "<th><a class=listheading href=$href&sort=employee>"
1521     . $locale->text('Employee') . "</th>";
1522   $column_header{globalprojectnumber} =
1523     qq|<th class="listheading">| . $locale->text('Project Number') . qq|</th>|;
1524
1525   $form->{title} = $locale->text('AP Transactions');
1526
1527   $form->header;
1528
1529   print qq|
1530 <body>
1531
1532 <table width=100%>
1533   <tr>
1534     <th class=listtop>$form->{title}</th>
1535   </tr>
1536   <tr height="5"></tr>
1537   <tr>
1538     <td>$option</td>
1539   </tr>
1540   <tr>
1541     <td>
1542       <table width=100%>
1543         <tr class=listheading>
1544 |;
1545
1546   map { print "\n$column_header{$_}" } @column_index;
1547
1548   print qq|
1549         </tr>
1550 |;
1551
1552   # add sort and escape callback
1553   $form->{callback} = "$callback&sort=$form->{sort}";
1554   $callback = $form->escape($form->{callback});
1555
1556   if (@{ $form->{AP} }) {
1557     $sameitem = $form->{AP}->[0]->{ $form->{sort} };
1558   }
1559
1560   # sums and tax on reports by Antonio Gallardo
1561   #
1562   foreach $ap (@{ $form->{AP} }) {
1563
1564     if ($form->{l_subtotal} eq 'Y') {
1565       if ($sameitem ne $ap->{ $form->{sort} }) {
1566         &ap_subtotal;
1567         $sameitem = $ap->{ $form->{sort} };
1568       }
1569     }
1570
1571     $column_data{netamount} =
1572         "<td align=right>"
1573       . $form->format_amount(\%myconfig, $ap->{netamount}, 2, "&nbsp;")
1574       . "</td>";
1575     $column_data{tax} = "<td align=right>"
1576       . $form->format_amount(\%myconfig, $ap->{amount} - $ap->{netamount},
1577                              2, "&nbsp;")
1578       . "</td>";
1579     $column_data{amount} =
1580       "<td align=right>"
1581       . $form->format_amount(\%myconfig, $ap->{amount}, 2, "&nbsp;") . "</td>";
1582     $column_data{paid} =
1583       "<td align=right>"
1584       . $form->format_amount(\%myconfig, $ap->{paid}, 2, "&nbsp;") . "</td>";
1585     $column_data{due} = "<td align=right>"
1586       . $form->format_amount(\%myconfig, $ap->{amount} - $ap->{paid},
1587                              2, "&nbsp;")
1588       . "</td>";
1589
1590     $totalnetamount += $ap->{netamount};
1591     $totalamount    += $ap->{amount};
1592     $totalpaid      += $ap->{paid};
1593     $totaldue       += ($ap->{amount} - $ap->{paid});
1594
1595     $subtotalnetamount += $ap->{netamount};
1596     $subtotalamount    += $ap->{amount};
1597     $subtotalpaid      += $ap->{paid};
1598     $subtotaldue       += ($ap->{amount} - $ap->{paid});
1599
1600     $column_data{transdate} = "<td>$ap->{transdate}&nbsp;</td>";
1601     $column_data{type} = "<td>" .
1602       ($ap->{invoice}    ? $locale->text("Invoice (one letter abbreviation)") :
1603                            $locale->text("AP Transaction (abbreviation)"))
1604         . "</td>";
1605     $column_data{duedate}   = "<td>$ap->{duedate}&nbsp;</td>";
1606     $column_data{datepaid}  = "<td>$ap->{datepaid}&nbsp;</td>";
1607
1608     $module = ($ap->{invoice}) ? "ir.pl" : $form->{script};
1609
1610     $column_data{invnumber} =
1611       qq|<td><a href="$module?action=edit&id=$ap->{id}&login=$form->{login}&password=$form->{password}&callback=$callback">$ap->{invnumber}</a></td>|;
1612     $column_data{id}        = "<td>$ap->{id}</td>";
1613     $column_data{ordnumber} = "<td>$ap->{ordnumber}&nbsp;</td>";
1614     $column_data{name}      = "<td>$ap->{name}</td>";
1615     $ap->{notes} =~ s/\r\n/<br>/g;
1616     $column_data{notes}    = "<td>$ap->{notes}&nbsp;</td>";
1617     $column_data{employee} = "<td>$ap->{employee}&nbsp;</td>";
1618     $column_data{globalprojectnumber}  =
1619       "<td>" . H($ap->{globalprojectnumber}) . "</td>";
1620
1621     $i++;
1622     $i %= 2;
1623     print "
1624         <tr class=listrow$i >
1625 ";
1626
1627     map { print "\n$column_data{$_}" } @column_index;
1628
1629     print qq|
1630         </tr>
1631 |;
1632
1633   }
1634
1635   if ($form->{l_subtotal} eq 'Y') {
1636     &ap_subtotal;
1637   }
1638
1639   # print totals
1640   print qq|
1641         <tr class=listtotal>
1642 |;
1643
1644   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1645
1646   $column_data{netamount} =
1647     "<th class=listtotal align=right>"
1648     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1649   $column_data{tax} = "<th class=listtotal align=right>"
1650     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1651                            2, "&nbsp;")
1652     . "</th>";
1653   $column_data{amount} =
1654     "<th class=listtotal align=right>"
1655     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1656   $column_data{paid} =
1657     "<th class=listtotal align=right>"
1658     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;") . "</th>";
1659   $column_data{due} =
1660     "<th class=listtotal align=right>"
1661     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;") . "</th>";
1662
1663   map { print "$column_data{$_}\n" } @column_index;
1664
1665   print qq|
1666         </tr>
1667       </table>
1668     </td>
1669   </tr>
1670   <tr>
1671     <td><hr size=3 noshade></td>
1672   </tr>
1673 </table>
1674
1675 <br>
1676 <form method=post action=$form->{script}>
1677
1678 <input name=callback type=hidden value="$form->{callback}">
1679
1680 <input type=hidden name=login value=$form->{login}>
1681 <input type=hidden name=password value=$form->{password}>
1682
1683 <input class=submit type=submit name=action value="|
1684     . $locale->text('AP Transaction') . qq|">
1685
1686 <input class=submit type=submit name=action value="|
1687     . $locale->text('Vendor Invoice') . qq|">
1688
1689   </form>
1690
1691 </body>
1692 </html>
1693 |;
1694
1695   $lxdebug->leave_sub();
1696 }
1697
1698 sub ap_subtotal {
1699   $lxdebug->enter_sub();
1700
1701   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1702
1703   $column_data{netamount} =
1704       "<th class=listsubtotal align=right>"
1705     . $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")
1706     . "</th>";
1707   $column_data{tax} = "<th class=listsubtotal align=right>"
1708     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1709                            2, "&nbsp;")
1710     . "</th>";
1711   $column_data{amount} =
1712     "<th class=listsubtotal align=right>"
1713     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1714   $column_data{paid} =
1715     "<th class=listsubtotal align=right>"
1716     . $form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;") . "</th>";
1717   $column_data{due} =
1718     "<th class=listsubtotal align=right>"
1719     . $form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;") . "</th>";
1720
1721   $subtotalnetamount = 0;
1722   $subtotalamount    = 0;
1723   $subtotalpaid      = 0;
1724   $subtotaldue       = 0;
1725
1726   print "<tr class=listsubtotal>";
1727
1728   map { print "\n$column_data{$_}" } @column_index;
1729
1730   print qq|
1731   </tr>
1732 |;
1733
1734   $lxdebug->leave_sub();
1735 }
1736
1737 sub storno {
1738   $lxdebug->enter_sub();
1739
1740   if (IS->has_storno(\%myconfig, $form, 'ap')) {
1741     $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1742     $form->error($locale->text("Transaction has already been cancelled!"));
1743   }
1744
1745   # negate amount/taxes
1746   for my $i (1 .. $form->{rowcount}) {
1747     $form->{"amount_$i"} *= -1;
1748     $form->{"tax_$i"}    *= -1; 
1749   }
1750
1751   # format things
1752   for my $i (1 .. $form->{rowcount}) {
1753     for (qw(amount tax)) {
1754       $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) if $form->{"${_}_$i"};
1755     }
1756   }
1757
1758   $form->{storno}      = 1;
1759   $form->{storno_id}   = $form->{id};
1760   $form->{id}          = 0;
1761
1762   $form->{invnumber}   = "Storno-" . $form->{invnumber};
1763
1764   post();
1765
1766   # saving the history
1767   if(!exists $form->{addition} && $form->{id} ne "") {
1768     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1769     $form->{addition} = "STORNO";
1770     $form->save_history($form->dbconnect(\%myconfig));
1771   }
1772   # /saving the history 
1773
1774   $lxdebug->leave_sub();
1775 }