1c37e41eecbf206910672b598aebd8521f09d6ea
[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 strict ("vars", "subs");
38 #use warnings;
39
40 require "bin/mozilla/arap.pl";
41 require "bin/mozilla/common.pl";
42
43 our ($form, %myconfig, $lxdebug, $locale, $auth);
44
45 1;
46
47 # end of main
48
49 sub payment {
50   $lxdebug->enter_sub();
51
52   $auth->assert('cash');
53
54   my (@curr);
55
56   $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
57   $form->{arap} = lc $form->{ARAP};
58
59   # setup customer/vendor selection for open invoices
60   if ($form->{all_vc}) {
61     $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
62   } else {
63     CP->get_openvc(\%myconfig, \%$form);
64   }
65
66   $form->{"select$form->{vc}"} = "";
67
68   if ($form->{"all_$form->{vc}"}) {
69     $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
70     map { $form->{"select$form->{vc}"} .= "<option>$_->{name}--$_->{id}\n" }
71       @{ $form->{"all_$form->{vc}"} };
72   }
73
74   # departments
75   if (@{ $form->{all_departments} || [] }) {
76     $form->{selectdepartment} = "<option>\n";
77     $form->{department}       = "$form->{department}--$form->{department_id}";
78
79     map {
80       $form->{selectdepartment} .=
81         "<option>$_->{description}--$_->{id}\n"
82     } (@{ $form->{all_departments} || [] });
83   }
84
85   CP->paymentaccounts(\%myconfig, \%$form);
86
87   $form->{selectaccount} = "";
88   $form->{"select$form->{ARAP}"} = "";
89
90   map { $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n" }
91     @{ $form->{PR}{"$form->{ARAP}_paid"} };
92   map {
93     $form->{"select$form->{ARAP}"} .=
94       "<option>$_->{accno}--$_->{description}\n"
95   } @{ $form->{PR}{ $form->{ARAP} } };
96
97   # currencies
98   @curr = split(/:/, $form->{currencies});
99   chomp $curr[0];
100   $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} =
101     $curr[0];
102
103   $form->{selectcurrency} = "";
104   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
105
106   $form->{media} = "screen";
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, $department, $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
161   foreach my $item ($form->{vc}, "account", "currency", $form->{ARAP}, "department") {
162     $form->{"select$item"} =~ s/ selected//;
163     $form->{"select$item"} =~
164       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
165   }
166
167   $vc =
168     ($form->{"select$form->{vc}"})
169     ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>|
170     : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
171
172   if ($form->{all_vc}) {
173     $allvc = "checked";
174     $form->{openinvoices} = "";
175   } else {
176     $allvc = "";
177     $form->{openinvoices} = 1;
178   }
179
180   # $locale->text('AR')
181   # $locale->text('AP')
182
183   $form->{jsscript} = 1;
184   $jsscript = "";
185   if ($form->{jsscript}) {
186
187     # with JavaScript Calendar
188     $button1 = qq|
189        <td><input name=datepaid id=datepaid size=11 title="$myconfig{dateformat}" value="$form->{datepaid}" onBlur=\"check_right_date_format(this)\">
190        <input type=button name=datepaid id="trigger1" value=|
191       . $locale->text('button') . qq|></td>
192        |;
193
194     #write Trigger
195     $jsscript =
196       Form->write_trigger(\%myconfig, "1", "datepaid", "BL", "trigger1");
197   } else {
198
199     # without JavaScript Calendar
200     $button1 = qq|
201                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
202   }
203   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
204   $form->header;
205
206   $arap = lc $form->{ARAP};
207   $onload = qq|focus()|;
208   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
209   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
210   print qq|
211 <body onLoad="$onload">
212
213 <form method=post action=cp.pl>
214
215 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
216 <input type=hidden name=closedto value=$form->{closedto}>
217 <input type=hidden name=vc value=$form->{vc}>
218 <input type=hidden name=type value=$form->{type}>
219 <input type=hidden name=formname value=$form->{type}>
220 <input type=hidden name=queued value="$form->{queued}">
221 <input type=hidden name=arap value=$arap>
222 <input type=hidden name=ARAP value=$form->{ARAP}>
223 <input type=hidden name=openinvoices value=$form->{openinvoices}>
224 <input type=hidden name=title value="$form->{title}">
225 <input type=hidden name=origtitle value="$form->{origtitle}">
226
227 <table width=100%>
228   <tr>
229     <th class=listtop>$form->{title}</th>
230   </tr>
231   <tr height="5"></tr>
232   <tr>
233     <td>
234       <table width=100%>
235         <tr valign=top>
236           <td>
237             <table>
238               <tr>
239                 <td align=right>
240                 <input name=all_vc type=checkbox style=checkbox value=Y $allvc>
241                 <input type=hidden name="oldall_vc" value="$form->{all_vc}"></td>
242                 <th align=left>| . $locale->text('All') . qq|</th>
243               </tr>
244               <tr>
245                 <th align=right>$vclabel</th>
246                 <td>$vc</td>
247                 <input type=hidden name="select$form->{vc}" value="| . H($form->{"select$form->{vc}"}) . qq|">
248                 <input type=hidden name="$form->{vc}_id" value="|    . H($form->{"$form->{vc}_id"}) . qq|">
249                 <input type=hidden name="old$form->{vc}" value="|    . H($form->{"old$form->{vc}"}) . qq|">
250               </tr>
251               <tr valign=top>
252                 <th align=right nowrap>| . $locale->text('Address') . qq|</th>
253                 <td colspan=2>
254                   <table>
255                     <tr>
256                       <td>$form->{street}</td>
257                     </tr>
258                     <tr>
259                       <td>$form->{zipcode}</td>
260                     </tr>
261                     <tr>
262                       <td>$form->{city}</td>
263                     </tr>
264                     <tr>
265                       <td>$form->{country}</td>
266                     </tr>
267                   </table>
268                 </td>
269                 <input type=hidden name=street value="$form->{street}">
270                 <input type=hidden name=zipcode value="$form->{zipcode}">
271                 <input type=hidden name=city value="$form->{city}">
272                 <input type=hidden name=country value="$form->{country}">
273               </tr>
274               <tr>
275                 <th align=right>| . $locale->text('Memo') . qq|</th>
276                 <td colspan=2><input name="memo" size=30 value="$form->{memo}"></td>
277               </tr>
278             </table>
279           </td>
280           <td align=right>
281             <table>
282               $department
283               <tr>
284                 <th align=right nowrap>| . $locale->text('Account') . qq|</th>
285                 <td colspan=3><select name=account>$form->{selectaccount}</select>
286                 <input type=hidden name=selectaccount value="$form->{selectaccount}">
287                 </td>
288               </tr>
289               <tr>
290                 <th align=right nowrap>| . $locale->text('Date') . qq|</th>
291                 $button1
292               </tr>
293               <tr>
294                 <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
295                 <td><select name=currency>$form->{selectcurrency}</select></td>
296                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
297                 <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
298               </tr>
299               $exchangerate
300               <tr>
301                 <th align=right nowrap>| . $locale->text('Source') . qq|</th>
302                 <td colspan=3><input name=source value="$form->{source}" size=10></td>
303               </tr>
304               <tr>
305                 <th align="right" nowrap>| . $locale->text('Amount') . qq|</th>
306                 <td colspan="3"><input name="amount" size="10" value="|
307     . $form->format_amount(\%myconfig, $form->{amount}, 2) . qq|" onBlur=\"check_right_number_format(this)\"></td>
308               </tr>
309             </table>
310           </td>
311         </tr>
312       </table>
313     </td>
314   </tr>
315
316 $jsscript
317 |;
318
319   if ($form->{openinvoices_other_currencies}) {
320     my $warning = $form->{vc} eq 'customer' ? $::locale->text('There are #1 more open invoices for this customer with other currencies.', $form->{openinvoices_other_currencies})
321                 :                             $::locale->text('There are #1 more open invoices from this vendor with other currencies.',  $form->{openinvoices_other_currencies});
322
323     print qq|
324
325   <input type="hidden" name="openinvoices_other_currencies" value="| . H($form->{openinvoices_other_currencies}) . qq|">
326   <tr>
327    <td><b>| . $::locale->text('Note') . qq|: $warning</b></td>
328   </tr>
329 |;
330   }
331
332   $lxdebug->leave_sub();
333 }
334
335 sub list_invoices {
336   $lxdebug->enter_sub();
337
338   $auth->assert('cash');
339
340   my (@column_index, %column_data, $colspan, $invoice);
341   my ($totalamount, $totaldue, $totalpaid);
342
343   @column_index = qw(invnumber transdate amount due checked paid);
344
345   $colspan = $#column_index + 1;
346
347   $invoice = $locale->text('Invoices');
348
349   print qq|
350   <input type=hidden name=column_index value="id @column_index">
351   <tr>
352     <td>
353       <table width=100%>
354         <tr>
355           <th class=listheading colspan=$colspan>$invoice</th>
356         </tr>
357 |;
358
359   $column_data{invnumber} =
360     qq|<th nowrap class=listheading>| . $locale->text('Invoice') . "</th>";
361   $column_data{transdate} =
362     qq|<th nowrap class=listheading>| . $locale->text('Date') . "</th>";
363   $column_data{amount} =
364     qq|<th nowrap class=listheading>| . $locale->text('Amount') . "</th>";
365   $column_data{due} =
366     qq|<th nowrap class=listheading>| . $locale->text('Due') . "</th>";
367   $column_data{paid} =
368     qq|<th nowrap class=listheading>| . $locale->text('Amount') . "</th>";
369   $column_data{checked} =
370     qq|<th nowrap class=listheading>| . $locale->text('Select') . "</th>";
371
372   print qq|
373         <tr>
374 |;
375   map { print "$column_data{$_}\n" } @column_index;
376   print qq|
377         </tr>
378 |;
379
380   for my $i (1 .. $form->{rowcount}) {
381
382     my $j = 0;
383
384     map {
385       $form->{"${_}_$i"} =
386         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
387     } qw(amount due paid);
388
389     $totalamount += $form->{"amount_$i"};
390     $totaldue    += $form->{"due_$i"};
391     $totalpaid   += $form->{"paid_$i"};
392
393     map {
394       $form->{"${_}_$i"} =
395         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
396     } qw(amount due paid);
397
398     $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
399       <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
400       <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
401     $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
402       <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
403     $column_data{amount} =
404       qq|<td align=right width=15%>$form->{"amount_$i"}</td>
405       <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
406     $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
407       <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
408
409     $column_data{paid} =
410       qq|<td align=right width=15%><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
411
412     $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
413     $column_data{checked} =
414       qq|<td align=center width=10%><input name="checked_$i" type=checkbox style=checkbox $form->{"checked_$i"}></td>|;
415
416     $j++;
417     $j %= 2;
418     print qq|
419         <tr class=listrow$j>
420 |;
421     map { print "$column_data{$_}\n" } @column_index;
422     print qq|
423         </tr>
424 |;
425   }
426
427   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
428
429   $column_data{amount} =
430       qq|<th class=listtotal align=right>|
431     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;")
432     . qq|</th>|;
433   $column_data{due} =
434       qq|<th class=listtotal align=right>|
435     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;")
436     . qq|</th>|;
437   $column_data{paid} =
438       qq|<th class=listtotal align=right>|
439     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;")
440     . qq|</th>|;
441
442   print qq|
443         <tr class=listtotal>
444 |;
445   map { print "$column_data{$_}\n" } @column_index;
446   print qq|
447         </tr>
448       </table>
449     </td>
450   </tr>
451 |;
452
453   $lxdebug->leave_sub();
454 }
455
456 sub form_footer {
457   $lxdebug->enter_sub();
458
459   $auth->assert('cash');
460
461   my ($media, $format, $latex_templates);
462
463   $form->{DF}{ $form->{format} } = "selected";
464   $form->{OP}{ $form->{media} }  = "selected";
465
466   $media = qq|
467           <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
468
469   if ($myconfig{printer} && $latex_templates) {
470     $media .= qq|
471           <option value=printer $form->{OP}{printer}>|
472       . $locale->text('Printer');
473   }
474   if ($latex_templates) {
475     $media .= qq|
476           <option value=queue $form->{OP}{queue}>| . $locale->text('Queue');
477     $format .= qq|
478             <option value=postscript $form->{DF}{postscript}>|
479       . $locale->text('Postscript') . qq|
480             <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF');
481   }
482
483   print qq|
484   <tr>
485     <td><hr size=3 noshade></td>
486   </tr>
487 </table>
488 <input type=hidden name=rowcount value=$form->{rowcount}>
489
490 <br>
491 <input class=submit type=submit name=action value="|
492     . $locale->text('Update') . qq|">
493 <input class=submit type=submit name=action value="|
494     . $locale->text('Post') . qq|">|;
495
496   if ($latex_templates) {
497     print qq|
498 <input class=submit type=submit name=action value="|
499       . $locale->text('Print') . qq|">|;
500   }
501
502   print qq|
503 <select name=format>$format</select>
504 <select name=media>$media</select>
505
506   </form>
507
508 </body>
509 </html>
510 |;
511
512   $lxdebug->leave_sub();
513 }
514
515 sub update {
516   $lxdebug->enter_sub();
517
518   $auth->assert('cash');
519
520   my ($new_name_selected) = @_;
521
522   my ($buysell, $newvc, $updated, $exchangerate, $amount);
523
524   if ($form->{vc} eq 'customer') {
525     $buysell = "buy";
526   } else {
527     $buysell = "sell";
528   }
529
530   # if we switched to all_vc
531   if ($form->{all_vc} ne $form->{oldall_vc}) {
532
533     $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
534
535     $form->{"select$form->{vc}"} = "";
536
537     if ($form->{all_vc}) {
538       $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
539
540       if ($form->{"all_$form->{vc}"}) {
541         map {
542           $form->{"select$form->{vc}"} .=
543             "<option>$_->{name}--$_->{id}\n"
544         } @{ $form->{"all_$form->{vc}"} };
545       }
546     } else {
547       CP->get_openvc(\%myconfig, \%$form);
548
549       if ($form->{"all_$form->{vc}"}) {
550         $newvc =
551           qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
552         map {
553           $form->{"select$form->{vc}"} .=
554             "<option>$_->{name}--$_->{id}\n"
555         } @{ $form->{"all_$form->{vc}"} };
556       }
557
558       # if the name is not the same
559       if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
560         $form->{ $form->{vc} } = $newvc;
561       }
562     }
563   }
564
565   # get customer and invoices
566   $updated = &check_name($form->{vc});
567
568   if ($new_name_selected || $updated) {
569     CP->get_openinvoices(\%myconfig, \%$form);
570     ($newvc) = split /--/, $form->{ $form->{vc} };
571     $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
572     $updated = 1;
573   }
574
575   if ($form->{currency} ne $form->{oldcurrency}) {
576     $form->{oldcurrency} = $form->{currency};
577     if (!$updated) {
578       CP->get_openinvoices(\%myconfig, \%$form);
579       $updated = 1;
580     }
581   }
582
583   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{datepaid}, $buysell);
584   $form->{exchangerate} = $form->{forex} if $form->{forex};
585
586   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
587
588   if ($updated) {
589     $form->{rowcount} = 0;
590
591     $form->{queued} = "";
592
593     my $i = 0;
594     foreach my $ref (@{ $form->{PR} }) {
595       $i++;
596       $form->{"id_$i"}        = $ref->{id};
597       $form->{"invnumber_$i"} = $ref->{invnumber};
598       $form->{"transdate_$i"} = $ref->{transdate};
599       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
600       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
601       $form->{"due_$i"}    =
602         ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
603       $form->{"checked_$i"} = "";
604       $form->{"paid_$i"}    = "";
605
606       # need to format
607       map {
608         $form->{"${_}_$i"} =
609           $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
610       } qw(amount due);
611
612     }
613     $form->{rowcount} = $i;
614   }
615
616   # recalculate
617
618   # Modified from $amount = $form->{amount} by J.Zach to update amount to total
619   # payment amount in Zahlungsausgang
620   $amount = 0;
621   for my $i (1 .. $form->{rowcount}) {
622
623     map {
624       $form->{"${_}_$i"} =
625         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
626     } qw(amount due paid);
627
628     if ($form->{"checked_$i"}) {
629
630       # calculate paid_$i
631       if (!$form->{"paid_$i"}) {
632         $form->{"paid_$i"} = $form->{"due_$i"};
633       }
634
635       # Modified by J.Zach, see abovev
636       $amount += $form->{"paid_$i"};
637
638     } else {
639       $form->{"paid_$i"} = "";
640     }
641
642     map {
643       $form->{"${_}_$i"} =
644         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
645     } qw(amount due paid);
646
647   }
648
649   # Line added by J.Zach, see above
650   $form->{amount}=$amount;
651
652   &form_header;
653   &list_invoices;
654   &form_footer;
655
656   $lxdebug->leave_sub();
657 }
658
659 sub post {
660   $lxdebug->enter_sub();
661
662   $auth->assert('cash');
663
664   &check_form;
665
666   if ($form->{currency} ne $form->{defaultcurrency}) {
667     $form->error($locale->text('Exchangerate missing!'))
668       unless $form->{exchangerate};
669   }
670
671   my $msg1 = "$form->{origtitle} posted!";
672   my $msg2 = "Cannot post $form->{origtitle}!";
673
674   # $locale->text('Payment posted!')
675   # $locale->text('Receipt posted!')
676   # $locale->text('Cannot post Payment!')
677   # $locale->text('Cannot post Receipt!')
678
679   $form->redirect($locale->text($msg1))
680     if (CP->process_payment(\%myconfig, \%$form));
681   $form->error($locale->text($msg2));
682
683   $lxdebug->leave_sub();
684 }
685
686 sub print {
687   $lxdebug->enter_sub();
688
689   $auth->assert('cash');
690
691   my ($whole, $check, %queued, $spool, $filename, $userspath);
692
693   &check_form;
694
695   ($whole, $form->{decimal}) = split(/\./, $form->{amount});
696
697   $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
698
699   #$form->{decimal} .= "00";
700   $form->{decimal} = substr($form->{decimal}, 0, 2);
701
702   $check = new CP $myconfig{countrycode};
703   $check->init;
704   $form->{text_amount} = $check->num2text($whole);
705
706   if ($form->{vc} eq 'customer') {
707     IS->customer_details(\%myconfig, $form);
708   } else {
709     IR->vendor_details(\%myconfig, $form);
710   }
711
712   $form->{callback} = "";
713
714   $form->{templates} = "$myconfig{templates}";
715   $form->{IN}        = "$form->{formname}.tex";
716
717   if ($form->{format} eq 'postscript') {
718     $form->{postscript} = 1;
719   }
720   if ($form->{format} eq 'pdf') {
721     $form->{pdf} = 1;
722   }
723
724   delete $form->{OUT};
725
726   if ($form->{media} eq 'printer') {
727     $form->{OUT} = "| $myconfig{printer}";
728   }
729   if ($form->{media} eq 'queue') {
730     %queued = map { s|.*/|| } split / /, $form->{queued};
731
732     if ($filename = $queued{ $form->{formname} }) {
733       unlink "$spool/$filename";
734       $filename =~ s/\..*$//g;
735     } else {
736       $filename = time;
737       $filename .= $$;
738     }
739     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
740     $form->{queued} = "$form->{formname} $filename";
741     $form->{OUT}    = ">$spool/$filename";
742
743     $form->update_status(\%myconfig);
744
745   }
746
747   $form->{company} = $myconfig{company};
748   $form->{address} = $myconfig{address};
749
750   $form->parse_template(\%myconfig, $userspath);
751
752   if ($form->{media} ne 'screen') {
753     $form->{callback} = "cp.pl?action=payment&vc=$form->{vc}&all_vc=$form->{all_vc}";
754
755     $form->redirect if (CP->process_payment(\%myconfig, \%$form));
756     $form->error($locale->text('Cannot post payment!'));
757   }
758
759   $lxdebug->leave_sub();
760 }
761
762 sub check_form {
763   $lxdebug->enter_sub();
764
765   $auth->assert('cash');
766
767   my ($closedto, $datepaid, $amount);
768
769   &check_name($form->{vc});
770
771   if ($form->{currency} ne $form->{oldcurrency}) {
772     &update;
773     ::end_of_request();
774   }
775   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
776   my $selected_check = 1; 
777   for my $i (1 .. $form->{rowcount}) {
778     if ($form->{"checked_$i"}) {
779       if ($form->parse_amount(\%myconfig, $form->{"paid_$i"}, 2) <= 0) { # negativen Betrag eingegeben
780           $form->error($locale->text('No zero or negative values, please! Correct row number:' . $i));
781       }
782         undef($selected_check);
783         # last; # ich muss doch über alle buchungen laufen, da ich noch
784         # die freitext-eingabe der werte prüfen will 
785     }
786   }
787   $form->error($locale->text('No transaction selected!')) if $selected_check;
788
789   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
790   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
791
792   $form->error($locale->text('Cannot process payment for a closed period!'))
793     if ($form->date_closed($form->{"datepaid"}, \%myconfig));
794
795   $amount = $form->parse_amount(\%myconfig, $form->{amount});
796   $form->{amount} = $amount;
797
798   for my $i (1 .. $form->{rowcount}) {
799     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
800       $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
801
802       push(@{ $form->{paid} },      $form->{"paid_$i"});
803       push(@{ $form->{due} },       $form->{"due_$i"});
804       push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
805       push(@{ $form->{invdate} },   $form->{"transdate_$i"});
806     }
807   }
808
809   if ($form->round_amount($amount, 2) != 0) {
810     push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
811     push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
812     push(@{ $form->{invnumber} },
813          ($form->{ARAP} eq 'AR')
814          ? $locale->text('Deposit')
815          : $locale->text('Prepayment'));
816     push(@{ $form->{invdate} }, $form->{datepaid});
817   }
818
819   $lxdebug->leave_sub();
820 }