default to a session cookie instead of setting an explicit timeout, weird timezone...
[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   'inherit_fields' => [ 'usage_Mixin', 'global_Mixin' ],
15   'fields' => {
16     'cutoff_day' => { 'name' => 'Billing day',
17                       'default' => 1,
18                     },
19     'seconds'       => { 'name' => 'Time limit for this package',
20                          'default' => '',
21                          'check' => sub { shift =~ /^\d*$/ },
22                        },
23     'upbytes'       => { 'name' => 'Upload limit for this package',
24                          'default' => '',
25                          'check' => sub { shift =~ /^\d*$/ },
26                          'format' => \&FS::UI::bytecount::display_bytecount,
27                          'parse' => \&FS::UI::bytecount::parse_bytecount,
28                        },
29     'downbytes'     => { 'name' => 'Download limit for this package',
30                          'default' => '',
31                          'check' => sub { shift =~ /^\d*$/ },
32                          'format' => \&FS::UI::bytecount::display_bytecount,
33                          'parse' => \&FS::UI::bytecount::parse_bytecount,
34                        },
35     'totalbytes'    => { 'name' => 'Transfer limit for this package',
36                          'default' => '',
37                          'check' => sub { shift =~ /^\d*$/ },
38                          'format' => \&FS::UI::bytecount::display_bytecount,
39                          'parse' => \&FS::UI::bytecount::parse_bytecount,
40                        },
41     'recharge_amount'       => { 'name' => 'Cost of recharge for this package',
42                          'default' => '',
43                          'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
44                        },
45     'recharge_seconds'      => { 'name' => 'Recharge time for this package',
46                          'default' => '',
47                          'check' => sub { shift =~ /^\d*$/ },
48                        },
49     'recharge_upbytes'      => { 'name' => 'Recharge upload for this package',
50                          'default' => '',
51                          'check' => sub { shift =~ /^\d*$/ },
52                          'format' => \&FS::UI::bytecount::display_bytecount,
53                          'parse' => \&FS::UI::bytecount::parse_bytecount,
54                        },
55     'recharge_downbytes'    => { 'name' => 'Recharge download for this package',                         'default' => '',
56                          'check' => sub { shift =~ /^\d*$/ },
57                          'format' => \&FS::UI::bytecount::display_bytecount,
58                          'parse' => \&FS::UI::bytecount::parse_bytecount,
59                        },
60     'recharge_totalbytes'   => { 'name' => 'Recharge transfer 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     'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
66                                     'over into current period',
67                           'type' => 'checkbox',
68                         },
69     'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
70                                     'package recharge',
71                           'type' => 'checkbox',
72                         },
73
74     #it would be better if this had to be turned on, its confusing
75     'externalid' => { 'name'   => 'Optional External ID',
76                       'default' => '',
77                     },
78   },
79   'fieldorder' => [ 'cutoff_day', 'seconds',
80                     'upbytes', 'downbytes', 'totalbytes',
81                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
82                     'recharge_downbytes', 'recharge_totalbytes',
83                     'usage_rollover', 'recharge_reset', 'externalid' ],
84   'freq' => 'm',
85   'weight' => 30,
86 );
87
88 sub calc_recur {
89   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
90   my $cutoff_day = $self->option('cutoff_day', 1) || 1;
91   my $cust_main = $cust_pkg->cust_main;
92   if ( $cust_main->force_prorate_day  and $cust_main->prorate_day ) {
93      $cutoff_day = $cust_main->prorate_day;
94   }
95
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, $sdate);
107
108   my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
109
110   sprintf('%.2f', ($cust_pkg->quantity || 1) * ($br - $discount) );
111 }
112
113 1;