class AcceptOrder < CommLogistics::Base::Model::SuperiorOrder
  include Comm::Module::Model::Logging
  #has_many :accept_order_details, :dependent => :destroy
  has_many :child_details,
           :class_name => 'AcceptOrderDetail',
           :dependent => :destroy,
           :order => 'seq_number'
  has_many :orders
  has_many :purchases
  has_many :sales
  has_many :samples
  has_many :shippings
  has_many :restorations
    #関連進捗率の100％となる本数を計算
  def self.set_stock_and_rels_total_quantity(table_params, details)
    super
    #ars = TableRel.find(:all, :conditions => ["invalid_flag_code=#{MCODE_FLAG_OFF} AND order_type_code = #{table_params['accept_order_type_code']} #{table_params['sign_type_code'].blank? ? '' : ' AND sign_type_code='+ table_params['sign_type_code'].to_s}"])
    ars = TableRel.target_rels(table_params, 'accept_orders')
    rels_total_quantity=0
    ars.each do |ar|
      case ar.table_name
      when 'sales'
        rels_total_quantity = rels_total_quantity + table_params['total_quantity'].to_i
      when 'purchases'
        rels_total_quantity = rels_total_quantity + table_params['stock_total_quantity'].to_i unless table_params['supplier_id'].to_i==App::Const::SystemCode::OWN_SUPPLIER_ID
      when 'orders'
        #なにもしない。
      else
        #shippings/restorations/samples
        rels_total_quantity = rels_total_quantity + table_params['stock_total_quantity'].to_i if table_params['accept_order_trigger_code'].to_i==MCODE_ACCEPT_ORDER_TRIGGER_SALES_AND_STOCKS
      end
    end
    table_params['rels_total_quantity'] = rels_total_quantity
  end
end
