--MAIN
select	--HEADER
						(SELECT TABLEID FROM SQLDICTIONARY WHERE NAME = 'PALEBI_SERVICEOPERATION' AND FIELDID = 0)                  SOTABLENUM,
						SOR.PALEBI_SERVICEOPERATIONID				                                                                SERVICEOPERATIONID, --ID
						SUBSTRING(SO.PROJID, 0, 11)                                                                                 PROJID, --PROGETTO
						ISNULL(DPEXTWRK.NAME, DPOWNER.NAME)			                                                                WORKERRESPONSIBLE, --RESPONSABILE
						DPCTPCUST.NAME              				                                                                CONTACT, --CONTATTO
						DPCUST.NAME									                                                                CUSTNAME, --CLIENTE

						SO.ENDOFERECTION,
						SO.ENDOFERECTIONDATE,
						SO.DRYSTARTUP,
						SO.DRYSTARTUPDATE,
						SO.HOTSTARTUP,
						SO.HOTSTARTUPDATE,
						SO.COMMISSIONING,
						SO.COMMISSIONINGDATE,
						SO.SIGNEDDATETIME,
						SO.RECID    'SORECID',
						ISNULL(LAN.LANGUAGEID,'it')																					'LANGUAGEID1',
						LAN.LANGUAGEID2,
						SO.ClientSupervisor																							CLIENT_SUPERVISOR,
						--HEADER
		
						--ROWS
						(SELECT TABLEID FROM SQLDICTIONARY WHERE NAME = 'PALEBI_SERVICEOPERATIONJOURNALDAY' AND FIELDID = 0)        SOJDTABLENUM,
						SOJD.RECID,
						SOJD.TRANSDATE,
						SOJD.TIMEFROMAM,
						SOJD.TIMETOAM,
						SOJD.TIMEFROMPM,
						SOJD.TIMETOPM,
						SOJD.HOURSTRAVEL,
						SOJD.HOURSWORK,
						SOJDD.DESCRIPTION,
						CASE WHEN SOJD.PALGUL_PRIVATE = 0 THEN  SOJD.DESCRIPTION ELSE '' END												as Notes,
						--ROWS
						PGSOJP.CLIENTSIGNATURE, 
						PGSOJP.TECHNICSIGNATURE,
						(SELECT 
							CASE
							WHEN D.CLIENTSUPERVISOR = '' OR (len(D.CLIENTSUPERVISOR)  - len(replace(D.CLIENTSUPERVISOR , '-', '')) <> 1) THEN ''
							WHEN D.CLIENTSUPERVISOR LIKE '520-%' THEN 
															(SELECT TOP 1 A.FirstName + ' ' + A.LastName
															 FROM DirPartyLookupGridView A 
															 INNER JOIN CONTACTPERSON B
															 ON A.PARTY = B.PARTY
															 AND A.PARTITION = B.PARTITION
															 AND GETDATE() >= A.ValidFrom
															 AND GETDATE() >= A.AddressValidFrom
															 AND GETDATE() <= A.ValidTo
															 AND GETDATE() <= A.AddressValidTo
															 WHERE A.RECID = REPLACE(D.CLIENTSUPERVISOR,'520-',''))

							WHEN D.CLIENTSUPERVISOR LIKE '106669-%' THEN 
															(SELECT TOP 1 A.Name + ' ' + A.LastName
															 FROM PALGUL_FieldContacts A 
															 WHERE A.RECID = REPLACE(D.CLIENTSUPERVISOR,'106669-',''))
															 END as s
							from PALEBI_SERVICEOPERATION D
							where PALEBI_SERVICEOPERATIONID = SOJD.PALEBI_SERVICEOPERATIONID) as CLIENTSUPERVISOR
				--HEADER
				--REVISIONE OPERAZIONE SERVICE
				from PALEBI_SERVICEOPERATIONREVISION									SOR
				--REVISIONE OPERAZIONE SERVICE

				--OPERAZIONE SERVICE
				left join PALEBI_SERVICEOPERATION											SO
					on SO.PALEBI_SERVICEOPERATIONID = SOR.PALEBI_SERVICEOPERATIONID
					and SO.DATAAREAID = SOR.DATAAREAID
				--OPERAZIONE SERVICE

				--RESPONSABILE
				left join HCMWORKER												HCMOWNER
					on HCMOWNER.RECID = SOR.OWNERWORKER

						left join DIRPARTYTABLE									DPOWNER
							on DPOWNER.RECID = HCMOWNER.PERSON
				--RESPONSABILE

				--RESPONSABILE ESTERNO
				left  join CONTACTPERSON											CTPEXTWRK
					on CTPEXTWRK.CONTACTPERSONID = SOR.EXTERNALWORKERCONTACTID
					and CTPEXTWRK.DATAAREAID = SOR.DATAAREAID

						left join DIRPARTYTABLE									DPEXTWRK
							on DPEXTWRK.RECID = CTPEXTWRK.PARTY 
				--RESPONSABILE ESTERNO

				--CONTATTO CLIENTE
				left  join CONTACTPERSON											CTPCUST
					on CTPCUST.CONTACTPERSONID = SOR.CONTACTPERSONID
					and CTPCUST.DATAAREAID = SOR.DATAAREAID

						left join DIRPARTYTABLE									DPCTPCUST
							on DPCTPCUST.RECID = CTPCUST.PARTY 
				--CONTATTO CLIENTE

				--CLIENTE
				left join DIRPARTYTABLE											DPCUST
					on DPCUST.RECID = SOR.PARTY
				--CLIENTE
				--HEADER

				--ROWS
				--DAYS
				left join PALEBI_SERVICEOPERATIONJOURNALDAY						            SOJD
					on SOJD.PALEBI_SERVICEOPERATIONID = SOR.PALEBI_SERVICEOPERATIONID
					AND SOJD.DATAAREAID = SOR.DATAAREAID
					AND SOJD.PARTITION = SOR.PARTITION
				left join PALGUL_SERVICEOPERATIONJOURNALACTPICS							PGSOJP
					on PGSOJP.SERVICEOPERATIONJOURNALACTREFRECID = SOJD.SERVICEOPERATIONJOURNALWEEKREFRECID
					and PGSOJP.DATAAREAID = SOJD.DATAAREAID
					and PGSOJP.PARTITION = SOJD.PARTITION
				--DAYS
				--ROWS
				LEFT JOIN PALDC_PGS_LANGUAGES									LAN
					ON LAN.RECID = SO.PALDC_PGS_LANGUAGE
				LEFT JOIN PALEBI_SERVICEOPERATIONDAYDESCRIPTION						   SOJDD
					ON SOJD.PALEBI_SERVICEOPERATIONDAYDESCRRECID = SOJDD.RECID
				where SOR.RECID = 5637324577 --@recId  RecId Of PALEBI_SERVICEOPERATIONREVISION
				order by SOJD.TRANSDATE

---- SUPERVISOR

--SELECT 
--CASE
--WHEN D.CLIENTSUPERVISOR = '' OR (len(D.CLIENTSUPERVISOR)  - len(replace(D.CLIENTSUPERVISOR , '-', '')) <> 1) THEN ''
--WHEN D.CLIENTSUPERVISOR LIKE '520-%' THEN 
--								(SELECT TOP 1 A.FirstName + ' ' + A.LastName
--								 FROM DirPartyLookupGridView A 
--								 INNER JOIN CONTACTPERSON B
--								 ON A.PARTY = B.PARTY
--								 AND A.PARTITION = B.PARTITION
--								 AND GETDATE() >= A.ValidFrom
--								 AND GETDATE() >= A.AddressValidFrom
--								 AND GETDATE() <= A.ValidTo
--								 AND GETDATE() <= A.AddressValidTo
--								 WHERE A.RECID = REPLACE(D.CLIENTSUPERVISOR,'520-',''))

--WHEN D.CLIENTSUPERVISOR LIKE '106669-%' THEN 
--								(SELECT TOP 1 A.Name + ' ' + A.LastName
--								 FROM PALGUL_FieldContacts A 
--								 WHERE A.RECID = REPLACE(D.CLIENTSUPERVISOR,'106669-',''))
--								 END as s
--from PALEBI_SERVICEOPERATION D
--where PALEBI_SERVICEOPERATIONID = 'SO21-00006'

---- SIGNATURES

--SELECT*
--FROM PALGUL_SERVICEOPERATIONJOURNALACTPICS