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