Sha256: adf586507a49baecfce5251a4ee66b234283d82ec38479eba7d20ecceac7ad72

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

require 'active_support/concern'

module Spree
  class Order < ActiveRecord::Base
    module LoyaltyPoints
      extend ActiveSupport::Concern

      def loyalty_points_total
        loyalty_points_credit_transactions.sum(:loyalty_points) - loyalty_points_debit_transactions.sum(:loyalty_points)
      end

      def award_loyalty_points
        loyalty_points_earned = loyalty_points_for(item_total)
        if !loyalty_points_used?
          create_credit_transaction(loyalty_points_earned)
        end
      end

      def loyalty_points_awarded?
        loyalty_points_credit_transactions.count > 0
      end

      def loyalty_points_used?
        payments.any_with_loyalty_points?
      end

      module ClassMethods
        
        def credit_loyalty_points_to_user
          points_award_period = Spree::Config.loyalty_points_award_period
          uncredited_orders = Spree::Order.with_uncredited_loyalty_points(points_award_period)
          uncredited_orders.each do |order|
            order.award_loyalty_points
          end
        end

      end
      
      def create_credit_transaction(points)
        user.loyalty_points_credit_transactions.create(source: self, loyalty_points: points)
      end

      def create_debit_transaction(points)
        user.loyalty_points_debit_transactions.create(source: self, loyalty_points: points)
      end

      private

        def complete_loyalty_points_payments
          payments.by_loyalty_points.with_state('checkout').each { |payment| payment.complete! }
        end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_loyalty_points-1.1.0 app/models/concerns/spree/order/loyalty_points.rb
spree_loyalty_points-1.0.2 app/models/concerns/spree/order/loyalty_points.rb
spree_loyalty_points-1.0.1 app/models/concerns/spree/order/loyalty_points.rb
spree_loyalty_points-1.0.0 app/models/concerns/spree/order/loyalty_points.rb