source: pro-violet-viettel/sourcecode/application/modules/admin/models/reportmodel.php @ 818

Last change on this file since 818 was 818, checked in by namnd, 10 years ago
File size: 11.3 KB
Line 
1<?php
2if (! defined ( 'BASEPATH' ))
3        exit ( 'No direct script access allowed' );
4
5class 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                $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";
36                $query = $this->db->query ( $sql );
37                $dtts = $query->result_array ();
38               
39                // tinh doanh thu trong ngay hien tai
40                if (isset ( $input ['to_month'] )) {
41                        $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";
42                }
43                $query = $this->db->query ( $sql );
44                $dtng = $query->result_array ();
45               
46                // Tinh thue bao theo tinh theo nam
47                $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";
48                $query = $this->db->query ( $sql );
49                write_file('./log/test.log', date('d-m-Y H:i:s').": ".$sql."\n", FOPEN_WRITE_CREATE);
50                $tbns = $query->result_array ();
51               
52                // tinh thue bao theo thang
53                //$sql = "SELECT *,count(us_id) as total FROM tbluser WHERE $from AND  $to GROUP BY tbluser.province";
54                $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";
55                $query = $this->db->query ( $sql );
56                $tbts = $query->result_array ();
57               
58                // tinh thue bao theo ngay
59                $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";
60                $query = $this->db->query ( $sql );
61                $tbng = $query->result_array ();
62                $dem = 1;
63               
64                $totalTbng = 0;
65                $totalTbth = 0;
66                $totalTbn = 0;
67                $totalDtng = 0;
68                $totalDtth = 0;
69                $totalDtn = 0;
70               
71                foreach ( $provinces as $index2 => $province ) {
72                        $provinces [$index2] = array (
73                                        "stt" => ($dem),
74                                        "province" => $province,
75                                        "dtn" => 0,
76                                        "dtt" => 0,
77                                        "dtng" => 0,
78                                        "tbn" => 0,
79                                        "tbt" => 0,
80                                        "tbng" => 0
81                        );
82                        foreach ( $dtns as $data ) {
83                                if ($index2 == $data ['province']) {
84                                        $provinces [$index2] ['dtn'] = $data ['total'];
85                                }
86                        }
87                        foreach ( $dtng as $data ) {
88                                if ($index2 == $data ['province']) {
89                                        $provinces [$index2] ['dtng'] = $data ['total'];
90                                }
91                        }
92                        foreach ( $dtts as $data ) {
93                                if ($index2 == $data ['province']) {
94                                        $provinces [$index2] ['dtt'] = $data ['total'];
95                                }
96                        }
97                       
98                        foreach ( $tbns as $data ) {
99                                if ($index2 == $data ['province']) {
100                                        $provinces [$index2] ['tbn'] = $data ['total'];
101                                }
102                        }
103                        foreach ( $tbng as $data ) {
104                                if ($index2 == $data ['province']) {
105                                        $provinces [$index2] ['tbng'] = $data ['total'];
106                                }
107                        }
108                        foreach ( $tbts as $data ) {
109                                if ($index2 == $data ['province']) {
110                                        $provinces [$index2] ['tbt'] = $data ['total'];
111                                }
112                        }
113                       
114                        $totalTbng = $totalTbng + $provinces [$index2] ['tbng'];
115                        $totalTbth = $totalTbth + $provinces [$index2] ['tbt'];
116                        $totalTbn = $totalTbn + $provinces [$index2] ['tbn'];
117                       
118                        $totalDtng = $totalDtng + $provinces [$index2] ['dtng'];
119                        $totalDtth = $totalDtth + $provinces [$index2] ['dtt'];
120                        $totalDtn = $totalDtn + $provinces [$index2] ['dtn'];
121                       
122                        $dem ++;
123                }
124
125                $data['provinces'] = $provinces;
126               
127                $data['totalTbng'] = $totalTbng;
128                $data['totalTbth'] = $totalTbth;
129                $data['totalTbn'] = $totalTbn;
130               
131                $data['totalDtng'] = $totalDtng;
132                $data['totalDtth'] = $totalDtth;
133                $data['totalDtn'] = $totalDtn;
134               
135                //return $provinces;
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}
Note: See TracBrowser for help on using the repository browser.