             ) as x;
         IF code_count > 0 THEN
           RAISE NOTICE '+ Need of new part creation';
-          SELECT createNewPart(part_id, recPartsDetails, recFirstPart) INTO new_part_id;
+          SELECT createNewPart(part_id, recPartsDetails) INTO new_part_id;
           IF new_part_id < 0 THEN
             return false;
           END IF;

          WHERE referenced_relation = 'specimen_parts'
            AND record_id = new_part_id;
          RAISE NOTICE '+++ Insurances after transfert for new part: %', recInsurances;
        END IF;
      ELSE
        RAISE NOTICE '+ New code creation for next part';

                            and modification_date_time = recPartsDetails.maintenance_modification_date_time
                            and modification_date_mask = recPartsDetails.maintenance_modification_date_mask
                       )
        and not exists (
                          select 1
                          from users_tracking
                          where referenced_relation = 'collection_maintenance'
                            and (action in ('delete', 'update')
                                  and old_value -> 'referenced_relation' = 'specimen_parts'
                                  and old_value -> 'record_id' = part_id::varchar
                                  and old_value -> 'people_ref' = recPartsDetails.maintenance_people_ref::varchar
                                  and old_value -> 'category' = recPartsDetails.maintenance_category::varchar
                                  and old_value -> 'action_observation' = recPartsDetails.maintenance_action_observation::varchar
                                  and old_value -> 'modification_date_time' = recPartsDetails.maintenance_modification_date_time::varchar
                                  and old_value -> 'modification_date_mask' = recPartsDetails.maintenance_modification_date_mask::varchar
                                )
                            or (action in ('delete', 'update')
                                  and new_value -> 'referenced_relation' = 'specimen_parts'
                                  and new_value -> 'record_id' = part_id::varchar
                                  and new_value -> 'people_ref' = recPartsDetails.maintenance_people_ref::varchar
                                  and new_value -> 'category' = recPartsDetails.maintenance_category::varchar
                                  and new_value -> 'action_observation' = recPartsDetails.maintenance_action_observation::varchar
                                  and new_value -> 'modification_date_time' = recPartsDetails.maintenance_modification_date_time::varchar
                                  and new_value -> 'modification_date_mask' = recPartsDetails.maintenance_modification_date_mask::varchar
                               )
                       )
    );
  END IF;
  /*Length level*/
  IF coalesce(recPartsDetails.old_length_min, '') != '' OR coalesce(recPartsDetails.old_length_max, '') != '' THEN
    SELECT id INTO cat_prop_id
    FROM catalogue_properties as cp
    WHERE referenced_relation = 'specimen_parts'
      AND record_id = part_id
      AND property_type = 'physical measurement'
      AND property_sub_type = 'length'
      AND property_qualifier NOT IN ('height','depth')
      AND coalesce(property_method, '') = ''
      AND coalesce(property_tool, '') = ''
      AND date_from = '0001-01-01 00:00:00'
      AND date_to = '2038-12-31 00:00:00'
      AND NOT EXISTS (SELECT 1 FROM users_tracking WHERE referenced_relation = 'catalogue_properties' AND record_id = cp.id AND action IN ('insert','update'));
    IF coalesce(cat_prop_id, 0) = 0 THEN
      SELECT NOT EXISTS(SELECT 1
                        FROM users_tracking
                        WHERE referenced_relation = 'catalogue_properties'
                          AND action = 'delete'
                          AND old_value -> 'referenced_relation' = 'specimen_parts'
                          AND old_value -> 'record_id' = part_id::varchar
                          AND old_value -> 'property_type' = 'physical measurement'
                          AND old_value -> 'property_sub_type' = 'length'
                          AND old_value -> 'property_qualifier' NOT IN ('height','depth')
                          AND coalesce(old_value -> 'property_method', '') = ''
                          AND coalesce(old_value -> 'property_tool', '') = ''
                          AND old_value -> 'date_from' = '0001-01-01 00:00:00'
                          AND old_value -> 'date_to' = '2038-12-31 00:00:00'
                       ) INTO booContinue;
    END IF;
    IF booContinue THEN
      SELECT NOT EXISTS ( SELECT 1
                          FROM properties_values as pv
                          WHERE pv.property_ref = cat_prop_id
                            AND EXISTS (SELECT 1 FROM users_tracking WHERE referenced_relation = 'properties_values' AND record_id = pv.id AND action IN ('insert','update'))
                        )
        AND NOT EXISTS ( SELECT 1
                         FROM users_tracking
                         WHERE referenced_relation = 'properties_values'
                           AND action = 'delete'
                           AND coalesce(old_value -> 'property_ref', '') = (coalesce(cat_prop_id,0))::varchar
                       )
      INTO booContinue;
      IF booContinue THEN
        DELETE FROM catalogue_properties
        WHERE id = cat_prop_id;
        insert into catalogue_properties (referenced_relation, record_id, property_type, property_sub_type, property_qualifier, property_unit, property_accuracy_unit)
        (
          select 'specimen_parts', part_id, 'physical measurement', 'length', '', recPartsDetails.old_length_unit, recPartsDetails.old_length_unit
        )
        returning id INTO cat_prop_id;
        IF cat_prop_id != 0 THEN
          insert into properties_values (property_ref, property_value)
          (
            select cat_prop_id, recPartsDetails.old_length_min
            where coalesce(recPartsDetails.old_length_min, '') != ''
            union
            select cat_prop_id, recPartsDetails.old_length_max
            where coalesce(recPartsDetails.old_length_max, '') != ''
          );
        END IF;
      END IF;
    END IF;
    cat_prop_id := 0;
    booContinue := true;
  END IF;
  /*Height level*/
  IF coalesce(recPartsDetails.old_height_min, '') != '' OR coalesce(recPartsDetails.old_height_max, '') != '' THEN
    SELECT id INTO cat_prop_id
    FROM catalogue_properties as cp
    WHERE referenced_relation = 'specimen_parts'
      AND record_id = part_id
      AND property_type = 'physical measurement'
      AND property_sub_type = 'length'
      AND property_qualifier = 'height'
      AND coalesce(property_method, '') = ''
      AND coalesce(property_tool, '') = ''
      AND date_from = '0001-01-01 00:00:00'
      AND date_to = '2038-12-31 00:00:00'
      AND NOT EXISTS (SELECT 1 FROM users_tracking WHERE referenced_relation = 'catalogue_properties' AND record_id = cp.id AND action IN ('insert','update'));
    IF coalesce(cat_prop_id, 0) = 0 THEN
      SELECT NOT EXISTS(SELECT 1
                        FROM users_tracking
                        WHERE referenced_relation = 'catalogue_properties'
                          AND action = 'delete'
                          AND old_value -> 'referenced_relation' = 'specimen_parts'
                          AND old_value -> 'record_id' = part_id::varchar
                          AND old_value -> 'property_type' = 'physical measurement'
                          AND old_value -> 'property_sub_type' = 'length'
                          AND old_value -> 'property_qualifier' = 'height'
                          AND coalesce(old_value -> 'property_method', '') = ''
                          AND coalesce(old_value -> 'property_tool', '') = ''
                          AND old_value -> 'date_from' = '0001-01-01 00:00:00'
                          AND old_value -> 'date_to' = '2038-12-31 00:00:00'
                       ) INTO booContinue;
    END IF;
    IF booContinue THEN
      SELECT NOT EXISTS ( SELECT 1
                          FROM properties_values as pv
                          WHERE pv.property_ref = cat_prop_id
                            AND EXISTS (SELECT 1 FROM users_tracking WHERE referenced_relation = 'properties_values' AND record_id = pv.id AND action IN ('insert','update'))
                        )
        AND NOT EXISTS ( SELECT 1
                         FROM users_tracking
                         WHERE referenced_relation = 'properties_values'
                           AND action = 'delete'
                           AND coalesce(old_value -> 'property_ref', '') = (coalesce(cat_prop_id,0))::varchar
                       )
      INTO booContinue;
      IF booContinue THEN
        DELETE FROM catalogue_properties
        WHERE id = cat_prop_id;
        insert into catalogue_properties (referenced_relation, record_id, property_type, property_sub_type, property_qualifier, property_unit, property_accuracy_unit)
        (
          select 'specimen_parts', part_id, 'physical measurement', 'length', 'height', recPartsDetails.old_height_unit, recPartsDetails.old_height_unit
        )
        returning id INTO cat_prop_id;
        IF cat_prop_id != 0 THEN
          insert into properties_values (property_ref, property_value)
          (
            select cat_prop_id, recPartsDetails.old_height_min
            where coalesce(recPartsDetails.old_height_min, '') != ''
            union
            select cat_prop_id, recPartsDetails.old_height_max
            where coalesce(recPartsDetails.old_height_max, '') != ''
          );
        END IF;
      END IF;
    END IF;
    cat_prop_id := 0;
    booContinue := true;
  END IF;
  /*Depth level*/
  IF coalesce(recPartsDetails.old_depth_min, '') != '' OR coalesce(recPartsDetails.old_depth_max, '') != '' THEN
    SELECT id INTO cat_prop_id
    FROM catalogue_properties as cp
    WHERE referenced_relation = 'specimen_parts'
      AND record_id = part_id
      AND property_type = 'physical measurement'
      AND property_sub_type = 'length'
      AND property_qualifier = 'depth'
      AND coalesce(property_method, '') = ''
      AND coalesce(property_tool, '') = ''
      AND date_from = '0001-01-01 00:00:00'
      AND date_to = '2038-12-31 00:00:00'
      AND NOT EXISTS (SELECT 1 FROM users_tracking WHERE referenced_relation = 'catalogue_properties' AND record_id = cp.id AND action IN ('insert','update'));
    IF coalesce(cat_prop_id, 0) = 0 THEN
      SELECT NOT EXISTS(SELECT 1
                        FROM users_tracking
                        WHERE referenced_relation = 'catalogue_properties'
                          AND action = 'delete'
                          AND old_value -> 'referenced_relation' = 'specimen_parts'
                          AND old_value -> 'record_id' = part_id::varchar
                          AND old_value -> 'property_type' = 'physical measurement'
                          AND old_value -> 'property_sub_type' = 'length'
                          AND old_value -> 'property_qualifier' = 'depth'
                          AND coalesce(old_value -> 'property_method', '') = ''
                          AND coalesce(old_value -> 'property_tool', '') = ''
                          AND old_value -> 'date_from' = '0001-01-01 00:00:00'
                          AND old_value -> 'date_to' = '2038-12-31 00:00:00'
                       ) INTO booContinue;
    END IF;
    IF booContinue THEN
      SELECT NOT EXISTS ( SELECT 1
                          FROM properties_values as pv
                          WHERE pv.property_ref = cat_prop_id
                            AND EXISTS (SELECT 1 FROM users_tracking WHERE referenced_relation = 'properties_values' AND record_id = pv.id AND action IN ('insert','update'))
                        )
        AND NOT EXISTS ( SELECT 1
                         FROM users_tracking
                         WHERE referenced_relation = 'properties_values'
                           AND action = 'delete'
                           AND coalesce(old_value -> 'property_ref', '') = (coalesce(cat_prop_id,0))::varchar
                       )
      INTO booContinue;
      IF booContinue THEN
        DELETE FROM catalogue_properties
        WHERE id = cat_prop_id;
        insert into catalogue_properties (referenced_relation, record_id, property_type, property_sub_type, property_qualifier, property_unit, property_accuracy_unit)
        (
          select 'specimen_parts', part_id, 'physical measurement', 'length', 'depth', recPartsDetails.old_depth_unit, recPartsDetails.old_depth_unit
        )
        returning id INTO cat_prop_id;
        IF cat_prop_id != 0 THEN
          insert into properties_values (property_ref, property_value)
          (
            select cat_prop_id, recPartsDetails.old_depth_min
            where coalesce(recPartsDetails.old_depth_min, '') != ''
            union
            select cat_prop_id, recPartsDetails.old_depth_max
            where coalesce(recPartsDetails.old_depth_max, '') != ''
          );
        END IF;
      END IF;
    END IF;
    cat_prop_id := 0;
    booContinue := true;
  END IF;
  /*Weight level*/
  IF coalesce(recPartsDetails.old_weight_min, '') != '' OR coalesce(recPartsDetails.old_weight_max, '') != '' THEN
    SELECT id INTO cat_prop_id
    FROM catalogue_properties as cp
    WHERE referenced_relation = 'specimen_parts'
      AND record_id = part_id
      AND property_type = 'physical measurement'
      AND property_sub_type = 'weight'
      AND coalesce(property_method, '') = ''
      AND coalesce(property_tool, '') = ''
      AND date_from = '0001-01-01 00:00:00'
      AND date_to = '2038-12-31 00:00:00'
      AND NOT EXISTS (SELECT 1 FROM users_tracking WHERE referenced_relation = 'catalogue_properties' AND record_id = cp.id AND action IN ('insert','update'));
    IF coalesce(cat_prop_id, 0) = 0 THEN
      SELECT NOT EXISTS(SELECT 1
                        FROM users_tracking
                        WHERE referenced_relation = 'catalogue_properties'
                          AND action = 'delete'
                          AND old_value -> 'referenced_relation' = 'specimen_parts'
                          AND old_value -> 'record_id' = part_id::varchar
                          AND old_value -> 'property_type' = 'physical measurement'
                          AND old_value -> 'property_sub_type' = 'weight'
                          AND coalesce(old_value -> 'property_method', '') = ''
                          AND coalesce(old_value -> 'property_tool', '') = ''
                          AND old_value -> 'date_from' = '0001-01-01 00:00:00'
                          AND old_value -> 'date_to' = '2038-12-31 00:00:00'
                       ) INTO booContinue;
    END IF;
    IF booContinue THEN
      SELECT NOT EXISTS ( SELECT 1
                          FROM properties_values as pv
                          WHERE pv.property_ref = cat_prop_id
                            AND EXISTS (SELECT 1 FROM users_tracking WHERE referenced_relation = 'properties_values' AND record_id = pv.id AND action IN ('insert','update'))
                        )
        AND NOT EXISTS ( SELECT 1
                         FROM users_tracking
                         WHERE referenced_relation = 'properties_values'
                           AND action = 'delete'
                           AND coalesce(old_value -> 'property_ref', '') = (coalesce(cat_prop_id,0))::varchar
                       )
      INTO booContinue;
      IF booContinue THEN
        DELETE FROM catalogue_properties
        WHERE id = cat_prop_id;
        insert into catalogue_properties (referenced_relation, record_id, property_type, property_sub_type, property_qualifier, property_unit, property_accuracy_unit)
        (
          select 'specimen_parts', part_id, 'physical measurement', 'weight', '', recPartsDetails.old_weight_unit, recPartsDetails.old_weight_unit
        )
        returning id INTO cat_prop_id;
        IF cat_prop_id != 0 THEN
          insert into properties_values (property_ref, property_value)
          (
            select cat_prop_id, recPartsDetails.old_weight_min
            where coalesce(recPartsDetails.old_weight_min, '') != ''
            union
            select cat_prop_id, recPartsDetails.old_weight_max
            where coalesce(recPartsDetails.old_weight_max, '') != ''
          );
        END IF;
      END IF;
    END IF;
    cat_prop_id := 0;
    booContinue := true;
  END IF;
  return 1;
exception
