<?php

use app\libraries\CommonHelper;
use app\libraries\DateHelper;
use app\libraries\Tenants;

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Report_model extends MY_Model {

//    protected $table = 'rbac_users';

    use Tenants;

    public function __construct()
	{
        parent::__construct();
    }

    public function yearlyIncome($year)
	{
		$this->load->model('Invoice_model');
		return $this->Invoice_model->yearlyIncome($year);
	}

	public function monthlyIncome($year, $month)
	{
		$this->load->model('Invoice_model');
		return $this->Invoice_model->monthlyIncome($year, $month);
	}

	public function dailyIncome($fromDate, $toDate)
	{
		$data = [];
		$data['label'] = [];
		$data['amount'] = [];
		$this->load->model('Invoice_model');
		$income = $this->Invoice_model->dailyIncome($fromDate, $toDate);
		$curr = $fromDate;
		$completed = false;
		while (! $completed) {
			$data['label'][] = "'" . $curr . "'";
			$data['amount'][] = array_key_exists($curr, $income) ? $income[$curr] : 0;
			$curr = date("Y-m-d", strtotime($curr . ' + 1 day'));
			if ($curr > $toDate) {
				$completed = true;
			}
		}
		return $data;
	}

	public function currentYearIncome($year)
	{
		$data = [];
		$data['label'] = [];
		$data['amount'] = [];
		$this->load->model('Invoice_model');
		$income = $this->Invoice_model->currentYearIncome($year);
		for ($curr = 1; $curr <= 12; $curr++) {
			$key = sprintf("%04d-%02d", $year, $curr);
			$data['label'][] = "'" . $key . "'";
		//$data['amount'][] = array_key_exists($key, $income) ? $income[$key] : 0;
		$data['amount'][] = array_key_exists($key, $income) ? $income[$key] : 0;
		}
		return $data;
	}

}
