class Product < ApplicationRecord
  validates_presence_of :name

  # Product Image Uploader
  mount_uploader :image_path, ProductImageUploader

  def self.search_by_product_code(item_code)
    Product.joins("JOIN accounts ON accounts.title = 'Product'")
    .where(item_code: item_code)
    .pluck("accounts.id AS account_id", :item_code, :name, :alt_name, :unit_price, :taxable)
    .map { |account_id, item_code, item_name, item_alt_name, price, taxable|
      {
        type: 'Product',
        account_id: account_id,
        item_code: item_code,
        item_instance_code: item_code,
        name: "#{item_name}",
        alt_name: "#{item_name}",
        price: price,
        promotion_price: 0,
        is_on_promotion: 0,
        is_available: 0,
        taxable: taxable
      }
    }
  end
end
