<?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/item.php';
include_once '../../objects/stock_in.php';
include_once '../../objects/stock_balance.php';

$database = new Database();
$db = $database->getConnection();
 
$item = new Item($db);
$stock_in = new StockIn($db);
$stock_balance = new StockBalance($db);
$data = json_decode(file_get_contents("php://input"));  

$item->item_code = $data->item_code;
$item->price = $data->price;

$stock_in->stock_in_date = $data->stock_in_date;
$stock_in->store = $data->store;
$stock_in->supplier = $data->supplier;
$stock_in->item_code = $data->item_code;
$stock_in->item_name = $data->item_name;
$stock_in->quantity = $data->quantity;
$stock_in->price = $data->price;
$stock_in->remark = $data->remark;
$stock_in->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";
$stock_in->entry_date_time = date("Y-m-d H:i:s");

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

$item->updatePrice();

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

?>