prepaid cdr pickup & bill daemon, RT#4184
[freeside.git] / FS / bin / freeside-cdrd
1 #!/usr/bin/perl -w
2
3 use strict;
4 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
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 #--
24
25 my $addl_from =
26   'LEFT JOIN part_pkg USING ( pkgpart ) '.
27   "LEFT JOIN part_pkg_option
28      ON ( cust_pkg.pkgpart = part_pkg_option.pkgpart
29           AND part_pkg_option.optionname = 'bill_every_call' )";
30
31 #XXX should pay attention to disable_src for efficiency
32
33 my $extra_sql =
34   "WHERE plan = 'voip_cdr' ".
35   " AND optionvalue = '1' ".
36   " AND ( susp   IS NULL OR susp   = 0)".
37   " AND ( cancel IS NULL OR cancel = 0)".
38   " AND 0 < ( 
39       SELECT COUNT(*) FROM svc_phone LEFT JOIN cust_svc USING (svcnum)
40         WHERE cust_pkg.pkgnum = cust_svc.pkgnum
41           AND 0 < ( SELECT COUNT(*) FROM cdr
42                       WHERE ( freesidestatus IS NULL OR freesidestatus = '' )
43                         AND (    charged_party = svc_phone.phonenum
44                               OR charged_party = svc_phone.countrycode
45                                                  || svc_phone.phonenum
46                               OR src = svc_phone.phonenum
47                               OR src = svc_phone.countrycode
48                                        || svc_phone.phonenum
49                             )
50                   )
51     ) 
52   ";
53
54 while (1) {
55
56   my $found = 0;
57   foreach my $cust_pkg ( 
58     qsearch( {
59       'select'    => 'cust_pkg.*, part_pkg.plan',
60       'table'     => 'cust_pkg',
61       'addl_from' => $addl_from,
62       'hashref'   => {},
63       'extra_sql' => $extra_sql,
64     } )
65   ) {
66
67     $found = 1;
68
69     my $work_cust_pkg = $cust_pkg;
70
71     my $cust_main = $cust_pkg->cust_main;
72
73     my $time = time;
74     $cust_main->bill_and_collect( 
75       'time'         => $time,
76       'invoice_time' => $time,
77       'actual_time'  => $time,
78       'check_freq'   => '1d', #well
79       #'debug'        => 1,
80     );
81
82   }
83
84   die "exiting" if sigterm() || sigint();
85   sleep 5; # unless $found;
86
87 }
88
89 #--
90
91 sub usage { 
92   die "Usage:\n\n  freeside-prepaidd user\n";
93 }
94
95 =head1 NAME
96
97 freeside-prepaidd - Real-time daemon for prepaid packages
98
99 =head1 SYNOPSIS
100
101   freeside-prepaidd
102
103 =head1 DESCRIPTION
104
105 Runs continuously and suspends or cancels any prepaid customer packages which
106 have passed their renewal date (next bill date).
107
108 =head1 SEE ALSO
109
110 =cut
111
112 1;