1: | <?php
2: | namespace Opencart\Catalog\Model\Account;
3: | /**
4: | * Class Transaction
5: | *
6: | * @package Opencart\Catalog\Model\Account
7: | */
8: | class Transaction extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Transaction
11: | *
12: | * @param int $customer_id
13: | * @param int $order_id
14: | * @param string $description
15: | * @param float $amount
16: | *
17: | * @return void
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: | * Delete Transaction
25: | *
26: | * @param int $customer_id
27: | * @param int $order_id
28: | *
29: | * @return void
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: | * Delete Transaction By Order ID
43: | *
44: | * @param int $order_id
45: | *
46: | * @return void
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: | * Get Transactions
54: | *
55: | * @param int $customer_id
56: | * @param array<string, mixed> $data
57: | *
58: | * @return array<int, array<string, mixed>>
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: | * Get Total Transactions
100: | *
101: | * @param int $customer_id
102: | *
103: | * @return int
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: | * Get Total Transactions By Order ID
113: | *
114: | * @param int $order_id
115: | *
116: | * @return int
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: | * Get Transaction Total
126: | *
127: | * @param int $customer_id
128: | *
129: | * @return float
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: |

OpenCart API API documentation generated by ApiGen dev-master