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