<?php
class ModelTotalCoupon extends Model
{
	public function getTotal(&$total_data, &$total, $calc_shipping = true)
	{
		if (isset($this->session->data['coupon'])) {
			$this->language->load('total/coupon');

			$this->load->model('checkout/coupon');
			$this->load->model('catalog/product');
			$this->load->model('catalog/coupon');

			$coupon_info = $this->model_checkout_coupon->getCoupon($this->session->data['coupon']);

			if ($coupon_info) {
				$discount_total = 0;

				if (!$coupon_info['product']) {
					$sub_total = $this->cart->getSubTotal();
				} else {
					$sub_total = 0;

					foreach ($this->cart->getProducts() as $product) {
						if (in_array($product['product_id'], $coupon_info['product']) || in_array($product['grid_id'], $coupon_info['product_grid'])) {
							$sub_total += $product['total'];
						}
					}
				}

				if ($coupon_info['type'] == 'F') {
					$coupon_info['discount'] = min($coupon_info['discount'], $sub_total);
				}

				if(count($coupon_info['product']) == 0 && count($coupon_info['product_grid']) == 0){
					$discount_total = $coupon_info['type'] == 'F' ? $coupon_info['discount'] : ($sub_total / 100) * $coupon_info['discount'];
				}else{
					foreach ($this->cart->getProducts() as $product) {
						$discount = 0;
	
						if (!$coupon_info['product']) {
							$status = true;
						} else {
							if (in_array($product['product_id'], $coupon_info['product']) || in_array($product['grid_id'], $coupon_info['product_grid'])) {
								$status = true;
							} else {
								$status = false;
							}
						}
	
						$couponProduct = $this->model_catalog_coupon->getProductCoupom($coupon_info['coupon_id'], $product['product_id']);
						$couponDepartment = $this->model_catalog_coupon->getDepartmentCoupom($coupon_info['coupon_id'], $product['product_id']);
						$couponGrid = null;
	
						if ($product['grid_id'] != false) {
							$couponGrid = $this->model_catalog_coupon->getGridCoupom($coupon_info['coupon_id'], $product['grid_id']);
						}
	
						if ($status) {
							switch ($couponProduct || $couponGrid || $couponDepartment) {
								case isset($couponProduct):
									if ($coupon_info['type'] == 'F' && !empty($coupon_info['quantity_product'])) {
										$quantity = $product['quantity'] >= $coupon_info['quantity_product'] ? $coupon_info['quantity_product'] : $product['quantity'];
										$discount = $coupon_info['discount'] * $quantity;
									} elseif ($coupon_info['type'] == 'F' && empty($coupon_info['quantity_product'])) {
										$discount = $coupon_info['discount'] * $product['quantity'];
									} elseif ($coupon_info['type'] == 'P' && !empty($coupon_info['quantity_product'])) {
										$quantity = $product['quantity'] >= $coupon_info['quantity_product'] ? $coupon_info['quantity_product'] : $product['quantity'];
										$discount = ($product['price'] * $quantity) / 100 * $coupon_info['discount'];
									} elseif ($coupon_info['type'] == 'P' && empty($coupon_info['quantity_product'])) {
										$discount = $product['total'] / 100 * $coupon_info['discount'];
									}
	
									break;
								case isset($couponGrid):
									if ($coupon_info['type'] == 'F' && !empty($coupon_info['quantity_grade'])) {
										$quantity = $product['quantity'] >= $coupon_info['quantity_grade'] ? $coupon_info['quantity_grade'] : $product['quantity'];
										$discount = $coupon_info['discount'] * $quantity;
									} elseif ($coupon_info['type'] == 'F' && empty($coupon_info['quantity_grade'])) {
										$discount = $coupon_info['discount'] * $product['quantity'];
									} elseif ($coupon_info['type'] == 'P' && !empty($coupon_info['quantity_grade'])) {
										$quantity = $product['quantity'] >= $coupon_info['quantity_grade'] ? $coupon_info['quantity_grade'] : $product['quantity'];
										$discount = ($product['price'] * $quantity) / 100 * $coupon_info['discount'];
									} elseif ($coupon_info['type'] == 'P' && empty($coupon_info['quantity_grade'])) {
										$discount = $product['total'] / 100 * $coupon_info['discount'];
									}
	
									break;
								case isset($couponDepartment):
									if ($coupon_info['type'] == 'F' && !empty($coupon_info['quantity_department'])) {
										$quantity = $product['quantity'] >= $coupon_info['quantity_department'] ? $coupon_info['quantity_department'] : $product['quantity'];
										$discount = $coupon_info['discount'] * $quantity;
									} elseif ($coupon_info['type'] == 'F' && empty($coupon_info['quantity_department'])) {
										$discount = $coupon_info['discount'] * $product['quantity'];
									} elseif ($coupon_info['type'] == 'P' && !empty($coupon_info['quantity_department'])) {
										$quantity = $product['quantity'] >= $coupon_info['quantity_department'] ? $coupon_info['quantity_department'] : $product['quantity'];
										$discount = ($product['price'] * $quantity) / 100 * $coupon_info['discount'];
									} elseif ($coupon_info['type'] == 'P' && empty($coupon_info['quantity_department'])) {
										$discount = $product['total'] / 100 * $coupon_info['discount'];
									}
	
									break;
							}
						}
						$discount_total += $discount;
					}
				}


				$shippingFree = false;
				if ($coupon_info['shipping'] && isset($this->session->data['shipping_method']) && $calc_shipping) {
					$shippingFree = true;
					$shippingFreeText = ($discount_total <> 0) ? $this->language->get('text_coupon_shipping_free') . ' / ' . $this->currency->format(-$discount_total) : $this->language->get('text_coupon_shipping_free');
				}

				$total_data[] = array(
					'code' => 'coupon',
					'title' => sprintf($this->language->get('text_coupon'), $this->session->data['coupon']),
					'text' => !$shippingFree ? $this->currency->format(-$discount_total) : $shippingFreeText,
					'value' => -$discount_total,
					'sort_order' => $this->config->get('coupon_sort_order')
				);

				$total -= $discount_total;
			}
		}
	}

	public function confirm($order_info, $order_total)
	{
		$code = '';

		$start = strpos($order_total['title'], '(') + 1;
		$end = strrpos($order_total['title'], ')');

		if ($start && $end) {
			$code = substr($order_total['title'], $start, $end - $start);
		}

		$this->load->model('checkout/coupon');

		$coupon_info = $this->model_checkout_coupon->getCoupon($code);

		if ($coupon_info) {
			$this->model_checkout_coupon->redeem($coupon_info['coupon_id'], $order_info['order_id'], $order_info['customer_id'], $order_total['value']);
		}
	}
}