<?php
class ModelExtensionDashboardChartFarsi extends Model {
	public function getTotalOrdersByMonthFarsi() {
		$order_data = array();
		for ($i = 1; $i <= jdate('t'); $i++) {
			$cur_jdate = jdate('Y') . '-' . jdate('m') . '-' . $i;
			$date = jtg($cur_jdate);
			$order_data[jdate('j', strtotime($date))] = array(
				'day'   => jdate('d', strtotime($date)),
				'total' => 0
			);
		}
		$first_jmdate = jdate('Y') . '-' . jdate('m') . '-1';
		$first_gmdate = jtg($first_jmdate);
		$query = $this->db->query("SELECT COUNT(*) AS total, date_added FROM `" . DB_PREFIX . "order` WHERE order_status_id > '0' AND DATE(date_added) >= '" . $this->db->escape($first_gmdate) . "' GROUP BY DATE(date_added)");
		foreach ($query->rows as $result) {
			$order_data[jdate('j', strtotime($result['date_added']))] = array(
				'day'   => jdate('d', strtotime($result['date_added'])),
				'total' => $result['total']
			);
		}
		return $order_data;
	}
	public function getTotalOrdersByYearFarsi() {
		$order_data = array();
		for ($i = 1; $i <= 12; $i++) {
			$jmonth = gregorian_to_jalali(date('Y'), $i, date('d')); 
			$jmonth = $jmonth[1];
			$jyear = jdate('Y');
			$order_data[$jmonth] = array(
				'month' => jdate('F', jmktime(0, 0, 0, $jmonth)),
				'total' => 0
			);
			$first_jmdate = jdate('Y') . '-' . $jmonth . '-1';
			$first_gmdate = jtg($first_jmdate);
			$jmonth_next = $jmonth + 1;
			if ($jmonth_next > 12) {
				$jyear = jdate('Y');
				$jyear = ($jyear + 1);
				$jmonth_next = 1;
				$next_jmdate = $jyear . '-' . $jmonth_next . '-1';
			} else {
				$next_jmdate = jdate('Y') . '-' . $jmonth_next . '-1';
			}
			$next_gmdate = jtg($next_jmdate);
			$query = $this->db->query("SELECT COUNT(*) AS total, date_added FROM `" . DB_PREFIX . "order` WHERE order_status_id > '0' AND DATE(date_added) >= '" . $this->db->escape($first_gmdate) . "' AND DATE(date_added) < '" . $this->db->escape($next_gmdate) . "' GROUP BY MONTH(date_added)");
			if ($query->num_rows) {
				foreach ($query->rows as $result) {
					$order_data[jdate('n', strtotime($result['date_added']))] = array(
						'month' => jdate('F', strtotime($result['date_added'])),
						'total' => $result['total']
					);
				}
			}
		}
		return $order_data;
	}
	public function getTotalCustomersByMonthFarsi() {
		$customer_data = array();
		for ($i = 1; $i <= jdate('t'); $i++) {
			$cur_jdate = jdate('Y') . '-' . jdate('m') . '-' . $i;
			$date = jtg($cur_jdate);
			$customer_data[jdate('j', strtotime($date))] = array(
				'day'   => jdate('d', strtotime($date)),
				'total' => 0
			);
		}
		$first_jmdate = jdate('Y') . '-' . jdate('m') . '-1';
		$first_gmdate = jtg($first_jmdate);
		$query = $this->db->query("SELECT COUNT(*) AS total, date_added FROM `" . DB_PREFIX . "customer` WHERE DATE(date_added) >= '" . $this->db->escape($first_gmdate) . "' GROUP BY DATE(date_added)");
		foreach ($query->rows as $result) {
			$customer_data[jdate('j', strtotime($result['date_added']))] = array(
				'day'   => jdate('d', strtotime($result['date_added'])),
				'total' => $result['total']
			);
		}
		return $customer_data;
	}
	public function getTotalCustomersByYearFarsi() {
		$customer_data = array();
		for ($i = 1; $i <= 12; $i++) {
			$jmonth = gregorian_to_jalali(date('Y'), $i, date('d')); 
			$jmonth = $jmonth[1];
			$jyear = jdate('Y');
			$customer_data[$jmonth] = array(
				'month' => jdate('F', jmktime(0, 0, 0, $jmonth)),
				'total' => 0
			);
			$first_jmdate = jdate('Y') . '-' . $jmonth . '-1';
			$first_gmdate = jtg($first_jmdate);
			$jmonth_next = $jmonth + 1;
			if ($jmonth_next > 12) {
				$jyear = jdate('Y');
				$jyear = ($jyear + 1);
				$jmonth_next = 1;
				$next_jmdate = $jyear . '-' . $jmonth_next . '-1';
			} else {
				$next_jmdate = jdate('Y') . '-' . $jmonth_next . '-1';
			}
			$next_gmdate = jtg($next_jmdate);
			$query = $this->db->query("SELECT COUNT(*) AS total, date_added FROM `" . DB_PREFIX . "customer` WHERE DATE(date_added) >= '" . $this->db->escape($first_gmdate) . "' AND DATE(date_added) < '" . $this->db->escape($next_gmdate) . "' GROUP BY MONTH(date_added)");
			if ($query->num_rows) {
				foreach ($query->rows as $result) {
					$customer_data[jdate('n', strtotime($result['date_added']))] = array(
						'month' => jdate('F', strtotime($result['date_added'])),
						'total' => $result['total']
					);
				}
			}
		}
		return $customer_data;
	}
}
?>