enable CardFortress in test database, #71513
[freeside.git] / FS / FS / Cron / set_lata_have_usage.pm
1 package FS::Cron::set_lata_have_usage;
2
3 use strict;
4 use warnings;
5 use vars qw( @ISA @EXPORT_OK $me $DEBUG );
6 use Exporter;
7 use FS::UID qw(adminsuidsetup);
8 use FS::Record qw(qsearch qsearchs dbh);
9 use FS::lata;
10 use FS::phone_avail;
11 use FS::svc_phone;
12 use Data::Dumper;
13
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw ( set_lata_have_usage );
16 $DEBUG = 0;
17 $me = '[FS::Cron::set_lata_have_usage]';
18
19 sub set_lata_have_usage {
20     my %opt = @_;
21     
22     my $debug = 0;
23     $debug = 1 if $opt{'v'};
24     $debug = $opt{'l'} if $opt{'l'};
25   
26     local $DEBUG = $debug if $debug;
27   
28     warn "$me set_lata_have_usage called time=".time."\n" if $DEBUG;
29
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';
36
37     my $oldAutoCommit = $FS::UID::AutoCommit;
38     local $FS::UID::AutoCommit = 0;
39     my $dbh = dbh;
40
41     my %latas = map { $_->latanum => $_ } qsearch('lata', {});
42
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;
47             if ( $error ) {
48                 $dbh->rollback if $oldAutoCommit;
49                 die "error replacing LATA $lata: $error";
50             }
51     }
52     warn "$me cleared have_usage for all LATAs time=".time."\n" if $DEBUG;
53
54     my @dids = qsearch({     'table'     => 'svc_phone',
55                                     'hashref'   => 
56                                         { 'latanum' =>
57                                             { 'op'      => '>',
58                                               'value'   => '0',
59                                             },
60                                         },
61                                     'addl_from' => 'join phone_avail using (svcnum)',
62                                       });
63     warn "$me DID query finished time=".time."\n" if $DEBUG;
64
65     my $count = 0;
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};
70         $count++;
71         if ( scalar(@cdrs) ) {
72             if ( !$lata->have_usage ) {
73                 $lata->have_usage(1);
74             }
75             else {
76                 $lata->have_usage($lata->have_usage+1);
77             }
78         }
79     }
80
81     warn "$me Set have_usage finished time=".time."\n" if $DEBUG;
82
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;
87             if ( $error ) {
88                 $dbh->rollback if $oldAutoCommit;
89                 die "error replacing LATA $lata: $error";
90             }
91         }
92     }
93
94     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
95     warn "$me done time=".time."\n" if $DEBUG;
96 }
97
98 1;