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);
11 my $user = shift or die &usage;
13 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
14 daemonize1('freeside-cdrd');
18 adminsuidsetup($user);
20 logfile( "%%%FREESIDE_LOG%%%/cdrd-log.". $FS::UID::datasrc );
24 die "not running; no voip_cdr package defs w/ bill_every_call and customer pkgs"
30 'LEFT JOIN part_pkg USING ( pkgpart ) '.
31 "LEFT JOIN part_pkg_option
32 ON ( cust_pkg.pkgpart = part_pkg_option.pkgpart
33 AND part_pkg_option.optionname = 'bill_every_call' )";
35 #XXX should pay attention to disable_src for efficiency
38 "WHERE plan = 'voip_cdr' ".
39 " AND optionvalue = '1' ".
40 " AND ( susp IS NULL OR susp = 0)".
41 " AND ( cancel IS NULL OR cancel = 0)".
43 SELECT 1 FROM svc_phone LEFT JOIN cust_svc USING (svcnum)
44 WHERE cust_pkg.pkgnum = cust_svc.pkgnum
45 AND EXISTS ( SELECT 1 FROM cdr
46 WHERE ( freesidestatus IS NULL OR freesidestatus = '' )
47 AND ( charged_party = svc_phone.phonenum
48 OR charged_party = svc_phone.countrycode
50 OR src = svc_phone.phonenum
51 OR src = svc_phone.countrycode
60 WHERE queue.job = 'FS::cust_main::queued_bill'
61 AND queue.custnum = cust_pkg.custnum
66 # don't repeatedly queue failures
67 # AND status != 'failed'
72 foreach my $cust_pkg (
74 'select' => 'cust_pkg.*, part_pkg.plan',
75 'table' => 'cust_pkg',
76 'addl_from' => $addl_from,
78 'extra_sql' => $extra_sql,
84 #my $work_cust_pkg = $cust_pkg;
86 #my $cust_main = $cust_pkg->cust_main;
90 my $job = new FS::queue {
91 'job' => 'FS::cust_main::queued_bill',
93 'custnum' => $cust_pkg->custnum,
95 my $error = $job->insert(
96 'custnum' => $cust_pkg->custnum,
98 'invoice_time' => $time,
99 'actual_time' => $time,
100 'check_freq' => '1d', #well
105 #die "FATAL: error inserting billing job: $error\n";
106 warn "WARNING: error inserting billing job (will retry in 30 seconds):".
108 sleep 30; #i dunno, wait and see if the database comes back?
113 myexit() if sigterm() || sigint();
114 sleep 5 unless $found;
123 ' AND EXISTS ( SELECT 1 FROM cust_pkg
124 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
125 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
130 grep $_->option('bill_every_call', 'hush'),
132 'table' => 'part_pkg',
133 'hashref' => { 'plan' => 'voip_cdr' },
134 'extra_sql' => $extra_sql,
143 die "Usage:\n\n freeside-cdrd user\n";
148 freeside-cdrd - Real-time daemon for CDRs
156 Runs continuously, searches for CDRs and bills customers who have VoIP
157 price plands with the B<bill_every_call> option set.