[702] | 1 | <?php |
---|
| 2 | |
---|
| 3 | if (!defined('BASEPATH')) |
---|
| 4 | exit('No direct script access allowed'); |
---|
| 5 | |
---|
| 6 | class 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) { |
---|
[761] | 18 | |
---|
| 19 | $from="tblpaidlog.paid_time >'" . $input['year'] . "-" . $input['month'] . "-" . $input['date'] . " 00:00:00'"; |
---|
| 20 | $to="tblpaidlog.paid_time <'" . $input['to_year'] . "-" . $input['to_month'] . "-" . $input['to_date'] . " 23:59:59'"; |
---|
| 21 | |
---|
[702] | 22 | $provinces = lang('_PROVINCES_'); |
---|
| 23 | // Tinh doanh thu theo tinh theo nam |
---|
| 24 | $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"; |
---|
| 25 | $query = $this->db->query($sql); |
---|
| 26 | $dtns = $query->result_array(); |
---|
[761] | 27 | |
---|
[702] | 28 | // tinh doanh thu theo thang |
---|
[761] | 29 | $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"; |
---|
[702] | 30 | $query = $this->db->query($sql); |
---|
| 31 | $dtts = $query->result_array(); |
---|
[761] | 32 | |
---|
[745] | 33 | // tinh doanh thu trong ngay hien tai |
---|
[757] | 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 | } |
---|
[745] | 38 | $query = $this->db->query($sql); |
---|
| 39 | $dtng = $query->result_array(); |
---|
[702] | 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 |
---|
[761] | 47 | $sql = "SELECT *,count(us_id) as total FROM tbluser WHERE $from AND $to GROUP BY tbluser.province"; |
---|
[702] | 48 | $query = $this->db->query($sql); |
---|
| 49 | $tbts = $query->result_array(); |
---|
[745] | 50 | |
---|
| 51 | // tinh thue bao theo ngay |
---|
[761] | 52 | $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"; |
---|
| 53 | |
---|
[745] | 54 | $query = $this->db->query($sql); |
---|
[747] | 55 | $tbng = $query->result_array(); |
---|
[745] | 56 | |
---|
[702] | 57 | |
---|
[755] | 58 | $dem=1; |
---|
[702] | 59 | foreach ($provinces as $index2 => $province) { |
---|
[755] | 60 | $provinces[$index2] = array("stt" => ($dem), "province" => $province, "dtn" => 0, "dtt" => 0, "dtng" => 0, "tbn" => 0, "tbt" => 0, "tbng" => 0); |
---|
[702] | 61 | foreach ($dtns as $data) { |
---|
[755] | 62 | if ($index2 == $data['province']) { |
---|
[702] | 63 | $provinces[$index2]['dtn'] = $data['total']; |
---|
| 64 | } |
---|
[745] | 65 | } |
---|
| 66 | foreach ($dtng as $data) { |
---|
[755] | 67 | if ($index2 == $data['province']) { |
---|
[745] | 68 | $provinces[$index2]['dtng'] = $data['total']; |
---|
| 69 | } |
---|
[702] | 70 | } |
---|
| 71 | foreach ($dtts as $data) { |
---|
[755] | 72 | if ($index2 == $data['province']) { |
---|
[702] | 73 | $provinces[$index2]['dtt'] = $data['total']; |
---|
[745] | 74 | |
---|
[702] | 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | foreach ($tbns as $data) { |
---|
[755] | 79 | if ($index2 == $data['province']) { |
---|
[702] | 80 | $provinces[$index2]['tbn'] = $data['total']; |
---|
| 81 | } |
---|
| 82 | } |
---|
[745] | 83 | foreach ($tbng as $data) { |
---|
[755] | 84 | if ($index2 == $data['province']) { |
---|
[745] | 85 | $provinces[$index2]['tbng'] = $data['total']; |
---|
| 86 | } |
---|
| 87 | } |
---|
[702] | 88 | foreach ($tbts as $data) { |
---|
[755] | 89 | if ($index2 == $data['province']) { |
---|
[702] | 90 | $provinces[$index2]['tbt'] = $data['total']; |
---|
| 91 | } |
---|
| 92 | } |
---|
[755] | 93 | $dem++; |
---|
[702] | 94 | } |
---|
[728] | 95 | |
---|
[702] | 96 | return $provinces; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | function getPackages($input) { |
---|
| 100 | |
---|
| 101 | $provinces = lang('_PROVINCES_'); |
---|
| 102 | // tinh thue bao theo thang |
---|
[761] | 103 | $sql = "SELECT * FROM tbluser LEFT JOIN tblservicepackage ON tbluser.p_id=tblservicepackage.p_id WHERE tbluser.created_time >'" . $input['year'] . "-" . $input['month'] . "-" . $input['date'] . "' AND tbluser.created_time <'" . $input['to_year'] . "-" . $input['to_month'] . "-" . $input['to_date'] . "' AND tblservicepackage.p_period > 0 "; |
---|
[702] | 104 | $query = $this->db->query($sql); |
---|
| 105 | $users = $query->result_array(); |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | $sql = "SELECT * FROM tblservicepackage WHERE p_period>0"; |
---|
| 109 | $query = $this->db->query($sql); |
---|
| 110 | $packages = $query->result_array(); |
---|
| 111 | foreach ($packages as $index => $package) { |
---|
| 112 | $packages[$index]['users'] = array(); |
---|
| 113 | foreach ($users as $user) { |
---|
| 114 | if ($user['p_id'] == $package['p_id']) { |
---|
| 115 | $packages[$index]['users'][] = $user; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | foreach ($provinces as $index => $province) { |
---|
| 121 | $provinces[$index] = array("province" => $province); |
---|
| 122 | } |
---|
[755] | 123 | $dem=1; |
---|
[702] | 124 | foreach ($provinces as $index => $province) { |
---|
[755] | 125 | $provinces[$index]['stt'] = $dem; |
---|
[702] | 126 | $provinces[$index]['total'] = 0; |
---|
| 127 | $totaluser = 0; |
---|
| 128 | foreach ($packages as $index2 => $package) { |
---|
| 129 | $provinces[$index]['packages'][$package['p_name']]['users'] = array(); |
---|
| 130 | $provinces[$index]['packages'][$package['p_name']]['total'] = 0; |
---|
| 131 | $total = 0; |
---|
| 132 | foreach ($package['users'] as $user) { |
---|
[755] | 133 | if ($index == $user['province']) { |
---|
[702] | 134 | $total = $total + 1; |
---|
| 135 | $totaluser = $totaluser + 1; |
---|
| 136 | $provinces[$index]['total'] = $totaluser; |
---|
| 137 | $provinces[$index]['packages'][$package['p_name']]['total'] = $total; |
---|
| 138 | |
---|
| 139 | $provinces[$index]['packages'][$package['p_name']]['users'][] = $user; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
[755] | 143 | $dem++; |
---|
[702] | 144 | } |
---|
| 145 | foreach ($provinces as $index => $province) { |
---|
| 146 | foreach ($packages as $index2 => $package) { |
---|
| 147 | $provinces[$index]['packages'][$package['p_name']]['percent'] = 0; |
---|
| 148 | if ($provinces[$index]['packages'][$package['p_name']]['total'] > 0 && $province['total'] > 0) { |
---|
| 149 | $provinces[$index]['packages'][$package['p_name']]['percent'] = round($provinces[$index]['packages'][$package['p_name']]['total'] / $province['total'], 2) * 100; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
[728] | 153 | |
---|
| 154 | $data['packages'] = $packages; |
---|
| 155 | $data['provinces'] = $provinces; |
---|
[702] | 156 | return $data; |
---|
| 157 | } |
---|
[749] | 158 | public function exportProvince($input) |
---|
[761] | 159 | { |
---|
| 160 | //$input['date']=1; |
---|
[749] | 161 | //$input['month']=1; |
---|
| 162 | //$input['year']=2014; |
---|
[757] | 163 | //$input['to_month']=1; |
---|
| 164 | //$input['to_year']=2015; |
---|
| 165 | //$input['to_date']=15; |
---|
[760] | 166 | if (isset($input['to_month'])) |
---|
| 167 | { |
---|
| 168 | $date=$input['to_year'].$input['to_month'].$input['to_date']; |
---|
| 169 | } |
---|
| 170 | else { |
---|
| 171 | $date=date("Ymd"); |
---|
| 172 | } |
---|
[759] | 173 | $provinces=array(); |
---|
[749] | 174 | $data=$this->getProvinces($input); |
---|
[759] | 175 | foreach ($data as $index=>$province) |
---|
| 176 | { |
---|
[760] | 177 | $provinces[]=array("stt"=>$province['stt'],"ngay"=>$date,"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']); |
---|
[759] | 178 | } |
---|
| 179 | return $provinces; |
---|
[749] | 180 | } |
---|
| 181 | public function exportPackage($input) |
---|
| 182 | { |
---|
[761] | 183 | // $input['date']=1; |
---|
[757] | 184 | //$input['month']=1; |
---|
[761] | 185 | // $input['year']=2014; |
---|
| 186 | // $input['to_month']=1; |
---|
| 187 | // $input['to_year']=2015; |
---|
| 188 | // $input['to_date']=15; |
---|
[760] | 189 | if (isset($input['month'])) |
---|
| 190 | { |
---|
| 191 | $date=$input['year'].$input['month']; |
---|
| 192 | } |
---|
| 193 | else { |
---|
| 194 | $date=date("Ym"); |
---|
| 195 | } |
---|
[759] | 196 | $result=array(); |
---|
[749] | 197 | $data=$this->getPackages($input); |
---|
[760] | 198 | $data=$data['provinces']; |
---|
| 199 | foreach ($data as $index=>$province) |
---|
| 200 | { |
---|
| 201 | $package=array(); |
---|
| 202 | foreach ($province['packages'] as $index2=>$packages) |
---|
| 203 | { |
---|
| 204 | $package= array_merge($package,array("tong_so_".$index2=>$packages['total'],"ti_le_".$index2=>$packages['percent'])); |
---|
| 205 | } |
---|
| 206 | $tong=array("tong_so_tb"=>$province['total']); |
---|
| 207 | $province=array("stt"=>$province['stt'],"ngay"=>$date,"ma"=>$index,"tinh"=>$province['province']); |
---|
| 208 | $provinces[]= array_merge($province,$package,$tong); |
---|
| 209 | } |
---|
[759] | 210 | return $provinces; |
---|
[749] | 211 | } |
---|
[702] | 212 | |
---|
| 213 | } |
---|