diff --git a/sql/upgrade.sql b/sql/upgrade.sql index 51a2dc965..cea027c0a 100644 --- a/sql/upgrade.sql +++ b/sql/upgrade.sql @@ -28,3 +28,74 @@ end; $$ LANGUAGE plpgsql; +-- add quick code for contact + +insert into attr_min (frd_id,ad_id) + select distinct 16,23 + from attr_min + where not exists (select * from attr_min where ad_id=23 and frd_id=16); + + +insert into jnt_fic_attr (fd_id,ad_id) + select fd_id,23 from fiche_def where frd_id=16 + and not exists (select * + from jnt_fic_attr join fiche_def using (fd_id) + where frd_id=16 and ad_id=23); + + +CREATE or replace FUNCTION update_quick_code(njft_id integer, tav_text text) RETURNS integer + AS $$ + declare + ns integer; + nExist integer; + tText text; + old_qcode varchar; + begin + -- get current value + select av_text into old_qcode from attr_value where jft_id=njft_id; + -- av_text didn't change so no update + if tav_text = upper( trim(old_qcode)) then + return 0; + end if; + + tText := trim(upper(tav_text)); + tText := replace(tText,' ',''); + if length ( tText) = 0 or tText is null then + return 0; + end if; + + ns := njft_id; + + loop + -- av_text already used ? + select count(*) into nExist + from jnt_fic_att_value join attr_value using (jft_id) + where + ad_id=23 and av_text=tText; + + if nExist = 0 then + exit; + end if; + if tText = 'FID'||ns then + -- take the next sequence + select nextval('s_jnt_fic_att_value') into ns; + end if; + tText :='FID'||ns; + + end loop; + update attr_value set av_text = tText where jft_id=njft_id; + + -- update also the contact + update attr_value set av_text = tText + where jft_id in + ( select jft_id + from jnt_fic_att_value join attr_value using (jft_id) + where ad_id=25 and av_text=old_qcode); + + + update jrnx set j_qcode=tText where j_qcode = old_qcode; + return ns; + end; +$$ + LANGUAGE plpgsql; +