<?php

class Accountreports_model extends CI_Model {

    function __construct() {

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

    public function salestaxreport($startdate, $enddate, $businessid=1){
      
         $invoice_type=$this->input->post('invoicetype');
        
        
         $sql = "SELECT id_invoice, date_format(invoice_date, '%d-%m-%Y') as invoice_date, 
                case when customers.customer_gender='F' then 'Female'
                else 'Male'
                End as 'Gender',
                group_concat(case when invoice.invoice_type='service' then concat(invoice_details.service_category, ' ', invoice_details.service_name)
                else invoice_products.product_name End SEPARATOR '<br> ') as 'Main Service', 
                ifnull(invoice_products.business_brand_name,'') as business_brand_name,
                case when invoice.invoice_type='service' then round(max(invoice_details.final_price),2) 
                else round(max(invoice_products.invoice_qty*invoice_products.final_price),2)  End as 'FinalPrice',
                business.business_name as 'Branch', invoice.created_by as 'Sales Rep',
                ucase(invoice_type) as 'Type', invoice.customer_name as 'Client Name',
                round(invoice.sub_total,2)+round(other_charges,2) as 'Value',
                round(invoice.tax_total,2) as 'Sales Tax', 

                case when business.tax_mode = 'after' then 
                    case when gross_wo_tax > 0 then gross_wo_tax else round(invoice.gross_amount-invoice.cctip,2) end
                else round(invoice.sub_total,2) End as 'Value Exc. Sales Tax',               

                case when business.tax_mode = 'after' then 
                    case when gross_wo_tax > 0 then round((invoice.tax_total * 100)/gross_wo_tax) else round((invoice.tax_total * 100)/(invoice.gross_amount-invoice.cctip)) end
                else round((invoice.tax_total * 100)/(invoice.sub_total),2) End as 'tax_perc', 


                round(invoice.total_payable) as 'Value Inc. Sales Tax',
                ucase(invoice.payment_mode) as 'Cash/Card', paid_cash, paid_card,
                invoice.imsfiscal_invoice_number
                FROM invoice
                join customers on customers.id_customers = invoice.customer_id
                left join invoice_details on invoice.id_invoice = invoice_details.invoice_id
                left join 
                    (select invoice_products.*, business_brands.business_brand_name from invoice_products 
                    join business_products on id_business_products = invoice_products.product_id
                    join business_brands on id_business_brands = business_products.brand_id
                    where 
                    date(invoice_products.invoice_product_date) >= '".$startdate."'
                    and date(invoice_products.invoice_product_date) <= '".$enddate."' 
                    ) as invoice_products on invoice.id_invoice = invoice_products.invoice_id
                
                join business on business.id_business = invoice.business_id
                Where invoice.invoice_status = 'valid'
                and ifnull(invoice.reference_invoice_number, '') = ''
                and date(invoice.invoice_date) >= '".$startdate."'
                and date(invoice.invoice_date) <= '".$enddate."' 
                ";
         if($this->session->userdata('ho') == 'No'){
             $sql.=" and invoice.business_id = ".$this->session->userdata('businessid')." ";
         } elseif($businessid !== "0") {
             $sql.=" and invoice.business_id = ".$businessid." ";
         }
         
         if($invoice_type != 'all'){
             $sql.=" and invoice.invoice_type = '" .$invoice_type."' ";
         } 
         
         
         
        $sql .= " group by id_invoice
                order by date(invoice_date) asc";
         
        $query=$this->db->query($sql);
        return $query->result_array();
    
    }
    
    
    public function vendor_account_report($startdate, $enddate, $business_partner, $business_partner_id, $id_business, $account_id=1){
        
        $sql = "
               select business.business_name as 'Branch', id_account_vouchers, business_partner_name,
               date_format(account_vouchers.voucher_date, '%d-%m-%Y') as voucher_date, date_format(account_vouchers.created_on, '%d-%m-%Y') as 'create_date',
               account_vouchers.description,
               debit, credit
                from account_voucher_detail
               join account_vouchers on account_vouchers.id_account_vouchers = account_voucher_detail.account_voucher_id
               join business on business.id_business = account_vouchers.business_id
               where account_vouchers.voucher_status='Active'
               and business_partner = ".$business_partner."
               and business_partner_id = ".$business_partner_id."
               and id_business = ".$id_business."
               and account_voucher_detail.account_head_id = ".$account_id."    
               and date(account_vouchers.voucher_date) >='".$startdate."'
               and date(account_vouchers.voucher_date) <='".$enddate."'                    
               order by date(voucher_date) 
               ";
         
       // echo $sql; exit();
        $query=$this->db->query($sql);
        return $query->result_array();
    
    }
    
    public function vendor_account_report_openging($startdate, $enddate, $business_partner, $business_partner_id, $id_business, $account_id=1, $account_type='Receivable') {
        
        $sql = "
               select ";
        
        if(strpos($account_type, "Payable")==false){
            $sql .= " ifnull(sum(debit)-sum(credit),0) as balance ";
        }else {
            $sql .= " ifnull(sum(credit)-sum(debit),0) as balance ";
        }
        
        
        $sql .= " from account_voucher_detail
               join account_vouchers on account_vouchers.id_account_vouchers = account_voucher_detail.account_voucher_id
               join business on business.id_business = account_vouchers.business_id
               join account_heads on id_account_heads = account_voucher_detail.account_head_id               
               where account_vouchers.voucher_status='Active'
               and business_partner = ".$business_partner."
               and business_partner_id = ".$business_partner_id."
               and id_business = ".$id_business."
               and account_voucher_detail.account_head_id = ".$account_id."    
               and date(account_vouchers.voucher_date) <'".$startdate."'                              
               group by account_voucher_detail.account_head_id 
               ";
         
       
      //  echo $sql; exit();
        $query=$this->db->query($sql);
        return $query->row();
    
    }
    
    function payable_aging_report($account_head_id){
        
        $sql = "select account_head_id, business_partner, branch, business_partner_name, business_partner_id, sum(over90) as 'over90', sum(60to90) as 'sixtyninty', sum(30to59) as 'thirtyfiftynine', sum(lessthen30) as 'lessthen30'
                from (
                        select business_partner, account_head_id, Branch, business_partner_name, business_partner_id,
                        case when lapseddays >= 90 Then sum(credit)-sum(debit) ELSE 0 End as 'over90',
                        case when lapseddays < 90 AND lapseddays >= 60  Then sum(credit)-sum(debit) ELSE 0  End as '60to90',
                        case when lapseddays < 60 AND lapseddays >= 30  Then sum(credit)-sum(debit) ELSE 0  End as '30to59',
                        case when lapseddays < 30  Then sum(credit)-sum(debit) ELSE 0 End as 'lessthen30'
                        from 
                                (select business_partner, account_voucher_detail.account_head_id, purchase_order_id, date(voucher_date), business.business_name as 'Branch',  business_partner_name,  account_vouchers.business_partner_id, account_vouchers.business_partner as partner_type,
                                sum(credit)-sum(debit) as Balance, sum(debit) as debit, sum(credit) as credit,
                                datediff(now(), (account_vouchers.voucher_date)) as lapseddays
                                from account_voucher_detail
                                join account_vouchers on account_vouchers.id_account_vouchers = account_voucher_detail.account_voucher_id
                                join business on business.id_business = account_vouchers.business_id
                                where account_vouchers.voucher_status='Active'                             
                                and id_business = ".$this->session->userdata('businessid') ."
                                and account_voucher_detail.account_head_id = ". $account_head_id." ";
                                    
        if($this->input->post('enddate') != null){
            $sql.=" AND date(account_vouchers.voucher_date) <= '". $this->input->post('enddate') ."' ";
        }
        
        $sql .= "               group by account_vouchers.purchase_order_id, business_partner_id) as a
                        group by business_partner_id, lapseddays) as b
                group by business_partner_id
                ";
        //echo $sql; exit();
        $query=$this->db->query($sql);
        return $query->result();
    }
    
    function receivable_aging_report($account_head_id){
        
        $sql = "select account_head_id, business_partner, branch, business_partner_name, business_partner_id, sum(over90) as 'over90', sum(60to90) as 'sixtyninty', sum(30to59) as 'thirtyfiftynine', sum(lessthen30) as 'lessthen30'
                from (
                        select account_head_id, business_partner, Branch, business_partner_name, business_partner_id,
                        case when lapseddays >= 90 Then sum(debit)-sum(credit) ELSE 0 End as 'over90',
                        case when lapseddays < 90 AND lapseddays >= 60  Then sum(debit)-sum(credit) ELSE 0  End as '60to90',
                        case when lapseddays < 60 AND lapseddays >= 30  Then sum(debit)-sum(credit) ELSE 0  End as '30to59',
                        case when lapseddays < 30  Then sum(debit)-sum(credit) ELSE 0 End as 'lessthen30'
                        from 
                                (select account_voucher_detail.account_head_id, business_partner, purchase_order_id, date(voucher_date), business.business_name as 'Branch',  business_partner_name,  account_vouchers.business_partner_id, account_vouchers.business_partner as partner_type,
                                sum(debit)-sum(credit) as Balance, sum(debit) as debit, sum(credit) as credit,
                                datediff(now(), (account_vouchers.voucher_date)) as lapseddays
                                from account_voucher_detail
                                join account_vouchers on account_vouchers.id_account_vouchers = account_voucher_detail.account_voucher_id
                                join business on business.id_business = account_vouchers.business_id
                                where account_vouchers.voucher_status='Active'                             
                                and id_business = ".$this->session->userdata('businessid') ."
                                and account_voucher_detail.account_head_id = ". $account_head_id."  ";
                                    
        if($this->input->post('enddate') != null){
            $sql.=" AND date(account_vouchers.voucher_date) <= '". $this->input->post('enddate') ."' ";
        }
        
        $sql .= "      group by account_vouchers.invoice_id, business_partner_id) as a
                       group by business_partner_id, lapseddays) as b
                group by business_partner_id
                ";
       // echo $sql; exit();
        $query=$this->db->query($sql);
        return $query->result();
    }
    
   
    
    function get_sale_and_tax($startdate, $enddate, $salesaccount, $salestaxaccount,$salesdiscount, $saletype){
        $sql="select round(sum(ifnull(a.sale,0)),2) Total, round(sum(ifnull(b.tax,0)),2) SalesTax, round(sum(ifnull(c.discount,0)),2) Discount
            from 
            (select invoice_id, credit as sale
        	from account_vouchers
        	join account_voucher_detail on account_voucher_detail.account_voucher_id = account_vouchers.id_account_vouchers
        	where account_voucher_detail.account_head_id = ". $salesaccount ."
                and account_vouchers.sale_type='".$saletype."' 
                and account_vouchers.voucher_status='Active'
        	and date(voucher_date)>= '".$startdate."' and date(voucher_date)<='".$enddate."'                    
                    ) as a
            join (select invoice_id, credit as tax
        	from account_vouchers
        	join account_voucher_detail on account_voucher_detail.account_voucher_id = account_vouchers.id_account_vouchers
        	where account_voucher_detail.account_head_id = ". $salestaxaccount ."
                and account_vouchers.sale_type='".$saletype."'
                and account_vouchers.voucher_status='Active'
        	and date(voucher_date)>= '".$startdate."' and date(voucher_date)<='".$enddate."'                    
        	) as b on a.invoice_id = b.invoice_id 
            left join (select invoice_id, debit as discount
        	from account_vouchers
        	join account_voucher_detail on account_voucher_detail.account_voucher_id = account_vouchers.id_account_vouchers
        	where account_voucher_detail.account_head_id = ". $salesdiscount ."
                and account_vouchers.sale_type='".$saletype."'
                and account_vouchers.voucher_status='Active'
        	and date(voucher_date)>= '".$startdate."' and date(voucher_date)<='".$enddate."'                    
        	) as c on a.invoice_id = c.invoice_id     
            ;";
//        if($saletype=='sale'){
//            echo $sql; exit();        
//        }
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
    function get_training_sale_and_tax($startdate, $enddate, $salesaccount, $salestaxaccount){
        $sql="select round(sum(sale),2) Total, round(sum(b.tax),2) SalesTax from 
            (select program_invoice_id, credit as sale
        	from account_vouchers
        	join account_voucher_detail on account_voucher_detail.account_voucher_id = account_vouchers.id_account_vouchers
        	where account_voucher_detail.account_head_id = ". $salesaccount ."
                
                and account_vouchers.voucher_status='Active'
        	and date(voucher_date)>= '".$startdate."' and date(voucher_date)<='".$enddate."'                    
                    ) as a
            join (select program_invoice_id, credit as tax
        	from account_vouchers
        	join account_voucher_detail on account_voucher_detail.account_voucher_id = account_vouchers.id_account_vouchers
        	where account_voucher_detail.account_head_id = ". $salestaxaccount ."
                
                and account_vouchers.voucher_status='Active'
        	and date(voucher_date)>= '".$startdate."' and date(voucher_date)<='".$enddate."'
                    
        	) as b on a.program_invoice_id = b.program_invoice_id ;";
       //echo $sql; exit();
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
    function get_franchise_sale_and_tax($startdate, $enddate, $salesaccount, $salestaxaccount){
        $sql="select round(sum(sale),2) Total, round(sum(b.tax),2) SalesTax from 
            (select franchise_order_id, credit as sale
        	from account_vouchers
        	join account_voucher_detail on account_voucher_detail.account_voucher_id = account_vouchers.id_account_vouchers
        	where account_voucher_detail.account_head_id = ". $salesaccount ."                
                and account_vouchers.voucher_status='Active'
        	and date(voucher_date)>= '".$startdate."' and date(voucher_date)<='".$enddate."'                    
                    ) as a
            join (select franchise_order_id, credit as tax
        	from account_vouchers
        	join account_voucher_detail on account_voucher_detail.account_voucher_id = account_vouchers.id_account_vouchers
        	where account_voucher_detail.account_head_id = ". $salestaxaccount ."                
                and account_vouchers.voucher_status='Active'
        	and date(voucher_date)>= '".$startdate."' and date(voucher_date)<='".$enddate."'
                    
        	) as b on a.franchise_order_id = b.franchise_order_id ;";
       //echo $sql; exit();
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
}