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