Sha256: 44f2d45a2a74f7d10a2da8fa604dd2e0f3ab4e979d419e82af01836bc84bb0cd

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

Spree::Variant.class_eval do

  scope :active_on_winkelstraat, -> {joins(:product).where(spree_products: {sell_on_winkelstraat: true})}

  delegate :sell_on_winkelstraat, to: :product

  def self.winkelstraat_stock_csv(stock_location_name = :all)
    stock_location = Spree::StockLocation.find_by_admin_name(stock_location_name) || Spree::StockLocation.find_by_name!(stock_location_name) if stock_location_name != :all
    CSV.generate(headers: false) do |csv|
      size_option_type = Spree::OptionType.includes(:translations).where(spree_option_type_translations: {name: 'size'}).first
      size_option_values = size_option_type.option_values.map do |ov|
        {
            option_value_id: ov.id,
            presentation: ov.presentation
        }
      end
      Spree::Variant.active_on_winkelstraat.active.each do |variant|
        next unless variant.product.available?
        size = size_option_values.detect {|ov| variant.option_values.ids.include?(ov[:option_value_id])}
        csv << [
            variant.sku, #sku
            size.present? ? size.fetch(:presentation) : nil, #maat
            variant.stock_for_stock_location(stock_location) #stock
        ]
      end
    end
  end

  def in_taxon?(taxon)
    return false if taxon.blank?
    product.taxon_ids.include?(taxon.id)
  end

  # pass :all for all stock locations
  def stock_for_stock_location(stock_location)
    begin
      if stock_location == :all
        total_on_hand
      else
        stock_items.where(stock_location: stock_location).first.try(:count_on_hand) || 0
      end
    rescue
      0
    end
  end

  def kassa_reference
    nil
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_winkelstraat-3.1.0.21 app/models/spree/variant_decorator.rb
spree_winkelstraat-3.1.0.20 app/models/spree/variant_decorator.rb