Bei Zahlungseingang/-ausgang Meldung anzeigen, wenn offene Rechnungen in anderen...
[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   if ($form->{openinvoices_other_currencies}) {
321     my $warning = $form->{vc} eq 'customer' ? $::locale->text('There are #1 more open invoices for this customer with other currencies.', $form->{openinvoices_other_currencies})
322                 :                             $::locale->text('There are #1 more open invoices from this vendor with other currencies.',  $form->{openinvoices_other_currencies});
323
324     print qq|
325
326   <input type="hidden" name="openinvoices_other_currencies" value="| . H($form->{openinvoices_other_currencies}) . qq|">
327   <tr>
328    <td><b>| . $::locale->text('Note') . qq|: $warning</b></td>
329   </tr>
330 |;
331   }
332
333   $lxdebug->leave_sub();
334 }
335
336 sub list_invoices {
337   $lxdebug->enter_sub();
338
339   $auth->assert('cash');
340
341   my (@column_index, %column_data, $colspan, $invoice);
342   my ($totalamount, $totaldue, $totalpaid);
343
344   @column_index = qw(invnumber transdate amount due checked paid);
345
346   $colspan = $#column_index + 1;
347
348   $invoice = $locale->text('Invoices');
349
350   print qq|
351   <input type=hidden name=column_index value="id @column_index">
352   <tr>
353     <td>
354       <table width=100%>
355         <tr>
356           <th class=listheading colspan=$colspan>$invoice</th>
357         </tr>
358 |;
359
360   $column_data{invnumber} =
361     qq|<th nowrap class=listheading>| . $locale->text('Invoice') . "</th>";
362   $column_data{transdate} =
363     qq|<th nowrap class=listheading>| . $locale->text('Date') . "</th>";
364   $column_data{amount} =
365     qq|<th nowrap class=listheading>| . $locale->text('Amount') . "</th>";
366   $column_data{due} =
367     qq|<th nowrap class=listheading>| . $locale->text('Due') . "</th>";
368   $column_data{paid} =
369     qq|<th nowrap class=listheading>| . $locale->text('Amount') . "</th>";
370   $column_data{checked} =
371     qq|<th nowrap class=listheading>| . $locale->text('Select') . "</th>";
372
373   print qq|
374         <tr>
375 |;
376   map { print "$column_data{$_}\n" } @column_index;
377   print qq|
378         </tr>
379 |;
380
381   for my $i (1 .. $form->{rowcount}) {
382
383     my $j = 0;
384
385     map {
386       $form->{"${_}_$i"} =
387         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
388     } qw(amount due paid);
389
390     $totalamount += $form->{"amount_$i"};
391     $totaldue    += $form->{"due_$i"};
392     $totalpaid   += $form->{"paid_$i"};
393
394     map {
395       $form->{"${_}_$i"} =
396         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
397     } qw(amount due paid);
398
399     $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
400       <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
401       <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
402     $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
403       <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
404     $column_data{amount} =
405       qq|<td align=right width=15%>$form->{"amount_$i"}</td>
406       <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
407     $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
408       <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
409
410     $column_data{paid} =
411       qq|<td align=right width=15%><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
412
413     $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
414     $column_data{checked} =
415       qq|<td align=center width=10%><input name="checked_$i" type=checkbox style=checkbox $form->{"checked_$i"}></td>|;
416
417     $j++;
418     $j %= 2;
419     print qq|
420         <tr class=listrow$j>
421 |;
422     map { print "$column_data{$_}\n" } @column_index;
423     print qq|
424         </tr>
425 |;
426   }
427
428   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
429
430   $column_data{amount} =
431       qq|<th class=listtotal align=right>|
432     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;")
433     . qq|</th>|;
434   $column_data{due} =
435       qq|<th class=listtotal align=right>|
436     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;")
437     . qq|</th>|;
438   $column_data{paid} =
439       qq|<th class=listtotal align=right>|
440     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;")
441     . qq|</th>|;
442
443   print qq|
444         <tr class=listtotal>
445 |;
446   map { print "$column_data{$_}\n" } @column_index;
447   print qq|
448         </tr>
449       </table>
450     </td>
451   </tr>
452 |;
453
454   $lxdebug->leave_sub();
455 }
456
457 sub form_footer {
458   $lxdebug->enter_sub();
459
460   $auth->assert('cash');
461
462   my ($media, $format, $latex_templates);
463
464   $form->{DF}{ $form->{format} } = "selected";
465   $form->{OP}{ $form->{media} }  = "selected";
466
467   $media = qq|
468           <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
469
470   if ($myconfig{printer} && $latex_templates) {
471     $media .= qq|
472           <option value=printer $form->{OP}{printer}>|
473       . $locale->text('Printer');
474   }
475   if ($latex_templates) {
476     $media .= qq|
477           <option value=queue $form->{OP}{queue}>| . $locale->text('Queue');
478     $format .= qq|
479             <option value=postscript $form->{DF}{postscript}>|
480       . $locale->text('Postscript') . qq|
481             <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF');
482   }
483
484   print qq|
485   <tr>
486     <td><hr size=3 noshade></td>
487   </tr>
488 </table>
489 <input type=hidden name=rowcount value=$form->{rowcount}>
490
491 <br>
492 <input class=submit type=submit name=action value="|
493     . $locale->text('Update') . qq|">
494 <input class=submit type=submit name=action value="|
495     . $locale->text('Post') . qq|">|;
496
497   if ($latex_templates) {
498     print qq|
499 <input class=submit type=submit name=action value="|
500       . $locale->text('Print') . qq|">|;
501   }
502
503   print qq|
504 <select name=format>$format</select>
505 <select name=media>$media</select>
506
507   </form>
508
509 </body>
510 </html>
511 |;
512
513   $lxdebug->leave_sub();
514 }
515
516 sub update {
517   $lxdebug->enter_sub();
518
519   $auth->assert('cash');
520
521   my ($new_name_selected) = @_;
522
523   my ($buysell, $newvc, $updated, $exchangerate, $amount);
524
525   if ($form->{vc} eq 'customer') {
526     $buysell = "buy";
527   } else {
528     $buysell = "sell";
529   }
530
531   # if we switched to all_vc
532   if ($form->{all_vc} ne $form->{oldall_vc}) {
533
534     $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
535
536     $form->{"select$form->{vc}"} = "";
537
538     if ($form->{all_vc}) {
539       $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
540
541       if ($form->{"all_$form->{vc}"}) {
542         map {
543           $form->{"select$form->{vc}"} .=
544             "<option>$_->{name}--$_->{id}\n"
545         } @{ $form->{"all_$form->{vc}"} };
546       }
547     } else {
548       CP->get_openvc(\%myconfig, \%$form);
549
550       if ($form->{"all_$form->{vc}"}) {
551         $newvc =
552           qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
553         map {
554           $form->{"select$form->{vc}"} .=
555             "<option>$_->{name}--$_->{id}\n"
556         } @{ $form->{"all_$form->{vc}"} };
557       }
558
559       # if the name is not the same
560       if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
561         $form->{ $form->{vc} } = $newvc;
562       }
563     }
564   }
565
566   # get customer and invoices
567   $updated = &check_name($form->{vc});
568
569   if ($new_name_selected || $updated) {
570     CP->get_openinvoices(\%myconfig, \%$form);
571     ($newvc) = split /--/, $form->{ $form->{vc} };
572     $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
573     $updated = 1;
574   }
575
576   if ($form->{currency} ne $form->{oldcurrency}) {
577     $form->{oldcurrency} = $form->{currency};
578     if (!$updated) {
579       CP->get_openinvoices(\%myconfig, \%$form);
580       $updated = 1;
581     }
582   }
583
584   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{datepaid}, $buysell);
585   $form->{exchangerate} = $form->{forex} if $form->{forex};
586
587   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
588
589   if ($updated) {
590     $form->{rowcount} = 0;
591
592     $form->{queued} = "";
593
594     my $i = 0;
595     foreach my $ref (@{ $form->{PR} }) {
596       $i++;
597       $form->{"id_$i"}        = $ref->{id};
598       $form->{"invnumber_$i"} = $ref->{invnumber};
599       $form->{"transdate_$i"} = $ref->{transdate};
600       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
601       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
602       $form->{"due_$i"}    =
603         ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
604       $form->{"checked_$i"} = "";
605       $form->{"paid_$i"}    = "";
606
607       # need to format
608       map {
609         $form->{"${_}_$i"} =
610           $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
611       } qw(amount due);
612
613     }
614     $form->{rowcount} = $i;
615   }
616
617   # recalculate
618
619   # Modified from $amount = $form->{amount} by J.Zach to update amount to total
620   # payment amount in Zahlungsausgang
621   $amount = 0;
622   for my $i (1 .. $form->{rowcount}) {
623
624     map {
625       $form->{"${_}_$i"} =
626         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
627     } qw(amount due paid);
628
629     if ($form->{"checked_$i"}) {
630
631       # calculate paid_$i
632       if (!$form->{"paid_$i"}) {
633         $form->{"paid_$i"} = $form->{"due_$i"};
634       }
635
636       # Modified by J.Zach, see abovev
637       $amount += $form->{"paid_$i"};
638
639     } else {
640       $form->{"paid_$i"} = "";
641     }
642
643     map {
644       $form->{"${_}_$i"} =
645         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
646     } qw(amount due paid);
647
648   }
649
650   # Line added by J.Zach, see above
651   $form->{amount}=$amount;
652
653   &form_header;
654   &list_invoices;
655   &form_footer;
656
657   $lxdebug->leave_sub();
658 }
659
660 sub post {
661   $lxdebug->enter_sub();
662
663   $auth->assert('cash');
664
665   &check_form;
666
667   if ($form->{currency} ne $form->{defaultcurrency}) {
668     $form->error($locale->text('Exchangerate missing!'))
669       unless $form->{exchangerate};
670   }
671
672   my $msg1 = "$form->{origtitle} posted!";
673   my $msg2 = "Cannot post $form->{origtitle}!";
674
675   # $locale->text('Payment posted!')
676   # $locale->text('Receipt posted!')
677   # $locale->text('Cannot post Payment!')
678   # $locale->text('Cannot post Receipt!')
679
680   $form->redirect($locale->text($msg1))
681     if (CP->process_payment(\%myconfig, \%$form));
682   $form->error($locale->text($msg2));
683
684   $lxdebug->leave_sub();
685 }
686
687 sub print {
688   $lxdebug->enter_sub();
689
690   $auth->assert('cash');
691
692   my ($whole, $check, %queued, $spool, $filename, $userspath);
693
694   &check_form;
695
696   ($whole, $form->{decimal}) = split(/\./, $form->{amount});
697
698   $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
699
700   #$form->{decimal} .= "00";
701   $form->{decimal} = substr($form->{decimal}, 0, 2);
702
703   $check = new CP $myconfig{countrycode};
704   $check->init;
705   $form->{text_amount} = $check->num2text($whole);
706
707   if ($form->{vc} eq 'customer') {
708     IS->customer_details(\%myconfig, $form);
709   } else {
710     IR->vendor_details(\%myconfig, $form);
711   }
712
713   $form->{callback} = "";
714
715   $form->{templates} = "$myconfig{templates}";
716   $form->{IN}        = "$form->{formname}.tex";
717
718   if ($form->{format} eq 'postscript') {
719     $form->{postscript} = 1;
720   }
721   if ($form->{format} eq 'pdf') {
722     $form->{pdf} = 1;
723   }
724
725   delete $form->{OUT};
726
727   if ($form->{media} eq 'printer') {
728     $form->{OUT} = "| $myconfig{printer}";
729   }
730   if ($form->{media} eq 'queue') {
731     %queued = map { s|.*/|| } split / /, $form->{queued};
732
733     if ($filename = $queued{ $form->{formname} }) {
734       unlink "$spool/$filename";
735       $filename =~ s/\..*$//g;
736     } else {
737       $filename = time;
738       $filename .= $$;
739     }
740     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
741     $form->{queued} = "$form->{formname} $filename";
742     $form->{OUT}    = ">$spool/$filename";
743
744     $form->update_status(\%myconfig);
745
746   }
747
748   $form->{company} = $myconfig{company};
749   $form->{address} = $myconfig{address};
750
751   $form->parse_template(\%myconfig, $userspath);
752
753   if ($form->{media} ne 'screen') {
754     $form->{callback} = "cp.pl?action=payment&vc=$form->{vc}&all_vc=$form->{all_vc}";
755
756     $form->redirect if (CP->process_payment(\%myconfig, \%$form));
757     $form->error($locale->text('Cannot post payment!'));
758   }
759
760   $lxdebug->leave_sub();
761 }
762
763 sub check_form {
764   $lxdebug->enter_sub();
765
766   $auth->assert('cash');
767
768   my ($closedto, $datepaid, $amount);
769
770   &check_name($form->{vc});
771
772   if ($form->{currency} ne $form->{oldcurrency}) {
773     &update;
774     exit;
775   }
776
777   $form->error($locale->text('Zero amount posting!')) if !$form->parse_amount(\%myconfig, $form->{amount});
778   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
779
780   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
781   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
782
783   $form->error($locale->text('Cannot process payment for a closed period!'))
784     if ($form->date_closed($form->{"datepaid"}, \%myconfig));
785
786   $amount = $form->parse_amount(\%myconfig, $form->{amount});
787   $form->{amount} = $amount;
788
789   for my $i (1 .. $form->{rowcount}) {
790     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
791       $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
792
793       push(@{ $form->{paid} },      $form->{"paid_$i"});
794       push(@{ $form->{due} },       $form->{"due_$i"});
795       push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
796       push(@{ $form->{invdate} },   $form->{"transdate_$i"});
797     }
798   }
799
800   if ($form->round_amount($amount, 2) != 0) {
801     push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
802     push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
803     push(@{ $form->{invnumber} },
804          ($form->{ARAP} eq 'AR')
805          ? $locale->text('Deposit')
806          : $locale->text('Prepayment'));
807     push(@{ $form->{invdate} }, $form->{datepaid});
808   }
809
810   $lxdebug->leave_sub();
811 }