| 1: | <?php
|
| 2: | namespace Opencart\Catalog\Model\Account;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Transaction extends \Opencart\System\Engine\Model {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: |
|
| 19: | public function addTransaction(int $customer_id, int $order_id, string $description, float $amount): void {
|
| 20: | $this->db->query("INSERT INTO `" . DB_PREFIX . "customer_transaction` SET `customer_id` = '" . (int)$customer_id . "', `order_id` = '" . (int)$order_id . "', `description` = '" . $this->db->escape($description) . "', `amount` = '" . (float)$amount . "', `date_added` = NOW()");
|
| 21: | }
|
| 22: |
|
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: |
|
| 31: | public function deleteTransaction(int $customer_id, int $order_id = 0): void {
|
| 32: | $sql = "DELETE FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$customer_id . "'";
|
| 33: |
|
| 34: | if ($order_id) {
|
| 35: | $sql .= " AND `order_id` = '" . (int)$order_id . "'";
|
| 36: | }
|
| 37: |
|
| 38: | $this->db->query($sql);
|
| 39: | }
|
| 40: |
|
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: |
|
| 48: | public function deleteTransactionByOrderId(int $order_id): void {
|
| 49: | $this->db->query("DELETE FROM `" . DB_PREFIX . "customer_transaction` WHERE `order_id` = '" . (int)$order_id . "' AND `amount` < 0");
|
| 50: | }
|
| 51: |
|
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: |
|
| 60: | public function getTransactions(int $customer_id, array $data = []): array {
|
| 61: | $sql = "SELECT * FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$customer_id . "'";
|
| 62: |
|
| 63: | $sort_data = [
|
| 64: | 'amount',
|
| 65: | 'description',
|
| 66: | 'date_added'
|
| 67: | ];
|
| 68: |
|
| 69: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
| 70: | $sql .= " ORDER BY `" . $data['sort'] . "`";
|
| 71: | } else {
|
| 72: | $sql .= " ORDER BY `date_added`";
|
| 73: | }
|
| 74: |
|
| 75: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
| 76: | $sql .= " DESC";
|
| 77: | } else {
|
| 78: | $sql .= " ASC";
|
| 79: | }
|
| 80: |
|
| 81: | if (isset($data['start']) || isset($data['limit'])) {
|
| 82: | if ($data['start'] < 0) {
|
| 83: | $data['start'] = 0;
|
| 84: | }
|
| 85: |
|
| 86: | if ($data['limit'] < 1) {
|
| 87: | $data['limit'] = 20;
|
| 88: | }
|
| 89: |
|
| 90: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
| 91: | }
|
| 92: |
|
| 93: | $query = $this->db->query($sql);
|
| 94: |
|
| 95: | return $query->rows;
|
| 96: | }
|
| 97: |
|
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: |
|
| 105: | public function getTotalTransactions(int $customer_id): int {
|
| 106: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$customer_id . "'");
|
| 107: |
|
| 108: | return (int)$query->row['total'];
|
| 109: | }
|
| 110: |
|
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: |
|
| 118: | public function getTotalTransactionsByOrderId(int $order_id): int {
|
| 119: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_transaction` WHERE `order_id` = '" . (int)$order_id . "'");
|
| 120: |
|
| 121: | return (int)$query->row['total'];
|
| 122: | }
|
| 123: |
|
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: |
|
| 131: | public function getTransactionTotal(int $customer_id): float {
|
| 132: | $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$customer_id . "' GROUP BY `customer_id`");
|
| 133: |
|
| 134: | if ($query->num_rows) {
|
| 135: | return (int)$query->row['total'];
|
| 136: | } else {
|
| 137: | return 0;
|
| 138: | }
|
| 139: | }
|
| 140: | }
|
| 141: | |