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