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