have freeside-cdrd disable itself if there's no appropriate package definition, RT...
[freeside.git] / FS / bin / freeside-cdrd
1 #!/usr/bin/perl -w
2
3 use strict;
4 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw(qsearch ); #qsearchs);
7 #use FS::cdr;
8 use FS::cust_pkg;
9
10 my $user = shift or die &usage;
11
12 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
13 daemonize1('freeside-cdrd');
14
15 drop_root();
16
17 adminsuidsetup($user);
18
19 logfile( "%%%FREESIDE_LOG%%%/cdrd-log.". $FS::UID::datasrc );
20
21 daemonize2();
22
23 die "not running; no voip_cdr package defs w/ bill_every_call and customer pkgs"
24   unless _shouldrun();
25
26 #--
27
28 my $addl_from =
29   'LEFT JOIN part_pkg USING ( pkgpart ) '.
30   "LEFT JOIN part_pkg_option
31      ON ( cust_pkg.pkgpart = part_pkg_option.pkgpart
32           AND part_pkg_option.optionname = 'bill_every_call' )";
33
34 #XXX should pay attention to disable_src for efficiency
35
36 my $extra_sql =
37   "WHERE plan = 'voip_cdr' ".
38   " AND optionvalue = '1' ".
39   " AND ( susp   IS NULL OR susp   = 0)".
40   " AND ( cancel IS NULL OR cancel = 0)".
41   " AND 0 < ( 
42       SELECT COUNT(*) FROM svc_phone LEFT JOIN cust_svc USING (svcnum)
43         WHERE cust_pkg.pkgnum = cust_svc.pkgnum
44           AND 0 < ( SELECT COUNT(*) FROM cdr
45                       WHERE ( freesidestatus IS NULL OR freesidestatus = '' )
46                         AND (    charged_party = svc_phone.phonenum
47                               OR charged_party = svc_phone.countrycode
48                                                  || svc_phone.phonenum
49                               OR src = svc_phone.phonenum
50                               OR src = svc_phone.countrycode
51                                        || svc_phone.phonenum
52                             )
53                   )
54     ) 
55   ";
56
57 while (1) {
58
59   my $found = 0;
60   foreach my $cust_pkg ( 
61     qsearch( {
62       'select'    => 'cust_pkg.*, part_pkg.plan',
63       'table'     => 'cust_pkg',
64       'addl_from' => $addl_from,
65       'hashref'   => {},
66       'extra_sql' => $extra_sql,
67     } )
68   ) {
69
70     $found = 1;
71
72     #my $work_cust_pkg = $cust_pkg;
73
74     my $cust_main = $cust_pkg->cust_main;
75
76     my $time = time;
77     $cust_main->bill_and_collect( 
78       'time'         => $time,
79       'invoice_time' => $time,
80       'actual_time'  => $time,
81       'check_freq'   => '1d', #well
82       #'debug'        => 1,
83     );
84
85   }
86
87   myexit() if sigterm() || sigint();
88   sleep 5; # unless $found;
89
90 }
91
92 #--
93
94 sub _shouldrun {
95
96   my $extra_sql =
97     ' AND 0 < ( SELECT COUNT(*) FROM cust_pkg
98                   WHERE cust_pkg.pkgpart = part_pkg.pkgpart
99                     AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
100               )
101     ';
102
103   my @part_pkg =
104     grep $_->option('bill_every_call', 'hush'),
105     qsearch({
106       'table'     => 'part_pkg',
107       'hashref'   => { 'plan' => 'voip_cdr' },
108       'extra_sql' => $extra_sql,
109     })
110   ;
111
112   scalar(@part_pkg);
113
114 }
115
116 sub usage { 
117   die "Usage:\n\n  freeside-prepaidd user\n";
118 }
119
120 =head1 NAME
121
122 freeside-prepaidd - Real-time daemon for prepaid packages
123
124 =head1 SYNOPSIS
125
126   freeside-prepaidd
127
128 =head1 DESCRIPTION
129
130 Runs continuously and suspends or cancels any prepaid customer packages which
131 have passed their renewal date (next bill date).
132
133 =head1 SEE ALSO
134
135 =cut
136
137 1;