<?php
ini_set('display_errors', 1);

require_once '../../../resources/base_path_include.php';
// should we not call header and ch
//valdation file
require_once $base_path_to_resources . "includes/config.php"; // use require for mandatory files
require_once $base_path_to_resources . 'lib/function.php';
require_once $base_path_to_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_resources . "lib/currency_functions.php";
require_once $base_path_to_resources . 'lib/validate_function.php';
require_once $base_path . 'user_management/authenticate.php';

//valdation file
//require_once $base_path . 'modules/accounting/groups_ledgers/functions/ledger_functions.php';


$postdata = json_decode(file_get_contents("php://input"), true);
checkIfNoPhpContentsReceive();

if ( !isset($postdata['stock_id']) or !checkIfIssetAndNotEmpty($postdata['stock_id']) or !isset($postdata['company_id']) or !checkIfIssetAndNotEmpty($postdata['company_id']) or  !isset($postdata['branch_id']) or !checkIfIssetAndNotEmpty($postdata['branch_id']) or
!isset($postdata['book_id']) or !checkIfIssetAndNotEmpty($postdata['book_id'])  or  !isset($postdata['api_signature']) or !checkIfIssetAndNotEmpty($postdata['api_signature']) or
!isset($postdata['function_type']) or !checkIfIssetAndNotEmpty($postdata['function_type']) or !isset($postdata['jwt_token']) or !checkIfIssetAndNotEmpty($postdata['jwt_token'])) {
    errorreturn("All fields are required.");
}

validateApiSignature($postdata['api_signature']);
//authenticate jwt token
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
//then workspace_id is decoded in data of jwt use from there
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$role = $jwt_validate->data->role;
$username = $jwt_validate->data->username;


// either the user has to fill gst or pan or ledgerhead 
function deleteStockDetails($conn, $stock_id, $workspace_id, $company_id, $branch_id, $book_id)
{
    
    $sql_stock = "SELECT count(*) as total FROM inventory_stocktransaction WHERE workspace_id = ? and company_id =? and branch_id =? and book_id =? and stock_id = ? ";
    $stmt_stock = $conn->prepare($sql_stock);
    $stmt_stock->execute(array($workspace_id, $company_id, $branch_id, $book_id, $stock_id));
    $result_stock = $stmt_stock->fetchAll();
    if ($result_stock[0]['total'] >= 1) {

        errorreturn("Unable to delete stock because of the presence in stock transaction.");
    }
    
    $sql_stock = "SELECT count(*) as total FROM inventory_batch_details WHERE workspace_id = ? and company_id =? and branch_id =? and book_id =? and stock_id = ? ";
    $stmt_stock = $conn->prepare($sql_stock);
    $stmt_stock->execute(array($workspace_id, $company_id, $branch_id, $book_id, $stock_id));
    $result_stock = $stmt_stock->fetchAll();
    if ($result_stock[0]['total'] >= 1) {

        errorreturn("Unable to delete stock because of the presence in batch details.");
    }
    
    
    $sql = "DELETE FROM inventory_stockdetails WHERE id = ? and workspace_id = ? and company_id =? and branch_id =? and book_id =?";
    $stmt = $conn->prepare($sql);
    $stmt->execute(array($stock_id, $workspace_id, $company_id, $branch_id, $book_id));
    $count = $stmt->rowCount();
    if ($count == 1) {
        return "Delete stock Successfully";
    } else {
        errorreturn("unable to delete stock");
    }
}

if ($postdata['function_type']  == "delete_stock") {

    $company_id = trim($postdata['company_id']);
    $branch_id = trim($postdata['branch_id']);
    $book_id = trim($postdata['book_id']);
    $stock_id = trim($postdata['stock_id']);

    $function_code = $postdata['function_type'];

    $confirmed_role_user = validatePrimaryAccess($conn, $workspace_id, $company_id, $branch_id, null, $role, $userid, $function_code, ['superadmin', 'admin', null]);
    if ($confirmed_role_user['status'] != true) errorreturn($confirmed_role_user['errormsg']);

    $success['data'] = deleteStockDetails($conn, $stock_id, $workspace_id, $company_id, $branch_id, $book_id);
    successreturn($success);
}
