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