1 | <?php |
---|
2 | |
---|
3 | if (!defined('BASEPATH')) |
---|
4 | exit('No direct script access allowed'); |
---|
5 | |
---|
6 | class Report extends MX_Controller { |
---|
7 | |
---|
8 | const TOKENPW = 'violet'; |
---|
9 | |
---|
10 | public function __construct() { |
---|
11 | parent::__construct(); |
---|
12 | $this->load->helper('cookie'); |
---|
13 | } |
---|
14 | |
---|
15 | public function index() { |
---|
16 | $data = array(); |
---|
17 | $admin_info = $this->session->userdata('adminInfo'); |
---|
18 | if ($admin_info) { |
---|
19 | $data['statistics'] = $this->getStatistics(); |
---|
20 | $data['content'] = $this->getPaidlogs(); |
---|
21 | $this->load->view('report/index', $data); |
---|
22 | } else { |
---|
23 | $this->load->view('login'); |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | public function getStatistics() { |
---|
28 | $data = array(); |
---|
29 | |
---|
30 | $last_month['start'] = strtotime(date("Y-m-d", strtotime("first day of previous month")) . " 00:00:00"); |
---|
31 | $last_month['end'] = strtotime(date("Y-m-d", strtotime("last day of previous month")) . " 23:59:59"); |
---|
32 | |
---|
33 | $last_week['start'] = strtotime(date("Y-m-d", strtotime("first day of previous week")) . " 00:00:00"); |
---|
34 | $last_week['end'] = strtotime(date("Y-m-d", strtotime("last day of previous week")) . " 23:59:59"); |
---|
35 | |
---|
36 | $last_week['end'] = strtotime('last sunday') + (24 * 60 * 60 - 1); |
---|
37 | $last_week['start'] = ($last_week['end'] - 7 * 24 * 60 * 60 + 1); |
---|
38 | |
---|
39 | $last_year['start'] = strtotime((date("Y") - 1) . "-01-01 00:00:00"); |
---|
40 | $last_year['end'] = strtotime((date("Y") - 1) . "-12-31 23:59:59"); |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | $last_quarter = $this->getLastquarter(); |
---|
45 | $last_quarter['start'] = $last_quarter['start']; |
---|
46 | $last_quarter['end'] = $last_quarter['end']; |
---|
47 | |
---|
48 | |
---|
49 | $this->load->model('paidlog_model'); |
---|
50 | $paidlogs = $this->paidlog_model->getAllPaidlogs($last_year['start']); |
---|
51 | |
---|
52 | $revenue['last_week'] = 0; |
---|
53 | $revenue['last_month'] = 0; |
---|
54 | $revenue['last_quarter'] = 0; |
---|
55 | $revenue['last_year'] = 0; |
---|
56 | |
---|
57 | $lastmonth = strtotime("first day of previous month"); |
---|
58 | $thismonth = strtotime("first day of this month"); |
---|
59 | $charts['last_month'] = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0); |
---|
60 | $charts['this_month'] = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0); |
---|
61 | foreach ($paidlogs as $paidlog) { |
---|
62 | |
---|
63 | if (($paidlog['time'] < $last_week['end']) && ($paidlog['time'] > $last_week['start'])) { |
---|
64 | $revenue['last_week']+=$paidlog['amount']; |
---|
65 | } |
---|
66 | if (($paidlog['time'] < $last_month['end']) && ($paidlog['time'] > $last_month['start'])) { |
---|
67 | $revenue['last_month']+=$paidlog['amount']; |
---|
68 | } |
---|
69 | if (($paidlog['time'] < $last_quarter['end']) && ($paidlog['time'] > $last_quarter['start'])) { |
---|
70 | $revenue['last_quarter']+=$paidlog['amount']; |
---|
71 | } |
---|
72 | if (($paidlog['time'] < $last_year['end']) && ($paidlog['time'] > $last_year['start'])) { |
---|
73 | $revenue['last_year']+=$paidlog['amount']; |
---|
74 | } |
---|
75 | if (($paidlog['time'] < $lastmonth + (7 * 24 * 60 * 60)) && ($paidlog['time'] > $lastmonth)) { |
---|
76 | $charts['last_month'][0]+=$paidlog['amount']; |
---|
77 | } |
---|
78 | if (($paidlog['time'] < $lastmonth + (14 * 24 * 60 * 60)) && ($paidlog['time'] > $lastmonth + (7 * 24 * 60 * 60))) { |
---|
79 | $charts['last_month'][1]+=$paidlog['amount']; |
---|
80 | } |
---|
81 | if (($paidlog['time'] < $lastmonth + (21 * 24 * 60 * 60)) && ($paidlog['time'] > $lastmonth + (14 * 24 * 60 * 60))) { |
---|
82 | $charts['last_month'][2]+=$paidlog['amount']; |
---|
83 | } |
---|
84 | if (($paidlog['time'] < $lastmonth + (28 * 24 * 60 * 60)) && ($paidlog['time'] > $lastmonth + (21 * 24 * 60 * 60))) { |
---|
85 | $charts['last_month'][3]+=$paidlog['amount']; |
---|
86 | } |
---|
87 | if (($paidlog['time'] < $thismonth) && ($paidlog['time'] > $lastmonth + (28 * 24 * 60 * 60))) { |
---|
88 | $charts['last_month'][3]+=$paidlog['amount']; |
---|
89 | } |
---|
90 | |
---|
91 | if (($paidlog['time'] < $thismonth + (7 * 24 * 60 * 60)) && ($paidlog['time'] > $thismonth)) { |
---|
92 | $charts['this_month'][0]+=$paidlog['amount']; |
---|
93 | } |
---|
94 | if (($paidlog['time'] < $thismonth + (14 * 24 * 60 * 60)) && ($paidlog['time'] > $thismonth + (7 * 24 * 60 * 60))) { |
---|
95 | $charts['this_month'][1]+=$paidlog['amount']; |
---|
96 | } |
---|
97 | if (($paidlog['time'] < $thismonth + (21 * 24 * 60 * 60)) && ($paidlog['time'] > $thismonth + (14 * 24 * 60 * 60))) { |
---|
98 | $charts['this_month'][2]+=$paidlog['amount']; |
---|
99 | } |
---|
100 | if (($paidlog['time'] < $thismonth + (28 * 24 * 60 * 60)) && ($paidlog['time'] > $thismonth + (21 * 24 * 60 * 60))) { |
---|
101 | $charts['this_month'][3]+=$paidlog['amount']; |
---|
102 | } |
---|
103 | if (($paidlog['time'] < $thismonth + (31 * 24 * 60 * 60)) && ($paidlog['time'] > $thismonth + (28 * 24 * 60 * 60))) { |
---|
104 | $charts['this_month'][3]+=$paidlog['amount']; |
---|
105 | } |
---|
106 | } |
---|
107 | $max = 0; |
---|
108 | foreach ($charts['last_month'] as $index => $total) { |
---|
109 | if ($total > $max) { |
---|
110 | $max = $total; |
---|
111 | } |
---|
112 | $chart_last_month[] = array($index, $total); |
---|
113 | } |
---|
114 | foreach ($charts['this_month'] as $index => $total) { |
---|
115 | if ($total > $max) { |
---|
116 | $max = $total; |
---|
117 | } |
---|
118 | $chart_this_month[] = array($index, $total); |
---|
119 | } |
---|
120 | |
---|
121 | $data['revenue'] = $revenue; |
---|
122 | $data['max'] = $max; |
---|
123 | $chard[] = array("color" => "#1abc9c", "label" => "Tháng hiá»n tại", "data" => $chart_this_month); |
---|
124 | $chard[] = array("color" => "#3498db", "label" => "Tháng trưá»c", "data" => $chart_last_month); |
---|
125 | $data['chard'] = $chard; |
---|
126 | |
---|
127 | return $this->load->view('report/statistics', $data, true); |
---|
128 | } |
---|
129 | |
---|
130 | public function getLastquarter() { |
---|
131 | $current_month = date('m'); |
---|
132 | $current_year = date('Y'); |
---|
133 | |
---|
134 | if ($current_month >= 1 && $current_month <= 3) { |
---|
135 | $start_date = strtotime('1-October-' . ($current_year - 1)); // timestamp or 1-October Last Year 12:00:00 AM |
---|
136 | $end_date = strtotime('1-Janauary-' . $current_year); // // timestamp or 1-January 12:00:00 AM means end of 31 December Last year |
---|
137 | } else if ($current_month >= 4 && $current_month <= 6) { |
---|
138 | $start_date = strtotime('1-January-' . $current_year); // timestamp or 1-Janauray 12:00:00 AM |
---|
139 | $end_date = strtotime('1-April-' . $current_year); // timestamp or 1-April 12:00:00 AM means end of 31 March |
---|
140 | } else if ($current_month >= 7 && $current_month <= 9) { |
---|
141 | $start_date = strtotime('1-April-' . $current_year); // timestamp or 1-April 12:00:00 AM |
---|
142 | $end_date = strtotime('1-July-' . $current_year); // timestamp or 1-July 12:00:00 AM means end of 30 June |
---|
143 | } else if ($current_month >= 10 && $current_month <= 12) { |
---|
144 | $start_date = strtotime('1-July-' . $current_year); // timestamp or 1-July 12:00:00 AM |
---|
145 | $end_date = strtotime('1-October-' . $current_year); // timestamp or 1-October 12:00:00 AM means end of 30 September |
---|
146 | } |
---|
147 | return array("start" => $start_date, "end" => $end_date); |
---|
148 | } |
---|
149 | |
---|
150 | public function getPaidlogs($filters = array()) { |
---|
151 | $this->load->helper('pagging'); |
---|
152 | $this->load->model('paidlog_model'); |
---|
153 | $data['current_page'] = $this->uri->segment(4, 1); |
---|
154 | $data['itemsoptions'] = array(10, 25, 50, 100); |
---|
155 | $data['perpage'] = 10; |
---|
156 | $data['keyword'] = ""; |
---|
157 | $data['sorting_order'] = "sorting_desc"; |
---|
158 | $data['sorting_field'] = "paid_id"; |
---|
159 | |
---|
160 | if ($this->input->post('sorting_order')) { |
---|
161 | if ($this->input->post('sorting_order') != "sorting") { |
---|
162 | $data['sorting_order'] = $this->input->post('sorting_order'); |
---|
163 | $data['sorting_field'] = $this->input->post('sorting_field'); |
---|
164 | } |
---|
165 | } |
---|
166 | if ($this->input->post('items')) { |
---|
167 | $data['perpage'] = $this->input->post('items'); |
---|
168 | } |
---|
169 | if ($this->input->post('keyword')) { |
---|
170 | $data['search_field'] = ""; |
---|
171 | $data['keyword'] = $this->input->post('keyword'); |
---|
172 | |
---|
173 | if (preg_match("/[0-9]/", $data['keyword'])) { |
---|
174 | $data['search_field'] = "cellphone"; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | $data['start'] = ($data['current_page'] - 1) * $data['perpage']; |
---|
179 | $data['total'] = $this->paidlog_model->countPaidlog($data); |
---|
180 | $data['paidlogs'] = $this->paidlog_model->getPaidlogs($data); |
---|
181 | |
---|
182 | $data['paging_url'] = base_url() . "/admin/doanh_thu_tong_hop/trang/"; |
---|
183 | $data['num_links'] = 2; |
---|
184 | $data['paging'] = pagging($data); |
---|
185 | foreach ($data['paidlogs'] as $index => $paidlog) { |
---|
186 | $data['paidlogs'][$index]['username'] = $this->get_username($paidlog['us_id']); |
---|
187 | } |
---|
188 | |
---|
189 | if ($this->input->is_ajax_request()) { |
---|
190 | return $this->load->view('report/listview', $data); |
---|
191 | } |
---|
192 | return $this->load->view('report/listview', $data, true); |
---|
193 | } |
---|
194 | |
---|
195 | public function get_username($us_id) { |
---|
196 | $src = 'violet'; |
---|
197 | $token = md5($us_id . self::TOKENPW); |
---|
198 | $this->load->model('user_model'); |
---|
199 | $data = $this->user_model->get_user_info($src, $us_id, $token); |
---|
200 | if (strlen($data) > 0) { |
---|
201 | $arr_users = explode("&", $data); |
---|
202 | $str_username = $arr_users[1]; |
---|
203 | $arr_username = explode("=", $str_username); |
---|
204 | return $arr_username[1]; |
---|
205 | } else { |
---|
206 | return ""; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | public function test() { |
---|
211 | |
---|
212 | |
---|
213 | for ($i = 1; $i <= 5000; $i++) { |
---|
214 | $input['us_id'] = rand(1, 100); |
---|
215 | $input['paid_type'] = rand(1, 2); |
---|
216 | $input['amount'] = rand(3, 10) * 1000; |
---|
217 | $time = (rand((time() - (12 * 30 * 24 * 60 * 60)), time())); |
---|
218 | $input['paid_time'] = date("Y-m-d H:i:s", $time); |
---|
219 | $this->load->model('paidlog_model'); |
---|
220 | $this->paidlog_model->insert($input); |
---|
221 | } |
---|
222 | |
---|
223 | die(); |
---|
224 | |
---|
225 | for ($i = 1; $i <= 100; $i++) { |
---|
226 | $input['us_id'] = $i; |
---|
227 | $input['cellphone'] = "01234567" . (100 + $i); |
---|
228 | $input['collaborator'] = rand(1, 100); |
---|
229 | $input['acc_balanced'] = rand(100000, 1000000); |
---|
230 | $input['expire_date'] = date("Y-m-d H:i:s", time()); |
---|
231 | $input['created_time'] = date("Y-m-d H:i:s", time()); |
---|
232 | $input['updated_time'] = date("Y-m-d H:i:s", time()); |
---|
233 | $this->load->model('user_model'); |
---|
234 | $this->user_model->insert($input); |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | } |
---|