<?php
//base path learning to resources

require_once '../../base_path_include.php';
require_once $base_path_to_learning_resources . "/includes/config.php";

require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';

$data = json_decode(file_get_contents('php://input'), true);

validateCurlBookSignature($data['api_signature']);
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';

$rows = $data['book_data'];

$workspace_id = $rows['workspace_id'];
$book_opening_date = $rows['book_opening_date'];
$book_closing_date = $rows['book_closing_date'];
$book_id = $rows['book_id'];
$company_id = $rows['company_id'];
$branch_id = $rows['branch_id'];
$function_type = $rows['function_type'];

$sqlDb = $conn_rjllearning->prepare("SELECT db_name FROM  workspace_database_info WHERE workspace_id = ? and status  = ? ");
$sqlDb->execute(array($workspace_id, 1));
$get_db = $sqlDb->fetchAll();
if (count($get_db) != 1) {
    return false;
}
$learning_db_name  = $get_db[0]['db_name'];

$conn_rjllearning_specific = dynamicDbConnection($learning_db_name);


// validate from branch book data exists or not
// $res = $conn_rjllearning_specific->prepare("SELECT id FROM branch WHERE rejoice_finware_book_id =? and company_id = ? and rejoice_solutions_company_id = ? and rejoice_solutions_branch_id=? AND rejoice_finware_book_opening_date > ?, rejoice_finware_book_closing_date < ?");
// $res->execute(array($book_id, $company_id, $book_opening_date, $book_closing_date));
// $result1 = $res->fetchAll();
// if (count($result1) >= 1) {
//     return false;
// }


if ($function_type == 'alter_book_learning_branch') {
    $sql = "UPDATE branch SET  rejoice_finware_book_opening_date=?, rejoice_finware_book_closing_date=? WHERE rejoice_finware_book_id=? AND rejoice_solutions_company_id = ? AND rejoice_solutions_branch_id  =?";
    $conn_rjllearning_specific->prepare($sql)->execute(array($book_opening_date, $book_closing_date, $book_id, $company_id, $branch_id));
}

return true;
