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