2019-07-30 11:43:51 +02:00
|
|
|
# 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
|
2019-07-30 11:43:51 +02:00
|
|
|
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
|
2023-02-24 17:26:55 +01:00
|
|
|
send_file Rails.root.join(export.file),
|
2019-07-30 11:43:51 +02:00
|
|
|
type: 'text/csv',
|
|
|
|
disposition: 'attachment'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|