summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/dialplan/cdr.lua
blob: 55a78894787578fba79de49b34a4de06725d7fed (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
-- Gemeinschaft 5 module: cdr class
-- (c) AMOOMA GmbH 2012
-- 

module(...,package.seeall)

Cdr = {}

local DEFAULT_MEMBER_TIMEOUT = 20;

-- Create Cdr object
function Cdr.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.log = arg.log;
  self.database = arg.database;
  return object;
end


function Cdr.save(self, caller, destination)
  require 'common.str'
  local cdr = {}
  cdr.uuid = common.str.to_sql(caller.uuid);
  cdr.bleg_uuid = common.str.to_sql(caller:to_s('bridge_uuid'));
  cdr.dialed_number = common.str.to_sql(caller.called_number);
  cdr.destination_number = common.str.to_sql(destination.number);
  cdr.caller_id_number = common.str.to_sql(caller:to_s('effective_caller_id_number'));
  cdr.caller_id_name = common.str.to_sql(caller:to_s('effective_caller_id_name'));
  cdr.callee_id_number = common.str.to_sql(caller:to_s('effective_callee_id_number'));
  cdr.callee_id_name = common.str.to_sql(caller:to_s('effective_callee_id_name'));
  cdr.start_stamp = 'FROM_UNIXTIME(' .. math.floor(caller:to_i('created_time') / 1000000) .. ')';
  cdr.answer_stamp = 'FROM_UNIXTIME(' .. math.floor(caller:to_i('answered_time') / 1000000) .. ')';
  cdr.end_stamp = 'NOW()';
  cdr.duration = 'UNIX_TIMESTAMP(NOW()) - ' .. math.floor(caller:to_i('created_time') / 1000000);
  cdr.hangup_cause = common.str.to_sql(caller.session:hangupCause());
  cdr.dialstatus = common.str.to_sql(caller:to_s('DIALSTATUS'));
  cdr.forwarding_number = common.str.to_sql(caller.forwarding_number);
  cdr.forwarding_service = common.str.to_sql(caller.forwarding_service);

  if caller.auth_account then
    cdr.forwarding_account_id = common.str.to_sql(caller.auth_account.id);
    cdr.forwarding_account_type = common.str.to_sql(caller.auth_account.class);
  end

  if caller.account then
    cdr.account_id = common.str.to_sql(caller.account.id);
    cdr.account_type = common.str.to_sql(caller.account.class);
  end

  if caller:to_i('answered_time') > 0 then
    cdr.billsec = 'UNIX_TIMESTAMP(NOW()) - ' .. math.floor(caller:to_i('answered_time') / 1000000);
  end

  cdr.bleg_account_id =   common.str.to_sql(tonumber(destination.id));
  cdr.bleg_account_type = common.str.to_sql(destination.type);

  local keys = {}
  local values = {}

  for key, value in pairs(cdr) do
    table.insert(keys, key);
    table.insert(values, value);
  end

  local sql_query = 'INSERT INTO `cdrs` (`' .. table.concat(keys, "`, `") .. '`) VALUES (' .. table.concat(values, ", ") .. ')';
  self.log:info('CDR_SAVE - caller: ', cdr.account_type, '=', cdr.account_id, ', callee: ',cdr.bleg_account_type, '=', cdr.bleg_account_id,', dialstatus: ', cdr.dialstatus);
  return self.database:query(sql_query);
end