Kosmetikmerge aus Revisionen 5187, 5191, 5193, 5194, 5218, 5219, 5222, 5228, 5229
[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 use List::Util qw(max);
38
39 require "bin/mozilla/io.pl";
40 require "bin/mozilla/arap.pl";
41 require "bin/mozilla/common.pl";
42 require "bin/mozilla/drafts.pl";
43
44 1;
45
46 # end of main
47
48 sub add {
49   $lxdebug->enter_sub();
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"} =~ 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>\n| if $form->{selectdepartment};
371
372   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
373
374   # use JavaScript Calendar or not
375   $form->{jsscript} = 1;
376   $jsscript = "";
377
378   $button1 = qq|
379      <td><input name=invdate id=invdate size=11 title="$myconfig{dateformat}" value="$form->{invdate}" onBlur=\"check_right_date_format(this)\">
380          <input type=button name=invdate id="trigger1" value=| . $locale->text('button') . qq|></td>\n|;
381   $button2 = qq|
382      <td width="13"><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}"  onBlur=\"check_right_date_format(this)\">
383                     <input type=button name=duedate id="trigger2" value=| . $locale->text('button') . qq|></td></td>\n|;
384
385   #write Trigger
386   $jsscript =
387     Form->write_trigger(\%myconfig, "2", 
388                         "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 closedto locked shippted storno storno_id
408                       max_dunning_level dunning_amount vendor_id oldvendor selectvendor taxaccounts
409                       fxgain_accno fxloss_accno taxpart taxservice),
410                       map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} );
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         $contact
424         <tr>
425           <td align="right">| . $locale->text('Credit Limit') . qq|</td>
426           <td>$form->{creditlimit}; | . $locale->text('Remaining') . qq| <span class="plus$n">$form->{creditremaining}</span></td>
427         </tr>
428               <tr>
429                 <th align="right">| . $locale->text('Record in') . qq|</th>
430                 <td colspan="3"><select name="AP" style="width: 250px">$form->{selectAP}</select></td>
431                 <input type="hidden" name="selectAP" value="$form->{selectAP}">
432               </tr>
433               $taxzone
434               $department
435               <tr>
436     $currencies
437                 $exchangerate
438               </tr>
439             </table>
440           </td>
441           <td align=right>
442             <table>
443      $employees
444               <tr>   
445                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
446                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
447               </tr>
448               <tr>
449                 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
450                 $button1
451               </tr>
452               <tr>
453                 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
454                 $button2
455               </tr>
456               <tr>
457                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
458                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
459 <input type=hidden name=quonumber value="$form->{quonumber}">
460               </tr>
461         <tr>
462           <th align="right" nowrap>| . $locale->text('Order Date') . qq|</th>
463           <td><input name="orddate" id="orddate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{orddate}) . qq|" onBlur=\"check_right_date_format(this)\">
464            <input type="button" name="b_orddate" id="trigger_orddate" value="?"></td>
465         </tr>
466         <tr>
467           <th align="right" nowrap>| . $locale->text('Quotation Date') . qq|</th>
468           <td><input name="quodate" id="quodate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{quodate}) . qq|" onBlur=\"check_right_date_format(this)\">
469            <input type="button" name="b_quodate" id="trigger_quodate" value="?"></td>
470         </tr>
471               <tr>
472           <th align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
473           <td>$globalprojectnumber</td>
474               </tr>
475      </table>
476           </td>
477         </tr>
478       </table>
479     </td>
480   </tr>
481
482 $jsscript
483
484 <input type=hidden name=webdav value=$webdav>
485 |;
486
487   foreach $item (split / /, $form->{taxaccounts}) {
488     print qq|
489 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
490 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
491 |;
492   }
493
494   $lxdebug->leave_sub();
495 }
496
497 sub form_footer {
498   $lxdebug->enter_sub();
499
500   $form->{invtotal} = $form->{invsubtotal};
501
502   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
503     $rows = 2;
504   }
505   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
506     $introws = 2;
507   }
508   $rows = ($rows > $introws) ? $rows : $introws;
509   $notes =
510     qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
511   $intnotes =
512     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
513
514   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
515
516   $taxincluded = "";
517   if ($form->{taxaccounts}) {
518     $taxincluded = qq|
519                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
520       . $locale->text('Tax Included') . qq|</b>
521 |;
522   }
523
524   if (!$form->{taxincluded}) {
525
526     foreach $item (split / /, $form->{taxaccounts}) {
527       if ($form->{"${item}_base"}) {
528         $form->{invtotal} += $form->{"${item}_total"} =
529           $form->round_amount(
530                              $form->{"${item}_base"} * $form->{"${item}_rate"},
531                              2);
532         $form->{"${item}_total"} =
533           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
534
535         $tax .= qq|
536                 <tr>
537                   <th align=right>$form->{"${item}_description"}&nbsp;|
538                     . $form->{"${item}_rate"} * 100 .qq|%</th>
539                   <td align=right>$form->{"${item}_total"}</td>
540                 </tr>
541 |;
542       }
543     }
544
545     $form->{invsubtotal} =
546       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
547
548     $subtotal = qq|
549               <tr>
550                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
551                 <td align=right>$form->{invsubtotal}</td>
552               </tr>
553 |;
554
555   }
556
557   if ($form->{taxincluded}) {
558     foreach $item (split / /, $form->{taxaccounts}) {
559       if ($form->{"${item}_base"}) {
560         $form->{"${item}_total"} =
561           $form->round_amount(
562                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
563                               (1 + $form->{"${item}_rate"})
564                            ),
565                            2);
566         $form->{"${item}_base"} =
567           $form->round_amount($form->{"${item}_base"}, 2);
568         $form->{"${item}_netto"} =
569           $form->round_amount(
570                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
571                           2);
572         $form->{"${item}_netto"} =
573           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
574         $form->{"${item}_total"} =
575           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
576
577         $tax .= qq|
578               <tr>
579                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
580                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
581                 <td align=right>$form->{"${item}_total"}</td>
582               </tr>
583               <tr>
584                 <th align=right>Nettobetrag</th>
585                 <td align=right>$form->{"${item}_netto"}</td>
586               </tr>
587 |;
588       }
589     }
590
591   }
592
593   $form->{oldinvtotal} = $form->{invtotal};
594   $form->{invtotal}    =
595     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
596
597   print qq|
598   <tr>
599     <td colspan=$colspan>
600       <table cellspacing="0">
601         <tr valign=bottom>
602           <td>
603             <table>
604               <tr>
605                 <th align=left>| . $locale->text('Notes') . qq|</th>
606                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
607               </tr>
608               <tr valign=top>
609                 <td>$notes</td>
610                 <td>$intnotes</td>
611               </tr>
612             </table>
613           </td>
614           <td colspan=2 align=right width=100%>
615             $taxincluded
616             <br>
617             <table width=100%>
618               $subtotal
619               $tax
620               <tr>
621                 <th align=right>| . $locale->text('Total') . qq|</th>
622                 <td align=right>$form->{invtotal}</td>
623               </tr>
624             </table>
625           </td>
626         </tr>
627       </table>
628     </td>
629   </tr>
630 |;
631   if ($webdav) {
632     $webdav_list = qq|
633   <tr>
634     <td><hr size=3 noshade></td>
635   </tr>
636   <tr>
637     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
638   </tr>
639     <table width=100%>
640       <td align=left width=30%><b>Dateiname</b></td>
641       <td align=left width=70%><b>Webdavlink</b></td>
642 |;
643     foreach $file (@{ $form->{WEBDAV} }) {
644       $webdav_list .= qq|
645       <tr>
646         <td align="left">$file->{name}</td>
647         <td align="left"><a href="$file->{link}">$file->{type}</a></td>
648       </tr>
649 |;
650     }
651     $webdav_list .= qq|
652     </table>
653   </tr>
654 |;
655
656     print $webdav_list;
657   }
658   print qq|
659   <tr>
660     <td colspan=$colspan>
661       <table width=100%>
662         <tr>
663           <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
664         </tr>
665 |;
666
667   if ($form->{currency} eq $form->{defaultcurrency}) {
668     @column_index = qw(datepaid source memo paid AP_paid);
669   } else {
670     @column_index = qw(datepaid source memo paid exchangerate AP_paid);
671   }
672
673   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
674   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
675   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
676   $column_data{AP_paid}      = "<th>" . $locale->text('Account') . "</th>";
677   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
678   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
679
680   print qq|
681         <tr>
682 |;
683   map { print "$column_data{$_}\n" } @column_index;
684   print qq|
685         </tr>
686 |;
687
688   my @triggers  = ();
689   my $totalpaid = 0;
690
691   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
692   for $i (1 .. $form->{paidaccounts}) {
693
694     print qq|
695         <tr>
696 |;
697
698     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
699     $form->{"selectAP_paid_$i"} =~
700       s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
701
702     $totalpaid += $form->{"paid_$i"};
703
704     # format amounts
705     if ($form->{"paid_$i"}) {
706       $form->{"paid_$i"} =
707         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
708     }
709     $form->{"exchangerate_$i"} =
710       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
711
712     $exchangerate = qq|&nbsp;|;
713     if ($form->{currency} ne $form->{defaultcurrency}) {
714       if ($form->{"forex_$i"}) {
715         $exchangerate =
716           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
717       } else {
718         $exchangerate =
719           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
720       }
721     }
722     $exchangerate .= qq|
723 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
724 |;
725
726     $column_data{"paid_$i"} =
727       qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
728     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
729     $column_data{"AP_paid_$i"}      =
730       qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
731     $column_data{"datepaid_$i"} =
732       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)\">
733          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
734     $column_data{"source_$i"} =
735       qq|<td align=center><input name="source_$i" size=11 value=$form->{"source_$i"}></td>|;
736     $column_data{"memo_$i"} =
737       qq|<td align=center><input name="memo_$i" size=11 value=$form->{"memo_$i"}></td>|;
738
739     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
740
741     print qq|
742         </tr>
743 |;
744     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
745   }
746
747   my $paid_missing = $form->{oldinvtotal} - $totalpaid;
748
749   print qq|
750         <tr>
751           <td></td>
752           <td></td>
753           <td align="center">| . $locale->text('Total') . qq|</td>
754           <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
755         </tr>
756         <tr>
757           <td></td>
758           <td></td>
759           <td align="center">| . $locale->text('Missing amount') . qq|</td>
760           <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
761         </tr>
762
763             <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
764             <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
765             <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
766       </table>
767     </td>
768   </tr>
769   <tr>
770     <td><hr size=3 noshade></td>
771   </tr>
772 </table>
773 <br>
774 |;
775
776   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
777   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
778
779   print qq|<input class=submit type=submit name=action id=update_button value="|
780     . $locale->text('Update') . qq|">
781 |;
782
783   if ($form->{id}) {
784     my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap");
785
786     print qq|<input class=submit type=submit name=action value="|
787       . $locale->text('Post Payment') . qq|">
788 |;
789     print qq|<input class=submit type=submit name=action value="|
790       . $locale->text('Storno') . qq|">
791 | if ($show_storno);
792     if ($form->{radier}) {
793     print qq|
794     <input class=submit type=submit name=action value="|
795       . $locale->text('Delete') . qq|">
796 |;
797   }
798     print qq|<input class=submit type=submit name=action value="|
799       . $locale->text('Use As Template') . qq|">
800 |;
801
802   }
803
804   if (!$form->{id} && ($invdate > $closedto)) {
805     print qq| <input class=submit type=submit name=action value="|
806       . $locale->text('Post') . qq|"> | .
807       NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
808                        '-class' => 'submit'));
809   }
810
811   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers);
812   $form->hide_form(qw(rowcount callback draft_id draft_description login password));
813
814   # button for saving history
815   if($form->{id} ne "") {
816     print qq|
817           <input type="button" class="submit" onclick="set_history_window(|
818           . Q($form->{id})
819           . qq|);" name="history" id="history" value="|
820           . $locale->text('history')
821           . qq|">|;
822   }
823   # /button for saving history
824   # mark_as_paid button 
825   if($form->{id} ne "") {  
826     print qq| <input type="submit" class="submit" name="action" value="| 
827           . $locale->text('mark as paid') . qq|">|;
828   }
829   # /mark_as_paid button
830 print qq|</form>
831
832 </body>
833 </html>
834 |;
835
836   $lxdebug->leave_sub();
837 }
838
839 sub mark_as_paid {
840   $lxdebug->enter_sub();
841   &mark_as_paid_common(\%myconfig,"ap");  
842   $lxdebug->leave_sub();
843 }
844
845 sub update {
846   $lxdebug->enter_sub();
847
848   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
849
850   &check_name(vendor);
851
852   $form->{exchangerate} = $exchangerate if
853     $form->{forex} = $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
854
855   for $i (1 .. $form->{paidaccounts}) {
856     next unless $form->{"paid_$i"};
857     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
858     $form->{"exchangerate_$i"} = $exchangerate if
859       $form->{"forex_$i"} = $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
860   }
861
862   $i            = $form->{rowcount};
863   $exchangerate = ($form->{exchangerate} * 1) || 1;
864
865   if (   ($form->{"partnumber_$i"} eq "")
866       && ($form->{"description_$i"} eq "")
867       && ($form->{"partsgroup_$i"} eq "")) {
868     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
869     &check_form;
870
871   } else {
872
873     IR->retrieve_item(\%myconfig, \%$form);
874
875     my $rows = scalar @{ $form->{item_list} };
876
877     if ($rows) {
878       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
879
880       if ($rows > 1) {
881
882         &select_item;
883         exit;
884
885       } else {
886
887         # override sellprice if there is one entered
888         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
889
890         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
891         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
892
893         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
894         $decimalplaces = max 2, length $1;
895
896         if ($sellprice) {
897           $form->{"sellprice_$i"} = $sellprice;
898         } else {
899           # if there is an exchange rate adjust sellprice
900           $form->{"sellprice_$i"} /= $exchangerate;
901         }
902
903         $amount                   = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
904         $form->{creditremaining} -= $amount;
905         $form->{"sellprice_$i"}   = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
906         $form->{"qty_$i"}         = $form->format_amount(\%myconfig, $form->{"qty_$i"},       $dec_qty);
907       }
908
909       &display_form;
910
911     } else {
912
913       # ok, so this is a new part
914       # ask if it is a part or service item
915
916       if (   $form->{"partsgroup_$i"}
917           && ($form->{"partsnumber_$i"} eq "")
918           && ($form->{"description_$i"} eq "")) {
919         $form->{rowcount}--;
920         $form->{"discount_$i"} = "";
921         display_form();
922
923       } else {
924         $form->{"id_$i"}   = 0;
925         new_item();
926       }
927     }
928   }
929   $lxdebug->leave_sub();
930 }
931
932 sub storno {
933   $lxdebug->enter_sub();
934
935   if ($form->{storno}) {
936     $form->error($locale->text('Cannot storno storno invoice!'));
937   }
938
939   if (IS->has_storno(\%myconfig, $form, "ap")) {
940     $form->error($locale->text("Invoice has already been storno'd!"));
941   }
942
943   my $employee_id = $form->{employee_id};
944   invoice_links();
945   prepare_invoice();
946   relink_accounts();
947
948   # Payments must not be recorded for the new storno invoice.
949   $form->{paidaccounts} = 0;
950   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
951   
952   # saving the history
953   if(!exists $form->{addition} && $form->{id} ne "") {
954     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};  
955     $form->{addition} = "CANCELED";
956     $form->save_history($form->dbconnect(\%myconfig));
957   }
958   # /saving the history
959   
960   $form->{storno_id} = $form->{id};
961   $form->{storno} = 1;
962   $form->{id} = "";
963   $form->{invnumber} = "Storno zu " . $form->{invnumber};
964   $form->{rowcount}++;
965   $form->{employee_id} = $employee_id;
966   post();
967   $lxdebug->leave_sub();
968
969 }
970
971 sub use_as_template {
972   $lxdebug->enter_sub();
973
974   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);
975   $form->{paidaccounts} = 1;
976   $form->{rowcount}--;
977   $form->{invdate} = $form->current_date(\%myconfig);
978   &display_form;
979
980   $lxdebug->leave_sub();
981 }
982
983 sub post_payment {
984   $lxdebug->enter_sub();
985
986   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
987   for $i (1 .. $form->{paidaccounts}) {
988     if ($form->{"paid_$i"}) {
989       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
990
991       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
992
993       $form->error($locale->text('Cannot post payment for a closed period!'))
994         if ($datepaid <= $closedto);
995
996       if ($form->{currency} ne $form->{defaultcurrency}) {
997         $form->{"exchangerate_$i"} = $form->{exchangerate}
998           if ($invdate == $datepaid);
999         $form->isblank("exchangerate_$i",
1000                        $locale->text('Exchangerate for payment missing!'));
1001       }
1002     }
1003   }
1004
1005   ($form->{AP})      = split /--/, $form->{AP};
1006   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1007   if (IR->post_payment(\%myconfig, \%$form)){
1008         if (!exists $form->{addition} && $form->{id} ne "") {
1009                 # saving the history
1010       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1011                 $form->{addition} = "PAYMENT POSTED";
1012       $form->{what_done} = $form->{currency} . qq| | . $form->{paid} . qq| | . $locale->text("POSTED");
1013                 $form->save_history($form->dbconnect(\%myconfig));
1014                 # /saving the history
1015         }
1016
1017     $form->redirect($locale->text('Payment posted!'));
1018   }
1019
1020   $form->error($locale->text('Cannot post payment!'));
1021
1022   $lxdebug->leave_sub();
1023 }
1024
1025 sub post {
1026   $lxdebug->enter_sub();
1027
1028   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1029
1030   $form->isblank("invdate", $locale->text('Invoice Date missing!'));
1031   $form->isblank("vendor",  $locale->text('Vendor missing!'));
1032
1033   $form->{invnumber} =~ s/^\s*//g;
1034   $form->{invnumber} =~ s/\s*$//g;
1035
1036   # if the vendor changed get new values
1037   if (&check_name(vendor)) {
1038     &update;
1039     exit;
1040   }
1041
1042   &validate_items;
1043
1044   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1045   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1046
1047   $form->error($locale->text('Cannot post invoice for a closed period!'))
1048     if ($invdate <= $closedto);
1049
1050   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1051     if ($form->{currency} ne $form->{defaultcurrency});
1052
1053   for $i (1 .. $form->{paidaccounts}) {
1054     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1055       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1056
1057       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1058
1059       $form->error($locale->text('Cannot post payment for a closed period!'))
1060         if ($datepaid <= $closedto);
1061
1062       if ($form->{currency} ne $form->{defaultcurrency}) {
1063         $form->{"exchangerate_$i"} = $form->{exchangerate}
1064           if ($invdate == $datepaid);
1065         $form->isblank("exchangerate_$i",
1066                        $locale->text('Exchangerate for payment missing!'));
1067       }
1068     }
1069   }
1070
1071   ($form->{AP})      = split /--/, $form->{AP};
1072   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1073   $form->{storno}    = 0;
1074
1075   $form->{id} = 0 if $form->{postasnew};
1076
1077
1078   relink_accounts();
1079   if (IR->post_invoice(\%myconfig, \%$form)){
1080         # saving the history
1081         if(!exists $form->{addition} && $form->{id} ne "") {
1082       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};  
1083       $form->{addition} = "POSTED";
1084                 #$form->{what_done} = $locale->text("Rechnungsnummer") . qq| | . $form->{invnumber};
1085                 $form->save_history($form->dbconnect(\%myconfig));
1086         }
1087         # /saving the history
1088     remove_draft() if $form->{remove_draft};
1089         $form->redirect(  $locale->text('Invoice')
1090                   . " $form->{invnumber} "
1091                   . $locale->text('posted!'));
1092   }
1093   $form->error($locale->text('Cannot post invoice!'));
1094
1095   $lxdebug->leave_sub();
1096 }
1097
1098 sub delete {
1099   $lxdebug->enter_sub();
1100
1101   $form->header;
1102   print qq|
1103 <body>
1104
1105 <form method=post action=$form->{script}>
1106 |;
1107
1108   # delete action variable
1109   map { delete $form->{$_} } qw(action header);
1110
1111   foreach $key (keys %$form) {
1112     $form->{$key} =~ s/\"/&quot;/g;
1113     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1114   }
1115
1116   print qq|
1117 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1118
1119 <h4>|
1120     . $locale->text('Are you sure you want to delete Invoice Number')
1121     . qq| $form->{invnumber}</h4>
1122 <p>
1123 <input name=action class=submit type=submit value="|
1124     . $locale->text('Yes') . qq|">
1125 </form>
1126 |;
1127
1128   $lxdebug->leave_sub();
1129 }
1130
1131 sub yes {
1132   $lxdebug->enter_sub();
1133   if (IR->delete_invoice(\%myconfig, \%$form)) {
1134     # saving the history
1135     if(!exists $form->{addition}) {
1136       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};  
1137           $form->{addition} = "DELETED";
1138           $form->save_history($form->dbconnect(\%myconfig));
1139     }
1140     # /saving the history 
1141     $form->redirect($locale->text('Invoice deleted!'));
1142   }
1143   $form->error($locale->text('Cannot delete invoice!'));
1144
1145   $lxdebug->leave_sub();
1146 }