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