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