<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="RH000030"
	label="Chiffrier RH-Planning" 
	title="Chiffrier RH-Planning" 
	database="iCTI" 
	swf="*CTI_view1" 
	softCode="iCTI_rh">
	<QUERIES>
		<QUERY label=""> 
			<SQL select="
				
				DROP TABLE IF EXISTS w_etablissements
				;
				CREATE TEMP TABLE w_etablissements AS
					SELECT lpad(t_entreprises.code,3,'0') || lpad(t_etablissements.code,3,'0') AS code_original
					FROM rh.t_etablissements
					JOIN rh.t_entreprises ON entreprise_id = t_entreprises.oid
					WHERE t_etablissements.oid IN (SELECT etablissement_id FROM rh.p_contrats)
				;

				DROP TABLE IF EXISTS w_salaries
				;
				CREATE TEMP TABLE w_salaries AS
					SELECT 
						p_salaries.oid AS salarie_id,
						p_salaries.matricule,
						p_salaries.nom || ' ' || p_salaries.prenom AS nom,
						lpad(t_entreprises.code,3,'0') ||
						lpad(t_etablissements.code,3,'0') ||
						p_salaries.matricule AS matricule_planning
					FROM rh.p_salaries
					JOIN rh.p_contrats ON salarie_id = p_salaries.oid
					JOIN rh.t_entreprises ON entreprise_id = t_entreprises.oid
					JOIN rh.t_etablissements ON etablissement_id = t_etablissements.oid
					GROUP BY 1,2,3,4
					ORDER BY 1,2,3,4
				;


				DROP TABLE IF EXISTS w_CPTRES1
				;
				
				CREATE TEMP TABLE w_CPTRES1 AS SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2010 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2011 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2012 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2013 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2014 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2015 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2016 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2017 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2018 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2019 CPTRES1 GROUP BY 1,2;
				INSERT INTO w_CPTRES1 SELECT CPTRES1.PERS_MAT, NIV_COD1 FROM prod_octime.CPTRES1_2020 CPTRES1 GROUP BY 1,2;
				

					SELECT
						CPTRES1.PERS_MAT,
						PERS_NOM || ' ' || PERS_PRE AS PERS_NOM,
						'Manque dans la paie'
					FROM w_CPTRES1 CPTRES1
						JOIN prod_octime.PERS ON CPTRES1.PERS_MAT = PERS.PERS_MAT
						LEFT JOIN w_etablissements ON w_etablissements.code_original = CPTRES1.NIV_COD1
					WHERE 1=1
						AND CPTRES1.PERS_MAT NOT IN (SELECT matricule_planning FROM w_salaries)
						AND CPTRES1.PERS_MAT NOT IN (SELECT matricule FROM w_salaries)
					GROUP BY 1,2
				UNION
					SELECT
						w_salaries.matricule,
						w_salaries.nom,
						'Manque dans OCTIME'
					FROM w_salaries
						JOIN rh.p_contrats ON w_salaries.salarie_id = p_contrats.salarie_id
					WHERE 1=1
						AND matricule_planning NOT IN (
							SELECT PERS_MAT 
							FROM w_CPTRES1 CPTRES1
								LEFT JOIN w_etablissements ON w_etablissements.code_original = CPTRES1.NIV_COD1)
						AND matricule NOT IN (
							SELECT PERS_MAT 
							FROM w_CPTRES1 CPTRES1
								LEFT JOIN w_etablissements ON w_etablissements.code_original = CPTRES1.NIV_COD1)
						AND p_contrats.date_fin >= (SELECT date(MIN(CAL_DAT)) FROM prod_octime.CPTRES1)
					GROUP BY 1,2
				ORDER BY 1

				"
				 >
				<FIELDS>
					<FIELD name="MAT" />
					<FIELD name="NOM" />
					<FIELD name="ERR" />
				</FIELDS>
			</SQL>
		</QUERY> 
	</QUERIES>
	<PRESENTATION>
		<ONGLET>
			<DATAGRID title="" total="false" sortable="true" headerHeight="40" >
				<ROWSTYLE name="fontSize" value="12" />
				<COLUMN dataField="MAT"
					width="120"
					type="Char"
					visible="true"
					headerText="Matricule" 
					textAlign="left">
				</COLUMN>
				<COLUMN dataField="NOM"
					minWidth="200"
					type="Char"
					visible="true"
					headerText="Nom" 
					textAlign="left">
				</COLUMN>
				<COLUMN dataField="ERR"
					minWidth="200"
					type="Char"
					visible="true"
					headerText="Erreur" 
					textAlign="left">
				</COLUMN>
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>
