shop code

This commit is contained in:
Myat Zin Wai Maw
2019-11-22 18:24:02 +06:30
parent 5a3f328789
commit d1ab2c194d
106 changed files with 834 additions and 895 deletions

View File

@@ -7,7 +7,6 @@ class Sale < ApplicationRecord
belongs_to :cashier, foreign_key: "cashier_id", class_name: "Employee"
belongs_to :customer, :optional => true
belongs_to :shift_sale
belongs_to :shop
has_many :sale_items
has_many :sale_discount_items
has_many :sale_discounts
@@ -30,7 +29,6 @@ class Sale < ApplicationRecord
scope :date_between, -> (from, to) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) }
scope :time_between, -> (from, to) { where("TIME(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) }
scope :along_with_sale_payments_except_void, -> { joins("LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND DATE(CONVERT_TZ(sale_payments.created_at,'+00:00','+06:30')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'))") }
def qty_of(item_instance_code)
order_items.select(:order_items_id, :item_instance_code, 'SUM(qty) as qty').where(item_instance_code: item_instance_code).group(:item_instance_code).first
end
@@ -137,8 +135,8 @@ class Sale < ApplicationRecord
order = Order.find(order_id)
# current cashier login
open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift
open_cashier = Employee.where("shop_code='#{order.shop_code}' and role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift(order.shop_code)
# shift with terminal zone
# set cashier
@@ -146,12 +144,12 @@ class Sale < ApplicationRecord
if !booking.dining_facility_id.nil?
table = DiningFacility.find(booking.dining_facility_id)
cashier_zone = CashierTerminalByZone.find_by_zone_id(table.zone_id)
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{cashier_zone.cashier_terminal_id}").first
shift = ShiftSale.where("shop_code='#{order.shop_code}' and shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{cashier_zone.cashier_terminal_id}").first
#for multiple zone with terminal
if shift.nil?
multiple_zone = CashierTerminalByZone.where("zone_id = #{table.zone_id}")
multiple_zone.each do |zone|
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{zone.cashier_terminal_id}").first
shift = ShiftSale.where("shop_code='#{order.shop_code}' and shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{zone.cashier_terminal_id}").first
if !shift.nil? then
break
end
@@ -159,7 +157,7 @@ class Sale < ApplicationRecord
end
end
else
shift = ShiftSale.current_open_shift(cashier.id)
shift = ShiftSale.current_open_shift(cashier)
end
# set cashier
if shift != nil #if current login employee open shift
@@ -171,7 +169,7 @@ class Sale < ApplicationRecord
# table and terminal in multiple shift
self.cashier_id = open_cashier[0].id
self.cashier_name = open_cashier[0].name
shift_id = ShiftSale.current_open_shift(open_cashier[0].id)
shift_id = ShiftSale.current_open_shift(open_cashier[0])
if shift_id
self.shift_sale_id = shift_id.id
else
@@ -189,7 +187,7 @@ class Sale < ApplicationRecord
self.requested_by = requested_by.name
self.requested_at = DateTime.now.utc.getlocal
self.shop_code = order.shop_code
Rails.logger.debug "Order -> #{order.id} | order_status -> #{order.status}"
if order
self.customer_id = order.customer_id
@@ -235,15 +233,13 @@ class Sale < ApplicationRecord
#fOR Quick Service pay and create
def self.request_bill(order,current_user,current_login_employee)
if !ShiftSale.current_shift.nil?
if !ShiftSale.current_shift(order.shop_code).nil?
order_id = order.order_id # order_id
bk_order = BookingOrder.find_by_order_id(order_id)
check_booking = Booking.find_by_booking_id(bk_order.booking_id)
if @sale_data = check_booking.sale
# Create Sale if it doesn't exist
# puts "current_login_employee"
# puts current_login_employee.name
@sale_items = SaleItem.where("sale_id=?",@sale_id)
elsif @sale_data = Sale.generate_invoice_from_booking(check_booking,current_login_employee,current_user,order.source)
@sale_items = SaleItem.where("sale_id=?",@sale_data.sale_id)
@@ -254,7 +250,7 @@ class Sale < ApplicationRecord
# @sale_data.save
# Promotion Activation
Promotion.promo_activate(@sale_data)
Promotion.promo_activate(@sale_data,order.shop_code)
@status = true
return @status, @sale_data
else
@@ -439,7 +435,7 @@ class Sale < ApplicationRecord
#compute - invoice total
def compute_by_sale_items(total_discount, discount_type=nil, order_source=nil, tax_type=nil, type=nil)
shop = Shop.first
# shop = Shop.first
#Computation Fields
subtotal_price = 0
@@ -506,7 +502,7 @@ class Sale < ApplicationRecord
# Tax Re-Calculte
def compute_tax(total_taxable, total_discount = 0, order_source = nil, tax_type=nil)
shop = Shop.first
shop = Shop.find_by_shop_code(self.shop_code)
#if tax is not apply create new record
# SaleTax.where("sale_id='#{sale.sale_id}'").find_each do |existing_tax|
@@ -560,7 +556,7 @@ class Sale < ApplicationRecord
# Tax Calculate
def apply_tax(total_taxable, order_source = nil, tax_type = nil)
shop = Shop.first
shop = Shop.find_by_shop_code(self.shop_code)
#if tax is not apply create new record
# SaleTax.where("sale_id='#{self.sale_id}'").find_each do |existing_tax|
@@ -640,7 +636,7 @@ class Sale < ApplicationRecord
end
def adjust_rounding
shop_details = Shop.first
shop_details = Shop.find_by_shop_code(self.shop_code)
# rounding adjustment
if shop_details.is_rounding_adj
new_total = Sale.get_rounding_adjustment(self.grand_total)
@@ -656,7 +652,7 @@ class Sale < ApplicationRecord
#Generate new Receipt No when it is not assigned
def generate_receipt_no
#shop_code and client_code
shop_details = Shop.first
shop_details = Shop.find_by_shop_code(self.shop_code)
#Date-Shift-
if self.receipt_no.nil?
prefix = DateTime.now().utc
@@ -1526,7 +1522,7 @@ end
.order("SUM(i.qty) ASC").limit(20)
end
else
shift = ShiftSale.current_open_shift(current_user.id,shop)
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.top_bottom(shop,today,shift,from,to,from_time,to_time)
if type == "top"
@@ -1557,7 +1553,7 @@ end
query = query.order("SUM(i.qty) ASC").limit(20)
end
else
shift = ShiftSale.current_open_shift(current_user.id,shop)
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.top_bottom(shop,today,shift).group('i.product_name')
if type == "top"
@@ -1579,7 +1575,7 @@ end
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.hourly_sale_data(shop,today,nil,from,to,from_time,to_time)
else
shift = ShiftSale.current_open_shift(current_user.id,shop)
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.hourly_sale_data(shop,today,shift,from,to,from_time,to_time)
end
@@ -1592,7 +1588,7 @@ end
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.hourly_sale_data(shop,today)
else
shift = ShiftSale.current_open_shift(current_user.id,shop)
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.hourly_sale_data(shop,today,shift)
end
@@ -1603,7 +1599,7 @@ end
def self.employee_sales(today,current_user,from,to,from_time,to_time,shop)
shift = if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
ShiftSale.current_open_shift(current_user.id,shop)
ShiftSale.current_open_shift(current_user)
end
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
@@ -1616,8 +1612,8 @@ end
end
def self.total_trans(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil,shop)
query = shop.sales.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count")
.where('sale_status = "completed"')
query = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count")
.where("sale_status = 'completed' AND shop_code='#{shop.shop_code}'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -1628,7 +1624,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1636,8 +1632,8 @@ end
end
def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil,shop)
query = shop.sales.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
.where('sales.sale_status = "completed" and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")')
query = Sale.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and (sp.payment_method = 'mpu' or sp.payment_method = 'visa' or sp.payment_method = 'master' or sp.payment_method = 'jcb' or sp.payment_method = 'unionpay' or sp.payment_method = 'alipay' or sp.payment_method = 'paymal' or sp.payment_method = 'dinga' or sp.payment_method = 'JunctionPay')")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1647,7 +1643,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1671,7 +1667,7 @@ end
query = query.merge(Sale.date_on(today))
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1680,8 +1676,8 @@ end
end
def self.summary_sale_receipt(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.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"')
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("shop_code='#{shop.shop_code}' and sale_status = 'completed'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1691,7 +1687,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1699,8 +1695,8 @@ end
end
def self.total_payment_methods(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("distinct sp.payment_method")
.where('sales.sale_status = "completed"')
query = Sale.select("distinct sp.payment_method")
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed'")
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -1711,7 +1707,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1721,9 +1717,10 @@ end
def self.payment_sale(shop,payment_method, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
query = shop.sales.select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount")
query = Sale.select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount")
.joins(:sale_payments)
.joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id")
.where("sales.shop_code='#{shop.shop_code}'")
.completed
if payment_method == 'card'
@@ -1742,7 +1739,7 @@ end
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1773,9 +1770,9 @@ end
end
def self.total_dinein_takeaway(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus")
query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and c.membership_id is null')
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and c.membership_id is null")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1785,7 +1782,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1793,9 +1790,9 @@ end
end
def self.total_membership(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("count(distinct sales.customer_id) as total_memb_cus")
query = Sale.select("count(distinct sales.customer_id) as total_memb_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))')
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and ((c.customer_type = 'Dinein' and c.membership_id is not null) or (c.customer_type = 'Takeaway' and c.membership_id is not null))")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1805,7 +1802,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1859,8 +1856,9 @@ end
# end
def self.total_order(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("count(distinct sale_orders.order_id) as total_order")
query = Sale.select("count(distinct sale_orders.order_id) as total_order")
.joins(:sale_orders)
.where("shop_code='#{shop.shop_code}'")
.completed
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -1872,7 +1870,7 @@ end
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1881,10 +1879,10 @@ end
end
def self.total_account(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("distinct b.id as account_id, b.title as title")
query = Sale.select("distinct b.id as account_id, b.title as title")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.joins("JOIN accounts as b ON b.id = a.account_id")
.where('sales.sale_status = "completed"')
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1894,7 +1892,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1902,9 +1900,9 @@ end
end
def self.account_data(shop,account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("count(*) as cnt_acc, SUM(a.price) as total_acc")
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}'")
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and a.account_id ='#{account_id}'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1914,7 +1912,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1922,9 +1920,9 @@ end
end
def self.top_items(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("a.product_name as item_name, SUM(a.price) as item_total_price")
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'")
.where("sales.shop_code='#{shop.shop_code}' and (a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1934,7 +1932,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1944,8 +1942,8 @@ end
end
def self.total_foc_items(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'")
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1955,7 +1953,7 @@ end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -2017,13 +2015,13 @@ end
query = query.where("bookings.booking_status = 'assign' AND orders.status = 'new' AND orders.source =? ","#{type}")
.group("bookings.booking_id")
end
def self.completed_sale(type)
def self.completed_sale(type,shop_code)
if type == "cashier"
type = "and orders.source = 'emenu' or orders.source = 'cashier'"
else
type = "and orders.source = '#{type}'"
end
query = Sale.all
query = Sale.where("sales.shop_code='#{shop_code}'")
query = query.joins("join sale_orders as sale_orders on sale_orders.sale_id = sales.sale_id")
.joins("join orders as orders on orders.order_id = sale_orders.order_id")
query = query.where("sales.sale_status != 'new' AND orders.status = 'billed' #{type}")
@@ -2086,8 +2084,7 @@ def self.get_sale_data_for_other_payment_credit(sale_id)
end
def unique_tax_profiles(order_source, customer_id)
tax_data = TaxProfile
.where(group_type: order_source)
tax_data = TaxProfile.where(group_type: order_source,shop_code: self.shop_code)
customer_tax_profiles = Customer.select(:tax_profiles).where(customer_id: customer_id).first
if customer_tax_profiles.present?
@@ -2099,24 +2096,24 @@ end
def self.top_bottom(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil?
query = shop.sales.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,i.product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id")
if !from_time.nil? && !to_time.nil?
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed'")
else
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
query = query.where("shop_code='#{shop.shop_code}' and (i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and sale_status= 'completed'")
end
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
else
query = shop.sales.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,i.product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id")
.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
.where("shop_code='#{shop.shop_code}' and (i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
" and sale_status= 'completed'")
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
@@ -2127,7 +2124,7 @@ end
def self.hourly_sale_data(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil?
query = shop.sales.select("grand_total")
query = Sale.select("grand_total")
if !from_time.nil? && !to_time.nil?
query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time)
else
@@ -2136,11 +2133,11 @@ def self.hourly_sale_data(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
query = query.where("shop_code='#{shop.shop_code}'").group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
.order('receipt_date')
else
query = shop.sales.select("grand_total")
.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today)
query = Sale.select("grand_total")
.where("shop_code='#{shop.shop_code}' and sale_status = 'completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = ?",today)
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
@@ -2152,9 +2149,9 @@ def self.hourly_sale_data(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_
end
def self.employee_sale(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.joins(:cashier)
query = Sale.joins(:cashier)
.joins(:sale_payments)
.paid.completed
.paid.completed.where("sales.shop_code='#{shop.shop_code}'")
if !from.nil? && !to.nil?
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -2475,7 +2472,7 @@ private
def update_stock_journal
if self.sale_status == "void" && self.sale_status_before_last_save != "void"
self.sale_items.each do |item|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item)
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item,self.shop_code)
if found
stock = StockJournal.where('item_code=?', item.item_instance_code).order("id DESC").first
unless stock.nil?
@@ -2488,7 +2485,7 @@ private
end
elsif self.sale_status == "waste" || self.sale_status == "spoile" || (self.payment_status == "foc" && self.payment_status_was != "foc")
self.bookings.first.order_items.each do |item|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item)
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item,self.shop_code)
if found
if stock_journal = StockJournal.find_by_trans_ref(item.order_items_id)
stock_journal.update(remark: self.sale_status)