class Origami::SplitBillController < BaseOrigamiController authorize_resource :class => false def index dining_id = params[:dining_id] @table = DiningFacility.find(dining_id) @booking = @table.get_booking @order_items = Array.new @sale_data = Array.new table_bookings = Booking.where("dining_facility_id = #{dining_id} and sale_id IS NOT NULL") if !table_bookings.nil? table_bookings.each do |table_booking| @sale_data.push(table_booking.sale) end end if @booking @booking.booking_orders.each do |booking_order| @order = Order.find(booking_order.order_id) if (@order.status == "new") @order.order_items.each do |item| @order_items.push(item) end end end else @booking = nil end end def create order_ids = params[:order_ids] order_items = JSON.parse(params[:order_items]) # byebug status = false if shift_by_terminal = ShiftSale.current_open_shift(get_cashier[0].id) #create Bill by Booking ID table = 0 if !params[:booking_id].empty? booking = Booking.find(params[:booking_id]) # for Multiple Cashier by Zone table = DiningFacility.find(booking.dining_facility_id) cashier_zone = CashierTerminalByZone.find_by_zone_id(table.zone_id) shift_by_terminal = ShiftSale.find_by_cashier_terminal_id_and_shift_closed_at(cashier_zone.cashier_terminal_id,nil) get_cashier_by_terminal = Employee.find(shift_by_terminal.employee_id) if booking if booking.sale_id.nil? sale = Sale.new status, sale_id = sale.generate_invoice_from_booking(params[:booking_id], current_user, get_cashier_by_terminal) 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 else if params[:type] == "Table" type = "TableBooking" else type = "RoomBooking" end booking = Booking.create({:dining_facility_id => params[:dining_id],:type => type, :checkin_at => Time.now.utc, :checkin_by => current_user.name, :booking_status => "assign" }) order_item_count = 0 order_id = nil order_items.each do |order_item| order_item_count = 0 order = Order.find(order_item["order_id"]) if order.order_items.count == 1 order_id = order.id break else order_item_count += 1 end end if !order_id.nil? BookingOrder.find_by_order_id(order_id).delete BookingOrder.create({:booking_id => booking.booking_id, :order_id => order_id}) order_items.each do |order_item| orderItem = OrderItem.find(order_item["id"]) orderItem.order_id = order_id orderItem.save! end else if order_ids.count == 1 && order_item_count == 1 BookingOrder.create({:booking_id => booking.booking_id, :order_id => order_ids[0]}) else customer = Customer.find(params[:customer_id]) order_type = "dine_in" if !customer.nil? if customer.customer_type == "Takeaway" order_type = "takeaway" elsif customer.customer_type == "Delivery" order_type = "delivery" end end # begin order = Order.new order.source = "cashier" order.order_type = order_type order.customer_id = params[:customer_id] == ""? "CUS-000000000001" : params[:customer_id] # for no customer id from mobile order.item_count = order_items.count order.status = "new" order.table_id = params[:dining_id] # this is dining facilities's id order.waiters = current_user.name order.employee_name = current_user.name order.guest_info = nil order.save! BookingOrder.create({:booking_id => booking.booking_id, :order_id => order.order_id}) order_items.each do |order_item| orderItem = OrderItem.find(order_item["id"]) orderItem.order_id = order.order_id orderItem.save! end end end sale = Sale.new status, sale_id = sale.generate_invoice_from_booking(booking.booking_id, current_user, get_cashier_by_terminal) sale_data = Sale.find_by_sale_id(sale_id) end # Bind shift sale id to sale sale_data.shift_sale_id = shift_by_terminal.id sale_data.save Promotion.promo_activate(sale) BillBroadcastJob.perform_later(table) render :json => { status: status } else render :json => { status: false, error_message: 'No Current Open Shift!'} end end end