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