b3efdec57d25e0cac2dcd1763300ddb455a32f17
[kivitendo-erp.git] / bin / mozilla / cp.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) 2002
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 # Payment module
31 #
32 #======================================================================
33
34 use SL::CP;
35 use SL::IS;
36 use SL::IR;
37 use SL::AR;
38 use SL::AP;
39 use strict ("vars", "subs");
40 #use warnings;
41
42 require "bin/mozilla/arap.pl";
43 require "bin/mozilla/common.pl";
44
45 our ($form, %myconfig, $lxdebug, $locale, $auth);
46
47 1;
48
49 # end of main
50
51 sub payment {
52   $lxdebug->enter_sub();
53
54   $auth->assert('cash');
55
56   my (@curr);
57
58   $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
59   $form->{arap} = lc $form->{ARAP};
60
61   # setup customer/vendor selection for open invoices
62   if ($form->{all_vc}) {
63     # Dieser Zweig funktioniert derzeit NIE. Ggf. ganz raus oder
64     # alle offenen Zahlungen wieder korrekt anzeigen. jb 12.10.2010
65     $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
66   } else {
67     CP->get_openvc(\%myconfig, \%$form);
68   }
69
70   $form->{"select$form->{vc}"} = "";
71
72   if ($form->{"all_$form->{vc}"}) {
73     # s.o. jb 12.10.2010
74     $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
75     map { $form->{"select$form->{vc}"} .= "<option>$_->{name}--$_->{id}\n" }
76       @{ $form->{"all_$form->{vc}"} };
77   }
78
79   CP->paymentaccounts(\%myconfig, \%$form);
80
81   $form->{selectaccount} = "";
82   $form->{"select$form->{ARAP}"} = "";
83
84   map { $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n" }
85     @{ $form->{PR}{"$form->{ARAP}_paid"} };
86   map {
87     $form->{"select$form->{ARAP}"} .=
88       "<option>$_->{accno}--$_->{description}\n"
89   } @{ $form->{PR}{ $form->{ARAP} } };
90
91   # currencies
92   # oldcurrency ist zwar noch hier als fragment enthalten, wird aber bei
93   # der aktualisierung der form auch nicht mitübernommen. das konzept
94   # old_$FOO habe ich auch noch nicht verstanden ...
95   # Ok. Wenn currency übernommen werden, dann in callback-string über-
96   # geben und hier reinparsen, oder besser multibox oder html auslagern?
97   # Antwort: form->currency wird mit oldcurrency oder curr[0] überschrieben
98   # Wofür macht das Sinn?
99   @curr = split(/:/, $form->{currencies});
100   chomp $curr[0];
101   $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} =
102     $curr[0];
103
104   $form->{selectcurrency} = "";
105   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
106
107
108   &form_header;
109   &form_footer;
110
111   $lxdebug->leave_sub();
112 }
113
114 sub form_header {
115   $lxdebug->enter_sub();
116
117   $auth->assert('cash');
118
119   my ($vc, $vclabel, $allvc, $arap, $exchangerate);
120   my ($jsscript, $button1, $button2, $onload);
121
122   $vclabel = ucfirst $form->{vc};
123   $vclabel = $locale->text($vclabel);
124
125   if ($form->{type} eq 'receipt') {
126     $form->{title}     = $locale->text('Receipt');
127     $form->{origtitle} = "Receipt";
128   }
129   if ($form->{type} eq 'check') {
130     $form->{title}     = $locale->text('Payment');
131     $form->{origtitle} = "Payment";
132   }
133
134   # $locale->text('Customer')
135   # $locale->text('Vendor')
136
137   if ($form->{ $form->{vc} } eq "") {
138     map { $form->{"addr$_"} = "" } (1 .. 4);
139   }
140
141   if ($form->{currency} ne $form->{defaultcurrency}) {
142     $form->{exchangerate} =
143       $form->format_amount(\%myconfig, $form->{exchangerate});
144     if ($form->{forex}) {
145       $exchangerate = qq|
146               <tr>
147                 <th align=right nowrap>| . $locale->text('Exchangerate') . qq|</th>
148                 <td colspan=3><input type=hidden name=exchangerate size=10 value=$form->{exchangerate}>$form->{exchangerate}</td>
149               </tr>
150 |;
151     } else {
152       $exchangerate = qq|
153                <tr>
154                 <th align=right nowrap>| . $locale->text('Exchangerate') . qq|</th>
155                 <td colspan=3><input name=exchangerate size=10 value=$form->{exchangerate}></td>
156               </tr>
157 |;
158     }
159   }
160   foreach my $item ($form->{vc}, "account", "currency", $form->{ARAP}) {
161     $form->{"select$item"} =~ s/ selected//;
162     $form->{"select$item"} =~
163       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
164   }
165
166   $vc =
167     ($form->{"select$form->{vc}"})
168     ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>|
169     : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
170
171   if ($form->{all_vc}) {
172     $allvc = "checked";
173     $form->{openinvoices} = "";
174   } else {
175     $allvc = "";
176     $form->{openinvoices} = 1;
177   }
178
179   # $locale->text('AR')
180   # $locale->text('AP')
181
182   $form->{jsscript} = 1;
183   $jsscript = "";
184   if ($form->{jsscript}) {
185
186     # with JavaScript Calendar
187     $button1 = qq|
188        <td><input name=datepaid id=datepaid size=11 title="$myconfig{dateformat}" value="$form->{datepaid}" onBlur=\"check_right_date_format(this)\">
189        <input type=button name=datepaid id="trigger1" value=|
190       . $locale->text('button') . qq|></td>
191        |;
192
193     #write Trigger
194     $jsscript =
195       Form->write_trigger(\%myconfig, "1", "datepaid", "BL", "trigger1");
196   } else {
197
198     # without JavaScript Calendar
199     $button1 = qq|
200                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
201   }
202   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
203   $form->header;
204
205   $arap = lc $form->{ARAP};
206   $onload = qq|focus()|;
207   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
208   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
209   print qq|
210 <body onLoad="$onload">
211
212 <form method=post action=cp.pl>
213
214 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
215 <input type=hidden name=closedto value=$form->{closedto}>
216 <input type=hidden name=vc value=$form->{vc}>
217 <input type=hidden name=type value=$form->{type}>
218 <input type=hidden name=formname value=$form->{type}>
219 <input type=hidden name=queued value="$form->{queued}">
220 <input type=hidden name=arap value=$arap>
221 <input type=hidden name=ARAP value=$form->{ARAP}>
222 <input type=hidden name=openinvoices value=$form->{openinvoices}>
223 <input type=hidden name=title value="$form->{title}">
224 <input type=hidden name=origtitle value="$form->{origtitle}">
225
226 <table width=100%>
227   <tr>
228     <th class=listtop>$form->{title}</th>
229   </tr>
230   <tr height="5"></tr>
231   <tr>
232     <td>
233       <table width=100%>
234         <tr valign=top>
235           <td>
236             <table>
237               <tr>
238                 <th align=right>$vclabel</th>
239                 <td>$vc</td>
240                 <input type=hidden name="select$form->{vc}" value="| . H($form->{"select$form->{vc}"}) . qq|">
241                 <input type=hidden name="$form->{vc}_id" value="|    . H($form->{"$form->{vc}_id"}) . qq|">
242                 <input type=hidden name="old$form->{vc}" value="|    . H($form->{"old$form->{vc}"}) . qq|">
243               </tr>
244               <tr>
245                 <th align=right>| . $locale->text('Invoice Number') . qq|</th>
246                 <td><input name="invnumber" size="35"</td>
247               </tr>
248               <tr valign=top>
249                 <th align=right nowrap>| . $locale->text('Address') . qq|</th>
250                 <td colspan=2>
251                   <table>
252                     <tr>
253                       <td>$form->{street}</td>
254                     </tr>
255                     <tr>
256                       <td>$form->{zipcode}</td>
257                     </tr>
258                     <tr>
259                       <td>$form->{city}</td>
260                     </tr>
261                     <tr>
262                       <td>$form->{country}</td>
263                     </tr>
264                   </table>
265                 </td>
266                 <input type=hidden name=street value="$form->{street}">
267                 <input type=hidden name=zipcode value="$form->{zipcode}">
268                 <input type=hidden name=city value="$form->{city}">
269                 <input type=hidden name=country value="$form->{country}">
270               </tr>
271               <tr>
272                 <th align=right>| . $locale->text('Memo') . qq|</th>
273                 <td colspan=2><input name="memo" size=30 value="$form->{memo}"></td>
274               </tr>
275             </table>
276           </td>
277           <td align=right>
278             <table>
279               <tr>
280                 <th align=right nowrap>| . $locale->text('Account') . qq|</th>
281                 <td colspan=3><select name=account>$form->{selectaccount}</select>
282                 <input type=hidden name=selectaccount value="$form->{selectaccount}">
283                 </td>
284               </tr>
285               <tr>
286                 <th align=right nowrap>| . $locale->text('Date') . qq|</th>
287                 $button1
288               </tr>
289               <tr>
290                 <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
291                 <td><select name=currency>$form->{selectcurrency}</select></td>
292                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
293                 <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
294               </tr>
295               $exchangerate
296               <tr>
297                 <th align=right nowrap>| . $locale->text('Source') . qq|</th>
298                 <td colspan=3><input name=source value="$form->{source}" size=10></td>
299               </tr>
300               <tr>
301                 <th align="right" nowrap>| . $locale->text('Amount') . qq|</th>
302                 <td colspan="3">| .  $form->format_amount(\%myconfig, $form->{amount}, 2) . qq|</td>
303               </tr>
304             </table>
305           </td>
306         </tr>
307       </table>
308     </td>
309   </tr>
310
311 $jsscript
312 |;
313
314   if ($form->{openinvoices_other_currencies}) {
315     my $warning = $form->{vc} eq 'customer' ? $::locale->text('There are #1 more open invoices for this customer with other currencies.', $form->{openinvoices_other_currencies})
316                 :                             $::locale->text('There are #1 more open invoices from this vendor with other currencies.',  $form->{openinvoices_other_currencies});
317
318     print qq|
319
320   <input type="hidden" name="openinvoices_other_currencies" value="| . H($form->{openinvoices_other_currencies}) . qq|">
321   <tr>
322    <td><b>| . $::locale->text('Note') . qq|: $warning</b></td>
323   </tr>
324 |;
325   }
326
327   $lxdebug->leave_sub();
328 }
329
330 sub list_invoices {
331   $lxdebug->enter_sub();
332
333   $auth->assert('cash');
334
335   my (@column_index, %column_data, $colspan, $invoice);
336   my ($totalamount, $totaldue, $totalpaid);
337
338   @column_index = qw(invnumber transdate amount due checked paid);
339
340   $colspan = $#column_index + 1;
341
342   $invoice = $locale->text('Invoices');
343
344   print qq|
345   <input type=hidden name=column_index value="id @column_index">
346   <tr>
347     <td>
348       <table width=100%>
349         <tr>
350           <th class=listheading colspan=$colspan>$invoice</th>
351         </tr>
352 |;
353
354   $column_data{invnumber} =
355     qq|<th nowrap class=listheading>| . $locale->text('Invoice') . "</th>";
356   $column_data{transdate} =
357     qq|<th nowrap class=listheading>| . $locale->text('Date') . "</th>";
358   $column_data{amount} =
359     qq|<th nowrap class=listheading>| . $locale->text('Amount') . "</th>";
360   $column_data{due} =
361     qq|<th nowrap class=listheading>| . $locale->text('Due') . "</th>";
362   $column_data{paid} =
363     qq|<th nowrap class=listheading>| . $locale->text('Amount') . "</th>";
364   $column_data{checked} =
365     qq|<th nowrap class=listheading>| . $locale->text('Select') . "</th>";
366
367   print qq|
368         <tr>
369 |;
370   map { print "$column_data{$_}\n" } @column_index;
371   print qq|
372         </tr>
373 |;
374
375   for my $i (1 .. $form->{rowcount}) {
376
377     my $j = 0;
378
379     map {
380       $form->{"${_}_$i"} =
381         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
382     } qw(amount due paid);
383
384     $totalamount += $form->{"amount_$i"};
385     $totaldue    += $form->{"due_$i"};
386     $totalpaid   += $form->{"paid_$i"};
387
388     map {
389       $form->{"${_}_$i"} =
390         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
391     } qw(amount due paid);
392
393     $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
394       <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
395       <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
396     $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
397       <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
398     $column_data{amount} =
399       qq|<td align=right width=15%>$form->{"amount_$i"}</td>
400       <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
401     $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
402       <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
403
404     $column_data{paid} =
405       qq|<td align=right width=15%><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
406
407     $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
408     $column_data{checked} =
409       qq|<td align=center width=10%><input name="checked_$i" type=checkbox style=checkbox $form->{"checked_$i"}></td>|;
410
411     $j++;
412     $j %= 2;
413     print qq|
414         <tr class=listrow$j>
415 |;
416     map { print "$column_data{$_}\n" } @column_index;
417     print qq|
418         </tr>
419 |;
420   }
421
422   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
423
424   $column_data{amount} =
425       qq|<th class=listtotal align=right>|
426     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;")
427     . qq|</th>|;
428   $column_data{due} =
429       qq|<th class=listtotal align=right>|
430     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;")
431     . qq|</th>|;
432   $column_data{paid} =
433       qq|<th class=listtotal align=right>|
434     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;")
435     . qq|</th>|;
436
437   print qq|
438         <tr class=listtotal>
439 |;
440   map { print "$column_data{$_}\n" } @column_index;
441   print qq|
442         </tr>
443       </table>
444     </td>
445   </tr>
446 |;
447
448   $lxdebug->leave_sub();
449 }
450
451 sub form_footer {
452   $lxdebug->enter_sub();
453
454   $auth->assert('cash');
455
456   print qq|
457   <tr>
458     <td><hr size=3 noshade></td>
459   </tr>
460 </table>
461 <input type=hidden name=rowcount value=$form->{rowcount}>
462
463 <br>
464 <input class=submit type=submit name=action value="|
465     . $locale->text('Update') . qq|">
466 <input class=submit type=submit name=action value="|
467     . $locale->text('Post') . qq|">
468  </form>
469
470 </body>
471 </html>
472 |;
473
474   $lxdebug->leave_sub();
475 }
476
477 sub update {
478   $lxdebug->enter_sub();
479
480   $auth->assert('cash');
481
482   my ($new_name_selected) = @_;
483
484   my ($buysell, $newvc, $updated, $exchangerate, $amount);
485
486   if ($form->{vc} eq 'customer') {
487     $buysell = "buy";
488   } else {
489     $buysell = "sell";
490   }
491
492   # if we switched to all_vc
493   # funktioniert derzeit nicht 12.10.2010 jb
494   if ($form->{all_vc} ne $form->{oldall_vc}) {
495
496     $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
497
498     $form->{"select$form->{vc}"} = "";
499
500     if ($form->{all_vc}) {
501       $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
502
503       if ($form->{"all_$form->{vc}"}) {
504         map {
505           $form->{"select$form->{vc}"} .=
506             "<option>$_->{name}--$_->{id}\n"
507         } @{ $form->{"all_$form->{vc}"} };
508       }
509     } else {  # ab hier wieder ausgeführter code (s.o.):
510       CP->get_openvc(\%myconfig, \%$form);
511
512       if ($form->{"all_$form->{vc}"}) {
513         $newvc =
514           qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
515         map {
516           $form->{"select$form->{vc}"} .=
517             "<option>$_->{name}--$_->{id}\n"
518         } @{ $form->{"all_$form->{vc}"} };
519       }
520
521       # if the name is not the same
522       if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
523         $form->{ $form->{vc} } = $newvc;
524       }
525     }
526   }
527   # Falls Suche über Rechnungsnummer und kein Kundenname vorhanden
528   if ($form->{invnumber} && !($form->{$form->{vc}})){
529   $form->{open} ='Y'; # nur die offenen rechnungen
530   if ($form->{ARAP} eq 'AR'){
531     AR->ar_transactions(\%myconfig, \%$form);
532     # den ersten treffen nehmen und mit dem namen überschreiben
533     $form->{$form->{vc}} = $form->{AR}[0]{name};
534   } else {
535     # s.o. nur für zahlungsausgang
536     AP->ap_transactions(\%myconfig, \%$form);
537     $form->{$form->{vc}} = $form->{AP}[0]{name};
538     }
539   }
540   # get customer and invoices
541   $updated = &check_name($form->{vc});
542
543   if ($new_name_selected || $updated) {
544     CP->get_openinvoices(\%myconfig, \%$form);
545     ($newvc) = split /--/, $form->{ $form->{vc} };
546     $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
547     $updated = 1;
548   }
549
550   if ($form->{currency} ne $form->{oldcurrency}) {
551     $form->{oldcurrency} = $form->{currency};
552     if (!$updated) {
553       CP->get_openinvoices(\%myconfig, \%$form);
554       $updated = 1;
555     }
556   }
557
558   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{datepaid}, $buysell);
559   $form->{exchangerate} = $form->{forex} if $form->{forex};
560
561   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
562
563   if ($updated) {
564     $form->{rowcount} = 0;
565
566     $form->{queued} = "";
567
568     my $i = 0;
569     foreach my $ref (@{ $form->{PR} }) {
570       $i++;
571       $form->{"id_$i"}        = $ref->{id};
572       $form->{"invnumber_$i"} = $ref->{invnumber};
573       $form->{"transdate_$i"} = $ref->{transdate};
574       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
575       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
576       $form->{"due_$i"}    =
577         ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
578       $form->{"checked_$i"} = "";
579       $form->{"paid_$i"}    = "";
580
581       # need to format
582       map {
583         $form->{"${_}_$i"} =
584           $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
585       } qw(amount due);
586
587     }
588     $form->{rowcount} = $i;
589   }
590
591   # recalculate
592
593   # Modified from $amount = $form->{amount} by J.Zach to update amount to total
594   # payment amount in Zahlungsausgang
595   $amount = 0;
596   for my $i (1 .. $form->{rowcount}) {
597
598     map {
599       $form->{"${_}_$i"} =
600         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
601     } qw(amount due paid);
602
603     if ($form->{"checked_$i"}) {
604
605       # calculate paid_$i
606       if (!$form->{"paid_$i"}) {
607         $form->{"paid_$i"} = $form->{"due_$i"};
608       }
609
610       # Modified by J.Zach, see abovev
611       $amount += $form->{"paid_$i"};
612
613     } else {
614       $form->{"paid_$i"} = "";
615     }
616
617     map {
618       $form->{"${_}_$i"} =
619         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
620     } qw(amount due paid);
621
622   }
623
624   # Line added by J.Zach, see above
625   $form->{amount}=$amount;
626
627   &form_header;
628   &list_invoices;
629   &form_footer;
630
631   $lxdebug->leave_sub();
632 }
633
634 sub post {
635   $lxdebug->enter_sub();
636
637   $auth->assert('cash');
638
639   &check_form;
640
641   if ($form->{currency} ne $form->{defaultcurrency}) {
642     $form->error($locale->text('Exchangerate missing!'))
643       unless $form->{exchangerate};
644   }
645
646   # Beim Aktualisieren wird das Konto übernommen
647   # und jetzt auch Beleg und Datum
648   $form->{callback} = "cp.pl?action=payment&vc=$form->{vc}&type=$form->{type}&account=$form->{account}&$form->{currency}" . 
649                       "&datepaid=$form->{datepaid}&source=$form->{source}";
650
651   my $msg1 = "$form->{origtitle} posted!";
652   my $msg2 = "Cannot post $form->{origtitle}!";
653
654   # $locale->text('Payment posted!')
655   # $locale->text('Receipt posted!')
656   # $locale->text('Cannot post Payment!')
657   # $locale->text('Cannot post Receipt!')
658   # Die Nachrichten (Receipt posted!) werden nicht angezeigt.
659   # Entweder wieder aktivieren oder komplett rausnehmen
660   $form->redirect($locale->text($msg1))
661     if (CP->process_payment(\%myconfig, \%$form));
662   $form->error($locale->text($msg2));
663
664   $lxdebug->leave_sub();
665 }
666
667 sub check_form {
668   $lxdebug->enter_sub();
669
670   $auth->assert('cash');
671
672   my ($closedto, $datepaid, $amount);
673
674   &check_name($form->{vc});
675
676   if ($form->{currency} ne $form->{oldcurrency}) {
677     &update;
678     ::end_of_request();
679   }
680   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
681   my $selected_check = 1; 
682   for my $i (1 .. $form->{rowcount}) {
683     if ($form->{"checked_$i"}) {
684       if ($form->parse_amount(\%myconfig, $form->{"paid_$i"}, 2) <= 0) { # negativen Betrag eingegeben
685           $form->error($locale->text('Amount has to be greater then zero! Wrong row number: ') . $i);
686       }
687         undef($selected_check);
688         # last; # ich muss doch über alle buchungen laufen, da ich noch
689         # die freitext-eingabe der werte prüfen will 
690     }
691   }
692   $form->error($locale->text('No transaction selected!')) if $selected_check;
693
694   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
695   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
696
697   $form->error($locale->text('Cannot process payment for a closed period!'))
698     if ($form->date_closed($form->{"datepaid"}, \%myconfig));
699
700   $amount = $form->parse_amount(\%myconfig, $form->{amount});
701   $form->{amount} = $amount;
702
703   for my $i (1 .. $form->{rowcount}) {
704     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
705       $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
706
707       push(@{ $form->{paid} },      $form->{"paid_$i"});
708       push(@{ $form->{due} },       $form->{"due_$i"});
709       push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
710       push(@{ $form->{invdate} },   $form->{"transdate_$i"});
711     }
712   }
713
714   if ($form->round_amount($amount, 2) != 0) {
715     push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
716     push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
717     push(@{ $form->{invnumber} },
718          ($form->{ARAP} eq 'AR')
719          ? $locale->text('Deposit')
720          : $locale->text('Prepayment'));
721     push(@{ $form->{invdate} }, $form->{datepaid});
722   }
723
724   $lxdebug->leave_sub();
725 }