2019-06-04 13:33:00 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This table will save the user's profile data needed for legal accounting (invoices, wallet, etc.)
|
|
|
|
# Legal accounting must be kept for 10 years but GDPR requires that an user can delete his account at any time.
|
|
|
|
# The data will be kept even if the user is deleted, but it will be unlinked from the user's account.
|
2020-03-25 10:16:47 +01:00
|
|
|
class InvoicingProfile < ApplicationRecord
|
2019-05-21 16:07:40 +02:00
|
|
|
belongs_to :user
|
2019-05-22 12:45:45 +02:00
|
|
|
has_one :address, as: :placeable, dependent: :destroy
|
2019-05-29 14:28:14 +02:00
|
|
|
accepts_nested_attributes_for :address, allow_destroy: true
|
2019-05-22 12:45:45 +02:00
|
|
|
has_one :organization, dependent: :destroy
|
2019-05-29 14:28:14 +02:00
|
|
|
accepts_nested_attributes_for :organization, allow_destroy: false
|
2019-05-22 17:49:22 +02:00
|
|
|
has_many :invoices, dependent: :destroy
|
2020-11-12 16:44:55 +01:00
|
|
|
has_many :payment_schedules, dependent: :destroy
|
2019-05-28 16:49:36 +02:00
|
|
|
|
2019-06-03 16:00:09 +02:00
|
|
|
has_one :wallet, dependent: :destroy
|
|
|
|
has_many :wallet_transactions, dependent: :destroy
|
|
|
|
|
2019-06-03 16:51:43 +02:00
|
|
|
has_many :history_values, dependent: :nullify
|
|
|
|
|
2022-11-18 16:42:11 +01:00
|
|
|
has_many :operated_invoices, foreign_key: :operator_profile_id, class_name: 'Invoice', dependent: :nullify, inverse_of: :operator_profile
|
|
|
|
has_many :operated_payment_schedules, foreign_key: :operator_profile_id, class_name: 'PaymentSchedule',
|
|
|
|
dependent: :nullify, inverse_of: :operator_profile
|
2019-06-12 12:22:38 +02:00
|
|
|
|
2022-11-18 16:42:11 +01:00
|
|
|
has_many :user_profile_custom_fields, dependent: :destroy
|
2022-03-18 19:44:30 +01:00
|
|
|
has_many :profile_custom_fields, through: :user_profile_custom_fields
|
|
|
|
accepts_nested_attributes_for :user_profile_custom_fields, allow_destroy: true
|
|
|
|
|
2022-11-18 16:42:11 +01:00
|
|
|
has_many :accounting_lines, dependent: :destroy
|
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
# as operator
|
|
|
|
has_many :operated_cart_item_event_reservations, class_name: 'CartItem::EventReservation', dependent: :nullify, inverse_of: :operator_profile
|
|
|
|
has_many :operated_cart_item_machine_reservations, class_name: 'CartItem::MachineReservation', dependent: :nullify,
|
|
|
|
inverse_of: :operator_profile
|
|
|
|
has_many :operated_cart_item_space_reservations, class_name: 'CartItem::SpaceReservation', dependent: :nullify, inverse_of: :operator_profile
|
|
|
|
has_many :operated_cart_item_training_reservations, class_name: 'CartItem::TrainingReservation', dependent: :nullify,
|
|
|
|
inverse_of: :operator_profile
|
|
|
|
has_many :operated_cart_item_coupon, class_name: 'CartItem::Coupon', dependent: :nullify, inverse_of: :operator_profile
|
|
|
|
# as customer
|
|
|
|
has_many :cart_item_event_reservations, class_name: 'CartItem::EventReservation', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
has_many :cart_item_machine_reservations, class_name: 'CartItem::MachineReservation', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
has_many :cart_item_space_reservations, class_name: 'CartItem::SpaceReservation', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
has_many :cart_item_training_reservations, class_name: 'CartItem::TrainingReservation', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
has_many :cart_item_free_extensions, class_name: 'CartItem::FreeExtension', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
has_many :cart_item_subscriptions, class_name: 'CartItem::Subscription', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
has_many :cart_item_prepaid_packs, class_name: 'CartItem::PrepaidPack', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
has_many :cart_item_coupons, class_name: 'CartItem::Coupon', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
has_many :cart_item_payment_schedules, class_name: 'CartItem::PaymentSchedule', dependent: :destroy, inverse_of: :customer_profile
|
|
|
|
|
2022-12-09 10:56:39 +01:00
|
|
|
before_validation :set_external_id_nil
|
|
|
|
validates :external_id, uniqueness: true, allow_blank: true
|
2021-03-22 18:02:56 +01:00
|
|
|
validates :address, presence: true, if: -> { Setting.get('address_required') }
|
|
|
|
|
2019-05-28 16:49:36 +02:00
|
|
|
def full_name
|
|
|
|
# if first_name or last_name is nil, the empty string will be used as a temporary replacement
|
2022-11-18 16:42:11 +01:00
|
|
|
"#{(first_name || '').humanize.titleize} #{(last_name || '').humanize.titleize}"
|
2019-05-28 16:49:36 +02:00
|
|
|
end
|
2022-12-07 12:56:35 +01:00
|
|
|
|
|
|
|
def invoicing_address
|
|
|
|
if organization&.address
|
|
|
|
organization.address.address
|
|
|
|
elsif address
|
|
|
|
address.address
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
2022-12-09 10:56:39 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_external_id_nil
|
|
|
|
self.external_id = nil if external_id.blank?
|
|
|
|
end
|
2019-05-21 16:07:40 +02:00
|
|
|
end
|