--------------------------------------------------------------------------
-- Play this script in PRUEBAS@VISANY to make it look like DESA@VISMAR
--                                                                      --
-- Please review the script before using it to make sure it won't       --
-- cause any unacceptable data loss.                                    --
--                                                                      --
-- PRUEBAS@VISANY Schema Extracted by User PRUEBAS 
-- DESA@VISMAR Schema Extracted by User DESA 
---DROP CONSTRAINT INV_PARAMETRO_INVPEND_CK;
ALTER TABLE INV_PARAMETRO
ADD CONSTRAINT INV_PARAMETRO_IND_COSPROGLO_CK CHECK (IND_COSPRO_GLO IN ('S','N'));

--- DROP CONSTRAINT INV_TOMAFIS_TIPO_CK;
ALTER TABLE INV_TOMAFIS
ADD CONSTRAINT INV_TOMAFIS_TIPO_CK CHECK (TIPO IN ('1','2','3','D'));

---DROP CONSTRAINT INV_DETTOMAFIS_CONTEO1_CK;
ALTER TABLE INV_DETTOMAFIS
ADD CONSTRAINT INV_DETTOMAFIS_CONTEO1_CK CHECK (CONTEO1 IS NULL OR (CONTEO1 >= 0 ));

---DROP CONSTRAINT INV_DETTOMAFIS_CONTEO2_CK;
ALTER TABLE INV_DETTOMAFIS
ADD CONSTRAINT INV_DETTOMAFIS_CONTEO2_CK CHECK (CONTEO2 IS NULL OR (CONTEO2 >= 0 ));

---DROP CONSTRAINT INV_DETTOMAFIS_CONTEO3_CK;
ALTER TABLE INV_DETTOMAFIS
ADD CONSTRAINT INV_DETTOMAFIS_CONTEO3_CK CHECK (CONTEO3 IS NULL OR (CONTEO3 >= 0 ));

---DROP CONSTRAINT INV_DINTOMAFIS_CANTIDADEN_CK;
ALTER TABLE INV_DINTOMAFIS
ADD CONSTRAINT INV_DINTOMAFIS_CANTIDADEN_CK CHECK (CANTIDADENT >= 0);

--- DROP CONSTRAINT INV_DINTOMAFIS_PESOENT_CK;
ALTER TABLE INV_DINTOMAFIS
ADD CONSTRAINT INV_DINTOMAFIS_PESOENT_CK CHECK (PESOENT >= 0);

--- DROP CONSTRAINT INV_DINTOMAFIS_CAN_EXISTE_CK;
ALTER TABLE INV_DINTOMAFIS
ADD CONSTRAINT INV_DINTOMAFIS_CAN_EXISTE_CK CHECK (CAN_EXISTE >= 0);

--- DROP CONSTRAINT INV_DINTOMAFIS_CAN_FISICO_CK;
ALTER TABLE INV_DINTOMAFIS
ADD CONSTRAINT INV_DINTOMAFIS_CAN_FISICO_CK CHECK (CAN_FISICO >= 0);

ALTER TABLE INV_ENCABMOVIMIENTO
DROP CONSTRAINT INV_ENCABMOVTO_ESTADO_CK;

ALTER TABLE INV_ENCABMOVIMIENTO
DROP CONSTRAINT INV_ENCABMOVIMIENTO_ESTADO_CK;

ALTER TABLE INV_ENCABMOVIMIENTO
 ADD CONSTRAINT INV_ENCABMOVIMIE_ESTADO_CK CHECK (ESTADO IN ('P','C','D','A'));

--- DROP CONSTRAINT INV_ARTICULO_MAXDESCUEN_CK;
ALTER TABLE INV_ARTICULO
ADD CONSTRAINT INV_ARTICULO_MAXDESCUEN_CK CHECK (MAXDESCUENTO IS NULL OR (MAXDESCUENTO >= 0 ));

--- DROP CONSTRAINT INV_ARTICULOBOD_COS_ULTIMO_CK;
ALTER TABLE INV_ARTICULOBOD
ADD CONSTRAINT INV_ARTICULOBOD_COS_ULTIMO_CK CHECK (COS_ULTIMO >= 0);

--- DROP CONSTRAINT INV_ARTICULOBOD_COS_MAYOR_CK;
ALTER TABLE INV_ARTICULOBOD
ADD CONSTRAINT INV_ARTICULOBOD_COS_MAYOR_CK CHECK (COS_MAYOR >= 0);

--- DROP CONSTRAINT INV_ARTICULOBOD_PORC_UTILI_CK;
ALTER TABLE INV_ARTICULOBOD
ADD CONSTRAINT INV_ARTICULOBOD_PORC_UTILI_CK CHECK (PORC_UTILIDAD IS NULL OR (PORC_UTILIDAD >= 0 ));

--- DROP CONSTRAINT INV_ARTICULOBOD_PUNTO_REOR_CK;
ALTER TABLE INV_ARTICULOBOD
ADD CONSTRAINT INV_ARTICULOBOD_PUNTO_REOR_CK CHECK (PUNTO_REORDEN >= 0);

--- DROP CONSTRAINT INV_ARTIC_CODBAR_TITULAR_CK;
ALTER TABLE INV_ARTIC_CODBAR
ADD CONSTRAINT INV_ARTIC_CODBAR_TITULAR_CK CHECK (TITULAR IN ('S','N'));

--- DROP CONSTRAINT INV_LOTE_COS_UNIDAD_CK;
ALTER TABLE INV_LOTE
ADD CONSTRAINT INV_LOTE_COS_UNIDAD_CK CHECK (COS_UNIDAD >= 0);

--- DROP CONSTRAINT INV_LOTE_COS_COMPRA_CK;
ALTER TABLE INV_LOTE
ADD CONSTRAINT INV_LOTE_COS_COMPRA_CK CHECK (COS_COMPRA >= 0);

--- DROP CONSTRAINT INV_LOTE_ARTIC_B_COS_TOTAL_CK;
ALTER TABLE INV_LOTE_ARTIC_BODEGA
ADD CONSTRAINT INV_LOTE_ARTIC_B_COS_TOTAL_CK CHECK (COS_TOTAL >= 0);

--- DROP CONSTRAINT INV_LOTE_ARTIC_BODEGA_CK;
ALTER TABLE INV_LOTE_ARTIC_BODEGA
ADD CONSTRAINT INV_LOTE_ARTIC_BODEGA_COSTO_CK CHECK ((CAN_EXISTE>0 AND COS_TOTAL>=0) OR (ABS(COS_TOTAL)<0.5 AND CAN_EXISTE=0));

--- DROP CONSTRAINT INVARTICBODEGA_ACUM_FK;
ALTER TABLE INV_ACUM_PERIODO
ADD CONSTRAINT INVARTICBODEGA_ACUM_FK FOREIGN KEY (COD_CIA, COD_BODEGA, COD_ARTICULO)
REFERENCES INV_ARTICULOBOD (COD_CIA, COD_BODEGA, COD_ARTICULO);

ALTER TABLE INV_ENCSOLTRASLADO
MODIFY(COD_USUARIO VARCHAR2(30));

ALTER TABLE INV_MOVGRUPO
MODIFY(USUARIO VARCHAR2(30));

ALTER TABLE INV_MOVGRUPO
MODIFY(CONSECUTIVO  DEFAULT NULL);

ALTER TABLE INV_CONSEC_MOVTOS
MODIFY(NUM_MOVTO NUMBER(10));

ALTER TABLE INV_ARTICULOBOD DROP 
CONSTRAINT INV_ATICULOBOD_PK;

ALTER TABLE INV_ARTICULOBOD
 ADD CONSTRAINT INV_ARTICULOBOD_PK PRIMARY KEY (COD_CIA, COD_BODEGA, COD_ARTICULO);

ALTER TABLE INV_ARTIC_CODBAR
 drop CONSTRAINT INV_ARTIC_CODBAR_IDX1;

ALTER TABLE INV_ARTIC_CODBAR
 ADD CONSTRAINT INV_ARTIC_CODBAR_uq
 UNIQUE (COD_CIA, COD_BARRAS);

ALTER TABLE INV_MOVGRUPODET
MODIFY(CONSECUTIVO  DEFAULT null);

** ESTOS NO SON CHECKS DE TABLA
---ALTER TABLE INV_TOMAFIS
--- ADD CHECK (tipo in ('1','2','3','D'));
ALTER TABLE INV_TOMAFIS DROP CONSTRAINT SYS_C008481;
ALTER TABLE INV_TOMAFIS DROP CONSTRAINT SYS_C0067207;
ALTER TABLE INV_TOMAFIS DROP CONSTRAINT SYS_C0067234;

ALTER TABLE INV_DETTOMAFIS DROP CONSTRAINT SYS_C008204;
ALTER TABLE INV_DETTOMAFIS DROP CONSTRAINT SYS_C008205;
ALTER TABLE INV_DETTOMAFIS DROP CONSTRAINT SYS_C008206;

ALTER TABLE INV_TABLADESC
DROP CONSTRAINT INV_TABLADESC_PORCENTAJE_CK;
ALTER TABLE INV_TABLADESC
ADD CONSTRAINT INV_TABLADESC_PORCENTAJE_CK CHECK (PORCENTAJE >= 0 );

ALTER TABLE INV_TIPINV
MODIFY(CTLEXISTENCIA  NOT NULL);

**CORREGIR LA FORMA
ALTER TABLE INV_ARTICULO
MODIFY(COD_DEPTO  NOT NULL);

--- DROP CONSTRAINT INV_ARTICULOBOD_MIN_EXIST_CK;
ALTER TABLE INV_ARTICULOBOD
ADD CONSTRAINT INV_ARTICULOBOD_MIN_EXIST_CK CHECK (MIN_EXIST >= 0);

--- DROP CONSTRAINT INV_ARTICULOBOD_MAX_EXIST_CK;
ALTER TABLE INV_ARTICULOBOD
ADD CONSTRAINT INV_ARTICULOBOD_MAX_EXIST_CK CHECK (MAX_EXIST >= 0);

ALTER TABLE INV_ARTIC_CODBAR
MODIFY(TITULAR  NOT NULL);

ALTER TABLE INV_TIPINV
MODIFY(CTLEXISTENCIA  DEFAULT ‘S’);

ALTER TABLE INV_ARTICULOBOD
DROP PRIMARY KEY CASCADE;
ALTER TABLE INV_ARTICULOBOD add
CONSTRAINT INV_ARTICULOBOD_PK PRIMARY KEY (COD_CIA, COD_BODEGA, COD_ARTICULO);

ALTER TABLE INV_ARTICULOBOD
ADD CONSTRAINT INVARTICULO_ARTICBOD_FK 
FOREIGN KEY (COD_CIA, COD_ARTICULO)
REFERENCES INV_ARTICULO (COD_CIA, COD_ARTICULO);

ALTER TABLE INV_ARTICULOBOD
ADD CONSTRAINT INVBODEGA_ARTICBOD_FK 
FOREIGN KEY (COD_CIA, COD_BODEGA)
REFERENCES INV_BODEGA (COD_CIA, COD_BODEGA);

ALTER TABLE INV_DINTOMAFIS
   ADD CONSTRAINT INVTOMAFIS_DINTOMA_FK FOREIGN KEY (SECUENCIA)
      REFERENCES INV_TOMAFIS (SECUENCIA);

ALTER TABLE INV_DETTOMAFIS
 ADD CONSTRAINT INV_DETTOMAFIS_COSTO_CK CHECK (COSTO >= 0);