src-db/database/model/functions/AD_COPY_CLIENT.xml
changeset 1605 8a0fe0193bef
parent 1597 04435880d32a
child 1774 78e88b2cffa3
equal deleted inserted replaced
1604:a1c518e6ed93 1605:8a0fe0193bef
     1 <?xml version="1.0"?>
     1 <?xml version="1.0"?>
     2   <database name="FUNCTION AD_COPY_CLIENT">
     2   <database name="FUNCTION AD_COPY_CLIENT">
     3     <function name="AD_COPY_CLIENT" type="NULL">
     3     <function name="AD_COPY_CLIENT" type="NULL">
     4       <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
     4       <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
     5         <default/>
     5         <default/>
     6       </parameter>
     6       </parameter>
     7       <body><![CDATA[/*************************************************************************
     7       <body><![CDATA[/*************************************************************************
     8 * The contents of this file are subject to the Openbravo  Public  License
     8 * The contents of this file are subject to the Openbravo  Public  License
     9 * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
     9 * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
    32   v_Result NUMBER:=1; -- 0=failure
    32   v_Result NUMBER:=1; -- 0=failure
    33   --  Parameter
    33   --  Parameter
    34   TYPE RECORD IS REF CURSOR;
    34   TYPE RECORD IS REF CURSOR;
    35     Cur_Parameter RECORD;
    35     Cur_Parameter RECORD;
    36     --  Parameter Variables
    36     --  Parameter Variables
    37     p_AD_Client_ID NUMBER(10) ;
    37     p_AD_Client_ID VARCHAR2(32) ;
    38     p_NewClientValue VARCHAR2(80) ;
    38     p_NewClientValue VARCHAR2(80) ;
    39     --
    39     --
    40     Cur_Triggers RECORD;
    40     Cur_Triggers RECORD;
    41     Cur_Constraints RECORD;
    41     Cur_Constraints RECORD;
    42     Cur_ConstraintsEnable RECORD;
    42     Cur_ConstraintsEnable RECORD;
    49     Cur_TranslateDataSelfDep RECORD;
    49     Cur_TranslateDataSelfDep RECORD;
    50     Cur_Script RECORD;
    50     Cur_Script RECORD;
    51     CUR_v_rc_ISOPEN BOOLEAN :=false;
    51     CUR_v_rc_ISOPEN BOOLEAN :=false;
    52     --
    52     --
    53     v_seqNo NUMBER:=0;
    53     v_seqNo NUMBER:=0;
    54     v_NextNo NUMBER(10):=1000000;
    54     v_NextNo VARCHAR2(32):='1000000';
    55     v_count NUMBER:=0;
    55     v_count NUMBER:=0;
    56     v_ID_Column VARCHAR2(30) ;
    56     v_ID_Column VARCHAR2(30) ;
    57     v_minID NUMBER(10) ;
    57     v_minID varchar2(32) ;
    58     v_maxID NUMBER(10) ;
    58     v_maxID varchar2(32) ;
    59     v_nextID NUMBER(10) ;
    59     v_nextID varchar2(32) ;
    60     v_strTemp VARCHAR2(4000):='';
    60     v_strTemp VARCHAR2(4000):='';
    61     v_strSql VARCHAR2(4000):='';
    61     v_strSql VARCHAR2(4000):='';
    62     v_tableActual VARCHAR2(30) ;
    62     v_tableActual VARCHAR2(30) ;
    63     v_translatedId NUMBER:=0;
    63     v_translatedId varchar2(32):='0';
    64     v_oldId NUMBER:=0;
    64     v_oldId varchar2(32):='0';
    65     v_dynamic_select VARCHAR2(4000):='';
    65     v_dynamic_select VARCHAR2(4000):='';
    66   TYPE TYPE_Ref IS REF CURSOR;
    66   TYPE TYPE_Ref IS REF CURSOR;
    67     v_rc TYPE_REF;
    67     v_rc TYPE_REF;
    68     v_Tree_ID NUMBER;
    68     v_Tree_ID VARCHAR2(32);
    69     v_offset NUMBER;
    69     v_offset NUMBER;
    70     v_offset2 NUMBER;
    70     v_offset2 NUMBER;
    71   BEGIN
    71   BEGIN
    72     IF(p_PInstance_ID IS NOT NULL) THEN
    72     IF(p_PInstance_ID IS NOT NULL) THEN
    73       --  Update AD_PInstance
    73       --  Update AD_PInstance
    84         WHERE i.AD_PInstance_ID=p_PInstance_ID
    84         WHERE i.AD_PInstance_ID=p_PInstance_ID
    85         ORDER BY p.SeqNo
    85         ORDER BY p.SeqNo
    86         )
    86         )
    87       LOOP
    87       LOOP
    88         IF(Cur_Parameter.ParameterName='AD_Client_ID') THEN
    88         IF(Cur_Parameter.ParameterName='AD_Client_ID') THEN
    89           p_AD_Client_ID:=Cur_Parameter.P_Number;
    89           p_AD_Client_ID:=Cur_Parameter.P_String;
    90           DBMS_OUTPUT.PUT_LINE('  AD_Client_ID=' || p_AD_Client_ID) ;
    90           DBMS_OUTPUT.PUT_LINE('  AD_Client_ID=' || p_AD_Client_ID) ;
    91         ELSIF(Cur_Parameter.ParameterName='ClientValue') THEN
    91         ELSIF(Cur_Parameter.ParameterName='ClientValue') THEN
    92           p_NewClientValue:=Cur_Parameter.P_String;
    92           p_NewClientValue:=Cur_Parameter.P_String;
    93           DBMS_OUTPUT.PUT_LINE('  ClientValue=' || p_NewClientValue) ;
    93           DBMS_OUTPUT.PUT_LINE('  ClientValue=' || p_NewClientValue) ;
    94         ELSE
    94         ELSE
   103     END IF;
   103     END IF;
   104   BEGIN --BODY
   104   BEGIN --BODY
   105 
   105 
   106     -- Update sequence values
   106     -- Update sequence values
   107     v_ResultStr:='Updating sequences';
   107     v_ResultStr:='Updating sequences';
   108     AD_UPDATE_SEQUENCE() ;
       
   109     v_ResultStr:='Deleting old data';
   108     v_ResultStr:='Deleting old data';
   110     DELETE FROM AD_SCRIPT_SQL;
   109     DELETE FROM AD_SCRIPT_SQL;
   111     DELETE FROM AD_ID_TRANSLATION;
   110     DELETE FROM AD_ID_TRANSLATION;
   112     AD_DEPENDENCIES_CREATE() ;
   111     AD_DEPENDENCIES_CREATE() ;
   113 
   112 
   116     v_ResultStr:='Disabling constraints';
   115     v_ResultStr:='Disabling constraints';
   117     v_seqNo:= ad_script_disable_constraints(v_seqNo);
   116     v_seqNo:= ad_script_disable_constraints(v_seqNo);
   118     v_ResultStr:='Dropping and recreating unique indexes';
   117     v_ResultStr:='Dropping and recreating unique indexes';
   119     v_seqNo:= ad_script_drop_recreate_index(v_seqNo);
   118     v_seqNo:= ad_script_drop_recreate_index(v_seqNo);
   120     v_ResultStr:='Insert client';
   119     v_ResultStr:='Insert client';
   121     AD_Sequence_Next('AD_Client', 1000000, v_NextNo) ;
   120     AD_Sequence_Next('AD_Client', '1000000', v_NextNo) ;
   122     --AD_Client
   121     --AD_Client
   123     v_seqNo:=v_seqNo + 1;
   122     v_seqNo:=v_seqNo + 1;
   124     INSERT
   123     INSERT
   125     INTO AD_SCRIPT_SQL VALUES
   124     INTO AD_SCRIPT_SQL VALUES
   126       (v_seqNo, 'INSERT INTO AD_Client (AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, '||
   125       (v_seqNo, 'INSERT INTO AD_Client (AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, '||
   127       'VALUE, NAME, DESCRIPTION, SMTPHOST, REQUESTEMAIL, REQUESTUSER, REQUESTUSERPW, REQUESTFOLDER, '||
   126       'VALUE, NAME, DESCRIPTION, SMTPHOST, REQUESTEMAIL, REQUESTUSER, REQUESTUSERPW, REQUESTFOLDER, '||
   128       'AD_LANGUAGE) '||
   127       'AD_LANGUAGE) '||
   129       'SELECT '||v_NextNo||', 0, ISACTIVE, to_date(now()), 0, to_date(now()), 0, '||''''||p_NewClientValue||''', '''||p_NewClientValue||''', DESCRIPTION, SMTPHOST, REQUESTEMAIL, '||
   128       'SELECT '''||v_NextNo||''', ''0'', ISACTIVE, to_date(now()), ''0'', to_date(now()), ''0'', '||''''||p_NewClientValue||''', '''||p_NewClientValue||''', DESCRIPTION, SMTPHOST, REQUESTEMAIL, '||
   130       'REQUESTUSER, REQUESTUSERPW, REQUESTFOLDER, AD_LANGUAGE FROM AD_Client '|| 'WHERE AD_Client_ID = '||p_AD_Client_ID) ;
   129       'REQUESTUSER, REQUESTUSERPW, REQUESTFOLDER, AD_LANGUAGE FROM AD_Client '|| 'WHERE AD_Client_ID = '||p_AD_Client_ID) ;
   131       v_ResultStr:='Insert role';
   130       v_ResultStr:='Insert role';
   132     --AD_Role
   131     --AD_Role
   133     SELECT MIN(AD_ROLE_ID)
   132     SELECT MIN(AD_ROLE_ID)
   134     INTO v_minID
   133     INTO v_minID
   148     v_seqNo:=v_seqNo + 1;
   147     v_seqNo:=v_seqNo + 1;
   149     INSERT
   148     INSERT
   150     INTO AD_SCRIPT_SQL VALUES
   149     INTO AD_SCRIPT_SQL VALUES
   151       (v_seqNo, 'INSERT INTO AD_Role (AD_ROLE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, '||
   150       (v_seqNo, 'INSERT INTO AD_Role (AD_ROLE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, '||
   152       'NAME, DESCRIPTION, USERLEVEL, CLIENTLIST, ORGLIST, C_CURRENCY_ID, AMTAPPROVAL, AD_TREE_MENU_ID, ISMANUAL) '||
   151       'NAME, DESCRIPTION, USERLEVEL, CLIENTLIST, ORGLIST, C_CURRENCY_ID, AMTAPPROVAL, AD_TREE_MENU_ID, ISMANUAL) '||
   153       'SELECT AD_ROLE_ID + '||TO_CHAR(v_nextID-v_minID) ||', '||v_NextNo||', 0, ISACTIVE, to_date(now()), 0, to_date(now()), 0, '||
   152       'SELECT AD_ROLE_ID + '||TO_CHAR(v_nextID-v_minID) ||', '''||v_NextNo||''', ''0'', ISACTIVE, to_date(now()), ''0'', to_date(now()), 0, '||
   154       'NAME, DESCRIPTION, USERLEVEL, '''||v_NextNo||''', ORGLIST, C_CURRENCY_ID, AMTAPPROVAL, AD_TREE_MENU_ID, ISMANUAL '||
   153       'NAME, DESCRIPTION, USERLEVEL, '''||v_NextNo||''', ORGLIST, C_CURRENCY_ID, AMTAPPROVAL, AD_TREE_MENU_ID, ISMANUAL '||
   155       'FROM AD_Role '|| 'WHERE ClientList = '''||p_AD_Client_ID||'''') ;
   154       'FROM AD_Role '|| 'WHERE ClientList = '''||p_AD_Client_ID||'''') ;
   156     v_ResultStr:='Insert into role tables';
   155     v_ResultStr:='Insert into role tables';
   157     FOR Cur_RoleTables IN
   156     FOR Cur_RoleTables IN
   158       (SELECT AD_Table_ID, UPPER(TableName) AS NAME
   157       (SELECT AD_Table_ID, UPPER(TableName) AS NAME
   189       FROM AD_Column
   188       FROM AD_Column
   190       WHERE AD_Table_ID=AD_Table.AD_Table_ID AND UPPER(ColumnName)='AD_CLIENT_ID'
   189       WHERE AD_Table_ID=AD_Table.AD_Table_ID AND UPPER(ColumnName)='AD_CLIENT_ID'
   191       )
   190       )
   192     ORDER BY TableName)
   191     ORDER BY TableName)
   193     LOOP
   192     LOOP
   194       EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM '||Cur_Tables.NAME||' WHERE AD_Client_ID = '||p_AD_Client_ID INTO v_count;
   193       EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM '||Cur_Tables.NAME||' WHERE AD_Client_ID = '''||p_AD_Client_ID||'''' INTO v_count;
   195       IF(v_count>0) THEN
   194       IF(v_count>0) THEN
   196         SELECT MAX(UPPER(ColumnName))
   195         SELECT MAX(UPPER(ColumnName))
   197         INTO v_ID_Column
   196         INTO v_ID_Column
   198         FROM AD_Column
   197         FROM AD_Column
   199         WHERE AD_Table_ID=Cur_Tables.AD_Table_ID AND AD_Reference_ID=13 AND UPPER(ColumnName)=Cur_Tables.NAME||'_ID';
   198         WHERE AD_Table_ID=Cur_Tables.AD_Table_ID AND AD_Reference_ID='13' AND UPPER(ColumnName)=Cur_Tables.NAME||'_ID';
   200         IF(v_ID_Column IS NOT NULL) THEN
   199         IF(v_ID_Column IS NOT NULL) THEN
   201           EXECUTE IMMEDIATE 'SELECT MIN('||v_ID_Column||') FROM '||Cur_Tables.NAME||' WHERE AD_Client_ID = '||p_AD_Client_ID INTO v_minID;
   200           EXECUTE IMMEDIATE 'SELECT MIN('||v_ID_Column||') FROM '||Cur_Tables.NAME||' WHERE AD_Client_ID = '''||p_AD_Client_ID||'''' INTO v_minID;
   202           SELECT CurrentNext
   201           SELECT CurrentNext
   203           INTO v_nextID
   202           INTO v_nextID
   204           FROM AD_Sequence
   203           FROM AD_Sequence
   205           WHERE UPPER(Name)=Cur_Tables.NAME AND IsActive='Y' AND IsTableID='Y' AND IsAutoSequence='Y';
   204           WHERE UPPER(Name)=Cur_Tables.NAME AND IsActive='Y' AND IsTableID='Y' AND IsAutoSequence='Y';
   206           INSERT
   205           INSERT
   226         END IF;
   225         END IF;
   227         v_strSql:=v_strSql || v_strTemp || ') SELECT ' || v_NextNo;
   226         v_strSql:=v_strSql || v_strTemp || ') SELECT ' || v_NextNo;
   228         IF(v_ID_Column IS NOT NULL) THEN
   227         IF(v_ID_Column IS NOT NULL) THEN
   229           v_strSql:=v_strSql || ',' || v_ID_Column || ' + '|| TO_CHAR(v_nextID-v_minID) ;
   228           v_strSql:=v_strSql || ',' || v_ID_Column || ' + '|| TO_CHAR(v_nextID-v_minID) ;
   230         END IF;
   229         END IF;
   231         v_strSql:=v_strSql || v_strTemp || ' FROM '|| Cur_Tables.NAME || ' WHERE AD_CLIENT_ID = ' || p_AD_Client_ID;
   230         v_strSql:=v_strSql || v_strTemp || ' FROM '|| Cur_Tables.NAME || ' WHERE AD_CLIENT_ID = ''' || p_AD_Client_ID||'''';
   232         v_seqNo:=v_seqNo + 1;
   231         v_seqNo:=v_seqNo + 1;
   233         INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   232         INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   234       END IF;
   233       END IF;
   235     END LOOP;
   234     END LOOP;
   236     v_ResultStr:='Updating tables';
   235     v_ResultStr:='Updating tables';
   245     LOOP
   244     LOOP
   246       IF(Cur_TranslateData.TABLENAME<>v_tableActual OR v_count>20) THEN
   245       IF(Cur_TranslateData.TABLENAME<>v_tableActual OR v_count>20) THEN
   247         v_count:=0;
   246         v_count:=0;
   248         v_tableActual:=Cur_TranslateData.TABLENAME;
   247         v_tableActual:=Cur_TranslateData.TABLENAME;
   249         IF(v_strSql IS NOT NULL) THEN
   248         IF(v_strSql IS NOT NULL) THEN
   250           v_strSql:=v_strSql || ' WHERE AD_CLIENT_ID = '|| v_NextNo;
   249           v_strSql:=v_strSql || ' WHERE AD_CLIENT_ID = '''|| v_NextNo||'''';
   251           v_seqNo:=v_seqNo + 1;
   250           v_seqNo:=v_seqNo + 1;
   252           INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   251           INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   253         END IF;
   252         END IF;
   254         v_strSql:='UPDATE '||v_tableActual||' SET AD_CLIENT_ID = '|| v_NextNo || ' ';
   253         v_strSql:='UPDATE '||v_tableActual||' SET AD_CLIENT_ID = '''|| v_NextNo || ''' ';
   255       END IF;
   254       END IF;
   256       v_strSql:=v_strSql || ', '||Cur_TranslateData.COLUMNNAME||' = AD_Translate_ID('||Cur_TranslateData.COLUMNNAME|| ',
   255       v_strSql:=v_strSql || ', '||Cur_TranslateData.COLUMNNAME||' = AD_Translate_ID('||Cur_TranslateData.COLUMNNAME|| ',
   257         '||Cur_TranslateData."OFFSET"||','''||Cur_TranslateData.DEPENDS_ON_TABLENAME||''','''||Cur_TranslateData.DEPENDS_ON_COLUMNNAME||''',
   256         '||Cur_TranslateData."OFFSET"||','''||Cur_TranslateData.DEPENDS_ON_TABLENAME||''','''||Cur_TranslateData.DEPENDS_ON_COLUMNNAME||''',
   258         '||p_AD_Client_ID||')';
   257         '||p_AD_Client_ID||')';
   259       v_count:=v_count + 1;
   258       v_count:=v_count + 1;
   260     END LOOP;
   259     END LOOP;
   261     v_strSql:=v_strSql || ' WHERE AD_CLIENT_ID = '|| v_NextNo;
   260     v_strSql:=v_strSql || ' WHERE AD_CLIENT_ID = '''|| v_NextNo||'''';
   262     v_seqNo:=v_seqNo + 1;
   261     v_seqNo:=v_seqNo + 1;
   263     INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   262     INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   264     v_ResultStr:='Updating tables';
   263     v_ResultStr:='Updating tables';
   265     v_strSql:='';
   264     v_strSql:='';
   266     v_tableActual:='xx';
   265     v_tableActual:='xx';
   273     LOOP
   272     LOOP
   274       IF(Cur_TranslateDataSelfDep.TABLENAME<>v_tableActual OR v_count>20) THEN
   273       IF(Cur_TranslateDataSelfDep.TABLENAME<>v_tableActual OR v_count>20) THEN
   275         v_count:=0;
   274         v_count:=0;
   276         v_tableActual:=Cur_TranslateDataSelfDep.TABLENAME;
   275         v_tableActual:=Cur_TranslateDataSelfDep.TABLENAME;
   277         IF(v_strSql IS NOT NULL) THEN
   276         IF(v_strSql IS NOT NULL) THEN
   278           v_strSql:=v_strSql || ' WHERE AD_CLIENT_ID = '|| v_NextNo;
   277           v_strSql:=v_strSql || ' WHERE AD_CLIENT_ID = '''|| v_NextNo||'''';
   279           v_seqNo:=v_seqNo + 1;
   278           v_seqNo:=v_seqNo + 1;
   280           INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   279           INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   281         END IF;
   280         END IF;
   282       END IF;
   281       END IF;
   283       v_dynamic_select:='SELECT DISTINCT ' || Cur_TranslateDataSelfDep.COLUMNNAME || ' FROM ' || Cur_TranslateDataSelfDep.TABLENAME || ' WHERE AD_CLIENT_ID = ' || p_AD_Client_ID || ' AND '||Cur_TranslateDataSelfDep.COLUMNNAME||' IS NOT NULL';
   282       v_dynamic_select:='SELECT DISTINCT ' || Cur_TranslateDataSelfDep.COLUMNNAME || ' FROM ' || Cur_TranslateDataSelfDep.TABLENAME || ' WHERE AD_CLIENT_ID = ''' || p_AD_Client_ID || ''' AND '||Cur_TranslateDataSelfDep.COLUMNNAME||' IS NOT NULL';
   284  IF (CUR_v_rc_ISOPEN=true) THEN
   283  IF (CUR_v_rc_ISOPEN=true) THEN
   285   CUR_v_rc_ISOPEN:=false;
   284   CUR_v_rc_ISOPEN:=false;
   286   CLOSE v_rc;
   285   CLOSE v_rc;
   287  END IF;
   286  END IF;
   288       OPEN v_rc FOR v_dynamic_select;
   287       OPEN v_rc FOR v_dynamic_select;
   289  CUR_v_rc_ISOPEN:=true;
   288  CUR_v_rc_ISOPEN:=true;
   290       LOOP
   289       LOOP
   291         FETCH v_rc INTO v_oldId;
   290         FETCH v_rc INTO v_oldId;
   292         EXIT WHEN v_rc%NOTFOUND;
   291         EXIT WHEN v_rc%NOTFOUND;
   293         v_strSql:='UPDATE '||v_tableActual||' SET AD_CLIENT_ID = '|| v_NextNo || ' ';
   292         v_strSql:='UPDATE '||v_tableActual||' SET AD_CLIENT_ID = '''|| v_NextNo || ''' ';
   294         v_translatedId:=AD_Translate_ID(v_oldId, Cur_TranslateDataSelfDep."OFFSET", Cur_TranslateDataSelfDep.DEPENDS_ON_TABLENAME, Cur_TranslateDataSelfDep.DEPENDS_ON_COLUMNNAME, p_AD_Client_ID) ;
   293         v_translatedId:=AD_Translate_ID(v_oldId, Cur_TranslateDataSelfDep."OFFSET", Cur_TranslateDataSelfDep.DEPENDS_ON_TABLENAME, Cur_TranslateDataSelfDep.DEPENDS_ON_COLUMNNAME, p_AD_Client_ID) ;
   295         v_strSql:=v_strSql || ', '||Cur_TranslateDataSelfDep.COLUMNNAME||' = ' || v_translatedId ;
   294         v_strSql:=v_strSql || ', '||Cur_TranslateDataSelfDep.COLUMNNAME||' = ''' || v_translatedId ||'''';
   296         v_strSql:=v_strSql || ' WHERE AD_CLIENT_ID = '|| v_NextNo || ' AND '||Cur_TranslateDataSelfDep.COLUMNNAME||' = ' || v_oldId;
   295         v_strSql:=v_strSql || ' WHERE AD_CLIENT_ID = '''|| v_NextNo || ''' AND '||Cur_TranslateDataSelfDep.COLUMNNAME||' = ''' || v_oldId||'''';
   297         v_count:=v_count + 1;
   296         v_count:=v_count + 1;
   298         v_seqNo:=v_seqNo + 1;
   297         v_seqNo:=v_seqNo + 1;
   299         INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   298         INSERT INTO AD_SCRIPT_SQL VALUES(v_seqNo, v_strSql) ;
   300         v_strSql:='';
   299         v_strSql:='';
   301       END LOOP;
   300       END LOOP;
   304 
   303 
   305     DECLARE
   304     DECLARE
   306       i RECORD;
   305       i RECORD;
   307     BEGIN
   306     BEGIN
   308       --Accounting is not copied so any doc should be posted
   307       --Accounting is not copied so any doc should be posted
   309       for i in (select 'update '||t.tablename||' set posted=''N'' where ad_client_id='||v_NextNo as vsql
   308       for i in (select 'update '||t.tablename||' set posted=''N'' where ad_client_id='''||v_NextNo||'''' as vsql
   310                   from ad_column c, ad_table t
   309                   from ad_column c, ad_table t
   311                  where upper(columnname)='POSTED'
   310                  where upper(columnname)='POSTED'
   312                   and c.ad_table_id = t.ad_table_id) loop
   311                   and c.ad_table_id = t.ad_table_id) loop
   313         v_seqNo:=v_seqNo + 1;
   312         v_seqNo:=v_seqNo + 1;
   314         insert into AD_SCRIPT_SQL VALUES
   313         insert into AD_SCRIPT_SQL VALUES
   318 
   317 
   319     --ACTUALIZACION DE NAMES PARA LAS TABLAS DE LOGIN
   318     --ACTUALIZACION DE NAMES PARA LAS TABLAS DE LOGIN
   320     v_seqNo:=v_seqNo + 1;
   319     v_seqNo:=v_seqNo + 1;
   321     INSERT
   320     INSERT
   322     INTO AD_SCRIPT_SQL VALUES
   321     INTO AD_SCRIPT_SQL VALUES
   323       (v_seqNo, 'UPDATE AD_ROLE SET NAME = '''||p_NewClientValue||'_''||NAME WHERE AD_CLIENT_ID = ' ||v_NextNo) ;
   322       (v_seqNo, 'UPDATE AD_ROLE SET NAME = '''||p_NewClientValue||'_''||NAME WHERE AD_CLIENT_ID = ''' ||v_NextNo||'''') ;
   324     v_seqNo:=v_seqNo + 1;
   323     v_seqNo:=v_seqNo + 1;
   325     INSERT
   324     INSERT
   326     INTO AD_SCRIPT_SQL VALUES
   325     INTO AD_SCRIPT_SQL VALUES
   327       (v_seqNo, 'UPDATE AD_USER SET USERNAME = (CASE WHEN USERNAME IS NULL THEN NULL ELSE '''||p_NewClientValue||'_''||USERNAME END) WHERE AD_CLIENT_ID = ' ||v_NextNo) ;
   326       (v_seqNo, 'UPDATE AD_USER SET USERNAME = (CASE WHEN USERNAME IS NULL THEN NULL ELSE '''||p_NewClientValue||'_''||USERNAME END) WHERE AD_CLIENT_ID = ''' ||v_NextNo||'''') ;
   328     v_seqNo:=v_seqNo + 1;
   327     v_seqNo:=v_seqNo + 1;
   329     INSERT
   328     INSERT
   330     INTO AD_SCRIPT_SQL VALUES
   329     INTO AD_SCRIPT_SQL VALUES
   331       (v_seqNo, 'UPDATE AD_ORG SET NAME = '''||p_NewClientValue||'_''||NAME WHERE AD_CLIENT_ID = ' ||v_NextNo) ;
   330       (v_seqNo, 'UPDATE AD_ORG SET NAME = '''||p_NewClientValue||'_''||NAME WHERE AD_CLIENT_ID = ''' ||v_NextNo||'''') ;
   332     v_seqNo:=v_seqNo + 1;
   331     v_seqNo:=v_seqNo + 1;
   333     INSERT
   332     INSERT
   334     INTO AD_SCRIPT_SQL VALUES
   333     INTO AD_SCRIPT_SQL VALUES
   335       (v_seqNo, 'UPDATE M_WAREHOUSE SET NAME = '''||p_NewClientValue||'_''||NAME WHERE AD_CLIENT_ID = ' ||v_NextNo) ;
   334       (v_seqNo, 'UPDATE M_WAREHOUSE SET NAME = '''||p_NewClientValue||'_''||NAME WHERE AD_CLIENT_ID = ''' ||v_NextNo||'''') ;
   336     --AD_ORGINFO; AD_ORG_ID = 0
   335     --AD_ORGINFO; AD_ORG_ID = '0'
   337     v_seqNo:=v_seqNo + 1;
   336     v_seqNo:=v_seqNo + 1;
   338     INSERT
   337     INSERT
   339     INTO AD_SCRIPT_SQL VALUES
   338     INTO AD_SCRIPT_SQL VALUES
   340       (v_seqNo, 'DELETE FROM AD_ORGINFO WHERE AD_ORG_ID = 0 AND AD_CLIENT_ID = ' ||v_NextNo) ;
   339       (v_seqNo, 'DELETE FROM AD_ORGINFO WHERE AD_ORG_ID = ''0'' AND AD_CLIENT_ID = ''' ||v_NextNo||'''') ;
   341     --AD_TreeNodes...
   340     --AD_TreeNodes...
   342     --AD_TreeNodePR
   341     --AD_TreeNodePR
   343     SELECT max("OFFSET")
   342     SELECT max("OFFSET")
   344     INTO v_offset
   343     INTO v_offset
   345     FROM AD_ID_TRANSLATION
   344     FROM AD_ID_TRANSLATION
   348       v_seqNo:=v_seqNo + 1;
   347       v_seqNo:=v_seqNo + 1;
   349       INSERT
   348       INSERT
   350       INTO AD_SCRIPT_SQL VALUES
   349       INTO AD_SCRIPT_SQL VALUES
   351         (v_seqNo, 'UPDATE AD_TREENODEPR SET '|| 'NODE_ID = AD_Translate_ID(Node_ID,'||v_offset||',''M_PRODUCT'',''M_PRODUCT_ID'',
   350         (v_seqNo, 'UPDATE AD_TREENODEPR SET '|| 'NODE_ID = AD_Translate_ID(Node_ID,'||v_offset||',''M_PRODUCT'',''M_PRODUCT_ID'',
   352         '||p_AD_Client_ID||')'|| ', PARENT_ID = AD_Translate_ID(Parent_ID,'||v_offset||',''M_PRODUCT'',''M_PRODUCT_ID'',
   351         '||p_AD_Client_ID||')'|| ', PARENT_ID = AD_Translate_ID(Parent_ID,'||v_offset||',''M_PRODUCT'',''M_PRODUCT_ID'',
   353         '||p_AD_Client_ID||') '|| 'WHERE AD_CLIENT_ID = ' ||v_NextNo) ;
   352         '||p_AD_Client_ID||') '|| 'WHERE AD_CLIENT_ID = ''' ||v_NextNo||'''') ;
   354     end if;
   353     end if;
   355     --AD_TreeNodeBP
   354     --AD_TreeNodeBP
   356     SELECT max("OFFSET")
   355     SELECT max("OFFSET")
   357     INTO v_offset
   356     INTO v_offset
   358     FROM AD_ID_TRANSLATION
   357     FROM AD_ID_TRANSLATION
   454     v_seqNo:= ad_script_enable_constraints(v_seqNo);
   453     v_seqNo:= ad_script_enable_constraints(v_seqNo);
   455     v_ResultStr:='Enabling triggers';
   454     v_ResultStr:='Enabling triggers';
   456     v_seqNo:= ad_script_enable_triggers(v_seqNo);
   455     v_seqNo:= ad_script_enable_triggers(v_seqNo);
   457     -- Update sequence values
   456     -- Update sequence values
   458     v_ResultStr:='Updating sequences';
   457     v_ResultStr:='Updating sequences';
   459     AD_UPDATE_SEQUENCE_GENERATE() ;
       
   460 
   458 
   461  v_ResultStr := ad_script_execute(v_ResultStr);
   459  v_ResultStr := ad_script_execute(v_ResultStr);
   462 
   460 
   463 
   461 
   464   select count(*)
   462   select count(*)