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