coupon.php (8611B)
1 <?php 2 class ModelExtensionTotalCoupon extends Model { 3 public function getCoupon($code) { 4 $status = true; 5 6 $coupon_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon` WHERE code = '" . $this->db->escape($code) . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'"); 7 8 if ($coupon_query->num_rows) { 9 if ($coupon_query->row['total'] > $this->cart->getSubTotal()) { 10 $status = false; 11 } 12 13 $coupon_total = $this->getTotalCouponHistoriesByCoupon($code); 14 15 if ($coupon_query->row['uses_total'] > 0 && ($coupon_total >= $coupon_query->row['uses_total'])) { 16 $status = false; 17 } 18 19 if ($coupon_query->row['logged'] && !$this->customer->getId()) { 20 $status = false; 21 } 22 23 if ($this->customer->getId()) { 24 $customer_total = $this->getTotalCouponHistoriesByCustomerId($code, $this->customer->getId()); 25 26 if ($coupon_query->row['uses_customer'] > 0 && ($customer_total >= $coupon_query->row['uses_customer'])) { 27 $status = false; 28 } 29 } 30 31 // Products 32 $coupon_product_data = array(); 33 34 $coupon_product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_product` WHERE coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "'"); 35 36 foreach ($coupon_product_query->rows as $product) { 37 $coupon_product_data[] = $product['product_id']; 38 } 39 40 // Categories 41 $coupon_category_data = array(); 42 43 $coupon_category_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_category` cc LEFT JOIN `" . DB_PREFIX . "category_path` cp ON (cc.category_id = cp.path_id) WHERE cc.coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "'"); 44 45 foreach ($coupon_category_query->rows as $category) { 46 $coupon_category_data[] = $category['category_id']; 47 } 48 49 $product_data = array(); 50 51 if ($coupon_product_data || $coupon_category_data) { 52 foreach ($this->cart->getProducts() as $product) { 53 if (in_array($product['product_id'], $coupon_product_data)) { 54 $product_data[] = $product['product_id']; 55 56 continue; 57 } 58 59 foreach ($coupon_category_data as $category_id) { 60 $coupon_category_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_category` WHERE `product_id` = '" . (int)$product['product_id'] . "' AND category_id = '" . (int)$category_id . "'"); 61 62 if ($coupon_category_query->row['total']) { 63 $product_data[] = $product['product_id']; 64 65 continue; 66 } 67 } 68 } 69 70 if (!$product_data) { 71 $status = false; 72 } 73 } 74 } else { 75 $status = false; 76 } 77 78 if ($status) { 79 return array( 80 'coupon_id' => $coupon_query->row['coupon_id'], 81 'code' => $coupon_query->row['code'], 82 'name' => $coupon_query->row['name'], 83 'type' => $coupon_query->row['type'], 84 'discount' => $coupon_query->row['discount'], 85 'shipping' => $coupon_query->row['shipping'], 86 'total' => $coupon_query->row['total'], 87 'product' => $product_data, 88 'date_start' => $coupon_query->row['date_start'], 89 'date_end' => $coupon_query->row['date_end'], 90 'uses_total' => $coupon_query->row['uses_total'], 91 'uses_customer' => $coupon_query->row['uses_customer'], 92 'status' => $coupon_query->row['status'], 93 'date_added' => $coupon_query->row['date_added'] 94 ); 95 } 96 } 97 98 public function getTotal($total) { 99 if (isset($this->session->data['coupon'])) { 100 $this->load->language('extension/total/coupon', 'coupon'); 101 102 $coupon_info = $this->getCoupon($this->session->data['coupon']); 103 104 if ($coupon_info) { 105 $discount_total = 0; 106 107 if (!$coupon_info['product']) { 108 $sub_total = $this->cart->getSubTotal(); 109 } else { 110 $sub_total = 0; 111 112 foreach ($this->cart->getProducts() as $product) { 113 if (in_array($product['product_id'], $coupon_info['product'])) { 114 $sub_total += $product['total']; 115 } 116 } 117 } 118 119 if ($coupon_info['type'] == 'F') { 120 $coupon_info['discount'] = min($coupon_info['discount'], $sub_total); 121 } 122 123 foreach ($this->cart->getProducts() as $product) { 124 $discount = 0; 125 126 if (!$coupon_info['product']) { 127 $status = true; 128 } else { 129 $status = in_array($product['product_id'], $coupon_info['product']); 130 } 131 132 if ($status) { 133 if ($coupon_info['type'] == 'F') { 134 $discount = $coupon_info['discount'] * ($product['total'] / $sub_total); 135 } elseif ($coupon_info['type'] == 'P') { 136 $discount = $product['total'] / 100 * $coupon_info['discount']; 137 } 138 139 if ($product['tax_class_id']) { 140 $tax_rates = $this->tax->getRates($product['total'] - ($product['total'] - $discount), $product['tax_class_id']); 141 142 foreach ($tax_rates as $tax_rate) { 143 if ($tax_rate['type'] == 'P') { 144 $total['taxes'][$tax_rate['tax_rate_id']] -= $tax_rate['amount']; 145 } 146 } 147 } 148 } 149 150 $discount_total += $discount; 151 } 152 153 if ($coupon_info['shipping'] && isset($this->session->data['shipping_method'])) { 154 if (!empty($this->session->data['shipping_method']['tax_class_id'])) { 155 $tax_rates = $this->tax->getRates($this->session->data['shipping_method']['cost'], $this->session->data['shipping_method']['tax_class_id']); 156 157 foreach ($tax_rates as $tax_rate) { 158 if ($tax_rate['type'] == 'P') { 159 $total['taxes'][$tax_rate['tax_rate_id']] -= $tax_rate['amount']; 160 } 161 } 162 } 163 164 $discount_total += $this->session->data['shipping_method']['cost']; 165 } 166 167 // If discount greater than total 168 if ($discount_total > $total['total']) { 169 $discount_total = $total['total']; 170 } 171 172 if ($discount_total > 0) { 173 $total['totals'][] = array( 174 'code' => 'coupon', 175 'title' => sprintf($this->language->get('coupon')->get('text_coupon'), $this->session->data['coupon']), 176 'value' => -$discount_total, 177 'sort_order' => $this->config->get('total_coupon_sort_order') 178 ); 179 180 $total['total'] -= $discount_total; 181 } 182 } 183 } 184 } 185 186 public function confirm($order_info, $order_total) { 187 $code = ''; 188 189 $start = strpos($order_total['title'], '(') + 1; 190 $end = strrpos($order_total['title'], ')'); 191 192 if ($start && $end) { 193 $code = substr($order_total['title'], $start, $end - $start); 194 } 195 196 if ($code) { 197 $status = true; 198 199 $coupon_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon` WHERE code = '" . $this->db->escape($code) . "' AND status = '1'"); 200 201 if ($coupon_query->num_rows) { 202 $coupon_total = $this->getTotalCouponHistoriesByCoupon($code); 203 204 if ($coupon_query->row['uses_total'] > 0 && ($coupon_total >= $coupon_query->row['uses_total'])) { 205 $status = false; 206 } 207 208 if ($order_info['customer_id']) { 209 $customer_total = $this->getTotalCouponHistoriesByCustomerId($code, $order_info['customer_id']); 210 211 if ($coupon_query->row['uses_customer'] > 0 && ($customer_total >= $coupon_query->row['uses_customer'])) { 212 $status = false; 213 } 214 } 215 } else { 216 $status = false; 217 } 218 219 if ($status) { 220 $this->db->query("INSERT INTO `" . DB_PREFIX . "coupon_history` SET coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "', order_id = '" . (int)$order_info['order_id'] . "', customer_id = '" . (int)$order_info['customer_id'] . "', amount = '" . (float)$order_total['value'] . "', date_added = NOW()"); 221 } else { 222 return $this->config->get('config_fraud_status_id'); 223 } 224 } 225 } 226 227 public function unconfirm($order_id) { 228 $this->db->query("DELETE FROM `" . DB_PREFIX . "coupon_history` WHERE order_id = '" . (int)$order_id . "'"); 229 } 230 231 public function getTotalCouponHistoriesByCoupon($coupon) { 232 $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "coupon_history` ch LEFT JOIN `" . DB_PREFIX . "coupon` c ON (ch.coupon_id = c.coupon_id) WHERE c.code = '" . $this->db->escape($coupon) . "'"); 233 234 return $query->row['total']; 235 } 236 237 public function getTotalCouponHistoriesByCustomerId($coupon, $customer_id) { 238 $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "coupon_history` ch LEFT JOIN `" . DB_PREFIX . "coupon` c ON (ch.coupon_id = c.coupon_id) WHERE c.code = '" . $this->db->escape($coupon) . "' AND ch.customer_id = '" . (int)$customer_id . "'"); 239 240 return $query->row['total']; 241 } 242 }