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)
    #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)

        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_login_employee, 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

      elsif (params[:order_id])
        @sale = Sale.new
        @status, @sale_id = @sale.generate_invoice_from_order(params[:order_id], current_login_employee, get_cashier)

        # 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

      Promotion.promo_activate(@sale)

      BillBroadcastJob.perform_later(table)
    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


  private
  def bill_params
    params.permit(:booking_id, :order_id)
  end
end
