class Origami::RequestBillsController < ApplicationController
  before_action :check_user

  def check_user
    if current_user.nil?
      redirect_to root_path
    end
  end
  # Print Request Bill and add to sale tables
  def print
    if !ShiftSale.current_shift.nil?
      order_id = params[:id] # order_id
      order = Order.find(order_id)
      booking = order.booking
      if booking.checkin_at > Time.current && booking.checkout_at.nil?
        @status = false
        @error_message = "Operation failed, Could not request bill!"
      else
        table = DiningFacility.find_by(id: booking.dining_facility_id)

        if booking.sale_id.nil?
          if sale_data = Sale.generate_invoice_from_booking(booking, current_login_employee, current_user, order.source, params[:current_checkin_induties_count])
            # in-duty update
            in_duties = InDuty.where("booking_id=?", booking.id)
            if !in_duties.empty?
              in_duties.each do |in_duty|
                induty = InDuty.find(in_duty.id)
                induty.sale_id = sale_data.sale_id
                induty.out_time = Time.current
                induty.save
              end
            end

            action_by = current_user.name
            type = "REQUEST_BILL"

            remark = "Request bill Receipt No #{sale_data.receipt_no}"
            sale_audit = SaleAudit.record_audit_sale(sale_data.sale_id,remark,action_by,type )

            # Promotion Activation
            Promotion.promo_activate(sale_data)

            #bill channel
            if ENV["SERVER_MODE"] == 'cloud'
              from = request.subdomain  + "." + request.domain
            else
              from = ""
            end

            if ["quick_service", "cashier"].include? order.source
              ActionCable.server.broadcast "bill_channel", table: table, from: from
            end

            unless ["quick_service", "food_court"].include? order.source
              #check checkInOut pdf print
              checkout_time = Lookup.collection_of('checkout_time')
              if !booking.dining_facility_id.nil?
                terminal = DiningFacility.find_by_id(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, booking, table)
                  end
                end
              end
            end
            @status = true
            sale_id = sale_data.sale_id
          else
            @status = false
            sale_id = nil
          end
        else
          @status = true
          sale_id = booking.sale_id
        end
      end
    else
      @status = false
      @error_message = "No Current Open Shift for This Employee"
    end
    if ["quick_service", "food_court"].include? order.source
      result = {:status=> @status, :data => sale_data.sale_id }
      render :json => result.to_json
    end
  end

end
