[freeside-commits] freeside/FS/FS/part_pkg agent.pm,NONE,1.1

Ivan,,, ivan at wavetail.420.am
Thu Jun 18 04:04:16 PDT 2009


Update of /home/cvs/cvsroot/freeside/FS/FS/part_pkg
In directory wavetail.420.am:/tmp/cvs-serv31644/FS/FS/part_pkg

Added Files:
	agent.pm 
Log Message:
add agent wholsale price plan, RT#4696

--- NEW FILE: agent.pm ---
package FS::part_pkg::agent;

use strict;
use vars qw(@ISA $DEBUG $me %info);
use Date::Format;
use FS::Record qw( qsearch );
use FS::agent;
use FS::cust_main;

#use FS::part_pkg::recur_Common;;
#@ISA = qw(FS::part_pkg::recur_Common);
use FS::part_pkg::prorate;
@ISA = qw(FS::part_pkg::prorate);

$DEBUG = 0;

$me = '[FS::part_pkg::agent]';

%info = (
  'name'      => 'Wholesale bulk billing, for master customers of an agent.',
  'shortname' => 'Wholesale bulk billing for agent.',

  'fields' => {
    'setup_fee'     => { 'name' => 'Setup fee for this package',
                         'default' => 0,
                       },
    'recur_fee'     => { 'name' => 'Base recurring fee for this package',
                         'default' => 0,
                       },


    #'recur_method'  => { 'name' => 'Recurring fee method',
    #                     #'type' => 'radio',
    #                     #'options' => \%recur_method,
    #                     'type' => 'select',
    #                     'select_options' => \%recur_Common::recur_method,
    #                   },
    'cutoff_day'    => { 'name' => 'Billing Day (1 - 28)',
                         'default' => '1',
                       },
  },

  #'fieldorder' => [qw( setup_fee recur_fee recur_method cutoff_day ) ],
  'fieldorder' => [qw( setup_fee recur_fee cutoff_day ) ],

  'weight' => 51,

);

#some false laziness-ish w/bulk.pm...  not a lot
sub calc_recur {
  my $self = shift;
  my($cust_pkg, $sdate, $details, $param ) = @_;

  my $last_bill = $cust_pkg->last_bill;

  return sprintf("%.2f", $self->SUPER::calc_recur(@_) )
    unless $$sdate > $last_bill;

  my $conf = new FS::Conf;
  my $money_char = $conf->config('money_char') || '$';

  my $prorate_sdate = $$sdate;
  
  my $total_agent_charge = 0;

  warn "$me billing for agent packages from ". time2str('%x', $last_bill).
                                       " to ". time2str('%x', $$sdate).
                                "prorated to". time2str('%x', $prorate_sdate ).
       "\n"
    if $DEBUG;

  #almost always just one,
  #unless you have multiple agents with same master customer0
  my @agents = qsearch('agent', { 'agent_custnum' => $cust_pkg->custnum } );

  foreach my $agent (@agents) {

    warn "$me billing for agent ". $agent->agent. "\n"
      if $DEBUG;

    #not the most efficient to load them all into memory,
    #but good enough for our current needs
    my @cust_main = qsearch('cust_main', { 'agentnum' => $agent->agentnum } );

    foreach my $cust_main (@cust_main) {

      warn "$me billing agent charges for ". $cust_main->name_short. "\n"
        if $DEBUG;

      #make sure setup dates are filled in
      my $error = $cust_main->bill; #options don't propogate from freeside-daily
      die "Error pre-billing agent customer: $error" if $error;

      my @cust_pkg = grep { my $setup  = $_->get('setup');
                            my $cancel = $_->get('cancel');

                            $setup < $$sdate  # END
                            && ( ! $cancel || $cancel > $last_bill ) #START
                          }
                     $cust_main->all_pkgs;

      foreach my $cust_pkg ( @cust_pkg ) {

        warn "$me billing agent charges for pkgnum ". $cust_pkg->pkgnum. "\n"
          if $DEBUG;

        my $pkg_details = $cust_main->name_short. ': '; #name?
        # + something to identify package... primary service probably

        my $pkg_charge = 0;

        my $part_pkg = $cust_pkg->part_pkg;
        #option to not fallback? via options above
        my $pkg_setup_fee  =
          $part_pkg->setup_cost || $part_pkg->option('setup_fee');
        my $pkg_base_recur =
          $part_pkg->recur_cost || $part_pkg->base_recur_permonth;

        my $pkg_start = $cust_pkg->get('setup');
        if ( $pkg_start < $last_bill ) {
          $pkg_start = $last_bill;
        } elsif ( $pkg_setup_fee ) {
          $pkg_charge += $pkg_setup_fee;
          $pkg_details .= $money_char. sprintf('%.2f setup, ', $pkg_setup_fee );
        }

        my $pkg_end = $cust_pkg->get('cancel');
        $pkg_end = ( !$pkg_end || $pkg_end > $$sdate ) ? $$sdate : $pkg_end;

        my $recur_charge += $pkg_base_recur * ( $pkg_end - $pkg_start )
                                            / ( $prorate_sdate  - $last_bill );

        $pkg_details .= $money_char. sprintf('%.2f', $recur_charge ).
                        ' ('.  time2str('%x', $pkg_start).
                        ' - '. time2str('%x', $pkg_end  ). ')'
          if $recur_charge;

        $pkg_charge += $recur_charge;

        push @$details, $pkg_details;
        $total_agent_charge += $pkg_charge;

      } #foreach $cust_pkg

    } #foreach $cust_main

  } #foreach $agent;

  my $charges = $total_agent_charge + $self->SUPER::calc_recur(@_); #prorate

  sprintf('%.2f', $charges );

}

sub hide_svc_detail {
  1;
}

sub is_free {
  0;
}

1;




More information about the freeside-commits mailing list