<?php
ini_set('display_errors', 1);
//error_reporting(0);


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

// should we not call header and ch
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/currency_functions.php";

//valdation file
require_once $base_path_to_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_resources . 'lib/validate_function.php';
require_once $base_path . 'user_management/authenticate.php';
require_once $base_path . 'modules/accounting/book_management/basic_book_info_function.php';
// require_once $base_path . 'modules/accounting/reports/net_group_summary/group_summary_classified/group_summary_classified.php';
//creating dynamic db
require_once $base_path_to_resources . 'includes/specific_db_config.php';

$postdata = json_decode(file_get_contents("php://input"), true);
checkIfNoPhpContentsReceive();
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
$created_by = $jwt_validate->data->username;
$workspace_id = $jwt_validate->data->workspace_id;
$role = $jwt_validate->data->role;
$userid = $jwt_validate->data->id;



if ($postdata['function_type'] == "group_summary_classified") {
  $company_id = trim($postdata['company_id']);
  $branch_id = trim($postdata['branch_id']);
  $book_id = trim($postdata['book_id']);
  $input_group_name = trim($postdata['group_name']);
  $function_code = $postdata['function_type'];
  $function_code = $postdata['function_type'];
  $confirmed_role_user = validatePrimaryAccess($conn, $workspace_id, $role, $userid, ['superadmin', 'admin', null]);
  if (!$confirmed_role_user) {
    errorreturn('Access denied.');
  } else {
    $specific_db_conn = dynamicDbConnection($confirmed_role_user);
    if ($role == null) {
      checkCompanyBranchFunctionAccess($specific_db_conn, $role, $userid, $workspace_id, $company_id, $branch_id, $book_id,  $function_code);
    }
  }
  require_once $base_path . 'modules/accounting/reports/net_group_summary/group_summary_classified/group_summary_classified.php';

  $book_info = GetBasicBook($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id);
  $book_name = $book_info['book_basic_info']['book_name'];
  $book_opening_date = $book_info['book_basic_info']['book_opening_date'];
  $book_closing_date = $book_info['book_basic_info']['book_closing_date'];



  if (isset($postdata['mindate']) and !empty($postdata['mindate'])) {
    $mindate = trim($postdata['mindate']);
  } else {
    $mindate = $book_opening_date;
  }

  if (isset($postdata['maxdate']) and !empty($postdata['maxdate'])) {
    $maxdate = trim($postdata['maxdate']);
  } else {
    $maxdate = $book_closing_date;
  }

  if (!strtotime($mindate) or $mindate < $book_opening_date) {
    // echo $mindate,$book_opening_date;die();
    errorreturn('Mininum date cannot be less than book opening date');
  }

  if (!strtotime($maxdate) or $maxdate > $book_closing_date) {

    errorreturn('Maximum date cannot be less than book opening date');
  }
  if ($maxdate < $mindate) {

    errorreturn('Invalid Date format');
  }
  $success['data'] = groupSummaryClassified($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $input_group_name, $maxdate, $mindate, $book_opening_date);
  successreturn($success);
}
