BIT/IAM-1814 - spObtenerInscImportacionMasCals - Se agrega condición para que coincida la OfertaEduc de @inscripciones con la de insReal
----------------------------INICIO PROCEDIMIENTO ------------------------
IF OBJECT_ID('spObtenerInscImportacionMasCals', 'P') IS NOT NULL
DROP PROC spObtenerInscImportacionMasCals
GO
CREATE PROCEDURE [dbo].[spObtenerInscImportacionMasCals]
@inscripciones LISTAINSACAD READONLY,
@sCodigoTemporales VARCHAR(32)
/* --------------------------------------------------------------------------------------------------------------
FECHA | AUTOR | DESCRIPCION
30/jun./2017 | Luis Bernal | BIT/IAM-1814 - Se agrega condición para que coincida la OfertaEduc de @inscripciones con la de insReal
--------------------------------------------------------------------------------------------------------------*/
AS
BEGIN
SET NOCOUNT ON;
IF @sCodigoTemporales IS NULL
SELECT alsparam.sMatricula
,al.idAlumno
,alsparam.idPlantel
,alsparam.iFolioPeriodo
,alsparam.idOfEduc
,alsparam.idPlanEstudios
,alsparam.idAsignatura
,alsparam.idGrupo
,NULL AS iFolioInscripcionTmp
,ins.iFolio AS iFolioInscripcionReal
,NULL AS iFolioTrayectoriaTmp
,acad.iFolio AS iFolioTrayectoriaReal
,CAST(validacion.bExistenCalifs AS BIT) bExistenCalifs
FROM @inscripciones alsparam
LEFT JOIN Cat_Alumnos al
ON al.sMatricula = alsparam.sMatricula
AND EXISTS(SELECT insPlantel.idAlumno
FROM Ctrl_Inscripciones insPlantel
WHERE insPlantel.idAlumno = al.idAlumno
AND insPlantel.idPlantel = alsparam.idPlantel)
LEFT JOIN ctrl_inscripciones ins
ON ins.idPlantel = alsparam.idPlantel
AND ins.idAlumno = al.idAlumno
AND ins.iFolioPeriodo = alsparam.iFolioPeriodo
LEFT JOIN Ctrl_TrayectoriaAcademica acad
ON acad.iFolioInscripcion = ins.iFolio
AND acad.idAsignatura = alsparam.idAsignatura
AND acad.idGrupo = alsparam.idGrupo
OUTER APPLY (SELECT CASE
WHEN EXISTS (SELECT iFolioTrayectoria
FROM Ctrl_TrayectoriaAcademicaDet acadet
WHERE acad.iFolio IS NOT NULL
AND acad.iFolio = acadet.iFolioTrayectoria
AND ( acadet.bHistorica IS NULL
OR acadet.bHistorica = 0 )) THEN 1
ELSE 0
END AS bExistenCalifs) validacion
WHERE ins.iFolio IS NULL
OR ins.idPlantel = alsparam.idPlantel
ELSE
BEGIN
SELECT alsparam.sMatricula
,COALESCE(insReal.idAlumno, insTmp.idAlumno) AS idAlumno
,alsparam.idPlantel
,alsparam.iFolioPeriodo
,alsparam.idOfEduc
,alsparam.idPlanEstudios
,alsparam.idAsignatura
,alsparam.idGrupo
,insTmp.iFolio AS iFolioInscripcionTmp
,insReal.iFolio AS iFolioInscripcionReal
,acad.iFolio AS iFolioTrayectoriaTmp
,insReal.iFolioTrayReal AS iFolioTrayectoriaReal
,CAST(isnull(validacion.bExistenCalifs, 0) AS BIT) bExistenCalifs
FROM @inscripciones alsparam
OUTER APPLY (SELECT al.idAlumno
,ins.iFolio
FROM Cat_Alumnos al
INNER JOIN Ctrl_InscripcionesTmpCalif ins
ON ins.idPlantel = alsparam.idPlantel
AND ins.idAlumno = al.idAlumno
AND ins.iFolioPeriodo = alsparam.iFolioPeriodo
AND al.sMatricula = alsparam.sMatricula
AND ins.scodigo = @sCodigoTemporales) insTmp
OUTER APPLY (SELECT al2.idAlumno
,insR.iFolio
,acadR.iFolio AS iFolioTrayReal
FROM Cat_Alumnos al2
INNER JOIN Ctrl_Inscripciones insR
ON insR.idPlantel = alsparam.idPlantel
AND insR.idAlumno = al2.idAlumno
AND insR.iFolioPeriodo = alsparam.iFolioPeriodo
AND insR.idOfEduc = alsparam.idOfEduc -- >> BIT/IAM-1814 << --
AND al2.sMatricula = alsparam.sMatricula
LEFT JOIN Ctrl_TrayectoriaAcademica acadR
ON acadR.iFolioInscripcion = insR.iFolio
AND acadR.idAsignatura = alsparam.idAsignatura
AND acadR.idGrupo = alsparam.idGrupo) insReal
LEFT JOIN Ctrl_TrayectoriaAcademicaTmpCalif acad
ON ( ( insReal.iFolio IS NOT NULL
AND acad.iFolioInscripcionReal = insReal.iFolio )
OR ( insTmp.iFolio IS NOT NULL
AND acad.iFolioInscripcion = insTmp.iFolio ) )
AND acad.idAsignatura = alsparam.idAsignatura
AND acad.idGrupo = alsparam.idGrupo
AND acad.scodigo = @sCodigoTemporales
OUTER APPLY (SELECT CASE
WHEN EXISTS (SELECT iFolioTrayectoria
FROM Ctrl_TrayectoriaAcademicaDet acadet
WHERE insReal.iFolioTrayReal IS NOT NULL
AND insReal.iFolioTrayReal = acadet.iFolioTrayectoria
AND ( acadet.bHistorica IS NULL
OR acadet.bHistorica = 0 )) THEN 1
ELSE 0
END AS bExistenCalifs) validacion
WHERE NOT EXISTS (SELECT ins.iFolio
FROM ctrl_inscripciones ins
WHERE ins.idAlumno = insTmp.idAlumno
AND ins.idplantel != alsparam.idPlantel)
END
END