<?php

function getProfitLossAccount($specific_db_conn, $mindate, $maxdate, $workspace_id, $company_id, $branch_id, $book_id, $book_opening_date, $closing_stock, $opening_stock) {
    
    $pl_main_ledgerhead_array = [
        'Income (Direct)', 'Income (Indirect)', 'Expenses (Direct)', 'Expenses (Indirect)', 'Indirect Expenses', 'Purchase Account', 'Sales Account', 'Direct Expenses' 
    ];

    // $expense_array = [];
    // $income_array = [];

    $gross_total = 0;
    $gross_expenses = [];
    $gross_income = [];

    $net_total = 0;
    $net_expenses = [];
    $net_income = [];

    $income_direct = netGroupSummary($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, 'Income (Direct)', $maxdate, $mindate, $book_opening_date);
    $purchase_account = netGroupSummary($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, 'Purchase Account', $maxdate, $mindate, $book_opening_date);
    $expenses_direct = netGroupSummary($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, 'Expenses (Direct)', $maxdate, $mindate, $book_opening_date);
    $direct_expenses = netGroupSummary($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, 'Direct Expenses', $maxdate, $mindate, $book_opening_date);
    $sales_account = netGroupSummary($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, 'Sales Account', $maxdate, $mindate, $book_opening_date);
    // print_r($purchase_account);
    // print_r($sales_account);die();
    function reverseSigns($data) {
      
       
        foreach ($data as &$entry) {
            if (isset($entry['balance'])) {
                $entry['balance'] = floatval($entry['balance']) * -1;
            }
        }
        
        return $data;
    }


    // all incomes balance should be negative to put them on the right side ie. gross_income or net_income, and reverse their child data signs
        // if income balance is positive, you abs it put it to the ledft side, i.e. gross_expense or net_expense, NO NEED TO REVERSE

    // all expense balance should be positive to put them on the left side ie. gross_expense or net_expense, NO NEED TO REVERSE
        // if expense is negative , abs the balance and put them on the right side that is gross_income / net_income, and reverse their child data signs


    if($opening_stock != 0.00) {
        $opening_stock > 0.00 ?
        $gross_expenses[] = array('name'=> 'Opening Stock', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($opening_stock))
        : 
        $gross_income[] = array('name'=> 'Opening Stock', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($opening_stock))
        ;
    }
    if($purchase_account['total_closing'] != 0.00) {
        $purchase_account['total_closing'] >= 0.00 ?
        $gross_expenses[] = array('name' => 'Purchase Account', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => $purchase_account['data'], 'balance' => abs($purchase_account['total_closing']))
        :
        $gross_income[] = array('name' => 'Purchase Account', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => reverseSigns($purchase_account['data']), 'balance' => abs($purchase_account['total_closing']));
    }
    if($expenses_direct['total_closing'] != 0.00) {
        $expenses_direct['total_closing'] >= 0.00 ?
        $gross_expenses[] = array('name' => 'Expenses (Direct)', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => $expenses_direct['data'], 'balance' => abs($expenses_direct['total_closing']))
        :
        $gross_income[] = array('name' => 'Expenses (Direct)', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => reverseSigns($expenses_direct['data']), 'balance' => abs($expenses_direct['total_closing']));
    }
    if($direct_expenses['total_closing'] != 0.00) {
        $direct_expenses['total_closing'] >= 0.00 ?
        $gross_expenses[] = array('name' => 'Direct Expenses', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => $direct_expenses['data'], 'balance' => abs($direct_expenses['total_closing']))
        :
        $gross_income[] = array('name' => 'Direct Expenses', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => reverseSigns($direct_expenses['data']), 'balance' => abs($direct_expenses['total_closing']));
    }
    if($closing_stock != 0.00) {
        $closing_stock > 0.00 ?
        $gross_income[] = array('name'=> 'Closing Stock', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($closing_stock))
        : 
        $gross_expenses[] = array('name'=> 'Closing Stock', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($closing_stock))
        ;
    }
    if($income_direct['total_closing'] != 0.00) {
        $income_direct['total_closing'] >= 0.00 ?
        $gross_income[] = array('name' => 'Income (Direct)', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => ($income_direct['data']), 'balance' => abs($income_direct['total_closing']))
        :
        $gross_expenses[] = array('name' => 'Income (Direct)', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => reverseSigns($income_direct['data']), 'balance' => abs($income_direct['total_closing']));
    }
    if($sales_account['total_closing'] != 0.00) {
        $sales_account['total_closing'] >= 0.00 ?
        $gross_expenses[] = array('name' => 'Sales Account', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => ($sales_account['data']), 'balance' => abs($sales_account['total_closing']))
        :
        $gross_income[] = array('name' => 'Sales Account', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => reverseSigns($sales_account['data']), 'balance' => abs($sales_account['total_closing']));
    }

    // echo "Closing Stock:       ".$closing_stock."<br>";
    // echo "sales_account:       ".$sales_account['total_closing']."<br>";
    // echo "income_direct:       ".$income_direct['total_closing']."<br>";
    // echo "purchase_account:       ".$purchase_account['total_closing']."<br>";
    // echo "expenses_direct:       ".$expenses_direct['total_closing']."<br>";
    // echo "direct_expenses:       ".$direct_expenses['total_closing']."<br>";
    // echo "opening_:       ".$opening_stock."<br>";

    // $closing_stock = -$closing_stock;

    $gross_profit = 0.00;
    foreach($gross_expenses as $temp) {
        $gross_profit -= $temp['balance'];
    }
    foreach($gross_income as $temp) {
        $gross_profit += $temp['balance'];
    }

    if($gross_profit > 0.00) {
        $gross_expenses[] = array('name'=> 'Gross Profit c/o', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($gross_profit));

        $net_income[] = array('name'=> 'Gross Profit b/f', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($gross_profit));
    } else if($gross_profit < 0.00){
        $gross_income[] = array('name'=> 'Gross Loss c/o', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($gross_profit));

        $net_expenses[] = array('name'=> 'Gross Loss b/f', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($gross_profit));
    }

    foreach($gross_expenses as $temp) {
        $gross_total += abs($temp['balance']);
    }


    $income_indirect = netGroupSummary($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, 'Income (Indirect)', $maxdate, $mindate, $book_opening_date);
    $expenses_indirect = netGroupSummary($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, 'Expenses (Indirect)', $maxdate, $mindate, $book_opening_date);
    $indirect_expenses = netGroupSummary($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, 'Indirect Expenses', $maxdate, $mindate, $book_opening_date);

    if($income_indirect['total_closing'] != 0.00) {
        $income_indirect['total_closing'] >= 0.00 ?
        $net_income[] = array('name' => 'Income (Indirect)', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => ($income_indirect['data']), 'balance' => abs($income_indirect['total_closing']))
        :
        $net_expenses[] = array('name' => 'Income (Indirect)', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => reverseSigns($income_indirect['data']), 'balance' => abs($income_indirect['total_closing']));
    }
    if($expenses_indirect['total_closing'] != 0.00) {
        $expenses_indirect['total_closing'] >= 0.00 ?
        $net_expenses[] = array('name' => 'Expenses (Indirect)', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => $expenses_indirect['data'], 'balance' => abs($expenses_indirect['total_closing']))
        :
        $net_income[] = array('name' => 'Expenses (Indirect)', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => reverseSigns($expenses_indirect['data']), 'balance' => abs($expenses_indirect['total_closing']));
    }
    if($indirect_expenses['total_closing'] != 0.00) {
        $indirect_expenses['total_closing'] >= 0.00 ?
        $net_expenses[] = array('name' => 'Indirect Expenses', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => $indirect_expenses['data'], 'balance' => abs($indirect_expenses['total_closing']))
        :
        $net_income[] = array('name' => 'Indirect Expenses', 'type' => 'group', 'expand' => true, 'clickable' => true, 'data' => reverseSigns($indirect_expenses['data']), 'balance' => abs($indirect_expenses['total_closing']));
    }

    // calculating net profit

    $net_profit = 0.00;
    foreach($net_expenses as $temp) {
        $net_profit -= $temp['balance'];
    }
    foreach($net_income as $temp) {
        $net_profit += $temp['balance'];
    }

    if($net_profit > 0.00) {
        $net_expenses[] = array('name'=> 'Nett Profit', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($net_profit));
    } else if($net_profit < 0.00){
        $net_income[] = array('name'=> 'Nett Loss', 'type' => 'label', 'expand' => false, 'clickable' => false, 'data' => null, 'balance' => abs($net_profit));
    }

    foreach($net_expenses as $temp) {
        $net_total += abs($temp['balance']);
    }

    return array('gross_expenses' => $gross_expenses, 'gross_income' => $gross_income, 'gross_total' => $gross_total, 'net_expenses' => $net_expenses, 'net_income' => $net_income, 'net_total' => $net_total);

}
?>