class Cart < ActiveRecord::Base

	has_many :item_in_carts
	belongs_to :user
	belongs_to :delivery_destination

  def delivery_cost
    place = Place.by_code_or_default(delivery_destination.place)
    if delivery_destination.instant_delivery
      place.instant_delivery_price
    else
      place.delivery_price
    end
  end

  def total
    sum = item_in_carts.inject (0) { |sum, iic| sum + (iic.price * iic.count) }
    sum += delivery_cost
  end

  def confirmed_at
    delivery_destination.updated_at.in_time_zone('Europe/Sarajevo')
  end

end
