Delimiter //
/**

**/

drop function if exists CASH_BUSINESS_GET_ENTRIES_STATUT_CODE //

Create Function CASH_BUSINESS_GET_ENTRIES_STATUT_CODE(
		accounts_ref varchar(40)
		, entries_acc_ext_ref varchar(45)
		, id_entities Int unsigned
	)
	returns varchar(5)
Begin
	return (
			Select 
				STT.DF_ENTRIES_STATUT_CODE
			From ENTRIES ENT
				Inner Join ENTRIES_has_ACCOUNTS EHA
					on ENT.ID_ENTRIES = EHA.ENTRIES_ID_ENTRIES
                    AND ENTRIES_PERMANENT_ATTACHEMENT = 1
				Inner join ACCOUNTS ACC
					On ACC.ID_ACCOUNTS = EHA.ACCOUNTS_ID_ACCOUNTS
				Inner join DF_ENTRIES_STATUT STT
					On STT.ID_DF_ENTRIES_STATUT = ENT.DF_ENTR_STT_ID_DF_ENTR_STT
			Where
				ACC.ACCOUNTS_REF = accounts_ref
				And EHA.ENTRIES_ACC_EXT_REF = entries_acc_ext_ref
				And ACC.ENTITIES_ID_ENTITIES = id_entities
			Limit 1
		)
	;
End //
Delimiter ;