Files
sx-fc/app/controllers/api/bill_controller.rb
2018-10-16 14:35:54 +06:30

198 lines
7.1 KiB
Ruby
Executable File

class Api::BillController < Api::ApiController
#Create invoice based on booking
#Output and invoice
def create
@status = false
@error_message = "Order ID or Booking ID is require to request for a bill."
# if shift_by_terminal = ShiftSale.current_open_shift(get_cashier[0].id)
if !ShiftSale.current_shift.nil?
#create Bill by Booking ID
table = 0
if (params[:booking_id])
booking = Booking.find(params[:booking_id])
# for Multiple Cashier by Zone
table = DiningFacility.find(booking.dining_facility_id)
bk_order = BookingOrder.find_by_booking_id(booking.booking_id)
order = Order.find(bk_order.order_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?
cashier = Employee.find(shift.employee_id)
else
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
cashier = Employee.find(shift.employee_id)
break
end
end
end
if booking
if booking.sale_id.nil?
@sale = Sale.new
@status, @sale_id = @sale.generate_invoice_from_booking(params[:booking_id], current_login_employee, cashier, order.source)
@sale_data = Sale.find_by_sale_id(@sale_id)
else
@status = true
@sale_id = booking.sale_id
@sale_data = Sale.find_by_sale_id(@sale_id)
end
end
elsif (params[:order_id])
order = Order.find(params[:order_id])
@sale = Sale.new
@status, @sale_id = @sale.generate_invoice_from_order(params[:order_id], current_login_employee, get_cashier, order.source)
# for Job
booking = Booking.find_by_sale_id(@sale_id)
table = DiningFacility.find(booking.dining_facility_id)
end
# Bind shift sale id to sale
# @sale_data.shift_sale_id = shift_by_terminal.id
# @sale_data.save
#check checkInOut pdf print
check_booking = Booking.find_by_sale_id(@sale_id)
checkout_time = Lookup.collection_of('checkout_time')
terminal = DiningFacility.find_by_id(check_booking.dining_facility_id)
cashier_terminal = CashierTerminal.find_by_id(terminal.zone_id)
if (!checkout_time.empty?) && (ENV["SERVER_MODE"] != "cloud") #no print in cloud server
unique_code = "CheckInOutPdf"
printer = PrintSetting.find_by_unique_code(unique_code)
# print when complete click
order_queue_printer = Printer::OrderQueuePrinter.new(printer)
if !printer.nil?
order_queue_printer.print_check_in_out(printer,cashier_terminal , check_booking, table)
end
end
Promotion.promo_activate(@sale)
#BillBroadcastJob.perform_later(table)
if ENV["SERVER_MODE"] == 'cloud'
from = request.subdomain + "." + request.domain
else
from = ""
end
ActionCable.server.broadcast "bill_channel",table: table, from: from
else
@status = false
@error_message = "No Current Open Shift"
end
#@sale_data = Sale.find_by_sale_id(@sale_id)
#@sale_items = SaleItem.where("sale_id=?",@sale_id)
# Not Use for these printed bill cannot give customer
# @sale_data = Sale.find_by_sale_id(@sale_id)
# @sale_items = SaleItem.where("sale_id=?",@sale_id)
# unique_code = "ReceiptBillPdf"
# #shop detail
# shop_details = Shop.find(1)
# customer= Customer.find(@sale_data.customer_id)
# # get member information
# member_info = Customer.get_member_account(customer)
# # get printer info
# print_settings=PrintSetting.find_by_unique_code(unique_code)
# # Calculate Price by accounts
# item_price_by_accounts = SaleItem.calculate_price_by_accounts(@sale_items)
#customer= Customer.find(@sale_data.customer_id)
# get member information
#member_info = Customer.get_member_account(customer)
# printer = Printer::ReceiptPrinter.new(print_settings)
# printer.print_receipt_bill(print_settings,@sale_items,@sale_data,customer.name, item_price_by_accounts, member_info, shop_details)
end
def request_bill
puts "order items"
puts params[:order_items]
if params[:order_items]
is_extra_time = false
extra_time = ''
# params[:order_items].each { |i|
# i["item_instance_code"] = i["item_instance_code"].downcase.to_s
# if i["item_instance_code"].include? "ext"
# is_extra_time = true
# arr_exts = i["item_instance_code"].split("_")
# if arr_exts[1].match(/^(\d)+$/)
# time = arr_exts[1].to_i*60*i["quantity"].to_i
# extra_time = Time.at(time)
# end
# end
# }
@order = Order.new
@order.source = "emenu"
@order.order_type = "Takeaway"
@order.customer_id = "CUS-000000000002" # for no customer id from mobile
@order.items = params[:order_items]
@order.guest = params[:guest_info]
@order.table_id = params[:table_id] # this is dining facilities's id
@order.new_booking = true
@order.waiters = current_login_employee.name
@order.employee_name = current_login_employee.name
@order.is_extra_time = is_extra_time
@order.extra_time = extra_time
@status, @booking = @order.generate
if @status && @booking
shift = ShiftSale.current_open_shift(current_login_employee.id)
if !shift.nil?
cashier = Employee.find(shift.employee_id)
if (@booking.booking_id)
booking_order = BookingOrder.find_by_booking_id(@booking.booking_id)
order = Order.find(booking_order.order_id)
if @booking.sale_id.nil?
@sale = Sale.new
@status, @sale_id = @sale.generate_invoice_from_booking(@booking.booking_id, current_login_employee, cashier, order.source)
@sale_data = Sale.find_by_sale_id(@sale_id)
else
@status = true
@sale_id = @booking.sale_id
@sale_data = Sale.find_by_sale_id(@sale_id)
end
end
# Promotion.promo_activate(@sale)
render json: JSON.generate({:status => true, :sale_id => @sale_id, :sale_data => ActiveSupport::JSON.encode(@sale_data)})
else
render json: JSON.generate({:status => false, :error_message => "No Current Open Shift!"})
end
else
render json: JSON.generate({:status => false, :error_message => "Create order failed, some error occurred!"})
end
else
render json: JSON.generate({:status => false, :error_message => "Parameters missing!"})
end
end
private
def bill_params
params.permit(:booking_id, :order_id)
end
end