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