<commit_before># frozen_string_literal: false

module Spree
  class DefaultTaxZoneValidator < ActiveModel::Validator
    def validate(record)
      return unless record.included_in_price

      return if Zone.default_tax

      record.errors.add(:included_in_price, Spree.t("errors.messages.included_price_validation"))
    end
  end
end

module Spree
  class TaxRate < ActiveRecord::Base
    acts_as_paranoid
    include Spree::Core::CalculatedAdjustments
    belongs_to :zone, class_name: "Spree::Zone", inverse_of: :tax_rates
    belongs_to :tax_category, class_name: "Spree::TaxCategory", inverse_of: :tax_rates

    validates :amount, presence: true, numericality: true
    validates :tax_category, presence: true
    validates_with DefaultTaxZoneValidator

    scope :by_zone, ->(zone) { where(zone_id: zone) }

    # Gets the array of TaxRates appropriate for the specified order
    def self.match(order)
      return [] if order.distributor && !order.distributor.charges_sales_tax
      return [] unless order.tax_zone

      all.select do |rate|
        rate.zone == order.tax_zone || rate.zone.contains?(order.tax_zone) || rate.zone.default_tax
      end
    end

    def self.adjust(order)
      order.all_adjustments.tax.destroy_all

      match(order).each do |rate|
        rate.adjust(order)
      end
    end

    # For VAT, the default rate is the rate that is configured for the default category
    # It is needed for every price calculation (as all customer facing prices include VAT)
    # Here we return the actual amount, which may be 0 in case of wrong setup, but is never nil
    def self.default
      category = TaxCategory.includes(:tax_rates).find_by(is_default: true)
      return 0 unless category

      address ||= Address.new(country_id: Spree::Config[:default_country_id])
      rate = category.tax_rates.detect { |tax_rate| tax_rate.zone.include? address }.try(:amount)

      rate || 0
    end

    # Creates necessary tax adjustments for the order.
    def adjust(order)
      label = create_label
      if included_in_price
        if default_zone_or_zone_match? order
          order.line_items.each { |line_item| create_adjustment(label, line_item, line_item) }
          order.shipments.each { |shipment| create_adjustment(label, shipment, shipment) }
        else
          amount = -1 * calculator.compute(order)
          label = Spree.t(:refund) + label

          order.adjustments.create(
            amount: amount,
            source: order,
            originator: self,
            order: order,
            state: "closed",
            label: label
          )
        end
      else
        create_adjustment(label, order, order)
      end

      order.adjustments.reload
      order.line_items.reload
    end

    def default_zone_or_zone_match?(order)
      Zone.default_tax.contains?(order.tax_zone) || order.tax_zone == zone<commit_msg>Tax adjustments should be open when created<commit_after># frozen_string_literal: false

module Spree
  class DefaultTaxZoneValidator < ActiveModel::Validator
    def validate(record)
      return unless record.included_in_price

      return if Zone.default_tax

      record.errors.add(:included_in_price, Spree.t("errors.messages.included_price_validation"))
    end
  end
end

module Spree
  class TaxRate < ActiveRecord::Base
    acts_as_paranoid
    include Spree::Core::CalculatedAdjustments
    belongs_to :zone, class_name: "Spree::Zone", inverse_of: :tax_rates
    belongs_to :tax_category, class_name: "Spree::TaxCategory", inverse_of: :tax_rates

    validates :amount, presence: true, numericality: true
    validates :tax_category, presence: true
    validates_with DefaultTaxZoneValidator

    scope :by_zone, ->(zone) { where(zone_id: zone) }

    # Gets the array of TaxRates appropriate for the specified order
    def self.match(order)
      return [] if order.distributor && !order.distributor.charges_sales_tax
      return [] unless order.tax_zone

      all.select do |rate|
        rate.zone == order.tax_zone || rate.zone.contains?(order.tax_zone) || rate.zone.default_tax
      end
    end

    def self.adjust(order)
      order.all_adjustments.tax.destroy_all

      match(order).each do |rate|
        rate.adjust(order)
      end
    end

    # For VAT, the default rate is the rate that is configured for the default category
    # It is needed for every price calculation (as all customer facing prices include VAT)
    # Here we return the actual amount, which may be 0 in case of wrong setup, but is never nil
    def self.default
      category = TaxCategory.includes(:tax_rates).find_by(is_default: true)
      return 0 unless category

      address ||= Address.new(country_id: Spree::Config[:default_country_id])
      rate = category.tax_rates.detect { |tax_rate| tax_rate.zone.include? address }.try(:amount)

      rate || 0
    end

    # Creates necessary tax adjustments for the order.
    def adjust(order)
      label = create_label
      if included_in_price
        if default_zone_or_zone_match? order
          order.line_items.each { |line_item| create_adjustment(label, line_item, line_item, false, "open") }
          order.shipments.each { |shipment| create_adjustment(label, shipment, shipment, false, "open") }
        else
          amount = -1 * calculator.compute(order)
          label = Spree.t(:refund) + label

          order.adjustments.create(
            amount: amount,
            source: order,
            originator: self,
            order: order,
            state: "closed",
            label: label
          )
        end
      else
        create_adjustment(label, order, order, false, "open")
      end

      order.adjustments.reload
      order.line_items.reload
    end

    def default_zone_or_zone_match?(order)
      Zone.default_tax.contains?(order.tax_zone) || order.tax_zone == zone
{'fraction_non_alphanumeric': 0.0845694799658994, 'fraction_numerical': 0.0013640238704177323, 'mean_word_length': 2.929002009377093, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5803457', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1675, 'n_words': 459}