2019-07-30 11:43:51 +02:00
|
|
|
# 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
|
2019-07-31 12:00:52 +02:00
|
|
|
authorize :accounting_export
|
2019-07-30 11:43:51 +02:00
|
|
|
|
2021-03-22 17:42:54 +01:00
|
|
|
export = Export.where(category: 'accounting', export_type: params[:type], key: params[:key])
|
2019-07-31 16:52:11 +02:00
|
|
|
.where(extension: params[:extension], query: params[:query])
|
2019-07-30 11:43:51 +02:00
|
|
|
.where('created_at > ?', Invoice.maximum('updated_at'))
|
|
|
|
.last
|
|
|
|
if export.nil? || !FileTest.exist?(export.file)
|
|
|
|
@export = Export.new(
|
|
|
|
category: 'accounting',
|
2019-09-16 14:39:47 +02:00
|
|
|
export_type: params[:type],
|
2019-07-30 11:43:51 +02:00
|
|
|
user: current_user,
|
2019-07-31 15:47:02 +02:00
|
|
|
extension: params[:extension],
|
|
|
|
query: params[:query],
|
|
|
|
key: params[:key]
|
2019-07-30 11:43:51 +02:00
|
|
|
)
|
|
|
|
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
|