sparqapi/api/application/controllers/Request_DataDump_Controller.php

251 lines
10 KiB
PHP
Executable File

<?php
/**
* Created by PhpStorm.
* User: kms
* Date: 11/13/17
* Time: 7:40 PM
*/
defined('BASEPATH') OR exit('No direct script access allowed');
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
/** @noinspection PhpIncludeInspection */
require_once APPPATH . '/libraries/REST_Controller.php';
include_once APPPATH.'/vendor/autoload.php';
require APPPATH.'/vendor/autoload.php';
/**
* This is an example of a few basic user interaction methods you could use
* all done with a hardcoded array
*
* @package CodeIgniter
* @subpackage Rest Server
* @category Controller
* @author
* @license MIT
* @link
*/
class Request_DataDump_Controller extends REST_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Data_Dump_Model','Data_Dump_Model');
}
### Request the Details For XL-Data-Dump.
##
public function saveDatadumpRequest_post(){
$records = $this->post();
if(!empty($records)){
$id = $this->Data_Dump_Model->saveRecords($records, DATADUMPREQUEST);
if ($id) {
$data['dataStatus'] = true;
$data['status'] = REST_Controller::HTTP_OK;
$data['records'] = $id;
$this->audit($id);
} else {
$data['dataStatus'] = false;
$data['status'] = REST_Controller::HTTP_NOT_MODIFIED;
$data['msg'] = 'Something went wrong..! Try Later...!';
}
}else{
$data['dataStatus'] = false;
$data['status'] = REST_Controller::HTTP_NOT_MODIFIED;
$data['msg'] = 'Parameter are Empty..! Try Later...!';
}
$this->response($data, REST_Controller::HTTP_OK);
}
public function getDatadumpRequestList_get(){
$result_data = $this->Data_Dump_Model->getDataDumpRequestList();
if (count($result_data)) {
$data['dataStatus'] = true;
$data['status'] = REST_Controller::HTTP_OK;
$data['records'] = $result_data;
$this->response($data, REST_Controller::HTTP_OK);
} else {
$data['dataStatus'] = false;
$data['status'] = REST_Controller::HTTP_NO_CONTENT;
$this->response($data, REST_Controller::HTTP_OK);
}
}
public function audit($id){
require_once APPPATH .'third_party/PHPExcel/Classes/PHPExcel/IOFactory.php';
// $this->load->library('PHPExcel/IOFactory');
log_message('ERROR','## inside audit Fns RequestID - '.$id);
## Gathering Data based on params.
$fields = array('*');
$where_condition_array = array('request_id' => $id);
$result_data = $this->Data_Dump_Model->selectCustomRecords($fields, $where_condition_array, DATADUMPREQUEST);
$limit = 10;
$offset = 0;
$requested_details = $this->Data_Dump_Model->getRequestForDataDump($result_data,$limit,$offset);
$G=0;
log_message('ERROR','## Request For XL-datadump Requested-data count - '.count($requested_details));
// print_r($requested_details);die();
if (!empty($requested_details)){
// Request_Data_Dump_Template
// if (file_exists(XLPATH."/tmp/testingDataDump.xls")) {
// unlink(XLPATH.'/tmp/testingDataDump.xls');
// }
$file = XLPATH.'/tmp/Request_Data_Dump_Template.xls';
// $file = APPPATH.'/docs/RequestDataDump.xls';
$reader= PHPExcel_IOFactory::createReaderForFile($file);
$XL = $reader->load($file);
for($i=0;$i < count($requested_details);$i++){
$processedJson = json_decode($requested_details[$i]['processed_json'],TRUE);
if($processedJson['formid'] == 18){
$XL->setActiveSheetIndex(0);
if($G==0){$G=3;}
$a = $G;$b = $G;$c = $G;$d = $G;
// print_r($processedJson['individuals']);
$individuals = $processedJson['individuals'];
if(!empty($individuals)){
for($j=0;$j<count($individuals);$j++){
// $XL->getActiveSheet()->setCellValue('A'.$a,$processedJson['pdid']);
$XL->getActiveSheet()->setCellValue('B'.$a,$individuals[$j]['applicant_name'])
->setCellValue('C'.$a,($individuals[$j]['is_applicant'] == 1) ? 'true' :'false')
->setCellValue('D'.$a,($individuals[$j]['is_person_met'] == 1) ? 'true' : 'false')
->setCellValue('E'.$a,!empty($individuals[$j]['age'])?$individuals[$j]['age']:"-")
->setCellValue('F'.$a,!empty($individuals[$j]['qualification_master'])?$individuals[$j]['qualification_master']:"-")
->setCellValue('G'.$a,!empty($individuals[$j]['course_name'])?$individuals[$j]['course_name']:"-");
$a++;
}}else{
$XL->getActiveSheet()->setCellValue('B'.$G,"-")
->setCellValue('C'.$G,"-")
->setCellValue('D'.$G,"-")
->setCellValue('E'.$G,"-")
->setCellValue('F'.$G,"-")
->setCellValue('G'.$G,"-");
}
$XL->getActiveSheet()->setCellValue('H'.$G,!empty($processedJson['is_kyc'])?$processedJson['is_kyc']:"-");
$companies = $processedJson['companies'];
if(!empty($companies)){
for($j=0;$j<count($companies);$j++){
$XL->getActiveSheet()->setCellValue('J'.$b,$companies[$j]['company_name'])
->setCellValue('K'.$b,($companies[$j]['is_company_applicant'] == 1) ? 'true' :'false')
->setCellValue('L'.$b,!empty($companies[$j]['business_entity_type_master'])?$companies[$j]['business_entity_type_master']:"-")
->setCellValue('M'.$b,!empty($companies[$j]['business_month'])?$companies[$j]['business_month']:"-")
->setCellValue('N'.$b,!empty($companies[$j]['business_years'])?$companies[$j]['business_years']:"-")
->setCellValue('O'.$b,!empty($companies[$j]['business_date_object'])?$companies[$j]['business_date_object']:"-");
$b++;
}}else{
$XL->getActiveSheet()->setCellValue('J'.$G,"-")
->setCellValue('K'.$G,"-")
->setCellValue('L'.$G,"-")
->setCellValue('M'.$G,"-")
->setCellValue('N'.$G,"-")
->setCellValue('O'.$G,"-");
}
$personsWithRelationships = $processedJson['persons_with_relationships'];
// print_r($personsWithRelationships);
if(!empty($personsWithRelationships)){
for($j=0;$j<count($personsWithRelationships);$j++){
$XL->getActiveSheet()->setCellValue('P'.$c,!empty($personsWithRelationships[$j]['pd_co_applicant_id_master'])?$personsWithRelationships[$j]['pd_co_applicant_id_master']:"-")
->setCellValue('Q'.$c,($personsWithRelationships[$j]['relationship_master'] == "") ? 'Not Related' : $personsWithRelationships[$j]['relationship_master'])
->setCellValue('R'.$c,!empty($personsWithRelationships[$j]['relation_to_master'])?$personsWithRelationships[$j]['relation_to_master']:"-");
$c++;
}}else{
$XL->getActiveSheet()->setCellValue('P'.$G,"-")
->setCellValue('Q'.$G,"-")
->setCellValue('R'.$G,"-");
}
$companyWithRelationships = $processedJson['company_with_relationships'];
if(!empty($companyWithRelationships)){
for($j=0;$j<count($companyWithRelationships);$j++){
$XL->getActiveSheet()->setCellValue('S'.$d,!empty($companyWithRelationships[$j]['applicant_name_master'])?$companyWithRelationships[$j]['applicant_name_master']:"-")
->setCellValue('T'.$d,!empty($companyWithRelationships[$j]['company_relationship_master'])?$companyWithRelationships[$j]['company_relationship_master']:"-")
->setCellValue('U'.$d,!empty($companyWithRelationships[$j]['company_name'])?$companyWithRelationships[$j]['company_name']:"-");
$d++;
}}else{
$XL->getActiveSheet()->setCellValue('S'.$G,"-")
->setCellValue('T'.$G,"-")
->setCellValue('U'.$G,"-");
}
$XL->getActiveSheet()->setCellValue('V'.$G,$processedJson['general_remark']);
if (count($individuals) >= count($companies) && count($individuals) >= count($companyWithRelationships)){
$max = count($individuals);}
if (count($companies) >= count($individuals) && count($companies) >= count($companyWithRelationships)){
$max = count($companies);}
if (count($companyWithRelationships) >= count($individuals) && count($companyWithRelationships) >= count($companies)){
$max = count($companyWithRelationships);}
if($max>0){
for($x=$G;$x<($G+$max);$x++){
$XL->getActiveSheet()->setCellValue('A'.$x,$processedJson['pdid']);
}
$G = ($G+$max);}
else{
$XL->getActiveSheet()->setCellValue('A'.$G,$processedJson['pdid']);
$G++;
}
}### General Info Ends Here.
}}
$objWriter = PHPExcel_IOFactory::createWriter($XL, 'Excel2007');
// $objWriter->save(APPPATH.'/docs/testingDataDump.xls');
$objWriter->save(XLPATH.'/tmp/'.$id.'.xls');
$update_fields = array('status' => "COMPLETED","file_path"=>XLPATH.'/tmp/'.$id.'.xls');
$this->Sales_PD_Model->updateRecords($update_fields,DATADUMPREQUEST,array('request_id' => $id));
}
public function downloadDatadumpRequest_get(){
log_message('ERROR','## Download Request For XL-datadump inside Fns');
## Sample Params :
$id = $this->get('request_id');
// echo XLPATH.'/tmp/'.$id.'.xls';
if($id){
log_message('ERROR','## Request For XL-datadump RequestID - '.$id);
## Gathering Data based on params.
$fields = array('*');
$where_condition_array = array('request_id' => $id);
$result_data = $this->Data_Dump_Model->selectCustomRecords($fields, $where_condition_array, DATADUMPREQUEST);
if($result_data[0]['status'] != 'PENDING'){
if($result_data[0]['file_path']){
//file Download here
$file = $result_data[0]['file_path'];
// check file exists
if (file_exists ( $file )) {
//force download
$this->load->helper('download');
$path = file_get_contents(XLPATH.'/tmp/SampleTest.xls');
// $path = file_get_contents(XLPATH.'/tmp/'.$id.'.xls');
$rename = "RequestID_".$id."_".date('Y-m-d').'.xls'; // new name for your file
force_download($rename, $path); // start download`
$data['dataStatus'] = true;
$data['status'] = REST_Controller::HTTP_OK;
$data['msg'] = 'File Downloaded Successfully';
$this->response($data, REST_Controller::HTTP_OK);
}else{
// Redirect to base url
$data['dataStatus'] = false;
$data['status'] = REST_Controller::HTTP_NO_CONTENT;
$data['msg'] = 'File Not Exists !';
$this->response($data,REST_Controller::HTTP_OK);
}
}else{
$data['dataStatus'] = false;
$data['status'] = REST_Controller::HTTP_NO_CONTENT;
$data['msg'] = 'Request ID ('.$id.') File Path Not available !';
$this->response($data,REST_Controller::HTTP_OK);
}
} else{
$data['dataStatus'] = false;
$data['status'] = REST_Controller::HTTP_NO_CONTENT;
$data['msg'] = 'Request ID ('.$id.') is in '.$result_data[0]['status'].' Status !';
$this->response($data,REST_Controller::HTTP_OK);
}
}else{
$data['dataStatus'] = false;
$data['status'] = REST_Controller::HTTP_NO_CONTENT;
$data['msg'] = 'Request ID ('.$id.') Not Founded in our DB';
$this->response($data,REST_Controller::HTTP_OK);
}
// print_r($result_data);
}
}// Class Closed Here.