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