sparqapi/api/application/controllers/Flyhi_Controller.php
2022-06-08 17:32:56 +05:30

215 lines
7.8 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . '/libraries/REST_Controller.php';
require APPPATH . 'vendor/autoload.php';
class Flyhi_Controller extends REST_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Flyhi_Model','Flyhi_Model');
$this->load->model('Sales_PD_Model','Sales_PD_Model');
}
public function listFlyhiPD_get(){
$user_id = $this->uri->segment(3);
$columns = array('SALES_PD_DATA.sales_pd_id','SALES_PD_DATA.sales_pd_sno','SALES_PD_DATA.applicant_name','concat(USERPROFILE.first_name," ",USERPROFILE.last_name) as officer_name','SALES_PD_DATA.applicant_id','SALES_PD_DATA.lender_id','SALES_PD_DATA.is_pdf','SALES_PD_DATA.is_edited','ENTITY.full_name','ENTITY.short_name','SALES_PD_DATA.status','SALES_PD_DATA.sales_pd_type','SALES_PD_DATA.rand_string','SALES_PD_DATA.isactive','SALES_PD_DATA.city_id','CITY.name as city_name',
'(CASE WHEN SALES_PD_DATA.status = "STARTED" THEN SALES_PD_DATA.createdon WHEN SALES_PD_DATA.status = "COMPLETED" THEN SALES_PD_DATA.completed_date ELSE NULL END) AS createdon');
$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' => CITY. ' as CITY',
'condition' =>'SALES_PD_DATA.city_id = CITY.city_id',
'jointype' => 'LEFT'),
array('table' => USERPROFILE. ' as USERPROFILE',
'condition' =>'SALES_PD_DATA.createdby = USERPROFILE.userid',
'jointype' => 'LEFT')
);
$or_where = array();
$where_condition_array['SALES_PD_DATA.isactive'] = 1;
// FILTERING THE LAST 10 DAYS RECORDS
$key_date='SALES_PD_DATA.createdon BETWEEN DATE_SUB(NOW(), INTERVAL 60 DAY) AND NOW()';
$condition_value=null;
$where_condition_array[$key_date] = $condition_value;
// ----END OF FILTERING THE LAST 10 DAYS RECORDS---
$FHFSL_lender = (ENVIRONMENT == 'production' ? 0 : 49);
if($user_id && is_numeric($user_id))
{ $where_condition_array['SALES_PD_DATA.createdby'] = $user_id; }
$where_condition_array['SALES_PD_DATA.sales_pd_type in (3,4)'] = null;
$order_by = 'SALES_PD_DATA.sales_pd_id';
$Flyhi_pd_data = $this->Flyhi_Model->getJoinRecords($columns,$table,$joins,$where_condition_array,$order_by,'DESC','',$or_where);
//print_r($this->db->last_query());die();
if(count($Flyhi_pd_data))
{
$data['dataStatus'] = true;
$data['status'] = REST_Controller::HTTP_OK;
$data['records'] = $Flyhi_pd_data;
$this->response($data,REST_Controller::HTTP_OK);
}
else
{
$data['dataStatus'] = false;
$data['status'] = REST_Controller::HTTP_NO_CONTENT;
$data['msg'] = 'FlyHI PD Data Not Found! Try Later';
$this->response($data,REST_Controller::HTTP_OK);
}
}
public function saveFlyhiPD_post()
{
$records = $this->post('records');
$is_entry_done = null;
$id = null;
log_message('ERROR','####FLYHI-PD SAVE CALLED');
$lender_short_name = $this->Flyhi_Model->selectCustomRecords(array('short_name'),array('entity_id' => $records['lender_id']),ENTITY);
// $section_names = array('1' => 'General Section','2' => 'Photography');
$section_names = array('1' => 'General Section');
// here i get the counts.
$count = $this->Flyhi_Model->selectCustomRecords(array('count(*) as count'),array('lender_id' => $records['lender_id'],'isactive' => 1),SALES_PD_DATA);
// If Condition inserting Data in Master Table
if($records['sales_pd_id'] == '' || $records['sales_pd_id'] == null)
{
/************ASSIGN Random URL******************/
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 16; $i++)
{
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$records['rand_string'] = $randomString;
log_message('ERROR','####INSERT NEW FLYHIPD');
$salepd_serial_no .= 'FHFSL-'.(++$count[0]['count']);
$records['sales_pd_sno'] = $salepd_serial_no;
// print_r($records);die();
$id = $this->Flyhi_Model->saveRecords($records,SALES_PD_DATA);
//start camunda process
//CAMUNDA::startSalesPDInstance($id);
}
else
{
if(isset($records['status']) && $records['status'] == 'COMPLETED')
{
log_message('ERROR','####COMPLETE FLYHI-PD'.$records['sales_pd_id']);
$drafted_sections = $this->Flyhi_Model->selectCustomRecords(array('section_id'),array('status' => 0,'sales_pd_id' => $records['sales_pd_id'],'isactive' => 1),SALESPD_SECTION_DATA);
$completed_sections = $this->Flyhi_Model->selectCustomRecords(array('section_id'),array('status' => 1,'sales_pd_id' => $records['sales_pd_id'],'isactive' => 1),SALESPD_SECTION_DATA);
// print_r($completed_sections);die();
$dup_of_section_names = $section_names;
// print_r($dup_of_section_names);
foreach ($completed_sections as $completed_section_key => $completed_section_value)
{
if($dup_of_section_names [ $completed_section_value['section_id'] ] )
{
unset($dup_of_section_names [ $completed_section_value['section_id'] ]);
}
}
unset($dup_of_section_names[9]);//unset photograph always
if(count($dup_of_section_names))
{
$res_data = array();
foreach ($dup_of_section_names as $key => $value)
{
$res_data[] = array('section_id' => $key,'sectin_name' => $section_names[$key]);
}
$data['dataStatus'] = true;
$data['records'] = $res_data;
$data['pd_status'] = 'DRAFT';
$data['status'] = REST_Controller::HTTP_OK;
$this->response($data,REST_Controller::HTTP_OK);
}
if(count($drafted_sections) )
{
$drafted_sections = array_map(function($single) use ($section_names)
{
return array('section_id' => $single['section_id'],'sectin_name' => $section_names [ $single['section_id'] ]);
},
$drafted_sections);
unset($records['status']);
$data['dataStatus'] = true;
$data['records'] = $drafted_sections;
$data['pd_status'] = 'DRAFT';
$data['status'] = REST_Controller::HTTP_OK;
$this->response($data,REST_Controller::HTTP_OK);
}
else
{
$records['status'] = 'COMPLETED';
$records['is_cus_link_disabled'] = 1;
$records['completed_date'] = date('Y-m-d H:i:s');
$id = $this->Flyhi_Model->updateRecords($records,SALES_PD_DATA,array('sales_pd_id' => $records['sales_pd_id']));
$city = '';
$form = $this->Flyhi_Model->selectCustomRecords(array(),array('isactive' => 1,'sales_pd_id' => $records['sales_pd_id'],'section_id' => 1,'status' => 1),SALESPD_SECTION_DATA);
$gen_form = json_decode($form[0]['section_data'],true);
if(!empty($gen_form)){
foreach ($gen_form['questions'] as $g_key => $g_value)
{
if($g_value['question_key'] == 'city')
{
$city = $g_value['answer_value'];
}
}
}//gen_form closed here.
}
}
else
{
log_message('ERROR','####UPDATE FLYHIPD'.$records['sales_pd_id']);
$id = $this->Flyhi_Model->updateRecords($records,SALES_PD_DATA,array('sales_pd_id' => $records['sales_pd_id']));
}
}
if($id)
{
$data['dataStatus'] = true;
$data['records'] = $id;
$data['status'] = REST_Controller::HTTP_OK;
if(isset($records['rand_string'])){ $data['rand_string'] = $records['rand_string']; }
$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);
}
}
}// Class Ends