ddd1c22a974a2b386e87093a0be7038220fb1d95
[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   print "
782 </form>
783
784 </body>
785 </html>
786 ";
787
788   $lxdebug->leave_sub();
789 }
790
791 sub update {
792   $lxdebug->enter_sub();
793
794   my $display = shift;
795
796   $form->{invtotal} = 0;
797
798   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
799     qw(exchangerate creditlimit creditremaining);
800
801   @flds  = qw(amount AP_amount projectnumber oldprojectnumber project_id);
802   $count = 0;
803   for $i (1 .. $form->{rowcount}) {
804     $form->{"amount_$i"} =
805       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
806     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
807     if ($form->{"amount_$i"}) {
808       push @a, {};
809       $j = $#a;
810       if (!$form->{"korrektur_$i"}) {
811         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
812         if ($taxkey > 1) {
813           if ($form->{taxincluded}) {
814             $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
815           } else {
816             $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
817           }
818         } else {
819           $form->{"tax_$i"} = 0;
820         }
821       }
822       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
823
824       $totaltax += $form->{"tax_$i"};
825       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
826       $count++;
827     }
828   }
829   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
830
831   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
832
833   $form->{exchangerate} = $exchangerate
834     if (
835         $form->{forex} = (
836                     $exchangerate =
837                       $form->check_exchangerate(
838                       \%myconfig, $form->{currency}, $form->{transdate}, 'sell'
839                       )));
840
841   $form->{invdate} = $form->{transdate};
842   $save_AP = $form->{AP};
843   &check_name(vendor);
844   $form->{AP} = $save_AP;
845
846   $form->{rowcount} = $count + 1;
847
848   $form->{invtotal} =
849     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
850
851   for $i (1 .. $form->{paidaccounts}) {
852     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
853       map {
854         $form->{"${_}_$i"} =
855           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
856       } qw(paid exchangerate);
857
858       $totalpaid += $form->{"paid_$i"};
859
860       $form->{"exchangerate_$i"} = $exchangerate
861         if (
862             $form->{"forex_$i"} = (
863                 $exchangerate =
864                   $form->check_exchangerate(
865                   \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'
866                   )));
867     }
868   }
869
870   $form->{creditremaining} -=
871     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
872      $form->{oldinvtotal});
873   $form->{oldinvtotal}  = $form->{invtotal};
874   $form->{oldtotalpaid} = $totalpaid;
875
876   &display_form;
877
878   $lxdebug->leave_sub();
879 }
880
881
882 sub post_payment {
883   $lxdebug->enter_sub();
884   for $i (1 .. $form->{paidaccounts}) {
885     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
886       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
887
888       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
889
890       $form->error($locale->text('Cannot post payment for a closed period!'))
891         if ($datepaid <= $closedto);
892
893       if ($form->{currency} ne $form->{defaultcurrency}) {
894         $form->{"exchangerate_$i"} = $form->{exchangerate}
895           if ($invdate == $datepaid);
896         $form->isblank("exchangerate_$i",
897                        $locale->text('Exchangerate for payment missing!'));
898       }
899     }
900   }
901
902   ($form->{AP})      = split /--/, $form->{AP};
903   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
904   $form->redirect($locale->text('Payment posted!'))
905       if (AP->post_payment(\%myconfig, \%$form));
906     $form->error($locale->text('Cannot post payment!'));
907
908
909   $lxdebug->leave_sub();
910 }
911
912
913 sub post {
914   $lxdebug->enter_sub();
915
916   # check if there is a vendor, invoice and due date
917   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
918   $form->isblank("duedate",   $locale->text("Due Date missing!"));
919   $form->isblank("vendor",    $locale->text('Vendor missing!'));
920
921   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
922   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
923   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
924
925   my $zero_amount_posting = 1;
926   for $i (1 .. $form->{rowcount}) {
927     if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
928       $zero_amount_posting = 0;
929       last;
930     }
931   }
932
933   $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
934
935   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
936     if ($form->{currency} ne $form->{defaultcurrency});
937   delete($form->{AP});
938
939   for $i (1 .. $form->{paidaccounts}) {
940     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
941       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
942
943       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
944
945       $form->error($locale->text('Cannot post payment for a closed period!'))
946         if ($datepaid <= $closedto);
947
948       if ($form->{currency} ne $form->{defaultcurrency}) {
949         $form->{"exchangerate_$i"} = $form->{exchangerate}
950           if ($transdate == $datepaid);
951         $form->isblank("exchangerate_$i",
952                        $locale->text('Exchangerate for payment missing!'));
953       }
954
955     }
956   }
957
958   # if old vendor ne vendor redo form
959   ($vendor) = split /--/, $form->{vendor};
960   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
961     &update;
962     exit;
963   }
964   ($debitaccno,    $debittaxkey)    = split /--/, $form->{AP_amountselected};
965   ($taxkey,        $NULL)           = split /--/, $form->{taxchartselected};
966   ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
967   $form->{AP}{amount_1} = $debitaccno;
968   $form->{AP}{payables} = $payablesaccno;
969   $form->{taxkey}       = $taxkey;
970
971   $form->{id} = 0 if $form->{postasnew};
972
973   if (AP->post_transaction(\%myconfig, \%$form)) {
974     # saving the history
975     if(!exists $form->{addition} && $form->{id} ne "") {
976       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
977       $form->{addition} = "POSTED";
978       $form->save_history($form->dbconnect(\%myconfig));
979     }
980     # /saving the history 
981     remove_draft() if $form->{remove_draft};
982     $form->redirect($locale->text('Transaction posted!'));
983   }
984   $form->error($locale->text('Cannot post transaction!'));
985
986   $lxdebug->leave_sub();
987 }
988
989 sub post_as_new {
990   $lxdebug->enter_sub();
991
992   $form->{postasnew} = 1;
993   # saving the history
994   if(!exists $form->{addition} && $form->{id} ne "") {
995     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
996         $form->{addition} = "POSTED AS NEW";
997         $form->save_history($form->dbconnect(\%myconfig));
998   }
999   # /saving the history 
1000   &post;
1001
1002   $lxdebug->leave_sub();
1003 }
1004
1005 sub use_as_template {
1006   $lxdebug->enter_sub();
1007
1008   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);
1009   $form->{paidaccounts} = 1;
1010   $form->{rowcount}--;
1011   $form->{invdate} = $form->current_date(\%myconfig);
1012   &update;
1013
1014   $lxdebug->leave_sub();
1015 }
1016
1017 sub delete {
1018   $lxdebug->enter_sub();
1019
1020   $form->{title} = $locale->text('Confirm!');
1021
1022   $form->header;
1023
1024   delete $form->{header};
1025
1026   print qq|
1027 <body>
1028
1029 <form method=post action=$form->{script}>
1030 |;
1031
1032   foreach $key (keys %$form) {
1033     $form->{$key} =~ s/\"/&quot;/g;
1034     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1035   }
1036
1037   print qq|
1038 <h2 class=confirm>$form->{title}</h2>
1039
1040 <h4>|
1041     . $locale->text('Are you sure you want to delete Transaction')
1042     . qq| $form->{invnumber}</h4>
1043
1044 <input name=action class=submit type=submit value="|
1045     . $locale->text('Yes') . qq|">
1046 </form>
1047
1048 </body>
1049 </html>
1050 |;
1051
1052   $lxdebug->leave_sub();
1053 }
1054
1055 sub yes {
1056   $lxdebug->enter_sub();
1057   if (AP->delete_transaction(\%myconfig, \%$form, $spool)) {
1058     # saving the history
1059     if(!exists $form->{addition}) {
1060       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1061           $form->{addition} = "DELETED";
1062       $form->save_history($form->dbconnect(\%myconfig));
1063     }
1064     # /saving the history 
1065     $form->redirect($locale->text('Transaction deleted!'));
1066   }
1067   $form->error($locale->text('Cannot delete transaction!'));
1068
1069   $lxdebug->leave_sub();
1070 }
1071
1072 sub search {
1073   $lxdebug->enter_sub();
1074
1075   # setup vendor selection
1076   $form->all_vc(\%myconfig, "vendor", "AP");
1077
1078   if (@{ $form->{all_vendor} }) {
1079     map { $vendor .= "<option>$_->{name}--$_->{id}\n" }
1080       @{ $form->{all_vendor} };
1081     $vendor = qq|<select name=vendor><option>\n$vendor\n</select>|;
1082   } else {
1083     $vendor = qq|<input name=vendor size=35>|;
1084   }
1085
1086   # departments
1087   if (@{ $form->{all_departments} }) {
1088     $form->{selectdepartment} = "<option>\n";
1089
1090     map {
1091       $form->{selectdepartment} .=
1092         "<option>$_->{description}--$_->{id}\n"
1093     } (@{ $form->{all_departments} });
1094   }
1095
1096   $department = qq|
1097         <tr>
1098           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1099           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1100         </tr>
1101 | if $form->{selectdepartment};
1102
1103   $form->{title} = $locale->text('AP Transactions');
1104
1105   # use JavaScript Calendar or not
1106   $form->{jsscript} = 1;
1107   $jsscript = "";
1108   if ($form->{jsscript}) {
1109
1110     # with JavaScript Calendar
1111     $button1 = qq|
1112        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1113        <input type=button name=transdatefrom id="trigger1" value=|
1114       . $locale->text('button') . qq|></td>
1115       |;
1116     $button2 = qq|
1117        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1118        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1119       . $locale->text('button') . qq|></td>
1120      |;
1121
1122     #write Trigger
1123     $jsscript =
1124       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1125                           "transdateto", "BL", "trigger2");
1126   } else {
1127
1128     # without JavaScript Calendar
1129     $button1 = qq|
1130                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1131     $button2 = qq|
1132                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1133   }
1134
1135   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1136                                    "all" => 1 });
1137
1138   my %labels = ();
1139   my @values = ("");
1140   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1141     push(@values, $item->{"id"});
1142     $labels{$item->{"id"}} = $item->{"projectnumber"};
1143   }
1144   my $projectnumber =
1145     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1146                          '-labels' => \%labels));
1147   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
1148   $form->header;
1149   $onload = qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
1150   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
1151   print qq|
1152 <body onLoad="$onload">
1153
1154 <form method=post action=$form->{script}>
1155
1156 <table width=100%>
1157   <tr>
1158     <th class=listtop>$form->{title}</th>
1159   </tr>
1160   <tr height="5"></tr>
1161   <tr>
1162     <td>
1163       <table>
1164         <tr>
1165           <th align=right>| . $locale->text('Vendor') . qq|</th>
1166           <td colspan=3>$vendor</td>
1167         </tr>
1168         $department
1169         <tr>
1170           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1171           <td colspan=3><input name=invnumber size=20></td>
1172         </tr>
1173         <tr>
1174           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1175           <td colspan=3><input name=ordnumber size=20></td>
1176         </tr>
1177         <tr>
1178           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1179           <td colspan=3><input name=notes size=40></td>
1180         </tr>
1181         <tr>
1182           <th align="right">| . $locale->text("Project Number") . qq|</th>
1183           <td colspan="3">$projectnumber</td>
1184         </tr>
1185         <tr>
1186           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1187           $button1
1188           <th align=right>| . $locale->text('Bis') . qq|</th>
1189           $button2
1190         </tr>
1191         <input type=hidden name=sort value=transdate>
1192       </table>
1193     </td>
1194   </tr>
1195   <tr>
1196     <td>
1197       <table>
1198         <tr>
1199           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1200           <td>
1201             <table width=100%>
1202               <tr>
1203                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1204                 <td nowrap>| . $locale->text('Open') . qq|</td>
1205                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1206                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1207               </tr>
1208               <tr>
1209                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1210                 <td nowrap>| . $locale->text('ID') . qq|</td>
1211                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1212                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1213                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1214                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1215               </tr>
1216               <tr>
1217                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1218                 <td nowrap>| . $locale->text('Vendor') . qq|</td>
1219                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1220                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1221                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1222                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1223               </tr>
1224               <tr>
1225                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1226                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1227                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1228                 <td nowrap>| . $locale->text('Total') . qq|</td>
1229                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1230                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1231               </tr>
1232               <tr>
1233                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1234                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1235                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1236                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1237                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1238                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1239               </tr>
1240               <tr>
1241                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1242                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1243                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1244                 <td nowrap>| . $locale->text('Employee') . qq|</td>
1245               </tr>
1246               <tr>
1247                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1248                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1249                 <td align=right><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y></td>
1250                 <td nowrap>| . $locale->text('Project Number') . qq|</td>
1251               </tr>
1252             </table>
1253           </td>
1254         </tr>
1255       </table>
1256     </td>
1257   </tr>
1258   <tr>
1259     <td><hr size=3 noshade></td>
1260   </tr>
1261 </table>
1262
1263 $jsscript
1264
1265 <br>
1266 <input type=hidden name=nextsub value=$form->{nextsub}>
1267 <input type=hidden name=login value=$form->{login}>
1268 <input type=hidden name=password value=$form->{password}>
1269
1270 <input class=submit type=submit name=action value="|
1271     . $locale->text('Continue') . qq|">
1272 </form>
1273
1274 </body>
1275 </html>
1276 |;
1277
1278   $lxdebug->leave_sub();
1279 }
1280
1281 sub ap_transactions {
1282   $lxdebug->enter_sub();
1283
1284   $form->{vendor} = $form->unescape($form->{vendor});
1285   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
1286
1287   AP->ap_transactions(\%myconfig, \%$form);
1288
1289   $callback =
1290     "$form->{script}?action=ap_transactions&login=$form->{login}&password=$form->{password}";
1291   $href = $callback;
1292
1293   if ($form->{vendor}) {
1294     $callback .= "&vendor=" . $form->escape($form->{vendor}, 1);
1295     $href .= "&vendor=" . $form->escape($form->{vendor});
1296     $option .= $locale->text('Vendor') . " : $form->{vendor}";
1297   }
1298   if ($form->{department}) {
1299     $callback .= "&department=" . $form->escape($form->{department}, 1);
1300     $href .= "&department=" . $form->escape($form->{department});
1301     ($department) = split /--/, $form->{department};
1302     $option .= "\n<br>" if ($option);
1303     $option .= $locale->text('Department') . " : $department";
1304   }
1305   if ($form->{invnumber}) {
1306     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
1307     $href .= "&invnumber=" . $form->escape($form->{invnumber});
1308     $option .= "\n<br>" if ($option);
1309     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
1310   }
1311   if ($form->{ordnumber}) {
1312     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
1313     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
1314     $option .= "\n<br>" if ($option);
1315     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
1316   }
1317   if ($form->{notes}) {
1318     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
1319     $href .= "&notes=" . $form->escape($form->{notes});
1320     $option .= "\n<br>" if $option;
1321     $option .= $locale->text('Notes') . " : $form->{notes}";
1322   }
1323
1324   if ($form->{transdatefrom}) {
1325     $callback .= "&transdatefrom=$form->{transdatefrom}";
1326     $href     .= "&transdatefrom=$form->{transdatefrom}";
1327     $option   .= "\n<br>" if ($option);
1328     $option   .=
1329         $locale->text('From') . " "
1330       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1331   }
1332   if ($form->{transdateto}) {
1333     $callback .= "&transdateto=$form->{transdateto}";
1334     $href     .= "&transdateto=$form->{transdateto}";
1335     $option   .= "\n<br>" if ($option);
1336     $option   .=
1337         $locale->text('Bis') . " "
1338       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1339   }
1340   if ($form->{open}) {
1341     $callback .= "&open=$form->{open}";
1342     $href     .= "&open=$form->{open}";
1343     $option   .= "\n<br>" if ($option);
1344     $option   .= $locale->text('Open');
1345   }
1346   if ($form->{closed}) {
1347     $callback .= "&closed=$form->{closed}";
1348     $href     .= "&closed=$form->{closed}";
1349     $option   .= "\n<br>" if ($option);
1350     $option   .= $locale->text('Closed');
1351   }
1352   if ($form->{globalproject_id}) {
1353     $callback .= "&globalproject_id=" . E($form->{globalproject_id});
1354     $href     .= "&globalproject_id=" . E($form->{globalproject_id});
1355   }
1356
1357   @columns =
1358     qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
1359        due duedate notes employee globalprojectnumber);
1360
1361   $form->{"l_type"} = "Y";
1362
1363   foreach $item (@columns) {
1364     if ($form->{"l_$item"} eq "Y") {
1365       push @column_index, $item;
1366
1367       # add column to href and callback
1368       $callback .= "&l_$item=Y";
1369       $href     .= "&l_$item=Y";
1370     }
1371   }
1372
1373   if ($form->{l_subtotal} eq 'Y') {
1374     $callback .= "&l_subtotal=Y";
1375     $href     .= "&l_subtotal=Y";
1376   }
1377
1378   $column_header{id} =
1379       qq|<th><a class=listheading href=$href&sort=id>|
1380     . $locale->text('ID')
1381     . qq|</a></th>|;
1382   $column_header{transdate} =
1383       qq|<th><a class=listheading href=$href&sort=transdate>|
1384     . $locale->text('Date')
1385     . qq|</a></th>|;
1386   $column_header{type} =
1387       "<th class=\"listheading\">" . $locale->text('Type') . "</th>";
1388   $column_header{duedate} =
1389       qq|<th><a class=listheading href=$href&sort=duedate>|
1390     . $locale->text('Due Date')
1391     . qq|</a></th>|;
1392   $column_header{due} =
1393     qq|<th class=listheading>| . $locale->text('Amount Due') . qq|</th>|;
1394   $column_header{invnumber} =
1395       qq|<th><a class=listheading href=$href&sort=invnumber>|
1396     . $locale->text('Invoice')
1397     . qq|</a></th>|;
1398   $column_header{ordnumber} =
1399       qq|<th><a class=listheading href=$href&sort=ordnumber>|
1400     . $locale->text('Order')
1401     . qq|</a></th>|;
1402   $column_header{name} =
1403       qq|<th><a class=listheading href=$href&sort=name>|
1404     . $locale->text('Vendor')
1405     . qq|</a></th>|;
1406   $column_header{netamount} =
1407     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1408   $column_header{tax} =
1409     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1410   $column_header{amount} =
1411     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1412   $column_header{paid} =
1413     qq|<th class=listheading>| . $locale->text('Paid') . qq|</th>|;
1414   $column_header{datepaid} =
1415       qq|<th><a class=listheading href=$href&sort=datepaid>|
1416     . $locale->text('Date Paid')
1417     . qq|</a></th>|;
1418   $column_header{notes} =
1419     qq|<th class=listheading>| . $locale->text('Notes') . qq|</th>|;
1420   $column_header{employee} =
1421     "<th><a class=listheading href=$href&sort=employee>"
1422     . $locale->text('Employee') . "</th>";
1423   $column_header{globalprojectnumber} =
1424     qq|<th class="listheading">| . $locale->text('Project Number') . qq|</th>|;
1425
1426   $form->{title} = $locale->text('AP Transactions');
1427
1428   $form->header;
1429
1430   print qq|
1431 <body>
1432
1433 <table width=100%>
1434   <tr>
1435     <th class=listtop>$form->{title}</th>
1436   </tr>
1437   <tr height="5"></tr>
1438   <tr>
1439     <td>$option</td>
1440   </tr>
1441   <tr>
1442     <td>
1443       <table width=100%>
1444         <tr class=listheading>
1445 |;
1446
1447   map { print "\n$column_header{$_}" } @column_index;
1448
1449   print qq|
1450         </tr>
1451 |;
1452
1453   # add sort and escape callback
1454   $form->{callback} = "$callback&sort=$form->{sort}";
1455   $callback = $form->escape($form->{callback});
1456
1457   if (@{ $form->{AP} }) {
1458     $sameitem = $form->{AP}->[0]->{ $form->{sort} };
1459   }
1460
1461   # sums and tax on reports by Antonio Gallardo
1462   #
1463   foreach $ap (@{ $form->{AP} }) {
1464
1465     if ($form->{l_subtotal} eq 'Y') {
1466       if ($sameitem ne $ap->{ $form->{sort} }) {
1467         &ap_subtotal;
1468         $sameitem = $ap->{ $form->{sort} };
1469       }
1470     }
1471
1472     $column_data{netamount} =
1473         "<td align=right>"
1474       . $form->format_amount(\%myconfig, $ap->{netamount}, 2, "&nbsp;")
1475       . "</td>";
1476     $column_data{tax} = "<td align=right>"
1477       . $form->format_amount(\%myconfig, $ap->{amount} - $ap->{netamount},
1478                              2, "&nbsp;")
1479       . "</td>";
1480     $column_data{amount} =
1481       "<td align=right>"
1482       . $form->format_amount(\%myconfig, $ap->{amount}, 2, "&nbsp;") . "</td>";
1483     $column_data{paid} =
1484       "<td align=right>"
1485       . $form->format_amount(\%myconfig, $ap->{paid}, 2, "&nbsp;") . "</td>";
1486     $column_data{due} = "<td align=right>"
1487       . $form->format_amount(\%myconfig, $ap->{amount} - $ap->{paid},
1488                              2, "&nbsp;")
1489       . "</td>";
1490
1491     $totalnetamount += $ap->{netamount};
1492     $totalamount    += $ap->{amount};
1493     $totalpaid      += $ap->{paid};
1494     $totaldue       += ($ap->{amount} - $ap->{paid});
1495
1496     $subtotalnetamount += $ap->{netamount};
1497     $subtotalamount    += $ap->{amount};
1498     $subtotalpaid      += $ap->{paid};
1499     $subtotaldue       += ($ap->{amount} - $ap->{paid});
1500
1501     $column_data{transdate} = "<td>$ap->{transdate}&nbsp;</td>";
1502     $column_data{type} = "<td>" .
1503       ($ap->{invoice}    ? $locale->text("Invoice (one letter abbreviation)") :
1504                            $locale->text("AP Transaction (abbreviation)"))
1505         . "</td>";
1506     $column_data{duedate}   = "<td>$ap->{duedate}&nbsp;</td>";
1507     $column_data{datepaid}  = "<td>$ap->{datepaid}&nbsp;</td>";
1508
1509     $module = ($ap->{invoice}) ? "ir.pl" : $form->{script};
1510
1511     $column_data{invnumber} =
1512       qq|<td><a href="$module?action=edit&id=$ap->{id}&login=$form->{login}&password=$form->{password}&callback=$callback">$ap->{invnumber}</a></td>|;
1513     $column_data{id}        = "<td>$ap->{id}</td>";
1514     $column_data{ordnumber} = "<td>$ap->{ordnumber}&nbsp;</td>";
1515     $column_data{name}      = "<td>$ap->{name}</td>";
1516     $ap->{notes} =~ s/\r\n/<br>/g;
1517     $column_data{notes}    = "<td>$ap->{notes}&nbsp;</td>";
1518     $column_data{employee} = "<td>$ap->{employee}&nbsp;</td>";
1519     $column_data{globalprojectnumber}  =
1520       "<td>" . H($ap->{globalprojectnumber}) . "</td>";
1521
1522     $i++;
1523     $i %= 2;
1524     print "
1525         <tr class=listrow$i >
1526 ";
1527
1528     map { print "\n$column_data{$_}" } @column_index;
1529
1530     print qq|
1531         </tr>
1532 |;
1533
1534   }
1535
1536   if ($form->{l_subtotal} eq 'Y') {
1537     &ap_subtotal;
1538   }
1539
1540   # print totals
1541   print qq|
1542         <tr class=listtotal>
1543 |;
1544
1545   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1546
1547   $column_data{netamount} =
1548     "<th class=listtotal align=right>"
1549     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1550   $column_data{tax} = "<th class=listtotal align=right>"
1551     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1552                            2, "&nbsp;")
1553     . "</th>";
1554   $column_data{amount} =
1555     "<th class=listtotal align=right>"
1556     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1557   $column_data{paid} =
1558     "<th class=listtotal align=right>"
1559     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;") . "</th>";
1560   $column_data{due} =
1561     "<th class=listtotal align=right>"
1562     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;") . "</th>";
1563
1564   map { print "$column_data{$_}\n" } @column_index;
1565
1566   print qq|
1567         </tr>
1568       </table>
1569     </td>
1570   </tr>
1571   <tr>
1572     <td><hr size=3 noshade></td>
1573   </tr>
1574 </table>
1575
1576 <br>
1577 <form method=post action=$form->{script}>
1578
1579 <input name=callback type=hidden value="$form->{callback}">
1580
1581 <input type=hidden name=login value=$form->{login}>
1582 <input type=hidden name=password value=$form->{password}>
1583
1584 <input class=submit type=submit name=action value="|
1585     . $locale->text('AP Transaction') . qq|">
1586
1587 <input class=submit type=submit name=action value="|
1588     . $locale->text('Vendor Invoice') . qq|">
1589
1590   </form>
1591
1592 </body>
1593 </html>
1594 |;
1595
1596   $lxdebug->leave_sub();
1597 }
1598
1599 sub ap_subtotal {
1600   $lxdebug->enter_sub();
1601
1602   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1603
1604   $column_data{netamount} =
1605       "<th class=listsubtotal align=right>"
1606     . $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")
1607     . "</th>";
1608   $column_data{tax} = "<th class=listsubtotal align=right>"
1609     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1610                            2, "&nbsp;")
1611     . "</th>";
1612   $column_data{amount} =
1613     "<th class=listsubtotal align=right>"
1614     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1615   $column_data{paid} =
1616     "<th class=listsubtotal align=right>"
1617     . $form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;") . "</th>";
1618   $column_data{due} =
1619     "<th class=listsubtotal align=right>"
1620     . $form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;") . "</th>";
1621
1622   $subtotalnetamount = 0;
1623   $subtotalamount    = 0;
1624   $subtotalpaid      = 0;
1625   $subtotaldue       = 0;
1626
1627   print "<tr class=listsubtotal>";
1628
1629   map { print "\n$column_data{$_}" } @column_index;
1630
1631   print qq|
1632   </tr>
1633 |;
1634
1635   $lxdebug->leave_sub();
1636 }
1637
1638 sub storno {
1639   $lxdebug->enter_sub();
1640
1641   if (IS->has_storno(\%myconfig, $form, 'ap')) {
1642     $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1643     $form->error($locale->text("Transaction has already been cancelled!"));
1644   }
1645
1646   # negate amount/taxes
1647   for my $i (1 .. $form->{rowcount}) {
1648     $form->{"amount_$i"} *= -1;
1649     $form->{"tax_$i"}    *= -1; 
1650   }
1651
1652   # format things
1653   for my $i (1 .. $form->{rowcount}) {
1654     for (qw(amount tax)) {
1655       $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) if $form->{"${_}_$i"};
1656     }
1657   }
1658
1659   $form->{storno}      = 1;
1660   $form->{storno_id}   = $form->{id};
1661   $form->{id}          = 0;
1662
1663   $form->{invnumber}   = "Storno-" . $form->{invnumber};
1664
1665   post();
1666
1667   # saving the history
1668   if(!exists $form->{addition} && $form->{id} ne "") {
1669     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1670     $form->{addition} = "STORNO";
1671     $form->save_history($form->dbconnect(\%myconfig));
1672   }
1673   # /saving the history 
1674
1675   $lxdebug->leave_sub();
1676 }