Class: Stay::Api::V1::InvoicesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/stay/api/v1/invoices_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/stay/api/v1/invoices_controller.rb', line 4

def index
  invoices = Stay::Invoice.joins(booking: :property)
                          .where(stay_properties: { user: current_devise_api_user })

  if params[:start_date].present? && params[:end_date].present?
    start_date = Date.parse(params[:start_date])
    end_date = Date.parse(params[:end_date])
    invoices = invoices.where(
      "stay_bookings.check_in_date >= :start_date AND stay_bookings.check_out_date <= :end_date",
      start_date: start_date,
      end_date: end_date
    )
  end

  if params[:type].present?
    invoices = invoices.where("invoice_type LIKE ?", "%#{params[:type]}%")
  end

  if params[:status].present?
    case params[:status]
    when "confirmed"
      invoices = invoices.where("stay_bookings.status = ?", "confirmed")
    when "any"
      invoices = invoices.where("stay_bookings.status = ?", "invoice_sent")
    end
  end

  if invoices.any?
    render json: { data: ActiveModelSerializers::SerializableResource.new(invoices, each_serializer: InvoiceSerializer), success: true }, status: :ok
  else
    render json: { error: "no invoice found", success: false }, status: :unprocessable_entity
  end
end

#showObject



39
40
41
42
43
44
45
46
# File 'app/controllers/stay/api/v1/invoices_controller.rb', line 39

def show
  invoice = Stay::Invoice.find_by(id: params[:id])
  if invoice.present?
    render json: { data: ActiveModelSerializers::SerializableResource.new(invoice, serializer: InvoiceSerializer), success: true }, status: :ok
  else
    render json: { error: "no invoice found", success: false }, status: :unprocessable_entity
  end
end