1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

export to flat json files closed periods

This commit is contained in:
Sylvain 2019-01-10 10:52:29 +01:00
parent 2f17d90054
commit b42d1614fd
5 changed files with 76 additions and 0 deletions

2
.gitignore vendored
View File

@ -34,6 +34,8 @@
# XLSX exports
/exports/*
# Archives of cLosed accounting periods
/accounting/*
.DS_Store

View File

@ -44,6 +44,7 @@ RUN mkdir -p /usr/src/app/exports
RUN mkdir -p /usr/src/app/log
RUN mkdir -p /usr/src/app/public/uploads
RUN mkdir -p /usr/src/app/public/assets
RUN mkdir -p /usr/src/app/accounting
RUN mkdir -p /usr/src/app/tmp/sockets
RUN mkdir -p /usr/src/app/tmp/pids
@ -64,6 +65,7 @@ VOLUME /usr/src/app/exports
VOLUME /usr/src/app/public
VOLUME /usr/src/app/public/uploads
VOLUME /usr/src/app/public/assets
VOLUME /usr/src/app/accounting
VOLUME /var/log/supervisor
# Expose port 3000 to the Docker host, so we can access it

View File

@ -5,6 +5,7 @@
class AccountingPeriod < ActiveRecord::Base
before_destroy { false }
before_update { false }
after_create :archive_closed_data
validates :start_at, :end_at, :closed_at, :closed_by, presence: true
validates_with DateRangeValidator
@ -13,4 +14,29 @@ class AccountingPeriod < ActiveRecord::Base
def delete
false
end
private
def archive_file
dir = 'accounting'
# create directory if it doesn't exists (accounting)
FileUtils.mkdir_p dir
"#{dir}/#{start_at.iso8601}_#{end_at.iso8601}.json"
end
def to_json_archive(invoices)
ApplicationController.new.view_context.render(
partial: 'archive/accounting',
locals: { invoices: invoices },
formats: [:json],
handlers: [:jbuilder]
)
end
def archive_closed_data
data = Invoice.where('created_at >= :start_date AND created_at < :end_date', start_date: start_at, end_date: end_at)
.includes(:invoice_items)
File.write(archive_file, to_json_archive(data))
end
end

View File

@ -0,0 +1,45 @@
json.array!(invoices) do |invoice|
json.extract! invoice, :id, :stp_invoice_id, :created_at, :reference
json.total number_to_currency(invoice.total / 100.0)
json.invoiced do
json.type invoice.invoiced_type
json.id invoice.invoiced_id
if invoice.invoiced_type == Subscription.name
json.extract! invoice.invoiced, :stp_subscription_id, :created_at, :expiration_date, :canceled_at
json.plan do
json.extract! invoice.invoiced.plan, :id, :base_name, :interval, :interval_count, :stp_plan_id, :is_rolling
json.group do
json.extract! invoice.invoiced.plan.group, :id, :name
end
end
elsif invoice.invoiced_type == Reservation.name
json.extract! invoice.invoiced, :created_at, :stp_invoice_id
json.reservable do
json.type invoice.invoiced.reservable_type
json.id invoice.invoiced.reservable_id
if [Training.name, Machine.name, Space.name].include?(invoice.invoiced.reservable_type) &&
!invoice.invoiced.reservable.nil?
json.extract! invoice.invoiced.reservable, :name, :created_at
elsif invoice.invoiced.reservable_type == Event.name && !invoice.invoiced.reservable.nil?
json.extract! invoice.invoiced.reservable, :title, :created_at
json.prices do
json.standard_price number_to_currency(invoice.invoiced.reservable.amount / 100.0)
json.other_prices invoice.invoiced.reservable.event_price_categories do |price|
json.amount number_to_currency(price.amount / 100.0)
json.price_category do
json.extract! price.price_category, :id, :name, :created_at
end
end
end
end
end
end
end
json.user do
json.extract! invoice.user, :id, :email, :created_at
json.profile do
json.extract! invoice.user.profile, :id, :first_name, :last_name, :birthday, :phone
json.gender invoice.user.profile.gender ? 'male' : 'female'
end
end
end

View File

@ -14,6 +14,7 @@ services:
- ${PWD}/exports:/usr/src/app/exports
- ${PWD}/log:/var/log/supervisor
- ${PWD}/plugins:/usr/src/app/plugins
- ${PWD}/accounting:/usr/src/app/accounting
depends_on:
- postgres
- redis