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 COUNT(*) FROM svc_phone LEFT JOIN cust_svc USING (svcnum)
44 WHERE cust_pkg.pkgnum = cust_svc.pkgnum
45 AND 0 < ( SELECT COUNT(*) 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
57 SELECT COUNT(*) FROM queue
58 WHERE queue.job = 'FS::cust_main::queued_bill'
59 AND queue.custnum = cust_pkg.custnum
63 # don't repeatedly queue failures
64 # AND status != 'failed'
69 foreach my $cust_pkg (
71 'select' => 'cust_pkg.*, part_pkg.plan',
72 'table' => 'cust_pkg',
73 'addl_from' => $addl_from,
75 'extra_sql' => $extra_sql,
81 #my $work_cust_pkg = $cust_pkg;
83 #my $cust_main = $cust_pkg->cust_main;
87 my $job = new FS::queue {
88 'job' => 'FS::cust_main::queued_bill',
90 'custnum' => $cust_pkg->custnum,
92 my $error = $job->insert(
93 'custnum' => $cust_pkg->custnum,
95 'invoice_time' => $time,
96 'actual_time' => $time,
97 'check_freq' => '1d', #well
102 #die "FATAL: error inserting billing job: $error\n";
103 warn "WARNING: error inserting billing job (will retry in 30 seconds):".
105 sleep 30; #i dunno, wait and see if the database comes back?
110 myexit() if sigterm() || sigint();
111 sleep 1 unless $found;
120 ' AND 0 < ( SELECT COUNT(*) FROM cust_pkg
121 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
122 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
127 grep $_->option('bill_every_call', 'hush'),
129 'table' => 'part_pkg',
130 'hashref' => { 'plan' => 'voip_cdr' },
131 'extra_sql' => $extra_sql,
140 die "Usage:\n\n freeside-cdrd user\n";
145 freeside-cdrd - Real-time daemon for CDRs
153 Runs continuously, searches for CDRs and bills customers who have VoIP
154 price plands with the B<bill_every_call> option set.