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