<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\ReportModel;
use App\Models\UserModel;

class Expense extends BaseController
{

    public function all_expense()
    {
        if(isset($_GET['month']))
        {
            $month = $_GET['month'];
            $year = $_GET['year'];
        }
        else
        {
            $month = date('m');
            $year = date('Y');
        }
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_expense'] = $setting->get_all_expense($month, $year);
        echo view('admin/include/header', $data);
        echo view('admin/expense/all_expense');
        echo view('admin/include/footer');
    }

    public function add_new_expense()
    {
        $setting = new SettingModel();
        $data['expense_amount']         = $this->request->getVar('expense_amount');
        $data['expense_date']           = $this->request->getVar('expense_date');
        $data['expense_note']           = $this->request->getVar('expense_note');
        $data['expense_category']       = 'Debit';
        $data['expense_added_by']       = user()->user_id;
        
        if($setting->add_new_expense($data))
        {
            session()->set('success', "Expense Details Successfully Added");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Expense Details Added Failed");
            return redirect()->back();
        }
    }

    public function all_income()
    {
        if(isset($_GET['month']) && isset($_GET['year']))
        {
            $month = str_pad($_GET['month'], 2, '0', STR_PAD_LEFT);
            $year = $_GET['year'];
        }
        else
        {
            $month = date('m');
            $year = date('Y');
        }
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_income'] = $setting->get_all_income($month, $year);
        echo view('admin/include/header', $data);
        echo view('admin/expense/all_income');
        echo view('admin/include/footer');
    }

    public function add_new_income()
    {
        $setting = new SettingModel();
        $data['expense_amount']         = $this->request->getVar('expense_amount');
        $data['expense_date']           = $this->request->getVar('expense_date');
        $data['expense_note']           = $this->request->getVar('expense_note');
        $data['expense_category']       = 'Credit';
        $data['expense_added_by']       = user()->user_id;
        
        if($setting->add_new_expense($data))
        {
            session()->set('success', "Income Details Successfully Added");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Income Details Added Failed");
            return redirect()->back();
        }
    }

}
