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);
9 my $user = shift or die &usage;
11 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
12 daemonize1('freeside-prepaidd');
16 adminsuidsetup($user);
18 logfile( "%%%FREESIDE_LOG%%%/prepaidd-log.". $FS::UID::datasrc );
26 foreach my $cust_pkg (
28 'select' => 'cust_pkg.*, part_pkg.plan',
29 'table' => 'cust_pkg',
30 'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
31 #'hashref' => { 'plan' => 'prepaid' },#should check part_pkg::is_prepaid
32 #'extra_sql' => "AND bill < ". time.
34 'extra_sql' => "WHERE plan = 'prepaid' AND bill < ". time.
35 " AND bill IS NOT NULL".
36 " AND ( susp IS NULL OR susp = 0)".
37 " AND ( cancel IS NULL OR cancel = 0)"
41 my $work_cust_pkg = $cust_pkg;
43 my $cust_main = $cust_pkg->cust_main;
45 #insurance: somehow winding up here without things properly applied...
46 my $a_error = $cust_main->apply_payments_and_credits;
48 warn "Error applying payments&credits, customer #". $cust_main->custnum;
52 if ( $cust_main->total_unapplied_payments > 0
53 || $cust_main->total_unapplied_credits > 0
57 #this needs a flag to say only do the prepaid packages...
58 # and only try em if the renewal price matches.. but this will do for now
59 my $b_error = $cust_main->bill;
61 warn "Error billing customer #". $cust_main->custnum;
64 $b_error = $cust_main->apply_payments_and_credits;
66 warn "Error applying payments&credits, customer #". $cust_main->custnum;
70 $work_cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $work_cust_pkg->pkgnum } );
73 if $cust_main->balance <= 0
74 and $work_cust_pkg->bill >= time;
77 my $action = $work_cust_pkg->part_pkg->option('recur_action') || 'suspend';
79 my $error = $work_cust_pkg->$action();
81 warn "Error ${action}ing package ". $work_cust_pkg->pkgnum.
82 " for custnum ". $work_cust_pkg->custnum.
87 die "exiting" if sigterm() || sigint();
95 die "Usage:\n\n freeside-prepaidd user\n";
100 freeside-prepaidd - Real-time daemon for prepaid packages
108 Runs continuously and suspends or cancels any prepaid customer packages which
109 have passed their renewal date (next bill date).