class SaleItem < ApplicationRecord
  self.primary_key = "sale_item_id"

  #primary key - need to be unique generated for multiple shops
  before_create :generate_custom_id

  belongs_to :sale

  #compute items - discount, tax, price_change
  def compute_item
  end


  def self.get_order_items_details(sale_id)
    order_details = SaleItem.select("sales.total_tax as tax_amount, sales.grand_total as grand_total_amount , sales.total_discount as discount_amount,sales.receipt_date as receipt_date,
                      sales.cashier_name,sales.receipt_no,sale_items.product_name as item_name,sale_items.qty,sale_items.price,sale_items.unit_price as total_price")
                   .joins("left join sales on sales.sale_id = sale_items.sale_id")
                   .where("sale_items.sale_id=?",sale_id)

    # sale_orders = SaleOrder.where("sale_id=?",sale_id)
    # if sale_orders
    #    sale_orders.each do |sale_order|
    #     order_details = SaleItem.select("sales.total_discount as discount_amount,DATE_FORMAT(sales.receipt_date,'%Y-%m-%d %h:%m') as receipt_date,sales.cashier_name,sales.receipt_no,sale_items.product_name as item_name,sale_items.qty,sale_items.price,sale_items.unit_price as total_price")
    #        .joins("left join sales on sales.id = sale_items.sale_id")
    #        .where("sale_items.sale_id=?",sale_order.sale_id)
    #     return order_details
    #   end
    # else
    #   return false
    # end
  end

  # Get Prices for each accounts (eg: food, beverage)
  def self.calculate_price_by_accounts(sale_items)
    price_accounts = []
    Account.all.each do |a|
      account_price = {:name => a.title, :price => 0}

      # Check for actual sale items
      sale_items.each do |si|
        if si.account_id == a.id
          account_price[:price] = account_price[:price] + si.price
        end
      end
      price_accounts.push(account_price)
    end

    return price_accounts
  end

  # Get discount Prices for each accounts (eg: food, beverage)
  def self.get_discount_price_by_accounts(sale_items)
    discount_accounts = []
    Account.all.each do |a|
      discount_account = {:name => a.title, :price => 0}

      # Check for actual sale items
      sale_items.where("is_taxable = false AND remark = 'Discount'").find_each do |si|
        if si.account_id == a.id
          discount_account[:price] = (discount_account[:price] + si.price) * -1
        end
      end
      discount_accounts.push(discount_account)
    end

    return discount_accounts
  end

  # Calculate rebate_by_account
  def self.calculate_rebate_by_account(sale_items)
    rebateacc =  Account.where("rebate=?",true)
    puts "Account that can rebate"
    rebateacc.each do |i|
      puts i.title
    end
    prices=0
    sale_items.each do |si|
      price = self.get_rebate_price(si.sale_item_id,rebateacc)

      prices = prices + price
    end
    return prices
  end

  # get food price or beverage price for item
  def self.get_rebate_price(sale_item_id,rebateacc)
    price=0

    item=SaleItem.select("sale_items.price , menu_items.account_id")
                          .joins("left join menu_items on menu_items.item_code = sale_items.product_code")
                          .where("sale_items.sale_item_id=?", sale_item_id.to_s)

    rebateacc.each do |i|
      if item[0].account_id == i.id
        price = item[0].price
      end
    end

    return price
  end

  # def self.get_overall_discount(sale_id)
  #   price = 0.0
  #   item=SaleItem.where("product_code=?", sale_id)
  #
  #   item.each do|i|
  #     price += i.price
  #   end
  #
  #   return price
  # end

  private
  def generate_custom_id
    self.sale_item_id = SeedGenerator.generate_id(self.class.name, "SLI")

  end
end
