Generate Access Token and Call the function with access token and send multiple records at single shot and get multiple validation and success responses:
In Controller:
<?php
require APPPATH . '/libraries/REST_Controller.php';
class Monthlydata extends REST_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set('Asia/Kolkata');
$this->load->model('Monthlydata_model');
}
public function generate_access_token_post(){
$username = $this->post('username');
$password = $this->post('password');
$data =array('username'=>$username,'password'=>$password);
$accesResponse= $this->Monthlydata_model->createAccessToken($data);
$this->set_response($accesResponse, REST_Controller::HTTP_OK);
}
public function monthly_update_post(){
$lrecord = array();
$inputdata = array();
$access_token = $this->post('access_token');
$user_id = $this->post('user_id');
$ibb_dealer_code = $this->post('ibb_dealer_code');
if(!empty($access_token) && !empty($user_id)){
$checkAccessToken = $this->Monthlydata_model->check_access_token($access_token,$user_id);
if(is_array($checkAccessToken) && count($checkAccessToken)>0){
$inputdata = $this->post('inputData');
$successData= array();
$err_msgs = array();
$i=0;
$row_count = 0;
$fail_count = 0;
$abm_fail_count = 0;
$int_validation_count = 0;
foreach ($inputdata as $val) {
$ibb_dealer_code = trim($val['ibb_dealer_code']);
$do_issued_as_on_date = trim($val['do_issued_as_on_date']);
if(empty($ibb_dealer_code) || empty($do_issued_as_on_date)){
$fail_count++;
}
if (filter_var($do_issued_as_on_date, FILTER_VALIDATE_INT)) {
$checkIbbDealerCode = $this->Monthlydata_model->ibb_dealer_code_already_exists($ibb_dealer_code);
if(!empty($ibb_dealer_code) && $checkIbbDealerCode[0]['abm_remarks']!='') {
$lrecord['percentage_achievement']=0;
if($checkIbbDealerCode[0]['target_for_the_curr_month']!=0 && $checkIbbDealerCode[0]['target_for_the_curr_month'] >0){
$lrecord['percentage_achievement'] = round(($do_issued_as_on_date/$checkIbbDealerCode[0]['target_for_the_curr_month'])*100);
$lrecord['difference_in_units'] = round($do_issued_as_on_date-$checkIbbDealerCode[0]['target_for_the_curr_month']);
}
$lrecord['do_issued_as_on_date'] = $do_issued_as_on_date;
$lrecord['last_modified_on'] = date('Y-m-d H:i:s');
$lrecord['last_modified_by'] = $val['user_id'];
$this->Monthlydata_model->update_single_row($checkIbbDealerCode[0]['ibb_dealer_code'],$lrecord);
$successData[$i]['message'] = "Updated successfully";
$successData[$i]['id'] = $checkIbbDealerCode[0]['id'];
$successData[$i]['ibb_dealer_code'] = $checkIbbDealerCode[0]['ibb_dealer_code'];
$i++;
$row_count++;
}else{
$abm_fail_count++;
}
}elseif(!empty($do_issued_as_on_date)){
$int_validation_count++;
} // do_issued_as_on_date integer validation end
} // foreach end
if($fail_count>0){
array_push($err_msgs,$fail_count." rows are failed to update, IBB dealer code and Do issued as on date fields are mandatory");
}
if($abm_fail_count>0){
array_push($err_msgs,$abm_fail_count." rows are failed to update, ABM does not have entered certain details or IBB dealer code was not found");
}
if($int_validation_count>0){
array_push($err_msgs,$int_validation_count." rows are failed to update, do issued as on date should be an integer");
}
$this->set_response(['errors'=>$err_msgs,'success' =>$successData,'status'=>'success','status_code'=>'200'], REST_Controller::HTTP_OK);
}else{
$this->set_response(['status'=>'false','message'=>'Access token expired or invalid'], REST_Controller::HTTP_OK);
}
}else{
$this->set_response(['status'=>'false','message'=>'access token or user id cannot be empty'], REST_Controller::HTTP_OK);
}
}
}
In Model:
<?php
class Monthlydata_model extends CI_Model {
function check_access_token($access_token,$user_id){
$this->db->select('id,access_token,expires_at');
$this->db->from('user_login');
$this->db->where('access_token',$access_token);
$this->db->where('expires_at >=',date("Y-m-d H:i:s"));
//$this->db->where('id',$user_id);
return $this->db->get()->result_array();
//echo $this->db->last_query();die;
}
function createAccessToken($data){
$this->db->select('id,access_token,expires_at');
$this->db->from('user_login');
$this->db->where('username',$data['username']);
$this->db->where('password', md5($data['password']));
$query = $this->db->get();
if($query->num_rows() == 1)
{
$result = $query->row();
if($result->expires_at < date("Y-m-d H:i:s")){
$accessToken = $this->randomKey(39);
$expire_time = date("Y-m-d H:i:s", strtotime('+24 hours'));
$this->db->where('id',$result->id);
$this->db->update('user_login',array('access_token'=>$accessToken,'expires_at'=>$expire_time));
$return = array('status'=>'success','access_token'=>$accessToken,'expires_at'=>$expire_time);
} else {
$return = array('status'=>'success','access_token'=>$result->access_token,'expires_at'=>$result->expires_at);
}
} else {
$return = array('status'=>'failure','message'=>'Invalid username/password.');
}
return $return;
}
function randomKey($length){
$pool = array_merge(range(0,9), range('a', 'z'),range('A', 'Z'));
$key = null;
for($i=0; $i < $length; $i++) {
$key .= $pool[mt_rand(0, count($pool) - 1)];
}
return $key;
}
function ibb_dealer_code_already_exists($ibb_dealer_code){
$this->db->where('ibb_dealer_code',$ibb_dealer_code);
$query = $this->db->get('master_data_reports');
if ($query->num_rows() > 0){
return $data = $query->result_array();
}else{
return false;
}
}
function update_single_row($lead_id,$data) {
$this->db->where("ibb_dealer_code", $lead_id);
$this->db->update('master_data_reports', $data);
return true;
}
}
?>
In Controller:
<?php
require APPPATH . '/libraries/REST_Controller.php';
class Monthlydata extends REST_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set('Asia/Kolkata');
$this->load->model('Monthlydata_model');
}
public function generate_access_token_post(){
$username = $this->post('username');
$password = $this->post('password');
$data =array('username'=>$username,'password'=>$password);
$accesResponse= $this->Monthlydata_model->createAccessToken($data);
$this->set_response($accesResponse, REST_Controller::HTTP_OK);
}
public function monthly_update_post(){
$lrecord = array();
$inputdata = array();
$access_token = $this->post('access_token');
$user_id = $this->post('user_id');
$ibb_dealer_code = $this->post('ibb_dealer_code');
if(!empty($access_token) && !empty($user_id)){
$checkAccessToken = $this->Monthlydata_model->check_access_token($access_token,$user_id);
if(is_array($checkAccessToken) && count($checkAccessToken)>0){
$inputdata = $this->post('inputData');
$successData= array();
$err_msgs = array();
$i=0;
$row_count = 0;
$fail_count = 0;
$abm_fail_count = 0;
$int_validation_count = 0;
foreach ($inputdata as $val) {
$ibb_dealer_code = trim($val['ibb_dealer_code']);
$do_issued_as_on_date = trim($val['do_issued_as_on_date']);
if(empty($ibb_dealer_code) || empty($do_issued_as_on_date)){
$fail_count++;
}
if (filter_var($do_issued_as_on_date, FILTER_VALIDATE_INT)) {
$checkIbbDealerCode = $this->Monthlydata_model->ibb_dealer_code_already_exists($ibb_dealer_code);
if(!empty($ibb_dealer_code) && $checkIbbDealerCode[0]['abm_remarks']!='') {
$lrecord['percentage_achievement']=0;
if($checkIbbDealerCode[0]['target_for_the_curr_month']!=0 && $checkIbbDealerCode[0]['target_for_the_curr_month'] >0){
$lrecord['percentage_achievement'] = round(($do_issued_as_on_date/$checkIbbDealerCode[0]['target_for_the_curr_month'])*100);
$lrecord['difference_in_units'] = round($do_issued_as_on_date-$checkIbbDealerCode[0]['target_for_the_curr_month']);
}
$lrecord['do_issued_as_on_date'] = $do_issued_as_on_date;
$lrecord['last_modified_on'] = date('Y-m-d H:i:s');
$lrecord['last_modified_by'] = $val['user_id'];
$this->Monthlydata_model->update_single_row($checkIbbDealerCode[0]['ibb_dealer_code'],$lrecord);
$successData[$i]['message'] = "Updated successfully";
$successData[$i]['id'] = $checkIbbDealerCode[0]['id'];
$successData[$i]['ibb_dealer_code'] = $checkIbbDealerCode[0]['ibb_dealer_code'];
$i++;
$row_count++;
}else{
$abm_fail_count++;
}
}elseif(!empty($do_issued_as_on_date)){
$int_validation_count++;
} // do_issued_as_on_date integer validation end
} // foreach end
if($fail_count>0){
array_push($err_msgs,$fail_count." rows are failed to update, IBB dealer code and Do issued as on date fields are mandatory");
}
if($abm_fail_count>0){
array_push($err_msgs,$abm_fail_count." rows are failed to update, ABM does not have entered certain details or IBB dealer code was not found");
}
if($int_validation_count>0){
array_push($err_msgs,$int_validation_count." rows are failed to update, do issued as on date should be an integer");
}
$this->set_response(['errors'=>$err_msgs,'success' =>$successData,'status'=>'success','status_code'=>'200'], REST_Controller::HTTP_OK);
}else{
$this->set_response(['status'=>'false','message'=>'Access token expired or invalid'], REST_Controller::HTTP_OK);
}
}else{
$this->set_response(['status'=>'false','message'=>'access token or user id cannot be empty'], REST_Controller::HTTP_OK);
}
}
}
In Model:
<?php
class Monthlydata_model extends CI_Model {
function check_access_token($access_token,$user_id){
$this->db->select('id,access_token,expires_at');
$this->db->from('user_login');
$this->db->where('access_token',$access_token);
$this->db->where('expires_at >=',date("Y-m-d H:i:s"));
//$this->db->where('id',$user_id);
return $this->db->get()->result_array();
//echo $this->db->last_query();die;
}
function createAccessToken($data){
$this->db->select('id,access_token,expires_at');
$this->db->from('user_login');
$this->db->where('username',$data['username']);
$this->db->where('password', md5($data['password']));
$query = $this->db->get();
if($query->num_rows() == 1)
{
$result = $query->row();
if($result->expires_at < date("Y-m-d H:i:s")){
$accessToken = $this->randomKey(39);
$expire_time = date("Y-m-d H:i:s", strtotime('+24 hours'));
$this->db->where('id',$result->id);
$this->db->update('user_login',array('access_token'=>$accessToken,'expires_at'=>$expire_time));
$return = array('status'=>'success','access_token'=>$accessToken,'expires_at'=>$expire_time);
} else {
$return = array('status'=>'success','access_token'=>$result->access_token,'expires_at'=>$result->expires_at);
}
} else {
$return = array('status'=>'failure','message'=>'Invalid username/password.');
}
return $return;
}
function randomKey($length){
$pool = array_merge(range(0,9), range('a', 'z'),range('A', 'Z'));
$key = null;
for($i=0; $i < $length; $i++) {
$key .= $pool[mt_rand(0, count($pool) - 1)];
}
return $key;
}
function ibb_dealer_code_already_exists($ibb_dealer_code){
$this->db->where('ibb_dealer_code',$ibb_dealer_code);
$query = $this->db->get('master_data_reports');
if ($query->num_rows() > 0){
return $data = $query->result_array();
}else{
return false;
}
}
function update_single_row($lead_id,$data) {
$this->db->where("ibb_dealer_code", $lead_id);
$this->db->update('master_data_reports', $data);
return true;
}
}
?>
No comments:
Post a Comment