class ProductCommission < ApplicationRecord
  belongs_to :commission, foreign_key: 'commission_id'
  belongs_to :commissioner, foreign_key: 'commissioner_id'
  belongs_to :menu_item, foreign_key: 'product_id'
  belongs_to :sale_item, foreign_key: 'sale_item_id'
  belongs_to :sale, foreign_key: 'sale_id'

  def self.create_product_commission(newSaleItem, oldSaleItem)
    old_product_commission = ProductCommission.find_by_sale_item_id(oldSaleItem.id)
    return if old_product_commission.nil?
    menu_item = MenuItem.find_by_item_code(newSaleItem.product_code)
    return if menu_item.nil?
    product_commission = ProductCommission.new
    commission = Commission.where('product_code = ? AND is_active = ?', menu_item.id, true).take
    product_commission.product_code = menu_item.id
    product_commission.product_type = 'menu_item' # use for dummy data ToDo::need to change product type
    unless commission.nil?
      product_commission.commission_id = commission.id
      if commission.commission_type == 'Percentage'
        product_commission.price = newSaleItem.unit_price * (commission.amount / 100.0)
        product_commission.amount = product_commission.price * newSaleItem.qty
      elsif commission.commission_type == 'Net Amount'
        product_commission.price = commission.amount
        product_commission.amount = product_commission.price * newSaleItem.qty
      end
    end
    unless old_product_commission.nil?
      product_commission.commissioner_id = old_product_commission.commissioner_id
    end
    product_commission.qty = newSaleItem.qty
    product_commission.sale_id = newSaleItem.sale_id
    product_commission.sale_item_id = newSaleItem.sale_item_id
    product_commission.save
  end

  def self.edit_product_commission(saleItemObj)
    menu_item = MenuItem.find_by_item_code(saleItemObj.product_code)
    commission = Commission.where('product_code = ? AND is_active = ?', menu_item.id, true).take
    product_commission = ProductCommission.where('sale_item_id = ?', saleItemObj.id).take

    if !product_commission.nil?
      return if commission.nil?
      product_commission.qty = saleItemObj.qty
      if commission.commission_type == 'Percentage'
        product_commission.price = saleItemObj.unit_price * (commission.amount / 100.0)
        product_commission.amount = product_commission.price * saleItemObj.qty
      elsif commission.commission_type == 'Net Amount'
        product_commission.price = commission.amount
        product_commission.amount = product_commission.price * saleItemObj.qty
      end
      product_commission.save
    end
    puts 'Edit Product Commission Success'
  end

  def self.remove_product_commission(sale_item_id)
    transaction = ProductCommission.find_by_sale_item_id(sale_item_id)
    return if transaction.nil?
    transaction.destroy
    puts 'Remove Product Commission Success'
  end

  def self.get_transaction(from, to, commissioner)
    transaction = ProductCommission.select("product_commissions.*,menu_items.name as porduct_name")
                 .joins("join menu_items on menu_items.item_code = product_commissions.product_code")
    if !from.nil? && !to.nil?
      transaction = transaction.where('product_commissions.updated_at between ? and ?', from, to)
    end
    if commissioner != 0
      transaction = transaction.where(commissioner_id: commissioner)
    end
    transaction
  end
end
