fixed conflit

This commit is contained in:
NyanLinHtut
2019-09-27 20:46:10 +06:30
10 changed files with 240 additions and 311 deletions

View File

@@ -13,6 +13,7 @@ class Sale < ApplicationRecord
has_many :sale_taxes
has_many :sale_payments
has_many :sale_orders
has_many :orders, through: :sale_orders
has_many :bookings
has_many :product_commissions
@@ -64,34 +65,31 @@ class Sale < ApplicationRecord
Rails.logger.debug '........ Sale data sync completed .......'
end
end
def generate_invoice_from_booking(booking_id, requested_by, cashier, order_source = nil, current_checkin_induties_count)
saleObj =''
booking = Booking.find(booking_id)
status = false
Rails.logger.debug "Booking -> " + booking.id.to_s
if (booking)
def self.generate_invoice_from_booking(booking, requested_by, cashier, order_source = nil, current_checkin_induties_count)
if booking
Rails.logger.debug "Booking -> " + booking.id.to_s
Rails.logger.debug "Booking -> Booking Order Count -> " + booking.booking_orders.count.to_s
#get all order attached to this booking and combine into 1 invoice
unless sale = booking.sale
sale = booking.build_sale(
{
tax_type: "execulive" # Default Tax - Values
}
)
end
booking.booking_orders.each do |order|
# puts "Hello order"
if booking.sale_id
status, sale_id = generate_invoice_from_order(order.order_id, nil, booking, requested_by, cashier, order_source)
else
status, sale_id = generate_invoice_from_order(order.order_id, booking.sale_id, booking, requested_by, cashier, order_source)
end
booking.sale_id = sale_id
saleObj = Sale.find(sale_id)
sale.generate_invoice_from_order(order.order_id, booking, requested_by, cashier, order_source)
# saleObj = Sale.find(sale_id)
# order = booking.booking_orders.take.order
link_order_sale(order.order_id)
# link_order_sale(order.order_id)
end
# InventoryJob.perform_now(self.id)
# InventoryDefinition.calculate_product_count(saleObj)
# dining charges
charges = DiningCharge.where('dining_facility_id=?',booking.dining_facility_id).take
charges = DiningCharge.where('dining_facility_id=?', booking.dining_facility_id).take
if !charges.nil?
block_count, diningprice = DiningCharge.amount_calculate(charges, booking.checkin_at, booking.checkout_at)
if charges.charge_type =='hr'
@@ -102,171 +100,139 @@ class Sale < ApplicationRecord
later_time = booking.checkout_at
early_time = booking.checkin_at
distance_in_minutes = ((later_time - early_time)/60.0).round
basic_pay_amount =0
name =""
basic_pay_amount = 0
name = ""
if current_checkin_induties_count != "0"
basic_pay = Commission.where('commission_type=?','Basic Pay')
basic_pay.each do |pay|
basic_pay_amount =pay.amount
name =pay.name
basic_pay_amount = pay.amount
name = pay.name
end
induties_pay_amount = (current_checkin_induties_count.to_i * (distance_in_minutes / 60.0).to_f * basic_pay_amount).to_i
create_saleitem_indutycharges(charges, current_checkin_induties_count.to_i, induties_pay_amount, booking.dining_facility.name, dining_time, order_source, basic_pay_amount)
sale.create_saleitem_indutycharges(charges, current_checkin_induties_count.to_i, induties_pay_amount, booking.dining_facility.name, dining_time, order_source, basic_pay_amount)
end
create_saleitem_diningcharges(charges, block_count, diningprice, booking.dining_facility.name, dining_time, order_source)
sale.create_saleitem_diningcharges(charges, block_count, diningprice, booking.dining_facility.name, dining_time, order_source)
end
return status, sale_id
return sale
end
end
def generate_invoice_from_order (order_id, sale_id, booking, requested_by, cashier = nil, order_source = nil)
def generate_invoice_from_order(order_id, booking, requested_by, cashier = nil, order_source = nil)
taxable = true
#if sale_id is exsit and validate
#add order to that invoice
if (sale_id)
self.find(sale_id)
end
Rails.logger.debug "Does it have Existing Sale -> [#{self.id.to_s}] - Status [#{self.sale_status}]"
if self.sale_status == "void"
return false, "Invoice is void. Cannot be edited"
order = Order.find(order_id)
# current cashier login
open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift
# shift with terminal zone
# set cashier
if order_source.present? && order_source.downcase == "emenu"
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
#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
if !shift.nil? then
break
end
end
end
end
else
#if this is new sale generate_receipt_no
# generate_receipt_no
order = Order.find(order_id)
#Default Tax - Values
self.tax_type = "exclusive"
# current cashier login
open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift
# shift with terminal zone
# set cashier
if !order_source.nil?
orderSoure = order_source.downcase
else
orderSoure = nil
end
if orderSoure == "emenu"
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
#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
if !shift.nil? then
break
end
end
shift = ShiftSale.current_open_shift(cashier.id)
end
# set cashier
if shift != nil #if current login employee open shift
self.cashier_id = cashier.id
self.cashier_name = cashier.name
self.shift_sale_id = shift.id
else
if open_cashier.count>0 # if we have two open cashier
# 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)
if shift_id
self.shift_sale_id = shift_id.id
else
self.shift_sale_id = current_shift.id
end
end
else
shift = ShiftSale.current_open_shift(cashier.id)
self.cashier_id = current_shift.employee_id
self.cashier_name = Employee.find(current_shift.employee_id).name
self.shift_sale_id = current_shift.id
end
# set cashier
if shift != nil #if current login employee open shift
self.cashier_id = cashier.id
self.cashier_name = cashier.name
self.shift_sale_id = shift.id
else
if open_cashier.count>0 # if we have two open cashier
# 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)
if shift_id
self.shift_sale_id = shift_id.id
else
self.shift_sale_id = current_shift.id
end
else
self.cashier_id = current_shift.employee_id
self.cashier_name = Employee.find(current_shift.employee_id).name
self.shift_sale_id = current_shift.id
end
# set waiter
self.requested_by = requested_by.name
self.requested_at = DateTime.now.utc.getlocal
Rails.logger.debug "Order -> #{order.id} | order_status -> #{order.status}"
if order
self.customer_id = order.customer_id
Rails.logger.debug "Order -> #{order.id} | Items Count -> #{order.order_items.count}"
order.order_items.each do |item|
self.add_item(item)
if !item.set_menu_items.nil?
self.add_sub_item(item.set_menu_items)
end
end
self.orders << order
end
#compute sales summary
if order_source.nil?
order_source = order.source
end
self.compute(order_source)
# set waiter
self.requested_by = requested_by.name
#Update the order items that is billed
order.update_items_status_to_billed(nil)
order.status = "billed"
order.save
self.requested_at = DateTime.now.utc.getlocal
Rails.logger.debug "Order -> #{order.id} | order_status -> #{order.status}"
if order
self.customer_id = order.customer_id
Rails.logger.debug "Order -> #{order.id} | Items Count -> #{order.order_items.count}"
order.order_items.each do |item|
add_item(item)
if !item.set_menu_items.nil?
add_sub_item(item.set_menu_items)
end
end
# link_order_sale(order.id)
end
self.save!
#compute sales summary
if order_source.nil?
order_source = order.source
end
compute(order_source)
#Update the order items that is billed
order.update_items_status_to_billed(nil)
order.status = "billed"
order.save
booking.sale_id = self.id
if !booking.checkout_at.nil?
if booking.checkout_at.utc < Time.now.utc
booking.checkout_at = Time.now.utc.getlocal
end
else
if !booking.checkout_at.nil?
if booking.checkout_at.utc < Time.now.utc
booking.checkout_at = Time.now.utc.getlocal
end
booking.checkout_by = requested_by.name
booking.save
# InventoryJob.perform_now(self.id)
saleObj = Sale.find(self.id)
# InventoryDefinition.calculate_product_count(saleObj)
return true, self.id
else
booking.checkout_at = Time.now.utc.getlocal
end
return false, nil
booking.checkout_by = requested_by.name
booking.save
# InventoryJob.perform_now(self.id)
# saleObj = Sale.find(self.id)
# InventoryDefinition.calculate_product_count(saleObj)
return self
end
#fOR Quick Service pay and create
def self.request_bill(order,current_user,current_login_employee)
@sale = Sale.new
sale_order=SaleOrder.new
if !ShiftSale.current_shift.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 check_booking.sale_id.nil?
if @sale_data = check_booking.sale
# Create Sale if it doesn't exist
# puts "current_login_employee"
# puts current_login_employee.name
@status, @sale_id = @sale.generate_invoice_from_booking(check_booking.id,current_login_employee,current_user,order.source)
@sale_data = Sale.find_by_sale_id(@sale_id)
@sale_items = SaleItem.where("sale_id=?",@sale_id)
else
@sale_data = Sale.find_by_sale_id(check_booking.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)
end