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