class Adjustment < CommLogistics::Base::Model::SuperiorStock
  include Comm::Module::Model::Logging
  include CommLogistics::Modules::MngProductsPrice
  #has_many :adjustment_details, :dependent => :destroy
  has_many :child_details,
           :class_name => 'AdjustmentDetail',
           :dependent => :destroy,
           :order => 'seq_number'
protected
  def create_exec_do(main, details)
    logger.debug(["Adjustment create_exec_do", "details:#{details.inspect}"].join("\n\t"))
    # edit adjustments
    self.attributes = main
    unless save
      raise EMJ0001 + EMD0001
    end
    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)
      calc_type = is_create ? CALC_ADD : CALC_SUB
      if own_warehouse?(main)
        #supplierはそのまま
        overwrite_params = {'customer_id'=>nil}
        s_details = get_details_params_for_stock(main, details, overwrite_params)
        update_own_stock(main, s_details, calc_type)
      else
        s_details = get_details_params_for_stock(main, details)
        update_stock(main, s_details, calc_type)
      end
      update_products_price(main, details, calc_type, false)
    end
  end
end
