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

Last change on this file since 759 was 759, checked in by quyenla, 10 years ago

report

File size: 8.6 KB
Line 
1<?php
2
3if (!defined('BASEPATH'))
4    exit('No direct script access allowed');
5
6class Reportmodel extends MY_Model {
7
8    protected $table_name = 'tblpaidlog';
9    protected $id_name = 'id';
10
11    function __construct() {
12        parent::__construct();
13        $this->load->helper('language');
14        $this->lang->load('messages', 'message');
15    }
16
17    function getProvinces($input) {
18        $ext="";
19        if (isset($input['to_month']))
20        {
21            $ext="AND tblpaidlog.paid_time <'" . $input['to_year'] . "-" . $input['to_month'] . "-01 23:59:59'";
22        }
23        $provinces = lang('_PROVINCES_');
24        // Tinh doanh thu theo tinh theo nam
25        $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";
26        $query = $this->db->query($sql);
27        $dtns = $query->result_array();
28        // tinh doanh thu theo thang
29        $sql = "SELECT *,SUM(amount) as total FROM tblpaidlog LEFT JOIN tbluser ON tbluser.us_id=tblpaidlog.us_id WHERE tblpaidlog.paid_time >'" . $input['year'] . "-" . $input['month'] . "-01' $ext  GROUP BY tbluser.province";
30        $query = $this->db->query($sql);
31        $dtts = $query->result_array();
32        // tinh doanh thu trong ngay hien tai
33        $sql = "SELECT *,SUM(amount) as total FROM tblpaidlog LEFT JOIN tbluser ON tbluser.us_id=tblpaidlog.us_id WHERE tblpaidlog.paid_time > '" . date('Y') . "-" . date('m') . "-".date('d')." 00:00:00' AND  tblpaidlog.paid_time <'" . date('Y') . "-" . date('m') . "-".date('d')." 23:59:59'  GROUP BY tbluser.province";
34        if (isset($input['to_month']))
35        {
36        $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";
37        }
38        $query = $this->db->query($sql);
39        $dtng = $query->result_array();
40
41        // Tinh thue bao theo tinh theo nam
42        $sql = "SELECT *,count(us_id) as total FROM tbluser WHERE tbluser.created_time >'" . $input['year'] . "-01-01' AND tbluser.created_time <'" . ($input['year'] + 1) . "-01-01' GROUP BY tbluser.province";
43        $query = $this->db->query($sql);
44        $tbns = $query->result_array();
45
46        // tinh thue bao theo thang
47        $sql = "SELECT *,count(us_id) as total FROM tbluser WHERE tbluser.created_time >'" . $input['year'] . "-" . $input['month'] . "-01'  $ext GROUP BY tbluser.province";
48        $query = $this->db->query($sql);
49        $tbts = $query->result_array();
50       
51         // tinh thue bao theo ngay
52        $sql = "SELECT *,count(us_id) as total FROM tbluser WHERE tbluser.created_time >'" . date('Y') . "-" . date('m') . "-".date('d')." 00:00:00' AND tbluser.created_time < '" . date('Y') . "-" . date('m') . "-".date('d')." 23:59:59'  GROUP BY tbluser.province";
53        if (isset($input['to_month']))
54        {
55            $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";
56        }
57        $query = $this->db->query($sql);
58        $tbng = $query->result_array();
59       
60
61        $dem=1;
62        foreach ($provinces as $index2 => $province) {
63            $provinces[$index2] = array("stt" => ($dem), "province" => $province, "dtn" => 0, "dtt" => 0, "dtng" => 0, "tbn" => 0, "tbt" => 0, "tbng" => 0);
64            foreach ($dtns as $data) {
65                if ($index2 == $data['province']) {
66                    $provinces[$index2]['dtn'] = $data['total'];
67                }
68            }
69            foreach ($dtng as $data) {
70                if ($index2 == $data['province']) {
71                    $provinces[$index2]['dtng'] = $data['total'];
72                }
73            }
74            foreach ($dtts as $data) {
75                if ($index2 == $data['province']) {
76                    $provinces[$index2]['dtt'] = $data['total'];
77                   
78                }
79            }
80
81            foreach ($tbns as $data) {
82                if ($index2 == $data['province']) {
83                    $provinces[$index2]['tbn'] = $data['total'];
84                }
85            }
86            foreach ($tbng as $data) {
87                if ($index2 == $data['province']) {
88                    $provinces[$index2]['tbng'] = $data['total'];
89                }
90            }
91            foreach ($tbts as $data) {
92                if ($index2 == $data['province']) {
93                    $provinces[$index2]['tbt'] = $data['total'];
94                }
95            }
96            $dem++;
97        }
98       
99        return $provinces;
100    }
101
102    function getPackages($input) {
103
104        $provinces = lang('_PROVINCES_');
105        // tinh thue bao theo thang
106        $sql = "SELECT * FROM tbluser LEFT JOIN tblservicepackage ON tbluser.p_id=tblservicepackage.p_id  WHERE tbluser.created_time >'" . $input['year'] . "-" . $input['month'] . "-01' AND tbluser.created_time <'" . $input['year'] . "-" . $input['month'] . "-31' AND tblservicepackage.p_period > 0 ";
107        $query = $this->db->query($sql);
108        $users = $query->result_array();
109
110
111        $sql = "SELECT * FROM tblservicepackage WHERE p_period>0";
112        $query = $this->db->query($sql);
113        $packages = $query->result_array();
114        foreach ($packages as $index => $package) {
115            $packages[$index]['users'] = array();
116            foreach ($users as $user) {
117                if ($user['p_id'] == $package['p_id']) {
118                    $packages[$index]['users'][] = $user;
119                }
120            }
121        }
122
123        foreach ($provinces as $index => $province) {
124            $provinces[$index] = array("province" => $province);
125        }
126        $dem=1;
127        foreach ($provinces as $index => $province) {
128            $provinces[$index]['stt'] = $dem;
129            $provinces[$index]['total'] = 0;
130            $totaluser = 0;
131            foreach ($packages as $index2 => $package) {
132                $provinces[$index]['packages'][$package['p_name']]['users'] = array();
133                $provinces[$index]['packages'][$package['p_name']]['total'] = 0;
134                $total = 0;
135                foreach ($package['users'] as $user) {
136                    if ($index == $user['province']) {
137                        $total = $total + 1;
138                        $totaluser = $totaluser + 1;
139                        $provinces[$index]['total'] = $totaluser;
140                        $provinces[$index]['packages'][$package['p_name']]['total'] = $total;
141
142                        $provinces[$index]['packages'][$package['p_name']]['users'][] = $user;
143                    }
144                }
145            }
146            $dem++;
147        }
148        foreach ($provinces as $index => $province) {
149            foreach ($packages as $index2 => $package) {
150                $provinces[$index]['packages'][$package['p_name']]['percent'] = 0;
151                if ($provinces[$index]['packages'][$package['p_name']]['total'] > 0 && $province['total'] > 0) {
152                    $provinces[$index]['packages'][$package['p_name']]['percent'] = round($provinces[$index]['packages'][$package['p_name']]['total'] / $province['total'], 2) * 100;
153                }
154            }
155        }
156
157        $data['packages'] = $packages;
158        $data['provinces'] = $provinces;
159        return $data;
160    }
161    public function exportProvince($input)
162    {
163        //$input['month']=1;
164        //$input['year']=2014;
165        //$input['to_month']=1;
166        //$input['to_year']=2015;
167        //$input['to_date']=15;
168        $provinces=array();
169        $data=$this->getProvinces($input);
170        foreach ($data as $index=>$province)
171        {
172            $provinces[]=array("stt"=>$province['stt'],"ngay"=>date("Ymd"),"ma"=>$index,"tinh"=>$province['province'],"thue_bao_ngay"=>$province['tbng'],"thue_bao_thang"=>$province['tbt'],"thue_bao_nam"=>$province['tbn'],"doanh_thu_ngay"=>$province['dtng'],"doanh_thu_thang"=>$province['dtt'],"doanh_thu_nam"=>$province['dtn']);
173        }
174        return $provinces;
175    }
176    public function exportPackage($input)
177    {
178        //$input['month']=1;
179        //$input['year']=2014;
180        $result=array();
181        $data=$this->getPackages($input);
182        $provinces=$data['provinces'];
183        return $provinces;
184    }
185
186}
Note: See TracBrowser for help on using the repository browser.