diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 0c89bace..7d87ea8f 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -119,8 +119,17 @@ class HomeController < ApplicationController @hourly_sales = Sale.hourly_sales(today,current_user,@from,@to,@from_time,@to_time).sum(:grand_total) # .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p') # .sum(:grand_total) - @employee_sales = Sale.employee_sales(today,current_user,@from,@to,@from_time,@to_time) - .sum('(CASE WHEN sp.payment_method="cash" THEN (sp.payment_amount - sales.amount_changed) ELSE sp.payment_amount END)') + + employee_sales = Sale.employee_sales(today,current_user,@from,@to,@from_time,@to_time) + # .sum("(CASE WHEN sp.payment_method='cash' THEN ((sp.payment_amount) - (sales.amount_changed)) ELSE (sp.payment_amount) END)") + @employee_sales = [] + if !employee_sales.nil? + employee_sales.each do |emp| + emp_data = [] + emp_data.push([emp.e_name, emp.payment_amount]) + @employee_sales.push({'name' => emp.payment_method, 'data' => emp_data}) + end + end @inventories = StockJournal.inventory_balances(today,@from,@to,@from_time,@to_time).sum(:balance) @total_trans = Sale.total_trans(today,current_user,@from,@to,@from_time,@to_time) diff --git a/app/controllers/origami/home_controller.rb b/app/controllers/origami/home_controller.rb index 71ce212d..46c7e236 100755 --- a/app/controllers/origami/home_controller.rb +++ b/app/controllers/origami/home_controller.rb @@ -11,6 +11,7 @@ class Origami::HomeController < BaseOrigamiController @customers = Customer.pluck("customer_id, name") @shop = shop_detail + @occupied_table = DiningFacility.where("status='occupied'").count; # @shift = ShiftSale.current_open_shift(current_user.id) end diff --git a/app/controllers/origami/pending_order_controller.rb b/app/controllers/origami/pending_order_controller.rb index 594e4b7c..f66c4017 100644 --- a/app/controllers/origami/pending_order_controller.rb +++ b/app/controllers/origami/pending_order_controller.rb @@ -6,12 +6,17 @@ class Origami::PendingOrderController < BaseOrigamiController @order = Sale.pending_order @completed = Sale.completed_sale("quick_service") @customers = Customer.pluck("customer_id, name") + + @occupied_table = @sale.count.length + @order.count.length end def show @sales = Sale.pending_sale @orders = Sale.pending_order @completed = Sale.completed_sale("quick_service") @customers = Customer.pluck("customer_id, name") + + @occupied_table = @sales.count.length + @orders.count.length + @id = params[:sale_id] if(@id[0,3] == "SAL") @sale = Sale.find(@id) @@ -50,6 +55,9 @@ class Origami::PendingOrderController < BaseOrigamiController @orders = Sale.pending_order @completed = Sale.completed_sale("quick_service") @customers = Customer.pluck("customer_id, name") + + @occupied_table = @sales.count.length + @orders.count.length + @id = params[:sale_id] @sale = Sale.find(@id) @@ -78,6 +86,9 @@ class Origami::PendingOrderController < BaseOrigamiController @sales = Sale.pending_sale @orders = Sale.pending_order @completed = Sale.completed_sale("quick_service") + + @occupied_table = @sales.count.length + @orders.count.length + @id = params[:sale_id] @customers = Customer.pluck("customer_id, name") diff --git a/app/models/sale.rb b/app/models/sale.rb index c2a6e6bc..63189759 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1550,6 +1550,23 @@ end end def self.employee_sales(today,current_user,from,to,from_time,to_time) + #sub query for credit payment + sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) AS payment_amount + FROM sale_payments + INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id + INNER JOIN sales ON sale_audits.sale_id = sales.sale_id + WHERE sales.sale_status='completed'" + if !from.nil? && !to.nil? + if !from_time.nil? && !to_time.nil? + sub_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 + sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" + end + else + sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" + end + #sub query for credit payment + if !from.nil? && !to.nil? if current_user.nil? query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") @@ -1559,7 +1576,8 @@ 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.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") + query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) 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 if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' @@ -1570,7 +1588,8 @@ 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.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") + query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) 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 shift = ShiftSale.current_open_shift(current_user.id) @@ -1582,7 +1601,8 @@ 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.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") + query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) 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 end @@ -1594,6 +1614,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) - SUM(sales.amount_changed)) 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") @@ -1601,6 +1622,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) - SUM(sales.amount_changed)) 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? @@ -1609,6 +1631,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) - SUM(sales.amount_changed)) 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 @@ -1718,63 +1741,106 @@ end end def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) + sub_query = SalePayment.select("(CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) as total_credit_payment") + .joins(" JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id") + .joins(" JOIN sales ON sale_audits.sale_id = sales.sale_id") + .where("sales.sale_status='completed'") + if !from.nil? && !to.nil? if current_user.nil? 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}'") + 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") + 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 + sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'") 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") + 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 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? + 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}'") + 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") + 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 + sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'") 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") + 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 end else shift = ShiftSale.current_open_shift(current_user.id) if !shift.nil? 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}'") 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") + 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 + sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'") 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") + 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 end end end end else + sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{today}' AND '#{today}'") 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") + 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") + 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") + + 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 end end end end + return query end def self.summary_sale_receipt(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -1902,68 +1968,82 @@ end if !from_time.nil? && !to_time.nil? time_query = " and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" end + + sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) + FROM sale_payments + INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id + INNER JOIN sales ON sale_audits.sale_id = sales.sale_id + WHERE sales.sale_status='completed'" + if !from.nil? && !to.nil? if current_user.nil? if !from_time.nil? && !to_time.nil? + sub_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}'" 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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else + sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" 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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) 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' if !from_time.nil? && !to_time.nil? + sub_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}'" 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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else + sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" 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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) 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) if !shift.nil? if !from_time.nil? && !to_time.nil? + sub_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}'" 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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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) - SUM(sales.amount_changed)) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else + sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" 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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + 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) - SUM(sales.amount_changed)) 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 end else + sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" if current_user.nil? query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' @@ -1971,7 +2051,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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) 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") @@ -1980,7 +2060,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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) 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? @@ -1990,7 +2070,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) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) 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 diff --git a/app/views/origami/home/index.html.erb b/app/views/origami/home/index.html.erb index 5c374475..6e5167e5 100755 --- a/app/views/origami/home/index.html.erb +++ b/app/views/origami/home/index.html.erb @@ -184,7 +184,7 @@ - + <%end%> @@ -319,19 +319,26 @@ $('#cash_out').on('click',function(){ $('#close_cashier').on('click',function(e){ e.preventDefault(); // Prevent the href from redirecting directly var linkURL = '/origami/shift/cashier/close'; - warnBeforeRedirect(linkURL); + occupied_count = $(this).data("count"); + warnBeforeRedirect(linkURL,occupied_count); }) $('#back').on('click',function(){ window.location.href = '/origami/dashboard'; }) -function warnBeforeRedirect(linkURL) { +function warnBeforeRedirect(linkURL,occupied_count) { + if (occupied_count>0) { + text = "
We have "+occupied_count+" occupied table
.
Are you sure you want to close cashier? "; + }else{ + text = "Are you sure you want to close cashier?"; + } swal({ title: "Alert!", - text: "Are you sure you want to close cashier?", + text: text, type: "warning", - showCancelButton: true + showCancelButton: true, + html: true }, function() { // Redirect the user window.location.href = linkURL; diff --git a/app/views/origami/payments/show.html.erb b/app/views/origami/payments/show.html.erb index fec0adda..6cb85113 100755 --- a/app/views/origami/payments/show.html.erb +++ b/app/views/origami/payments/show.html.erb @@ -401,26 +401,34 @@ <% if current_login_employee.role == "cashier" %> - <% if @other_payment <= 0 && @sale_payment.nil? %> + <% if @other_payment < 0 && @sale_payment.nil? %> <%if @sale_data.total_discount == 0 %> FOC <% end %> <%end %> <% if @sale_payment.nil? %> Void - Waste - Spoile + <% if @other_payment < 0 %> + <%if @sale_data.total_discount == 0 %> + Waste + Spoile + <% end %> + <% end %> <% end %> <% else %> - <% if @other_payment <= 0 && @sale_payment.nil?%> + <% if @other_payment < 0 && @sale_payment.nil?%> <%if @sale_data.total_discount == 0 %> - + <% if @other_payment < 0 %> + <%if @sale_data.total_discount == 0 %> + + + <% end %> + <% end %> <% end %> <% end %> diff --git a/app/views/origami/pending_order/completed_sale.html.erb b/app/views/origami/pending_order/completed_sale.html.erb index 7540a1e0..bc4719f9 100644 --- a/app/views/origami/pending_order/completed_sale.html.erb +++ b/app/views/origami/pending_order/completed_sale.html.erb @@ -224,7 +224,7 @@ <% end %> <% end %> - + <% end %> @@ -337,8 +337,30 @@ $(document).ready(function(){ $('#close_cashier').on('click',function(e){ e.preventDefault(); // Prevent the href from redirecting directly var linkURL = '/origami/shift/quick_service/close'; - warnBeforeRedirect(linkURL); + occupied_count = $(this).data("count"); + warnBeforeRedirect(linkURL,occupied_count); }); + + + function warnBeforeRedirect(linkURL,occupied_count) { + + if (occupied_count>0) { + text = "
We have "+occupied_count+" pending orders and sales
.
Are you sure you want to close cashier? "; + }else{ + text = "Are you sure you want to close cashier?"; + } + swal({ + title: "Alert!", + text: text, + type: "warning", + showCancelButton: true, + html: true + }, function() { + // Redirect the user + window.location.href = linkURL; + }); + } + $('#cash_in').on('click',function(){ window.location.href = '/origami/quick_service/cash_ins'; }) diff --git a/app/views/origami/pending_order/credit_sale.html.erb b/app/views/origami/pending_order/credit_sale.html.erb index aefc3c98..f8fd7ceb 100644 --- a/app/views/origami/pending_order/credit_sale.html.erb +++ b/app/views/origami/pending_order/credit_sale.html.erb @@ -215,7 +215,7 @@ <% if current_user.role != "waiter" %> - + <% end %> @@ -255,7 +255,9 @@ $(document).ready(function(){ $('#close_cashier').on('click',function(e){ e.preventDefault(); // Prevent the href from redirecting directly var linkURL = '/origami/shift/quick_service/close'; - warnBeforeRedirect(linkURL); + occupied_count = $(this).data("count"); + + warnBeforeRedirect(linkURL,occupied_count); }); $('#cash_in').on('click',function(){ @@ -270,5 +272,25 @@ $(document).ready(function(){ var sale_id = '<%= @sale.sale_id %>'; window.location.href = '/origami/sale/'+ sale_id + "/"+cashier_type+"/credit_payment"; }); + + + function warnBeforeRedirect(linkURL,occupied_count) { + + if (occupied_count>0) { + text = "
We have "+occupied_count+" pending orders and sales
.
Are you sure you want to close cashier? "; + }else{ + text = "Are you sure you want to close cashier?"; + } + swal({ + title: "Alert!", + text: text, + type: "warning", + showCancelButton: true, + html: true + }, function() { + // Redirect the user + window.location.href = linkURL; + }); + } }); diff --git a/app/views/origami/pending_order/index.html.erb b/app/views/origami/pending_order/index.html.erb index f785449f..a6e91d63 100644 --- a/app/views/origami/pending_order/index.html.erb +++ b/app/views/origami/pending_order/index.html.erb @@ -91,7 +91,7 @@ <%= t("views.btn.back") %> <% if current_user.role != "waiter" %> - + <% end %> @@ -135,7 +135,8 @@ $('#close_cashier').on('click',function(e){ e.preventDefault(); // Prevent the href from redirecting directly var linkURL = '/origami/shift/quick_service/close'; - warnBeforeRedirect(linkURL); + occupied_count = $(this).data("count"); + warnBeforeRedirect(linkURL,occupied_count); }); $('#cash_in').on('click',function(){ @@ -147,11 +148,18 @@ }); }); - function warnBeforeRedirect(linkURL) { + function warnBeforeRedirect(linkURL,occupied_count) { + + if (occupied_count>0) { + text = "
We have "+occupied_count+" pending orders and sales
.
Are you sure you want to close cashier? "; + }else{ + text = "Are you sure you want to close cashier?"; + } swal({ title: "Alert!", - text: "Are you sure you want to close cashier?", + text: text, type: "warning", + html: true, showCancelButton: true }, function() { // Redirect the user diff --git a/app/views/origami/pending_order/show.html.erb b/app/views/origami/pending_order/show.html.erb index 9658f1c5..bf0e7d3a 100644 --- a/app/views/origami/pending_order/show.html.erb +++ b/app/views/origami/pending_order/show.html.erb @@ -224,7 +224,7 @@ <%end%> <% if current_user.role != "waiter" %> - + <% end %> @@ -353,15 +353,23 @@ $(document).ready(function(){ $('#close_cashier').on('click',function(e){ e.preventDefault(); // Prevent the href from redirecting directly var linkURL = '/origami/shift/quick_service/close'; - warnBeforeRedirect(linkURL); + occupied_count = $(this).data("count"); + warnBeforeRedirect(linkURL,occupied_count); }); - function warnBeforeRedirect(linkURL) { + function warnBeforeRedirect(linkURL,occupied_count) { + + if (occupied_count>0) { + text = "
We have "+occupied_count+" pending orders and sales
.
Are you sure you want to close cashier? "; + }else{ + text = "Are you sure you want to close cashier?"; + } swal({ title: "Alert!", - text: "Are you sure you want to close cashier?", + text: text, type: "warning", - showCancelButton: true + showCancelButton: true, + html: true }, function() { // Redirect the user window.location.href = linkURL; diff --git a/app/views/origami/shifts/new.html.erb b/app/views/origami/shifts/new.html.erb index c1de7927..7a98a4d2 100755 --- a/app/views/origami/shifts/new.html.erb +++ b/app/views/origami/shifts/new.html.erb @@ -109,11 +109,6 @@ }); }) -// $("#print_receipt").on('click touchstart', function(event){ - -// print_receipt(); -// }); - $('#open_cashier').on('click touchstart', function(event){ $(this).off("click touchstart touchend"); var cashier_terminal = $('#cashier_terminal').val();