#!/usr/bin/php -q
<?php

require ("config.php");
require ("function.php");

global $agi, $prompt, $minmoney, $agidir;

$phone = $agi->request['agi_callerid'];
$phonefull = "84$phone";

$result = query("SELECT time_remaining FROM customer_viettel WHERE phone_number='$phonefull'");
$row = mysql_fetch_array($result); 
$time_remaining = $row['time_remaining'];

$channel = getvar('CDR(channel)');
$duration = getvar('CDR(duration)');
$start = getvar('CDR(start)');
$uid = getvar('CDR(uniqueid)');
$timestamp = strtotime($start);
$timestampplus = $timestamp + $time_remaining;
$timeexpire = date("Y-m-d H:i:s",$timestampplus);

### if there is no argurment, go to main
$package = check_package($phone);

### update time remaining
if($argv[1] == 'update')
{
	$logcharging='Logcharge_minute_'.date("Ym");
	$tblCallLog='tblCallLog'.date("Ym");
	
	$result = query("SELECT time_remaining FROM customer_viettel WHERE phone_number='$phonefull'");
	$row = mysql_fetch_array($result);
	$time_remaining = $row['time_remaining'];
	$keystr = getvar('keystr');

	query("DELETE FROM smsgw.Charge_minute WHERE Phone_number='$phonefull'");
	query("INSERT INTO $tblCallLog (StartTime,Duration,ServiceID,ProviderID,KeyPress,PhoneNumber,Package,EndTime,Uniqueid) VALUES ('$start',$duration,'3401','ICOM','$keystr',$phonefull,'$package',now(),$uid)");

	$result = query("SELECT count(1) count from smsgw.$logcharging WHERE Phone_number='$phonefull' AND Time_charge>'$start' AND Amount='70' AND Error_code='0'");
        $row = mysql_fetch_array($result);
	$count = $row['count'];

	$time = $time_remaining+$count*60-$duration;
	$timenew = ($time<=0)?0:$time;
	$sql="UPDATE customer_viettel set time_remaining='$timenew' where phone_number='$phonefull'"; query($sql);
	verbose("+--------------------------------------------------------------------------------------------------------------------------------+");
	verbose("+--------+ UPDATE: $phonefull +---+ PHIM BAM $keystr +---+ CHARGE $count +---+ DURATION $duration +---+ TIME REMAINING $timenew +-----------+");
	verbose("+--------------------------------------------------------------------------------------------------------------------------------+");
	exit();
}

if($package == '0')
{
	verbose("******************************************* DANGKY: Dang ky dich vu Viettel Radio $phonefull ***********************************");
	$count = 0;
	while($count < 6)
	{
        	$keypress = keypress("$prompt/dk_no",1,3);
		switch($keypress)
		{
			case '1':
			{
				verbose("Dang ky dich vu theo Ngay");
				return register(1,"$prompt/dk_ngay");
				break;
			}
			case '2':
			{
				verbose("Dang ky dich vu theo Thang");
				return register(2,"$prompt/dk_thang");
				break;
			}
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			{
				play("hd_sai");
				$count++;
				break;
			}
			default:
			{
				play("hd_bam");
				$count++;
                	        break;
			}
		}
	}
	if ($count>=6) $agi->hangup();
}
else
{
	verbose("+---------------------------------------------------------------------------------------------------------------------------------+");
	verbose("+-----------+ MAIN: $phonefull +------+ PACKAGE: $package +------+ START: $start +------+ REMAINING: $time_remaining +---------+");
	verbose("+---------------------------------------------------------------------------------------------------------------------------------+");
        query("INSERT INTO smsgw.Charge_minute(Phone_number,Channel,Time_charge,Amount,Status,Note) VALUES('$phonefull','$channel',DATE_ADD(NOW(),INTERVAL $time_remaining second),'70','0','')");
	return main();
}

$agi->hangup();
?>
