1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00

[ongoing] fix api to use the invoicing_profile

This commit is contained in:
Sylvain 2019-05-28 16:49:36 +02:00
parent 0df97cffc5
commit ffbca98276
6 changed files with 28 additions and 9 deletions

View File

@ -8,7 +8,7 @@ class API::InvoicesController < API::ApiController
def index
authorize Invoice
@invoices = Invoice.includes(
:avoir, :invoiced, invoice_items: %i[subscription invoice_item], user: %i[profile trainings]
:avoir, :invoiced, :invoicing_profile, invoice_items: %i[subscription invoice_item]
).all.order('reference DESC')
end

View File

@ -12,11 +12,14 @@ class Invoice < ActiveRecord::Base
has_many :invoice_items, dependent: :destroy
accepts_nested_attributes_for :invoice_items
belongs_to :user
belongs_to :invoicing_profile
belongs_to :wallet_transaction
belongs_to :coupon
belongs_to :subscription, foreign_type: 'Subscription', foreign_key: 'invoiced_id'
belongs_to :reservation, foreign_type: 'Reservation', foreign_key: 'invoiced_id'
belongs_to :offer_day, foreign_type: 'OfferDay', foreign_key: 'invoiced_id'
has_one :avoir, class_name: 'Invoice', foreign_key: :invoice_id, dependent: :destroy
belongs_to :operator, foreign_key: :operator_id, class_name: 'User'
@ -38,6 +41,10 @@ class Invoice < ActiveRecord::Base
"#{ENV['INVOICE_PREFIX']}-#{id}_#{created_at.strftime('%d%m%Y')}.pdf"
end
def user
invoicing_profile.user
end
def generate_reference
pattern = Setting.find_by(name: 'invoice_reference').value
@ -245,7 +252,7 @@ class Invoice < ActiveRecord::Base
def generate_and_send_invoice
unless Rails.env.test?
puts "Creating an InvoiceWorker job to generate the following invoice: id(#{id}), invoiced_id(#{invoiced_id}), " \
"invoiced_type(#{invoiced_type}), user_id(#{user_id})"
"invoiced_type(#{invoiced_type}), user_id(#{invoicing_profile.user_id})"
end
InvoiceWorker.perform_async(id, user&.subscription&.expired_at)
end

View File

@ -3,4 +3,10 @@ class InvoicingProfile < ActiveRecord::Base
has_one :address, as: :placeable, dependent: :destroy
has_one :organization, dependent: :destroy
has_many :invoices, dependent: :destroy
def full_name
# if first_name or last_name is nil, the empty string will be used as a temporary replacement
(first_name || '').humanize.titleize + ' ' + (last_name || '').humanize.titleize
end
end

View File

@ -45,7 +45,6 @@ class User < ActiveRecord::Base
has_many :training_credits, through: :users_credits, source: :training_credit
has_many :machine_credits, through: :users_credits, source: :machine_credit
has_many :invoices, dependent: :destroy
has_many :operated_invoices, foreign_key: :operator_id, class_name: 'Invoice', dependent: :nullify
has_many :user_tags, dependent: :destroy
@ -133,6 +132,10 @@ class User < ActiveRecord::Base
my_projects.to_a.concat projects
end
def invoices
invoicing_profile.invoices
end
def generate_subscription_invoice(operator_id)
return unless subscription

View File

@ -9,8 +9,8 @@ class InvoicesService
# @param size {number} number of items per page
# @param filters {Hash} allowed filters: number, customer, date.
def self.list(order_key, direction, page, size, filters = {})
invoices = Invoice.includes(:avoir, :invoiced, invoice_items: %i[subscription invoice_item], user: %i[profile trainings])
.joins(user: :profile)
invoices = Invoice.includes(:avoir, :invoicing_profile, invoice_items: %i[subscription invoice_item])
.joins(:invoicing_profile)
.order("#{order_key} #{direction}")
.page(page)
.per(size)
@ -25,7 +25,7 @@ class InvoicesService
if filters[:customer].size.positive?
# ILIKE => PostgreSQL case-insensitive LIKE
invoices = invoices.where(
'profiles.first_name ILIKE :search OR profiles.last_name ILIKE :search',
'invoicing_profiles.first_name ILIKE :search OR invoicing_profiles.last_name ILIKE :search',
search: "%#{filters[:customer]}%"
)
end

View File

@ -1,11 +1,14 @@
# frozen_string_literal: true
max_invoices = @invoices.except(:offset, :limit, :order).count
json.array!(@invoices) do |invoice|
json.maxInvoices max_invoices
json.extract! invoice, :id, :created_at, :reference, :invoiced_type, :user_id, :avoir_date
json.extract! invoice, :id, :created_at, :reference, :invoiced_type, :avoir_date
json.user_id invoice.invoicing_profile.user_id
json.total (invoice.total / 100.00)
json.url invoice_url(invoice, format: :json)
json.name invoice.user.profile.full_name
json.name invoice.invoicing_profile.full_name
json.has_avoir invoice.refunded?
json.is_avoir invoice.is_a?(Avoir)
json.is_subscription_invoice invoice.subscription_invoice?