1 package FS::part_pkg::agent;
4 use vars qw(@ISA $DEBUG $me %info);
6 use FS::Record qw( qsearch );
10 #use FS::part_pkg::recur_Common;;
11 #@ISA = qw(FS::part_pkg::recur_Common);
12 use FS::part_pkg::prorate;
13 @ISA = qw(FS::part_pkg::prorate);
17 $me = '[FS::part_pkg::agent]';
20 'name' => 'Wholesale bulk billing, for master customers of an agent.',
21 'shortname' => 'Wholesale bulk billing for agent.',
24 'setup_fee' => { 'name' => 'Setup fee for this package',
27 'recur_fee' => { 'name' => 'Base recurring fee for this package',
32 #'recur_method' => { 'name' => 'Recurring fee method',
34 # #'options' => \%recur_method,
36 # 'select_options' => \%recur_Common::recur_method,
38 'cutoff_day' => { 'name' => 'Billing Day (1 - 28)',
43 #'fieldorder' => [qw( setup_fee recur_fee recur_method cutoff_day ) ],
44 'fieldorder' => [qw( setup_fee recur_fee cutoff_day ) ],
50 #some false laziness-ish w/bulk.pm... not a lot
53 my($cust_pkg, $sdate, $details, $param ) = @_;
55 my $last_bill = $cust_pkg->last_bill;
57 return sprintf("%.2f", $self->SUPER::calc_recur(@_) )
58 unless $$sdate > $last_bill;
60 my $conf = new FS::Conf;
61 my $money_char = $conf->config('money_char') || '$';
63 my $prorate_sdate = $$sdate;
65 my $total_agent_charge = 0;
67 warn "$me billing for agent packages from ". time2str('%x', $last_bill).
68 " to ". time2str('%x', $$sdate).
69 "prorated to". time2str('%x', $prorate_sdate ).
73 #almost always just one,
74 #unless you have multiple agents with same master customer0
75 my @agents = qsearch('agent', { 'agent_custnum' => $cust_pkg->custnum } );
77 foreach my $agent (@agents) {
79 warn "$me billing for agent ". $agent->agent. "\n"
82 #not the most efficient to load them all into memory,
83 #but good enough for our current needs
84 my @cust_main = qsearch('cust_main', { 'agentnum' => $agent->agentnum } );
86 foreach my $cust_main (@cust_main) {
88 warn "$me billing agent charges for ". $cust_main->name_short. "\n"
91 #make sure setup dates are filled in
92 my $error = $cust_main->bill; #options don't propogate from freeside-daily
93 die "Error pre-billing agent customer: $error" if $error;
95 my @cust_pkg = grep { my $setup = $_->get('setup');
96 my $cancel = $_->get('cancel');
98 $setup < $$sdate # END
99 && ( ! $cancel || $cancel > $last_bill ) #START
101 $cust_main->all_pkgs;
103 foreach my $cust_pkg ( @cust_pkg ) {
105 warn "$me billing agent charges for pkgnum ". $cust_pkg->pkgnum. "\n"
108 my $pkg_details = $cust_main->name_short. ': '; #name?
109 # + something to identify package... primary service probably
113 my $part_pkg = $cust_pkg->part_pkg;
114 #option to not fallback? via options above
116 $part_pkg->setup_cost || $part_pkg->option('setup_fee');
118 $part_pkg->recur_cost || $part_pkg->base_recur_permonth;
120 my $pkg_start = $cust_pkg->get('setup');
121 if ( $pkg_start < $last_bill ) {
122 $pkg_start = $last_bill;
123 } elsif ( $pkg_setup_fee ) {
124 $pkg_charge += $pkg_setup_fee;
125 $pkg_details .= $money_char. sprintf('%.2f setup, ', $pkg_setup_fee );
128 my $pkg_end = $cust_pkg->get('cancel');
129 $pkg_end = ( !$pkg_end || $pkg_end > $$sdate ) ? $$sdate : $pkg_end;
131 my $recur_charge += $pkg_base_recur * ( $pkg_end - $pkg_start )
132 / ( $prorate_sdate - $last_bill );
134 $pkg_details .= $money_char. sprintf('%.2f', $recur_charge ).
135 ' ('. time2str('%x', $pkg_start).
136 ' - '. time2str('%x', $pkg_end ). ')'
139 $pkg_charge += $recur_charge;
141 push @$details, $pkg_details;
142 $total_agent_charge += $pkg_charge;
146 } #foreach $cust_main
150 my $charges = $total_agent_charge + $self->SUPER::calc_recur(@_); #prorate
152 sprintf('%.2f', $charges );
156 sub hide_svc_detail {