1 package FS::Cron::set_lata_have_usage;
5 use vars qw( @ISA @EXPORT_OK $me $DEBUG );
7 use FS::UID qw(adminsuidsetup);
8 use FS::Record qw(qsearch qsearchs dbh);
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw ( set_lata_have_usage );
17 $me = '[FS::Cron::set_lata_have_usage]';
19 sub set_lata_have_usage {
23 $debug = 1 if $opt{'v'};
24 $debug = $opt{'l'} if $opt{'l'};
26 local $DEBUG = $debug if $debug;
28 warn "$me set_lata_have_usage called time=".time."\n" if $DEBUG;
30 local $SIG{HUP} = 'IGNORE';
31 local $SIG{INT} = 'IGNORE';
32 local $SIG{QUIT} = 'IGNORE';
33 local $SIG{TERM} = 'IGNORE';
34 local $SIG{TSTP} = 'IGNORE';
35 local $SIG{PIPE} = 'IGNORE';
37 my $oldAutoCommit = $FS::UID::AutoCommit;
38 local $FS::UID::AutoCommit = 0;
41 my %latas = map { $_->latanum => $_ } qsearch('lata', {});
43 foreach my $lata ( keys %latas ) {
44 next unless $latas{$lata}->have_usage && $latas{$lata}->have_usage > 0;
45 $latas{$lata}->have_usage(0);
46 my $error = $latas{$lata}->replace;
48 $dbh->rollback if $oldAutoCommit;
49 die "error replacing LATA $lata: $error";
52 warn "$me cleared have_usage for all LATAs time=".time."\n" if $DEBUG;
54 my @dids = qsearch({ 'table' => 'svc_phone',
61 'addl_from' => 'join phone_avail using (svcnum)',
63 warn "$me DID query finished time=".time."\n" if $DEBUG;
66 foreach my $did ( @dids ) {
67 warn "$me count=$count time=".time."\n" if $DEBUG && ($count % 1000 == 0);
68 my @cdrs = $did->get_cdrs;
69 my $lata = $latas{$did->latanum};
71 if ( scalar(@cdrs) ) {
72 if ( !$lata->have_usage ) {
76 $lata->have_usage($lata->have_usage+1);
81 warn "$me Set have_usage finished time=".time."\n" if $DEBUG;
83 foreach my $lata ( keys %latas ) {
84 if ( $latas{$lata}->modified ) {
85 print "$lata ".$latas{$lata}->have_usage."\n";
86 my $error = $latas{$lata}->replace;
88 $dbh->rollback if $oldAutoCommit;
89 die "error replacing LATA $lata: $error";
94 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
95 warn "$me done time=".time."\n" if $DEBUG;