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