This commit was generated by cvs2svn to compensate for changes in r11022,
[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     {
56
57       #this needs a flag to say only do the prepaid packages... 
58       # and only try em if the renewal price matches.. but this will do for now
59       my $b_error = $cust_main->bill;
60       if ( $b_error ) {
61         warn "Error billing customer #". $cust_main->custnum;
62         next;
63       }
64       $b_error = $cust_main->apply_payments_and_credits;
65       if ( $b_error ) {
66         warn "Error applying payments&credits, customer #". $cust_main->custnum;
67         next;
68       }
69
70       $work_cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $work_cust_pkg->pkgnum } );
71
72       next
73         if  $cust_main->balance <= 0 
74         and $work_cust_pkg->bill >= time;
75     }
76
77     my $action = $work_cust_pkg->part_pkg->option('recur_action') || 'suspend';
78
79     my $error = $work_cust_pkg->$action();
80
81     warn "Error ${action}ing package ". $work_cust_pkg->pkgnum.
82          " for custnum ". $work_cust_pkg->custnum.
83          ": $error\n"
84       if $error;
85   }
86
87   die "exiting" if sigterm() || sigint();
88   sleep 5;
89
90 }
91
92 #--
93
94 sub usage { 
95   die "Usage:\n\n  freeside-prepaidd user\n";
96 }
97
98 =head1 NAME
99
100 freeside-prepaidd - Real-time daemon for prepaid packages
101
102 =head1 SYNOPSIS
103
104   freeside-prepaidd
105
106 =head1 DESCRIPTION
107
108 Runs continuously and suspends or cancels any prepaid customer packages which
109 have passed their renewal date (next bill date).
110
111 =head1 SEE ALSO
112
113 =cut
114
115 1;