class CardSettleTran < ApplicationRecord

	belongs_to :shift_sale

    def self.search(filter,from,to,payment_type,status_type)
	    if filter.blank?
	        keyword = ''
	    else
	        keyword = " e.name LIKE ?","%#{filter}%"
	    end

	    if payment_type.blank?
	    	payment = ''
	    else
	    	payment = " req_type LIKE '#{payment_type}'"	
	    end

	    if status_type.blank?
	    	status = ''
	    else
	    	status = " status = '#{status_type}'"
	    end

	    if from.present? && to.present?

	        query  = CardSettleTran.joins("Join shift_sales ss ON ss.id = card_settle_trans.shift_sale_id"+
	        								" JOIN employees e ON e.id = ss.employee_id")
	        cardSettle = query.where("DATE_FORMAT(req_date,'%Y-%m-%d') >= ? and DATE_FORMAT(req_date,'%Y-%m-%d') <= ?",from,to)
	        query1 = cardSettle.where(keyword)
	        query2 = query1.where(payment)
	        query3 = query2.where(status)

	    end
    end

    def self.searchReport(from,to,payment_type,shift_sale_range,shift)

	    if payment_type.blank?
	    	payment = ''
	    else
	    	payment = " req_type LIKE '#{payment_type}'"
	    end

	    if from.present? && to.present?

	        query  = CardSettleTran.joins("Join shift_sales ss ON ss.id = card_settle_trans.shift_sale_id"+
	        								" JOIN employees e ON e.id = ss.employee_id")
	        cardSettle = query.where("DATE_FORMAT(req_date,'%Y-%m-%d') >= ? and DATE_FORMAT(req_date,'%Y-%m-%d') <= ? and status = 'Approved'",from,to)
	        
	        if shift.present?
	        	query1 = cardSettle.where("ss.id in (?)", shift.to_a)
	        elsif shift_sale_range.present?	
	        	query1 = cardSettle.where("ss.id in (?)",shift_sale_range.to_a)
	        else
	        	query1 = cardSettle.where("DATE_FORMAT(req_date,'%Y-%m-%d') between ? and ?",from,to)
	        end
	        query2 = query1.where(payment)

	    end
    end

    def self.get_by_shift_sale_by_card(from,to,status)
	  	query = ShiftSale.select("shift_sales.id ,shift_started_at AS opening_date,
	                          shift_closed_at As closing_date,")
	                          .order("shift_sales.id DESC")
	  	return query = query.where("shift_sales.shift_started_at >= ?" , from)
	  	byebug
	end	
end
