1 package FS::part_pkg::agent;
2 #use base qw(FS::part_pkg::recur_Common);
3 use base qw(FS::part_pkg::prorate);
6 use vars qw($DEBUG $me %info);
8 use FS::Record qw( qsearch );
14 $me = '[FS::part_pkg::agent]';
17 'name' => 'Wholesale bulk billing, for master customers of an agent.',
18 'shortname' => 'Wholesale bulk billing for agent.',
19 'inherit_fields' => [qw( prorate global_Mixin)],
21 #'recur_method' => { 'name' => 'Recurring fee method',
23 # #'options' => \%recur_method,
25 # 'select_options' => \%recur_Common::recur_method,
27 'cutoff_day' => { 'name' => 'Billing Day (1 - 28)',
30 'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
31 'for one full period after that',
35 'no_pkg_prorate' => { 'name' => 'Disable prorating bulk packages (charge full price for packages active only a portion of the month)',
41 'fieldorder' => [qw( cutoff_day add_full_period no_pkg_prorate ) ],
47 #some false laziness-ish w/bulk.pm... not a lot
50 my($cust_pkg, $sdate, $details, $param ) = @_;
52 my $last_bill = $cust_pkg->last_bill;
54 return sprintf("%.2f", $self->SUPER::calc_recur(@_) )
55 unless $$sdate > $last_bill;
57 my $conf = new FS::Conf;
58 my $money_char = $conf->config('money_char') || '$';
60 my $total_agent_charge = 0;
62 warn "$me billing for agent packages from ". time2str('%x', $last_bill).
63 " to ". time2str('%x', $$sdate). "\n"
66 my $prorate_ratio = ( $$sdate - $last_bill )
67 / ( $self->add_freq($last_bill) - $last_bill );
69 #almost always just one,
70 #unless you have multiple agents with same master customer0
71 my @agents = qsearch('agent', { 'agent_custnum' => $cust_pkg->custnum } );
73 foreach my $agent (@agents) {
75 warn "$me billing for agent ". $agent->agent. "\n"
78 #not the most efficient to load them all into memory,
79 #but good enough for our current needs
80 my @cust_main = qsearch('cust_main', { 'agentnum' => $agent->agentnum } );
82 foreach my $cust_main (@cust_main) {
84 warn "$me billing agent charges for ". $cust_main->name_short. "\n"
87 #make sure setup dates are filled in
88 my $error = $cust_main->bill; #options don't propogate from freeside-daily
89 die "Error pre-billing agent customer: $error" if $error;
91 my @cust_pkg = grep { my $setup = $_->get('setup');
92 my $cancel = $_->get('cancel');
94 $setup < $$sdate # END
95 && ( ! $cancel || $cancel > $last_bill ) #START
99 foreach my $cust_pkg ( @cust_pkg ) {
101 warn "$me billing agent charges for pkgnum ". $cust_pkg->pkgnum. "\n"
104 my $pkg_details = $cust_main->name_short. ': '; #name?
105 # + something to identify package... primary service probably
109 my $part_pkg = $cust_pkg->part_pkg;
110 #option to not fallback? via options above
112 $part_pkg->setup_cost || $part_pkg->option('setup_fee');
114 $part_pkg->recur_cost || $part_pkg->base_recur_permonth($cust_pkg);
116 my $pkg_start = $cust_pkg->get('setup');
117 if ( $pkg_start < $last_bill ) {
118 $pkg_start = $last_bill;
119 } elsif ( $pkg_setup_fee ) {
120 $pkg_charge += $pkg_setup_fee;
121 $pkg_details .= $money_char. sprintf('%.2f setup, ', $pkg_setup_fee );
124 my $pkg_end = $cust_pkg->get('cancel');
125 $pkg_end = ( !$pkg_end || $pkg_end > $$sdate ) ? $$sdate : $pkg_end;
128 my $pkg_recur_charge = $prorate_ratio * $pkg_base_recur;
129 $pkg_recur_charge *= ( $pkg_end - $pkg_start )
130 / ( $$sdate - $last_bill )
131 unless $self->option('no_pkg_prorate');
133 my $recur_charge += $pkg_recur_charge;
135 $pkg_details .= $money_char. sprintf('%.2f', $recur_charge ).
136 ' ('. time2str('%x', $pkg_start).
137 ' - '. time2str('%x', $pkg_end ). ')'
140 $pkg_charge += $recur_charge;
142 push @$details, $pkg_details
144 $total_agent_charge += $pkg_charge;
148 } #foreach $cust_main
152 my $charges = $total_agent_charge + $self->SUPER::calc_recur(@_); #prorate
154 sprintf('%.2f', $charges );
158 sub can_discount { 0; }
160 sub hide_svc_detail { 1; }
164 sub can_usageprice { 0; }