<?php

namespace App\Http\Controllers\Administrator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\SearchModel;

class IncomeStatementController extends Controller
{
    public function index()
    {
        $branch_id=\Branch::branch_id();
   		$date_start = date("Y-m-d",strtotime("first day of this month"));
   		$date_end = date("Y-m-d",strtotime("last day of this month"));
    	$incomes = DB::select("SELECT 
							    Sum(nso007_journal.journal_credit) AS income,
							    nso007_journal.journal_acccode,
							    nso007_account_chart.acc_name
							    FROM nso007_journal
							    INNER JOIN nso007_account_chart ON nso007_journal.journal_acccode = nso007_account_chart.id
								WHERE journal_acccode like '4%' AND nso007_journal.journal_paydate BETWEEN  '$date_start' AND '$date_end' AND nso007_journal.branch_id= $branch_id
								GROUP BY journal_acccode");
    	$expenses = DB::select("SELECT 
    							Sum(nso007_journal.journal_debit) AS expense,
    							nso007_journal.journal_acccode,
    							nso007_account_chart.acc_name
								FROM nso007_journal
								INNER JOIN nso007_account_chart ON nso007_journal.journal_acccode = nso007_account_chart.id
								WHERE journal_acccode like '5%' AND journal_type NOT IN (13) AND nso007_journal.journal_paydate BETWEEN  '$date_start' AND '$date_end' AND nso007_journal.branch_id= $branch_id
								GROUP BY journal_acccode");
    	$net_income = 0;
    	$total_income = 0;
    	$total_expense = 0;
    	foreach ($incomes as $key => $value) {
    		$total_income += $value->income;
    	}
    	foreach ($expenses as $key => $value1) {
    		$total_expense += $value1->expense;
    	}
    	$net_income = $total_income - $total_expense;

    	return view('administrator.accounting.income-statement.index',compact('net_income','incomes','expenses'));
    }

    public function filter_income_statement(Request $request)
    {
        $mdoel_searching = new SearchModel();
        $mdoel_searching1 = new SearchModel();
        $journal_date =  (empty($request->start_date))? '':' nso007_journal.journal_paydate BETWEEN "'.$request->start_date.'" AND "'.$request->end_date.'" ';
        

        $data_search = array(
            'journal_date' =>$journal_date,
        );
        $search_income = $mdoel_searching->search($data_search);
        $search_expense = $mdoel_searching1->search($data_search);
        $search_income = (empty($search_income))? " WHERE journal_acccode like '4%' ":" " .$search_income. "  AND journal_acccode like '4%'";
        $search_expense = (empty($search_expense))? " WHERE journal_acccode like '5%' AND journal_type NOT IN (13) ":" " .$search_expense. "  AND journal_acccode like '5%' AND journal_type NOT IN (13)";


        $incomes = DB::select("SELECT 
                                Sum(nso007_journal.journal_credit) AS income,
                                nso007_journal.journal_acccode,
                                nso007_account_chart.acc_name
                                FROM nso007_journal
                                INNER JOIN nso007_account_chart ON nso007_journal.journal_acccode = nso007_account_chart.id
                                $search_income
                                GROUP BY journal_acccode");

        $expenses = DB::select("SELECT 
                                Sum(nso007_journal.journal_debit) AS expense,
                                nso007_journal.journal_acccode,
                                nso007_account_chart.acc_name
                                FROM nso007_journal
                                INNER JOIN nso007_account_chart ON nso007_journal.journal_acccode = nso007_account_chart.id
                                $search_expense
                                GROUP BY journal_acccode");
        $net_income = 0;
        $total_income = 0;
        $total_expense = 0;
        foreach ($incomes as $key => $value) {
            $total_income += $value->income;
        }
        foreach ($expenses as $key => $value1) {
            $total_expense += $value1->expense;
        }
        $net_income = $total_income - $total_expense;

        $data =
        [
            'income' => $incomes,
            'expense' => $expenses,
            'net_income' => $net_income,
        ];

        echo json_encode($data);

    }
}
