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