import torrus 1.0.9
[freeside.git] / FS / FS / part_pkg / cdr_termination.pm
1 package FS::part_pkg::cdr_termination;
2
3 use strict;
4 use base qw( FS::part_pkg::recur_Common );
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' => [ '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     'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
50                                     'for one full period after that',
51                           'type' => 'checkbox',
52                         },
53
54     'recur_method'  => { 'name' => 'Recurring fee method',
55                          #'type' => 'radio',
56                          #'options' => \%recur_method,
57                          'type' => 'select',
58                          'select_options' => \%FS::part_pkg::recur_Common::recur_method,
59                        },
60
61     #false laziness w/voip_cdr.pm
62     'output_format' => { 'name' => 'CDR invoice display format',
63                          'type' => 'select',
64                          'select_options' => { FS::cdr::invoice_formats() },
65                          'default'        => 'simple2', #XXX test
66                        },
67
68     'usage_section' => { 'name' => 'Section in which to place separate usage charges',
69                        },
70
71     'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
72                           'type' => 'checkbox',
73                         },
74
75     'usage_mandate' => { 'name' => 'Always put usage details in separate section',
76                           'type' => 'checkbox',
77                        },
78     #eofalse
79
80   },
81                        #cdr_column
82   'fieldorder' => [qw(
83                        recur_temporality recur_method cutoff_day
84                        add_full_period
85                        output_format usage_section summarize_usage usage_mandate
86                      )
87                   ],
88
89   'weight' => 48,
90
91 );
92
93 sub calc_setup {
94   my($self, $cust_pkg ) = @_;
95   $self->option('setup_fee');
96 }
97
98 sub calc_recur {
99   my $self = shift;
100   my($cust_pkg, $sdate, $details, $param ) = @_;
101
102   #my $last_bill = $cust_pkg->last_bill;
103   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
104
105   return 0
106     if $self->option('recur_temporality', 1) eq 'preceding'
107     && ( $last_bill eq '' || $last_bill == 0 );
108
109   # termination calculations
110
111   my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
112   die "no customer termination percentage" unless $term_percent;
113
114   my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
115
116   my $charges = 0;
117
118   #find an svc_external record
119   my @svc_external = map  { $_->svc_x }
120                      grep { $_->part_svc->svcdb eq 'svc_external' }
121                      $cust_pkg->cust_svc;
122
123   die "cdr_termination package has no svc_external service"
124     unless @svc_external;
125   die "cdr_termination package has multiple svc_external services"
126     if scalar(@svc_external) > 1;
127
128   my $svc_external = $svc_external[0];
129
130   # find CDRs:
131   # - matching our customer via svc_external.id/title?  (and via what field?)
132
133   #let's try carrierid for now, can always make it configurable or rewrite
134   my $cdr_column = 'carrierid';
135
136   my %hashref = ( 'freesidestatus' => 'done' );
137
138   # try matching on svc_external.id for now... (or title?  if ints don't cut it)
139   $hashref{$cdr_column} = $svc_external[0]->id; 
140
141   # - with no cdr_termination.status
142
143   my $termpart = 1; #or from an option
144
145   #false lazienss w/search/cdr.html (i should be a part_termination method)
146   my $where_term =
147     "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
148   #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
149   my $extra_sql =
150     "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
151
152   #may need to process in batches if there's waaay too many
153   my @cdrs = qsearch({
154     'table'     => 'cdr',
155     #'addl_from' => $join_term,
156     'hashref'   => \%hashref,
157     'extra_sql' => "$extra_sql FOR UPDATE",
158   });
159
160   foreach my $cdr (@cdrs) {
161
162     #add a cdr_termination record and the charges
163
164     # XXX config?
165     #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
166     my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
167
168     my $cdr_termination = new FS::cdr_termination {
169       'acctid'      => $cdr->acctid,
170       'termpart'    => $termpart,
171       'rated_price' => $term_price,
172       'status'      => 'done',
173     };
174
175     my $error = $cdr_termination->insert;
176     die $error if $error; #next if $error; #or just skip this one???  why?
177
178     $charges += $term_price;
179
180     # and add a line to the invoice
181
182     my $call_details = $cdr->downstream_csv( 'format' => $output_format,
183                                              'charge' => $term_price,
184                                            );
185
186     my $classnum = ''; #usage class?
187
188     #option to turn off?  or just use squelch_cdr for the customer probably
189     push @$details, [ 'C', $call_details, $term_price, $classnum ];
190
191   }
192     
193   # eotermiation calculation
194
195   $charges += $self->calc_recur_Common(@_);
196
197   $charges;
198 }
199
200 sub is_free {
201   0;
202 }
203
204 1;