Files
sx-fc/app/models/dining_charge.rb
2017-10-23 11:38:10 +06:30

81 lines
2.6 KiB
Ruby
Executable File

class DiningCharge < ApplicationRecord
belongs_to :table
belongs_to :room
def self.amount_calculate(dining_charges_obj, checkin , checkout)
# note :: the first Charge Block will cost all, the Time rounding block will included in 2nd Charge Block
if !checkin.nil? && !checkout.nil? && !dining_charges_obj.nil?
price = 0
minutes = DiningCharge.time_diff(checkout, checkin)
free_time = DiningCharge.convert_to_minutes(dining_charges_obj.minimum_free_time.utc.localtime.strftime('%H:%M'))
dining_minutes = minutes #- free_time # stayminutes - free minutes
if dining_minutes <= free_time
price = 0
else
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 = charges(dining_charges_obj, dining_minutes, 'day')
end
end
return price
else
puts "<<<<<<<< NO"
end
end
def self.charges(chargesObj, dining_minutes, type)
solid_price = 0
block = DiningCharge.convert_to_minutes(chargesObj.charge_block.utc.localtime.strftime('%H:%M'))
result = dining_minutes / block
if result.to_i < 1
return chargesObj.unit_price
elsif result.to_i >= 1
solid_price = result * chargesObj.unit_price
remain_value = dining_minutes % block
rounding_block = DiningCharge.convert_to_minutes(chargesObj.time_rounding_block.utc.localtime.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,solid_price)
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