summaryrefslogtreecommitdiff
path: root/bin/taqua-accountcode-rewrite
blob: e6d579013419058782877107ea3f47b98098b3bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl

my $usage = "
This script is for fixing CDRs that were supposed to receive Taqua
accountcode/caller ID rewrites but didn't. It will reprocess records that
were already rewritten, so use with caution. Options:

-s DATE: find calls on or after DATE (required)
-e DATE: find calls on or before DATE (optional, defaults to right now)
-v: show records as they're updated
";

use strict;
use FS::Misc::Getopt;
use FS::Record qw(qsearch qsearchs dbh);
use FS::Cursor;
our %opt;
our $DEBUG;

getopts('');

die $usage unless $opt{start};

my $fixed = 0;
my $notfound = 0;
my $failed = 0;
my $extra_sql = 'WHERE lastapp IS NOT NULL '.
                ' AND cdrtypenum = 1'.
                ' AND calldate >= to_timestamp('.$opt{start}.')';
if ( $opt{end} ) {
  $extra_sql .= ' AND calldate < to_timestamp('.$opt{end}.')';
}
my $cursor = FS::Cursor->new({
  table     => 'cdr',
  hashref   => {},
  extra_sql => $extra_sql,
});

$FS::UID::AutoCommit = 0;

while (my $cdr = $cursor->fetch) {
  # copy-paste from cdrrewrited, except:
  # - move all conditions for this rewrite into the SQL
  # - don't check for config option to enable rewriting
  # - don't retry, don't remember not-found acctids

  my @status;

  #find the matching CDR
  my %search = ( 'sessionnum' => $cdr->sessionnum );
  if ( $cdr->lastapp eq 'acctcode' ) {
    $search{'src'} = $cdr->subscriber;
  } elsif ( $cdr->lastapp eq 'CallerId' ) {
    $search{'dst'} = $cdr->subscriber;
  }
  if ($DEBUG) {
    my $desc = '#'.$cdr->acctid . ': sessionnum ' . $cdr->sessionnum .  ', '.
      "subscriber ".$cdr->subscriber;
    warn $desc."\n";
  }
  my $primary = qsearchs('cdr', \%search);

  unless ( $primary ) {

    my $cantfind = "can't find primary CDR with session ". $cdr->sessionnum .
                   ', ';
    if ($search{'src'}) {
      $cantfind .= 'src '.$search{'src'};
    } else {
      $cantfind .= 'dst '.$search{'dst'};
    }
    warn "ERROR: $cantfind\n";
    $notfound++;
    next;

  } else {

    if ( $cdr->lastapp eq 'acctcode' ) {
      # lastdata contains the dialed account code
      $primary->accountcode( $cdr->lastdata );
      push @status, 'taqua-accountcode';
      warn '#'.$primary->acctid . ': accountcode = '.$cdr->lastdata . "\n"
        if $DEBUG;
    } elsif ( $cdr->lastapp eq 'CallerId' ) {
      # lastdata contains "allowed" or "restricted"
      # or case variants thereof
      if ( lc($cdr->lastdata) eq 'restricted' ) {
        $primary->clid( 'PRIVATE' );
      }
      push @status, 'taqua-callerid';
      warn '#'.$primary->acctid . ': clid = '.$cdr->lastdata . "\n"
        if $DEBUG;
    } else {
      warn "unknown Taqua service name: ".$cdr->lastapp."\n";
    }
    #$primary->freesiderewritestatus( 'taqua-accountcode-primary' );
    my $error = $primary->replace if $primary->modified;
    if ( $error ) {
      warn "WARNING: error rewriting primary CDR: $error\n";
      $failed++;
      dbh->rollback;
      next;
    }
    if ( $primary->freesidestatus eq 'done' and
         $cdr->lastapp eq 'acctcode' and
         $primary->rated_price > 0 ) {
      # then have to update the billing detail also
      my $detail;
      if ( $primary->detailnum ) {
        # has been on 3.x since January 2016...
        $detail = qsearchs('cust_bill_pkg_detail', {
          'detailnum' => $primary->detailnum
        });
      } else {
        # otherwise, try our best: find a detail with the right price,
        # source number, and start and end dates
        $detail = qsearchs('cust_bill_pkg_detail', {
          'amount'     => $primary->rated_price,
          'classnum'   => $primary->rated_classnum,
          'duration'   => $primary->rated_seconds,
          'startdate'  => $primary->startdate,
          'format'     => 'C',
          'phonenum'   => $primary->src, # not perfect
        });
      }
      if ($detail) {
        warn "detail #".$detail->detailnum."\n" if $DEBUG;
        $detail->set('accountcode', $primary->accountcode);
        $error = $detail->replace;
        if ($error) {
          warn "WARNING: error rewriting invoice detail: $error\n";
          $failed++;
          dbh->rollback;
          next;
        }
      } else {
        warn "ERROR: can't find invoice detail for cdr#".$primary->acctid."\n";
        $notfound++;
        dbh->rollback;
        next;
      }
    } # if this is an acctcode record and the primary was billed

    $cdr->status('done'); #so it doesn't try to rate

  }

  $cdr->freesiderewritestatus(
    scalar(@status) ? join('/', @status) : 'skipped'
  );

  my $error = $cdr->replace;
  if ( $error ) {
    warn "WARNING: error rewriting CDR: $error\n";
    dbh->rollback;
    $failed++;
  } else {
    dbh->commit;
    $fixed++;
  }
}

print "Finished.
Rewrites: $fixed
Primary record not found: $notfound
Errors: $failed
";