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


require_once '../../../../resources/base_path_include.php';

//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';

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

if (!isset($postdata['unit_id']) or !checkIfIssetAndNotEmpty($postdata['unit_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['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 deleteUnits($conn, $unit_id, $workspace_id, $company_id, $branch_id, $book_id)
{
    $sql = "Select unit_name  from  inventory_stockdetails where id = ? and  workspace_id = ? and company_id =? and branch_id =? and book_id =? ";
    $stmt_units = $conn->prepare($sql);
    $stmt_units->execute(array($unit_id, $workspace_id, $company_id, $branch_id, $book_id));
    $fetch_units = $stmt_units->fetchAll();
    if (count($fetch_units) == 1) {
        $sql = "SELECT count(*) as total FROM inventory_stockdetails WHERE unit_id = ? and workspace_id = ? and company_id =? and branch_id =? and book_id =? ";
        $stmt = $conn->prepare($sql);
        $stmt->execute(array($unit_id, $workspace_id, $company_id, $branch_id, $book_id));
        $act_groupinfo = $stmt->fetchAll();
        if ($act_groupinfo[0]['total'] >= 1) {

            errorreturn("Unable to delete unit name because of the presence of stock details.");
        }
    } else {

        $sql = "DELETE FROM unit_creation WHERE id=? and workspace_id = ? and company_id =? and branch_id =? and book_id =?";
        $stmt = $conn->prepare($sql);
        $stmt->execute(array($unit_id, $workspace_id, $company_id, $branch_id, $book_id));
        $count = $stmt->rowCount();
        if ($count == 1) {
            return "Delete unit Successfully";
        } else {
            errorreturn("unable to delete unit");
        }
    }
}

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

    $company_id = trim($postdata['company_id']);
    $branch_id = trim($postdata['branch_id']);
    $book_id = trim($postdata['book_id']);
    $unit_id = trim($postdata['unit_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'] = deleteUnits($conn, $unit_id, $workspace_id, $company_id, $branch_id, $book_id);
    successreturn($success);
}
