<?php
ini_set("display_errors", 1);

function viewLedgerMonthly($specific_db_conn, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $ledger_opening_bal, $mindate, $maxdate, $book_opening_date)
{

    try {
        $monthly_data = array();
        $debit_total = 0.00;
        $credit_total = 0.00;
        $total_closing = 0.00;
    
        $ledger_opening_bal = fetchLedgerOpeningBalanceSpecificDate($specific_db_conn, $ledgerid, $workspace_id, $company_id, $branch_id, $book_id, $ledger_opening_bal, $mindate, $book_opening_date);
        $total_closing += $ledger_opening_bal;
        // echo $ledger_opening_bal;die();    
        $monthly_data[] = array('month' => 'Opening Balance', 'debit_total' => null, 'credit_total' => null, 'month_closing' => displayAmount($ledger_opening_bal));
    
        function getMonthsBetweenDates($start_date, $end_date) {
            $start = new DateTime($start_date);
            $end = new DateTime($end_date);
            $interval = DateInterval::createFromDateString('1 month');
            $period = new DatePeriod($start, $interval, $end);
        
            $months = [];
            foreach ($period as $dt) {
                $months[] = $dt->format('Y-m');
            }
        
            return $months;
        }
        
        
        $months_between = getMonthsBetweenDates($mindate, $maxdate);
    
    
        $sql_fetch_records_monthly = "Select SUM(amount) as amount, balancetype from act_transactionrecord where workspace_id = ? and company_id = ? and branch_id = ? and book_id = ? and ledgerid = ? and date <= ? and date >= ? and date LIKE ? group by balancetype";
        $fetch_records_monthly = $specific_db_conn->prepare($sql_fetch_records_monthly);
        foreach($months_between as $temp_date) {
            $debit_month_total = 0.00;
            $credit_month_total = 0.00;
    
            $fetch_records_monthly->execute(array($workspace_id, $company_id, $branch_id, $book_id, $ledgerid, $maxdate, $mindate, $temp_date."-%"));
            $result_records_monthly = $fetch_records_monthly->fetchAll();
            foreach($result_records_monthly as $row) {
                if($row['balancetype'] == 'dr') {
                    $debit_month_total += $row['amount'];
                } else {
                    $credit_month_total += $row['amount'];
                }
            }
    
            $total_closing = $total_closing + $debit_month_total - $credit_month_total;
    
            $monthly_data[] = array('month' => date_format(new DateTime($temp_date), 'M Y'), 'debit_total' => $debit_month_total, 'credit_total' => $credit_month_total, 'month_closing' => displayAmount($total_closing));
    
        }
    
        $monthly_data[] = array('month' => 'Grand Total', 'debit_total' =>  $debit_total, 'credit_total' =>  $credit_total, 'month_closing' => displayAmount($total_closing));
    
        return array('monthly_data' => $monthly_data);
    } catch (Exception $e) {
        errorreturn("Error : ".$e->getMessage());
    }

}
