diff --git a/app/models/sale.rb b/app/models/sale.rb index 1362f5ce..e2d1aff4 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1710,63 +1710,48 @@ end end def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .where('sales.sale_status = "completed" and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")') if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")',from,to,from_time,to_time) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")',from,to) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' if !from_time.nil? && !to_time.nil? - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")',from,to,from_time,to_time) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")',from,to) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? if !from_time.nil? && !to_time.nil? - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay") and shift_sale_id=?',from,to,from_time,to_time,shift.id) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) else - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay") and shift_sale_id=?',from,to,shift.id) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) end end end end else if current_user.nil? - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")',today,today) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")',today,today) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay") and shift_sale_id=?',today,today,shift.id) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) end end end end + query = query.sum("sp.payment_amount") end def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -1791,7 +1776,7 @@ end .joins(" JOIN sales ON sale_audits.sale_id = sales.sale_id") .where("sales.sale_status='completed'") - if !from.nil? && !to.nil? + if (!from.nil? && !to.nil?) && (from != "" && to!="") if !from_time.nil? && !to_time.nil? sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'") else @@ -1801,19 +1786,19 @@ end sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'") end - if !from.nil? && !to.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote"') + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end else - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end @@ -1821,16 +1806,14 @@ end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' if !from_time.nil? && !to_time.nil? - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end else - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end @@ -1839,16 +1822,14 @@ end shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? if !from_time.nil? && !to_time.nil? - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and s.shift_sale_id=?',from,to,from_time,to_time,shift.id) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and s.shift_sale_id=?',from,to,from_time,to_time,shift.id) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end else - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',from,to,shift.id) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',from,to,shift.id) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end @@ -1858,27 +1839,23 @@ end end else if current_user.nil? - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',today,today,shift.id) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") - + query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',today,today,shift.id) + .sum("payment_amount") if !sub_query.nil? query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) end @@ -1890,119 +1867,89 @@ end end def self.summary_sale_receipt(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed"') if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' if !from_time.nil? && !to_time.nil? - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? if !from_time.nil? && !to_time.nil? - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) else - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) end end end end else if current_user.nil? - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) end end end end + query = query.first() end def self.total_payment_methods(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed"') + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' if !from_time.nil? && !to_time.nil? - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? if !from_time.nil? && !to_time.nil? - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) else - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) end end end end else if current_user.nil? - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) end end end @@ -2061,101 +2008,55 @@ end changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" end + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + where('sales.sale_status = "completed"') + if payment_method == 'card' + query = query.where('(sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method="alipay" or sp.payment_method="paymal" or sp.payment_method="dinga" or sp.payment_method="JunctionPay" or sp.payment_method = "giftvoucher")') + else + query = query.where("sp.payment_method = '#{payment_method}'") + end if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method="alipay" or sp.payment_method="paymal" or sp.payment_method="dinga" or sp.payment_method="JunctionPay" or sp.payment_method = "giftvoucher")',from,to,from_time,to_time) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) else - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher")',from,to) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ?",from,to) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' if !from_time.nil? && !to_time.nil? - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher")',from,to,from_time,to_time) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) else - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher")',from,to) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ?",from,to) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? changed_query += " AND s.shift_sale_id = #{shift.id}" if !from_time.nil? && !to_time.nil? - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher") and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) else - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher") and sales.shift_sale_id=?',from,to,shift.id) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) end end end end else if current_user.nil? - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher")',today) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher")',today) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? changed_query += " AND s.shift_sale_id = #{shift.id}" - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher") and sales.shift_sale_id=?',today,shift.id) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) end end end end + query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() end def self.total_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -2182,325 +2083,231 @@ end end def self.total_dinein_takeaway(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and c.membership_id is null') if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.membership_id is null',from,to,from_time,to_time) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.membership_id is null',from,to) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' if !from_time.nil? && !to_time.nil? - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.membership_id is null',from,to,from_time,to_time) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.membership_id is null',from,to) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? if !from_time.nil? && !to_time.nil? - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.membership_id is null and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) else - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) end end end end else if current_user.nil? - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.membership_id is null',today) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.membership_id is null',today) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) end end end end + + query = query.first() end def self.total_membership(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))') if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? if !from_time.nil? && !to_time.nil? - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to,from_time,to_time) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?)',from,to,from_time,to_time) else - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' if !from_time.nil? && !to_time.nil? - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to,from_time,to_time) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) else - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") if !from_time.nil? && !to_time.nil? - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id).first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id).first() else - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',from,to,shift.id).first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id).first() end end end end else if current_user.nil? - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) - .first() + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',today,shift.id) - .first() + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) end end end end + query = query.first() end # def self.total_other_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + # query = Sale.select("count(sales.customer_id) as total_cus") + # .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + # .where('sales.sale_status = "completed" and c.customer_type is null and c.membership_id is null') # if !from.nil? && !to.nil? # if current_user.nil? - # query = Sale.select("count(sales.customer_id) as total_cus") - # .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - # if !from_time.nil? && !to_time.nil? - # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null',from,to,from_time,to_time) - # .first() - # else - # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null',from,to) - # .first() - # end + # if !from_time.nil? && !to_time.nil? + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) + # else + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + # end # else # if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - # query = Sale.select("count(sales.customer_id) as total_cus") - # .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - # if !from_time.nil? && !to_time.nil? - # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null',from,to,from_time,to_time) - # .first() - # else - # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null',from,to) - # .first() - # end + # if !from_time.nil? && !to_time.nil? + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) + # else + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + # end # else # shift = ShiftSale.current_open_shift(current_user.id) # if !shift.nil? - # query = Sale.select("count(sales.customer_id) as total_cus") - # .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - # if !from_time.nil? && !to_time.nil? - # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - # .first() - # else - # query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) - # .first() - # end + # if !from_time.nil? && !to_time.nil? + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) + # else + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + # end # end # end # end # else # if current_user.nil? - # query = Sale.select("count(sales.customer_id) as total_cus") - # .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - # .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) - # .first() + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) # else # if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - # query = Sale.select("count(sales.customer_id) as total_cus") - # .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - # .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) - # .first() + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) # else # shift = ShiftSale.current_open_shift(current_user.id) # if !shift.nil? - # query = Sale.select("count(sales.customer_id) as total_cus") - # .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - # .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) - # .first() + # query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) # end # end # end # end + # query = query.first() # end def self.total_order(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed"') if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - if !from_time.nil? && !to_time.nil? - query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .first() - else - query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .first() - end + if !from_time.nil? && !to_time.nil? + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) + else + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - if !from_time.nil? && !to_time.nil? - query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .first() - else - query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .first() - end + if !from_time.nil? && !to_time.nil? + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) + else + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - if !from_time.nil? && !to_time.nil? - query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - .first() - else - query = query.where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - .first() - end + if !from_time.nil? && !to_time.nil? + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) + else + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + end end end end else if current_user.nil? - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - .first() + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) end end end end + query = query.first() end def self.total_account(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - query = Sale.select("distinct b.id as account_id, b.title as title") + query = Sale.select("distinct b.id as account_id, b.title as title") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .joins("JOIN accounts as b ON b.id = a.account_id") - if !from_time.nil? && !to_time.nil? - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end + .where('sales.sale_status = "completed"') + if (!from.nil? && !to.nil?) && (from != "" && to!="") + if current_user.nil? + if !from_time.nil? && !to_time.nil? + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) + else + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - if !from_time.nil? && !to_time.nil? - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end + if !from_time.nil? && !to_time.nil? + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) + else + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) + end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - if !from_time.nil? && !to_time.nil? - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end + if !from_time.nil? && !to_time.nil? + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) + else + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + end end end end else if current_user.nil? - query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) end end end @@ -2508,195 +2315,142 @@ end end def self.account_data(account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}'") if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - if !from_time.nil? && !to_time.nil? - query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - .first() - else - query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - .first() - end + if !from_time.nil? && !to_time.nil? + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - if !from_time.nil? && !to_time.nil? - query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - .first() - else - query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - .first() - end + if !from_time.nil? && !to_time.nil? + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - if !from_time.nil? && !to_time.nil? - query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - .first() - else - query = query.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - .first() - end + if !from_time.nil? && !to_time.nil? + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + end end end end else if current_user.nil? - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - .first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) end end end end + query = query.first() end def self.top_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed'") if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - if !from_time.nil? && !to_time.nil? - query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - query = query.group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + if !from_time.nil? && !to_time.nil? + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - if !from_time.nil? && !to_time.nil? - query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - query = query.group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + if !from_time.nil? && !to_time.nil? + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) + end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - if !from_time.nil? && !to_time.nil? - query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - query = query.group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + if !from_time.nil? && !to_time.nil? + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + end end end end else if current_user.nil? - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - .group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) end end end end + query = query.group("a.product_code") + .order("SUM(a.qty) DESC") + .first() end def self.total_foc_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'") if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") if !from_time.nil? && !to_time.nil? - query = query.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - .count() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) else - query = query.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - .count() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") if !from_time.nil? && !to_time.nil? - query = query.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - .count() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) else - query = query.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - .count() + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - if !from_time.nil? && !to_time.nil? - query = query.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - .count() - else - query = query.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - .count() - end + if !from_time.nil? && !to_time.nil? + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) + else + query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + end end end end else if current_user.nil? - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .count() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .count() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - .count() + query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) end end end end + query = query.count() end #card sale trans data