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);
10 my $user = shift or die &usage;
12 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
13 daemonize1('freeside-prepaidd');
17 adminsuidsetup($user);
19 logfile( "%%%FREESIDE_LOG%%%/prepaidd-log.". $FS::UID::datasrc );
27 foreach my $cust_pkg (
29 'select' => 'cust_pkg.*, part_pkg.plan',
30 'table' => 'cust_pkg',
31 'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
32 #'hashref' => { 'plan' => 'prepaid' },#should check part_pkg::is_prepaid
33 #'extra_sql' => "AND bill < ". time.
35 'extra_sql' => "WHERE plan = 'prepaid' AND bill < ". time.
36 " AND bill IS NOT NULL".
37 " AND ( susp IS NULL OR susp = 0)".
38 " AND ( cancel IS NULL OR cancel = 0)"
42 my $work_cust_pkg = $cust_pkg;
44 my $cust_main = $cust_pkg->cust_main;
46 #insurance: somehow winding up here without things properly applied...
47 my $a_error = $cust_main->apply_payments_and_credits;
49 warn "Error applying payments&credits, customer #". $cust_main->custnum;
53 if ( ( $cust_main->total_unapplied_payments > 0
54 || $cust_main->total_unapplied_credits > 0
56 && ! FS::Conf->new->exists('prepaid-never_renew')
60 #this needs a flag to say only do the prepaid packages...
61 # and only try em if the renewal price matches.. but this will do for now
62 my $b_error = $cust_main->bill;
64 warn "Error billing customer #". $cust_main->custnum;
67 $b_error = $cust_main->apply_payments_and_credits;
69 warn "Error applying payments&credits, customer #". $cust_main->custnum;
73 $work_cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $work_cust_pkg->pkgnum } );
76 if $cust_main->balance <= 0
77 and $work_cust_pkg->bill >= time;
80 my $action = $work_cust_pkg->part_pkg->option('recur_action') || 'suspend';
82 my $error = $work_cust_pkg->$action();
84 warn "Error ${action}ing package ". $work_cust_pkg->pkgnum.
85 " for custnum ". $work_cust_pkg->custnum.
90 die "exiting" if sigterm() || sigint();
98 die "Usage:\n\n freeside-prepaidd user\n";
103 freeside-prepaidd - Real-time daemon for prepaid packages
111 Runs continuously and suspends or cancels any prepaid customer packages which
112 have passed their renewal date (next bill date).