2022-08-19 19:59:13 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
# Order is a model used to hold orders data
|
2022-08-26 15:56:20 +02:00
|
|
|
class Order < PaymentDocument
|
2022-08-19 19:59:13 +02:00
|
|
|
belongs_to :statistic_profile
|
2022-08-26 11:55:35 +02:00
|
|
|
belongs_to :operator_profile, class_name: 'InvoicingProfile'
|
2022-08-26 20:10:21 +02:00
|
|
|
belongs_to :coupon
|
2022-09-09 16:35:49 +02:00
|
|
|
belongs_to :invoice
|
2022-08-19 19:59:13 +02:00
|
|
|
has_many :order_items, dependent: :destroy
|
2022-10-12 11:42:15 +02:00
|
|
|
has_one :payment_gateway_object, as: :item, dependent: :destroy
|
2022-09-15 20:19:19 +02:00
|
|
|
has_many :order_activities, dependent: :destroy
|
2022-08-19 19:59:13 +02:00
|
|
|
|
2022-09-16 17:54:41 +02:00
|
|
|
ALL_STATES = %w[cart paid payment_failed refunded in_progress ready canceled delivered].freeze
|
2022-08-19 19:59:13 +02:00
|
|
|
enum state: ALL_STATES.zip(ALL_STATES).to_h
|
|
|
|
|
|
|
|
validates :token, :state, presence: true
|
2022-08-26 11:19:09 +02:00
|
|
|
|
2022-08-26 15:56:20 +02:00
|
|
|
before_create :add_environment
|
2022-09-06 19:16:29 +02:00
|
|
|
after_create :update_reference
|
2022-08-26 15:56:20 +02:00
|
|
|
|
2022-08-26 20:10:21 +02:00
|
|
|
delegate :user, to: :statistic_profile
|
|
|
|
|
2023-02-14 13:10:58 +01:00
|
|
|
def generate_reference(_date = Time.current)
|
2022-09-09 10:43:44 +02:00
|
|
|
self.reference = PaymentDocumentService.generate_order_number(self)
|
|
|
|
end
|
|
|
|
|
2022-08-26 15:56:20 +02:00
|
|
|
def footprint_children
|
|
|
|
order_items
|
|
|
|
end
|
|
|
|
|
|
|
|
def paid_by_card?
|
|
|
|
!payment_gateway_object.nil? && payment_method == 'card'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.columns_out_of_footprint
|
|
|
|
%w[payment_method]
|
2022-08-26 11:19:09 +02:00
|
|
|
end
|
2022-08-19 19:59:13 +02:00
|
|
|
end
|