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