<?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");
session_start();
date_default_timezone_set('Asia/Rangoon');
include_once '../../config/database.php';
include_once '../../objects/stock_out.php';
include_once '../../objects/stock_balance.php';

$database = new Database();
$db = $database->getConnection();
 
$stock_out = new StockOut($db);
$stock_balance = new StockBalance($db);
$data = json_decode(file_get_contents("php://input"));  

$stock_out->stock_out_date = $data->stock_out_date;
$stock_out->store = $data->store;
$stock_out->stock_out_by = $data->stock_out_by;
$stock_out->item_code = $data->item_code;
$stock_out->item_name = $data->item_name;
$stock_out->quantity = $data->quantity;
$stock_out->status = $data->status;
$stock_out->remark = $data->remark;
$stock_out->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";
$stock_out->entry_date_time = date("Y-m-d H:i:s");

$stock_balance->stock_balance_date = $data->stock_out_date;
$stock_balance->store = $data->store;
$stock_balance->item_code = $data->item_code;
$stock_balance->item_name = $data->item_name;
$stock_balance->quantity = 0;

if($stock_out->create()){
    if($stock_balance->checkMaterial()){
        echo 'created';
   }else{
       if($stock_balance->createSB()){
           echo 'created';
       }    
   }
}else{
    echo 'error';
}

?>