PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/model/sale/voucher.php

https://gitlab.com/reclamare/mao
PHP | 213 lines | 160 code | 50 blank | 3 comment | 24 complexity | 7f16cba94e19e85ca48f78f8eac01e6b MD5 | raw file
  1. <?php
  2. class ModelSaleVoucher extends Model {
  3. public function addVoucher($data) {
  4. $this->db->query("INSERT INTO " . DB_PREFIX . "voucher SET code = '" . $this->db->escape($data['code']) . "', from_name = '" . $this->db->escape($data['from_name']) . "', from_email = '" . $this->db->escape($data['from_email']) . "', to_name = '" . $this->db->escape($data['to_name']) . "', to_email = '" . $this->db->escape($data['to_email']) . "', voucher_theme_id = '" . (int)$data['voucher_theme_id'] . "', message = '" . $this->db->escape($data['message']) . "', amount = '" . (float)$data['amount'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
  5. }
  6. public function editVoucher($voucher_id, $data) {
  7. $this->db->query("UPDATE " . DB_PREFIX . "voucher SET code = '" . $this->db->escape($data['code']) . "', from_name = '" . $this->db->escape($data['from_name']) . "', from_email = '" . $this->db->escape($data['from_email']) . "', to_name = '" . $this->db->escape($data['to_name']) . "', to_email = '" . $this->db->escape($data['to_email']) . "', voucher_theme_id = '" . (int)$data['voucher_theme_id'] . "', message = '" . $this->db->escape($data['message']) . "', amount = '" . (float)$data['amount'] . "', status = '" . (int)$data['status'] . "' WHERE voucher_id = '" . (int)$voucher_id . "'");
  8. }
  9. public function deleteVoucher($voucher_id) {
  10. $this->db->query("DELETE FROM " . DB_PREFIX . "voucher WHERE voucher_id = '" . (int)$voucher_id . "'");
  11. $this->db->query("DELETE FROM " . DB_PREFIX . "voucher_history WHERE voucher_id = '" . (int)$voucher_id . "'");
  12. }
  13. public function getVoucher($voucher_id) {
  14. $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "voucher WHERE voucher_id = '" . (int)$voucher_id . "'");
  15. return $query->row;
  16. }
  17. public function getVoucherByCode($code) {
  18. $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "voucher WHERE code = '" . $this->db->escape($code) . "'");
  19. return $query->row;
  20. }
  21. public function getVouchers($data = array()) {
  22. $sql = "SELECT v.voucher_id, v.code, v.from_name, v.from_email, v.to_name, v.to_email, (SELECT vtd.name FROM " . DB_PREFIX . "voucher_theme_description vtd WHERE vtd.voucher_theme_id = v.voucher_theme_id AND vtd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS theme, v.amount, v.status, v.date_added FROM " . DB_PREFIX . "voucher v";
  23. $sort_data = array(
  24. 'v.code',
  25. 'v.from_name',
  26. 'v.from_email',
  27. 'v.to_name',
  28. 'v.to_email',
  29. 'v.theme',
  30. 'v.amount',
  31. 'v.status',
  32. 'v.date_added'
  33. );
  34. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  35. $sql .= " ORDER BY " . $data['sort'];
  36. } else {
  37. $sql .= " ORDER BY v.date_added";
  38. }
  39. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  40. $sql .= " DESC";
  41. } else {
  42. $sql .= " ASC";
  43. }
  44. if (isset($data['start']) || isset($data['limit'])) {
  45. if ($data['start'] < 0) {
  46. $data['start'] = 0;
  47. }
  48. if ($data['limit'] < 1) {
  49. $data['limit'] = 20;
  50. }
  51. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  52. }
  53. $query = $this->db->query($sql);
  54. return $query->rows;
  55. }
  56. public function sendVoucher($voucher_id) {
  57. $voucher_info = $this->getVoucher($voucher_id);
  58. if ($voucher_info) {
  59. if ($voucher_info['order_id']) {
  60. $order_id = $voucher_info['order_id'];
  61. } else {
  62. $order_id = 0;
  63. }
  64. $this->load->model('sale/order');
  65. $order_info = $this->model_sale_order->getOrder($order_id);
  66. // If voucher belongs to an order
  67. if ($order_info) {
  68. $this->load->model('localisation/language');
  69. $language = new Language($order_info['language_directory']);
  70. $language->load($order_info['language_directory']);
  71. $language->load('mail/voucher');
  72. // HTML Mail
  73. $data = array();
  74. $data['title'] = sprintf($language->get('text_subject'), $voucher_info['from_name']);
  75. $data['text_greeting'] = sprintf($language->get('text_greeting'), $this->currency->format($voucher_info['amount'], $order_info['currency_code'], $order_info['currency_value']));
  76. $data['text_from'] = sprintf($language->get('text_from'), $voucher_info['from_name']);
  77. $data['text_message'] = $language->get('text_message');
  78. $data['text_redeem'] = sprintf($language->get('text_redeem'), $voucher_info['code']);
  79. $data['text_footer'] = $language->get('text_footer');
  80. $this->load->model('sale/voucher_theme');
  81. $voucher_theme_info = $this->model_sale_voucher_theme->getVoucherTheme($voucher_info['voucher_theme_id']);
  82. if ($voucher_theme_info && is_file(DIR_IMAGE . $voucher_theme_info['image'])) {
  83. $data['image'] = HTTP_CATALOG . 'image/' . $voucher_theme_info['image'];
  84. } else {
  85. $data['image'] = '';
  86. }
  87. $data['store_name'] = $order_info['store_name'];
  88. $data['store_url'] = $order_info['store_url'];
  89. $data['message'] = nl2br($voucher_info['message']);
  90. $mail = new Mail();
  91. $mail->protocol = $this->config->get('config_mail_protocol');
  92. $mail->parameter = $this->config->get('config_mail_parameter');
  93. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  94. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  95. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  96. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  97. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  98. $mail->setTo($voucher_info['to_email']);
  99. $mail->setFrom($this->config->get('config_email'));
  100. $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
  101. $mail->setSubject(sprintf($language->get('text_subject'), html_entity_decode($voucher_info['from_name'], ENT_QUOTES, 'UTF-8')));
  102. $mail->setHtml($this->load->view('mail/voucher.tpl', $data));
  103. $mail->send();
  104. // If voucher does not belong to an order
  105. } else {
  106. $this->load->language('mail/voucher');
  107. $data = array();
  108. $data['title'] = sprintf($this->language->get('text_subject'), $voucher_info['from_name']);
  109. $data['text_greeting'] = sprintf($this->language->get('text_greeting'), $this->currency->format($voucher_info['amount'], $order_info['currency_code'], $order_info['currency_value']));
  110. $data['text_from'] = sprintf($this->language->get('text_from'), $voucher_info['from_name']);
  111. $data['text_message'] = $this->language->get('text_message');
  112. $data['text_redeem'] = sprintf($this->language->get('text_redeem'), $voucher_info['code']);
  113. $data['text_footer'] = $this->language->get('text_footer');
  114. $this->load->model('sale/voucher_theme');
  115. $voucher_theme_info = $this->model_sale_voucher_theme->getVoucherTheme($voucher_info['voucher_theme_id']);
  116. if ($voucher_theme_info && is_file(DIR_IMAGE . $voucher_theme_info['image'])) {
  117. $data['image'] = HTTP_CATALOG . 'image/' . $voucher_theme_info['image'];
  118. } else {
  119. $data['image'] = '';
  120. }
  121. $data['store_name'] = $this->config->get('config_name');
  122. $data['store_url'] = HTTP_CATALOG;
  123. $data['message'] = nl2br($voucher_info['message']);
  124. $mail = new Mail();
  125. $mail->protocol = $this->config->get('config_mail_protocol');
  126. $mail->parameter = $this->config->get('config_mail_parameter');
  127. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  128. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  129. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  130. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  131. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  132. $mail->setTo($voucher_info['to_email']);
  133. $mail->setFrom($this->config->get('config_email'));
  134. $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
  135. $mail->setSubject(html_entity_decode(sprintf($this->language->get('text_subject'), $voucher_info['from_name']), ENT_QUOTES, 'UTF-8'));
  136. $mail->setHtml($this->load->view('mail/voucher.tpl', $data));
  137. $mail->send();
  138. }
  139. }
  140. }
  141. public function getTotalVouchers() {
  142. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "voucher");
  143. return $query->row['total'];
  144. }
  145. public function getTotalVouchersByVoucherThemeId($voucher_theme_id) {
  146. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "voucher WHERE voucher_theme_id = '" . (int)$voucher_theme_id . "'");
  147. return $query->row['total'];
  148. }
  149. public function getVoucherHistories($voucher_id, $start = 0, $limit = 10) {
  150. if ($start < 0) {
  151. $start = 0;
  152. }
  153. if ($limit < 1) {
  154. $limit = 10;
  155. }
  156. $query = $this->db->query("SELECT vh.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, vh.amount, vh.date_added FROM " . DB_PREFIX . "voucher_history vh LEFT JOIN `" . DB_PREFIX . "order` o ON (vh.order_id = o.order_id) WHERE vh.voucher_id = '" . (int)$voucher_id . "' ORDER BY vh.date_added ASC LIMIT " . (int)$start . "," . (int)$limit);
  157. return $query->rows;
  158. }
  159. public function getTotalVoucherHistories($voucher_id) {
  160. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "voucher_history WHERE voucher_id = '" . (int)$voucher_id . "'");
  161. return $query->row['total'];
  162. }
  163. }