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