<?php

class Salesdashboard_model extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
         
    }    
    
    function totalbills($startdate, $enddate){
        $sql="SELECT *, invoice_tax + voucher_tax as tax_total, 
            invoice_cc_charge + voucher_cc_charge + advance_cc_charge as cc_charge
            from (select count(invoice.id_invoice) totalbills, 
            sum(invoice.paid_cash + invoice.paid_card + invoice.paid_check) paid, 
            sum(discount) invoice_discount, sum(advance_amount) advance_adj ,
            sum(balance) balance, sum(retained_amount) retained, 
            sum(loyalty_earned) loyalty_earned, 
            sum(loyalty_used) loyalty_used, sum(paid_cash) cash, 
           
            case when ifnull(invoice.reference_invoice_number,'') = '' then ifnull(sum(invoice.tax_total),0) else 0 end as invoice_tax,
            sum(total_payable) as total_payable,
            ifnull(sum(cc_charge),0) as invoice_cc_charge
            from invoice
           
            where invoice.business_id = ".$this->session->userdata('businessid')." 
            and date(invoice.invoice_date) >='".$startdate."'  
            and date(invoice.invoice_date) <='".$enddate."'   
            and invoice_status='valid') as a,
            (SELECT ifnull(sum(voucher_tax),0) as voucher_tax,
            ifnull(sum(voucher_cccharge),0) as voucher_cc_charge
            from 
            order_vouchers where business_id =  ".$this->session->userdata('businessid')." 
            and date(voucher_date) >='".$startdate."'  
            and date(voucher_date) <='".$enddate."'   
            and voucher_status !='cancelled'
                ) as b,
            (SELECT ifnull(sum(advance_cc_charge),0) as advance_cc_charge
            from 
            visit_advance join customer_visits on id_customer_visits = customer_visit_id
            where business_id =  ".$this->session->userdata('businessid')." 
            and date(visit_advance.advance_date) >='".$startdate."'  
            and date(visit_advance.advance_date) <='".$enddate."'   
            and customer_visits.visit_status in ('open','invoiced')
                ) as c
            ";       
        
        $query = $this->db->query($sql);
        return $query->row();
    }
    
    function payment_breakup($startdate, $enddate){
        $today = $startdate;
        $tomorrow = $enddate;
        $business_id=$this->session->userdata('businessid');
        $sub_query1 = ""
                ."select sum(ifnull(casha,0) + ifnull(cashb,0) + ifnull(cashc,0)) as 'Cash' from (
                    SELECT ifnull(SUM(paid_cash),0) as 'casha' 
                    FROM invoice WHERE payment_mode in ('Cash','Mixed') AND date(invoice_date) >= '".$today."'
                    AND date(invoice_date) <= '".$tomorrow."' AND business_id = ".$business_id." AND invoice_status = 'valid' 
                    ) a, (
                    select sum(ifnull(visit_advance.advance_amount,0)) as 'cashb' From visit_advance
                    join customer_visits on customer_visits.id_customer_visits = visit_advance.customer_visit_id 
                    WHERE visit_status <> 'canceled' AND visit_advance.advance_date >= '".$today."'
                    AND date(visit_advance.advance_date) <= '".$tomorrow."' And visit_advance.advance_mode='cash'                    
                    AND business_id = ".$business_id.") b, (
                    SELECT SUM(ifnull(retained_amount,0)) as 'cashc' 
                    FROM invoice WHERE payment_mode in ('Cash','Mixed') and paid_cash>0 AND invoice_date >= '".$today."'
                    AND date(invoice_date) <= '".$tomorrow."' AND business_id = ".$business_id." AND invoice_status = 'valid'
                    ) c";
         //   }
        //Card changing paid_amount to paid_card
        $sub_query2 = ""
                ."select sum(carda + cardb + cardc) as 'Card' from (
                    SELECT ifnull(SUM(paid_card),0) as 'carda' 
                    FROM invoice WHERE payment_mode in ('Card','Mixed') AND date(invoice_date) >= '".$today."'
                    AND date(invoice_date) <= '".$tomorrow."' AND business_id = ".$business_id." AND invoice_status = 'valid' 
                    ) a, (
                    select ifnull(sum(visit_advance.advance_amount),0) + ifnull(sum(visit_advance.advance_cc_charge),0) as 'cardb' From visit_advance
                    join customer_visits on customer_visits.id_customer_visits = visit_advance.customer_visit_id
                    WHERE visit_status <> 'canceled' AND date(visit_advance.advance_date) >= '".$today."'
                    AND date(visit_advance.advance_date) <= '".$tomorrow."' And visit_advance.advance_mode='card'                    
                    AND business_id = ".$business_id.") b, (
                    SELECT ifnull(SUM(retained_amount),0) as 'cardc' 
                    FROM invoice WHERE payment_mode = 'Card' AND date(invoice_date) >= '".$today."'
                    AND date(invoice_date) <= '".$tomorrow."' AND business_id = ".$business_id." AND invoice_status = 'valid'     
                    ) c
                    ";
       
        //Check changing paid_amount to paid_check
        $sub_query3 = ""
                ."select sum(checka + checkb + checkc) as 'Check' from (
                    SELECT ifnull(SUM(paid_check),0) as 'checka' 
                    FROM invoice WHERE payment_mode = 'Check' AND date(invoice_date) >= '".$today."'
                    AND date(invoice_date) <= '".$tomorrow."' AND business_id = ".$business_id." AND invoice_status = 'valid' 
                    ) a, (
                    select ifnull(sum(visit_advance.advance_amount),0)   as 'checkb' From visit_advance
                    join customer_visits on customer_visits.id_customer_visits = visit_advance.customer_visit_id
                    WHERE visit_status <> 'canceled' AND date(visit_advance.advance_date) >= '".$today."'
                    AND date(visit_advance.advance_date) <= '".$tomorrow."' And visit_advance.advance_mode='check'                    
                    AND business_id = ".$business_id.") b, (
                    SELECT ifnull(SUM(retained_amount),0) as 'checkc' 
                    FROM invoice WHERE payment_mode = 'Check' AND date(invoice_date) >= '".$today."'
                    AND date(invoice_date) <= '".$tomorrow."' AND business_id = ".$business_id." AND invoice_status = 'valid'     
                    ) c
                    ";
        
        $sub_query4 =  "SELECT ifnull(SUM(paid_cash),0) as voucher_cash
                    FROM order_vouchers 
                    WHERE payment_mode = 'Cash'  AND 
                    date(voucher_date) >= '$today' AND 
                    date(voucher_date) <= '$tomorrow' AND     
                    business_id = $business_id";
        $sub_query5 =  "SELECT ifnull(SUM(paid_card),0) as voucher_card
                    FROM order_vouchers 
                    WHERE payment_mode = 'Card'  AND 
                    date(voucher_date) >= '$today' AND 
                    date(voucher_date) <= '$tomorrow' AND     
                    business_id = $business_id";
        $sub_query6 =  "SELECT ifnull(SUM(paid_check),0) as voucher_check
                    FROM order_vouchers 
                    WHERE payment_mode = 'Check'  AND 
                    date(voucher_date) >= '$today' AND 
                    date(voucher_date) <= '$tomorrow' AND   
                    business_id = $business_id";
        
        $this->db->select("SUM(paid_amount) AS totalPaid, "
                . "($sub_query1) AS totalCash, "
                . "($sub_query2) AS totalCard, "
                . "($sub_query3) AS totalCheck, "
                . "($sub_query4) AS voucherCash, "
                . "($sub_query5) AS voucherCard, "        
                . "($sub_query6) AS voucherCheck "
                . "");
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->where('date(invoice_date) >=', $startdate);
        $this->db->where('date(invoice_date) <=', $enddate);
        $this->db->where('invoice_status', 'valid');
        
        $query = $this->db->get('invoice');
       // echo $query; exit();
        return $query->row();        
    }
    
    function billitems_service($startdate, $enddate){
        $sql="select 'Services' as 'name', ifnull(round(sum(paid_amount)/T.total*100,2),0) as 'y', ifnull(count(id_invoice),0) totalbills, ifnull(sum(paid_amount),0) paid, ifnull(sum(discount),0) invoice_discount, ifnull(sum(advance_amount),0) advance_adj ,
            ifnull(sum(balance),0) balance, ifnull(sum(retained_amount),0) retained, ifnull(sum(loyalty_earned),0) loyalty_earned
            from invoice,
            (select ifnull(sum(paid_amount),0) total from invoice
            where invoice.business_id = ".$this->session->userdata('businessid')." 
                and invoice.invoice_date >='".$startdate." 00:00'  
                and invoice.invoice_date <='".$enddate." 23:59'   
                and invoice_status='valid' ) as T
            
                where invoice.business_id = ".$this->session->userdata('businessid')." 
                and invoice.invoice_date >='".$startdate." 00:00'  
                and invoice.invoice_date <='".$enddate." 23:59'   
                and invoice_status='valid'   
                and invoice_type='service'
                
                ";       
        $query = $this->db->query($sql);
        return $query->row();
    }
    function billitems_retail($startdate, $enddate){
        $sql="select 'Retail' as 'name', ifnull(round(sum(paid_amount)/T.total*100,2),0) as 'y', ifnull(count(id_invoice),0) totalbills, ifnull(sum(paid_amount),0) paid, ifnull(sum(discount),0) invoice_discount, ifnull(sum(advance_amount),0) advance_adj ,
            ifnull(sum(balance),0) balance, ifnull(sum(retained_amount),0) retained, ifnull(sum(loyalty_earned),0) loyalty_earned
            from invoice,
            (select ifnull(sum(paid_amount),0) total from invoice
            where invoice.business_id = ".$this->session->userdata('businessid')." 
                and invoice.invoice_date >='".$startdate." 00:00'  
                and invoice.invoice_date <='".$enddate." 23:59'   
                and invoice_status='valid' ) as T
                where invoice.business_id = ".$this->session->userdata('businessid')." 
                and invoice.invoice_date >='".$startdate." 00:00'  
                and invoice.invoice_date <='".$enddate." 23:59'   
                and invoice_status='valid'   
                and invoice_type='sale'
                ";       
        $query = $this->db->query($sql);
        return $query->row();
    }
    
    function canceledbills($startdate, $enddate){
        $sql="select count(id_invoice) canceledbills, sum(paid_amount) paid, sum(discount) invoice_discount, sum(advance_amount) advance_adj from invoice
                where invoice.business_id = ".$this->session->userdata('businessid')." 
                and invoice.invoice_date >='".$startdate." 00:00'  
                and invoice.invoice_date <='".$enddate." 23:59'   
                and invoice_status!='valid'    
                ";       
        $query = $this->db->query($sql);
        return $query->row();
    }
    
    function service_details($startdate, $enddate){
        $sql="select sum(paid) serivce_paid, sum(invoice_details.discount) service_discount 
            from invoice_details
            join invoice on invoice.id_invoice = invoice_details.invoice_id
                where invoice.business_id = ".$this->session->userdata('businessid')." 
                and invoice.invoice_date >='".$startdate." 00:00'  
                and invoice.invoice_date <='".$enddate." 23:59'   
                and invoice_status='valid'    
                ";       
        $query = $this->db->query($sql);
        return $query->row();
    }
    
    function product_details($startdate, $enddate){
        $sql="select sum(paid) product_paid, sum(invoice_products.discount) product_discount 
            from invoice_products
            join invoice on invoice.id_invoice = invoice_products.invoice_id
                where invoice_products.business_id = ".$this->session->userdata('businessid')." 
                and invoice.invoice_date >='".$startdate." 00:00'  
                and invoice.invoice_date <='".$enddate." 23:59'   
                and invoice_status='valid'    
                ";       
        $query = $this->db->query($sql);
        return $query->row();
    }
    
    function expenses($startdate,$enddate){
        $this->db->select('sum(debit) as expense');
        $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->join('account_sub_control asctr', 'asctr.id_account_sub_control = ah.account_sub_control_id');
        $this->db->join('account_control ac', 'ac.id_account_control = asctr.account_control_id');
        $this->db->join('account_main am', 'am.id_account_main = ac.account_main_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('am.account_main', 'Expenses');
        
      
        $this->db->where('av.voucher_type', 1);
        $this->db->where('av.voucher_status =', 'Active');
        
        $query = $this->db->get('account_vouchers av');
       
         return $query->row();
    }
    
   
    
    function advance($startdate,$enddate){
        
        $sql="select sum(visit_advance.advance_amount + advance_cc_charge) advance,            
            sum(case when visit_advance.advance_mode = 'cash' then visit_advance.advance_amount
            else 0 
            end)
            as cash,
            sum(case when visit_advance.advance_mode = 'card' then visit_advance.advance_amount + visit_advance.advance_cc_charge
            else 0 
            end)
            as card,
            sum(case when visit_advance.advance_mode = 'check' then visit_advance.advance_amount
            else 0 
            end)
            as 'check'
            
            from visit_advance 
            join customer_visits on customer_visits.id_customer_visits = visit_advance.customer_visit_id
            where customer_visits.business_id = ".$this->session->userdata('businessid')." 
                and visit_advance.advance_date>='".$startdate." 00:00'  
                and visit_advance.advance_date <='".$enddate." 23:59'   
                and visit_status!='canceled'    
        ";
        $query = $this->db->query($sql);
        return $query->row();
    }
    
    function vouchers($startdate,$enddate){
        
        $sql="select sum((order_vouchers.amount) - voucher_tax) as vouchers, 
            sum(paid_cash) cash, sum(paid_card) card, sum(paid_check) as 'check',
            ifnull(sum(voucher_tax),0) as voucher_tax, 
            ifnull(sum(voucher_cccharge),0) as voucher_cc_charge
            from order_vouchers 
            
            where order_vouchers.business_id = ".$this->session->userdata('businessid')." 
                and order_vouchers.voucher_date>='".$startdate." 00:00'  
                and order_vouchers.voucher_date <='".$enddate." 23:59'   
                  
        ";
        $query = $this->db->query($sql);
        return $query->row();
    }
    
    function grossing_services($startdate, $enddate){
        
        $sql="select invoice_details.service_category as 'name', sum(paid)/T.total*100 as 'y' , sum(paid) 'amount', T.total as 'paid'
	from invoice_details 
	join invoice on invoice.id_invoice = invoice_details.invoice_id 
	and invoice.invoice_status = 'valid'
        and reference_invoice_number=''
        and invoice_details.service_category is not null
	and invoice.business_id=".$this->session->userdata('businessid')." 
	and  invoice_detail_date >='".$startdate." 00:00'  
	and  invoice_detail_date <='".$enddate." 23:59' ,  
        
	(
            select subtotal as 'total' 
            from 
                (
                SELECT service_category, sum(paid) as subtotal 
                FROM invoice_details
                join invoice on invoice.id_invoice = invoice_details.invoice_id 
                and invoice.invoice_status = 'valid' 
                and invoice_details.service_category is not null
                and invoice.business_id=".$this->session->userdata('businessid')."  
                and  invoice.invoice_date >='".$startdate." 00:00' 
                and  invoice.invoice_date <='".$enddate." 23:59'
                and invoice.reference_invoice_number=''     
                
                ) as M
            ) as T
	group by service_category
	order by 2 desc";
//      /  echo $sql;
        $query = $this->db->query($sql);        
        
        return $query->result_array();
        
    }
    
}