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
55 && ! FS::Conf->new->exists('prepaid-never_renew')
59 #this needs a flag to say only do the prepaid packages...
60 # and only try em if the renewal price matches.. but this will do for now
61 my $b_error = $cust_main->bill;
63 warn "Error billing customer #". $cust_main->custnum;
66 $b_error = $cust_main->apply_payments_and_credits;
68 warn "Error applying payments&credits, customer #". $cust_main->custnum;
72 $work_cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $work_cust_pkg->pkgnum } );
75 if $cust_main->balance <= 0
76 and $work_cust_pkg->bill >= time;
79 my $action = $work_cust_pkg->part_pkg->option('recur_action') || 'suspend';
81 my $error = $work_cust_pkg->$action();
83 warn "Error ${action}ing package ". $work_cust_pkg->pkgnum.
84 " for custnum ". $work_cust_pkg->custnum.
89 die "exiting" if sigterm() || sigint();
97 die "Usage:\n\n freeside-prepaidd user\n";
102 freeside-prepaidd - Real-time daemon for prepaid packages
110 Runs continuously and suspends or cancels any prepaid customer packages which
111 have passed their renewal date (next bill date).