<commit_before>module SolidusAvataxCertified
  class Seeder
    class << self

      def seed!
        create_use_codes
        create_tax
        add_tax_category_to_shipping_methods
        add_tax_category_to_products
        populate_default_stock_location

        puts "***** SOLIDUS AVATAX CERTIFIED *****"
        puts ""
        puts "Please remember to:"
        puts "- Add tax category to all shipping methods that need to be taxed."
        puts "- Don't assign anything default tax."
        puts "- Assign proper Tax Category to each product"
        puts "- Fill in Stock Location Address."
        puts "- Fill Origin Address in Avatax Settings."
        puts ""
        puts "***** SOLIDUS AVATAX CERTIFIED *****"
      end

      def seed_use_codes!
        create_use_codes
        puts "***** SOLIDUS AVATAX CERTIFIED: Use Codes Seeded"
      end

      def create_tax
        default_tax_category = Spree::TaxCategory.find_by(name: 'Default')
        default_tax_rate = Spree::TaxRate.find_by(name: 'North America')

        default_tax_category.destroy if default_tax_category
        default_tax_rate.destroy if default_tax_rate

        clothing = Spree::TaxCategory.find_or_create_by(name: 'Clothing')
        clothing.update_attributes(tax_code: 'P0000000')

        shipping = Spree::TaxCategory.find_or_create_by(name: 'Shipping', tax_code: 'FR000000')

        sales_tax = Spree::TaxRate.create(tax_category: clothing, name: 'Tax', amount: BigDecimal.new('0'), zone: Spree::Zone.find_by_name('North America'), show_rate_in_label: false)
        sales_tax.calculator = Spree::Calculator::AvalaraTransaction.create!
        sales_tax.save!

        shipping_tax = Spree::TaxRate.create(name: 'Shipping Tax', tax_category: shipping, amount: BigDecimal.new('0'), zone: Spree::Zone.find_by_name('North America'), show_rate_in_label: false)
        shipping_tax.calculator = Spree::Calculator::AvalaraTransaction.create!
        shipping_tax.save!
      end

      def add_tax_category_to_shipping_methods
        Spree::ShippingMethod.update_all(tax_category_id: Spree::TaxCategory.find_by(name: 'Shipping').id)
      end

      def add_tax_category_to_products
        Spree::Product.update_all(tax_category_id: Spree::TaxCategory.find_by(name: 'Clothing').id)
      end

      def populate_default_stock_location
        default = Spree::StockLocation.find_by(name: 'default')
        default.destroy if default

        state = Spree::State.find_by(name: 'Alabama')
<commit_msg>Some seeds, specifically `Spree::TaxRate.find_or_create_by!(name: 'North America')` were failing if that TaxRate didn't exist. This fixes the validation errors and allows it to be re-run without causing errors or further issues.<commit_after>module SolidusAvataxCertified
  class Seeder
    class << self

      def seed!
        create_use_codes
        create_tax
        add_tax_category_to_shipping_methods
        add_tax_category_to_products
        populate_default_stock_location

        puts "***** SOLIDUS AVATAX CERTIFIED *****"
        puts ""
        puts "Please remember to:"
        puts "- Add tax category to all shipping methods that need to be taxed."
        puts "- Don't assign anything default tax."
        puts "- Assign proper Tax Category to each product"
        puts "- Fill in Stock Location Address."
        puts "- Fill Origin Address in Avatax Settings."
        puts ""
        puts "***** SOLIDUS AVATAX CERTIFIED *****"
      end

      def seed_use_codes!
        create_use_codes
        puts "***** SOLIDUS AVATAX CERTIFIED: Use Codes Seeded"
      end

      def create_tax
        default_tax_category = Spree::TaxCategory.find_by(name: 'Default')
        default_tax_rate = Spree::TaxRate.find_by(name: 'North America')

        default_tax_category.destroy if default_tax_category
        default_tax_rate.destroy if default_tax_rate

        clothing = Spree::TaxCategory.find_or_create_by(name: 'Clothing')
        clothing.update_attributes(tax_code: 'P0000000')
        tax_zone = Spree::Zone.find_or_create_by(name: 'North America')
        tax_calculator = Spree::Calculator::AvalaraTransactionCalculator.create!
        sales_tax = Spree::TaxRate.find_or_create_by(name: 'Tax') do |tax_rate|
          # default values for the create
          tax_rate.amount = BigDecimal.new('0')
          tax_rate.calculator = tax_calculator
          tax_rate.tax_category = clothing
        end
        sales_tax.update!(tax_category: clothing, name: 'Tax', amount: BigDecimal.new('0'), zone: tax_zone, show_rate_in_label: false, calculator: tax_calculator)

        shipping = Spree::TaxCategory.find_or_create_by(name: 'Shipping', tax_code: 'FR000000')
        shipping_tax = Spree::TaxRate.find_or_create_by(name: 'Shipping Tax') do |shipping_tax|
          shipping_tax.tax_category = shipping
          shipping_tax.amount = BigDecimal.new('0')
          shipping_tax.zone = tax_zone
          shipping_tax.show_rate_in_label = false
        end
        shipping_tax.update!(tax_category: shipping, amount: BigDecimal.new('0'), zone: Spree::Zone.find_by_name('North America'), show_rate_in_label: false, calculator: Spree::Calculator::AvalaraTransactionCalculator.create!)
      end

      def add_tax_category_to_shipping_methods
        Spree::ShippingMethod.update_all(tax_category_id: Spree::TaxCategory.find_by(name: 'Shipping').id)
      end

      def add_tax_category_to_products
        Spree::Product.update_all(tax_category_id: Spree::TaxCategory.find_by(name: 'Clothing').id)
      end

      def populate_default_stock_location
        default = Spree::StockLocation.find_by(name: 'default')
        default.destroy if default

        state = Spree::State.find_by(name: 'Alabama')

{'fraction_non_alphanumeric': 0.09324982602644398, 'fraction_numerical': 0.0055671537926235215, 'mean_word_length': 3.2870991797166296, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5986379', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1717, 'n_words': 377}