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

35 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# API Controller for exporting accounting data to external accounting softwares
2023-02-24 17:26:55 +01:00
class API::AccountingExportsController < API::APIController
before_action :authenticate_user!
def export
2019-07-31 12:00:52 +02:00
authorize :accounting_export
export = Export.where(category: 'accounting', export_type: params[:type], key: params[:key])
.where(extension: params[:extension], query: params[:query])
.where('created_at > ?', Invoice.maximum('updated_at'))
.last
if export.nil? || !FileTest.exist?(export.file)
@export = Export.new(
category: 'accounting',
export_type: params[:type],
user: current_user,
extension: params[:extension],
query: params[:query],
key: params[:key]
)
if @export.save
render json: { export_id: @export.id }, status: :ok
else
render json: @export.errors, status: :unprocessable_entity
end
else
2023-02-24 17:26:55 +01:00
send_file Rails.root.join(export.file),
type: 'text/csv',
disposition: 'attachment'
end
end
end