|
|
|
|
@@ -548,11 +548,22 @@ class Sale < ApplicationRecord
|
|
|
|
|
|
|
|
|
|
#Generate new Receipt No when it is not assigned
|
|
|
|
|
def generate_receipt_no
|
|
|
|
|
#shop_code and client_code
|
|
|
|
|
shop_details = Shop::ShopDetail
|
|
|
|
|
|
|
|
|
|
#Date-Shift-
|
|
|
|
|
if self.receipt_no.nil?
|
|
|
|
|
prefix = DateTime.now().utc
|
|
|
|
|
#self.receipt_no = prefix.to_s + "/" + self.shit_id.to_s + "/" + SeedGenerator.new_receipt_no().to_s
|
|
|
|
|
self.receipt_no = prefix.strftime("%Y%m%d") + "-" + SeedGenerator.new_receipt_no().to_s
|
|
|
|
|
if !shop_details.nil?
|
|
|
|
|
if !shop_details.shop_code.nil?
|
|
|
|
|
self.receipt_no = shop_details.shop_code + "-" + prefix.strftime("%Y%m%d") + "-" + SeedGenerator.new_receipt_no().to_s
|
|
|
|
|
else
|
|
|
|
|
self.receipt_no = prefix.strftime("%Y%m%d") + "-" + SeedGenerator.new_receipt_no().to_s
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
self.receipt_no = prefix.strftime("%Y%m%d") + "-" + SeedGenerator.new_receipt_no().to_s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
self.receipt_date = prefix
|
|
|
|
|
Rails.logger.debug "Receipt No #{self.receipt_no} | Date #{ self.receipt_date.to_s}"
|
|
|
|
|
@@ -1170,7 +1181,7 @@ end
|
|
|
|
|
if current_user.nil?
|
|
|
|
|
total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total")
|
|
|
|
|
else
|
|
|
|
|
shift = ShiftSale.current_open_shift(current_user.id)
|
|
|
|
|
@@ -1184,7 +1195,7 @@ end
|
|
|
|
|
if current_user.nil?
|
|
|
|
|
total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total")
|
|
|
|
|
else
|
|
|
|
|
shift = ShiftSale.current_open_shift(current_user.id)
|
|
|
|
|
@@ -1202,7 +1213,7 @@ end
|
|
|
|
|
if current_user.nil?
|
|
|
|
|
total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count
|
|
|
|
|
else
|
|
|
|
|
shift = ShiftSale.current_open_shift(current_user.id)
|
|
|
|
|
@@ -1215,7 +1226,7 @@ end
|
|
|
|
|
if current_user.nil?
|
|
|
|
|
total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count
|
|
|
|
|
else
|
|
|
|
|
shift = ShiftSale.current_open_shift(current_user.id)
|
|
|
|
|
@@ -1234,7 +1245,7 @@ end
|
|
|
|
|
.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
.sum("sp.payment_amount")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.where('sales.sale_status = "completed" and sales.receipt_date 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")',from,to)
|
|
|
|
|
.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
.sum("sp.payment_amount")
|
|
|
|
|
@@ -1253,7 +1264,7 @@ end
|
|
|
|
|
.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
.sum("sp.payment_amount")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.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")',today)
|
|
|
|
|
.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
.sum("sp.payment_amount")
|
|
|
|
|
@@ -1276,7 +1287,7 @@ end
|
|
|
|
|
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
|
|
|
|
|
.sum("payment_amount")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to)
|
|
|
|
|
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
|
|
|
|
|
.sum("payment_amount")
|
|
|
|
|
@@ -1295,7 +1306,7 @@ end
|
|
|
|
|
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
|
|
|
|
|
.sum("payment_amount")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today)
|
|
|
|
|
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
|
|
|
|
|
.sum("payment_amount")
|
|
|
|
|
@@ -1318,7 +1329,7 @@ end
|
|
|
|
|
.where('sale_status = "completed" and receipt_date between ? and ?',from,to)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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 receipt_date between ? and ?',from,to)
|
|
|
|
|
.first()
|
|
|
|
|
@@ -1337,7 +1348,7 @@ end
|
|
|
|
|
.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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(receipt_date,"%Y-%m-%d") = ?',today)
|
|
|
|
|
.first()
|
|
|
|
|
@@ -1360,7 +1371,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to)
|
|
|
|
|
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.select("distinct sp.payment_method")
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to)
|
|
|
|
|
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
@@ -1379,7 +1390,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
|
|
|
|
|
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.select("distinct sp.payment_method")
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
|
|
|
|
|
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
|
|
|
|
|
@@ -1406,7 +1417,7 @@ end
|
|
|
|
|
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()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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 sales.receipt_date 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")',from,to)
|
|
|
|
|
@@ -1437,7 +1448,7 @@ end
|
|
|
|
|
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()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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")',today)
|
|
|
|
|
@@ -1482,7 +1493,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.select("count(sales.customer_id) as total_dinein_cus")
|
|
|
|
|
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to)
|
|
|
|
|
@@ -1504,7 +1515,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.select("count(sales.customer_id) as total_dinein_cus")
|
|
|
|
|
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today)
|
|
|
|
|
@@ -1530,7 +1541,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.select("count(sales.customer_id) as total_take_cus")
|
|
|
|
|
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to)
|
|
|
|
|
@@ -1552,7 +1563,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.select("count(sales.customer_id) as total_take_cus")
|
|
|
|
|
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today)
|
|
|
|
|
@@ -1578,7 +1589,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date 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()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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 sales.receipt_date 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)
|
|
|
|
|
@@ -1600,7 +1611,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%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()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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(sales.receipt_date,"%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)
|
|
|
|
|
@@ -1626,7 +1637,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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 sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to)
|
|
|
|
|
@@ -1648,7 +1659,7 @@ end
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today)
|
|
|
|
|
@@ -1675,7 +1686,7 @@ end
|
|
|
|
|
.where('b.status = "billed" and sales.receipt_date between ? and ?',from,to)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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")
|
|
|
|
|
@@ -1700,7 +1711,7 @@ end
|
|
|
|
|
.where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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")
|
|
|
|
|
@@ -1728,7 +1739,7 @@ end
|
|
|
|
|
.joins("JOIN accounts as b ON b.id = a.account_id")
|
|
|
|
|
.where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to)
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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")
|
|
|
|
|
@@ -1750,7 +1761,7 @@ end
|
|
|
|
|
.joins("JOIN accounts as b ON b.id = a.account_id")
|
|
|
|
|
.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today)
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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")
|
|
|
|
|
@@ -1776,7 +1787,7 @@ end
|
|
|
|
|
.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ?",from,to)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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 sales.receipt_date between ? and ?",from,to)
|
|
|
|
|
@@ -1798,7 +1809,7 @@ end
|
|
|
|
|
.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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)
|
|
|
|
|
@@ -1826,7 +1837,7 @@ end
|
|
|
|
|
.order("SUM(a.qty) DESC")
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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 sales.receipt_date between ? and ?",from,to)
|
|
|
|
|
@@ -1854,7 +1865,7 @@ end
|
|
|
|
|
.order("SUM(a.qty) DESC")
|
|
|
|
|
.first()
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
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)
|
|
|
|
|
@@ -1883,7 +1894,7 @@ end
|
|
|
|
|
.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to)
|
|
|
|
|
.sum("a.qty")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
|
|
|
|
|
.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to)
|
|
|
|
|
.sum("a.qty")
|
|
|
|
|
@@ -1902,7 +1913,7 @@ end
|
|
|
|
|
.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
|
|
|
|
|
.sum("a.qty")
|
|
|
|
|
else
|
|
|
|
|
if current_user.role == 'administrator'
|
|
|
|
|
if current_user.role == 'administrator' || current_user.role == 'manager'
|
|
|
|
|
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
|
|
|
|
|
.where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today)
|
|
|
|
|
.sum("a.qty")
|
|
|
|
|
@@ -2011,6 +2022,44 @@ end
|
|
|
|
|
.group("bookings.booking_id")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.all_receipts
|
|
|
|
|
query = Sale.select("sales.*, sale_payments.created_at as receipt_close_time,
|
|
|
|
|
case when (sale_audits.action='SALEPAYMENT') then sale_audits.remark else 0 end as remark,
|
|
|
|
|
case when (sale_taxes.tax_name='Service Charges') then sale_taxes.tax_payable_amount else 0 end as service_charges,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='mpu') then sale_payments.payment_amount else 0 end) as mpu_amount,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='master') then sale_payments.payment_amount else 0 end) as master_amount,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='visa') then sale_payments.payment_amount else 0 end) as visa_amount,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='jcb') then sale_payments.payment_amount else 0 end) as jcb_amount,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='unionpay') then sale_payments.payment_amount else 0 end) as unionpay_amount,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='creditnote') then sale_payments.payment_amount else 0 end) as credit_amount,
|
|
|
|
|
SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount,
|
|
|
|
|
SUM(case when (sale_items.status='foc') then sale_items.price else 0 end) as item_foc,
|
|
|
|
|
SUM(case when (sale_items.status='Discount') then sale_items.price else 0 end) as item_discount,
|
|
|
|
|
SUM(sale_items.qty) as qty,
|
|
|
|
|
sales.cashier_name as cashier_name,
|
|
|
|
|
surveys.child as child,
|
|
|
|
|
surveys.adult as adult")
|
|
|
|
|
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
|
|
|
|
|
.joins("join sale_taxes on sale_taxes.sale_id = sales.sale_id")
|
|
|
|
|
.joins("join sale_items on sale_items.sale_id = sales.sale_id")
|
|
|
|
|
.joins("join sale_audits on sale_audits.sale_id = sales.sale_id")
|
|
|
|
|
.joins("left join surveys on surveys.receipt_no = sales.receipt_no")
|
|
|
|
|
|
|
|
|
|
query = query.where("sale_status != 'new' && sale_payments.payment_amount > 0")
|
|
|
|
|
.group("sale_id")
|
|
|
|
|
return query
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.all_receipt_details
|
|
|
|
|
query = SaleItem.select("sale_items.*, sale_payments.created_at as receipt_close_time,
|
|
|
|
|
sales.requested_at as requested_at, sales.receipt_no as receipt_no,sales.sale_id as s_id")
|
|
|
|
|
.joins("join sale_payments on sale_payments.sale_id = sale_items.sale_id")
|
|
|
|
|
.joins("join sales on sales.sale_id = sale_items.sale_id")
|
|
|
|
|
.group("sale_items.sale_item_id")
|
|
|
|
|
query = query.where("qty > 0 and sales.sale_status !='new'")
|
|
|
|
|
return query
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def generate_custom_id
|
|
|
|
|
|