Uebernahme der kompletten Version, so wie sie Philip als "Demo-Version" gezeigt hat...
[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   if ($form->{menubar}) {
901     require "$form->{path}/menu.pl";
902     &menubar;
903   }
904
905   print qq|
906
907 <input type=hidden name=rowcount value=$form->{rowcount}>
908
909 <input name=callback type=hidden value="$form->{callback}">
910
911 <input type=hidden name=path value=$form->{path}>
912 <input type=hidden name=login value=$form->{login}>
913 <input type=hidden name=password value=$form->{password}>
914
915 </form>
916
917 </body>
918
919  </html>
920 |;
921
922   $lxdebug->leave_sub();
923 }
924
925 sub update {
926   $lxdebug->enter_sub();
927
928   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
929     qw(exchangerate creditlimit creditremaining);
930   if ($form->{second_run}) {
931     $form->{print_and_post} = 0;
932   }
933   &check_name(customer);
934
935   &check_project;
936
937   $form->{exchangerate} = $exchangerate
938     if (
939         $form->{forex} = (
940                        $exchangerate =
941                          $form->check_exchangerate(
942                          \%myconfig, $form->{currency}, $form->{invdate}, 'buy'
943                          )));
944
945   for $i (1 .. $form->{paidaccounts}) {
946     if ($form->{"paid_$i"}) {
947       map {
948         $form->{"${_}_$i"} =
949           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
950       } qw(paid exchangerate);
951
952       $form->{"exchangerate_$i"} = $exchangerate
953         if (
954             $form->{"forex_$i"} = (
955                  $exchangerate =
956                    $form->check_exchangerate(
957                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
958                    )));
959     }
960   }
961
962   $i            = $form->{rowcount};
963   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
964
965   # if last row empty, check the form otherwise retrieve new item
966   if (   ($form->{"partnumber_$i"} eq "")
967       && ($form->{"description_$i"} eq "")
968       && ($form->{"partsgroup_$i"}  eq "")) {
969
970     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
971     &check_form;
972
973       } else {
974
975     IS->retrieve_item(\%myconfig, \%$form);
976
977     $rows = scalar @{ $form->{item_list} };
978
979     $form->{"discount_$i"} =
980       $form->format_amount(\%myconfig, $form->{discount} * 100);
981
982     if ($rows) {
983       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
984
985       if ($rows > 1) {
986
987         &select_item;
988         exit;
989
990       } else {
991
992         $sellprice = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
993
994         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
995           qw(partnumber description unit);
996         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
997           keys %{ $form->{item_list}[0] };
998
999         $form->{"discount_$i"} = $form->{discount} * 100;
1000
1001         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1002         ($dec) = ($s =~ /\.(\d+)/);
1003         $dec           = length $dec;
1004         $decimalplaces = ($dec > 2) ? $dec : 2;
1005
1006         if ($sellprice) {
1007           $form->{"sellprice_$i"} = $sellprice;
1008         } else {
1009
1010           # if there is an exchange rate adjust sellprice
1011           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1012           $form->{"sellprice_$i"} /= $exchangerate;
1013         }
1014
1015         $form->{"listprice_$i"} /= $exchangerate;
1016
1017         $amount =
1018           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1019           (1 - $form->{"discount_$i"} / 100);
1020         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1021         map { $form->{"${_}_base"} += $amount }
1022           (split / /, $form->{"taxaccounts_$i"});
1023         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1024           split / /, $form->{"taxaccounts_$i"}
1025           if !$form->{taxincluded};
1026
1027         $form->{creditremaining} -= $amount;
1028
1029         map {
1030           $form->{"${_}_$i"} =
1031             $form->format_amount(\%myconfig, $form->{"${_}_$i"},
1032                                  $decimalplaces)
1033         } qw(sellprice listprice);
1034
1035         $form->{"qty_$i"} =
1036           $form->format_amount(\%myconfig, $form->{"qty_$i"});
1037
1038         if ($lizenzen) {
1039           if ($form->{"inventory_accno_$i"} ne "") {
1040             $form->{"lizenzen_$i"} = qq|<option></option>|;
1041             foreach $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
1042               $form->{"lizenzen_$i"} .=
1043                 qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
1044             }
1045             $form->{"lizenzen_$i"} .=
1046               qq|<option value=-1>Neue Lizenz</option>|;
1047           }
1048         }
1049
1050         # get pricegroups for parts
1051         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1052
1053         # build up html code for prices_$i
1054         &set_pricegroup($i);
1055       }
1056
1057       &display_form;
1058
1059     } else {
1060
1061       # ok, so this is a new part
1062       # ask if it is a part or service item
1063
1064       if (   $form->{"partsgroup_$i"}
1065           && ($form->{"partsnumber_$i"} eq "")
1066           && ($form->{"description_$i"} eq "")) {
1067         $form->{rowcount}--;
1068         $form->{"discount_$i"} = "";
1069         &display_form;
1070           } else {
1071
1072         $form->{"id_$i"}   = 0;
1073         $form->{"unit_$i"} = $locale->text('ea');
1074
1075         &new_item;
1076
1077       }
1078     }
1079   }
1080   $lxdebug->leave_sub();
1081 }
1082
1083 sub post {
1084   $lxdebug->enter_sub();
1085   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
1086   $form->isblank("customer", $locale->text('Customer missing!'));
1087
1088   # if oldcustomer ne customer redo form
1089   if (&check_name(customer)) {
1090     &update;
1091     exit;
1092   }
1093   if ($form->{second_run}) {
1094     $form->{print_and_post} = 0;
1095   }
1096
1097   &validate_items;
1098
1099   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1100   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1101
1102   $form->error($locale->text('Cannot post invoice for a closed period!'))
1103     if ($invdate <= $closedto);
1104
1105   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1106     if ($form->{currency} ne $form->{defaultcurrency});
1107
1108   for $i (1 .. $form->{paidaccounts}) {
1109     if ($form->{"paid_$i"}) {
1110       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1111
1112       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1113
1114       $form->error($locale->text('Cannot post payment for a closed period!'))
1115         if ($datepaid <= $closedto);
1116
1117       if ($form->{currency} ne $form->{defaultcurrency}) {
1118         $form->{"exchangerate_$i"} = $form->{exchangerate}
1119           if ($invdate == $datepaid);
1120         $form->isblank("exchangerate_$i",
1121                        $locale->text('Exchangerate for payment missing!'));
1122       }
1123     }
1124   }
1125
1126   ($form->{AR})      = split /--/, $form->{AR};
1127   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1128
1129   $form->{label} = $locale->text('Invoice');
1130
1131   $form->{id} = 0 if $form->{postasnew};
1132
1133   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1134   if (!$form->{invnumber} || $form->{postasnew}) {
1135     $form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber");
1136   }
1137   if ($print_post) {
1138     if (!(IS->post_invoice(\%myconfig, \%$form))) {
1139       $form->error($locale->text('Cannot post invoice!'));
1140     }
1141   } else {
1142     $form->redirect(
1143             $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1144       if (IS->post_invoice(\%myconfig, \%$form));
1145     $form->error($locale->text('Cannot post invoice!'));
1146   }
1147
1148   $lxdebug->leave_sub();
1149 }
1150
1151 sub print_and_post {
1152   $lxdebug->enter_sub();
1153
1154   $old_form               = new Form;
1155   $print_post             = 1;
1156   $form->{print_and_post} = 1;
1157   &post();
1158
1159   &edit();
1160   $lxdebug->leave_sub();
1161
1162 }
1163
1164 sub preview {
1165   $lxdebug->enter_sub();
1166
1167   $form->{preview} = 1;
1168   $old_form = new Form;
1169   for (keys %$form) { $old_form->{$_} = $form->{$_} }
1170   $old_form->{rowcount}++;
1171
1172   &print_form($old_form);
1173   $lxdebug->leave_sub();
1174
1175 }
1176
1177 sub delete {
1178   $lxdebug->enter_sub();
1179   if ($form->{second_run}) {
1180     $form->{print_and_post} = 0;
1181   }
1182   $form->header;
1183
1184   print qq|
1185 <body>
1186
1187 <form method=post action=$form->{script}>
1188 |;
1189
1190   # delete action variable
1191   map { delete $form->{$_} } qw(action header);
1192
1193   foreach $key (keys %$form) {
1194     $form->{$key} =~ s/\"/&quot;/g;
1195     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1196   }
1197
1198   print qq|
1199 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1200
1201 <h4>|
1202     . $locale->text('Are you sure you want to delete Invoice Number')
1203     . qq| $form->{invnumber}
1204 </h4>
1205
1206 <p>
1207 <input name=action class=submit type=submit value="|
1208     . $locale->text('Yes') . qq|">
1209 </form>
1210 |;
1211
1212   $lxdebug->leave_sub();
1213 }
1214
1215 sub yes {
1216   $lxdebug->enter_sub();
1217
1218   $form->redirect($locale->text('Invoice deleted!'))
1219     if (IS->delete_invoice(\%myconfig, \%$form, $spool));
1220   $form->error($locale->text('Cannot delete invoice!'));
1221
1222   $lxdebug->leave_sub();
1223 }