2019-01-09 12:07:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of Invoice and Avoir
|
2023-02-24 17:26:55 +01:00
|
|
|
class API::InvoicesController < API::APIController
|
2016-03-23 18:39:41 +01:00
|
|
|
before_action :authenticate_user!
|
2019-01-09 12:07:31 +01:00
|
|
|
before_action :set_invoice, only: %i[show download]
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
def index
|
|
|
|
authorize Invoice
|
2019-01-09 12:07:31 +01:00
|
|
|
@invoices = Invoice.includes(
|
2021-05-27 15:58:55 +02:00
|
|
|
:avoir, :invoicing_profile, invoice_items: %i[subscription invoice_item]
|
2019-01-09 12:07:31 +01:00
|
|
|
).all.order('reference DESC')
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2023-01-03 11:07:41 +01:00
|
|
|
def show; end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def download
|
|
|
|
authorize @invoice
|
2023-01-03 11:07:41 +01:00
|
|
|
send_file Rails.root.join(@invoice.file), type: 'application/pdf', disposition: 'attachment'
|
|
|
|
rescue ActionController::MissingFile
|
|
|
|
render html: I18n.t('invoices.unable_to_find_pdf'), status: :not_found
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2016-05-31 10:02:27 +02:00
|
|
|
def list
|
|
|
|
authorize Invoice
|
|
|
|
|
|
|
|
p = params.require(:query).permit(:number, :customer, :date, :order_by, :page, :size)
|
|
|
|
|
2019-01-09 12:07:31 +01:00
|
|
|
render json: { error: 'page must be an integer' }, status: :unprocessable_entity and return unless p[:page].is_a? Integer
|
|
|
|
render json: { error: 'size must be an integer' }, status: :unprocessable_entity and return unless p[:size].is_a? Integer
|
2016-05-31 10:02:27 +02:00
|
|
|
|
2019-01-09 12:07:31 +01:00
|
|
|
order = InvoicesService.parse_order(p[:order_by])
|
|
|
|
@invoices = InvoicesService.list(
|
|
|
|
order[:order_key],
|
|
|
|
order[:direction],
|
|
|
|
p[:page],
|
|
|
|
p[:size],
|
|
|
|
number: p[:number], customer: p[:customer], date: p[:date]
|
|
|
|
)
|
2016-05-31 10:02:27 +02:00
|
|
|
end
|
|
|
|
|
2016-12-12 14:20:26 +01:00
|
|
|
# only for create refund invoices (avoir)
|
2016-03-23 18:39:41 +01:00
|
|
|
def create
|
|
|
|
authorize Invoice
|
|
|
|
invoice = Invoice.only_invoice.find(avoir_params[:invoice_id])
|
|
|
|
@avoir = invoice.build_avoir(avoir_params)
|
|
|
|
if @avoir.save
|
2017-12-13 13:16:32 +01:00
|
|
|
# when saved, expire the subscription if needed
|
2019-01-09 12:07:31 +01:00
|
|
|
@avoir.expire_subscription if @avoir.subscription_to_expire
|
2017-12-13 13:16:32 +01:00
|
|
|
# then answer the API call
|
2016-03-23 18:39:41 +01:00
|
|
|
render :avoir, status: :created
|
|
|
|
else
|
|
|
|
render json: @avoir.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-31 12:00:52 +02:00
|
|
|
def first
|
|
|
|
authorize Invoice
|
|
|
|
invoice = Invoice.order(:created_at).first
|
|
|
|
@first = invoice&.created_at
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
|
|
|
|
2019-01-09 12:07:31 +01:00
|
|
|
def avoir_params
|
2023-03-16 17:17:00 +01:00
|
|
|
params.require(:avoir).permit(:invoice_id, :payment_method, :subscription_to_expire, :description,
|
2019-01-09 12:07:31 +01:00
|
|
|
invoice_items_ids: [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_invoice
|
|
|
|
@invoice = Invoice.find(params[:id])
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|