1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Report extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('Report_model');
$this->load->model('Company_model');
if(!$this->session->userdata('logged_in')) {
redirect(base_url('Login'));
}
}
public function generate() {
$template['page'] = 'Report/generate';
$template['menu'] = "Report Management";
$template['sub_menu'] = "Report Generate";
$template['page_desc'] = "Report Generation Page";
$template['page_title'] = "Report Management";
$template['company_data'] = $this->Company_model->getCompany('','0,1');
$template['broker_data'] = '';
if($this->session->userdata['user_type'] != 1){
$this->load->model('Broker_model');
$company_id = $this->session->userdata('id');
$template['broker_data'] = $this->Broker_model->getBroker('','0,1',$company_id);
}
$this->load->view('template',$template);
}
public function rGenerate(){
$return_arr = array('status'=>'0');
if(!isset($_POST) || empty($_POST) || !isset($_POST['fields']) || empty($_POST['fields']) ||
!isset($_POST['action']) || empty($_POST['action'])){
echo json_encode($return_arr);exit;
}
$action = $_POST['action'];
$fields = str_replace('+',' ',trim($_POST['fields'],','));
$where_cond = array();
if(isset($_POST['where_cond']) && !empty($_POST['where_cond'])){
parse_str($_POST['where_cond'], $where_cond);
}
$report_data = $this->Report_model->get_report($fields,$where_cond);
if(!empty($report_data) && !is_array($report_data)){
$return_arr['status'] = $report_data;
echo json_encode($return_arr);exit;
}
if(!empty($report_data)){
if($action == 'view'){
$return_arr['status'] = 1;
$return_arr['report_data'] = $report_data;
}
if($action == 'export'){
$return_arr['status'] = 1;
$this->exportExcel($report_data);
}
}
echo json_encode($return_arr);exit;
}
function exportExcel($reportData = array()){
if(empty($reportData)){
return 0;
}
$this->load->helper('csv');
$fileName = 'reportExport_'.time().'.csv';
$dataRow = array();
$headerFlg = 0;
foreach ($reportData AS $data) {
$row = array();
if($headerFlg == 0){
foreach($data AS $index => $value){
$row[] = $index;
}
$dataRow[] = $row;
$row = array();
$headerFlg = 1;
}
foreach ($data AS $rowVal) {
$row[] = $rowVal;
}
$dataRow[] = $row;
}
if(empty($dataRow)){
return 0;
}
$this->session->set_userdata('file_name',$fileName);
$this->session->set_userdata('report_data',$dataRow);
return 1;
}
function downloadCSV(){
$dataRow = $this->session->userdata('report_data');
$fileName = $this->session->userdata('file_name');
$this->session->set_userdata('file_name','');
$this->session->set_userdata('report_data','');
if(empty($dataRow) || empty($fileName)){
return;
}
if($this->session->userdata['user_type'] != 1){
$company_id = $this->session->userdata['id'];
$this->db->query("UPDATE `company_payment_details`
SET `report_count`=report_count+1,
`outstanding_amount`=outstanding_amount+report_charge
WHERE `company_id`=".$company_id);
}
//Download CSV\\
$temp_memory = fopen('php://memory', 'w');
foreach ($dataRow as $line) {
fputcsv($temp_memory, $line, ',');
}
fseek($temp_memory, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $fileName . '";');
fpassthru($temp_memory);
}
}
?>