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