Einige Browser (Opera) mögen es nicht, wenn "checked" bei Checkboxen in Anführungszei...
[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/reportgenerator.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->{id}));
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="| . $locale->text('mark as paid') . qq|"> |;
802   }
803   # /mark_as_paid button
804   print "
805 </form>
806
807 </body>
808 </html>
809 ";
810
811   $lxdebug->leave_sub();
812 }
813
814 sub mark_as_paid {
815   $lxdebug->enter_sub();
816   &mark_as_paid_common(\%myconfig,"ap");  
817   $lxdebug->leave_sub();
818 }
819
820 sub update {
821   $lxdebug->enter_sub();
822
823   my $display = shift;
824
825   $form->{invtotal} = 0;
826
827   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
828     qw(exchangerate creditlimit creditremaining);
829
830   @flds  = qw(amount AP_amount projectnumber oldprojectnumber project_id);
831   $count = 0;
832   for $i (1 .. $form->{rowcount}) {
833     $form->{"amount_$i"} =
834       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
835     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
836     if ($form->{"amount_$i"}) {
837       push @a, {};
838       $j = $#a;
839       if (!$form->{"korrektur_$i"}) {
840         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
841         if ($taxkey > 1) {
842           if ($form->{taxincluded}) {
843             $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
844           } else {
845             $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
846           }
847         } else {
848           $form->{"tax_$i"} = 0;
849         }
850       }
851       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
852
853       $totaltax += $form->{"tax_$i"};
854       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
855       $count++;
856     }
857   }
858   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
859
860   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
861
862   $form->{exchangerate} = $exchangerate
863     if (
864         $form->{forex} = (
865                     $exchangerate =
866                       $form->check_exchangerate(
867                       \%myconfig, $form->{currency}, $form->{transdate}, 'sell'
868                       )));
869
870   $form->{invdate} = $form->{transdate};
871   $save_AP = $form->{AP};
872   &check_name(vendor);
873   $form->{AP} = $save_AP;
874
875   $form->{rowcount} = $count + 1;
876
877   $form->{invtotal} =
878     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
879
880   for $i (1 .. $form->{paidaccounts}) {
881     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
882       map {
883         $form->{"${_}_$i"} =
884           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
885       } qw(paid exchangerate);
886
887       $totalpaid += $form->{"paid_$i"};
888
889       $form->{"exchangerate_$i"} = $exchangerate
890         if (
891             $form->{"forex_$i"} = (
892                 $exchangerate =
893                   $form->check_exchangerate(
894                   \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'
895                   )));
896     }
897   }
898
899   $form->{creditremaining} -=
900     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
901      $form->{oldinvtotal});
902   $form->{oldinvtotal}  = $form->{invtotal};
903   $form->{oldtotalpaid} = $totalpaid;
904
905   &display_form;
906
907   $lxdebug->leave_sub();
908 }
909
910
911 sub post_payment {
912   $lxdebug->enter_sub();
913
914   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
915
916   for $i (1 .. $form->{paidaccounts}) {
917     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
918       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
919
920       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
921
922       $form->error($locale->text('Cannot post payment for a closed period!'))
923         if ($datepaid <= $closedto);
924
925       if ($form->{currency} ne $form->{defaultcurrency}) {
926         $form->{"exchangerate_$i"} = $form->{exchangerate}
927           if ($invdate == $datepaid);
928         $form->isblank("exchangerate_$i",
929                        $locale->text('Exchangerate for payment missing!'));
930       }
931     }
932   }
933
934   ($form->{AP})      = split /--/, $form->{AP};
935   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
936   $form->redirect($locale->text('Payment posted!'))
937       if (AP->post_payment(\%myconfig, \%$form));
938     $form->error($locale->text('Cannot post payment!'));
939
940
941   $lxdebug->leave_sub();
942 }
943
944
945 sub post {
946   $lxdebug->enter_sub();
947
948   # check if there is a vendor, invoice and due date
949   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
950   $form->isblank("duedate",   $locale->text("Due Date missing!"));
951   $form->isblank("vendor",    $locale->text('Vendor missing!'));
952
953   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
954   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
955   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
956
957   my $zero_amount_posting = 1;
958   for $i (1 .. $form->{rowcount}) {
959     if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
960       $zero_amount_posting = 0;
961       last;
962     }
963   }
964
965   $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
966
967   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
968     if ($form->{currency} ne $form->{defaultcurrency});
969   delete($form->{AP});
970
971   for $i (1 .. $form->{paidaccounts}) {
972     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
973       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
974
975       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
976
977       $form->error($locale->text('Cannot post payment for a closed period!'))
978         if ($datepaid <= $closedto);
979
980       if ($form->{currency} ne $form->{defaultcurrency}) {
981         $form->{"exchangerate_$i"} = $form->{exchangerate}
982           if ($transdate == $datepaid);
983         $form->isblank("exchangerate_$i",
984                        $locale->text('Exchangerate for payment missing!'));
985       }
986
987     }
988   }
989
990   # if old vendor ne vendor redo form
991   ($vendor) = split /--/, $form->{vendor};
992   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
993     &update;
994     exit;
995   }
996   ($debitaccno,    $debittaxkey)    = split /--/, $form->{AP_amountselected};
997   ($taxkey,        $NULL)           = split /--/, $form->{taxchartselected};
998   ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
999   $form->{AP}{amount_1} = $debitaccno;
1000   $form->{AP}{payables} = $payablesaccno;
1001   $form->{taxkey}       = $taxkey;
1002   $form->{storno}       = 0;
1003
1004   $form->{id} = 0 if $form->{postasnew};
1005
1006   if (AP->post_transaction(\%myconfig, \%$form)) {
1007     # saving the history
1008     if(!exists $form->{addition} && $form->{id} ne "") {
1009       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1010       $form->{addition} = "POSTED";
1011       $form->save_history($form->dbconnect(\%myconfig));
1012     }
1013     # /saving the history 
1014     remove_draft() if $form->{remove_draft};
1015     $form->redirect($locale->text('Transaction posted!'));
1016   }
1017   $form->error($locale->text('Cannot post transaction!'));
1018
1019   $lxdebug->leave_sub();
1020 }
1021
1022 sub post_as_new {
1023   $lxdebug->enter_sub();
1024
1025   $form->{postasnew} = 1;
1026   # saving the history
1027   if(!exists $form->{addition} && $form->{id} ne "") {
1028     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1029         $form->{addition} = "POSTED AS NEW";
1030         $form->save_history($form->dbconnect(\%myconfig));
1031   }
1032   # /saving the history 
1033   &post;
1034
1035   $lxdebug->leave_sub();
1036 }
1037
1038 sub use_as_template {
1039   $lxdebug->enter_sub();
1040
1041   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);
1042   $form->{paidaccounts} = 1;
1043   $form->{rowcount}--;
1044   $form->{invdate} = $form->current_date(\%myconfig);
1045   &update;
1046
1047   $lxdebug->leave_sub();
1048 }
1049
1050 sub delete {
1051   $lxdebug->enter_sub();
1052
1053   $form->{title} = $locale->text('Confirm!');
1054
1055   $form->header;
1056
1057   delete $form->{header};
1058
1059   print qq|
1060 <body>
1061
1062 <form method=post action=$form->{script}>
1063 |;
1064
1065   foreach $key (keys %$form) {
1066     $form->{$key} =~ s/\"/&quot;/g;
1067     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1068   }
1069
1070   print qq|
1071 <h2 class=confirm>$form->{title}</h2>
1072
1073 <h4>|
1074     . $locale->text('Are you sure you want to delete Transaction')
1075     . qq| $form->{invnumber}</h4>
1076
1077 <input name=action class=submit type=submit value="|
1078     . $locale->text('Yes') . qq|">
1079 </form>
1080
1081 </body>
1082 </html>
1083 |;
1084
1085   $lxdebug->leave_sub();
1086 }
1087
1088 sub yes {
1089   $lxdebug->enter_sub();
1090   if (AP->delete_transaction(\%myconfig, \%$form, $spool)) {
1091     # saving the history
1092     if(!exists $form->{addition}) {
1093       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1094           $form->{addition} = "DELETED";
1095       $form->save_history($form->dbconnect(\%myconfig));
1096     }
1097     # /saving the history 
1098     $form->redirect($locale->text('Transaction deleted!'));
1099   }
1100   $form->error($locale->text('Cannot delete transaction!'));
1101
1102   $lxdebug->leave_sub();
1103 }
1104
1105 sub search {
1106   $lxdebug->enter_sub();
1107
1108   # setup vendor selection
1109   $form->all_vc(\%myconfig, "vendor", "AP");
1110
1111   if (@{ $form->{all_vendor} }) {
1112     map { $vendor .= "<option>$_->{name}--$_->{id}\n" }
1113       @{ $form->{all_vendor} };
1114     $vendor = qq|<select name=vendor><option>\n$vendor\n</select>|;
1115   } else {
1116     $vendor = qq|<input name=vendor size=35>|;
1117   }
1118
1119   # departments
1120   if (@{ $form->{all_departments} }) {
1121     $form->{selectdepartment} = "<option>\n";
1122
1123     map {
1124       $form->{selectdepartment} .=
1125         "<option>$_->{description}--$_->{id}\n"
1126     } (@{ $form->{all_departments} });
1127   }
1128
1129   $department = qq|
1130         <tr>
1131           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1132           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1133         </tr>
1134 | if $form->{selectdepartment};
1135
1136   $form->{title} = $locale->text('AP Transactions');
1137
1138   # use JavaScript Calendar or not
1139   $form->{jsscript} = 1;
1140   $jsscript = "";
1141   if ($form->{jsscript}) {
1142
1143     # with JavaScript Calendar
1144     $button1 = qq|
1145        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1146        <input type=button name=transdatefrom id="trigger1" value=|
1147       . $locale->text('button') . qq|></td>
1148       |;
1149     $button2 = qq|
1150        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1151        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1152       . $locale->text('button') . qq|></td>
1153      |;
1154
1155     #write Trigger
1156     $jsscript =
1157       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1158                           "transdateto", "BL", "trigger2");
1159   } else {
1160
1161     # without JavaScript Calendar
1162     $button1 = qq|
1163                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1164     $button2 = qq|
1165                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1166   }
1167
1168   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1169                                    "all" => 1 });
1170
1171   my %labels = ();
1172   my @values = ("");
1173   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1174     push(@values, $item->{"id"});
1175     $labels{$item->{"id"}} = $item->{"projectnumber"};
1176   }
1177   my $projectnumber =
1178     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1179                          '-labels' => \%labels));
1180   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
1181   $form->header;
1182   $onload = qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
1183   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
1184   print qq|
1185 <body onLoad="$onload">
1186
1187 <form method=post action=$form->{script}>
1188
1189 <table width=100%>
1190   <tr>
1191     <th class=listtop>$form->{title}</th>
1192   </tr>
1193   <tr height="5"></tr>
1194   <tr>
1195     <td>
1196       <table>
1197         <tr>
1198           <th align=right>| . $locale->text('Vendor') . qq|</th>
1199           <td colspan=3>$vendor</td>
1200         </tr>
1201         $department
1202         <tr>
1203           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1204           <td colspan=3><input name=invnumber size=20></td>
1205         </tr>
1206         <tr>
1207           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1208           <td colspan=3><input name=ordnumber size=20></td>
1209         </tr>
1210         <tr>
1211           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1212           <td colspan=3><input name=notes size=40></td>
1213         </tr>
1214         <tr>
1215           <th align="right">| . $locale->text("Project Number") . qq|</th>
1216           <td colspan="3">$projectnumber</td>
1217         </tr>
1218         <tr>
1219           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1220           $button1
1221           <th align=right>| . $locale->text('Bis') . qq|</th>
1222           $button2
1223         </tr>
1224         <input type=hidden name=sort value=transdate>
1225       </table>
1226     </td>
1227   </tr>
1228   <tr>
1229     <td>
1230       <table>
1231         <tr>
1232           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1233           <td>
1234             <table width=100%>
1235               <tr>
1236                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1237                 <td nowrap>| . $locale->text('Open') . qq|</td>
1238                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1239                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1240               </tr>
1241               <tr>
1242                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1243                 <td nowrap>| . $locale->text('ID') . qq|</td>
1244                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1245                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1246                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1247                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1248               </tr>
1249               <tr>
1250                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1251                 <td nowrap>| . $locale->text('Vendor') . qq|</td>
1252                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1253                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1254                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1255                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1256               </tr>
1257               <tr>
1258                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1259                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1260                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1261                 <td nowrap>| . $locale->text('Total') . qq|</td>
1262                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1263                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1264               </tr>
1265               <tr>
1266                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1267                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1268                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1269                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1270                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1271                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1272               </tr>
1273               <tr>
1274                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1275                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1276                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1277                 <td nowrap>| . $locale->text('Employee') . qq|</td>
1278               </tr>
1279               <tr>
1280                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1281                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1282                 <td align=right><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y></td>
1283                 <td nowrap>| . $locale->text('Project Number') . qq|</td>
1284               </tr>
1285             </table>
1286           </td>
1287         </tr>
1288       </table>
1289     </td>
1290   </tr>
1291   <tr>
1292     <td><hr size=3 noshade></td>
1293   </tr>
1294 </table>
1295
1296 $jsscript
1297
1298 <br>
1299 <input type=hidden name=nextsub value=$form->{nextsub}>
1300 <input type=hidden name=login value=$form->{login}>
1301 <input type=hidden name=password value=$form->{password}>
1302
1303 <input class=submit type=submit name=action value="|
1304     . $locale->text('Continue') . qq|">
1305 </form>
1306
1307 </body>
1308 </html>
1309 |;
1310
1311   $lxdebug->leave_sub();
1312 }
1313
1314 sub create_subtotal_row {
1315   $lxdebug->enter_sub();
1316
1317   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
1318
1319   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
1320
1321   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
1322
1323   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
1324
1325   map { $totals->{$_} = 0 } @{ $subtotal_columns };
1326
1327   $lxdebug->leave_sub();
1328
1329   return $row;
1330 }
1331
1332 sub ap_transactions {
1333   $lxdebug->enter_sub();
1334
1335   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
1336
1337   $form->{sort} ||= 'transdate';
1338
1339   AP->ap_transactions(\%myconfig, \%$form);
1340
1341   $form->{title} = $locale->text('AP Transactions');
1342
1343   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1344
1345   my @columns =
1346     qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
1347        due duedate transaction_description notes employee globalprojectnumber);
1348
1349   my @hidden_variables = map { "l_${_}" } @columns;
1350   push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto);
1351
1352   my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
1353
1354   my %column_defs = (
1355     'transdate'               => { 'text' => $locale->text('Date'), },
1356     'id'                      => { 'text' => $locale->text('ID'), },
1357     'type'                    => { 'text' => $locale->text('Type'), },
1358     'invnumber'               => { 'text' => $locale->text('Invoice'), },
1359     'ordnumber'               => { 'text' => $locale->text('Order'), },
1360     'name'                    => { 'text' => $locale->text('Vendor'), },
1361     'netamount'               => { 'text' => $locale->text('Amount'), },
1362     'tax'                     => { 'text' => $locale->text('Tax'), },
1363     'amount'                  => { 'text' => $locale->text('Total'), },
1364     'paid'                    => { 'text' => $locale->text('Paid'), },
1365     'datepaid'                => { 'text' => $locale->text('Date Paid'), },
1366     'due'                     => { 'text' => $locale->text('Amount Due'), },
1367     'duedate'                 => { 'text' => $locale->text('Due Date'), },
1368     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1369     'notes'                   => { 'text' => $locale->text('Notes'), },
1370     'employee'                => { 'text' => $locale->text('Salesperson'), },
1371     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
1372   );
1373
1374   foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid
1375                        employee shippingpoint shipvia)) {
1376     $column_defs{$name}->{link} = $href . "&sort=$name";
1377   }
1378
1379   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1380
1381   $form->{"l_type"} = "Y";
1382   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1383
1384   $report->set_columns(%column_defs);
1385   $report->set_column_order(@columns);
1386
1387   $report->set_export_options('ap_transactions', @hidden_variables);
1388
1389   $report->set_sort_indicator($form->{sort}, 1);
1390
1391   my @options;
1392   if ($form->{vendor}) {
1393     push @options, $locale->text('Vendor') . " : $form->{vendor}";
1394   }
1395   if ($form->{department}) {
1396     ($department) = split /--/, $form->{department};
1397     push @options, $locale->text('Department') . " : $department";
1398   }
1399   if ($form->{invnumber}) {
1400     push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
1401   }
1402   if ($form->{ordnumber}) {
1403     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1404   }
1405   if ($form->{notes}) {
1406     push @options, $locale->text('Notes') . " : $form->{notes}";
1407   }
1408   if ($form->{transaction_description}) {
1409     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1410   }
1411   if ($form->{transdatefrom}) {
1412     push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1413   }
1414   if ($form->{transdateto}) {
1415     push @options, $locale->text('Bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdateto}, 1);
1416   }
1417   if ($form->{open}) {
1418     push @options, $locale->text('Open');
1419   }
1420   if ($form->{closed}) {
1421     push @options, $locale->text('Closed');
1422   }
1423
1424   $report->set_options('top_info_text'        => join("\n", @options),
1425                        'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
1426                        'output_format'        => 'HTML',
1427                        'title'                => $form->{title},
1428                        'attachment_basename'  => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1429     );
1430   $report->set_options_from_form();
1431
1432   # add sort and escape callback, this one we use for the add sub
1433   $form->{callback} = $href .= "&sort=$form->{sort}";
1434
1435   # escape callback for href
1436   $callback = $form->escape($href);
1437
1438   my @subtotal_columns = qw(netamount amount paid due);
1439
1440   my %totals    = map { $_ => 0 } @subtotal_columns;
1441   my %subtotals = map { $_ => 0 } @subtotal_columns;
1442
1443   my $idx = 0;
1444
1445   foreach $ap (@{ $form->{AP} }) {
1446     $ap->{tax} = $ap->{amount} - $ap->{netamount};
1447     $ap->{due} = $ap->{amount} - $ap->{paid};
1448
1449     map { $subtotals{$_} += $ap->{$_};
1450           $totals{$_}    += $ap->{$_} } @subtotal_columns;
1451
1452     map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
1453
1454     $ap->{type} =
1455       $ap->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1456                        $locale->text("AP Transaction (abbreviation)");
1457
1458     my $row = { };
1459
1460     foreach my $column (@columns) {
1461       $row->{$column} = {
1462         'data'  => $ap->{$column},
1463         'align' => $column_alignment{$column},
1464       };
1465     }
1466
1467     $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1468       . "&id=" . E($ap->{id}) . "&callback=${callback}";
1469
1470     my $row_set = [ $row ];
1471
1472     if (($form->{l_subtotal} eq 'Y')
1473         && (($idx == (scalar @{ $form->{AP} } - 1))
1474             || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1475       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1476     }
1477
1478     $report->add_data($row_set);
1479
1480     $idx++;
1481   }
1482
1483   $report->add_separator();
1484   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1485
1486   $report->generate_with_headers();
1487
1488   $lxdebug->leave_sub();
1489 }
1490
1491 sub storno {
1492   $lxdebug->enter_sub();
1493
1494   if (IS->has_storno(\%myconfig, $form, 'ap')) {
1495     $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1496     $form->error($locale->text("Transaction has already been cancelled!"));
1497   }
1498
1499   AP->storno($form, \%myconfig, $form->{id});
1500
1501   # saving the history
1502   if(!exists $form->{addition} && $form->{id} ne "") {
1503     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1504     $form->{addition} = "STORNO";
1505     $form->save_history($form->dbconnect(\%myconfig));
1506   }
1507   # /saving the history 
1508
1509   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id}); 
1510
1511   $lxdebug->leave_sub();
1512 }