add price plan option for prepaid packages to cancel instead of suspend
[freeside.git] / FS / bin / freeside-prepaidd
1 #!/usr/bin/perl -w
2
3 use strict;
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);
7 use FS::cust_pkg;
8
9 my $user = shift or die &usage;
10
11 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
12 daemonize1('freeside-prepaidd');
13
14 drop_root();
15
16 adminsuidsetup($user);
17
18 logfile( "/usr/local/etc/freeside/prepaidd-log.". $FS::UID::datasrc );
19
20 daemonize2();
21
22 #--
23
24 while (1) {
25
26   foreach my $cust_pkg ( 
27     qsearch( {
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.
33       'hashref'   => {},
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)"
38     } )
39   ) {
40
41     my $action = $cust_pkg->part_pkg->option('recur_action') || 'suspend';
42
43     my $error = $cust_pkg->$action();
44
45     warn "Error ${action}ing package ". $cust_pkg->pkgnum.
46          " for custnum ". $cust_pkg->custnum.
47          ": $error\n"
48       if $error;
49   }
50
51   die "exiting" if sigterm() || sigint();
52   sleep 5;
53
54 }
55
56 #--
57
58 sub usage { 
59   die "Usage:\n\n  freeside-prepaidd user\n";
60 }
61
62 =head1 NAME
63
64 freeside-prepaidd - Real-time daemon for prepaid packages
65
66 =head1 SYNOPSIS
67
68   freeside-prepaidd
69
70 =head1 DESCRIPTION
71
72 Runs continuously and suspends or cancels any prepaid customer packages which
73 have passed their renewal date (next bill date).
74
75 =head1 SEE ALSO
76
77 =cut
78
79 1;