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