1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/controllers/api/invoices_controller.rb

74 lines
2.2 KiB
Ruby
Raw Normal View History

2019-01-09 12:07:31 +01:00
# frozen_string_literal: true
# API Controller for resources of Invoice and Avoir
2016-03-23 18:39:41 +01:00
class API::InvoicesController < API::ApiController
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
def show; end
2016-03-23 18:39:41 +01:00
def download
authorize @invoice
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
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
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]
)
end
# 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
# when saved, expire the subscription if needed
2019-01-09 12:07:31 +01:00
@avoir.expire_subscription if @avoir.subscription_to_expire
# 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
2019-09-17 14:48:06 +02:00
params.require(:avoir).permit(:invoice_id, :avoir_date, :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