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