1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
|
/****** Object: StoredProcedure [dbo].[recherchenbedossier] Script Date: 08/20/2014 11:40:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[recherchenbedossier]
(
@GroupeUserId varchar(36) = '',
@DepartementId varchar(36) = '',
@TypeStatusTf int,
@SupplierId varchar(36) = '',
@CustomerId varchar(36) = '',
@ItemCodeSearch varchar(MAX),
@SupplierReferenceSearch varchar(MAX),
@DueDateSearch varchar(50) = '',
@OperationSearch varchar(36) = '',
@SupplierSearch varchar(36) = '',
@LaboratorySearch varchar(MAX),
@StatusIsNewJob varchar(50) = '',
@StatusHasDocument varchar(50) = '',
@StatusValidationDateStandards varchar(50) = '',
@StatusTestValidationDate varchar(50) = '',
@StatusValidationDateMarking varchar(50) = '',
@StatusTrackingAlert varchar(50) = '',
@StatutsQuestionInstanceTestResult varchar(50) = '',
@StatusCalcHasDeadlineComingSoon varchar(50) = '',
@StatusIsCompositionValid varchar(50) = '',
@Status1 varchar(50) = '',
@Status2 varchar(50) = '',
@Status3 varchar(50) = '',
@Status4 varchar(50) = '',
@Status5 varchar(50) = '',
@Status6 varchar(50) = '',
@Status7 varchar(50) = '',
@Status8 varchar(50) = '',
@PageIndex int,
@PageSize int,
@TotalRowCount int Output,
@OrderBy varchar(MAX),
@OrderSort varchar(5),
@startdate datetime = '1970-01-01 00:00:00',
@enddate datetime= '2039-12-31 23:59:59',
@debug bit = 0
)
AS
BEGIN
-- Déclaration(s)
DECLARE @FirstPage int;
DECLARE @LastPage int;
DECLARE @Requete varchar(MAX);
-- Passage en UpperCase
Set @ItemCodeSearch = upper(@ItemCodeSearch)
Set @LaboratorySearch = upper(@LaboratorySearch)
--Initialisation(s)
SET @PageIndex = @PageIndex + 1;
SET @FirstPage = ((@PageIndex - 1) * @PageSize + 1);
SET @LastPage = (@PageIndex * @PageSize);
SET NOCOUNT ON;
CREATE TABLE #TempTab0
(
[Id] UniqueIdentifier,
[DepartementId] UniqueIdentifier NULL,
[IsNewJob] bit NULL,
[IsArchived] bit NULL,
[HasDocument] int NULL,
[ValidationDateStandards] Datetime NULL,
[TestValidationDate] Datetime NULL,
[ValidationDateMarking] Datetime NULL,
[TrackingAlert] bit NULL,
[QuestionInstanceTestResult] int NOT NULL,
[CountDeadlineDayLeft] int NULL,
[IsCompositionValid] bit NULL,
[ItemCode] Varchar(MAX) NULL,
[SuppliersReference] Varchar(MAX) NULL,
[SupplyChaineDeadline] Datetime NULL,
[OperationDescription] Varchar(MAX) NULL,
[OperationId] UniqueIdentifier NULL,
[SupplierName] nVarchar(MAX) NULL,
[SupplierId] UniqueIdentifier NULL,
[Laboratory] Varchar(MAX) NULL,
[GroupeUserId] UniqueIdentifier NULL,
[CompletedOn] Datetime NULL,
[Completeness] float NULL,
[Result] dec(3,2) NULL,
[KPIStatus] nvarchar(10) NULL,
[klist] Varchar(10) NULL
);
CREATE TABLE #TempTab1
(
[Id] UniqueIdentifier NOT NULL,
[IsNewJob] bit NOT NULL,
[HasDocument] int NOT NULL,
[ValidationDateStandards] Datetime NULL,
[TestValidationDate] Datetime NULL,
[ValidationDateMarking] Datetime NULL,
[TrackingAlert] bit NOT NULL,
[QuestionInstanceTestResult] int NOT NULL,
[CountDeadlineDayLeft] int NULL,
[IsCompositionValid] bit NOT NULL,
[ItemCode] Varchar(MAX) NULL,
[SuppliersReference] Varchar(MAX) NULL,
[SupplyChaineDeadline] Datetime NULL,
[OperationDescription] Varchar(MAX) NULL,
[OperationId] UniqueIdentifier NULL,
[SupplierName] nVarchar(MAX) NULL,
[SupplierId] UniqueIdentifier NULL,
[Laboratory] Varchar(MAX) NULL,
[CompletedOn] Datetime NULL,
[Completeness] float NULL,
[Status1] dec(3,2) NULL,
[Status2] dec(3,2) NULL,
[Status3] dec(3,2) NULL,
[Status4] dec(3,2) NULL,
[Status5] dec(3,2) NULL,
[Status6] dec(3,2) NULL,
[Status7] dec(3,2) NULL,
[Status8] dec(3,2) NULL,
[Text1] Varchar(MAX) NULL,
[Text2] Varchar(MAX) NULL,
[Text3] Varchar(MAX) NULL,
[Text4] Varchar(MAX) NULL,
[Text5] Varchar(MAX) NULL,
[Text6] Varchar(MAX) NULL,
[Text7] Varchar(MAX) NULL,
[Text8] Varchar(MAX) NULL
-- [Row] Int NULL
);
CREATE TABLE #TempTab2
(
[Id] UniqueIdentifier NOT NULL,
[IsNewJob] bit NOT NULL,
[HasDocument] int NOT NULL,
[ValidationDateStandards] Datetime NULL,
[TestValidationDate] Datetime NULL,
[ValidationDateMarking] Datetime NULL,
[TrackingAlert] bit NOT NULL,
[QuestionInstanceTestResult] int NOT NULL,
[CountDeadlineDayLeft] int NULL,
[IsCompositionValid] bit NOT NULL,
[ItemCode] Varchar(MAX) NULL,
[SuppliersReference] Varchar(MAX) NULL,
[SupplyChaineDeadline] Datetime NULL,
[OperationDescription] Varchar(MAX) NULL,
[SupplierName] nVarchar(MAX) NULL,
[Laboratory] Varchar(MAX) NULL,
[CompletedOn] Datetime NULL,
[Completeness] float NULL,
[Status1] dec(3,2) NULL,
[Status2] dec(3,2) NULL,
[Status3] dec(3,2) NULL,
[Status4] dec(3,2) NULL,
[Status5] dec(3,2) NULL,
[Status6] dec(3,2) NULL,
[Status7] dec(3,2) NULL,
[Status8] dec(3,2) NULL,
[Text1] Varchar(MAX) NULL,
[Text2] Varchar(MAX) NULL,
[Text3] Varchar(MAX) NULL,
[Text4] Varchar(MAX) NULL,
[Text5] Varchar(MAX) NULL,
[Text6] Varchar(MAX) NULL,
[Text7] Varchar(MAX) NULL,
[Text8] Varchar(MAX) NULL,
[Row] Int NULL
);
CREATE TABLE #TempTab0b
(
[Id] UniqueIdentifier NOT NULL,
[GroupeUserId] UniqueIdentifier NOT NULL,
[DepartementId] UniqueIdentifier NULL,
[IsNewJob] bit NOT NULL,
[IsArchived] bit NULL,
[HasDocument] int NOT NULL,
[ValidationDateStandards] Datetime NULL,
[TestValidationDate] Datetime NULL,
[ValidationDateMarking] Datetime NULL,
[TrackingAlert] bit NOT NULL,
[QuestionInstanceTestResult] int NOT NULL,
[CountDeadlineDayLeft] int NULL,
[IsCompositionValid] bit NOT NULL,
[ItemCode] Varchar(MAX) NULL,
[SuppliersReference] Varchar(MAX) NULL,
[SupplyChaineDeadline] Datetime NULL,
[OperationDescription] Varchar(MAX) NULL,
[OperationId] UniqueIdentifier NULL,
[SupplierName] nVarchar(MAX) NULL,
[SupplierId] UniqueIdentifier NULL,
[Laboratory] Varchar(MAX) NULL,
[CompletedOn] Datetime NULL,
[Completeness] float NULL,
[Status1] dec(3,2) NULL,
[Status2] dec(3,2) NULL,
[Status3] dec(3,2) NULL,
[Status4] dec(3,2) NULL,
[Status5] dec(3,2) NULL,
[Status6] dec(3,2) NULL,
[Status7] dec(3,2) NULL,
[Status8] dec(3,2) NULL,
[Text1] Varchar(MAX) NULL,
[Text2] Varchar(MAX) NULL,
[Text3] Varchar(MAX) NULL,
[Text4] Varchar(MAX) NULL,
[Text5] Varchar(MAX) NULL,
[Text6] Varchar(MAX) NULL,
[Text7] Varchar(MAX) NULL,
[Text8] Varchar(MAX) NULL
-- [Row] Int NULL
);
DECLARE @SQL nvarchar(MAX);
DECLARE @SQLSELECT nvarchar(MAX);
DECLARE @SQLFROM nvarchar(MAX);
DECLARE @SQLWHERE nvarchar(MAX);
DECLARE @ParmDefinition nvarchar(MAX);
-- Création de la table 0
INSERT INTO #TempTab0
SELECT job.Id as 'Id'
,job.DepartementId as 'DepartementId'
,job.IsNewJob as 'IsNewJob'
,job.IsArchived as 'IsArchived'
,0 as'HasDocument'
,job.ValidationDateStandards as 'ValidationDateStandards'
,job.TestValidationDate as 'TestValidationDate'
,job.ValidationDateMarking as 'ValidationDateMarking'
,job.TrackingAlert as 'TrackingAlert'
,QuestionInstanceResult=
CASE
WHEN pis.Result = 'Pending' THEN 1
WHEN pis.Result = 'Waive' THEN 3
WHEN pis.Result = 'Fail' THEN 4
WHEN pis.Result = 'Pass' THEN 2
WHEN pis.Result = 'Not acceptable' THEN 5
ELSE 0
END
,DATEDIFF(day, GETDATE(), SupplyChaineDeadline) as 'CountDeadlineDayLeft'
,job.IsCompositionValid as 'IsCompositionValid'
,job.ItemCode as 'ItemCode'
,job.SuppliersReference as 'SuppliersReference'
,job.SupplyChaineDeadline as 'SupplyChaineDeadline'
,(Select operation.Description From Operation operation where operation.Id = job.OperationId) as 'OperationDescription'
,job.OperationId as 'OperationId'
,(Select supplier.Name From Supplier supplier where supplier.Id = job.SupplierId) as 'SupplierName'
,job.SupplierId as 'SupplierId'
,job.Laboratory as 'Laboratory'
,job.GroupeUserId as 'GroupeUserId'
,job.CompletedOn as 'CompletedOn'
--,job.IsArchived as 'IsArchived'
,pis.Completeness
,Result =
CASE
WHEN pis1.Result = 'Pending' and pis1.Completeness = 0
THEN 1 -- Pending
WHEN pis1.Result = 'Pending' and pis1.Completeness > 0 -- Pending + Completeness
THEN Case
When pis1.Completeness< 0.01 THEN 0.01
When pis1.Completeness> 0.99 THEN 0.99
Else pis1.Completeness
END
WHEN pis1.Result = 'Waive' THEN 3 -- Waive
WHEN pis1.Result = 'Fail' THEN 4 -- Fail
WHEN pis1.Result = 'Pass' THEN 2 -- Pass
WHEN pis1.Result = 'Not acceptable' THEN 5 -- Not acceptable
ELSE 0
END
,vpd1.KPIStatus
,'Status' + cast(kpistatus as varchar(1)) klist
FROM dbo.Job job
LEFT JOIN ProcessInstance pis on pis.JobId=job.Id and pis.ParentId is null and pis.active = 1
LEFT JOIN VProcessInstance2 pis1 on pis1.JobId = job.id
LEFT JOIN vprocessdesign vpd1 on (pis1.RawProcessId=vpd1.Id or pis1.Standardid=vpd1.StandardId) and job.ProcessId=vpd1.ProcessId and KPIStatus is not Null
WHERE job.GroupeUserId = @GroupeUserId
AND Job.CreatedOn between @startdate and @enddate
-- pour inclure les KPIStatus qui viennent des Actions -- début
UNION ALL
SELECT job.Id as 'Id'
,job.DepartementId as 'DepartementId'
,job.IsNewJob as 'IsNewJob'
,job.IsArchived as 'IsArchived'
,0 as'HasDocument'
,job.ValidationDateStandards as 'ValidationDateStandards'
,job.TestValidationDate as 'TestValidationDate'
,job.ValidationDateMarking as 'ValidationDateMarking'
,job.TrackingAlert as 'TrackingAlert'
,QuestionInstanceResult=
CASE
WHEN pis.Result = 'Pending' THEN 1 -- Pending
WHEN pis.Result = 'Waive' THEN 3 -- Waive
WHEN pis.Result = 'Fail' THEN 4 -- Fail
WHEN pis.Result = 'Pass' THEN 2 -- Pass
WHEN pis.Result = 'Not acceptable' THEN 5 -- Not acceptable
ELSE 0
END
,DATEDIFF(day, GETDATE(), SupplyChaineDeadline) as 'CountDeadlineDayLeft'
,job.IsCompositionValid as 'IsCompositionValid'
,job.ItemCode as 'ItemCode'
,job.SuppliersReference as 'SuppliersReference'
,job.SupplyChaineDeadline as 'SupplyChaineDeadline'
,(Select operation.Description From Operation operation where operation.Id = job.OperationId) as 'OperationDescription'
,job.OperationId as 'OperationId'
,(Select supplier.Name From Supplier supplier where supplier.Id = job.SupplierId) as 'SupplierName'
,job.SupplierId as 'SupplierId'
,job.Laboratory as 'Laboratory'
,job.GroupeUserId as 'GroupeUserId'
,job.CompletedOn as 'CompletedOn'
--,job.IsArchived as 'IsArchived'
,pis.Completeness
,Result =
CASE
WHEN ia.Completed is null THEN 0 -- Nothing
WHEN ia.Completed = 0 THEN 1 -- Pending
WHEN ia.Completed = 1 THEN 2 -- Pass
ELSE 0
END
,a.KPIStatus
, 'Status' + cast(a.KPIStatus as varchar(1)) klist
FROM dbo.Job job
LEFT JOIN ProcessInstance pis on pis.JobId=job.Id and pis.ParentId is null and pis.active = 1
JOIN InstanceAction ia on job.id=ia.JobId
LEFT JOIN irisaction a on a.id=ia.ActionId
WHERE job.GroupeUserId = @GroupeUserId
AND Job.CreatedOn between @startdate and @enddate
-- pour inclure les KPIStatus qui viennent des Actions -- fin
IF @debug = 1 SELECT * FROM #TempTab0
INSERT INTO #TempTab0b
SELECT
[Id]
,[GroupeUserId]
,[DepartementId]
,[IsNewJob]
,[IsArchived]
--,[CountDocumentDateJob]
--,[CountDocumentDateCertif]
,[HasDocument]
,[ValidationDateStandards]
,[TestValidationDate]
,[ValidationDateMarking]
,[TrackingAlert]
,[QuestionInstanceTestResult]
,[CountDeadlineDayLeft]
,[IsCompositionValid]
,[ItemCode]
,[SuppliersReference]
,[SupplyChaineDeadline]
,[OperationDescription]
,[OperationId]
,[SupplierName]
,[SupplierId]
,[Laboratory]
,[CompletedOn]
,[Completeness]
,case when Status1 is null then 0 else Status1 end Status1
,case when Status2 is null then 0 else Status2 end Status2
,case when Status3 is null then 0 else Status3 end Status3
,case when Status4 is null then 0 else Status4 end Status4
,case when Status5 is null then 0 else Status5 end Status5
,case when Status6 is null then 0 else Status6 end Status6
,case when Status7 is null then 0 else Status7 end Status7
,case when Status8 is null then 0 else Status8 end Status8
,'' Text1
,'' Text2
,'' Text3
,'' Text4
,'' Text5
,'' Text6
,'' Text7
,'' Text8
FROM
(select
[Id]
,[DepartementId]
,[IsNewJob]
,[IsArchived]
,[HasDocument]
,[ValidationDateStandards]
,[TestValidationDate]
,[ValidationDateMarking]
,[TrackingAlert]
,[QuestionInstanceTestResult]
,[CountDeadlineDayLeft]
,[IsCompositionValid]
,[ItemCode]
,[SuppliersReference]
,[SupplyChaineDeadline]
,[OperationDescription]
,[OperationId]
,[SupplierName]
,[SupplierId]
,[Laboratory]
,[GroupeUserId]
,[CompletedOn]
,[Completeness]
,Result, klist
from #TempTab0
) sourcetable
pivot (max(Result) for [klist] in ([Status1],[Status2],[Status3],[Status4],[Status5],[Status6],[Status7],[Status8])) PivotTable order by Id
IF @debug = 1 SELECT * FROM #TempTab0b
SET @SQLSELECT = 'INSERT INTO #TempTab1
SELECT Job.Id as ''Id''
,IsNewJob as ''IsNewJob''
,0 as ''HasDocument''
,ValidationDateStandards as ''ValidationDateStandards''
,TestValidationDate as ''TestValidationDate''
,ValidationDateMarking as ''ValidationDateMarking''
,TrackingAlert as ''TrackingAlert''
,QuestionInstanceTestResult as ''QuestionInstanceTestResult''
,DATEDIFF(day, GETDATE(), SupplyChaineDeadline) as ''CountDeadlineDayLeft''
,IsCompositionValid as ''IsCompositionValid''
,ItemCode as ''ItemCode''
,SuppliersReference as ''SuppliersReference''
,SupplyChaineDeadline as ''SupplyChaineDeadline''
,OperationDescription as ''OperationDescription''
,OperationId as ''OperationId''
,SupplierName as ''SupplierName''
,SupplierId as ''SupplierId''
,Laboratory as ''Laboratory''
,CompletedOn as ''CompletedOn''
,Completeness as ''Completeness''
,Status1 as ''Status1''
,Status2 as ''Status2''
,Status3 as ''Status3''
,Status4 as ''Status4''
,Status5 as ''Status5''
,Status6 as ''Status6''
,Status7 as ''Status7''
,Status8 as ''Status8''
,Text1 as ''Text1''
,Text2 as ''Text2''
,Text3 as ''Text3''
,Text4 as ''Text4''
,Text5 as ''Text5''
,Text6 as ''Text6''
,Text7 as ''Text7''
,Text8 as ''Text8''
'--,ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ' ' + @OrderSort + ')'
SET @SQLFROM = ' FROM #TempTab0b job WITH (NOLOCK) '
IF @CustomerId <> '' SET @SQLFROM = @SQLFROM + ' INNER JOIN JobCustomer ON job.Id = JobCustomer.JobId '
IF @DepartementId <> '' SET @SQLFROM = @SQLFROM + ' INNER JOIN Departement on job.DepartementId = Departement.Id '
SET @SQLWHERE = ' WHERE (job.GroupeUserId = @GroupeUserId) '
IF @DepartementId = '00000000-0000-0000-0000-000000000000' SET @SQLWHERE = @SQLWHERE + ' AND job.DepartementId IS NULL '
ELSE IF @DepartementId <> '' SET @SQLWHERE = @SQLWHERE + ' AND (Departement.Id = @DepartementId OR Departement.ParentId = @DepartementId) '
IF @SupplierId <> '' SET @SQLWHERE = @SQLWHERE + ' AND job.SupplierId = @SupplierId '
IF @CustomerId <> '' SET @SQLWHERE = @SQLWHERE + ' AND JobCustomer.CustomerId = @CustomerId '
IF @TypeStatusTf = '0' SET @SQLWHERE = @SQLWHERE + ' AND (CompletedOn IS NULL) AND (IsArchived = 0) '
IF @TypeStatusTf = '1' SET @SQLWHERE = @SQLWHERE + ' AND (CompletedOn IS NOT NULL) AND (IsArchived = 0) '
IF @TypeStatusTf = '2' SET @SQLWHERE = @SQLWHERE + ' AND (IsArchived = 1) '
IF @ItemCodeSearch <> '' SET @SQLWHERE = @SQLWHERE + ' AND upper(job.ItemCode) like ''%' + @ItemCodeSearch + '%'' '
IF @SupplierReferenceSearch <> '' SET @SQLWHERE = @SQLWHERE + ' AND job.SuppliersReference like ''%' + @SupplierReferenceSearch + '%'' '
IF @DueDateSearch <> '' SET @SQLWHERE = @SQLWHERE + ' AND job.SupplyChaineDeadline = ''' + @DueDateSearch + ''' '
IF @OperationSearch <> '' SET @SQLWHERE = @SQLWHERE + ' AND job.OperationId = @OperationSearch '
IF @SupplierSearch <> '' SET @SQLWHERE = @SQLWHERE + ' AND job.SupplierId = @SupplierSearch '
IF @LaboratorySearch <> '' SET @SQLWHERE = @SQLWHERE + ' AND upper(job.Laboratory) like ''%' + @LaboratorySearch + '%'' '
IF(@StatusIsNewJob <> '')
SET @SQLWHERE =
CASE @StatusIsNewJob
WHEN 'new-20' THEN @SQLWHERE + ' AND IsNewJob = 1 '
WHEN 'unchecked' THEN @SQLWHERE + ' AND IsNewJob = 0 '
END;
IF(@StatusValidationDateStandards <> '')
SET @SQLWHERE =
CASE @StatusValidationDateStandards
WHEN 'buddy-20' THEN @SQLWHERE + ' AND ValidationDateStandards IS NULL '
WHEN 'unchecked' THEN @SQLWHERE + ' AND ValidationDateStandards IS NOT NULL '
END;
IF(@StatusTestValidationDate <> '')
SET @SQLWHERE =
CASE @StatusTestValidationDate
WHEN 'engrenage-20' THEN @SQLWHERE + ' AND TestValidationDate IS NULL '
WHEN 'unchecked' THEN @SQLWHERE + ' AND TestValidationDate IS NOT NULL '
END;
IF(@StatusValidationDateMarking <> '')
SET @SQLWHERE =
CASE @StatusValidationDateMarking
WHEN 'pinceau-20' THEN @SQLWHERE + ' AND ValidationDateMarking IS NULL '
WHEN 'unchecked' THEN @SQLWHERE + ' AND ValidationDateMarking IS NOT NULL '
END;
IF(@StatusTrackingAlert <> '')
SET @SQLWHERE =
CASE @StatusTrackingAlert
WHEN 'warning-20' THEN @SQLWHERE + ' AND TrackingAlert = 1 '
WHEN 'unchecked' THEN @SQLWHERE + ' AND TrackingAlert = 0 '
END;
IF(@StatutsQuestionInstanceTestResult <> '')
SET @SQLWHERE =
CASE @StatutsQuestionInstanceTestResult
WHEN 'unchecked' THEN @SQLWHERE + ' AND QuestionInstanceTestResult = 0 '
WHEN 'processna' THEN @SQLWHERE + ' AND QuestionInstanceTestResult = 1 '
WHEN 'processpass' THEN @SQLWHERE + ' AND QuestionInstanceTestResult = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND QuestionInstanceTestResult = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND QuestionInstanceTestResult = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND QuestionInstanceTestResult = 5 '
END;
IF(@StatusCalcHasDeadlineComingSoon <> '')
SET @SQLWHERE =
CASE @StatusCalcHasDeadlineComingSoon
WHEN 'calendrier-20' THEN @SQLWHERE + ' AND CompletedOn IS NULL AND SupplyChaineDeadline IS NOT NULL AND CountDeadlineDayLeft IS NOT NULL AND CountDeadlineDayLeft <= 15 AND CountDeadlineDayLeft >= 0 '
WHEN 'unchecked' THEN @SQLWHERE + ' AND NOT (CompletedOn IS NULL AND SupplyChaineDeadline IS NOT NULL AND CountDeadlineDayLeft IS NOT NULL AND CountDeadlineDayLeft <= 15 AND CountDeadlineDayLeft >= 0) '
END;
IF(@StatusIsCompositionValid <> '')
SET @SQLWHERE =
CASE @StatusIsCompositionValid
WHEN 'molecule-20' THEN @SQLWHERE + ' AND IsCompositionValid = 0 '
WHEN 'unchecked' THEN @SQLWHERE + ' AND IsCompositionValid = 1 '
END;
IF(@Status1 <> '')
SET @SQLWHERE =
CASE @Status1
WHEN 'unchecked' THEN @SQLWHERE + ' AND Status1 = 0 '
-- WHEN 'processna' THEN @SQLWHERE + ' AND Status1 = 1 '
WHEN 'processna' THEN @SQLWHERE + ' AND (Status1 > 0.00 and Status1 <= 1.00)'
WHEN 'processpass' THEN @SQLWHERE + ' AND Status1 = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND Status1 = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND Status1 = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND Status1 = 5 '
END;
IF(@Status2 <> '')
SET @SQLWHERE =
CASE @Status2
WHEN 'unchecked' THEN @SQLWHERE + ' AND Status2 = 0 '
-- WHEN 'processna' THEN @SQLWHERE + ' AND Status2 = 1 '
WHEN 'processna' THEN @SQLWHERE + ' AND (Status2 > 0.00 and Status2 <= 1.00)'
WHEN 'processpass' THEN @SQLWHERE + ' AND Status2 = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND Status2 = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND Status2 = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND Status2 = 5 '
END;
IF(@Status3 <> '')
SET @SQLWHERE =
CASE @Status3
WHEN 'unchecked' THEN @SQLWHERE + ' AND Status3 = 0 '
-- WHEN 'processna' THEN @SQLWHERE + ' AND Status3 = 1 '
WHEN 'processna' THEN @SQLWHERE + ' AND (Status3 > 0.00 and Status3 <= 1.00)'
WHEN 'processpass' THEN @SQLWHERE + ' AND Status3 = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND Status3 = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND Status3 = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND Status3 = 5 '
END;
IF(@Status4 <> '')
SET @SQLWHERE =
CASE @Status4
WHEN 'unchecked' THEN @SQLWHERE + ' AND Status4 = 0 '
-- WHEN 'processna' THEN @SQLWHERE + ' AND Status4 = 1 '
WHEN 'processna' THEN @SQLWHERE + ' AND (Status4 > 0.00 and Status4 <= 1.00)'
WHEN 'processpass' THEN @SQLWHERE + ' AND Status4 = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND Status4 = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND Status4 = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND Status4 = 5 '
END;
IF(@Status5 <> '')
SET @SQLWHERE =
CASE @Status5
WHEN 'unchecked' THEN @SQLWHERE + ' AND Status5 = 0 '
-- WHEN 'processna' THEN @SQLWHERE + ' AND Status5 = 1 '
WHEN 'processna' THEN @SQLWHERE + ' AND (Status5 > 0.00 and Status5 <= 1.00)'
WHEN 'processpass' THEN @SQLWHERE + ' AND Status5 = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND Status5 = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND Status5 = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND Status5 = 5 '
END;
IF(@Status6 <> '')
SET @SQLWHERE =
CASE @Status6
WHEN 'unchecked' THEN @SQLWHERE + ' AND Status6 = 0 '
-- WHEN 'processna' THEN @SQLWHERE + ' AND Status6 = 1 '
WHEN 'processna' THEN @SQLWHERE + ' AND (Status6 > 0.00 and Status6 <= 1.00)'
WHEN 'processpass' THEN @SQLWHERE + ' AND Status6 = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND Status6 = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND Status6 = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND Status6 = 5 '
END;
IF(@Status7 <> '')
SET @SQLWHERE =
CASE @Status7
WHEN 'unchecked' THEN @SQLWHERE + ' AND Status7 = 0 '
-- WHEN 'processna' THEN @SQLWHERE + ' AND Status7 = 1 '
WHEN 'processna' THEN @SQLWHERE + ' AND (Status7 > 0.00 and Status7 <= 1.00)'
WHEN 'processpass' THEN @SQLWHERE + ' AND Status7 = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND Status7 = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND Status7 = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND Status7 = 5 '
END;
IF(@Status8 <> '')
SET @SQLWHERE =
CASE @Status8
WHEN 'unchecked' THEN @SQLWHERE + ' AND Status8 = 0 '
-- WHEN 'processna' THEN @SQLWHERE + ' AND Status8 = 1 '
WHEN 'processna' THEN @SQLWHERE + ' AND (Status8 > 0.00 and Status8 <= 1.00)'
WHEN 'processpass' THEN @SQLWHERE + ' AND Status8 = 2 '
WHEN 'waived' THEN @SQLWHERE + ' AND Status8 = 3 '
WHEN 'bombe-20' THEN @SQLWHERE + ' AND Status8 = 4 '
WHEN 'moinsorange' THEN @SQLWHERE + ' AND Status8 = 5 '
END;
---------------------------------------------------------------------------
IF @OrderBy = 'QuestionInstanceTestResult' SET @OrderBy = @OrderBy + '] ' + @OrderSort + ' , [Completeness'
--
SET @SQL = @SQLSELECT + @SQLFROM + @SQLWHERE
IF @debug=1 PRINT @SQL --DEBUG
SET @ParmDefinition = '
@GroupeUserId varchar(36),
@DepartementId varchar(36),
@SupplierId varchar(36),
@CustomerId varchar(36),
@OperationSearch varchar(36),
@SupplierSearch varchar(36)
'
EXECUTE sp_executesql @SQL, @ParmDefinition, @GroupeUserId, @DepartementId, @SupplierId, @CustomerId, @OperationSearch, @SupplierSearch;
--'ORDER BY [' + @OrderBy + '] '
--Deuxième table temporaire pour filtre sur HasDocument
SET @SQLSELECT = 'INSERT INTO #TempTab2
select * ,ROW_NUMBER() OVER (ORDER BY [' + @OrderBy + '] ' + @OrderSort + ') from (
SELECT Id as ''Id''
,IsNewJob as ''IsNewJob''
,HasDocument=
CASE
WHEN ((Select count (*) From DocumentDateJob Where DocumentDateJob.JobId = job.Id) > 0 AND (Select count (*) From DocumentDateCertif inner join CertificateJob on CertificateJob.CertificateId = DocumentDateCertif.CertificateId Where CertificateJob.JobId = job.Id) > 0) THEN 3
WHEN ((Select count (*) From DocumentDateJob Where DocumentDateJob.JobId = job.Id) = 0 AND (Select count (*) From DocumentDateCertif inner join CertificateJob on CertificateJob.CertificateId = DocumentDateCertif.CertificateId Where CertificateJob.JobId = job.Id) > 0) THEN 2
WHEN ((Select count (*) From DocumentDateJob Where DocumentDateJob.JobId = job.Id) > 0 AND (Select count (*) From DocumentDateCertif inner join CertificateJob on CertificateJob.CertificateId = DocumentDateCertif.CertificateId Where CertificateJob.JobId = job.Id) = 0) THEN 1
ELSE 0
END
,ValidationDateStandards as ''ValidationDateStandards''
,TestValidationDate as ''TestValidationDate''
,ValidationDateMarking as ''ValidationDateMarking''
,TrackingAlert as ''TrackingAlert''
,QuestionInstanceTestResult as ''QuestionInstanceTestResult''
,DATEDIFF(day, GETDATE(), SupplyChaineDeadline) as ''CountDeadlineDayLeft''
,IsCompositionValid as ''IsCompositionValid''
,ItemCode as ''ItemCode''
,SuppliersReference as ''SuppliersReference''
,SupplyChaineDeadline as ''SupplyChaineDeadline''
,OperationDescription as ''OperationDescription''
,SupplierName as ''SupplierName''
,Laboratory as ''Laboratory''
,CompletedOn as ''CompletedOn''
,Completeness as ''Completeness''
,Status1 as ''Status1''
,Status2 as ''Status2''
,Status3 as ''Status3''
,Status4 as ''Status4''
,Status5 as ''Status5''
,Status6 as ''Status6''
,Status7 as ''Status7''
,Status8 as ''Status8''
,Text1 as ''Text1''
,Text2 as ''Text2''
,Text3 as ''Text3''
,Text4 as ''Text4''
,Text5 as ''Text5''
,Text6 as ''Text6''
,Text7 as ''Text7''
,Text8 as ''Text8''
'-- ,ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ' ' + @OrderSort + ')'
SET @SQLFROM = ' FROM #TempTab1 job WITH (NOLOCK) ) wip '
SET @SQLWHERE = ' where 1=1'
IF(@StatusHasDocument <> '')
SET @SQLWHERE =
CASE @StatusHasDocument
WHEN 'unchecked' THEN @SQLWHERE + ' AND HasDocument = 0 '
WHEN 'trombonef-20' THEN @SQLWHERE + ' AND HasDocument = 1 '
WHEN 'trombonec-20' THEN @SQLWHERE + ' AND HasDocument = 2 '
WHEN 'trombonefc-20' THEN @SQLWHERE + ' AND HasDocument = 3 '
END;
SET @SQL = @SQLSELECT + @SQLFROM + @SQLWHERE
IF @debug = 1 PRINT @SQL
EXECUTE sp_executesql @SQL;
-- Récupération de la bonne page d'enregistrements
SET @SQLSELECT = '
SELECT Id
,IsNewJob
,HasDocument
,ValidationDateStandards
,TestValidationDate
,ValidationDateMarking
,TrackingAlert
,QuestionInstanceTestResult
,CountDeadlineDayLeft
,IsCompositionValid
,ItemCode
,SuppliersReference
,SupplyChaineDeadline
,OperationDescription
,SupplierName
,Laboratory
,CompletedOn
,Completeness
,Status1 as ''Status1''
,Status2 as ''Status2''
,Status3 as ''Status3''
,Status4 as ''Status4''
,Status5 as ''Status5''
,Status6 as ''Status6''
,Status7 as ''Status7''
,Status8 as ''Status8''
,Text1 as ''Text1''
,Text2 as ''Text2''
,Text3 as ''Text3''
,Text4 as ''Text4''
,Text5 as ''Text5''
,Text6 as ''Text6''
,Text7 as ''Text7''
,Text8 as ''Text8''
FROM #TempTab2 job WITH (NOLOCK)
WHERE [Row] BETWEEN ' + CAST(@FirstPage AS VARCHAR) + ' AND ' + CAST(@LastPage AS VARCHAR) +
' ORDER BY [' + @OrderBy + '] ' + @OrderSort + ';';
EXECUTE(@SQLSELECT);
-- Récupération du nombre total d'enregistrements
SELECT @TotalRowCount = COUNT([Id]) FROM #TempTab2 WITH (NOLOCK);
-- Suppression des tables temporaires
-- 14 fév 2014 : On ne supprime plus, c'est la fin de la procédure qui s'en charge.
-- De plus on permet éventuellement du Cache.
-- DROP TABLE #TempTab1;
-- DROP TABLE #TempTab2;
-- DROP TABLE #TempTab0;
-- DROP TABLE #TempTab0b;
END |
Partager