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

class Sales_sms extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->library('session');	
		$this->load->helper(array('form', 'url')); 
		$this->load->model('Salestest_model');
		$this->db2 = $this->load->database('otherdb', TRUE);
	}
	
	
	
	
	public function dayend_send_email_pdf() 
	{
	    $branch = $this->session->userdata('branch');
	    //echo $branch;
		$cur_date = date("d-m-Y");
		//$cur_date = '26-09-2022';
		//$branch_code = substr($branch,2);
	    $paymentmode = $this->input->post('payment_mode');
		$paymentamount = $this->input->post('payment_amount');
        $datainsert = $this->Salestest_model->entered_data_insert($paymentmode,$paymentamount);
        $mobile ="";
	    $q=$this->db2->query("SELECT api_key,sender_id,mobile_no,template_id,email_id,adminstrative_email,branch_title FROM sms_authorize WHERE branch='$branch' and message_type_id='1'");
		if ($q->num_rows() > 0) 
		{
		    foreach($q->result() as $row)
            {
                $mobile = $row->mobile_no;
                $email_id = $row->email_id;
                $admin_email = $row->adminstrative_email;
                // $mobile = "8722808511,9986015156";
                // $email_id = "hanumantha@gravitysoft.in";
                // $admin_email = "hanumantha@gravitysoft.in";
                $authKey = $row->api_key;
                $senderId = $row->sender_id;
                $templateId = $row->template_id;
                $branch_title = $row->branch_title;
                
                //echo "emial".$email_id;
                
                $this->data['all_mode_payment'] = $this->Salestest_model->day_close_all_mode_pdf($branch);
    	        $this->data['all_edit_bills'] = $this->Salestest_model->get_edit_billdetails($branch);
    	        $this->data['credit_bills'] = $this->Salestest_model->get_credits_billsdetails($branch);
    	        $this->data['nc_bills'] = $this->Salestest_model->get_nc_billsdetails($branch);
    	        $this->data['cancelled_bills'] = $this->Salestest_model->get_cancelled_billsdetails($branch);
    	        $this->data['discount_bills'] = $this->Salestest_model->get_discount_billsdetails($branch);
    	        $this->data['bill_range'] = $this->Salestest_model->get_bill_ids($branch);
    	        $this->data['nc_details'] = $this->Salestest_model->nc_crdit_pdf($branch);
    	         $this->data['credit_details'] = $this->Salestest_model->crdit_pdf_details($branch);
    	        $this->data['vocher_entry'] = $this->Salestest_model->vocher_amount($branch);
    	        $this->data['customer_data'] = $this->Salestest_model->customer_data_collection($branch);
    	        $this->data['urbanpiper_data'] = $this->Salestest_model->get_urbanpipersales($branch);
                        
        		$this->load->library('html2pdf');
                $filename = 'DayReceipt'.'.pdf';
        		$this->html2pdf->folder('./assets/pdfs/');
        		$this->html2pdf->filename($filename);
        		$this->html2pdf->paper('a4', 'portrait');	
        		$this->html2pdf->html($this->load->view('day_receipt', $this->data, true));	
        		//$this->html2pdf->create('download');
        	    if($path = $this->html2pdf->create('save')) 
        	    {
        	        //PDF was successfully saved or downloaded
            		$this->load->library('email');
            		$this->email->initialize();
            		$this->email->from('info@gsrgroup.biz');
            		$this->email->to($email_id);
            		//$this->email->to('hanumantha@gravitysoft.in');
            		$this->email->cc('info@gsrgroup.biz');
            		//$this->email->to('akshatha0001@gmail.com','Cakewla');
            		//$this->email->to('navyashetty@gravitysoft.in');
            		$this->email->subject('DAY CLOSE REPORT : '.$branch.' : '.$cur_date);
            		$content = "Dear Sir: \r\n Please find below day end sales report \r\n  \r\n ";
            		$this->email->message($content);
            		$this->email->attach($path);
            		$this->email->send();
                    $this->email->print_debugger();			
        	    }
        	    // SMS Triggers
        	    $data = array();
    		    $data1 = array();
    			$data3 = array();
    			$data4 = array();
    			$data5 = array();
    			$tot_sales = 0;
    		    $total_urbans=0;
    		    $mode_other = "";
 
    		    // Sales Details - not nc and credit and cancelled
    		    $all_pay = $this->Salestest_model->all_modes_day_new($branch); 
    		    $cash =0;
    		    $card =0;
    		    $paytm =0;
    		    $swiggy =0;
    		    $zomato =0;
    		    $pay_nc =0;
    		    $pay_credit =0;
    		    $total =0;
    		    foreach ($all_pay as $pay) 
    			{  
    			    $cash = round($pay->cash_amt);
    			    $card = round($pay->cart_amt);
    			    $paytm = round($pay->ptm_amt);
    			    $swiggy = round($pay->swiggy_amt);
    			    $zomato = round($pay->zomato_amt);
    			    $total = round($pay->total_amount);
    			    $pay_nc = round($pay->nc_amt);
    			    $pay_credit = round($pay->credit_amt);
    			}
    			
        			
    			// current day max and min invoice numbers.
    			$dayclose = $this->Salestest_model->max_min_bills($branch); 
    			foreach ($dayclose as $day) 
    			{  
    			   //$ids ="Starting Bill No : ".$day->min." and End No : ".$day->max;
    			   $ids ="Starting Bill No : ".$day->min." and End Bill No : ".$day->max;
    			    $data1[] = (
    			        $ids
    			     );
    			}
    			$bills = implode(', ', $data1);
        			
    			// Current vocher details
    			$mode_vocher =0;
    			$vocher = $this->Salestest_model->vocher_sales_sms($branch);
    			foreach ($vocher as $voche) 
    			{  
    			    $mode_vocher = round($voche->vocher_amt);
    			}
    			$message = urlencode("CWSALE $branch : Today's $cur_date closing day sales is $total. CARD : $card, CASH : $cash, PAYTM : $paytm, SWIGGY : $swiggy, ZOMATO : $zomato.=============================NC : $pay_nc, CREDIT : $pay_credit, Voucher : $mode_vocher, $bills.");
    			//$message = urlencode("$branch : Today's $cur_date closing day sales is $total. CARD : $card,CASH : $cash,PAYTM : $paytm,SWIGGY : $swiggy,ZOMATO : $zomato. =============================NC : $pay_nc, CREDIT : $pay_credit, Vocher : $mode_vocher, $bills.");
                // now live 
                //$message = urlencode("$branch : Today's $cur_date closing day sales is $tot_sales. $mode. ========================= $paymodes, $vochermode, $bills");
                $url="https://portal.mobtexting.com/api/v2/sms/send?access_token=".$authKey."&to=".$mobile."&sender=".$senderId."&template_id=".$templateId."&message=".$message;
                //$url="https://google.com";
                //echo $url;
                $ch = curl_init();                       // initialize CURL
                curl_setopt($ch, CURLOPT_POST, false);    // Set CURL Post Data
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $output = curl_exec($ch);
                curl_close($ch); 
            }
		}
		redirect('https://gsrgroup.biz/SalesMaster/index.php/User_section/dayendsalesupdate/');
	}
	
	// only mail pdf details 
	public function sales_details_pdf($branch)
	{
		$cur_date = date("d-m-Y");
	    $q=$this->db2->query("SELECT api_key,sender_id,mobile_no,template_id,email_id,adminstrative_email,branch_title FROM sms_authorize WHERE branch='$branch' and message_type_id='1'");
		if ($q->num_rows() > 0) 
		{
		    foreach($q->result() as $row)
            {
                 $email_id = $row->email_id;
                //$email_id = "r.adiga@gravitysoft.in,hanumantha@gravitysoft.in,madwesh@gravitysoft.in";
                //$email_id = "jagdish@cakewala.in,r.adiga@gravitysoft.in,suhas@cakewala.in,hanumantha@gravitysoft.in";
                $this->data['all_mode_payment'] = $this->Salestest_model->day_close_all_mode_pdf($branch);
    	        $this->data['all_edit_bills'] = $this->Salestest_model->get_edit_billdetails($branch);
    	        $this->data['credit_bills'] = $this->Salestest_model->get_credits_billsdetails($branch);
    	        $this->data['nc_bills'] = $this->Salestest_model->get_nc_billsdetails($branch);
    	        $this->data['cancelled_bills'] = $this->Salestest_model->get_cancelled_billsdetails($branch);
    	        $this->data['discount_bills'] = $this->Salestest_model->get_discount_billsdetails($branch);
    	        $this->data['bill_range'] = $this->Salestest_model->get_bill_ids($branch);
    	        $this->data['nc_details'] = $this->Salestest_model->nc_crdit_pdf($branch);
    	         $this->data['credit_details'] = $this->Salestest_model->crdit_pdf_details($branch);
    	        $this->data['vocher_entry'] = $this->Salestest_model->vocher_amount($branch);
    	        $this->data['customer_data'] = $this->Salestest_model->customer_data_collection($branch);
    	        $this->data['urbanpiper_data'] = $this->Salestest_model->get_urbanpipersales($branch);

        		$this->load->library('html2pdf');
                $filename = 'DayReceipt'.'.pdf';
        		$this->html2pdf->folder('./assets/pdfs/');
        		$this->html2pdf->filename($filename);
        		$this->html2pdf->paper('a4', 'portrait');	
        		$this->html2pdf->html($this->load->view('day_receipt', $this->data, true));	
        	    if($path = $this->html2pdf->create('save')) 
        	    {
            		$this->load->library('email');
            		$this->email->initialize();
            		$this->email->from('info@gsrgroup.biz');
            		$this->email->to($email_id);
            		$this->email->cc('info@gsrgroup.biz');
            		$this->email->subject('DAY CLOSE REPORT : '.$branch.' : '.$cur_date);
            		$content = "Dear Sir: \r\n Please find below day end sales report \r\n  \r\n ";
            		$this->email->message($content);
            		$this->email->attach($path);
            		$this->email->send();
                    $this->email->print_debugger();			
        	    }
            }
		}
	}
	
	// shastry school mail 
	public function send_contact_mail()
	{
	    header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
        $row = json_decode(file_get_contents('php://input'),true);
        $form_name = $row['name'];
        $form_email = $row['email'];
        $form_phone = $row['mobile'];
        $school_name = $row['school'];
        $form_message = $row['message'];
		$form_subject = "Online Enquiry Details";
		$admin_email = "shastryhunsur@gmail.com";

    	$this->load->library('email');
		$this->email->initialize();
		$this->email->from($form_email,$form_name);
		$this->email->to($admin_email);
		$this->email->subject($form_subject);
		$content = "Name :".$form_name."\n Email :".$form_email."\n Phone Number :".$form_phone."\n School name :".$school_name."\n Message".$form_message;
		$this->email->message($content);
		$this->email->send();
		
		//Send mail 
         if (!$this->email->send()) 
         {
             $this->load->library('email');
    		$this->email->initialize();
    		$this->email->from($admin_email);
    		$this->email->to($form_email);
    		$this->email->subject($form_subject);
    		$content2 = "Thank you :".$form_name." we will get back to you soon..!";
    		$this->email->message($content2);
    		$this->email->send();
            $data = array(
                "status" => "success",
                "message" => "Message Sent Success"
            );
         } else { 
            $data = array(
                "status" => "success",
                "message" => "Message Sent Failed"
            );
         } 
	}
}
?>