<?php
// this file expects connection file to be already imported before calling this file as include or require

function UpdateLedgerBalance($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $ledgerid, $relative_amount, $transaction_date, $main_ledgerhead, $parent_ledgerhead, $rollback_bool = 0)
{
    try {
        $sql_update_ledger_balances_select_specific_date = "SELECT closing_balance,date FROM  act_ledgerbalances where  workspace_id = ? and company_id =? and branch_id =? and book_id =? and  ledgerid =  ? and date <= ? ORDER BY date DESC LIMIT 1";
        $update_ledger_balances_select_specific_date = $specific_db_conn->prepare($sql_update_ledger_balances_select_specific_date);

        $update_ledger_balances_select_specific_date->execute(array($workspace_id, $company_id, $branch_id, $book_id, $ledgerid, $transaction_date));
        $result_ledgerbalance = $update_ledger_balances_select_specific_date->fetchAll();

        if (count($result_ledgerbalance) == 1) {

            $ledger_date = $result_ledgerbalance[0]['date'];
            $closing_balance = $result_ledgerbalance[0]['closing_balance'];
        } else {
            // no ledgerbalance for this ledgerid exists
            $ledger_date = null;
            $closing_balance = 0.00;
        }

        $uid = $transaction_date . "_" . $ledgerid;

        if ($ledger_date != $transaction_date) {

            $sql_insert_ledgerbalance = "INSERT IGNORE INTO act_ledgerbalances(workspace_id,company_id,branch_id,book_id,ledgerid,date,closing_balance,main_ledgerhead,parent_ledgerhead, uid)
             VALUES(?,?,?,?,?,?,?,?,?, ?)";
            $insert_act_ledgerbalances = $specific_db_conn->prepare($sql_insert_ledgerbalance);

            $insert_act_ledgerbalances->execute(array($workspace_id, $company_id, $branch_id, $book_id, $ledgerid, $transaction_date, $closing_balance, $main_ledgerhead, $parent_ledgerhead, $uid));
        }
        $sql_update_ledger_balance = "Update act_ledgerbalances SET closing_balance = closing_balance + ? where ledgerid = ? and workspace_id = ? and company_id =? and branch_id =? and book_id =? and date >= ?";
        $stmt_update_ledger_balance = $specific_db_conn->prepare($sql_update_ledger_balance);
        $stmt_update_ledger_balance->execute(array($relative_amount, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $transaction_date));

        $sql_update_ledgerdetails_currentbalance = "Update act_ledgerdetails SET ledger_current_bal =  ledger_current_bal + ? WHERE id=? and workspace_id  =? and company_id =? and branch_id =? and book_id =? ";
        $update_ledgerdetails_currentbalance = $specific_db_conn->prepare($sql_update_ledgerdetails_currentbalance);

        //code to update ledgerdetails automatically when this is called.
        $update_ledgerdetails_currentbalance->execute(array($relative_amount, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id));
    } catch (Exception $e) {
        if ($rollback_bool == 1) {
            $specific_db_conn->rollBack();
        }
        errorreturn($e->getMessage());
    }
}
