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