summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-prepaidd
blob: a68db39131b8d576df8c1b01b69324e7823eb6e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/perl -w

use strict;
use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs);
use FS::cust_pkg;

my $user = shift or die &usage;

#daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
daemonize1('freeside-prepaidd');

drop_root();

adminsuidsetup($user);

logfile( "/usr/local/etc/freeside/prepaidd-log.". $FS::UID::datasrc );

daemonize2();

#--

while (1) {

  foreach my $cust_pkg ( 
    qsearch( {
      'select'    => 'cust_pkg.*, part_pkg.plan',
      'table'     => 'cust_pkg',
      'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
      #'hashref'   => { 'plan' => 'prepaid' },#should check part_pkg::is_prepaid
      #'extra_sql' => "AND bill < ". time.
      'hashref'   => {},
      'extra_sql' => "WHERE plan = 'prepaid' AND bill < ". time.
                     " AND bill IS NOT NULL".
                     " AND ( susp   IS NULL OR susp   = 0)".
                     " AND ( cancel IS NULL OR cancel = 0)"
    } )
  ) {

    my $work_cust_pkg = $cust_pkg;

    my $cust_main = $cust_pkg->cust_main;
    if (    $cust_main->total_unapplied_payments > 0
         or $cust_main->total_credited > 0
       )
    {
      #this needs a flag to say only do the prepaid packages... 
      # and only try em if the renewal price matches.. but this will do for now
      my $b_error = $cust_main->bill;
      if ( $b_error ) {
        warn "Error billing customer #". $cust_main->custnum;
	next;
      }
      $b_error = $cust_main->apply_payments_and_credits;
      if ( $b_error ) {
        warn "Error applying payments&credits, customer #". $cust_main->custnum;
	next;
      }

      $work_cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $work_cust_pkg->pkgnum } );

      next
        if  $cust_main->balance <= 0 
        and $work_cust_pkg->bill >= time;
    }

    my $action = $work_cust_pkg->part_pkg->option('recur_action') || 'suspend';

    my $error = $work_cust_pkg->$action();

    warn "Error ${action}ing package ". $work_cust_pkg->pkgnum.
         " for custnum ". $work_cust_pkg->custnum.
         ": $error\n"
      if $error;
  }

  die "exiting" if sigterm() || sigint();
  sleep 5;

}

#--

sub usage { 
  die "Usage:\n\n  freeside-prepaidd user\n";
}

=head1 NAME

freeside-prepaidd - Real-time daemon for prepaid packages

=head1 SYNOPSIS

  freeside-prepaidd

=head1 DESCRIPTION

Runs continuously and suspends or cancels any prepaid customer packages which
have passed their renewal date (next bill date).

=head1 SEE ALSO

=cut

1;