This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / sql-ledger / bin / mozilla / ir.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 received module
25 #
26 #======================================================================
27
28
29 use SL::IR;
30 use SL::PE;
31
32 require "$form->{path}/io.pl";
33 require "$form->{path}/arap.pl";
34
35 1;
36 # end of main
37
38
39
40 sub add {
41
42   $form->{title} = $locale->text('Add Vendor Invoice');
43
44   $form->{callback} = "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&sessionid=$form->{sessionid}" unless $form->{callback};
45   &invoice_links;
46   &prepare_invoice;
47   &display_form;
48   
49 }
50
51
52 sub edit {
53
54   $form->{title} = $locale->text('Edit Vendor Invoice');
55
56   &invoice_links;
57   &prepare_invoice;
58   &display_form;
59   
60 }
61
62
63 sub invoice_links {
64   
65   $form->{vc} = "vendor";
66
67   # create links
68   $form->create_links("AP", \%myconfig, "vendor");
69   
70   # currencies
71   @curr = split /:/, $form->{currencies};
72   chomp $curr[0];
73   $form->{defaultcurrency} = $curr[0];
74
75   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
76
77   if ($form->{all_vendor}) {
78     unless ($form->{vendor_id}) {
79       $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
80     }
81   }
82
83   IR->get_vendor(\%myconfig, \%$form);
84   delete $form->{notes};
85   IR->retrieve_invoice(\%myconfig, \%$form);
86
87   $form->{oldlanguage_code} = $form->{language_code};
88
89   $form->get_partsgroup(\%myconfig, { language_code => $form->{language_code} });
90   if (@ { $form->{all_partsgroup} }) {
91     $form->{selectpartsgroup} = "<option>\n";
92     foreach $ref (@ { $form->{all_partsgroup} }) {
93       if ($ref->{translation}) {
94         $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{translation}\n|;
95       } else {
96         $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{partsgroup}\n|;
97       }
98     }
99   }
100
101   if (@{ $form->{all_projects} }) { 
102     $form->{selectprojectnumber} = "<option>\n";
103     map { $form->{selectprojectnumber} .= qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n| } @{ $form->{all_projects} };
104   }
105
106   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
107   $form->{oldtransdate} = $form->{transdate};
108
109   # vendors
110   if ($form->{all_vendor}) {
111     $form->{vendor} = "$form->{vendor}--$form->{vendor_id}";
112     map { $form->{selectvendor} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } (@{ $form->{all_vendor} });
113   }
114
115   # departments
116   if ($form->{all_departments}) {
117     $form->{selectdepartment} = "<option>\n";
118     $form->{department} = "$form->{department}--$form->{department_id}";
119
120     map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
121   }
122
123   if (@{ $form->{all_languages} }) {
124     $form->{selectlanguage} = "<option>\n";
125     map { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| } @{ $form->{all_languages} };
126   }
127   
128   # forex
129   $form->{forex} = $form->{exchangerate};
130   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
131   
132   foreach $key (keys %{ $form->{AP_links} }) {
133
134     foreach $ref (@{ $form->{AP_links}{$key} }) {
135       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
136     }
137
138     if ($key eq "AP_paid") {
139       for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
140         $form->{"AP_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
141         # reverse paid
142         $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount};
143         $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
144         $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
145         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
146         $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
147
148         $form->{paidaccounts} = $i;
149       }
150     } else {
151       $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
152     }
153     
154   }
155
156   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
157
158   $form->{AP} = $form->{AP_1} unless $form->{id};
159
160   $form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum($form->{transdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
161
162   $form->{readonly} = 1 if $myconfig{acs} =~ /AP--Vendor Invoice/;
163   
164 }
165
166
167
168 sub prepare_invoice {
169
170   $form->{type} = "invoice";
171   $form->{oldcurrency} = $form->{currency};
172
173   if ($form->{id}) {
174         
175     map { $form->{$_} = $form->quote($form->{$_}) } qw(invnumber ordnumber quonumber);
176
177     foreach $ref (@{ $form->{invoice_details} }) {
178       $i++;
179       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
180
181       $form->{"projectnumber_$i"} = qq|$ref->{projectnumber}--$ref->{project_id}|;
182       $form->{"partsgroup_$i"} = qq|$ref->{partsgroup}--$ref->{partsgroup_id}|;
183
184       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
185       
186       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
187       $dec = length $dec;
188       $decimalplaces = ($dec > 2) ? $dec : 2;
189       
190       $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
191       $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
192       $form->{"oldqty_$i"} = $form->{"qty_$i"};
193
194       map { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) } qw(partnumber sku description unit);
195
196       $form->{rowcount} = $i;
197     }
198   }
199   
200 }
201
202
203
204 sub form_header {
205
206   # set option selected
207   foreach $item (qw(AP currency)) {
208     $form->{"select$item"} =~ s/ selected//;
209     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
210   }
211   
212   foreach $item (qw(vendor department)) {
213     $form->{"select$item"} = $form->unescape($form->{"select$item"});
214     $form->{"select$item"} =~ s/ selected//;
215     $form->{"select$item"} =~ s/(<option value="\Q$form->{$item}\E")/$1 selected/;
216   }
217
218   if ($form->{selectlanguage}) {
219     $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
220     $form->{"selectlanguage"} =~ s/ selected//;
221     $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
222
223     $lang = qq|
224               <tr>
225                 <th align=right nowrap>|.$locale->text('Language').qq|</th>
226                 <td><select name=language_code>$form->{selectlanguage}</select></td>
227                 <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
228                 <input type=hidden name="selectlanguage" value="|.
229                 $form->escape($form->{selectlanguage},1).qq|">
230               </tr>
231 |;
232
233   }
234   
235
236   $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
237
238   $exchangerate = "";
239   if ($form->{currency} ne $form->{defaultcurrency}) {
240     if ($form->{forex}) {
241       $exchangerate .= qq|
242                 <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
243                 <td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>
244 |;
245     } else {
246       $exchangerate .= qq|
247                 <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
248                 <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
249 |;
250     }
251   }
252   $exchangerate .= qq|
253 <input type=hidden name=forex value=$form->{forex}>
254 |;
255   
256   if ($form->{selectvendor}) {
257     $vendor = qq|<select name=vendor>$form->{selectvendor}</select>
258                  <input type=hidden name="selectvendor" value="|.
259                  $form->escape($form->{selectvendor},1).qq|">|;
260   } else {
261     $vendor = qq|<input name=vendor value="$form->{vendor}" size=35>|;
262   }
263   
264   $department = qq|
265               <tr>
266               <th align="right" nowrap>|.$locale->text('Department').qq|</th>
267               <td colspan=3><select name=department>$form->{selectdepartment}</select>
268               <input type=hidden name=selectdepartment value="|.
269               $form->escape($form->{selectdepartment},1).qq|">
270               </td>
271             </tr>
272 | if $form->{selectdepartment};
273
274   $n = ($form->{creditremaining} < 0) ? "0" : "1";
275
276
277   $form->header;
278
279   print qq|
280 <body>
281
282 <form method=post action="$form->{script}#end">
283
284 <input type=hidden name=id value=$form->{id}>
285 <input type=hidden name=title value="$form->{title}">
286 <input type=hidden name=vc value="vendor">
287 <input type=hidden name=type value=$form->{type}>
288
289 <input type=hidden name=terms value=$form->{terms}>
290
291 <input type=hidden name=creditlimit value=$form->{creditlimit}>
292 <input type=hidden name=creditremaining value=$form->{creditremaining}>
293
294 <input type=hidden name=closedto value=$form->{closedto}>
295 <input type=hidden name=locked value=$form->{locked}>
296
297 <input type=hidden name=shipped value=$form->{shipped}>
298
299 <input type=hidden name=oldtransdate value=$form->{oldtransdate}>
300
301 <table width=100%>
302   <tr class=listtop>
303     <th>$form->{title}</th>
304   </tr>
305   <tr height="5"></tr>
306   <tr>
307     <td>
308       <table width=100%>
309         <tr valign=top>
310           <td>
311             <table>
312               <tr>
313                 <th align=right nowrap>|.$locale->text('Vendor').qq|</th>
314                 <td colspan=3>$vendor</td>
315                 
316                 <input type=hidden name=vendor_id value=$form->{vendor_id}>
317                 <input type=hidden name=oldvendor value="$form->{oldvendor}">
318
319               </tr>
320               <tr>
321                 <td></td>
322                 <td colspan=3>
323                   <table>
324                     <tr>
325                       <th nowrap>|.$locale->text('Credit Limit').qq|</th>
326                       <td>|.$form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0").qq|</td>
327                       <td width=20%></td>
328                       <th nowrap>|.$locale->text('Remaining').qq|</th>
329                       <td class="plus$n" nowrap>|.$form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0").qq|</td>
330                     </tr>
331                   </table>
332                 </td>
333               <tr>
334                 <th align=right>|.$locale->text('Record in').qq|</th>
335                 <td colspan=3><select name=AP>$form->{selectAP}</select></td>
336                 <input type=hidden name=selectAP value="$form->{selectAP}">
337               </tr>
338               $department
339               <tr>
340                 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
341                 <td><select name=currency>$form->{selectcurrency}</select></td>
342                 $exchangerate
343               </tr>
344             </table>
345           </td>
346           <td align=right>
347             <table>
348               <tr>
349                 <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
350                 <td><input name=invnumber size=20 value="$form->{invnumber}"></td>
351               </tr>
352               <tr>
353                 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
354                 <td><input name=ordnumber size=20 value="$form->{ordnumber}"></td>
355 <input type=hidden name=quonumber value="$form->{quonumber}">
356               </tr>
357               <tr>
358                 <th align=right nowrap>|.$locale->text('Invoice Date').qq|</th>
359                 <td><input name=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
360               </tr>
361               <tr>
362                 <th align=right nowrap>|.$locale->text('Due Date').qq|</th>
363                 <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
364               </tr>
365               $lang
366             </table>
367           </td>
368         </tr>
369       </table>
370     </td>
371   </tr>
372 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
373 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
374 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
375 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
376
377 <input type=hidden name=taxpart value="$form->{taxpart}">
378 <input type=hidden name=taxservice value="$form->{taxservice}">
379
380 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
381 |;
382
383   foreach $item (split / /, $form->{taxaccounts}) {
384     print qq|
385 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
386 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
387 |;
388   }
389
390 }
391
392
393
394 sub form_footer {
395
396   $form->{invtotal} = $form->{invsubtotal};
397   
398   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
399     $rows = 2;
400   }
401   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
402     $introws = 2;
403   }
404   $rows = ($rows > $introws) ? $rows : $introws;
405   $notes = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
406   $intnotes = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
407   
408   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
409
410   $taxincluded = "";
411   if ($form->{taxaccounts}) {
412     $taxincluded = qq|
413                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b>
414 |;
415   }
416   
417   if (!$form->{taxincluded}) {
418     
419     foreach $item (split / /, $form->{taxaccounts}) {
420       if ($form->{"${item}_base"}) {
421         $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
422         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
423         
424         $tax .= qq|
425                 <tr>
426                   <th align=right>$form->{"${item}_description"}</th>
427                   <td align=right>$form->{"${item}_total"}</td>
428                 </tr>
429 |;
430       }
431     }
432
433     $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
434     
435     $subtotal = qq|
436               <tr>
437                 <th align=right>|.$locale->text('Subtotal').qq|</th>
438                 <td align=right>$form->{invsubtotal}</td>
439               </tr>
440 |;
441
442   }
443
444
445   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
446
447   print qq|
448   <tr>
449     <td>
450       <table width=100%>
451         <tr valign=bottom>
452           <td>
453             <table>
454               <tr>
455                 <th align=left>|.$locale->text('Notes').qq|</th>
456                 <th align=left>|.$locale->text('Internal Notes').qq|</th>
457               </tr>
458               <tr valign=top>
459                 <td>$notes</td>
460                 <td>$intnotes</td>
461               </tr>
462             </table>
463           </td>
464           <td align=right>
465             $taxincluded
466             <br>
467             <table>
468               $subtotal
469               $tax
470               <tr>
471                 <th align=right>|.$locale->text('Total').qq|</th>
472                 <td align=right>$form->{invtotal}</td>
473               </tr>
474             </table>
475           </td>
476         </tr>
477       </table>
478     </td>
479   </tr>
480   <tr>
481     <td>
482       <table width=100%>
483         <tr>
484           <th colspan=6 class=listheading>|.$locale->text('Payments').qq|</th>
485         </tr>
486 |;
487
488     if ($form->{currency} eq $form->{defaultcurrency}) {
489       @column_index = qw(datepaid source memo paid AP_paid);
490     } else {
491       @column_index = qw(datepaid source memo paid exchangerate AP_paid);
492     }
493
494     $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
495     $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
496     $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
497     $column_data{AP_paid} = "<th>".$locale->text('Account')."</th>";
498     $column_data{source} = "<th>".$locale->text('Source')."</th>";
499     $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
500
501     print qq|
502         <tr>
503 |;
504     map { print "$column_data{$_}\n" } @column_index;
505     print qq|
506         </tr>
507 |;
508
509     $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
510     for $i (1 .. $form->{paidaccounts}) {
511
512       print qq|
513         <tr>
514 |;
515
516       $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
517       $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
518
519       # format amounts
520       $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
521       $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
522
523       $exchangerate = qq|&nbsp;|;
524       if ($form->{currency} ne $form->{defaultcurrency}) {
525         if ($form->{"forex_$i"}) {
526           $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
527         } else {
528           $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
529         }
530       }
531       $exchangerate .= qq|
532 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
533 |;
534
535       $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
536       $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
537       $column_data{"AP_paid_$i"} = qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
538       $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
539       $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
540       $column_data{"memo_$i"} = qq|<td align=center><input name="memo_$i" size=11 value=$form->{"memo_$i"}></td>|;
541
542       map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
543
544       print qq|
545         </tr>
546 |;
547     }
548     
549     print qq|
550             <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
551             <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
552       </table>
553     </td>
554   </tr>
555   <tr>
556     <td><hr size=3 noshade></td>
557   </tr>
558 </table>
559 <br>
560 |;
561
562   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
563   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
564   
565   if (! $form->{readonly}) {
566     if ($form->{id}) {
567       print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
568 |;
569
570       if (!$form->{locked}) {
571         print qq|
572         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
573         <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
574 |;
575       }
576
577       print qq|
578         <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
579         <input class=submit type=submit name=action value="|.$locale->text('Purchase Order').qq|">
580 |;
581
582     } else {
583       if ($transdate > $closedto) {
584         print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
585         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
586       }
587     }
588   }
589
590   if ($form->{menubar}) {
591     require "$form->{path}/menu.pl";
592     &menubar;
593   }
594   
595 print qq|
596
597 <input type=hidden name=rowcount value=$form->{rowcount}>
598
599 <input name=callback type=hidden value="$form->{callback}">
600
601 <input type=hidden name=path value=$form->{path}>
602 <input type=hidden name=login value=$form->{login}>
603 <input type=hidden name=sessionid value=$form->{sessionid}>
604
605 </form>
606
607 <a name="end"></a>
608
609 </body>
610 </html>
611 |;
612
613 }
614
615
616
617 sub update {
618
619   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate);
620   
621   &check_name(vendor);
622
623   if ($form->{transdate} ne $form->{oldtransdate}) {
624     $form->{duedate} = $form->current_date(\%myconfig, $form->{transdate}, $form->{terms} * 1);
625     $form->{oldtransdate} = $form->{transdate};
626   }
627
628
629   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'sell')));
630   
631   $j = 1;
632   for $i (1 .. $form->{paidaccounts}) {
633     if ($form->{"paid_$i"}) {
634       map { $form->{"${_}_$j"} = $form->{"${_}_$i"} } qw(datepaid source memo);
635       map { $form->{"${_}_$j"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
636
637       $form->{"exchangerate_$j"} = $exchangerate if ($form->{"forex_$j"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$j"}, 'sell')));
638       map { delete $form->{"${_}_$i"} } qw(datepaid source memo paid exchangerate forex) if $j != $i;
639     } else {
640       map { delete $form->{"${_}_$i"} } qw(datepaid source memo paid exchangerate forex);
641     }
642     $form->{paidaccounts} = $j;
643   }
644   
645   $i = $form->{rowcount};
646   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
647
648   foreach $item (qw(partsgroup projectnumber)) {
649     $form->{"select$item"} = $form->unescape($form->{"select$item"}) if $form->{"select$item"};
650   }
651   
652   if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
653
654     &check_form;
655     
656   } else {
657    
658     $form->{transdate} = $form->{oldtransdate};
659     IR->retrieve_item(\%myconfig, \%$form);
660
661     my $rows = scalar @{ $form->{item_list} };
662
663     if ($rows) {
664       $form->{"qty_$i"}                     = 1 unless ($form->{"qty_$i"});
665       
666       if ($rows > 1) {
667         
668         &select_item;
669         exit;
670         
671       } else {
672         # override sellprice if there is one entered
673         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
674
675         map { $form->{item_list}[$i]{$_} = $form->quote($form->{item_list}[$i]{$_}) } qw(partnumber description unit);
676         
677         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
678
679         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
680         
681         ($dec) = ($s =~ /\.(\d+)/);
682         $dec = length $dec;
683         $decimalplaces = ($dec > 2) ? $dec : 2;
684  
685         if ($sellprice) {
686           $form->{"sellprice_$i"} = $sellprice;
687         } else {
688           # if there is an exchange rate adjust sellprice
689           $form->{"sellprice_$i"} /= $exchangerate;
690         }
691      
692         $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
693         $form->{"qty_$i"} =  $form->format_amount(\%myconfig, $form->{"qty_$i"});
694       }
695
696       &display_form;
697
698     } else {
699       # ok, so this is a new part
700       # ask if it is a part or service item
701
702       if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
703         $form->{rowcount}--;
704         $form->{"discount_$i"} = "";
705         &display_form;
706       } else {
707         
708         $form->{"id_$i"}        = 0;
709         $form->{"unit_$i"}      = $locale->text('ea');
710
711         &new_item;
712
713       }
714     }
715   }
716 }
717
718
719
720 sub post {
721
722   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
723   $form->isblank("vendor", $locale->text('Vendor missing!'));
724   
725   # if the vendor changed get new values
726   if (&check_name(vendor)) {
727     &update;
728     exit;
729   }
730
731   &validate_items;
732
733   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
734   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
735
736   $form->error($locale->text('Cannot post invoice for a closed period!')) if ($transdate <= $closedto);
737
738   $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
739   
740   for $i (1 .. $form->{paidaccounts}) {
741     if ($form->{"paid_$i"}) {
742       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
743
744       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
745       
746       $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
747       
748       if ($form->{currency} ne $form->{defaultcurrency}) {
749         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
750         $form->isblank("exchangerate_$i", $locale->text('Exchange rate for payment missing!'));
751       }
752     }
753   }
754   
755   
756   ($form->{AP}) = split /--/, $form->{AP};
757   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
758   
759   $form->{id} = 0 if $form->{postasnew};
760
761   $form->{invnumber} = $form->update_defaults(\%myconfig, "vinumber") unless $form->{invnumber};
762
763   $form->redirect($locale->text('Invoice')." $form->{invnumber} ".$locale->text('posted!')) if (IR->post_invoice(\%myconfig, \%$form));
764   $form->error($locale->text('Cannot post invoice!'));
765   
766 }
767
768
769
770 sub delete {
771
772   $form->header;
773
774   print qq|
775 <body>
776
777 <form method=post action=$form->{script}>
778 |;
779
780   # delete action variable
781   map { delete $form->{$_} } qw(action header);
782
783   $form->hide_form();
784
785   print qq|
786 <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
787
788 <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}</h4>
789 <p>
790 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
791 </form>
792 |;
793
794
795 }
796
797
798
799 sub yes {
800
801   $form->redirect($locale->text('Invoice deleted!')) if (IR->delete_invoice(\%myconfig, \%$form));
802   $form->error($locale->text('Cannot delete invoice!'));
803
804 }
805
806