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