This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / is.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 1998-2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
23 #
24 # Inventory invoicing module
25 #
26 #======================================================================
27
28
29 use SL::IS;
30 use SL::PE;
31
32 require "$form->{path}/io.pl";
33 require "$form->{path}/arap.pl";
34
35
36 1;
37 # end of main
38
39
40
41 sub add {
42
43   $form->{title} = $locale->text('Add Sales Invoice');
44
45   &invoice_links;
46   &prepare_invoice;
47   &display_form;
48   
49 }
50
51
52 sub edit {
53
54   $form->{title} = $locale->text('Edit Sales Invoice');
55
56   &invoice_links;
57   &prepare_invoice;
58   &display_form;
59   
60 }
61
62
63 sub invoice_links {
64
65   # create links
66   $form->create_links("AR", \%myconfig, "customer");
67   
68   IS->get_customer(\%myconfig, \%$form);
69   IS->retrieve_invoice(\%myconfig, \%$form);
70
71   # currencies
72   @curr = split /:/, $form->{currencies};
73   chomp $curr[0];
74   $form->{defaultcurrency} = $curr[0];
75
76   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
77
78   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
79   
80   if (@{ $form->{all_customer} }) {
81     $form->{customer} = qq|$form->{customer}--$form->{customer_id}|;
82     map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" } (@{ $form->{all_customer} });
83   }
84
85   # forex
86   $form->{forex} = $form->{exchangerate};
87   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
88
89   foreach $key (keys %{ $form->{AR_links} }) {
90     
91     foreach $ref (@{ $form->{AR_links}{$key} }) {
92       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
93     }
94
95     if ($key eq "AR_paid") {
96       for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
97         $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
98         # reverse paid
99         $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1;
100         $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
101         $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
102         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
103         $form->{paidaccounts} = $i;
104       }
105     } else {
106       $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
107     }
108   }
109
110   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
111
112   $form->{AR} = $form->{AR_1} unless $form->{id};
113
114   $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
115
116 }
117
118
119 sub prepare_invoice {
120
121   $form->{type} = "invoice";
122   $form->{format} = "html";
123   $form->{media} = "screen";
124   
125   if ($form->{id}) {
126     
127     map { $form->{$_} =~ s/"/&quot;/g } qw(invnumber ordnumber shippingpoint notes);
128
129     foreach $ref (@{ $form->{invoice_details} } ) {
130       $i++;
131       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
132       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
133
134       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
135       $dec = length $dec;
136       $decimalplaces = ($dec > 2) ? $dec : 2;
137       
138       $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
139       $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
140       
141       map { $form->{"${_}_$i"} =~ s/"/&quot;/g } qw(partnumber description unit);
142       $form->{rowcount} = $i;
143     }
144   }
145   
146 }
147
148
149
150 sub form_header {
151
152
153   # set option selected
154   foreach $item (qw(AR customer currency)) {
155     $form->{"select$item"} =~ s/ selected//;
156     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
157   }
158     
159   $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
160
161   $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
162   $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
163
164   
165   $exchangerate = "";
166   if ($form->{currency} ne $form->{defaultcurrency}) {
167     if ($form->{forex}) {
168       $exchangerate .= qq|<th align=right>|.$locale->text('Exchangerate').qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
169     } else {
170       $exchangerate .= qq|<th align=right>|.$locale->text('Exchangerate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
171     }
172   }
173   $exchangerate .= qq|
174 <input type=hidden name=forex value=$form->{forex}>
175 |;
176
177   $customer = ($form->{selectcustomer}) ? qq|<select name=customer>$form->{selectcustomer}</select>\n<input type=hidden name="selectcustomer" value="$form->{selectcustomer}">| : qq|<input name=customer value="$form->{customer}" size=35>|;
178   
179   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
180
181   
182   $form->header;
183
184   print qq|
185 <body>
186
187 <form method=post action=$form->{script}>
188
189 <input type=hidden name=id value=$form->{id}>
190
191 <input type=hidden name=type value=$form->{type}>
192 <input type=hidden name=media value=$form->{media}>
193 <input type=hidden name=format value=$form->{format}>
194
195 <input type=hidden name=title value="$form->{title}">
196 <input type=hidden name=vc value="customer">
197 <input type=hidden name=employee value="$form->{employee}">
198
199 <input type=hidden name=discount value=$form->{discount}>
200 <input type=hidden name=creditlimit value=$form->{creditlimit}>
201 <input type=hidden name=creditremaining value=$form->{creditremaining}>
202
203 <input type=hidden name=closedto value=$form->{closedto}>
204 <input type=hidden name=locked value=$form->{locked}>
205
206 <table width=100%>
207   <tr class=listtop>
208     <th class=listtop>$form->{title}</font></th>
209   </tr>
210   <tr height="5"></tr>
211   <tr>
212     <td>
213       <table width=100%>
214         <tr valign=top>
215           <td>
216             <table>
217               <tr>
218                 <th align=right nowrap>|.$locale->text('Record in').qq|</th>
219                 <td colspan=3><select name=AR>$form->{selectAR}</select></td>
220                 <input type=hidden name=selectAR value="$form->{selectAR}">
221               </tr>
222               <tr>
223                 <th align=right nowrap>|.$locale->text('Customer').qq|</th>
224                 <td colspan=3>$customer</td>
225                 <input type=hidden name=customer_id value=$form->{customer_id}>
226                 <input type=hidden name=oldcustomer value="$form->{oldcustomer}"> 
227               </tr>
228               <tr>
229                 <td></td>
230                 <td colspan=3>
231                   <table width=100%>
232                     <tr>
233                       <th align=left nowrap>|.$locale->text('Credit Limit').qq|</th>
234                       <td>$form->{creditlimit}</td>
235                       <th align=left nowrap>|.$locale->text('Remaining').qq|</th>
236                       <td class="plus$n">$form->{creditremaining}</font></td>
237                     </tr>
238                   </table>
239                 </td>
240               </tr>
241               <tr>
242                 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
243                 <td><select name=currency>$form->{selectcurrency}</select></td>
244                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
245                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
246                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
247                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
248                 $exchangerate
249               </tr>
250               <tr>
251                 <th align=right nowrap>|.$locale->text('Ship via').qq|</th>
252                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
253               </tr>
254             </table>
255           </td>
256           <td align=right>
257             <table>
258               <tr>
259                 <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
260                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
261               </tr>
262               <tr>
263                 <th align=right>|.$locale->text('Invoice Date').qq|</th>
264                 <td><input name=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>
265               </tr>
266               <tr>
267                 <th align=right>|.$locale->text('Due Date').qq|</th>
268                 <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
269               </tr>
270               <tr>
271                 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
272                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
273               </tr>
274             </table>
275           </td>
276         </tr>
277       </table>
278     </td>
279   </tr>
280   <tr>
281     <td>
282     </td>
283   </tr>
284 <!-- shipto are in hidden variables -->
285
286 <input type=hidden name=shiptoname value="$form->{shiptoname}">
287 <input type=hidden name=shiptoaddr1 value="$form->{shiptoaddr1}">
288 <input type=hidden name=shiptoaddr2 value="$form->{shiptoaddr2}">
289 <input type=hidden name=shiptoaddr3 value="$form->{shiptoaddr3}">
290 <input type=hidden name=shiptoaddr4 value="$form->{shiptoaddr4}">
291 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
292 <input type=hidden name=shiptophone value="$form->{shiptophone}">
293 <input type=hidden name=shiptofax value="$form->{shiptofax}">
294 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
295
296 <!-- email variables -->
297 <input type=hidden name=message value="$form->{message}">
298 <input type=hidden name=email value="$form->{email}">
299 <input type=hidden name=subject value="$form->{subject}">
300 <input type=hidden name=cc value="$form->{cc}">
301 <input type=hidden name=bcc value="$form->{bcc}">
302
303 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
304 |;
305
306   foreach $item (split / /, $form->{taxaccounts}) {
307     print qq|
308 <input type=hidden name="${item}_rate" value="$form->{"${item}_rate"}">
309 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
310 <input type=hidden name="${item}_taxnumber" value="$form->{"${item}_taxnumber"}">
311 |;
312   }
313
314 }
315
316
317
318 sub form_footer {
319
320   $form->{invtotal} = $form->{invsubtotal};
321
322   if (($rows = $form->numtextrows($form->{notes}, 50, 8)) < 2) {
323     $rows = 2;
324   }
325   $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
326
327
328   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
329
330   if ($form->{taxaccounts}) {
331     $taxincluded = qq|
332                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b><br><br>
333 |;
334   }
335   
336   if (!$form->{taxincluded}) {
337     
338     foreach $item (split / /, $form->{taxaccounts}) {
339       if ($form->{"${item}_base"}) {
340         $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
341         $form->{invtotal} += $form->{"${item}_total"};
342         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
343         
344         $tax .= qq|
345               <tr>
346                 <th align=right>$form->{"${item}_description"}</th>
347                 <td align=right>$form->{"${item}_total"}</td>
348               </tr>
349 |;
350       }
351     }
352
353     $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
354     
355     $subtotal = qq|
356               <tr>
357                 <th align=right>|.$locale->text('Subtotal').qq|</th>
358                 <td align=right>$form->{invsubtotal}</td>
359               </tr>
360 |;
361
362   }
363
364   $form->{oldinvtotal} = $form->{invtotal};
365   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
366   
367   print qq|
368   <tr>
369     <td>
370       <table width=100%>
371         <tr valign=bottom>
372           <td>
373             <table>
374               <tr>
375                 <th align=left>|.$locale->text('Notes').qq|</th>
376               </tr>
377               <tr>
378                 <td>$notes</td>
379               </tr>
380             </table>
381           </td>
382           <td align=right width=100%>
383             $taxincluded
384             <table width=100%>
385               $subtotal
386               $tax
387               <tr>
388                 <th align=right>|.$locale->text('Total').qq|</th>
389                 <td align=right>$form->{invtotal}</td>
390               </tr>
391             </table>
392           </td>
393         </tr>
394       </table>
395     </td>
396   </tr>
397   <tr>
398     <td>
399       <table width=100%>
400         <tr class=listheading>
401           <th class=listheading colspan=5>|.$locale->text('Payments')
402           .qq|</font></th>
403         </tr>
404 |;
405
406   if ($form->{currency} eq $form->{defaultcurrency}) {
407     @column_index = qw(datepaid source paid AR_paid);
408   } else {
409     @column_index = qw(datepaid source paid exchangerate AR_paid);
410   }
411
412   $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
413   $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
414   $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
415   $column_data{AR_paid} = "<th>".$locale->text('Account')."</th>";
416   $column_data{source} = "<th>".$locale->text('Source')."</th>";
417   
418   print "
419         <tr>
420 ";
421   map { print "$column_data{$_}\n" } @column_index;
422   print "
423         </tr>
424 ";
425   
426   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
427   for $i (1 .. $form->{paidaccounts}) {
428
429     print "
430         <tr>\n";
431
432     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
433     $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
434     
435     # format amounts
436     $totalpaid += $form->{"paid_$i"};
437     $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
438     $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
439
440     $exchangerate = qq|&nbsp;|;
441     if ($form->{currency} ne $form->{defaultcurrency}) {
442       if ($form->{"forex_$i"}) {
443         $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
444       } else {
445         $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
446       }
447     }
448
449     $exchangerate .= qq|
450 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
451 |;
452
453     $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
454     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
455     $column_data{"AR_paid_$i"} = qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
456     $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
457     $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
458
459     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
460     print "
461         </tr>\n";
462   }
463
464   print qq|
465 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
466 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
467 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
468 <input type=hidden name=oldtotalpaid value=$totalpaid>
469       </table>
470     </td>
471   </tr>
472   <tr>
473     <td>
474 |;
475
476   &print_options;
477
478   print qq|
479     </td>
480   </tr>
481   <tr>
482     <td><hr size=3 noshade></td>
483   </tr>
484 </table>
485 |;
486
487
488   $invdate = $form->datetonum($form->{invdate}, \%myconfig);
489   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
490  
491   if ($form->{id}) {
492     print qq|
493     <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
494     <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
495     <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
496     <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
497 |;
498
499     if (!$form->{revtrans}) {
500       if (!$form->{locked}) {
501         print qq|
502         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
503         <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
504 |;
505       }
506     }
507
508     if ($invdate > $closedto) {
509       print qq|
510       <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
511       <input class=submit type=submit name=action value="|.$locale->text('Order').qq|">
512 |;
513     }
514
515   } else {
516     if ($invdate > $closedto) {
517       print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
518       <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
519       <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
520       <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
521       <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
522     }
523   }
524
525   print qq|
526
527 <input type=hidden name=rowcount value=$form->{rowcount}>
528
529 <input name=callback type=hidden value="$form->{callback}">
530
531 <input type=hidden name=path value=$form->{path}>
532 <input type=hidden name=login value=$form->{login}>
533 <input type=hidden name=password value=$form->{password}>
534
535 </form>
536
537 </body>
538 </html>
539 |;
540
541 }
542
543
544 sub update {
545
546   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
547   
548   &check_name(customer);
549
550   &check_project;
551
552   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy')));
553
554   for $i (1 .. $form->{paidaccounts}) {
555     if ($form->{"paid_$i"}) {
556       map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
557
558       $form->{"exchangerate_$i"} = $exchangerate if ($form->{"forex_$i"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy')));
559     }
560   }
561
562   $i = $form->{rowcount};
563   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
564
565   # if last row empty, check the form otherwise retrieve new item
566   if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
567
568     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
569     &check_form;
570
571   } else {
572
573     IS->retrieve_item(\%myconfig, \%$form);
574     
575     $rows = scalar @{ $form->{item_list} };
576
577     $form->{"discount_$i"}      = $form->format_amount(\%myconfig, $form->{discount} * 100);
578
579     if ($rows) {
580       $form->{"qty_$i"}         = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
581       
582       if ($rows > 1) {
583         
584         &select_item;
585         exit;
586         
587       } else {
588
589         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
590         
591         map { $form->{item_list}[$i]{$_} =~ s/"/&quot;/g } qw(partnumber description unit);
592         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
593         
594         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
595         ($dec) = ($s =~ /\.(\d+)/);
596         $dec = length $dec;
597         $decimalplaces = ($dec > 2) ? $dec : 2;
598
599         if ($sellprice) {
600           $form->{"sellprice_$i"} = $sellprice;
601         } else {
602           # if there is an exchange rate adjust sellprice
603           $form->{"sellprice_$i"} /= $exchangerate;
604         }
605         
606         $form->{"listprice_$i"} /= $exchangerate;
607
608         $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
609         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
610         map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
611         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
612         
613         $form->{creditremaining} -= $amount;
614         
615
616         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
617         
618         $form->{"qty_$i"} =  $form->format_amount(\%myconfig, $form->{"qty_$i"});
619
620       }
621
622       &display_form;
623
624     } else {
625       # ok, so this is a new part
626       # ask if it is a part or service item
627
628       if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
629         $form->{rowcount}--;
630         $form->{"discount_$i"} = "";
631         &display_form;
632       } else {
633
634         $form->{"id_$i"}          = 0;
635         $form->{"unit_$i"}        = $locale->text('ea');
636
637         &new_item;
638
639       }
640     }
641   }
642 }
643
644
645
646 sub post {
647
648   $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
649   $form->isblank("invdate", $locale->text('Invoice Date missing!'));
650   $form->isblank("customer", $locale->text('Customer missing!'));
651
652   # if oldcustomer ne customer redo form
653   if (&check_name(customer)) {
654     &update;
655     exit;
656   }
657   
658   &validate_items;
659
660   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
661   $invdate = $form->datetonum($form->{invdate}, \%myconfig);
662   
663   $form->error($locale->text('Cannot post invoice for a closed period!')) if ($invdate <= $closedto);
664
665   $form->isblank("exchangerate", $locale->text('Exchangerate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
666   
667   for $i (1 .. $form->{paidaccounts}) {
668     if ($form->{"paid_$i"}) {
669       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
670
671       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
672       
673       $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
674
675       if ($form->{currency} ne $form->{defaultcurrency}) {
676         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
677         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
678       }
679     }
680   }
681
682       
683   ($form->{AR}) = split /--/, $form->{AR};
684   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
685   
686   $form->{label} = $locale->text('Invoice');
687
688   $form->{id} = 0 if $form->{postasnew};
689   
690   $form->redirect($locale->text('Invoice posted!')) if (IS->post_invoice(\%myconfig, \%$form));
691   $form->error($locale->text('Cannot post invoice!'));
692     
693 }
694
695
696
697
698 sub delete {
699
700   $form->header;
701
702   print qq|
703 <body>
704
705 <form method=post action=$form->{script}>
706 |;
707
708   # delete action variable
709   delete $form->{action};
710
711   foreach $key (keys %$form) {
712     $form->{$key} =~ s/"/&quot;/g;
713     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
714   }
715
716   print qq|
717 <h2 class=confirm>|.$locale->text('Confirm!').qq|</font></h2>
718
719 <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}
720 </h4>
721
722 <p>
723 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
724 </form>
725 |;
726
727
728 }
729
730
731
732 sub yes {
733
734   $form->redirect($locale->text('Invoice deleted!')) if (IS->delete_invoice(\%myconfig, \%$form));
735   $form->error($locale->text('Cannot delete invoice!'));
736
737 }
738
739