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