import sql-ledger 2.4.4
[freeside.git] / sql-ledger / bin / mozilla / is.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 2001
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}/arap.pl";
33 require "$form->{path}/io.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   $form->{callback} = "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&sessionid=$form->{sessionid}" unless $form->{callback};
46
47   &invoice_links;
48   &prepare_invoice;
49   &display_form;
50   
51 }
52
53
54 sub edit {
55
56   $form->{title} = $locale->text('Edit Sales Invoice');
57
58   &invoice_links;
59   &prepare_invoice;
60   &display_form;
61   
62 }
63
64
65 sub invoice_links {
66
67   $form->{vc} = 'customer';
68
69   # create links
70   $form->create_links("AR", \%myconfig, "customer");
71   
72   # currencies
73   @curr = split /:/, $form->{currencies};
74   chomp $curr[0];
75   $form->{defaultcurrency} = $curr[0];
76
77   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
78
79   if ($form->{all_customer}) {
80     unless ($form->{customer_id}) {
81       $form->{customer_id} = $form->{all_customer}->[0]->{id};
82     }
83   }
84
85   IS->get_customer(\%myconfig, \%$form);
86   delete $form->{notes};
87   IS->retrieve_invoice(\%myconfig, \%$form);
88
89   $form->{oldlanguage_code} = $form->{language_code};
90   
91   $form->get_partsgroup(\%myconfig, { language_code => $form->{language_code} });
92   if (@{ $form->{all_partsgroup} }) {
93     $form->{selectpartsgroup} = "<option>\n";
94     foreach $ref (@ { $form->{all_partsgroup} }) {
95       if ($ref->{translation}) {
96         $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{translation}\n|;
97       } else {
98         $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{partsgroup}\n|;
99       }
100     }
101   }
102   
103   if (@{ $form->{all_projects} }) {
104     $form->{selectprojectnumber} = "<option>\n";
105     map { $form->{selectprojectnumber} .= qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n| } @{ $form->{all_projects} };
106   }
107
108   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
109   $form->{oldtransdate} = $form->{transdate};
110   
111   if ($form->{all_customer}) {
112     $form->{customer} = "$form->{customer}--$form->{customer_id}";
113     map { $form->{selectcustomer} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } (@{ $form->{all_customer} });
114   }
115
116   # departments
117   if ($form->{all_departments}) {
118     $form->{selectdepartment} = "<option>\n";
119     $form->{department} = "$form->{department}--$form->{department_id}";
120
121     map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
122   }
123   
124   $form->{employee} = "$form->{employee}--$form->{employee_id}";
125   # sales staff
126   if ($form->{all_employees}) {
127     $form->{selectemployee} = "";
128     map { $form->{selectemployee} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } (@{ $form->{all_employees} });
129   }
130   
131   if (@{ $form->{all_languages} }) {
132     $form->{selectlanguage} = "<option>\n";
133     map { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| } @{ $form->{all_languages} };
134   }
135   
136   # forex
137   $form->{forex} = $form->{exchangerate};
138   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
139   
140   foreach $key (keys %{ $form->{AR_links} }) {
141     foreach $ref (@{ $form->{AR_links}{$key} }) {
142       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
143     }
144
145     if ($key eq "AR_paid") {
146       for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
147         $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
148         # reverse paid
149         $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1;
150         $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
151         $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
152         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
153         $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
154         
155         $form->{paidaccounts} = $i;
156       }
157     } else {
158       $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
159     }
160     
161   }
162
163   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
164
165   $form->{AR} = $form->{AR_1} unless $form->{id};
166   
167   $form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum($form->{transdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
168
169   $form->{readonly} = 1 if $myconfig{acs} =~ /AR--Sales Invoice/;
170
171 }
172
173
174 sub prepare_invoice {
175
176   $form->{type} = "invoice";
177   $form->{formname} = "invoice";
178   $form->{format} = "postscript" if $myconfig{printer};
179   $form->{media} = $myconfig{printer};
180   
181   $form->{oldcurrency} = $form->{currency};
182   
183   if ($form->{id}) {
184     
185     map { $form->{$_} = $form->quote($form->{$_}) } qw(invnumber ordnumber quonumber shippingpoint shipvia notes intnotes);
186
187     foreach $ref (@{ $form->{invoice_details} } ) {
188       $i++;
189       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
190
191       $form->{"projectnumber_$i"} = qq|$ref->{projectnumber}--$ref->{project_id}|;
192       $form->{"partsgroup_$i"} = qq|$ref->{partsgroup}--$ref->{partsgroup_id}|;
193
194       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
195
196       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
197       $dec = length $dec;
198       $decimalplaces = ($dec > 2) ? $dec : 2;
199
200       $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
201       $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
202       $form->{"oldqty_$i"} = $form->{"qty_$i"};
203       
204       map { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) } qw(partnumber sku description unit);
205       $form->{rowcount} = $i;
206     }
207   }
208   
209 }
210
211
212
213 sub form_header {
214
215   # set option selected
216   foreach $item (qw(AR currency)) {
217     $form->{"select$item"} =~ s/ selected//;
218     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
219   }
220   
221   foreach $item (qw(customer department employee)) {
222     $form->{"select$item"} = $form->unescape($form->{"select$item"});
223     $form->{"select$item"} =~ s/ selected//;
224     $form->{"select$item"} =~ s/(<option value="\Q$form->{$item}\E")/$1 selected/;
225   }
226     
227   $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
228
229  
230   $exchangerate = "";
231   if ($form->{currency} ne $form->{defaultcurrency}) {
232     if ($form->{forex}) {
233       $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
234     } else {
235       $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
236     }
237   }
238   $exchangerate .= qq|
239 <input type=hidden name=forex value=$form->{forex}>
240 |;
241
242   if ($form->{selectcustomer}) {
243     $customer = qq|<select name=customer>$form->{selectcustomer}</select>
244                    <input type=hidden name="selectcustomer" value="|.
245                    $form->escape($form->{selectcustomer},1).qq|">|;
246   } else {
247     $customer = qq|<input name=customer value="$form->{customer}" size=35>|;
248   }
249   
250   $department = qq|
251               <tr>
252                 <th align="right" nowrap>|.$locale->text('Department').qq|</th>
253                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
254                 <input type=hidden name=selectdepartment value="|.
255                 $form->escape($form->{selectdepartment},1).qq|">
256                 </td>
257               </tr>
258 | if $form->{selectdepartment};
259
260
261   $n = ($form->{creditremaining} < 0) ? "0" : "1";
262
263
264   if ($form->{business}) {
265     $business = qq|
266               <tr>
267                 <th align=right>|.$locale->text('Business').qq|</th>
268                 <td>$form->{business}</td>
269                 <th align=right>|.$locale->text('Trade Discount').qq|</th>
270                 <td>|.$form->format_amount(\%myconfig, $form->{tradediscount} * 100).qq| %</td>
271               </tr>
272 |;
273   }
274
275
276   $form->header;
277
278   print qq|
279 <body>
280
281 <form method=post action="$form->{script}#end">
282
283 <input type=hidden name=id value=$form->{id}>
284
285 <input type=hidden name=type value=$form->{type}>
286 <input type=hidden name=media value=$form->{media}>
287 <input type=hidden name=format value=$form->{format}>
288
289 <input type=hidden name=queued value="$form->{queued}">
290 <input type=hidden name=printed value="$form->{printed}">
291 <input type=hidden name=emailed value="$form->{emailed}">
292
293 <input type=hidden name=title value="$form->{title}">
294 <input type=hidden name=vc value=$form->{vc}>
295
296 <input type=hidden name=terms value=$form->{terms}>
297
298 <input type=hidden name=discount value=$form->{discount}>
299 <input type=hidden name=creditlimit value=$form->{creditlimit}>
300 <input type=hidden name=creditremaining value=$form->{creditremaining}>
301
302 <input type=hidden name=tradediscount value=$form->{tradediscount}>
303 <input type=hidden name=business value="$form->{business}">
304
305 <input type=hidden name=closedto value=$form->{closedto}>
306 <input type=hidden name=locked value=$form->{locked}>
307
308 <input type=hidden name=shipped value=$form->{shipped}>
309
310 <input type=hidden name=oldtransdate value=$form->{oldtransdate}>
311
312
313 <table width=100%>
314   <tr class=listtop>
315     <th class=listtop>$form->{title}</th>
316   </tr>
317   <tr height="5"></tr>
318   <tr>
319     <td>
320       <table width=100%>
321         <tr valign=top>
322           <td>
323             <table>
324               <tr>
325                 <th align=right nowrap>|.$locale->text('Customer').qq|</th>
326                 <td colspan=3>$customer</td>
327                 <input type=hidden name=customer_id value=$form->{customer_id}>
328                 <input type=hidden name=oldcustomer value="$form->{oldcustomer}"> 
329               </tr>
330               <tr>
331                 <td></td>
332                 <td colspan=3>
333                   <table>
334                     <tr>
335                       <th nowrap>|.$locale->text('Credit Limit').qq|</th>
336                       <td>|.$form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0").qq|</td>
337                       <td width=20%></td>
338                       <th nowrap>|.$locale->text('Remaining').qq|</th>
339                       <td class="plus$n" nowrap>|.$form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0").qq|</td>
340                     </tr>
341                   </table>
342                 </td>
343               </tr>
344               $business
345               <tr>
346                 <th align=right nowrap>|.$locale->text('Record in').qq|</th>
347                 <td colspan=3><select name=AR>$form->{selectAR}</select></td>
348                 <input type=hidden name=selectAR value="$form->{selectAR}">
349               </tr>
350               $department
351               <tr>
352                 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
353                 <td><select name=currency>$form->{selectcurrency}</select></td>
354                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
355                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
356                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
357                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
358                 $exchangerate
359               </tr>
360               <tr>
361                 <th align=right nowrap>|.$locale->text('Shipping Point').qq|</th>
362                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
363               </tr>
364               <tr>
365                 <th align=right nowrap>|.$locale->text('Ship via').qq|</th>
366                 <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
367               </tr>
368             </table>
369           </td>
370           <td align=right>
371             <table>
372               <tr>
373                 <th align=right nowrap>|.$locale->text('Salesperson').qq|</th>
374                 <td><select name=employee>$form->{selectemployee}</select></td>
375                 <input type=hidden name=selectemployee value="|.
376                 $form->escape($form->{selectemployee},1).qq|">
377               </tr>
378               <tr>
379                 <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
380                 <td><input name=invnumber size=20 value="$form->{invnumber}"></td>
381               </tr>
382               <tr>
383                 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
384                 <td><input name=ordnumber size=20 value="$form->{ordnumber}"></td>
385 <input type=hidden name=quonumber value="$form->{quonumber}">
386               </tr>
387               <tr>
388                 <th align=right>|.$locale->text('Invoice Date').qq|</th>
389                 <td><input name=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
390               </tr>
391               <tr>
392                 <th align=right>|.$locale->text('Due Date').qq|</th>
393                 <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
394               </tr>
395             </table>
396           </td>
397         </tr>
398       </table>
399     </td>
400   </tr>
401   <tr>
402     <td>
403     </td>
404   </tr>
405 <!-- shipto are in hidden variables -->
406
407 <input type=hidden name=shiptoname value="$form->{shiptoname}">
408 <input type=hidden name=shiptoaddress1 value="$form->{shiptoaddress1}">
409 <input type=hidden name=shiptoaddress2 value="$form->{shiptoaddress2}">
410 <input type=hidden name=shiptocity value="$form->{shiptocity}">
411 <input type=hidden name=shiptostate value="$form->{shiptostate}">
412 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
413 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
414 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
415 <input type=hidden name=shiptophone value="$form->{shiptophone}">
416 <input type=hidden name=shiptofax value="$form->{shiptofax}">
417 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
418
419 <!-- email variables -->
420 <input type=hidden name=message value="$form->{message}">
421 <input type=hidden name=email value="$form->{email}">
422 <input type=hidden name=subject value="$form->{subject}">
423 <input type=hidden name=cc value="$form->{cc}">
424 <input type=hidden name=bcc value="$form->{bcc}">
425
426 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
427 |;
428
429   foreach $item (split / /, $form->{taxaccounts}) {
430     print qq|
431 <input type=hidden name="${item}_rate" value="$form->{"${item}_rate"}">
432 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
433 <input type=hidden name="${item}_taxnumber" value="$form->{"${item}_taxnumber"}">
434 |;
435   }
436
437 }
438
439
440
441 sub form_footer {
442
443   $form->{invtotal} = $form->{invsubtotal};
444
445   if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) {
446     $rows = 2;
447   }
448   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
449     $introws = 2;
450   }
451   $rows = ($rows > $introws) ? $rows : $introws;
452   $notes = qq|<textarea name=notes rows=$rows cols=26 wrap=soft>$form->{notes}</textarea>|;
453   $intnotes = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
454
455   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
456
457   $taxincluded = "";
458   if ($form->{taxaccounts}) {
459     $taxincluded = qq|
460               <tr height="5"></tr>
461               <tr>
462                 <td align=right>
463                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td><th align=left>|.$locale->text('Tax Included').qq|</th>
464              </tr>
465 |;
466   }
467
468   if (!$form->{taxincluded}) {
469     
470     foreach $item (split / /, $form->{taxaccounts}) {
471       if ($form->{"${item}_base"}) {
472         $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
473         $form->{invtotal} += $form->{"${item}_total"};
474         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
475         
476         $tax .= qq|
477               <tr>
478                 <th align=right>$form->{"${item}_description"}</th>
479                 <td align=right>$form->{"${item}_total"}</td>
480               </tr>
481 |;
482       }
483     }
484
485     $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
486     
487     $subtotal = qq|
488               <tr>
489                 <th align=right>|.$locale->text('Subtotal').qq|</th>
490                 <td align=right>$form->{invsubtotal}</td>
491               </tr>
492 |;
493
494   }
495
496   $form->{oldinvtotal} = $form->{invtotal};
497   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
498   
499   print qq|
500   <tr>
501     <td>
502       <table width=100%>
503         <tr valign=bottom>
504           <td>
505             <table>
506               <tr>
507                 <th align=left>|.$locale->text('Notes').qq|</th>
508                 <th align=left>|.$locale->text('Internal Notes').qq|</th>
509               </tr>
510               <tr valign=top>
511                 <td>$notes</td>
512                 <td>$intnotes</td>
513               </tr>
514             </table>
515           </td>
516           <td align=right>
517             <table>
518               $subtotal
519               $tax
520               <tr>
521                 <th align=right>|.$locale->text('Total').qq|</th>
522                 <td align=right>$form->{invtotal}</td>
523               </tr>
524               $taxincluded
525             </table>
526           </td>
527         </tr>
528       </table>
529     </td>
530   </tr>
531   <tr>
532     <td>
533       <table width=100%>
534         <tr class=listheading>
535           <th colspan=6 class=listheading>|.$locale->text('Payments')
536           .qq|</th>
537         </tr>
538 |;
539
540   if ($form->{currency} eq $form->{defaultcurrency}) {
541     @column_index = qw(datepaid source memo paid AR_paid);
542   } else {
543     @column_index = qw(datepaid source memo paid exchangerate AR_paid);
544   }
545
546   $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
547   $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
548   $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
549   $column_data{AR_paid} = "<th>".$locale->text('Account')."</th>";
550   $column_data{source} = "<th>".$locale->text('Source')."</th>";
551   $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
552   
553   print "
554         <tr>
555 ";
556   map { print "$column_data{$_}\n" } @column_index;
557   print "
558         </tr>
559 ";
560   
561   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
562   for $i (1 .. $form->{paidaccounts}) {
563
564     print "
565         <tr>\n";
566
567     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
568     $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
569     
570     # format amounts
571     $totalpaid += $form->{"paid_$i"};
572     $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
573     $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
574
575     $exchangerate = qq|&nbsp;|;
576     if ($form->{currency} ne $form->{defaultcurrency}) {
577       if ($form->{"forex_$i"}) {
578         $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
579       } else {
580         $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
581       }
582     }
583
584     $exchangerate .= qq|
585 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
586 |;
587
588     $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
589     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
590     $column_data{"AR_paid_$i"} = qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
591     $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
592     $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
593     $column_data{"memo_$i"} = qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
594
595     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
596     print "
597         </tr>\n";
598   }
599
600   print qq|
601 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
602 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
603 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
604 <input type=hidden name=oldtotalpaid value=$totalpaid>
605       </table>
606     </td>
607   </tr>
608   <tr>
609     <td><hr size=3 noshade></td>
610   </tr>
611   <tr>
612     <td>
613 |;
614
615   &print_options;
616
617   print qq|
618     </td>
619   </tr>
620 </table>
621 <br>
622 |;
623
624
625   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
626   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
627
628   if (! $form->{readonly}) {
629     
630     if ($form->{id}) {
631       
632       print qq|
633       <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
634       <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
635       <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
636       <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
637 |;
638
639         if (! $form->{locked}) {
640           print qq|
641         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
642
643           if ($latex) {
644             print qq|
645         <input class=submit type=submit name=action value="|.$locale->text('Print and Post').qq|">|;
646           }
647
648           print qq|
649         <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
650 |;
651         }
652
653         print qq|
654       <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
655       <input class=submit type=submit name=action value="|.$locale->text('Sales Order').qq|">
656 |;
657
658     } else {
659
660       if ($transdate > $closedto) {
661         print qq|
662         <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
663         <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
664         <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
665         <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
666         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
667
668         if ($latex) {
669           print qq|
670         <input class=submit type=submit name=action value="|.$locale->text('Print and Post').qq|">|;
671         }
672       }
673     }
674   }
675
676   if ($form->{menubar}) {
677     require "$form->{path}/menu.pl";
678     &menubar;
679   }
680
681   print qq|
682
683 <input type=hidden name=rowcount value=$form->{rowcount}>
684
685 <input name=callback type=hidden value="$form->{callback}">
686
687 <input type=hidden name=path value=$form->{path}>
688 <input type=hidden name=login value=$form->{login}>
689 <input type=hidden name=sessionid value=$form->{sessionid}>
690
691 </form>
692
693 <a name="end"></a>
694
695 </body>
696 </html>
697 |;
698
699 }
700
701
702 sub update {
703
704   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate);
705   
706   &check_name(customer);
707
708   if ($form->{transdate} ne $form->{oldtransdate}) {
709     $form->{duedate} = $form->current_date(\%myconfig, $form->{transdate}, $form->{terms} * 1);
710     $form->{oldtransdate} = $form->{transdate};
711   }
712
713
714   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'buy')));
715
716   $j = 1;
717   for $i (1 .. $form->{paidaccounts}) {
718     if ($form->{"paid_$i"}) {
719       map { $form->{"${_}_$j"} = $form->{"${_}_$i"} } qw(datepaid source memo);
720       map { $form->{"${_}_$j"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
721
722       $form->{"exchangerate_$j"} = $exchangerate if ($form->{"forex_$j"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$j"}, 'buy')));
723       map { delete $form->{"${_}_$i"} } qw(datepaid source memo paid exchangerate forex) if $j != $i;
724       $j++;
725     } else {
726       map { delete $form->{"${_}_$i"} } qw(datepaid source memo paid exchangerate forex);
727     }
728     $form->{paidaccounts} = $j;
729   }
730
731   $i = $form->{rowcount};
732   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
733
734   foreach $item (qw(partsgroup projectnumber)) {
735     $form->{"select$item"} = $form->unescape($form->{"select$item"}) if $form->{"select$item"};
736   }
737     
738   # if last row empty, check the form otherwise retrieve new item
739   if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
740
741     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
742     &check_form;
743
744   } else {
745
746     IS->retrieve_item(\%myconfig, \%$form);
747     
748     $rows = scalar @{ $form->{item_list} };
749
750     $form->{"discount_$i"}      = $form->format_amount(\%myconfig, $form->{discount} * 100);
751
752     if ($rows > 0) {
753       $form->{"qty_$i"}         = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
754       
755       if ($rows > 1) {
756         
757         &select_item;
758         exit;
759         
760       } else {
761
762         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
763         
764         map { $form->{item_list}[$i]{$_} = $form->quote($form->{item_list}[$i]{$_}) } qw(partnumber description unit);
765         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
766         
767         $form->{"discount_$i"} = $form->{discount} * 100;
768         
769         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
770         ($dec) = ($s =~ /\.(\d+)/);
771         $dec = length $dec;
772         $decimalplaces = ($dec > 2) ? $dec : 2;
773
774         if ($sellprice) {
775           $form->{"sellprice_$i"} = $sellprice;
776         } else {
777           # if there is an exchange rate adjust sellprice
778           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
779           $form->{"sellprice_$i"} /= $exchangerate;
780         }
781         
782         map { $form->{"${_}_$i"} /= $exchangerate } qw(listprice lastcost);
783
784         $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
785         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
786         map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
787         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
788         
789         $form->{creditremaining} -= $amount;
790         
791         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice lastcost);
792         
793         $form->{"qty_$i"} =  $form->format_amount(\%myconfig, $form->{"qty_$i"});
794       }
795
796       &display_form;
797
798     } else {
799       # ok, so this is a new part
800       # ask if it is a part or service item
801
802       if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
803         $form->{rowcount}--;
804         $form->{"discount_$i"} = "";
805         &display_form;
806       } else {
807         
808         $form->{"id_$i"}          = 0;
809         $form->{"unit_$i"}        = $locale->text('ea');
810
811         &new_item;
812         
813       }
814     }
815   }
816 }
817
818
819
820 sub post {
821
822   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
823   $form->isblank("customer", $locale->text('Customer missing!'));
824
825   # if oldcustomer ne customer redo form
826   if (&check_name(customer)) {
827     &update;
828     exit;
829   }
830
831   &validate_items;
832
833   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
834   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
835   
836   $form->error($locale->text('Cannot post invoice for a closed period!')) if ($transdate <= $closedto);
837
838   $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
839   
840   for $i (1 .. $form->{paidaccounts}) {
841     if ($form->{"paid_$i"}) {
842       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
843
844       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
845       
846       $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
847
848       if ($form->{currency} ne $form->{defaultcurrency}) {
849         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
850         $form->isblank("exchangerate_$i", $locale->text('Exchange rate for payment missing!'));
851       }
852     }
853   }
854
855   
856   ($form->{AR}) = split /--/, $form->{AR};
857   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
858   
859   $form->{label} = $locale->text('Invoice');
860
861   $form->{id} = 0 if $form->{postasnew};
862   
863   $form->{invnumber} = $form->update_defaults(\%myconfig, "sinumber") unless $form->{invnumber};
864
865   $form->redirect($locale->text('Invoice posted!')) if (IS->post_invoice(\%myconfig, \%$form));
866   $form->error($locale->text('Cannot post invoice!'));
867     
868 }
869
870
871 sub print_and_post {
872
873   $form->error($locale->text('Select postscript or PDF!')) if $form->{format} !~ /(postscript|pdf)/;
874   $form->error($locale->text('Select Printer or Queue!')) if $form->{media} eq 'screen';
875
876   $old_form = new Form;
877   $form->{display_form} = "post";
878   map { $old_form->{$_} = $form->{$_} } keys %$form;
879   $old_form->{rowcount}++;
880
881   &print_form($old_form);
882
883 }
884
885
886
887 sub delete {
888
889   $form->header;
890
891   print qq|
892 <body>
893
894 <form method=post action=$form->{script}>
895 |;
896
897   # delete action variable
898   map { delete $form->{$_} } qw(action header);
899
900   $form->hide_form();
901
902   print qq|
903 <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
904
905 <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}
906 </h4>
907
908 <p>
909 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
910 </form>
911 |;
912
913
914 }
915
916
917
918 sub yes {
919
920   $form->redirect($locale->text('Invoice deleted!')) if (IS->delete_invoice(\%myconfig, \%$form, $spool));
921   $form->error($locale->text('Cannot delete invoice!'));
922
923 }
924
925
926 sub redirect {
927
928   $form->redirect;
929   $form->error($locale->text('Invoice processed!'));
930
931 }
932