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