<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('send_sms_using_template'))
{
    function send_sms_using_template($data_id,$template_id){
    	/*
				SALES = TEMPLATE ID = 1
				ORDER = TEMPLATE ID = 2
	
    	*/
		$template_name='';
		if($template_id==1){
			$template_name = 'GREETING TO CUSTOMER ON SALES';
		}
		if($template_id==2){
			$template_name = 'GREETING TO CUSTOMER ON SALES RETURN';
		}

    	$CI =& get_instance();
		$q1=$CI->db->query("select * from db_smstemplates where template_name='$template_name' and status=1");
		if($q1->num_rows()>0){
			$content	=	$q1->row()->content;
			if(!empty($content)){
				switch ($template_id) {
					case 1:
						/* SALES SMS */
						
						$CI->db->select("a.store_id,a.customer_id,b.customer_name,b.mobile,a.sales_code,
												   a.sales_date,a.grand_total,a.paid_amount");
						$CI->db->from("db_sales a");
						$CI->db->where("a.id",$data_id);
						$CI->db->join("db_customers b","b.id=a.customer_id");

						$q2 = $CI->db->get();

						if($q2->num_rows()>0 && $q2->row()->customer_id!=1 && !empty($q2->row()->mobile)){
							//Replace Content
							
							$content = str_replace("{{customer_name}}", $q2->row()->customer_name, $content);
							$content = str_replace("{{sales_id}}", $q2->row()->sales_code, $content);
							$content = str_replace("{{sales_date}}", show_date($q2->row()->sales_date), $content);
							$content = str_replace("{{sales_amount}}", $CI->currency_code(number_format($q2->row()->grand_total,2,'.','')), $content);
							$content = str_replace("{{paid_amt}}", $CI->currency_code(number_format($q2->row()->paid_amount,2,'.','')), $content);
							$content = str_replace("{{due_amt}}",$CI->currency_code(number_format($q2->row()->grand_total-$q2->row()->paid_amount,2,'.','')), $content);

							/*Find Company Details*/
							$q3=$CI->db->select('*')->from('db_store')->where('id',$q2->row()->store_id)->get()->row();

							/*Insert/Replace into Content*/
							$content = str_replace("{{store_name}}", $q3->store_name, $content);
							$content = str_replace("{{store_mobile}}", $q3->mobile, $content);
							$content = str_replace("{{store_address}}", $q3->address, $content);
							$content = str_replace("{{store_website}}", $q3->store_website, $content);
							$content = str_replace("{{store_email}}", $q3->email, $content);

							//echo $content;exit();
							$CI->load->model('sms_model');
							return $CI->sms_model->send_sms($q2->row()->mobile,$content);
						}
						else{
							return false;
						}
						
						break;
					case 2:
						/* SALES RETURN SMS */
						$q2=$CI->db->query("SELECT a.store_id,a.customer_id,b.customer_name,b.mobile,a.return_code,
												   a.return_date,a.grand_total,a.paid_amount
											FROM db_salesreturn a,db_customers b where b.id=a.customer_id and a.id='$data_id'");
						if($q2->num_rows()>0 && $q2->row()->customer_id!=1 && !empty($q2->row()->mobile)){
							//Replace Content
							$content = str_replace("{{customer_name}}", $q2->row()->customer_name, $content);
							$content = str_replace("{{return_id}}", $q2->row()->return_code, $content);
							$content = str_replace("{{return_date}}", show_date($q2->row()->return_date), $content);
							$content = str_replace("{{return_amount}}", $CI->currency_code(number_format($q2->row()->grand_total,2,'.','')), $content);
							$content = str_replace("{{paid_amt}}", $CI->currency_code(number_format($q2->row()->paid_amount,2,'.','')), $content);
							$content = str_replace("{{due_amt}}",$CI->currency_code(number_format($q2->row()->grand_total-$q2->row()->paid_amount,2,'.','')), $content);

							/*Find Company Details*/
							$q3=$CI->db->select('*')->from('db_store')->where('id',$q2->row()->store_id)->get()->row();

							/*Insert/Replace into Content*/
							$content = str_replace("{{store_name}}", $q3->store_name, $content);
							$content = str_replace("{{store_mobile}}", $q3->mobile, $content);
							$content = str_replace("{{store_address}}", $q3->address, $content);
							$content = str_replace("{{store_website}}", $q3->store_website, $content);
							$content = str_replace("{{store_email}}", $q3->email, $content);

							//echo $content;exit();
							$CI->load->model('sms_model');
							return $CI->sms_model->send_sms($q2->row()->mobile,$content);
						}
						else{
							return false;
						}
						
						break;
					
					default:
						return true;
						# code...
						break;
				}
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}
}

if ( ! function_exists('is_sms_enabled'))
{
    function is_sms_enabled(){
    	$CI =& get_instance();
    	$sms_status=$CI->db->select('sms_status')
    			->where("id",get_current_store_id())
    			->get('db_store')->row()->sms_status;
    	return ($sms_status) ? true : false;
	}
}