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