2020-12-22 14:43:08 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of PaymentSchedule
|
|
|
|
class API::PaymentSchedulesController < API::ApiController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
before_action :set_payment_schedule, only: %i[download]
|
2021-02-04 17:51:16 +01:00
|
|
|
before_action :set_payment_schedule_item, only: %i[cash_check]
|
2020-12-22 14:43:08 +01:00
|
|
|
|
2021-01-25 17:42:30 +01:00
|
|
|
def list
|
|
|
|
authorize PaymentSchedule
|
|
|
|
|
|
|
|
p = params.require(:query).permit(:reference, :customer, :date, :page, :size)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
@payment_schedules = PaymentScheduleService.list(
|
|
|
|
p[:page],
|
|
|
|
p[:size],
|
|
|
|
reference: p[:reference], customer: p[:customer], date: p[:date]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-12-22 14:43:08 +01:00
|
|
|
def download
|
|
|
|
authorize @payment_schedule
|
2020-12-23 15:29:56 +01:00
|
|
|
send_file File.join(Rails.root, @payment_schedule.file), type: 'application/pdf', disposition: 'attachment'
|
2020-12-22 14:43:08 +01:00
|
|
|
end
|
|
|
|
|
2021-02-04 17:51:16 +01:00
|
|
|
def cash_check
|
|
|
|
schedule = @payment_schedule_item.payment_schedule
|
|
|
|
authorize schedule
|
|
|
|
PaymentScheduleService.new.generate_invoice(@payment_schedule_item)
|
|
|
|
@payment_schedule_item.update_attributes(state: 'paid', payment_method: 'check')
|
|
|
|
|
|
|
|
render :show, status: :ok, location: schedule
|
|
|
|
end
|
|
|
|
|
2020-12-22 14:43:08 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_payment_schedule
|
|
|
|
@payment_schedule = PaymentSchedule.find(params[:id])
|
|
|
|
end
|
2021-02-04 17:51:16 +01:00
|
|
|
|
|
|
|
def set_payment_schedule_item
|
|
|
|
@payment_schedule_item = PaymentScheduleItem.find(params[:id])
|
|
|
|
end
|
2020-12-22 14:43:08 +01:00
|
|
|
end
|