· 9 years ago · Jan 11, 2017, 04:34 PM
1USE [KARIBE]
2GO
3/****** Object: StoredProcedure [dbo].[MinutaBuscarDocumentQueue] Script Date: 1/11/2017 12:31:46 PM ******/
4SET ANSI_NULLS ON
5GO
6SET QUOTED_IDENTIFIER ON
7GO
8--------------------------------------------------
9-- REGISTRO DE LA PROPIEDAD DE PR
10-- APLICACION: RIDPR
11-- PROPOSITO: Busca minutas por varios filtros
12-- POR: INVID, LLC
13-- FECHA: 6/24/2014
14--------------------------------------------------
15
16ALTER PROCEDURE [dbo].[MinutaBuscarDocumentQueue]
17 --declare
18 @UsuarioId int = 6,
19
20 @Presentacion varchar(3) = '216',
21 @EstatusId int,
22 @Demarcacion varchar(10),
23 @Finca varchar(50),
24 @Asiento varchar(30) = '@',
25
26 @PageNumber int = 1,
27 @PageSize int = 100000
28AS
29
30BEGIN
31 set @Presentacion = isnull(nullif(@Presentacion,''), '0')
32 set @demarcacion = isnull(nullif(@demarcacion,''), '0')
33 set @EstatusId = isnull(nullif(@EstatusId,''), '0')
34 set @finca = isnull(nullif(@finca,''), '0')
35
36 declare @rol int = (select top 1 rolid from sistema.UsuarioRol where UsuarioId = @UsuarioId)
37 DECLARE @offset int = (@PageNumber - 1) * @PageSize
38
39 --------------------------------------------------------------------------------------------------------------------
40 --presentadas
41 select a.UltimaBitacoraId as MaxBitacoraId,
42 b.MinutaId,
43 a.Id as AsientoId
44 , convert(int, null) as UsuarioAsignadoId
45 , convert(int, null) as CreadoPor
46 , convert(int, null) as MinutaCount
47 into #bitacora
48 from Asiento a
49 join Bitacora b
50 on b.Id = a.UltimaBitacoraId
51 join Minuta m
52 on b.MinutaId = m.Id
53 where b.UsuarioAsignadoId = @UsuarioId
54
55 union
56 --------------------------------------------------------------------------------------------------------------------
57 --no presentadas
58 select BitacoraId,MinutaId,null,null,null,null
59 from (
60 select m.Id as MinutaId,
61 b.Id as BitacoraId,
62 b.AsientoId,
63 b.EstatusId,
64 row_number() over (partition by m.id order by b.id desc) as rownum
65 from Minuta m
66 cross apply(
67 select top 1 bCInner.Id
68 from Bitacora bCInner
69 join Minuta mC
70 on mc.Id = bCInner.MinutaId
71 where bCInner.MinutaId = m.Id
72 and bCInner.UsuarioAsignadoId = @UsuarioId
73 order by bCInner.id desc
74 )bC
75 join Bitacora b
76 on b.Id = bc.Id
77 where b.UsuarioAsignadoId = @UsuarioId
78 and b.EstatusId = 1
79 )noPres
80 where rownum = 1
81
82 ---- todas los max bitacora id de los asientos ya presentados ...
83 --select max(b.Id) as MaxBitacoraId, b.MinutaId, b.AsientoId
84 --, convert(int, null) as UsuarioAsignadoId
85 --, convert(int, null) as CreadoPor
86 --, convert(int, null) as MinutaCount
87 --from bitacora b with (nolock)
88 --where asientoid is not null
89 --group by MinutaId, AsientoId
90
91 --union
92 ---- todas las que aun no han sido presentadas - mismo query del robot, pero con la bitacoraid que no se usa allá.
93 --select min(BitacoraId), MinutaId, null, null, null, null
94 --from
95 --(
96 -- select
97 -- m.Id as MinutaId
98 -- , b.Id as BitacoraId
99 -- , b.AsientoId
100 -- , EstatusId
101 -- , row_number() over (partition by m.id order by b.id desc) as rownum
102 -- from Minuta m
103 -- join Bitacora b
104 -- on b.MinutaId = m.Id
105 --) allahu_akbar
106 --where EstatusId = 1
107 --and rownum = 1
108 --group by MinutaId
109
110 --create index to speedup the process
111 alter table #bitacora alter column maxbitacoraid integer not null
112 create clustered index ix_maxbitacoraid on #bitacora (maxbitacoraid);
113
114 update b2
115 set b2.UsuarioAsignadoId = b.UsuarioAsignadoId
116 , b2.CreadoPor = m.CreadoPor
117 from #bitacora b2
118 join bitacora b with (nolock)
119 on b.id = b2.maxbitacoraid
120 join minuta m with (nolock)
121 on m.id = b.MinutaId
122
123
124 delete #bitacora where MaxBitacoraId not in (
125 select MaxBitacoraId
126 from #bitacora b
127 where 1 = case
128 when b.UsuarioAsignadoId = @UsuarioId then 1
129 when b.CreadoPor = @UsuarioId and @rol = 3 then 1
130 else 0 end
131
132 )
133 --------------------------------------------------------------------------------------------------------------------
134
135
136 if @Asiento <> '@'
137 begin
138
139 select count(*) over() TotalRows
140 ,@PageNumber PageNumber
141 ,ceiling((count(*) over()) / cast(@PageSize as float)) TotalPages,
142 null as DocumentoYaPresentado,
143
144 EstatusId,Estatus,UsuarioAsignadoId,AsientoId,Asiento,Id,MinutaId,DocumentoEnAtraso,DocumentoLey216,OficialACargo,DatosAdicionalesNotario,
145 NombreDocumentoId,NumeroRua,NombreNotario,AgenciaId,
146
147 convert(bit, case when UsuarioAsignadoId = @UsuarioId then 1 else 0 end) as EsMio,
148 convert(bit, case when @rol in (1,2,3,4,5,6,10,12) then 1 else 0 end) as PuedoVer,
149
150 NumeroAffidavitCasoInstancia,
151 LugarOtorgamientoEscritura, CasoJudicial, AccionJudicial, DemandadoJudicial, DemandanteJudicial,
152 ExParteJudicial, FechaEscritura, ArancelesCalculados, Exento,
153
154 TipoDocumentoId,
155 TipoOrigenDocumentoId,
156 TipoDocumento,
157
158 SegundoNombreNotario,
159 PrimerApellidoNotario,
160 SegundoApellidoNotario,
161 EmailNotario,
162 TelefonoNotario,
163 DireccionPostalNotario,
164
165 TipoExpedicion, Razon,
166 Modelo, Texto, GUIDArchivoPresentado, GUIDArchivoMinuta,
167 FechaPresentacion,
168 Fincas,
169 TransaccionList,
170 CreadoPor,
171 CreadoPorRol,
172 Modificado, ModificadoPor,
173
174 case when @rol = 2 then FechaDespacho else FechaAsignado end as FechaAsignado,
175
176 Email1,
177 Email2,
178 CreadoFull,
179 CreadoEmail,
180 Telematica,
181 UsuarioAnterior
182
183 from
184 (
185 select row_number() over(partition by m.id, b.AsientoId order by b.id desc) RowNumber,
186 max(case when b.EstatusId in (3,12) then b.creado else null end) over(partition by b.id) as FechaDespacho,
187 max(b.EstatusId) over(partition by m.id) as MaxEstatusId,
188 isnull(max(isnull(b.AsientoId,0)) over(partition by m.id),0) as MaxAsientoId,
189 isnull(min(isnull(b.AsientoId,0)) over(partition by m.id),0) as MinAsientoId,
190
191 b.EstatusId,
192 case when n.FechaCaducidad >= cast(getdate() as date) and n.FechaCaducidad <= dateadd(day, 5, cast(getdate() as date))
193 and b.EstatusId not in (7,9,11,6) then me.Estatus + ', Pendiente a caducar' else me.Estatus end as Estatus,
194
195 b.UsuarioAsignadoId,
196 b.AsientoId,
197 a.Asiento,
198
199 m.Id,
200 m.Id MinutaId,
201 m.DocumentoEnAtraso,
202 m.DocumentoLey216,
203 m.OficialACargo,
204 m.DatosAdicionalesNotario,
205
206 m.SegundoNombreNotario,
207 m.PrimerApellidoNotario,
208 m.SegundoApellidoNotario,
209 m.EmailNotario,
210 m.TelefonoNotario,
211 m.DireccionPostalNotario,
212 m.Email1,
213 m.Email2,
214 td.Id TipoDocumentoId,
215 TipoOrigenDocumentoId,
216 td.Nombre TipoDocumento,
217
218 NombreDocumentoId,
219 m.NumeroRua, NombreNotario, m.AgenciaId,
220
221 NumeroAffidavitCasoInstancia,
222 LugarOtorgamientoEscritura, CasoJudicial, AccionJudicial, DemandadoJudicial, DemandanteJudicial,
223 ExParteJudicial, FechaEscritura, ArancelesCalculados, Exento,
224
225 TipoExpedicion, m.Razon,
226 Modelo, Texto, GUIDArchivoPresentado, GUIDArchivoMinuta,
227 FechaPresentacion,
228
229 m.Creado,
230 --isnull(uc.Nombre, '') + isnull(' ' + uc.PrimerApellido, '') as CreadoFull,
231 usuarioAnterior.Nombre + ' ' + usuarioAnterior.SegundoNombre + ' ' + usuarioAnterior.PrimerApellido + ' ' + usuarioAnterior.SegundoApellido as CreadoFull,
232 isnull(stuff((
233 select distinct '|'
234 + isnull(nullif(p.NumeroPropiedad,''),'?'), ' - ' + p.CodigoDemarcacion + ' ' + d.Nombre
235 from dbo.Propiedad p with (nolock)
236 join ux.MinutaPropiedad MP on MP.PropiedadId = P.Id
237 join sistema.Demarcacion d on d.Codigo = p.CodigoDemarcacion
238 where MP.MinutaId = m.Id
239 for xml path ('')
240 ), 1, 1, ''), '') as Fincas,
241 STUFF
242 ((
243 select ', ' + tt.Descripcion
244 from Transaccion ti with (nolock)
245 left join sistema.TipoTransaccion tt with (nolock)
246 on tt.Id = ti.TipoTransaccionId
247
248 where ti.MinutaId = m.Id
249 group by ti.Creado,tt.Descripcion
250 order by ti.Creado
251 for xml path ('')
252 ), 1, 1, '') as TransaccionList,
253 m.CreadoPor,
254 ur.RolId CreadoPorRol,
255
256 m.Modificado, m.ModificadoPor,
257
258 b.Creado AS FechaAsignado,
259 uc.Email as CreadoEmail,
260 m.Telematica,
261 usuarioAnterior.Nombre + ' ' + usuarioAnterior.SegundoNombre + ' ' + usuarioAnterior.PrimerApellido + ' ' + usuarioAnterior.SegundoApellido as UsuarioAnterior
262
263 FROM dbo.Bitacora b with (nolock)
264 join #bitacora b2
265 on b2.MaxBitacoraId = b.Id
266 inner join dbo.Minuta m with (nolock)
267 on b.MinutaId = m.Id
268 and 1 = case when @Presentacion = '0' then 1
269 when m.DocumentoEnAtraso = 1 and @Presentacion = 'PK' then 1
270 when m.DocumentoLey216 = 1 and @Presentacion = '216' then 1
271 when isnull(m.DocumentoEnAtraso,0) = 0 and isnull(m.DocumentoLey216,0) = 0 and @Presentacion = 'K' then 1
272 else 0 end
273
274 inner join [sistema].[MinutaEstatus] me with (nolock)
275 on me.Id = b.EstatusId
276
277 outer apply(
278 select top 1 Id
279 from Bitacora
280 where MinutaId = m.Id
281 and Id <> b2.MaxBitacoraId
282 order by id desc
283 )bo2
284
285 join dbo.Asiento a with (nolock)
286 on b.AsientoId = a.Id
287 and ((a.Asiento like '%' + @Asiento + '%' and @Asiento <> '@' and a.id is not null) or @Asiento = '@')
288
289 left join sistema.Usuario u with (nolock)
290 on b.UsuarioAsignadoId = u.Id
291
292 left join sistema.Usuario uc with (nolock)
293 on b.CreadoPor = uc.Id
294
295 left join Bitacora b3
296 on b3.Id = bo2.Id
297 left join sistema.Usuario usuarioAnterior
298 on usuarioAnterior.Id = case when @rol = 2 and (b.EstatusId = 3 or b.EstatusId = 12) and b.EstatusSecundarioId is not null then b3.CreadoPor
299 else b3.UsuarioAsignadoId end
300
301 left join sistema.UsuarioRol ur with (nolock)
302 on m.CreadoPor = ur.UsuarioId
303
304 left join sistema.TipoDocumentoPresentado td with (nolock)
305 on td.Id = m.NombreDocumentoId
306 left join Notificacion n
307 on n.AsientoId = a.Id
308 where 1 = case when m.DocumentoEnAtraso = 1 and @Presentacion = 'PK' then 1
309 when m.DocumentoLey216 = 1 and @Presentacion = '216' then 1
310 when isnull(m.DocumentoEnAtraso,0) = 0 and isnull(m.DocumentoLey216,0) = 0 and @Presentacion = 'K' then 1
311 when @Asiento <> '@' and a.id is not null then 1
312 when @Presentacion = '0' then 1
313 else 0 end
314
315 group by b.UsuarioAsignadoId,
316 b.AsientoId,
317 a.Asiento,
318 b.EstatusId,
319 b.Id,
320 me.Estatus,
321 m.Id,
322 m.DocumentoEnAtraso,
323 m.DocumentoLey216,
324 td.Id,
325 TipoOrigenDocumentoId,
326 td.Nombre,
327 NombreDocumentoId,
328 m.NumeroRua, NombreNotario, m.AgenciaId,
329 m.OficialACargo,
330 m.DatosAdicionalesNotario,
331
332 m.SegundoNombreNotario,
333 m.PrimerApellidoNotario,
334 m.SegundoApellidoNotario,
335 m.EmailNotario,
336 m.TelefonoNotario,
337 m.DireccionPostalNotario,
338 m.Email1,
339 m.Email2,
340 NumeroAffidavitCasoInstancia,
341 LugarOtorgamientoEscritura, CasoJudicial, AccionJudicial, DemandadoJudicial, DemandanteJudicial,
342 ExParteJudicial, FechaEscritura, ArancelesCalculados, Exento,
343
344 TipoExpedicion, m.Razon,
345 Modelo, Texto, GUIDArchivoPresentado, GUIDArchivoMinuta,
346 FechaPresentacion,
347 m.Creado,
348 uc.Nombre, uc.PrimerApellido,
349 m.CreadoPor, m.Modificado, m.ModificadoPor,
350 ur.RolId,
351 b.Creado,
352 uc.Email,
353 m.Telematica,
354 usuarioAnterior.Nombre,
355 usuarioAnterior.SegundoNombre,
356 usuarioAnterior.PrimerApellido,
357 usuarioAnterior.SegundoApellido,
358 n.FechaCaducidad
359 ) m
360 where m.RowNumber = 1
361 and 1 = case
362 -- minuta no ha sido presentada
363 when MaxAsientoId = 0 and AsientoId is null then 1
364 -- si fue presentada, solo el record que tenga asiento y no la bitacora inicial con asiento null
365 when MaxAsientoId > 0 and AsientoId is not null then 1
366 -- no señor
367 else 0 end
368 and 1 = case when m.UsuarioAsignadoId = @UsuarioId then 1
369 when m.CreadoPor = @UsuarioId and @rol = 3 then 1
370 else 0 end
371 and 1 = case when @EstatusId = 0 then 1
372 when m.EstatusId = @EstatusId then 1
373 else 0 end
374 and exists
375 (
376 select 1
377 from dbo.split(fincas,'|') s
378 where 1 = case when @finca = '0' then 1
379 when charindex('-',item) > 0 and ltrim(rtrim(left(item, charindex('-',item)-1))) like '%' + @finca + '%' then 1
380 else 0 end
381 and 1 = case when @demarcacion = '0' then 1
382 when charindex('-',item) > 0 and left(ltrim(rtrim(right(item, len(item)-charindex('-',item)))),6) like '%' + @demarcacion + '%' then 1
383 else 0 end
384 )
385 -- primero los no presentados, para que los acabe de presentar y luego la fecha asignado que es el orden en que cambiaron de estatus (se presentaron o despacharon, etc)
386 order by case when EstatusId = 1 then 0 else 1 end, FechaAsignado
387
388 OFFSET @offset ROWS FETCH NEXT @PageSize ROWS ONLY
389
390
391 end
392 else
393 begin
394 select count(*) over() TotalRows
395 ,@PageNumber PageNumber
396 ,ceiling((count(*) over()) / cast(@PageSize as float)) TotalPages,
397 null as DocumentoYaPresentado,
398
399 EstatusId,Estatus,UsuarioAsignadoId,AsientoId,Asiento,Id,MinutaId,DocumentoEnAtraso,DocumentoLey216,OficialACargo,DatosAdicionalesNotario,
400 NombreDocumentoId,NumeroRua,NombreNotario,AgenciaId,
401
402 convert(bit, case when UsuarioAsignadoId = @UsuarioId then 1 else 0 end) as EsMio,
403 convert(bit, case when @rol in (1,2,3,4,5,6,10,12) then 1 else 0 end) as PuedoVer,
404
405 NumeroAffidavitCasoInstancia,
406 LugarOtorgamientoEscritura, CasoJudicial, AccionJudicial, DemandadoJudicial, DemandanteJudicial,
407 ExParteJudicial, FechaEscritura, ArancelesCalculados, Exento,
408
409 TipoDocumentoId,
410 TipoOrigenDocumentoId,
411 TipoDocumento,
412
413 SegundoNombreNotario,
414 PrimerApellidoNotario,
415 SegundoApellidoNotario,
416 EmailNotario,
417 TelefonoNotario,
418 DireccionPostalNotario,
419
420 TipoExpedicion, Razon,
421 Modelo, Texto, GUIDArchivoPresentado, GUIDArchivoMinuta,
422 FechaPresentacion,
423 Fincas,
424 TransaccionList,
425 CreadoPor,
426 CreadoPorRol,
427 Modificado, ModificadoPor,
428
429 case when @rol = 2 then FechaDespacho else FechaAsignado end as FechaAsignado,
430
431 Email1,
432 Email2,
433 CreadoFull,
434 CreadoEmail,
435 Telematica,
436 UsuarioAnterior
437
438 from
439 (
440 select row_number() over(partition by m.id, b.AsientoId order by b.id desc) RowNumber,
441 max(case when b.EstatusId in (3,12) then b.creado else null end) over(partition by b.id) as FechaDespacho,
442 max(b.EstatusId) over(partition by m.id) as MaxEstatusId,
443 isnull(max(isnull(b.AsientoId,0)) over(partition by m.id),0) as MaxAsientoId,
444 isnull(min(isnull(b.AsientoId,0)) over(partition by m.id),0) as MinAsientoId,
445
446 b.EstatusId,
447 case when n.FechaCaducidad >= cast(getdate() as date) and n.FechaCaducidad <= dateadd(day, 5, cast(getdate() as date)) then me.Estatus + ', Pendiente a caducar' else me.Estatus end as Estatus,
448
449 b.UsuarioAsignadoId,
450 b.AsientoId,
451 a.Asiento,
452
453 m.Id,
454 m.Id MinutaId,
455 m.DocumentoEnAtraso,
456 m.DocumentoLey216,
457 m.OficialACargo,
458 m.DatosAdicionalesNotario,
459
460 m.SegundoNombreNotario,
461 m.PrimerApellidoNotario,
462 m.SegundoApellidoNotario,
463 m.EmailNotario,
464 m.TelefonoNotario,
465 m.DireccionPostalNotario,
466 m.Email1,
467 m.Email2,
468 td.Id TipoDocumentoId,
469 TipoOrigenDocumentoId,
470 td.Nombre TipoDocumento,
471
472 NombreDocumentoId,
473 m.NumeroRua, NombreNotario, m.AgenciaId,
474
475 NumeroAffidavitCasoInstancia,
476 LugarOtorgamientoEscritura, CasoJudicial, AccionJudicial, DemandadoJudicial, DemandanteJudicial,
477 ExParteJudicial, FechaEscritura, ArancelesCalculados, Exento,
478
479 TipoExpedicion, m.Razon,
480 Modelo, Texto, GUIDArchivoPresentado, GUIDArchivoMinuta,
481 FechaPresentacion,
482
483 m.Creado,
484 --isnull(uc.Nombre, '') + isnull(' ' + uc.PrimerApellido, '') as CreadoFull,
485 usuarioAnterior.Nombre + ' ' + usuarioAnterior.SegundoNombre + ' ' + usuarioAnterior.PrimerApellido + ' ' + usuarioAnterior.SegundoApellido as CreadoFull,
486 isnull(stuff((
487 select distinct '|'
488 + isnull(nullif(p.NumeroPropiedad,''),'?'), ' - ' + p.CodigoDemarcacion + ' ' + d.Nombre
489 from dbo.Propiedad p with (nolock)
490 join ux.MinutaPropiedad MP on MP.PropiedadId = P.Id
491 join sistema.Demarcacion d on d.Codigo = p.CodigoDemarcacion
492 where MP.MinutaId = m.Id
493 for xml path ('')
494 ), 1, 1, ''), '') as Fincas,
495 STUFF
496 ((
497 select ', ' + tt.Descripcion
498 from Transaccion ti with (nolock)
499 left join sistema.TipoTransaccion tt with (nolock)
500 on tt.Id = ti.TipoTransaccionId
501
502 where ti.MinutaId = m.Id
503 group by ti.Creado,tt.Descripcion
504 order by ti.Creado
505 for xml path ('')
506 ), 1, 1, '') as TransaccionList,
507 m.CreadoPor,
508 ur.RolId CreadoPorRol,
509
510 m.Modificado, m.ModificadoPor,
511
512 b.Creado AS FechaAsignado,
513 uc.Email as CreadoEmail,
514 m.Telematica,
515 usuarioAnterior.Nombre + ' ' + usuarioAnterior.SegundoNombre + ' ' + usuarioAnterior.PrimerApellido + ' ' + usuarioAnterior.SegundoApellido as UsuarioAnterior
516
517 FROM dbo.Bitacora b with (nolock)
518 join #bitacora b2
519 on b2.MaxBitacoraId = b.Id
520 inner join dbo.Minuta m with (nolock)
521 on b.MinutaId = m.Id
522 and 1 = case when @Presentacion = '0' then 1
523 when m.DocumentoEnAtraso = 1 and @Presentacion = 'PK' then 1
524 when m.DocumentoLey216 = 1 and @Presentacion = '216' then 1
525 when isnull(m.DocumentoEnAtraso,0) = 0 and isnull(m.DocumentoLey216,0) = 0 and @Presentacion = 'K' then 1
526 else 0 end
527
528 inner join [sistema].[MinutaEstatus] me with (nolock)
529 on me.Id = b.EstatusId
530
531 outer apply(
532 select top 1 Id
533 from Bitacora
534 where MinutaId = m.Id
535 and Id <> b2.MaxBitacoraId
536 order by id desc
537 )bo2
538
539 left join sistema.Usuario u with (nolock)
540 on b.UsuarioAsignadoId = u.Id
541
542 left join sistema.Usuario uc with (nolock)
543 on b.CreadoPor = uc.Id
544
545 left join Bitacora b3
546 on b3.Id = bo2.Id
547 left join sistema.Usuario usuarioAnterior
548 on usuarioAnterior.Id = case when @rol = 2 and (b.EstatusId = 3 or b.EstatusId = 12) and b.EstatusSecundarioId is not null then b3.CreadoPor
549 else b3.UsuarioAsignadoId end
550
551 left join sistema.UsuarioRol ur with (nolock)
552 on m.CreadoPor = ur.UsuarioId
553
554 left join sistema.TipoDocumentoPresentado td with (nolock)
555 on td.Id = m.NombreDocumentoId
556
557 left join dbo.Asiento a with (nolock)
558 on b.AsientoId = a.Id
559 left join Notificacion n
560 on n.AsientoId = a.Id
561 where 1 = case when m.DocumentoEnAtraso = 1 and @Presentacion = 'PK' then 1
562 when m.DocumentoLey216 = 1 and @Presentacion = '216' then 1
563 when isnull(m.DocumentoEnAtraso,0) = 0 and isnull(m.DocumentoLey216,0) = 0 and @Presentacion = 'K' then 1
564 when @Presentacion = '0' then 1
565 else 0 end
566
567 group by b.UsuarioAsignadoId,
568 b.AsientoId,
569 a.Asiento,
570 b.EstatusId,
571 b.Id,
572 me.Estatus,
573 m.Id,
574 m.DocumentoEnAtraso,
575 m.DocumentoLey216,
576 td.Id,
577 TipoOrigenDocumentoId,
578 td.Nombre,
579 NombreDocumentoId,
580 m.NumeroRua, NombreNotario, m.AgenciaId,
581 m.OficialACargo,
582 m.DatosAdicionalesNotario,
583
584 m.SegundoNombreNotario,
585 m.PrimerApellidoNotario,
586 m.SegundoApellidoNotario,
587 m.EmailNotario,
588 m.TelefonoNotario,
589 m.DireccionPostalNotario,
590 m.Email1,
591 m.Email2,
592 NumeroAffidavitCasoInstancia,
593 LugarOtorgamientoEscritura, CasoJudicial, AccionJudicial, DemandadoJudicial, DemandanteJudicial,
594 ExParteJudicial, FechaEscritura, ArancelesCalculados, Exento,
595
596 TipoExpedicion, m.Razon,
597 Modelo, Texto, GUIDArchivoPresentado, GUIDArchivoMinuta,
598 FechaPresentacion,
599 m.Creado,
600 uc.Nombre, uc.PrimerApellido,
601 m.CreadoPor, m.Modificado, m.ModificadoPor,
602 ur.RolId,
603 b.Creado,
604 uc.Email,
605 m.Telematica,
606 usuarioAnterior.Nombre,
607 usuarioAnterior.SegundoNombre,
608 usuarioAnterior.PrimerApellido,
609 usuarioAnterior.SegundoApellido,
610 n.FechaCaducidad
611 ) m
612 where m.RowNumber = 1
613 and 1 = case
614 -- minuta no ha sido presentada
615 when MaxAsientoId = 0 and AsientoId is null then 1
616 -- si fue presentada, solo el record que tenga asiento y no la bitacora inicial con asiento null
617 when MaxAsientoId > 0 and AsientoId is not null then 1
618 -- no señor
619 else 0 end
620 and 1 = case when m.UsuarioAsignadoId = @UsuarioId then 1
621 when m.CreadoPor = @UsuarioId and @rol = 3 then 1
622 else 0 end
623 and 1 = case when @EstatusId = 0 then 1
624 when m.EstatusId = @EstatusId then 1
625 else 0 end
626 and exists
627 (
628 select 1
629 from dbo.split(fincas,'|') s
630 where 1 = case when @finca = '0' then 1
631 when charindex('-',item) > 0 and ltrim(rtrim(left(item, charindex('-',item)-1))) like '%' + @finca + '%' then 1
632 else 0 end
633 and 1 = case when @demarcacion = '0' then 1
634 when charindex('-',item) > 0 and left(ltrim(rtrim(right(item, len(item)-charindex('-',item)))),6) like '%' + @demarcacion + '%' then 1
635 else 0 end
636 )
637 -- primero los no presentados, para que los acabe de presentar y luego la fecha asignado que es el orden en que cambiaron de estatus (se presentaron o despacharon, etc)
638 order by case when EstatusId = 1 then 0 else 1 end, FechaAsignado
639
640 OFFSET @offset ROWS FETCH NEXT @PageSize ROWS ONLY
641 end
642
643 drop table #bitacora
644
645END