c9c472c2df11ecc6bae577af66ccf00132d813bd
[freeside.git] / FS / FS / part_pkg / subscription.pm
1 package FS::part_pkg::subscription;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use Time::Local qw(timelocal);
6 #use FS::Record qw(qsearch qsearchs);
7 use FS::part_pkg::flat;
8
9 @ISA = qw(FS::part_pkg::flat);
10
11 %info = (
12   'name' => 'First partial month full charge, then flat-rate (selectable billing day)',
13   'fields' => {
14     'setup_fee' => { 'name' => 'Setup fee for this package',
15                      'default' => 0,
16                    },
17     'recur_fee' => { 'name' => 'Recurring fee for this package',
18                      'default' => 0,
19                            },
20     'cutoff_day' => { 'name' => 'billing day',
21                       'default' => 1,
22                     },
23     'seconds'       => { 'name' => 'Time limit for this package',
24                          'default' => '',
25                          'check' => sub { shift =~ /^\d*$/ },
26                        },
27     'upbytes'       => { 'name' => 'Upload limit for this package',
28                          'default' => '',
29                          'check' => sub { shift =~ /^\d*$/ },
30                          'format' => \&FS::UI::bytecount::display_bytecount,
31                          'parse' => \&FS::UI::bytecount::parse_bytecount,
32                        },
33     'downbytes'     => { 'name' => 'Download limit for this package',
34                          'default' => '',
35                          'check' => sub { shift =~ /^\d*$/ },
36                          'format' => \&FS::UI::bytecount::display_bytecount,
37                          'parse' => \&FS::UI::bytecount::parse_bytecount,
38                        },
39     'totalbytes'    => { 'name' => 'Transfer limit for this package',
40                          'default' => '',
41                          'check' => sub { shift =~ /^\d*$/ },
42                          'format' => \&FS::UI::bytecount::display_bytecount,
43                          'parse' => \&FS::UI::bytecount::parse_bytecount,
44                        },
45     'recharge_amount'       => { 'name' => 'Cost of recharge for this package',
46                          'default' => '',
47                          'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
48                        },
49     'recharge_seconds'      => { 'name' => 'Recharge time for this package',
50                          'default' => '',
51                          'check' => sub { shift =~ /^\d*$/ },
52                        },
53     'recharge_upbytes'      => { 'name' => 'Recharge upload for this package',
54                          'default' => '',
55                          'check' => sub { shift =~ /^\d*$/ },
56                          'format' => \&FS::UI::bytecount::display_bytecount,
57                          'parse' => \&FS::UI::bytecount::parse_bytecount,
58                        },
59     'recharge_downbytes'    => { 'name' => 'Recharge download for this package',                         'default' => '',
60                          'check' => sub { shift =~ /^\d*$/ },
61                          'format' => \&FS::UI::bytecount::display_bytecount,
62                          'parse' => \&FS::UI::bytecount::parse_bytecount,
63                        },
64     'recharge_totalbytes'   => { 'name' => 'Recharge transfer for this package',                         'default' => '',
65                          'check' => sub { shift =~ /^\d*$/ },
66                          'format' => \&FS::UI::bytecount::display_bytecount,
67                          'parse' => \&FS::UI::bytecount::parse_bytecount,
68                        },
69     'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
70                                     'over into current period',
71                           'type' => 'checkbox',
72                         },
73     'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
74                                     'package recharge',
75                           'type' => 'checkbox',
76                         },
77
78     #it would be better if this had to be turned on, its confusing
79     'externalid' => { 'name'   => 'Optional External ID',
80                       'default' => '',
81                     },
82   },
83   'fieldorder' => [ 'setup_fee', 'recur_fee', 'cutoff_day', 'seconds',
84                     'upbytes', 'downbytes', 'totalbytes',
85                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
86                     'recharge_downbytes', 'recharge_totalbytes',
87                     'usage_rollover', 'recharge_reset', 'externalid' ],
88   'freq' => 'm',
89   'weight' => 30,
90 );
91
92 sub calc_recur {
93   my($self, $cust_pkg, $sdate ) = @_;
94   my $cutoff_day = $self->option('cutoff_day', 1) || 1;
95   my $mnow = $$sdate;
96   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($mnow) )[0,1,2,3,4,5];
97
98   if ( $mday < $cutoff_day ) {
99      if ($mon==0) {$mon=11;$year--;}
100      else {$mon--;}
101   }
102
103   $$sdate = timelocal(0,0,0,$cutoff_day,$mon,$year);
104
105   $self->option('recur_fee');
106 }
107
108 1;