add sip_passwordmin/max, RT#25239
[freeside.git] / FS / FS / svc_phone.pm
1 package FS::svc_phone;
2
3 use strict;
4 use base qw( FS::svc_Domain_Mixin FS::location_Mixin FS::svc_Common );
5 use vars qw( $DEBUG $me @pw_set $conf $phone_name_max
6              $passwordmin $passwordmax
7            );
8 use Data::Dumper;
9 use Scalar::Util qw( blessed );
10 use List::Util qw( min );
11 use FS::Conf;
12 use FS::Record qw( qsearch qsearchs dbh );
13 use FS::Msgcat qw(gettext);
14 use FS::part_svc;
15 use FS::phone_device;
16 use FS::svc_pbx;
17 use FS::svc_domain;
18 use FS::cust_location;
19 use FS::phone_avail;
20
21 $me = '[' . __PACKAGE__ . ']';
22 $DEBUG = 0;
23
24 #avoid l 1 and o O 0
25 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
26
27 #ask FS::UID to run this stuff for us later
28 $FS::UID::callback{'FS::svc_acct'} = sub { 
29   $conf = new FS::Conf;
30   $phone_name_max = $conf->config('svc_phone-phone_name-max_length');
31   $passwordmin = $conf->config('sip_passwordmin') || 0;
32   $passwordmax = $conf->config('sip_passwordmax') || 80;
33 };
34
35 =head1 NAME
36
37 FS::svc_phone - Object methods for svc_phone records
38
39 =head1 SYNOPSIS
40
41   use FS::svc_phone;
42
43   $record = new FS::svc_phone \%hash;
44   $record = new FS::svc_phone { 'column' => 'value' };
45
46   $error = $record->insert;
47
48   $error = $new_record->replace($old_record);
49
50   $error = $record->delete;
51
52   $error = $record->check;
53
54   $error = $record->suspend;
55
56   $error = $record->unsuspend;
57
58   $error = $record->cancel;
59
60 =head1 DESCRIPTION
61
62 An FS::svc_phone object represents a phone number.  FS::svc_phone inherits
63 from FS::Record.  The following fields are currently supported:
64
65 =over 4
66
67 =item svcnum
68
69 primary key
70
71 =item countrycode
72
73 =item phonenum
74
75 =item sip_password
76
77 =item pin
78
79 Voicemail PIN
80
81 =item phone_name
82
83 =item pbxsvc
84
85 Optional svcnum from svc_pbx
86
87 =item forwarddst
88
89 Forwarding destination
90
91 =item email
92
93 Email address for virtual fax (fax-to-email) services
94
95 =item lnp_status
96
97 LNP Status (can be null, native, portedin, portingin, portin-reject,
98 portingout, portout-reject)
99
100 =item portable
101
102 =item lrn
103
104 =item lnp_desired_due_date
105
106 =item lnp_due_date
107
108 =item lnp_other_provider
109
110 If porting the number in or out, name of the losing or winning provider, 
111 respectively.
112
113 =item lnp_other_provider_account
114
115 Account number of other provider. See lnp_other_provider.
116
117 =item lnp_reject_reason
118
119 See lnp_status. If lnp_status is portin-reject or portout-reject, this is an
120 optional reject reason.
121
122 =back
123
124 =head1 METHODS
125
126 =over 4
127
128 =item new HASHREF
129
130 Creates a new phone number.  To add the number to the database, see L<"insert">.
131
132 Note that this stores the hash reference, not a distinct copy of the hash it
133 points to.  You can ask the object for a copy with the I<hash> method.
134
135 =cut
136
137 # the new method can be inherited from FS::Record, if a table method is defined
138 #
139 sub table_info {
140  my %dis2 = ( disable_inventory=>1, disable_select=>1 );
141   {
142     'name' => 'Phone number',
143     'sorts' => 'phonenum',
144     'display_weight' => 60,
145     'cancel_weight'  => 80,
146     'fields' => {
147         'svcnum'       => 'Service',
148         'countrycode'  => { label => 'Country code',
149                             type  => 'text',
150                             disable_inventory => 1,
151                             disable_select => 1,
152                           },
153         'phonenum'     => 'Phone number',
154         'pin'          => { label => 'Voicemail PIN', #'Personal Identification Number',
155                             type  => 'text',
156                             disable_inventory => 1,
157                             disable_select => 1,
158                           },
159         'sip_password' => 'SIP password',
160         'phone_name'   => 'Name',
161         'pbxsvc'       => { label => 'PBX',
162                             type  => 'select-svc_pbx.html',
163                             disable_inventory => 1,
164                             disable_select => 1, #UI wonky, pry works otherwise
165                           },
166         'domsvc'    => {
167                          label     => 'Domain',
168                          type      => 'select',
169                          select_table => 'svc_domain',
170                          select_key   => 'svcnum',
171                          select_label => 'domain',
172                          disable_inventory => 1,
173                        },
174         'locationnum' => {
175                            label => 'E911 location',
176                            disable_inventory => 1,
177                            disable_select    => 1,
178                          },
179         'forwarddst' => {       label => 'Forward Destination', 
180                                 %dis2,
181                         },
182         'email' => {            label => 'Email',
183                                 %dis2,
184                     },
185         'lnp_status' => {       label => 'LNP Status',
186                                 type => 'select-lnp_status.html',
187                                 %dis2,
188                         },
189         'lnp_reject_reason' => { 
190                                 label => 'LNP Reject Reason',
191                                 %dis2,
192                         },
193         'portable' =>   {       label => 'Portable?', %dis2, },
194         'lrn'   =>      {       label => 'LRN', 
195                                 disable_inventory => 1, 
196                         },
197         'lnp_desired_due_date' =>
198                         { label => 'LNP Desired Due Date', %dis2 },
199         'lnp_due_date' =>
200                         { label => 'LNP Due Date', %dis2 },
201         'lnp_other_provider' =>
202                         {       label => 'LNP Other Provider', 
203                                 disable_inventory => 1, 
204                         },
205         'lnp_other_provider_account' =>
206                         {       label => 'LNP Other Provider Account #', 
207                                 %dis2 
208                         },
209     },
210   };
211 }
212
213 sub table { 'svc_phone'; }
214
215 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
216
217 =item search_sql STRING
218
219 Class method which returns an SQL fragment to search for the given string.
220
221 =cut
222
223 sub search_sql {
224   my( $class, $string ) = @_;
225
226   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
227     $string =~ s/\W//g;
228   } else {
229     $string =~ s/\D//g;
230   }
231
232   my $conf = new FS::Conf;
233   my $ccode = (    $conf->exists('default_phone_countrycode')
234                 && $conf->config('default_phone_countrycode')
235               )
236                 ? $conf->config('default_phone_countrycode') 
237                 : '1';
238
239   $string =~ s/^$ccode//;
240
241   $class->search_sql_field('phonenum', $string );
242 }
243
244 =item label
245
246 Returns the phone number.
247
248 =cut
249
250 sub label {
251   my $self = shift;
252   my $phonenum = $self->phonenum; #XXX format it better
253   my $label = $phonenum;
254   $label .= '@'.$self->domain if $self->domsvc;
255   $label .= ' ('.$self->phone_name.')' if $self->phone_name;
256   $label;
257 }
258
259 =item insert
260
261 Adds this phone number to the database.  If there is an error, returns the
262 error, otherwise returns false.
263
264 =cut
265
266 sub insert {
267   my $self = shift;
268   my %options = @_;
269
270   if ( $DEBUG ) {
271     warn "[$me] insert called on $self: ". Dumper($self).
272          "\nwith options: ". Dumper(%options);
273   }
274
275   local $SIG{HUP} = 'IGNORE';
276   local $SIG{INT} = 'IGNORE';
277   local $SIG{QUIT} = 'IGNORE';
278   local $SIG{TERM} = 'IGNORE';
279   local $SIG{TSTP} = 'IGNORE';
280   local $SIG{PIPE} = 'IGNORE';
281
282   my $oldAutoCommit = $FS::UID::AutoCommit;
283   local $FS::UID::AutoCommit = 0;
284   my $dbh = dbh;
285
286   #false laziness w/cust_pkg.pm... move this to location_Mixin?  that would
287   #make it more of a base class than a mixin... :)
288   if ( $options{'cust_location'}
289          && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
290     my $error = $options{'cust_location'}->insert;
291     if ( $error ) {
292       $dbh->rollback if $oldAutoCommit;
293       return "inserting cust_location (transaction rolled back): $error";
294     }
295     $self->locationnum( $options{'cust_location'}->locationnum );
296   }
297   #what about on-the-fly edits?  if the ui supports it?
298
299   my $error = $self->SUPER::insert(%options);
300   if ( $error ) {
301     $dbh->rollback if $oldAutoCommit;
302     return $error;
303   }
304
305   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
306   '';
307
308 }
309
310 =item delete
311
312 Delete this record from the database.
313
314 =cut
315
316 sub delete {
317   my $self = shift;
318
319   local $SIG{HUP} = 'IGNORE';
320   local $SIG{INT} = 'IGNORE';
321   local $SIG{QUIT} = 'IGNORE';
322   local $SIG{TERM} = 'IGNORE';
323   local $SIG{TSTP} = 'IGNORE';
324   local $SIG{PIPE} = 'IGNORE';
325
326   my $oldAutoCommit = $FS::UID::AutoCommit;
327   local $FS::UID::AutoCommit = 0;
328   my $dbh = dbh;
329
330   foreach my $phone_device ( $self->phone_device ) {
331     my $error = $phone_device->delete;
332     if ( $error ) {
333       $dbh->rollback if $oldAutoCommit;
334       return $error;
335     }
336   }
337
338   my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
339   foreach my $phone_avail ( @phone_avail ) {
340     $phone_avail->svcnum('');
341     my $error = $phone_avail->replace;
342     if ( $error ) {
343       $dbh->rollback if $oldAutoCommit;
344       return $error;
345     }
346   }
347
348   my $error = $self->SUPER::delete;
349   if ( $error ) {
350     $dbh->rollback if $oldAutoCommit;
351     return $error;
352   }
353
354   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
355   '';
356
357 }
358
359 # the delete method can be inherited from FS::Record
360
361 =item replace OLD_RECORD
362
363 Replaces the OLD_RECORD with this one in the database.  If there is an error,
364 returns the error, otherwise returns false.
365
366 =cut
367
368 sub replace {
369   my $new = shift;
370
371   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
372               ? shift
373               : $new->replace_old;
374
375   my %options = @_;
376
377   if ( $DEBUG ) {
378     warn "[$me] replacing $old with $new\n".
379          "\nwith options: ". Dumper(%options);
380   }
381
382   local $SIG{HUP} = 'IGNORE';
383   local $SIG{INT} = 'IGNORE';
384   local $SIG{QUIT} = 'IGNORE';
385   local $SIG{TERM} = 'IGNORE';
386   local $SIG{TSTP} = 'IGNORE';
387   local $SIG{PIPE} = 'IGNORE';
388
389   my $oldAutoCommit = $FS::UID::AutoCommit;
390   local $FS::UID::AutoCommit = 0;
391   my $dbh = dbh;
392
393   #false laziness w/cust_pkg.pm... move this to location_Mixin?  that would
394   #make it more of a base class than a mixin... :)
395   if ( $options{'cust_location'}
396          && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
397     my $error = $options{'cust_location'}->insert;
398     if ( $error ) {
399       $dbh->rollback if $oldAutoCommit;
400       return "inserting cust_location (transaction rolled back): $error";
401     }
402     $new->locationnum( $options{'cust_location'}->locationnum );
403   }
404   #what about on-the-fly edits?  if the ui supports it?
405
406   # LNP data validation
407  return 'Invalid LNP status' # if someone does really stupid stuff
408     if (  ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
409         || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
410         || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
411         || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
412         || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
413         || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
414         || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
415         );
416
417   my $error = $new->SUPER::replace($old, %options);
418   if ( $error ) {
419     $dbh->rollback if $oldAutoCommit;
420     return $error if $error;
421   }
422
423   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
424   ''; #no error
425 }
426
427 =item suspend
428
429 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
430
431 =item unsuspend
432
433 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
434
435 =item cancel
436
437 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
438
439 =item check
440
441 Checks all fields to make sure this is a valid phone number.  If there is
442 an error, returns the error, otherwise returns false.  Called by the insert
443 and replace methods.
444
445 =cut
446
447 # the check method should currently be supplied - FS::Record contains some
448 # data checking routines
449
450 sub check {
451   my $self = shift;
452
453   my $conf = new FS::Conf;
454
455   my $phonenum = $self->phonenum;
456   my $phonenum_check_method;
457   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
458     $phonenum =~ s/\W//g;
459     $phonenum_check_method = 'ut_alpha';
460   } else {
461     $phonenum =~ s/\D//g;
462     $phonenum_check_method = 'ut_number';
463   }
464   $self->phonenum($phonenum);
465
466   $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
467
468   my $error = 
469     $self->ut_numbern('svcnum')
470     || $self->ut_numbern('countrycode')
471     || $self->$phonenum_check_method('phonenum')
472     || $self->ut_anything('sip_password')
473     || $self->ut_numbern('pin')
474     || $self->ut_textn('phone_name')
475     || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx',    'svcnum' )
476     || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
477     || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
478     || $self->ut_numbern('forwarddst')
479     || $self->ut_textn('email')
480     || $self->ut_numbern('lrn')
481     || $self->ut_numbern('lnp_desired_due_date')
482     || $self->ut_numbern('lnp_due_date')
483     || $self->ut_textn('lnp_other_provider')
484     || $self->ut_textn('lnp_other_provider_account')
485     || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
486                                 'native', 'portin-reject', 'portout-reject'])
487     || $self->ut_enumn('portable', ['','Y'])
488     || $self->ut_textn('lnp_reject_reason')
489   ;
490   return $error if $error;
491
492     # LNP data validation
493     return 'Cannot set LNP fields: no LNP in progress'
494         if ( ($self->lnp_desired_due_date || $self->lnp_due_date 
495             || $self->lnp_other_provider || $self->lnp_other_provider_account
496             || $self->lnp_reject_reason) 
497             && (!$self->lnp_status || $self->lnp_status eq 'native') );
498     return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
499         if ($self->lnp_reject_reason && (!$self->lnp_status 
500                             || $self->lnp_status !~ /^port(in|out)-reject$/) );
501     return 'Cannot port-out a non-portable number' 
502         if (!$self->portable && $self->lnp_status eq 'portingout');
503
504
505   return 'Name ('. $self->phone_name.
506          ") is longer than $phone_name_max characters"
507     if $phone_name_max && length($self->phone_name) > $phone_name_max;
508
509   $self->countrycode(1) unless $self->countrycode;
510
511   unless ( length($self->pin) ) {
512     my $random_pin = $conf->config('svc_phone-random_pin');
513     if ( $random_pin =~ /^\d+$/ ) {
514       $self->pin(
515         join('', map int(rand(10)), 0..($random_pin-1))
516       );
517     }
518   }
519
520   if ( length($self->sip_password) ) {
521
522     return "SIP password must be longer than $passwordmin characters"
523       if length($self->sip_password) < $passwordmin;
524     return "SIP password must be shorter than $passwordmax characters"
525       if length($self->sip_password) > $passwordmax;
526
527   } else { # option for this?
528
529     $self->sip_password(
530       join('', map $pw_set[ int(rand $#pw_set) ], (1..min($passwordmax,16)) )
531     );
532
533   }
534
535   $self->SUPER::check;
536 }
537
538 =item _check duplicate
539
540 Internal method to check for duplicate phone numers.
541
542 =cut
543
544 #false laziness w/svc_acct.pm's _check_duplicate.
545 sub _check_duplicate {
546   my $self = shift;
547
548   my $global_unique = $conf->config('global_unique-phonenum') || 'none';
549   return '' if $global_unique eq 'disabled';
550
551   $self->lock_table;
552
553   my @dup_ccphonenum =
554     grep { !$self->svcnum || $_->svcnum != $self->svcnum }
555     qsearch( 'svc_phone', {
556       'countrycode' => $self->countrycode,
557       'phonenum'    => $self->phonenum,
558     });
559
560   return gettext('phonenum_in_use')
561     if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
562
563   my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
564   unless ( $part_svc ) {
565     return 'unknown svcpart '. $self->svcpart;
566   }
567
568   if ( @dup_ccphonenum ) {
569
570     my $exports = FS::part_export::export_info('svc_phone');
571     my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
572
573     foreach my $part_export ( $part_svc->part_export ) {
574
575       #this will catch to the same exact export
576       my @svcparts = map { $_->svcpart } $part_export->export_svc;
577
578       $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
579         foreach @svcparts;
580
581     }
582
583     foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
584       my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
585       if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
586         return "duplicate phone number ".
587                $self->countrycode. ' '. $self->phonenum.
588                ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
589                " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
590       }
591     }
592
593   }
594
595   return '';
596
597 }
598
599 =item check_pin
600
601 Checks the supplied PIN against the PIN in the database.  Returns true for a
602 sucessful authentication, false if no match.
603
604 =cut
605
606 sub check_pin {
607   my($self, $check_pin) = @_;
608   length($self->pin) && $check_pin eq $self->pin;
609 }
610
611 =item radius_reply
612
613 =cut
614
615 sub radius_reply {
616   my $self = shift;
617   #XXX Session-Timeout!  holy shit, need rlm_perl to ask for this in realtime
618   ();
619 }
620
621 =item radius_check
622
623 =cut
624
625 sub radius_check {
626   my $self = shift;
627   my %check = ();
628
629   my $conf = new FS::Conf;
630
631   $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
632
633   %check;
634 }
635
636 sub radius_groups {
637   ();
638 }
639
640 =item phone_device
641
642 Returns any FS::phone_device records associated with this service.
643
644 =cut
645
646 sub phone_device {
647   my $self = shift;
648   qsearch('phone_device', { 'svcnum' => $self->svcnum } );
649 }
650
651 #override location_Mixin version cause we want to try the cust_pkg location
652 #in between us and cust_main
653 # XXX what to do in the unlinked case???  return a pseudo-object that returns
654 # empty fields?
655 sub cust_location_or_main {
656   my $self = shift;
657   return $self->cust_location if $self->locationnum;
658   my $cust_pkg = $self->cust_svc->cust_pkg;
659   $cust_pkg ? $cust_pkg->cust_location_or_main : '';
660 }
661
662 =item get_cdrs
663
664 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this 
665 service.  By default, "associated with" means that either the "src" or the 
666 "charged_party" field of the CDR matches the "phonenum" field of the service.
667
668 =over 2
669
670 Accepts the following options:
671
672 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
673
674 =item status => "" (or "processing-tiered", "done"): Return only CDRs with that processing status.
675
676 =item inbound => 1: Return CDRs for inbound calls.  With "status", will filter 
677 on inbound processing status.
678
679 =item default_prefix => "XXX": Also accept the phone number of the service prepended 
680 with the chosen prefix.
681
682 =item begin, end: Start and end of a date range, as unix timestamp.
683
684 =item cdrtypenum: Only return CDRs with this type number.
685
686 =item disable_src => 1: Only match on "charged_party", not "src".
687
688 =item nonzero: Only return CDRs where duration > 0.
689
690 =item by_svcnum: not supported for svc_phone
691
692 =back
693
694 =cut
695
696 sub _search_cdrs { # transitional; replaced with psearch_cdrs in 3.0
697   my($self, %options) = @_;
698   my @fields;
699   my %hash;
700   my @where;
701
702   if ( $options{'inbound'} ) {
703
704     @fields = ( 'dst' );
705     if ( exists($options{'status'}) ) {
706       my $status = $options{'status'};
707       if ( $status ) {
708         push @where, 'EXISTS ( SELECT 1 FROM cdr_termination '.
709           'WHERE cdr.acctid = cdr_termination.acctid '.
710           "AND cdr_termination.status = '$status' ". #quoting kludge
711           'AND cdr_termination.termpart = 1 )';
712       } else {
713         push @where, 'NOT EXISTS ( SELECT 1 FROM cdr_termination '.
714           'WHERE cdr.acctid = cdr_termination.acctid '.
715           'AND cdr_termination.termpart = 1 )';
716       }
717     }
718
719   } else {
720
721     @fields = ( 'charged_party' );
722     push @fields, 'src' if !$options{'disable_src'};
723     $hash{'freesidestatus'} = $options{'status'}
724       if exists($options{'status'});
725   }
726
727   if ($options{'cdrtypenum'}) {
728     $hash{'cdrtypenum'} = $options{'cdrtypenum'};
729   }
730   
731   my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
732
733   my $number = $self->phonenum;
734
735   my $prefix = $options{'default_prefix'};
736
737   my @orwhere =  map " $_ = '$number'        ", @fields;
738   push @orwhere, map " $_ = '$prefix$number' ", @fields
739     if defined($prefix) && length($prefix);
740   if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
741     push @orwhere, map " $_ = '$1$number' ", @fields
742   }
743
744   push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
745
746   if ( $options{'begin'} ) {
747     push @where, 'startdate >= '. $options{'begin'};
748   }
749   if ( $options{'end'} ) {
750     push @where, 'startdate < '.  $options{'end'};
751   }
752   if ( $options{'nonzero'} ) {
753     push @where, 'duration > 0';
754   }
755
756   my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
757
758
759   return {
760       'table'      => 'cdr',
761       'hashref'    => \%hash,
762       'extra_sql'  => $extra_sql,
763       'order_by'   => $options{'billsec_sum'} ? '' : "ORDER BY startdate $for_update",
764       'select'     => $options{'billsec_sum'} ? 'sum(billsec) as billsec_sum' : '*',
765   };
766 }
767
768 sub get_cdrs {
769   qsearch(_search_cdrs(@_));
770 }
771
772 =item sum_cdrs
773
774 Takes the same options as psearch_cdrs, but returns a single row containing
775 "count" (the number of CDRs) and the sums of the following fields: duration,
776 billsec, rated_price, rated_seconds, rated_minutes.
777
778 Note that if any calls are not rated, their rated_* fields will be null.
779 If you want to use those fields, pass the 'status' option to limit to 
780 calls that have been rated.  This is intentional; please don't "fix" it.
781
782 =cut
783
784 sub sum_cdrs {
785   my $self = shift;
786   my $search = $self->_search_cdrs(@_);
787   $search->{'select'} = join(',',
788     'COUNT(*) AS count',
789     map { "SUM($_) AS $_" }
790       qw(duration billsec rated_price rated_seconds rated_minutes)
791   );
792   $search->{'order_by'} = '';
793   qsearchs ( $search );
794 }
795
796 =back
797
798 =head1 BUGS
799
800 =head1 SEE ALSO
801
802 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
803 L<FS::cust_pkg>, schema.html from the base documentation.
804
805 =cut
806
807 1;
808