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