1 | <?php
|
---|
2 | if (! defined ( 'BASEPATH' ))
|
---|
3 | exit ( 'No direct script access allowed' );
|
---|
4 |
|
---|
5 | class Reportmodel extends MY_Model {
|
---|
6 |
|
---|
7 | protected $table_name = 'tblpaidlog';
|
---|
8 | protected $id_name = 'id';
|
---|
9 |
|
---|
10 | /**
|
---|
11 | *
|
---|
12 | */
|
---|
13 | function __construct() {
|
---|
14 | parent::__construct ();
|
---|
15 | $this->load->helper ( 'language' );
|
---|
16 | $this->lang->load ( 'messages', 'message' );
|
---|
17 | }
|
---|
18 |
|
---|
19 | /**
|
---|
20 | *
|
---|
21 | * @param unknown $input
|
---|
22 | * @return Ambigous <multitype:number unknown , unknown, string>
|
---|
23 | */
|
---|
24 | function getProvinces($input) {
|
---|
25 | $from = "tblpaidlog.paid_time >= '" . $input ['year'] . "-" . $input ['month'] . "-" . $input ['date'] . " 00:00:00'";
|
---|
26 | $to = "tblpaidlog.paid_time <= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . " 23:59:59'";
|
---|
27 |
|
---|
28 | $provinces = lang ( '_PROVINCES_' );
|
---|
29 | // Tinh doanh thu theo tinh theo nam
|
---|
30 | $sql = "SELECT *,SUM(amount) as total FROM tblpaidlog LEFT JOIN tbluser ON tbluser.us_id=tblpaidlog.us_id WHERE tblpaidlog.paid_time >= '" . $input ['year'] . "-01-01' AND tblpaidlog.paid_time <= '" . ($input ['year'] + 1) . "-01-01' GROUP BY tbluser.province";
|
---|
31 | $query = $this->db->query ( $sql );
|
---|
32 | $dtns = $query->result_array ();
|
---|
33 |
|
---|
34 | // tinh doanh thu theo thang
|
---|
35 | $from = "tblpaidlog.paid_time >= '" . $input ['year'] . "-" . $input ['month'] . "-01 00:00:00'";
|
---|
36 | $sql = "SELECT *,SUM(amount) as total FROM tblpaidlog LEFT JOIN tbluser ON tbluser.us_id=tblpaidlog.us_id WHERE $from AND $to GROUP BY tbluser.province";
|
---|
37 | $query = $this->db->query ( $sql );
|
---|
38 | $dtts = $query->result_array ();
|
---|
39 |
|
---|
40 | // tinh doanh thu trong ngay hien tai
|
---|
41 | if (isset ( $input ['to_month'] )) {
|
---|
42 | $sql = "SELECT *,SUM(amount) as total FROM tblpaidlog LEFT JOIN tbluser ON tbluser.us_id=tblpaidlog.us_id WHERE tblpaidlog.paid_time >= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . " 00:00:00' AND tblpaidlog.paid_time <= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . " 23:59:59' GROUP BY tbluser.province";
|
---|
43 | }
|
---|
44 | $query = $this->db->query ( $sql );
|
---|
45 | $dtng = $query->result_array ();
|
---|
46 |
|
---|
47 | // Tinh thue bao theo tinh theo nam
|
---|
48 | $sql = "SELECT *,count(us_id) as total FROM tbluser WHERE tbluser.created_time >= '" . $input ['year'] . "-01-01 00:00:00' AND tbluser.created_time <= '" . ($input ['year'] + 1) . "-01-01 23:59:59' GROUP BY tbluser.province";
|
---|
49 | $query = $this->db->query ( $sql );
|
---|
50 | write_file('./log/test.log', date('d-m-Y H:i:s').": ".$sql."\n", FOPEN_WRITE_CREATE);
|
---|
51 | $tbns = $query->result_array ();
|
---|
52 |
|
---|
53 | // tinh thue bao theo thang
|
---|
54 | //$sql = "SELECT *,count(us_id) as total FROM tbluser WHERE $from AND $to GROUP BY tbluser.province";
|
---|
55 | $sql = "SELECT *,count(us_id) as total FROM tbluser WHERE created_time >= '" . $input ['year'] . "-" . $input ['month'] . "-" . '01 00:00:00' . "' AND created_time <= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . " 23:59:59' GROUP BY tbluser.province";
|
---|
56 | $query = $this->db->query ( $sql );
|
---|
57 | $tbts = $query->result_array ();
|
---|
58 |
|
---|
59 | // tinh thue bao theo ngay
|
---|
60 | $sql = "SELECT *,count(us_id) as total FROM tbluser WHERE tbluser.created_time >='" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . " 00:00:00' AND tbluser.created_time <= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . " 23:59:59' GROUP BY tbluser.province";
|
---|
61 | $query = $this->db->query ( $sql );
|
---|
62 | $tbng = $query->result_array ();
|
---|
63 | $dem = 1;
|
---|
64 |
|
---|
65 | $totalTbng = 0;
|
---|
66 | $totalTbth = 0;
|
---|
67 | $totalTbn = 0;
|
---|
68 | $totalDtng = 0;
|
---|
69 | $totalDtth = 0;
|
---|
70 | $totalDtn = 0;
|
---|
71 |
|
---|
72 | foreach ( $provinces as $index2 => $province ) {
|
---|
73 | $provinces [$index2] = array (
|
---|
74 | "stt" => ($dem),
|
---|
75 | "province" => $province,
|
---|
76 | "dtn" => 0,
|
---|
77 | "dtt" => 0,
|
---|
78 | "dtng" => 0,
|
---|
79 | "tbn" => 0,
|
---|
80 | "tbt" => 0,
|
---|
81 | "tbng" => 0
|
---|
82 | );
|
---|
83 | foreach ( $dtns as $data ) {
|
---|
84 | if ($index2 == $data ['province']) {
|
---|
85 | $provinces [$index2] ['dtn'] = $data ['total'];
|
---|
86 | }
|
---|
87 | }
|
---|
88 | foreach ( $dtng as $data ) {
|
---|
89 | if ($index2 == $data ['province']) {
|
---|
90 | $provinces [$index2] ['dtng'] = $data ['total'];
|
---|
91 | }
|
---|
92 | }
|
---|
93 | foreach ( $dtts as $data ) {
|
---|
94 | if ($index2 == $data ['province']) {
|
---|
95 | $provinces [$index2] ['dtt'] = $data ['total'];
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | foreach ( $tbns as $data ) {
|
---|
100 | if ($index2 == $data ['province']) {
|
---|
101 | $provinces [$index2] ['tbn'] = $data ['total'];
|
---|
102 | }
|
---|
103 | }
|
---|
104 | foreach ( $tbng as $data ) {
|
---|
105 | if ($index2 == $data ['province']) {
|
---|
106 | $provinces [$index2] ['tbng'] = $data ['total'];
|
---|
107 | }
|
---|
108 | }
|
---|
109 | foreach ( $tbts as $data ) {
|
---|
110 | if ($index2 == $data ['province']) {
|
---|
111 | $provinces [$index2] ['tbt'] = $data ['total'];
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | $totalTbng = $totalTbng + $provinces [$index2] ['tbng'];
|
---|
116 | $totalTbth = $totalTbth + $provinces [$index2] ['tbt'];
|
---|
117 | $totalTbn = $totalTbn + $provinces [$index2] ['tbn'];
|
---|
118 |
|
---|
119 | $totalDtng = $totalDtng + $provinces [$index2] ['dtng'];
|
---|
120 | $totalDtth = $totalDtth + $provinces [$index2] ['dtt'];
|
---|
121 | $totalDtn = $totalDtn + $provinces [$index2] ['dtn'];
|
---|
122 |
|
---|
123 | $dem ++;
|
---|
124 | }
|
---|
125 |
|
---|
126 | $data['provinces'] = $provinces;
|
---|
127 |
|
---|
128 | $data['totalTbng'] = $totalTbng;
|
---|
129 | $data['totalTbth'] = $totalTbth;
|
---|
130 | $data['totalTbn'] = $totalTbn;
|
---|
131 |
|
---|
132 | $data['totalDtng'] = $totalDtng;
|
---|
133 | $data['totalDtth'] = $totalDtth;
|
---|
134 | $data['totalDtn'] = $totalDtn;
|
---|
135 |
|
---|
136 | return $data;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | *
|
---|
141 | * @param unknown $input
|
---|
142 | * @return Ambigous <multitype:, number, string>
|
---|
143 | */
|
---|
144 | function getPackages($input) {
|
---|
145 | $provinces = lang ( '_PROVINCES_' );
|
---|
146 | // tinh thue bao theo thang
|
---|
147 | //$sql = "SELECT * FROM tbluser LEFT JOIN tblservicepackage ON tbluser.p_id=tblservicepackage.p_id LEFT JOIN tblregisterpackagelog ON tbluser.cellphone = tblregisterpackagelog.username WHERE tbluser.updated_time >= '" . $input ['year'] . "-" . $input ['month'] . "-" . $input ['date'] . "' AND tbluser.updated_time <= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . "' AND tblregisterpackagelog.created_time >= '" . $input ['year'] . "-" . $input ['month'] . "-" . $input ['date'] . "' AND tblregisterpackagelog.created_time <= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . "' AND tblservicepackage.p_period > 0 ";
|
---|
148 | $sql = "SELECT * FROM tbluser LEFT JOIN tblregisterpackagelog ON tbluser.cellphone = tblregisterpackagelog.username WHERE tbluser.updated_time >= '" . $input ['year'] . "-" . $input ['month'] . "-" . $input ['date'] . "' AND tbluser.updated_time <= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . "' AND tblregisterpackagelog.created_time >= '" . $input ['year'] . "-" . $input ['month'] . "-" . $input ['date'] . "' AND tblregisterpackagelog.created_time <= '" . $input ['to_year'] . "-" . $input ['to_month'] . "-" . $input ['to_date'] . "'";
|
---|
149 | $query = $this->db->query ( $sql );
|
---|
150 | $users = $query->result_array ();
|
---|
151 |
|
---|
152 | $sql = "SELECT * FROM tblservicepackage WHERE p_period>0";
|
---|
153 | $query = $this->db->query ( $sql );
|
---|
154 | $packages = $query->result_array ();
|
---|
155 | foreach ( $packages as $index => $package ) {
|
---|
156 | $packages [$index] ['users'] = array ();
|
---|
157 | foreach ( $users as $user ) {
|
---|
158 | if ($user ['p_code'] == $package ['p_code']) {
|
---|
159 | $packages [$index] ['users'] [] = $user;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | foreach ( $provinces as $index => $province ) {
|
---|
164 | $provinces [$index] = array (
|
---|
165 | "province" => $province
|
---|
166 | );
|
---|
167 | }
|
---|
168 |
|
---|
169 | $dem = 1;
|
---|
170 |
|
---|
171 | foreach ( $provinces as $index => $province ) {
|
---|
172 | $provinces [$index] ['stt'] = $dem;
|
---|
173 | $provinces [$index] ['total'] = 0;
|
---|
174 | $totaluser = 0;
|
---|
175 | foreach ( $packages as $index2 => $package ) {
|
---|
176 | $provinces [$index] ['packages'] [$package ['p_name']] ['users'] = array ();
|
---|
177 | $provinces [$index] ['packages'] [$package ['p_name']] ['total'] = 0;
|
---|
178 | $total = 0;
|
---|
179 | foreach ( $package ['users'] as $user ) {
|
---|
180 | if ($index == $user ['province']) {
|
---|
181 | $total = $total + 1;
|
---|
182 | $totaluser = $totaluser + 1;
|
---|
183 | $provinces [$index] ['total'] = $totaluser;
|
---|
184 | $provinces [$index] ['packages'] [$package ['p_name']] ['total'] = $total;
|
---|
185 | $provinces [$index] ['packages'] [$package ['p_name']] ['users'] [] = $user;
|
---|
186 |
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | $dem ++;
|
---|
191 | }
|
---|
192 |
|
---|
193 | $totalByVIP = array();
|
---|
194 |
|
---|
195 | foreach ($packages as $index => $package){
|
---|
196 | $totalByVIP[$package ['p_name']] = 0;
|
---|
197 | foreach ( $provinces as $index=>$province){
|
---|
198 | foreach ($province['packages'] as $index2 => $p){
|
---|
199 | if ($index2 == $package['p_name'])
|
---|
200 | $totalByVIP[$package ['p_name']] = $totalByVIP[$package ['p_name']] + $p['total'];
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | $totalVip = 0;
|
---|
206 |
|
---|
207 | foreach ($totalByVIP as $vipName=>$total){
|
---|
208 | $totalVip = $totalVip + $total;
|
---|
209 | }
|
---|
210 |
|
---|
211 | $totalByVIP['total'] = $totalVip;
|
---|
212 |
|
---|
213 | foreach ( $provinces as $index => $province ) {
|
---|
214 | foreach ( $packages as $index2 => $package ) {
|
---|
215 | $provinces [$index] ['packages'] [$package ['p_name']] ['percent'] = 0;
|
---|
216 | if ($provinces [$index] ['packages'] [$package ['p_name']] ['total'] > 0 && $province ['total'] > 0) {
|
---|
217 | $provinces [$index] ['packages'] [$package ['p_name']] ['percent'] = round ( $provinces [$index] ['packages'] [$package ['p_name']] ['total'] / $province ['total'], 2 ) * 100;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | $data ['packages'] = $packages;
|
---|
223 | $data ['provinces'] = $provinces;
|
---|
224 | $data['totalVip'] = $totalByVIP;
|
---|
225 | return $data;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | *
|
---|
230 | * @param unknown $input
|
---|
231 | * @return multitype:multitype:unknown string Ambigous <Ambigous, multitype:number unknown , unknown, string>
|
---|
232 | */
|
---|
233 | public function exportProvince($input) {
|
---|
234 | // $input['date']=1;
|
---|
235 | // $input['month']=1;
|
---|
236 | // $input['year']=2014;
|
---|
237 | // $input['to_month']=1;
|
---|
238 | // $input['to_year']=2015;
|
---|
239 | // $input['to_date']=15;
|
---|
240 | if (isset ( $input ['to_month'] )) {
|
---|
241 | $date = $input ['to_year'] . $input ['to_month'] . $input ['to_date'];
|
---|
242 | } else {
|
---|
243 | $date = date ( "Ymd" );
|
---|
244 | }
|
---|
245 | $provinces = array ();
|
---|
246 | $data = $this->getProvinces ( $input );
|
---|
247 | foreach ( $data as $index => $province ) {
|
---|
248 | $provinces [] = array (
|
---|
249 | "stt" => $province ['stt'],
|
---|
250 | "ngay" => $date,
|
---|
251 | "ma" => $index,
|
---|
252 | "tinh" => $province ['province'],
|
---|
253 | "thue_bao_ngay" => $province ['tbng'],
|
---|
254 | "thue_bao_thang" => $province ['tbt'],
|
---|
255 | "thue_bao_nam" => $province ['tbn'],
|
---|
256 | "doanh_thu_ngay" => $province ['dtng'],
|
---|
257 | "doanh_thu_thang" => $province ['dtt'],
|
---|
258 | "doanh_thu_nam" => $province ['dtn']
|
---|
259 | );
|
---|
260 | }
|
---|
261 | return $provinces;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | *
|
---|
266 | * @param unknown $input
|
---|
267 | * @return multitype:
|
---|
268 | */
|
---|
269 | public function exportPackage($input) {
|
---|
270 | // $input['date']=1;
|
---|
271 | // $input['month']=1;
|
---|
272 | // $input['year']=2014;
|
---|
273 | // $input['to_month']=1;
|
---|
274 | // $input['to_year']=2015;
|
---|
275 | // $input['to_date']=15;
|
---|
276 | if (isset ( $input ['month'] )) {
|
---|
277 | $date = $input ['year'] . $input ['month'];
|
---|
278 | } else {
|
---|
279 | $date = date ( "Ym" );
|
---|
280 | }
|
---|
281 | $result = array ();
|
---|
282 | $data = $this->getPackages ( $input );
|
---|
283 | $data = $data ['provinces'];
|
---|
284 | foreach ( $data as $index => $province ) {
|
---|
285 | $package = array ();
|
---|
286 | foreach ( $province ['packages'] as $index2 => $packages ) {
|
---|
287 | $package = array_merge ( $package, array (
|
---|
288 | "tong_so_" . $index2 => $packages ['total'],
|
---|
289 | "ti_le_" . $index2 => $packages ['percent']
|
---|
290 | ) );
|
---|
291 | }
|
---|
292 | $tong = array (
|
---|
293 | "tong_so_tb" => $province ['total']
|
---|
294 | );
|
---|
295 | $province = array (
|
---|
296 | "stt" => $province ['stt'],
|
---|
297 | "ngay" => $date,
|
---|
298 | "ma" => $index,
|
---|
299 | "tinh" => $province ['province']
|
---|
300 | );
|
---|
301 | $provinces [] = array_merge ( $province, $package, $tong );
|
---|
302 | }
|
---|
303 | return $provinces;
|
---|
304 | }
|
---|
305 | } |
---|