#
# リビジョン管理
# コントローラ用
# Comm::Const::Errorをincludeしてください
# 
#
#
#  他のコントローラからも使うので、superior_order.rbモデルに移して現在使っていない
#
#
#
module CommLogistics::Modules::Controller::Revision
  def get_all_rels(id, mcls=@mcls)
    ar = mcls.find_by_id(id)
    #受注/発注タイプコードによって、必要な関連処理を割り出す。
    #order_type_code = ar.order_type_code.to_i
    #受注/発注のtotal_quantityを求める。
    total_quantity = ar.total_quantity.to_i
    #受注/発注
    table_name = mcls.name.tableize
    #関連処理
#    table_array = []
#    #受注に必要な関連処理
#    if table_name=="accept_orders"
#      table_array.push("sales");
#      if ar.accept_order_trigger_code.to_i==Comm::Const::MasterCode::MCODE_ACCEPT_ORDER_TRIGGER_SALES_AND_STOCKS
#        table_array.push("shippings")
#        table_array.push("restorations")
#        table_array.push("samples")
#      end
#      if ar.supplier_id.to_i!=App::Const::SystemCode::OWN_SUPPLIER_ID 
#        table_array.push("purchases")
#      end
#    #発注に必要な関連処理
#    elsif table_name=="orders"
#      table_array.push("purchases")
#    end
#    rels = Rel.find_by_sql(["SELECT tr.table_name, r.record_id, #{total_quantity} AS total_quantity, tr.trans_type_code, tr.sign_type_code FROM master_app_production.table_rels AS tr
#                             LEFT JOIN rels AS r ON tr.table_name=r.table_name AND r.parent_table_name= ? AND r.parent_record_id= ?  
#                             WHERE tr.order_type_code = ? AND (tr.table_name IN (?) OR (tr.table_name='orders' AND r.record_id > 0) )
#                             ORDER BY tr.disp_sort", table_name, id, order_type_code, table_array])
#    rels = Rel.find_by_sql(["SELECT tr.table_name, r.record_id, #{total_quantity} AS total_quantity, tr.trans_type_code, tr.sign_type_code FROM master_app_production.table_rels AS tr
#                             LEFT JOIN rels AS r ON tr.table_name=r.table_name AND r.parent_table_name= ? AND r.parent_record_id= ? 
#                             WHERE tr.order_type_code = ? AND r.record_id > 0
#                             ORDER BY tr.disp_sort", table_name, id, order_type_code])
    rels = Rel.find_by_sql(["SELECT r.table_name, r.record_id, #{total_quantity} AS total_quantity
                             FROM rels AS r 
                             WHERE r.parent_table_name= ? AND r.parent_record_id= ? 
                             ORDER BY r.created_at,r.table_name ", table_name, id])
                             
#    if rels.length > 0
#      additional_rels = []
#      rels.each do |rel|
#        arels = Rel.find_by_sql(["SELECT r.table_name, r.record_id, #{total_quantity} AS total_quantity
#                                            FROM rels AS r
#                                            WHERE r.parent_table_name=? AND r.parent_record_id = ?
#                                            ORDER BY r.created_at,r.table_name ", rel.table_name, rel.record_id.to_i])
#        if arels.length > 0
#          additional_rels.concat(arels)
#        end
#      end
#      rels.concat(additional_rels)
#    end
    return rels
  end
end