<?php
$bill_date  = strtotime($_POST['bill_date']);
$product_id = intval($_POST['product_id']);
$qty        = intval($_POST['qty']);
$note       = $_POST['note'];

if($bill_date<strtotime('-2 years')){$error=fl();setMessage(63,'date');}
elseif($bill_date>strtotime('2 years')){$error=fl();setMessage(63,'date');}
elseif($qty<=0){$error=fl();setMessage(63,'quantity');}
elseif($note==''){$error=fl();setMessage(36,'note');}
if(!isset($error)){
    $p=$smt->productInfoByID($product_id);
    if(empty($p)){$error=fl();setMessage(63,'product');}
}

if(!isset($error)){
    if($bill_date==TODAY_TIME){
        $bill_date=TIME;
    }
    $total=$p['unit_cost']*$qty;
    $data=[
        'product_id'=> $product_id,
        'quantity'  => $qty,
        'date'      => $bill_date,
        'unit_cost' => $p['unit_cost'],
        'total'     => $total,
        'note'      => $note,
    ];
    $db->arrayUserInfoAdd($data);

    $db->transactionStart();

    $reject_id=$db->insert('reject_products',$data,true);
    if($reject_id){

        $code=$db->setAutoCode('reject',$reject_id);


        $reject_head=$acc->getSystemHead(AH_PRODUCT_REJECT);
        if($reject_head==false){$error=fl();setMessage(66);}
        $process_head=$acc->getSystemHead(AH_PRODUCT_REJECT_PROCESS);
        if($process_head==false){$error=fl();setMessage(66);}

        if(!isset($error)){
            $nextQty=$p['stock'];
            $log=$smt->productStockChangeLog($product_id,$qty,ST_CH_REJECT,$reject_id,$bill_date);
            if($log==false){$error=fl();setMessage(66);}
            $update=$smt->update_product_closing_stock($product_id,$jArray);
            if($update==false){$error=fl();setMessage(66);}
            // $nextQty-=$qty;
            // $where=['id'=>$product_id];
            // $update=$db->update('products',['stock'=>$nextQty],$where);
            // if($update==false){$error=fl();setMessage(66);}            
        }
        if(!isset($error)){
            $voucher=$acc->voucher_create(V_T_PRODUCT_REJECT,$total,$reject_head,$process_head,$bill_date,"Reject product $code",$reject_id,0,[],$jArray);
            if($voucher==false){$error=fl();setMessage(66);}
        }
    }
    else{
        $error=fl();setMessage(66);
    }

    if(!isset($error)){
        $ac=true;
        $jArray['status']=1;
        setMessage(2,'Reject entry successfully');
    }
    else{
        $ac=false;
    }
    $db->transactionStop($ac);
}
