<?php
class ModelExtensionReportAffiliateTransaction extends Model {
	public function getTransactions($data = array()) {
		$sql = "SELECT ct.customer_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, c.email, cgd.name AS customer_group, c.status, SUM(ct.amount) AS total, (SELECT SUM(ct2.amount) FROM `" . DB_PREFIX . "customer_affiliate_transaction` ct2 WHERE ct2.customer_id = ct.customer_id AND ct2.amount > 0) AS total_earnings, COUNT(o.order_id) AS orders, SUM(o.total) AS order_total FROM `" . DB_PREFIX . "customer_affiliate_transaction` ct LEFT JOIN `" . DB_PREFIX . "customer` c ON (ct.customer_id = c.customer_id) LEFT JOIN `" . DB_PREFIX . "customer_group_description` cgd ON (c.customer_group_id = cgd.customer_group_id) LEFT JOIN `" . DB_PREFIX . "order` o ON o.order_id = ct.order_id LEFT JOIN " . DB_PREFIX . "customer_affiliate ca ON ca.customer_id = c.customer_id WHERE c.customer_id IS NOT NULL AND c.status = '1' AND ca.customer_id IS NOT NULL AND ca.status = '1' AND cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'";

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

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

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
		}

		$sql .= " GROUP BY ct.customer_id ";
		
		if (!empty($data['filter_balance']) && preg_match("/^\d+(?:\.\d+)?$/", $data['filter_balance']) && floatval($data['filter_balance']) > 0) {			
			$sql .= " HAVING SUM(ct.amount) >= '" . floatval($data['filter_balance']) . "' ";
		}
		
		$sql .= " ORDER BY total 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 getTotalTransactions($data = array()) {
		$sql = "SELECT COUNT(DISTINCT ct.customer_id) AS total FROM `" . DB_PREFIX . "customer_affiliate_transaction` ct LEFT JOIN `" . DB_PREFIX . "customer` c ON ct.customer_id = c.customer_id LEFT JOIN " . DB_PREFIX . "customer_affiliate ca ON ca.customer_id = c.customer_id";

		$implode = array(
			"c.customer_id IS NOT NULL AND c.status = '1' AND ca.customer_id IS NOT NULL AND ca.status = '1'"
		);

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

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

		if (!empty($data['filter_customer'])) {
			$implode[] = "CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
		}

		if (!empty($data['filter_balance']) && preg_match("/^\d+(?:\.\d+)?$/", $data['filter_balance']) && floatval($data['filter_balance']) > 0) {
			$implode[] = " (SELECT SUM(ct2.amount) FROM `" . DB_PREFIX . "customer_affiliate_transaction` ct2 WHERE ct2.customer_id = ct.customer_id) >= '" . floatval($data['filter_balance']) . "'";
		}	

		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}

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

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