Class: Discount
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Discount
- 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
- #amount ⇒ Object readonly
-
#coupon_description ⇒ Object
Returns the value of attribute coupon_description.
- #coupon_id ⇒ Object readonly
- #coupon_serial_number_id ⇒ Object readonly
- #user_amount ⇒ Object readonly
Belongs to collapse
Methods included from Models::Auditable
Has many collapse
Delegated Instance Attributes collapse
-
#auto_apply ⇒ Object
Alias for Coupon#auto_apply.
-
#blacklistable? ⇒ Object
Alias for Coupon#blacklistable?.
-
#code ⇒ Object
Alias for Coupon#code.
Class Method Summary collapse
-
.active ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are active.
-
.auto_applied ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are auto applied.
-
.excluding_shipping ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are excluding shipping.
-
.fixed_dollar ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are fixed dollar.
-
.free_online_shipping ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are free online shipping.
-
.manually_applied ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are manually applied.
-
.non_blacklisted ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are non blacklisted.
-
.non_zero_value ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are non zero value.
-
.sorted ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are sorted.
-
.tier1 ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are tier1.
-
.tier1and3 ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are tier1and3.
-
.tier2 ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are tier2.
-
.tier3 ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are tier3.
-
.visible ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are visible.
-
.with_economy_shipping_match_crm ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are with economy shipping match crm.
-
.zero_value ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are zero value.
Instance Method Summary collapse
-
#apply_source ⇒ Object
Apply the source specified in the coupon if applicable.
- #discount_sum_by_tax_class(tax_class) ⇒ Object
- #goods ⇒ Object
- #invisible? ⇒ Boolean
- #mark_as_blacklisted ⇒ Object
- #preserve_amount? ⇒ Boolean
- #services ⇒ Object
- #shipping ⇒ Object
- #sticky? ⇒ Boolean
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
Instance Attribute Details
#amount ⇒ Object (readonly)
45 |
# File 'app/models/discount.rb', line 45 validates :amount, numericality: true |
#coupon_description ⇒ Object
Returns the value of attribute coupon_description.
41 42 43 |
# File 'app/models/discount.rb', line 41 def coupon_description @coupon_description end |
#coupon_id ⇒ Object (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_id ⇒ Object (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_amount ⇒ Object (readonly)
47 |
# File 'app/models/discount.rb', line 47 validates :user_amount, presence: { if: ->(_d) { coupon&.adjustable } } |
Class Method Details
.active ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are active. Active Record Scope
53 |
# File 'app/models/discount.rb', line 53 scope :active, -> { joins(:coupon).merge(Coupon.active) } |
.auto_applied ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are auto applied. Active Record Scope
58 |
# File 'app/models/discount.rb', line 58 scope :auto_applied, -> { where(auto_apply: true) } |
.excluding_shipping ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are excluding shipping. Active Record Scope
66 |
# File 'app/models/discount.rb', line 66 scope :excluding_shipping, -> { joins(line_discounts: :line_item).where.not(line_item: { tax_class: 'shp' }) } |
.fixed_dollar ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are fixed dollar. Active Record Scope
65 |
# File 'app/models/discount.rb', line 65 scope :fixed_dollar, -> { joins(:coupon).merge(Coupon.fixed_dollar) } |
.free_online_shipping ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are free online shipping. Active Record Scope
67 |
# File 'app/models/discount.rb', line 67 scope :free_online_shipping, -> { joins(:coupon).merge(Coupon.free_online_shipping) } |
.manually_applied ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are manually applied. Active Record Scope
57 |
# File 'app/models/discount.rb', line 57 scope :manually_applied, -> { where.not(auto_apply: true) } |
.non_blacklisted ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are non blacklisted. Active Record Scope
59 |
# File 'app/models/discount.rb', line 59 scope :non_blacklisted, -> { where(blacklisted: [nil, false]) } |
.non_zero_value ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are non zero value. Active Record Scope
56 |
# File 'app/models/discount.rb', line 56 scope :non_zero_value, -> { where.not(amount: 0) } |
.sorted ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are sorted. Active Record Scope
60 |
# File 'app/models/discount.rb', line 60 scope :sorted, -> { joins(:coupon).merge(Coupon.sorted) } |
.tier1 ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are tier1. Active Record Scope
61 |
# File 'app/models/discount.rb', line 61 scope :tier1, -> { sorted.merge(Coupon.tier1) } |
.tier1and3 ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are tier1and3. Active Record Scope
64 |
# File 'app/models/discount.rb', line 64 scope :tier1and3, -> { sorted.merge(Coupon.tier1and3) } |
.tier2 ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are tier2. Active Record Scope
62 |
# File 'app/models/discount.rb', line 62 scope :tier2, -> { sorted.merge(Coupon.tier2) } |
.tier3 ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are tier3. Active Record Scope
63 |
# File 'app/models/discount.rb', line 63 scope :tier3, -> { sorted.merge(Coupon.tier3) } |
.visible ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are visible. Active Record Scope
54 |
# File 'app/models/discount.rb', line 54 scope :visible, -> { where(Discount[:amount].lt(0)) } |
.with_economy_shipping_match_crm ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are with economy shipping match crm. Active Record Scope
68 |
# File 'app/models/discount.rb', line 68 scope :with_economy_shipping_match_crm, -> { joins(:coupon).merge(Coupon.with_economy_shipping_match_crm) } |
.zero_value ⇒ ActiveRecord::Relation<Discount>
A relation of Discounts that are zero value. Active Record Scope
55 |
# File 'app/models/discount.rb', line 55 scope :zero_value, -> { where(amount: 0) } |
Instance Method Details
#apply_source ⇒ Object
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_apply ⇒ Object
Alias for Coupon#auto_apply
70 |
# File 'app/models/discount.rb', line 70 delegate :blacklistable?, :auto_apply, :code, to: :coupon |
#blacklistable? ⇒ Object
Alias for Coupon#blacklistable?
70 |
# File 'app/models/discount.rb', line 70 delegate :blacklistable?, :auto_apply, :code, to: :coupon |
#code ⇒ Object
Alias for Coupon#code
70 |
# File 'app/models/discount.rb', line 70 delegate :blacklistable?, :auto_apply, :code, to: :coupon |
#coupon ⇒ Coupon
35 |
# File 'app/models/discount.rb', line 35 belongs_to :coupon, touch: :last_used_at |
#coupon_serial_number ⇒ CouponSerialNumber
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 |
#goods ⇒ Object
80 81 82 |
# File 'app/models/discount.rb', line 80 def goods discount_sum_by_tax_class 'g' end |
#invisible? ⇒ Boolean
100 101 102 |
# File 'app/models/discount.rb', line 100 def invisible? amount.zero? or blacklisted end |
#itemizable ⇒ Itemizable
36 |
# File 'app/models/discount.rb', line 36 belongs_to :itemizable, polymorphic: true |
#line_discounts ⇒ ActiveRecord::Relation<LineDiscount>
39 |
# File 'app/models/discount.rb', line 39 has_many :line_discounts, dependent: :destroy, autosave: true |
#mark_as_blacklisted ⇒ Object
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
104 105 106 |
# File 'app/models/discount.rb', line 104 def preserve_amount? coupon.adjustable end |
#services ⇒ Object
84 85 86 |
# File 'app/models/discount.rb', line 84 def services discount_sum_by_tax_class 'svc' end |
#shipping ⇒ Object
88 89 90 |
# File 'app/models/discount.rb', line 88 def shipping discount_sum_by_tax_class 'shp' end |
#sticky? ⇒ Boolean
72 73 74 |
# File 'app/models/discount.rb', line 72 def sticky? blacklisted or coupon.sticky? end |