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