<?php

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

class Customer_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('customer_model');
        $this->load->model('visits_model');
        $this->load->model('colors_model');
        $this->load->model('service_model');
        $this->load->model('eyelashes_model');
        $this->load->model('invoice_model');
        $this->load->model('business_model');
    }
    
    
    
    function sendEmail(){
        $customerid = $this->input->post("customerid", TRUE);
        $result = $this->customer_model->get_byid($customerid);
        if(!empty($result[0]['customer_email'])){
            echo $result[0]['customer_email'];
        } else{
            echo 'empty';
        }
    }
    
    function updateCustomerEmail($customerid, $email){
        
        $this->customer_model->update_customer_info($customerid, array('customer_email' => $email));
    }
    
    function sendEmailDo(){
        
        $customerid = $this->input->post("customerid", TRUE);
        $email = $this->input->post("email", TRUE);
        $invoiceid = $this->input->post("invoiceid", TRUE);
        $default = $this->input->post("senddefault", TRUE);

        if($default === 'yes'){
            $this->updateCustomerEmail($customerid, $email);
        }
        
        $business = $this->business_model->getbusinessdetails();
        $body = $this->openinvoiceemailtemplate($invoiceid);

        $this->load->library('email');
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'mail.mexyon.net';
        $config['smtp_user'] = $business[0]['business_email'];
        $config['smtp_pass'] = $business[0]['business_email_password'];
        $config['smtp_port'] = 25;
        $config['mailtype'] = 'html';
        $config['charset'] = 'iso-8859-1';
        $config['wordwrap'] = TRUE;

        $this->email->initialize($config);
        $this->email->from($business[0]['business_email'], $business[0]['business_name']);
        $this->email->to($email);
        $this->email->subject('Invoice Receipt');
        $this->email->message($body);
        $this->email->send();
        
    }
    
    function openinvoiceemailtemplate($invoiceid){
        
        $data['invoice']=$this->invoice_model->getinvoicebyid($invoiceid);
        
        if($data['invoice'][0]['reference_invoice_number'] !== ''){
            $invoiceno = $data['invoice'][0]['reference_invoice_number'];
            $invoice=$this->invoice_model->getinvoicebyno($invoiceno);
            $invoiceid = $invoice[0]['id_invoice'];
        }
        
        $data['invoicedetails'] = $this->invoice_model->getinvoicedetails($invoiceid);
        $data['business'] = $this->business_model->getbusinessdetails();
        $data['taxes']=$this->invoice_model->getinvocietaxes($invoiceid);
        
        $html = $this->load->view('exist_invoice_email_template', $data, TRUE);
        return $html;
    }

    public function search() {
        //get the posted values
        
        $customername = $this->input->post("customersearch", TRUE);
        $customername = $this->input->get("customername", TRUE);
        
        $data = $this->customer_model->get_customer($customername);

        echo(json_encode($data));
        //return  $data['customer'];
        //$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid username and password!</div>');
    }
    
    public function searchwithcell() {
        
         $customername = $this->input->post("customersearch", TRUE);
        $customername = $this->input->get("customername", TRUE);
        
        $data = $this->customer_model->get_customer_withcell($customername);

        echo(json_encode($data));
        
    }

    public function searchname() {
        //get the posted values
        $customername = $this->input->post("customername", TRUE);
        
        $data = $this->customer_model->get_byname($customername);

        echo(json_encode($data));
    }
    
    public function searchnameforco() {
        //get the posted values
        $customername = $this->input->get("customername", TRUE);
        $data = $this->customer_model->get_bynameco($customername);

               echo(json_encode($data));
    }

    public function searchcell() {
        //get the posted values
        $customercell = $this->input->post("customercell", TRUE);

        $data = $this->customer_model->get_bycell($customercell);

               echo(json_encode($data));
    }
    
    public function searchcard() {
        //get the posted values
        $customercard = $this->input->post("customercard", TRUE);

        $data = $this->customer_model->get_bycard($customercard);

               echo(json_encode($data));
    }

    public function searchemail() {
        //get the posted values
        $customeremail = $this->input->post("customeremail", TRUE);

        $data = $this->customer_model->get_byemail($customeremail);

               echo(json_encode($data));
    }

    public function searchid() {
        //get the posted values
        $customerid = $this->input->post("id", TRUE);
        $data = $this->customer_model->get_byid($customerid);

            
        echo(json_encode($data));
    }

    public function addnew() {
        
        
        $customerid = $this->input->post('txt-customer-id', TRUE);
        if ($customerid == "") {
            $result = $this->customer_model->add_new_customer();
            echo "success|" . $result;
        } else {
            echo "error|Something went wrong! you initiated a new customer input but I already have an existing customer selected. Please start over.";
        }
    }

    public function updatecustomer() {
        $customerid = $this->input->post('detail-customer-id', TRUE);
        if ($customerid != "") {
            $result = $this->customer_model->update_customer();
            echo "success|" . $result;
        } else {
            echo "error|Something went wrong! you tried updating a customer but I dont have the customer id selected. Please start over.";
        }
    }

    public function getcustomeralerts() {
        
    }

    //Customer List view function....
    public function customer_list() {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'customers';

            //$data['customers'] = $this->customer_model->get_customers();
            
            $this->load->model('staff_model');
            $data['staff_list']=$this->staff_model->get_active_staff();
            //$data['customers'] = $this->customer_model->general_search('', '', '');
            $this->load->view('includes/header', $data);
            $this->load->view('setting/customer_list_view');
            $this->load->view('includes/footer');
        }
    }

    function getCustomerBirthdays() {
        $data['today_birthdays'] = $this->customer_model->get_customers_birthday_today();
        $data['tomorrow_birthdays'] = $this->customer_model->get_customers_birthday_tomorrow();
              
        echo json_encode($data);
    }

    //Customer Add by modal function....in list view file
    public function add_customer() {
        $result = $this->customer_model->add_new_customer_by_list();
        echo('success|' . $result);
    }

    //Customer Update by modal function....in list view file
    public function update_customer() {

        $result = $this->customer_model->update_customer_by_list();
        echo('success|' . $result);
    }

    public function customer_history($idcustomer = NULL) {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            if ($idcustomer && $idcustomer != NULL) {
                $data["customer_history"] = $this->customer_model->customer_history($idcustomer);
                $data["customer"] = $this->customer_model->get_byid($idcustomer);
                $this->load->view('includes/header');
                $this->load->view('setting/customer_history', $data);
                $this->load->view('includes/footer');
            }
        }
    }

    //Previous visit.....work start............
    //Using views files ---> event_view_modal,customer_previous_visit,visit_js,scheduler_modal
    //Using model files ---> customer_model,visits_model
    public function customer_previous_visit($customerid, $getvisits='No', $getinvoices='No') {//getbusinessdetails
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 1);

            $visit_ids = array();
            $data['menu'] = 'hidden';
            $visitcount=0;
            $invoicecount=0;

            $data['getvisits']=$getvisits;
            $data['getinvoices']=$getinvoices;
            $data['customer'] = $this->customer_model->get_byid($customerid);
            $data['business'] = $this->business_model->getbusinessdetails();
            $data['city_areas'] = $this->customer_model->get_city_areas();
            
            $this->load->model('staff_model');
            $data['staff_list']=$this->staff_model->get_active_staff();

            if($getvisits=="No"){
                $visitcount=$this->customer_model->customer_visit_count($customerid);
            }
            if($getinvoices=="No"){
                $invoicecount=$this->customer_model->customer_invoice_count($customerid);
            }
            if($getvisits=='Yes' || $visitcount->count<=50){
                $data['visits'] = $this->customer_model->customer_all_visits($customerid, 0);

                foreach ($data['visits'] as $visitid) {
                    $visit_ids[] = $visitid['id_customer_visits'];
                }

                $data['visit_services'] = $this->customer_model->customer_visit_services($visit_ids);
                $data['staff'] = $this->customer_model->get_visit_staff($visit_ids);
                $data['product'] = $this->customer_model->get_visit_product($visit_ids);

                $data['novisits'] = "false";

            } else {
                $data['novisits'] = $visitcount->count ." previous visits in the database.";
            }    

            if($getinvoices=='Yes' || $invoicecount->count<=50){    
                $data['invoices'] = $this->customer_model->get_invoice_visit($customerid, 0);
                $data['noinvoices']= "false";
            } else{
                //$data['invoices'] = $this->customer_model->get_invoice_visit($customerid, 10);
                $data['noinvoices'] = $invoicecount->count ." previous invoices in the database.";
            }
            $data['color_records'] = $this->colors_model->get_customer_color_records($customerid);
            $data['facial_records'] = $this->service_model->get_customer_facial_records($customerid);
            $data['eyelashes'] = $this->eyelashes_model->get_customer_eyelashes_records($customerid);

            $data['customerbalance'] = $this->customer_model->customerbalance($customerid);

            $data['customer_loyalty'] = $this->customer_model->get_customer_loyalty($customerid);
            $data['other_retained'] = $this->customer_model->get_other_branch_retained($customerid);
            
            
            $data['open_visits'] = $this->customer_model->get_customer_openvisits($customerid);

            //echo "<pre>";print_r($data['staff']);echo "</pre>";die;
            $this->load->view('includes/header', $data);
            $this->load->view('customer_previous_visit');
            $this->load->view('includes/footer');
        }
    }

    //Previous visit.....work end............

    public function customer_balance() {
        if ($this->input->post('id', TRUE)) {
            $customerbalance = $this->customer_model->customerbalance($this->input->post('id', TRUE));
            
            echo (json_encode($customerbalance));
            die;
        }
    }
    
    public function customer_enrollment(){
        if ($this->input->post('id', TRUE)) {
            $enrollment = $this->customer_model->customerenrollment($this->input->post('id', TRUE));
            
            echo (json_encode($enrollment));
            die;
        }
        
        
    }
    
    public function customer_exist() {
        $customername = $this->input->post('customer_name', TRUE);
        $customercell = $this->input->post('customer_cell', TRUE);
        $customerexist = $this->customer_model->customer_exist($customername, $customercell);
        echo $customerexist;
        die;
    }
    
    public function customer_update_exist() {
        $customername = $this->input->post('customer_name', TRUE);
        $customercell = $this->input->post('customer_cell', TRUE);
        $customer_id = $this->input->post('customer_id', TRUE);
        $customerexist = $this->customer_model->customer_update_exist($customer_id, $customername, $customercell);
        echo $customerexist;
        die;
    }
    
    public function edit_customers(){
        $data = $this->customer_model->edit_customers();
        echo (json_encode($data));
        die;
    }
    
    function CheckCustomer_Exist(){
        $customername = $this->input->post('customer_name', TRUE);
        $customercell = $this->input->post('customer_cell', TRUE);
        $customerexist = $this->customer_model->CheckCustomer_Exist($customername, $customercell);
        echo $customerexist;
        die;
    }

    function generalsearch(){
        
        $customername = $this->input->post('customername', TRUE);
        $customercell = $this->input->post('customercell', TRUE);
        $customercard = $this->input->post('customercard', TRUE);
        if($customername !='' || $customercell!='' || $customercard!=''){
            $data = $this->customer_model->general_search($customername, $customercell, $customercard);
        }else{
            $data=[];
        }
//        $data=[];
//        foreach($customer as $d){
//            $loyalty = $this->customer_model->get_customer_loyalty($d['id_customers']);
//            $merged_array = array_push($d, $loyalty);
//            
//            $balance = $this->customer_model->customerbalance($d['id_customers']);
//            //$merged_array1 = array_push($merged_array, $balance);
//        
//            array_push($data, $merged_array);
//        }
        
        
        echo(json_encode($data));
        
    }
    
    public function mark_inservice(){
        $visit_id=$this->input->post('visit_id');
        $data = $this->visits_model->mark_inservice($visit_id);
        echo (json_encode($data));
    }
    
    
    function open_merge_customer(){
         if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            //checkroles(0, 0, 0);

            $data['nav'] = 'customer_merge';
            $data['subnav'] = 'customer_merge';
        
        
            $this->load->view('includes/header');
            $this->load->view('setting/customer_merge');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function get_customer_counts(){
        
        $customerid= $this->input->post('customer_id');
        
        $query = $this->db->query("select count(*) as mCount from invoice where customer_id = ".$customerid);
        $data['invoices'] =$query->row();
        
        $query = $this->db->query("select count(*) as mCount from customer_visits where customer_id = ".$customerid);
        $data['visits'] =$query->row();
        
        $query = $this->db->query("select count(*) as mCount from account_vouchers where business_partner_id = ".$customerid." and business_partner=1");
        $data['account_vouchers'] =$query->row();
        
        $query = $this->db->query("select count(*) as mCount from order_vouchers where customer_id=".$customerid);
        $data['order_vouchers'] = $query->row();
        
        $query = $this->db->query("select count(*) as mCount from customer_orders where customer_id=".$customerid);
        $data['customer_orders'] = $query->row();
        
        $query = $this->db->query("select count(*) as mCount  from color_records  where customer_id=".$customerid);
        $data['color_records'] = $query->row();
        
        $query = $this->db->query("select count(*) as mCount  from eyelashes_record  where customer_id=".$customerid);
        $data['eyelashes_record'] = $query->row();
        
        $query = $this->db->query("select count(*) as mCount  from facial_records  where customer_id=".$customerid);
        $data['facial_records'] = $query->row();
        
        echo (json_encode($data));
        
    }
    
    public function merge_customer_data(){
        
        $id_customer_keep = $this->input->post('id_customer_keep');
        $id_customer_remove = $this->input->post('id_customer_remove');
        
        if($id_customer_remove !=null && $id_customer_remove!=''){
            if($id_customer_remove !=null && $id_customer_remove!=''){
                //Invoices
                //$sql = "update invoice set customer_id = ".$id_customer_keep."where customer_id=".$id_customer_remove;
                $this->db->set("customer_id", $id_customer_keep);
                $this->db->where("customer_id", $id_customer_remove);
                $this->db->update("invoice");               
                
                //visits
                //$sql = "update customer_visits set customer_id = ".$id_customer_keep."where customer_id=".$id_customer_remove;
                $this->db->set("customer_id", $id_customer_keep);
                $this->db->where("customer_id", $id_customer_remove);
                $this->db->update("customer_visits");
                
                //accont vouchers
                $sql = "update account_vouchers set business_partner_id = ".$id_customer_keep." where business_partner_id = ".$id_customer_remove." and business_partner=1";
                $this->db->set("business_partner_id", $id_customer_keep);
                $this->db->where("business_partner_id", $id_customer_remove);
                $this->db->where("business_partner", 1);
                $this->db->update("account_vouchers");
                
                //gift vouchers
                //$sql = "update order_vouchers set customer_id = " . $id_customer_keep . " where customer_id= ".$id_customer_remove ;
                $this->db->set("customer_id", $id_customer_keep);
                $this->db->where("customer_id", $id_customer_remove);
                $this->db->update("order_vouchers");            
                
                
                //customer_orders
               // $sql = "update customer_orders set customer_id = " . $id_customer_keep . " where customer_id= ".$id_customer_remove;
                $this->db->set("customer_id", $id_customer_keep);
                $this->db->where("customer_id", $id_customer_remove);
                $this->db->update("customer_orders"); 
                
                //color records
                //$sql = "update color_records set customer_id = " . $id_customer_keep . " where customer_id= ".$id_customer_remove;
                $this->db->set("customer_id", $id_customer_keep);
                $this->db->where("customer_id", $id_customer_remove);
                $this->db->update("color_records"); 
                
                //eyelashes_record
                //$sql = "update eyelashes_record set customer_id = " . $id_customer_keep . " where customer_id= ".$id_customer_remove;
                $this->db->set("customer_id", $id_customer_keep);
                $this->db->where("customer_id", $id_customer_remove);
                $this->db->update("eyelashes_record");
                
                //facial records 
                //$sql = "update facial_records set customer_id = " . $id_customer_keep . " where customer_id= ".$id_customer_remove;
                $this->db->set("customer_id", $id_customer_keep);
                $this->db->where("customer_id", $id_customer_remove);
                $this->db->update("facial_records");
                
                
                //customer backup
                if ($this->db->table_exists('customers_backup'))
                {
                  // table exists
                    $sql = "insert into customers_backup select * from customers where id_customers = ".$id_customer_remove;
                    $this->db->query($sql);
                    //customer remove
                    
                    $this->db->where("id_customers", $id_customer_remove);   
                    $this->db->delete("customers");
                }
                else
                {
                    $sql = "create table customers_backup as select * from customers where id_customers = ".$id_customer_remove;
                    $this->db->query($sql);
                    
                    //customer remove
                    $this->db->where("id_customers", $id_customer_remove);   
                    $this->db->delete("customers");
                }
                echo "success";
            }           
        }
    }

    public function add_ext_program(){

        $ext_id = $this->input->post('external_id');
        $id_customer = $this->input->post('customer_id');

        //print_r($id_customer); exit();

        $this->db->set('fcm_id', $this->input->post('external_id'));
        $this->db->where('id_customers', $id_customer);
        $this->db->update('customers');

        echo "success|".$id_customer;

    }
}
