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