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