sparqapi/api/application/helpers/readexceldata_helper.php

564 lines
22 KiB
PHP

<?php
class readexceldata
{
private static $current_section_id = null;
private static $current_excel_file_id = null;
public static function getExcelSectionData($file_path_withname,$section_id = null,$pd_id = null,$excel_file_id = null)
{
$CI =&get_instance();
$CI->load->library('myspreadsheet');
SELF::$current_section_id = $section_id;
SELF::$current_excel_file_id = $excel_file_id;
// echo is_numeric($section_id);
// var_dump($section_id);die();
//echo $file_path_withname;echo file_exists($file_path_withname); die();
// $file_path_withname = 'C:/xampp/htdocs/ssa_external_data/testing/tmp/mySMCPD.xlsx';
// $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
$spreadsheet_io_fact = $CI->myspreadsheet->getIOFactory($file_path_withname);
//$curr_spreadshett = $spreadsheet_io_fact::load($file_path_withname);
//echo $spreadsheet_io_fact->getSheetCount();
//$spreadsheet_io_fact->setReadDataOnly(true);
$spreadsheet_io_fact->setActiveSheetIndex(0);
$sheetData = $spreadsheet_io_fact->getActiveSheet()->toArray(null, true, false, true);
//print_r($sheetData);die();
return
SELF::seperateSectionData($section_id,$sheetData,$pd_id,$file_path_withname);
// print_r(SELF::seperateSectionData($section_id,$sheetData,$pd_id));die();
// $CI->load->model('PD_Model');
//return ( $CI->PD_Model->selectCustomRecords(array('pd_co_applicant_id','applicant_name'),array('fk_pd_id' => $pd_id),PDAPPLICANTSDETAILS));
// return array('sam'=>'dsnlskdnm');
// print_r(array('sam'=>'dsnlskdnm'));
}
static function seperateSectionData($section_id,$sheetData,$pd_id,$file)
{
// echo 's-worked';die();
$CI =&get_instance();
$CI->load->model('PD_Model');
$where_condition_array = array('isactive' => 1,'section_id' => $section_id);
//if($section_id == null){ $where_condition_array = array('isactive' => 1,'section_id in ' => '(null,1)'); }
$section_ref_data = $CI->PD_Model->selectCustomRecords(array('*'),$where_condition_array,EXCEL_COLUMN_REF_NAME);
$return_data = array();
// $section_id = is_null($section_id) ? '' : $section_id;
if($section_id)
{
$return_data = SELF::getIndividualSectionData($sheetData,$section_ref_data);
if($section_id == 2 || $section_id == 8) //get pd initialted address as custom function
{
if($section_id == 8)
{
$address_section = $CI->PD_Model->selectCustomRecords(array('*'),array('isactive' => 1,'fk_form_id' => 2,'fk_pd_id' => $pd_id),SMC_CREDIT_PD_SECTION_DATA);
// print_r($address_section);die();
if(is_array($address_section) && count($address_section))
{
$jsn = json_decode($address_section[0]['json'],true);
// echo strtolower($jsn['is_pd_done_on_initiated_address']);die();
if(strtolower($jsn['is_pd_done_on_initiated_address']) == 'no')
{
$return_data['initiated_address'] = $jsn['alternative_address'];
}
else
{
$return_data['initiated_address'] = $jsn['initiated_address'];
}
}
}
else if($section_id == 2 || (!isset($return_data['initiated_address'])))
{
$pd_address = $CI->PD_Model->getPDAddress($pd_id);
if(count($pd_address)){ $return_data['initiated_address'] = $pd_address[0]['addressline1'].', '.$pd_address[0]['city_name'].', '.$pd_address[0]['state_name'].' - '.$pd_address[0]['pincode']; }
}
}
// if($section_id == 1)
// {
//echo "line at 91";die();
// if(!empty($return_data['borrower_details'])){
// SELF::change_key($return_data['borrower_details'],'numbers_of_years_business_running','numbers_of_years_applicant_in_business');
// for($i=0;$i<count($return_data['borrower_details']);$i++){
// $entity_type = $return_data['borrower_details'][$i]['entity_type'];
// if($entity_type != "Individual"){
// $temp = $return_data['borrower_details'][$i]['numbers_of_years_business_running'];
// }else{
// $temp = $return_data['borrower_details'][$i]['numbers_of_years_borrower_in_this_business'];
// }
// $return_data['borrower_details'][$i]['numbers_of_years_business_running'] = $temp;
// unset($return_data['borrower_details'][$i]['numbers_of_years_borrower_in_this_business']);
// }
// }
// }
if($section_id == 3) //get sub product as custom func tion
{
$pd_master = $CI->PD_Model->getPDMasterDetails($pd_id);
if(count($pd_master)){ $return_data['sub_product'] = $pd_master[0]['fk_subproduct_id']; }
}
else if($section_id == 5) //handle multiple stake holders data
{
$re_arranged_stake_holders_details = array();
if(isset($return_data['key_stakeholders']))
{ $pd_master = $CI->PD_Model->getPDMasterDetails($pd_id);
$product_abbr = $pd_master[0]['product_abbr'];
if($product_abbr == "LFMP"){ // ### LFMP Started Here.
$LFMP_company_data = SELF::getCompanyListForSMCList($file);
$companycount = (count($LFMP_company_data)>0) ? count($LFMP_company_data) : 1;
for($i = 0;$i < $companycount; $i++)
{
$j =0 ; //### temp inx (bcoz maran asked)
foreach ($return_data['key_stakeholders'] as $key => $value)
{
$dynamic_share_key = ('share_holding#'.($i + 1));
$dynamic_vintage_key = ('vintage_in_current_biz#'.($i + 1));
$dynamic_work_exp_key = ('total_work_exp#'.($i + 1));
if((array_key_exists( $dynamic_share_key, $value)) && (array_key_exists( $dynamic_vintage_key, $value)) && (array_key_exists( $dynamic_work_exp_key, $value)))
{
$temp['company_id'] = isset($LFMP_company_data[$i]['company_id'])?$LFMP_company_data[$i]['company_id']:0;
$temp['company_name'] = isset($LFMP_company_data[$i]['borrower_name'])?$LFMP_company_data[$i]['borrower_name']:"";
$temp['started_biz'] = "NO";
$temp['stakeholder_name'.$j] = $value['stakeholder_name'] != "" ? $value['stakeholder_name'] : NULL;
$temp['share_holding'.$j] = $value[$dynamic_share_key] != "" ? $value[$dynamic_share_key] : NULL;
$temp['vintage_in_current_biz'.$j] = $value[$dynamic_vintage_key] != "" ? $value[$dynamic_vintage_key] : 0;
$temp['total_work_exp'.$j] = $value[ $dynamic_work_exp_key ] != "" ? $value[ $dynamic_work_exp_key ] : 0;
$temp['stakeholder_length'] = ($j+1);
$re_arranged_stake_holders_details[$i] = $temp;
}
$j++;
}
}
$return_data['company_details'] = $LFMP_company_data;
}//###LFMP Flow Closed Here
else{
foreach ($return_data['key_stakeholders'] as $key => $value)
{
for($i = 0;$i <= 2; $i++)
{
$dynamic_share_key = ('share_holding#'.($i + 1));
$dynamic_vintage_key = ('vintage_in_current_biz#'.($i + 1));
$dynamic_work_exp_key = ('total_work_exp#'.($i + 1));
if((array_key_exists( $dynamic_share_key, $value)) && (array_key_exists( $dynamic_vintage_key, $value)) && (array_key_exists( $dynamic_work_exp_key, $value)))
{
$temp['stakeholder_name'] = $value['stakeholder_name'];
$temp['share_holding'] = $value[ $dynamic_share_key ];
$temp['company_id'] = ($i + 1);
$temp['vintage_in_current_biz'] = $value[$dynamic_vintage_key] != "" ? $value[$dynamic_vintage_key] : 0;
$temp['total_work_exp'] = $value[$dynamic_work_exp_key] != "" ? $value[ $dynamic_work_exp_key ] : 0;
$re_arranged_stake_holders_details[] = $temp;
}
}// ### default for loop closed here
}// ### foreach closed here
}//### normal flow closed here
}//### isset if closed here
$return_data['key_stakeholders'] = $re_arranged_stake_holders_details;
}
}
else
{
$return_data = SELF::getTriggerData($sheetData,$section_ref_data);
//store address details in temp table SMC_TEMP_ADDRESS
if(SELF::$current_excel_file_id)
{
if(array_key_exists('address_details', $return_data))
{
foreach ($return_data['address_details'] as $key => $address)
{
$display = $address['address'].', '.$address['city'].', '.$address['state'].', '.$address['pincode'];
$state = $CI->PD_Model->selectCustomRecords(array('*'),array('lower(name)' => strtolower($address['state'])),STATE);
if(is_array($state) && count($state))
{ $state_id = $state[0]['state_id'];
$pincode = $CI->PD_Model->selectCustomRecords(array('*'),array('pincode' => $address['pincode'],'fk_state_id'=>$state_id ),PINCODE);
$city = $CI->PD_Model->selectCustomRecords(array('*'),array('lower(name)' => strtolower($address['city']),'fk_state_id'=>$state_id),CITY);
}
// print_r($CI->db->last_query());
// $state = $CI->PD_Model->selectCustomRecords(array('*'),array('pincode' => $address['pincode']),PINCODE,1);
$fields_to_insert = array('file_id' => SELF::$current_excel_file_id,'addressline' => $address['address']);
if(is_array($pincode) && count($pincode))
{
$fields_to_insert['pincode'] = $pincode[0]['pincode_id'];
}
if(is_array($city) && count($city))
{
$fields_to_insert['city'] = $city[0]['city_id'];
$fields_to_insert['state'] = $city[0]['fk_state_id'];
}
// print_r($fields_to_insert);
$temp_add_id = $CI->PD_Model->saveRecords($fields_to_insert,SMC_TEMP_ADDRESS);
$fields_to_insert['address_id'] = $temp_add_id;
$return_data['address_details'][ $key ]['display'] = $display;
$return_data['address_details'][ $key ]['id'] = $fields_to_insert;
}
}
}
}
return ($return_data);
}
static function getIndividualSectionData($sheetData,$section_ref_data)
{
// echo "now I am In ind part";die();
$return_data = array();
$keys = array();
foreach ($sheetData as $key => $value)
{
$tmp = SELF::returnMatchedData($section_ref_data,$value);//print_r($tmp);
if(is_array($tmp))
{
if(isset($tmp['parent_key']))
{
$trimedData = SELF::trimExcelEmptyCells($value);
// $trimedData = ($value);
$temp[$tmp['parent_key']][$tmp['key_name']] = SELF::getMultipleCellValues($trimedData,$tmp['key_name']);
$keys[$tmp['parent_key']][] = $tmp['key_name'];
$return_data = $temp;
}
else
{
$temp[$tmp['key_name']] = $value['C'];
$return_data = $temp;
}
}
}
// print_r($keys);
//print_r($return_data);die();
$return_data = SELF::transformRowColumsForMultipleData($return_data,$keys);
if(SELF::$current_section_id == 10)
{
//implement financial calculations
$pre_yr_fin_data = array();
if(isset($return_data['financial_info']) && count($return_data['financial_info']) >= 1)
{
$return_data['financial_info'] = array_reverse($return_data['financial_info']);
$return_data['financial_info_available'] = 'yes';
foreach ($return_data['financial_info'] as $fy_key => $fy_value)
{
$net_profit_sales_percent = (isset($fy_value['sales_revenue']) && is_numeric($fy_value['sales_revenue']) && isset($fy_value['net_profit']) && is_numeric($fy_value['net_profit']) ? (($fy_value['net_profit'] / $fy_value['sales_revenue']) * 100): 0.00);
$return_data['financial_info'][$fy_key]['net_profit_sales_percent'] = number_format($net_profit_sales_percent,2,'.','');
$return_data['financial_info'][$fy_key]['sales_py'] = ($fy_key == 0 ? 'NA' : number_format ((($fy_value['sales_revenue'] - $pre_yr_fin_data['sales_revenue']) / $pre_yr_fin_data['sales_revenue']),2,'.',''));
$return_data['financial_info'][$fy_key]['netprofit_py'] = ($fy_key == 0 ? 'NA' : number_format ((($fy_value['net_profit'] - $pre_yr_fin_data['net_profit']) / $pre_yr_fin_data['net_profit'] ),2,'.',''));
$return_data['financial_info'][$fy_key]['netprofit_py_percent'] = ($fy_key == 0 ? 'NA' : number_format((($net_profit_sales_percent - $pre_yr_fin_data['net_profit_sales_percent']) / $pre_yr_fin_data['net_profit_sales_percent']),2,'.',''));
$pre_yr_fin_data = $fy_value;
$pre_yr_fin_data['net_profit_sales_percent'] = $net_profit_sales_percent;
}
}
$return_data['broken_period_turnover_percent'] = SELF::getNumFormat($return_data['broken_period_turnover_percent'],'per');
// $return_data['net_profit_to_sales_percent'] = SELF::getNumFormat($return_data['net_profit_to_sales_percent'],'per');
// $return_data['net_profit_rs'] = SELF::getNumFormat($return_data['net_profit_rs']);
// $return_data['sales_amount'] = SELF::getNumFormat($return_data['sales_amount']);
$return_data['broken_period_turnover_cy'] = SELF::getNumFormat($return_data['broken_period_turnover_cy']);
$return_data['broken_period_turnover_py'] = SELF::getNumFormat($return_data['broken_period_turnover_py']);
$return_data['broken_period_from_month'] = SELF::getExcel2DateFormat($return_data['broken_period_from_month']);
$return_data['broken_period_to_month'] = SELF::getExcel2DateFormat($return_data['broken_period_to_month']);
}
// print_r($return_data);
// die();
return ($return_data);
}
static function getTriggerData($sheetData,$section_ref_data)
{
// echo 'worked';
$return_data = array();
unset($sheetData[1]);
// print_r($sheetData);
// die();
$main_applicant_column_name = '';
foreach ($sheetData as $key => $value)
{
$value = SELF::trimExcelEmptyCells($value);
//print_r($value);//die();
$tmp = SELF::returnMatchedData($section_ref_data,$value);
// print_r($tmp);
// $temp = array();
if(is_array($tmp) && isset($tmp['key_name']))
{
// $return_data[$tmp['key_name']] = $value['C'];
//$temp['value'] = $value['C'];
// print_r($return_data);
// echo '################';
if(isset($tmp['parent_key']))// & !array_key_exists($tmp['parent_key'], $return_data))
{
if($tmp['parent_key'] == 'address_details')
{ $return_data[$tmp['parent_key']][] = array('city' => $value['C'],'address' =>$value['B'],'state' => $value['D'],'pincode' => $value['E']); }
}
else{ $return_data[$tmp['key_name']] = $value['C']; }
// print_r($temp);
// echo '@@@@@@@@@@@@@';
// $return_data = $temp;
if($key == 8)
{
// print_r($value);
//getting co applicants only
$value = (array_splice(($value),3));$value = array_reverse($value);
// print_r($value);
foreach ($value as $val)
{
$return_data['co_applicants'][] = array('applicant_name' => $val);
}
}
}
// if($key == 9)
// {break;}
}
// print_r($return_data);die();
return ($return_data);
}
static function returnMatchedData($arr,$val)//$arr is sheetdata where $val is section column ref's
{
foreach ($arr as $key => $value)
{
if(!$value['parent_key'] && (isset($val['A'])) && ($val['A'] == $value['ref']))
{
return array('key_name' => $value['key_name']);
}
else if($value['parent_key'] && (isset($val['A'])) && ($val['A'] == $value['ref']))
{
return array('key_name' => $value['key_name'],'parent_key' => $value['parent_key']);
}
}
}
public static function getApplicantsNameById($id,$latest_applicants_names,$type = 1) //$type i for return name only, 2 for return title only, and 3 for return both
{
foreach ($latest_applicants_names as $key => $value) {
if($id == $value['pd_co_applicant_id'])
{
return $type == 1 ? $value['applicant_name'] : ($type == 2 ? $value['title'] : ($value['title'].' '.$value['applicant_name']));
}
}
}
static function trimExcelEmptyCells($arr)
{
$return_data = array();
foreach ($arr as $key => $value)
{
if((is_string($value) && $value != '') || is_numeric($value))// && $value != '')
// if($value)
{
$return_data[$key] = $value;
}
}
return $return_data;
}
static function getMultipleCellValues($trimedData,$key_name)
{
$return_data = array();
foreach ($trimedData as $key => $value)
{
if($key != 'A' && $key != 'B')
{
// $return_data[] = array($key_name => $value);
$return_data[] = $value;
}
}
// if(SELF::$current_section_id == 5)
// {
// print_r($return_data);
// }
return $return_data;
}
static function transformRowColumsForMultipleData($return_data,$keys)
{
// print_r($return_data);die();
$final_data = array();
$keys_to_be_decimal = array('sales_revenue','net_profit');
$keys_to_be_per = array('share_holding');
if(isset($return_data['borrower_details'])){
$array2 = array_filter($return_data['borrower_details']['numbers_of_years_business_running'], function($a) {return $a !== 'NA';});
$array1 = array_filter($return_data['borrower_details']['numbers_of_years_applicant_in_business'], function($a) {return $a !== 'NA';});
$return_data['borrower_details']['numbers_of_years_business_running'] = array_replace_recursive($array1, $array2);
}
if(isset($return_data['existing_loan_details'])){
$return_data['existing_loan_details']['original_tenure_months'] = array_map(function($num){return number_format($num,0);}, $return_data['existing_loan_details']['original_tenure_months']);
$return_data['existing_loan_details']['current_tenure_months'] = array_map(function($num){return number_format($num,0);}, $return_data['existing_loan_details']['current_tenure_months']);
$return_data['existing_loan_details']['elapsed_tenure_months'] = array_map(function($num){return number_format($num,0);}, $return_data['existing_loan_details']['elapsed_tenure_months']);
}
foreach ($keys as $current_key => $arr)
{
if(array_key_exists($current_key, $return_data))
{
$data_to_be_transform = $return_data[$current_key];
// print_r($data_to_be_transform);
$total_no_of_fields = count($data_to_be_transform);
$total_no_of_data = count( (reset($data_to_be_transform)) );
// echo $total_no_of_fields.'-'.$total_no_of_data;die();
$temp = array();
for($i = 0;$i<$total_no_of_fields;$i++)
{
for($j=0;$j<$total_no_of_data;$j++)
{
if(in_array($arr[$i],$keys_to_be_decimal))
{
$temp[$j][ $arr[$i] ] = isset($data_to_be_transform[ $arr[$i] ][$j]) ? (SELF::getNumFormat($data_to_be_transform[ $arr[$i] ][$j])) : '';
}
else if(in_array($arr[$i],$keys_to_be_per))
{
$temp[$j][ $arr[$i] ] = isset($data_to_be_transform[ $arr[$i] ][$j]) ? (SELF::getNumFormat($data_to_be_transform[ $arr[$i] ][$j],'per')) : '';
}
else
{
$temp[$j][ $arr[$i] ] = isset($data_to_be_transform[ $arr[$i] ][$j]) ? $data_to_be_transform[ $arr[$i] ][$j] : '';
}
}
}
$return_data[$current_key] = $temp;
}
}
return $return_data;
}
static function getNumFormat($field,$field_type = '')
{
if(isset($field) && $field != '')
{
if($field_type == 'per')
{
$field = $field * 100;
}
$field = number_format($field,2,'.','');
}
return $field;
}
static function getExcel2DateFormat($excel_date)
{
// $excel_date = 39994;
return gmdate('d-m-Y',$UNIX_DATE = (($excel_date) - 25569) * 86400);
// return date('m/d/Y',mktime(0,0,0,1,($excel_date-1),1900));
//gmdate("d-m-Y H:i:s", $UNIX_DATE);
}
public static function getCompanyListForSMCList($file_path_withname,$pd_id = null,$excel_file_id = null)
{
$CI =&get_instance();
$CI->load->library('myspreadsheet');
$CI->load->model('PD_Model');
$section_id = 1;
SELF::$current_section_id = $section_id;
SELF::$current_excel_file_id = $excel_file_id;
$spreadsheet_io_fact = $CI->myspreadsheet->getIOFactory($file_path_withname);
$spreadsheet_io_fact->setActiveSheetIndex(0);
$sheetData = $spreadsheet_io_fact->getActiveSheet()->toArray(null, true, false, true);
$where_condition_array = array('isactive' => 1,'section_id' => $section_id);
$section_ref_data = $CI->PD_Model->selectCustomRecords(array('*'),$where_condition_array,EXCEL_COLUMN_REF_NAME);
$return_data = array();
$keys = array();
$company = array();
foreach ($sheetData as $key => $value)
{
$tmp = SELF::returnMatchedData($section_ref_data,$value);//print_r($tmp);
if(is_array($tmp))
{
if(isset($tmp['parent_key']))
{
$trimedData = SELF::trimExcelEmptyCells($value);
$temp[$tmp['parent_key']][$tmp['key_name']] = SELF::getMultipleCellValues($trimedData,$tmp['key_name']);
$keys[$tmp['parent_key']][] = $tmp['key_name'];
$return_data = $temp;
}
else
{
$temp[$tmp['key_name']] = $value['C'];
$return_data = $temp;
}
}
}
$return_data = SELF::transformRowColumsForMultipleData($return_data,$keys);
if(!empty($return_data['borrower_details'])){
$cid=0;//for company ID
for($i=0;$i<count($return_data['borrower_details']);$i++){
$entity_type = $return_data['borrower_details'][$i]['entity_type'];;
if($entity_type != "Individual"){
$cid++;//for company ID
$return_data['borrower_details'][$i]['company_id'] = $cid;
array_push($company,$return_data['borrower_details'][$i]);
}
}
}
return $company;
}
public static function change_key( $array, $old_key, $new_key) {
echo "line at 545";
if( ! array_key_exists( $old_key, $array[0] ) )
return $array;
print_r(array_keys($array[0]));
// print_r($keys);die();
$keys[ array_search( $old_key, $keys ) ] = $new_key;
print_r(array_combine( $keys, $array[0] ));
// return array_combine( $keys, $array );
}
}
?>