Sha256: 9e4506da098382a49fafbf72f6fc28f6d936056062cf594edcd92e00a0a5da49

Contents?: true

Size: 944 Bytes

Versions: 1

Compression:

Stored size: 944 Bytes

Contents

class Plugins::Ecommerce::UserProductService
  def initialize(site, user, product, variation_id = nil)
    @site = site
    @user = user
    @product = product
    @variation_id = variation_id
  end
  
  attr_reader :site, :user, :product, :variation_id
  
  def available_qty
    available_qty = Plugins::Ecommerce::ProductService.new(
      site, product, variation_id).available_qty
    available_qty -= qty_in_carts
  end
  
  private
  
  def qty_in_carts
    carts = site.carts.where.not(user_id: user.id).active_cart.joins(:product_items)
    if variation_id.present?
      carts.where("#{Plugins::Ecommerce::ProductItemDecorator.table_name}" =>
        {variation_id: variation_id}).sum("#{Plugins::Ecommerce::ProductItem.table_name}.qty")
    else
      carts.where("#{Plugins::Ecommerce::ProductItemDecorator.table_name}" =>
        {product_id: product.id}).sum("#{Plugins::Ecommerce::ProductItem.table_name}.qty")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
camaleon_ecommerce-1.2 app/services/plugins/ecommerce/user_product_service.rb