1 package FS::part_pkg::cdr_termination;
2 use base qw( FS::part_pkg::recur_Common );
5 use vars qw( $DEBUG %info );
7 use FS::Record qw( qsearch ); #qsearchs );
9 use FS::cdr_termination;
11 tie my %temporalities, 'Tie::IxHash',
12 'upcoming' => "Upcoming (future)",
13 'preceding' => "Preceding (past)",
17 'name' => 'VoIP rating of CDR records for termination partners.',
18 'shortname' => 'VoIP/telco CDR termination',
19 'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
21 #'cdr_column' => { 'name' => 'Column from CDR records',
23 # 'select_enum' => [qw(
39 #false laziness w/flat.pm
40 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
42 'select_options' => \%temporalities,
45 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
49 'recur_method' => { 'name' => 'Recurring fee method',
51 #'options' => \%recur_method,
53 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
56 #false laziness w/voip_cdr.pm
57 'output_format' => { 'name' => 'CDR invoice display format',
59 'select_options' => { FS::cdr::invoice_formats() },
60 'default' => 'simple2', #XXX test
63 'usage_section' => { 'name' => 'Section in which to place separate usage charges',
66 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
70 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
77 'fieldorder' => [ qw( recur_temporality recur_method cutoff_day ),
78 FS::part_pkg::prorate_Mixin::fieldorder,
80 output_format usage_section summarize_usage usage_mandate
90 my($cust_pkg, $sdate, $details, $param ) = @_;
92 #my $last_bill = $cust_pkg->last_bill;
93 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
96 if $self->recur_temporality eq 'preceding'
97 && ( $last_bill eq '' || $last_bill == 0 );
99 # termination calculations
101 my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
102 die "no customer termination percentage" unless $term_percent;
104 my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
108 #find an svc_external record
109 my @svc_external = map { $_->svc_x }
110 $cust_pkg->cust_svc_unsorted( svcdb=>'svc_external' );
112 die "cdr_termination package has no svc_external service"
113 unless @svc_external;
114 die "cdr_termination package has multiple svc_external services"
115 if scalar(@svc_external) > 1;
117 my $svc_external = $svc_external[0];
120 # - matching our customer via svc_external.id/title? (and via what field?)
122 #let's try carrierid for now, can always make it configurable or rewrite
123 my $cdr_column = 'carrierid';
125 my %hashref = ( 'freesidestatus' => 'done' );
127 # try matching on svc_external.id for now... (or title? if ints don't cut it)
128 $hashref{$cdr_column} = $svc_external[0]->id;
130 # - with no cdr_termination.status
132 my $termpart = 1; #or from an option
134 #false lazienss w/search/cdr.html (i should be a part_termination method)
136 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
137 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
139 "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
141 #may need to process in batches if there's waaay too many
144 #'addl_from' => $join_term,
145 'hashref' => \%hashref,
146 'extra_sql' => "$extra_sql FOR UPDATE",
149 foreach my $cdr (@cdrs) {
151 #add a cdr_termination record and the charges
154 #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
155 my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
157 my $cdr_termination = new FS::cdr_termination {
158 'acctid' => $cdr->acctid,
159 'termpart' => $termpart,
160 'rated_price' => $term_price,
164 my $error = $cdr_termination->insert;
165 die $error if $error; #next if $error; #or just skip this one??? why?
167 $charges += $term_price;
169 # and add a line to the invoice
171 my $call_details = $cdr->downstream_csv( 'format' => $output_format,
172 'charge' => $term_price,
175 my $classnum = ''; #usage class?
177 #option to turn off? or just use squelch_cdr for the customer probably
178 push @$details, [ 'C', $call_details, $term_price, $classnum ];
182 # eotermiation calculation
184 $charges += ($cust_pkg->quantity || 1) * $self->calc_recur_Common(@_);
191 sub can_usageprice { 0; }