<?php
ini_set("display_errors", 1);
ini_set("error_reporting", 1);

$dsn = "sqlsrv:server=localhost;Database=tok;";
$username = "SYSDBA";
$password = "masterkey";
$pdo = new PDO($dsn, $username, $password);

//產生 UUID
function createUUID(){
	return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
		mt_rand(0, 0xffff), mt_rand(0, 0xffff),
		mt_rand(0, 0xffff),
		mt_rand(0, 0x0fff) | 0x4000,
		mt_rand(0, 0x3fff) | 0x8000,
		mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
	);
}

//寫入傳送紀錄
function saveUpload($file_name="", $encData="", $supplierInventory="", $empNo="", $pdo=null){
	try{
		$sql = "
		SET ANSI_WARNINGS OFF;
		INSERT INTO [tok].[dbo].[UPLOADDATA]([ERI], [FILE_NAME], [ENCDATA], [SUPPLIERINVENTORY], [USER_ID], [CREATE_TIME])
		VALUES(dbo.SOPHIAERI('UPLOADDATA'), '".$file_name."', '".$encData."', '".$supplierInventory."', '".$empNo."', '".date("Y-m-d H:i:s")."')
	";
		$query = $pdo->prepare($sql);
		$query->execute();
	}catch(PDOException $e){
		echo json_encode(array("msg"=>$e->getMessage()));
	}
}

//寫入回傳訊息
function saveResponse($file_name="", $response="", $empNo="", $pdo=null){
	try{
		$sql = "
		SET ANSI_WARNINGS OFF;
		INSERT INTO [tok].[dbo].[RESPONSEDATA]([ERI], [FILE_NAME], [CONTENTS], [USER_ID], [CREATE_TIME])
		VALUES(dbo.SOPHIAERI('RESPONSEDATA'), '".$file_name."', '".$response."', '".$empNo."', '".date("Y-m-d H:i:s")."')
	";
		$query = $pdo->prepare($sql);
		$query->execute();
	}catch(PDOException $e){
		echo json_encode(array("msg"=>$e->getMessage()));
	}
}
?>