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