<?php

function getBook($specific_db_conn, $workspace_id, $company_id, $branch_id, $search_string, $limit, $offset)
{

    $sql = "SELECT  count(*) as total_rows FROM  act_books  where workspace_id =? and  company_id= ? and branch_id=? and name LIKE ?";
    $array_params = [$workspace_id, $company_id, $branch_id, '%' . $search_string . '%'];
    $stmt = $specific_db_conn->prepare($sql);
    $stmt->execute($array_params);
    $result_total = $stmt->fetchAll();
    $totalrows = $result_total[0]['total_rows'];

    // reeturns all data from the specific row of the profile
    $sql_book = "SELECT * FROM  act_books  where workspace_id =? and company_id= ? and branch_id=? and name LIKE ? ORDER BY id ASC LIMIT " . $offset . " ," . $limit . " ";
    $array_params = [$workspace_id, $company_id, $branch_id, '%' . $search_string . '%'];
    $stmt = $specific_db_conn->prepare($sql_book);
    $stmt->execute($array_params);
    $book_rows = $stmt->fetchAll();

    $array_return = [];

    foreach ($book_rows as $rows) {
        $array_return[] =  array(
            'book_id' => $rows['id'], 'name' => $rows['name'], 'book_opening_date' => CheckDateValidation($rows['book_opening_date']),
            'book_closing_date' => CheckDateValidation($rows['book_closing_date']), 'financial_year_startdate' => date("d-m", strtotime("1970-" . $rows['financial_year_startdate']))
        );
    }

    if (count($book_rows) < 50) {

        return array('total_rows' => $totalrows, 'book_info' => $array_return, 'maxreach' => 1);
    } else {
        return array('total_rows' => $totalrows, 'book_info' => $array_return, 'maxreach' => 0);
    }
}


function getSpecificBook($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id)
{
    // reeturns all data from the specific row of the profile
    $sql = $specific_db_conn->prepare("SELECT * FROM  act_books  where id = ? and workspace_id =?  and company_id= ? and branch_id=? ");
    $sql->execute(array($book_id, $workspace_id, $company_id, $branch_id));
    $rows = $sql->fetchAll();

    if (count($rows) == 1) {
        // get transaction setting from book setting
        $sql = $specific_db_conn->prepare("SELECT * FROM  act_book_settings  where workspace_id =?  and company_id= ? and branch_id=? and book_id =? ");
        $sql->execute(array($workspace_id, $company_id, $branch_id, $book_id));
        $book_data = $sql->fetchAll();

        if (count($book_data) >= 1) {

            $transaction_setting = array("transaction_numbering_system" => json_decode($book_data[0]['transaction_numbering_system'], true), "prefix_financial_year" => $book_data[0]['prefix_financial_year']);
        }

        return array(
            'book_id' => $rows[0]['id'], 'name' => $rows[0]['name'], 'book_opening_date' => CheckDateValidation($rows[0]['book_opening_date']),
            'book_closing_date' => CheckDateValidation($rows[0]['book_closing_date']),  'financial_year_startdate' => date("d-m", strtotime("1970-" . $rows[0]['financial_year_startdate'])),
            'transaction_setting' => $transaction_setting
        );
    } else {
        errorreturn("Enter Invalid Book ID");
    }
}
