import sql-ledger 2.4.4
[freeside.git] / sql-ledger / sql-ledger / bin / mozilla / cp.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 # Contributors:
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
24 #
25 # Payment module
26 #
27 #======================================================================
28
29
30 use SL::CP;
31 use SL::OP;
32 use SL::IS;
33 use SL::IR;
34
35 require "$form->{path}/arap.pl";
36
37 1;
38 # end of main
39
40
41 sub payment {
42   
43   $form->{callback} = "$form->{script}?action=payment&vc=$form->{vc}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&all_vc=$form->{all_vc}&type=$form->{type}";
44   
45   $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
46   $form->{arap} = lc $form->{ARAP};
47
48   # setup customer/vendor selection for open invoices
49   if ($form->{all_vc}) {
50     $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
51   } else {
52     CP->get_openvc(\%myconfig, \%$form);
53   }
54
55   $form->{"select$form->{vc}"} = "";
56   
57   if ($form->{"all_$form->{vc}"}) {
58     $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
59     map { $form->{"select$form->{vc}"} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } @{ $form->{"all_$form->{vc}"} };
60   }
61
62   # departments
63   if (@{ $form->{all_departments} }) { 
64     $form->{selectdepartment} = "<option>\n";
65     $form->{department} = "$form->{department}--$form->{department_id}" if $form->{department};
66
67     map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
68   }
69
70   if (@{ $form->{all_languages} }) {
71     $form->{selectlanguage} = "<option>\n";
72     map { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| } @{ $form->{all_languages} };
73   }
74
75   CP->paymentaccounts(\%myconfig, \%$form);
76
77   $form->{selectaccount} = "";
78   $form->{"select$form->{ARAP}"} = "";
79
80   map { $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{PR}{"$form->{ARAP}_paid"} };
81   map { $form->{"select$form->{ARAP}"} .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{PR}{$form->{ARAP}} };
82
83   # currencies
84   @curr = split /:/, $form->{currencies};
85   chomp $curr[0];
86   $form->{defaultcurrency} = $curr[0];
87
88   $form->{selectcurrency} = "";
89   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
90
91   $form->{oldcurrency} = $form->{currency};
92
93   if ($form->{currency} ne $form->{defaultcurrency}) {
94     $form->{forex} = $form->{exchangerate} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid}, ($form->{vc} eq 'customer') ? "buy" : "sell");
95   }
96   
97   $form->{media} = $myconfig{printer};
98   $form->{format} = "pdf" unless $myconfig{printer};
99
100   &form_header;
101   &form_footer;
102
103 }
104
105
106
107 sub form_header {
108
109   $vclabel = ucfirst $form->{vc};
110   $vclabel = $locale->text($vclabel);
111   
112   if ($form->{type} eq 'receipt') {
113     $form->{title} = $locale->text('Receipt');
114   }
115   if ($form->{type} eq 'check') {
116     $form->{title} = $locale->text('Payment');
117   }
118
119
120 # $locale->text('Customer')
121 # $locale->text('Vendor')
122
123   if ($form->{$form->{vc}} eq "") {
124     map { $form->{$_} = "" } qw(address1 address2 city zipcode state country);
125   }
126
127   $exchangerate = "";
128   if ($form->{currency} ne $form->{defaultcurrency}) {
129     $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
130     if ($form->{forex}) {
131       $exchangerate = qq|
132               <tr>
133                 <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
134                 <td colspan=3><input type=hidden name=exchangerate size=10 value=$form->{exchangerate}>$form->{exchangerate}</td>
135               </tr>
136 |;
137     } else {
138       $exchangerate = qq|
139               <tr>
140                 <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
141                 <td colspan=3><input name=exchangerate size=10 value=$form->{exchangerate}></td>
142               </tr>
143 |;
144     }
145   }
146
147   foreach $item ($form->{vc}, department) {
148     $form->{"select$item"} = $form->unescape($form->{"select$item"});
149     $form->{"select$item"} =~ s/ selected//;
150     $form->{"select$item"} =~ s/(<option value="\Q$form->{$item}\E")/$1 selected/;
151   }
152   
153   foreach $item (account, currency, $form->{ARAP}) {
154     $form->{"select$item"} =~ s/ selected//;
155     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
156   }
157
158   $vc = ($form->{"select$form->{vc}"}) ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>| : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
159
160   if ($form->{all_vc}) {
161     $allvc = "checked";
162     $form->{openinvoices} = "";
163   } else {
164     $allvc = "";
165     $form->{openinvoices} = 1;
166   }
167   
168 # $locale->text('AR')
169 # $locale->text('AP')
170
171   $department = qq|
172               <tr>
173                 <th align="right" nowrap>|.$locale->text('Department').qq|</th>
174                 <td><select name=department>$form->{selectdepartment}</select>
175                 <input type=hidden name=selectdepartment value="|.$form->escape($form->{selectdepartment},1).qq|">
176                 <input type=hidden name=olddepartment value="$form->{department}">
177
178               </td>
179             </tr>
180 | if $form->{selectdepartment};
181
182   $form->header;
183
184   $arap = lc $form->{ARAP};
185
186   print qq|
187 <body>
188
189 <form method=post action=$form->{script}>
190
191 <!-- <input type=hidden name=id value=1> -->
192
193 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
194 <input type=hidden name=closedto value=$form->{closedto}>
195 <input type=hidden name=vc value=$form->{vc}>
196 <input type=hidden name=type value=$form->{type}>
197 <input type=hidden name=formname value=$form->{type}>
198 <input type=hidden name=queued value="$form->{queued}">
199 <input type=hidden name=arap value=$arap>
200 <input type=hidden name=ARAP value=$form->{ARAP}>
201 <input type=hidden name=openinvoices value=$form->{openinvoices}>
202 <input type=hidden name=title value="$form->{title}">
203
204 <table width=100%>
205   <tr>
206     <th class=listtop>$form->{title}</th>
207   </tr>
208   <tr height="5"></tr>
209   <tr>
210     <td>
211       <table width=100%>
212         <tr valign=top>
213           <td>
214             <table>
215               <tr>
216                 <td align=right>
217                 <input name=all_vc type=checkbox style=checkbox value=Y $allvc>
218                 <input type=hidden name="oldall_vc" value="$form->{all_vc}"></td>
219                 <th align=left>|.$locale->text('All').qq|</th>
220               </tr>
221               <tr>
222                 <th align=right>$vclabel</th>
223                 <td>$vc</td>
224                 <input type=hidden name="select$form->{vc}" value="|.$form->escape($form->{"select$form->{vc}"},1).qq|">
225                 <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
226                 <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
227               </tr>
228               <tr valign=top>
229                 <th align=right nowrap>|.$locale->text('Address').qq|</th>
230                 <td colspan=2>
231                   <table>
232                     <tr>
233                       <td>$form->{address1}</td>
234                     </tr>
235                     <tr>
236                       <td>$form->{address2}</td>
237                     </tr>
238                       <td>$form->{city}</td>
239                     </tr>
240                     </tr>
241                       <td>$form->{state}</td>
242                     </tr>
243                     </tr>
244                       <td>$form->{zipcode}</td>
245                     </tr>
246                     <tr>
247                       <td>$form->{country}</td>
248                     </tr>
249                   </table>
250                 </td>
251                 <input type=hidden name=address1 value="$form->{address1}">
252                 <input type=hidden name=address2 value="$form->{address2}">
253                 <input type=hidden name=city value="$form->{city}">
254                 <input type=hidden name=state value="$form->{state}">
255                 <input type=hidden name=zipcode value="$form->{zipcode}">
256                 <input type=hidden name=country value="$form->{country}">
257               </tr>
258               <tr>
259                 <th align=right>|.$locale->text('Memo').qq|</th>
260                 <td colspan=2><input name="memo" size=30 value="$form->{memo}"></td>
261               </tr>
262             </table>
263           </td>
264           <td align=right>
265             <table>
266               $department
267               <tr>
268                 <th align=right nowrap>|.$locale->text($form->{ARAP}).qq|</th>
269                 <td colspan=3><select name=$form->{ARAP}>$form->{"select$form->{ARAP}"}</select>
270                 </td>
271                 <input type=hidden name="select$form->{ARAP}" value="$form->{"select$form->{ARAP}"}">
272               </tr>
273               <tr>
274                 <th align=right nowrap>|.$locale->text('Account').qq|</th>
275                 <td colspan=3><select name=account>$form->{selectaccount}</select>
276                 <input type=hidden name=selectaccount value="$form->{selectaccount}">
277                 </td>
278               </tr>
279               <tr>
280                 <th align=right nowrap>|.$locale->text('Date').qq|</th>
281                 <td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11></td>
282               </tr>
283               <tr>
284                 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
285                 <td><select name=currency>$form->{selectcurrency}</select></td>
286                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
287                 <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
288               </tr>
289               $exchangerate
290               <tr>
291                 <th align=right nowrap>|.$locale->text('Source').qq|</th>
292                 <td colspan=3><input name=source value="$form->{source}" size=10></td>
293               </tr>
294               <tr>
295                 <th align=right nowrap>|.$locale->text('Amount').qq|</th>
296                 <td colspan=3><input name=amount size=10 value=|.$form->format_amount(\%myconfig, $form->{amount}, 2).qq|></td>
297               </tr>
298             </table>
299           </td>
300         </tr>
301       </table>
302     </td>
303   </tr>
304 |;
305
306 }
307
308
309 sub list_invoices {
310
311   @column_index = qw(invnumber transdate amount due checked paid);
312   
313   $colspan = $#column_index + 1;
314
315   $invoice = $locale->text('Invoices');
316   
317   print qq|
318   <input type=hidden name=column_index value="id @column_index">
319   <tr>
320     <td>
321       <table width=100%>
322         <tr>
323           <th class=listheading colspan=$colspan>$invoice</th>
324         </tr>
325 |;
326
327   $column_data{invnumber} = qq|<th nowrap>|.$locale->text('Invoice')."</th>";
328   $column_data{transdate} = qq|<th nowrap>|.$locale->text('Date')."</th>";
329   $column_data{amount} = qq|<th nowrap>|.$locale->text('Amount')."</th>";
330   $column_data{due} = qq|<th nowrap>|.$locale->text('Amount Due')."</th>";
331   $column_data{paid} = qq|<th nowrap>|.$locale->text('Amount')."</th>";
332   $column_data{checked} = qq|<th nowrap>|.$locale->text('Select')."</th>";
333   
334   print qq|
335         <tr>
336 |;
337   map { print "$column_data{$_}\n" } @column_index;
338   print qq|
339         </tr>
340 |;
341
342   for $i (1 .. $form->{rowcount}) {
343
344     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(amount due paid);
345     
346     $totalamount += $form->{"amount_$i"};
347     $totaldue += $form->{"due_$i"};
348     $totalpaid += $form->{"paid_$i"};
349
350     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due paid);
351
352     $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
353       <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
354       <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
355     $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
356       <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
357     $column_data{amount} = qq|<td align=right width=15%>$form->{"amount_$i"}</td>
358       <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
359     $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
360       <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
361
362     $column_data{paid} = qq|<td align=right width=15%><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
363
364     $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
365     $column_data{checked} = qq|<td align=center width=10%><input name="checked_$i" type=checkbox style=checkbox $form->{"checked_$i"}></td>|;
366
367     $j++; $j %= 2;
368     print qq|
369         <tr class=listrow$j>
370 |;
371     map { print "$column_data{$_}\n" } @column_index;
372     print qq|
373         </tr>
374 |;
375   }
376
377   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
378
379   $column_data{amount} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;").qq|</th>|;
380   $column_data{due} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;").qq|</th>|;
381   $column_data{paid} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;").qq|</th>|;
382
383   print qq|
384         <tr class=listtotal>
385 |;
386   map { print "$column_data{$_}\n" } @column_index;
387   print qq|
388         </tr>
389       </table>
390     </td>
391   </tr>
392 |;
393
394 }
395
396
397 sub form_footer {
398
399   $form->{DF}{$form->{format}} = "selected";
400
401   if ($form->{selectlanguage}) {
402     $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
403     $form->{"selectlanguage"} =~ s/ selected//;
404     $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
405     $lang = qq|<select name=language_code>$form->{selectlanguage}</select>
406     <input type=hidden name=selectlanguage value="|.
407     $form->escape($form->{selectlanguage},1).qq|">|;
408   }
409   
410   $media = qq|
411           <option value=screen>|.$locale->text('Screen') if $latex;
412
413   if (%printer && $latex) {
414     map { $media .= qq| 
415           <option value="$_">$_| } sort keys %printer;
416   }
417   if ($latex) {
418     $media .= qq|
419           <option value=queue>|.$locale->text('Queue');
420     $format .= qq|
421             <option value=postscript $form->{DF}{postscript}>|.$locale->text('Postscript').qq|
422             <option value=pdf $form->{DF}{pdf}>|.$locale->text('PDF');
423   }
424   
425       
426   print qq|
427   <tr>
428     <td><hr size=3 noshade></td>
429   </tr>
430 </table>
431
432 <input type=hidden name=callback value="$form->{callback}">
433 <input type=hidden name=rowcount value=$form->{rowcount}>
434
435 <input type=hidden name=path value=$form->{path}>
436 <input type=hidden name=login value=$form->{login}>
437 <input type=hidden name=sessionid value=$form->{sessionid}>
438
439 <br>
440 <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
441 <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
442
443   if ($latex) {
444     print qq|
445 <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">|;
446   }
447
448   $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
449
450   print qq|
451   $lang
452 <select name=format>$format</select>
453 <select name=media>$media</select>
454 |;
455
456   if ($form->{menubar}) {
457     require "$form->{path}/menu.pl";
458     &menubar;
459   }
460
461   print qq|
462   </form>
463
464 </body>
465 </html>
466 |;
467
468 }
469
470
471 sub update {
472   my ($new_name_selected) = @_;
473
474   if ($form->{vc} eq 'customer') {
475     $buysell = "buy";
476   } else {
477     $buysell = "sell";
478   }
479
480   # get customer
481   $updated = &check_name($form->{vc}, $locale->text('Nothing outstanding for ') . $form->{customer});
482
483   if ($form->{department} ne $form->{olddepartment}) {
484     $updated = 1;
485   }
486   
487   # if we switched to all_vc
488   if ($form->{all_vc} ne $form->{oldall_vc}) {
489
490     $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
491     
492     $form->{"select$form->{vc}"} = "";
493
494     if ($form->{all_vc}) {
495       $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
496       
497       if ($form->{"all_$form->{vc}"}) {
498         map { $form->{"select$form->{vc}"} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } @{ $form->{"all_$form->{vc}"} };
499       }
500       
501     } else {
502       CP->get_openvc(\%myconfig, \%$form);
503       
504       if ($form->{"all_$form->{vc}"}) {
505         $newvc = qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
506         map { $form->{"select$form->{vc}"} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } @{ $form->{"all_$form->{vc}"} };
507
508
509         # if the name is not the same
510         if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
511           $form->{$form->{vc}} = $newvc;
512           &check_name($form->{vc}, $locale->text('Nothing outstanding for ') . $form->{customer});
513         }
514       }
515     }
516
517     if (@{ $form->{all_languages} }) {
518       $form->{selectlanguage} = "<option>\n";
519       map { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| } @{ $form->{all_languages} };
520     }
521
522   }
523
524   if ($new_name_selected || $updated) {
525     CP->get_openinvoices(\%myconfig, \%$form);
526     ($newvc) = split /--/, $form->{$form->{vc}};
527     $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;;
528     $updated = 1;
529   }
530
531   if ($form->{currency} ne $form->{oldcurrency}) {
532     $form->{oldcurrency} = $form->{currency};
533     if (!$updated) {
534       CP->get_openinvoices(\%myconfig, \%$form);
535       $updated = 1;
536     }
537   }
538   
539   
540   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid}, $buysell)));
541
542   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
543
544
545   if ($updated) {
546     $form->{rowcount} = 0;
547
548     $form->{queued} = "";
549     
550     $i = 0;
551     foreach $ref (@{ $form->{PR} }) {
552       $i++;
553       $form->{"id_$i"} = $ref->{id};
554       $form->{"invnumber_$i"} = $ref->{invnumber};
555       $form->{"transdate_$i"} = $ref->{transdate};
556       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
557       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
558       $form->{"due_$i"} = ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
559       $form->{"checked_$i"} = "";
560       $form->{"paid_$i"} = "";
561
562       # need to format
563       map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due);
564
565     }
566     $form->{rowcount} = $i;
567   }
568
569   # recalculate
570   $amount = $form->{amount};
571   for $i (1 .. $form->{rowcount}) {
572
573     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(amount due paid);
574
575     if ($form->{"checked_$i"}) {
576       # calculate paid_$i
577       if (!$form->{"paid_$i"}) {
578         $form->{"paid_$i"} = $form->{"due_$i"};
579       }
580       
581       $amount -= $form->{"paid_$i"};
582     } else {
583       $form->{"paid_$i"} = "";
584     }
585
586     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due paid);
587
588   }
589
590   &form_header;
591   &list_invoices;
592   &form_footer;
593   
594 }
595
596
597 sub post {
598   
599   &check_form;
600   
601   if ($form->{currency} ne $form->{defaultcurrency}) {
602     $form->error($locale->text('Exchange rate missing!')) unless $form->{exchangerate};
603   }
604
605   $msg1 = "$form->{title} posted!";
606   $msg2 = "Cannot post $form->{title}!";
607
608 # $locale->text('Payment posted!')
609 # $locale->text('Receipt posted!')
610 # $locale->text('Cannot post Payment!')
611 # $locale->text('Cannot post Receipt!')
612
613
614   $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
615
616   $form->redirect($locale->text($msg1)) if (CP->process_payment(\%myconfig, \%$form));
617   $form->error($locale->text($msg2));
618
619 }
620
621
622 sub print {
623  
624   &check_form;
625
626   ($whole, $form->{decimal}) = split /\./, $form->{amount};
627   
628   $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
629   
630   $form->{decimal} .= "00";
631   $form->{decimal} = substr($form->{decimal}, 0, 2);
632
633   $countrycode = ($form->{language_code}) ? $form->{language_code} : $myconfig{countrycode};
634   $check = new CP $countrycode;
635   $check->init;
636   $form->{text_amount} = $check->num2text($whole);
637
638   &{ "$form->{vc}_details" };
639
640   $form->{templates} = "$myconfig{templates}";
641   $form->{IN} = "$form->{formname}.tex";
642
643   if ($form->{media} !~ /(screen|queue)/) {
644     $form->{OUT} = "| $printer{$form->{media}}";
645   }
646   if ($form->{media} eq 'queue') {
647     %queued = split / /, $form->{queued};
648     
649     if ($filename = $queued{$form->{formname}}) {
650       unlink "$spool/$filename";
651       $filename =~ s/\..*$//g;
652     } else {
653       $filename = time;
654       $filename .= $$;
655     }
656     $filename .= ($form->{format} eq 'postscript') ? '.ps' : '.pdf';
657     $form->{queued} = "$form->{formname} $filename";
658     $form->{OUT} = ">$spool/$filename";
659
660     $form->update_status(\%myconfig);
661
662   }
663
664
665   $form->{company} = $myconfig{company};
666   $form->{address} = $myconfig{address};
667
668   @a = qw(name company address text_amount address1 address2 city state zipcode country memo);
669   $form->format_string(@a);
670
671   $form->parse_template(\%myconfig, $userspath);
672
673   &update if $form->{media} ne 'screen';
674
675 }
676
677
678 sub customer_details { IS->customer_details(\%myconfig, \%$form) };
679 sub vendor_details { IR->vendor_details(\%myconfig, \%$form) };
680   
681
682 sub check_form {
683   
684   &check_name($form->{vc}, $locale->text('Nothing outstanding for ') . $form->{customer});
685
686   if ($form->{currency} ne $form->{oldcurrency}) {
687     &update;
688     exit;
689   }
690   
691   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
692
693   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
694   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
695   
696   $form->error($locale->text('Cannot process payment for a closed period!')) if ($datepaid <= $closedto);
697
698   # this is just to format the year
699   $form->{datepaid} = $locale->date(\%myconfig, $form->{datepaid});
700   
701   $amount = $form->parse_amount(\%myconfig, $form->{amount});
702   $form->{amount} = $amount;
703   
704   for $i (1 .. $form->{rowcount}) {
705     if ($form->{"paid_$i"}) {
706       $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
707       
708       push(@{ $form->{paid} }, $form->{"paid_$i"});
709       push(@{ $form->{due} }, $form->{"due_$i"});
710       push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
711       push(@{ $form->{invdate} }, $form->{"transdate_$i"});
712     }
713   }
714
715   if ($form->round_amount($amount, 2) != 0) {
716     push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
717     push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
718     push(@{ $form->{invnumber} }, ($form->{ARAP} eq 'AR') ? $locale->text('Deposit') : $locale->text('Prepayment'));
719     push(@{ $form->{invdate} }, $form->{datepaid});
720   }
721    
722 }
723
724