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