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