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