<?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/coa.php';
date_default_timezone_set('Asia/Rangoon');
session_start();

$database = new Database();
$db = $database->getConnection();
 
$coa = new COA($db);
$data = json_decode(file_get_contents("php://input"));

$coa->type_of_expense = $data->type_of_expense;	
$coa->account_type = $data->account_type;	
$coa->account_code = $data->account_code;
$coa->account_head = $data->account_head;
$coa->debit = $data->debit;	
$coa->credit = $data->credit;		
$coa->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";	
$coa->entry_date_time = date("Y-m-d H:i:s");	

if($data->id){
	$coa->id = $data->id;
	if($coa->update()){
		$msg_arr = array(
			"message" => "updated"
		);
	}else{
		$msg_arr = array(
			"message" => "errorIU"
		);
	}
}else{
	if($coa->exist()){
			$msg_arr = array(
				"message" => "duplicate"
			);
		}else if($coa->create()){
			$msg_arr = array(
				"message" => "created"
			);
		}else{
			$msg_arr = array(
				"message" => "errorc"
			);	
		}
}

echo json_encode($msg_arr);
?>