<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="ACTI000007" 
	label="Activité. Export écritures d'encours" 
	title="= 'Export écritures d'encours (' + SELECT.TOPERIODE_LABEL + ')'" 
	database="iCTI" 
	swf="*CTI_view1" 
	softCode="iCTI_activite"
	globals="ACTI_globals.XML"
	componentsFile="ACTI_components.XML"
    helpFile="iCTI_ACTI_ACTI000007.HTML"
    helpDokuWIkiDir="activite:comptabilisation_et_en_cours:export" 
    helpDokuWIkiFile="acti000007" 
>    
	<CACHE cachable="false" />
	<VIEWPROPERTIES>
		<PROPERTYx dataLink="VIEWPROPERTIES" nameField="name" valueField="value" />
	</VIEWPROPERTIES>
	
	<SELECTIONS label="Paramètres de l'analyse" displayText="= var display:String = '';
						
						return display;
						">
		<GROUP label="Période comptable">
			<FIELDx name="PERIODE" label="de" UI="combo" tree="true" width="200" default="2015-12-01" globalListProvider="DATE_DEBUT">
			</FIELDx>
			<FIELD name="TOPERIODE" label="" UI="combo" tree="true" width="200" default="2015-12-31" globalListProvider="DATE_FIN">
			</FIELD>
		</GROUP>
		
	</SELECTIONS>
		

	<QUERIES>
		<QUERY label="Ecritures"> 
			<SQL>
				<select><![CDATA[
				
					INSERT INTO activite.t_divers (code, texte, valeur, description)
					SELECT
						'ENCOURSEXPORT_ENTETE',
						'Export comptable encours. Entete fichier',
						'',
						'Export comptable encours. Entete fichier'
					WHERE 'ENCOURSEXPORT_ENTETE' NOT IN (SELECT code FROM activite.t_divers);
                    
					INSERT INTO activite.t_divers (code, texte, valeur, description)
					SELECT
						'ENCOURSEXPORT_LIGNE',
						'Export comptable encours. Ligne fichier',
						'',
						'Export comptable encours. Ligne fichier. ([CPT]=Compte,[CRE]=Crédit,[DEB]=Débit'
					WHERE 'ENCOURSEXPORT_LIGNE' NOT IN (SELECT code FROM activite.t_divers);
                    
					
					INSERT INTO activite.t_divers (code, texte, valeur, description)
					SELECT
						'ENCOURSEXPORT_CONTRESOR',
						'Export comptable encours. Compte de contrepartie encours sortis',
						'',
						'Export comptable encours. Compte de contrepartie encours sortis'
					WHERE 'ENCOURSEXPORT_CONTRESOR' NOT IN (SELECT code FROM activite.t_divers);
                    
					INSERT INTO activite.t_divers (code, texte, valeur, description)
					SELECT
						'ENCOURSEXPORT_CONTREPRE',
						'Export comptable encours. Compte de contrepartie encours présents',
						'',
						'Export comptable encours. Compte de contrepartie encours présents'
					WHERE 'ENCOURSEXPORT_CONTREPRE' NOT IN (SELECT code FROM activite.t_divers);
                    
                    
                    
                    
					DROP TABLE IF EXISTS w_encours;
					CREATE TEMP TABLE w_encours AS
					SELECT 
						compte_produit_id,
						MAX(date_comptable) AS date_comptable,
						SUM(montant_encours) AS montant_encours_ttc,
						SUM(montant_encours_sorti) AS montant_encours_sorti_ttc,
						SUM(montant_encours_present) AS montant_encours_present_ttc,
						SUM(montant_encours) AS montant_encours_ht,
						0::numeric AS montant_encours_tva,
						MAX(COALESCE(compte_tva_id,0)) AS compte_tva_id,
						MAX(COALESCE(taux_tva,0)) AS taux_tva
					FROM 
                        activite.v_factures_encours_lignes_c_2
                        JOIN activite.t_compte ON compte_produit_id = t_compte.oid
					WHERE code_origine = 'C' AND
						date_trunc('month', date_comptable) = date_trunc('month', '[TOPERIODE]'::date) 
					GROUP BY 1
					HAVING SUM(montant_encours) <> 0
					;
                    
					UPDATE w_encours SET
						montant_encours_ht = round(montant_encours_ttc / (100 + taux_tva) * 100,2)
					WHERE taux_tva <> 0
					;
                    
					UPDATE w_encours SET
						montant_encours_tva = montant_encours_ttc - montant_encours_ht
					WHERE taux_tva <> 0
					;
                    
					DROP SEQUENCE IF EXISTS w_sequence;
					CREATE TEMP SEQUENCE w_sequence;
					
					DROP TABLE IF EXISTS w_ecritures;
					CREATE TEMP TABLE w_ecritures AS
					SELECT 
						date_comptable,
						compte_code,
						nextval('w_sequence'::regclass) AS seq,
						montant_credit,
						montant_debit,
						ligne
					FROM
					(
						SELECT
							date_comptable,
							t_compte.code AS compte_code,
							SUM(CASE WHEN montant_encours_ht > 0 THEN montant_encours_ht ELSE 0 END) AS montant_credit,
							SUM(CASE WHEN montant_encours_ht < 0 THEN 0-montant_encours_ht ELSE 0 END) AS montant_debit,
							MAX(t_divers.valeur) AS ligne
						FROM w_encours
                            JOIN activite.t_compte ON compte_produit_id = t_compte.oid
                            JOIN activite.t_divers ON t_divers.code = 'ENCOURSEXPORT_LIGNE'
						GROUP BY 1,2
						ORDER BY 2
					) subview
					;	
                    
					INSERT INTO w_ecritures
					SELECT 
						date_comptable,
						compte_code,
						nextval('w_sequence'::regclass) AS seq,
						montant_credit,
						montant_debit,
						ligne
					FROM
					(
						SELECT
							date_comptable,
							COALESCE(t_compte.code,'445?????') AS compte_code,
							SUM(CASE WHEN montant_encours_tva > 0 THEN montant_encours_tva ELSE 0 END) AS montant_credit,
							SUM(CASE WHEN montant_encours_tva < 0 THEN 0-montant_encours_tva ELSE 0 END) AS montant_debit,
							MAX(t_divers.valeur) AS ligne
						FROM w_encours
                            LEFT JOIN activite.t_compte ON w_encours.compte_tva_id = t_compte.oid
                            JOIN activite.t_divers ON t_divers.code = 'ENCOURSEXPORT_LIGNE'
						WHERE montant_encours_tva <> 0
						GROUP BY 1,2
						ORDER BY 2
					) subview
					;
                    
					INSERT INTO w_ecritures
					SELECT 
						date_comptable,
						compte_code,
						nextval('w_sequence'::regclass) AS seq,
						montant_credit,
						montant_debit,
						ligne
					FROM
					(
						SELECT
							MAX(date_comptable) AS date_comptable,
							MAX(t_divers_compte.valeur) AS compte_code,
							CASE WHEN SUM(montant_encours_sorti_ttc) > 0 THEN SUM(montant_encours_sorti_ttc)  ELSE 0 END AS montant_debit,
							CASE WHEN SUM(montant_encours_sorti_ttc)  < 0 THEN 0-SUM(montant_encours_sorti_ttc)  ELSE 0 END AS montant_credit	,
							MAX(t_divers.valeur) AS ligne
						FROM w_encours
                            JOIN activite.t_compte ON w_encours.compte_tva_id = t_compte.oid
                            JOIN activite.t_divers ON t_divers.code = 'ENCOURSEXPORT_LIGNE'
                            JOIN activite.t_divers t_divers_compte ON t_divers_compte.code = 'ENCOURSEXPORT_CONTRESOR'
						WHERE montant_encours_sorti_ttc <> 0
						
					) subview
					;
                    
					INSERT INTO w_ecritures
					SELECT 
						date_comptable,
						compte_code,
						nextval('w_sequence'::regclass) AS seq,
						montant_credit,
						montant_debit,
						ligne
					FROM
					(
						SELECT
							MAX(date_comptable) AS date_comptable,
							MAX(t_divers_compte.valeur) AS compte_code,
							CASE WHEN SUM(montant_encours_present_ttc) > 0 THEN SUM(montant_encours_present_ttc)  ELSE 0 END AS montant_debit,
							CASE WHEN SUM(montant_encours_present_ttc)  < 0 THEN 0-SUM(montant_encours_present_ttc)  ELSE 0 END AS montant_credit,
							MAX(t_divers.valeur) AS ligne
						FROM w_encours
                            JOIN activite.t_compte ON w_encours.compte_tva_id = t_compte.oid
                            JOIN activite.t_divers ON t_divers.code = 'ENCOURSEXPORT_LIGNE'
                            JOIN activite.t_divers t_divers_compte ON t_divers_compte.code = 'ENCOURSEXPORT_CONTREPRE'
						WHERE montant_encours_present_ttc <> 0
						
					) subview
					;
                    
                    
                    
                    
					UPDATE w_ecritures
					SET ligne = replace(ligne,'[JJ]',to_char(date_part('day',date_comptable),'FM00'))
					;
					UPDATE w_ecritures
					SET ligne = replace(ligne,'[MM]',to_char(date_part('month',date_comptable),'FM00'))
					;
					UPDATE w_ecritures
					SET ligne = replace(ligne,'[AAAA]',date_part('year',date_comptable))
					;
					UPDATE w_ecritures
					SET ligne = replace(ligne,'[CPT]',compte_code)
					;
					UPDATE w_ecritures
					SET ligne = replace(ligne,'[SEQ]',seq)
					;
					UPDATE w_ecritures
					SET ligne = replace(ligne,'[CREDIT]',montant_credit)
					;
					UPDATE w_ecritures
					SET ligne = replace(ligne,'[DEBIT]',montant_debit)
					;
                    
					SELECT 
						ligne
					FROM w_ecritures
					ORDER BY seq;
					
				]]></select>
				<FIELDS>
					<FIELD name="LIGNE" />
				</FIELDS>
			</SQL>
		</QUERY>
		
		
	</QUERIES>
	
	<CSVFILES>
		<CSVFILE label="Ecritures" name="EcrituresEncours-CTI-[TOPERIODE]" queryNumber="0" format="TXT_DATA">
				
			<COLUMN dataField="LIGNE"	headerText="" />
			
		</CSVFILE>
		
	</CSVFILES>
	
	<PRESENTATION>
	
		
		
	</PRESENTATION>
	
</VUE>

			
