PageRenderTime 73ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/model/report/customer.php

https://gitlab.com/reclamare/mao
PHP | 392 lines | 281 code | 111 blank | 0 comment | 58 complexity | 560ffcab0ccd75717a3eda94fe30f4f0 MD5 | raw file
  1. <?php
  2. class ModelReportCustomer extends Model {
  3. public function getTotalCustomersByDay() {
  4. $customer_data = array();
  5. for ($i = 0; $i < 24; $i++) {
  6. $customer_data[$i] = array(
  7. 'hour' => $i,
  8. 'total' => 0
  9. );
  10. }
  11. $query = $this->db->query("SELECT COUNT(*) AS total, HOUR(date_added) AS hour FROM `" . DB_PREFIX . "customer` WHERE DATE(date_added) = DATE(NOW()) GROUP BY HOUR(date_added) ORDER BY date_added ASC");
  12. foreach ($query->rows as $result) {
  13. $customer_data[$result['hour']] = array(
  14. 'hour' => $result['hour'],
  15. 'total' => $result['total']
  16. );
  17. }
  18. return $customer_data;
  19. }
  20. public function getTotalCustomersByWeek() {
  21. $customer_data = array();
  22. $date_start = strtotime('-' . date('w') . ' days');
  23. for ($i = 0; $i < 7; $i++) {
  24. $date = date('Y-m-d', $date_start + ($i * 86400));
  25. $order_data[date('w', strtotime($date))] = array(
  26. 'day' => date('D', strtotime($date)),
  27. 'total' => 0
  28. );
  29. }
  30. $query = $this->db->query("SELECT COUNT(*) AS total, date_added FROM `" . DB_PREFIX . "customer` WHERE DATE(date_added) >= DATE('" . $this->db->escape(date('Y-m-d', $date_start)) . "') GROUP BY DAYNAME(date_added)");
  31. foreach ($query->rows as $result) {
  32. $customer_data[date('w', strtotime($result['date_added']))] = array(
  33. 'day' => date('D', strtotime($result['date_added'])),
  34. 'total' => $result['total']
  35. );
  36. }
  37. return $customer_data;
  38. }
  39. public function getTotalCustomersByMonth() {
  40. $customer_data = array();
  41. for ($i = 1; $i <= date('t'); $i++) {
  42. $date = date('Y') . '-' . date('m') . '-' . $i;
  43. $customer_data[date('j', strtotime($date))] = array(
  44. 'day' => date('d', strtotime($date)),
  45. 'total' => 0
  46. );
  47. }
  48. $query = $this->db->query("SELECT COUNT(*) AS total, date_added FROM `" . DB_PREFIX . "customer` WHERE DATE(date_added) >= '" . $this->db->escape(date('Y') . '-' . date('m') . '-1') . "' GROUP BY DATE(date_added)");
  49. foreach ($query->rows as $result) {
  50. $customer_data[date('j', strtotime($result['date_added']))] = array(
  51. 'day' => date('d', strtotime($result['date_added'])),
  52. 'total' => $result['total']
  53. );
  54. }
  55. return $customer_data;
  56. }
  57. public function getTotalCustomersByYear() {
  58. $customer_data = array();
  59. for ($i = 1; $i <= 12; $i++) {
  60. $customer_data[$i] = array(
  61. 'month' => date('M', mktime(0, 0, 0, $i)),
  62. 'total' => 0
  63. );
  64. }
  65. $query = $this->db->query("SELECT COUNT(*) AS total, date_added FROM `" . DB_PREFIX . "customer` WHERE YEAR(date_added) = YEAR(NOW()) GROUP BY MONTH(date_added)");
  66. foreach ($query->rows as $result) {
  67. $customer_data[date('n', strtotime($result['date_added']))] = array(
  68. 'month' => date('M', strtotime($result['date_added'])),
  69. 'total' => $result['total']
  70. );
  71. }
  72. return $customer_data;
  73. }
  74. public function getOrders($data = array()) {
  75. $sql = "SELECT c.customer_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, c.email, cgd.name AS customer_group, c.status, o.order_id, SUM(op.quantity) as products, SUM(DISTINCT o.total) AS total FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "order_product` op ON (o.order_id = op.order_id)LEFT JOIN `" . DB_PREFIX . "customer` c ON (o.customer_id = c.customer_id) LEFT JOIN `" . DB_PREFIX . "customer_group_description` cgd ON (c.customer_group_id = cgd.customer_group_id) WHERE o.customer_id > 0 AND cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  76. if (!empty($data['filter_order_status_id'])) {
  77. $sql .= " AND o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
  78. } else {
  79. $sql .= " AND o.order_status_id > '0'";
  80. }
  81. if (!empty($data['filter_date_start'])) {
  82. $sql .= " AND DATE(o.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  83. }
  84. if (!empty($data['filter_date_end'])) {
  85. $sql .= " AND DATE(o.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  86. }
  87. $sql .= " GROUP BY o.order_id";
  88. if (isset($data['start']) || isset($data['limit'])) {
  89. if ($data['start'] < 0) {
  90. $data['start'] = 0;
  91. }
  92. if ($data['limit'] < 1) {
  93. $data['limit'] = 20;
  94. }
  95. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  96. }
  97. $sql = "SELECT t.customer_id, t.customer, t.email, t.customer_group, t.status, COUNT(t.order_id) AS orders, SUM(t.products) AS products, SUM(t.total) AS total FROM (" . $sql . ") AS t GROUP BY t.customer_id ORDER BY total DESC";
  98. $query = $this->db->query($sql);
  99. return $query->rows;
  100. }
  101. public function getTotalOrders($data = array()) {
  102. $sql = "SELECT COUNT(DISTINCT o.customer_id) AS total FROM `" . DB_PREFIX . "order` o WHERE o.customer_id > '0'";
  103. if (!empty($data['filter_order_status_id'])) {
  104. $sql .= " AND o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
  105. } else {
  106. $sql .= " AND o.order_status_id > '0'";
  107. }
  108. if (!empty($data['filter_date_start'])) {
  109. $sql .= " AND DATE(o.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  110. }
  111. if (!empty($data['filter_date_end'])) {
  112. $sql .= " AND DATE(o.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  113. }
  114. $query = $this->db->query($sql);
  115. return $query->row['total'];
  116. }
  117. public function getRewardPoints($data = array()) {
  118. $sql = "SELECT cr.customer_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, c.email, cgd.name AS customer_group, c.status, SUM(cr.points) AS points, COUNT(o.order_id) AS orders, SUM(o.total) AS total FROM " . DB_PREFIX . "customer_reward cr LEFT JOIN `" . DB_PREFIX . "customer` c ON (cr.customer_id = c.customer_id) LEFT JOIN " . DB_PREFIX . "customer_group_description cgd ON (c.customer_group_id = cgd.customer_group_id) LEFT JOIN `" . DB_PREFIX . "order` o ON (cr.order_id = o.order_id) WHERE cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  119. if (!empty($data['filter_date_start'])) {
  120. $sql .= " AND DATE(cr.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  121. }
  122. if (!empty($data['filter_date_end'])) {
  123. $sql .= " AND DATE(cr.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  124. }
  125. $sql .= " GROUP BY cr.customer_id ORDER BY points DESC";
  126. if (isset($data['start']) || isset($data['limit'])) {
  127. if ($data['start'] < 0) {
  128. $data['start'] = 0;
  129. }
  130. if ($data['limit'] < 1) {
  131. $data['limit'] = 20;
  132. }
  133. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  134. }
  135. $query = $this->db->query($sql);
  136. return $query->rows;
  137. }
  138. public function getTotalRewardPoints($data = array()) {
  139. $sql = "SELECT COUNT(DISTINCT customer_id) AS total FROM `" . DB_PREFIX . "customer_reward`";
  140. $implode = array();
  141. if (!empty($data['filter_date_start'])) {
  142. $implode[] = "DATE(date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  143. }
  144. if (!empty($data['filter_date_end'])) {
  145. $implode[] = "DATE(date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  146. }
  147. if ($implode) {
  148. $sql .= " WHERE " . implode(" AND ", $implode);
  149. }
  150. $query = $this->db->query($sql);
  151. return $query->row['total'];
  152. }
  153. public function getCredit($data = array()) {
  154. $sql = "SELECT ct.customer_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, c.email, cgd.name AS customer_group, c.status, SUM(ct.amount) AS total FROM `" . DB_PREFIX . "customer_transaction` ct LEFT JOIN `" . DB_PREFIX . "customer` c ON (ct.customer_id = c.customer_id) LEFT JOIN `" . DB_PREFIX . "customer_group_description` cgd ON (c.customer_group_id = cgd.customer_group_id) WHERE cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  155. if (!empty($data['filter_date_start'])) {
  156. $sql .= " AND DATE(ct.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  157. }
  158. if (!empty($data['filter_date_end'])) {
  159. $sql .= " AND DATE(ct.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  160. }
  161. $sql .= " GROUP BY ct.customer_id ORDER BY total DESC";
  162. if (isset($data['start']) || isset($data['limit'])) {
  163. if ($data['start'] < 0) {
  164. $data['start'] = 0;
  165. }
  166. if ($data['limit'] < 1) {
  167. $data['limit'] = 20;
  168. }
  169. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  170. }
  171. $query = $this->db->query($sql);
  172. return $query->rows;
  173. }
  174. public function getTotalCredit($data = array()) {
  175. $sql = "SELECT COUNT(DISTINCT customer_id) AS total FROM `" . DB_PREFIX . "customer_transaction`";
  176. $implode = array();
  177. if (!empty($data['filter_date_start'])) {
  178. $implode[] = "DATE(date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  179. }
  180. if (!empty($data['filter_date_end'])) {
  181. $implode[] = "DATE(date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  182. }
  183. if ($implode) {
  184. $sql .= " WHERE " . implode(" AND ", $implode);
  185. }
  186. $query = $this->db->query($sql);
  187. return $query->row['total'];
  188. }
  189. public function getCustomersOnline($data = array()) {
  190. $sql = "SELECT co.ip, co.customer_id, co.url, co.referer, co.date_added FROM " . DB_PREFIX . "customer_online co LEFT JOIN " . DB_PREFIX . "customer c ON (co.customer_id = c.customer_id)";
  191. $implode = array();
  192. if (!empty($data['filter_ip'])) {
  193. $implode[] = "co.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
  194. }
  195. if (!empty($data['filter_customer'])) {
  196. $implode[] = "co.customer_id > 0 AND CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
  197. }
  198. if ($implode) {
  199. $sql .= " WHERE " . implode(" AND ", $implode);
  200. }
  201. $sql .= " ORDER BY co.date_added DESC";
  202. if (isset($data['start']) || isset($data['limit'])) {
  203. if ($data['start'] < 0) {
  204. $data['start'] = 0;
  205. }
  206. if ($data['limit'] < 1) {
  207. $data['limit'] = 20;
  208. }
  209. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  210. }
  211. $query = $this->db->query($sql);
  212. return $query->rows;
  213. }
  214. public function getTotalCustomersOnline($data = array()) {
  215. $sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "customer_online` co LEFT JOIN " . DB_PREFIX . "customer c ON (co.customer_id = c.customer_id)";
  216. $implode = array();
  217. if (!empty($data['filter_ip'])) {
  218. $implode[] = "co.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
  219. }
  220. if (!empty($data['filter_customer'])) {
  221. $implode[] = "co.customer_id > 0 AND CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
  222. }
  223. if ($implode) {
  224. $sql .= " WHERE " . implode(" AND ", $implode);
  225. }
  226. $query = $this->db->query($sql);
  227. return $query->row['total'];
  228. }
  229. public function getCustomerActivities($data = array()) {
  230. $sql = "SELECT ca.activity_id, ca.customer_id, ca.key, ca.data, ca.ip, ca.date_added FROM " . DB_PREFIX . "customer_activity ca LEFT JOIN " . DB_PREFIX . "customer c ON (ca.customer_id = c.customer_id)";
  231. $implode = array();
  232. if (!empty($data['filter_customer'])) {
  233. $implode[] = "CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
  234. }
  235. if (!empty($data['filter_ip'])) {
  236. $implode[] = "ca.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
  237. }
  238. if (!empty($data['filter_date_start'])) {
  239. $implode[] = "DATE(ca.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  240. }
  241. if (!empty($data['filter_date_end'])) {
  242. $implode[] = "DATE(ca.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  243. }
  244. if ($implode) {
  245. $sql .= " WHERE " . implode(" AND ", $implode);
  246. }
  247. $sql .= " ORDER BY ca.date_added DESC";
  248. if (isset($data['start']) || isset($data['limit'])) {
  249. if ($data['start'] < 0) {
  250. $data['start'] = 0;
  251. }
  252. if ($data['limit'] < 1) {
  253. $data['limit'] = 20;
  254. }
  255. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  256. }
  257. $query = $this->db->query($sql);
  258. return $query->rows;
  259. }
  260. public function getTotalCustomerActivities($data = array()) {
  261. $sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "customer_activity` ca LEFT JOIN " . DB_PREFIX . "customer c ON (ca.customer_id = c.customer_id)";
  262. $implode = array();
  263. if (!empty($data['filter_customer'])) {
  264. $implode[] = "CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
  265. }
  266. if (!empty($data['filter_ip'])) {
  267. $implode[] = "ca.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
  268. }
  269. if (!empty($data['filter_date_start'])) {
  270. $implode[] = "DATE(ca.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  271. }
  272. if (!empty($data['filter_date_end'])) {
  273. $implode[] = "DATE(ca.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  274. }
  275. if ($implode) {
  276. $sql .= " WHERE " . implode(" AND ", $implode);
  277. }
  278. $query = $this->db->query($sql);
  279. return $query->row['total'];
  280. }
  281. }