load->model('SMC_Credit_PD_Model'); $this->load->model('PD_Model'); $this->external_path = $this->config->item('external_data_path'); $this->load->library('AWS_S3'); $this->load->library('pdfgenerator'); $this->load->helper('satellite_image'); $this->load->library('phpmailer_library'); $this->load->helper('cet_creditpd'); } public function loadSalesPDTemplate_post() { $records = $this->post('records'); $lender_id = $records['lender_id']; $product = $records['product_id']; $sales_pd_type = $records['sales_pd_type']; $template = $this->SMC_Credit_PD_Model->selectCustomRecords(array('*'),array('lender_id' => $lender_id,'product_id' => $product,'sales_pd_type' => $sales_pd_type,'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 saveSalesPDOld_post() // { // $records = $this->post('records'); // $applicant_name = ''; // $applicant_id = ''; // $officer = ''; // //satellite_image::getSatelliteImage(22,2);//die(); // foreach (json_decode($records['pd_json'],true) as $key => $value) // { // //print_r($value);die(); // if($value['section_id'] == 1) // { // foreach ($value['questions'] as $g_key => $g_value) // { // if($g_value['question_key'] == 'application_id') // { // $applicant_id = $g_value['answer_value'] ? $g_value['answer_value'] : 'Not Provided'; // } // if($records['product_id'] == 4) // { // if($g_value['question_key'] == 'company_name') // { // $applicant_name = $g_value['answer_value']; // } // } // else // { // if($g_value['question_key'] == 'person_met_during_visit') // { // $applicant_name = $g_value['answer_value']; // } // } // } // } // if($value['section_id'] == 8) // { // foreach ($value['questions'] as $g_key => $g_value) // { // if($g_value['question_key'] == 'name_of_sales_team_member') // { // $officer = $g_value['answer_value']; // } // } // } // } // $records['applicant_id'] = $applicant_id; // $records['applicant_name'] = $applicant_name; // $records['officer_name'] = $officer; // $id = null; // if(!$records['smc_credit_pd']) // { // //generate sales pd serial no // $count = $this->SMC_Credit_PD_Model->selectCustomRecords(array('count(*) as count'),array('lender_id' => $records['lender_id'],'product_id' => $records['product_id']),SMC_CREDIT_PD); // $lender_short_name = $this->SMC_Credit_PD_Model->selectCustomRecords(array('short_name'),array('entity_id' => $records['lender_id']),ENTITY); // $product_short_name = $this->SMC_Credit_PD_Model->selectCustomRecords(array('abbr'),array('product_id' => $records['product_id']),PRODUCTS); // //print_r($product_short_name); // $salepd_serial_no = count($lender_short_name)? $lender_short_name[0]['short_name']:'UNKNOWN'; // $salepd_serial_no .= count($product_short_name)? '-'.$product_short_name[0]['abbr']:'-UNKNOWN'; // $salepd_serial_no .= '-'.(++$count[0]['count']); // //echo $salepd_serial_no; // //end generate sales pd serial no // // echo $salepd_serial_no; // // die(); // $records['sales_pd_sno'] = $salepd_serial_no; // $id = $this->SMC_Credit_PD_Model->saveRecords($records,SMC_CREDIT_PD); // satellite_image::getSatelliteImage($id,2); // } // else // { // $id = $this->SMC_Credit_PD_Model->updateRecords($records,SMC_CREDIT_PD,array('smc_credit_pd' => $records['smc_credit_pd'])); // } // 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); // } // } function prepareSalesPDReport($smc_credit_pd,$api = true) { log_message('ERROR','####PREPARE SALESPD REPORT CALLED'.$smc_credit_pd); $columns = array('SMC_CREDIT_PD.*','ENTITY.short_name','PRODUCT.abbr'); $table = SMC_CREDIT_PD.' as SMC_CREDIT_PD'; $joins = array( array('table' => ENTITY. ' as ENTITY', 'condition' =>'SMC_CREDIT_PD.lender_id = ENTITY.entity_id', 'jointype' => 'INNER'), array('table' => PRODUCTS. ' as PRODUCT', 'condition' =>'SMC_CREDIT_PD.product_id = PRODUCT.product_id', 'jointype' => 'INNER') ); $where_condition_array = array('SMC_CREDIT_PD.smc_credit_pd' => $smc_credit_pd); $SMC_CREDIT_PD = $this->SMC_Credit_PD_Model->getJoinRecords($columns,$table,$joins,$where_condition_array); if($SMC_CREDIT_PD) { ini_set('max_execution_time', 0); $bucket_name = LENDER_BUCKET_NAME_PREFIX.$SMC_CREDIT_PD[0]['lender_id']; // 1st Type Key name. (name,abbr and date). $pdf_name = strtolower(trim($SMC_CREDIT_PD[0]['applicant_name'])); $pdf_name .= '-'.$SMC_CREDIT_PD[0]['abbr']; $pdf_name .= '-'.(date('dmY',strtotime($SMC_CREDIT_PD[0]['completed_date']))); // 2nd Type Key name. (name,type,abbr and date). $new_pdf_name = strtolower(trim($SMC_CREDIT_PD[0]['applicant_name'])); $new_pdf_name .= '-'.$SMC_CREDIT_PD[0]['abbr']; $new_pdf_name .= 'PD_Report-'.(date('dmY',strtotime($SMC_CREDIT_PD[0]['completed_date']))); // $this->PD_Model->updateRecords(array('pd_report_filename' => $pdf_name),SMC_CREDIT_PD,array('smc_credit_pd' => $smc_credit_pd)); //$pdf_name = str_replace(' ', '_', $pdf_name); $key = 'sales_pd/'.$smc_credit_pd.'/'.$pdf_name.'.pdf'; $new_key = 'sales_pd/'.$smc_credit_pd.'/'.$new_pdf_name.'.pdf'; if(!file_exists($this->external_path.'/sales_pd/'.$smc_credit_pd)) { mkdir($this->external_path.'/sales_pd/'.$smc_credit_pd,0777); } if(!$SMC_CREDIT_PD[0]['is_pdf'] || $SMC_CREDIT_PD[0]['is_edited']) { $all_section_data = $this->SMC_Credit_PD_Model->selectCustomRecords(array('*'),array('smc_credit_pd' => $smc_credit_pd,'isactive' => 1),SMC_CREDIT_PD_SECTION_DATA); // print_r($this->db->last_query());die(); $view_data['master'] = $SMC_CREDIT_PD; foreach ($all_section_data as $sec_key => $section) { //print_r($section);die(); $view_data['section'.$section['section_id'] ] = $this->processSalesPDJSON($section); } //print_r($view_data['section1']);die(); $view_data['section9'] = $this->SMC_Credit_PD_Model->selectCustomRecords(array('*'),array('fk_smc_credit_pd' => $smc_credit_pd,'img_name !=' => 'satellite','isactive' => 1),SALEPD_DOCS); $view_data['satellite_image'] = $this->SMC_Credit_PD_Model->selectCustomRecords(array('*'),array('fk_smc_credit_pd' => $smc_credit_pd,'img_name' => 'satellite','isactive' => 1),SALEPD_DOCS); $view_data['product_id'] = $SMC_CREDIT_PD[0]['product_id']; $view_data['geo_location'] = $SMC_CREDIT_PD[0]['geo_location']; //print_r($this->db->last_query());die(); //$view_name = $SMC_CREDIT_PD[0]['short_name'].'_'.$SMC_CREDIT_PD[0]['abbr']; $view_name = $SMC_CREDIT_PD[0]['short_name']; $html_content = $this->load->view('sale_pd_templates/'.$view_name,$view_data,true); //echo $html_content;die(); $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/" . $smc_credit_pd . "/temp.pdf"; $bytes = file_put_contents($output_file, $pdf_doc); // echo $bytes; // die(); $bucket_data = array('bucket_name'=>$bucket_name,'key'=>$new_key,'sourcefile'=>$this->external_path.'/sales_pd/'.$smc_credit_pd.'/temp.pdf'); log_message('ERROR','####SALES PD REPORT UPLOAD TO S3'.$smc_credit_pd); $s3result= $this->aws_s3->uploadFileToS3Bucket($bucket_data); if(is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200) { $this->SMC_Credit_PD_Model->updateRecords(array('is_pdf' => 1, 'is_edited' => 0),SMC_CREDIT_PD,array('smc_credit_pd' => $smc_credit_pd)); // ### Automation CET Data Push Credit PD - 2/2/23 // sleep(10); // $result = $this->dataSynchronization($smc_credit_pd); // log_message('ERROR', '#### Automation CET Data Push Credit PD ('.$smc_credit_pd.') status '.$result->status.' Data Transfered'); // if($result->status == 200){ // log_message('ERROR', '#### Credit PD ('.$smc_credit_pd.') status '.$result->status." Data Transfered"); // $update_fields2 = array('is_the_data_transfer_cet' => 1); // $this->SMC_Credit_PD_Model->updateRecords($update_fields2,PDTRIGGER,array('pd_id' => $smc_credit_pd)); // }else{ // log_message('ERROR', '#### Credit PD ('.$smc_credit_pd.') status '.$result->status); // } if($api) { // $singed_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucket_name,$key,'30 minutes'); $_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucket_name,$key,'30 minutes'); $headers = @get_headers($_uri); if($headers && strpos( $headers[0], '200')) { $singed_uri = $_uri; }else { $singed_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucket_name,$new_key,'30 minutes'); } $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['records'] = $singed_uri; $this->response($data,REST_Controller::HTTP_OK); } } } else { if($api) { $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 getSalesPDReport_get() { $smc_credit_pd = $this->uri->segment(2); $this->prepareSalesPDReport($smc_credit_pd); } function uploadCreditPDDataToCETApp_get() { $smc_credit_pd = $this->uri->segment(2); if($smc_credit_pd){ log_message('ERROR', '#### upload SMC-Credit PDData To CETApp Function Called'); $columns = array('PDTRIGGER.pd_id'); $table = PDTRIGGER . ' as PDTRIGGER'; $where_arr = array('PDTRIGGER.is_the_data_transfer_cet'=>0,'PDTRIGGER.pd_status' => COMPLETED,'PDTRIGGER.pd_id'=>$smc_credit_pd); $details = $this->SMC_Credit_PD_Model->selectCustomRecords($columns, $where_arr, $table); if(!empty($details)){ $response = $this->dataSynchronization($smc_credit_pd); if($response){ log_message('ERROR', '#### Credit PD ('.$smc_credit_pd.') status '.$response." Data Transfered"); $update_fields2 = array('is_the_data_transfer_cet' => 1); $this->SMC_Credit_PD_Model->updateRecords($update_fields2,PDTRIGGER,array('pd_id' => $smc_credit_pd)); $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['msg'] = 'Data Shared Successfully'; $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); } }else{ log_message('ERROR', $smc_credit_pd.' Data already uploaded'); $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = 'Data already uploaded! Try Later'; $this->response($data, REST_Controller::HTTP_OK); } }else{ $msg = 'Can`t Upload ID Not Founded ! Try Later'; $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = $msg; log_message('ERROR', $msg); $this->response($data, REST_Controller::HTTP_OK); } } public function transferCreditPDToCETApp_get() { $from_date = $this->get('from_date'); $to_date = $this->get('to_date'); if(($from_date != "" || $from_date != null) && ($to_date != "" || $to_date != null)){ $newDate1 = date("Y-m-d", strtotime($from_date)); $newDate2 = date("Y-m-d", strtotime($to_date)); $smc_lender_id = ENVIRONMENT == 'production' ? 7 : 35; //SMC lender id MWISE in live and demo $msg = " SMC-Credit PDData To CETApp Date Given Format => " . $from_date ." to ".$to_date; // $where = 'DATE(PDTRIGGER.completed_on) BETWEEN ' ."'$newDate1'". ' AND ' ."'$newDate2'"; $columns = array('PDTRIGGER.pd_id', 'PDTRIGGER.pd_status'); $table = PDTRIGGER . ' as PDTRIGGER'; $where = '(SELECT MAX(DATE(logmaster.createdon)) FROM h_common_masters as logmaster WHERE logmaster.new_value = "COMPLETED" AND PDTRIGGER.pd_id = logmaster.primary_key) BETWEEN ' ."'$newDate1'". ' AND ' ."'$newDate2'"; $where_arr = array('PDTRIGGER.is_the_data_transfer_cet'=>0,'PDTRIGGER.fk_lender_id' => $smc_lender_id,'PDTRIGGER.pd_status' => COMPLETED, $where => null); $details = $this->SMC_Credit_PD_Model->selectCustomRecords($columns, $where_arr, $table); log_message('ERROR', $msg." Changed ".$newDate1." to ".$newDate2." Total Records => " . count($details)); if(!empty($details)){ $flag = 0;$ids=array(); for ($i = 0; $i < count($details); $i++) { log_message('ERROR', "smcCreditPD inx = $i ".$smc_credit_pd); $smc_credit_pd = $details[$i]['pd_id']; // echo $smc_credit_pd; $result = $this->dataSynchronization($smc_credit_pd); // $flag = $result == true ? $flag+1 : $flag; if($result->status == 200){ log_message('ERROR', '#### Credit PD ('.$smc_credit_pd.') status '.$result->status." Data Transfered"); $flag = $flag+1; array_push($ids, $smc_credit_pd); //update here. $update_fields2 = array('is_the_data_transfer_cet' => 1); $this->SMC_Credit_PD_Model->updateRecords($update_fields2,PDTRIGGER,array('pd_id' => $smc_credit_pd)); } } $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['msg'] = implode(', ', $ids).' ( '.$flag.' ) Data Shared Successfully'; $this->response($data, REST_Controller::HTTP_OK); }else{ $msg = 'There is no data for Given Date ! Try Later'; $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = $msg; log_message('ERROR', $msg); $this->response($data, REST_Controller::HTTP_OK); } } else { $msg = 'Can`t Upload Date Not Founded ! Try Later'; $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = $msg; log_message('ERROR', $msg); $this->response($data, REST_Controller::HTTP_OK); } } function dataSynchronization($smc_credit_pd) { $this->load->helper('smc_creditpd'); $mater_details = $this->PD_Model->getPDMasterDetails($smc_credit_pd); $pd_section_data = $this->SMC_Credit_PD_Model->getSMCTemplateForPD($smc_credit_pd,$mater_details[0]['fk_product_id'],null,null,'REPORT'); log_message('ERROR', '#### Credit PD ('.$smc_credit_pd.') There are ' . count($pd_section_data).' Forms '); if (!empty($pd_section_data)) { $forms_data_to_be_get_direct_from_excel_sheet = array(3, 9, 10, 15); $companylist = $this->getCompanyName($pd_section_data[0]); if (count($pd_section_data) > 0) { $common_fields = array('pdid'=>$mater_details[0]['pd_id'], 'product'=>$mater_details[0]['product_abbr'], 'caseno'=>$mater_details[0]['pd_sno']); // foreach ($pd_section_data as $key => $value) { // if (isset($value['json']) && $value['json'] != '') { // $filter_form_id[$key] = $value['fk_form_id']; // $CETAPP_creditPD_data[$value['form_name']] = json_decode($value['json'],true); // } else { // if (in_array($value['fk_form_id'], $forms_data_to_be_get_direct_from_excel_sheet)) { // $data_from_excel = $this->loadDataFromExcelBusiProcess($mater_details, $value['fk_form_id']); // $filter_form_id[$key] = $value['fk_form_id']; // $CETAPP_creditPD_data[$value['form_name']] = json_decode($data_from_excel,true); // } // } // } foreach ($pd_section_data as $key => $value) { if (isset($value['json']) && $value['json'] != '') { $value['json'] = smc_creditpd::getSMCCreditPDReportContent($value,$smc_credit_pd,$companylist); $filter_form_id[$key] = $value['fk_form_id']; // $view_data['pd_section_data'][$value['fk_form_id']] = $value['json']; $CETAPP_creditPD_data[$value['form_name']] = $value['json']; } else { if (in_array($value['fk_form_id'], $forms_data_to_be_get_direct_from_excel_sheet)) { $data_from_excel = $this->loadDataFromExcelBusiProcess($mater_details, $value['fk_form_id']); //print_r($data_from_excel);die(); // $view_data['pd_section_data'][$value['fk_form_id']] = $data_from_excel; $CETAPP_creditPD_data[$value['form_name']] = $data_from_excel; $filter_form_id[$key] = $value['fk_form_id']; } } } if ($mater_details[0]['product_abbr'] == 'LFMP' && (array_key_exists("key_stakeholders",$pd_section_data[5])) ) { $LFMP_KeyStackholders = $pd_section_data[5]['key_stakeholders']; $temp_arr = []; for ($i = 0; $i < count($LFMP_KeyStackholders); $i++) { $value = $LFMP_KeyStackholders[$i]; if (isset($value["stakeholder_name0"])) { $temp_arr[] = array( "company_name" => $value["company_name"], "stakeholder_name" => $value["stakeholder_name0"], "share_holding" => $value["share_holding0"], "vintage_in_current_biz" => $value["vintage_in_current_biz0"], "total_work_exp" => $value["total_work_exp0"], "started_biz" => $value["started_biz0"], "previous_experience_details" => (isset($value['previous_experience_details0']) ? $value['previous_experience_details0'] : '') ); } if (isset($value["stakeholder_name1"])) { $temp_arr[] = array( "company_name" => $value["company_name"], "stakeholder_name" => $value["stakeholder_name1"], "share_holding" => $value["share_holding1"], "vintage_in_current_biz" => $value["vintage_in_current_biz1"], "total_work_exp" => $value["total_work_exp1"], "started_biz" => $value["started_biz1"], "previous_experience_details" => (isset($value['previous_experience_details1']) ? $value['previous_experience_details1'] : '') ); } if (isset($value["stakeholder_name2"])) { $temp_arr[] = array( "company_name" => $value["company_name"], "stakeholder_name" => $value["stakeholder_name2"], "share_holding" => $value["share_holding2"], "vintage_in_current_biz" => $value["vintage_in_current_biz2"], "total_work_exp" => $value["total_work_exp2"], "started_biz" => $value["started_biz2"], "previous_experience_details" => (isset($value['previous_experience_details2']) ? $value['previous_experience_details2'] : '') ); } $CETAPP_creditPD_data['Key Stakeholders in Business']['key_stakeholders'] = $temp_arr; } } // ### LFMP key_stakeholders ends // print_r($filter_form_id);die(); $officer_id = isset($mater_details[0]['fk_updatedby']) ? $mater_details[0]['fk_updatedby'] : ''; $visit_date = isset($mater_details[0]['pd_visit_date']) ? $mater_details[0]['pd_visit_date'] : ''; if (!empty($officer_id)) { $officer_details = $this->SMC_Credit_PD_Model->selectCustomRecords(array('userid as officer_id', 'email', 'mobile_no', 'concat(first_name," ",last_name) as officer'), array('userid' => $officer_id), USERPROFILE); if (!empty($officer_details)) { foreach ($officer_details as $key => $value) { $CETAPP_creditPD_data['Officer Details'] = $value; $CETAPP_creditPD_data['Officer Details']['pd_visit_date'] = $visit_date; } } } $result = cet_creditpd::pushwithcetcreditpdapi($common_fields, $CETAPP_creditPD_data); if($result == true){ $photograph_data = $this->SMC_Credit_PD_Model->selectCustomRecords(array('doc_id', 'fk_smc_credit_pd_id','img','img_name','isactive'),array('fk_smc_credit_pd_id' => $smc_credit_pd,'isactive' => 1,'img_name!=' => 'satellite'),SMC_CREDIT_PD_DOCS); $satellite_image = $this->SMC_Credit_PD_Model->selectCustomRecords(array('doc_id', 'fk_smc_credit_pd_id','img','img_name','isactive'),array('fk_smc_credit_pd_id' => $smc_credit_pd,'isactive' => 1,'img_name=' => 'satellite'),SMC_CREDIT_PD_DOCS); log_message('ERROR', '#### Credit PD ('.$smc_credit_pd.') There are ' . count($photograph_data).' Images'); if(count($photograph_data)>0){ for ($i = 0; $i < count($photograph_data); $i++){ $img_fields = $common_fields; $img_fields['img_name'] = $photograph_data[$i]['img_name']; $img_split = explode( ',', $photograph_data[$i]['img']); $img_fields['img'] = $img_split[1]; $resultimages = cet_creditpd::pushwithcetphotographapi($img_fields,$i); }} if(!empty($satellite_image)){ log_message('ERROR', '#### Credit PD ('.$smc_credit_pd.') satellite Available'); $stt_fields = $common_fields; $stt_fields['img_name'] = $satellite_image[0]['img_name']; $stt_fields['img'] = $satellite_image[0]['img']; $resultstatellite = cet_creditpd::pushwithcetphotographapi($stt_fields,0); } // print_r($CETAPP_creditPD_data); // $path = $this->external_path ."/pd_reports/" . $mater_details[0]['pd_id'] . "/new-file-5-".$mater_details[0]['pd_id'].".json"; // $json = json_decode($view_data['pd_section_data'],true); // $fp = fopen($path, 'w'); // fwrite($fp, $jsonString); // fclose($fp); // if(file_put_contents($path, $output_data)) // { // $message = "