add prepaid-never_renew flag, RT#26274
[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( "%%%FREESIDE_LOG%%%/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 $work_cust_pkg = $cust_pkg;
42
43     my $cust_main = $cust_pkg->cust_main;
44
45     #insurance: somehow winding up here without things properly applied...
46     my $a_error = $cust_main->apply_payments_and_credits;
47     if ( $a_error ) {
48       warn "Error applying payments&credits, customer #". $cust_main->custnum;
49       next;
50     }
51
52     if ( (    $cust_main->total_unapplied_payments > 0
53            || $cust_main->total_unapplied_credits > 0
54          )
55          && ! FS::Conf->new->exists('prepaid-never_renew')
56        )
57     {
58
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;
62       if ( $b_error ) {
63         warn "Error billing customer #". $cust_main->custnum;
64         next;
65       }
66       $b_error = $cust_main->apply_payments_and_credits;
67       if ( $b_error ) {
68         warn "Error applying payments&credits, customer #". $cust_main->custnum;
69         next;
70       }
71
72       $work_cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $work_cust_pkg->pkgnum } );
73
74       next
75         if  $cust_main->balance <= 0 
76         and $work_cust_pkg->bill >= time;
77     }
78
79     my $action = $work_cust_pkg->part_pkg->option('recur_action') || 'suspend';
80
81     my $error = $work_cust_pkg->$action();
82
83     warn "Error ${action}ing package ". $work_cust_pkg->pkgnum.
84          " for custnum ". $work_cust_pkg->custnum.
85          ": $error\n"
86       if $error;
87   }
88
89   die "exiting" if sigterm() || sigint();
90   sleep 60;
91
92 }
93
94 #--
95
96 sub usage { 
97   die "Usage:\n\n  freeside-prepaidd user\n";
98 }
99
100 =head1 NAME
101
102 freeside-prepaidd - Real-time daemon for prepaid packages
103
104 =head1 SYNOPSIS
105
106   freeside-prepaidd
107
108 =head1 DESCRIPTION
109
110 Runs continuously and suspends or cancels any prepaid customer packages which
111 have passed their renewal date (next bill date).
112
113 =head1 SEE ALSO
114
115 =cut
116
117 1;