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