Verlagern der USTVA Kennziffern auf einen zentralen Datenbankeintrag.
[kivitendo-erp.git] / bin / mozilla / ir.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 received module
31 #
32 #======================================================================
33
34 use SL::IR;
35 use SL::IS;
36 use SL::PE;
37
38 require "bin/mozilla/io.pl";
39 require "bin/mozilla/arap.pl";
40 require "bin/mozilla/common.pl";
41 require "bin/mozilla/drafts.pl";
42
43 1;
44
45 # end of main
46
47 sub add {
48   $lxdebug->enter_sub();
49
50   return $lxdebug->leave_sub() if (load_draft_maybe());
51
52   $form->{title} = $locale->text('Add Vendor Invoice');
53
54   &invoice_links;
55   &prepare_invoice;
56   &display_form;
57
58   $lxdebug->leave_sub();
59 }
60
61 sub edit {
62   $lxdebug->enter_sub();
63   
64   # show history button
65   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
66   #/show hhistory button
67   
68   $form->{title} = $locale->text('Edit Vendor Invoice');
69
70   &invoice_links;
71   &prepare_invoice;
72   &display_form;
73
74   $lxdebug->leave_sub();
75 }
76
77 sub invoice_links {
78   $lxdebug->enter_sub();
79
80   # create links
81   $form->{webdav} = $webdav;
82
83   # set jscalendar
84   $form->{jscalendar} = $jscalendar;
85
86   $form->create_links("AP", \%myconfig, "vendor");
87
88   #quote all_vendor Bug 133
89   foreach $ref (@{ $form->{all_vendor} }) {
90     $ref->{name} = $form->quote($ref->{name});
91   }
92
93   if ($form->{all_vendor}) {
94     unless ($form->{vendor_id}) {
95       $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
96     }
97   }
98   if ($form->{payment_id}) {
99     $payment_id = $form->{payment_id};
100   }
101   if ($form->{language_id}) {
102     $language_id = $form->{language_id};
103   }
104   if ($form->{taxzone_id}) {
105     $taxzone_id = $form->{taxzone_id};
106   }
107
108   $cp_id = $form->{cp_id};
109   IR->get_vendor(\%myconfig, \%$form);
110   IR->retrieve_invoice(\%myconfig, \%$form);
111   $form->{cp_id} = $cp_id;
112  
113   if ($payment_id) {
114     $form->{payment_id} = $payment_id;
115   }
116   if ($language_id) {
117     $form->{language_id} = $language_id;
118   }
119   if ($taxzone_id) {
120     $form->{taxzone_id} = $taxzone_id;
121   }
122
123   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
124
125   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
126
127   # vendors
128   if (@{ $form->{all_vendor} }) {
129     $form->{vendor} = "$form->{vendor}--$form->{vendor_id}";
130     map { $form->{selectvendor} .= "<option>$_->{name}--$_->{id}\n" }
131       (@{ $form->{all_vendor} });
132   }
133
134   # departments
135   if ($form->{all_departments}) {
136     $form->{selectdepartment} = "<option>\n";
137     $form->{department}       = "$form->{department}--$form->{department_id}";
138
139     map {
140       $form->{selectdepartment} .=
141         "<option>$_->{description}--$_->{id}\n"
142     } (@{ $form->{all_departments} });
143   }
144
145   # forex
146   $form->{forex} = $form->{exchangerate};
147   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
148
149   foreach $key (keys %{ $form->{AP_links} }) {
150
151     foreach $ref (@{ $form->{AP_links}{$key} }) {
152       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
153     }
154
155     if ($key eq "AP_paid") {
156       for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
157         $form->{"AP_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};
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->{AP} = $form->{AP_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   if ($form->{id}) {
193
194     map { $form->{$_} =~ s/\"/&quot;/g } qw(invnumber ordnumber quonumber);
195
196     my $i = 0;
197     foreach $ref (@{ $form->{invoice_details} }) {
198       $i++;
199       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
200
201       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
202       $dec           = length $dec;
203       $decimalplaces = ($dec > 2) ? $dec : 2;
204
205       $form->{"sellprice_$i"} =
206         $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
207                              $decimalplaces);
208
209       (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
210       $dec_qty = length $dec_qty;
211
212       $form->{"qty_$i"} =
213         $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
214
215       $form->{rowcount} = $i;
216     }
217   }
218
219   $lxdebug->leave_sub();
220 }
221
222 sub form_header {
223   $lxdebug->enter_sub();
224
225   # set option selected
226   foreach $item (qw(AP vendor currency department)) {
227     $form->{"select$item"} =~ s/ selected//;
228     $form->{"select$item"} =~
229       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
230   }
231
232   $form->{radier} =
233     ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
234
235   #quote selectvendor Bug 133
236   $form->{"selectvendor"} = $form->quote($form->{"selectvendor"});
237
238   #substitute \n and \r to \s (bug 543)
239   $form->{"selectvendor"} =~ s/[\n\r]/&nbsp;/g;
240   
241   $form->{exchangerate} =
242     $form->format_amount(\%myconfig, $form->{exchangerate});
243
244   $form->{creditlimit} =
245     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
246   $form->{creditremaining} =
247     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
248
249   $exchangerate = "";
250   if ($form->{currency} ne $form->{defaultcurrency}) {
251     if ($form->{forex}) {
252       $exchangerate .= qq|
253                 <th align=right nowrap>|
254         . $locale->text('Exchangerate') . qq|</th>
255                 <td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>
256 |;
257     } else {
258       $exchangerate .= qq|
259                 <th align=right nowrap>|
260         . $locale->text('Exchangerate') . qq|</th>
261                 <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
262 |;
263     }
264   }
265   $exchangerate .= qq|
266 <input type=hidden name=forex value=$form->{forex}>
267 |;
268
269   my @old_project_ids = ($form->{"globalproject_id"});
270   map({ push(@old_project_ids, $form->{"project_id_$_"})
271           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
272
273   $form->get_lists("contacts" => "ALL_CONTACTS",
274                    "projects" => { "key" => "ALL_PROJECTS",
275                                    "all" => 0,
276                                    "old_id" => \@old_project_ids },
277                    "taxzones" => "ALL_TAXZONES",
278                    "currencies" => "ALL_CURRENCIES");
279
280   my %labels;
281   my @values = (undef);
282   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
283     push(@values, $item->{"cp_id"});
284     $labels{$item->{"cp_id"}} = $item->{"cp_name"} .
285       ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
286   }
287   my $contact =
288     NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values,
289                          '-labels' => \%labels, '-default' => $form->{"cp_id"}));
290
291   %labels = ();
292   @values = ("");
293   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
294     push(@values, $item->{"id"});
295     $labels{$item->{"id"}} = $item->{"projectnumber"};
296   }
297   my $globalprojectnumber =
298     NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values,
299                          '-labels' => \%labels,
300                          '-default' => $form->{"globalproject_id"}));
301  
302   %labels = ();
303   @values = ();
304   my $i = 0;
305   foreach my $item (@{ $form->{"ALL_CURRENCIES"} }) {
306     push(@values, $item);
307     $labels{$item} = $item;
308   }
309   
310   $form->{currency}        = $form->{defaultcurrency} unless $form->{currency};
311   my $currencies = qq|
312     <tr>
313       <th align="right">| . $locale->text('Currency') . qq|</th>
314       <td>| .
315         NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
316                              '-values' => \@values, '-labels' => \%labels)) . qq|
317       </td>
318     </tr>|;
319   
320   %labels = ();
321   @values = ();
322   foreach my $item (@{ $form->{"ALL_TAXZONES"} }) {
323     push(@values, $item->{"id"});
324     $labels{$item->{"id"}} = $item->{"description"};
325   }
326
327   if (!$form->{"id"}) {
328     $taxzone = qq|
329     <tr>
330       <th align="right">| . $locale->text('Steuersatz') . qq|</th>
331       <td>| .
332         NTI($cgi->popup_menu('-name' => 'taxzone_id', '-default' => $form->{"taxzone_id"},
333                              '-values' => \@values, '-labels' => \%labels)) . qq|
334       </td>
335     </tr>|;
336
337   } else {
338     $taxzone = qq|
339     <tr>
340       <th align="right">| . $locale->text('Steuersatz') . qq|</th>
341       <td>
342         <input type="hidden" name="taxzone_id" value="| . H($form->{"taxzone_id"}) . qq|">
343         | . H($labels{$form->{"taxzone_id"}}) . qq|
344       </td>
345     </tr>|;
346   }
347
348   $vendor =
349     ($form->{selectvendor})
350     ? qq|<select name="vendor"
351 onchange="document.getElementById('update_button').click();">| .
352     qq|$form->{selectvendor}</select>\n<input type=hidden name="selectvendor" value="| .
353     Q($form->{selectvendor}) . qq|">|
354     : qq|<input name=vendor value="$form->{vendor}" size=35>|;
355
356   $department = qq|
357               <tr>
358               <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
359               <td colspan=3><select name=department>$form->{selectdepartment}</select>
360               <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
361               </td>
362             </tr>
363 | if $form->{selectdepartment};
364
365   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
366
367   # use JavaScript Calendar or not
368   $form->{jsscript} = $form->{jscalendar};
369   $jsscript = "";
370   if ($form->{jsscript}) {
371
372     # with JavaScript Calendar
373     $button1 = qq|
374        <td><input name=invdate id=invdate size=11 title="$myconfig{dateformat}" value="$form->{invdate}" onBlur=\"check_right_date_format(this)\"></td>
375        <td><input type=button name=invdate id="trigger1" value=|
376       . $locale->text('button') . qq|></td>
377        |;
378     $button2 = qq|
379        <td width="13"><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}"  onBlur=\"check_right_date_format(this)\"></td>
380        <td width="4"><input type=button name=duedate id="trigger2" value=|
381       . $locale->text('button') . qq|></td></td>
382      |;
383
384     #write Trigger
385     $jsscript =
386       Form->write_trigger(\%myconfig, "2", "invdate", "BL", "trigger1",
387                           "duedate", "BL", "trigger2");
388   } else {
389
390     # without JavaScript Calendar
391     $button1 =
392       qq|<td><input name=invdate size=11 title="$myconfig{dateformat}" value="$form->{invdate}"  onBlur=\"check_right_date_format(this)\"></td>|;
393     $button2 =
394       qq|<td width="13"><input name=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\"></td>|;
395   }
396
397   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
398   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
399
400   $jsscript .=
401     $form->write_trigger(\%myconfig, 2,
402                          "orddate", "BL", "trigger_orddate",
403                          "quodate", "BL", "trigger_quodate");
404
405   $form->header;
406   $onload = qq|focus()|;
407   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
408   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
409   print qq|
410 <body onLoad="$onload">
411
412 <form method=post action=$form->{script}>
413
414 <input type=hidden name=id value=$form->{id}>
415 <input type=hidden name=title value="$form->{title}">
416 <input type=hidden name=vc value="vendor">
417 <input type=hidden name=type value=$form->{type}>
418 <input type=hidden name=level value=$form->{level}>
419
420 <input type=hidden name=creditlimit value=$form->{creditlimit}>
421 <input type=hidden name=creditremaining value=$form->{creditremaining}>
422
423 <input type=hidden name=closedto value=$form->{closedto}>
424 <input type=hidden name=locked value=$form->{locked}>
425
426 <input type=hidden name=shipped value=$form->{shipped}>
427 <input type=hidden name=storno value=$form->{storno}>
428 <input type=hidden name=storno_id value=$form->{storno_id}>
429
430 | . ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") . qq|
431
432 <table width=100%>
433   <tr class=listtop>
434     <th class=listtop>$form->{title}</th>
435   </tr>
436   <tr height="5"></tr>
437   <tr>
438     <td>
439       <table width=100%>
440         <tr valign=top>
441           <td>
442             <table>
443               <tr>
444                 <th align=right nowrap>| . $locale->text('Vendor') . qq|</th>
445                 <td colspan=3>$vendor</td>
446
447                 <th align=richt nowrap>|
448     . $locale->text('Contact Person') . qq|</th>
449                 <td colspan=3>$contact</td>
450
451                 <input type=hidden name=vendor_id value=$form->{vendor_id}>
452                 <input type=hidden name=oldvendor value="$form->{oldvendor}">
453
454               </tr>
455               <tr>
456                 <td></td>
457                 <td colspan=3>
458                   <table>
459                     <tr>
460                       <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
461                       <td>$form->{creditlimit}</td>
462                       <td width=20%></td>
463                       <th nowrap>| . $locale->text('Remaining') . qq|</th>
464                       <td class="plus$n">$form->{creditremaining}</td>
465                     </tr>
466                   </table>
467                 </td>
468               <tr>
469                 <th align=right>| . $locale->text('Record in') . qq|</th>
470                 <td colspan=3><select name=AP>$form->{selectAP}</select></td>
471                 <input type=hidden name=selectAP value="$form->{selectAP}">
472               </tr>
473               $taxzone
474               $department
475               <tr>
476     $currencies
477                 $exchangerate
478               </tr>
479             </table>
480           </td>
481           <td align=right>
482             <table>
483               <tr>
484                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
485                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
486               </tr>
487               <tr>
488                 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
489                 $button1
490               </tr>
491               <tr>
492                 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
493                 $button2
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 <input type=hidden name=quonumber value="$form->{quonumber}">
499               </tr>
500         <tr>
501           <th align="right" nowrap>| . $locale->text('Order Date') . qq|</th>
502           <td><input name="orddate" id="orddate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{orddate}) . qq|" onBlur=\"check_right_date_format(this)\"></td>
503           <td><input type="button" name="b_orddate" id="trigger_orddate" value="?"></td>
504         </tr>
505         <tr>
506           <th align="right" nowrap>| . $locale->text('Quotation Date') . qq|</th>
507           <td><input name="quodate" id="quodate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{quodate}) . qq|" onBlur=\"check_right_date_format(this)\"></td>
508           <td><input type="button" name="b_quodate" id="trigger_quodate" value="?"></td>
509         </tr>
510               <tr>
511           <th align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
512           <td>$globalprojectnumber</td>
513               </tr>
514      </table>
515           </td>
516         </tr>
517       </table>
518     </td>
519   </tr>
520
521 $jsscript
522
523 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
524 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
525 <input type=hidden name=webdav value=$webdav>
526
527 <input type=hidden name=taxpart value="$form->{taxpart}">
528 <input type=hidden name=taxservice value="$form->{taxservice}">
529
530 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
531 |;
532
533   foreach $item (split / /, $form->{taxaccounts}) {
534     print qq|
535 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
536 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
537 |;
538   }
539
540   $lxdebug->leave_sub();
541 }
542
543 sub form_footer {
544   $lxdebug->enter_sub();
545
546   $form->{invtotal} = $form->{invsubtotal};
547
548   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
549     $rows = 2;
550   }
551   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
552     $introws = 2;
553   }
554   $rows = ($rows > $introws) ? $rows : $introws;
555   $notes =
556     qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
557   $intnotes =
558     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
559
560   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
561
562   $taxincluded = "";
563   if ($form->{taxaccounts}) {
564     $taxincluded = qq|
565                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
566       . $locale->text('Tax Included') . qq|</b>
567 |;
568   }
569
570   if (!$form->{taxincluded}) {
571
572     foreach $item (split / /, $form->{taxaccounts}) {
573       if ($form->{"${item}_base"}) {
574         $form->{invtotal} += $form->{"${item}_total"} =
575           $form->round_amount(
576                              $form->{"${item}_base"} * $form->{"${item}_rate"},
577                              2);
578         $form->{"${item}_total"} =
579           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
580
581         $tax .= qq|
582                 <tr>
583                   <th align=right>$form->{"${item}_description"}&nbsp;|
584                     . $form->{"${item}_rate"} * 100 .qq|%</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}_base"} =
613           $form->round_amount($form->{"${item}_base"}, 2);
614         $form->{"${item}_netto"} =
615           $form->round_amount(
616                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
617                           2);
618         $form->{"${item}_netto"} =
619           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
620         $form->{"${item}_total"} =
621           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
622
623         $tax .= qq|
624               <tr>
625                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
626                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
627                 <td align=right>$form->{"${item}_total"}</td>
628               </tr>
629               <tr>
630                 <th align=right>Nettobetrag</th>
631                 <td align=right>$form->{"${item}_netto"}</td>
632               </tr>
633 |;
634       }
635     }
636
637   }
638
639   $form->{oldinvtotal} = $form->{invtotal};
640   $form->{invtotal}    =
641     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
642
643   print qq|
644   <tr>
645     <td colspan=$colspan>
646       <table cellspacing="0">
647         <tr valign=bottom>
648           <td>
649             <table>
650               <tr>
651                 <th align=left>| . $locale->text('Notes') . qq|</th>
652                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
653               </tr>
654               <tr valign=top>
655                 <td>$notes</td>
656                 <td>$intnotes</td>
657               </tr>
658             </table>
659           </td>
660           <td colspan=2 align=right width=100%>
661             $taxincluded
662             <br>
663             <table width=100%>
664               $subtotal
665               $tax
666               <tr>0
667                 <th align=right>| . $locale->text('Total') . qq|</th>
668                 <td align=right>$form->{invtotal}</td>
669               </tr>
670             </table>
671           </td>
672         </tr>
673       </table>
674     </td>
675   </tr>
676 |;
677   if ($webdav) {
678     $webdav_list = qq|
679   <tr>
680     <td><hr size=3 noshade></td>
681   </tr>
682   <tr>
683     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
684   </tr>
685     <table width=100%>
686       <td align=left width=30%><b>Dateiname</b></td>
687       <td align=left width=70%><b>Webdavlink</b></td>
688 |;
689     foreach $file (keys %{ $form->{WEBDAV} }) {
690       $webdav_list .= qq|
691       <tr>
692         <td align=left>$file</td>
693         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
694       </tr>
695 |;
696     }
697     $webdav_list .= qq|
698     </table>
699   </tr>
700 |;
701
702     print $webdav_list;
703   }
704   print qq|
705 <input type=hidden name=jscalendar value=$form->{jscalendar}>
706 |;
707   print qq|
708   <tr>
709     <td colspan=$colspan>
710       <table width=100%>
711         <tr>
712           <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
713         </tr>
714 |;
715
716   if ($form->{currency} eq $form->{defaultcurrency}) {
717     @column_index = qw(datepaid source memo paid AP_paid);
718   } else {
719     @column_index = qw(datepaid source memo paid exchangerate AP_paid);
720   }
721
722   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
723   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
724   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
725   $column_data{AP_paid}      = "<th>" . $locale->text('Account') . "</th>";
726   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
727   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
728
729   print qq|
730         <tr>
731 |;
732   map { print "$column_data{$_}\n" } @column_index;
733   print qq|
734         </tr>
735 |;
736
737   my @triggers = ();
738   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
739   for $i (1 .. $form->{paidaccounts}) {
740
741     print qq|
742         <tr>
743 |;
744
745     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
746     $form->{"selectAP_paid_$i"} =~
747       s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
748
749     # format amounts
750     if ($form->{"paid_$i"}) {
751       $form->{"paid_$i"} =
752         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
753     }
754     $form->{"exchangerate_$i"} =
755       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
756
757     $exchangerate = qq|&nbsp;|;
758     if ($form->{currency} ne $form->{defaultcurrency}) {
759       if ($form->{"forex_$i"}) {
760         $exchangerate =
761           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
762       } else {
763         $exchangerate =
764           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
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"}" onBlur=\"check_right_number_format(this)\"></td>|;
773     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
774     $column_data{"AP_paid_$i"}      =
775       qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
776     $column_data{"datepaid_$i"} =
777       qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 title="$myconfig{dateformat}" value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
778          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
779     $column_data{"source_$i"} =
780       qq|<td align=center><input name="source_$i" size=11 value=$form->{"source_$i"}></td>|;
781     $column_data{"memo_$i"} =
782       qq|<td align=center><input name="memo_$i" size=11 value=$form->{"memo_$i"}></td>|;
783
784     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
785
786     print qq|
787         </tr>
788 |;
789     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
790   }
791
792   print qq|
793             <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
794             <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
795             <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
796       </table>
797     </td>
798   </tr>
799   <tr>
800     <td><hr size=3 noshade></td>
801   </tr>
802 </table>
803 <br>
804 |;
805
806   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
807   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
808
809   if ($form->{id}) {
810     my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap");
811
812     print qq|<input class=submit type=submit name=action value="|
813       . $locale->text('Post Payment') . qq|">
814 |;
815     print qq|<input class=submit type=submit name=action value="|
816       . $locale->text('Storno') . qq|">
817 | if ($show_storno);
818     if ($form->{radier}) {
819     print qq|
820     <input class=submit type=submit name=action value="|
821       . $locale->text('Delete') . qq|">
822 |;
823   }
824     print qq|<input class=submit type=submit name=action value="|
825       . $locale->text('Use As Template') . qq|">
826 |;
827
828   }
829
830   print qq|<input class=submit type=submit name=action id=update_button value="|
831     . $locale->text('Update') . qq|">|;
832
833   if (!$form->{id} && ($invdate > $closedto)) {
834     print qq| <input class=submit type=submit name=action value="|
835       . $locale->text('Post') . qq|"> | .
836       NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
837                        '-class' => 'submit'));
838   }
839
840   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers);
841   $form->hide_form(qw(rowcount callback draft_id draft_description login password));
842
843   # button for saving history
844   if($form->{id} ne "") {
845     print qq|
846           <input type="button" class="submit" onclick="set_history_window(|
847           . Q($form->{id})
848           . qq|);" name="history" id="history" value="|
849           . $locale->text('history')
850           . qq|">|;
851   }
852   # /button for saving history
853
854 print qq|</form>
855
856 </body>
857 </html>
858 |;
859
860   $lxdebug->leave_sub();
861 }
862
863 sub update {
864   $lxdebug->enter_sub();
865
866   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
867     qw(exchangerate creditlimit creditremaining);
868
869   &check_name(vendor);
870
871   $form->{exchangerate} = $exchangerate
872     if (
873         $form->{forex} = (
874                       $exchangerate =
875                         $form->check_exchangerate(
876                         \%myconfig, $form->{currency}, $form->{invdate}, 'sell'
877                         )));
878
879   for $i (1 .. $form->{paidaccounts}) {
880     if ($form->{"paid_$i"}) {
881       map {
882         $form->{"${_}_$i"} =
883           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
884       } qw(paid exchangerate);
885
886       $form->{"exchangerate_$i"} = $exchangerate
887         if (
888             $form->{"forex_$i"} = (
889                 $exchangerate =
890                   $form->check_exchangerate(
891                   \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'
892                   )));
893     }
894   }
895
896   $i            = $form->{rowcount};
897   $exchangerate = ($form->{exchangerate} * 1) ? $form->{exchangerate} * 1 : 1;
898
899   if (   ($form->{"partnumber_$i"} eq "")
900       && ($form->{"description_$i"} eq "")
901       && ($form->{"partsgroup_$i"} eq "")) {
902     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
903     &check_form;
904
905   } else {
906
907     IR->retrieve_item(\%myconfig, \%$form);
908
909     my $rows = scalar @{ $form->{item_list} };
910
911     if ($rows) {
912       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
913
914       if ($rows > 1) {
915
916         &select_item;
917         exit;
918
919       } else {
920
921         # override sellprice if there is one entered
922         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
923
924         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
925           qw(partnumber description unit);
926
927         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
928           keys %{ $form->{item_list}[0] };
929
930         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
931
932         ($dec) = ($s =~ /\.(\d+)/);
933         $dec           = length $dec;
934         $decimalplaces = ($dec > 2) ? $dec : 2;
935
936         if ($sellprice) {
937           $form->{"sellprice_$i"} = $sellprice;
938         } else {
939
940           # if there is an exchange rate adjust sellprice
941           $form->{"sellprice_$i"} /= $exchangerate;
942         }
943
944         $amount =
945           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
946           (1 - $form->{"discount_$i"} / 100);
947         $form->{creditremaining} -= $amount;
948         $form->{"sellprice_$i"} =
949           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
950                                $decimalplaces);
951         $form->{"qty_$i"} =
952           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
953       }
954
955       &display_form;
956
957     } else {
958
959       # ok, so this is a new part
960       # ask if it is a part or service item
961
962       if (   $form->{"partsgroup_$i"}
963           && ($form->{"partsnumber_$i"} eq "")
964           && ($form->{"description_$i"} eq "")) {
965         $form->{rowcount}--;
966         $form->{"discount_$i"} = "";
967         &display_form;
968           } else {
969
970         $form->{"id_$i"}   = 0;
971         $form->{"unit_$i"} = $locale->text('ea');
972
973         &new_item;
974
975       }
976     }
977   }
978   $lxdebug->leave_sub();
979 }
980
981 sub storno {
982   $lxdebug->enter_sub();
983
984   if ($form->{storno}) {
985     $form->error($locale->text('Cannot storno storno invoice!'));
986   }
987
988   if (IS->has_storno(\%myconfig, $form, "ap")) {
989     $form->error($locale->text("Invoice has already been storno'd!"));
990   }
991
992   invoice_links();
993   prepare_invoice();
994   relink_accounts();
995   
996   # saving the history
997   if(!exists $form->{addition} && $form->{id} ne "") {
998     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};  
999     $form->{addition} = "CANCELED";
1000     $form->save_history($form->dbconnect(\%myconfig));
1001   }
1002   # /saving the history
1003   
1004   $form->{storno_id} = $form->{id};
1005   $form->{storno} = 1;
1006   $form->{id} = "";
1007   $form->{invnumber} = "Storno zu " . $form->{invnumber};
1008   $form->{rowcount}++;
1009   &post();
1010   $lxdebug->leave_sub();
1011
1012 }
1013
1014 sub use_as_template {
1015   $lxdebug->enter_sub();
1016
1017   map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
1018   $form->{paidaccounts} = 1;
1019   $form->{rowcount}--;
1020   $form->{invdate} = $form->current_date(\%myconfig);
1021   &display_form;
1022
1023   $lxdebug->leave_sub();
1024 }
1025
1026 sub post_payment {
1027   $lxdebug->enter_sub();
1028   for $i (1 .. $form->{paidaccounts}) {
1029     if ($form->{"paid_$i"}) {
1030       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1031
1032       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1033
1034       $form->error($locale->text('Cannot post payment for a closed period!'))
1035         if ($datepaid <= $closedto);
1036
1037       if ($form->{currency} ne $form->{defaultcurrency}) {
1038         $form->{"exchangerate_$i"} = $form->{exchangerate}
1039           if ($invdate == $datepaid);
1040         $form->isblank("exchangerate_$i",
1041                        $locale->text('Exchangerate for payment missing!'));
1042       }
1043     }
1044   }
1045
1046   ($form->{AP})      = split /--/, $form->{AP};
1047   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1048   if (IR->post_payment(\%myconfig, \%$form)){
1049         
1050         if(!exists $form->{addition} && $form->{id} ne "") {
1051                 # saving the history
1052       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};  
1053                 $form->{addition} = "PAYMENT POSTED";
1054       $form->{what_done} = $form->{currency} . qq| | . $form->{paid} . qq| | . $locale->text("POSTED");
1055                 $form->save_history($form->dbconnect(\%myconfig));
1056                 # /saving the history 
1057                 $form->redirect($locale->text(' Payment posted!'));
1058         }
1059   }
1060     $form->error($locale->text('Cannot post payment!'));
1061
1062
1063   $lxdebug->leave_sub();
1064 }
1065
1066 sub post {
1067   $lxdebug->enter_sub();
1068
1069   $form->isblank("invdate", $locale->text('Invoice Date missing!'));
1070   $form->isblank("vendor",  $locale->text('Vendor missing!'));
1071
1072   # if the vendor changed get new values
1073   if (&check_name(vendor)) {
1074     &update;
1075     exit;
1076   }
1077
1078   &validate_items;
1079
1080   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1081   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1082
1083   $form->error($locale->text('Cannot post invoice for a closed period!'))
1084     if ($invdate <= $closedto);
1085
1086   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1087     if ($form->{currency} ne $form->{defaultcurrency});
1088
1089   for $i (1 .. $form->{paidaccounts}) {
1090     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1091       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1092
1093       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1094
1095       $form->error($locale->text('Cannot post payment for a closed period!'))
1096         if ($datepaid <= $closedto);
1097
1098       if ($form->{currency} ne $form->{defaultcurrency}) {
1099         $form->{"exchangerate_$i"} = $form->{exchangerate}
1100           if ($invdate == $datepaid);
1101         $form->isblank("exchangerate_$i",
1102                        $locale->text('Exchangerate for payment missing!'));
1103       }
1104     }
1105   }
1106
1107   ($form->{AP})      = split /--/, $form->{AP};
1108   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1109
1110   $form->{id} = 0 if $form->{postasnew};
1111
1112
1113   relink_accounts();
1114   if (IR->post_invoice(\%myconfig, \%$form)){
1115         # saving the history
1116         if(!exists $form->{addition} && $form->{id} ne "") {
1117       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};  
1118       $form->{addition} = "POSTED";
1119                 #$form->{what_done} = $locale->text("Rechnungsnummer") . qq| | . $form->{invnumber};
1120                 $form->save_history($form->dbconnect(\%myconfig));
1121         }
1122         # /saving the history
1123     remove_draft() if $form->{remove_draft};
1124         $form->redirect(  $locale->text('Invoice')
1125                   . " $form->{invnumber} "
1126                   . $locale->text('posted!'));
1127   }
1128   $form->error($locale->text('Cannot post invoice!'));
1129
1130   $lxdebug->leave_sub();
1131 }
1132
1133 sub delete {
1134   $lxdebug->enter_sub();
1135
1136   $form->header;
1137   print qq|
1138 <body>
1139
1140 <form method=post action=$form->{script}>
1141 |;
1142
1143   # delete action variable
1144   map { delete $form->{$_} } qw(action header);
1145
1146   foreach $key (keys %$form) {
1147     $form->{$key} =~ s/\"/&quot;/g;
1148     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1149   }
1150
1151   print qq|
1152 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1153
1154 <h4>|
1155     . $locale->text('Are you sure you want to delete Invoice Number')
1156     . qq| $form->{invnumber}</h4>
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   if (IR->delete_invoice(\%myconfig, \%$form)) {
1169     # saving the history
1170     if(!exists $form->{addition}) {
1171       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};  
1172           $form->{addition} = "DELETED";
1173           $form->save_history($form->dbconnect(\%myconfig));
1174     }
1175     # /saving the history 
1176     $form->redirect($locale->text('Invoice deleted!'));
1177   }
1178   $form->error($locale->text('Cannot delete invoice!'));
1179
1180   $lxdebug->leave_sub();
1181 }