load->model('Sales_PD_Model'); $this->external_path = $this->config->item('external_data_path'); $this->load->library('AWS_S3'); $this->load->library('pdfgenerator'); } public function loadSalesPDTemplate_post() { $records = $this->post('records'); $lender_id = $records['lender_id']; $product = $records['product_id']; $template = $this->Sales_PD_Model->selectCustomRecords(array('*'),array('lender_id' => $lender_id,'product_id' => $product,'isactive' => 1),SALESPDTEMPLATE); if(is_array($template) && count($template)) { $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['records'] = $template[0]; $this->response($data,REST_Controller::HTTP_OK); } else { $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = 'No Tempate Found!'; $this->response($data,REST_Controller::HTTP_OK); } } public function saveSalesPD_post() { $records = $this->post('records'); // $pd_data = $records['pd_json']; // $lender_id = $records['lender_id']; // $product_id = $records['product_id']; // $created_by = $records['created_by'];// //sales_pd_id, pd_json, lender_id, product_id, createdby, updatedby, createdon, updatedon, isactive $id = null; if(!$records['sales_pd_id']) { $id = $this->Sales_PD_Model->saveRecords($records,SALES_PD_DATA); } else { $id = $this->Sales_PD_Model->updateRecords($records,SALES_PD_DATA,array('sales_pd_id' => $records['sales_pd_id'])); } if($id) { $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['records'] = $id; $this->response($data,REST_Controller::HTTP_OK); } else { $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = 'Something went wrong! Try Later'; $this->response($data,REST_Controller::HTTP_OK); } } public function getSalesPDReport_get() { $sales_pd_id = $this->uri->segment(2); $columns = array('SALES_PD_DATA.*','ENTITY.short_name','PRODUCT.abbr'); $table = SALES_PD_DATA.' as SALES_PD_DATA'; $joins = array( array('table' => ENTITY. ' as ENTITY', 'condition' =>'SALES_PD_DATA.lender_id = ENTITY.entity_id', 'jointype' => 'INNER'), array('table' => PRODUCTS. ' as PRODUCT', 'condition' =>'SALES_PD_DATA.product_id = PRODUCT.product_id', 'jointype' => 'INNER') ); $where_condition_array = array('SALES_PD_DATA.sales_pd_id' => $sales_pd_id); $sales_pd_data = $this->Sales_PD_Model->getJoinRecords($columns,$table,$joins,$where_condition_array); //print_r($sales_pd_data);//die(); if($sales_pd_data) { $bucket_name = LENDER_BUCKET_NAME_PREFIX.$sales_pd_data[0]['lender_id']; $key = 'sales_pd/'.$sales_pd_id.'/'.$sales_pd_id.'.pdf'; if(!file_exists($this->external_path.'/sales_pd/'.$sales_pd_id)) { mkdir($this->external_path.'/sales_pd/'.$sales_pd_id,0761); } if(!$sales_pd_data[0]['is_pdf'] || $sales_pd_data[0]['is_edited']) { //echo 'FIRST'; //Get Data from DB and process //print_r(json_decode($sales_pd_data[0]['pd_json'],true));die(); $json_data = (json_decode($sales_pd_data[0]['pd_json'],true)); foreach ($json_data as $sec_key => $section) { $data[$section['section_name'] ] = $this->processSalesPDJSON($section); } //die(); print_r($data);die(); //End of JSON processing $view_name = $sales_pd_data[0]['short_name'].'_'.$sales_pd_data[0]['abbr']; $html_content = $this->load->view('sale_pd_templates/'.$view_name,$data,true); $pdf_doc = $this->pdfgenerator->generate($html_content, $filename='version', $stream=false, $paper = 'A4', $orientation = "portrait"); //End of PDF Gen using DOM PDF $output_file = $this->external_path ."/sales_pd/" . $sales_pd_id . "/temp.pdf"; $bytes = file_put_contents($output_file, $pdf_doc); //die(); $bucket_data = array('bucket_name'=>$bucket_name,'key'=>$key,'sourcefile'=>$this->external_path.'/sales_pd/'.$sales_pd_id.'/temp.pdf'); $s3result= $this->aws_s3->uploadFileToS3Bucket($bucket_data); if(is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200) { //echo 'SUCESS'; $this->Sales_PD_Model->updateRecords(array('is_pdf' => 1,'is_edited' => 0),SALES_PD_DATA,array('sales_pd_id' => $sales_pd_id)); $singed_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucket_name,$key,'30 minutes'); $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['records'] = $singed_uri; $this->response($data,REST_Controller::HTTP_OK); } } else { $singed_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucket_name,$key,'30 minutes'); $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['records'] = $singed_uri; $this->response($data,REST_Controller::HTTP_OK); } } } public function listSalesPD_get() { $lender_id = $this->uri->segment(2);//sales_pd_id, pd_json, lender_id, product_id, createdby, updatedby, createdon, updatedon, isactive, is_pdf, is_edited, length(pd_json) $columns = array('SALES_PD_DATA.sales_pd_id','SALES_PD_DATA.lender_id','SALES_PD_DATA.product_id','SALES_PD_DATA.is_pdf','SALES_PD_DATA.is_edited','ENTITY.short_name','PRODUCT.abbr'); $table = SALES_PD_DATA.' as SALES_PD_DATA'; $joins = array( array('table' => ENTITY. ' as ENTITY', 'condition' =>'SALES_PD_DATA.lender_id = ENTITY.entity_id', 'jointype' => 'INNER'), array('table' => PRODUCTS. ' as PRODUCT', 'condition' =>'SALES_PD_DATA.product_id = PRODUCT.product_id', 'jointype' => 'INNER') ); $where_condition_array = array(); if(is_numeric($lender_id)) { $where_condition_array = array('SALES_PD_DATA.lender_id' => $lender_id); } $order_by = 'SALES_PD_DATA.sales_pd_id'; $sales_pd_data = $this->Sales_PD_Model->getJoinRecords($columns,$table,$joins,$where_condition_array); if(count($sales_pd_data)) { $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['records'] = $sales_pd_data; $this->response($data,REST_Controller::HTTP_OK); } else { $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = 'Something went wrong! Try Later'; $this->response($data,REST_Controller::HTTP_OK); } } function processSalesPDJSON($section_array) { // echo '###'.$section_array['section_id'].'####'; if($section_array['section_id'] == 3) { //print_r($section_array); } if($section_array['section_id'] != 8) { //print_r($section_array); return $this->processSalesPDQuestions($section_array['questions']); } // die(); // $general_section_dta = array(); // foreach ($section_array as $sec_key => $section) // { //} } function processSalesPDQuestions($questions) { $temp_array = array(); foreach ($questions as $ques_key => $question) { if($question['is_repeatable'] == null && ($question['type'] == 1 || $question['type'] == 2 || $question['type'] == 3))//text,radio,SDD { $temp_array[ $question['question_key'] ] = $question['answer_value']; } else if($question['is_repeatable'] == 1) { //print_r($question['questions_group']);die(); foreach ($question['questions_group'] as $group_key => $group_value) { //print_r($group_value['']);die(); foreach ($group_value['questions'] as $q_key => $q_value) { //print_r($q_value);die(); $temp_array[ $group_key ][ $q_value['question_key'] ] = $q_value['answer_value']; } } } } //print_r($temp_array);die(); return $temp_array; } }