PageRenderTime 22ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/ecart/admin/model/sale/return.php

https://github.com/kjsenthil02/genplus
PHP | 251 lines | 187 code | 64 blank | 0 comment | 46 complexity | c31f9bb52e8c8aae57cf87bc08d61dae 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'] . "', date_ordered = '" . $this->db->escape($data['date_ordered']) . "', 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']) . "', return_status_id = '" . (int)$data['return_status_id'] . "', comment = '" . $this->db->escape($data['comment']) . "', date_added = NOW(), date_modified = NOW()");
  5. $return_id = $this->db->getLastId();
  6. if (isset($data['return_product'])) {
  7. foreach ($data['return_product'] as $return_product) {
  8. $this->db->query("INSERT INTO " . DB_PREFIX . "return_product SET return_id = '" . (int)$return_id . "', product_id = '" . (int)$return_product['product_id'] . "', name = '" . $this->db->escape($return_product['name']) . "', model = '" . $this->db->escape($return_product['model']) . "', quantity = '" . (int)$return_product['quantity'] . "', return_reason_id = '" . (int)$return_product['return_reason_id'] . "', opened = '" . (int)$return_product['opened'] . "', comment = '" . $this->db->escape($return_product['comment']) . "', return_action_id = '" . (int)$return_product['return_action_id'] . "'");
  9. }
  10. }
  11. }
  12. public function editReturn($return_id, $data) {
  13. $this->db->query("UPDATE `" . DB_PREFIX . "return`SET order_id = '" . (int)$data['order_id'] . "', date_ordered = '" . $this->db->escape($data['date_ordered']) . "', 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']) . "', return_status_id = '" . (int)$data['return_status_id'] . "', comment = '" . $this->db->escape($data['comment']) . "', date_modified = NOW() WHERE return_id = '" . (int)$return_id . "'");
  14. $this->db->query("DELETE FROM " . DB_PREFIX . "return_product WHERE return_id = '" . (int)$return_id . "'");
  15. if (isset($data['return_product'])) {
  16. foreach ($data['return_product'] as $return_product) {
  17. $this->db->query("INSERT INTO " . DB_PREFIX . "return_product SET return_id = '" . (int)$return_id . "', product_id = '" . (int)$return_product['product_id'] . "', name = '" . $this->db->escape($return_product['name']) . "', model = '" . $this->db->escape($return_product['model']) . "', quantity = '" . (int)$return_product['quantity'] . "', return_reason_id = '" . (int)$return_product['return_reason_id'] . "', opened = '" . (int)$return_product['opened'] . "', comment = '" . $this->db->escape($return_product['comment']) . "', return_action_id = '" . (int)$return_product['return_action_id'] . "'");
  18. }
  19. }
  20. }
  21. public function deleteReturn($return_id) {
  22. $this->db->query("DELETE FROM `" . DB_PREFIX . "return` WHERE return_id = '" . (int)$return_id . "'");
  23. $this->db->query("DELETE FROM " . DB_PREFIX . "return_product WHERE return_id = '" . (int)$return_id . "'");
  24. $this->db->query("DELETE FROM " . DB_PREFIX . "return_history WHERE return_id = '" . (int)$return_id . "'");
  25. }
  26. public function getReturn($return_id) {
  27. $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 . "'");
  28. return $query->row;
  29. }
  30. public function getReturns($data = array()) {
  31. $sql = "SELECT *, CONCAT(r.firstname, ' ', r.lastname) AS customer, (SELECT SUM(rp.quantity) FROM " . DB_PREFIX . "return_product rp WHERE rp.return_id = r.return_id GROUP BY rp.return_id) AS quantity, (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";
  32. $implode = array();
  33. if (isset($data['filter_return_id']) && !is_null($data['filter_return_id'])) {
  34. $implode[] = "r.return_id = '" . (int)$data['filter_return_id'] . "'";
  35. }
  36. if (isset($data['filter_order_id']) && !is_null($data['filter_order_id'])) {
  37. $implode[] = "r.order_id = '" . $this->db->escape($data['filter_order_id']) . "'";
  38. }
  39. if (isset($data['filter_customer']) && !is_null($data['filter_customer'])) {
  40. $implode[] = "LCASE(CONCAT(r.firstname, ' ', r.lastname)) LIKE '" . $this->db->escape(strtolower($data['filter_customer'])) . "%'";
  41. }
  42. if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
  43. $implode[] = "(SELECT SUM(rp.quantity) FROM " . DB_PREFIX . "return_product rp WHERE rp.return_id = r.return_id GROUP BY rp.return_id) = '" . (int)$data['filter_quantity'] . "'";
  44. }
  45. if (isset($data['filter_return_status_id']) && !is_null($data['filter_return_status_id'])) {
  46. $implode[] = "r.return_status_id = '" . (int)$data['filter_return_status_id'] . "'";
  47. }
  48. if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
  49. $implode[] = "DATE(r.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  50. }
  51. if (isset($data['filter_date_modified']) && !is_null($data['filter_date_modified'])) {
  52. $implode[] = "DATE(r.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
  53. }
  54. if ($implode) {
  55. $sql .= " WHERE " . implode(" AND ", $implode);
  56. }
  57. $sort_data = array(
  58. 'r.return_id',
  59. 'r.order_id',
  60. 'customer',
  61. 'quantity',
  62. 'status',
  63. 'r.date_added'
  64. );
  65. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  66. $sql .= " ORDER BY " . $data['sort'];
  67. } else {
  68. $sql .= " ORDER BY name";
  69. }
  70. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  71. $sql .= " DESC";
  72. } else {
  73. $sql .= " ASC";
  74. }
  75. if (isset($data['start']) || isset($data['limit'])) {
  76. if ($data['start'] < 0) {
  77. $data['start'] = 0;
  78. }
  79. if ($data['limit'] < 1) {
  80. $data['limit'] = 20;
  81. }
  82. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  83. }
  84. $query = $this->db->query($sql);
  85. return $query->rows;
  86. }
  87. public function getTotalReturns($data = array()) {
  88. $sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return`r";
  89. $implode = array();
  90. if (isset($data['filter_return_id']) && !is_null($data['filter_return_id'])) {
  91. $implode[] = "r.return_id = '" . (int)$data['filter_return_id'] . "'";
  92. }
  93. if (isset($data['filter_customer']) && !is_null($data['filter_customer'])) {
  94. $implode[] = "LCASE(CONCAT(r.firstname, ' ', r.lastname)) LIKE '" . $this->db->escape(strtolower($data['filter_customer'])) . "%'";
  95. }
  96. if (isset($data['filter_order_id']) && !is_null($data['filter_order_id'])) {
  97. $implode[] = "r.order_id = '" . $this->db->escape($data['filter_order_id']) . "'";
  98. }
  99. if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
  100. $implode[] = "(SELECT SUM(rp.quantity) FROM " . DB_PREFIX . "return_product rp WHERE rp.return_id = r.return_id GROUP BY rp.return_id) = '" . (int)$data['filter_quantity'] . "'";
  101. }
  102. if (isset($data['filter_return_status_id']) && !is_null($data['filter_return_status_id'])) {
  103. $implode[] = "r.return_status_id = '" . (int)$data['filter_return_status_id'] . "'";
  104. }
  105. if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
  106. $implode[] = "DATE(r.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  107. }
  108. if (isset($data['filter_date_modified']) && !is_null($data['filter_date_modified'])) {
  109. $implode[] = "DATE(r.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
  110. }
  111. if ($implode) {
  112. $sql .= " WHERE " . implode(" AND ", $implode);
  113. }
  114. $query = $this->db->query($sql);
  115. return $query->row['total'];
  116. }
  117. public function getTotalReturnsByReturnStatusId($return_status_id) {
  118. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return`WHERE return_status_id = '" . (int)$return_status_id . "'");
  119. return $query->row['total'];
  120. }
  121. public function editReturnProduct($return_id, $return_product_id, $return_action_id) {
  122. $this->db->query("UPDATE " . DB_PREFIX . "return_product SET return_action_id = '" . (int)$return_action_id . "' WHERE return_id = '" . (int)$return_id . "' AND return_product_id = '" . (int)$return_product_id . "'");
  123. }
  124. public function getReturnProducts($return_id) {
  125. $query = $this->db->query("SELECT *, (SELECT rr.name FROM " . DB_PREFIX . "return_reason rr WHERE rr.return_reason_id = rp.return_reason_id AND rr.language_id = '" . (int)$this->config->get('config_language_id') . "') AS reason, (SELECT ra.name FROM " . DB_PREFIX . "return_action ra WHERE ra.return_action_id = rp.return_action_id AND ra.language_id = '" . (int)$this->config->get('config_language_id') . "') AS action FROM " . DB_PREFIX . "return_product rp WHERE rp.return_id = '" . $return_id . "'");
  126. return $query->rows;
  127. }
  128. public function getTotalReturnProducts($return_id) {
  129. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return`WHERE return_id = '" . (int)$return_id . "'");
  130. return $query->rows;
  131. }
  132. public function getTotalReturnProductsByReturnReasonId($return_reason_id) {
  133. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "return_product WHERE return_reason_id = '" . (int)$return_reason_id . "'");
  134. return $query->row['total'];
  135. }
  136. public function getTotalReturnProductsByReturnActionId($return_action_id) {
  137. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "return_product WHERE return_action_id = '" . (int)$return_action_id . "'");
  138. return $query->row['total'];
  139. }
  140. public function addReturnHistory($return_id, $data) {
  141. $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 . "'");
  142. $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()");
  143. if ($data['notify']) {
  144. $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') . "'");
  145. if ($return_query->num_rows) {
  146. $this->language->load('mail/return');
  147. $subject = sprintf($this->language->get('text_subject'), $this->config->get('config_name'), $return_id);
  148. $message = $this->language->get('text_return_id') . ' ' . $return_id . "\n";
  149. $message .= $this->language->get('text_date_added') . ' ' . date($this->language->get('date_format_short'), strtotime($return_query->row['date_added'])) . "\n\n";
  150. $message .= $this->language->get('text_return_status') . "\n";
  151. $message .= $return_query->row['status'] . "\n\n";
  152. if ($data['comment']) {
  153. $message .= $this->language->get('text_comment') . "\n\n";
  154. $message .= strip_tags(html_entity_decode($data['comment'], ENT_QUOTES, 'UTF-8')) . "\n\n";
  155. }
  156. $message .= $this->language->get('text_footer');
  157. $mail = new Mail();
  158. $mail->protocol = $this->config->get('config_mail_protocol');
  159. $mail->parameter = $this->config->get('config_mail_parameter');
  160. $mail->hostname = $this->config->get('config_smtp_host');
  161. $mail->username = $this->config->get('config_smtp_username');
  162. $mail->password = $this->config->get('config_smtp_password');
  163. $mail->port = $this->config->get('config_smtp_port');
  164. $mail->timeout = $this->config->get('config_smtp_timeout');
  165. $mail->setTo($return_query->row['email']);
  166. $mail->setFrom($this->config->get('config_email'));
  167. $mail->setSender($this->config->get('config_name'));
  168. $mail->setSubject($subject);
  169. $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
  170. $mail->send();
  171. }
  172. }
  173. }
  174. public function getReturnHistories($return_id, $start = 0, $limit = 10) {
  175. $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);
  176. return $query->rows;
  177. }
  178. public function getTotalReturnHistories($return_id) {
  179. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "return_history WHERE return_id = '" . (int)$return_id . "'");
  180. return $query->row['total'];
  181. }
  182. public function getTotalReturnHistoriesByReturnStatusId($return_status_id) {
  183. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "return_history WHERE return_status_id = '" . (int)$return_status_id . "' GROUP BY order_id");
  184. return $query->row['total'];
  185. }
  186. }
  187. ?>