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