1 package FS::part_pkg::bulk;
4 use vars qw(@ISA $DEBUG $me %info);
6 use FS::part_pkg::flat;
9 @ISA = qw(FS::part_pkg::flat);
12 $me = '[FS::part_pkg::bulk]';
15 'name' => 'Bulk billing based on number of active services',
16 'inherit_fields' => [ 'global_Mixin' ],
18 'svc_setup_fee' => { 'name' => 'Setup fee for each new service',
21 'svc_recur_fee' => { 'name' => 'Recurring fee for each service',
24 'summarize_svcs'=> { 'name' => 'Show a count of services on the invoice, '.
25 'instead of a detailed list',
28 'no_prorate' => { 'name' => 'Don\'t prorate recurring fees on services '.
29 'active for a partial month',
33 'fieldorder' => [ 'svc_setup_fee', 'svc_recur_fee',
34 'summarize_svcs', 'no_prorate' ],
40 my $str = $self->SUPER::price_info;
41 my $svc_setup_fee = $self->option('svc_setup_fee');
42 my $svc_recur_fee = $self->option('svc_recur_fee');
43 my $conf = new FS::Conf;
44 my $money_char = $conf->config('money_char') || '$';
45 $str .= " , bulk" if $str;
46 $str .= ": $money_char" . $svc_setup_fee . " one-time per service"
48 $str .= ", " if ($svc_setup_fee && $svc_recur_fee);
49 $str .= $money_char . $svc_recur_fee . " recurring per service"
54 #some false laziness-ish w/agent.pm... not a lot
56 my($self, $cust_pkg, $sdate, $details ) = @_;
58 my $conf = new FS::Conf;
59 my $money_char = $conf->config('money_char') || '$';
61 my $svc_setup_fee = $self->option('svc_setup_fee');
63 my $last_bill = $cust_pkg->last_bill;
65 return sprintf("%.2f", $self->base_recur($cust_pkg, $sdate) )
66 unless $$sdate > $last_bill;
68 my $total_svc_charge = 0;
71 my %part_svc_label = ();
73 my $summarize = $self->option('summarize_svcs',1);
75 warn "$me billing for bulk services from ". time2str('%x', $last_bill).
76 " to ". time2str('%x', $$sdate). "\n"
80 foreach my $h_cust_svc ( $cust_pkg->h_cust_svc( $$sdate, $last_bill ) ) {
82 my @label = $h_cust_svc->label_long( $$sdate, $last_bill );
83 die "fatal: no historical label found, wtf?" unless scalar(@label); #?
84 my $svc_details = $label[0]. ': '. $label[1]. ': ';
85 $part_svc_label{$h_cust_svc->svcpart} ||= $label[0];
89 my $svc_start = $h_cust_svc->date_inserted;
90 if ( $svc_start < $last_bill ) {
91 $svc_start = $last_bill;
92 } elsif ( $svc_setup_fee ) {
93 $svc_charge += $svc_setup_fee;
94 $svc_details .= $money_char. sprintf('%.2f setup, ', $svc_setup_fee);
95 $n_setup{$h_cust_svc->svcpart}++;
98 my $svc_end = $h_cust_svc->date_deleted;
99 $svc_end = ( !$svc_end || $svc_end > $$sdate ) ? $$sdate : $svc_end;
102 if ( $self->option('no_prorate',1) ) {
103 $recur_charge = $self->option('svc_recur_fee');
106 $recur_charge = $self->option('svc_recur_fee')
107 * ( $svc_end - $svc_start )
108 / ( $$sdate - $last_bill );
111 $svc_details .= $money_char. sprintf('%.2f', $recur_charge ).
112 ' ('. time2str('%x', $svc_start).
113 ' - '. time2str('%x', $svc_end ). ')'
116 $svc_charge += $recur_charge;
117 $n_recur{$h_cust_svc->svcpart}++;
118 push @$details, $svc_details if !$summarize;
119 $total_svc_charge += $svc_charge;
123 foreach my $svcpart (keys %part_svc_label) {
124 push @$details, sprintf('Setup fee: %d @ '.$money_char.'%.2f',
125 $n_setup{$svcpart}, $svc_setup_fee )
126 if $svc_setup_fee and $n_setup{$svcpart};
127 push @$details, sprintf('%d services @ '.$money_char.'%.2f',
128 $n_recur{$svcpart}, $self->option('svc_recur_fee') )
129 if $n_recur{$svcpart};
133 sprintf('%.2f', $self->base_recur($cust_pkg, $sdate) + $total_svc_charge );
136 sub can_discount { 0; }
138 sub hide_svc_detail {
142 sub is_free_options {
143 qw( setup_fee recur_fee svc_setup_fee svc_recur_fee );