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;
29 # find tax classes with double taxes
32 #my $sql = "select distinct taxclass from cust_main_county where taxname = '$taxname';";
33 #my $sth = dbh->prepare($sql) or die dbh->errstr;
34 #$sth->execute or die $sth->errstr;
35 #my @taxclasses = map $_->[0], @{ $sth->fetchall_arrayref() };
39 # my @cust_main_county = qsearch('cust_main_county', {
40 # 'taxclass' => $taxclass,
41 # 'taxname' => $taxname,
42 # 'tax' => { op=>'>', value=>'0' },
45 # $taxclass => ( scalar(@cust_main_county) > 1 );
49 my %taxclass = map { $_ => 1 } (
51 'ComputerSetup-Remote',
52 'Config File- Static IPs',
60 'InternetService_WEx',
61 'LondDistanceFixedRate',
62 'LondDistanceVariableRate',
64 'SARA-ONLY-GSTPayable',
68 'SoftwareLicenseToUse',
71 'WarrantyCoverageHardware',
72 'WarrantyCoverageSoftware',
76 'LongDistanceFixedRate_WEx',
77 'LongDistanceVariableRate_WEx',
84 #my $extra_sql = $opt_s ? " AND cust_main.state = '$opt_s' " : '';
86 my $start_date = $opt_d ? str2time($opt_d) : 0;
87 my $end_date = 1375471643;
89 my @cust_bill = qsearch({
90 'select' => 'cust_bill.*',
91 'table' => 'cust_bill',
92 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
93 'hashref' => { '_date' => { op=>'>', value=>$start_date } },
94 'extra_sql' => " AND _date < $end_date ",
95 # 'extra_sql' => $extra_sql,
98 my( @billpkgnums, @setuprecurs, @amounts );
100 foreach my $cust_bill ( @cust_bill ) {
102 my $tax = $cust_bill->tax;
104 my $cust_main = $cust_bill->cust_main;
106 next if $cust_bill->charged == 0
108 or $cust_main->tax eq 'Y';
116 foreach my $cust_bill_pkg ( $cust_bill->cust_bill_pkg ) {
117 my $cust_pkg = $cust_bill_pkg->cust_pkg or next;
119 my $loc = $cust_pkg->cust_location_or_main;
120 next if $opt_s && $loc->state ne $opt_s;
122 my $part_pkg = $cust_pkg->part_pkg;
123 next unless $taxclass{ $part_pkg->taxclass };
125 #my $amount = $cust_bill_pkg->setup + $cust_bill_pkg->recur;
126 #$credit += $rate * $amount / 100;
128 if ( $cust_bill_pkg->setup ) {
129 my $scredit = sprintf('%.2f', ($rate * $cust_bill_pkg->setup / 100) + 0.00000001);
131 push @setuprecurs, 'setup';
132 push @billpkgnums, $cust_bill_pkg->billpkgnum;
133 push @amounts, $scredit;
136 if ( $cust_bill_pkg->recur ) {
137 my $rcredit = sprintf('%.2f', ($rate * $cust_bill_pkg->recur / 100) + 0.00000001);
139 push @setuprecurs, 'recur';
140 push @billpkgnums, $cust_bill_pkg->billpkgnum;
141 push @amounts, $rcredit;
146 $credit = sprintf('%.2f', $credit + 0.00000001);
148 next if $credit == 0;
150 #$credit = sprintf('%.2f', $credit + 0.00000001);
152 warn "invoice ". $cust_bill->invnum. ": credit of $credit is more than orignally taxed ($tax)\n" if $credit > $tax;
154 warn "invoice ". $cust_bill->invnum. ": credit of $credit is more than 50% of originally taxed ($tax)\n" if $credit-0.01 > $tax/2;
156 #my $cr_percent = sprintf('%.1f', 100 * $credit / $tax);
159 #print $cust_bill->invnum. ','. $cust_bill->custnum. ",$tax,$credit,$cr_percent%\n";
160 print $cust_bill->invnum. ','. $cust_bill->custnum. ',"'.
161 $cust_bill->cust_main->name. '",'. "$tax,$credit\n";
164 if ( $opt_m && ! $opt_r ) {
166 my $msg_template = qsearchs('msg_template', { 'msgnum' => $opt_m } )
167 or die "Template $opt_m not found";
168 my $error = $msg_template->send(
169 'cust_main' => $cust_main,
170 'object' => $cust_main,
173 warn "error sending email for invnum ". $cust_bill->invnum. ','.
174 " custnum ". $cust_bill->custnum. ": $error\n";
179 my $cust_credit = new FS::cust_credit {
180 'custnum' => $cust_main->custnum,
182 'reasonnum' => $opt_c,
184 my $error = $cust_credit->insert;
186 warn "error inserting credit: $error\n";
188 my $cust_credit_bill = new FS::cust_credit_bill {
189 'crednum' => $cust_credit->crednum,
190 'invnum' => $cust_bill->invnum,
193 my $aerror = $cust_credit_bill->insert;
195 warn "error applying credit to invnum ". $cust_bill->invnum. ": $aerror\n";
199 if ( $opt_e && ! $opt_r ) {
200 eval { $cust_bill->email };
202 warn "error sending invnum ". $cust_bill->invnum. ','.
203 " custnum ". $cust_bill->custnum. ": $@\n";
210 $dbh->rollback or die $dbh->errstr; #if $oldAutoCommit;
212 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
216 die "usage: cust_bill-credit [ -d date ] [ -s state ] [ -p ] [ -m templatenum ] [ -c reasonnum ] [ -e ] [ -r ] employee_username\n";
225 cust_bill-credit [ -d date ] [ -s state ] [ -p ] [ -m templatenum ] [ -c reasonnum ] [ -e ] employee_username
229 Command-line tool to search for and credit invoices.
231 -d: Search for invoices starting from this date
233 -s: Search for invoices for packages within this state
235 -p: Print an informational line for each invoice with invnum, custnum, original tax amount, calculate credit, and credit percentage of original.
237 -m: Send an email to the customer with this message template.
239 -c: Credit the invoice for one-half of the taxation amount, using this reason.