add a bulk_simple price plan that behaves more intuitively for intial invoices, RT...
[freeside.git] / FS / FS / part_pkg / bulk.pm
1 package FS::part_pkg::bulk;
2 use base qw( FS::part_pkg::bulk_Common );
3
4 use strict;
5 use vars qw($DEBUG $me %info);
6 use Date::Format;
7 use List::Util qw( max );
8 use FS::Conf;
9
10 $DEBUG = 0;
11 $me = '[FS::part_pkg::bulk]';
12
13 %info = (
14   'name' => 'Bulk billing based on number of active services (during billing period)',
15   'inherit_fields' => [ 'bulk_Common', 'global_Mixin' ],
16   'fields' => {
17     'no_prorate'    => { 'name' => 'Don\'t prorate recurring fees on services '.
18                                    'active for a partial month',
19                          'type' => 'checkbox',
20                        },
21   },
22   'fieldorder' => [ 'no_prorate' ],
23   'weight' => 51,
24 );
25
26
27 sub _bulk_cust_svc {
28   my( $self, $cust_pkg, $sdate ) = @_;
29                        #   END      START
30   $cust_pkg->h_cust_svc( $$sdate, $cust_pkg->last_bill );
31 }
32
33 sub _bulk_setup {
34   my( $self, $cust_pkg, $h_cust_svc ) = @_;
35   ($h_cust_svc->date_inserted < $cust_pkg->last_bill)
36     ? $self->option('svc_setup_fee')
37     : 0;
38 }
39
40 sub _bulk_recur {
41   my( $self, $cust_pkg, $h_cust_svc, $sdate ) = @_;
42
43   return ($self->option('svc_recur_fee'), '')
44     if $self->option('no_prorate',1);
45
46   my $last_bill = $cust_pkg->last_bill;
47   my $svc_start = max( $h_cust_svc->date_inserted, $last_bill);
48   my $svc_end = $h_cust_svc->date_deleted;
49   $svc_end = ( !$svc_end || $svc_end > $$sdate ) ? $$sdate : $svc_end;
50
51   my $recur_charge = $self->option('svc_recur_fee') 
52                                    * ( $svc_end - $svc_start )
53                                    / ( $$sdate  - $last_bill );
54
55   return (0, '') unless $recur_charge;
56
57   my $svc_details .= ' ('.  time2str('%x', $svc_start).
58                   ' - '. time2str('%x', $svc_end  ). ')';
59
60   ( $recur_charge, $svc_details );
61
62 }
63
64 1;
65