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

/admin/model/sale/voucher.php

https://bitbucket.org/monobasic/shop.volero.ch
PHP | 214 lines | 161 code | 50 blank | 3 comment | 26 complexity | 006c18b19bfc324e731f43b09cc9a09e 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']) . "', message = '" . $this->db->escape($data['message']) . "', amount = '" . (float)$data['amount'] . "', voucher_theme_id = '" . (int)$data['voucher_theme_id'] . "', 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']) . "', message = '" . $this->db->escape($data['message']) . "', amount = '" . (float)$data['amount'] . "', voucher_theme_id = '" . (int)$data['voucher_theme_id'] . "', 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 getVouchers($data = array()) {
  18. $sql = "SELECT v.voucher_id, v.code, v.from_name, v.from_email, v.to_name, v.to_email, v.amount, (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.status, v.date_added FROM " . DB_PREFIX . "voucher v";
  19. $sort_data = array(
  20. 'v.code',
  21. 'v.from_name',
  22. 'v.from_email',
  23. 'v.to_name',
  24. 'v.to_email',
  25. 'v.amount',
  26. 'v.theme',
  27. 'v.status',
  28. 'v.date_added'
  29. );
  30. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  31. $sql .= " ORDER BY " . $data['sort'];
  32. } else {
  33. $sql .= " ORDER BY v.date_added";
  34. }
  35. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  36. $sql .= " DESC";
  37. } else {
  38. $sql .= " ASC";
  39. }
  40. if (isset($data['start']) || isset($data['limit'])) {
  41. if ($data['start'] < 0) {
  42. $data['start'] = 0;
  43. }
  44. if ($data['limit'] < 1) {
  45. $data['limit'] = 20;
  46. }
  47. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  48. }
  49. $query = $this->db->query($sql);
  50. return $query->rows;
  51. }
  52. public function getVouchersByOrderId($order_id) {
  53. $query = $this->db->query("SELECT v.voucher_id, v.code, v.from_name, v.from_email, v.to_name, v.to_email, v.amount, vtd.name AS theme, v.status, v.date_added FROM " . DB_PREFIX . "voucher v LEFT JOIN " . DB_PREFIX . "voucher_theme_description vtd ON (v.voucher_theme_id = vtd.voucher_theme_id) WHERE v.order_id = '" . (int)$order_id . "' AND vtd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
  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_filename']);
  71. $language->load('mail/voucher');
  72. // HTML Mail
  73. $template = new Template();
  74. $template->data['title'] = sprintf($language->get('text_subject'), $voucher_info['from_name']);
  75. $template->data['text_greeting'] = sprintf($language->get('text_greeting'), $this->currency->format($voucher_info['amount'], $order_info['currency_code'], $order_info['currency_value']));
  76. $template->data['text_from'] = sprintf($language->get('text_from'), $voucher_info['from_name']);
  77. $template->data['text_message'] = $language->get('text_message');
  78. $template->data['text_redeem'] = sprintf($language->get('text_redeem'), $voucher_info['code']);
  79. $template->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_info && file_exists(DIR_IMAGE . $voucher_theme_info['image'])) {
  83. $template->data['image'] = 'cid:' . md5(basename($voucher_theme_info['image']));
  84. } else {
  85. $template->data['image'] = '';
  86. }
  87. $template->data['store_name'] = $order_info['store_name'];
  88. $template->data['store_url'] = $order_info['store_url'];
  89. $template->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->hostname = $this->config->get('config_smtp_host');
  94. $mail->username = $this->config->get('config_smtp_username');
  95. $mail->password = $this->config->get('config_smtp_password');
  96. $mail->port = $this->config->get('config_smtp_port');
  97. $mail->timeout = $this->config->get('config_smtp_timeout');
  98. $mail->setTo($voucher_info['to_email']);
  99. $mail->setFrom($this->config->get('config_email'));
  100. $mail->setSender($order_info['store_name']);
  101. $mail->setSubject(sprintf($language->get('text_subject'), $voucher_info['from_name']));
  102. $mail->setHtml($template->fetch('mail/voucher.tpl'));
  103. if ($voucher_info && file_exists(DIR_IMAGE . $voucher_theme_info['image'])) {
  104. $mail->addAttachment(DIR_IMAGE . $voucher_theme_info['image'], md5(basename($voucher_theme_info['image'])));
  105. }
  106. $mail->send();
  107. // If voucher does not belong to an order
  108. } else {
  109. $this->language->load('mail/voucher');
  110. $template = new Template();
  111. $template->data['title'] = sprintf($this->language->get('text_subject'), $voucher_info['from_name']);
  112. $template->data['text_greeting'] = sprintf($this->language->get('text_greeting'), $this->currency->format($voucher_info['amount'], $order_info['currency_code'], $order_info['currency_value']));
  113. $template->data['text_from'] = sprintf($this->language->get('text_from'), $voucher_info['from_name']);
  114. $template->data['text_message'] = $this->language->get('text_message');
  115. $template->data['text_redeem'] = sprintf($this->language->get('text_redeem'), $voucher_info['code']);
  116. $template->data['text_footer'] = $this->language->get('text_footer');
  117. $this->load->model('sale/voucher_theme');
  118. $voucher_theme_info = $this->model_sale_voucher_theme->getVoucherTheme($voucher_info['voucher_theme_id']);
  119. if ($voucher_info && file_exists(DIR_IMAGE . $voucher_theme_info['image'])) {
  120. $template->data['image'] = 'cid:' . md5(basename($voucher_theme_info['image']));
  121. } else {
  122. $template->data['image'] = '';
  123. }
  124. $template->data['store_name'] = $this->config->get('config_name');
  125. $template->data['store_url'] = HTTP_CATALOG;
  126. $template->data['message'] = nl2br($voucher_info['message']);
  127. $mail = new Mail();
  128. $mail->protocol = $this->config->get('config_mail_protocol');
  129. $mail->parameter = $this->config->get('config_mail_parameter');
  130. $mail->hostname = $this->config->get('config_smtp_host');
  131. $mail->username = $this->config->get('config_smtp_username');
  132. $mail->password = $this->config->get('config_smtp_password');
  133. $mail->port = $this->config->get('config_smtp_port');
  134. $mail->timeout = $this->config->get('config_smtp_timeout');
  135. $mail->setTo($voucher_info['to_email']);
  136. $mail->setFrom($this->config->get('config_email'));
  137. $mail->setSender($this->config->get('config_name'));
  138. $mail->setSubject(sprintf($this->language->get('text_subject'), $voucher_info['from_name']));
  139. $mail->setHtml($template->fetch('mail/voucher.tpl'));
  140. if ($voucher_info && file_exists(DIR_IMAGE . $voucher_theme_info['image'])) {
  141. $mail->addAttachment(DIR_IMAGE . $voucher_theme_info['image'], md5(basename($voucher_theme_info['image'])));
  142. }
  143. $mail->send();
  144. }
  145. }
  146. }
  147. public function getTotalVouchers() {
  148. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "voucher");
  149. return $query->row['total'];
  150. }
  151. public function getTotalVouchersByVoucherThemeId($voucher_theme_id) {
  152. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "voucher WHERE voucher_theme_id = '" . (int)$voucher_theme_id . "'");
  153. return $query->row['total'];
  154. }
  155. public function getVoucherHistories($voucher_id, $start = 0, $limit = 10) {
  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. }
  164. ?>