room charges
This commit is contained in:
@@ -2,40 +2,75 @@ class DiningCharge < ApplicationRecord
|
||||
belongs_to :table
|
||||
belongs_to :room
|
||||
|
||||
def amount_calculate(dining_charges_obj, checkin , checkout)
|
||||
def self.amount_calculate(dining_charges_obj, checkin , checkout)
|
||||
|
||||
if !checkin.nil? && !checkout.nil? && !dining_charges_obj.nil?
|
||||
|
||||
minutes = ((checkin - checkout) * 24 * 60).to_i # stay minutes
|
||||
dining_minutes = minutes - dining_charges_obj.minimum_free_time # stayminutes - free minutes
|
||||
price = 0
|
||||
minutes = DiningCharge.time_diff(checkout, checkin)
|
||||
free_time = DiningCharge.convert_to_minutes(dining_charges_obj.minimum_free_time.strftime('%H:%M'))
|
||||
dining_minutes = minutes - free_time # stayminutes - free minutes
|
||||
charge_type = dining_charges_obj.charge_type
|
||||
if charge_type == 'hr'
|
||||
|
||||
price = DiningCharge.charges(dining_charges_obj, dining_minutes, 'hr')
|
||||
elsif charge_type == 'day'
|
||||
price = charge_by_day
|
||||
price = charges(dining_charges_obj, dining_minutes, 'day')
|
||||
end
|
||||
|
||||
return price
|
||||
else
|
||||
puts "<<<<<<<< NO"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def charge_by_hour
|
||||
|
||||
end
|
||||
|
||||
def charge_by_day(chargesObj, dining_minutes)
|
||||
minues_per_day = 12 * 60
|
||||
result = dining_minutes / minues_per_day
|
||||
if result < 1
|
||||
def self.charges(chargesObj, dining_minutes, type)
|
||||
solid_price = 0
|
||||
block = DiningCharge.convert_to_minutes(chargesObj.charge_block.strftime('%H:%M'))
|
||||
result = dining_minutes / block
|
||||
if result.to_i < 1
|
||||
return chargesObj.unit_price
|
||||
elsif result > 1
|
||||
elsif result.to_i >= 1
|
||||
solid_price = result * chargesObj.unit_price
|
||||
|
||||
remain_value = dining_minutes % minues_per_day
|
||||
roundingblock = remain_value / chargesObj.time_rounding_block
|
||||
if roundingblock > 1
|
||||
|
||||
remain_value = dining_minutes % block
|
||||
rounding_block = DiningCharge.convert_to_minutes(chargesObj.time_rounding_block.strftime('%H:%M'))
|
||||
roundingblock = remain_value / rounding_block
|
||||
if roundingblock.to_i < 1
|
||||
return DiningCharge.check_rounding(chargesObj, solid_price)
|
||||
else
|
||||
solid_price += roundingblock * chargesObj.time_rounding_block_price
|
||||
remain_rounding = dining_minutes % block
|
||||
if remain_rounding.to_i < 1
|
||||
return DiningCharge.check_rounding(chargesObj, solid_price)
|
||||
else
|
||||
return solid_price
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.check_rounding(chargesObj)
|
||||
if chargesObj.time_rounding == "down"
|
||||
return solid_price
|
||||
else
|
||||
return solid_price += chargesObj.time_rounding_block_price
|
||||
end
|
||||
end
|
||||
|
||||
def self.time_diff(start_time, end_time)
|
||||
seconds_diff = (start_time - end_time).to_i.abs
|
||||
hours = seconds_diff / 3600
|
||||
seconds_diff -= hours * 3600
|
||||
|
||||
minutes = seconds_diff / 60
|
||||
seconds_diff -= minutes * 60
|
||||
|
||||
seconds = seconds_diff
|
||||
return hours * 60 + minutes
|
||||
end
|
||||
|
||||
def self.convert_to_minutes(time)
|
||||
arr = time.split(":")
|
||||
hour = arr[0].to_i * 60
|
||||
return hour + arr[1].to_i
|
||||
end
|
||||
end
|
||||
|
||||
@@ -109,6 +109,14 @@ class Sale < ApplicationRecord
|
||||
booking.checkout_by = requested_by.name
|
||||
booking.save
|
||||
|
||||
# dining charges
|
||||
charges = DiningCharge.where('dining_facility_id=?',booking.dining_facility_id).take
|
||||
if !charges.nil?
|
||||
diningprice = DiningCharge.amount_calculate(charges, booking.checkin_at, booking.checkout_at)
|
||||
dining_time = booking.checkin_at.strftime('%H:%M %p').to_s + " - " + booking.checkout_at.strftime('%H:%M %p').to_s
|
||||
create_saleitem_diningcharges(charges, diningprice, booking.dining_facility.name, dining_time)
|
||||
end
|
||||
|
||||
return true, self.id
|
||||
end
|
||||
|
||||
@@ -162,6 +170,20 @@ class Sale < ApplicationRecord
|
||||
self.sale_items << sale_item
|
||||
end
|
||||
|
||||
def create_saleitem_diningcharges(chargeObj, diningprice, dining_name, dining_time)
|
||||
sale_item = SaleItem.new
|
||||
sale_item.product_code = chargeObj.item_code
|
||||
sale_item.product_name = dining_name.to_s + " ( " + dining_time.to_s + " )"
|
||||
# sale_item.account_id = item.account_id
|
||||
sale_item.product_alt_name = "-"
|
||||
sale_item.qty = 1
|
||||
sale_item.unit_price = chargeObj.unit_price
|
||||
sale_item.taxable_price = diningprice
|
||||
sale_item.is_taxable = chargeObj.taxable
|
||||
sale_item.sale_id = self.id
|
||||
sale_item.price = diningprice
|
||||
sale_item.save
|
||||
end
|
||||
|
||||
def update_item (item)
|
||||
#save sale_audit
|
||||
@@ -314,7 +336,7 @@ class Sale < ApplicationRecord
|
||||
total_tax_amount = 0
|
||||
#tax_profile - list by order_by
|
||||
tax_profiles = TaxProfile.all.order("order_by asc")
|
||||
|
||||
|
||||
# #Creat new tax records
|
||||
tax_profiles.each do |tax|
|
||||
sale_tax = SaleTax.new(:sale => self)
|
||||
@@ -385,7 +407,7 @@ class Sale < ApplicationRecord
|
||||
else
|
||||
keyword = "receipt_no LIKE ? OR cashier_name LIKE ? OR sale_status ='#{filter}'","%#{filter}%","%#{filter}%"
|
||||
end
|
||||
|
||||
|
||||
if from.present? && to.present?
|
||||
sale = Sale.where("DATE_FORMAT(receipt_date,'%d-%m-%Y') >= ?" + " AND DATE_FORMAT(receipt_date,'%d-%m-%Y') <= ? and NOT sale_status = 'void' ", from,to)
|
||||
query = sale.where(keyword)
|
||||
@@ -552,67 +574,67 @@ def self.get_by_shift_sale(from,to,status)
|
||||
query = ShiftSale.select("shift_sales.id ,shift_started_at AS opening_date,
|
||||
shift_closed_at As closing_date," +
|
||||
" grand_total AS grand_total, cash_sales AS cash," +
|
||||
"total_taxes AS total_tax,total_discounts As total_discount")
|
||||
"total_taxes AS total_tax,total_discounts As total_discount")
|
||||
.order("shift_sales.id DESC")
|
||||
return query = query.where("shift_sales.shift_started_at >= ?" + " AND shift_sales.shift_closed_at <= ?", from,to)
|
||||
end
|
||||
|
||||
def self.get_item_query()
|
||||
query = Sale.select("acc.title as account_name,mi.account_id, mi.item_code as item_code, " +
|
||||
query = Sale.select("acc.title as account_name,mi.account_id, mi.item_code as item_code, " +
|
||||
"(SUM(i.qty * i.unit_price)) as grand_total,SUM(i.qty) as total_item," +
|
||||
" i.unit_price as unit_price,mi.name as product_name, mc.name as" +
|
||||
" menu_category_name,mc.id as menu_category_id ")
|
||||
|
||||
query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code" +
|
||||
|
||||
query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code" +
|
||||
" JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id JOIN menu_categories mc ON mc.id = mi.menu_category_id ")
|
||||
# "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ")
|
||||
|
||||
query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id")
|
||||
query = query.group('i.product_code ').order("mi.account_id, mi.menu_category_id")
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
|
||||
|
||||
|
||||
# date_type_selection = get_sql_function_for_report_type(report_type)
|
||||
|
||||
|
||||
query = self.get_item_query()
|
||||
discount_query = 0
|
||||
total_card_amount = 0
|
||||
total_cash_amount = 0
|
||||
total_credit_amount = 0
|
||||
total_foc_amount = 0
|
||||
total_grand_total = 0
|
||||
total_grand_total = 0
|
||||
|
||||
if shift.present?
|
||||
query = query.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift.to_a)
|
||||
discount_query = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:total_discount)
|
||||
change_amount = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:amount_changed)
|
||||
discount_query = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:total_discount)
|
||||
change_amount = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:amount_changed)
|
||||
sale_cash = Sale.select("SUM(case when (sale_payments.payment_method ='mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar') then (sale_payments.payment_amount) else 0 end) as card_amount,
|
||||
SUM(case when (sale_payments.payment_method='cash') then (sale_payments.payment_amount) else 0 end) as cash_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='cash') then (sale_payments.payment_amount) else 0 end) as cash_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")
|
||||
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
|
||||
.where("sales.shift_sale_id in (?) and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift.to_a)
|
||||
.where("sales.shift_sale_id in (?) and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift.to_a)
|
||||
sale_cash.each do |s_c|
|
||||
total_cash_amount += s_c.cash_amount.to_f
|
||||
total_card_amount += s_c.card_amount.to_f
|
||||
total_credit_amount += s_c.credit_amount.to_f
|
||||
total_foc_amount += s_c.foc_amount.to_f
|
||||
end
|
||||
total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f
|
||||
|
||||
total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f
|
||||
|
||||
### => get all sales range in shift_sales
|
||||
elsif shift_sale_range.present?
|
||||
query = query.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift_sale_range.to_a)
|
||||
discount_query = Sale.where("sales.shift_sale_id IN (?) and sale_status ='completed'", shift_sale_range.to_a).sum(:total_discount)
|
||||
change_amount = Sale.where("sales.shift_sale_id IN (?) and sale_status ='completed'", shift_sale_range.to_a).sum(:amount_changed)
|
||||
sale_cash = Sale.select("SUM(case when (sale_payments.payment_method = 'mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar') then (sale_payments.payment_amount) else 0 end) as card_amount,
|
||||
SUM(case when (sale_payments.payment_method='cash') then (sale_payments.payment_amount) else 0 end) as cash_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='cash') then (sale_payments.payment_amount) else 0 end) as cash_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")
|
||||
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
|
||||
.where("sales.shift_sale_id in (?) and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift_sale_range.to_a)
|
||||
.where("sales.shift_sale_id in (?) and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift_sale_range.to_a)
|
||||
sale_cash.each do |s_c|
|
||||
total_cash_amount += s_c.cash_amount.to_f
|
||||
total_card_amount += s_c.card_amount.to_f
|
||||
@@ -620,17 +642,17 @@ def self.get_item_query()
|
||||
total_foc_amount += s_c.foc_amount.to_f
|
||||
end
|
||||
total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f
|
||||
|
||||
|
||||
else
|
||||
query = query.where("sales.receipt_date between ? and ? and sale_status='completed'",from,to)
|
||||
discount_query = Sale.where("sales.receipt_date between ? and ? and sale_status ='completed'", from,to).sum(:total_discount)
|
||||
change_amount = Sale.where("sales.receipt_date between ? and ? and sale_status ='completed'", from,to).sum(:amount_changed)
|
||||
sale_cash = Sale.select("SUM(case when (sale_payments.payment_method = 'mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar') then (sale_payments.payment_amount) else 0 end) as card_amount,
|
||||
SUM(case when (sale_payments.payment_method='cash') then (sale_payments.payment_amount) else 0 end) as cash_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='cash') then (sale_payments.payment_amount) else 0 end) as cash_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")
|
||||
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
|
||||
.where("sales.receipt_date between ? and ? and sale_status = 'completed' and sale_payments.payment_amount != 0 ", from,to)
|
||||
.where("sales.receipt_date between ? and ? and sale_status = 'completed' and sale_payments.payment_amount != 0 ", from,to)
|
||||
sale_cash.each do |s_c|
|
||||
total_cash_amount += s_c.cash_amount.to_f
|
||||
total_card_amount += s_c.card_amount.to_f
|
||||
@@ -638,11 +660,11 @@ def self.get_item_query()
|
||||
total_foc_amount += s_c.foc_amount.to_f
|
||||
end
|
||||
total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f
|
||||
|
||||
|
||||
end
|
||||
|
||||
return query, discount_query , total_cash_amount , total_card_amount , total_credit_amount , total_foc_amount , total_grand_total , change_amount
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_type)
|
||||
## => left join -> show all sales although no orders
|
||||
@@ -655,7 +677,7 @@ def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_ty
|
||||
payment_type = " and sale_payments.payment_method = '#{payment_type}'"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
query = Sale.all
|
||||
if shift.present?
|
||||
|
||||
@@ -674,10 +696,10 @@ def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_ty
|
||||
query = query.where("sale_status='completed' and sales.receipt_date between ? and ? #{payment_type} and sale_payments.payment_amount != 0",from,to)
|
||||
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
|
||||
.group("sales.sale_id")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to)
|
||||
def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to)
|
||||
query = SalePayment.select("s.receipt_no, sale_payments.*,s.receipt_date as sale_date,
|
||||
s.cashier_name as cashier_name")
|
||||
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
|
||||
@@ -689,16 +711,16 @@ def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to)
|
||||
else
|
||||
query = query.where("payment_method='creditnote' and s.sale_status = 'completed' and s.receipt_date between ? and ? ",from,to)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_void_sale(shift,from,to)
|
||||
sale_arr = Array.new
|
||||
|
||||
|
||||
query = Sale.select("sales.receipt_no,sales.receipt_date, sales.payment_status, sales.sale_status,sales.total_amount,sales.grand_total, sales.rounding_adjustment")
|
||||
# .joins("INNER JOIN shift_sales sh ON sh.id = sales.shift_sale_id")
|
||||
# .where("sales.sale_status = 'void' and (sh.shift_started_at between ? and ?
|
||||
# .where("sales.sale_status = 'void' and (sh.shift_started_at between ? and ?
|
||||
# OR sh.shift_closed_at between ? and ? )", from ,to, from, to)
|
||||
|
||||
|
||||
if shift.present?
|
||||
query = query.where("sales.sale_status = 'void' and sales.shift_sale_id in (?)",shift.to_a)
|
||||
else
|
||||
@@ -725,10 +747,10 @@ def self.get_separate_tax(shift_sale_range=nil,shift,from,to,payment_type)
|
||||
if shift.present?
|
||||
query = SaleTax.select("SUM(tax_payable_amount) AS st_amount,tax_name")
|
||||
.joins("LEFT JOIN sales ON sales.sale_id = sale_taxes.sale_id")
|
||||
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
|
||||
.where("sales.shift_sale_id in (?) #{payment_type} and sale_status= 'completed' and sale_payments.payment_amount != 0", shift.to_a)
|
||||
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
|
||||
.where("sales.shift_sale_id in (?) #{payment_type} and sale_status= 'completed' and sale_payments.payment_amount != 0", shift.to_a)
|
||||
.group("sale_taxes.tax_name")
|
||||
.order("sale_taxes.sale_tax_id asc")
|
||||
.order("sale_taxes.sale_tax_id asc")
|
||||
elsif shift_sale_range.present?
|
||||
query = SaleTax.select("SUM(tax_payable_amount) AS st_amount,tax_name")
|
||||
.joins("LEFT JOIN sales ON sales.sale_id = sale_taxes.sale_id")
|
||||
@@ -743,15 +765,15 @@ def self.get_separate_tax(shift_sale_range=nil,shift,from,to,payment_type)
|
||||
.where("sales.receipt_date between ? and ? #{payment_type} and sale_status= 'completed' and sale_payments.payment_amount != 0", from,to)
|
||||
.group("sale_taxes.tax_name")
|
||||
.order("sale_taxes.sale_tax_id asc")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# def self.get_separate_tax(from,to,payment_method=nil)
|
||||
|
||||
|
||||
# query = SaleTax.select("SUM(tax_payable_amount) AS st_amount,tax_name")
|
||||
# .joins("INNER JOIN sales ON sales.sale_id = sale_taxes.sale_id")
|
||||
# .group("sale_taxes.tax_name")
|
||||
|
||||
|
||||
# return query = query.where("sale_status=? and receipt_date between ? and ?","completed",from,to)
|
||||
# end
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ class CreateDiningCharges < ActiveRecord::Migration[5.1]
|
||||
t.time :charge_block
|
||||
t.string :time_rounding, :default => "down"
|
||||
t.time :time_rounding_block
|
||||
t.integer :time_rounding_block_price, :default => 0
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user