File: /home/cafsindia/login_cafsindia_com/application/models/Sms_model.php
<?php
class Sms_model extends CI_Model
{
// GET SMS TEXT
public function get_sms_text($sms_for){
$this->db->select('sms_txt');
$this->db->from('nch_sms');
$this->db->where('sms_for', $sms_for);
$sms_info = $this->db->get()->result();
return $sms_info;
}
//TRIGGER SMS
public function trigger_sms($mobile_no,$sms_content){
$sms_url = "http://sms.cafsinfotech.com/httpapi/smsapi?";
$sms_id = "nationalchn";
$sms_pwd = "national@19";
$sender_id = "NATCHN";
$url = $sms_url."uname=$sms_id&password=$sms_pwd&sender=".urlencode($sender_id)."&receiver=".urlencode($mobile_no)."&route=TA&msgtype=1&sms=".urlencode($sms_content);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if(curl_exec($ch)){
curl_close($ch);
return true;
}else{
curl_close($ch);
return false;
}
}
//GET SMS CONTANT
public function send_sms($db_id,$mode){
if($mode === 'booking'){
$booking_info = $this->get_booking_data($db_id);
$booking_info = json_decode($booking_info);
$booking_count= $booking_info->booking_count;
foreach($booking_info->booking_result as $book_result){
$mobile_no = $book_result->contact_mobile;
$status = $book_result->booking_sts;
$send_data = $book_result;
if((int)$status === 2){
$sms_for = 'confirm';
$sms_txt = "";
$sms_content = "";
$db_sms = $this->get_sms_text($sms_for);
if($db_sms){
$sms_txt = $db_sms[0]->sms_txt;
}
if($sms_txt !== ""){
$sms_content = $this->replace_txt($sms_txt,$sms_for,$send_data);
return $this->send_sms_cus($mobile_no,$sms_for,$sms_content,$booking_count);
}
}else
if((int)$status === 3){
$sms_for = 'cancelled';
$sms_txt = "";
$sms_content = "";
$db_sms = $this->get_sms_text($sms_for);
if($db_sms){
$sms_txt = $db_sms[0]->sms_txt;
}
if($sms_txt !== ""){
$sms_content = $this->replace_txt($sms_txt,$sms_for,$send_data);
return $this->send_sms_cus($mobile_no,$sms_for,$sms_content,$booking_count);
}
}else{
return false;
}
}return true;
}else
if($mode === 'account'){
$send_data = '';
$accounts_info = $this->get_accounts_data($db_id);
$accounts_rslt = json_decode($accounts_info);
$accounts_count= $accounts_rslt->accounts_count;
if(!empty($accounts_count)){
foreach($accounts_rslt->accounts_result as $acc_result){
$mobile_no = $acc_result->contact_mobile;
$status = $acc_result->accounts_sts;
$send_data = $acc_result;
if((int)$status === 5){
$sms_for = 'completed';
$sms_txt = "";
$sms_content = "";
$db_sms = $this->get_sms_text($sms_for);
if($db_sms){
$sms_txt = $db_sms[0]->sms_txt;
}
if($sms_txt !== ""){
$sms_content = $this->replace_txt($sms_txt,$sms_for,$send_data);
return $this->send_sms_cus($mobile_no,$sms_for,$sms_content,$booking_count);
}
}else{
continue;
}
}return true;
}else{
return false;
}
}else
if($mode === 'operation'){
$send_data = '';
$operation_rslt = $this->get_operation_data($db_id);
$operation_rslt = json_decode($operation_rslt);
$operation_count= $operation_rslt->operation_count;
if(!empty($operation_count)){
foreach($operation_rslt->operation_result as $oper_result){
$mobile_no = $oper_result->contact_mobile;
$status = $oper_result->operation_sts;
$send_data = $oper_result;
if((int)$status === 4){
$sms_for = 'on_progress';
$sms_txt = "";
$sms_content = "";
$db_sms = $this->get_sms_text($sms_for);
if($db_sms){
$sms_txt = $db_sms[0]->sms_txt;
}
if($sms_txt !== ""){
$sms_content = $this->replace_txt($sms_txt,$sms_for,$send_data);
return $this->send_sms_cus($mobile_no,$sms_for,$sms_content,$booking_count);
}
}
}return true;
}
}else
if($mode === 'balance'){
$balance_info = $this->get_balance_data($db_id);
if(!empty($balance_info)){
foreach($balance_info as $balance_rlst){
$mobile_no = $balance_rlst->contact_mobile;
$sms_for = 'balance';
$sms_txt = "";
$sms_content = "";
$count = 0;
$db_sms = $this->get_sms_text($sms_for);
if($db_sms){
$sms_txt = $db_sms[0]->sms_txt;
}
if($sms_txt !== ""){
$sms_content = $this->replace_txt($sms_txt,$sms_for,$balance_rlst);
return $this->send_sms_cus($mobile_no,$sms_for,$sms_content,$count);
}
}return true;
}else{
return false;
}
}
}
/*** SEND SMS TO CUSTOMER ****/
public function send_sms_cus($mobile_no,$sms_for,$sms_content,$count){
if($mobile_no){
$this->db->select('sent_for,created_date');
$this->db->from('nch_sms_log');
$this->db->where('sent_for', $sms_for);
$this->db->where('sms_txt', $sms_content);
$this->db->where('to_mobile', $mobile_no);
$sms_log = $this->db->get();
$sms_log_info = $sms_log->row();
$sms_log_count = (int)$sms_log->num_rows();
$created_date = '';
if($sms_log_count > 0){
$created_date = date('Y-m-d',strtotime($sms_log_info->created_date));
}
$today = date('Y-m-d');
if($created_date != $today){
$sms_data = array(
'to_mobile' => $mobile_no,
'sms_txt' => $sms_content,
'sent_for' => $sms_for,
'created_date' => date("Y-m-d h:i:s")
);
if($this->db->insert('nch_sms_log', $sms_data)){
if($this->trigger_sms($mobile_no,$sms_content)){
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}else{
return FALSE;
}
}else{
return false;
}
}
//GET BOOKING INFOMATION
public function get_booking_data($prime_booking_id){
$this->db->select('booking.tour_no,booking.from_date,booking.origin,booking.cancel_reason, booking.destination,booking.booking_sts,customer_name,booking_type.booking_type,trip_type.trip_type,vehicle_type.vehicle_type,vehicle_number,no_of_pax,vech_avg_km,booking_amount,discount_percentage,gst_sts,gst_percentage,vech_total_amount,contact_name,contact_mobile,reporting_address,reporting_time');
$this->db->from('booking');
$this->db->join('booking_line', 'booking_line.prime_booking_id = booking.prime_booking_id','inner');
$this->db->join('customer', 'booking_line.prime_customer_id = customer.prime_customer_id','inner');
$this->db->join('booking_type', 'booking_line.booking_type = booking_type.prime_booking_type_id','inner');
$this->db->join('trip_type', 'booking_line.trip_type = trip_type.prime_trip_type_id','inner');
$this->db->join('vehicle_type', 'booking_line.vehicle_type = vehicle_type.prime_vehicle_type_id','inner');
$this->db->join('vehicle', 'booking_line.preferred_vehicle = vehicle.prime_vehicle_id','inner');
$this->db->where('booking.prime_booking_id', $prime_booking_id);
$this->db->where('booking.trans_status', 1);
$this->db->group_by('booking_line.prime_booking_line_id');
$this->db->group_by('booking_line.preferred_vehicle');
$booking_line_info = $this->db->get();
$booking_count = $booking_line_info->num_rows();
$booking_result = $booking_line_info->result();
return json_encode(array('booking_result' => $booking_result,'booking_count' => $booking_count));
}
//GET ACCOUNTS DATA
public function get_accounts_data($prime_booking_id){
$this->db->select('tour_no,customer_name, from_date, to_date, origin, destination, booking_sts, operation_sts, accounts_sts, ncd, cancel_reason, balance_amount,vehicle_type.vehicle_type,vehicle_number, assign_vehicle, review_status, primary_driver, primary_driver_name, primary_driver_mobile, secondary_driver, secondary_driver_name, secondary_driver_mobile, cleaner_from, cleaner_name, cleaner_mobile,contact_name,contact_mobile,reporting_address,reporting_time ');
$this->db->from('accounts');
$this->db->join('booking_line', 'booking_line.prime_booking_id = accounts.prime_booking_id','inner');
$this->db->join('accounts_line', 'accounts.prime_accounts_id = accounts_line.prime_accounts_id','inner');
$this->db->join('customer', 'booking_line.prime_customer_id = customer.prime_customer_id','inner');
$this->db->join('vehicle', 'accounts_line.assign_vehicle = vehicle.prime_vehicle_id','inner');
$this->db->join('vehicle_type', 'accounts_line.vehicle_type = vehicle_type.prime_vehicle_type_id','inner');
$this->db->where('accounts.prime_booking_id',$prime_booking_id);
$this->db->where('accounts.trans_status',1);
$this->db->group_by('accounts_line.prime_accounts_line_id');
$this->db->group_by('accounts_line.assign_vehicle');
$accounts_info = $this->db->get();
$accounts_count = $accounts_info->num_rows();
$accounts_result = $accounts_info->result();
return json_encode(array('accounts_result' => $accounts_result,'accounts_count' => $accounts_count));
}
//GET OPERATION DATA
public function get_operation_data($prime_booking_id){
$this->db->select('tour_no,customer_name, from_date, to_date, origin, destination, booking_sts, operation_sts, ncd, cancel_reason, balance_amount,vehicle_type.vehicle_type,vehicle_number, operation_line.assign_vehicle, review_status, primary_driver, primary_driver_name, primary_driver_mobile, secondary_driver, secondary_driver_name, secondary_driver_mobile, cleaner_from, cleaner_name, cleaner_mobile,contact_name,contact_mobile,reporting_address,reporting_time ');
$this->db->from('operation');
$this->db->join('booking_line', 'booking_line.prime_booking_id = operation.prime_booking_id','inner');
$this->db->join('operation_line', 'operation.prime_operation_id = operation_line.prime_operation_id','inner');
$this->db->join('customer', 'booking_line.prime_customer_id = customer.prime_customer_id','inner');
$this->db->join('vehicle', 'operation_line.assign_vehicle = vehicle.prime_vehicle_id','inner');
$this->db->join('vehicle_type', 'operation_line.vehicle_type = vehicle_type.prime_vehicle_type_id','inner');
$this->db->where('operation.prime_booking_id',$prime_booking_id);
$this->db->where('operation.trans_status',1);
$this->db->group_by('operation_line.prime_operation_line_id');
$this->db->group_by('operation_line.assign_vehicle');
$operation_info = $this->db->get();
$operation_count = $operation_info->num_rows();
$operation_result = $operation_info->result();
return json_encode(array('operation_result' => $operation_result,'operation_count' => $operation_count));
}
//GET BALANACE DATA
public function get_balance_data($prime_booking_id){
$this->db->select('booked_total, extra_km_amount, paid_total, balance_amount,balance_line.customer_id, paid_date,reference_no, paid_amount, paid_remarks ,contact_name ,contact_mobile');
$this->db->from('balance');
$this->db->join('balance_line', 'balance_line.prime_balance_id = balance.prime_balance_id','inner');
$this->db->join('booking_line', 'booking_line.prime_booking_id = balance.prime_balance_booking_id','inner');
$this->db->where('balance.prime_balance_booking_id',$prime_booking_id);
$this->db->where('balance.trans_status',1);
$this->db->group_by('balance.prime_balance_booking_id');
$balance_info = $this->db->get()->result();
return $balance_info;
}
//REPLACE TEXT
public function replace_txt($sms_txt,$sms_for,$send_data){
$replace = "";
if($sms_for === "confirm"){
$tour_no = $send_data->tour_no;
$customer_name = $send_data->customer_name;
$vehicle_type = $send_data->vehicle_type;
$vehicle_number = $send_data->vehicle_number;
$no_of_pax = $send_data->no_of_pax;
$contact_name = $send_data->contact_name;
$contact_mobile = $send_data->contact_mobile;
$reporting_time = $send_data->reporting_time;
$reporting_address = $send_data->reporting_address;
$from_date = date('d-m-Y',strtotime($send_data->from_date));
$alt_mobile_number = 9841584495;
$company_mobile_number = 9841584496;
$replace = str_replace("@TOURNO",$tour_no,$sms_txt);
$replace = str_replace("@CUSNAME",$contact_name,$replace);
$replace = str_replace("@VEHICLETYPE",$vehicle_type,$replace);
$replace = str_replace("@MOB1",$company_mobile_number,$replace);
$replace = str_replace("@MOB2",$alt_mobile_number,$replace);
}else
if($sms_for === "cancelled"){
$tour_no = $send_data->tour_no;
$contact_name = $send_data->contact_name;
$cancel_reason = $send_data->cancel_reason;
$today = date('Y-m-d');
$alt_mobile_number = 9841584495;
$company_mobile_number = 9841584496;
$replace = str_replace("@TOURNO",$tour_no,$sms_txt);
$replace = str_replace("@CUSNAME",$contact_name,$replace);
$replace = str_replace("@MOB1",$company_mobile_number,$replace);
$replace = str_replace("@MOB2",$alt_mobile_number,$replace);
}else
if($sms_for === "on_progress"){
$tour_no = $send_data->tour_no;
$customer_name = $send_data->customer_name;
$origin = $send_data->origin;
$destination = $send_data->destination;
$vehicle_type = $send_data->vehicle_type;
$vehicle_number = $send_data->vehicle_number;
$primary_driver = $send_data->primary_driver;
$primary_driver_name = $send_data->primary_driver_name;
$primary_driver_mobile = $send_data->primary_driver_mobile;
$secondary_driver = $send_data->secondary_driver;
$secondary_driver_name = $send_data->secondary_driver_name;
$secondary_driver_mobile = $send_data->secondary_driver_mobile;
$contact_name = $send_data->contact_name;
$contact_mobile = $send_data->contact_mobile;
$reporting_time = $send_data->reporting_time;
$reporting_address = $send_data->reporting_address;
$from_date = date('d-m-Y',strtotime($send_data->from_date));
$current_data = date('d-m-Y h:i:s');
$alt_mobile_number = 9841584495;
$company_mobile_number = 9841584496;
$replace = str_replace("@TOURNO",$tour_no,$sms_txt);
$replace = str_replace("@CUSNAME",$contact_name,$replace);
$replace = str_replace("@VEHICLETYPE",$vehicle_type,$replace);
$replace = str_replace("@VEHICLENO",$vehicle_number,$replace);
$replace = str_replace("@MOB1",$company_mobile_number,$replace);
$replace = str_replace("@MOB2",$alt_mobile_number,$replace);
$replace = str_replace("@DATETIME",$current_data,$replace);
if($primary_driver){
$driver_name = $primary_driver_name ."/".$secondary_driver_name;
}
if($primary_driver_mobile){
$driver_mob = $primary_driver_mobile ."/".$secondary_driver_mobile;
}
$replace = str_replace("@DRIVER",$driver_name,$replace);
$replace = str_replace("@DRIVERMOB",$driver_mob,$replace);
}else
if($sms_for === "completed"){
$tour_no = $send_data->tour_no;
$replace = str_replace("@TOURNO",$tour_no,$sms_txt);
}else
if($sms_for === "balance"){
$contact_name = $send_data->contact_name;
$paid_total = $send_data->paid_total ;
$balance_amount = $send_data->balance_amount;
$booked_total = $send_data->booked_total;
$replace = str_replace("@CUSNAME",$contact_name,$sms_txt);
$replace = str_replace("@TOTAL",$booked_total,$replace);
$replace = str_replace("@PAID",$paid_total,$replace);
$replace = str_replace("@BALANCE ",$balance_amount,$replace);
}
return $replace;
}
}
?>