From 2dd940cc2e91e4e946d18cb16a431f88fc0ed00f Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 28 Nov 2018 11:29:43 +0630 Subject: [PATCH] check no order --- app/controllers/api/bill_controller.rb | 75 ++++++++++++----------- app/views/api/bookings/show.json.jbuilder | 3 +- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/app/controllers/api/bill_controller.rb b/app/controllers/api/bill_controller.rb index b9cbe2ca..b5cb5573 100755 --- a/app/controllers/api/bill_controller.rb +++ b/app/controllers/api/bill_controller.rb @@ -11,45 +11,50 @@ class Api::BillController < Api::ApiController table = 0 if (params[:booking_id]) booking = Booking.find(params[:booking_id]) - if booking.checkin_at.utc.strftime("%Y-%m-%d %H:%M") > Time.now.utc.strftime("%Y-%m-%d %H:%M") && booking.checkout_at.nil? - @status = false - @error_message = "Operation failed, Could not request bill!" - else - # 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) + if booking.booking_orders.count > 0 + if booking.checkin_at.utc.strftime("%Y-%m-%d %H:%M") > Time.now.utc.strftime("%Y-%m-%d %H:%M") && booking.checkout_at.nil? + @status = false + @error_message = "Operation failed, Could not request bill!" 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 + # 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 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 + else + @status = false + @error_message = "There is no order for '#{params[:booking_id]}'" end elsif (params[:order_id]) order = Order.find(params[:order_id]) diff --git a/app/views/api/bookings/show.json.jbuilder b/app/views/api/bookings/show.json.jbuilder index a8236f05..5cef3269 100755 --- a/app/views/api/bookings/show.json.jbuilder +++ b/app/views/api/bookings/show.json.jbuilder @@ -37,8 +37,9 @@ if (@booking) json.remark item.remark json.item_status item.order_item_status @total_amount = @total_amount + (item.price * item.qty) + else + json.success false end - end json.sub_total @total_amount