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 received module
26 #======================================================================
32 require "$form->{path}/io.pl";
33 require "$form->{path}/arap.pl";
42 $form->{title} = $locale->text('Add Vendor Invoice');
53 $form->{title} = $locale->text('Edit Vendor Invoice');
65 $form->create_links("AP", \%myconfig, "vendor");
67 IR->get_vendor(\%myconfig, \%$form);
68 IR->retrieve_invoice(\%myconfig, \%$form);
71 @curr = split /:/, $form->{currencies};
73 $form->{defaultcurrency} = $curr[0];
75 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
77 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
80 if (@{ $form->{all_vendor} }) {
81 $form->{vendor} = "$form->{vendor}--$form->{vendor_id}";
82 map { $form->{selectvendor} .= "<option>$_->{name}--$_->{id}\n" } (@{ $form->{all_vendor} });
87 $form->{forex} = $form->{exchangerate};
88 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
90 foreach $key (keys %{ $form->{AP_links} }) {
92 foreach $ref (@{ $form->{AP_links}{$key} }) {
93 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
96 if ($key eq "AP_paid") {
97 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
98 $form->{"AP_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
100 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount};
101 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
102 $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
103 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
104 $form->{paidaccounts} = $i;
107 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
111 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
113 $form->{AP} = $form->{AP_1} unless $form->{id};
115 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
121 sub prepare_invoice {
125 map { $form->{$_} =~ s/"/"/g } qw(invnumber ordnumber);
127 foreach $ref (@{ $form->{invoice_details} }) {
129 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
131 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
133 $decimalplaces = ($dec > 2) ? $dec : 2;
135 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
136 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
138 $form->{rowcount} = $i;
148 # set option selected
149 foreach $item (qw(AP vendor currency)) {
150 $form->{"select$item"} =~ s/ selected//;
151 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
154 $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
157 if ($form->{currency} ne $form->{defaultcurrency}) {
158 if ($form->{forex}) {
160 <th align=right nowrap>|.$locale->text('Exchangerate').qq|</th>
161 <td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>
165 <th align=right nowrap>|.$locale->text('Exchangerate').qq|</th>
166 <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
171 <input type=hidden name=forex value=$form->{forex}>
174 $vendor = ($form->{selectvendor}) ? qq|<select name=vendor>$form->{selectvendor}</select>\n<input type=hidden name="selectvendor" value="$form->{selectvendor}">| : qq|<input name=vendor value="$form->{vendor}" size=35>|;
182 <form method=post action=$form->{script}>
184 <input type=hidden name=id value=$form->{id}>
185 <input type=hidden name=title value="$form->{title}">
186 <input type=hidden name=vc value="vendor">
188 <input type=hidden name=closedto value=$form->{closedto}>
189 <input type=hidden name=locked value=$form->{locked}>
194 <th>$form->{title}</th>
204 <th align=right>|.$locale->text('Record in').qq|</th>
205 <td colspan=3><select name=AP>$form->{selectAP}</select></td>
206 <input type=hidden name=selectAP value="$form->{selectAP}">
209 <th align=right nowrap>|.$locale->text('Vendor').qq|</th>
210 <td colspan=3>$vendor</td>
212 <input type=hidden name=vendor_id value=$form->{vendor_id}>
213 <input type=hidden name=oldvendor value="$form->{oldvendor}">
217 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
218 <td><select name=currency>$form->{selectcurrency}</select></td>
226 <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
227 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
230 <th align=right nowrap>|.$locale->text('Invoice Date').qq|</th>
231 <td><input name=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>
234 <th align=right nowrap>|.$locale->text('Due Date').qq|</th>
235 <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
238 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
239 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
247 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
248 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
249 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
250 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
252 <input type=hidden name=taxpart value="$form->{taxpart}">
253 <input type=hidden name=taxservice value="$form->{taxservice}">
255 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
258 foreach $item (split / /, $form->{taxaccounts}) {
260 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
261 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
271 $form->{invtotal} = $form->{invsubtotal};
273 if (($rows = $form->numtextrows($form->{notes}, 50, 8)) < 2) {
276 $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
279 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
280 if ($form->{taxaccounts}) {
282 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b><br><br>
286 if (!$form->{taxincluded}) {
288 foreach $item (split / /, $form->{taxaccounts}) {
289 if ($form->{"${item}_base"}) {
290 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
291 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
295 <th align=right>$form->{"${item}_description"}</th>
296 <td align=right>$form->{"${item}_total"}</td>
302 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
306 <th align=right>|.$locale->text('Subtotal').qq|</th>
307 <td align=right>$form->{invsubtotal}</td>
314 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
318 <td colspan=$colspan>
319 <table cellspacing="0">
324 <th align=left>|.$locale->text('Notes').qq|</th>
331 <td colspan=2 align=right width=100%>
337 <th align=right>|.$locale->text('Total').qq|</th>
338 <td align=right>$form->{invtotal}</td>
347 <td colspan=$colspan>
350 <th colspan=5 class=listheading>|.$locale->text('Payments').qq|</th>
354 if ($form->{currency} eq $form->{defaultcurrency}) {
355 @column_index = qw(datepaid source paid AP_paid);
357 @column_index = qw(datepaid source paid exchangerate AP_paid);
360 $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
361 $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
362 $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
363 $column_data{AP_paid} = "<th>".$locale->text('Account')."</th>";
364 $column_data{source} = "<th>".$locale->text('Source')."</th>";
369 map { print "$column_data{$_}\n" } @column_index;
374 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
375 for $i (1 .. $form->{paidaccounts}) {
381 $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
382 $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
385 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
386 $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
388 $exchangerate = qq| |;
389 if ($form->{currency} ne $form->{defaultcurrency}) {
390 if ($form->{"forex_$i"}) {
391 $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
393 $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
397 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
400 $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
401 $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
402 $column_data{"AP_paid_$i"} = qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
403 $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
404 $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value=$form->{"source_$i"}></td>|;
406 map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
414 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
415 <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
420 <td><hr size=3 noshade></td>
426 $invdate = $form->datetonum($form->{invdate}, \%myconfig);
427 $closedto = $form->datetonum($form->{closedto}, \%myconfig);
430 print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
433 if (!$form->{revtrans}) {
434 if (!$form->{locked}) {
436 <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
437 <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
442 if ($invdate > $closedto) {
444 <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
445 <input class=submit type=submit name=action value="|.$locale->text('Order').qq|">
450 if ($invdate > $closedto) {
451 print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
452 <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
458 <input type=hidden name=rowcount value=$form->{rowcount}>
460 <input name=callback type=hidden value="$form->{callback}">
462 <input type=hidden name=path value=$form->{path}>
463 <input type=hidden name=login value=$form->{login}>
464 <input type=hidden name=password value=$form->{password}>
478 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
484 $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell')));
487 for $i (1 .. $form->{paidaccounts}) {
488 if ($form->{"paid_$i"}) {
489 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
491 $form->{"exchangerate_$i"} = $exchangerate if ($form->{"forex_$i"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell')));
495 $i = $form->{rowcount};
496 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
498 if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
504 IR->retrieve_item(\%myconfig, \%$form);
506 my $rows = scalar @{ $form->{item_list} };
509 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
517 # override sellprice if there is one entered
518 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
520 map { $form->{item_list}[$i]{$_} =~ s/"/"/g } qw(partnumber description unit);
522 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
524 $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
526 ($dec) = ($s =~ /\.(\d+)/);
528 $decimalplaces = ($dec > 2) ? $dec : 2;
531 $form->{"sellprice_$i"} = $sellprice;
533 # if there is an exchange rate adjust sellprice
534 $form->{"sellprice_$i"} /= $exchangerate;
537 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
538 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
544 # ok, so this is a new part
545 # ask if it is a part or service item
547 if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
549 $form->{"discount_$i"} = "";
553 $form->{"id_$i"} = 0;
554 $form->{"unit_$i"} = $locale->text('ea');
567 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
568 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
569 $form->isblank("vendor", $locale->text('Vendor missing!'));
571 # if the vendor changed get new values
572 if (&check_name(vendor)) {
579 $closedto = $form->datetonum($form->{closedto}, \%myconfig);
580 $invdate = $form->datetonum($form->{invdate}, \%myconfig);
582 $form->error($locale->text('Cannot post invoice for a closed period!')) if ($invdate <= $closedto);
584 $form->isblank("exchangerate", $locale->text('Exchangerate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
586 for $i (1 .. $form->{paidaccounts}) {
587 if ($form->{"paid_$i"}) {
588 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
590 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
592 $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
594 if ($form->{currency} ne $form->{defaultcurrency}) {
595 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
596 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
602 ($form->{AP}) = split /--/, $form->{AP};
603 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
605 $form->{id} = 0 if $form->{postasnew};
607 $form->redirect($locale->text('Invoice posted!')) if (IR->post_invoice(\%myconfig, \%$form));
608 $form->error($locale->text('Cannot post invoice!'));
621 <form method=post action=$form->{script}>
624 # delete action variable
625 delete $form->{action};
627 foreach $key (keys %$form) {
628 $form->{$key} =~ s/"/"/g;
629 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
633 <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
635 <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}</h4>
637 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
648 $form->redirect($locale->text('Invoice deleted!')) if (IR->delete_invoice(\%myconfig, \%$form));
649 $form->error($locale->text('Cannot delete invoice!'));