PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/model/report/customer.php

https://gitlab.com/hazelnuts23/unitedfoodstuff
PHP | 390 lines | 280 code | 110 blank | 0 comment | 58 complexity | ad1244a3188402f65892ea9e58d456a8 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, COUNT(o.order_id) AS orders, SUM(op.quantity) AS products, SUM(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.customer_id ORDER BY total DESC";
  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. $query = $this->db->query($sql);
  98. return $query->rows;
  99. }
  100. public function getTotalOrders($data = array()) {
  101. $sql = "SELECT COUNT(DISTINCT o.customer_id) AS total FROM `" . DB_PREFIX . "order` o WHERE o.customer_id > '0'";
  102. if (!empty($data['filter_order_status_id'])) {
  103. $sql .= " AND o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
  104. } else {
  105. $sql .= " AND o.order_status_id > '0'";
  106. }
  107. if (!empty($data['filter_date_start'])) {
  108. $sql .= " AND DATE(o.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  109. }
  110. if (!empty($data['filter_date_end'])) {
  111. $sql .= " AND DATE(o.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  112. }
  113. $query = $this->db->query($sql);
  114. return $query->row['total'];
  115. }
  116. public function getRewardPoints($data = array()) {
  117. $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') . "'";
  118. if (!empty($data['filter_date_start'])) {
  119. $sql .= " AND DATE(cr.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  120. }
  121. if (!empty($data['filter_date_end'])) {
  122. $sql .= " AND DATE(cr.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  123. }
  124. $sql .= " GROUP BY cr.customer_id ORDER BY points DESC";
  125. if (isset($data['start']) || isset($data['limit'])) {
  126. if ($data['start'] < 0) {
  127. $data['start'] = 0;
  128. }
  129. if ($data['limit'] < 1) {
  130. $data['limit'] = 20;
  131. }
  132. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  133. }
  134. $query = $this->db->query($sql);
  135. return $query->rows;
  136. }
  137. public function getTotalRewardPoints($data = array()) {
  138. $sql = "SELECT COUNT(DISTINCT customer_id) AS total FROM `" . DB_PREFIX . "customer_reward`";
  139. $implode = array();
  140. if (!empty($data['filter_date_start'])) {
  141. $implode[] = "DATE(date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  142. }
  143. if (!empty($data['filter_date_end'])) {
  144. $implode[] = "DATE(date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  145. }
  146. if ($implode) {
  147. $sql .= " WHERE " . implode(" AND ", $implode);
  148. }
  149. $query = $this->db->query($sql);
  150. return $query->row['total'];
  151. }
  152. public function getCredit($data = array()) {
  153. $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') . "'";
  154. if (!empty($data['filter_date_start'])) {
  155. $sql .= " AND DATE(ct.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  156. }
  157. if (!empty($data['filter_date_end'])) {
  158. $sql .= " AND DATE(ct.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  159. }
  160. $sql .= " GROUP BY ct.customer_id ORDER BY total DESC";
  161. if (isset($data['start']) || isset($data['limit'])) {
  162. if ($data['start'] < 0) {
  163. $data['start'] = 0;
  164. }
  165. if ($data['limit'] < 1) {
  166. $data['limit'] = 20;
  167. }
  168. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  169. }
  170. $query = $this->db->query($sql);
  171. return $query->rows;
  172. }
  173. public function getTotalCredit($data = array()) {
  174. $sql = "SELECT COUNT(DISTINCT customer_id) AS total FROM `" . DB_PREFIX . "customer_transaction`";
  175. $implode = array();
  176. if (!empty($data['filter_date_start'])) {
  177. $implode[] = "DATE(date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  178. }
  179. if (!empty($data['filter_date_end'])) {
  180. $implode[] = "DATE(date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  181. }
  182. if ($implode) {
  183. $sql .= " WHERE " . implode(" AND ", $implode);
  184. }
  185. $query = $this->db->query($sql);
  186. return $query->row['total'];
  187. }
  188. public function getCustomersOnline($data = array()) {
  189. $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)";
  190. $implode = array();
  191. if (!empty($data['filter_ip'])) {
  192. $implode[] = "co.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
  193. }
  194. if (!empty($data['filter_customer'])) {
  195. $implode[] = "co.customer_id > 0 AND CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
  196. }
  197. if ($implode) {
  198. $sql .= " WHERE " . implode(" AND ", $implode);
  199. }
  200. $sql .= " ORDER BY co.date_added DESC";
  201. if (isset($data['start']) || isset($data['limit'])) {
  202. if ($data['start'] < 0) {
  203. $data['start'] = 0;
  204. }
  205. if ($data['limit'] < 1) {
  206. $data['limit'] = 20;
  207. }
  208. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  209. }
  210. $query = $this->db->query($sql);
  211. return $query->rows;
  212. }
  213. public function getTotalCustomersOnline($data = array()) {
  214. $sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "customer_online` co LEFT JOIN " . DB_PREFIX . "customer c ON (co.customer_id = c.customer_id)";
  215. $implode = array();
  216. if (!empty($data['filter_ip'])) {
  217. $implode[] = "co.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
  218. }
  219. if (!empty($data['filter_customer'])) {
  220. $implode[] = "co.customer_id > 0 AND CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
  221. }
  222. if ($implode) {
  223. $sql .= " WHERE " . implode(" AND ", $implode);
  224. }
  225. $query = $this->db->query($sql);
  226. return $query->row['total'];
  227. }
  228. public function getCustomerActivities($data = array()) {
  229. $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)";
  230. $implode = array();
  231. if (!empty($data['filter_customer'])) {
  232. $implode[] = "CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
  233. }
  234. if (!empty($data['filter_ip'])) {
  235. $implode[] = "ca.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
  236. }
  237. if (!empty($data['filter_date_start'])) {
  238. $implode[] = "DATE(ca.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  239. }
  240. if (!empty($data['filter_date_end'])) {
  241. $implode[] = "DATE(ca.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  242. }
  243. if ($implode) {
  244. $sql .= " WHERE " . implode(" AND ", $implode);
  245. }
  246. $sql .= " ORDER BY ca.date_added DESC";
  247. if (isset($data['start']) || isset($data['limit'])) {
  248. if ($data['start'] < 0) {
  249. $data['start'] = 0;
  250. }
  251. if ($data['limit'] < 1) {
  252. $data['limit'] = 20;
  253. }
  254. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  255. }
  256. $query = $this->db->query($sql);
  257. return $query->rows;
  258. }
  259. public function getTotalCustomerActivities($data = array()) {
  260. $sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "customer_activity` ca LEFT JOIN " . DB_PREFIX . "customer c ON (ca.customer_id = c.customer_id)";
  261. $implode = array();
  262. if (!empty($data['filter_customer'])) {
  263. $implode[] = "CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
  264. }
  265. if (!empty($data['filter_ip'])) {
  266. $implode[] = "ca.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
  267. }
  268. if (!empty($data['filter_date_start'])) {
  269. $implode[] = "DATE(ca.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
  270. }
  271. if (!empty($data['filter_date_end'])) {
  272. $implode[] = "DATE(ca.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
  273. }
  274. if ($implode) {
  275. $sql .= " WHERE " . implode(" AND ", $implode);
  276. }
  277. $query = $this->db->query($sql);
  278. return $query->row['total'];
  279. }
  280. }