<?php

class Marketing_model extends CI_Model {

    function __construct() {

        // Call the Model constructor
        parent::__construct();
    }

    function getmarketingsheet($today) {

        //$today = date('Y-m-d');
        //$tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));

        $this->db->select("invoice.payment_mode, id_invoice, invoice_type, customer_name, invoice_products.staff_name as staff, 
        reference_invoice_number,
        concat(invoice_details.service_type, ' ', invoice_details.service_category, ' ', invoice_details.service_name) as service, 
        invoice.discount as 'invoice_discount',
        paid_amount, total_payable as gross_amount, retained_amount, invoice.balance, invoice.advance_amount, cctip, tax_total, cc_charge,
        invoice_staff.price, invoice_staff.discount, paid_amount, paid_cash, paid_card, paid_voucher, paid_check, 
        concat(invoice_products.brand_name, ' ', invoice_products.product_name, ' x ', invoice_products.invoice_qty) product_name, 
        invoice_qty, invoice_products.price product_price, invoice_products.staff_name product_staff, 
        invoice_products.discount product_discount,
        DATE_FORMAT(invoice_date, '%d-%c-%Y') as invoice_date, DATE_FORMAT(visit_time, '%H:%i') as visit_time, DATE_FORMAT(visit_time, '%d-%m-%Y') as visit_date, retained_amount_used", False);
        //$this->db->join('invoice_details', 'invoice_details.invoice_id = invoice.id_invoice', 'left', false);
        $this->db->join('invoice_details', 'invoice_details.invoice_id = invoice.id_invoice', 'left', false);
        $this->db->join('invoice_staff', 'invoice_staff.invoice_detail_id = invoice_details.id_invoice_details and invoice_staff.invoice_id=invoice.id_invoice', 'left', false);
        $this->db->join('invoice_products', 'invoice_products.invoice_id = invoice.id_invoice', 'left', false);
        $this->db->where('invoice.business_id', $this->session->userdata('businessid'));
        $this->db->where('invoice_status', 'valid');

        $this->db->where('date_format(invoice_date , "%Y-%m") =', $today);

        //$this->db->where('reference_invoice_number !=', 'bad debts');
        $query = $this->db->get('invoice');
        //echo $query; exit();

        return $query->result_array();
    }

    function getservicedayinvoices($today) {

        //$today = date('Y-m-d');
        //$tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));

//        round((invoice_details.price-invoice_details.discount)*((100*invoice.discount)/invoice.sub_total)/100,2) invoice_discount,
//        round(invoice_details.discount+((invoice_details.price-invoice_details.discount)*((100*invoice.discount)/invoice.sub_total)/100),2) as total_discount,
//        round(invoice_details.price-(invoice_details.discount+((invoice_details.price-invoice_details.discount)*((100*invoice.discount)/invoice.sub_total)/100) ),2) as discounted_price,

        
        
        $this->db->select("id_invoice, invoice_type, customer_name, invoice_staff.staff_name as staff, staff_id, 
        reference_invoice_number,
        concat(invoice_details.service_type, ' ', invoice_details.service_category, ' ', invoice_details.service_name) as service,         
        invoice.payment_mode,
        total_payable as gross_amount, 
        paid_earlier,
        retained_amount, 
        invoice.balance, invoice.advance_amount, cctip, tax_total, cc_charge,
        paid_amount, paid_cash, paid_card, paid_voucher, paid_check, 
        (100*invoice.advance_amount)/invoice.sub_total as invoice_advance_percentage,         
        (100*invoice.discount)/invoice.sub_total as invoice_discount_percentage,
        invoice_details.id_invoice_details, 
        invoice_staff.price, 
        invoice_staff.paid as paid_details, 
        invoice_staff.discount as service_discount,
        invoice_details.discount_type,
        invoice_staff.service_addition,
        invoice_staff.invoice_discount,
        invoice_staff.discount + invoice_staff.invoice_discount as total_discount,
        invoice_staff.final_price as discounted_price,
        
        '' as product_name, 
        '' as invoice_qty, '' as product_price, '' as product_staff, 
        '' as product_discount, additional_staff,
        DATE_FORMAT(invoice_date, '%d-%c-%Y') as invoice_date, DATE_FORMAT(visit_time, '%H:%i') as visit_time, DATE_FORMAT(visit_time, '%d-%m-%Y') as visit_date, retained_amount_used", False);
        $this->db->join('invoice_details', 'invoice_details.invoice_id = invoice.id_invoice',  false);
        $this->db->join('invoice_staff', 'invoice_staff.invoice_detail_id = invoice_details.id_invoice_details ', false);
        //$this->db->join('(select invoice_id, count(id_invoice_details) service_count from invoice_details
	//group by invoice_id) as a', 'a.invoice_id = invoice.id_invoice', false);
        $this->db->where('invoice.business_id', $this->session->userdata('businessid'));
        $this->db->where('invoice_status', 'valid');
        $this->db->where('invoice_type', 'service');
        //$this->db->where('reference_invoice_number', '');
        $this->db->where('date_format(invoice_date , "%Y-%m") =', $today);
        //$this->db->where('reference_invoice_number !=', 'bad debts');
        
        $this->db->order_by('staff_id');
        $this->db->order_by('id_invoice');
        $this->db->order_by('id_invoice_details');
        $query = $this->db->get('invoice');
        //echo $query; exit();

        return $query->result_array();
    }

    function getproductdayinvoices($today) {

        //$today = date('Y-m-d');
        //$tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));

        
//        round(((invoice_products.price*invoice_products.invoice_qty)-invoice_products.discount)*((100*invoice.discount)/invoice.sub_total)/100,2) as invoice_discount,
//        invoice_products.discount  + round(((invoice_products.price*invoice_products.invoice_qty)-invoice_products.discount)*((100*invoice.discount)/invoice.sub_total)/100,2) as total_discount,
//        (round(invoice_products.price*invoice_products.invoice_qty,2) - invoice_products.discount)  - round(((invoice_products.price*invoice_products.invoice_qty)-invoice_products.discount)*((100*invoice.discount)/invoice.sub_total)/100,2) as discounted_price,
//        
        $this->db->select("id_invoice, invoice_type, customer_name, ifnull(invoice_products.staff_name,'N/A') as staff, 
            ifnull(staff_id,0) staff_id, reference_invoice_number,
       '' as service, 
        invoice.payment_mode,
        paid_earlier,
        paid_amount, total_payable as gross_amount, retained_amount, invoice.balance, invoice.advance_amount, 
        cctip, tax_total, cc_charge,
        paid_amount, paid_cash, paid_card, paid_voucher, paid_check,         
        '' id_invoice_details, 
        invoice_products.price, 
        invoice_products.final_price as discounted_price, 
        invoice_products.paid as paid_details, 
        invoice_products.discount, 
        invoice_products.invoice_discount,
        invoice_products.discount as product_discount,
        invoice_products.invoice_discount + invoice_products.discount as total_discount,
        (100*invoice.advance_amount)/invoice.sub_total as invoice_advance_percentage,
        (100*invoice.discount)/invoice.sub_total as invoice_discount_percentage,
        invoice_products.service_addition,        
        invoice_products.invoice_qty,
        concat(invoice_products.brand_name, ' ', invoice_products.product_name, ' x ', invoice_products.invoice_qty) product_name, 
        invoice_qty, invoice_products.price product_price, invoice_products.staff_name product_staff,         
        DATE_FORMAT(invoice_date, '%d-%c-%Y') as invoice_date, DATE_FORMAT(visit_time, '%H:%i') as visit_time, 
        DATE_FORMAT(visit_time, '%d-%m-%Y') as visit_date, retained_amount_used", False);
        $this->db->join('invoice_products', 'invoice_products.invoice_id = invoice.id_invoice', false);
        
        
        $this->db->where('invoice.business_id', $this->session->userdata('businessid'));
        $this->db->where('invoice_status', 'valid');
        $this->db->where('invoice_type', 'sale');
        //$this->db->where('reference_invoice_number', '');
        $this->db->where('date_format(invoice_date , "%Y-%m") =', $today);
        //$this->db->where('reference_invoice_number !=', 'bad debts');
        $this->db->order_by('staff_id');
        $this->db->order_by('id_invoice');
        $this->db->order_by('id_invoice_products');
        $query = $this->db->get('invoice');
        
        //echo $query; exit();

        return $query->result_array();
    }

    //Currently this query is not using
    function getmarketingsummary2($today) {
        //$today = date('Y-m-d');
        //$tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));

        $this->db->select("invoice.payment_mode, id_invoice, invoice_type, customer_name, invoice_staff.staff_name as staff, 
        reference_invoice_number,
        concat(invoice_details.service_type, ' ', invoice_details.service_category, ' ', invoice_details.service_name) as service, 
        invoice.discount as 'invoice_discount',
        paid_amount, total_payable as gross_amount, retained_amount, invoice.balance, invoice.advance_amount, cctip, tax_total, cc_charge,
        sum(invoice_staff.price) as price, sum(invoice_staff.discount) as discount, paid_amount, paid_cash, paid_card, paid_voucher, paid_check, 
        concat(invoice_products.brand_name, ' ', invoice_products.product_name, ' x ', invoice_products.invoice_qty) product_name, 
        invoice_qty, 
        sum(invoice_products.price) as product_price, 
        invoice_products.staff_name product_staff, 
        sum(invoice_products.discount) as product_discount,        
        DATE_FORMAT(invoice_date, '%d-%c-%Y') as invoice_date, DATE_FORMAT(visit_time, '%H:%i') as visit_time, DATE_FORMAT(visit_time, '%d-%m-%Y') as visit_date, retained_amount_used", False);
        //$this->db->join('invoice_details', 'invoice_details.invoice_id = invoice.id_invoice', 'left', false);
        $this->db->join('invoice_details', 'invoice_details.invoice_id = invoice.id_invoice', 'left', false);
        $this->db->join('invoice_staff', 'invoice_staff.invoice_detail_id = invoice_details.id_invoice_details and invoice_staff.invoice_id=invoice.id_invoice', 'left', false);
        $this->db->join('invoice_products', 'invoice_products.invoice_id = invoice.id_invoice', 'left', false);
        $this->db->where('invoice.business_id', $this->session->userdata('businessid'));
        $this->db->where('invoice_status', 'valid');

        $this->db->where('date_format(invoice_date , "%Y-%m") =', $today);
        $this->db->group_by('invoice_staff.staff_name');
        $this->db->group_by('invoice_products.staff_name');

        //$this->db->where('reference_invoice_number !=', 'bad debts');
        $query = $this->db->get('invoice');
        //echo $query; exit();

        return $query->result_array();
    }

    //This query is using for marketing staff summary
    function getmarketingsummary($date) {
        $service = "(SELECT (SUM(IF(invoice_staff.price,invoice_staff.price,0))-SUM(IF(invoice_staff.discount,invoice_staff.discount,0))) as price FROM invoice 
        JOIN invoice_details ON invoice.id_invoice = invoice_details.invoice_id 
        JOIN invoice_staff ON invoice_details.id_invoice_details = invoice_staff.invoice_detail_id AND invoice_staff.invoice_id = invoice.id_invoice 
        WHERE invoice_staff.staff_name = staff.staff_fullname AND date_format(invoice_date , '%Y-%m') = '$date' AND invoice.invoice_type = 'service' 
        AND invoice.business_id = " . $this->session->userdata('businessid') . " AND invoice.invoice_status = 'valid')";
        $product = "(SELECT (SUM(IF(invoice_products.price,invoice_products.price,0))-SUM(IF(invoice_products.discount,invoice_products.discount,0))) as price FROM invoice 
        JOIN invoice_products ON invoice_products.invoice_id = invoice.id_invoice 
        WHERE invoice_products.staff_name = staff.staff_fullname AND date_format(invoice_date , '%Y-%m') = '$date' AND invoice.invoice_type = 'sale' 
        AND invoice.business_id = " . $this->session->userdata('businessid') . " AND invoice.invoice_status = 'valid')";
        $sql = "SELECT staff.staff_fullname, $service as service_price, $product as product_price FROM staff WHERE staff.staff_active = 'Y'  ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    function get_advance_breakup($t = null) {
        $today = $this->input->post('calendar_date', TRUE);

        if (!isset($today) || empty($today)) {
            if ($t !== null) {
                $today = $t;
            } else {
                $today = date('Y-m-d');
            }
        }
        $sql = "select id_visit_advance, date_format(va.advance_date,'%Y-%m-%d') as advance_date, CONCAT(id_customer_visits,id_visit_advance) id_customer_visits, customer_name, adv_mode advance_mode, adv_amount advance_amount, service_category, business_services.service_name, service_rate
                from customer_visits
                join (
                        select id_visit_advance, visit_advance.advance_date, customer_visit_id, sum(advance_amount) adv_amount, advance_mode adv_mode, advance_inst adv_inst, advance_date adv_date
                        from visit_advance 
                        where date_format(advance_date,'%Y-%m') ='" . $today . "'
                        group by advance_mode, advance_inst, advance_date, id_visit_advance
                    ) va on customer_visits.id_customer_visits = va.customer_visit_id
            join visit_services on customer_visits.id_customer_visits = visit_services.customer_visit_id and visit_services.visit_service_status = 'Active'
            join customers on customers.id_customers = customer_visits.customer_id
            join business_services on business_services.id_business_services = visit_services.service_id
            join service_category on business_services.service_category_id = service_category.id_service_category
            where customer_visits.business_id = " . $this->session->userdata('businessid') . "
            and customer_visits.visit_status != 'canceled' and advance='true' 
            group by CONCAT(id_customer_visits,id_visit_advance), service_category, business_services.service_name, service_rate";
        //echo $sql; exit();
        $query = $this->db->query($sql);

        return $query->result_array();
    }

    function pettycashreport($startdate, $enddate) {
        $this->db->select('*,DATE_FORMAT(av.created_on, "%d-%m-%Y") voucher_date,DATE_FORMAT(av.created_on, "%H:%i:%s") voucher_time');
        $this->db->join('account_voucher_detail avd', 'av.id_account_vouchers = account_voucher_id');
        $this->db->join('account_heads ah', 'ah.id_account_heads = avd.account_head_id');
        $this->db->where('av.business_id', $this->session->userdata('businessid'));
        $this->db->where('av.voucher_date >= ', $startdate);
        $this->db->where('av.voucher_date <= ', $enddate);
        $this->db->where('ah.account_type', 'Expense');
        $this->db->where('ah.account_head', 'office expense');
        $this->db->where('av.voucher_type', 1);
        $this->db->where('av.voucher_status =', 'Active');
        $this->db->order_by('av.created_on', 'desc');
        $query = $this->db->get('account_vouchers av');

        return $query->result_array();
    }

    function retailTransaction($startdate, $enddate) {
        $this->db->select('business_brands.id_business_brands, business_brands.business_brand_name');
        $this->db->where([
            'business_brands.business_id' => $this->session->userdata('businessid'),
            'business_brands.business_brand_active' => 'Yes'
        ]);
        $query = $this->db->get('business_brands');
        $brands = $query->result_array();

        $data = [];
        if (isset($brands) && !empty($brands)) {
            foreach ($brands as $row) {
                $where = [
                    'i.business_id' => $this->session->userdata('businessid'),
                    'i.invoice_date >= ' => $startdate,
                    'i.invoice_date <= ' => $enddate,
                    'i.invoice_status' => 'valid',
                    'i.balance' => 0,
                    'bp.brand_id' => $row['id_business_brands']
                ];
                //$subquery = "select paid from invoice_products join invoice on invoice.id_invoice = invoice_products.invoice_id where invoice.invoice_number = i.invoice_number";
                $this->db->select('DATE_FORMAT(i.invoice_date, "%d-%m-%Y") as invoice_date, ip.id_invoice_products, bp.product as product_name, ip.product_id, ip.price, ip.discount, ip.paid, gd.grn_unit_price as purchase_price, gd.grn_batch_number as batch_number, gd.grn_batch_id as batch_id, bp.measure_unit, bp.qty_per_unit, ip.brand_name, i.reference_invoice_number');
                $this->db->join('invoice_products ip', 'ip.invoice_id = i.id_invoice');
                $this->db->join('business_products bp', 'bp.id_business_products = ip.product_id');
                $this->db->join('grn_details gd', 'gd.grn_product_id = bp.id_business_products AND ip.batch_id = gd.grn_batch_id');
                $this->db->where($where);
                $query = $this->db->get('invoice i');
                $data[] = ['brand_name' => $row['business_brand_name'], 'data' => $query->result_array()];
            }
            return $data;
        }
    }

    function retailTransactionByItem($startdate, $enddate) {
        $this->db->select('business_brands.id_business_brands, business_brands.business_brand_name');
        $this->db->where([
            'business_brands.business_id' => $this->session->userdata('businessid'),
            'business_brands.business_brand_active' => 'Yes'
        ]);
        $query = $this->db->get('business_brands');
        $brands = $query->result_array();

        $data = [];
        if (isset($brands) && !empty($brands)) {
            foreach ($brands as $row) {
                $where = [
                    'i.business_id' => $this->session->userdata('businessid'),
                    'i.invoice_date >= ' => $startdate,
                    'i.invoice_date <= ' => $enddate,
                    'i.invoice_status' => 'valid',
                    'i.balance' => 0,
                    'bp.brand_id' => $row['id_business_brands']
                ];
                //$subquery = "select paid from invoice_products join invoice on invoice.id_invoice = invoice_products.invoice_id where invoice.invoice_number = i.invoice_number";
                $this->db->select('DATE_FORMAT(i.invoice_date, "%d-%m-%Y") as invoice_date, ip.id_invoice_products, bp.product as product_name, bp.barcode_products, COUNT(ip.product_id) as sold, SUM(ip.price) as price, SUM(ip.discount) as discount, SUM(ip.paid) as paid, SUM(gd.grn_unit_price) as purchase_price, gd.grn_batch_number as batch_number, gd.grn_batch_id as batch_id, bp.measure_unit, bp.qty_per_unit, ip.brand_name, i.reference_invoice_number');
                $this->db->join('invoice_products ip', 'ip.invoice_id = i.id_invoice');
                $this->db->join('business_products bp', 'bp.id_business_products = ip.product_id');
                $this->db->join('grn_details gd', 'gd.grn_product_id = bp.id_business_products AND ip.batch_id = gd.grn_batch_id');
                $this->db->where($where);
                $this->db->group_by('ip.product_id');
                $query = $this->db->get('invoice i');
                $data[] = ['brand_name' => $row['business_brand_name'], 'data' => $query->result_array()];
            }
            return $data;
        }
    }

    function getBrands($startdate, $enddate) {
        $this->db->select('*');
        $this->db->join('(select distinct brand_name from invoice_products join invoice i on i.id_invoice = invoice_products.invoice_id where i.business_id = ' . $this->session->userdata('businessid') . ' and i.invoice_status = "valid" and i.invoice_date >= "' . $startdate . '" and i.invoice_date <= "' . $enddate . '") ip', 'ip.brand_name = business_brands.business_brand_name');
        $this->db->where([
            'business_brands.business_id' => $this->session->userdata('businessid'),
            'business_brands.business_brand_active' => 'Yes'
        ]);
        $query = $this->db->get('business_brands');
        return $query->result_array();
    }

    function service_sale_details($startdate, $enddate) {
        //echo $startdate.'---'.$enddate;exit;
        // $date = strtotime("+ 1 days", strtotime($enddate));
        // $enddate = date("Y-m-d", $date);

        $select = "COUNT(invoice_details.service_id) as sold, service_category, invoice_details.service_name, SUM(ifnull(invoice_details.price,0)) as price, SUM(ifnull(invoice_details.discount,0)) as 'service_discount', SUM(ifnull(invoice.balance,0)) as 'balance', SUM(invoice_details.discounted_price) as discounted_price, SUM(invoice_details.paid) as paid";

        $this->db->select($select, FALSE);
        $this->db->join('invoice_details', 'invoice.id_invoice = invoice_details.invoice_id');
        //$this->db->join('(select invoice_id, staff_name, service_name from invoice_staff)  a', 'a.invoice_id=invoice_details.invoice_id and a.service_name collate utf8_general_ci = invoice_details.service_name', FALSE, FALSE);
        $this->db->where('invoice_date >=', $startdate . " 00:00");
        $this->db->where('invoice_date <=', $enddate . " 23:59");
        $this->db->where('invoice_status', 'valid');
        $this->db->where('invoice.business_id', $this->session->userdata('businessid'));
        $this->db->where('invoice_type', 'service');
        //$this->db->where('invoice.balance', 0);

        $this->db->group_by('invoice_details.service_id');
        $this->db->order_by('service_type', 'service_category', 'service_name');

        $query = $this->db->get('invoice');

        return $query->result_array();
    }

    function service_by_staff($startdate, $enddate, $sh=false) {
        $servicesbystaff=[];
        
        $where = "(staff.business_id=". $this->session->userdata('businessid') ." OR staff.staff_shared = 'Yes') ";
        
        $this->db->select('staff.id_staff, staff.staff_fullname');
        $this->db->where($where);
        $this->db->where('staff.staff_active', 'Y');
        $this->db->order_by('staff_fullname');
        $query = $this->db->get('staff');
        
        $staff = $query->result_array(); 
        
       
        foreach($staff as $s){
        $sql="select invoice_staff.staff_id, invoice_staff.staff_name, invoice.customer_name, 
            invoice.customer_id,
               invoice_staff.service_category, invoice_staff.service_name,
               invoice_staff.price, invoice_staff.service_addition,
               invoice_staff.discount + invoice_staff.invoice_discount as 'discount',
               invoice_staff.final_price 'discounted_price', 
               invoice_staff.paid 'staff_paid',
               round(invoice_details.taxes,2) as taxes, 
               invoice_staff.requested, invoice_staff.additional_staff, invoice_staff.invoice_id,
               date_format(invoice_details.invoice_detail_date, '%d-%m-%Y %h:%i') as 'invoice_date',
               invoice_details.service_flag,
               invoice.paid_amount 'total_paid',
               invoice_staff.commission 'commission'
               from invoice_staff
               join invoice_details on invoice_details.id_invoice_details = invoice_staff.invoice_detail_id
               join invoice on invoice.id_invoice = invoice_details.invoice_id
               where invoice.invoice_status='valid'
               and invoice.invoice_date >= '".$startdate." 00:00:00'
               and invoice.invoice_date <= '".$startdate." 23:59:59'
               and invoice.business_id=".$this->session->userdata('businessid')." ";
                if($sh==true ||  $this->session->userdata('role')=="Sh-Users"){
                    $sql.=" and invoice_seq >  0 ";
                } 
                $sql .= "  and staff_id = ".$s['id_staff']."
                       order by invoice_staff.staff_id, invoice_id
                       ";
        
               
//            print_r($sql);
//            exit();
        
            $query = $this->db->query($sql);
            $services = $query->result_array();


            $sql = "SELECT staff_id, staff_name, invoice_id, customer_name, product_name, invoice_qty,
            price, invoice_products.discount, invoice_products.service_addition,
            invoice_products.discounted_price, 
            invoice.discount as invoice_discount, 
            invoice_products.paid,
            invoice_products.taxes,
            date_format(invoice_product_date,'%d-%m-%Y') as invoice_date,
            invoice_products.commission 'commission'
            FROM invoice_products
            join invoice on invoice.id_invoice = invoice_products.invoice_id
            and invoice.invoice_date >= '".$startdate." 00:00:00'
            and invoice.invoice_date <= '".$startdate." 23:59:59'
            and invoice.business_id=".$this->session->userdata('businessid')." 
            and invoice.invoice_status= 'valid'        
                    ";
            if($sh==true ||  $this->session->userdata('role')=="Sh-Users"){
                $sql.=" and invoice_seq >  0 ";
            } 
            $sql .="  and staff_id = ".$s['id_staff']."
            order by invoice_products.staff_id, invoice_products.invoice_id";  

            $query = $this->db->query($sql);
            $products = $query->result_array();
            
            $staff = ['staff_id' => $s['id_staff']];
            $services = ['services' => $services];
            $products = ['products' => $products];
            
            $totaldata = array_merge($staff, $services, $products);
            
            array_push($servicesbystaff, $totaldata);
        }
        
        //print_r($servicesbystaff);
        //exit();
       return $servicesbystaff;
        

    }

    
    function customer_staff_history($customerid, $staffid){
        
        
        $sql = "select *, date_format(invoice_date,'%d-%m-%Y') as invoice_Date from invoice_staff 
                join invoice on invoice.id_invoice = invoice_staff.invoice_id
                where invoice_staff.staff_id = ". $staffid ." and 
                invoice.customer_id = ".$customerid."     
                and date(invoice_date) < date(now())
                and invoice.invoice_status='valid'
                order by id_invoice desc
                 ";
        $query = $this->db->query($sql);
       // echo $sql; exit();
        return $query->result_array();
    }
    
    function daily_sheet_by_category($startdate, $enddate) {

        // $date = strtotime("+ 1 days", strtotime($enddate));
        // $enddate = date("Y-m-d", $date);

        $this->db->select('service_category.id_service_category, service_category.service_category');
        $this->db->where(['service_category.business_id' => $this->session->userdata('businessid'), 'service_category.service_category_active' => 'Yes']);
        $query = $this->db->get('service_category');

        $staff = $query->result_array();
        $data = [];
        if (isset($staff) && !empty($staff)) {
            foreach ($staff as $row) {
                $select = "invoice.payment_mode, id_invoice_details, id_invoice, invoice_details.service_name, 
                date_format(invoice_date, '%d-%m-%Y %H:%i') as 'invoiced', staff_name as 'staff', ifnull(invoice_details.discounted_price,0) as 'discounted_price', ifnull(invoice.discount,0) 'Invoice Discount', 
                invoice_details.price, ifnull(invoice_details.discount,0) as 'Service Discount', 
                invoice_details.paid as 'paid', balance as 'balance', ifnull(reference_invoice_number,'') as reference_number";

                $this->db->select($select, FALSE);
                $this->db->join('invoice_details', 'invoice.id_invoice = invoice_details.invoice_id');
                $this->db->join('(select invoice_id, staff_name, service_name from invoice_staff)  a', 'a.invoice_id=invoice_details.invoice_id and a.service_name collate utf8_general_ci = invoice_details.service_name', FALSE, FALSE);
                $this->db->where('invoice_date >=', $startdate . " 00:00");
                $this->db->where('invoice_date <=', $enddate . " 23:59");
                $this->db->where('invoice_status', 'valid');
                $this->db->where('invoice.business_id', $this->session->userdata('businessid'));
                $this->db->where('invoice_type', 'service');
                $this->db->where('invoice_details.service_category', $row['service_category']);

                $this->db->group_by('id_invoice_details');
                $this->db->order_by('service_type', 'service_category', 'service_name');

                $query = $this->db->get('invoice');

                $result = $query->result_array();
                $data[] = ['service_category' => $row['service_category'], 'data' => $result];
            }
            return $data;
        }
    }
    
    //This query is in development mode for dupilex template
    function getServiceCashDetails($today){
        $this->db->select('invoice.id_invoice,SUM(invoice_details.paid) as paid,invoice_details.service_type,SUM(invoice.paid_cash) as paid_cash,SUM(invoice.paid_card) as paid_card, invoice.payment_mode, SUM(invoice.paid_amount) as paid_amount');
        $this->db->join('invoice_details', 'invoice_details.invoice_id = invoice.id_invoice', '', false);
        //$this->db->join('service_type', 'service_type.service_type = invoice_details.service_type collate utf8_general_ci', 'LEFT', FALSE);
        $this->db->where('invoice.business_id', $this->session->userdata('businessid'));
        $this->db->where('invoice_status', 'valid');
        $this->db->where('date_format(invoice_date , "%Y-%m-%d") =', $today);
        $this->db->where_in('invoice.payment_mode', ['Cash', 'Mixed']);
        $this->db->where('invoice_type', 'service');
        $this->db->group_by('invoice_details.service_type');
        $query = $this->db->get('invoice');
        //echo $query; exit();
              
        return $query->result_array();
    }
    
    //This query is in development mode for dupilex template
    function get_today_cash_info($t = null) {
        $today = $this->input->post('calendar_date', TRUE);
        if (!isset($today) || empty($today)) {
            if ($t !== null) {
                $today = $t;
            } else {
                $today = date('Y-m-d');
            }
        }
        $datetime = new DateTime($today);
        $datetime->modify('+1 day');
        $tomorrow = $datetime->format('Y-m-d H:i:s');
        $business_id = $this->session->userdata('businessid');
        //Cash changing paid_amount to paid_cash as service cash
        $sub_query1 = "SELECT ifnull(SUM(paid_cash),0) as 'casha' 
                    FROM invoice WHERE payment_mode in ('Cash','Mixed') AND invoice_date > '" . $today . "'
                    AND invoice_date < '" . $tomorrow . "' AND business_id = " . $business_id . " AND invoice_status = 'valid' and invoice_type = 'service'";

        //Card changing paid_amount to paid_card as service card
        $sub_query2 = "SELECT ifnull(SUM(paid_card),0) as 'carda' 
                    FROM invoice WHERE payment_mode in ('Card','Mixed') AND invoice_date > '" . $today . "'
                    AND invoice_date < '" . $tomorrow . "' AND business_id = " . $business_id . " AND invoice_status = 'valid' and invoice_type = 'service'";
        
        //Cash changing paid_amount to paid_cash as product cash
        $sub_query3 = "SELECT ifnull(SUM(paid_cash),0) as 'casha' 
                    FROM invoice WHERE payment_mode in ('Cash','Mixed') AND invoice_date > '" . $today . "'
                    AND invoice_date < '" . $tomorrow . "' AND business_id = " . $business_id . " AND invoice_status = 'valid' and invoice_type = 'sale'";

        //Card changing paid_amount to paid_card as product card
        $sub_query4 = "SELECT ifnull(SUM(paid_card),0) as 'carda' 
                    FROM invoice WHERE payment_mode in ('Card','Mixed') AND invoice_date > '" . $today . "'
                    AND invoice_date < '" . $tomorrow . "' AND business_id = " . $business_id . " AND invoice_status = 'valid' and invoice_type = 'sale'";

        $this->db->select(" "
                . "($sub_query1) AS ServiceCash, "
                . "($sub_query2) AS ServiceCard, "
                . "($sub_query3) AS ProductCash, "
                . "($sub_query4) AS ProductCard, "
                . "");
        $query = $this->db->get('invoice');

        return $query->row_array();
    }

    
    function repeated_customer_category($year=null, $category_id=null, $flag=null){
        if($category_id==null){
            $category_id = 7;
        }
        if($year==null){
            $year = '2020';
        }
        
        if($flag==null){
            $flag = "servicetype";
        }
        
        $sql = "SELECT customers.id_customers, customers.customer_name,  customers.customer_cell,
            month(visit_service_start) as monthint, date_format(visit_service_start, '%M') as 'month',
             id_invoice, 
            date_format(visit_service_start, '%d-%m-%Y %h:%i:%s') service_date,
               invoice_date,  service_id, service_name, service_category, id_service_category, invoice_status
            from customers
            join
            (select customer_id, invoice_details.service_name, invoice_details.service_category, visit_services.id_service_category,
            id_invoice, visit_service_start, invoice_date, invoice_details.service_id , invoice_status 
            from invoice
            join invoice_details on invoice_details.invoice_id = invoice.id_invoice
            join visit_services on visit_services.customer_visit_id = invoice.visit_id 
            and visit_services.visit_service_status='Active' 
            and visit_services.service_id = invoice_details.service_id
            where invoice_status='valid'
            and year(visit_service_start) = '".$year."' 
            and invoice.business_id = ".$this->session->userdata('businessid')."
            and visit_services.id_service_category = ".$category_id." and visit_services.service_flag = '".$flag."' 
                group by id_invoice, service_id, month(visit_service_start)
            ) as a
            on a.customer_id = customers.id_customers
            order by 1,4 asc";
        
        $query = $this->db->query($sql);
        
        return $query->result_array();
        
    }
    
    function staff_grossing_services($start_date, $end_date, $staff_id){
        
        $sql = "SELECT 
                    invoice_staff.service_category AS 'name',
                    sum(paid)/T.total*100 as 'y',
                    sum(paid) 'amount', 
                    T.total as 'paid'
                FROM invoice_staff
                JOIN invoice ON invoice.id_invoice = invoice_staff.invoice_id
                        AND invoice.invoice_status = 'valid'
                        and  date(invoice_date) >= '".$start_date."'
                        and  date(invoice_date) <= '".$end_date."'      
                        AND reference_invoice_number = ''
                        AND service_category IS NOT NULL
                        AND invoice.business_id = 1
                        AND invoice_staff.staff_id=".$staff_id.",
                    (SELECT SUM(subtotal) AS 'total'
                                FROM (SELECT  service_category, SUM(paid) AS subtotal
                                                FROM invoice_staff
                                                JOIN invoice ON invoice.id_invoice = invoice_staff.invoice_id
                                                AND invoice.invoice_status = 'valid'
                                                and  date(invoice_date) >= '".$start_date."'
                                                and  date(invoice_date) <= '".$end_date."'      
                                                AND reference_invoice_number = ''
                                                AND service_category IS NOT NULL
                                                AND invoice.business_id = ".$this->session->userdata('businessid')." 
                                AND invoice_staff.staff_id=".$staff_id."
                    ORDER BY 2 DESC) AS M) AS T
                GROUP BY service_category
                ORDER BY 2 DESC";
        
        //echo $sql; exit();
        $query = $this->db->query($sql);        
        
        return $query->result_array();
        
    }
    
    function staff_retail_sale($start_date, $end_date, $staff_id){
        
        $sql = "SELECT product_name,  sum(invoice_products.invoice_qty) as count,
                sum(invoice_products.final_price) final_price, sum(invoice_products.paid) as total
                FROM 
                invoice
                join invoice_products on  invoice.id_invoice=invoice_products.invoice_id
                Where invoice.invoice_status='valid'
                AND date(invoice.invoice_date)>='".$start_date."'
                AND date(invoice.invoice_date)<='".$end_date."'
                AND invoice_products.staff_id = ".$staff_id."
                group by product_name";
        
        $query = $this->db->query($sql);        
        
        return $query->result_array();
    }
    
    
    function grossing_retail($start_date, $end_date, $staff_id){
        
        
        $sql="select invoice_products.product_name as 'name', sum(paid)/T.total*100 as 'y', 
                sum(paid) as 'paid'
                from invoice_products
                join invoice on invoice.id_invoice = invoice_products.invoice_id 
                and invoice.invoice_status = 'valid'
                and invoice.business_id=".$this->session->userdata('businessid')."
                AND invoice_products.staff_id=".$staff_id."
                and  date(invoice_product_date) >= '".$start_date."'
                and  date(invoice_product_date) <= '".$end_date."',
                (
                    select sum(subtotal) as 'total' 
                    from 
                        (
                        SELECT product_name, sum(paid) as subtotal 
                        FROM invoice_products
                        join invoice on invoice.id_invoice = invoice_products.invoice_id 
                        and invoice.invoice_status = 'valid'
                        and invoice.business_id=".$this->session->userdata('businessid')."
                            AND invoice_products.staff_id=".$staff_id."
                        and  date(invoice_product_date) >= '".$start_date."'
                        and  date(invoice_product_date) <= '".$end_date."'
                        group by product_name
                        order by 2 desc
                        limit 10
                        ) as M
                    ) as T
                group by product_name
                order by 2 desc
                
                ";
       
       // echo $sql; exit();
        $query = $this->db->query($sql);        
        
        return $query->result_array();
        
    }
    
    
    function customer_profile(){
     //   $date = strtotime("+ 1 days", strtotime($enddate));
     //   $enddate = date("Y-m-d", $date);
     $sql="SELECT customers.id_customers, customers.customer_name, customers.customer_cell, ifnull(customers.customer_card,'') card, customers.customer_email, customers.customer_address, 
     date_format(customer_birthday,'%d-%m-%Y') customer_birthday, 
     date_format(customer_anniversary,'%d-%m-%Y') customer_anniversary, 
     customer_alert, customer_allergies, ifnull(i,0) invoices, 
     ifnull(purchases,0) Purchases, ifnull(discount,0) Discounts, ifnull(balance,0) balance ,
     ifnull(earned,0) earned, ifnull(used,0) used,
     ifnull(customer_loyalty_points,0) loyalty_points
     FROM customers 
     join (
             select customer_id, count(id_invoice) i, sum(ifnull(balance,0)) balance, sum(ifnull(paid_amount,0)) purchases, 
             sum(ifnull(discount,0)) discount, sum(ifnull(loyalty_earned,0)) earned, sum(ifnull(loyalty_used,0)) used,
             sum(ifnull(loyalty_earned,0))  - sum(ifnull(loyalty_used,0)) customer_loyalty_points
             from invoice 
             where  invoice_status='valid'                         
             group by customer_id 
             HAVING sum(invoice.loyalty_earned)-sum(invoice.loyalty_used)>0
     ) as bal on bal.customer_id=customers.id_customers ";
        
        $query = $this->db->query($sql);
        return $query->result_array();
        
    }

    function staff_day_wise_service_count($month, $year){
        

        $sql = "SELECT
        date_format( invoice.invoice_date, '%Y-%m-%d' ) as SaleMonth, 
        invoice_staff.staff_name, concat(invoice_staff.service_name, invoice_staff.service_category) as service_name, staff_id,
        round(invoice_details.price,2) as price ";
        
        $d = cal_days_in_month(CAL_GREGORIAN, $month, $year);

        for($x=1; $x<=$d; $x++){            
            $sql.=", sum( if( day( invoice.invoice_date ) = ".$x.", 1, 0 )) as '".sprintf("%02d", $x)."'";            
        }
            
                
        $sql .=" FROM
                invoice
            JOIN invoice_details on invoice_details.invoice_id = invoice.id_invoice   
            JOIN invoice_staff on invoice_details.id_invoice_details = invoice_staff.invoice_detail_id and invoice_staff.additional_staff='NO'   
            JOIN customers on customers.id_customers = invoice.customer_id
            WHERE invoice.invoice_status='valid' AND customers.created_on < '".$year."-".$month."-01'
            AND month(invoice_date)=".$month." AND year(invoice_date)='".$year."'
            group by
                staff_id, invoice_details.service_id
            order by
            staff_name, service_name, day( invoice.invoice_date )";

        //echo $sql; exit();
        $result = $this->db->query($sql);        

        return $result->result_array();

    }

}
