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