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