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