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