1 #=====================================================================
2 # SQL-Ledger Accounting
5 # Author: Dieter Simader
6 # Email: dsimader@sql-ledger.org
7 # Web: http://www.sql-ledger.org
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
27 #======================================================================
35 require "$form->{path}/arap.pl";
43 $form->{callback} = "$form->{script}?action=payment&vc=$form->{vc}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&all_vc=$form->{all_vc}&type=$form->{type}";
45 $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
46 $form->{arap} = lc $form->{ARAP};
48 # setup customer/vendor selection for open invoices
49 if ($form->{all_vc}) {
50 $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
52 CP->get_openvc(\%myconfig, \%$form);
55 $form->{"select$form->{vc}"} = "";
57 if ($form->{"all_$form->{vc}"}) {
58 $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
59 map { $form->{"select$form->{vc}"} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } @{ $form->{"all_$form->{vc}"} };
63 if (@{ $form->{all_departments} }) {
64 $form->{selectdepartment} = "<option>\n";
65 $form->{department} = "$form->{department}--$form->{department_id}" if $form->{department};
67 map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
70 if (@{ $form->{all_languages} }) {
71 $form->{selectlanguage} = "<option>\n";
72 map { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| } @{ $form->{all_languages} };
75 CP->paymentaccounts(\%myconfig, \%$form);
77 $form->{selectaccount} = "";
78 $form->{"select$form->{ARAP}"} = "";
80 map { $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{PR}{"$form->{ARAP}_paid"} };
81 map { $form->{"select$form->{ARAP}"} .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{PR}{$form->{ARAP}} };
84 @curr = split /:/, $form->{currencies};
86 $form->{defaultcurrency} = $curr[0];
88 $form->{selectcurrency} = "";
89 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
91 $form->{oldcurrency} = $form->{currency};
93 if ($form->{currency} ne $form->{defaultcurrency}) {
94 $form->{forex} = $form->{exchangerate} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid}, ($form->{vc} eq 'customer') ? "buy" : "sell");
97 $form->{media} = $myconfig{printer};
98 $form->{format} = "pdf" unless $myconfig{printer};
109 $vclabel = ucfirst $form->{vc};
110 $vclabel = $locale->text($vclabel);
112 if ($form->{type} eq 'receipt') {
113 $form->{title} = $locale->text('Receipt');
115 if ($form->{type} eq 'check') {
116 $form->{title} = $locale->text('Payment');
120 # $locale->text('Customer')
121 # $locale->text('Vendor')
123 if ($form->{$form->{vc}} eq "") {
124 map { $form->{$_} = "" } qw(address1 address2 city zipcode state country);
128 if ($form->{currency} ne $form->{defaultcurrency}) {
129 $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
130 if ($form->{forex}) {
133 <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
134 <td colspan=3><input type=hidden name=exchangerate size=10 value=$form->{exchangerate}>$form->{exchangerate}</td>
140 <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
141 <td colspan=3><input name=exchangerate size=10 value=$form->{exchangerate}></td>
147 foreach $item ($form->{vc}, department) {
148 $form->{"select$item"} = $form->unescape($form->{"select$item"});
149 $form->{"select$item"} =~ s/ selected//;
150 $form->{"select$item"} =~ s/(<option value="\Q$form->{$item}\E")/$1 selected/;
153 foreach $item (account, currency, $form->{ARAP}) {
154 $form->{"select$item"} =~ s/ selected//;
155 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
158 $vc = ($form->{"select$form->{vc}"}) ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>| : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
160 if ($form->{all_vc}) {
162 $form->{openinvoices} = "";
165 $form->{openinvoices} = 1;
168 # $locale->text('AR')
169 # $locale->text('AP')
173 <th align="right" nowrap>|.$locale->text('Department').qq|</th>
174 <td><select name=department>$form->{selectdepartment}</select>
175 <input type=hidden name=selectdepartment value="|.$form->escape($form->{selectdepartment},1).qq|">
176 <input type=hidden name=olddepartment value="$form->{department}">
180 | if $form->{selectdepartment};
184 $arap = lc $form->{ARAP};
189 <form method=post action=$form->{script}>
191 <!-- <input type=hidden name=id value=1> -->
193 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
194 <input type=hidden name=closedto value=$form->{closedto}>
195 <input type=hidden name=vc value=$form->{vc}>
196 <input type=hidden name=type value=$form->{type}>
197 <input type=hidden name=formname value=$form->{type}>
198 <input type=hidden name=queued value="$form->{queued}">
199 <input type=hidden name=arap value=$arap>
200 <input type=hidden name=ARAP value=$form->{ARAP}>
201 <input type=hidden name=openinvoices value=$form->{openinvoices}>
202 <input type=hidden name=title value="$form->{title}">
206 <th class=listtop>$form->{title}</th>
217 <input name=all_vc type=checkbox style=checkbox value=Y $allvc>
218 <input type=hidden name="oldall_vc" value="$form->{all_vc}"></td>
219 <th align=left>|.$locale->text('All').qq|</th>
222 <th align=right>$vclabel</th>
224 <input type=hidden name="select$form->{vc}" value="|.$form->escape($form->{"select$form->{vc}"},1).qq|">
225 <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
226 <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
229 <th align=right nowrap>|.$locale->text('Address').qq|</th>
233 <td>$form->{address1}</td>
236 <td>$form->{address2}</td>
238 <td>$form->{city}</td>
241 <td>$form->{state}</td>
244 <td>$form->{zipcode}</td>
247 <td>$form->{country}</td>
251 <input type=hidden name=address1 value="$form->{address1}">
252 <input type=hidden name=address2 value="$form->{address2}">
253 <input type=hidden name=city value="$form->{city}">
254 <input type=hidden name=state value="$form->{state}">
255 <input type=hidden name=zipcode value="$form->{zipcode}">
256 <input type=hidden name=country value="$form->{country}">
259 <th align=right>|.$locale->text('Memo').qq|</th>
260 <td colspan=2><input name="memo" size=30 value="$form->{memo}"></td>
268 <th align=right nowrap>|.$locale->text($form->{ARAP}).qq|</th>
269 <td colspan=3><select name=$form->{ARAP}>$form->{"select$form->{ARAP}"}</select>
271 <input type=hidden name="select$form->{ARAP}" value="$form->{"select$form->{ARAP}"}">
274 <th align=right nowrap>|.$locale->text('Account').qq|</th>
275 <td colspan=3><select name=account>$form->{selectaccount}</select>
276 <input type=hidden name=selectaccount value="$form->{selectaccount}">
280 <th align=right nowrap>|.$locale->text('Date').qq|</th>
281 <td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11></td>
284 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
285 <td><select name=currency>$form->{selectcurrency}</select></td>
286 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
287 <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
291 <th align=right nowrap>|.$locale->text('Source').qq|</th>
292 <td colspan=3><input name=source value="$form->{source}" size=10></td>
295 <th align=right nowrap>|.$locale->text('Amount').qq|</th>
296 <td colspan=3><input name=amount size=10 value=|.$form->format_amount(\%myconfig, $form->{amount}, 2).qq|></td>
311 @column_index = qw(invnumber transdate amount due checked paid);
313 $colspan = $#column_index + 1;
315 $invoice = $locale->text('Invoices');
318 <input type=hidden name=column_index value="id @column_index">
323 <th class=listheading colspan=$colspan>$invoice</th>
327 $column_data{invnumber} = qq|<th nowrap>|.$locale->text('Invoice')."</th>";
328 $column_data{transdate} = qq|<th nowrap>|.$locale->text('Date')."</th>";
329 $column_data{amount} = qq|<th nowrap>|.$locale->text('Amount')."</th>";
330 $column_data{due} = qq|<th nowrap>|.$locale->text('Amount Due')."</th>";
331 $column_data{paid} = qq|<th nowrap>|.$locale->text('Amount')."</th>";
332 $column_data{checked} = qq|<th nowrap>|.$locale->text('Select')."</th>";
337 map { print "$column_data{$_}\n" } @column_index;
342 for $i (1 .. $form->{rowcount}) {
344 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(amount due paid);
346 $totalamount += $form->{"amount_$i"};
347 $totaldue += $form->{"due_$i"};
348 $totalpaid += $form->{"paid_$i"};
350 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due paid);
352 $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
353 <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
354 <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
355 $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
356 <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
357 $column_data{amount} = qq|<td align=right width=15%>$form->{"amount_$i"}</td>
358 <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
359 $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
360 <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
362 $column_data{paid} = qq|<td align=right width=15%><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
364 $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
365 $column_data{checked} = qq|<td align=center width=10%><input name="checked_$i" type=checkbox style=checkbox $form->{"checked_$i"}></td>|;
371 map { print "$column_data{$_}\n" } @column_index;
377 map { $column_data{$_} = "<td> </td>" } @column_index;
379 $column_data{amount} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalamount, 2, " ").qq|</th>|;
380 $column_data{due} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totaldue, 2, " ").qq|</th>|;
381 $column_data{paid} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalpaid, 2, " ").qq|</th>|;
386 map { print "$column_data{$_}\n" } @column_index;
399 $form->{DF}{$form->{format}} = "selected";
401 if ($form->{selectlanguage}) {
402 $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
403 $form->{"selectlanguage"} =~ s/ selected//;
404 $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
405 $lang = qq|<select name=language_code>$form->{selectlanguage}</select>
406 <input type=hidden name=selectlanguage value="|.
407 $form->escape($form->{selectlanguage},1).qq|">|;
411 <option value=screen>|.$locale->text('Screen') if $latex;
413 if (%printer && $latex) {
415 <option value="$_">$_| } sort keys %printer;
419 <option value=queue>|.$locale->text('Queue');
421 <option value=postscript $form->{DF}{postscript}>|.$locale->text('Postscript').qq|
422 <option value=pdf $form->{DF}{pdf}>|.$locale->text('PDF');
428 <td><hr size=3 noshade></td>
432 <input type=hidden name=callback value="$form->{callback}">
433 <input type=hidden name=rowcount value=$form->{rowcount}>
435 <input type=hidden name=path value=$form->{path}>
436 <input type=hidden name=login value=$form->{login}>
437 <input type=hidden name=sessionid value=$form->{sessionid}>
440 <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
441 <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
445 <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">|;
448 $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
452 <select name=format>$format</select>
453 <select name=media>$media</select>
456 if ($form->{menubar}) {
457 require "$form->{path}/menu.pl";
472 my ($new_name_selected) = @_;
474 if ($form->{vc} eq 'customer') {
481 $updated = &check_name($form->{vc}, $locale->text('Nothing outstanding for ') . $form->{customer});
483 if ($form->{department} ne $form->{olddepartment}) {
487 # if we switched to all_vc
488 if ($form->{all_vc} ne $form->{oldall_vc}) {
490 $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
492 $form->{"select$form->{vc}"} = "";
494 if ($form->{all_vc}) {
495 $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
497 if ($form->{"all_$form->{vc}"}) {
498 map { $form->{"select$form->{vc}"} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } @{ $form->{"all_$form->{vc}"} };
502 CP->get_openvc(\%myconfig, \%$form);
504 if ($form->{"all_$form->{vc}"}) {
505 $newvc = qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
506 map { $form->{"select$form->{vc}"} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } @{ $form->{"all_$form->{vc}"} };
509 # if the name is not the same
510 if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
511 $form->{$form->{vc}} = $newvc;
512 &check_name($form->{vc}, $locale->text('Nothing outstanding for ') . $form->{customer});
517 if (@{ $form->{all_languages} }) {
518 $form->{selectlanguage} = "<option>\n";
519 map { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| } @{ $form->{all_languages} };
524 if ($new_name_selected || $updated) {
525 CP->get_openinvoices(\%myconfig, \%$form);
526 ($newvc) = split /--/, $form->{$form->{vc}};
527 $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;;
531 if ($form->{currency} ne $form->{oldcurrency}) {
532 $form->{oldcurrency} = $form->{currency};
534 CP->get_openinvoices(\%myconfig, \%$form);
540 $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid}, $buysell)));
542 $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
546 $form->{rowcount} = 0;
548 $form->{queued} = "";
551 foreach $ref (@{ $form->{PR} }) {
553 $form->{"id_$i"} = $ref->{id};
554 $form->{"invnumber_$i"} = $ref->{invnumber};
555 $form->{"transdate_$i"} = $ref->{transdate};
556 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
557 $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
558 $form->{"due_$i"} = ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
559 $form->{"checked_$i"} = "";
560 $form->{"paid_$i"} = "";
563 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due);
566 $form->{rowcount} = $i;
570 $amount = $form->{amount};
571 for $i (1 .. $form->{rowcount}) {
573 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(amount due paid);
575 if ($form->{"checked_$i"}) {
577 if (!$form->{"paid_$i"}) {
578 $form->{"paid_$i"} = $form->{"due_$i"};
581 $amount -= $form->{"paid_$i"};
583 $form->{"paid_$i"} = "";
586 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due paid);
601 if ($form->{currency} ne $form->{defaultcurrency}) {
602 $form->error($locale->text('Exchange rate missing!')) unless $form->{exchangerate};
605 $msg1 = "$form->{title} posted!";
606 $msg2 = "Cannot post $form->{title}!";
608 # $locale->text('Payment posted!')
609 # $locale->text('Receipt posted!')
610 # $locale->text('Cannot post Payment!')
611 # $locale->text('Cannot post Receipt!')
614 $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
616 $form->redirect($locale->text($msg1)) if (CP->process_payment(\%myconfig, \%$form));
617 $form->error($locale->text($msg2));
626 ($whole, $form->{decimal}) = split /\./, $form->{amount};
628 $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
630 $form->{decimal} .= "00";
631 $form->{decimal} = substr($form->{decimal}, 0, 2);
633 $countrycode = ($form->{language_code}) ? $form->{language_code} : $myconfig{countrycode};
634 $check = new CP $countrycode;
636 $form->{text_amount} = $check->num2text($whole);
638 &{ "$form->{vc}_details" };
640 $form->{templates} = "$myconfig{templates}";
641 $form->{IN} = "$form->{formname}.tex";
643 if ($form->{media} !~ /(screen|queue)/) {
644 $form->{OUT} = "| $printer{$form->{media}}";
646 if ($form->{media} eq 'queue') {
647 %queued = split / /, $form->{queued};
649 if ($filename = $queued{$form->{formname}}) {
650 unlink "$spool/$filename";
651 $filename =~ s/\..*$//g;
656 $filename .= ($form->{format} eq 'postscript') ? '.ps' : '.pdf';
657 $form->{queued} = "$form->{formname} $filename";
658 $form->{OUT} = ">$spool/$filename";
660 $form->update_status(\%myconfig);
665 $form->{company} = $myconfig{company};
666 $form->{address} = $myconfig{address};
668 @a = qw(name company address text_amount address1 address2 city state zipcode country memo);
669 $form->format_string(@a);
671 $form->parse_template(\%myconfig, $userspath);
673 &update if $form->{media} ne 'screen';
678 sub customer_details { IS->customer_details(\%myconfig, \%$form) };
679 sub vendor_details { IR->vendor_details(\%myconfig, \%$form) };
684 &check_name($form->{vc}, $locale->text('Nothing outstanding for ') . $form->{customer});
686 if ($form->{currency} ne $form->{oldcurrency}) {
691 $form->error($locale->text('Date missing!')) unless $form->{datepaid};
693 $closedto = $form->datetonum($form->{closedto}, \%myconfig);
694 $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
696 $form->error($locale->text('Cannot process payment for a closed period!')) if ($datepaid <= $closedto);
698 # this is just to format the year
699 $form->{datepaid} = $locale->date(\%myconfig, $form->{datepaid});
701 $amount = $form->parse_amount(\%myconfig, $form->{amount});
702 $form->{amount} = $amount;
704 for $i (1 .. $form->{rowcount}) {
705 if ($form->{"paid_$i"}) {
706 $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
708 push(@{ $form->{paid} }, $form->{"paid_$i"});
709 push(@{ $form->{due} }, $form->{"due_$i"});
710 push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
711 push(@{ $form->{invdate} }, $form->{"transdate_$i"});
715 if ($form->round_amount($amount, 2) != 0) {
716 push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
717 push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
718 push(@{ $form->{invnumber} }, ($form->{ARAP} eq 'AR') ? $locale->text('Deposit') : $locale->text('Prepayment'));
719 push(@{ $form->{invdate} }, $form->{datepaid});