Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / part_pkg / agent_cdr.pm
1 package FS::part_pkg::agent_cdr;
2 use base qw( FS::part_pkg::recur_Common );
3
4 #kind of glommed together from cdr_termination, agent, voip_cdr
5 # some false laziness w/ all of them
6
7 use strict;
8 use vars qw( $DEBUG $me %info );
9 use FS::Record qw( qsearch );
10 use FS::PagedSearch qw( psearch );
11 use FS::agent;
12 use FS::cust_main;
13 use FS::cdr;
14
15 $DEBUG = 0;
16
17 $me = '[FS::part_pkg::agent_cdr]';
18
19 tie my %temporalities, 'Tie::IxHash',
20   'upcoming'  => "Upcoming (future)",
21   'preceding' => "Preceding (past)",
22 ;
23
24 %info = (
25   'name'      => 'Wholesale CDR cost billing, for master customers of an agent.',
26   'shortname' => 'Wholesale CDR cost billing for agent',
27   'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
28   'fields' => { #false laziness w/cdr_termination
29
30     #false laziness w/flat.pm
31     'recur_temporality' => { 'name' => 'Charge recurring fee for period',
32                              'type' => 'select',
33                              'select_options' => \%temporalities,
34                            },
35
36     'cutoff_day'    => { 'name' => 'Billing Day (1 - 28) for prorating or '.
37                                    'subscription',
38                          'default' => '1',
39                        },
40     'recur_method'  => { 'name' => 'Recurring fee method',
41                          #'type' => 'radio',
42                          #'options' => \%recur_method,
43                          'type' => 'select',
44                          'select_options' => \%FS::part_pkg::recur_Common::recur_method,
45                        },
46
47     #false laziness w/voip_cdr.pm
48     'output_format' => { 'name' => 'CDR invoice display format',
49                          'type' => 'select',
50                          'select_options' => { FS::cdr::invoice_formats() },
51                          'default'        => 'simple2', #with source
52                        },
53
54     'usage_section' => { 'name' => 'Section in which to place separate usage charges',
55                        },
56
57     'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
58                           'type' => 'checkbox',
59                         },
60
61     'usage_mandate' => { 'name' => 'Always put usage details in separate section',
62                           'type' => 'checkbox',
63                        },
64     #eofalse
65
66   },
67
68   'fieldorder' => [ qw( recur_temporality recur_method cutoff_day ),
69                     FS::part_pkg::prorate_Mixin::fieldorder, 
70                     qw(
71                        output_format usage_section summarize_usage usage_mandate
72                     ),
73                   ],
74
75   'weight' => 53,
76
77 );
78
79 sub calc_recur {
80   my( $self, $cust_pkg, $sdate, $details, $param ) = @_;
81
82   #my $last_bill = $cust_pkg->last_bill;
83   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
84
85   return 0
86     if $self->recur_temporality eq 'preceding'
87     && ( $last_bill eq '' || $last_bill == 0 );
88
89   my $charges = 0;
90
91   my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
92
93   #CDR calculations
94
95   #false laziness w/agent.pm
96   #almost always just one,
97   #unless you have multiple agents with same master customer0
98   my @agents = qsearch('agent', { 'agent_custnum' => $cust_pkg->custnum } );
99
100   foreach my $agent (@agents) {
101
102     warn "$me billing wholesale CDRs for agent ". $agent->agent. "\n"
103       if $DEBUG;
104
105     #not the most efficient to load them all into memory,
106     #but good enough for our current needs
107     my @cust_main = qsearch('cust_main', { 'agentnum' => $agent->agentnum } );
108
109     foreach my $cust_main (@cust_main) {
110
111       warn "$me billing agent wholesale CDRs for ". $cust_main->name_short. "\n"
112         if $DEBUG;
113
114       #eofalse laziness w/agent.pm
115
116       my @svcnum = ();
117       foreach my $cust_pkg ( $cust_main->cust_pkg ) {
118         push @svcnum, map $_->svcnum, $cust_pkg->cust_svc( svcdb=>'svc_phone' );
119       }
120
121       next unless @svcnum;
122
123       #false laziness w/cdr_termination
124
125       my $termpart = 1; #or from an option -- we're not termination, we're wholesale?  for now, use one or the other
126
127       #false lazienss w/search/cdr.html (i should be a part_termination method)
128       my $where_term =
129         "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
130       #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
131       my $extra_sql =
132         "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
133
134       #eofalse laziness w/cdr_termination.pm
135
136       #false laziness w/ svc_phone->psearch_cdrs, kinda
137       my $cdr_search = psearch({
138         'table'     => 'cdr',
139         #'addl_from' => $join_term,
140         'hashref'   => {},
141         'extra_sql' => " WHERE freesidestatus IN ( 'rated', 'done' ) ".
142                        "   AND svcnum IN (". join(',', @svcnum). ") ".
143                        $extra_sql,
144         'order_by'  => 'ORDER BY startdate FOR UPDATE ',
145
146       });
147
148       #false laziness w/voip_cdr
149       $cdr_search->limit(1000);
150       $cdr_search->increment(0); #because we're adding cdr_termination as we go?
151       while ( my $cdr = $cdr_search->fetch ) {
152
153         my $cost = $cdr->rate_cost;
154         #XXX exception handling?  return undef? (and err?) ref to a scalar err?
155
156         #false laziness w/cdr_termination
157
158         #add a cdr_termination record and the charges
159
160         my $cdr_termination = new FS::cdr_termination {
161           'acctid'      => $cdr->acctid,
162           'termpart'    => $termpart,
163           'rated_price' => $cost,
164           'status'      => 'done',
165         };
166
167         my $error = $cdr_termination->insert;
168         die $error if $error; #next if $error; #or just skip this one???  why?
169
170         $charges += $cost;
171
172         # and add a line to the invoice
173
174         my $call_details = $cdr->downstream_csv( 'format' => $output_format,
175                                                  'charge' => $cost,
176                                                );
177         my $classnum = ''; #usage class?
178
179        #option to turn off?  or just use squelch_cdr for the customer probably
180         # XXX use detail_format for this at some point
181         push @$details, { 'format'    => 'C',
182                           'detail'    => $call_details,
183                           'amount'    => $cost,
184                           'classnum'  => $classnum };
185
186         #eofalse laziness w/cdr_termination
187
188       }
189
190     }
191
192   }
193
194   #eo CDR calculations
195
196   $charges += ($cust_pkg->quantity || 1)
197                 * $self->calc_recur_Common($cust_pkg, $sdate, $details, $param);
198
199   $charges;
200 }
201
202 sub can_discount { 0; }
203
204 #?  sub hide_svc_detail { 1; }
205
206 sub is_free { 0; }
207
208 sub can_usageprice { 0; }
209
210 1;