class Disposal < CommLogistics::Base::Model::SuperiorStock
  include Comm::Module::Model::Logging
  include CommLogistics::Modules::MngProductsPrice
  #has_many :disposal_details, :dependent => :destroy
  has_many :child_details,
           :class_name => 'DisposalDetail',
           :dependent => :destroy,
           :order => 'seq_number'
  
protected
  def create_exec_do(main, details)
    # check params
    unless own_warehouse?(main)
      raise LOEMJ0001 + LOEMD0008
    end
    self.attributes = main
    unless save
      raise EMJ0001 + EMD0001
    end
    #detailsはstock_infoより先に更新が必要なので注意
    update_details(self.id, details)
    update_stock_info(main, details)
    true
  end
  
  def destroy_exec_do
    details = child_details.ext_hashfy
    update_stock_info(self, details, false)
    true
  end
  
  def update_stock_info(main, details, is_create=true)
    if status_complete?(main)
      # sub own stocks
      s_details = get_details_params_for_stock(main, details)
      calc_type = is_create ? CALC_SUB : CALC_ADD
      update_own_stock(main, s_details, calc_type)
      update_products_price(main, details, calc_type, false)
    end
  end
end
