<?php
class ModelExtensionDashboardSale extends Model {
    public function getTotalSales(array $data = []): float {
        $sql = "SELECT SUM(total) AS `total` FROM `" . DB_PREFIX . "order` WHERE `order_status_id` > '0'";

        if (!empty($data['filter_date_added'])) {
            $sql .= " AND DATE(`date_added`) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
        }

        $query = $this->db->query($sql);

        return isset($query->row['total']) ? (float)$query->row['total'] : 0.00;
    }

    public function getTotalOrdersByCountry(): array {
        $query = $this->db->query("SELECT COUNT(*) AS `total`, SUM(o.`total`) AS `amount`, c.`iso_code_2` FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "country` c ON (o.`payment_country_id` = c.`country_id`) WHERE o.`order_status_id` > '0' GROUP BY o.`payment_country_id`");

        return $query->rows;
    }

    public function getTotalOrdersByDay(): array {
        $implode = [];

        foreach ((array)$this->config->get('config_complete_status') as $order_status_id) {
            $implode[] = "'" . (int)$order_status_id . "'";
        }

        $order_data = [];

        for ($i = 0; $i < 24; $i++) {
            $order_data[$i] = [
                'hour'  => $i,
                'total' => 0
            ];
        }

        $query = $this->db->query("SELECT COUNT(*) AS `total`, HOUR(`date_added`) AS `hour` FROM `" . DB_PREFIX . "order` WHERE `order_status_id` IN(" . implode(",", $implode) . ") AND DATE(`date_added`) = DATE(NOW()) GROUP BY HOUR(`date_added`) ORDER BY `date_added` ASC");

        foreach ($query->rows as $result) {
            $order_data[$result['hour']] = [
                'hour'  => $result['hour'],
                'total' => $result['total']
            ];
        }

        return $order_data;
    }

    public function getTotalOrdersByWeek(): array {
        $implode = [];

        foreach ((array)$this->config->get('config_complete_status') as $order_status_id) {
            $implode[] = "'" . (int)$order_status_id . "'";
        }

        $order_data = [];

        $date_start = strtotime('-' . date('w') . ' days');

        for ($i = 0; $i < 7; $i++) {
            $date = date('Y-m-d', $date_start + ($i * 86400));

            $order_data[date('w', strtotime($date))] = [
                'day'   => date('D', strtotime($date)),
                'total' => 0
            ];
        }

        $query = $this->db->query("SELECT COUNT(*) AS `total`, `date_added` FROM `" . DB_PREFIX . "order` WHERE `order_status_id` IN(" . implode(",", $implode) . ") AND DATE(`date_added`) >= DATE('" . $this->db->escape(date('Y-m-d', $date_start)) . "') GROUP BY DAYNAME(`date_added`)");

        foreach ($query->rows as $result) {
            $order_data[date('w', strtotime($result['date_added']))] = [
                'day'   => date('D', strtotime($result['date_added'])),
                'total' => $result['total']
            ];
        }

        return $order_data;
    }

    public function getTotalOrdersByMonth(): array {
        $implode = [];

        foreach ((array)$this->config->get('config_complete_status') as $order_status_id) {
            $implode[] = "'" . (int)$order_status_id . "'";
        }

        $order_data = [];

        for ($i = 1; $i <= date('t'); $i++) {
            $date = date('Y') . '-' . date('m') . '-' . $i;

            $order_data[date('j', strtotime($date))] = [
                'day'   => date('d', strtotime($date)),
                'total' => 0
            ];
        }

        $query = $this->db->query("SELECT COUNT(*) AS `total`, `date_added` FROM `" . DB_PREFIX . "order` WHERE `order_status_id` IN(" . implode(",", $implode) . ") AND DATE(`date_added`) >= DATE('" . $this->db->escape(date('Y') . '-' . date('m') . '-1') . "') GROUP BY DATE(`date_added`)");

        foreach ($query->rows as $result) {
            $order_data[date('j', strtotime($result['date_added']))] = [
                'day'   => date('d', strtotime($result['date_added'])),
                'total' => $result['total']
            ];
        }

        return $order_data;
    }

    public function getTotalOrdersByYear(): array {
        $implode = [];

        foreach ((array)$this->config->get('config_complete_status') as $order_status_id) {
            $implode[] = "'" . (int)$order_status_id . "'";
        }

        $order_data = [];

        for ($i = 1; $i <= 12; $i++) {
            $order_data[$i] = [
                'month' => date('M', mktime(0, 0, 0, $i)),
                'total' => 0
            ];
        }

        $query = $this->db->query("SELECT COUNT(*) AS `total`, `date_added` FROM `" . DB_PREFIX . "order` WHERE `order_status_id` IN(" . implode(",", $implode) . ") AND YEAR(`date_added`) = YEAR(NOW()) GROUP BY MONTH(`date_added`)");

        foreach ($query->rows as $result) {
            $order_data[date('n', strtotime($result['date_added']))] = [
                'month' => date('M', strtotime($result['date_added'])),
                'total' => $result['total']
            ];
        }

        return $order_data;
    }

    public function getOrders(array $data = []): array {
        $sql = "SELECT MIN(o.`date_added`) AS `date_start`, MAX(o.`date_added`) AS `date_end`, COUNT(*) AS `orders`, SUM((SELECT SUM(op.`quantity`) FROM `" . DB_PREFIX . "order_product` op WHERE op.`order_id` = o.`order_id` GROUP BY op.`order_id`)) AS `products`, SUM((SELECT SUM(ot.`value`) FROM `" . DB_PREFIX . "order_total` ot WHERE ot.`order_id` = o.`order_id` AND ot.`code` = 'tax' GROUP BY ot.`order_id`)) AS `tax`, SUM(o.`total`) AS `total` FROM `" . DB_PREFIX . "order` o";

        if (!empty($data['filter_order_status_id'])) {
            $sql .= " WHERE o.`order_status_id` = '" . (int)$data['filter_order_status_id'] . "'";
        } else {
            $sql .= " WHERE o.`order_status_id` > '0'";
        }

        if (!empty($data['filter_date_start'])) {
            $sql .= " AND DATE(o.`date_added`) >= DATE('" . $this->db->escape($data['filter_date_start']) . "')";
        }

        if (!empty($data['filter_date_end'])) {
            $sql .= " AND DATE(o.`date_added`) <= DATE('" . $this->db->escape($data['filter_date_end']) . "')";
        }

        if (!empty($data['filter_group'])) {
            $group = $data['filter_group'];
        } else {
            $group = 'week';
        }

        switch ($group) {
            case 'day';
                $sql .= " GROUP BY YEAR(o.`date_added`), MONTH(o.`date_added`), DAY(o.`date_added`)";
                break;
            default:
            case 'week':
                $sql .= " GROUP BY YEAR(o.`date_added`), WEEK(o.`date_added`)";
                break;
            case 'month':
                $sql .= " GROUP BY YEAR(o.`date_added`), MONTH(o.`date_added`)";
                break;
            case 'year':
                $sql .= " GROUP BY YEAR(o.`date_added`)";
                break;
        }

        $sql .= " ORDER BY o.`date_added` DESC";

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

    public function getTotalOrders(array $data = []): int {
        if (!empty($data['filter_group'])) {
            $group = $data['filter_group'];
        } else {
            $group = 'week';
        }

        switch ($group) {
            case 'day';
                $sql = "SELECT COUNT(DISTINCT YEAR(`date_added`), MONTH(`date_added`), DAY(`date_added`)) AS `total` FROM `" . DB_PREFIX . "order`";
                break;
            default:
            case 'week':
                $sql = "SELECT COUNT(DISTINCT YEAR(`date_added`), WEEK(`date_added`)) AS `total` FROM `" . DB_PREFIX . "order`";
                break;
            case 'month':
                $sql = "SELECT COUNT(DISTINCT YEAR(`date_added`), MONTH(`date_added`)) AS `total` FROM `" . DB_PREFIX . "order`";
                break;
            case 'year':
                $sql = "SELECT COUNT(DISTINCT YEAR(`date_added`)) AS `total` FROM `" . DB_PREFIX . "order`";
                break;
        }

        if (!empty($data['filter_order_status_id'])) {
            $sql .= " WHERE `order_status_id` = '" . (int)$data['filter_order_status_id'] . "'";
        } else {
            $sql .= " WHERE `order_status_id` > '0'";
        }

        if (!empty($data['filter_date_start'])) {
            $sql .= " AND DATE(`date_added`) >= DATE('" . $this->db->escape($data['filter_date_start']) . "')";
        }

        if (!empty($data['filter_date_end'])) {
            $sql .= " AND DATE(`date_added`) <= DATE('" . $this->db->escape($data['filter_date_end']) . "')";
        }

        $query = $this->db->query($sql);

        return (int)$query->row['total'];
    }

    public function getTaxes(array $data = []): array {
        $sql = "SELECT MIN(o.`date_added`) AS `date_start`, MAX(o.`date_added`) AS `date_end`, ot.`title`, SUM(ot.`value`) AS `total`, COUNT(o.`order_id`) AS `orders` FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "order_total` ot ON (ot.`order_id` = o.`order_id`) WHERE ot.`code` = 'tax'";

        if (!empty($data['filter_order_status_id'])) {
            $sql .= " AND o.`order_status_id` = '" . (int)$data['filter_order_status_id'] . "'";
        } else {
            $sql .= " AND o.`order_status_id` > '0'";
        }

        if (!empty($data['filter_date_start'])) {
            $sql .= " AND DATE(o.`date_added`) >= DATE('" . $this->db->escape($data['filter_date_start']) . "')";
        }

        if (!empty($data['filter_date_end'])) {
            $sql .= " AND DATE(o.`date_added`) <= DATE('" . $this->db->escape($data['filter_date_end']) . "')";
        }

        if (!empty($data['filter_group'])) {
            $group = $data['filter_group'];
        } else {
            $group = 'week';
        }

        switch ($group) {
            case 'day';
                $sql .= " GROUP BY YEAR(o.`date_added`), MONTH(o.`date_added`), DAY(o.`date_added`), ot.`title`";
                break;
            default:
            case 'week':
                $sql .= " GROUP BY YEAR(o.`date_added`), WEEK(o.`date_added`), ot.`title`";
                break;
            case 'month':
                $sql .= " GROUP BY YEAR(o.`date_added`), MONTH(o.`date_added`), ot.`titl`e";
                break;
            case 'year':
                $sql .= " GROUP BY YEAR(o.`date_added`), ot.`title`";
                break;
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

    public function getTotalTaxes(array $data = []): int {
        if (!empty($data['filter_group'])) {
            $group = $data['filter_group'];
        } else {
            $group = 'week';
        }

        switch ($group) {
            case 'day';
                $sql = "SELECT COUNT(DISTINCT YEAR(o.`date_added`), MONTH(o.`date_added`), DAY(o.`date_added`), ot.`title`) AS `total` FROM `" . DB_PREFIX . "order` o";
                break;
            default:
            case 'week':
                $sql = "SELECT COUNT(DISTINCT YEAR(o.`date_added`), WEEK(o.`date_added`), ot.`title`) AS `total` FROM `" . DB_PREFIX . "order` o";
                break;
            case 'month':
                $sql = "SELECT COUNT(DISTINCT YEAR(o.`date_added`), MONTH(o.`date_added`), ot.`title`) AS `total` FROM `" . DB_PREFIX . "order` o";
                break;
            case 'year':
                $sql = "SELECT COUNT(DISTINCT YEAR(o.`date_added`), ot.`title`) AS `total` FROM `" . DB_PREFIX . "order` o";
                break;
        }

        $sql .= " LEFT JOIN `" . DB_PREFIX . "order_total` ot ON (o.`order_id` = ot.`order_id`) WHERE ot.`code` = 'tax'";

        if (!empty($data['filter_order_status_id'])) {
            $sql .= " AND o.`order_status_id` = '" . (int)$data['filter_order_status_id'] . "'";
        } else {
            $sql .= " AND o.`order_status_id` > '0'";
        }

        if (!empty($data['filter_date_start'])) {
            $sql .= " AND DATE(o.`date_added`) >= DATE('" . $this->db->escape($data['filter_date_start']) . "')";
        }

        if (!empty($data['filter_date_end'])) {
            $sql .= " AND DATE(o.`date_added`) <= DATE('" . $this->db->escape($data['filter_date_end']) . "')";
        }

        $query = $this->db->query($sql);

        return (int)$query->row['total'];
    }

    public function getShipping(array $data = []): array {
        $sql = "SELECT MIN(o.`date_added`) AS `date_start`, MAX(o.`date_added`) AS `date_end`, ot.`title`, SUM(ot.`value`) AS `total`, COUNT(o.`order_id`) AS `orders` FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "order_total` ot ON (o.`order_id` = ot.`order_id`) WHERE ot.`code` = 'shipping'";

        if (!empty($data['filter_order_status_id'])) {
            $sql .= " AND o.`order_status_id` = '" . (int)$data['filter_order_status_id'] . "'";
        } else {
            $sql .= " AND o.`order_status_id` > '0'";
        }

        if (!empty($data['filter_date_start'])) {
            $sql .= " AND DATE(o.`date_added`) >= DATE('" . $this->db->escape($data['filter_date_start']) . "')";
        }

        if (!empty($data['filter_date_end'])) {
            $sql .= " AND DATE(o.`date_added`) <= DATE('" . $this->db->escape($data['filter_date_end']) . "')";
        }

        if (!empty($data['filter_group'])) {
            $group = $data['filter_group'];
        } else {
            $group = 'week';
        }

        switch ($group) {
            case 'day';
                $sql .= " GROUP BY YEAR(o.`date_added`), MONTH(o.`date_added`), DAY(o.`date_added`), ot.`title`";
                break;
            default:
            case 'week':
                $sql .= " GROUP BY YEAR(o.`date_added`), WEEK(o.`date_added`), ot.`title`";
                break;
            case 'month':
                $sql .= " GROUP BY YEAR(o.`date_added`), MONTH(o.`date_added`), ot.`title`";
                break;
            case 'year':
                $sql .= " GROUP BY YEAR(o.`date_added`), ot.`title`";
                break;
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

    public function getTotalShipping(array $data = []): int {
        if (!empty($data['filter_group'])) {
            $group = $data['filter_group'];
        } else {
            $group = 'week';
        }

        switch ($group) {
            case 'day';
                $sql = "SELECT COUNT(DISTINCT YEAR(o.`date_added`), MONTH(o.`date_added`), DAY(o.`date_added`), ot.`title`) AS `total` FROM `" . DB_PREFIX . "order` o";
                break;
            default:
            case 'week':
                $sql = "SELECT COUNT(DISTINCT YEAR(o.`date_added`), WEEK(o.`date_added`), ot.`title`) AS `total` FROM `" . DB_PREFIX . "order` o";
                break;
            case 'month':
                $sql = "SELECT COUNT(DISTINCT YEAR(o.`date_added`), MONTH(o.`date_added`), ot.`title`) AS `total` FROM `" . DB_PREFIX . "order` o";
                break;
            case 'year':
                $sql = "SELECT COUNT(DISTINCT YEAR(o.`date_added`), ot.`title`) AS `total` FROM `" . DB_PREFIX . "order` o";
                break;
        }

        $sql .= " LEFT JOIN `" . DB_PREFIX . "order_total` ot ON (o.`order_id` = ot.`order_id`) WHERE ot.`code` = 'shipping'";

        if (!empty($data['filter_order_status_id'])) {
            $sql .= " AND `order_status_id` = '" . (int)$data['filter_order_status_id'] . "'";
        } else {
            $sql .= " AND `order_status_id` > '0'";
        }

        if (!empty($data['filter_date_start'])) {
            $sql .= " AND DATE(o.`date_added`) >= DATE('" . $this->db->escape($data['filter_date_start']) . "')";
        }

        if (!empty($data['filter_date_end'])) {
            $sql .= " AND DATE(o.`date_added`) <= DATE('" . $this->db->escape($data['filter_date_end']) . "')";
        }

        $query = $this->db->query($sql);

        return (int)$query->row['total'];
    }
}