From 8e75cb4e433f7f2f829556f3b1a73d980eabf16e Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 3 Sep 2018 10:17:43 +0630 Subject: [PATCH 1/2] change query for credit payment --- app/models/sale.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 275da03d..4e6dc47c 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1678,7 +1678,7 @@ end else query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query}) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") else @@ -1690,7 +1690,7 @@ end else query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query}) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") else @@ -1703,7 +1703,7 @@ end else query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and shift_sale_id='#{shift.id}'") end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query}) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") end @@ -1716,7 +1716,7 @@ end .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query}) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") @@ -1724,7 +1724,7 @@ end .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query}) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? @@ -1733,7 +1733,7 @@ end .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}' and shift_sale_id='#{shift.id}'") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query}) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") end end end @@ -2133,7 +2133,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' @@ -2141,7 +2141,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() end else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' @@ -2152,7 +2152,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' @@ -2160,7 +2160,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() end else shift = ShiftSale.current_open_shift(current_user.id) @@ -2172,7 +2172,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' @@ -2180,7 +2180,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() end end end @@ -2193,7 +2193,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() 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") @@ -2202,7 +2202,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? @@ -2212,7 +2212,7 @@ end 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) + (#{outstanding_query}) - (#{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.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() end end end From 1579618831b8e4e16ee307d749f0a965b4f865e0 Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 3 Sep 2018 10:40:21 +0630 Subject: [PATCH 2/2] change query for credit payment --- app/models/sale.rb | 54 +++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 4e6dc47c..cb120fd0 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1667,7 +1667,17 @@ end changed_query = "SELECT SUM(sales.amount_changed) FROM `sales` - WHERE sales.sale_id=sales.sale_id" + WHERE sales.sale_status='completed'" + + if !from.nil? && !to.nil? + if !from_time.nil? && !to_time.nil? + changed_query += " AND 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 + changed_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" + end + else + changed_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" + end if !from.nil? && !to.nil? if current_user.nil? @@ -1678,7 +1688,7 @@ end else query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + 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, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") else @@ -1690,7 +1700,7 @@ end else query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + 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, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") else @@ -1703,7 +1713,7 @@ end else query = query.where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and shift_sale_id='#{shift.id}'") end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + 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, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") end @@ -1716,7 +1726,7 @@ end .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + .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, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") @@ -1724,7 +1734,7 @@ end .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + .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, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? @@ -1733,7 +1743,7 @@ end .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}' and shift_sale_id='#{shift.id}'") .group("(CASE WHEN (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') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") - .select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + .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, (CASE WHEN (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') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") end end end @@ -2122,7 +2132,17 @@ end changed_query = "SELECT SUM(sales.amount_changed) FROM `sales` - WHERE sales.sale_id=sales.sale_id" + WHERE sales.sale_status='completed'" + + if !from.nil? && !to.nil? + if !from_time.nil? && !to_time.nil? + changed_query += " AND 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 + changed_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" + end + else + changed_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" + end if !from.nil? && !to.nil? if current_user.nil? @@ -2133,7 +2153,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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() else query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' @@ -2141,7 +2161,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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 else if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' @@ -2152,7 +2172,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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() else query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' @@ -2160,7 +2180,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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 else shift = ShiftSale.current_open_shift(current_user.id) @@ -2172,7 +2192,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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() else query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' @@ -2180,7 +2200,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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 end end @@ -2193,7 +2213,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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() 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") @@ -2202,7 +2222,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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() else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? @@ -2212,7 +2232,7 @@ end 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) + (#{outstanding_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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 end end