36503 Scalability issues with amcom CDRs
[freeside.git] / FS / FS / cdr / amcom.pm
1 package FS::cdr::amcom;
2
3 use strict;
4 use base qw( FS::cdr );
5 use vars qw( %info %cdrtypes);
6 use DateTime;
7 use FS::Record qw( qsearch );
8 use FS::cdr_type;
9
10 my ($tmp_mday, $tmp_mon, $tmp_year);
11
12 %info = (
13   'name'          => 'Amcom',
14   'weight'        => 500,
15   'header'        => 1,
16   'type'          => 'csv',
17   'sep_char'      => ',',
18   'disabled'      => 0,
19   'header_buffer' => sub {
20
21         %cdrtypes = ( map { $_->cdrtypename => $_->cdrtypenum }
22                              qsearch('cdr_type', {})
23             );
24     },
25
26   #listref of what to do with each field from the CDR, in order
27   'import_fields' => [
28
29     sub {         # 1. Field Type (must be "DCR", yes, "DCR")
30       my ($cdr, $field, $conf, $hashref) = @_;
31       $hashref->{skiprow} = 1 unless $field eq 'DCR';
32     },
33     'accountcode',# 2. BWGroupID (centrex group)
34     sub {         # 3. BWGroupNumber
35       my ($cdr, $field) = @_; #, $conf, $hashref) = @_;
36
37         if ($cdr->accountcode eq '' && $field =~ /^(1800|1300)/){
38         $cdr->charged_party($field);
39         $cdr->accountcode($field);
40         }
41     },
42     'uniqueid',   # 4. Record ID
43     sub {          # 5. Call Category (LOCAL, NATIONAL, FREECALL, MOBILE)
44       my ($cdr, $data) = @_;
45       $data ||= 'none';
46       $cdr->cdrtypenum($cdrtypes{$data} || '');
47       $cdr->set('dcontext', $data);  
48     },
49     sub {         # 6. Start Date (DDMMYYYY
50       my ($cdr, $date) = @_;
51       $date =~ /^(\d{2})(\d{2})(\d{4})$/
52         or die "unparseable date: $date";
53       ($tmp_mday, $tmp_mon, $tmp_year) = ($1, $2, $3);
54     },
55     sub {         # 7. Start Time (HHMMSS)
56       my ($cdr, $time) = @_;
57       $time =~ /^(\d{2})(\d{2})(\d{2})$/
58         or die "unparseable time: $time";
59       my $dt = DateTime->new(
60         year    => $tmp_year,
61         month   => $tmp_mon,
62         day     => $tmp_mday,
63         hour    => $1,
64         minute  => $2,
65         second  => $3,
66         time_zone => 'local',
67       );
68       $cdr->set('startdate', $dt->epoch);
69     },
70     sub {         # 8. Duration (seconds, 3 decimals)
71       my ($cdr, $seconds) = @_;
72       $cdr->set('duration', sprintf('%.0f', $seconds));
73       $cdr->set('billsec', sprintf('%.0f', $seconds));
74     },
75     'src',        # 9. Calling Number
76     'dst',        # 10. Called Number
77     'upstream_src_regionname',  # 11. Calling Party Zone
78     'upstream_dst_regionname',  # 12. Called Party Zone
79     'upstream_price',           # 13. Call Cost
80     '',                         # 14. Call Cost 2 (seems to be the same?)
81     '',           # 15. Service Provider ID
82     ('') x 4,     # 16-20. Reserved fields
83   ],
84 );
85
86 1;