diff --git a/.gitignore b/.gitignore index 07aafe0e2..2e46c2324 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,8 @@ # XLSX exports /exports/* +# Archives of cLosed accounting periods +/accounting/* .DS_Store diff --git a/Dockerfile b/Dockerfile index cf2d7db52..6205425e9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/app/models/accounting_period.rb b/app/models/accounting_period.rb index 08d3817b5..73880060f 100644 --- a/app/models/accounting_period.rb +++ b/app/models/accounting_period.rb @@ -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 diff --git a/app/views/archive/_accounting.json.jbuilder b/app/views/archive/_accounting.json.jbuilder new file mode 100644 index 000000000..1bd2def1e --- /dev/null +++ b/app/views/archive/_accounting.json.jbuilder @@ -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 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index b61e3b989..6a86fba14 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -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