PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/model/sale/affiliate.php

https://bitbucket.org/sandeepbhaskar/inspiredliving
PHP | 256 lines | 193 code | 63 blank | 0 comment | 33 complexity | 0d57d26760c811b3e0cb0d7f841ca845 MD5 | raw file
  1. <?php
  2. class ModelSaleAffiliate extends Model {
  3. public function addAffiliate($data) {
  4. $this->db->query("INSERT INTO " . DB_PREFIX . "affiliate SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', company = '" . $this->db->escape($data['company']) . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', city = '" . $this->db->escape($data['city']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', country_id = '" . (int)$data['country_id'] . "', zone_id = '" . (int)$data['zone_id'] . "', code = '" . $this->db->escape($data['code']) . "', commission = '" . (float)$data['commission'] . "', tax = '" . $this->db->escape($data['tax']) . "', payment = '" . $this->db->escape($data['payment']) . "', cheque = '" . $this->db->escape($data['cheque']) . "', paypal = '" . $this->db->escape($data['paypal']) . "', bank_name = '" . $this->db->escape($data['bank_name']) . "', bank_branch_number = '" . $this->db->escape($data['bank_branch_number']) . "', bank_swift_code = '" . $this->db->escape($data['bank_swift_code']) . "', bank_account_name = '" . $this->db->escape($data['bank_account_name']) . "', bank_account_number = '" . $this->db->escape($data['bank_account_number']) . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
  5. }
  6. public function editAffiliate($affiliate_id, $data) {
  7. $this->db->query("UPDATE " . DB_PREFIX . "affiliate SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', company = '" . $this->db->escape($data['company']) . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', city = '" . $this->db->escape($data['city']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', country_id = '" . (int)$data['country_id'] . "', zone_id = '" . (int)$data['zone_id'] . "', code = '" . $this->db->escape($data['code']) . "', commission = '" . (float)$data['commission'] . "', tax = '" . $this->db->escape($data['tax']) . "', payment = '" . $this->db->escape($data['payment']) . "', cheque = '" . $this->db->escape($data['cheque']) . "', paypal = '" . $this->db->escape($data['paypal']) . "', bank_name = '" . $this->db->escape($data['bank_name']) . "', bank_branch_number = '" . $this->db->escape($data['bank_branch_number']) . "', bank_swift_code = '" . $this->db->escape($data['bank_swift_code']) . "', bank_account_name = '" . $this->db->escape($data['bank_account_name']) . "', bank_account_number = '" . $this->db->escape($data['bank_account_number']) . "', status = '" . (int)$data['status'] . "' WHERE affiliate_id = '" . (int)$affiliate_id . "'");
  8. if ($data['password']) {
  9. $this->db->query("UPDATE " . DB_PREFIX . "affiliate SET salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "' WHERE affiliate_id = '" . (int)$affiliate_id . "'");
  10. }
  11. }
  12. public function deleteAffiliate($affiliate_id) {
  13. $this->db->query("DELETE FROM " . DB_PREFIX . "affiliate WHERE affiliate_id = '" . (int)$affiliate_id . "'");
  14. $this->db->query("DELETE FROM " . DB_PREFIX . "affiliate_transaction WHERE affiliate_id = '" . (int)$affiliate_id . "'");
  15. }
  16. public function getAffiliate($affiliate_id) {
  17. $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "affiliate WHERE affiliate_id = '" . (int)$affiliate_id . "'");
  18. return $query->row;
  19. }
  20. public function getAffiliateByEmail($email) {
  21. $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "affiliate WHERE LCASE(email) = '" . $this->db->escape(strtolower($email)) . "'");
  22. return $query->row;
  23. }
  24. public function getAffiliates($data = array()) {
  25. $sql = "SELECT *, CONCAT(a.firstname, ' ', a.lastname) AS name, (SELECT SUM(at.amount) FROM " . DB_PREFIX . "affiliate_transaction at WHERE at.affiliate_id = a.affiliate_id GROUP BY at.affiliate_id) AS balance FROM " . DB_PREFIX . "affiliate a";
  26. $implode = array();
  27. if (!empty($data['filter_name'])) {
  28. $implode[] = "LCASE(CONCAT(a.firstname, ' ', a.lastname)) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
  29. }
  30. if (!empty($data['filter_email'])) {
  31. $implode[] = "a.email = '" . $this->db->escape($data['filter_email']) . "'";
  32. }
  33. if (!empty($data['filter_code'])) {
  34. $implode[] = "a.code = '" . $this->db->escape($data['filter_code']) . "'";
  35. }
  36. if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
  37. $implode[] = "a.status = '" . (int)$data['filter_status'] . "'";
  38. }
  39. if (isset($data['filter_approved']) && !is_null($data['filter_approved'])) {
  40. $implode[] = "a.approved = '" . (int)$data['filter_approved'] . "'";
  41. }
  42. if (!empty($data['filter_date_added'])) {
  43. $implode[] = "DATE(a.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  44. }
  45. if ($implode) {
  46. $sql .= " WHERE " . implode(" AND ", $implode);
  47. }
  48. $sort_data = array(
  49. 'name',
  50. 'a.email',
  51. 'a.code',
  52. 'a.status',
  53. 'a.approved',
  54. 'a.date_added'
  55. );
  56. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  57. $sql .= " ORDER BY " . $data['sort'];
  58. } else {
  59. $sql .= " ORDER BY name";
  60. }
  61. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  62. $sql .= " DESC";
  63. } else {
  64. $sql .= " ASC";
  65. }
  66. if (isset($data['start']) || isset($data['limit'])) {
  67. if ($data['start'] < 0) {
  68. $data['start'] = 0;
  69. }
  70. if ($data['limit'] < 1) {
  71. $data['limit'] = 20;
  72. }
  73. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  74. }
  75. $query = $this->db->query($sql);
  76. return $query->rows;
  77. }
  78. public function approve($affiliate_id) {
  79. $affiliate_info = $this->getAffiliate($affiliate_id);
  80. if ($affiliate_info) {
  81. $this->db->query("UPDATE " . DB_PREFIX . "affiliate SET approved = '1' WHERE affiliate_id = '" . (int)$affiliate_id . "'");
  82. $this->load->language('mail/affiliate');
  83. $message = sprintf($this->language->get('text_approve_welcome'), $this->config->get('config_name')) . "\n\n";
  84. $message .= $this->language->get('text_approve_login') . "\n";
  85. $message .= HTTP_CATALOG . 'index.php?route=affiliate/login' . "\n\n";
  86. $message .= $this->language->get('text_approve_services') . "\n\n";
  87. $message .= $this->language->get('text_approve_thanks') . "\n";
  88. $message .= $this->config->get('config_name');
  89. $mail = new Mail();
  90. $mail->protocol = $this->config->get('config_mail_protocol');
  91. $mail->hostname = $this->config->get('config_smtp_host');
  92. $mail->username = $this->config->get('config_smtp_username');
  93. $mail->password = $this->config->get('config_smtp_password');
  94. $mail->port = $this->config->get('config_smtp_port');
  95. $mail->timeout = $this->config->get('config_smtp_timeout');
  96. $mail->setTo($affiliate_info['email']);
  97. $mail->setFrom($this->config->get('config_email'));
  98. $mail->setSender($this->config->get('config_name'));
  99. $mail->setSubject(html_entity_decode(sprintf($this->language->get('text_approve_subject'), $this->config->get('config_name')), ENT_QUOTES, 'UTF-8'));
  100. $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
  101. $mail->send();
  102. }
  103. }
  104. public function getAffiliatesByNewsletter() {
  105. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "affiliate WHERE newsletter = '1' ORDER BY firstname, lastname, email");
  106. return $query->rows;
  107. }
  108. public function getTotalAffiliates($data = array()) {
  109. $sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "affiliate";
  110. $implode = array();
  111. if (!empty($data['filter_name'])) {
  112. $implode[] = "CONCAT(firstname, ' ', lastname) LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
  113. }
  114. if (!empty($data['filter_email'])) {
  115. $implode[] = "email = '" . $this->db->escape($data['filter_email']) . "'";
  116. }
  117. if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
  118. $implode[] = "status = '" . (int)$data['filter_status'] . "'";
  119. }
  120. if (isset($data['filter_approved']) && !is_null($data['filter_approved'])) {
  121. $implode[] = "approved = '" . (int)$data['filter_approved'] . "'";
  122. }
  123. if (!empty($data['filter_date_added'])) {
  124. $implode[] = "DATE(date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  125. }
  126. if ($implode) {
  127. $sql .= " WHERE " . implode(" AND ", $implode);
  128. }
  129. $query = $this->db->query($sql);
  130. return $query->row['total'];
  131. }
  132. public function getTotalAffiliatesAwaitingApproval() {
  133. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "affiliate WHERE status = '0' OR approved = '0'");
  134. return $query->row['total'];
  135. }
  136. public function getTotalAffiliatesByCountryId($country_id) {
  137. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "affiliate WHERE country_id = '" . (int)$country_id . "'");
  138. return $query->row['total'];
  139. }
  140. public function getTotalAffiliatesByZoneId($zone_id) {
  141. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "affiliate WHERE zone_id = '" . (int)$zone_id . "'");
  142. return $query->row['total'];
  143. }
  144. public function addTransaction($affiliate_id, $description = '', $amount = '', $order_id = 0) {
  145. $affiliate_info = $this->getAffiliate($affiliate_id);
  146. if ($affiliate_info) {
  147. $this->db->query("INSERT INTO " . DB_PREFIX . "affiliate_transaction SET affiliate_id = '" . (int)$affiliate_id . "', order_id = '" . (float)$order_id . "', description = '" . $this->db->escape($description) . "', amount = '" . (float)$amount . "', date_added = NOW()");
  148. $this->language->load('mail/affiliate');
  149. $message = sprintf($this->language->get('text_transaction_received'), $this->currency->format($amount, $this->config->get('config_currency'))) . "\n\n";
  150. $message .= sprintf($this->language->get('text_transaction_total'), $this->currency->format($this->getTransactionTotal($affiliate_id), $this->config->get('config_currency')));
  151. $mail = new Mail();
  152. $mail->protocol = $this->config->get('config_mail_protocol');
  153. $mail->parameter = $this->config->get('config_mail_parameter');
  154. $mail->hostname = $this->config->get('config_smtp_host');
  155. $mail->username = $this->config->get('config_smtp_username');
  156. $mail->password = $this->config->get('config_smtp_password');
  157. $mail->port = $this->config->get('config_smtp_port');
  158. $mail->timeout = $this->config->get('config_smtp_timeout');
  159. $mail->setTo($affiliate_info['email']);
  160. $mail->setFrom($this->config->get('config_email'));
  161. $mail->setSender($this->config->get('config_name'));
  162. $mail->setSubject(html_entity_decode(sprintf($this->language->get('text_transaction_subject'), $this->config->get('config_name')), ENT_QUOTES, 'UTF-8'));
  163. $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
  164. $mail->send();
  165. }
  166. }
  167. public function deleteTransaction($order_id) {
  168. $this->db->query("DELETE FROM " . DB_PREFIX . "affiliate_transaction WHERE order_id = '" . (int)$order_id . "'");
  169. }
  170. public function getTransactions($affiliate_id, $start = 0, $limit = 10) {
  171. if ($start < 0) {
  172. $start = 0;
  173. }
  174. if ($limit < 1) {
  175. $limit = 10;
  176. }
  177. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "affiliate_transaction WHERE affiliate_id = '" . (int)$affiliate_id . "' ORDER BY date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
  178. return $query->rows;
  179. }
  180. public function getTotalTransactions($affiliate_id) {
  181. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "affiliate_transaction WHERE affiliate_id = '" . (int)$affiliate_id . "'");
  182. return $query->row['total'];
  183. }
  184. public function getTransactionTotal($affiliate_id) {
  185. $query = $this->db->query("SELECT SUM(amount) AS total FROM " . DB_PREFIX . "affiliate_transaction WHERE affiliate_id = '" . (int)$affiliate_id . "'");
  186. return $query->row['total'];
  187. }
  188. public function getTotalTransactionsByOrderId($order_id) {
  189. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "affiliate_transaction WHERE order_id = '" . (int)$order_id . "'");
  190. return $query->row['total'];
  191. }
  192. }
  193. ?>