4 use vars qw( $opt_d $opt_s $opt_p $opt_m $opt_c $opt_e $opt_r );
6 use Date::Parse qw(str2time);
7 use FS::UID qw( dbh adminsuidsetup );
8 use FS::Record qw( qsearch qsearchs );
9 use FS::cust_main_county;
13 use FS::cust_credit_bill;
15 getopts('d:s:pm:c:er');
17 my $user = shift or &usage;
20 #i should be an option
21 my $taxname = 'HST, ON';
24 my $oldAutoCommit = $FS::UID::AutoCommit;
25 local $FS::UID::AutoCommit = 0;
34 #my $extra_sql = $opt_s ? " AND cust_main.state = '$opt_s' " : '';
36 my $start_date = $opt_d ? str2time($opt_d) : 0;
37 my $end_date = 1375471643;
39 my @cust_bill = qsearch({
40 'select' => 'cust_bill.*',
41 'table' => 'cust_bill',
42 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
43 'hashref' => { '_date' => { op=>'>', value=>$start_date } },
44 'extra_sql' => " AND _date < $end_date ",
45 # 'extra_sql' => $extra_sql,
48 my( @billpkgnums, @setuprecurs, @amounts );
50 foreach my $cust_bill ( @cust_bill ) {
52 my $tax = $cust_bill->tax;
54 my $cust_main = $cust_bill->cust_main;
56 next if $cust_bill->charged == 0
58 or $cust_main->tax eq 'Y';
66 foreach my $cust_bill_pkg ( $cust_bill->cust_bill_pkg ) {
67 my $cust_pkg = $cust_bill_pkg->cust_pkg or next;
69 my $loc = $cust_pkg->cust_location_or_main;
70 next if $opt_s && $loc->state ne $opt_s;
72 my $part_pkg = $cust_pkg->part_pkg;
73 next unless $cust_pkg->pkgpart == 34; #only pkgpart 34
75 #my $amount = $cust_bill_pkg->setup + $cust_bill_pkg->recur;
76 #$credit += $rate * $amount / 100;
78 if ( $cust_bill_pkg->setup ) {
79 my $scredit = sprintf('%.2f', ($rate * $cust_bill_pkg->setup / 100) + 0.00000001);
81 push @setuprecurs, 'setup';
82 push @billpkgnums, $cust_bill_pkg->billpkgnum;
83 push @amounts, $scredit;
86 if ( $cust_bill_pkg->recur ) {
87 my $rcredit = sprintf('%.2f', ($rate * $cust_bill_pkg->recur / 100) + 0.00000001);
89 push @setuprecurs, 'recur';
90 push @billpkgnums, $cust_bill_pkg->billpkgnum;
91 push @amounts, $rcredit;
96 $credit = sprintf('%.2f', $credit + 0.00000001);
100 #$credit = sprintf('%.2f', $credit + 0.00000001);
102 warn "invoice ". $cust_bill->invnum. ": credit of $credit is more than orignally taxed ($tax)\n" if $credit > $tax;
104 warn "invoice ". $cust_bill->invnum. ": credit of $credit is more than 50% of originally taxed ($tax)\n" if $credit-0.01 > $tax/2;
106 #my $cr_percent = sprintf('%.1f', 100 * $credit / $tax);
109 #print $cust_bill->invnum. ','. $cust_bill->custnum. ",$tax,$credit,$cr_percent%\n";
110 print $cust_bill->invnum. ','. $cust_bill->custnum. ',"'.
111 $cust_bill->cust_main->name. '",'. "$tax,$credit\n";
114 if ( $opt_m && ! $opt_r ) {
116 my $msg_template = qsearchs('msg_template', { 'msgnum' => $opt_m } )
117 or die "Template $opt_m not found";
118 my $error = $msg_template->send(
119 'cust_main' => $cust_main,
120 'object' => $cust_main,
123 warn "error sending email for invnum ". $cust_bill->invnum. ','.
124 " custnum ". $cust_bill->custnum. ": $error\n";
129 my $cust_credit = new FS::cust_credit {
130 'custnum' => $cust_main->custnum,
132 'reasonnum' => $opt_c,
134 my $error = $cust_credit->insert;
136 warn "error inserting credit: $error\n";
138 my $cust_credit_bill = new FS::cust_credit_bill {
139 'crednum' => $cust_credit->crednum,
140 'invnum' => $cust_bill->invnum,
143 my $aerror = $cust_credit_bill->insert;
145 warn "error applying credit to invnum ". $cust_bill->invnum. ": $aerror\n";
149 if ( $opt_e && ! $opt_r ) {
150 eval { $cust_bill->email };
152 warn "error sending invnum ". $cust_bill->invnum. ','.
153 " custnum ". $cust_bill->custnum. ": $@\n";
160 $dbh->rollback or die $dbh->errstr; #if $oldAutoCommit;
162 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
166 die "usage: cust_bill-credit [ -d date ] [ -s state ] [ -p ] [ -m templatenum ] [ -c reasonnum ] [ -e ] [ -r ] employee_username\n";
175 cust_bill-credit [ -d date ] [ -s state ] [ -p ] [ -m templatenum ] [ -c reasonnum ] [ -e ] employee_username
179 Command-line tool to search for and credit invoices.
181 -d: Search for invoices starting from this date
183 -s: Search for invoices for packages within this state
185 -p: Print an informational line for each invoice with invnum, custnum, original tax amount, calculate credit, and credit percentage of original.
187 -m: Send an email to the customer with this message template.
189 -c: Credit the invoice for one-half of the taxation amount, using this reason.