<commit_before>module Tray
  module Calculator
    module Discounters
      class ReductionCode < Discounter

        def initialize(cart, registers)
          @ticket_count = 0
          super(cart, registers)
        end

        def call
          @cart.reduction_codes.each do |code|
            next unless code.gift_card.current_value_in_cents > 0
            reduction_code_registers = applicable_registers(code)
            next unless reduction_code_registers.count > 0
            apply_reduction_code_registers(code, reduction_code_registers)
          end
        end

        def applicable_registers(code)
          @registers.select do |register|
            next false unless code.gift_card.organization_id == register.event.organization_id
            next false if code.gift_card.event_exclusions.include?(register.event.id)
            next false if (code.gift_card.valid_range_start && code.gift_card.valid_range_start > Time.now) || (code.gift_card.valid_range_end && code.gift_card.valid_range_end < Time.now)
            next true
          end
        end

        def apply_reduction_code_registers(code, registers)
          card_amount = code.gift_card.current_value_in_cents
          registers.each do |reg|
            if reg.line_items_total - card_amount >= 0
              discount = card_amount
            else
              discount = reg.line_items_total
            end
            card_amount = card_amount - discount
            reg.applied_reduction_codes.push({card: code, amount: discount, type: :credit})
          end
        end

      end
    end
  end
end<commit_msg>Apply gift card amount after memberships discount amounts.<commit_after>module Tray
  module Calculator
    module Discounters
      class ReductionCode < Discounter

        def initialize(cart, registers)
          @ticket_count = 0
          super(cart, registers)
        end

        def call
          @cart.reduction_codes.each do |code|
            next unless code.gift_card.current_value_in_cents > 0
            reduction_code_registers = applicable_registers(code)
            next unless reduction_code_registers.count > 0
            apply_reduction_code_registers(code, reduction_code_registers)
          end
        end

        def applicable_registers(code)
          @registers.select do |register|
            next false unless code.gift_card.organization_id == register.event.organization_id
            next false if code.gift_card.event_exclusions.include?(register.event.id)
            next false if (code.gift_card.valid_range_start && code.gift_card.valid_range_start > Time.now) || (code.gift_card.valid_range_end && code.gift_card.valid_range_end < Time.now)
            next true
          end
        end

        def apply_reduction_code_registers(code, registers)
          card_amount = code.gift_card.current_value_in_cents
          registers.each do |reg|
            line_items_total = reg.line_items_total - reg.membership_discount_total # Calculate the $ amount of gift card used after the membership discounts have been applied
            if line_items_total - card_amount >= 0
              discount = card_amount
            else
              discount = line_items_total
            end
            card_amount = card_amount - discount
            reg.applied_reduction_codes.push({card: code, amount: discount, type: :credit})
          end
        end

      end
    end
  end
end
{'fraction_non_alphanumeric': 0.059541097879756026, 'fraction_numerical': 0.0023235550392099913, 'mean_word_length': 2.215686274509804, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1783611', 'n_tokens_mistral': 966, 'n_tokens_neox': 927, 'n_words': 215}