 ALTER TABLE insurances DISABLE TRIGGER trg_trk_log_table_insurances;
 ALTER TABLE insurances DISABLE TRIGGER fct_cpy_trg_del_dict_insurances;
 ALTER TABLE insurances DISABLE TRIGGER fct_cpy_trg_ins_update_dict_insurances;
-
--- CREATE TEMPORARY TABLE partsSplitFromAndTo ("start" boolean not null default true, "id" integer not null);
 
 CREATE OR REPLACE FUNCTION convert_to_real(v_input varchar) RETURNS REAL IMMUTABLE
 AS $$

end;
$$;

create or replace function createNewPart(IN part_id specimen_parts.id%TYPE, IN recPartsDetails recPartsDetail) RETURNS specimen_parts.id%TYPE language plpgsql AS
$$
DECLARE
  new_part_id specimen_parts.id%TYPE;

      INSERT INTO codes
      (referenced_relation, record_id, code_category, code_prefix, code_prefix_separator, code, code_suffix, code_suffix_separator, code_date, code_date_mask)
      (SELECT 'specimen_parts', new_part_id, code_category, code_prefix, code_prefix_separator, code, code_suffix, code_suffix_separator, code_date, code_date_mask
       FROM codes
       WHERE referenced_relation = 'specimen_parts'
         AND record_id = part_id
         AND code_category = 'main'
         AND (full_code_order_by IN (recPartsDetails.main_code, recPartsDetails.rbins_code, recPartsDetails.inventory_code, recPartsDetails.batch_main_code)
              OR
              CASE WHEN coalesce(new_code_id,0) = 0 THEN true ELSE id = new_code_id END
             )
      );
      GET DIAGNOSTICS code_count = ROW_COUNT;
      IF code_count = 0 THEN
