<?php
function insertDBNumber($userId,$userSender,$numbers,$msg,$type,$msgUnique,$numbersNotAccepted=0,$countOfNotExpted=0,$excel=0,$api=0,$viewTime=0)
{
	global $spilter;
	global $conn;
	$randomGroup = rand(1,10);
	if($spilter == 1)
		$newSpilter = 10000;
	else
		$newSpilter = $spilter;
	$numbersArray = explode(",",$numbers.',');
	$arrayNumbers = array_filter($numbersArray);
	$allNumberArray = @array_chunk($arrayNumbers, $newSpilter);
	$countNumberArray = count($allNumberArray);
	if(!empty($countNumberArray))
	foreach( $allNumberArray AS $keyArray => $vallNumArray )
	{
		$countNumbersarray = count($vallNumArray); 
		$tempNumber = implode(',', $vallNumArray);
		$msgMd5 = $md5STR = $userId.$msg.$tempNumber.$userSender.date('H').date('m').date('d').date('Y');
		$msgKey = md5($msgMd5);
		$getMsgLen = getMsgLen($msg);
		$points = getPoints($tempNumber,$msg);
		$mysqlError = "";
		if(mysqli_query($conn,"START TRANSACTION"))
		{
			$resultAccount1 = mysqli_query($conn,"SELECT `account` FROM `user_login` WHERE `id`='".$userId."'");
			$rowAccount1 = mysqli_fetch_assoc($resultAccount1);
			$accountBeforSend=$rowAccount1["account"];
			
			$queryAccountUpdate = "UPDATE `user_login` SET `account`=`account`-".ceil($points)." WHERE `id`=$userId";
			$resultAccountUpdate = mysqli_query($conn,$queryAccountUpdate);
			$mysqlError.= mysqli_error($conn);
			
			$resultAccount2 = mysqli_query($conn,"SELECT `account` FROM `user_login` WHERE `id`='".$userId."'");
			$rowAccount2 = mysqli_fetch_assoc($resultAccount2);
			$accountAfterSend=$rowAccount2["account"];
			if(!empty($resultAccountUpdate)){
				$resultMsgIdHistory = mysqli_query($conn,"SELECT 1 FROM `history` WHERE `idUser`='".$userId."' AND `msgKey`='".$msgKey."' LIMIT 0 , 1 ");
				$numMsgIdHistory = mysqli_num_rows($resultMsgIdHistory);
				$mysqlError.= mysqli_error($conn);
				
				$resultMsgIdTemp = mysqli_query($conn,"SELECT 1 FROM `msg_send_temp` WHERE `userId`='".$userId."' AND `msgKey`='".$msgKey."' LIMIT 0 , 1 ");
				$numMsgIdTemp = mysqli_num_rows($resultMsgIdTemp);
				$mysqlError.= mysqli_error($conn);
				$insertId = 0;
				if(empty($numMsgIdHistory) && empty($numMsgIdTemp)){
					$querySedMsgTemp = "INSERT INTO `msg_send_temp` (numbers,msg,userSender,userId,countNumbers,numMsgs,points,msgKey,msgUnique,msgType,accountBeforSend,accountAfterSend,`numbersNotAccepted`,`countNotAccepted`,`insert_date`,`type`,`groupSend`,`excel`,`api`) VALUES ";
					$querySedMsgTemp.= " ('".mysqli_real_escape_string($conn, $tempNumber)."','".mysqli_real_escape_string($conn, $msg)."','".mysqli_real_escape_string($conn, $userSender)."','".$userId."','".$countNumbersarray."','".$getMsgLen."','".$points."','".$msgKey."','".$msgUnique."','1','".$accountBeforSend."','".$accountAfterSend."','".$numbersNotAccepted."','".$countOfNotExpted."','".time()."','".$type."','".$randomGroup."','".$excel."','".$api."')";
					$resultSedMsgTemp = mysqli_query($conn,$querySedMsgTemp);
					$mysqlError.= mysqli_error($conn);
					if(empty($mysqlError))
						$insertId = mysqli_insert_id($conn);
					
				}
			}
			if(!empty($mysqlError))
				mysqli_query($conn, "ROLLBACK");
			else{
				mysqli_query($conn, "COMMIT");
				if($api==1)
					return $insertId;
			}
		}
	}
	return $insertId;
}
?>