
CREATE TABLE sk_card_data (
  cid integer PRIMARY KEY,
  sid integer NOT NULL default '0',
  pin text NOT NULL default '' UNIQUE,
  cost numeric(16,2) NOT NULL default '0',
  status char(1) NOT NULL default 'a',
  used_by integer NOT NULL default '0',
  used_at text NOT NULL default (datetime('now'))
);

CREATE TABLE sk_card_error (
  error_time text NOT NULL default (datetime('now')) PRIMARY KEY,
  error_user integer NOT NULL default '0',
  error_input text NOT NULL default ''
);

CREATE TABLE sk_card_series (
  sid integer PRIMARY KEY,
  gid integer NOT NULL default '0',
  cost numeric(16,2) NOT NULL default '0.00',
  date_add text NOT NULL default (datetime('now')),
  date_expire text NOT NULL default ('1970-01-01 00:00'),
  url text,
  descr text NOT NULL default '',
  card_type integer NOT NULL default '0',
  admin integer NOT NULL default '0',
  cards_col integer not null default '0'
);

CREATE TABLE sk_group_tracker (
	obj_id integer default '1' NOT NULL,
	gid integer default '0' NOT NULL
);

CREATE TABLE sk_users (
    uid integer PRIMARY KEY,
    username text NOT NULL default '',
    password text NOT NULL default '*',
    auth_info text NOT NULL default '',
    auth_type integer NOT NULL default '0',
    account_id integer NOT NULL default '0',
    gid integer NOT NULL default '1',
    sid integer NOT NULL default '0',
    deposit numeric(16,2) NOT NULL default '0.00',
    credit numeric(16,2) NOT NULL default '0.00',
    add_date text NOT NULL default (datetime('now')),
    tos integer NULL,
    do_with_tos integer NULL,
    direction integer NULL,	
    fixed integer NOT NULL default '0',	
    fixed_cost numeric(16,2) default '0.0',		
    activation_time integer NULL,
    total_time_limit integer NOT NULL default '0',	
    month_time_limit integer NOT NULL default '0',	
    week_time_limit integer NOT NULL default '0',	
    day_time_limit integer NOT NULL default '0',	
    total_traffic_limit integer NOT NULL default '0',
    month_traffic_limit integer NOT NULL default '0',
    week_traffic_limit integer NOT NULL default '0',	
    day_traffic_limit integer NOT NULL default '0',	
    total_money_limit numeric(16,2) NOT NULL default '0.0',
    month_money_limit numeric(16,2) NOT NULL default '0.0',
    week_money_limit numeric(16,2) NOT NULL default '0.0',	
    day_money_limit numeric(16,2) NOT NULL default '0.0',	
    login_time text NULL,
    huntgroup_name text NULL,
    simultaneous_use integer NOT NULL default '1',	
    port_limit integer NOT NULL default '1',	
    session_timeout integer NOT NULL default '0',			
    idle_timeout integer NOT NULL default '0',		
    no_pass integer NOT NULL default '0',		
    no_acct integer NOT NULL default '0',		
    allow_callback integer NOT NULL default '0',	
    other_params text NULL,
    allowed_servers text NULL,
    blocked integer NOT NULL default '1',
    activated integer NOT NULL default '0',
    expired text NOT NULL default ('1970-01-01 00:00'),
    total_time integer NOT NULL default '0',	
    total_traffic integer NOT NULL default '0',
    total_money numeric(16,2) NOT NULL default '0.00',
    last_connection text NOT NULL default '1970-01-01 00:00:00',
    framed_ip text NOT NULL default '',	
    framed_mask text NOT NULL default '',
    framed_route text NULL,
    callback_number text NOT NULL default '',
    up integer NOT NULL default '0',
    down integer NOT NULL default '0',
    ippool_name text NULL,
    last_info text NULL,
    res_gid integer NOT NULL default '0'
);

CREATE TABLE sk_actions (
    username text NOT NULL default '',
    uid integer NOT NULL default '0',
    gid integer NOT NULL default '0',
    id text NOT NULL default '',		
    unique_id text NOT NULL default '' UNIQUE,
    connect_info text default NULL,
    time_on integer NOT NULL default '0',
    start_time text NOT NULL default (datetime('now')),
    stop_time text NULL,
    in_bytes integer NOT NULL default '0',	
    out_bytes integer NOT NULL default '0',	
    ip text NULL,
    server text NULL,
    client_ip text NULL,
    port integer NOT NULL default '0',
    call_to text NOT NULL default '',
    call_from text NOT NULL default '',
    protocol text NULL,
    terminate_cause text NOT NULL default '',
    last_change integer NOT NULL default '0',
    hour_in_bytes integer NOT NULL default '0',
    hour_out_bytes integer NOT NULL default '0',
    hour_traffic_money numeric(16,2) NOT NULL default '0.00',
    before_billing numeric(16,2) NOT NULL default '0.00',
    billing_minus numeric(16,2) NOT NULL default '0.00'
);

CREATE TABLE sk_packets (
    gid integer PRIMARY KEY,
    packet text NOT NULL default '',
    deposit numeric(16,2) NOT NULL default '0.00',
    credit numeric(16,2) NOT NULL default '0.00',
    tos integer NOT NULL default '0',
    do_with_tos integer NOT NULL default '1',
    direction integer NOT NULL default '0',
    fixed integer NOT NULL default '0',
    fixed_cost numeric(16,2) NOT NULL default '0.00',
    activated integer NOT NULL default '0',
    activation_time integer NOT NULL default '2678400',
    blocked integer NOT NULL default '0',
    total_time_limit integer NOT NULL default '0',
    month_time_limit integer NOT NULL default '0',
    week_time_limit integer NOT NULL default '0',
    day_time_limit integer NOT NULL default '0',
    total_traffic_limit integer NOT NULL default '0',
    month_traffic_limit integer NOT NULL default '0',
    week_traffic_limit integer NOT NULL default '0',
    day_traffic_limit integer NOT NULL default '0',
    total_money_limit numeric(16,2) NOT NULL default '0.00',
    month_money_limit numeric(16,2) NOT NULL default '0.00',
    week_money_limit numeric(16,2) NOT NULL default '0.00',
    day_money_limit numeric(16,2) NOT NULL default '0.00',
    login_time text NOT NULL default '',
    huntgroup_name text NOT NULL default '',
    simultaneous_use integer NOT NULL default '1',
    port_limit integer NOT NULL default '0',
    session_timeout integer NOT NULL default '0',
    idle_timeout integer NOT NULL default '0',
    framed_ip text NOT NULL default '',
    framed_mask text NOT NULL default '',
    no_pass integer NOT NULL default '0',
    no_acct integer NOT NULL default '0',
    allow_callback integer NOT NULL default '0',
    other_params text NULL,
    framed_route text NULL,
    allowed_servers text NULL,
    up integer not null default '0',
    down integer not null default '0',
    cos integer not null default '0',
    ippool_name text NULL,
    users_col integer not null default '0',
    service_set_id integer not null default '1'
);

CREATE TABLE sk_prices (
    gid integer NOT NULL default '1',
    week_day integer NOT NULL default '0',
    h0 numeric(10,6) NOT NULL default '0.000000',
    input0 numeric(10,6) NOT NULL default '0.000000',
    output0 numeric(10,6) NOT NULL default '0.000000',
    h1 numeric(10,6) NOT NULL default '0.000000',
    input1 numeric(10,6) NOT NULL default '0.000000',
    output1 numeric(10,6) NOT NULL default '0.000000',
    h2 numeric(10,6) NOT NULL default '0.000000',
    input2 numeric(10,6) NOT NULL default '0.000000',
    output2 numeric(10,6) NOT NULL default '0.000000',
    h3 numeric(10,6) NOT NULL default '0.000000',
    input3 numeric(10,6) NOT NULL default '0.000000',
    output3 numeric(10,6) NOT NULL default '0.000000',
    h4 numeric(10,6) NOT NULL default '0.000000',
    input4 numeric(10,6) NOT NULL default '0.000000',
    output4 numeric(10,6) NOT NULL default '0.000000',
    h5 numeric(10,6) NOT NULL default '0.000000',
    input5 numeric(10,6) NOT NULL default '0.000000',
    output5 numeric(10,6) NOT NULL default '0.000000',
    h6 numeric(10,6) NOT NULL default '0.000000',
    input6 numeric(10,6) NOT NULL default '0.000000',
    output6 numeric(10,6) NOT NULL default '0.000000',
    h7 numeric(10,6) NOT NULL default '0.000000',
    input7 numeric(10,6) NOT NULL default '0.000000',
    output7 numeric(10,6) NOT NULL default '0.000000',
    h8 numeric(10,6) NOT NULL default '0.000000',
    input8 numeric(10,6) NOT NULL default '0.000000',
    output8 numeric(10,6) NOT NULL default '0.000000',
    h9 numeric(10,6) NOT NULL default '0.000000',
    input9 numeric(10,6) NOT NULL default '0.000000',
    output9 numeric(10,6) NOT NULL default '0.000000',
    h10 numeric(10,6) NOT NULL default '0.000000',
    input10 numeric(10,6) NOT NULL default '0.000000',
    output10 numeric(10,6) NOT NULL default '0.000000',
    h11 numeric(10,6) NOT NULL default '0.000000',
    input11 numeric(10,6) NOT NULL default '0.000000',
    output11 numeric(10,6) NOT NULL default '0.000000',
     h12 numeric(10,6) NOT NULL default '0.000000',
    input12 numeric(10,6) NOT NULL default '0.000000',
    output12 numeric(10,6) NOT NULL default '0.000000',
    h13 numeric(10,6) NOT NULL default '0.000000',
    input13 numeric(10,6) NOT NULL default '0.000000',
    output13 numeric(10,6) NOT NULL default '0.000000',
    h14 numeric(10,6) NOT NULL default '0.000000',
    input14 numeric(10,6) NOT NULL default '0.000000',
    output14 numeric(10,6) NOT NULL default '0.000000',
    h15 numeric(10,6) NOT NULL default '0.000000',
    input15 numeric(10,6) NOT NULL default '0.000000',
    output15 numeric(10,6) NOT NULL default '0.000000',
    h16 numeric(10,6) NOT NULL default '0.000000',
    input16 numeric(10,6) NOT NULL default '0.000000',
    output16 numeric(10,6) NOT NULL default '0.000000',
    h17 numeric(10,6) NOT NULL default '0.000000',
    input17 numeric(10,6) NOT NULL default '0.000000',
    output17 numeric(10,6) NOT NULL default '0.000000',
    h18 numeric(10,6) NOT NULL default '0.000000',
    input18 numeric(10,6) NOT NULL default '0.000000',
    output18 numeric(10,6) NOT NULL default '0.000000',
    h19 numeric(10,6) NOT NULL default '0.000000',
    input19 numeric(10,6) NOT NULL default '0.000000',
    output19 numeric(10,6) NOT NULL default '0.000000',
    h20 numeric(10,6) NOT NULL default '0.000000',
    input20 numeric(10,6) NOT NULL default '0.000000',
    output20 numeric(10,6) NOT NULL default '0.000000',
    h21 numeric(10,6) NOT NULL default '0.000000',
    input21 numeric(10,6) NOT NULL default '0.000000',
    output21 numeric(10,6) NOT NULL default '0.000000',
    h22 numeric(10,6) NOT NULL default '0.000000',
    input22 numeric(10,6) NOT NULL default '0.000000',
    output22 numeric(10,6) NOT NULL default '0.000000',
    h23 numeric(10,6) NOT NULL default '0.000000',
    input23 numeric(10,6) NOT NULL default '0.000000',
    output23 numeric(10,6) NOT NULL default '0.000000',
    servers text NULL,
    price_type integer NOT NULL default '0',
    uid integer NOT NULL default '0'
);

CREATE TABLE sk_radlog (
    insertion_time text default (datetime('now')),
    rad_time text,
    rad_msg text 
);

DROP VIEW IF EXISTS sk_online;
CREATE VIEW IF NOT EXISTS sk_online AS SELECT * FROM sk_actions WHERE Terminate_Cause = 'Online' ORDER BY uid;

DROP VIEW IF EXISTS sk_expired_1h;
create view IF NOT EXISTS sk_expired_1h as select sk_users.uid as uid, sk_users.username as username, sk_packets.packet as packet, 
sk_users.deposit as deposit, sk_users.credit as credit, sk_users.expired as expired, 
sk_users.last_connection as last_connection, sk_users.total_time as total_time, 
sk_users.total_traffic as total_traffic, sk_users.total_money as total_money, 
sk_users.blocked as blocked, sk_users.gid as gid, sk_packets.service_set_id as service_set_id 
from sk_users, sk_packets where sk_users.gid=sk_packets.gid and (sk_users.total_time_limit=3600 or sk_packets.total_time_limit=3600);

DROP VIEW IF EXISTS sk_expired_24h;
create view IF NOT EXISTS sk_expired_24h as select sk_users.uid as uid, sk_users.username as username, sk_packets.packet as packet, 
sk_users.deposit as deposit, sk_users.credit as credit, sk_users.expired as expired, 
sk_users.last_connection as last_connection, sk_users.total_time as total_time, 
sk_users.total_traffic as total_traffic, sk_users.total_money as total_money, 
sk_users.blocked as blocked, sk_users.gid as gid, sk_packets.service_set_id as service_set_id 
from sk_users, sk_packets where sk_users.gid=sk_packets.gid and (sk_users.total_time_limit=86400 or sk_packets.total_time_limit=86400);

DROP VIEW IF EXISTS sk_stat_month_uid;
CREATE VIEW IF NOT EXISTS sk_stat_month_uid AS SELECT uid, user, sum(time_on) as time_on, sum(in_bytes) as in_bytes, sum(out_bytes) as out_bytes, sum(billing_minus) as billing_minus FROM sk_actions WHERE strftime('%Y-%m', start_time) = strftime('%Y-%m', 'now') GROUP BY sk_actions.uid;
DROP VIEW IF EXISTS sk_stat_week_uid;
CREATE VIEW IF NOT EXISTS sk_stat_week_uid AS SELECT uid, user, sum(time_on) as time_on, sum(in_bytes) as in_bytes, sum(out_bytes) as out_bytes, sum(billing_minus) as billing_minus FROM sk_actions WHERE strftime('%Y-%W', start_time) = strftime('%Y-%W', 'now') GROUP BY sk_actions.uid;
DROP VIEW IF EXISTS sk_stat_day_uid;
CREATE VIEW IF NOT EXISTS sk_stat_day_uid AS SELECT uid, user, sum(time_on) as time_on, sum(in_bytes) as in_bytes, sum(out_bytes) as out_bytes, sum(billing_minus) as billing_minus FROM sk_actions WHERE strftime('%Y-%j', start_time) = strftime('%Y-%j', 'now') GROUP BY sk_actions.uid;

DROP VIEW IF EXISTS sk_enabled_packets;
CREATE VIEW IF NOT EXISTS sk_enabled_packets AS SELECT sk_packets.gid AS gid FROM sk_packets, sk_service_set, sk_providers 
WHERE sk_packets.service_set_id = sk_service_set.service_set_id AND sk_service_set.provider_id = sk_providers.provider_id AND sk_providers.role='P' AND sk_providers.active='Y'
EXCEPT SELECT sk_packets.gid AS gid FROM sk_packets, sk_group_tracker, sk_providers
WHERE sk_packets.gid=sk_group_tracker.gid AND sk_group_tracker.obj_id=sk_providers.obj_id AND sk_providers.role='R' AND sk_providers.active='N';

DROP VIEW IF EXISTS sk_enabled_users;
CREATE VIEW IF NOT EXISTS sk_enabled_users AS
SELECT sk_users.username as username, sk_users.gid as gid, sk_users.auth_type as auth_type, sk_users.password as password, sk_users.auth_info as auth_info
FROM sk_users, sk_enabled_packets WHERE sk_users.gid=sk_enabled_packets.gid;

DROP VIEW IF EXISTS sk_authorize_users;
CREATE VIEW IF NOT EXISTS sk_authorize_users AS
SELECT sk_users.user, sk_users.password, sk_users.auth_type, sk_users.uid, sk_users.gid, sk_users.deposit, sk_users.credit, 
strftime('%%s', sk_users.add_date) as add_date, sk_users.blocked, sk_users.activated, strftime('%%s', sk_users.expired) as expired,
 sk_users.total_time, sk_users.total_traffic, sk_users.total_money, strftime('%%s', sk_users.last_connection) as last_connection, 
sk_users.framed_ip, sk_users.framed_mask, sk_users.callback_number, sk_users.tos, sk_users.do_with_tos, sk_users.direction, sk_users.fixed, 
sk_users.fixed_cost, sk_users.activation_time, sk_users.total_time_limit, sk_users.month_time_limit, sk_users.week_time_limit, 
sk_users.day_time_limit, sk_users.total_traffic_limit, sk_users.month_traffic_limit, sk_users.week_traffic_limit, sk_users.day_traffic_limit, 
sk_users.total_money_limit, sk_users.month_money_limit, sk_users.week_money_limit, sk_users.day_money_limit, sk_users.huntgroup_name, 
sk_users.simultaneous_use, sk_users.port_limit, sk_users.session_timeout, sk_users.idle_timeout, sk_users.ippool_name, sk_users.allow_callback, 
sk_users.other_params, sk_users.auth_info, sk_users.up, sk_users.down, sk_packets.service_set_id FROM sk_users, sk_packets 
WHERE sk_packets.gid=sk_users.gid;
