update order and request bill

This commit is contained in:
Thein Lin Kyaw
2019-12-11 17:56:39 +06:30
parent 26edf23b46
commit 58117d0f48
28 changed files with 336 additions and 448 deletions

View File

@@ -5,10 +5,11 @@ class Sale < ApplicationRecord
#primary key - need to be unique generated for multiple shops
before_create :generate_custom_id
before_create :generate_receipt_no
belongs_to :cashier, foreign_key: "cashier_id", class_name: "Employee"
belongs_to :customer, :optional => true
belongs_to :shift_sale
has_one :survey, foreign_key: "receipt_no"
has_one :survey, foreign_key: "receipt_no"
has_many :sale_audits
has_many :sale_items
has_many :sale_discount_items
@@ -20,6 +21,7 @@ class Sale < ApplicationRecord
has_many :orders, through: :sale_orders
has_many :order_items, through: :sale_orders
has_many :bookings
has_one :booking
has_many :product_commissions
before_validation :round_to_precision
@@ -79,188 +81,109 @@ class Sale < ApplicationRecord
Rails.logger.debug '........ Sale data sync completed .......'
end
end
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(
def self.generate_invoice_from_booking(booking, requested_by, cashier, order_source = nil, in_duties_count = 0)
Sale.transaction do
if booking
current = Time.now
#get all order attached to this booking and combine into 1 invoice
sale = booking.sale || booking.build_sale(
{
tax_type: "execulive" # Default Tax - Values
}
)
end
booking.booking_orders.each do |order|
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)
end
# InventoryJob.perform_now(self.id)
# InventoryDefinition.calculate_product_count(saleObj)
# dining charges
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'
dining_time = booking.checkin_at.strftime('%H:%M %p').to_s + " - " + booking.checkout_at.strftime('%H:%M %p').to_s
if cashier.role == 'cashier'
sale.cashier = cashier
sale.shift_sale = cashier.current_shift
elsif booking.dining_facility_id
sale.shift_sale = booking.dining_facility.cashier_terminal.current_shift
sale.cashier = booking.dining_facility.cashier_terminal.current_shift.employee
else
dining_time = booking.checkin_at.strftime('%B %d, %H:%M %p').to_s + " - " + booking.checkout_at.strftime('%B %d, %H:%M %p').to_s
sale.cashier = Employee.where(role: 'cashier').where.not(token_session: [nil, '']).first
sale.shift_sale = sale.cashier.current_shift
end
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 = ""
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
end
induties_pay_amount = (current_checkin_induties_count.to_i * (distance_in_minutes / 60.0).to_f * basic_pay_amount).to_i
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
sale.create_saleitem_diningcharges(charges, block_count, diningprice, booking.dining_facility.name, dining_time, order_source)
end
return sale
end
end
sale.cashier_name = sale.cashier.name
sale.requested_by = requested_by.name
sale.requested_at = current
def generate_invoice_from_order(order_id, booking, requested_by, cashier = nil, order_source = nil)
taxable = true
sale.sale_items << booking.order_items.to_sale_items
order = Order.find(order_id)
if dining_charge = booking.dining_facility.dining_charge
block_count, dining_price = DiningCharge.amount_calculate(dining_charge, booking.checkin_at, booking.checkout_at)
format = "%I:%M %p" if dining_charge.charge_type == "hr"
format ||= "%B %d, %I:%M %p"
dining_time = "#{booking.checkin_at.strftime(format)} - #{booking.checkout_at.strftime(format)}"
# current cashier login
open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift
# shift with terminal zone
sale.sale_items.build({
menu_category_name: "Dining Charge",
menu_category_code: "DiningCharge",
product_name: "#{booking.dining_facility.name} ( #{dining_time} )",
product_code: dining_charge.item_code,
product_alt_name: '-',
account_id: 0,
is_taxable: dining_charge.taxable,
qty: block_count,
unit_price: dining_charge.unit_price,
price: dining_price,
taxable_price: dining_price,
})
# 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
if in_duties_count.to_i > 0
basic_pay = Commission.find_by(commission_type: 'Basic Pay')
in_duties_pay_amount = (in_duties_count.to_i * ((booking.checkout_at - booking.checkin_at) / 1.hours) * basic_pay_amount).to_i
sale.sale_items.build({
menu_category_name: "Induty Charge",
menu_category_code: "IndutyCharge",
product_name: "#{basic_pay.name} ( #{dining_time} )",
product_code: "",
product_alt_name: '-',
account_id: 0,
is_taxable: dining_charge.taxable,
qty: in_duties_count,
unit_price: basic_pay.amount,
price: in_duties_pay_amount,
taxable_price: in_duties_pay_amount,
})
end
end
end
else
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
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
sale.orders << booking.orders
sale.customer_id = booking.orders[0].customer_id
sale.compute(booking.orders[0].source)
booking.orders.update_all(status: "billed")
booking.order_items.update_all(order_item_status: "billed")
booking.checkout_at = current unless booking.checkout_at && booking.checkout_at > current
booking.checkout_by = requested_by.name
booking.save
return sale
end
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)
#Update the order items that is billed
order.update_items_status_to_billed(nil)
order.status = "billed"
order.save
if !booking.checkout_at.nil?
if booking.checkout_at.utc < Time.now.utc
booking.checkout_at = Time.now.utc.getlocal
end
else
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 self
end
#fOR Quick Service pay and create
def self.request_bill(order,current_user,current_login_employee)
def self.request_bill(order, current_user, current_login_employee)
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)
booking = order.booking
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)
end
sale_data = Sale.generate_invoice_from_booking(check_booking, current_login_employee, current_user, order.source)
# Bind shift sale id to sale
# @sale_data.shift_sale_id = shift.id
# @sale_data.save
# Promotion Activation
Promotion.promo_activate(@sale_data)
@status = true
return @status, @sale_data
Promotion.promo_activate(sale_data)
return true, sale_data
else
@status = false
@message = "No Current Open Shift for This Employee"
return false, "No Current Open Shift for This Employee"
end
end
#This is when spilt bill is request - then we cannot link order to invoice