<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/purchase.php';
include_once '../objects/purchase_detail.php';

session_start();
date_default_timezone_set('Asia/Bangkok'); 

$database = new Database();
$db = $database->getConnection();

$purchase = new Purchase($db);
$purchase_detail = new Purchase_Detail($db);

$data = json_decode(file_get_contents("php://input"));

$purchase->id = $data->id;
$purchase_detail->purchase_id = $data->id;

$purchase->is_deleted = $_SESSION['user'] . " " . date("Y-m-d H:i:s");
$purchase_detail->is_deleted = $purchase->is_deleted;

if($purchase->delete()){
	if($purchase_detail->delete()){
		$arr = array(
			"message" => "deleted",
		);
	}else{
		$arr = array(
			"message" => "errpr pd",
		);
		die();
	}
}else{
	$arr = array(
		"message" => "errpr p",
	);
	die();
}

echo json_encode($arr);
?>