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

42 lines
859 B
Ruby
Raw Normal View History

# frozen_string_literal: true
require_relative 'cart_item'
# A discount coupon applied to the whole shopping cart
2022-12-28 17:51:27 +01:00
class CartItem::Coupon < ApplicationRecord
belongs_to :operator_profile, class_name: 'InvoicingProfile'
belongs_to :customer_profile, class_name: 'InvoicingProfile'
belongs_to :coupon
def operator
operator_profile.user
end
2022-12-28 17:51:27 +01:00
def customer
customer_profile.user
end
def price(cart_total = 0)
cs = CouponService.new
new_total = cs.apply(cart_total, coupon)
amount = new_total - cart_total
{ amount: amount, total_with_coupon: new_total, total_without_coupon: cart_total }
end
def type
'coupon'
end
def valid?(_all_items)
2022-12-28 17:51:27 +01:00
return true if coupon.nil?
2022-12-28 17:51:27 +01:00
if coupon.status(customer.id) != 'active'
errors.add(:coupon, 'invalid coupon')
return false
end
true
end
end