return: 'numeric[]'
lang: plpgsql
parameters:
  p0:
    type: numeric
    name: montant_fact
  p1:
    type: 'bigint[]'
    name: oid_array
  p2:
    type: 'numeric[]'
    name: montant_reglement_array
strict: STRICT
src: |
  DECLARE
  	i integer;
  	i1 integer;
  	i2 integer;
  	i3 integer;
  	l integer;
  	n integer;
  	new_montant_fact numeric;
  	new_montant_reglement_array numeric[500][3];
  	signe_array_1 numeric[];
  	signe_array_2 numeric[];
  	signe_array_3 numeric[];
  BEGIN
  	i = 1;
  	l = split_part(split_part(split_part(array_dims(oid_array),':',2),'[',1),']',1)::int;
  	FOR i IN 1..l LOOP
  		signe_array_1[i] = 1;
  		signe_array_2[i] = 1;
  		signe_array_3[i] = 1;
  	END LOOP;
  	FOR i1 IN 1..l LOOP
  		signe_array_1[l - i1 + 1] = 0 - signe_array_1[l - i1 + 1];
  		new_montant_fact = 0;
  		new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0]];
  		n = 0;
  		FOR i IN 1..l LOOP
  			IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  				n  = n  + 1;
  				new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  				new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  			END IF;
  			new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  		END LOOP;
  		
  		IF new_montant_fact = montant_fact THEN
  			return new_montant_reglement_array;
  		END IF;
  		FOR i2 IN 1..l LOOP
  			signe_array_2[l - i2 + 1] = 0 - signe_array_2[l - i2 + 1];
  			new_montant_fact = 0;
  			new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0]];
  			n = 0;
  			FOR i IN 1..l LOOP
  				IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  					n  = n  + 1;
  					new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  					new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  				END IF;
  				new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  			END LOOP;
  			
  			IF new_montant_fact = montant_fact THEN
  				return new_montant_reglement_array;
  			END IF;
  			
  			FOR i3 IN 1..l LOOP
  				signe_array_2[l - i3 + 1] = 0 - signe_array_2[l - i3 + 1];
  				new_montant_fact = 0;
  				new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  					array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  					array[0,0]];
  				n = 0;
  				FOR i IN 1..l LOOP
  					IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  						n  = n  + 1;
  						new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  						new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  					END IF;
  					new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  				END LOOP;
  				
  				IF new_montant_fact = montant_fact THEN
  					return new_montant_reglement_array;
  				END IF;
  					
  			END LOOP;
  				
  		END LOOP;
  			
  	END LOOP;
  	FOR i IN 1..l LOOP
  		signe_array_1[i] = 1;
  		signe_array_2[i] = 1;
  		signe_array_3[i] = 1;
  	END LOOP;
  	FOR i1 IN 1..l LOOP
  		FOR i IN 1..l LOOP
  			signe_array_1[i] = 1;
  		END LOOP;
  		signe_array_1[l - i1 + 1] = 0;
  		new_montant_fact = 0;
  		new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0]];
  		n = 0;
  		FOR i IN 1..l LOOP
  			IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  				n  = n  + 1;
  				new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  				new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  			END IF;
  			new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  		END LOOP;
  		
  		IF new_montant_fact = montant_fact THEN
  			return new_montant_reglement_array;
  		END IF;
  		FOR i2 IN 1..l LOOP
  			
  			FOR i IN 1..l LOOP
  				signe_array_2[i] = 1;
  			END LOOP;
  			signe_array_2[l - i2 + 1] = 0;
  			new_montant_fact = 0;
  			new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0]];
  			n = 0;
  			FOR i IN 1..l LOOP
  				IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  					n  = n  + 1;
  					new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  					new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  				END IF;
  				new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  			END LOOP;
  			
  			IF new_montant_fact = montant_fact THEN
  				return new_montant_reglement_array;
  			END IF;
  			
  			FOR i3 IN 1..l LOOP
  				FOR i IN 1..l LOOP
  					signe_array_3[i] = 1;
  				END LOOP;
  				signe_array_3[l - i3 + 1] = 0;
  				new_montant_fact = 0;
  				new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  						array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  						array[0,0]];
  				n = 0;
  				FOR i IN 1..l LOOP
  					IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  						n  = n  + 1;
  						new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  						new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  					END IF;
  					new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  				END LOOP;
  				
  				IF new_montant_fact = montant_fact THEN
  					return new_montant_reglement_array;
  				END IF;
  					
  			END LOOP;
  				
  		END LOOP;
  			
  	END LOOP;
  	FOR i IN 1..l LOOP
  		signe_array_1[i] = 1;
  		signe_array_2[i] = 1;
  		signe_array_3[i] = 1;
  	END LOOP;
  	FOR i1 IN 1..l LOOP
  		signe_array_1[l - i1 + 1] = 0 - signe_array_1[l - i1 + 1];
  		new_montant_fact = 0;
  		new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  				array[0,0]];
  		n = 0;
  		FOR i IN 1..l LOOP
  			IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  				n  = n  + 1;
  				new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  				new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  			END IF;
  			new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  		END LOOP;
  		
  		IF new_montant_fact = 0 THEN
  			return new_montant_reglement_array;
  		END IF;
  		FOR i2 IN 1..l LOOP
  			signe_array_2[l - i2 + 1] = 0 - signe_array_2[l - i2 + 1];
  			new_montant_fact = 0;
  			new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  					array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  					array[0,0]];
  			n = 0;
  			FOR i IN 1..l LOOP
  				IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  					n  = n  + 1;
  					new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  					new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  				END IF;
  				new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  			END LOOP;
  			
  			IF new_montant_fact = 0 THEN
  				return new_montant_reglement_array;
  			END IF;
  			
  			FOR i3 IN 1..l LOOP
  				signe_array_2[l - i3 + 1] = 0 - signe_array_2[l - i3 + 1];
  				new_montant_fact = 0;
  				new_montant_reglement_array = array[array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  						array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],array[0,0],
  						array[0,0]];
  				n = 0;
  				FOR i IN 1..l LOOP
  					IF signe_array_1[i] * signe_array_2[i] * signe_array_3[i] <> 1 THEN
  						n  = n  + 1;
  						new_montant_reglement_array[n][1] = oid_array[i]::numeric;
  						new_montant_reglement_array[n][2] = montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  					END IF;
  					new_montant_fact = new_montant_fact + montant_reglement_array[i] * signe_array_1[i] * signe_array_2[i] * signe_array_3[i];
  				END LOOP;
  				
  				IF new_montant_fact = 0 THEN
  					return new_montant_reglement_array;
  				END IF;
  					
  			END LOOP;
  				
  		END LOOP;
  			
  	END LOOP;
  	return null;
  END;