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