Back to Opencart

File admin\model\marketing\coupon.php

docs/api/source-admin.model.marketing.coupon.html

4.1.0.316.6 KB
Original Source

Namespaces

Classes

| 1: | <?php | | 2: | namespace Opencart\Admin\Model\Marketing; | | 3: | /** | | 4: | * Class Coupon | | 5: | * | | 6: | * @package Opencart\Admin\Model\Marketing | | 7: | */ | | 8: | class Coupon extends \Opencart\System\Engine\Model { | | 9: | /** | | 10: | * Add Coupon | | 11: | * | | 12: | * @param array<string, mixed> $data | | 13: | * | | 14: | * @return int | | 15: | */ | | 16: | public function addCoupon(array $data): int { | | 17: | $this->db->query("INSERT INTO " . DB\_PREFIX . "coupon SET name = '" . $this->db->escape((string)$data['name']) . "', code = '" . $this->db->escape((string)$data['code']) . "', discount = '" . (float)$data['discount'] . "', type = '" . $this->db->escape((string)$data['type']) . "', total = '" . (float)$data['total'] . "', logged = '" . (isset($data['logged']) ? (bool)$data['logged'] : 0) . "', shipping = '" . (isset($data['shipping']) ? (bool)$data['shipping'] : 0) . "', date_start = '" . $this->db->escape((string)$data['date_start']) . "', date_end = '" . $this->db->escape((string)$data['date_end']) . "', uses_total = '" . (int)$data['uses_total'] . "', uses_customer = '" . (int)$data['uses_customer'] . "', status = '" . (bool)($data['status'] ?? 0) . "', date_added = NOW()"); | | 18: | | | 19: | $coupon_id = $this->db->getLastId(); | | 20: | | | 21: | if (isset($data['coupon_product'])) { | | 22: | foreach ($data['coupon_product'] as $product_id) { | | 23: | $this->addProduct($coupon_id, $product_id); | | 24: | } | | 25: | } | | 26: | | | 27: | if (isset($data['coupon_category'])) { | | 28: | foreach ($data['coupon_category'] as $category_id) { | | 29: | $this->addCategory($coupon_id, $category_id); | | 30: | } | | 31: | } | | 32: | | | 33: | return $coupon_id; | | 34: | } | | 35: | | | 36: | /** | | 37: | * Edit Coupon | | 38: | * | | 39: | * @param int $coupon_id | | 40: | * @param array<string, mixed> $data | | 41: | * | | 42: | * @return void | | 43: | */ | | 44: | public function editCoupon(int $coupon_id, array $data): void { | | 45: | $this->db->query("UPDATE " . DB\_PREFIX . "coupon SET name = '" . $this->db->escape((string)$data['name']) . "', code = '" . $this->db->escape((string)$data['code']) . "', discount = '" . (float)$data['discount'] . "', type = '" . $this->db->escape((string)$data['type']) . "', total = '" . (float)$data['total'] . "', logged = '" . (isset($data['logged']) ? (bool)$data['logged'] : 0) . "', shipping = '" . (isset($data['shipping']) ? (bool)$data['shipping'] : 0) . "', date_start = '" . $this->db->escape((string)$data['date_start']) . "', date_end = '" . $this->db->escape((string)$data['date_end']) . "', uses_total = '" . (int)$data['uses_total'] . "', uses_customer = '" . (int)$data['uses_customer'] . "', status = '" . (bool)($data['status'] ?? 0) . "' WHERE coupon_id = '" . (int)$coupon_id . "'"); | | 46: | | | 47: | $this->deleteProducts($coupon_id); | | 48: | | | 49: | if (isset($data['coupon_product'])) { | | 50: | foreach ($data['coupon_product'] as $product_id) { | | 51: | $this->addProduct($coupon_id, $product_id); | | 52: | } | | 53: | } | | 54: | | | 55: | $this->deleteCategories($coupon_id); | | 56: | | | 57: | if (isset($data['coupon_category'])) { | | 58: | foreach ($data['coupon_category'] as $category_id) { | | 59: | $this->addCategory($coupon_id, $category_id); | | 60: | } | | 61: | } | | 62: | } | | 63: | | | 64: | /** | | 65: | * Delete Coupon | | 66: | * | | 67: | * @param int $coupon_id | | 68: | * | | 69: | * @return void | | 70: | */ | | 71: | public function deleteCoupon(int $coupon_id): void { | | 72: | $this->db->query("DELETE FROM " . DB\_PREFIX . "coupon WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 73: | | | 74: | $this->deleteProducts($coupon_id); | | 75: | $this->deleteCategories($coupon_id); | | 76: | $this->deleteHistories($coupon_id); | | 77: | } | | 78: | | | 79: | /** | | 80: | * Get Coupon | | 81: | * | | 82: | * @param int $coupon_id | | 83: | * | | 84: | * @return array<string, mixed> | | 85: | */ | | 86: | public function getCoupon(int $coupon_id): array { | | 87: | $query = $this->db->query("SELECT DISTINCT * FROM " . DB\_PREFIX . "coupon WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 88: | | | 89: | return $query->row; | | 90: | } | | 91: | | | 92: | /** | | 93: | * Get Coupon By Code | | 94: | * | | 95: | * @param string $code | | 96: | * | | 97: | * @return array<string, mixed> | | 98: | */ | | 99: | public function getCouponByCode(string $code): array { | | 100: | $query = $this->db->query("SELECT DISTINCT * FROM " . DB\_PREFIX . "coupon WHERE code = '" . $this->db->escape($code) . "'"); | | 101: | | | 102: | return $query->row; | | 103: | } | | 104: | | | 105: | /** | | 106: | * Get Coupons | | 107: | * | | 108: | * @param array<string, mixed> $data | | 109: | * | | 110: | * @return array<int, array<string, mixed>> | | 111: | */ | | 112: | public function getCoupons(array $data = []): array { | | 113: | $sql = "SELECT coupon_id, name, code, discount, date_start, date_end, status FROM " . DB\_PREFIX . "coupon"; | | 114: | | | 115: | $sort_data = [ | | 116: | 'name', | | 117: | 'code', | | 118: | 'discount', | | 119: | 'date_start', | | 120: | 'date_end', | | 121: | 'status' | | 122: | ]; | | 123: | | | 124: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { | | 125: | $sql .= " ORDER BY " . $data['sort']; | | 126: | } else { | | 127: | $sql .= " ORDER BY name"; | | 128: | } | | 129: | | | 130: | if (isset($data['order']) && ($data['order'] == 'DESC')) { | | 131: | $sql .= " DESC"; | | 132: | } else { | | 133: | $sql .= " ASC"; | | 134: | } | | 135: | | | 136: | if (isset($data['start']) || isset($data['limit'])) { | | 137: | if ($data['start'] < 0) { | | 138: | $data['start'] = 0; | | 139: | } | | 140: | | | 141: | if ($data['limit'] < 1) { | | 142: | $data['limit'] = 20; | | 143: | } | | 144: | | | 145: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; | | 146: | } | | 147: | | | 148: | $query = $this->db->query($sql); | | 149: | | | 150: | return $query->rows; | | 151: | } | | 152: | | | 153: | /** | | 154: | * Add Product | | 155: | * | | 156: | * @param int $coupon_id | | 157: | * @param int $product_id | | 158: | * | | 159: | * @return void | | 160: | */ | | 161: | public function addProduct(int $coupon_id, int $product_id): void { | | 162: | $this->db->query("INSERT INTO " . DB\_PREFIX . "coupon\_product SET coupon\_id = '" . (int)$coupon_id . "', product_id = '" . (int)$product_id . "'"); | | 163: | } | | 164: | | | 165: | /** | | 166: | * Delete Products | | 167: | * | | 168: | * @param int $coupon_id | | 169: | * | | 170: | * @return void | | 171: | */ | | 172: | public function deleteProducts(int $coupon_id): void { | | 173: | $this->db->query("DELETE FROM " . DB\_PREFIX . "coupon\_product WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 174: | } | | 175: | | | 176: | /** | | 177: | * Delete Products By Product ID | | 178: | * | | 179: | * @param int $product_id | | 180: | * | | 181: | * @return void | | 182: | */ | | 183: | public function deleteProductsByProductId(int $product_id): void { | | 184: | $this->db->query("DELETE FROM " . DB\_PREFIX . "coupon\_product WHERE product\_id = '" . (int)$product_id . "'"); | | 185: | } | | 186: | | | 187: | /** | | 188: | * Get Products | | 189: | * | | 190: | * @param int $coupon_id | | 191: | * | | 192: | * @return array<int, int> | | 193: | */ | | 194: | public function getProducts(int $coupon_id): array { | | 195: | $coupon_product_data = []; | | 196: | | | 197: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "coupon\_product WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 198: | | | 199: | foreach ($query->rows as $result) { | | 200: | $coupon_product_data[] = $result['product_id']; | | 201: | } | | 202: | | | 203: | return $coupon_product_data; | | 204: | } | | 205: | | | 206: | /** | | 207: | * Add Category | | 208: | * | | 209: | * @param int $coupon_id | | 210: | * @param int $category_id | | 211: | * | | 212: | * @return void | | 213: | */ | | 214: | public function addCategory(int $coupon_id, int $category_id): void { | | 215: | $this->db->query("INSERT INTO " . DB\_PREFIX . "coupon\_category SET coupon\_id = '" . (int)$coupon_id . "', category_id = '" . (int)$category_id . "'"); | | 216: | } | | 217: | | | 218: | /** | | 219: | * Delete Categories | | 220: | * | | 221: | * @param int $coupon_id | | 222: | * | | 223: | * @return void | | 224: | */ | | 225: | public function deleteCategories(int $coupon_id): void { | | 226: | $this->db->query("DELETE FROM " . DB\_PREFIX . "coupon\_category WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 227: | } | | 228: | | | 229: | /** | | 230: | * Delete Categories By Category ID | | 231: | * | | 232: | * @param int $category_id | | 233: | * | | 234: | * @return void | | 235: | */ | | 236: | public function deleteCategoriesByCategoryId(int $category_id): void { | | 237: | $this->db->query("DELETE FROM " . DB\_PREFIX . "coupon\_category WHERE category\_id = '" . (int)$category_id . "'"); | | 238: | } | | 239: | | | 240: | /** | | 241: | * Get Categories | | 242: | * | | 243: | * @param int $coupon_id | | 244: | * | | 245: | * @return array<int, int> | | 246: | */ | | 247: | public function getCategories(int $coupon_id): array { | | 248: | $coupon_category_data = []; | | 249: | | | 250: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "coupon\_category WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 251: | | | 252: | foreach ($query->rows as $result) { | | 253: | $coupon_category_data[] = $result['category_id']; | | 254: | } | | 255: | | | 256: | return $coupon_category_data; | | 257: | } | | 258: | | | 259: | /** | | 260: | * Get Total Coupons | | 261: | * | | 262: | * @return int | | 263: | */ | | 264: | public function getTotalCoupons(): int { | | 265: | $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB\_PREFIX . "coupon"); | | 266: | | | 267: | return (int)$query->row['total']; | | 268: | } | | 269: | | | 270: | /** | | 271: | * Get Histories | | 272: | * | | 273: | * @param int $coupon_id | | 274: | * @param int $start | | 275: | * @param int $limit | | 276: | * | | 277: | * @return array<int, array<string, mixed>> | | 278: | */ | | 279: | public function getHistories(int $coupon_id, int $start = 0, int $limit = 10): array { | | 280: | if ($start < 0) { | | 281: | $start = 0; | | 282: | } | | 283: | | | 284: | if ($limit < 1) { | | 285: | $limit = 10; | | 286: | } | | 287: | | | 288: | $query = $this->db->query("SELECT ch.order_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, ch.amount, ch.date_added FROM " . DB\_PREFIX . "coupon\_history ch LEFT JOIN " . DB\_PREFIX . "customer c ON (ch.customer\_id = c.customer\_id) WHERE ch.coupon\_id = '" . (int)$coupon_id . "' ORDER BY ch.date_added ASC LIMIT " . (int)$start . "," . (int)$limit); | | 289: | | | 290: | return $query->rows; | | 291: | } | | 292: | | | 293: | /** | | 294: | * Delete Coupon Histories | | 295: | * | | 296: | * @param int $coupon_id | | 297: | * | | 298: | * @return void | | 299: | */ | | 300: | public function deleteHistories(int $coupon_id): void { | | 301: | $this->db->query("DELETE FROM " . DB\_PREFIX . "coupon\_history WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 302: | } | | 303: | | | 304: | /** | | 305: | * Get Total Histories | | 306: | * | | 307: | * @param int $coupon_id | | 308: | * | | 309: | * @return int | | 310: | */ | | 311: | public function getTotalHistories(int $coupon_id): int { | | 312: | $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB\_PREFIX . "coupon\_history WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 313: | | | 314: | return (int)$query->row['total']; | | 315: | } | | 316: | } | | 317: | |

OpenCart API API documentation generated by ApiGen dev-master