1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/controllers/api/accounting_periods_controller.rb

46 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# API Controller for resources of AccountingPeriod
2019-01-07 12:47:23 +01:00
class API::AccountingPeriodsController < API::ApiController
before_action :authenticate_user!
before_action :set_period, only: %i[show]
2019-01-07 12:47:23 +01:00
def index
@accounting_periods = AccountingPeriodService.all_periods_with_users
2019-01-07 12:47:23 +01:00
end
def show; end
def create
authorize AccountingPeriod
@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
def last_closing_end
authorize AccountingPeriod
last_period = AccountingPeriodService.find_last_period
if last_period.nil?
invoice = Invoice.order(:created_at).first
@last_end = invoice.created_at if invoice
else
@last_end = last_period.end_at
end
end
2019-01-07 12:47:23 +01:00
private
def set_period
@tag = AccountingPeriod.find(params[:id])
end
def period_params
params.require(:accounting_period).permit(:start_at, :end_at)
2019-01-07 12:47:23 +01:00
end
end