1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package FS::cust_main::Credit_Limit;
use strict;
use vars qw( $conf $default_credit_limit $credit_limit_delay );
use FS::UID qw( dbh );
use FS::Record qw( qsearchs );
use FS::cust_main_credit_limit;
#ask FS::UID to run this stuff for us later
install_callback FS::UID sub {
$conf = new FS::Conf;
#yes, need it for stuff below (prolly should be cached)
$default_credit_limit = $conf->config('default_credit_limit') || 0;
};
$credit_limit_delay = 6 * 60 * 60; #6 hours? conf?
sub check_credit_limit {
my $self = shift;
my $credit_limit = $self->credit_limit || $default_credit_limit;
return '' unless $credit_limit > 0;
#see if we've already triggered this credit limit recently
return ''
if qsearchs({
'table' => 'cust_main_credit_limit',
'hashref' => {
'custnum' => $self->custnum,
'credit_limit' => { op=>'>=', value=> $credit_limit },
'_date' => { op=>'>=', value=> time - $credit_limit_delay, },
},
'order_by' => 'LIMIT 1',
});
#count up prerated CDRs
my @cust_svc = map $_->cust_svc_unsorted( 'svcdb'=>'svc_phone' ),
$self->all_pkgs;
my @svcnum = map $_->svcnum, @cust_svc;
#false laziness w/svc_phone->sum_cdrs / psearch_cdrs
my $sum = qsearchs( {
'select' => 'SUM(rated_price) AS rated_price',
'table' => 'cdr',
#'hashref' => { 'freesidestatus' => 'rated', },
'extra_sql' => " WHERE freesidestatus = 'rated' ".
' AND svcnum IN ('. join(',',@svcnum). ') ',
} );
return '' unless $sum->rated_price > $credit_limit;
#XXX trigger an alert
# (email send / ticket create / nagios alert export) ?
# maybe an over_credit_limit cust_main export or some such?
# record we did it so we don't do it continuously
my $cust_main_credit_limit = new FS::cust_main_credit_limit {
'custnum' => $self->custnum,
'_date' => time,
'credit_limit' => $credit_limit,
'amount' => sprintf('%.2f', $sum->rated_price ),
};
my $error = $cust_main_credit_limit->insert;
if ( $error ) {
#"should never happen", but better to survive e.g. database going
# away and coming back and resume doing our thing
warn $error;
sleep 30;
}
}
sub num_cust_main_credit_limit {
my $self = shift;
my $sql = 'SELECT COUNT(*) FROM cust_main_credit_limit WHERE custnum = ?';
my $sth = dbh->prepare($sql) or die dbh->errstr;
$sth->execute( $self->custnum) or die $sth->errstr;
$sth->fetchrow_arrayref->[0];
}
1;
|