1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-26 20:54:21 +01:00
fab-manager/app/controllers/api/accounting_exports_controller.rb
2019-07-31 12:37:31 +02:00

39 lines
1.2 KiB
Ruby

# frozen_string_literal: true
# API Controller for exporting accounting data to external accounting softwares
class API::AccountingExportsController < API::ApiController
before_action :authenticate_user!
def export
authorize :accounting_export
export = Export.where(category: 'accounting', export_type: 'accounting-software')
.where('created_at > ?', Invoice.maximum('updated_at'))
.last
if export.nil? || !FileTest.exist?(export.file)
@export = Export.new(
category: 'accounting',
export_type: 'accounting-software',
user: current_user,
extension: params[:settings][:format],
query: {
columns: params[:settings][:columns],
encoding: params[:settings][:encoding],
date_format: params[:settings][:dateFormat]
}.to_json,
key: params[:settings][:separator]
)
if @export.save
render json: { export_id: @export.id }, status: :ok
else
render json: @export.errors, status: :unprocessable_entity
end
else
send_file File.join(Rails.root, export.file),
type: 'text/csv',
disposition: 'attachment'
end
end
end