1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 1998-2002
5 # Author: Dieter Simader
6 # Email: dsimader@sql-ledger.org
7 # Web: http://www.sql-ledger.org
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
24 # Inventory invoicing module
26 #======================================================================
32 require "$form->{path}/io.pl";
33 require "$form->{path}/arap.pl";
43 $form->{title} = $locale->text('Add Sales Invoice');
54 $form->{title} = $locale->text('Edit Sales Invoice');
66 $form->create_links("AR", \%myconfig, "customer");
68 IS->get_customer(\%myconfig, \%$form);
69 IS->retrieve_invoice(\%myconfig, \%$form);
72 @curr = split /:/, $form->{currencies};
74 $form->{defaultcurrency} = $curr[0];
76 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
78 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
80 if (@{ $form->{all_customer} }) {
81 $form->{customer} = qq|$form->{customer}--$form->{customer_id}|;
82 map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" } (@{ $form->{all_customer} });
86 $form->{forex} = $form->{exchangerate};
87 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
89 foreach $key (keys %{ $form->{AR_links} }) {
91 foreach $ref (@{ $form->{AR_links}{$key} }) {
92 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
95 if ($key eq "AR_paid") {
96 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
97 $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
99 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1;
100 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
101 $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
102 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
103 $form->{paidaccounts} = $i;
106 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
110 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
112 $form->{AR} = $form->{AR_1} unless $form->{id};
114 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
119 sub prepare_invoice {
121 $form->{type} = "invoice";
122 $form->{format} = "html";
123 $form->{media} = "screen";
127 map { $form->{$_} =~ s/"/"/g } qw(invnumber ordnumber shippingpoint notes);
129 foreach $ref (@{ $form->{invoice_details} } ) {
131 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
132 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
134 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
136 $decimalplaces = ($dec > 2) ? $dec : 2;
138 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
139 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
141 map { $form->{"${_}_$i"} =~ s/"/"/g } qw(partnumber description unit);
142 $form->{rowcount} = $i;
153 # set option selected
154 foreach $item (qw(AR customer currency)) {
155 $form->{"select$item"} =~ s/ selected//;
156 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
159 $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
161 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
162 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
166 if ($form->{currency} ne $form->{defaultcurrency}) {
167 if ($form->{forex}) {
168 $exchangerate .= qq|<th align=right>|.$locale->text('Exchangerate').qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
170 $exchangerate .= qq|<th align=right>|.$locale->text('Exchangerate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
174 <input type=hidden name=forex value=$form->{forex}>
177 $customer = ($form->{selectcustomer}) ? qq|<select name=customer>$form->{selectcustomer}</select>\n<input type=hidden name="selectcustomer" value="$form->{selectcustomer}">| : qq|<input name=customer value="$form->{customer}" size=35>|;
179 $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
187 <form method=post action=$form->{script}>
189 <input type=hidden name=id value=$form->{id}>
191 <input type=hidden name=type value=$form->{type}>
192 <input type=hidden name=media value=$form->{media}>
193 <input type=hidden name=format value=$form->{format}>
195 <input type=hidden name=title value="$form->{title}">
196 <input type=hidden name=vc value="customer">
197 <input type=hidden name=employee value="$form->{employee}">
199 <input type=hidden name=discount value=$form->{discount}>
200 <input type=hidden name=creditlimit value=$form->{creditlimit}>
201 <input type=hidden name=creditremaining value=$form->{creditremaining}>
203 <input type=hidden name=closedto value=$form->{closedto}>
204 <input type=hidden name=locked value=$form->{locked}>
208 <th class=listtop>$form->{title}</font></th>
218 <th align=right nowrap>|.$locale->text('Record in').qq|</th>
219 <td colspan=3><select name=AR>$form->{selectAR}</select></td>
220 <input type=hidden name=selectAR value="$form->{selectAR}">
223 <th align=right nowrap>|.$locale->text('Customer').qq|</th>
224 <td colspan=3>$customer</td>
225 <input type=hidden name=customer_id value=$form->{customer_id}>
226 <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
233 <th align=left nowrap>|.$locale->text('Credit Limit').qq|</th>
234 <td>$form->{creditlimit}</td>
235 <th align=left nowrap>|.$locale->text('Remaining').qq|</th>
236 <td class="plus$n">$form->{creditremaining}</font></td>
242 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
243 <td><select name=currency>$form->{selectcurrency}</select></td>
244 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
245 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
246 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
247 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
251 <th align=right nowrap>|.$locale->text('Ship via').qq|</th>
252 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
259 <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
260 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
263 <th align=right>|.$locale->text('Invoice Date').qq|</th>
264 <td><input name=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>
267 <th align=right>|.$locale->text('Due Date').qq|</th>
268 <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
271 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
272 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
284 <!-- shipto are in hidden variables -->
286 <input type=hidden name=shiptoname value="$form->{shiptoname}">
287 <input type=hidden name=shiptoaddr1 value="$form->{shiptoaddr1}">
288 <input type=hidden name=shiptoaddr2 value="$form->{shiptoaddr2}">
289 <input type=hidden name=shiptoaddr3 value="$form->{shiptoaddr3}">
290 <input type=hidden name=shiptoaddr4 value="$form->{shiptoaddr4}">
291 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
292 <input type=hidden name=shiptophone value="$form->{shiptophone}">
293 <input type=hidden name=shiptofax value="$form->{shiptofax}">
294 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
296 <!-- email variables -->
297 <input type=hidden name=message value="$form->{message}">
298 <input type=hidden name=email value="$form->{email}">
299 <input type=hidden name=subject value="$form->{subject}">
300 <input type=hidden name=cc value="$form->{cc}">
301 <input type=hidden name=bcc value="$form->{bcc}">
303 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
306 foreach $item (split / /, $form->{taxaccounts}) {
308 <input type=hidden name="${item}_rate" value="$form->{"${item}_rate"}">
309 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
310 <input type=hidden name="${item}_taxnumber" value="$form->{"${item}_taxnumber"}">
320 $form->{invtotal} = $form->{invsubtotal};
322 if (($rows = $form->numtextrows($form->{notes}, 50, 8)) < 2) {
325 $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
328 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
330 if ($form->{taxaccounts}) {
332 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b><br><br>
336 if (!$form->{taxincluded}) {
338 foreach $item (split / /, $form->{taxaccounts}) {
339 if ($form->{"${item}_base"}) {
340 $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
341 $form->{invtotal} += $form->{"${item}_total"};
342 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
346 <th align=right>$form->{"${item}_description"}</th>
347 <td align=right>$form->{"${item}_total"}</td>
353 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
357 <th align=right>|.$locale->text('Subtotal').qq|</th>
358 <td align=right>$form->{invsubtotal}</td>
364 $form->{oldinvtotal} = $form->{invtotal};
365 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
375 <th align=left>|.$locale->text('Notes').qq|</th>
382 <td align=right width=100%>
388 <th align=right>|.$locale->text('Total').qq|</th>
389 <td align=right>$form->{invtotal}</td>
400 <tr class=listheading>
401 <th class=listheading colspan=5>|.$locale->text('Payments')
406 if ($form->{currency} eq $form->{defaultcurrency}) {
407 @column_index = qw(datepaid source paid AR_paid);
409 @column_index = qw(datepaid source paid exchangerate AR_paid);
412 $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
413 $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
414 $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
415 $column_data{AR_paid} = "<th>".$locale->text('Account')."</th>";
416 $column_data{source} = "<th>".$locale->text('Source')."</th>";
421 map { print "$column_data{$_}\n" } @column_index;
426 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
427 for $i (1 .. $form->{paidaccounts}) {
432 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
433 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
436 $totalpaid += $form->{"paid_$i"};
437 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
438 $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
440 $exchangerate = qq| |;
441 if ($form->{currency} ne $form->{defaultcurrency}) {
442 if ($form->{"forex_$i"}) {
443 $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
445 $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
450 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
453 $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
454 $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
455 $column_data{"AR_paid_$i"} = qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
456 $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
457 $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
459 map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
465 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
466 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
467 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
468 <input type=hidden name=oldtotalpaid value=$totalpaid>
482 <td><hr size=3 noshade></td>
488 $invdate = $form->datetonum($form->{invdate}, \%myconfig);
489 $closedto = $form->datetonum($form->{closedto}, \%myconfig);
493 <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
494 <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
495 <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
496 <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
499 if (!$form->{revtrans}) {
500 if (!$form->{locked}) {
502 <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
503 <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
508 if ($invdate > $closedto) {
510 <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
511 <input class=submit type=submit name=action value="|.$locale->text('Order').qq|">
516 if ($invdate > $closedto) {
517 print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
518 <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
519 <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
520 <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
521 <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
527 <input type=hidden name=rowcount value=$form->{rowcount}>
529 <input name=callback type=hidden value="$form->{callback}">
531 <input type=hidden name=path value=$form->{path}>
532 <input type=hidden name=login value=$form->{login}>
533 <input type=hidden name=password value=$form->{password}>
546 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
548 &check_name(customer);
552 $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy')));
554 for $i (1 .. $form->{paidaccounts}) {
555 if ($form->{"paid_$i"}) {
556 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
558 $form->{"exchangerate_$i"} = $exchangerate if ($form->{"forex_$i"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy')));
562 $i = $form->{rowcount};
563 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
565 # if last row empty, check the form otherwise retrieve new item
566 if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
568 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
573 IS->retrieve_item(\%myconfig, \%$form);
575 $rows = scalar @{ $form->{item_list} };
577 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
580 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
589 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
591 map { $form->{item_list}[$i]{$_} =~ s/"/"/g } qw(partnumber description unit);
592 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
594 $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
595 ($dec) = ($s =~ /\.(\d+)/);
597 $decimalplaces = ($dec > 2) ? $dec : 2;
600 $form->{"sellprice_$i"} = $sellprice;
602 # if there is an exchange rate adjust sellprice
603 $form->{"sellprice_$i"} /= $exchangerate;
606 $form->{"listprice_$i"} /= $exchangerate;
608 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
609 map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
610 map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
611 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
613 $form->{creditremaining} -= $amount;
616 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
618 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
625 # ok, so this is a new part
626 # ask if it is a part or service item
628 if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
630 $form->{"discount_$i"} = "";
634 $form->{"id_$i"} = 0;
635 $form->{"unit_$i"} = $locale->text('ea');
648 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
649 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
650 $form->isblank("customer", $locale->text('Customer missing!'));
652 # if oldcustomer ne customer redo form
653 if (&check_name(customer)) {
660 $closedto = $form->datetonum($form->{closedto}, \%myconfig);
661 $invdate = $form->datetonum($form->{invdate}, \%myconfig);
663 $form->error($locale->text('Cannot post invoice for a closed period!')) if ($invdate <= $closedto);
665 $form->isblank("exchangerate", $locale->text('Exchangerate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
667 for $i (1 .. $form->{paidaccounts}) {
668 if ($form->{"paid_$i"}) {
669 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
671 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
673 $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
675 if ($form->{currency} ne $form->{defaultcurrency}) {
676 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
677 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
683 ($form->{AR}) = split /--/, $form->{AR};
684 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
686 $form->{label} = $locale->text('Invoice');
688 $form->{id} = 0 if $form->{postasnew};
690 $form->redirect($locale->text('Invoice posted!')) if (IS->post_invoice(\%myconfig, \%$form));
691 $form->error($locale->text('Cannot post invoice!'));
705 <form method=post action=$form->{script}>
708 # delete action variable
709 delete $form->{action};
711 foreach $key (keys %$form) {
712 $form->{$key} =~ s/"/"/g;
713 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
717 <h2 class=confirm>|.$locale->text('Confirm!').qq|</font></h2>
719 <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}
723 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
734 $form->redirect($locale->text('Invoice deleted!')) if (IS->delete_invoice(\%myconfig, \%$form));
735 $form->error($locale->text('Cannot delete invoice!'));