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

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

Report by provinces

File size: 5.7 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
19        $provinces = lang('_PROVINCES_');
20        // Tinh doanh thu theo tinh theo nam
21        $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";
22        $query = $this->db->query($sql);
23        $dtns = $query->result_array();
24        // tinh doanh thu theo thang
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'] . "-" . $input['month'] . "-01' AND tblpaidlog.paid_time <'" . $input['year'] . "-" . $input['month'] . "-31' GROUP BY tbluser.province";
26        $query = $this->db->query($sql);
27        $dtts = $query->result_array();
28
29
30        // Tinh thue bao theo tinh theo nam
31        $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";
32        $query = $this->db->query($sql);
33        $tbns = $query->result_array();
34
35        // tinh thue bao theo thang
36        $sql = "SELECT *,count(us_id) as total FROM tbluser WHERE tbluser.created_time >'" . $input['year'] . "-" . $input['month'] . "-01' AND tbluser.created_time <'" . $input['year'] . "-" . $input['month'] . "-31' GROUP BY tbluser.province";
37        $query = $this->db->query($sql);
38        $tbts = $query->result_array();
39
40
41        foreach ($provinces as $index2 => $province) {
42            $provinces[$index2] = array("stt" => ($index2 + 1), "province" => $province, "dtn" => 0, "dtt" => 0, "dtng" => 0, "tbn" => 0, "tbt" => 0, "tbng" => 0);
43            foreach ($dtns as $data) {
44                if ($province == $data['province']) {
45                    $provinces[$index2]['dtn'] = $data['total'];
46                }
47            }
48            foreach ($dtts as $data) {
49                if ($province == $data['province']) {
50                    $provinces[$index2]['dtt'] = $data['total'];
51                    $provinces[$index2]['dtng'] = round($data['total'] / $input['month_days'], 2);
52                }
53            }
54
55
56
57            foreach ($tbns as $data) {
58                if ($province == $data['province']) {
59                    $provinces[$index2]['tbn'] = $data['total'];
60                }
61            }
62            foreach ($tbts as $data) {
63                if ($province == $data['province']) {
64                    $provinces[$index2]['tbt'] = $data['total'];
65                    $provinces[$index2]['tbng'] = round($data['total'] / $input['month_days'], 2);
66                }
67            }
68        }
69        return $provinces;
70    }
71
72    function getPackages($input) {
73
74        $provinces = lang('_PROVINCES_');
75        // tinh thue bao theo thang
76        $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 " ;
77        $query = $this->db->query($sql);
78        $users = $query->result_array();
79
80
81        $sql = "SELECT * FROM tblservicepackage WHERE p_period>0";
82        $query = $this->db->query($sql);
83        $packages = $query->result_array();
84        foreach ($packages as $index => $package) {
85            $packages[$index]['users'] = array();
86            foreach ($users as $user) {
87                if ($user['p_id'] == $package['p_id']) {
88                    $packages[$index]['users'][] = $user;
89                }
90            }
91        }
92
93        foreach ($provinces as $index => $province) {
94            $provinces[$index] = array("province" => $province);
95        }
96
97        foreach ($provinces as $index => $province) {
98            $provinces[$index]['stt']=$index+1;
99            $provinces[$index]['total'] = 0;
100            $totaluser = 0;
101            foreach ($packages as $index2 => $package) {
102                $provinces[$index]['packages'][$package['p_name']]['users'] = array();
103                $provinces[$index]['packages'][$package['p_name']]['total'] = 0;
104                $total = 0;
105                foreach ($package['users'] as $user) {
106                    if ($province['province'] == $user['province']) {
107                        $total = $total + 1;
108                        $totaluser = $totaluser + 1;
109                        $provinces[$index]['total'] = $totaluser;
110                        $provinces[$index]['packages'][$package['p_name']]['total'] = $total;
111
112                        $provinces[$index]['packages'][$package['p_name']]['users'][] = $user;
113                    }
114                }
115            }
116        }
117        foreach ($provinces as $index => $province) {
118            foreach ($packages as $index2 => $package) {
119                $provinces[$index]['packages'][$package['p_name']]['percent'] = 0;
120                if ($provinces[$index]['packages'][$package['p_name']]['total'] > 0 && $province['total'] > 0) {
121                    $provinces[$index]['packages'][$package['p_name']]['percent'] = round($provinces[$index]['packages'][$package['p_name']]['total'] / $province['total'], 2) * 100;
122                }
123            }
124        }
125     
126        $data['packages']=$packages;
127        $data['provinces']=$provinces;
128        return $data;
129    }
130
131}
Note: See TracBrowser for help on using the repository browser.