class StocksController < CommLogistics::Base::Controller::LogisticsController
  def initialize
    @mcls = Stock
  end
  
  def find_all(opt)
    return set_price(super(opt))
  end
  
  def set_price(hrs)
    hrs.each do |hr|
      # 単価
      if hr['existing_quantity'] <= 0 || hr['supplier_id'] != OWN_SUPPLIER_ID
        hr['price'] = 0
      else
        hr['price'] = get_price(hr['product_id'])
      end
      # 総額
      hr['total_price'] = hr['price'] * hr['existing_quantity']
    end
  end
  
  def get_price(product_id)
    result = 0
    # 最終仕入値
    opt = {:order => 'purchases.target_date DESC, purchases.id DESC',
           :conditions => ['purchase_details.product_id = ? AND purchases.sign_type_code = ? AND purchase_details.inspect_status_code = ?',
                            product_id, MCODE_SIGN_TYPE_BLACK, MCODE_STATUS2_COMP ] }
    ar = PurchaseDetail.with_parent.first(opt)
    if ar
      result = ar.price
    end
    return result
  end
end
