<?php
ini_set('display_errors', 1);
// this file is used to create all other users except superadmin
require_once '../../../resources/base_path_include.php';
require_once $base_path_to_learning_resources  . "includes/connect.php";
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';
//valdation file
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';
require_once $base_path_to_learning_resources . 'external_requests/finware_curl/groups_ledgers/curl_get_student_ledgers_finware.php';
require_once $base_path_to_learning . 'functions/common_functions.php';
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';

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

if (
    !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']);

$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
//then workspace_id is iin decodede data of jwt use from there
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$role = $jwt_validate->data->role;

$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}

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


    $institution_id = ($postdata['institution_id']);
    $branch_id = ($postdata['branch_id']);
    getBranchWorkspaceConnection($conn_rjllearning_specific, $branch_id, $institution_id);
    
    $finware_company_id = ($postdata['finware_company_id']);
    $finware_branch_id = ($postdata['finware_branch_id']);
    $finware_book_id = ($postdata['finware_book_id']);
    $limit = ($postdata['limit']);
    if (isset($postdata['search_string']) and !empty($postdata['search_string'])) {
        $search_string = trim($postdata['search_string']);
    } else {
        $search_string = null;
    }
  
    $page = (isset($postdata['page']) and isNumeric($postdata['page'])) ? (int)$postdata['page'] : 1;
    if($limit != 'all'){
    $offset = ($page - 1) * $limit;
    }else{
        $offset = null;
    }
    $student_ledgers_info = array('workspace_id' => $workspace_id, "company_id" => $finware_company_id, "branch_id" => $finware_branch_id, "book_id" => $finware_book_id, 
    'search_string' => $search_string,"limit"=>$limit,"offset"=>$offset);
    //  print_r($student_ledgers_info);die();
    
    $success['data'] = curlGetFinwareStudentsLedgers($student_ledgers_info);
    successreturn($success);
}
