Class: Discount

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/discount.rb

Overview

== Schema Information

Table name: discounts
Database name: primary

id :integer not null, primary key
amount :decimal(10, 2)
blacklisted :boolean
effective_date :datetime
itemizable_type :string(50)
notes :text
user_amount :decimal(10, 2)
created_at :datetime
updated_at :datetime
coupon_id :integer not null
coupon_serial_number_id :uuid
itemizable_id :integer not null
legacy_coupon_id :integer

Indexes

by_itid_itt_csnid (itemizable_id,itemizable_type,coupon_serial_number_id)
idx_coupon_id (coupon_id)
index_discounts_unique_per_itemizable (itemizable_type,itemizable_id,coupon_id) UNIQUE

Foreign Keys

fk_rails_... (coupon_serial_number_id => coupon_serial_numbers.id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Delegated Instance Attributes collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#amountObject (readonly)



45
# File 'app/models/discount.rb', line 45

validates :amount, numericality: true

#coupon_descriptionObject

Returns the value of attribute coupon_description.



41
42
43
# File 'app/models/discount.rb', line 41

def coupon_description
  @coupon_description
end

#coupon_idObject (readonly)



48
# File 'app/models/discount.rb', line 48

validates :coupon_id, uniqueness: { scope: %i[itemizable_type itemizable_id], if: :itemizable_id }

#coupon_serial_number_idObject (readonly)



49
# File 'app/models/discount.rb', line 49

validates :coupon_serial_number_id, uniqueness: { allow_nil: true, if: :itemizable_requires_unique_coupon_serial? }

#user_amountObject (readonly)



47
# File 'app/models/discount.rb', line 47

validates :user_amount, presence: { if: ->(_d) { coupon&.adjustable } }

Class Method Details

.activeActiveRecord::Relation<Discount>

A relation of Discounts that are active. Active Record Scope

Returns:

See Also:



53
# File 'app/models/discount.rb', line 53

scope :active, -> { joins(:coupon).merge(Coupon.active) }

.auto_appliedActiveRecord::Relation<Discount>

A relation of Discounts that are auto applied. Active Record Scope

Returns:

See Also:



58
# File 'app/models/discount.rb', line 58

scope :auto_applied, -> { where(auto_apply: true) }

.excluding_shippingActiveRecord::Relation<Discount>

A relation of Discounts that are excluding shipping. Active Record Scope

Returns:

See Also:



66
# File 'app/models/discount.rb', line 66

scope :excluding_shipping, -> { joins(line_discounts: :line_item).where.not(line_item: { tax_class: 'shp' }) }

.fixed_dollarActiveRecord::Relation<Discount>

A relation of Discounts that are fixed dollar. Active Record Scope

Returns:

See Also:



65
# File 'app/models/discount.rb', line 65

scope :fixed_dollar, -> { joins(:coupon).merge(Coupon.fixed_dollar) }

.free_online_shippingActiveRecord::Relation<Discount>

A relation of Discounts that are free online shipping. Active Record Scope

Returns:

See Also:



67
# File 'app/models/discount.rb', line 67

scope :free_online_shipping, -> { joins(:coupon).merge(Coupon.free_online_shipping) }

.manually_appliedActiveRecord::Relation<Discount>

A relation of Discounts that are manually applied. Active Record Scope

Returns:

See Also:



57
# File 'app/models/discount.rb', line 57

scope :manually_applied, -> { where.not(auto_apply: true) }

.non_blacklistedActiveRecord::Relation<Discount>

A relation of Discounts that are non blacklisted. Active Record Scope

Returns:

See Also:



59
# File 'app/models/discount.rb', line 59

scope :non_blacklisted, -> { where(blacklisted: [nil, false]) }

.non_zero_valueActiveRecord::Relation<Discount>

A relation of Discounts that are non zero value. Active Record Scope

Returns:

See Also:



56
# File 'app/models/discount.rb', line 56

scope :non_zero_value, -> { where.not(amount: 0) }

.sortedActiveRecord::Relation<Discount>

A relation of Discounts that are sorted. Active Record Scope

Returns:

See Also:



60
# File 'app/models/discount.rb', line 60

scope :sorted, -> { joins(:coupon).merge(Coupon.sorted) }

.tier1ActiveRecord::Relation<Discount>

A relation of Discounts that are tier1. Active Record Scope

Returns:

See Also:



61
# File 'app/models/discount.rb', line 61

scope :tier1, -> { sorted.merge(Coupon.tier1) }

.tier1and3ActiveRecord::Relation<Discount>

A relation of Discounts that are tier1and3. Active Record Scope

Returns:

See Also:



64
# File 'app/models/discount.rb', line 64

scope :tier1and3, -> { sorted.merge(Coupon.tier1and3) }

.tier2ActiveRecord::Relation<Discount>

A relation of Discounts that are tier2. Active Record Scope

Returns:

See Also:



62
# File 'app/models/discount.rb', line 62

scope :tier2, -> { sorted.merge(Coupon.tier2) }

.tier3ActiveRecord::Relation<Discount>

A relation of Discounts that are tier3. Active Record Scope

Returns:

See Also:



63
# File 'app/models/discount.rb', line 63

scope :tier3, -> { sorted.merge(Coupon.tier3) }

.visibleActiveRecord::Relation<Discount>

A relation of Discounts that are visible. Active Record Scope

Returns:

See Also:



54
# File 'app/models/discount.rb', line 54

scope :visible, -> { where(Discount[:amount].lt(0)) }

.with_economy_shipping_match_crmActiveRecord::Relation<Discount>

A relation of Discounts that are with economy shipping match crm. Active Record Scope

Returns:

See Also:



68
# File 'app/models/discount.rb', line 68

scope :with_economy_shipping_match_crm, -> { joins(:coupon).merge(Coupon.with_economy_shipping_match_crm) }

.zero_valueActiveRecord::Relation<Discount>

A relation of Discounts that are zero value. Active Record Scope

Returns:

See Also:



55
# File 'app/models/discount.rb', line 55

scope :zero_value, -> { where(amount: 0) }

Instance Method Details

#apply_sourceObject

Apply the source specified in the coupon if applicable.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/discount.rb', line 109

def apply_source
  return unless coupon && itemizable && coupon.source

  case itemizable_type.downcase.to_sym
  when :order
    coupon.apply_source_to_order(itemizable)
    coupon.apply_source_to_opportunity(itemizable.opportunity) if itemizable.opportunity
    coupon.apply_source_to_customer(itemizable.customer)
  when :quote
    return unless itemizable.opportunity

    coupon.apply_source_to_opportunity(itemizable.opportunity)
    itemizable.opportunity.orders.each { |order| coupon.apply_source_to_order(order) }
    coupon.apply_source_to_customer(itemizable.opportunity.customer)
  end
end

#auto_applyObject

Alias for Coupon#auto_apply

Returns:

  • (Object)

    Coupon#auto_apply

See Also:



70
# File 'app/models/discount.rb', line 70

delegate :blacklistable?, :auto_apply, :code, to: :coupon

#blacklistable?Object

Alias for Coupon#blacklistable?

Returns:

  • (Object)

    Coupon#blacklistable?

See Also:



70
# File 'app/models/discount.rb', line 70

delegate :blacklistable?, :auto_apply, :code, to: :coupon

#codeObject

Alias for Coupon#code

Returns:

  • (Object)

    Coupon#code

See Also:



70
# File 'app/models/discount.rb', line 70

delegate :blacklistable?, :auto_apply, :code, to: :coupon

#couponCoupon

Returns:

See Also:



35
# File 'app/models/discount.rb', line 35

belongs_to :coupon, touch: :last_used_at

#coupon_serial_numberCouponSerialNumber



37
# File 'app/models/discount.rb', line 37

belongs_to :coupon_serial_number, optional: true

#discount_sum_by_tax_class(tax_class) ⇒ Object



76
77
78
# File 'app/models/discount.rb', line 76

def discount_sum_by_tax_class(tax_class)
  line_discounts.joins(:line_item).where(LineItem[:tax_class].eq(tax_class)).sum(:amount)
end

#goodsObject



80
81
82
# File 'app/models/discount.rb', line 80

def goods
  discount_sum_by_tax_class 'g'
end

#invisible?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/models/discount.rb', line 100

def invisible?
  amount.zero? or blacklisted
end

#itemizableItemizable

Returns:

  • (Itemizable)

See Also:



36
# File 'app/models/discount.rb', line 36

belongs_to :itemizable, polymorphic: true

#line_discountsActiveRecord::Relation<LineDiscount>

Returns:

See Also:



39
# File 'app/models/discount.rb', line 39

has_many :line_discounts, dependent: :destroy, autosave: true

#mark_as_blacklistedObject



92
93
94
95
96
97
98
# File 'app/models/discount.rb', line 92

def mark_as_blacklisted
  return if Coupon::SKIP_BLACKLISTING_COUPON_CODES.include?(code)

  self.amount = 0
  line_discounts.each(&:destroy)
  self.blacklisted = true
end

#preserve_amount?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/discount.rb', line 104

def preserve_amount?
  coupon.adjustable
end

#servicesObject



84
85
86
# File 'app/models/discount.rb', line 84

def services
  discount_sum_by_tax_class 'svc'
end

#shippingObject



88
89
90
# File 'app/models/discount.rb', line 88

def shipping
  discount_sum_by_tax_class 'shp'
end

#sticky?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/discount.rb', line 72

def sticky?
  blacklisted or coupon.sticky?
end