work around missing id, RT#83146
[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::Conf;
8 use FS::cust_pkg;
9
10 my $user = shift or die &usage;
11
12 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
13 daemonize1('freeside-prepaidd');
14
15 drop_root();
16
17 adminsuidsetup($user);
18
19 logfile( "%%%FREESIDE_LOG%%%/prepaidd-log.". $FS::UID::datasrc );
20
21 daemonize2();
22
23 #--
24
25 while (1) {
26
27   foreach my $cust_pkg ( 
28     qsearch( {
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.
34       'hashref'   => {},
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)"
39     } )
40   ) {
41
42     my $work_cust_pkg = $cust_pkg;
43
44     my $cust_main = $cust_pkg->cust_main;
45
46     #insurance: somehow winding up here without things properly applied...
47     my $a_error = $cust_main->apply_payments_and_credits;
48     if ( $a_error ) {
49       warn "Error applying payments&credits, customer #". $cust_main->custnum;
50       next;
51     }
52
53     if ( (    $cust_main->total_unapplied_payments > 0
54            || $cust_main->total_unapplied_credits > 0
55          )
56          && ! FS::Conf->new->exists('prepaid-never_renew')
57        )
58     {
59
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;
63       if ( $b_error ) {
64         warn "Error billing customer #". $cust_main->custnum;
65         next;
66       }
67       $b_error = $cust_main->apply_payments_and_credits;
68       if ( $b_error ) {
69         warn "Error applying payments&credits, customer #". $cust_main->custnum;
70         next;
71       }
72
73       $work_cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $work_cust_pkg->pkgnum } );
74
75       next
76         if  $cust_main->balance <= 0 
77         and $work_cust_pkg->bill >= time;
78     }
79
80     my $action = $work_cust_pkg->part_pkg->option('recur_action') || 'suspend';
81
82     my $error = $work_cust_pkg->$action();
83
84     warn "Error ${action}ing package ". $work_cust_pkg->pkgnum.
85          " for custnum ". $work_cust_pkg->custnum.
86          ": $error\n"
87       if $error;
88   }
89
90   die "exiting" if sigterm() || sigint();
91   sleep 60;
92
93 }
94
95 #--
96
97 sub usage { 
98   die "Usage:\n\n  freeside-prepaidd user\n";
99 }
100
101 =head1 NAME
102
103 freeside-prepaidd - Real-time daemon for prepaid packages
104
105 =head1 SYNOPSIS
106
107   freeside-prepaidd
108
109 =head1 DESCRIPTION
110
111 Runs continuously and suspends or cancels any prepaid customer packages which
112 have passed their renewal date (next bill date).
113
114 =head1 SEE ALSO
115
116 =cut
117
118 1;