2019-01-07 15:33:09 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of AccountingPeriod
|
2019-01-07 12:47:23 +01:00
|
|
|
class API::AccountingPeriodsController < API::ApiController
|
|
|
|
|
2019-01-07 15:33:09 +01:00
|
|
|
before_action :authenticate_user!
|
|
|
|
before_action :set_period, only: %i[show]
|
2019-01-07 12:47:23 +01:00
|
|
|
|
|
|
|
def index
|
2019-01-07 16:45:31 +01:00
|
|
|
@accounting_periods = AccountingPeriodService.all_periods_with_users
|
2019-01-07 12:47:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show; end
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize AccountingPeriod
|
2019-01-07 16:45:31 +01:00
|
|
|
@accounting_period = AccountingPeriod.new(period_params.merge(closed_at: DateTime.now, closed_by: current_user.id))
|
2019-01-07 12:47:23 +01:00
|
|
|
if @accounting_period.save
|
|
|
|
render :show, status: :created, location: @accounting_period
|
|
|
|
else
|
|
|
|
render json: @accounting_period.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-07 15:33:09 +01:00
|
|
|
def last_closing_end
|
|
|
|
authorize AccountingPeriod
|
2019-01-07 17:21:32 +01:00
|
|
|
last_period = AccountingPeriodService.find_last_period
|
|
|
|
if last_period.nil?
|
|
|
|
invoice = Invoice.order(:created_at).first
|
|
|
|
@last_end = invoice.created_at if invoice
|
|
|
|
else
|
2019-01-08 11:19:07 +01:00
|
|
|
@last_end = last_period.end_at + 1.day
|
2019-01-07 17:21:32 +01:00
|
|
|
end
|
2019-01-07 15:33:09 +01:00
|
|
|
end
|
|
|
|
|
2019-01-07 12:47:23 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_period
|
|
|
|
@tag = AccountingPeriod.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def period_params
|
2019-01-07 15:33:09 +01:00
|
|
|
params.require(:accounting_period).permit(:start_at, :end_at)
|
2019-01-07 12:47:23 +01:00
|
|
|
end
|
|
|
|
end
|