add skip_dcontext_suffix to skip CDRs with dcontext ending in a definable string...
[freeside.git] / FS / FS / part_pkg / cdr_termination.pm
1 package FS::part_pkg::cdr_termination;
2 use base qw( FS::part_pkg::recur_Common );
3
4 use strict;
5 use vars qw( $DEBUG %info );
6 use Tie::IxHash;
7 use FS::Record qw( qsearch ); #qsearchs );
8 use FS::cdr;
9 use FS::cdr_termination;
10
11 tie my %temporalities, 'Tie::IxHash',
12   'upcoming'  => "Upcoming (future)",
13   'preceding' => "Preceding (past)",
14 ;
15
16 %info = (
17   'name' => 'VoIP rating of CDR records for termination partners.',
18   'shortname' => 'VoIP/telco CDR termination',
19   'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
20   'fields' => {
21     #'cdr_column'    => { 'name' => 'Column from CDR records',
22     #                     'type' => 'select',
23     #                     'select_enum' => [qw(
24     #                       dcontext
25     #                       channel
26     #                       dstchannel
27     #                       lastapp
28     #                       lastdata
29     #                       accountcode
30     #                       userfield
31     #                       cdrtypenum
32     #                       calltypenum
33     #                       description
34     #                       carrierid
35     #                       upstream_rateid
36     #                     )],
37     #                   },
38
39     #false laziness w/flat.pm
40     'recur_temporality' => { 'name' => 'Charge recurring fee for period',
41                              'type' => 'select',
42                              'select_options' => \%temporalities,
43                            },
44
45     'cutoff_day'    => { 'name' => 'Billing Day (1 - 28) for prorating or '.
46                                    'subscription',
47                          'default' => '1',
48                        },
49     'recur_method'  => { 'name' => 'Recurring fee method',
50                          #'type' => 'radio',
51                          #'options' => \%recur_method,
52                          'type' => 'select',
53                          'select_options' => \%FS::part_pkg::recur_Common::recur_method,
54                        },
55
56     #false laziness w/voip_cdr.pm
57     'output_format' => { 'name' => 'CDR invoice display format',
58                          'type' => 'select',
59                          'select_options' => { FS::cdr::invoice_formats() },
60                          'default'        => 'simple2', #XXX test
61                        },
62
63     'usage_section' => { 'name' => 'Section in which to place separate usage charges',
64                        },
65
66     'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
67                           'type' => 'checkbox',
68                         },
69
70     'usage_mandate' => { 'name' => 'Always put usage details in separate section',
71                           'type' => 'checkbox',
72                        },
73     #eofalse
74
75   },
76                        #cdr_column
77   'fieldorder' => [ qw( recur_temporality recur_method cutoff_day ),
78                     FS::part_pkg::prorate_Mixin::fieldorder, 
79                     qw(
80                        output_format usage_section summarize_usage usage_mandate
81                     ),
82                   ],
83
84   'weight' => 48,
85
86 );
87
88 sub calc_recur {
89   my $self = shift;
90   my($cust_pkg, $sdate, $details, $param ) = @_;
91
92   #my $last_bill = $cust_pkg->last_bill;
93   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
94
95   return 0
96     if $self->recur_temporality eq 'preceding'
97     && ( $last_bill eq '' || $last_bill == 0 );
98
99   # termination calculations
100
101   my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
102   die "no customer termination percentage" unless $term_percent;
103
104   my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
105
106   my $charges = 0;
107
108   #find an svc_external record
109   my @svc_external = map { $_->svc_x }
110                      $cust_pkg->cust_svc_unsorted( svcdb=>'svc_external' );
111
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;
116
117   my $svc_external = $svc_external[0];
118
119   # find CDRs:
120   # - matching our customer via svc_external.id/title?  (and via what field?)
121
122   #let's try carrierid for now, can always make it configurable or rewrite
123   my $cdr_column = 'carrierid';
124
125   my %hashref = ( 'freesidestatus' => 'done' );
126
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; 
129
130   # - with no cdr_termination.status
131
132   my $termpart = 1; #or from an option
133
134   #false lazienss w/search/cdr.html (i should be a part_termination method)
135   my $where_term =
136     "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
137   #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
138   my $extra_sql =
139     "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
140
141   #may need to process in batches if there's waaay too many
142   my @cdrs = qsearch({
143     'table'     => 'cdr',
144     #'addl_from' => $join_term,
145     'hashref'   => \%hashref,
146     'extra_sql' => "$extra_sql FOR UPDATE",
147   });
148
149   foreach my $cdr (@cdrs) {
150
151     #add a cdr_termination record and the charges
152
153     # XXX config?
154     #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
155     my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
156
157     my $cdr_termination = new FS::cdr_termination {
158       'acctid'      => $cdr->acctid,
159       'termpart'    => $termpart,
160       'rated_price' => $term_price,
161       'status'      => 'done',
162     };
163
164     my $error = $cdr_termination->insert;
165     die $error if $error; #next if $error; #or just skip this one???  why?
166
167     $charges += $term_price;
168
169     # and add a line to the invoice
170
171     my $call_details = $cdr->downstream_csv( 'format' => $output_format,
172                                              'charge' => $term_price,
173                                            );
174
175     my $classnum = ''; #usage class?
176
177     #option to turn off?  or just use squelch_cdr for the customer probably
178     push @$details, [ 'C', $call_details, $term_price, $classnum ];
179
180   }
181     
182   # eotermiation calculation
183
184   $charges += ($cust_pkg->quantity || 1) * $self->calc_recur_Common(@_);
185
186   $charges;
187 }
188
189 sub is_free { 0; }
190
191 sub can_usageprice { 0; }
192
193 1;