<?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");
	    $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;
                $mobile = "8722808511";
                $authKey = $row->api_key;
                $senderId = $row->sender_id;
                $templateId = $row->template_id;
                $email_id = $row->email_id;
                //$email_id = "hanumantha@gravitysoft.in";
                $admin_email = "hanumantha@gravitysoft.in";
               // $admin_email = $row->adminstrative_email;
                $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->from($admin_email,$branch_title, '-', $branch);
            		$this->email->to($email_id);
            		//$this->email->to('hanumantha@gravitysoft.in','Cakewala');
            		$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($branch); 
    		    foreach ($all_pay as $pay) 
    			{  
    			    $tot_sales = $tot_sales + $pay->total_amount;
    			    $mode_of_payments = $pay->payment_types.":".$pay->total_amount;
    			    $data[] = (
    			        $mode_of_payments
    			        );
    			}
    			$mode = implode(', ', $data);
        			
    			// 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;
    			    $data1[] = (
    			        $ids
    			     );
    			}
    			$bills = implode(', ', $data1);
        			
    			// Only NC and Credit  and not cancelled
    			$otherpay = $this->Salestest_model->othersmodedetails($branch); 
    			foreach ($otherpay as $other) 
    			{  
    			    $mode_other = $other->payment_types.":".$other->total_amount;
    			    $data3[] = (
    			        $mode_other
    			     );
    			}
    			$paymodes = implode(', ', $data3);
    			
    			// Current vocher details
    			$vocher = $this->Salestest_model->vocher_sales_sms($branch);
    			foreach ($vocher as $voche) 
    			{  
    			    $mode_vocher = "Vocher :".$voche->vocher_amt;
    			    $data4[] = (
    			        $mode_vocher
    			     );
    			}
    			$vochermode = implode(', ', $data4);
    			
    			// after urbanpiper live 
                	$urban = $this->Salestest_model->get_urbanpipersales($branch);
                	foreach ($urban as $urb) 
                	{  
                	    $total_urbans = $total_urbans + $urb->total_amount;
                		$mode_of_urbans = $urb->payment_types.":".$urb->total_amount;
                		$data5[] = (
                			    $mode_of_urbans
                		);
                	}
                	$urbapipermode = implode(', ', $data5);
    			
    			$final_sales = $tot_sales + $total_urbans;
    			$message = urlencode("{{1}} : Today's {{2}} closing day sales is {{3}}. CARD : {{4}},CASH : {{5}},PAYTM : {{6}},SWIGGY : {{7}},ZOMATO : {{8}}. =============================, Vocher : {{9}}, Starting Bill No : {{10}} and End No : {{11}}.");
                // 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,madwesh@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();			
        	    }
            }
		}
	}
}
?>