#
#= stocks モデル
# Authors:: Sumiyo Yamamoto
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
# date        name                   note
# 2010.11.5   Sumiyo Yamamoto        新規登録
#-------------------------------------------------------------------------------
#++
class Stock < CommLogistics::Base::Model::LogisticsModel
  include Comm::Const::MasterCode
  include CommLogistics::Const::Code
  include CommLogistics::Modules::ErrorStore
  
  named_scope :has_quantity, :conditions => 'existing_quantity > 0'
  #曖昧&&仕様箇所無さそうだったのでいったんCO
  #在庫主(supplier)か倉庫(warehouse)かを明確にしていくべし
  #named_scope :own, :conditions => ["`supplier_id` = ?", OWN_SUPPLIER_ID]
  
  named_scope :customer_is, lambda{ |id| {
    :conditions => ["`customer_id` = ?", id]
  }}
  named_scope :warehouse_is, lambda{ |id| {
    :conditions => ["`warehouse_id` = ?", id]
  }}
  named_scope :supplier_is, lambda{ |id| {
    :conditions => ["`supplier_id` = ?", id]
  }}
  named_scope :product_set_is, lambda{ |id| {
    :conditions => ["`product_set_id` = ?", id]
  }}
  named_scope :product_is, lambda{ |id| {
    :conditions => ["`product_id` = ?", id]
  }}
  named_scope :lot_number_is, lambda{ |val| {
    :conditions => ["`lot_number` = ?", val]
  }}
  named_scope :ubd_is, lambda{ |val| {
    :conditions => ["`ubd` = ?", val]
  }}
  
  named_scope :own_search, lambda {|params| {
    :conditions => [
      "`warehouse_id` = ? AND `supplier_id` = ? AND `product_id` = ? AND `lot_number` = ? AND `serial_number` = ? AND  `ubd` = ? AND `location_number` = ?",
      params['warehouse_id'],
      params['supplier_id'],
      params['product_id'],
      params['lot_number'],
      params['serial_number'],
      params['ubd'],
      params['location_number']
    ]
  }}
  class << columns_hash['ubd']
    def type
      :string
    end
  end
  
  def self.out_search(params)
    if params['customer_id'] == ""
      cond = "`customer_id` is ? "
      params['customer_id'] = nil
    else
      cond = "`customer_id` = ? " 
    end
    cond += "AND `warehouse_id` = ? AND `supplier_id` = ? AND `product_id` = ? AND `lot_number` = ? AND `serial_number` = ? AND `ubd` = ?"
    results = self.find(:all,
                        :conditions => [cond,
                                        params['customer_id'],
                                        params['warehouse_id'],
                                        params['supplier_id'],
                                        params['product_id'],
                                        params['lot_number'],
                                        params['serial_number'],
                                        params['ubd']])
    return results
  end
  
  def self.check_quantities
    results = self.find_by_sql("SELECT * FROM stocks 
                                WHERE existing_quantity < 0 
                                OR available_quantity < 0
                                OR short_shipped_quantity < 0
                                OR existing_quantity < short_shipped_quantity 
                                OR existing_quantity < available_quantity")
    if results.length > 0
      msg = ["在庫数量が異常になるので本処理は完了できません。<br>下記を確認して操作をやり直してください。"]
      results.each do |rec|
        rmsg = []
        rmsg << "o 製品ID:"+rec.product_id.to_s
        pd = Product.find(rec.product_id)
        if pd and pd.disp_name
          rmsg << "【"+pd.disp_name+"】"
        end
        rmsg << "ロット:"+rec.lot_number.to_s
        unless rec.serial_number.blank?
          rmsg << "シリアル:"+rec.serial_number.to_s
        end
        rmsg << "有効期限:"+rec.ubd.to_s
        rmsg << "実数量:"+rec.existing_quantity.to_s
        rmsg << "有効数量:"+rec.available_quantity.to_s
        rmsg << "短期分数量:"+rec.short_shipped_quantity.to_s
        rmsg << " ( "
        rmsg << "得意先ID:"+rec.customer_id.to_s
        rmsg << "納品先ID:"+rec.warehouse_id.to_s
        rmsg << "仕入先ID:"+rec.supplier_id.to_s
        rmsg << " ) "
        msg << rmsg.join(" ")
      end
      return msg.join("<br>\n")
    end
    return nil
  end
  
  def add_quantity(quantity, short_flg)
    self.existing_quantity  += quantity
    self.available_quantity += quantity
    if short_flg && !Warehouse.is_own?(self.warehouse_id)
      self.short_shipped_quantity += quantity
    end
    true
  end
  
  def add_quantity_avail(quantity)
    self.available_quantity += quantity
    true
  end
    
  def get_find_flag
    return MFIND_A
  end
end