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!
|
2022-01-17 12:38:53 +01:00
|
|
|
before_action :set_payment_schedule, only: %i[download cancel update]
|
2022-01-05 15:58:33 +01:00
|
|
|
before_action :set_payment_schedule_item, only: %i[show_item cash_check confirm_transfer refresh_item pay_item]
|
2020-12-22 14:43:08 +01:00
|
|
|
|
2022-01-03 11:24:08 +01:00
|
|
|
# retrieve all payment schedules for the current user, paginated
|
2021-02-09 17:18:33 +01:00
|
|
|
def index
|
|
|
|
@payment_schedules = PaymentSchedule.where('invoicing_profile_id = ?', current_user.invoicing_profile.id)
|
2021-06-14 14:40:54 +02:00
|
|
|
.includes(:invoicing_profile, :payment_schedule_items, :payment_schedule_objects)
|
2021-02-09 17:18:33 +01:00
|
|
|
.joins(:invoicing_profile)
|
|
|
|
.order('payment_schedules.created_at DESC')
|
|
|
|
.page(params[:page])
|
|
|
|
.per(params[:size])
|
|
|
|
end
|
|
|
|
|
2022-01-03 11:24:08 +01:00
|
|
|
# retrieve all payment schedules for all users. Filtering is supported
|
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
|
2021-02-08 08:56:01 +01:00
|
|
|
authorize @payment_schedule_item.payment_schedule
|
2021-04-21 17:38:06 +02:00
|
|
|
PaymentScheduleService.new.generate_invoice(@payment_schedule_item, payment_method: 'check')
|
2021-02-08 08:56:01 +01:00
|
|
|
attrs = { state: 'paid', payment_method: 'check' }
|
|
|
|
@payment_schedule_item.update_attributes(attrs)
|
2021-02-04 17:51:16 +01:00
|
|
|
|
2022-01-05 15:58:33 +01:00
|
|
|
render json: attrs, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_transfer
|
|
|
|
authorize @payment_schedule_item.payment_schedule
|
|
|
|
PaymentScheduleService.new.generate_invoice(@payment_schedule_item, payment_method: 'transfer')
|
|
|
|
attrs = { state: 'paid', payment_method: 'transfer' }
|
|
|
|
@payment_schedule_item.update_attributes(attrs)
|
|
|
|
|
2021-02-08 08:56:01 +01:00
|
|
|
render json: attrs, status: :ok
|
2021-02-04 17:51:16 +01:00
|
|
|
end
|
|
|
|
|
2021-02-08 15:28:47 +01:00
|
|
|
def refresh_item
|
|
|
|
authorize @payment_schedule_item.payment_schedule
|
2021-02-09 15:44:56 +01:00
|
|
|
PaymentScheduleItemWorker.new.perform(@payment_schedule_item.id)
|
2021-02-08 15:28:47 +01:00
|
|
|
|
|
|
|
render json: { state: 'refreshed' }, status: :ok
|
|
|
|
end
|
|
|
|
|
2021-02-09 12:09:26 +01:00
|
|
|
def pay_item
|
|
|
|
authorize @payment_schedule_item.payment_schedule
|
|
|
|
|
2021-06-03 16:25:42 +02:00
|
|
|
res = PaymentGatewayService.new.pay_payment_schedule_item(@payment_schedule_item)
|
|
|
|
if res.error
|
|
|
|
render json: res, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
render json: res, status: :ok
|
|
|
|
end
|
2021-02-09 12:09:26 +01:00
|
|
|
end
|
|
|
|
|
2022-01-03 11:24:08 +01:00
|
|
|
def show_item
|
|
|
|
authorize @payment_schedule_item.payment_schedule
|
|
|
|
render json: @payment_schedule_item, status: :ok
|
|
|
|
end
|
|
|
|
|
2021-02-09 15:44:56 +01:00
|
|
|
def cancel
|
|
|
|
authorize @payment_schedule
|
|
|
|
|
|
|
|
canceled_at = PaymentScheduleService.cancel(@payment_schedule)
|
|
|
|
render json: { canceled_at: canceled_at }, status: :ok
|
|
|
|
end
|
|
|
|
|
2022-01-17 12:38:53 +01:00
|
|
|
## Only the update of the payment method is allowed
|
|
|
|
def update
|
|
|
|
authorize PaymentSchedule
|
|
|
|
|
|
|
|
if PaymentScheduleService.new.update_payment_mean(@payment_schedule, update_params)
|
|
|
|
render :show, status: :ok, location: @payment_schedule
|
|
|
|
else
|
|
|
|
render json: @payment_schedule.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
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
|
2022-01-17 12:38:53 +01:00
|
|
|
|
|
|
|
def update_params
|
|
|
|
params.require(:payment_schedule).permit(:payment_method)
|
|
|
|
end
|
2020-12-22 14:43:08 +01:00
|
|
|
end
|