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