PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/model/sale/return.php

https://gitlab.com/shapcy/opencart
PHP | 238 lines | 177 code | 61 blank | 0 comment | 34 complexity | cb65d945c55eafccb357a61a6f529876 MD5 | raw file
  1. <?php
  2. class ModelSaleReturn extends Model {
  3. public function addReturn($data) {
  4. $this->db->query("INSERT INTO `" . DB_PREFIX . "return` SET order_id = '" . (int)$data['order_id'] . "', product_id = '" . (int)$data['product_id'] . "', customer_id = '" . (int)$data['customer_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', product = '" . $this->db->escape($data['product']) . "', model = '" . $this->db->escape($data['model']) . "', quantity = '" . (int)$data['quantity'] . "', opened = '" . (int)$data['opened'] . "', return_reason_id = '" . (int)$data['return_reason_id'] . "', return_action_id = '" . (int)$data['return_action_id'] . "', return_status_id = '" . (int)$data['return_status_id'] . "', comment = '" . $this->db->escape($data['comment']) . "', date_ordered = '" . $this->db->escape($data['date_ordered']) . "', date_added = NOW(), date_modified = NOW()");
  5. return $this->db->getLastId();
  6. }
  7. public function editReturn($return_id, $data) {
  8. $this->db->query("UPDATE `" . DB_PREFIX . "return` SET order_id = '" . (int)$data['order_id'] . "', product_id = '" . (int)$data['product_id'] . "', customer_id = '" . (int)$data['customer_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', product = '" . $this->db->escape($data['product']) . "', model = '" . $this->db->escape($data['model']) . "', quantity = '" . (int)$data['quantity'] . "', opened = '" . (int)$data['opened'] . "', return_reason_id = '" . (int)$data['return_reason_id'] . "', return_action_id = '" . (int)$data['return_action_id'] . "', comment = '" . $this->db->escape($data['comment']) . "', date_ordered = '" . $this->db->escape($data['date_ordered']) . "', date_modified = NOW() WHERE return_id = '" . (int)$return_id . "'");
  9. }
  10. public function deleteReturn($return_id) {
  11. $this->db->query("DELETE FROM `" . DB_PREFIX . "return` WHERE return_id = '" . (int)$return_id . "'");
  12. $this->db->query("DELETE FROM " . DB_PREFIX . "return_history WHERE return_id = '" . (int)$return_id . "'");
  13. }
  14. public function getReturn($return_id) {
  15. $query = $this->db->query("SELECT DISTINCT *, (SELECT CONCAT(c.firstname, ' ', c.lastname) FROM " . DB_PREFIX . "customer c WHERE c.customer_id = r.customer_id) AS customer FROM `" . DB_PREFIX . "return` r WHERE r.return_id = '" . (int)$return_id . "'");
  16. return $query->row;
  17. }
  18. public function getReturns($data = array()) {
  19. $sql = "SELECT *, CONCAT(r.firstname, ' ', r.lastname) AS customer, (SELECT rs.name FROM " . DB_PREFIX . "return_status rs WHERE rs.return_status_id = r.return_status_id AND rs.language_id = '" . (int)$this->config->get('config_language_id') . "') AS status FROM `" . DB_PREFIX . "return` r";
  20. $implode = array();
  21. if (!empty($data['filter_return_id'])) {
  22. $implode[] = "r.return_id = '" . (int)$data['filter_return_id'] . "'";
  23. }
  24. if (!empty($data['filter_order_id'])) {
  25. $implode[] = "r.order_id = '" . (int)$data['filter_order_id'] . "'";
  26. }
  27. if (!empty($data['filter_customer'])) {
  28. $implode[] = "CONCAT(r.firstname, ' ', r.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "%'";
  29. }
  30. if (!empty($data['filter_product'])) {
  31. $implode[] = "r.product = '" . $this->db->escape($data['filter_product']) . "'";
  32. }
  33. if (!empty($data['filter_model'])) {
  34. $implode[] = "r.model = '" . $this->db->escape($data['filter_model']) . "'";
  35. }
  36. if (!empty($data['filter_return_status_id'])) {
  37. $implode[] = "r.return_status_id = '" . (int)$data['filter_return_status_id'] . "'";
  38. }
  39. if (!empty($data['filter_date_added'])) {
  40. $implode[] = "DATE(r.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  41. }
  42. if (!empty($data['filter_date_modified'])) {
  43. $implode[] = "DATE(r.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
  44. }
  45. if ($implode) {
  46. $sql .= " WHERE " . implode(" AND ", $implode);
  47. }
  48. $sort_data = array(
  49. 'r.return_id',
  50. 'r.order_id',
  51. 'customer',
  52. 'r.product',
  53. 'r.model',
  54. 'status',
  55. 'r.date_added',
  56. 'r.date_modified'
  57. );
  58. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  59. $sql .= " ORDER BY " . $data['sort'];
  60. } else {
  61. $sql .= " ORDER BY r.return_id";
  62. }
  63. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  64. $sql .= " DESC";
  65. } else {
  66. $sql .= " ASC";
  67. }
  68. if (isset($data['start']) || isset($data['limit'])) {
  69. if ($data['start'] < 0) {
  70. $data['start'] = 0;
  71. }
  72. if ($data['limit'] < 1) {
  73. $data['limit'] = 20;
  74. }
  75. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  76. }
  77. $query = $this->db->query($sql);
  78. return $query->rows;
  79. }
  80. public function getTotalReturns($data = array()) {
  81. $sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return`r";
  82. $implode = array();
  83. if (!empty($data['filter_return_id'])) {
  84. $implode[] = "r.return_id = '" . (int)$data['filter_return_id'] . "'";
  85. }
  86. if (!empty($data['filter_customer'])) {
  87. $implode[] = "CONCAT(r.firstname, ' ', r.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "%'";
  88. }
  89. if (!empty($data['filter_order_id'])) {
  90. $implode[] = "r.order_id = '" . $this->db->escape($data['filter_order_id']) . "'";
  91. }
  92. if (!empty($data['filter_product'])) {
  93. $implode[] = "r.product = '" . $this->db->escape($data['filter_product']) . "'";
  94. }
  95. if (!empty($data['filter_model'])) {
  96. $implode[] = "r.model = '" . $this->db->escape($data['filter_model']) . "'";
  97. }
  98. if (!empty($data['filter_return_status_id'])) {
  99. $implode[] = "r.return_status_id = '" . (int)$data['filter_return_status_id'] . "'";
  100. }
  101. if (!empty($data['filter_date_added'])) {
  102. $implode[] = "DATE(r.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  103. }
  104. if (!empty($data['filter_date_modified'])) {
  105. $implode[] = "DATE(r.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
  106. }
  107. if ($implode) {
  108. $sql .= " WHERE " . implode(" AND ", $implode);
  109. }
  110. $query = $this->db->query($sql);
  111. return $query->row['total'];
  112. }
  113. public function getTotalReturnsByReturnStatusId($return_status_id) {
  114. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return` WHERE return_status_id = '" . (int)$return_status_id . "'");
  115. return $query->row['total'];
  116. }
  117. public function getTotalReturnsByReturnReasonId($return_reason_id) {
  118. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return` WHERE return_reason_id = '" . (int)$return_reason_id . "'");
  119. return $query->row['total'];
  120. }
  121. public function getTotalReturnsByReturnActionId($return_action_id) {
  122. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return` WHERE return_action_id = '" . (int)$return_action_id . "'");
  123. return $query->row['total'];
  124. }
  125. public function addReturnHistory($return_id, $data) {
  126. $this->db->query("UPDATE `" . DB_PREFIX . "return` SET return_status_id = '" . (int)$data['return_status_id'] . "', date_modified = NOW() WHERE return_id = '" . (int)$return_id . "'");
  127. $this->db->query("INSERT INTO " . DB_PREFIX . "return_history SET return_id = '" . (int)$return_id . "', return_status_id = '" . (int)$data['return_status_id'] . "', notify = '" . (isset($data['notify']) ? (int)$data['notify'] : 0) . "', comment = '" . $this->db->escape(strip_tags($data['comment'])) . "', date_added = NOW()");
  128. if ($data['notify']) {
  129. $return_query = $this->db->query("SELECT *, rs.name AS status FROM `" . DB_PREFIX . "return` r LEFT JOIN " . DB_PREFIX . "return_status rs ON (r.return_status_id = rs.return_status_id) WHERE r.return_id = '" . (int)$return_id . "' AND rs.language_id = '" . (int)$this->config->get('config_language_id') . "'");
  130. if ($return_query->num_rows) {
  131. $this->load->language('mail/return');
  132. $subject = sprintf($this->language->get('text_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'), $return_id);
  133. $message = $this->language->get('text_return_id') . ' ' . $return_id . "\n";
  134. $message .= $this->language->get('text_date_added') . ' ' . date($this->language->get('date_format_short'), strtotime($return_query->row['date_added'])) . "\n\n";
  135. $message .= $this->language->get('text_return_status') . "\n";
  136. $message .= $return_query->row['status'] . "\n\n";
  137. if ($data['comment']) {
  138. $message .= $this->language->get('text_comment') . "\n\n";
  139. $message .= strip_tags(html_entity_decode($data['comment'], ENT_QUOTES, 'UTF-8')) . "\n\n";
  140. }
  141. $message .= $this->language->get('text_footer');
  142. $mail = new Mail();
  143. $mail->protocol = $this->config->get('config_mail_protocol');
  144. $mail->parameter = $this->config->get('config_mail_parameter');
  145. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  146. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  147. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  148. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  149. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  150. $mail->setTo($return_query->row['email']);
  151. $mail->setFrom($this->config->get('config_email'));
  152. $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
  153. $mail->setSubject($subject);
  154. $mail->setText($message);
  155. $mail->send();
  156. }
  157. }
  158. }
  159. public function getReturnHistories($return_id, $start = 0, $limit = 10) {
  160. if ($start < 0) {
  161. $start = 0;
  162. }
  163. if ($limit < 1) {
  164. $limit = 10;
  165. }
  166. $query = $this->db->query("SELECT rh.date_added, rs.name AS status, rh.comment, rh.notify FROM " . DB_PREFIX . "return_history rh LEFT JOIN " . DB_PREFIX . "return_status rs ON rh.return_status_id = rs.return_status_id WHERE rh.return_id = '" . (int)$return_id . "' AND rs.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY rh.date_added ASC LIMIT " . (int)$start . "," . (int)$limit);
  167. return $query->rows;
  168. }
  169. public function getTotalReturnHistories($return_id) {
  170. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "return_history WHERE return_id = '" . (int)$return_id . "'");
  171. return $query->row['total'];
  172. }
  173. public function getTotalReturnHistoriesByReturnStatusId($return_status_id) {
  174. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "return_history WHERE return_status_id = '" . (int)$return_status_id . "'");
  175. return $query->row['total'];
  176. }
  177. }