

/* 
 * PROCEDURE: SP_InsProcAR_CCDOC 
 */

CREATE PROCEDURE SP_InsProcAR_CCDOC
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          numeric(10, 0),
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2)                   = NULL,
    @REFERENCE            numeric(10, 0),
    @CURYTYPEID           UD_CURYTYPEID,
    @DOCDATE              smalldatetime,
    @DATEPROCESALES       smalldatetime,
    @DUEDATE              smalldatetime,
    @TOTALPEN             numeric(11, 4),
    @TOTALUSD             numeric(11, 4),
    @PAIDMENTPEN          numeric(11, 4),
    @PAIDMENTUSD          numeric(11, 4),
    @DOCVALPEN            numeric(11, 4),
    @DOCVALUSD            numeric(11, 4),
    @CUSTIDSS             UD_CODCLIENT,
    @TAXREGNBR            UD_CODCLIENT              = NULL,
    @DOCSTATUSID          char(2),
    @CURYRATE             UD_CAMBIO,
    @CURYID               UD_CURYID,
    @DIAPAGID             char(10)                  = NULL,
    @DAYGRACE             int,
    @NBRBONUS             numeric(10, 0),
    @USERID               UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL,
    @USER3                UD_USER6                  = NULL,
    @USER4                UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO AR_CCDOC(DOCTYPEID,
                         NBRDOCUMENT,
                         SITEID,
                         DOCSUMMARYTYPEID,
                         REFERENCE,
                         CURYTYPEID,
                         DOCDATE,
                         DATEPROCESALES,
                         DUEDATE,
                         TOTALPEN,
                         TOTALUSD,
                         PAIDMENTPEN,
                         PAIDMENTUSD,
                         DOCVALPEN,
                         DOCVALUSD,
                         CUSTIDSS,
                         TAXREGNBR,
                         DOCSTATUSID,
                         CURYRATE,
                         CURYID,
                         DIAPAGID,
                         DAYGRACE,
                         NBRBONUS,
                         USERID,
                         STKSEND,
                         STKRECEIVE,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SITEID,
           @DOCSUMMARYTYPEID,
           @REFERENCE,
           @CURYTYPEID,
           @DOCDATE,
           @DATEPROCESALES,
           @DUEDATE,
           @TOTALPEN,
           @TOTALUSD,
           @PAIDMENTPEN,
           @PAIDMENTUSD,
           @DOCVALPEN,
           @DOCVALUSD,
           @CUSTIDSS,
           @TAXREGNBR,
           @DOCSTATUSID,
           @CURYRATE,
           @CURYID,
           @DIAPAGID,
           @DAYGRACE,
           @NBRBONUS,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcAR_CCDOC: Cannot insert because primary key value not found in AR_CCDOC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcAR_CCDOC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcAR_CCDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcAR_CCDOC >>>'
go


/* 
 * PROCEDURE: SP_UpdProcAR_CCDOC 
 */

CREATE PROCEDURE SP_UpdProcAR_CCDOC
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          numeric(10, 0),
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2)                   = NULL,
    @REFERENCE            numeric(10, 0),
    @CURYTYPEID           UD_CURYTYPEID,
    @DOCDATE              smalldatetime,
    @DATEPROCESALES       smalldatetime,
    @DUEDATE              smalldatetime,
    @TOTALPEN             numeric(11, 4),
    @TOTALUSD             numeric(11, 4),
    @PAIDMENTPEN          numeric(11, 4),
    @PAIDMENTUSD          numeric(11, 4),
    @DOCVALPEN            numeric(11, 4),
    @DOCVALUSD            numeric(11, 4),
    @CUSTIDSS             UD_CODCLIENT,
    @TAXREGNBR            UD_CODCLIENT              = NULL,
    @DOCSTATUSID          char(2),
    @CURYRATE             UD_CAMBIO,
    @CURYID               UD_CURYID,
    @DIAPAGID             char(10)                  = NULL,
    @DAYGRACE             int,
    @NBRBONUS             numeric(10, 0),
    @USERID               UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL,
    @USER3                UD_USER6                  = NULL,
    @USER4                UD_USER7                  = NULL,
    @TSTAMP               timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE AR_CCDOC
       SET DOCSUMMARYTYPEID      = @DOCSUMMARYTYPEID,
           REFERENCE             = @REFERENCE,
           CURYTYPEID            = @CURYTYPEID,
           DOCDATE               = @DOCDATE,
           DATEPROCESALES        = @DATEPROCESALES,
           DUEDATE               = @DUEDATE,
           TOTALPEN              = @TOTALPEN,
           TOTALUSD              = @TOTALUSD,
           PAIDMENTPEN           = @PAIDMENTPEN,
           PAIDMENTUSD           = @PAIDMENTUSD,
           DOCVALPEN             = @DOCVALPEN,
           DOCVALUSD             = @DOCVALUSD,
           CUSTIDSS              = @CUSTIDSS,
           TAXREGNBR             = @TAXREGNBR,
           DOCSTATUSID           = @DOCSTATUSID,
           CURYRATE              = @CURYRATE,
           CURYID                = @CURYID,
           DIAPAGID              = @DIAPAGID,
           DAYGRACE              = @DAYGRACE,
           NBRBONUS              = @NBRBONUS,
           USERID                = @USERID,
           STKSEND               = @STKSEND,
           STKRECEIVE            = @STKRECEIVE,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcAR_CCDOC: Cannot update  in AR_CCDOC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcAR_CCDOC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcAR_CCDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcAR_CCDOC >>>'
go


/* 
 * PROCEDURE: SP_DelProcAR_CCDOC 
 */

CREATE PROCEDURE SP_DelProcAR_CCDOC
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          numeric(10, 0),
    @SITEID               char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM AR_CCDOC
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcAR_CCDOC: Cannot delete because foreign keys still exist in AR_CCDOC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcAR_CCDOC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcAR_CCDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcAR_CCDOC >>>'
go


/* 
 * PROCEDURE: SP_SelProcAR_CCDOC 
 */

CREATE PROCEDURE SP_SelProcAR_CCDOC
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          numeric(10, 0),
    @SITEID               char(5))
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SITEID,
           DOCSUMMARYTYPEID,
           REFERENCE,
           CURYTYPEID,
           DOCDATE,
           DATEPROCESALES,
           DUEDATE,
           TOTALPEN,
           TOTALUSD,
           PAIDMENTPEN,
           PAIDMENTUSD,
           DOCVALPEN,
           DOCVALUSD,
           CUSTIDSS,
           TAXREGNBR,
           DOCSTATUSID,
           CURYRATE,
           CURYID,
           DIAPAGID,
           DAYGRACE,
           NBRBONUS,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM AR_CCDOC
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcAR_CCDOC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcAR_CCDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcAR_CCDOC >>>'
go


/* 
 * PROCEDURE: SP_InsProcAR_CCDOCDET 
 */

CREATE PROCEDURE SP_InsProcAR_CCDOCDET
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @SEQUENCE              smallint,
    @CURYTYPEID            char(6),
    @DOCSTATUSID           char(2),
    @DOCDATE               smalldatetime,
    @DATEPROCEPAIDMENT     smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @BANKID                char(2),
    @CHEQNUMBER            char(15),
    @NBRBANKACCOUNT        char(15),
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @PAIDMENTPEN           numeric(11, 4),
    @PAIDMENTUSD           numeric(11, 4),
    @CURYID                char(3),
    @CURYRATE              UD_CAMBIO,
    @CUSTIDSS              char(20),
    @TAXREGNBR             UD_CODCLIENT              = NULL,
    @DOCSUMMARYTTYPEID     char(2)                   = NULL,
    @REFNBR                numeric(10, 0),
    @USERID                UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @SITEID                char(5),
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO AR_CCDOCDET(DOCTYPEID,
                            NBRDOCUMENT,
                            SEQUENCE,
                            CURYTYPEID,
                            DOCSTATUSID,
                            DOCDATE,
                            DATEPROCEPAIDMENT,
                            DATEPROCESALES,
                            BANKID,
                            CHEQNUMBER,
                            NBRBANKACCOUNT,
                            TOTALPEN,
                            TOTALUSD,
                            PAIDMENTPEN,
                            PAIDMENTUSD,
                            CURYID,
                            CURYRATE,
                            CUSTIDSS,
                            TAXREGNBR,
                            DOCSUMMARYTTYPEID,
                            REFNBR,
                            USERID,
                            STKSEND,
                            STKRECEIVE,
                            SITEID,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SEQUENCE,
           @CURYTYPEID,
           @DOCSTATUSID,
           @DOCDATE,
           @DATEPROCEPAIDMENT,
           @DATEPROCESALES,
           @BANKID,
           @CHEQNUMBER,
           @NBRBANKACCOUNT,
           @TOTALPEN,
           @TOTALUSD,
           @PAIDMENTPEN,
           @PAIDMENTUSD,
           @CURYID,
           @CURYRATE,
           @CUSTIDSS,
           @TAXREGNBR,
           @DOCSUMMARYTTYPEID,
           @REFNBR,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @SITEID,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcAR_CCDOCDET: Cannot insert because primary key value not found in AR_CCDOCDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcAR_CCDOCDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcAR_CCDOCDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcAR_CCDOCDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcAR_CCDOCDET 
 */

CREATE PROCEDURE SP_UpdProcAR_CCDOCDET
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @SEQUENCE              smallint,
    @CURYTYPEID            char(6),
    @DOCSTATUSID           char(2),
    @DOCDATE               smalldatetime,
    @DATEPROCEPAIDMENT     smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @BANKID                char(2),
    @CHEQNUMBER            char(15),
    @NBRBANKACCOUNT        char(15),
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @PAIDMENTPEN           numeric(11, 4),
    @PAIDMENTUSD           numeric(11, 4),
    @CURYID                char(3),
    @CURYRATE              UD_CAMBIO,
    @CUSTIDSS              char(20),
    @TAXREGNBR             UD_CODCLIENT              = NULL,
    @DOCSUMMARYTTYPEID     char(2)                   = NULL,
    @REFNBR                numeric(10, 0),
    @USERID                UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @SITEID                char(5),
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE AR_CCDOCDET
       SET CURYTYPEID             = @CURYTYPEID,
           DOCSTATUSID            = @DOCSTATUSID,
           DOCDATE                = @DOCDATE,
           DATEPROCEPAIDMENT      = @DATEPROCEPAIDMENT,
           DATEPROCESALES         = @DATEPROCESALES,
           BANKID                 = @BANKID,
           CHEQNUMBER             = @CHEQNUMBER,
           NBRBANKACCOUNT         = @NBRBANKACCOUNT,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           PAIDMENTPEN            = @PAIDMENTPEN,
           PAIDMENTUSD            = @PAIDMENTUSD,
           CURYID                 = @CURYID,
           CURYRATE               = @CURYRATE,
           CUSTIDSS               = @CUSTIDSS,
           TAXREGNBR              = @TAXREGNBR,
           DOCSUMMARYTTYPEID      = @DOCSUMMARYTTYPEID,
           REFNBR                 = @REFNBR,
           USERID                 = @USERID,
           STKSEND                = @STKSEND,
           STKRECEIVE             = @STKRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE
       AND SITEID      = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcAR_CCDOCDET: Cannot update  in AR_CCDOCDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcAR_CCDOCDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcAR_CCDOCDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcAR_CCDOCDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcAR_CCDOCDET 
 */

CREATE PROCEDURE SP_DelProcAR_CCDOCDET
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @SEQUENCE              smallint,
    @SITEID                char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM AR_CCDOCDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE
       AND SITEID      = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcAR_CCDOCDET: Cannot delete because foreign keys still exist in AR_CCDOCDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcAR_CCDOCDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcAR_CCDOCDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcAR_CCDOCDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcAR_CCDOCDET 
 */

CREATE PROCEDURE SP_SelProcAR_CCDOCDET
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @SEQUENCE              smallint,
    @SITEID                char(5))
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SEQUENCE,
           CURYTYPEID,
           DOCSTATUSID,
           DOCDATE,
           DATEPROCEPAIDMENT,
           DATEPROCESALES,
           BANKID,
           CHEQNUMBER,
           NBRBANKACCOUNT,
           TOTALPEN,
           TOTALUSD,
           PAIDMENTPEN,
           PAIDMENTUSD,
           CURYID,
           CURYRATE,
           CUSTIDSS,
           TAXREGNBR,
           DOCSUMMARYTTYPEID,
           REFNBR,
           USERID,
           STKSEND,
           STKRECEIVE,
           SITEID,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM AR_CCDOCDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE
       AND SITEID      = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcAR_CCDOCDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcAR_CCDOCDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcAR_CCDOCDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcBANK 
 */

CREATE PROCEDURE SP_InsProcBANK
(
    @BANKID         char(2),
    @DESCR          UD_NOMBRE,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER01         UD_USER1             = NULL,
    @USER02         UD_USER2             = NULL,
    @USER03         UD_USER6             = NULL,
    @USER04         UD_USER7             = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO BANK(BANKID,
                     DESCR,
                     STKNEW,
                     STKMODIFY,
                     STKDELETE,
                     STKSEND,
                     STKRECEIVE,
                     USER01,
                     USER02,
                     USER03,
                     USER04 
                     )
    VALUES(@BANKID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcBANK: Cannot insert because primary key value not found in BANK '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcBANK') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcBANK >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcBANK >>>'
go


/* 
 * PROCEDURE: SP_UpdProcBANK 
 */

CREATE PROCEDURE SP_UpdProcBANK
(
    @BANKID         char(2),
    @DESCR          UD_NOMBRE,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER01         UD_USER1             = NULL,
    @USER02         UD_USER2             = NULL,
    @USER03         UD_USER6             = NULL,
    @USER04         UD_USER7             = NULL,
    @TSTAMP         timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE BANK
       SET DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER01          = @USER01,
           USER02          = @USER02,
           USER03          = @USER03,
           USER04          = @USER04
     WHERE BANKID = @BANKID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcBANK: Cannot update  in BANK '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcBANK') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcBANK >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcBANK >>>'
go


/* 
 * PROCEDURE: SP_DelProcBANK 
 */

CREATE PROCEDURE SP_DelProcBANK
(
    @BANKID         char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM BANK
     WHERE BANKID = @BANKID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcBANK: Cannot delete because foreign keys still exist in BANK '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcBANK') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcBANK >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcBANK >>>'
go


/* 
 * PROCEDURE: SP_SelProcBANK 
 */

CREATE PROCEDURE SP_SelProcBANK
(
    @BANKID         char(2))
AS
BEGIN
    SELECT BANKID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM BANK
     WHERE BANKID = @BANKID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcBANK') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcBANK >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcBANK >>>'
go


/* 
 * PROCEDURE: SP_InsProcCITY 
 */

CREATE PROCEDURE SP_InsProcCITY
(
    @CITYID         char(3),
    @STATEID        char(2),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CITY(CITYID,
                     STATEID,
                     DESCR,
                     STKNEW,
                     STKMODIFY,
                     STKDELETE,
                     STKSEND,
                     STKRECEIVE 
                     )
    VALUES(@CITYID,
           @STATEID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCITY: Cannot insert because primary key value not found in CITY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCITY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCITY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCITY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCITY 
 */

CREATE PROCEDURE SP_UpdProcCITY
(
    @CITYID         char(3),
    @STATEID        char(2),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CITY
       SET STATEID         = @STATEID,
           DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE
     WHERE CITYID = @CITYID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCITY: Cannot update  in CITY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCITY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCITY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCITY >>>'
go


/* 
 * PROCEDURE: SP_DelProcCITY 
 */

CREATE PROCEDURE SP_DelProcCITY
(
    @CITYID         char(3))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CITY
     WHERE CITYID = @CITYID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCITY: Cannot delete because foreign keys still exist in CITY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCITY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCITY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCITY >>>'
go


/* 
 * PROCEDURE: SP_SelProcCITY 
 */

CREATE PROCEDURE SP_SelProcCITY
(
    @CITYID         char(3))
AS
BEGIN
    SELECT CITYID,
           STATEID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           TSTAMP
      FROM CITY
     WHERE CITYID = @CITYID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCITY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCITY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCITY >>>'
go


/* 
 * PROCEDURE: SP_InsProcCOMPETITION 
 */

CREATE PROCEDURE SP_InsProcCOMPETITION
(
    @COMPETITIONID       char(2),
    @COMPETITIONNAME     UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1               UD_USER1                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO COMPETITION(COMPETITIONID,
                            COMPETITIONNAME,
                            STKNEW,
                            STKMODIFY,
                            STKDELETE,
                            STKSEND,
                            STKRECEIVE,
                            USER1 
                            )
    VALUES(@COMPETITIONID,
           @COMPETITIONNAME,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCOMPETITION: Cannot insert because primary key value not found in COMPETITION '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCOMPETITION') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCOMPETITION >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCOMPETITION >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCOMPETITION 
 */

CREATE PROCEDURE SP_UpdProcCOMPETITION
(
    @COMPETITIONID       char(2),
    @COMPETITIONNAME     UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1               UD_USER1                  = NULL,
    @TSTAMP              timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE COMPETITION
       SET COMPETITIONNAME      = @COMPETITIONNAME,
           STKNEW               = @STKNEW,
           STKMODIFY            = @STKMODIFY,
           STKDELETE            = @STKDELETE,
           STKSEND              = @STKSEND,
           STKRECEIVE           = @STKRECEIVE,
           USER1                = @USER1
     WHERE COMPETITIONID = @COMPETITIONID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCOMPETITION: Cannot update  in COMPETITION '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCOMPETITION') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCOMPETITION >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCOMPETITION >>>'
go


/* 
 * PROCEDURE: SP_DelProcCOMPETITION 
 */

CREATE PROCEDURE SP_DelProcCOMPETITION
(
    @COMPETITIONID       char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM COMPETITION
     WHERE COMPETITIONID = @COMPETITIONID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCOMPETITION: Cannot delete because foreign keys still exist in COMPETITION '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCOMPETITION') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCOMPETITION >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCOMPETITION >>>'
go


/* 
 * PROCEDURE: SP_SelProcCOMPETITION 
 */

CREATE PROCEDURE SP_SelProcCOMPETITION
(
    @COMPETITIONID       char(2))
AS
BEGIN
    SELECT COMPETITIONID,
           COMPETITIONNAME,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           TSTAMP
      FROM COMPETITION
     WHERE COMPETITIONID = @COMPETITIONID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCOMPETITION') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCOMPETITION >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCOMPETITION >>>'
go


/* 
 * PROCEDURE: SP_InsProcCOUNTRY 
 */

CREATE PROCEDURE SP_InsProcCOUNTRY
(
    @COUNTRYID      char(3),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO COUNTRY(COUNTRYID,
                        DESCR,
                        STKNEW,
                        STKMODIFY,
                        STKDELETE,
                        STKSEND,
                        STKRECEIVE 
                        )
    VALUES(@COUNTRYID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCOUNTRY: Cannot insert because primary key value not found in COUNTRY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCOUNTRY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCOUNTRY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCOUNTRY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCOUNTRY 
 */

CREATE PROCEDURE SP_UpdProcCOUNTRY
(
    @COUNTRYID      char(3),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE COUNTRY
       SET DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE
     WHERE COUNTRYID = @COUNTRYID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCOUNTRY: Cannot update  in COUNTRY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCOUNTRY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCOUNTRY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCOUNTRY >>>'
go


/* 
 * PROCEDURE: SP_DelProcCOUNTRY 
 */

CREATE PROCEDURE SP_DelProcCOUNTRY
(
    @COUNTRYID      char(3))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM COUNTRY
     WHERE COUNTRYID = @COUNTRYID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCOUNTRY: Cannot delete because foreign keys still exist in COUNTRY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCOUNTRY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCOUNTRY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCOUNTRY >>>'
go


/* 
 * PROCEDURE: SP_SelProcCOUNTRY 
 */

CREATE PROCEDURE SP_SelProcCOUNTRY
(
    @COUNTRYID      char(3))
AS
BEGIN
    SELECT COUNTRYID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           TSTAMP
      FROM COUNTRY
     WHERE COUNTRYID = @COUNTRYID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCOUNTRY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCOUNTRY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCOUNTRY >>>'
go


/* 
 * PROCEDURE: SP_InsProcCURRENCY 
 */

CREATE PROCEDURE SP_InsProcCURRENCY
(
    @CURYID         UD_CURYID,
    @DESCR          UD_DESCRIPCION,
    @DESCRMONEY     char(5),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CURRENCY(CURYID,
                         DESCR,
                         DESCRMONEY,
                         STKNEW,
                         STKMODIFY,
                         STKDELETE,
                         STKSEND,
                         STKRECEIVE,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@CURYID,
           @DESCR,
           @DESCRMONEY,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCURRENCY: Cannot insert because primary key value not found in CURRENCY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCURRENCY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCURRENCY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCURRENCY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCURRENCY 
 */

CREATE PROCEDURE SP_UpdProcCURRENCY
(
    @CURYID         UD_CURYID,
    @DESCR          UD_DESCRIPCION,
    @DESCRMONEY     char(5),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CURRENCY
       SET DESCR           = @DESCR,
           DESCRMONEY      = @DESCRMONEY,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE CURYID = @CURYID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCURRENCY: Cannot update  in CURRENCY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCURRENCY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCURRENCY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCURRENCY >>>'
go


/* 
 * PROCEDURE: SP_DelProcCURRENCY 
 */

CREATE PROCEDURE SP_DelProcCURRENCY
(
    @CURYID         UD_CURYID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CURRENCY
     WHERE CURYID = @CURYID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCURRENCY: Cannot delete because foreign keys still exist in CURRENCY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCURRENCY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCURRENCY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCURRENCY >>>'
go


/* 
 * PROCEDURE: SP_SelProcCURRENCY 
 */

CREATE PROCEDURE SP_SelProcCURRENCY
(
    @CURYID         UD_CURYID)
AS
BEGIN
    SELECT CURYID,
           DESCR,
           DESCRMONEY,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CURRENCY
     WHERE CURYID = @CURYID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCURRENCY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCURRENCY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCURRENCY >>>'
go


/* 
 * PROCEDURE: SP_InsProcCURYRATE 
 */

CREATE PROCEDURE SP_InsProcCURYRATE
(
    @CURYTYPEID     UD_CURYTYPEID,
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime,
    @RATE           UD_CURYRATE,
    @OPERADOR       char(1)                  = NULL,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @USER4          UD_USER7                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CURYRATE(CURYTYPEID,
                         FROMCURYID,
                         TOCURYID,
                         CURYDATE,
                         RATE,
                         OPERADOR,
                         USERID,
                         STKSEND,
                         STKRECEIVE,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@CURYTYPEID,
           @FROMCURYID,
           @TOCURYID,
           @CURYDATE,
           @RATE,
           @OPERADOR,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCURYRATE: Cannot insert because primary key value not found in CURYRATE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCURYRATE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCURYRATE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCURYRATE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCURYRATE 
 */

CREATE PROCEDURE SP_UpdProcCURYRATE
(
    @CURYTYPEID     UD_CURYTYPEID,
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime,
    @RATE           UD_CURYRATE,
    @OPERADOR       char(1)                  = NULL,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @USER4          UD_USER7                 = NULL,
    @TSTAMP         timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CURYRATE
       SET RATE            = @RATE,
           OPERADOR        = @OPERADOR,
           USERID          = @USERID,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE CURYTYPEID = @CURYTYPEID
       AND FROMCURYID = @FROMCURYID
       AND TOCURYID   = @TOCURYID
       AND CURYDATE   = @CURYDATE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCURYRATE: Cannot update  in CURYRATE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCURYRATE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCURYRATE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCURYRATE >>>'
go


/* 
 * PROCEDURE: SP_DelProcCURYRATE 
 */

CREATE PROCEDURE SP_DelProcCURYRATE
(
    @CURYTYPEID     UD_CURYTYPEID,
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CURYRATE
     WHERE CURYTYPEID = @CURYTYPEID
       AND FROMCURYID = @FROMCURYID
       AND TOCURYID   = @TOCURYID
       AND CURYDATE   = @CURYDATE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCURYRATE: Cannot delete because foreign keys still exist in CURYRATE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCURYRATE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCURYRATE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCURYRATE >>>'
go


/* 
 * PROCEDURE: SP_SelProcCURYRATE 
 */

CREATE PROCEDURE SP_SelProcCURYRATE
(
    @CURYTYPEID     UD_CURYTYPEID,
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime)
AS
BEGIN
    SELECT CURYTYPEID,
           FROMCURYID,
           TOCURYID,
           CURYDATE,
           RATE,
           OPERADOR,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CURYRATE
     WHERE CURYTYPEID = @CURYTYPEID
       AND FROMCURYID = @FROMCURYID
       AND TOCURYID   = @TOCURYID
       AND CURYDATE   = @CURYDATE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCURYRATE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCURYRATE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCURYRATE >>>'
go


/* 
 * PROCEDURE: SP_InsProcCURYRATETYPE 
 */

CREATE PROCEDURE SP_InsProcCURYRATETYPE
(
    @CURYTYPEID     UD_CURYTYPEID,
    @DESCR          char(30)                 = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @USER4          UD_USER7                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CURYRATETYPE(CURYTYPEID,
                             DESCR,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CURYTYPEID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCURYRATETYPE: Cannot insert because primary key value not found in CURYRATETYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCURYRATETYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCURYRATETYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCURYRATETYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCURYRATETYPE 
 */

CREATE PROCEDURE SP_UpdProcCURYRATETYPE
(
    @CURYTYPEID     UD_CURYTYPEID,
    @DESCR          char(30)                 = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @USER4          UD_USER7                 = NULL,
    @TSTAMP         timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CURYRATETYPE
       SET DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE CURYTYPEID = @CURYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCURYRATETYPE: Cannot update  in CURYRATETYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCURYRATETYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCURYRATETYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCURYRATETYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcCURYRATETYPE 
 */

CREATE PROCEDURE SP_DelProcCURYRATETYPE
(
    @CURYTYPEID     UD_CURYTYPEID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CURYRATETYPE
     WHERE CURYTYPEID = @CURYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCURYRATETYPE: Cannot delete because foreign keys still exist in CURYRATETYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCURYRATETYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCURYRATETYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCURYRATETYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcCURYRATETYPE 
 */

CREATE PROCEDURE SP_SelProcCURYRATETYPE
(
    @CURYTYPEID     UD_CURYTYPEID)
AS
BEGIN
    SELECT CURYTYPEID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CURYRATETYPE
     WHERE CURYTYPEID = @CURYTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCURYRATETYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCURYRATETYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCURYRATETYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcCUSTOMER 
 */

CREATE PROCEDURE SP_InsProcCUSTOMER
(
    @CUSTIDSS         UD_CODCLIENT,
    @DIAPAGID         char(10)                    = NULL,
    @TERMIDSUM        char(2),
    @CUSTID           UD_CODCLIENT,
    @TAXREGNBR        UD_CODCLIENT                = NULL,
    @CUSTNAME         UD_NOMBRE                   = NULL,
    @ADDR1            UD_DIRECCION                = NULL,
    @ADDR2            UD_DIRECCION                = NULL,
    @PHONE            char(10)                    = NULL,
    @FAX              char(10)                    = NULL,
    @CUSTZONEID       char(2)                     = NULL,
    @COUNTRYID        char(3)                     = NULL,
    @STATEID          char(2)                     = NULL,
    @CITYID           char(3)                     = NULL,
    @CUSTCLASSID      char(2),
    @TAXID1           char(2)                     = NULL,
    @TAXID2           UD_TAXID                    = NULL,
    @CURYTYPEID       UD_CURYTYPEID,
    @TERMIDDOC        char(2),
    @DATEORIG         datetime                    = NULL,
    @DATEPROC         datetime,
    @DAYGRACE         int,
    @CURYID           UD_CURYID                   = NULL,
    @SALESPERID       UD_SALESPERSONID            = NULL,
    @USERID           UD_USERID,
    @CUSTSTATUSID     char(1),
    @MTOTOTLIMIT      numeric(11, 4),
    @TOTCURRBAL       numeric(11, 4),
    @AFFECT           bit                         = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                    = NULL,
    @USER2            UD_USER2                    = NULL,
    @USER3            UD_USER3                    = NULL,
    @USER4            UD_USER4                    = NULL,
    @USER5            UD_USER5                    = NULL,
    @USER6            UD_USER6                    = NULL,
    @USER7            UD_USER7                    = NULL,
    @USER8            UD_USER8                    = NULL,
    @USER9            UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTOMER(CUSTIDSS,
                         DIAPAGID,
                         TERMIDSUM,
                         CUSTID,
                         TAXREGNBR,
                         CUSTNAME,
                         ADDR1,
                         ADDR2,
                         PHONE,
                         FAX,
                         CUSTZONEID,
                         COUNTRYID,
                         STATEID,
                         CITYID,
                         CUSTCLASSID,
                         TAXID1,
                         TAXID2,
                         CURYTYPEID,
                         TERMIDDOC,
                         DATEORIG,
                         DATEPROC,
                         DAYGRACE,
                         CURYID,
                         SALESPERID,
                         USERID,
                         CUSTSTATUSID,
                         MTOTOTLIMIT,
                         TOTCURRBAL,
                         AFFECT,
                         STKNEW,
                         STKMODIFY,
                         STKDELETE,
                         STKSEND,
                         STKRECEIVE,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@CUSTIDSS,
           @DIAPAGID,
           @TERMIDSUM,
           @CUSTID,
           @TAXREGNBR,
           @CUSTNAME,
           @ADDR1,
           @ADDR2,
           @PHONE,
           @FAX,
           @CUSTZONEID,
           @COUNTRYID,
           @STATEID,
           @CITYID,
           @CUSTCLASSID,
           @TAXID1,
           @TAXID2,
           @CURYTYPEID,
           @TERMIDDOC,
           @DATEORIG,
           @DATEPROC,
           @DAYGRACE,
           @CURYID,
           @SALESPERID,
           @USERID,
           @CUSTSTATUSID,
           @MTOTOTLIMIT,
           @TOTCURRBAL,
           @AFFECT,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCUSTOMER: Cannot insert because primary key value not found in CUSTOMER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCUSTOMER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCUSTOMER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCUSTOMER >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCUSTOMER 
 */

CREATE PROCEDURE SP_UpdProcCUSTOMER
(
    @CUSTIDSS         UD_CODCLIENT,
    @DIAPAGID         char(10)                    = NULL,
    @TERMIDSUM        char(2),
    @CUSTID           UD_CODCLIENT,
    @TAXREGNBR        UD_CODCLIENT                = NULL,
    @CUSTNAME         UD_NOMBRE                   = NULL,
    @ADDR1            UD_DIRECCION                = NULL,
    @ADDR2            UD_DIRECCION                = NULL,
    @PHONE            char(10)                    = NULL,
    @FAX              char(10)                    = NULL,
    @CUSTZONEID       char(2)                     = NULL,
    @COUNTRYID        char(3)                     = NULL,
    @STATEID          char(2)                     = NULL,
    @CITYID           char(3)                     = NULL,
    @CUSTCLASSID      char(2),
    @TAXID1           char(2)                     = NULL,
    @TAXID2           UD_TAXID                    = NULL,
    @CURYTYPEID       UD_CURYTYPEID,
    @TERMIDDOC        char(2),
    @DATEORIG         datetime                    = NULL,
    @DATEPROC         datetime,
    @DAYGRACE         int,
    @CURYID           UD_CURYID                   = NULL,
    @SALESPERID       UD_SALESPERSONID            = NULL,
    @USERID           UD_USERID,
    @CUSTSTATUSID     char(1),
    @MTOTOTLIMIT      numeric(11, 4),
    @TOTCURRBAL       numeric(11, 4),
    @AFFECT           bit                         = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                    = NULL,
    @USER2            UD_USER2                    = NULL,
    @USER3            UD_USER3                    = NULL,
    @USER4            UD_USER4                    = NULL,
    @USER5            UD_USER5                    = NULL,
    @USER6            UD_USER6                    = NULL,
    @USER7            UD_USER7                    = NULL,
    @USER8            UD_USER8                    = NULL,
    @USER9            UD_USER9                    = NULL,
    @TSTAMP           timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTOMER
       SET DIAPAGID          = @DIAPAGID,
           TERMIDSUM         = @TERMIDSUM,
           CUSTID            = @CUSTID,
           TAXREGNBR         = @TAXREGNBR,
           CUSTNAME          = @CUSTNAME,
           ADDR1             = @ADDR1,
           ADDR2             = @ADDR2,
           PHONE             = @PHONE,
           FAX               = @FAX,
           CUSTZONEID        = @CUSTZONEID,
           COUNTRYID         = @COUNTRYID,
           STATEID           = @STATEID,
           CITYID            = @CITYID,
           CUSTCLASSID       = @CUSTCLASSID,
           TAXID1            = @TAXID1,
           TAXID2            = @TAXID2,
           CURYTYPEID        = @CURYTYPEID,
           TERMIDDOC         = @TERMIDDOC,
           DATEORIG          = @DATEORIG,
           DATEPROC          = @DATEPROC,
           DAYGRACE          = @DAYGRACE,
           CURYID            = @CURYID,
           SALESPERID        = @SALESPERID,
           USERID            = @USERID,
           CUSTSTATUSID      = @CUSTSTATUSID,
           MTOTOTLIMIT       = @MTOTOTLIMIT,
           TOTCURRBAL        = @TOTCURRBAL,
           AFFECT            = @AFFECT,
           STKNEW            = @STKNEW,
           STKMODIFY         = @STKMODIFY,
           STKDELETE         = @STKDELETE,
           STKSEND           = @STKSEND,
           STKRECEIVE        = @STKRECEIVE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4,
           USER5             = @USER5,
           USER6             = @USER6,
           USER7             = @USER7,
           USER8             = @USER8,
           USER9             = @USER9
     WHERE CUSTIDSS = @CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCUSTOMER: Cannot update  in CUSTOMER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCUSTOMER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCUSTOMER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCUSTOMER >>>'
go


/* 
 * PROCEDURE: SP_DelProcCUSTOMER 
 */

CREATE PROCEDURE SP_DelProcCUSTOMER
(
    @CUSTIDSS         UD_CODCLIENT)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUSTOMER
     WHERE CUSTIDSS = @CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCUSTOMER: Cannot delete because foreign keys still exist in CUSTOMER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCUSTOMER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCUSTOMER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCUSTOMER >>>'
go


/* 
 * PROCEDURE: SP_SelProcCUSTOMER 
 */

CREATE PROCEDURE SP_SelProcCUSTOMER
(
    @CUSTIDSS         UD_CODCLIENT)
AS
BEGIN
    SELECT CUSTIDSS,
           DIAPAGID,
           TERMIDSUM,
           CUSTID,
           TAXREGNBR,
           CUSTNAME,
           ADDR1,
           ADDR2,
           PHONE,
           FAX,
           CUSTZONEID,
           COUNTRYID,
           STATEID,
           CITYID,
           CUSTCLASSID,
           TAXID1,
           TAXID2,
           CURYTYPEID,
           TERMIDDOC,
           DATEORIG,
           DATEPROC,
           DAYGRACE,
           CURYID,
           SALESPERID,
           USERID,
           CUSTSTATUSID,
           MTOTOTLIMIT,
           TOTCURRBAL,
           AFFECT,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM CUSTOMER
     WHERE CUSTIDSS = @CUSTIDSS

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCUSTOMER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCUSTOMER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCUSTOMER >>>'
go


/* 
 * PROCEDURE: SP_InsProcCUSTSTATUS 
 */

CREATE PROCEDURE SP_InsProcCUSTSTATUS
(
    @CUSTSTATUSID     char(1),
    @DESCRCUST        UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                  = NULL,
    @USER2            UD_USER2                  = NULL,
    @USER3            UD_USER6                  = NULL,
    @USER4            UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTSTATUS(CUSTSTATUSID,
                           DESCRCUST,
                           STKNEW,
                           STKMODIFY,
                           STKDELETE,
                           STKSEND,
                           STKRECEIVE,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@CUSTSTATUSID,
           @DESCRCUST,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCUSTSTATUS: Cannot insert because primary key value not found in CUSTSTATUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCUSTSTATUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCUSTSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCUSTSTATUS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCUSTSTATUS 
 */

CREATE PROCEDURE SP_UpdProcCUSTSTATUS
(
    @CUSTSTATUSID     char(1),
    @DESCRCUST        UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                  = NULL,
    @USER2            UD_USER2                  = NULL,
    @USER3            UD_USER6                  = NULL,
    @USER4            UD_USER7                  = NULL,
    @TSTAMP           timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTSTATUS
       SET DESCRCUST         = @DESCRCUST,
           STKNEW            = @STKNEW,
           STKMODIFY         = @STKMODIFY,
           STKDELETE         = @STKDELETE,
           STKSEND           = @STKSEND,
           STKRECEIVE        = @STKRECEIVE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE CUSTSTATUSID = @CUSTSTATUSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCUSTSTATUS: Cannot update  in CUSTSTATUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCUSTSTATUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCUSTSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCUSTSTATUS >>>'
go


/* 
 * PROCEDURE: SP_DelProcCUSTSTATUS 
 */

CREATE PROCEDURE SP_DelProcCUSTSTATUS
(
    @CUSTSTATUSID     char(1))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUSTSTATUS
     WHERE CUSTSTATUSID = @CUSTSTATUSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCUSTSTATUS: Cannot delete because foreign keys still exist in CUSTSTATUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCUSTSTATUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCUSTSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCUSTSTATUS >>>'
go


/* 
 * PROCEDURE: SP_SelProcCUSTSTATUS 
 */

CREATE PROCEDURE SP_SelProcCUSTSTATUS
(
    @CUSTSTATUSID     char(1))
AS
BEGIN
    SELECT CUSTSTATUSID,
           DESCRCUST,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CUSTSTATUS
     WHERE CUSTSTATUSID = @CUSTSTATUSID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCUSTSTATUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCUSTSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCUSTSTATUS >>>'
go


/* 
 * PROCEDURE: SP_InsProcCUSTWARRANTY 
 */

CREATE PROCEDURE SP_InsProcCUSTWARRANTY
(
    @CUSTIDSS               UD_CODCLIENT,
    @CUSTWARRANTYTYPEID     char(2),
    @TOTGAR                 numeric(11, 4),
    @DESCRGAR               UD_DESCRIPCION,
    @EXPIREDATE             smalldatetime,
    @CURYID                 UD_CURYID,
    @USERID                 UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @USER3                  UD_USER6                  = NULL,
    @USER4                  UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTWARRANTY(CUSTIDSS,
                             CUSTWARRANTYTYPEID,
                             TOTGAR,
                             DESCRGAR,
                             EXPIREDATE,
                             CURYID,
                             USERID,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CUSTIDSS,
           @CUSTWARRANTYTYPEID,
           @TOTGAR,
           @DESCRGAR,
           @EXPIREDATE,
           @CURYID,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCUSTWARRANTY: Cannot insert because primary key value not found in CUSTWARRANTY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCUSTWARRANTY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCUSTWARRANTY 
 */

CREATE PROCEDURE SP_UpdProcCUSTWARRANTY
(
    @CUSTIDSS               UD_CODCLIENT,
    @CUSTWARRANTYTYPEID     char(2),
    @TOTGAR                 numeric(11, 4),
    @DESCRGAR               UD_DESCRIPCION,
    @EXPIREDATE             smalldatetime,
    @CURYID                 UD_CURYID,
    @USERID                 UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @USER3                  UD_USER6                  = NULL,
    @USER4                  UD_USER7                  = NULL,
    @TSTAMP                 timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTWARRANTY
       SET TOTGAR                  = @TOTGAR,
           DESCRGAR                = @DESCRGAR,
           EXPIREDATE              = @EXPIREDATE,
           CURYID                  = @CURYID,
           USERID                  = @USERID,
           STKNEW                  = @STKNEW,
           STKMODIFY               = @STKMODIFY,
           STKDELETE               = @STKDELETE,
           STKSEND                 = @STKSEND,
           STKRECEIVE              = @STKRECEIVE,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE CUSTIDSS           = @CUSTIDSS
       AND CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCUSTWARRANTY: Cannot update  in CUSTWARRANTY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCUSTWARRANTY >>>'
go


/* 
 * PROCEDURE: SP_DelProcCUSTWARRANTY 
 */

CREATE PROCEDURE SP_DelProcCUSTWARRANTY
(
    @CUSTIDSS               UD_CODCLIENT,
    @CUSTWARRANTYTYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUSTWARRANTY
     WHERE CUSTIDSS           = @CUSTIDSS
       AND CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCUSTWARRANTY: Cannot delete because foreign keys still exist in CUSTWARRANTY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCUSTWARRANTY >>>'
go


/* 
 * PROCEDURE: SP_SelProcCUSTWARRANTY 
 */

CREATE PROCEDURE SP_SelProcCUSTWARRANTY
(
    @CUSTIDSS               UD_CODCLIENT,
    @CUSTWARRANTYTYPEID     char(2))
AS
BEGIN
    SELECT CUSTIDSS,
           CUSTWARRANTYTYPEID,
           TOTGAR,
           DESCRGAR,
           EXPIREDATE,
           CURYID,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CUSTWARRANTY
     WHERE CUSTIDSS           = @CUSTIDSS
       AND CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCUSTWARRANTY >>>'
go


/* 
 * PROCEDURE: SP_InsProcCUSTWARRANTYTYPE 
 */

CREATE PROCEDURE SP_InsProcCUSTWARRANTYTYPE
(
    @CUSTWARRANTYTYPEID     char(2),
    @DECRCUSTWARRANTY       UD_DESCRIPCION,
    @USERID                 UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTWARRANTYTYPE(CUSTWARRANTYTYPEID,
                                 DECRCUSTWARRANTY,
                                 USERID,
                                 STKNEW,
                                 STKMODIFY,
                                 STKDELETE,
                                 STKSEND,
                                 STKRECEIVE,
                                 USER1,
                                 USER2 
                                 )
    VALUES(@CUSTWARRANTYTYPEID,
           @DECRCUSTWARRANTY,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcCUSTWARRANTYTYPE: Cannot insert because primary key value not found in CUSTWARRANTYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcCUSTWARRANTYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcCUSTWARRANTYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcCUSTWARRANTYTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcCUSTWARRANTYTYPE 
 */

CREATE PROCEDURE SP_UpdProcCUSTWARRANTYTYPE
(
    @CUSTWARRANTYTYPEID     char(2),
    @DECRCUSTWARRANTY       UD_DESCRIPCION,
    @USERID                 UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @TSTAMP                 timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTWARRANTYTYPE
       SET DECRCUSTWARRANTY        = @DECRCUSTWARRANTY,
           USERID                  = @USERID,
           STKNEW                  = @STKNEW,
           STKMODIFY               = @STKMODIFY,
           STKDELETE               = @STKDELETE,
           STKSEND                 = @STKSEND,
           STKRECEIVE              = @STKRECEIVE,
           USER1                   = @USER1,
           USER2                   = @USER2
     WHERE CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcCUSTWARRANTYTYPE: Cannot update  in CUSTWARRANTYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcCUSTWARRANTYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcCUSTWARRANTYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcCUSTWARRANTYTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcCUSTWARRANTYTYPE 
 */

CREATE PROCEDURE SP_DelProcCUSTWARRANTYTYPE
(
    @CUSTWARRANTYTYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUSTWARRANTYTYPE
     WHERE CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcCUSTWARRANTYTYPE: Cannot delete because foreign keys still exist in CUSTWARRANTYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCUSTWARRANTYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCUSTWARRANTYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCUSTWARRANTYTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcCUSTWARRANTYTYPE 
 */

CREATE PROCEDURE SP_SelProcCUSTWARRANTYTYPE
(
    @CUSTWARRANTYTYPEID     char(2))
AS
BEGIN
    SELECT CUSTWARRANTYTYPEID,
           DECRCUSTWARRANTY,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           TSTAMP
      FROM CUSTWARRANTYTYPE
     WHERE CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcCUSTWARRANTYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcCUSTWARRANTYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcCUSTWARRANTYTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcDOCTYPE 
 */

CREATE PROCEDURE SP_InsProcDOCTYPE
(
    @DOCTYPEID      UD_DOCTYPEID,
    @DESCR          UD_DESCRIPCION,
    @MINDES         char(5),
    @STKSCREEN      char(1)                   = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO DOCTYPE(DOCTYPEID,
                        DESCR,
                        MINDES,
                        STKSCREEN,
                        STKNEW,
                        STKMODIFY,
                        STKDELETE,
                        STKSEND,
                        STKRECEIVE,
                        USER1,
                        USER2,
                        USER3,
                        USER4 
                        )
    VALUES(@DOCTYPEID,
           @DESCR,
           @MINDES,
           @STKSCREEN,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcDOCTYPE: Cannot insert because primary key value not found in DOCTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcDOCTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcDOCTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcDOCTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcDOCTYPE 
 */

CREATE PROCEDURE SP_UpdProcDOCTYPE
(
    @DOCTYPEID      UD_DOCTYPEID,
    @DESCR          UD_DESCRIPCION,
    @MINDES         char(5),
    @STKSCREEN      char(1)                   = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE DOCTYPE
       SET DESCR           = @DESCR,
           MINDES          = @MINDES,
           STKSCREEN       = @STKSCREEN,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE DOCTYPEID = @DOCTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcDOCTYPE: Cannot update  in DOCTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcDOCTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcDOCTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcDOCTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcDOCTYPE 
 */

CREATE PROCEDURE SP_DelProcDOCTYPE
(
    @DOCTYPEID      UD_DOCTYPEID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM DOCTYPE
     WHERE DOCTYPEID = @DOCTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcDOCTYPE: Cannot delete because foreign keys still exist in DOCTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcDOCTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcDOCTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcDOCTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcDOCTYPE 
 */

CREATE PROCEDURE SP_SelProcDOCTYPE
(
    @DOCTYPEID      UD_DOCTYPEID)
AS
BEGIN
    SELECT DOCTYPEID,
           DESCR,
           MINDES,
           STKSCREEN,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM DOCTYPE
     WHERE DOCTYPEID = @DOCTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcDOCTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcDOCTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcDOCTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_CATEGORY 
 */

CREATE PROCEDURE SP_InsProcIN_CATEGORY
(
    @CLASSID           UD_CATEGORY,
    @DESCR             UD_DESCRIPCION,
    @BUSINESSTYPE      char(1),
    @INVTIDSOLOMON     char(20),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_CATEGORY(CLASSID,
                            DESCR,
                            BUSINESSTYPE,
                            INVTIDSOLOMON,
                            STKNEW,
                            STKMODIFY,
                            STKDELETE,
                            STKSEND,
                            STKRECEIVE,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CLASSID,
           @DESCR,
           @BUSINESSTYPE,
           @INVTIDSOLOMON,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_CATEGORY: Cannot insert because primary key value not found in IN_CATEGORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_CATEGORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_CATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_CATEGORY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_CATEGORY 
 */

CREATE PROCEDURE SP_UpdProcIN_CATEGORY
(
    @CLASSID           UD_CATEGORY,
    @DESCR             UD_DESCRIPCION,
    @BUSINESSTYPE      char(1),
    @INVTIDSOLOMON     char(20),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL,
    @TSTAMP            timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_CATEGORY
       SET DESCR              = @DESCR,
           BUSINESSTYPE       = @BUSINESSTYPE,
           INVTIDSOLOMON      = @INVTIDSOLOMON,
           STKNEW             = @STKNEW,
           STKMODIFY          = @STKMODIFY,
           STKDELETE          = @STKDELETE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE CLASSID = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_CATEGORY: Cannot update  in IN_CATEGORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_CATEGORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_CATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_CATEGORY >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_CATEGORY 
 */

CREATE PROCEDURE SP_DelProcIN_CATEGORY
(
    @CLASSID           UD_CATEGORY)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_CATEGORY
     WHERE CLASSID = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_CATEGORY: Cannot delete because foreign keys still exist in IN_CATEGORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_CATEGORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_CATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_CATEGORY >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_CATEGORY 
 */

CREATE PROCEDURE SP_SelProcIN_CATEGORY
(
    @CLASSID           UD_CATEGORY)
AS
BEGIN
    SELECT CLASSID,
           DESCR,
           BUSINESSTYPE,
           INVTIDSOLOMON,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_CATEGORY
     WHERE CLASSID = @CLASSID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_CATEGORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_CATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_CATEGORY >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_CLASSVENDOR 
 */

CREATE PROCEDURE SP_InsProcIN_CLASSVENDOR
(
    @CLASSVENDORID     char(2),
    @DESCR             UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_CLASSVENDOR(CLASSVENDORID,
                               DESCR,
                               STKNEW,
                               STKMODIFY,
                               STKDELETE,
                               STKSEND,
                               STKRECEIVE,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@CLASSVENDORID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_CLASSVENDOR: Cannot insert because primary key value not found in IN_CLASSVENDOR '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_CLASSVENDOR') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_CLASSVENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_CLASSVENDOR >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_CLASSVENDOR 
 */

CREATE PROCEDURE SP_UpdProcIN_CLASSVENDOR
(
    @CLASSVENDORID     char(2),
    @DESCR             UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL,
    @TSTAMP            timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_CLASSVENDOR
       SET DESCR              = @DESCR,
           STKNEW             = @STKNEW,
           STKMODIFY          = @STKMODIFY,
           STKDELETE          = @STKDELETE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE CLASSVENDORID = @CLASSVENDORID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_CLASSVENDOR: Cannot update  in IN_CLASSVENDOR '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_CLASSVENDOR') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_CLASSVENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_CLASSVENDOR >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_CLASSVENDOR 
 */

CREATE PROCEDURE SP_DelProcIN_CLASSVENDOR
(
    @CLASSVENDORID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_CLASSVENDOR
     WHERE CLASSVENDORID = @CLASSVENDORID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_CLASSVENDOR: Cannot delete because foreign keys still exist in IN_CLASSVENDOR '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_CLASSVENDOR') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_CLASSVENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_CLASSVENDOR >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_CLASSVENDOR 
 */

CREATE PROCEDURE SP_SelProcIN_CLASSVENDOR
(
    @CLASSVENDORID     char(2))
AS
BEGIN
    SELECT CLASSVENDORID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_CLASSVENDOR
     WHERE CLASSVENDORID = @CLASSVENDORID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_CLASSVENDOR') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_CLASSVENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_CLASSVENDOR >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_CLUSTER 
 */

CREATE PROCEDURE SP_InsProcIN_CLUSTER
(
    @CLASSUBID            char(5),
    @CLASSID              char(5),
    @INTRDATEPHISICAL     smalldatetime)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_CLUSTER(CLASSUBID,
                           CLASSID,
                           INTRDATEPHISICAL)
    VALUES(@CLASSUBID,
           @CLASSID,
           @INTRDATEPHISICAL)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_CLUSTER: Cannot insert because primary key value not found in IN_CLUSTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_CLUSTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_CLUSTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_CLUSTER >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_CLUSTER 
 */

CREATE PROCEDURE SP_UpdProcIN_CLUSTER
(
    @CLASSUBID            char(5),
    @CLASSID              char(5),
    @INTRDATEPHISICAL     smalldatetime)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_CLUSTER
       SET INTRDATEPHISICAL      = @INTRDATEPHISICAL
     WHERE CLASSUBID = @CLASSUBID
       AND CLASSID   = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_CLUSTER: Cannot update  in IN_CLUSTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_CLUSTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_CLUSTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_CLUSTER >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_CLUSTER 
 */

CREATE PROCEDURE SP_DelProcIN_CLUSTER
(
    @CLASSUBID            char(5),
    @CLASSID              char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_CLUSTER
     WHERE CLASSUBID = @CLASSUBID
       AND CLASSID   = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_CLUSTER: Cannot delete because foreign keys still exist in IN_CLUSTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_CLUSTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_CLUSTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_CLUSTER >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_CLUSTER 
 */

CREATE PROCEDURE SP_SelProcIN_CLUSTER
(
    @CLASSUBID            char(5),
    @CLASSID              char(5))
AS
BEGIN
    SELECT CLASSUBID,
           CLASSID,
           INTRDATEPHISICAL
      FROM IN_CLUSTER
     WHERE CLASSUBID = @CLASSUBID
       AND CLASSID   = @CLASSID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_CLUSTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_CLUSTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_CLUSTER >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_COMPETITIONPRICE 
 */

CREATE PROCEDURE SP_InsProcIN_COMPETITIONPRICE
(
    @SITEID             char(5),
    @DATEPROCE          smalldatetime,
    @INVTIDSKU          char(20),
    @PRICESUGGESTED     numeric(8, 4),
    @PRICECURRENT       numeric(8, 4),
    @PRICEPREVIOUS      numeric(8, 4),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                 = NULL,
    @USER2              UD_USER2                 = NULL,
    @USER3              UD_USER6                 = NULL,
    @USER4              UD_USER7                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_COMPETITIONPRICE(SITEID,
                                    DATEPROCE,
                                    INVTIDSKU,
                                    PRICESUGGESTED,
                                    PRICECURRENT,
                                    PRICEPREVIOUS,
                                    STKSEND,
                                    STKRECEIVE,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@SITEID,
           @DATEPROCE,
           @INVTIDSKU,
           @PRICESUGGESTED,
           @PRICECURRENT,
           @PRICEPREVIOUS,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_COMPETITIONPRICE: Cannot insert because primary key value not found in IN_COMPETITIONPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_COMPETITIONPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_COMPETITIONPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_COMPETITIONPRICE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_COMPETITIONPRICE 
 */

CREATE PROCEDURE SP_UpdProcIN_COMPETITIONPRICE
(
    @SITEID             char(5),
    @DATEPROCE          smalldatetime,
    @INVTIDSKU          char(20),
    @PRICESUGGESTED     numeric(8, 4),
    @PRICECURRENT       numeric(8, 4),
    @PRICEPREVIOUS      numeric(8, 4),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                 = NULL,
    @USER2              UD_USER2                 = NULL,
    @USER3              UD_USER6                 = NULL,
    @USER4              UD_USER7                 = NULL,
    @TSTAMP             timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_COMPETITIONPRICE
       SET PRICESUGGESTED      = @PRICESUGGESTED,
           PRICECURRENT        = @PRICECURRENT,
           PRICEPREVIOUS       = @PRICEPREVIOUS,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_COMPETITIONPRICE: Cannot update  in IN_COMPETITIONPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_COMPETITIONPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_COMPETITIONPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_COMPETITIONPRICE >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_COMPETITIONPRICE 
 */

CREATE PROCEDURE SP_DelProcIN_COMPETITIONPRICE
(
    @SITEID             char(5),
    @DATEPROCE          smalldatetime,
    @INVTIDSKU          char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_COMPETITIONPRICE
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_COMPETITIONPRICE: Cannot delete because foreign keys still exist in IN_COMPETITIONPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_COMPETITIONPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_COMPETITIONPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_COMPETITIONPRICE >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_COMPETITIONPRICE 
 */

CREATE PROCEDURE SP_SelProcIN_COMPETITIONPRICE
(
    @SITEID             char(5),
    @DATEPROCE          smalldatetime,
    @INVTIDSKU          char(20))
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           INVTIDSKU,
           PRICESUGGESTED,
           PRICECURRENT,
           PRICEPREVIOUS,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_COMPETITIONPRICE
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_COMPETITIONPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_COMPETITIONPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_COMPETITIONPRICE >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_COMPETITIONPRICEDET 
 */

CREATE PROCEDURE SP_InsProcIN_COMPETITIONPRICEDET
(
    @SITEID            UD_IDSITE,
    @DATEPROCE         smalldatetime,
    @COMPETITIONID     char(2),
    @INVTIDSKU         char(20),
    @SLS_PRICE         UD_PRECIO,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                 = NULL,
    @USER2             UD_USER2                 = NULL,
    @USER3             UD_USER6                 = NULL,
    @USER4             UD_USER7                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_COMPETITIONPRICEDET(SITEID,
                                       DATEPROCE,
                                       COMPETITIONID,
                                       INVTIDSKU,
                                       SLS_PRICE,
                                       STKSEND,
                                       STKRECEIVE,
                                       USER1,
                                       USER2,
                                       USER3,
                                       USER4 
                                       )
    VALUES(@SITEID,
           @DATEPROCE,
           @COMPETITIONID,
           @INVTIDSKU,
           @SLS_PRICE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_COMPETITIONPRICEDET: Cannot insert because primary key value not found in IN_COMPETITIONPRICEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_COMPETITIONPRICEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_COMPETITIONPRICEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_COMPETITIONPRICEDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_COMPETITIONPRICEDET 
 */

CREATE PROCEDURE SP_UpdProcIN_COMPETITIONPRICEDET
(
    @SITEID            UD_IDSITE,
    @DATEPROCE         smalldatetime,
    @COMPETITIONID     char(2),
    @INVTIDSKU         char(20),
    @SLS_PRICE         UD_PRECIO,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                 = NULL,
    @USER2             UD_USER2                 = NULL,
    @USER3             UD_USER6                 = NULL,
    @USER4             UD_USER7                 = NULL,
    @TSTAMP            timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_COMPETITIONPRICEDET
       SET SLS_PRICE          = @SLS_PRICE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE SITEID        = @SITEID
       AND DATEPROCE     = @DATEPROCE
       AND COMPETITIONID = @COMPETITIONID
       AND INVTIDSKU     = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_COMPETITIONPRICEDET: Cannot update  in IN_COMPETITIONPRICEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_COMPETITIONPRICEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_COMPETITIONPRICEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_COMPETITIONPRICEDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_COMPETITIONPRICEDET 
 */

CREATE PROCEDURE SP_DelProcIN_COMPETITIONPRICEDET
(
    @SITEID            UD_IDSITE,
    @DATEPROCE         smalldatetime,
    @COMPETITIONID     char(2),
    @INVTIDSKU         char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_COMPETITIONPRICEDET
     WHERE SITEID        = @SITEID
       AND DATEPROCE     = @DATEPROCE
       AND COMPETITIONID = @COMPETITIONID
       AND INVTIDSKU     = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_COMPETITIONPRICEDET: Cannot delete because foreign keys still exist in IN_COMPETITIONPRICEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_COMPETITIONPRICEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_COMPETITIONPRICEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_COMPETITIONPRICEDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_COMPETITIONPRICEDET 
 */

CREATE PROCEDURE SP_SelProcIN_COMPETITIONPRICEDET
(
    @SITEID            UD_IDSITE,
    @DATEPROCE         smalldatetime,
    @COMPETITIONID     char(2),
    @INVTIDSKU         char(20))
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           COMPETITIONID,
           INVTIDSKU,
           SLS_PRICE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_COMPETITIONPRICEDET
     WHERE SITEID        = @SITEID
       AND DATEPROCE     = @DATEPROCE
       AND COMPETITIONID = @COMPETITIONID
       AND INVTIDSKU     = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_COMPETITIONPRICEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_COMPETITIONPRICEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_COMPETITIONPRICEDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_DOCSTATUS 
 */

CREATE PROCEDURE SP_InsProcIN_DOCSTATUS
(
    @DOCSTATUSID     char(2),
    @DESCR           UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                  = NULL,
    @USER2           UD_USER2                  = NULL,
    @USER3           UD_USER6                  = NULL,
    @USER4           UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_DOCSTATUS(DOCSTATUSID,
                             DESCR,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@DOCSTATUSID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_DOCSTATUS: Cannot insert because primary key value not found in IN_DOCSTATUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_DOCSTATUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_DOCSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_DOCSTATUS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_DOCSTATUS 
 */

CREATE PROCEDURE SP_UpdProcIN_DOCSTATUS
(
    @DOCSTATUSID     char(2),
    @DESCR           UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                  = NULL,
    @USER2           UD_USER2                  = NULL,
    @USER3           UD_USER6                  = NULL,
    @USER4           UD_USER7                  = NULL,
    @TSTAMP          timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_DOCSTATUS
       SET DESCR            = @DESCR,
           STKNEW           = @STKNEW,
           STKMODIFY        = @STKMODIFY,
           STKDELETE        = @STKDELETE,
           STKSEND          = @STKSEND,
           STKRECEIVE       = @STKRECEIVE,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE DOCSTATUSID = @DOCSTATUSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_DOCSTATUS: Cannot update  in IN_DOCSTATUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_DOCSTATUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_DOCSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_DOCSTATUS >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_DOCSTATUS 
 */

CREATE PROCEDURE SP_DelProcIN_DOCSTATUS
(
    @DOCSTATUSID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_DOCSTATUS
     WHERE DOCSTATUSID = @DOCSTATUSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_DOCSTATUS: Cannot delete because foreign keys still exist in IN_DOCSTATUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_DOCSTATUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_DOCSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_DOCSTATUS >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_DOCSTATUS 
 */

CREATE PROCEDURE SP_SelProcIN_DOCSTATUS
(
    @DOCSTATUSID     char(2))
AS
BEGIN
    SELECT DOCSTATUSID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_DOCSTATUS
     WHERE DOCSTATUSID = @DOCSTATUSID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_DOCSTATUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_DOCSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_DOCSTATUS >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INTRAHEAD 
 */

CREATE PROCEDURE SP_InsProcIN_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @VENDIDSS          UD_IDVENDOR               = NULL,
    @DOCTYPEID         UD_DOCTYPEID              = NULL,
    @DOCSTATUSID       char(2),
    @REFNBR            char(15),
    @CURYRATE          UD_CAMBIO,
    @TOCURYID          char(3),
    @FROMCURYID        char(3),
    @SITEPROCES        UD_IDSITE,
    @NBRORDER          char(10)                  = NULL,
    @DATENBRORDER      UD_FECHA                  = NULL,
    @USERIDORDER       UD_USERID                 = NULL,
    @INTRDATE          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @SITEDEST          UD_IDSITE,
    @INTRTOTPEN        UD_MONTO,
    @INTRTAXPEN        UD_IMPUESTO,
    @INTRTOTUSD        UD_MONTO,
    @INTRTAXUSD        UD_IMPUESTO,
    @PERPOST           UD_PERPOST,
    @RLSED             bit,
    @DISASSEMBLE       bit,
    @INTRSUBTOTPEN     numeric(11, 4),
    @INTRSUBTOTUSD     numeric(11, 4),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USERID            UD_USERID,
    @STKCLOSED         bit,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER3                  = NULL,
    @USER4             UD_USER5                  = NULL,
    @USER5             UD_USER5                  = NULL,
    @USER6             UD_USER6                  = NULL,
    @USER7             UD_USER7                  = NULL,
    @USER8             UD_USER8                  = NULL,
    @USER9             UD_USER9                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INTRAHEAD(SITEID,
                             BATNBR,
                             TYPEDOCID,
                             VENDIDSS,
                             DOCTYPEID,
                             DOCSTATUSID,
                             REFNBR,
                             CURYRATE,
                             TOCURYID,
                             FROMCURYID,
                             SITEPROCES,
                             NBRORDER,
                             DATENBRORDER,
                             USERIDORDER,
                             INTRDATE,
                             INTRDATEPROCE,
                             SITEDEST,
                             INTRTOTPEN,
                             INTRTAXPEN,
                             INTRTOTUSD,
                             INTRTAXUSD,
                             PERPOST,
                             RLSED,
                             DISASSEMBLE,
                             INTRSUBTOTPEN,
                             INTRSUBTOTUSD,
                             STKSEND,
                             STKRECEIVE,
                             USERID,
                             STKCLOSED,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9 
                             )
    VALUES(@SITEID,
           @BATNBR,
           @TYPEDOCID,
           @VENDIDSS,
           @DOCTYPEID,
           @DOCSTATUSID,
           @REFNBR,
           @CURYRATE,
           @TOCURYID,
           @FROMCURYID,
           @SITEPROCES,
           @NBRORDER,
           @DATENBRORDER,
           @USERIDORDER,
           @INTRDATE,
           @INTRDATEPROCE,
           @SITEDEST,
           @INTRTOTPEN,
           @INTRTAXPEN,
           @INTRTOTUSD,
           @INTRTAXUSD,
           @PERPOST,
           @RLSED,
           @DISASSEMBLE,
           @INTRSUBTOTPEN,
           @INTRSUBTOTUSD,
           @STKSEND,
           @STKRECEIVE,
           @USERID,
           @STKCLOSED,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INTRAHEAD: Cannot insert because primary key value not found in IN_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INTRAHEAD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INTRAHEAD 
 */

CREATE PROCEDURE SP_UpdProcIN_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @VENDIDSS          UD_IDVENDOR               = NULL,
    @DOCTYPEID         UD_DOCTYPEID              = NULL,
    @DOCSTATUSID       char(2),
    @REFNBR            char(15),
    @CURYRATE          UD_CAMBIO,
    @TOCURYID          char(3),
    @FROMCURYID        char(3),
    @SITEPROCES        UD_IDSITE,
    @NBRORDER          char(10)                  = NULL,
    @DATENBRORDER      UD_FECHA                  = NULL,
    @USERIDORDER       UD_USERID                 = NULL,
    @INTRDATE          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @SITEDEST          UD_IDSITE,
    @INTRTOTPEN        UD_MONTO,
    @INTRTAXPEN        UD_IMPUESTO,
    @INTRTOTUSD        UD_MONTO,
    @INTRTAXUSD        UD_IMPUESTO,
    @PERPOST           UD_PERPOST,
    @RLSED             bit,
    @DISASSEMBLE       bit,
    @INTRSUBTOTPEN     numeric(11, 4),
    @INTRSUBTOTUSD     numeric(11, 4),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USERID            UD_USERID,
    @STKCLOSED         bit,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER3                  = NULL,
    @USER4             UD_USER5                  = NULL,
    @USER5             UD_USER5                  = NULL,
    @USER6             UD_USER6                  = NULL,
    @USER7             UD_USER7                  = NULL,
    @USER8             UD_USER8                  = NULL,
    @USER9             UD_USER9                  = NULL,
    @TSTAMP            timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INTRAHEAD
       SET VENDIDSS           = @VENDIDSS,
           DOCTYPEID          = @DOCTYPEID,
           DOCSTATUSID        = @DOCSTATUSID,
           REFNBR             = @REFNBR,
           CURYRATE           = @CURYRATE,
           TOCURYID           = @TOCURYID,
           FROMCURYID         = @FROMCURYID,
           SITEPROCES         = @SITEPROCES,
           NBRORDER           = @NBRORDER,
           DATENBRORDER       = @DATENBRORDER,
           USERIDORDER        = @USERIDORDER,
           INTRDATE           = @INTRDATE,
           INTRDATEPROCE      = @INTRDATEPROCE,
           SITEDEST           = @SITEDEST,
           INTRTOTPEN         = @INTRTOTPEN,
           INTRTAXPEN         = @INTRTAXPEN,
           INTRTOTUSD         = @INTRTOTUSD,
           INTRTAXUSD         = @INTRTAXUSD,
           PERPOST            = @PERPOST,
           RLSED              = @RLSED,
           DISASSEMBLE        = @DISASSEMBLE,
           INTRSUBTOTPEN      = @INTRSUBTOTPEN,
           INTRSUBTOTUSD      = @INTRSUBTOTUSD,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USERID             = @USERID,
           STKCLOSED          = @STKCLOSED,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4,
           USER5              = @USER5,
           USER6              = @USER6,
           USER7              = @USER7,
           USER8              = @USER8,
           USER9              = @USER9
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INTRAHEAD: Cannot update  in IN_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INTRAHEAD >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INTRAHEAD 
 */

CREATE PROCEDURE SP_DelProcIN_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INTRAHEAD
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INTRAHEAD: Cannot delete because foreign keys still exist in IN_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INTRAHEAD >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INTRAHEAD 
 */

CREATE PROCEDURE SP_SelProcIN_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC)
AS
BEGIN
    SELECT SITEID,
           BATNBR,
           TYPEDOCID,
           VENDIDSS,
           DOCTYPEID,
           DOCSTATUSID,
           REFNBR,
           CURYRATE,
           TOCURYID,
           FROMCURYID,
           SITEPROCES,
           NBRORDER,
           DATENBRORDER,
           USERIDORDER,
           INTRDATE,
           INTRDATEPROCE,
           SITEDEST,
           INTRTOTPEN,
           INTRTAXPEN,
           INTRTOTUSD,
           INTRTAXUSD,
           PERPOST,
           RLSED,
           DISASSEMBLE,
           INTRSUBTOTPEN,
           INTRSUBTOTUSD,
           STKSEND,
           STKRECEIVE,
           USERID,
           STKCLOSED,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM IN_INTRAHEAD
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INTRAHEAD >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INTRAITEM 
 */

CREATE PROCEDURE SP_InsProcIN_INTRAITEM
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @SEQUENCE          smallint,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDBARR        UD_CODIGOBARRA              = NULL,
    @QTY               UD_STOCK,
    @TAXIGV            numeric(4, 2)               = NULL,
    @STDCOSTPEN        UD_COSTO,
    @STDCOSTUSD        UD_COSTO,
    @STKFISI           bit,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @RLSED             bit,
    @TAXISC            numeric(4, 2),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER3                    = NULL,
    @USER4             UD_USER5                    = NULL,
    @USER5             UD_USER5                    = NULL,
    @USER6             UD_USER6                    = NULL,
    @USER7             UD_USER7                    = NULL,
    @USER8             UD_USER8                    = NULL,
    @USER9             UD_USER9                    = NULL,
    @BUYCOD            UD_CODIGOBARRA              = NULL,
    @DOCSTATUSID       char(2))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INTRAITEM(SITEID,
                             BATNBR,
                             TYPEDOCID,
                             SEQUENCE,
                             INVTIDSKU,
                             INVTIDBARR,
                             QTY,
                             TAXIGV,
                             STDCOSTPEN,
                             STDCOSTUSD,
                             STKFISI,
                             INTRDATE,
                             INTRDATEPROCE,
                             RLSED,
                             TAXISC,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9,
                             BUYCOD,
                             DOCSTATUSID)
    VALUES(@SITEID,
           @BATNBR,
           @TYPEDOCID,
           @SEQUENCE,
           @INVTIDSKU,
           @INVTIDBARR,
           @QTY,
           @TAXIGV,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @STKFISI,
           @INTRDATE,
           @INTRDATEPROCE,
           @RLSED,
           @TAXISC,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9,
           @BUYCOD,
           @DOCSTATUSID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INTRAITEM: Cannot insert because primary key value not found in IN_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INTRAITEM 
 */

CREATE PROCEDURE SP_UpdProcIN_INTRAITEM
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @SEQUENCE          smallint,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDBARR        UD_CODIGOBARRA              = NULL,
    @QTY               UD_STOCK,
    @TAXIGV            numeric(4, 2)               = NULL,
    @STDCOSTPEN        UD_COSTO,
    @STDCOSTUSD        UD_COSTO,
    @STKFISI           bit,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @RLSED             bit,
    @TAXISC            numeric(4, 2),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER3                    = NULL,
    @USER4             UD_USER5                    = NULL,
    @USER5             UD_USER5                    = NULL,
    @USER6             UD_USER6                    = NULL,
    @USER7             UD_USER7                    = NULL,
    @USER8             UD_USER8                    = NULL,
    @USER9             UD_USER9                    = NULL,
    @TSTAMP            timestamp                   = NULL,
    @BUYCOD            UD_CODIGOBARRA              = NULL,
    @DOCSTATUSID       char(2))
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INTRAITEM
       SET INVTIDSKU          = @INVTIDSKU,
           INVTIDBARR         = @INVTIDBARR,
           QTY                = @QTY,
           TAXIGV             = @TAXIGV,
           STDCOSTPEN         = @STDCOSTPEN,
           STDCOSTUSD         = @STDCOSTUSD,
           STKFISI            = @STKFISI,
           INTRDATE           = @INTRDATE,
           INTRDATEPROCE      = @INTRDATEPROCE,
           RLSED              = @RLSED,
           TAXISC             = @TAXISC,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4,
           USER5              = @USER5,
           USER6              = @USER6,
           USER7              = @USER7,
           USER8              = @USER8,
           USER9              = @USER9,
           BUYCOD             = @BUYCOD,
           DOCSTATUSID        = @DOCSTATUSID
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND SEQUENCE  = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INTRAITEM: Cannot update  in IN_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INTRAITEM 
 */

CREATE PROCEDURE SP_DelProcIN_INTRAITEM
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @SEQUENCE          smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INTRAITEM
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND SEQUENCE  = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INTRAITEM: Cannot delete because foreign keys still exist in IN_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INTRAITEM 
 */

CREATE PROCEDURE SP_SelProcIN_INTRAITEM
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @SEQUENCE          smallint)
AS
BEGIN
    SELECT SITEID,
           BATNBR,
           TYPEDOCID,
           SEQUENCE,
           INVTIDSKU,
           INVTIDBARR,
           QTY,
           TAXIGV,
           STDCOSTPEN,
           STDCOSTUSD,
           STKFISI,
           INTRDATE,
           INTRDATEPROCE,
           RLSED,
           TAXISC,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP,
           BUYCOD,
           DOCSTATUSID
      FROM IN_INTRAITEM
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND SEQUENCE  = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INVENTHISTOPHISI 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTHISTOPHISI
(
    @SITEID               char(5),
    @INVTIDSKU            char(20),
    @INTRDATEPHISICAL     smalldatetime,
    @CLASSID              char(5),
    @CLASSUBID            char(5),
    @STOCKPHISICAL        numeric(10, 3),
    @QTYAVAIL             numeric(10, 3),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTHISTOPHISI(SITEID,
                                    INVTIDSKU,
                                    INTRDATEPHISICAL,
                                    CLASSID,
                                    CLASSUBID,
                                    STOCKPHISICAL,
                                    QTYAVAIL,
                                    STKSEND,
                                    STKRECEIVE)
    VALUES(@SITEID,
           @INVTIDSKU,
           @INTRDATEPHISICAL,
           @CLASSID,
           @CLASSUBID,
           @STOCKPHISICAL,
           @QTYAVAIL,
           @STKSEND,
           @STKRECEIVE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INVENTHISTOPHISI: Cannot insert because primary key value not found in IN_INVENTHISTOPHISI '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INVENTHISTOPHISI') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INVENTHISTOPHISI >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INVENTHISTOPHISI >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INVENTHISTOPHISI 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTHISTOPHISI
(
    @SITEID               char(5),
    @INVTIDSKU            char(20),
    @INTRDATEPHISICAL     smalldatetime,
    @CLASSID              char(5),
    @CLASSUBID            char(5),
    @STOCKPHISICAL        numeric(10, 3),
    @QTYAVAIL             numeric(10, 3),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTHISTOPHISI
       SET CLASSID               = @CLASSID,
           CLASSUBID             = @CLASSUBID,
           STOCKPHISICAL         = @STOCKPHISICAL,
           QTYAVAIL              = @QTYAVAIL,
           STKSEND               = @STKSEND,
           STKRECEIVE            = @STKRECEIVE
     WHERE SITEID           = @SITEID
       AND INVTIDSKU        = @INVTIDSKU
       AND INTRDATEPHISICAL = @INTRDATEPHISICAL

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INVENTHISTOPHISI: Cannot update  in IN_INVENTHISTOPHISI '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INVENTHISTOPHISI') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INVENTHISTOPHISI >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INVENTHISTOPHISI >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INVENTHISTOPHISI 
 */

CREATE PROCEDURE SP_DelProcIN_INVENTHISTOPHISI
(
    @SITEID               char(5),
    @INVTIDSKU            char(20),
    @INTRDATEPHISICAL     smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTHISTOPHISI
     WHERE SITEID           = @SITEID
       AND INVTIDSKU        = @INVTIDSKU
       AND INTRDATEPHISICAL = @INTRDATEPHISICAL

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INVENTHISTOPHISI: Cannot delete because foreign keys still exist in IN_INVENTHISTOPHISI '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INVENTHISTOPHISI') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INVENTHISTOPHISI >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INVENTHISTOPHISI >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INVENTHISTOPHISI 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTHISTOPHISI
(
    @SITEID               char(5),
    @INVTIDSKU            char(20),
    @INTRDATEPHISICAL     smalldatetime)
AS
BEGIN
    SELECT SITEID,
           INVTIDSKU,
           INTRDATEPHISICAL,
           CLASSID,
           CLASSUBID,
           STOCKPHISICAL,
           QTYAVAIL,
           STKSEND,
           STKRECEIVE
      FROM IN_INVENTHISTOPHISI
     WHERE SITEID           = @SITEID
       AND INVTIDSKU        = @INVTIDSKU
       AND INTRDATEPHISICAL = @INTRDATEPHISICAL

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INVENTHISTOPHISI') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INVENTHISTOPHISI >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INVENTHISTOPHISI >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INVENTORY 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORY
(
    @INVTIDSKU       UD_IDCORPORATIVO,
    @TAXISC          UD_TAXID                    = NULL,
    @TAXIGV          UD_TAXID,
    @CLASSID         UD_CATEGORY,
    @CLASSUBID       char(5),
    @INVTYPEID       char(5),
    @STKUNITID       UD_STKUNITID,
    @DESCR           UD_DESCR_INVENT,
    @WTSTK1          numeric(7, 3),
    @STKFUELS        bit,
    @STKSTORE        bit,
    @STKOTHER        bit,
    @STKITEM         bit,
    @KIT             bit,
    @DISCMARGIN      numeric(5, 2),
    @STKRECEPT       bit,
    @STKSALES        bit,
    @STATUS          bit,
    @CONSIGNMENT     bit,
    @DISASSEMBLE     bit,
    @USERID          UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                    = NULL,
    @USER2           UD_USER2                    = NULL,
    @USER3           UD_USER3                    = NULL,
    @USER4           UD_USER5                    = NULL,
    @USER5           UD_USER5                    = NULL,
    @USER6           UD_USER6                    = NULL,
    @USER7           UD_USER7                    = NULL,
    @USER8           UD_USER8                    = NULL,
    @USER9           UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTORY(INVTIDSKU,
                             TAXISC,
                             TAXIGV,
                             CLASSID,
                             CLASSUBID,
                             INVTYPEID,
                             STKUNITID,
                             DESCR,
                             WTSTK1,
                             STKFUELS,
                             STKSTORE,
                             STKOTHER,
                             STKITEM,
                             KIT,
                             DISCMARGIN,
                             STKRECEPT,
                             STKSALES,
                             STATUS,
                             CONSIGNMENT,
                             DISASSEMBLE,
                             USERID,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9 
                             )
    VALUES(@INVTIDSKU,
           @TAXISC,
           @TAXIGV,
           @CLASSID,
           @CLASSUBID,
           @INVTYPEID,
           @STKUNITID,
           @DESCR,
           @WTSTK1,
           @STKFUELS,
           @STKSTORE,
           @STKOTHER,
           @STKITEM,
           @KIT,
           @DISCMARGIN,
           @STKRECEPT,
           @STKSALES,
           @STATUS,
           @CONSIGNMENT,
           @DISASSEMBLE,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INVENTORY: Cannot insert because primary key value not found in IN_INVENTORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INVENTORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INVENTORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INVENTORY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INVENTORY 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORY
(
    @INVTIDSKU       UD_IDCORPORATIVO,
    @TAXISC          UD_TAXID                    = NULL,
    @TAXIGV          UD_TAXID,
    @CLASSID         UD_CATEGORY,
    @CLASSUBID       char(5),
    @INVTYPEID       char(5),
    @STKUNITID       UD_STKUNITID,
    @DESCR           UD_DESCR_INVENT,
    @WTSTK1          numeric(7, 3),
    @STKFUELS        bit,
    @STKSTORE        bit,
    @STKOTHER        bit,
    @STKITEM         bit,
    @KIT             bit,
    @DISCMARGIN      numeric(5, 2),
    @STKRECEPT       bit,
    @STKSALES        bit,
    @STATUS          bit,
    @CONSIGNMENT     bit,
    @DISASSEMBLE     bit,
    @USERID          UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                    = NULL,
    @USER2           UD_USER2                    = NULL,
    @USER3           UD_USER3                    = NULL,
    @USER4           UD_USER5                    = NULL,
    @USER5           UD_USER5                    = NULL,
    @USER6           UD_USER6                    = NULL,
    @USER7           UD_USER7                    = NULL,
    @USER8           UD_USER8                    = NULL,
    @USER9           UD_USER9                    = NULL,
    @TSTAMP          timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTORY
       SET TAXISC           = @TAXISC,
           TAXIGV           = @TAXIGV,
           CLASSID          = @CLASSID,
           CLASSUBID        = @CLASSUBID,
           INVTYPEID        = @INVTYPEID,
           STKUNITID        = @STKUNITID,
           DESCR            = @DESCR,
           WTSTK1           = @WTSTK1,
           STKFUELS         = @STKFUELS,
           STKSTORE         = @STKSTORE,
           STKOTHER         = @STKOTHER,
           STKITEM          = @STKITEM,
           KIT              = @KIT,
           DISCMARGIN       = @DISCMARGIN,
           STKRECEPT        = @STKRECEPT,
           STKSALES         = @STKSALES,
           STATUS           = @STATUS,
           CONSIGNMENT      = @CONSIGNMENT,
           DISASSEMBLE      = @DISASSEMBLE,
           USERID           = @USERID,
           STKNEW           = @STKNEW,
           STKMODIFY        = @STKMODIFY,
           STKDELETE        = @STKDELETE,
           STKSEND          = @STKSEND,
           STKRECEIVE       = @STKRECEIVE,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4,
           USER5            = @USER5,
           USER6            = @USER6,
           USER7            = @USER7,
           USER8            = @USER8,
           USER9            = @USER9
     WHERE INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INVENTORY: Cannot update  in IN_INVENTORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INVENTORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INVENTORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INVENTORY >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INVENTORY 
 */

CREATE PROCEDURE SP_DelProcIN_INVENTORY
(
    @INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTORY
     WHERE INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INVENTORY: Cannot delete because foreign keys still exist in IN_INVENTORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INVENTORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INVENTORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INVENTORY >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INVENTORY 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORY
(
    @INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
    SELECT INVTIDSKU,
           TAXISC,
           TAXIGV,
           CLASSID,
           CLASSUBID,
           INVTYPEID,
           STKUNITID,
           DESCR,
           WTSTK1,
           STKFUELS,
           STKSTORE,
           STKOTHER,
           STKITEM,
           KIT,
           DISCMARGIN,
           STKRECEPT,
           STKSALES,
           STATUS,
           CONSIGNMENT,
           DISASSEMBLE,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM IN_INVENTORY
     WHERE INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INVENTORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INVENTORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INVENTORY >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYALTER
(
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTORYALTER(INVTIDBARR,
                                  INVTIDSKU,
                                  STKNEW,
                                  STKMODIFY,
                                  STKDELETE,
                                  STKSEND,
                                  STKRECEIVE,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@INVTIDBARR,
           @INVTIDSKU,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INVENTORYALTER: Cannot insert because primary key value not found in IN_INVENTORYALTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INVENTORYALTER >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYALTER
(
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL,
    @TSTAMP         timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTORYALTER
       SET INVTIDSKU       = @INVTIDSKU,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE INVTIDBARR = @INVTIDBARR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INVENTORYALTER: Cannot update  in IN_INVENTORYALTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INVENTORYALTER >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_DelProcIN_INVENTORYALTER
(
    @INVTIDBARR     UD_CODIGOBARRA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTORYALTER
     WHERE INVTIDBARR = @INVTIDBARR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INVENTORYALTER: Cannot delete because foreign keys still exist in IN_INVENTORYALTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INVENTORYALTER >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYALTER
(
    @INVTIDBARR     UD_CODIGOBARRA)
AS
BEGIN
    SELECT INVTIDBARR,
           INVTIDSKU,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYALTER
     WHERE INVTIDBARR = @INVTIDBARR

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INVENTORYALTER >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYDAT
(
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           numeric(10, 3),
    @STOCKMAX           numeric(10, 3),
    @STOCKSTAR          numeric(10, 3),
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       smalldatetime               = NULL,
    @ENDDATEINV         smalldatetime               = NULL,
    @LASTINVFIS         UD_STOCK                    = NULL,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @LASTSTDCOSTPEN     numeric(8, 4),
    @LASTSTDCOSTUSD     numeric(8, 4),
    @SLS_PRICE          UD_PRECIO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID                 = NULL,
    @INVTIDSTK          char(20)                    = NULL,
    @INVTIDNOSTK        char(20)                    = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER3                    = NULL,
    @USER4              UD_USER5                    = NULL,
    @USER5              UD_USER5                    = NULL,
    @USER6              UD_USER6                    = NULL,
    @USER7              UD_USER7                    = NULL,
    @USER8              UD_USER8                    = NULL,
    @USER9              UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTORYDAT(INVTIDSKU,
                                SITEID,
                                STOCKMIN,
                                STOCKMAX,
                                STOCKSTAR,
                                QTYAVAIL,
                                STARTDATEINV,
                                ENDDATEINV,
                                LASTINVFIS,
                                AVGCOSTPEN,
                                AVGCOSTUSD,
                                STDCOSTPEN,
                                STDCOSTUSD,
                                LASTSTDCOSTPEN,
                                LASTSTDCOSTUSD,
                                SLS_PRICE,
                                LASTDAYIN,
                                LASTDAYOU,
                                PRECLVID,
                                INVTIDSTK,
                                INVTIDNOSTK,
                                STKNEW,
                                STKMODIFY,
                                STKDELETE,
                                STKSEND,
                                STKRECEIVE,
                                USER1,
                                USER2,
                                USER3,
                                USER4,
                                USER5,
                                USER6,
                                USER7,
                                USER8,
                                USER9 
                                )
    VALUES(@INVTIDSKU,
           @SITEID,
           @STOCKMIN,
           @STOCKMAX,
           @STOCKSTAR,
           @QTYAVAIL,
           @STARTDATEINV,
           @ENDDATEINV,
           @LASTINVFIS,
           @AVGCOSTPEN,
           @AVGCOSTUSD,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @LASTSTDCOSTPEN,
           @LASTSTDCOSTUSD,
           @SLS_PRICE,
           @LASTDAYIN,
           @LASTDAYOU,
           @PRECLVID,
           @INVTIDSTK,
           @INVTIDNOSTK,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INVENTORYDAT: Cannot insert because primary key value not found in IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INVENTORYDAT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYDAT
(
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           numeric(10, 3),
    @STOCKMAX           numeric(10, 3),
    @STOCKSTAR          numeric(10, 3),
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       smalldatetime               = NULL,
    @ENDDATEINV         smalldatetime               = NULL,
    @LASTINVFIS         UD_STOCK                    = NULL,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @LASTSTDCOSTPEN     numeric(8, 4),
    @LASTSTDCOSTUSD     numeric(8, 4),
    @SLS_PRICE          UD_PRECIO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID                 = NULL,
    @INVTIDSTK          char(20)                    = NULL,
    @INVTIDNOSTK        char(20)                    = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER3                    = NULL,
    @USER4              UD_USER5                    = NULL,
    @USER5              UD_USER5                    = NULL,
    @USER6              UD_USER6                    = NULL,
    @USER7              UD_USER7                    = NULL,
    @USER8              UD_USER8                    = NULL,
    @USER9              UD_USER9                    = NULL,
    @TSTAMP             timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTORYDAT
       SET STOCKMIN            = @STOCKMIN,
           STOCKMAX            = @STOCKMAX,
           STOCKSTAR           = @STOCKSTAR,
           QTYAVAIL            = @QTYAVAIL,
           STARTDATEINV        = @STARTDATEINV,
           ENDDATEINV          = @ENDDATEINV,
           LASTINVFIS          = @LASTINVFIS,
           AVGCOSTPEN          = @AVGCOSTPEN,
           AVGCOSTUSD          = @AVGCOSTUSD,
           STDCOSTPEN          = @STDCOSTPEN,
           STDCOSTUSD          = @STDCOSTUSD,
           LASTSTDCOSTPEN      = @LASTSTDCOSTPEN,
           LASTSTDCOSTUSD      = @LASTSTDCOSTUSD,
           SLS_PRICE           = @SLS_PRICE,
           LASTDAYIN           = @LASTDAYIN,
           LASTDAYOU           = @LASTDAYOU,
           PRECLVID            = @PRECLVID,
           INVTIDSTK           = @INVTIDSTK,
           INVTIDNOSTK         = @INVTIDNOSTK,
           STKNEW              = @STKNEW,
           STKMODIFY           = @STKMODIFY,
           STKDELETE           = @STKDELETE,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4,
           USER5               = @USER5,
           USER6               = @USER6,
           USER7               = @USER7,
           USER8               = @USER8,
           USER9               = @USER9
     WHERE INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INVENTORYDAT: Cannot update  in IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INVENTORYDAT >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_DelProcIN_INVENTORYDAT
(
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTORYDAT
     WHERE INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INVENTORYDAT: Cannot delete because foreign keys still exist in IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INVENTORYDAT >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYDAT
(
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE)
AS
BEGIN
    SELECT INVTIDSKU,
           SITEID,
           STOCKMIN,
           STOCKMAX,
           STOCKSTAR,
           QTYAVAIL,
           STARTDATEINV,
           ENDDATEINV,
           LASTINVFIS,
           AVGCOSTPEN,
           AVGCOSTUSD,
           STDCOSTPEN,
           STDCOSTUSD,
           LASTSTDCOSTPEN,
           LASTSTDCOSTUSD,
           SLS_PRICE,
           LASTDAYIN,
           LASTDAYOU,
           PRECLVID,
           INVTIDSTK,
           INVTIDNOSTK,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM IN_INVENTORYDAT
     WHERE INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INVENTORYDAT >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INVENTORYDES 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYDES
(
    @INVTIDSKU        UD_IDCORPORATIVO,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                    = NULL,
    @USER2            UD_USER2                    = NULL,
    @USER3            UD_USER6                    = NULL,
    @USER4            UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTORYDES(INVTIDSKU,
                                INVTIDSKUDES,
                                WTSTK2,
                                STKNEW,
                                STKMODIFY,
                                STKDELETE,
                                STKSEND,
                                STKRECEIVE,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@INVTIDSKU,
           @INVTIDSKUDES,
           @WTSTK2,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INVENTORYDES: Cannot insert because primary key value not found in IN_INVENTORYDES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INVENTORYDES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INVENTORYDES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INVENTORYDES >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INVENTORYDES 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYDES
(
    @INVTIDSKU        UD_IDCORPORATIVO,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                    = NULL,
    @USER2            UD_USER2                    = NULL,
    @USER3            UD_USER6                    = NULL,
    @USER4            UD_USER7                    = NULL,
    @TSTAMP           timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTORYDES
       SET INVTIDSKUDES      = @INVTIDSKUDES,
           WTSTK2            = @WTSTK2,
           STKNEW            = @STKNEW,
           STKMODIFY         = @STKMODIFY,
           STKDELETE         = @STKDELETE,
           STKSEND           = @STKSEND,
           STKRECEIVE        = @STKRECEIVE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INVENTORYDES: Cannot update  in IN_INVENTORYDES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INVENTORYDES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INVENTORYDES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INVENTORYDES >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INVENTORYDES 
 */

CREATE PROCEDURE SP_DelProcIN_INVENTORYDES
(
    @INVTIDSKU        UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTORYDES
     WHERE INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INVENTORYDES: Cannot delete because foreign keys still exist in IN_INVENTORYDES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INVENTORYDES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INVENTORYDES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INVENTORYDES >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INVENTORYDES 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYDES
(
    @INVTIDSKU        UD_IDCORPORATIVO)
AS
BEGIN
    SELECT INVTIDSKU,
           INVTIDSKUDES,
           WTSTK2,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYDES
     WHERE INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INVENTORYDES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INVENTORYDES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INVENTORYDES >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INVENTORYTYPE 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYTYPE
(
    @INVTYPEID      char(5),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTORYTYPE(INVTYPEID,
                                 DESCR,
                                 STKNEW,
                                 STKMODIFY,
                                 STKDELETE,
                                 STKSEND,
                                 STKRECEIVE,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@INVTYPEID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INVENTORYTYPE: Cannot insert because primary key value not found in IN_INVENTORYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INVENTORYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INVENTORYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INVENTORYTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INVENTORYTYPE 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYTYPE
(
    @INVTYPEID      char(5),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTORYTYPE
       SET DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE INVTYPEID = @INVTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INVENTORYTYPE: Cannot update  in IN_INVENTORYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INVENTORYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INVENTORYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INVENTORYTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INVENTORYTYPE 
 */

CREATE PROCEDURE SP_DelProcIN_INVENTORYTYPE
(
    @INVTYPEID      char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTORYTYPE
     WHERE INVTYPEID = @INVTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INVENTORYTYPE: Cannot delete because foreign keys still exist in IN_INVENTORYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INVENTORYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INVENTORYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INVENTORYTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INVENTORYTYPE 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYTYPE
(
    @INVTYPEID      char(5))
AS
BEGIN
    SELECT INVTYPEID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYTYPE
     WHERE INVTYPEID = @INVTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INVENTORYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INVENTORYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INVENTORYTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INVENTORYUNITMED 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYUNITMED
(
    @STKUNITID      UD_STKUNITID,
    @DESCR          UD_DESCRIPCION,
    @STKDIVIDE      bit,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTORYUNITMED(STKUNITID,
                                    DESCR,
                                    STKDIVIDE,
                                    STKNEW,
                                    STKMODIFY,
                                    STKDELETE,
                                    STKSEND,
                                    STKRECEIVE,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@STKUNITID,
           @DESCR,
           @STKDIVIDE,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INVENTORYUNITMED: Cannot insert because primary key value not found in IN_INVENTORYUNITMED '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INVENTORYUNITMED') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INVENTORYUNITMED >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INVENTORYUNITMED >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INVENTORYUNITMED 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYUNITMED
(
    @STKUNITID      UD_STKUNITID,
    @DESCR          UD_DESCRIPCION,
    @STKDIVIDE      bit,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTORYUNITMED
       SET DESCR           = @DESCR,
           STKDIVIDE       = @STKDIVIDE,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE STKUNITID = @STKUNITID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INVENTORYUNITMED: Cannot update  in IN_INVENTORYUNITMED '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INVENTORYUNITMED') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INVENTORYUNITMED >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INVENTORYUNITMED >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INVENTORYUNITMED 
 */

CREATE PROCEDURE SP_DelProcIN_INVENTORYUNITMED
(
    @STKUNITID      UD_STKUNITID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTORYUNITMED
     WHERE STKUNITID = @STKUNITID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INVENTORYUNITMED: Cannot delete because foreign keys still exist in IN_INVENTORYUNITMED '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INVENTORYUNITMED') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INVENTORYUNITMED >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INVENTORYUNITMED >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INVENTORYUNITMED 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYUNITMED
(
    @STKUNITID      UD_STKUNITID)
AS
BEGIN
    SELECT STKUNITID,
           DESCR,
           STKDIVIDE,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYUNITMED
     WHERE STKUNITID = @STKUNITID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INVENTORYUNITMED') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INVENTORYUNITMED >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INVENTORYUNITMED >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_INVENTPHISICAL 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTPHISICAL
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @CLASSID           char(5),
    @CLASSUBID         char(5),
    @STOCKPHISICAL     UD_STOCK)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTPHISICAL(SITEID,
                                  INVTIDSKU,
                                  CLASSID,
                                  CLASSUBID,
                                  STOCKPHISICAL)
    VALUES(@SITEID,
           @INVTIDSKU,
           @CLASSID,
           @CLASSUBID,
           @STOCKPHISICAL)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_INVENTPHISICAL: Cannot insert because primary key value not found in IN_INVENTPHISICAL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_INVENTPHISICAL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_INVENTPHISICAL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_INVENTPHISICAL >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_INVENTPHISICAL 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTPHISICAL
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @CLASSID           char(5),
    @CLASSUBID         char(5),
    @STOCKPHISICAL     UD_STOCK)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTPHISICAL
       SET CLASSID            = @CLASSID,
           CLASSUBID          = @CLASSUBID,
           STOCKPHISICAL      = @STOCKPHISICAL
     WHERE SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_INVENTPHISICAL: Cannot update  in IN_INVENTPHISICAL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_INVENTPHISICAL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_INVENTPHISICAL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_INVENTPHISICAL >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_INVENTPHISICAL 
 */

CREATE PROCEDURE SP_DelProcIN_INVENTPHISICAL
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTPHISICAL
     WHERE SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_INVENTPHISICAL: Cannot delete because foreign keys still exist in IN_INVENTPHISICAL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_INVENTPHISICAL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_INVENTPHISICAL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_INVENTPHISICAL >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_INVENTPHISICAL 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTPHISICAL
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO)
AS
BEGIN
    SELECT SITEID,
           INVTIDSKU,
           CLASSID,
           CLASSUBID,
           STOCKPHISICAL
      FROM IN_INVENTPHISICAL
     WHERE SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_INVENTPHISICAL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_INVENTPHISICAL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_INVENTPHISICAL >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_KDXHIST 
 */

CREATE PROCEDURE SP_InsProcIN_KDXHIST
(
    @YEARHIST        smallint,
    @SITEID          UD_IDSITE,
    @INVTIDSKU       UD_IDCORPORATIVO,
    @QTYEND00        numeric(10, 3),
    @QTYEND01        numeric(10, 3),
    @QTYEND02        numeric(10, 3),
    @QTYEND03        numeric(10, 3),
    @QTYEND04        numeric(10, 3),
    @QTYEND05        numeric(10, 3),
    @QTYEND06        numeric(10, 3),
    @QTYEND07        numeric(10, 3),
    @QTYEND08        numeric(10, 3),
    @QTYEND09        numeric(10, 3),
    @QTYEND10        numeric(10, 3),
    @QTYEND11        numeric(10, 3),
    @QTYEND12        numeric(10, 3),
    @QTYEND13        numeric(10, 3),
    @AVGPEN00        numeric(8, 4),
    @AVGPEN01        numeric(8, 4),
    @AVGPEN02        numeric(8, 4),
    @AVGPEN03        numeric(8, 4),
    @AVGPEN04        numeric(8, 4),
    @AVGPEN06        numeric(8, 4),
    @AVGPEN05        numeric(8, 4),
    @AVGPEN07        numeric(8, 4),
    @AVGPEN08        numeric(8, 4),
    @AVGPEN09        numeric(8, 4),
    @AVGPEN10        numeric(8, 4),
    @AVGPEN11        numeric(8, 4),
    @AVGPEN12        numeric(8, 4),
    @AVGPEN13        numeric(8, 4),
    @AVGUSD00        numeric(8, 4),
    @AVGUSD01        numeric(8, 4),
    @AVGUSD02        numeric(8, 4),
    @AVGUSD03        numeric(8, 4),
    @AVGUSD04        numeric(8, 4),
    @AVGUSD05        numeric(8, 4),
    @AVGUSD06        numeric(8, 4),
    @AVGUSD07        numeric(8, 4),
    @AVGUSD08        numeric(8, 4),
    @AVGUSD09        numeric(8, 4),
    @AVGUSD10        numeric(8, 4),
    @AVGUSD11        numeric(8, 4),
    @AVGUSD12        numeric(8, 4),
    @AVGUSD13        numeric(8, 4),
    @TOTAVGPEN00     numeric(11, 4),
    @TOTAVGPEN01     numeric(11, 4),
    @TOTAVGPEN02     numeric(11, 4),
    @TOTAVGPEN03     numeric(11, 4),
    @TOTAVGPEN04     numeric(11, 4),
    @TOTAVGPEN05     numeric(11, 4),
    @TOTAVGPEN06     numeric(11, 4),
    @TOTAVGPEN07     numeric(11, 4),
    @TOTAVGPEN08     numeric(11, 4),
    @TOTAVGPEN09     numeric(11, 4),
    @TOTAVGPEN10     numeric(11, 4),
    @TOTAVGPEN11     numeric(11, 4),
    @TOTAVGPEN12     numeric(11, 4),
    @TOTAVGPEN13     numeric(11, 4),
    @TOTAVGUSD00     numeric(11, 4),
    @TOTAVGUSD01     numeric(11, 4),
    @TOTAVGUSD02     numeric(11, 4),
    @TOTAVGUSD03     numeric(11, 4),
    @TOTAVGUSD04     numeric(11, 4),
    @TOTAVGUSD05     numeric(11, 4),
    @TOTAVGUSD06     numeric(11, 4),
    @TOTAVGUSD07     numeric(11, 4),
    @TOTAVGUSD08     numeric(11, 4),
    @TOTAVGUSD09     numeric(11, 4),
    @TOTAVGUSD10     numeric(11, 4),
    @TOTAVGUSD11     numeric(11, 4),
    @TOTAVGUSD12     numeric(11, 4),
    @TOTAVGUSD13     numeric(11, 4),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                    = NULL,
    @USER2           UD_USER2                    = NULL,
    @USER3           UD_USER6                    = NULL,
    @USER4           UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_KDXHIST(YEARHIST,
                           SITEID,
                           INVTIDSKU,
                           QTYEND00,
                           QTYEND01,
                           QTYEND02,
                           QTYEND03,
                           QTYEND04,
                           QTYEND05,
                           QTYEND06,
                           QTYEND07,
                           QTYEND08,
                           QTYEND09,
                           QTYEND10,
                           QTYEND11,
                           QTYEND12,
                           QTYEND13,
                           AVGPEN00,
                           AVGPEN01,
                           AVGPEN02,
                           AVGPEN03,
                           AVGPEN04,
                           AVGPEN06,
                           AVGPEN05,
                           AVGPEN07,
                           AVGPEN08,
                           AVGPEN09,
                           AVGPEN10,
                           AVGPEN11,
                           AVGPEN12,
                           AVGPEN13,
                           AVGUSD00,
                           AVGUSD01,
                           AVGUSD02,
                           AVGUSD03,
                           AVGUSD04,
                           AVGUSD05,
                           AVGUSD06,
                           AVGUSD07,
                           AVGUSD08,
                           AVGUSD09,
                           AVGUSD10,
                           AVGUSD11,
                           AVGUSD12,
                           AVGUSD13,
                           TOTAVGPEN00,
                           TOTAVGPEN01,
                           TOTAVGPEN02,
                           TOTAVGPEN03,
                           TOTAVGPEN04,
                           TOTAVGPEN05,
                           TOTAVGPEN06,
                           TOTAVGPEN07,
                           TOTAVGPEN08,
                           TOTAVGPEN09,
                           TOTAVGPEN10,
                           TOTAVGPEN11,
                           TOTAVGPEN12,
                           TOTAVGPEN13,
                           TOTAVGUSD00,
                           TOTAVGUSD01,
                           TOTAVGUSD02,
                           TOTAVGUSD03,
                           TOTAVGUSD04,
                           TOTAVGUSD05,
                           TOTAVGUSD06,
                           TOTAVGUSD07,
                           TOTAVGUSD08,
                           TOTAVGUSD09,
                           TOTAVGUSD10,
                           TOTAVGUSD11,
                           TOTAVGUSD12,
                           TOTAVGUSD13,
                           STKNEW,
                           STKMODIFY,
                           STKDELETE,
                           STKSEND,
                           STKRECEIVE,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@YEARHIST,
           @SITEID,
           @INVTIDSKU,
           @QTYEND00,
           @QTYEND01,
           @QTYEND02,
           @QTYEND03,
           @QTYEND04,
           @QTYEND05,
           @QTYEND06,
           @QTYEND07,
           @QTYEND08,
           @QTYEND09,
           @QTYEND10,
           @QTYEND11,
           @QTYEND12,
           @QTYEND13,
           @AVGPEN00,
           @AVGPEN01,
           @AVGPEN02,
           @AVGPEN03,
           @AVGPEN04,
           @AVGPEN06,
           @AVGPEN05,
           @AVGPEN07,
           @AVGPEN08,
           @AVGPEN09,
           @AVGPEN10,
           @AVGPEN11,
           @AVGPEN12,
           @AVGPEN13,
           @AVGUSD00,
           @AVGUSD01,
           @AVGUSD02,
           @AVGUSD03,
           @AVGUSD04,
           @AVGUSD05,
           @AVGUSD06,
           @AVGUSD07,
           @AVGUSD08,
           @AVGUSD09,
           @AVGUSD10,
           @AVGUSD11,
           @AVGUSD12,
           @AVGUSD13,
           @TOTAVGPEN00,
           @TOTAVGPEN01,
           @TOTAVGPEN02,
           @TOTAVGPEN03,
           @TOTAVGPEN04,
           @TOTAVGPEN05,
           @TOTAVGPEN06,
           @TOTAVGPEN07,
           @TOTAVGPEN08,
           @TOTAVGPEN09,
           @TOTAVGPEN10,
           @TOTAVGPEN11,
           @TOTAVGPEN12,
           @TOTAVGPEN13,
           @TOTAVGUSD00,
           @TOTAVGUSD01,
           @TOTAVGUSD02,
           @TOTAVGUSD03,
           @TOTAVGUSD04,
           @TOTAVGUSD05,
           @TOTAVGUSD06,
           @TOTAVGUSD07,
           @TOTAVGUSD08,
           @TOTAVGUSD09,
           @TOTAVGUSD10,
           @TOTAVGUSD11,
           @TOTAVGUSD12,
           @TOTAVGUSD13,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_KDXHIST: Cannot insert because primary key value not found in IN_KDXHIST '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_KDXHIST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_KDXHIST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_KDXHIST >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_KDXHIST 
 */

CREATE PROCEDURE SP_UpdProcIN_KDXHIST
(
    @YEARHIST        smallint,
    @SITEID          UD_IDSITE,
    @INVTIDSKU       UD_IDCORPORATIVO,
    @QTYEND00        numeric(10, 3),
    @QTYEND01        numeric(10, 3),
    @QTYEND02        numeric(10, 3),
    @QTYEND03        numeric(10, 3),
    @QTYEND04        numeric(10, 3),
    @QTYEND05        numeric(10, 3),
    @QTYEND06        numeric(10, 3),
    @QTYEND07        numeric(10, 3),
    @QTYEND08        numeric(10, 3),
    @QTYEND09        numeric(10, 3),
    @QTYEND10        numeric(10, 3),
    @QTYEND11        numeric(10, 3),
    @QTYEND12        numeric(10, 3),
    @QTYEND13        numeric(10, 3),
    @AVGPEN00        numeric(8, 4),
    @AVGPEN01        numeric(8, 4),
    @AVGPEN02        numeric(8, 4),
    @AVGPEN03        numeric(8, 4),
    @AVGPEN04        numeric(8, 4),
    @AVGPEN06        numeric(8, 4),
    @AVGPEN05        numeric(8, 4),
    @AVGPEN07        numeric(8, 4),
    @AVGPEN08        numeric(8, 4),
    @AVGPEN09        numeric(8, 4),
    @AVGPEN10        numeric(8, 4),
    @AVGPEN11        numeric(8, 4),
    @AVGPEN12        numeric(8, 4),
    @AVGPEN13        numeric(8, 4),
    @AVGUSD00        numeric(8, 4),
    @AVGUSD01        numeric(8, 4),
    @AVGUSD02        numeric(8, 4),
    @AVGUSD03        numeric(8, 4),
    @AVGUSD04        numeric(8, 4),
    @AVGUSD05        numeric(8, 4),
    @AVGUSD06        numeric(8, 4),
    @AVGUSD07        numeric(8, 4),
    @AVGUSD08        numeric(8, 4),
    @AVGUSD09        numeric(8, 4),
    @AVGUSD10        numeric(8, 4),
    @AVGUSD11        numeric(8, 4),
    @AVGUSD12        numeric(8, 4),
    @AVGUSD13        numeric(8, 4),
    @TOTAVGPEN00     numeric(11, 4),
    @TOTAVGPEN01     numeric(11, 4),
    @TOTAVGPEN02     numeric(11, 4),
    @TOTAVGPEN03     numeric(11, 4),
    @TOTAVGPEN04     numeric(11, 4),
    @TOTAVGPEN05     numeric(11, 4),
    @TOTAVGPEN06     numeric(11, 4),
    @TOTAVGPEN07     numeric(11, 4),
    @TOTAVGPEN08     numeric(11, 4),
    @TOTAVGPEN09     numeric(11, 4),
    @TOTAVGPEN10     numeric(11, 4),
    @TOTAVGPEN11     numeric(11, 4),
    @TOTAVGPEN12     numeric(11, 4),
    @TOTAVGPEN13     numeric(11, 4),
    @TOTAVGUSD00     numeric(11, 4),
    @TOTAVGUSD01     numeric(11, 4),
    @TOTAVGUSD02     numeric(11, 4),
    @TOTAVGUSD03     numeric(11, 4),
    @TOTAVGUSD04     numeric(11, 4),
    @TOTAVGUSD05     numeric(11, 4),
    @TOTAVGUSD06     numeric(11, 4),
    @TOTAVGUSD07     numeric(11, 4),
    @TOTAVGUSD08     numeric(11, 4),
    @TOTAVGUSD09     numeric(11, 4),
    @TOTAVGUSD10     numeric(11, 4),
    @TOTAVGUSD11     numeric(11, 4),
    @TOTAVGUSD12     numeric(11, 4),
    @TOTAVGUSD13     numeric(11, 4),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                    = NULL,
    @USER2           UD_USER2                    = NULL,
    @USER3           UD_USER6                    = NULL,
    @USER4           UD_USER7                    = NULL,
    @TSTAMP          timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_KDXHIST
       SET QTYEND00         = @QTYEND00,
           QTYEND01         = @QTYEND01,
           QTYEND02         = @QTYEND02,
           QTYEND03         = @QTYEND03,
           QTYEND04         = @QTYEND04,
           QTYEND05         = @QTYEND05,
           QTYEND06         = @QTYEND06,
           QTYEND07         = @QTYEND07,
           QTYEND08         = @QTYEND08,
           QTYEND09         = @QTYEND09,
           QTYEND10         = @QTYEND10,
           QTYEND11         = @QTYEND11,
           QTYEND12         = @QTYEND12,
           QTYEND13         = @QTYEND13,
           AVGPEN00         = @AVGPEN00,
           AVGPEN01         = @AVGPEN01,
           AVGPEN02         = @AVGPEN02,
           AVGPEN03         = @AVGPEN03,
           AVGPEN04         = @AVGPEN04,
           AVGPEN06         = @AVGPEN06,
           AVGPEN05         = @AVGPEN05,
           AVGPEN07         = @AVGPEN07,
           AVGPEN08         = @AVGPEN08,
           AVGPEN09         = @AVGPEN09,
           AVGPEN10         = @AVGPEN10,
           AVGPEN11         = @AVGPEN11,
           AVGPEN12         = @AVGPEN12,
           AVGPEN13         = @AVGPEN13,
           AVGUSD00         = @AVGUSD00,
           AVGUSD01         = @AVGUSD01,
           AVGUSD02         = @AVGUSD02,
           AVGUSD03         = @AVGUSD03,
           AVGUSD04         = @AVGUSD04,
           AVGUSD05         = @AVGUSD05,
           AVGUSD06         = @AVGUSD06,
           AVGUSD07         = @AVGUSD07,
           AVGUSD08         = @AVGUSD08,
           AVGUSD09         = @AVGUSD09,
           AVGUSD10         = @AVGUSD10,
           AVGUSD11         = @AVGUSD11,
           AVGUSD12         = @AVGUSD12,
           AVGUSD13         = @AVGUSD13,
           TOTAVGPEN00      = @TOTAVGPEN00,
           TOTAVGPEN01      = @TOTAVGPEN01,
           TOTAVGPEN02      = @TOTAVGPEN02,
           TOTAVGPEN03      = @TOTAVGPEN03,
           TOTAVGPEN04      = @TOTAVGPEN04,
           TOTAVGPEN05      = @TOTAVGPEN05,
           TOTAVGPEN06      = @TOTAVGPEN06,
           TOTAVGPEN07      = @TOTAVGPEN07,
           TOTAVGPEN08      = @TOTAVGPEN08,
           TOTAVGPEN09      = @TOTAVGPEN09,
           TOTAVGPEN10      = @TOTAVGPEN10,
           TOTAVGPEN11      = @TOTAVGPEN11,
           TOTAVGPEN12      = @TOTAVGPEN12,
           TOTAVGPEN13      = @TOTAVGPEN13,
           TOTAVGUSD00      = @TOTAVGUSD00,
           TOTAVGUSD01      = @TOTAVGUSD01,
           TOTAVGUSD02      = @TOTAVGUSD02,
           TOTAVGUSD03      = @TOTAVGUSD03,
           TOTAVGUSD04      = @TOTAVGUSD04,
           TOTAVGUSD05      = @TOTAVGUSD05,
           TOTAVGUSD06      = @TOTAVGUSD06,
           TOTAVGUSD07      = @TOTAVGUSD07,
           TOTAVGUSD08      = @TOTAVGUSD08,
           TOTAVGUSD09      = @TOTAVGUSD09,
           TOTAVGUSD10      = @TOTAVGUSD10,
           TOTAVGUSD11      = @TOTAVGUSD11,
           TOTAVGUSD12      = @TOTAVGUSD12,
           TOTAVGUSD13      = @TOTAVGUSD13,
           STKNEW           = @STKNEW,
           STKMODIFY        = @STKMODIFY,
           STKDELETE        = @STKDELETE,
           STKSEND          = @STKSEND,
           STKRECEIVE       = @STKRECEIVE,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE YEARHIST  = @YEARHIST
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_KDXHIST: Cannot update  in IN_KDXHIST '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_KDXHIST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_KDXHIST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_KDXHIST >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_KDXHIST 
 */

CREATE PROCEDURE SP_DelProcIN_KDXHIST
(
    @YEARHIST        smallint,
    @SITEID          UD_IDSITE,
    @INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_KDXHIST
     WHERE YEARHIST  = @YEARHIST
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_KDXHIST: Cannot delete because foreign keys still exist in IN_KDXHIST '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_KDXHIST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_KDXHIST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_KDXHIST >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_KDXHIST 
 */

CREATE PROCEDURE SP_SelProcIN_KDXHIST
(
    @YEARHIST        smallint,
    @SITEID          UD_IDSITE,
    @INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
    SELECT YEARHIST,
           SITEID,
           INVTIDSKU,
           QTYEND00,
           QTYEND01,
           QTYEND02,
           QTYEND03,
           QTYEND04,
           QTYEND05,
           QTYEND06,
           QTYEND07,
           QTYEND08,
           QTYEND09,
           QTYEND10,
           QTYEND11,
           QTYEND12,
           QTYEND13,
           AVGPEN00,
           AVGPEN01,
           AVGPEN02,
           AVGPEN03,
           AVGPEN04,
           AVGPEN06,
           AVGPEN05,
           AVGPEN07,
           AVGPEN08,
           AVGPEN09,
           AVGPEN10,
           AVGPEN11,
           AVGPEN12,
           AVGPEN13,
           AVGUSD00,
           AVGUSD01,
           AVGUSD02,
           AVGUSD03,
           AVGUSD04,
           AVGUSD05,
           AVGUSD06,
           AVGUSD07,
           AVGUSD08,
           AVGUSD09,
           AVGUSD10,
           AVGUSD11,
           AVGUSD12,
           AVGUSD13,
           TOTAVGPEN00,
           TOTAVGPEN01,
           TOTAVGPEN02,
           TOTAVGPEN03,
           TOTAVGPEN04,
           TOTAVGPEN05,
           TOTAVGPEN06,
           TOTAVGPEN07,
           TOTAVGPEN08,
           TOTAVGPEN09,
           TOTAVGPEN10,
           TOTAVGPEN11,
           TOTAVGPEN12,
           TOTAVGPEN13,
           TOTAVGUSD00,
           TOTAVGUSD01,
           TOTAVGUSD02,
           TOTAVGUSD03,
           TOTAVGUSD04,
           TOTAVGUSD05,
           TOTAVGUSD06,
           TOTAVGUSD07,
           TOTAVGUSD08,
           TOTAVGUSD09,
           TOTAVGUSD10,
           TOTAVGUSD11,
           TOTAVGUSD12,
           TOTAVGUSD13,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_KDXHIST
     WHERE YEARHIST  = @YEARHIST
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_KDXHIST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_KDXHIST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_KDXHIST >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_KIT 
 */

CREATE PROCEDURE SP_InsProcIN_KIT
(
    @KITID          UD_KITID,
    @STKUNITID      UD_STKUNITID,
    @CURYID         UD_CURYID,
    @TAXID          UD_TAXID,
    @DESCR          UD_DESCRIPCION,
    @MTOCTOREPS     numeric(8, 4),
    @STDCOSTUSD     numeric(8, 4),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_KIT(KITID,
                       STKUNITID,
                       CURYID,
                       TAXID,
                       DESCR,
                       MTOCTOREPS,
                       STDCOSTUSD,
                       STKNEW,
                       STKMODIFY,
                       STKDELETE,
                       STKSEND,
                       STKRECEIVE,
                       USER1,
                       USER2,
                       USER3,
                       USER4 
                       )
    VALUES(@KITID,
           @STKUNITID,
           @CURYID,
           @TAXID,
           @DESCR,
           @MTOCTOREPS,
           @STDCOSTUSD,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_KIT: Cannot insert because primary key value not found in IN_KIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_KIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_KIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_KIT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_KIT 
 */

CREATE PROCEDURE SP_UpdProcIN_KIT
(
    @KITID          UD_KITID,
    @STKUNITID      UD_STKUNITID,
    @CURYID         UD_CURYID,
    @TAXID          UD_TAXID,
    @DESCR          UD_DESCRIPCION,
    @MTOCTOREPS     numeric(8, 4),
    @STDCOSTUSD     numeric(8, 4),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_KIT
       SET STKUNITID       = @STKUNITID,
           CURYID          = @CURYID,
           TAXID           = @TAXID,
           DESCR           = @DESCR,
           MTOCTOREPS      = @MTOCTOREPS,
           STDCOSTUSD      = @STDCOSTUSD,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE KITID = @KITID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_KIT: Cannot update  in IN_KIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_KIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_KIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_KIT >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_KIT 
 */

CREATE PROCEDURE SP_DelProcIN_KIT
(
    @KITID          UD_KITID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_KIT
     WHERE KITID = @KITID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_KIT: Cannot delete because foreign keys still exist in IN_KIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_KIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_KIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_KIT >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_KIT 
 */

CREATE PROCEDURE SP_SelProcIN_KIT
(
    @KITID          UD_KITID)
AS
BEGIN
    SELECT KITID,
           STKUNITID,
           CURYID,
           TAXID,
           DESCR,
           MTOCTOREPS,
           STDCOSTUSD,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_KIT
     WHERE KITID = @KITID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_KIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_KIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_KIT >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_KITCOMPONENT 
 */

CREATE PROCEDURE SP_InsProcIN_KITCOMPONENT
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     numeric(8, 4),
    @STDCOSTUSD     numeric(8, 4),
    @QTY            numeric(9, 3),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_KITCOMPONENT(KITID,
                                INVTIDSKU,
                                SEQUENCE,
                                STDCOSTPEN,
                                STDCOSTUSD,
                                QTY,
                                STKNEW,
                                STKMODIFY,
                                STKDELETE,
                                STKSEND,
                                STKRECEIVE,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@KITID,
           @INVTIDSKU,
           @SEQUENCE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @QTY,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_KITCOMPONENT: Cannot insert because primary key value not found in IN_KITCOMPONENT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_KITCOMPONENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_KITCOMPONENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_KITCOMPONENT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_KITCOMPONENT 
 */

CREATE PROCEDURE SP_UpdProcIN_KITCOMPONENT
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     numeric(8, 4),
    @STDCOSTUSD     numeric(8, 4),
    @QTY            numeric(9, 3),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL,
    @TSTAMP         timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_KITCOMPONENT
       SET SEQUENCE        = @SEQUENCE,
           STDCOSTPEN      = @STDCOSTPEN,
           STDCOSTUSD      = @STDCOSTUSD,
           QTY             = @QTY,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE KITID     = @KITID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_KITCOMPONENT: Cannot update  in IN_KITCOMPONENT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_KITCOMPONENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_KITCOMPONENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_KITCOMPONENT >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_KITCOMPONENT 
 */

CREATE PROCEDURE SP_DelProcIN_KITCOMPONENT
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_KITCOMPONENT
     WHERE KITID     = @KITID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_KITCOMPONENT: Cannot delete because foreign keys still exist in IN_KITCOMPONENT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_KITCOMPONENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_KITCOMPONENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_KITCOMPONENT >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_KITCOMPONENT 
 */

CREATE PROCEDURE SP_SelProcIN_KITCOMPONENT
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO)
AS
BEGIN
    SELECT KITID,
           INVTIDSKU,
           SEQUENCE,
           STDCOSTPEN,
           STDCOSTUSD,
           QTY,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_KITCOMPONENT
     WHERE KITID     = @KITID
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_KITCOMPONENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_KITCOMPONENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_KITCOMPONENT >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_PRICELEVELTEM 
 */

CREATE PROCEDURE SP_InsProcIN_PRICELEVELTEM
(
    @BATNBRPRICELEVELTEM     UD_CORRELATIVOS,
    @SITEID                  UD_IDSITE,
    @USERIDAUTORIZED01       UD_USERID,
    @DATETIMEAUTORIZED01     smalldatetime,
    @USERIDAUTORIZED02       UD_USERID                  = NULL,
    @DATETIMEAUTORIZED02     smalldatetime,
    @PRECLVID                UD_PRECLVID,
    @STKPRICESS              bit,
    @STARTDATEPRICE          smalldatetime,
    @ENDDATEPRICE            smalldatetime,
    @STARTTIMEPRICE          char(5),
    @ENDTIMEPRICE            char(5),
    @STATUS                  bit,
    @STKPRICESEND            bit,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                   = NULL,
    @USER2                   UD_USER2                   = NULL,
    @USER3                   UD_USER6                   = NULL,
    @USER4                   UD_USER7                   = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_PRICELEVELTEM(BATNBRPRICELEVELTEM,
                                 SITEID,
                                 USERIDAUTORIZED01,
                                 DATETIMEAUTORIZED01,
                                 USERIDAUTORIZED02,
                                 DATETIMEAUTORIZED02,
                                 PRECLVID,
                                 STKPRICESS,
                                 STARTDATEPRICE,
                                 ENDDATEPRICE,
                                 STARTTIMEPRICE,
                                 ENDTIMEPRICE,
                                 STATUS,
                                 STKPRICESEND,
                                 STKNEW,
                                 STKMODIFY,
                                 STKDELETE,
                                 STKSEND,
                                 STKRECEIVE,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@BATNBRPRICELEVELTEM,
           @SITEID,
           @USERIDAUTORIZED01,
           @DATETIMEAUTORIZED01,
           @USERIDAUTORIZED02,
           @DATETIMEAUTORIZED02,
           @PRECLVID,
           @STKPRICESS,
           @STARTDATEPRICE,
           @ENDDATEPRICE,
           @STARTTIMEPRICE,
           @ENDTIMEPRICE,
           @STATUS,
           @STKPRICESEND,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_PRICELEVELTEM: Cannot insert because primary key value not found in IN_PRICELEVELTEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_PRICELEVELTEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_PRICELEVELTEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_PRICELEVELTEM >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_PRICELEVELTEM 
 */

CREATE PROCEDURE SP_UpdProcIN_PRICELEVELTEM
(
    @BATNBRPRICELEVELTEM     UD_CORRELATIVOS,
    @SITEID                  UD_IDSITE,
    @USERIDAUTORIZED01       UD_USERID,
    @DATETIMEAUTORIZED01     smalldatetime,
    @USERIDAUTORIZED02       UD_USERID                  = NULL,
    @DATETIMEAUTORIZED02     smalldatetime,
    @PRECLVID                UD_PRECLVID,
    @STKPRICESS              bit,
    @STARTDATEPRICE          smalldatetime,
    @ENDDATEPRICE            smalldatetime,
    @STARTTIMEPRICE          char(5),
    @ENDTIMEPRICE            char(5),
    @STATUS                  bit,
    @STKPRICESEND            bit,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                   = NULL,
    @USER2                   UD_USER2                   = NULL,
    @USER3                   UD_USER6                   = NULL,
    @USER4                   UD_USER7                   = NULL,
    @TSTAMP                  timestamp                  = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_PRICELEVELTEM
       SET USERIDAUTORIZED01        = @USERIDAUTORIZED01,
           DATETIMEAUTORIZED01      = @DATETIMEAUTORIZED01,
           USERIDAUTORIZED02        = @USERIDAUTORIZED02,
           DATETIMEAUTORIZED02      = @DATETIMEAUTORIZED02,
           PRECLVID                 = @PRECLVID,
           STKPRICESS               = @STKPRICESS,
           STARTDATEPRICE           = @STARTDATEPRICE,
           ENDDATEPRICE             = @ENDDATEPRICE,
           STARTTIMEPRICE           = @STARTTIMEPRICE,
           ENDTIMEPRICE             = @ENDTIMEPRICE,
           STATUS                   = @STATUS,
           STKPRICESEND             = @STKPRICESEND,
           STKNEW                   = @STKNEW,
           STKMODIFY                = @STKMODIFY,
           STKDELETE                = @STKDELETE,
           STKSEND                  = @STKSEND,
           STKRECEIVE               = @STKRECEIVE,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE BATNBRPRICELEVELTEM = @BATNBRPRICELEVELTEM
       AND SITEID              = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_PRICELEVELTEM: Cannot update  in IN_PRICELEVELTEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_PRICELEVELTEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_PRICELEVELTEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_PRICELEVELTEM >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_PRICELEVELTEM 
 */

CREATE PROCEDURE SP_DelProcIN_PRICELEVELTEM
(
    @BATNBRPRICELEVELTEM     UD_CORRELATIVOS,
    @SITEID                  UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_PRICELEVELTEM
     WHERE BATNBRPRICELEVELTEM = @BATNBRPRICELEVELTEM
       AND SITEID              = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_PRICELEVELTEM: Cannot delete because foreign keys still exist in IN_PRICELEVELTEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_PRICELEVELTEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_PRICELEVELTEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_PRICELEVELTEM >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_PRICELEVELTEM 
 */

CREATE PROCEDURE SP_SelProcIN_PRICELEVELTEM
(
    @BATNBRPRICELEVELTEM     UD_CORRELATIVOS,
    @SITEID                  UD_IDSITE)
AS
BEGIN
    SELECT BATNBRPRICELEVELTEM,
           SITEID,
           USERIDAUTORIZED01,
           DATETIMEAUTORIZED01,
           USERIDAUTORIZED02,
           DATETIMEAUTORIZED02,
           PRECLVID,
           STKPRICESS,
           STARTDATEPRICE,
           ENDDATEPRICE,
           STARTTIMEPRICE,
           ENDTIMEPRICE,
           STATUS,
           STKPRICESEND,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_PRICELEVELTEM
     WHERE BATNBRPRICELEVELTEM = @BATNBRPRICELEVELTEM
       AND SITEID              = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_PRICELEVELTEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_PRICELEVELTEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_PRICELEVELTEM >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE SP_InsProcIN_PRICELEVELTEMDET
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  UD_IDSITE,
    @SEQUENCE                smallint,
    @INVTIDSKU               UD_IDCORPORATIVO,
    @DESCR                   UD_DESCRIPCION,
    @SLS_PRICE               numeric(8, 4),
    @DISCMTO                 numeric(11, 4),
    @NEWSLS_PRICE            UD_PRECIO,
    @STDCOST                 numeric(8, 4),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                    = NULL,
    @USER2                   UD_USER2                    = NULL,
    @USER3                   UD_USER6                    = NULL,
    @USER4                   UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_PRICELEVELTEMDET(BATNBRPRICELEVELTEM,
                                    SITEID,
                                    SEQUENCE,
                                    INVTIDSKU,
                                    DESCR,
                                    SLS_PRICE,
                                    DISCMTO,
                                    NEWSLS_PRICE,
                                    STDCOST,
                                    STKNEW,
                                    STKMODIFY,
                                    STKDELETE,
                                    STKSEND,
                                    STKRECEIVE,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@BATNBRPRICELEVELTEM,
           @SITEID,
           @SEQUENCE,
           @INVTIDSKU,
           @DESCR,
           @SLS_PRICE,
           @DISCMTO,
           @NEWSLS_PRICE,
           @STDCOST,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_PRICELEVELTEMDET: Cannot insert because primary key value not found in IN_PRICELEVELTEMDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_PRICELEVELTEMDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_PRICELEVELTEMDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_PRICELEVELTEMDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE SP_UpdProcIN_PRICELEVELTEMDET
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  UD_IDSITE,
    @SEQUENCE                smallint,
    @INVTIDSKU               UD_IDCORPORATIVO,
    @DESCR                   UD_DESCRIPCION,
    @SLS_PRICE               numeric(8, 4),
    @DISCMTO                 numeric(11, 4),
    @NEWSLS_PRICE            UD_PRECIO,
    @STDCOST                 numeric(8, 4),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                    = NULL,
    @USER2                   UD_USER2                    = NULL,
    @USER3                   UD_USER6                    = NULL,
    @USER4                   UD_USER7                    = NULL,
    @TSTAMP                  timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_PRICELEVELTEMDET
       SET INVTIDSKU                = @INVTIDSKU,
           DESCR                    = @DESCR,
           SLS_PRICE                = @SLS_PRICE,
           DISCMTO                  = @DISCMTO,
           NEWSLS_PRICE             = @NEWSLS_PRICE,
           STDCOST                  = @STDCOST,
           STKNEW                   = @STKNEW,
           STKMODIFY                = @STKMODIFY,
           STKDELETE                = @STKDELETE,
           STKSEND                  = @STKSEND,
           STKRECEIVE               = @STKRECEIVE,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE BATNBRPRICELEVELTEM = @BATNBRPRICELEVELTEM
       AND SITEID              = @SITEID
       AND SEQUENCE            = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_PRICELEVELTEMDET: Cannot update  in IN_PRICELEVELTEMDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_PRICELEVELTEMDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_PRICELEVELTEMDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_PRICELEVELTEMDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE SP_DelProcIN_PRICELEVELTEMDET
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  UD_IDSITE,
    @SEQUENCE                smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_PRICELEVELTEMDET
     WHERE BATNBRPRICELEVELTEM = @BATNBRPRICELEVELTEM
       AND SITEID              = @SITEID
       AND SEQUENCE            = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_PRICELEVELTEMDET: Cannot delete because foreign keys still exist in IN_PRICELEVELTEMDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_PRICELEVELTEMDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_PRICELEVELTEMDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_PRICELEVELTEMDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE SP_SelProcIN_PRICELEVELTEMDET
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  UD_IDSITE,
    @SEQUENCE                smallint)
AS
BEGIN
    SELECT BATNBRPRICELEVELTEM,
           SITEID,
           SEQUENCE,
           INVTIDSKU,
           DESCR,
           SLS_PRICE,
           DISCMTO,
           NEWSLS_PRICE,
           STDCOST,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_PRICELEVELTEMDET
     WHERE BATNBRPRICELEVELTEM = @BATNBRPRICELEVELTEM
       AND SITEID              = @SITEID
       AND SEQUENCE            = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_PRICELEVELTEMDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_PRICELEVELTEMDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_PRICELEVELTEMDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_PRICELEVELTYPE 
 */

CREATE PROCEDURE SP_InsProcIN_PRICELEVELTYPE
(
    @PRECLVID       UD_PRECLVID,
    @CURYID         UD_CURYID,
    @CURYTYPEID     UD_CURYTYPEID,
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_PRICELEVELTYPE(PRECLVID,
                                  CURYID,
                                  CURYTYPEID,
                                  DESCR,
                                  STKNEW,
                                  STKMODIFY,
                                  STKDELETE,
                                  STKSEND,
                                  STKRECEIVE,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@PRECLVID,
           @CURYID,
           @CURYTYPEID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_PRICELEVELTYPE: Cannot insert because primary key value not found in IN_PRICELEVELTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_PRICELEVELTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_PRICELEVELTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_PRICELEVELTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_PRICELEVELTYPE 
 */

CREATE PROCEDURE SP_UpdProcIN_PRICELEVELTYPE
(
    @PRECLVID       UD_PRECLVID,
    @CURYID         UD_CURYID,
    @CURYTYPEID     UD_CURYTYPEID,
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_PRICELEVELTYPE
       SET CURYID          = @CURYID,
           CURYTYPEID      = @CURYTYPEID,
           DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE PRECLVID = @PRECLVID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_PRICELEVELTYPE: Cannot update  in IN_PRICELEVELTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_PRICELEVELTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_PRICELEVELTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_PRICELEVELTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_PRICELEVELTYPE 
 */

CREATE PROCEDURE SP_DelProcIN_PRICELEVELTYPE
(
    @PRECLVID       UD_PRECLVID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_PRICELEVELTYPE
     WHERE PRECLVID = @PRECLVID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_PRICELEVELTYPE: Cannot delete because foreign keys still exist in IN_PRICELEVELTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_PRICELEVELTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_PRICELEVELTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_PRICELEVELTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_PRICELEVELTYPE 
 */

CREATE PROCEDURE SP_SelProcIN_PRICELEVELTYPE
(
    @PRECLVID       UD_PRECLVID)
AS
BEGIN
    SELECT PRECLVID,
           CURYID,
           CURYTYPEID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_PRICELEVELTYPE
     WHERE PRECLVID = @PRECLVID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_PRICELEVELTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_PRICELEVELTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_PRICELEVELTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_RESU_INTRAITEM 
 */

CREATE PROCEDURE SP_InsProcIN_RESU_INTRAITEM
(
    @SITEID        UD_IDSITE,
    @BATNBR        char(10),
    @TYPEDOCID     UD_TYPEDOC,
    @CLASSID       UD_IDCORPORATIVO,
    @QTY           numeric(10, 3),
    @COSTPEN       numeric(8, 4),
    @COSTUSD       numeric(8, 4),
    @TOTALPEN      numeric(11, 4),
    @TOTALUSD      numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_RESU_INTRAITEM(SITEID,
                                  BATNBR,
                                  TYPEDOCID,
                                  CLASSID,
                                  QTY,
                                  COSTPEN,
                                  COSTUSD,
                                  TOTALPEN,
                                  TOTALUSD)
    VALUES(@SITEID,
           @BATNBR,
           @TYPEDOCID,
           @CLASSID,
           @QTY,
           @COSTPEN,
           @COSTUSD,
           @TOTALPEN,
           @TOTALUSD)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_RESU_INTRAITEM: Cannot insert because primary key value not found in IN_RESU_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_RESU_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_RESU_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_RESU_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_RESU_INTRAITEM 
 */

CREATE PROCEDURE SP_UpdProcIN_RESU_INTRAITEM
(
    @SITEID        UD_IDSITE,
    @BATNBR        char(10),
    @TYPEDOCID     UD_TYPEDOC,
    @CLASSID       UD_IDCORPORATIVO,
    @QTY           numeric(10, 3),
    @COSTPEN       numeric(8, 4),
    @COSTUSD       numeric(8, 4),
    @TOTALPEN      numeric(11, 4),
    @TOTALUSD      numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_RESU_INTRAITEM
       SET QTY            = @QTY,
           COSTPEN        = @COSTPEN,
           COSTUSD        = @COSTUSD,
           TOTALPEN       = @TOTALPEN,
           TOTALUSD       = @TOTALUSD
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND CLASSID   = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_RESU_INTRAITEM: Cannot update  in IN_RESU_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_RESU_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_RESU_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_RESU_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_RESU_INTRAITEM 
 */

CREATE PROCEDURE SP_DelProcIN_RESU_INTRAITEM
(
    @SITEID        UD_IDSITE,
    @BATNBR        char(10),
    @TYPEDOCID     UD_TYPEDOC,
    @CLASSID       UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_RESU_INTRAITEM
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND CLASSID   = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_RESU_INTRAITEM: Cannot delete because foreign keys still exist in IN_RESU_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_RESU_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_RESU_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_RESU_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_RESU_INTRAITEM 
 */

CREATE PROCEDURE SP_SelProcIN_RESU_INTRAITEM
(
    @SITEID        UD_IDSITE,
    @BATNBR        char(10),
    @TYPEDOCID     UD_TYPEDOC,
    @CLASSID       UD_IDCORPORATIVO)
AS
BEGIN
    SELECT SITEID,
           BATNBR,
           TYPEDOCID,
           CLASSID,
           QTY,
           COSTPEN,
           COSTUSD,
           TOTALPEN,
           TOTALUSD
      FROM IN_RESU_INTRAITEM
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND CLASSID   = @CLASSID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_RESU_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_RESU_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_RESU_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_SETUP 
 */

CREATE PROCEDURE SP_InsProcIN_SETUP
(
    @PERPOST               UD_PERPOST,
    @INTYPETRANSFERIN      UD_TYPEDOC,
    @INTYPETRANSFEROUT     UD_TYPEDOC,
    @REPORTPATH            UD_REPORTES,
    @INTYPESALES           UD_TYPEDOC,
    @INTYPEDESIN           UD_TYPEDOC,
    @INTYPEDESOUT          UD_TYPEDOC,
    @INTYPECREDMEM         UD_TYPEDOC,
    @INTYPEBONUS           UD_TYPEDOC,
    @INTYPEFISIN           UD_TYPEDOC,
    @INTYPEFISOUT          UD_TYPEDOC,
    @INTYPETRBUYSTORE      UD_TYPEDOC,
    @INTYPETRBUYFUELS      UD_TYPEDOC,
    @BATNBRTYPEDOC         char(10),
    @BATNBRTYPEPRICE       char(10),
    @STKFISI               bit,
    @INTYPEQTYINI          UD_TYPEDOC,
    @STKSTOCK              bit,
    @DOCTYPEIDFIS          UD_DOCTYPEID,
    @IDVENDORSS            UD_IDVENDOR,
    @CLOSEDMONTH           char(6),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                = NULL,
    @USER2                 UD_USER2                = NULL,
    @USER3                 UD_USER3                = NULL,
    @USER4                 UD_USER5                = NULL,
    @USER5                 UD_USER5                = NULL,
    @USER6                 UD_USER6                = NULL,
    @USER7                 UD_USER7                = NULL,
    @USER8                 UD_USER8                = NULL,
    @USER9                 UD_USER9                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_SETUP(PERPOST,
                         INTYPETRANSFERIN,
                         INTYPETRANSFEROUT,
                         REPORTPATH,
                         INTYPESALES,
                         INTYPEDESIN,
                         INTYPEDESOUT,
                         INTYPECREDMEM,
                         INTYPEBONUS,
                         INTYPEFISIN,
                         INTYPEFISOUT,
                         INTYPETRBUYSTORE,
                         INTYPETRBUYFUELS,
                         BATNBRTYPEDOC,
                         BATNBRTYPEPRICE,
                         STKFISI,
                         INTYPEQTYINI,
                         STKSTOCK,
                         DOCTYPEIDFIS,
                         IDVENDORSS,
                         CLOSEDMONTH,
                         STKNEW,
                         STKMODIFY,
                         STKDELETE,
                         STKSEND,
                         STKRECEIVE,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9)
    VALUES(@PERPOST,
           @INTYPETRANSFERIN,
           @INTYPETRANSFEROUT,
           @REPORTPATH,
           @INTYPESALES,
           @INTYPEDESIN,
           @INTYPEDESOUT,
           @INTYPECREDMEM,
           @INTYPEBONUS,
           @INTYPEFISIN,
           @INTYPEFISOUT,
           @INTYPETRBUYSTORE,
           @INTYPETRBUYFUELS,
           @BATNBRTYPEDOC,
           @BATNBRTYPEPRICE,
           @STKFISI,
           @INTYPEQTYINI,
           @STKSTOCK,
           @DOCTYPEIDFIS,
           @IDVENDORSS,
           @CLOSEDMONTH,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_SETUP: Cannot insert because primary key value not found in IN_SETUP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_SETUP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_SETUP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_SETUP >>>'
go





/* 
 * PROCEDURE: SP_InsProcIN_SETUP_GEN 
 */

CREATE PROCEDURE SP_InsProcIN_SETUP_GEN
(
    @SITEID                UD_IDSITE,
    @PERPOST               UD_PERPOST,
    @INTYPETRANSFERIN      UD_TYPEDOC,
    @INTYPETRANSFEROUT     UD_TYPEDOC,
    @REPORTPATH            UD_REPORTES,
    @INTYPESALES           UD_TYPEDOC,
    @INTYPEDESIN           UD_TYPEDOC,
    @INTYPEDESOUT          UD_TYPEDOC,
    @INTYPECREDMEM         UD_TYPEDOC,
    @INTYPEBONUS           UD_TYPEDOC,
    @INTYPEFISIN           UD_TYPEDOC,
    @INTYPEFISOUT          UD_TYPEDOC,
    @INTYPETRBUYSTORE      UD_TYPEDOC,
    @INTYPETRBUYFUELS      UD_TYPEDOC,
    @BATNBRTYPEDOC         char(10),
    @BATNBRTYPEPRICE       char(10),
    @STKFISI               bit,
    @INTYPEQTYINI          UD_TYPEDOC,
    @STKSTOCK              bit,
    @DOCTYPEIDFIS          UD_DOCTYPEID,
    @IDVENDORSS            UD_IDVENDOR,
    @CLOSEDMONTH           char(6),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                = NULL,
    @USER2                 UD_USER2                = NULL,
    @USER3                 UD_USER3                = NULL,
    @USER4                 UD_USER5                = NULL,
    @USER5                 UD_USER5                = NULL,
    @USER6                 UD_USER6                = NULL,
    @USER7                 UD_USER7                = NULL,
    @USER8                 UD_USER8                = NULL,
    @USER9                 UD_USER9                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_SETUP_GEN(SITEID,
                             PERPOST,
                             INTYPETRANSFERIN,
                             INTYPETRANSFEROUT,
                             REPORTPATH,
                             INTYPESALES,
                             INTYPEDESIN,
                             INTYPEDESOUT,
                             INTYPECREDMEM,
                             INTYPEBONUS,
                             INTYPEFISIN,
                             INTYPEFISOUT,
                             INTYPETRBUYSTORE,
                             INTYPETRBUYFUELS,
                             BATNBRTYPEDOC,
                             BATNBRTYPEPRICE,
                             STKFISI,
                             INTYPEQTYINI,
                             STKSTOCK,
                             DOCTYPEIDFIS,
                             IDVENDORSS,
                             CLOSEDMONTH,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9)
    VALUES(@SITEID,
           @PERPOST,
           @INTYPETRANSFERIN,
           @INTYPETRANSFEROUT,
           @REPORTPATH,
           @INTYPESALES,
           @INTYPEDESIN,
           @INTYPEDESOUT,
           @INTYPECREDMEM,
           @INTYPEBONUS,
           @INTYPEFISIN,
           @INTYPEFISOUT,
           @INTYPETRBUYSTORE,
           @INTYPETRBUYFUELS,
           @BATNBRTYPEDOC,
           @BATNBRTYPEPRICE,
           @STKFISI,
           @INTYPEQTYINI,
           @STKSTOCK,
           @DOCTYPEIDFIS,
           @IDVENDORSS,
           @CLOSEDMONTH,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_SETUP_GEN: Cannot insert because primary key value not found in IN_SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_SETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_SETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_SETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_SETUP_GEN 
 */

CREATE PROCEDURE SP_UpdProcIN_SETUP_GEN
(
    @SITEID                UD_IDSITE,
    @PERPOST               UD_PERPOST,
    @INTYPETRANSFERIN      UD_TYPEDOC,
    @INTYPETRANSFEROUT     UD_TYPEDOC,
    @TSTAMP                timestamp,
    @REPORTPATH            UD_REPORTES,
    @INTYPESALES           UD_TYPEDOC,
    @INTYPEDESIN           UD_TYPEDOC,
    @INTYPEDESOUT          UD_TYPEDOC,
    @INTYPECREDMEM         UD_TYPEDOC,
    @INTYPEBONUS           UD_TYPEDOC,
    @INTYPEFISIN           UD_TYPEDOC,
    @INTYPEFISOUT          UD_TYPEDOC,
    @INTYPETRBUYSTORE      UD_TYPEDOC,
    @INTYPETRBUYFUELS      UD_TYPEDOC,
    @BATNBRTYPEDOC         char(10),
    @BATNBRTYPEPRICE       char(10),
    @STKFISI               bit,
    @INTYPEQTYINI          UD_TYPEDOC,
    @STKSTOCK              bit,
    @DOCTYPEIDFIS          UD_DOCTYPEID,
    @IDVENDORSS            UD_IDVENDOR,
    @CLOSEDMONTH           char(6),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                = NULL,
    @USER2                 UD_USER2                = NULL,
    @USER3                 UD_USER3                = NULL,
    @USER4                 UD_USER5                = NULL,
    @USER5                 UD_USER5                = NULL,
    @USER6                 UD_USER6                = NULL,
    @USER7                 UD_USER7                = NULL,
    @USER8                 UD_USER8                = NULL,
    @USER9                 UD_USER9                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_SETUP_GEN
       SET PERPOST                = @PERPOST,
           INTYPETRANSFERIN       = @INTYPETRANSFERIN,
           INTYPETRANSFEROUT      = @INTYPETRANSFEROUT,
           REPORTPATH             = @REPORTPATH,
           INTYPESALES            = @INTYPESALES,
           INTYPEDESIN            = @INTYPEDESIN,
           INTYPEDESOUT           = @INTYPEDESOUT,
           INTYPECREDMEM          = @INTYPECREDMEM,
           INTYPEBONUS            = @INTYPEBONUS,
           INTYPEFISIN            = @INTYPEFISIN,
           INTYPEFISOUT           = @INTYPEFISOUT,
           INTYPETRBUYSTORE       = @INTYPETRBUYSTORE,
           INTYPETRBUYFUELS       = @INTYPETRBUYFUELS,
           BATNBRTYPEDOC          = @BATNBRTYPEDOC,
           BATNBRTYPEPRICE        = @BATNBRTYPEPRICE,
           STKFISI                = @STKFISI,
           INTYPEQTYINI           = @INTYPEQTYINI,
           STKSTOCK               = @STKSTOCK,
           DOCTYPEIDFIS           = @DOCTYPEIDFIS,
           IDVENDORSS             = @IDVENDORSS,
           CLOSEDMONTH            = @CLOSEDMONTH,
           STKNEW                 = @STKNEW,
           STKMODIFY              = @STKMODIFY,
           STKDELETE              = @STKDELETE,
           STKSEND                = @STKSEND,
           STKRECEIVE             = @STKRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4,
           USER5                  = @USER5,
           USER6                  = @USER6,
           USER7                  = @USER7,
           USER8                  = @USER8,
           USER9                  = @USER9
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_SETUP_GEN: Cannot update  in IN_SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_SETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_SETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_SETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_SETUP_GEN 
 */

CREATE PROCEDURE SP_DelProcIN_SETUP_GEN
(
    @SITEID                UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_SETUP_GEN
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_SETUP_GEN: Cannot delete because foreign keys still exist in IN_SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_SETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_SETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_SETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_SETUP_GEN 
 */

CREATE PROCEDURE SP_SelProcIN_SETUP_GEN
(
    @SITEID                UD_IDSITE)
AS
BEGIN
    SELECT SITEID,
           PERPOST,
           INTYPETRANSFERIN,
           INTYPETRANSFEROUT,
           TSTAMP,
           REPORTPATH,
           INTYPESALES,
           INTYPEDESIN,
           INTYPEDESOUT,
           INTYPECREDMEM,
           INTYPEBONUS,
           INTYPEFISIN,
           INTYPEFISOUT,
           INTYPETRBUYSTORE,
           INTYPETRBUYFUELS,
           BATNBRTYPEDOC,
           BATNBRTYPEPRICE,
           STKFISI,
           INTYPEQTYINI,
           STKSTOCK,
           DOCTYPEIDFIS,
           IDVENDORSS,
           CLOSEDMONTH,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9
      FROM IN_SETUP_GEN
     WHERE SITEID = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_SETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_SETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_SETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_SUBCATEGORY 
 */

CREATE PROCEDURE SP_InsProcIN_SUBCATEGORY
(
    @CLASSUBID             char(5),
    @CLASSID               UD_CATEGORY,
    @DESCR                 UD_DESCRIPCION,
    @TYPEDOCFISIN          UD_TYPEDOC,
    @TYPEDOCFISOUT         UD_TYPEDOC,
    @PORCENTDIFFERENCE     numeric(5, 2),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_SUBCATEGORY(CLASSUBID,
                               CLASSID,
                               DESCR,
                               TYPEDOCFISIN,
                               TYPEDOCFISOUT,
                               PORCENTDIFFERENCE,
                               STKNEW,
                               STKMODIFY,
                               STKDELETE,
                               STKSEND,
                               STKRECEIVE,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@CLASSUBID,
           @CLASSID,
           @DESCR,
           @TYPEDOCFISIN,
           @TYPEDOCFISOUT,
           @PORCENTDIFFERENCE,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_SUBCATEGORY: Cannot insert because primary key value not found in IN_SUBCATEGORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_SUBCATEGORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_SUBCATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_SUBCATEGORY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_SUBCATEGORY 
 */

CREATE PROCEDURE SP_UpdProcIN_SUBCATEGORY
(
    @CLASSUBID             char(5),
    @CLASSID               UD_CATEGORY,
    @DESCR                 UD_DESCRIPCION,
    @TYPEDOCFISIN          UD_TYPEDOC,
    @TYPEDOCFISOUT         UD_TYPEDOC,
    @PORCENTDIFFERENCE     numeric(5, 2),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_SUBCATEGORY
       SET DESCR                  = @DESCR,
           TYPEDOCFISIN           = @TYPEDOCFISIN,
           TYPEDOCFISOUT          = @TYPEDOCFISOUT,
           PORCENTDIFFERENCE      = @PORCENTDIFFERENCE,
           STKNEW                 = @STKNEW,
           STKMODIFY              = @STKMODIFY,
           STKDELETE              = @STKDELETE,
           STKSEND                = @STKSEND,
           STKRECEIVE             = @STKRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE CLASSUBID = @CLASSUBID
       AND CLASSID   = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_SUBCATEGORY: Cannot update  in IN_SUBCATEGORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_SUBCATEGORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_SUBCATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_SUBCATEGORY >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_SUBCATEGORY 
 */

CREATE PROCEDURE SP_DelProcIN_SUBCATEGORY
(
    @CLASSUBID             char(5),
    @CLASSID               UD_CATEGORY)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_SUBCATEGORY
     WHERE CLASSUBID = @CLASSUBID
       AND CLASSID   = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_SUBCATEGORY: Cannot delete because foreign keys still exist in IN_SUBCATEGORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_SUBCATEGORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_SUBCATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_SUBCATEGORY >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_SUBCATEGORY 
 */

CREATE PROCEDURE SP_SelProcIN_SUBCATEGORY
(
    @CLASSUBID             char(5),
    @CLASSID               UD_CATEGORY)
AS
BEGIN
    SELECT CLASSUBID,
           CLASSID,
           DESCR,
           TYPEDOCFISIN,
           TYPEDOCFISOUT,
           PORCENTDIFFERENCE,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_SUBCATEGORY
     WHERE CLASSUBID = @CLASSUBID
       AND CLASSID   = @CLASSID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_SUBCATEGORY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_SUBCATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_SUBCATEGORY >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_SUMMARY_INTRAHEAD 
 */

CREATE PROCEDURE SP_InsProcIN_SUMMARY_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @DOCTYPEID         UD_DOCTYPEID,
    @REFNBR            char(15),
    @VENDIDSS          UD_IDVENDOR,
    @VENDORID          UD_IDVENDOR,
    @TERMID            char(2),
    @TAXID             UD_TAXID,
    @CURYTYPEID        UD_CURYTYPEID,
    @CURYRATE          UD_CAMBIO,
    @SITEPROCES        UD_IDSITE,
    @SITEDEST          UD_IDSITE,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @FROMCURYID        char(3),
    @INTRTAXPEN        numeric(9, 2),
    @INTRTAXUSD        numeric(9, 2),
    @INTRTOTPEN        numeric(11, 4),
    @INTRTOTUSD        numeric(11, 4),
    @STKSEND        bit=0,
    @STKRECEIVE        bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_SUMMARY_INTRAHEAD(SITEID,
                                     BATNBR,
                                     TYPEDOCID,
                                     DOCTYPEID,
                                     REFNBR,
                                     VENDIDSS,
                                     VENDORID,
                                     TERMID,
                                     TAXID,
                                     CURYTYPEID,
                                     CURYRATE,
                                     SITEPROCES,
                                     SITEDEST,
                                     INTRDATE,
                                     INTRDATEPROCE,
                                     FROMCURYID,
                                     INTRTAXPEN,
                                     INTRTAXUSD,
                                     INTRTOTPEN,
                                     INTRTOTUSD,
                                     STKSEND,
                                     STKRECEIVE)
    VALUES(@SITEID,
           @BATNBR,
           @TYPEDOCID,
           @DOCTYPEID,
           @REFNBR,
           @VENDIDSS,
           @VENDORID,
           @TERMID,
           @TAXID,
           @CURYTYPEID,
           @CURYRATE,
           @SITEPROCES,
           @SITEDEST,
           @INTRDATE,
           @INTRDATEPROCE,
           @FROMCURYID,
           @INTRTAXPEN,
           @INTRTAXUSD,
           @INTRTOTPEN,
           @INTRTOTUSD,
           @STKSEND,
           @STKRECEIVE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_SUMMARY_INTRAHEAD: Cannot insert because primary key value not found in IN_SUMMARY_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_SUMMARY_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_SUMMARY_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_SUMMARY_INTRAHEAD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_SUMMARY_INTRAHEAD 
 */

CREATE PROCEDURE SP_UpdProcIN_SUMMARY_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @DOCTYPEID         UD_DOCTYPEID,
    @REFNBR            char(15),
    @VENDIDSS          UD_IDVENDOR,
    @VENDORID          UD_IDVENDOR,
    @TERMID            char(2),
    @TAXID             UD_TAXID,
    @CURYTYPEID        UD_CURYTYPEID,
    @CURYRATE          UD_CAMBIO,
    @SITEPROCES        UD_IDSITE,
    @SITEDEST          UD_IDSITE,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @FROMCURYID        char(3),
    @INTRTAXPEN        numeric(9, 2),
    @INTRTAXUSD        numeric(9, 2),
    @INTRTOTPEN        numeric(11, 4),
    @INTRTOTUSD        numeric(11, 4),
    @STKSEND        bit=0,
    @STKRECEIVE        bit)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_SUMMARY_INTRAHEAD
       SET DOCTYPEID          = @DOCTYPEID,
           REFNBR             = @REFNBR,
           VENDIDSS           = @VENDIDSS,
           VENDORID           = @VENDORID,
           TERMID             = @TERMID,
           TAXID              = @TAXID,
           CURYTYPEID         = @CURYTYPEID,
           CURYRATE           = @CURYRATE,
           SITEPROCES         = @SITEPROCES,
           SITEDEST           = @SITEDEST,
           INTRDATE           = @INTRDATE,
           INTRDATEPROCE      = @INTRDATEPROCE,
           FROMCURYID         = @FROMCURYID,
           INTRTAXPEN         = @INTRTAXPEN,
           INTRTAXUSD         = @INTRTAXUSD,
           INTRTOTPEN         = @INTRTOTPEN,
           INTRTOTUSD         = @INTRTOTUSD,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_SUMMARY_INTRAHEAD: Cannot update  in IN_SUMMARY_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_SUMMARY_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_SUMMARY_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_SUMMARY_INTRAHEAD >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_SUMMARY_INTRAHEAD 
 */

CREATE PROCEDURE SP_DelProcIN_SUMMARY_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_SUMMARY_INTRAHEAD
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_SUMMARY_INTRAHEAD: Cannot delete because foreign keys still exist in IN_SUMMARY_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_SUMMARY_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_SUMMARY_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_SUMMARY_INTRAHEAD >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_SUMMARY_INTRAHEAD 
 */

CREATE PROCEDURE SP_SelProcIN_SUMMARY_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC)
AS
BEGIN
    SELECT SITEID,
           BATNBR,
           TYPEDOCID,
           DOCTYPEID,
           REFNBR,
           VENDIDSS,
           VENDORID,
           TERMID,
           TAXID,
           CURYTYPEID,
           CURYRATE,
           SITEPROCES,
           SITEDEST,
           INTRDATE,
           INTRDATEPROCE,
           FROMCURYID,
           INTRTAXPEN,
           INTRTAXUSD,
           INTRTOTPEN,
           INTRTOTUSD,
           STKSEND,
           STKRECEIVE
      FROM IN_SUMMARY_INTRAHEAD
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_SUMMARY_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_SUMMARY_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_SUMMARY_INTRAHEAD >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_SUMMARY_INTRAITEM 
 */

CREATE PROCEDURE SP_InsProcIN_SUMMARY_INTRAITEM
(
    @SITEID            char(5),
    @BATNBR            char(10),
    @TYPEDOCID         char(5),
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDSOLOMON     char(20),
    @INTRDATEPROCE     smalldatetime,
    @QTY               numeric(10, 3),
    @STDCOSTPEN        numeric(8, 4),
    @STDCOSTUSD        numeric(8, 4),
    @INTRSUBTOTPEN     numeric(11, 4),
    @INTRSUBTOTUSD     numeric(11, 4),
    @STKSEND        bit=0,
    @STKRECEIVE        bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_SUMMARY_INTRAITEM(SITEID,
                                     BATNBR,
                                     TYPEDOCID,
                                     INVTIDSKU,
                                     INVTIDSOLOMON,
                                     INTRDATEPROCE,
                                     QTY,
                                     STDCOSTPEN,
                                     STDCOSTUSD,
                                     INTRSUBTOTPEN,
                                     INTRSUBTOTUSD,
                                     STKSEND,
                                     STKRECEIVE)
    VALUES(@SITEID,
           @BATNBR,
           @TYPEDOCID,
           @INVTIDSKU,
           @INVTIDSOLOMON,
           @INTRDATEPROCE,
           @QTY,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INTRSUBTOTPEN,
           @INTRSUBTOTUSD,
           @STKSEND,
           @STKRECEIVE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_SUMMARY_INTRAITEM: Cannot insert because primary key value not found in IN_SUMMARY_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_SUMMARY_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_SUMMARY_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_SUMMARY_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_SUMMARY_INTRAITEM 
 */

CREATE PROCEDURE SP_UpdProcIN_SUMMARY_INTRAITEM
(
    @SITEID            char(5),
    @BATNBR            char(10),
    @TYPEDOCID         char(5),
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDSOLOMON     char(20),
    @INTRDATEPROCE     smalldatetime,
    @QTY               numeric(10, 3),
    @STDCOSTPEN        numeric(8, 4),
    @STDCOSTUSD        numeric(8, 4),
    @INTRSUBTOTPEN     numeric(11, 4),
    @INTRSUBTOTUSD     numeric(11, 4),
    @STKSEND        bit=0,
    @STKRECEIVE        bit)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_SUMMARY_INTRAITEM
       SET INVTIDSOLOMON      = @INVTIDSOLOMON,
           INTRDATEPROCE      = @INTRDATEPROCE,
           QTY                = @QTY,
           STDCOSTPEN         = @STDCOSTPEN,
           STDCOSTUSD         = @STDCOSTUSD,
           INTRSUBTOTPEN      = @INTRSUBTOTPEN,
           INTRSUBTOTUSD      = @INTRSUBTOTUSD,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_SUMMARY_INTRAITEM: Cannot update  in IN_SUMMARY_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_SUMMARY_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_SUMMARY_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_SUMMARY_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_SUMMARY_INTRAITEM 
 */

CREATE PROCEDURE SP_DelProcIN_SUMMARY_INTRAITEM
(
    @SITEID            char(5),
    @BATNBR            char(10),
    @TYPEDOCID         char(5),
    @INVTIDSKU         UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_SUMMARY_INTRAITEM
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_SUMMARY_INTRAITEM: Cannot delete because foreign keys still exist in IN_SUMMARY_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_SUMMARY_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_SUMMARY_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_SUMMARY_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_SUMMARY_INTRAITEM 
 */

CREATE PROCEDURE SP_SelProcIN_SUMMARY_INTRAITEM
(
    @SITEID            char(5),
    @BATNBR            char(10),
    @TYPEDOCID         char(5),
    @INVTIDSKU         UD_IDCORPORATIVO)
AS
BEGIN
    SELECT SITEID,
           BATNBR,
           TYPEDOCID,
           INVTIDSKU,
           INVTIDSOLOMON,
           INTRDATEPROCE,
           QTY,
           STDCOSTPEN,
           STDCOSTUSD,
           INTRSUBTOTPEN,
           INTRSUBTOTUSD,
           STKSEND,
           STKRECEIVE
      FROM IN_SUMMARY_INTRAITEM
     WHERE SITEID    = @SITEID
       AND BATNBR    = @BATNBR
       AND TYPEDOCID = @TYPEDOCID
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_SUMMARY_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_SUMMARY_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_SUMMARY_INTRAITEM >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_TYPEDOC 
 */

CREATE PROCEDURE SP_InsProcIN_TYPEDOC
(
    @TYPEDOCID       UD_TYPEDOC,
    @DESCR           UD_DESCRIPCION,
    @INOUT           bit,
    @STKVALUE        bit,
    @STKCOSPRICE     bit,
    @STKAVGCOST      bit,
    @PRECLVLID       UD_PRECLVID               = NULL,
    @STKBUY          bit,
    @STKVENDOR       bit,
    @STKAVERAGE      bit,
    @DESCR_ABR       char(12),
    @STKDISPLAY      char(1),
    @STKNEW         bit=1,
    @MODBUY          char(1),
    @MODCXP          char(1),
    @MODINV          char(1),
    @TYPEINV         char(3),
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                  = NULL,
    @USER2           UD_USER2                  = NULL,
    @USER3           UD_USER6                  = NULL,
    @USER4           UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_TYPEDOC(TYPEDOCID,
                           DESCR,
                           INOUT,
                           STKVALUE,
                           STKCOSPRICE,
                           STKAVGCOST,
                           PRECLVLID,
                           STKBUY,
                           STKVENDOR,
                           STKAVERAGE,
                           DESCR_ABR,
                           STKDISPLAY,
                           STKNEW,
                           MODBUY,
                           MODCXP,
                           MODINV,
                           TYPEINV,
                           STKMODIFY,
                           STKDELETE,
                           STKSEND,
                           STKRECEIVE,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@TYPEDOCID,
           @DESCR,
           @INOUT,
           @STKVALUE,
           @STKCOSPRICE,
           @STKAVGCOST,
           @PRECLVLID,
           @STKBUY,
           @STKVENDOR,
           @STKAVERAGE,
           @DESCR_ABR,
           @STKDISPLAY,
           @STKNEW,
           @MODBUY,
           @MODCXP,
           @MODINV,
           @TYPEINV,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_TYPEDOC: Cannot insert because primary key value not found in IN_TYPEDOC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_TYPEDOC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_TYPEDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_TYPEDOC >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_TYPEDOC 
 */

CREATE PROCEDURE SP_UpdProcIN_TYPEDOC
(
    @TYPEDOCID       UD_TYPEDOC,
    @DESCR           UD_DESCRIPCION,
    @INOUT           bit,
    @STKVALUE        bit,
    @STKCOSPRICE     bit,
    @STKAVGCOST      bit,
    @PRECLVLID       UD_PRECLVID               = NULL,
    @STKBUY          bit,
    @STKVENDOR       bit,
    @STKAVERAGE      bit,
    @DESCR_ABR       char(12),
    @STKDISPLAY      char(1),
    @STKNEW         bit=1,
    @MODBUY          char(1),
    @MODCXP          char(1),
    @MODINV          char(1),
    @TYPEINV         char(3),
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                  = NULL,
    @USER2           UD_USER2                  = NULL,
    @USER3           UD_USER6                  = NULL,
    @USER4           UD_USER7                  = NULL,
    @TSTAMP          timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_TYPEDOC
       SET DESCR            = @DESCR,
           INOUT            = @INOUT,
           STKVALUE         = @STKVALUE,
           STKCOSPRICE      = @STKCOSPRICE,
           STKAVGCOST       = @STKAVGCOST,
           PRECLVLID        = @PRECLVLID,
           STKBUY           = @STKBUY,
           STKVENDOR        = @STKVENDOR,
           STKAVERAGE       = @STKAVERAGE,
           DESCR_ABR        = @DESCR_ABR,
           STKDISPLAY       = @STKDISPLAY,
           STKNEW           = @STKNEW,
           MODBUY           = @MODBUY,
           MODCXP           = @MODCXP,
           MODINV           = @MODINV,
           TYPEINV          = @TYPEINV,
           STKMODIFY        = @STKMODIFY,
           STKDELETE        = @STKDELETE,
           STKSEND          = @STKSEND,
           STKRECEIVE       = @STKRECEIVE,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE TYPEDOCID = @TYPEDOCID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_TYPEDOC: Cannot update  in IN_TYPEDOC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_TYPEDOC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_TYPEDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_TYPEDOC >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_TYPEDOC 
 */

CREATE PROCEDURE SP_DelProcIN_TYPEDOC
(
    @TYPEDOCID       UD_TYPEDOC)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_TYPEDOC
     WHERE TYPEDOCID = @TYPEDOCID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_TYPEDOC: Cannot delete because foreign keys still exist in IN_TYPEDOC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_TYPEDOC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_TYPEDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_TYPEDOC >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_TYPEDOC 
 */

CREATE PROCEDURE SP_SelProcIN_TYPEDOC
(
    @TYPEDOCID       UD_TYPEDOC)
AS
BEGIN
    SELECT TYPEDOCID,
           DESCR,
           INOUT,
           STKVALUE,
           STKCOSPRICE,
           STKAVGCOST,
           PRECLVLID,
           STKBUY,
           STKVENDOR,
           STKAVERAGE,
           DESCR_ABR,
           STKDISPLAY,
           STKNEW,
           MODBUY,
           MODCXP,
           MODINV,
           TYPEINV,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_TYPEDOC
     WHERE TYPEDOCID = @TYPEDOCID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_TYPEDOC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_TYPEDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_TYPEDOC >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_VENDOR 
 */

CREATE PROCEDURE SP_InsProcIN_VENDOR
(
    @VENDIDSS          UD_IDVENDOR,
    @CITYID            char(3),
    @COUNTRYID         char(3),
    @CLASSVENDORID     char(2),
    @TERMID            char(2),
    @VENDORID          UD_IDVENDOR,
    @DESCR             UD_DESCRIPCION,
    @CONTACT           UD_NOMBRE                 = NULL,
    @TAXREGNBR         varchar(20)               = NULL,
    @ADDRES            UD_DIRECCION,
    @ADDRES2           UD_DIRECCION              = NULL,
    @FONO1             UD_TELEFONO               = NULL,
    @FONO2             UD_TELEFONO               = NULL,
    @FAX               UD_TELEFONO               = NULL,
    @MAIL              varchar(40)               = NULL,
    @STATUS            char(1),
    @CURRBAL           numeric(9, 2),
    @CURYID            char(3),
    @CURYTYPEID        char(6),
    @TAXID             char(2),
    @USERID            UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_VENDOR(VENDIDSS,
                          CITYID,
                          COUNTRYID,
                          CLASSVENDORID,
                          TERMID,
                          VENDORID,
                          DESCR,
                          CONTACT,
                          TAXREGNBR,
                          ADDRES,
                          ADDRES2,
                          FONO1,
                          FONO2,
                          FAX,
                          MAIL,
                          STATUS,
                          CURRBAL,
                          CURYID,
                          CURYTYPEID,
                          TAXID,
                          USERID,
                          STKNEW,
                          STKMODIFY,
                          STKDELETE,
                          STKSEND,
                          STKRECEIVE,
                          USER1,
                          USER2,
                          USER3,
                          USER4 
                          )
    VALUES(@VENDIDSS,
           @CITYID,
           @COUNTRYID,
           @CLASSVENDORID,
           @TERMID,
           @VENDORID,
           @DESCR,
           @CONTACT,
           @TAXREGNBR,
           @ADDRES,
           @ADDRES2,
           @FONO1,
           @FONO2,
           @FAX,
           @MAIL,
           @STATUS,
           @CURRBAL,
           @CURYID,
           @CURYTYPEID,
           @TAXID,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_VENDOR: Cannot insert because primary key value not found in IN_VENDOR '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_VENDOR') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_VENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_VENDOR >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_VENDOR 
 */

CREATE PROCEDURE SP_UpdProcIN_VENDOR
(
    @VENDIDSS          UD_IDVENDOR,
    @CITYID            char(3),
    @COUNTRYID         char(3),
    @CLASSVENDORID     char(2),
    @TERMID            char(2),
    @VENDORID          UD_IDVENDOR,
    @DESCR             UD_DESCRIPCION,
    @CONTACT           UD_NOMBRE                 = NULL,
    @TAXREGNBR         varchar(20)               = NULL,
    @ADDRES            UD_DIRECCION,
    @ADDRES2           UD_DIRECCION              = NULL,
    @FONO1             UD_TELEFONO               = NULL,
    @FONO2             UD_TELEFONO               = NULL,
    @FAX               UD_TELEFONO               = NULL,
    @MAIL              varchar(40)               = NULL,
    @STATUS            char(1),
    @CURRBAL           numeric(9, 2),
    @CURYID            char(3),
    @CURYTYPEID        char(6),
    @TAXID             char(2),
    @USERID            UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL,
    @TSTAMP            timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_VENDOR
       SET CITYID             = @CITYID,
           COUNTRYID          = @COUNTRYID,
           CLASSVENDORID      = @CLASSVENDORID,
           TERMID             = @TERMID,
           VENDORID           = @VENDORID,
           DESCR              = @DESCR,
           CONTACT            = @CONTACT,
           TAXREGNBR          = @TAXREGNBR,
           ADDRES             = @ADDRES,
           ADDRES2            = @ADDRES2,
           FONO1              = @FONO1,
           FONO2              = @FONO2,
           FAX                = @FAX,
           MAIL               = @MAIL,
           STATUS             = @STATUS,
           CURRBAL            = @CURRBAL,
           CURYID             = @CURYID,
           CURYTYPEID         = @CURYTYPEID,
           TAXID              = @TAXID,
           USERID             = @USERID,
           STKNEW             = @STKNEW,
           STKMODIFY          = @STKMODIFY,
           STKDELETE          = @STKDELETE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE VENDIDSS = @VENDIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_VENDOR: Cannot update  in IN_VENDOR '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_VENDOR') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_VENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_VENDOR >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_VENDOR 
 */

CREATE PROCEDURE SP_DelProcIN_VENDOR
(
    @VENDIDSS          UD_IDVENDOR)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_VENDOR
     WHERE VENDIDSS = @VENDIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_VENDOR: Cannot delete because foreign keys still exist in IN_VENDOR '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_VENDOR') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_VENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_VENDOR >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_VENDOR 
 */

CREATE PROCEDURE SP_SelProcIN_VENDOR
(
    @VENDIDSS          UD_IDVENDOR)
AS
BEGIN
    SELECT VENDIDSS,
           CITYID,
           COUNTRYID,
           CLASSVENDORID,
           TERMID,
           VENDORID,
           DESCR,
           CONTACT,
           TAXREGNBR,
           ADDRES,
           ADDRES2,
           FONO1,
           FONO2,
           FAX,
           MAIL,
           STATUS,
           CURRBAL,
           CURYID,
           CURYTYPEID,
           TAXID,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_VENDOR
     WHERE VENDIDSS = @VENDIDSS

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_VENDOR') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_VENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_VENDOR >>>'
go


/* 
 * PROCEDURE: SP_InsProcIN_VENDORINVT 
 */

CREATE PROCEDURE SP_InsProcIN_VENDORINVT
(
    @VENDIDSS       char(20),
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SITEID         UD_IDSITE,
    @BUYCOD         varchar(20),
    @DESCR          UD_DESCRIPCION,
    @STDCOST        numeric(8, 4),
    @CURYID         UD_CURYID,
    @BUYDATE        smalldatetime               = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_VENDORINVT(VENDIDSS,
                              INVTIDSKU,
                              SITEID,
                              BUYCOD,
                              DESCR,
                              STDCOST,
                              CURYID,
                              BUYDATE,
                              STKNEW,
                              STKMODIFY,
                              STKDELETE,
                              STKSEND,
                              STKRECEIVE,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@VENDIDSS,
           @INVTIDSKU,
           @SITEID,
           @BUYCOD,
           @DESCR,
           @STDCOST,
           @CURYID,
           @BUYDATE,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcIN_VENDORINVT: Cannot insert because primary key value not found in IN_VENDORINVT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcIN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcIN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcIN_VENDORINVT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcIN_VENDORINVT 
 */

CREATE PROCEDURE SP_UpdProcIN_VENDORINVT
(
    @VENDIDSS       char(20),
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SITEID         UD_IDSITE,
    @BUYCOD         varchar(20),
    @DESCR          UD_DESCRIPCION,
    @STDCOST        numeric(8, 4),
    @CURYID         UD_CURYID,
    @BUYDATE        smalldatetime               = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL,
    @TSTAMP         timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_VENDORINVT
       SET BUYCOD          = @BUYCOD,
           DESCR           = @DESCR,
           STDCOST         = @STDCOST,
           CURYID          = @CURYID,
           BUYDATE         = @BUYDATE,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcIN_VENDORINVT: Cannot update  in IN_VENDORINVT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcIN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcIN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcIN_VENDORINVT >>>'
go


/* 
 * PROCEDURE: SP_DelProcIN_VENDORINVT 
 */

CREATE PROCEDURE SP_DelProcIN_VENDORINVT
(
    @VENDIDSS       char(20),
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SITEID         UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_VENDORINVT
     WHERE VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcIN_VENDORINVT: Cannot delete because foreign keys still exist in IN_VENDORINVT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcIN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcIN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcIN_VENDORINVT >>>'
go


/* 
 * PROCEDURE: SP_SelProcIN_VENDORINVT 
 */

CREATE PROCEDURE SP_SelProcIN_VENDORINVT
(
    @VENDIDSS       char(20),
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SITEID         UD_IDSITE)
AS
BEGIN
    SELECT VENDIDSS,
           INVTIDSKU,
           SITEID,
           BUYCOD,
           DESCR,
           STDCOST,
           CURYID,
           BUYDATE,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_VENDORINVT
     WHERE VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcIN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcIN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcIN_VENDORINVT >>>'
go


/* 
 * PROCEDURE: SP_InsProcINTRAMOV 
 */

CREATE PROCEDURE SP_InsProcINTRAMOV
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @BATNBR            char(10),
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     UD_FECHA,
    @TYPEDOCID         UD_TYPEDOC,
    @CURYRATE          UD_CAMBIO,
    @CURYDATE          smalldatetime,
    @PERPOST           UD_PERPOST,
    @SWTINOUT          bit,
    @QTY               numeric(10, 3),
    @STDCOSTUSD        numeric(8, 4)               = NULL,
    @STDCOSTPEN        numeric(8, 4)               = NULL,
    @COMPONENT         bit,
    @SLSPRICE          numeric(8, 4),
    @REFNBR            char(15)                    = NULL,
    @DOCTYPEID         UD_DOCTYPEID                = NULL,
    @STKFISI           bit,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER3                    = NULL,
    @USER4             UD_USER5                    = NULL,
    @USER5             UD_USER5                    = NULL,
    @USER6             UD_USER6                    = NULL,
    @USER7             UD_USER7                    = NULL,
    @USER8             UD_USER8                    = NULL,
    @USER9             UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO INTRAMOV(SITEID,
                         INVTIDSKU,
                         BATNBR,
                         INTRDATE,
                         INTRDATEPROCE,
                         TYPEDOCID,
                         CURYRATE,
                         CURYDATE,
                         PERPOST,
                         SWTINOUT,
                         QTY,
                         STDCOSTUSD,
                         STDCOSTPEN,
                         COMPONENT,
                         SLSPRICE,
                         REFNBR,
                         DOCTYPEID,
                         STKFISI,
                         STKSEND,
                         STKRECEIVE,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@SITEID,
           @INVTIDSKU,
           @BATNBR,
           @INTRDATE,
           @INTRDATEPROCE,
           @TYPEDOCID,
           @CURYRATE,
           @CURYDATE,
           @PERPOST,
           @SWTINOUT,
           @QTY,
           @STDCOSTUSD,
           @STDCOSTPEN,
           @COMPONENT,
           @SLSPRICE,
           @REFNBR,
           @DOCTYPEID,
           @STKFISI,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcINTRAMOV: Cannot insert because primary key value not found in INTRAMOV '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcINTRAMOV') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcINTRAMOV >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcINTRAMOV >>>'
go


/* 
 * PROCEDURE: SP_UpdProcINTRAMOV 
 */

CREATE PROCEDURE SP_UpdProcINTRAMOV
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @BATNBR            char(10),
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     UD_FECHA,
    @TYPEDOCID         UD_TYPEDOC,
    @CURYRATE          UD_CAMBIO,
    @CURYDATE          smalldatetime,
    @PERPOST           UD_PERPOST,
    @SWTINOUT          bit,
    @QTY               numeric(10, 3),
    @STDCOSTUSD        numeric(8, 4)               = NULL,
    @STDCOSTPEN        numeric(8, 4)               = NULL,
    @COMPONENT         bit,
    @SLSPRICE          numeric(8, 4),
    @REFNBR            char(15)                    = NULL,
    @DOCTYPEID         UD_DOCTYPEID                = NULL,
    @STKFISI           bit,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER3                    = NULL,
    @USER4             UD_USER5                    = NULL,
    @USER5             UD_USER5                    = NULL,
    @USER6             UD_USER6                    = NULL,
    @USER7             UD_USER7                    = NULL,
    @USER8             UD_USER8                    = NULL,
    @USER9             UD_USER9                    = NULL,
    @TSTAMP            timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE INTRAMOV
       SET INTRDATE           = @INTRDATE,
           INTRDATEPROCE      = @INTRDATEPROCE,
           TYPEDOCID          = @TYPEDOCID,
           CURYRATE           = @CURYRATE,
           CURYDATE           = @CURYDATE,
           PERPOST            = @PERPOST,
           SWTINOUT           = @SWTINOUT,
           QTY                = @QTY,
           STDCOSTUSD         = @STDCOSTUSD,
           STDCOSTPEN         = @STDCOSTPEN,
           COMPONENT          = @COMPONENT,
           SLSPRICE           = @SLSPRICE,
           REFNBR             = @REFNBR,
           DOCTYPEID          = @DOCTYPEID,
           STKFISI            = @STKFISI,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4,
           USER5              = @USER5,
           USER6              = @USER6,
           USER7              = @USER7,
           USER8              = @USER8,
           USER9              = @USER9
     WHERE SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU
       AND BATNBR    = @BATNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcINTRAMOV: Cannot update  in INTRAMOV '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcINTRAMOV') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcINTRAMOV >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcINTRAMOV >>>'
go


/* 
 * PROCEDURE: SP_DelProcINTRAMOV 
 */

CREATE PROCEDURE SP_DelProcINTRAMOV
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @BATNBR            char(10))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM INTRAMOV
     WHERE SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU
       AND BATNBR    = @BATNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcINTRAMOV: Cannot delete because foreign keys still exist in INTRAMOV '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcINTRAMOV') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcINTRAMOV >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcINTRAMOV >>>'
go


/* 
 * PROCEDURE: SP_SelProcINTRAMOV 
 */

CREATE PROCEDURE SP_SelProcINTRAMOV
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @BATNBR            char(10))
AS
BEGIN
    SELECT SITEID,
           INVTIDSKU,
           BATNBR,
           INTRDATE,
           INTRDATEPROCE,
           TYPEDOCID,
           CURYRATE,
           CURYDATE,
           PERPOST,
           SWTINOUT,
           QTY,
           STDCOSTUSD,
           STDCOSTPEN,
           COMPONENT,
           SLSPRICE,
           REFNBR,
           DOCTYPEID,
           STKFISI,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM INTRAMOV
     WHERE SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU
       AND BATNBR    = @BATNBR

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcINTRAMOV') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcINTRAMOV >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcINTRAMOV >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_BONUS 
 */

CREATE PROCEDURE SP_InsProcOP_BONUS
(
    @DOCTYPEID       char(3),
    @NBRDOCUMENT     numeric(10, 0),
    @CUSTIDSS        char(20),
    @SITEID          char(5),
    @NBRBONUS        numeric(10, 0),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01          UD_USER1                  = NULL,
    @USER02          UD_USER2                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_BONUS(DOCTYPEID,
                         NBRDOCUMENT,
                         CUSTIDSS,
                         SITEID,
                         NBRBONUS,
                         STKNEW,
                         STKMODIFY,
                         STKDELETE,
                         STKSEND,
                         STKRECEIVE,
                         USER01,
                         USER02 
                         )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @CUSTIDSS,
           @SITEID,
           @NBRBONUS,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_BONUS: Cannot insert because primary key value not found in OP_BONUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_BONUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_BONUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_BONUS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_BONUS 
 */

CREATE PROCEDURE SP_UpdProcOP_BONUS
(
    @DOCTYPEID       char(3),
    @NBRDOCUMENT     numeric(10, 0),
    @CUSTIDSS        char(20),
    @SITEID          char(5),
    @NBRBONUS        numeric(10, 0),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01          UD_USER1                  = NULL,
    @USER02          UD_USER2                  = NULL,
    @TSTAMP          timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_BONUS
       SET STKNEW           = @STKNEW,
           STKMODIFY        = @STKMODIFY,
           STKDELETE        = @STKDELETE,
           STKSEND          = @STKSEND,
           STKRECEIVE       = @STKRECEIVE,
           USER01           = @USER01,
           USER02           = @USER02
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND CUSTIDSS    = @CUSTIDSS
       AND SITEID      = @SITEID
       AND NBRBONUS    = @NBRBONUS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_BONUS: Cannot update  in OP_BONUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_BONUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_BONUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_BONUS >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_BONUS 
 */

CREATE PROCEDURE SP_DelProcOP_BONUS
(
    @DOCTYPEID       char(3),
    @NBRDOCUMENT     numeric(10, 0),
    @CUSTIDSS        char(20),
    @SITEID          char(5),
    @NBRBONUS        numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_BONUS
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND CUSTIDSS    = @CUSTIDSS
       AND SITEID      = @SITEID
       AND NBRBONUS    = @NBRBONUS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_BONUS: Cannot delete because foreign keys still exist in OP_BONUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_BONUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_BONUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_BONUS >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_BONUS 
 */

CREATE PROCEDURE SP_SelProcOP_BONUS
(
    @DOCTYPEID       char(3),
    @NBRDOCUMENT     numeric(10, 0),
    @CUSTIDSS        char(20),
    @SITEID          char(5),
    @NBRBONUS        numeric(10, 0))
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           CUSTIDSS,
           SITEID,
           NBRBONUS,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           TSTAMP
      FROM OP_BONUS
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND CUSTIDSS    = @CUSTIDSS
       AND SITEID      = @SITEID
       AND NBRBONUS    = @NBRBONUS

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_BONUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_BONUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_BONUS >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_BUSINESSTYPE 
 */

CREATE PROCEDURE SP_InsProcOP_BUSINESSTYPE
(
    @BUSINESSTYPE      char(1),
    @DESCRBUSINESS     UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE        bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_BUSINESSTYPE(BUSINESSTYPE,
                                DESCRBUSINESS,
                                STKNEW,
                                STKMODIFY,
                                STKDELETE,
                                STKSEND,
                                STKRECEIVE)
    VALUES(@BUSINESSTYPE,
           @DESCRBUSINESS,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_BUSINESSTYPE: Cannot insert because primary key value not found in OP_BUSINESSTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_BUSINESSTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_BUSINESSTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_BUSINESSTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_BUSINESSTYPE 
 */

CREATE PROCEDURE SP_UpdProcOP_BUSINESSTYPE
(
    @BUSINESSTYPE      char(1),
    @DESCRBUSINESS     UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE        bit)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_BUSINESSTYPE
       SET DESCRBUSINESS      = @DESCRBUSINESS,
           STKNEW             = @STKNEW,
           STKMODIFY          = @STKMODIFY,
           STKDELETE          = @STKDELETE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE
     WHERE BUSINESSTYPE = @BUSINESSTYPE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_BUSINESSTYPE: Cannot update  in OP_BUSINESSTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_BUSINESSTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_BUSINESSTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_BUSINESSTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_BUSINESSTYPE 
 */

CREATE PROCEDURE SP_DelProcOP_BUSINESSTYPE
(
    @BUSINESSTYPE      char(1))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_BUSINESSTYPE
     WHERE BUSINESSTYPE = @BUSINESSTYPE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_BUSINESSTYPE: Cannot delete because foreign keys still exist in OP_BUSINESSTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_BUSINESSTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_BUSINESSTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_BUSINESSTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_BUSINESSTYPE 
 */

CREATE PROCEDURE SP_SelProcOP_BUSINESSTYPE
(
    @BUSINESSTYPE      char(1))
AS
BEGIN
    SELECT BUSINESSTYPE,
           DESCRBUSINESS,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE
      FROM OP_BUSINESSTYPE
     WHERE BUSINESSTYPE = @BUSINESSTYPE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_BUSINESSTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_BUSINESSTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_BUSINESSTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CARD 
 */

CREATE PROCEDURE SP_InsProcOP_CARD
(
    @CARDID          char(2),
    @ACCOUNT         char(12),
    @DESCR           UD_NOMBRE,
    @NUMBERORDER     tinyint,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01          UD_USER1             = NULL,
    @USER02          UD_USER2             = NULL,
    @USER03          UD_USER6             = NULL,
    @USER04          UD_USER7             = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CARD(CARDID,
                        ACCOUNT,
                        DESCR,
                        NUMBERORDER,
                        STKNEW,
                        STKMODIFY,
                        STKDELETE,
                        STKSEND,
                        STKRECEIVE,
                        USER01,
                        USER02,
                        USER03,
                        USER04 
                        )
    VALUES(@CARDID,
           @ACCOUNT,
           @DESCR,
           @NUMBERORDER,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CARD: Cannot insert because primary key value not found in OP_CARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CARD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CARD 
 */

CREATE PROCEDURE SP_UpdProcOP_CARD
(
    @CARDID          char(2),
    @ACCOUNT         char(12),
    @DESCR           UD_NOMBRE,
    @NUMBERORDER     tinyint,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01          UD_USER1             = NULL,
    @USER02          UD_USER2             = NULL,
    @USER03          UD_USER6             = NULL,
    @USER04          UD_USER7             = NULL,
    @TSTAMP          timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CARD
       SET ACCOUNT          = @ACCOUNT,
           DESCR            = @DESCR,
           NUMBERORDER      = @NUMBERORDER,
           STKNEW           = @STKNEW,
           STKMODIFY        = @STKMODIFY,
           STKDELETE        = @STKDELETE,
           STKSEND          = @STKSEND,
           STKRECEIVE       = @STKRECEIVE,
           USER01           = @USER01,
           USER02           = @USER02,
           USER03           = @USER03,
           USER04           = @USER04
     WHERE CARDID = @CARDID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CARD: Cannot update  in OP_CARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CARD >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CARD 
 */

CREATE PROCEDURE SP_DelProcOP_CARD
(
    @CARDID          char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CARD
     WHERE CARDID = @CARDID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CARD: Cannot delete because foreign keys still exist in OP_CARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CARD >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CARD 
 */

CREATE PROCEDURE SP_SelProcOP_CARD
(
    @CARDID          char(2))
AS
BEGIN
    SELECT CARDID,
           ACCOUNT,
           DESCR,
           NUMBERORDER,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_CARD
     WHERE CARDID = @CARDID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CARD >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CARD_MONT 
 */

CREATE PROCEDURE SP_InsProcOP_CARD_MONT
(
    @SITEID         UD_IDSITE,
    @CARDID         char(2),
    @DATEPOS        UD_FECHA,
    @DATEPROCE      UD_FECHA,
    @CURYID         UD_CURYID,
    @TOTALCARD      UD_MONTO,
    @STATUS         bit                  = NULL,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1             = NULL,
    @USER2          UD_USER2             = NULL,
    @USER3          UD_USER6             = NULL,
    @USER4          UD_USER7             = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CARD_MONT(SITEID,
                             CARDID,
                             DATEPOS,
                             DATEPROCE,
                             CURYID,
                             TOTALCARD,
                             STATUS,
                             USERID,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@SITEID,
           @CARDID,
           @DATEPOS,
           @DATEPROCE,
           @CURYID,
           @TOTALCARD,
           @STATUS,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CARD_MONT: Cannot insert because primary key value not found in OP_CARD_MONT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CARD_MONT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CARD_MONT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CARD_MONT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CARD_MONT 
 */

CREATE PROCEDURE SP_UpdProcOP_CARD_MONT
(
    @SITEID         UD_IDSITE,
    @CARDID         char(2),
    @DATEPOS        UD_FECHA,
    @DATEPROCE      UD_FECHA,
    @CURYID         UD_CURYID,
    @TOTALCARD      UD_MONTO,
    @STATUS         bit                  = NULL,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1             = NULL,
    @USER2          UD_USER2             = NULL,
    @USER3          UD_USER6             = NULL,
    @USER4          UD_USER7             = NULL,
    @TSTAMP         timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CARD_MONT
       SET CURYID          = @CURYID,
           TOTALCARD       = @TOTALCARD,
           STATUS          = @STATUS,
           USERID          = @USERID,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SITEID    = @SITEID
       AND CARDID    = @CARDID
       AND DATEPOS   = @DATEPOS
       AND DATEPROCE = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CARD_MONT: Cannot update  in OP_CARD_MONT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CARD_MONT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CARD_MONT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CARD_MONT >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CARD_MONT 
 */

CREATE PROCEDURE SP_DelProcOP_CARD_MONT
(
    @SITEID         UD_IDSITE,
    @CARDID         char(2),
    @DATEPOS        UD_FECHA,
    @DATEPROCE      UD_FECHA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CARD_MONT
     WHERE SITEID    = @SITEID
       AND CARDID    = @CARDID
       AND DATEPOS   = @DATEPOS
       AND DATEPROCE = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CARD_MONT: Cannot delete because foreign keys still exist in OP_CARD_MONT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CARD_MONT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CARD_MONT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CARD_MONT >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CARD_MONT 
 */

CREATE PROCEDURE SP_SelProcOP_CARD_MONT
(
    @SITEID         UD_IDSITE,
    @CARDID         char(2),
    @DATEPOS        UD_FECHA,
    @DATEPROCE      UD_FECHA)
AS
BEGIN
    SELECT SITEID,
           CARDID,
           DATEPOS,
           DATEPROCE,
           CURYID,
           TOTALCARD,
           STATUS,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CARD_MONT
     WHERE SITEID    = @SITEID
       AND CARDID    = @CARDID
       AND DATEPOS   = @DATEPOS
       AND DATEPROCE = @DATEPROCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CARD_MONT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CARD_MONT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CARD_MONT >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CCDOCDETTEMP 
 */

CREATE PROCEDURE SP_InsProcOP_CCDOCDETTEMP
(
    @DOCSUMMARYTTYPEID     char(2),
    @REFERENCE             numeric(10, 0),
    @SEQUENCE              smallint,
    @TAXREGNBR             UD_CODCLIENT              = NULL,
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @CURYRATE              UD_CAMBIO,
    @PAIDMENTPEN           numeric(11, 4),
    @PAIDMENTUSD           numeric(11, 4),
    @DATEPROCEPAIDMENT     smalldatetime,
    @BANKID                char(2),
    @CHEQNUMBER            char(15),
    @NBRBANKACCOUNT        char(15),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CCDOCDETTEMP(DOCSUMMARYTTYPEID,
                                REFERENCE,
                                SEQUENCE,
                                TAXREGNBR,
                                DOCTYPEID,
                                NBRDOCUMENT,
                                CURYRATE,
                                PAIDMENTPEN,
                                PAIDMENTUSD,
                                DATEPROCEPAIDMENT,
                                BANKID,
                                CHEQNUMBER,
                                NBRBANKACCOUNT,
                                STKSEND,
                                STKRECEIVE,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@DOCSUMMARYTTYPEID,
           @REFERENCE,
           @SEQUENCE,
           @TAXREGNBR,
           @DOCTYPEID,
           @NBRDOCUMENT,
           @CURYRATE,
           @PAIDMENTPEN,
           @PAIDMENTUSD,
           @DATEPROCEPAIDMENT,
           @BANKID,
           @CHEQNUMBER,
           @NBRBANKACCOUNT,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CCDOCDETTEMP: Cannot insert because primary key value not found in OP_CCDOCDETTEMP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CCDOCDETTEMP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CCDOCDETTEMP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CCDOCDETTEMP >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CCDOCDETTEMP 
 */

CREATE PROCEDURE SP_UpdProcOP_CCDOCDETTEMP
(
    @DOCSUMMARYTTYPEID     char(2),
    @REFERENCE             numeric(10, 0),
    @SEQUENCE              smallint,
    @TAXREGNBR             UD_CODCLIENT              = NULL,
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @CURYRATE              UD_CAMBIO,
    @PAIDMENTPEN           numeric(11, 4),
    @PAIDMENTUSD           numeric(11, 4),
    @DATEPROCEPAIDMENT     smalldatetime,
    @BANKID                char(2),
    @CHEQNUMBER            char(15),
    @NBRBANKACCOUNT        char(15),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CCDOCDETTEMP
       SET TAXREGNBR              = @TAXREGNBR,
           DOCTYPEID              = @DOCTYPEID,
           NBRDOCUMENT            = @NBRDOCUMENT,
           CURYRATE               = @CURYRATE,
           PAIDMENTPEN            = @PAIDMENTPEN,
           PAIDMENTUSD            = @PAIDMENTUSD,
           DATEPROCEPAIDMENT      = @DATEPROCEPAIDMENT,
           BANKID                 = @BANKID,
           CHEQNUMBER             = @CHEQNUMBER,
           NBRBANKACCOUNT         = @NBRBANKACCOUNT,
           STKSEND                = @STKSEND,
           STKRECEIVE             = @STKRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE DOCSUMMARYTTYPEID = @DOCSUMMARYTTYPEID
       AND REFERENCE         = @REFERENCE
       AND SEQUENCE          = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CCDOCDETTEMP: Cannot update  in OP_CCDOCDETTEMP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CCDOCDETTEMP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CCDOCDETTEMP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CCDOCDETTEMP >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CCDOCDETTEMP 
 */

CREATE PROCEDURE SP_DelProcOP_CCDOCDETTEMP
(
    @DOCSUMMARYTTYPEID     char(2),
    @REFERENCE             numeric(10, 0),
    @SEQUENCE              smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CCDOCDETTEMP
     WHERE DOCSUMMARYTTYPEID = @DOCSUMMARYTTYPEID
       AND REFERENCE         = @REFERENCE
       AND SEQUENCE          = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CCDOCDETTEMP: Cannot delete because foreign keys still exist in OP_CCDOCDETTEMP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CCDOCDETTEMP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CCDOCDETTEMP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CCDOCDETTEMP >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CCDOCDETTEMP 
 */

CREATE PROCEDURE SP_SelProcOP_CCDOCDETTEMP
(
    @DOCSUMMARYTTYPEID     char(2),
    @REFERENCE             numeric(10, 0),
    @SEQUENCE              smallint)
AS
BEGIN
    SELECT DOCSUMMARYTTYPEID,
           REFERENCE,
           SEQUENCE,
           TAXREGNBR,
           DOCTYPEID,
           NBRDOCUMENT,
           CURYRATE,
           PAIDMENTPEN,
           PAIDMENTUSD,
           DATEPROCEPAIDMENT,
           BANKID,
           CHEQNUMBER,
           NBRBANKACCOUNT,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCDOCDETTEMP
     WHERE DOCSUMMARYTTYPEID = @DOCSUMMARYTTYPEID
       AND REFERENCE         = @REFERENCE
       AND SEQUENCE          = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CCDOCDETTEMP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CCDOCDETTEMP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CCDOCDETTEMP >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CCSALESPERSON 
 */

CREATE PROCEDURE SP_InsProcOP_CCSALESPERSON
(
    @SITEID         UD_IDSITE,
    @DATEPROCE      smalldatetime,
    @USERID         UD_USERID,
    @CURYID         UD_CURYID,
    @STATUS         bit,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @USER4          UD_USER7                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CCSALESPERSON(SITEID,
                                 DATEPROCE,
                                 USERID,
                                 CURYID,
                                 STATUS,
                                 STKSEND,
                                 STKRECEIVE,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @DATEPROCE,
           @USERID,
           @CURYID,
           @STATUS,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CCSALESPERSON: Cannot insert because primary key value not found in OP_CCSALESPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CCSALESPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CCSALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CCSALESPERSON >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CCSALESPERSON 
 */

CREATE PROCEDURE SP_UpdProcOP_CCSALESPERSON
(
    @SITEID         UD_IDSITE,
    @DATEPROCE      smalldatetime,
    @USERID         UD_USERID,
    @CURYID         UD_CURYID,
    @STATUS         bit,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @USER4          UD_USER7                 = NULL,
    @TSTAMP         timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CCSALESPERSON
       SET USERID          = @USERID,
           CURYID          = @CURYID,
           STATUS          = @STATUS,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CCSALESPERSON: Cannot update  in OP_CCSALESPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CCSALESPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CCSALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CCSALESPERSON >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CCSALESPERSON 
 */

CREATE PROCEDURE SP_DelProcOP_CCSALESPERSON
(
    @SITEID         UD_IDSITE,
    @DATEPROCE      smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CCSALESPERSON
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CCSALESPERSON: Cannot delete because foreign keys still exist in OP_CCSALESPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CCSALESPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CCSALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CCSALESPERSON >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CCSALESPERSON 
 */

CREATE PROCEDURE SP_SelProcOP_CCSALESPERSON
(
    @SITEID         UD_IDSITE,
    @DATEPROCE      smalldatetime)
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           USERID,
           CURYID,
           STATUS,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCSALESPERSON
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CCSALESPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CCSALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CCSALESPERSON >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CCSALESPERSONDET 
 */

CREATE PROCEDURE SP_InsProcOP_CCSALESPERSONDET
(
    @SITEID                  char(5),
    @DATEPROCE               smalldatetime,
    @SALESPERID              char(20),
    @CCSALESPERSONTYPEID     char(2),
    @TOTALPEN                numeric(11, 4),
    @TOTALUSD                numeric(11, 4),
    @OBS                     varchar(100)              = NULL,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                  = NULL,
    @USER2                   UD_USER2                  = NULL,
    @USER3                   UD_USER6                  = NULL,
    @USER4                   UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CCSALESPERSONDET(SITEID,
                                    DATEPROCE,
                                    SALESPERID,
                                    CCSALESPERSONTYPEID,
                                    TOTALPEN,
                                    TOTALUSD,
                                    OBS,
                                    STKSEND,
                                    STKRECEIVE,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@SITEID,
           @DATEPROCE,
           @SALESPERID,
           @CCSALESPERSONTYPEID,
           @TOTALPEN,
           @TOTALUSD,
           @OBS,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CCSALESPERSONDET: Cannot insert because primary key value not found in OP_CCSALESPERSONDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CCSALESPERSONDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CCSALESPERSONDET 
 */

CREATE PROCEDURE SP_UpdProcOP_CCSALESPERSONDET
(
    @SITEID                  char(5),
    @DATEPROCE               smalldatetime,
    @SALESPERID              char(20),
    @CCSALESPERSONTYPEID     char(2),
    @TOTALPEN                numeric(11, 4),
    @TOTALUSD                numeric(11, 4),
    @OBS                     varchar(100)              = NULL,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                  = NULL,
    @USER2                   UD_USER2                  = NULL,
    @USER3                   UD_USER6                  = NULL,
    @USER4                   UD_USER7                  = NULL,
    @TSTAMP                  timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CCSALESPERSONDET
       SET TOTALPEN                 = @TOTALPEN,
           TOTALUSD                 = @TOTALUSD,
           OBS                      = @OBS,
           STKSEND                  = @STKSEND,
           STKRECEIVE               = @STKRECEIVE,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE SITEID              = @SITEID
       AND DATEPROCE           = @DATEPROCE
       AND SALESPERID          = @SALESPERID
       AND CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CCSALESPERSONDET: Cannot update  in OP_CCSALESPERSONDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CCSALESPERSONDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CCSALESPERSONDET 
 */

CREATE PROCEDURE SP_DelProcOP_CCSALESPERSONDET
(
    @SITEID                  char(5),
    @DATEPROCE               smalldatetime,
    @SALESPERID              char(20),
    @CCSALESPERSONTYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CCSALESPERSONDET
     WHERE SITEID              = @SITEID
       AND DATEPROCE           = @DATEPROCE
       AND SALESPERID          = @SALESPERID
       AND CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CCSALESPERSONDET: Cannot delete because foreign keys still exist in OP_CCSALESPERSONDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CCSALESPERSONDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CCSALESPERSONDET 
 */

CREATE PROCEDURE SP_SelProcOP_CCSALESPERSONDET
(
    @SITEID                  char(5),
    @DATEPROCE               smalldatetime,
    @SALESPERID              char(20),
    @CCSALESPERSONTYPEID     char(2))
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           SALESPERID,
           CCSALESPERSONTYPEID,
           TOTALPEN,
           TOTALUSD,
           OBS,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCSALESPERSONDET
     WHERE SITEID              = @SITEID
       AND DATEPROCE           = @DATEPROCE
       AND SALESPERID          = @SALESPERID
       AND CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CCSALESPERSONDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CCSALESPERSONTYPE 
 */

CREATE PROCEDURE SP_InsProcOP_CCSALESPERSONTYPE
(
    @CCSALESPERSONTYPEID        char(2),
    @DESCRCCSALESPERSONTYPE     UD_DESCRIPCION,
    @DEBEHABER                  UD_DEBEHABER,
    @USERID                     UD_USERID,
    @STKNEW                     bit,
    @STKMODIFY                  bit,
    @STKDELETE                  bit,
    @STKSEND                    bit,
    @STKRECEIVE                 bit,
    @USER1                      UD_USER1                  = NULL,
    @USER2                      UD_USER2                  = NULL,
    @USER3                      UD_USER6                  = NULL,
    @USER4                      UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CCSALESPERSONTYPE(CCSALESPERSONTYPEID,
                                     DESCRCCSALESPERSONTYPE,
                                     DEBEHABER,
                                     USERID,
                                     STKNEW,
                                     STKMODIFY,
                                     STKDELETE,
                                     STKSEND,
                                     STKRECEIVE,
                                     USER1,
                                     USER2,
                                     USER3,
                                     USER4 
                                     )
    VALUES(@CCSALESPERSONTYPEID,
           @DESCRCCSALESPERSONTYPE,
           @DEBEHABER,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CCSALESPERSONTYPE: Cannot insert because primary key value not found in OP_CCSALESPERSONTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CCSALESPERSONTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CCSALESPERSONTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CCSALESPERSONTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CCSALESPERSONTYPE 
 */

CREATE PROCEDURE SP_UpdProcOP_CCSALESPERSONTYPE
(
    @CCSALESPERSONTYPEID        char(2),
    @DESCRCCSALESPERSONTYPE     UD_DESCRIPCION,
    @DEBEHABER                  UD_DEBEHABER,
    @USERID                     UD_USERID,
    @STKNEW                     bit,
    @STKMODIFY                  bit,
    @STKDELETE                  bit,
    @STKSEND                    bit,
    @STKRECEIVE                 bit,
    @USER1                      UD_USER1                  = NULL,
    @USER2                      UD_USER2                  = NULL,
    @USER3                      UD_USER6                  = NULL,
    @USER4                      UD_USER7                  = NULL,
    @TSTAMP                     timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CCSALESPERSONTYPE
       SET DESCRCCSALESPERSONTYPE      = @DESCRCCSALESPERSONTYPE,
           DEBEHABER                   = @DEBEHABER,
           USERID                      = @USERID,
           STKNEW                      = @STKNEW,
           STKMODIFY                   = @STKMODIFY,
           STKDELETE                   = @STKDELETE,
           STKSEND                     = @STKSEND,
           STKRECEIVE                  = @STKRECEIVE,
           USER1                       = @USER1,
           USER2                       = @USER2,
           USER3                       = @USER3,
           USER4                       = @USER4
     WHERE CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CCSALESPERSONTYPE: Cannot update  in OP_CCSALESPERSONTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CCSALESPERSONTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CCSALESPERSONTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CCSALESPERSONTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CCSALESPERSONTYPE 
 */

CREATE PROCEDURE SP_DelProcOP_CCSALESPERSONTYPE
(
    @CCSALESPERSONTYPEID        char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CCSALESPERSONTYPE
     WHERE CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CCSALESPERSONTYPE: Cannot delete because foreign keys still exist in OP_CCSALESPERSONTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CCSALESPERSONTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CCSALESPERSONTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CCSALESPERSONTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CCSALESPERSONTYPE 
 */

CREATE PROCEDURE SP_SelProcOP_CCSALESPERSONTYPE
(
    @CCSALESPERSONTYPEID        char(2))
AS
BEGIN
    SELECT CCSALESPERSONTYPEID,
           DESCRCCSALESPERSONTYPE,
           DEBEHABER,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCSALESPERSONTYPE
     WHERE CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CCSALESPERSONTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CCSALESPERSONTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CCSALESPERSONTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CORELATIVE 
 */

CREATE PROCEDURE SP_InsProcOP_CORELATIVE
(
    @TYPECORELATIVE     char(20),
    @NBRCORELATIVE      numeric(10, 0),
    @USERID             UD_USERID,
    @USER01             UD_USER1                  = NULL,
    @USER02             UD_USER2                  = NULL,
    @USER03             UD_USER6                  = NULL,
    @USER04             UD_USER7                  = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE         bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CORELATIVE(TYPECORELATIVE,
                              NBRCORELATIVE,
                              USERID,
                              USER01,
                              USER02,
                              USER03,
                              USER04,
                              STKNEW,
                              STKMODIFY,
                              STKDELETE,
                              STKSEND,
                              STKRECEIVE)
    VALUES(@TYPECORELATIVE,
           @NBRCORELATIVE,
           @USERID,
           @USER01,
           @USER02,
           @USER03,
           @USER04,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CORELATIVE: Cannot insert because primary key value not found in OP_CORELATIVE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CORELATIVE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CORELATIVE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CORELATIVE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CORELATIVE 
 */

CREATE PROCEDURE SP_UpdProcOP_CORELATIVE
(
    @TYPECORELATIVE     char(20),
    @NBRCORELATIVE      numeric(10, 0),
    @USERID             UD_USERID,
    @USER01             UD_USER1                  = NULL,
    @USER02             UD_USER2                  = NULL,
    @USER03             UD_USER6                  = NULL,
    @USER04             UD_USER7                  = NULL,
    @TSTAMP             timestamp                 = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE         bit)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CORELATIVE
       SET NBRCORELATIVE       = @NBRCORELATIVE,
           USERID              = @USERID,
           USER01              = @USER01,
           USER02              = @USER02,
           USER03              = @USER03,
           USER04              = @USER04,
           STKNEW              = @STKNEW,
           STKMODIFY           = @STKMODIFY,
           STKDELETE           = @STKDELETE,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE
     WHERE TYPECORELATIVE = @TYPECORELATIVE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CORELATIVE: Cannot update  in OP_CORELATIVE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CORELATIVE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CORELATIVE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CORELATIVE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CORELATIVE 
 */

CREATE PROCEDURE SP_DelProcOP_CORELATIVE
(
    @TYPECORELATIVE     char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CORELATIVE
     WHERE TYPECORELATIVE = @TYPECORELATIVE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CORELATIVE: Cannot delete because foreign keys still exist in OP_CORELATIVE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CORELATIVE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CORELATIVE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CORELATIVE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CORELATIVE 
 */

CREATE PROCEDURE SP_SelProcOP_CORELATIVE
(
    @TYPECORELATIVE     char(20))
AS
BEGIN
    SELECT TYPECORELATIVE,
           NBRCORELATIVE,
           USERID,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE
      FROM OP_CORELATIVE
     WHERE TYPECORELATIVE = @TYPECORELATIVE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CORELATIVE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CORELATIVE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CORELATIVE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CTRLFUEL 
 */

CREATE PROCEDURE SP_InsProcOP_CTRLFUEL
(
    @SITEID         UD_IDSITE,
    @DATEPROCE      smalldatetime,
    @STATUS         bit,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @USER4          UD_USER7                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLFUEL(SITEID,
                            DATEPROCE,
                            STATUS,
                            USERID,
                            STKSEND,
                            STKRECEIVE,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@SITEID,
           @DATEPROCE,
           @STATUS,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CTRLFUEL: Cannot insert because primary key value not found in OP_CTRLFUEL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CTRLFUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CTRLFUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CTRLFUEL >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CTRLFUEL 
 */

CREATE PROCEDURE SP_UpdProcOP_CTRLFUEL
(
    @SITEID         UD_IDSITE,
    @DATEPROCE      smalldatetime,
    @STATUS         bit,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @USER4          UD_USER7                 = NULL,
    @TSTAMP         timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLFUEL
       SET STATUS          = @STATUS,
           USERID          = @USERID,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CTRLFUEL: Cannot update  in OP_CTRLFUEL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CTRLFUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CTRLFUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CTRLFUEL >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CTRLFUEL 
 */

CREATE PROCEDURE SP_DelProcOP_CTRLFUEL
(
    @SITEID         UD_IDSITE,
    @DATEPROCE      smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLFUEL
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CTRLFUEL: Cannot delete because foreign keys still exist in OP_CTRLFUEL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CTRLFUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CTRLFUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CTRLFUEL >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CTRLFUEL 
 */

CREATE PROCEDURE SP_SelProcOP_CTRLFUEL
(
    @SITEID         UD_IDSITE,
    @DATEPROCE      smalldatetime)
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           STATUS,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLFUEL
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CTRLFUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CTRLFUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CTRLFUEL >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CTRLFUELDET 
 */

CREATE PROCEDURE SP_InsProcOP_CTRLFUELDET
(
    @SITEID               char(5),
    @DATEPROCE            smalldatetime,
    @INVTIDSKU            char(20),
    @STOCKINITIAL         numeric(10, 3),
    @QTYIMPUT             numeric(10, 3),
    @QTYOUTPUT            numeric(10, 3),
    @QTYSALES             numeric(10, 3),
    @QTYAVAIL             numeric(10, 3),
    @STKUINITID           char(5),
    @VALUEUNIMED          numeric(4, 0),
    @QTYPHISICAL          numeric(10, 3),
    @QTYDIFFERENCE        numeric(10, 3),
    @PORCENTDIFERENCE     numeric(5, 2),
    @REMARK               UD_DESCRIPCION            = NULL,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLFUELDET(SITEID,
                               DATEPROCE,
                               INVTIDSKU,
                               STOCKINITIAL,
                               QTYIMPUT,
                               QTYOUTPUT,
                               QTYSALES,
                               QTYAVAIL,
                               STKUINITID,
                               VALUEUNIMED,
                               QTYPHISICAL,
                               QTYDIFFERENCE,
                               PORCENTDIFERENCE,
                               REMARK,
                               STKSEND,
                               STKRECEIVE,
                               USER1,
                               USER2 
                               )
    VALUES(@SITEID,
           @DATEPROCE,
           @INVTIDSKU,
           @STOCKINITIAL,
           @QTYIMPUT,
           @QTYOUTPUT,
           @QTYSALES,
           @QTYAVAIL,
           @STKUINITID,
           @VALUEUNIMED,
           @QTYPHISICAL,
           @QTYDIFFERENCE,
           @PORCENTDIFERENCE,
           @REMARK,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CTRLFUELDET: Cannot insert because primary key value not found in OP_CTRLFUELDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CTRLFUELDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CTRLFUELDET 
 */

CREATE PROCEDURE SP_UpdProcOP_CTRLFUELDET
(
    @SITEID               char(5),
    @DATEPROCE            smalldatetime,
    @INVTIDSKU            char(20),
    @STOCKINITIAL         numeric(10, 3),
    @QTYIMPUT             numeric(10, 3),
    @QTYOUTPUT            numeric(10, 3),
    @QTYSALES             numeric(10, 3),
    @QTYAVAIL             numeric(10, 3),
    @STKUINITID           char(5),
    @VALUEUNIMED          numeric(4, 0),
    @QTYPHISICAL          numeric(10, 3),
    @QTYDIFFERENCE        numeric(10, 3),
    @PORCENTDIFERENCE     numeric(5, 2),
    @REMARK               UD_DESCRIPCION            = NULL,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL,
    @TSTAMP               timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLFUELDET
       SET STOCKINITIAL          = @STOCKINITIAL,
           QTYIMPUT              = @QTYIMPUT,
           QTYOUTPUT             = @QTYOUTPUT,
           QTYSALES              = @QTYSALES,
           QTYAVAIL              = @QTYAVAIL,
           STKUINITID            = @STKUINITID,
           VALUEUNIMED           = @VALUEUNIMED,
           QTYPHISICAL           = @QTYPHISICAL,
           QTYDIFFERENCE         = @QTYDIFFERENCE,
           PORCENTDIFERENCE      = @PORCENTDIFERENCE,
           REMARK                = @REMARK,
           STKSEND               = @STKSEND,
           STKRECEIVE            = @STKRECEIVE,
           USER1                 = @USER1,
           USER2                 = @USER2
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CTRLFUELDET: Cannot update  in OP_CTRLFUELDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CTRLFUELDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CTRLFUELDET 
 */

CREATE PROCEDURE SP_DelProcOP_CTRLFUELDET
(
    @SITEID               char(5),
    @DATEPROCE            smalldatetime,
    @INVTIDSKU            char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLFUELDET
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CTRLFUELDET: Cannot delete because foreign keys still exist in OP_CTRLFUELDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CTRLFUELDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CTRLFUELDET 
 */

CREATE PROCEDURE SP_SelProcOP_CTRLFUELDET
(
    @SITEID               char(5),
    @DATEPROCE            smalldatetime,
    @INVTIDSKU            char(20))
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           INVTIDSKU,
           STOCKINITIAL,
           QTYIMPUT,
           QTYOUTPUT,
           QTYSALES,
           QTYAVAIL,
           STKUINITID,
           VALUEUNIMED,
           QTYPHISICAL,
           QTYDIFFERENCE,
           PORCENTDIFERENCE,
           REMARK,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           TSTAMP
      FROM OP_CTRLFUELDET
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CTRLFUELDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CTRLFUELPRICE 
 */

CREATE PROCEDURE SP_InsProcOP_CTRLFUELPRICE
(
    @SITEID         char(5),
    @DATEPROCE      smalldatetime,
    @INVTIDSKU      char(20),
    @SLS_PRICE      numeric(8, 4),
    @CURYID         UD_CURYID,
    @QTYSALES       numeric(10, 3),
    @STATUS         bit,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLFUELPRICE(SITEID,
                                 DATEPROCE,
                                 INVTIDSKU,
                                 SLS_PRICE,
                                 CURYID,
                                 QTYSALES,
                                 STATUS,
                                 USERID,
                                 STKSEND,
                                 STKRECEIVE,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @DATEPROCE,
           @INVTIDSKU,
           @SLS_PRICE,
           @CURYID,
           @QTYSALES,
           @STATUS,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CTRLFUELPRICE: Cannot insert because primary key value not found in OP_CTRLFUELPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CTRLFUELPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CTRLFUELPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CTRLFUELPRICE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CTRLFUELPRICE 
 */

CREATE PROCEDURE SP_UpdProcOP_CTRLFUELPRICE
(
    @SITEID         char(5),
    @DATEPROCE      smalldatetime,
    @INVTIDSKU      char(20),
    @SLS_PRICE      numeric(8, 4),
    @CURYID         UD_CURYID,
    @QTYSALES       numeric(10, 3),
    @STATUS         bit,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLFUELPRICE
       SET CURYID          = @CURYID,
           QTYSALES        = @QTYSALES,
           STATUS          = @STATUS,
           USERID          = @USERID,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU
       AND SLS_PRICE = @SLS_PRICE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CTRLFUELPRICE: Cannot update  in OP_CTRLFUELPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CTRLFUELPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CTRLFUELPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CTRLFUELPRICE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CTRLFUELPRICE 
 */

CREATE PROCEDURE SP_DelProcOP_CTRLFUELPRICE
(
    @SITEID         char(5),
    @DATEPROCE      smalldatetime,
    @INVTIDSKU      char(20),
    @SLS_PRICE      numeric(8, 4))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLFUELPRICE
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU
       AND SLS_PRICE = @SLS_PRICE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CTRLFUELPRICE: Cannot delete because foreign keys still exist in OP_CTRLFUELPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CTRLFUELPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CTRLFUELPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CTRLFUELPRICE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CTRLFUELPRICE 
 */

CREATE PROCEDURE SP_SelProcOP_CTRLFUELPRICE
(
    @SITEID         char(5),
    @DATEPROCE      smalldatetime,
    @INVTIDSKU      char(20),
    @SLS_PRICE      numeric(8, 4))
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           INVTIDSKU,
           SLS_PRICE,
           CURYID,
           QTYSALES,
           STATUS,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLFUELPRICE
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU
       AND SLS_PRICE = @SLS_PRICE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CTRLFUELPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CTRLFUELPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CTRLFUELPRICE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CTRLTURN 
 */

CREATE PROCEDURE SP_InsProcOP_CTRLTURN
(
    @BATNBRTURN     UD_CORR_CONTOMET,
    @SITEID         UD_IDSITE,
    @DATEPROCES     smalldatetime,
    @NBRTUR         char(1),
    @SALESPERID     UD_SALESPERSONID,
    @USERID         UD_USERID,
    @STATUS         bit,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER01         UD_USER1                    = NULL,
    @USER02         UD_USER2                    = NULL,
    @USER03         UD_USER6                    = NULL,
    @USER04         UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLTURN(BATNBRTURN,
                            SITEID,
                            DATEPROCES,
                            NBRTUR,
                            SALESPERID,
                            USERID,
                            STATUS,
                            STKSEND,
                            STKRECEIVE,
                            USER01,
                            USER02,
                            USER03,
                            USER04 
                            )
    VALUES(@BATNBRTURN,
           @SITEID,
           @DATEPROCES,
           @NBRTUR,
           @SALESPERID,
           @USERID,
           @STATUS,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CTRLTURN: Cannot insert because primary key value not found in OP_CTRLTURN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CTRLTURN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CTRLTURN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CTRLTURN >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CTRLTURN 
 */

CREATE PROCEDURE SP_UpdProcOP_CTRLTURN
(
    @BATNBRTURN     UD_CORR_CONTOMET,
    @SITEID         UD_IDSITE,
    @DATEPROCES     smalldatetime,
    @NBRTUR         char(1),
    @SALESPERID     UD_SALESPERSONID,
    @USERID         UD_USERID,
    @STATUS         bit,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER01         UD_USER1                    = NULL,
    @USER02         UD_USER2                    = NULL,
    @USER03         UD_USER6                    = NULL,
    @USER04         UD_USER7                    = NULL,
    @TSTAMP         timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLTURN
       SET SALESPERID      = @SALESPERID,
           USERID          = @USERID,
           STATUS          = @STATUS,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER01          = @USER01,
           USER02          = @USER02,
           USER03          = @USER03,
           USER04          = @USER04
     WHERE BATNBRTURN = @BATNBRTURN
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CTRLTURN: Cannot update  in OP_CTRLTURN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CTRLTURN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CTRLTURN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CTRLTURN >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CTRLTURN 
 */

CREATE PROCEDURE SP_DelProcOP_CTRLTURN
(
    @BATNBRTURN     UD_CORR_CONTOMET,
    @SITEID         UD_IDSITE,
    @DATEPROCES     smalldatetime,
    @NBRTUR         char(1))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLTURN
     WHERE BATNBRTURN = @BATNBRTURN
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CTRLTURN: Cannot delete because foreign keys still exist in OP_CTRLTURN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CTRLTURN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CTRLTURN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CTRLTURN >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CTRLTURN 
 */

CREATE PROCEDURE SP_SelProcOP_CTRLTURN
(
    @BATNBRTURN     UD_CORR_CONTOMET,
    @SITEID         UD_IDSITE,
    @DATEPROCES     smalldatetime,
    @NBRTUR         char(1))
AS
BEGIN
    SELECT BATNBRTURN,
           SITEID,
           DATEPROCES,
           NBRTUR,
           SALESPERID,
           USERID,
           STATUS,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_CTRLTURN
     WHERE BATNBRTURN = @BATNBRTURN
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CTRLTURN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CTRLTURN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CTRLTURN >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CTRLTURNDET 
 */

CREATE PROCEDURE SP_InsProcOP_CTRLTURNDET
(
    @BATNBRTURN       UD_CORR_CONTOMET,
    @SITEID           UD_IDSITE,
    @DATEPROCES       smalldatetime,
    @NBRTUR           char(1),
    @SIDE             UD_CARA,
    @HOSENUMBER       tinyint,
    @INVTIDBARR       UD_CODIGOBARRA,
    @STARTREADING     numeric(13, 3),
    @ENDREADING       numeric(13, 3),
    @QTYSALES         numeric(10, 3),
    @QTYSERAF         numeric(10, 3),
    @QTYOTHER         numeric(10, 3),
    @TOTQTY           numeric(10, 3),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                    = NULL,
    @USER2            UD_USER2                    = NULL,
    @USER3            UD_USER6                    = NULL,
    @USER4            UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLTURNDET(BATNBRTURN,
                               SITEID,
                               DATEPROCES,
                               NBRTUR,
                               SIDE,
                               HOSENUMBER,
                               INVTIDBARR,
                               STARTREADING,
                               ENDREADING,
                               QTYSALES,
                               QTYSERAF,
                               QTYOTHER,
                               TOTQTY,
                               STKSEND,
                               STKRECEIVE,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@BATNBRTURN,
           @SITEID,
           @DATEPROCES,
           @NBRTUR,
           @SIDE,
           @HOSENUMBER,
           @INVTIDBARR,
           @STARTREADING,
           @ENDREADING,
           @QTYSALES,
           @QTYSERAF,
           @QTYOTHER,
           @TOTQTY,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CTRLTURNDET: Cannot insert because primary key value not found in OP_CTRLTURNDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CTRLTURNDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CTRLTURNDET 
 */

CREATE PROCEDURE SP_UpdProcOP_CTRLTURNDET
(
    @BATNBRTURN       UD_CORR_CONTOMET,
    @SITEID           UD_IDSITE,
    @DATEPROCES       smalldatetime,
    @NBRTUR           char(1),
    @SIDE             UD_CARA,
    @HOSENUMBER       tinyint,
    @INVTIDBARR       UD_CODIGOBARRA,
    @STARTREADING     numeric(13, 3),
    @ENDREADING       numeric(13, 3),
    @QTYSALES         numeric(10, 3),
    @QTYSERAF         numeric(10, 3),
    @QTYOTHER         numeric(10, 3),
    @TOTQTY           numeric(10, 3),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                    = NULL,
    @USER2            UD_USER2                    = NULL,
    @USER3            UD_USER6                    = NULL,
    @USER4            UD_USER7                    = NULL,
    @TSTAMP           timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLTURNDET
       SET INVTIDBARR        = @INVTIDBARR,
           STARTREADING      = @STARTREADING,
           ENDREADING        = @ENDREADING,
           QTYSALES          = @QTYSALES,
           QTYSERAF          = @QTYSERAF,
           QTYOTHER          = @QTYOTHER,
           TOTQTY            = @TOTQTY,
           STKSEND           = @STKSEND,
           STKRECEIVE        = @STKRECEIVE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE BATNBRTURN = @BATNBRTURN
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR
       AND SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CTRLTURNDET: Cannot update  in OP_CTRLTURNDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CTRLTURNDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CTRLTURNDET 
 */

CREATE PROCEDURE SP_DelProcOP_CTRLTURNDET
(
    @BATNBRTURN       UD_CORR_CONTOMET,
    @SITEID           UD_IDSITE,
    @DATEPROCES       smalldatetime,
    @NBRTUR           char(1),
    @SIDE             UD_CARA,
    @HOSENUMBER       tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLTURNDET
     WHERE BATNBRTURN = @BATNBRTURN
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR
       AND SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CTRLTURNDET: Cannot delete because foreign keys still exist in OP_CTRLTURNDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CTRLTURNDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CTRLTURNDET 
 */

CREATE PROCEDURE SP_SelProcOP_CTRLTURNDET
(
    @BATNBRTURN       UD_CORR_CONTOMET,
    @SITEID           UD_IDSITE,
    @DATEPROCES       smalldatetime,
    @NBRTUR           char(1),
    @SIDE             UD_CARA,
    @HOSENUMBER       tinyint)
AS
BEGIN
    SELECT BATNBRTURN,
           SITEID,
           DATEPROCES,
           NBRTUR,
           SIDE,
           HOSENUMBER,
           INVTIDBARR,
           STARTREADING,
           ENDREADING,
           QTYSALES,
           QTYSERAF,
           QTYOTHER,
           TOTQTY,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLTURNDET
     WHERE BATNBRTURN = @BATNBRTURN
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR
       AND SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CTRLTURNDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CUST_LIMIT 
 */

CREATE PROCEDURE SP_InsProcOP_CUST_LIMIT
(
    @CUSTIDSS           UD_CODCLIENT,
    @SITEID             UD_IDSITE,
    @PORCENTLIMITED     numeric(5, 2),
    @CREDLIMITED        numeric(11, 4),
    @CURRBAL            numeric(9, 2),
    @PORCENTSURPLUS     numeric(5, 2),
    @MTOSURPLUS         numeric(11, 4),
    @USERID             UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                  = NULL,
    @USER2              UD_USER2                  = NULL,
    @USER3              UD_USER6                  = NULL,
    @USER4              UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUST_LIMIT(CUSTIDSS,
                              SITEID,
                              PORCENTLIMITED,
                              CREDLIMITED,
                              CURRBAL,
                              PORCENTSURPLUS,
                              MTOSURPLUS,
                              USERID,
                              STKNEW,
                              STKMODIFY,
                              STKDELETE,
                              STKSEND,
                              STKRECEIVE,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@CUSTIDSS,
           @SITEID,
           @PORCENTLIMITED,
           @CREDLIMITED,
           @CURRBAL,
           @PORCENTSURPLUS,
           @MTOSURPLUS,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CUST_LIMIT: Cannot insert because primary key value not found in OP_CUST_LIMIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CUST_LIMIT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CUST_LIMIT 
 */

CREATE PROCEDURE SP_UpdProcOP_CUST_LIMIT
(
    @CUSTIDSS           UD_CODCLIENT,
    @SITEID             UD_IDSITE,
    @PORCENTLIMITED     numeric(5, 2),
    @CREDLIMITED        numeric(11, 4),
    @CURRBAL            numeric(9, 2),
    @PORCENTSURPLUS     numeric(5, 2),
    @MTOSURPLUS         numeric(11, 4),
    @USERID             UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                  = NULL,
    @USER2              UD_USER2                  = NULL,
    @USER3              UD_USER6                  = NULL,
    @USER4              UD_USER7                  = NULL,
    @TSTAMP             timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUST_LIMIT
       SET PORCENTLIMITED      = @PORCENTLIMITED,
           CREDLIMITED         = @CREDLIMITED,
           CURRBAL             = @CURRBAL,
           PORCENTSURPLUS      = @PORCENTSURPLUS,
           MTOSURPLUS          = @MTOSURPLUS,
           USERID              = @USERID,
           STKNEW              = @STKNEW,
           STKMODIFY           = @STKMODIFY,
           STKDELETE           = @STKDELETE,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CUST_LIMIT: Cannot update  in OP_CUST_LIMIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CUST_LIMIT >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CUST_LIMIT 
 */

CREATE PROCEDURE SP_DelProcOP_CUST_LIMIT
(
    @CUSTIDSS           UD_CODCLIENT,
    @SITEID             UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUST_LIMIT
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CUST_LIMIT: Cannot delete because foreign keys still exist in OP_CUST_LIMIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CUST_LIMIT >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CUST_LIMIT 
 */

CREATE PROCEDURE SP_SelProcOP_CUST_LIMIT
(
    @CUSTIDSS           UD_CODCLIENT,
    @SITEID             UD_IDSITE)
AS
BEGIN
    SELECT CUSTIDSS,
           SITEID,
           PORCENTLIMITED,
           CREDLIMITED,
           CURRBAL,
           PORCENTSURPLUS,
           MTOSURPLUS,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUST_LIMIT
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CUST_LIMIT >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CUSTBADGE 
 */

CREATE PROCEDURE SP_InsProcOP_CUSTBADGE
(
    @CUSTIDSS       UD_CODCLIENT,
    @BADGE          varchar(12),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUSTBADGE(CUSTIDSS,
                             BADGE,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1 
                             )
    VALUES(@CUSTIDSS,
           @BADGE,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CUSTBADGE: Cannot insert because primary key value not found in OP_CUSTBADGE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CUSTBADGE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CUSTBADGE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CUSTBADGE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CUSTBADGE 
 */

CREATE PROCEDURE SP_UpdProcOP_CUSTBADGE
(
    @CUSTIDSS       UD_CODCLIENT,
    @BADGE          varchar(12),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                = NULL,
    @TSTAMP         timestamp               = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUSTBADGE
       SET STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1
     WHERE CUSTIDSS = @CUSTIDSS
       AND BADGE    = @BADGE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CUSTBADGE: Cannot update  in OP_CUSTBADGE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CUSTBADGE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CUSTBADGE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CUSTBADGE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CUSTBADGE 
 */

CREATE PROCEDURE SP_DelProcOP_CUSTBADGE
(
    @CUSTIDSS       UD_CODCLIENT,
    @BADGE          varchar(12))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUSTBADGE
     WHERE CUSTIDSS = @CUSTIDSS
       AND BADGE    = @BADGE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CUSTBADGE: Cannot delete because foreign keys still exist in OP_CUSTBADGE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CUSTBADGE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CUSTBADGE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CUSTBADGE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CUSTBADGE 
 */

CREATE PROCEDURE SP_SelProcOP_CUSTBADGE
(
    @CUSTIDSS       UD_CODCLIENT,
    @BADGE          varchar(12))
AS
BEGIN
    SELECT CUSTIDSS,
           BADGE,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           TSTAMP
      FROM OP_CUSTBADGE
     WHERE CUSTIDSS = @CUSTIDSS
       AND BADGE    = @BADGE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CUSTBADGE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CUSTBADGE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CUSTBADGE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CUSTCARD 
 */

CREATE PROCEDURE SP_InsProcOP_CUSTCARD
(
    @CUSTIDSS          UD_CODCLIENT,
    @CARDPASSWORD      varchar(60),
    @EXPIREDATE        smalldatetime,
    @DESCRCUSTCARD     UD_DESCRIPCION,
    @USERID            UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUSTCARD(CUSTIDSS,
                            CARDPASSWORD,
                            EXPIREDATE,
                            DESCRCUSTCARD,
                            USERID,
                            STKNEW,
                            STKMODIFY,
                            STKDELETE,
                            STKSEND,
                            STKRECEIVE,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CUSTIDSS,
           @CARDPASSWORD,
           @EXPIREDATE,
           @DESCRCUSTCARD,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CUSTCARD: Cannot insert because primary key value not found in OP_CUSTCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CUSTCARD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CUSTCARD 
 */

CREATE PROCEDURE SP_UpdProcOP_CUSTCARD
(
    @CUSTIDSS          UD_CODCLIENT,
    @CARDPASSWORD      varchar(60),
    @EXPIREDATE        smalldatetime,
    @DESCRCUSTCARD     UD_DESCRIPCION,
    @USERID            UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL,
    @TSTAMP            timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUSTCARD
       SET EXPIREDATE         = @EXPIREDATE,
           DESCRCUSTCARD      = @DESCRCUSTCARD,
           USERID             = @USERID,
           STKNEW             = @STKNEW,
           STKMODIFY          = @STKMODIFY,
           STKDELETE          = @STKDELETE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE CUSTIDSS     = @CUSTIDSS
       AND CARDPASSWORD = @CARDPASSWORD

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CUSTCARD: Cannot update  in OP_CUSTCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CUSTCARD >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CUSTCARD 
 */

CREATE PROCEDURE SP_DelProcOP_CUSTCARD
(
    @CUSTIDSS          UD_CODCLIENT,
    @CARDPASSWORD      varchar(60))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUSTCARD
     WHERE CUSTIDSS     = @CUSTIDSS
       AND CARDPASSWORD = @CARDPASSWORD

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CUSTCARD: Cannot delete because foreign keys still exist in OP_CUSTCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CUSTCARD >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CUSTCARD 
 */

CREATE PROCEDURE SP_SelProcOP_CUSTCARD
(
    @CUSTIDSS          UD_CODCLIENT,
    @CARDPASSWORD      varchar(60))
AS
BEGIN
    SELECT CUSTIDSS,
           CARDPASSWORD,
           EXPIREDATE,
           DESCRCUSTCARD,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUSTCARD
     WHERE CUSTIDSS     = @CUSTIDSS
       AND CARDPASSWORD = @CARDPASSWORD

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CUSTCARD >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CUSTCLASS 
 */

CREATE PROCEDURE SP_InsProcOP_CUSTCLASS
(
    @CUSTCLASSID     UD_CUSTCLASSID,
    @DESCR           UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                  = NULL,
    @USER2           UD_USER2                  = NULL,
    @USER3           UD_USER6                  = NULL,
    @USER4           UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUSTCLASS(CUSTCLASSID,
                             DESCR,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CUSTCLASSID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CUSTCLASS: Cannot insert because primary key value not found in OP_CUSTCLASS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CUSTCLASS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CUSTCLASS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CUSTCLASS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CUSTCLASS 
 */

CREATE PROCEDURE SP_UpdProcOP_CUSTCLASS
(
    @CUSTCLASSID     UD_CUSTCLASSID,
    @DESCR           UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1           UD_USER1                  = NULL,
    @USER2           UD_USER2                  = NULL,
    @USER3           UD_USER6                  = NULL,
    @USER4           UD_USER7                  = NULL,
    @TSTAMP          timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUSTCLASS
       SET DESCR            = @DESCR,
           STKNEW           = @STKNEW,
           STKMODIFY        = @STKMODIFY,
           STKDELETE        = @STKDELETE,
           STKSEND          = @STKSEND,
           STKRECEIVE       = @STKRECEIVE,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE CUSTCLASSID = @CUSTCLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CUSTCLASS: Cannot update  in OP_CUSTCLASS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CUSTCLASS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CUSTCLASS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CUSTCLASS >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CUSTCLASS 
 */

CREATE PROCEDURE SP_DelProcOP_CUSTCLASS
(
    @CUSTCLASSID     UD_CUSTCLASSID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUSTCLASS
     WHERE CUSTCLASSID = @CUSTCLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CUSTCLASS: Cannot delete because foreign keys still exist in OP_CUSTCLASS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CUSTCLASS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CUSTCLASS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CUSTCLASS >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CUSTCLASS 
 */

CREATE PROCEDURE SP_SelProcOP_CUSTCLASS
(
    @CUSTCLASSID     UD_CUSTCLASSID)
AS
BEGIN
    SELECT CUSTCLASSID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUSTCLASS
     WHERE CUSTCLASSID = @CUSTCLASSID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CUSTCLASS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CUSTCLASS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CUSTCLASS >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CUSTOMER_PRCLVL 
 */

CREATE PROCEDURE SP_InsProcOP_CUSTOMER_PRCLVL
(
    @CUSTIDSS         UD_CODCLIENT,
    @PRCLVLCUSTID     UD_PRCLVLCUSTID,
    @USERID           UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                   = NULL,
    @USER2            UD_USER2                   = NULL,
    @USER3            UD_USER6                   = NULL,
    @USER4            UD_USER7                   = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUSTOMER_PRCLVL(CUSTIDSS,
                                   PRCLVLCUSTID,
                                   USERID,
                                   STKNEW,
                                   STKMODIFY,
                                   STKDELETE,
                                   STKSEND,
                                   STKRECEIVE,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@CUSTIDSS,
           @PRCLVLCUSTID,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CUSTOMER_PRCLVL: Cannot insert because primary key value not found in OP_CUSTOMER_PRCLVL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CUSTOMER_PRCLVL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CUSTOMER_PRCLVL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CUSTOMER_PRCLVL >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CUSTOMER_PRCLVL 
 */

CREATE PROCEDURE SP_UpdProcOP_CUSTOMER_PRCLVL
(
    @CUSTIDSS         UD_CODCLIENT,
    @PRCLVLCUSTID     UD_PRCLVLCUSTID,
    @USERID           UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                   = NULL,
    @USER2            UD_USER2                   = NULL,
    @USER3            UD_USER6                   = NULL,
    @USER4            UD_USER7                   = NULL,
    @TSTAMP           timestamp                  = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUSTOMER_PRCLVL
       SET USERID            = @USERID,
           STKNEW            = @STKNEW,
           STKMODIFY         = @STKMODIFY,
           STKDELETE         = @STKDELETE,
           STKSEND           = @STKSEND,
           STKRECEIVE        = @STKRECEIVE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE CUSTIDSS     = @CUSTIDSS
       AND PRCLVLCUSTID = @PRCLVLCUSTID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CUSTOMER_PRCLVL: Cannot update  in OP_CUSTOMER_PRCLVL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CUSTOMER_PRCLVL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CUSTOMER_PRCLVL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CUSTOMER_PRCLVL >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CUSTOMER_PRCLVL 
 */

CREATE PROCEDURE SP_DelProcOP_CUSTOMER_PRCLVL
(
    @CUSTIDSS         UD_CODCLIENT,
    @PRCLVLCUSTID     UD_PRCLVLCUSTID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUSTOMER_PRCLVL
     WHERE CUSTIDSS     = @CUSTIDSS
       AND PRCLVLCUSTID = @PRCLVLCUSTID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CUSTOMER_PRCLVL: Cannot delete because foreign keys still exist in OP_CUSTOMER_PRCLVL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CUSTOMER_PRCLVL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CUSTOMER_PRCLVL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CUSTOMER_PRCLVL >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CUSTOMER_PRCLVL 
 */

CREATE PROCEDURE SP_SelProcOP_CUSTOMER_PRCLVL
(
    @CUSTIDSS         UD_CODCLIENT,
    @PRCLVLCUSTID     UD_PRCLVLCUSTID)
AS
BEGIN
    SELECT CUSTIDSS,
           PRCLVLCUSTID,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUSTOMER_PRCLVL
     WHERE CUSTIDSS     = @CUSTIDSS
       AND PRCLVLCUSTID = @PRCLVLCUSTID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CUSTOMER_PRCLVL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CUSTOMER_PRCLVL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CUSTOMER_PRCLVL >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_CUSTZONE 
 */

CREATE PROCEDURE SP_InsProcOP_CUSTZONE
(
    @CUSTZONEID     char(2),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUSTZONE(CUSTZONEID,
                            DESCR,
                            STKNEW,
                            STKMODIFY,
                            STKDELETE,
                            STKSEND,
                            STKRECEIVE,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CUSTZONEID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_CUSTZONE: Cannot insert because primary key value not found in OP_CUSTZONE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_CUSTZONE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_CUSTZONE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_CUSTZONE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_CUSTZONE 
 */

CREATE PROCEDURE SP_UpdProcOP_CUSTZONE
(
    @CUSTZONEID     char(2),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUSTZONE
       SET DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE CUSTZONEID = @CUSTZONEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_CUSTZONE: Cannot update  in OP_CUSTZONE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_CUSTZONE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_CUSTZONE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_CUSTZONE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_CUSTZONE 
 */

CREATE PROCEDURE SP_DelProcOP_CUSTZONE
(
    @CUSTZONEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUSTZONE
     WHERE CUSTZONEID = @CUSTZONEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_CUSTZONE: Cannot delete because foreign keys still exist in OP_CUSTZONE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_CUSTZONE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_CUSTZONE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_CUSTZONE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_CUSTZONE 
 */

CREATE PROCEDURE SP_SelProcOP_CUSTZONE
(
    @CUSTZONEID     char(2))
AS
BEGIN
    SELECT CUSTZONEID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUSTZONE
     WHERE CUSTZONEID = @CUSTZONEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_CUSTZONE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_CUSTZONE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_CUSTZONE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DEFINITIONPD 
 */

CREATE PROCEDURE SP_InsProcOP_DEFINITIONPD
(
    @DEFINITIONID     char(2),
    @CUSTIDSS         char(20)                = NULL,
    @CLASSID          char(5)                 = NULL,
    @CLASSUBID        char(5)                 = NULL,
    @INVTIDSKU        char(20)                = NULL,
    @DEBEHABER        UD_DEBEHABER,
    @USERID           UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                = NULL,
    @USER2            UD_USER2                = NULL,
    @USER3            UD_USER6                = NULL,
    @USER4            UD_USER7                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEFINITIONPD(DEFINITIONID,
                                CUSTIDSS,
                                CLASSID,
                                CLASSUBID,
                                INVTIDSKU,
                                DEBEHABER,
                                USERID,
                                STKSEND,
                                STKRECEIVE,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@DEFINITIONID,
           @CUSTIDSS,
           @CLASSID,
           @CLASSUBID,
           @INVTIDSKU,
           @DEBEHABER,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DEFINITIONPD: Cannot insert because primary key value not found in OP_DEFINITIONPD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DEFINITIONPD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DEFINITIONPD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DEFINITIONPD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DEFINITIONPD 
 */

CREATE PROCEDURE SP_UpdProcOP_DEFINITIONPD
(
    @DEFINITIONID     char(2),
    @CUSTIDSS         char(20)                = NULL,
    @CLASSID          char(5)                 = NULL,
    @CLASSUBID        char(5)                 = NULL,
    @INVTIDSKU        char(20)                = NULL,
    @DEBEHABER        UD_DEBEHABER,
    @USERID           UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                = NULL,
    @USER2            UD_USER2                = NULL,
    @USER3            UD_USER6                = NULL,
    @USER4            UD_USER7                = NULL,
    @TSTAMP           timestamp               = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEFINITIONPD
       SET CUSTIDSS          = @CUSTIDSS,
           CLASSID           = @CLASSID,
           CLASSUBID         = @CLASSUBID,
           INVTIDSKU         = @INVTIDSKU,
           DEBEHABER         = @DEBEHABER,
           USERID            = @USERID,
           STKSEND           = @STKSEND,
           STKRECEIVE        = @STKRECEIVE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE DEFINITIONID = @DEFINITIONID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DEFINITIONPD: Cannot update  in OP_DEFINITIONPD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DEFINITIONPD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DEFINITIONPD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DEFINITIONPD >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DEFINITIONPD 
 */

CREATE PROCEDURE SP_DelProcOP_DEFINITIONPD
(
    @DEFINITIONID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEFINITIONPD
     WHERE DEFINITIONID = @DEFINITIONID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DEFINITIONPD: Cannot delete because foreign keys still exist in OP_DEFINITIONPD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DEFINITIONPD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DEFINITIONPD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DEFINITIONPD >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DEFINITIONPD 
 */

CREATE PROCEDURE SP_SelProcOP_DEFINITIONPD
(
    @DEFINITIONID     char(2))
AS
BEGIN
    SELECT DEFINITIONID,
           CUSTIDSS,
           CLASSID,
           CLASSUBID,
           INVTIDSKU,
           DEBEHABER,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEFINITIONPD
     WHERE DEFINITIONID = @DEFINITIONID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DEFINITIONPD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DEFINITIONPD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DEFINITIONPD >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DEPOSIT 
 */

CREATE PROCEDURE SP_InsProcOP_DEPOSIT
(
    @BATNBRDEPOSIT       numeric(10, 0),
    @SITEID              UD_IDSITE,
    @SALESPERSONTURN     tinyint,
    @OPDATE              smalldatetime,
    @DATEPROCE           smalldatetime,
    @STATUS              bit                       = NULL,
    @SALESPERID          char(20),
    @USERID              UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1               UD_USER1                  = NULL,
    @USER2               UD_USER2                  = NULL,
    @USER3               UD_USER6                  = NULL,
    @USER4               UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSIT(BATNBRDEPOSIT,
                           SITEID,
                           SALESPERSONTURN,
                           OPDATE,
                           DATEPROCE,
                           STATUS,
                           SALESPERID,
                           USERID,
                           STKSEND,
                           STKRECEIVE,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@BATNBRDEPOSIT,
           @SITEID,
           @SALESPERSONTURN,
           @OPDATE,
           @DATEPROCE,
           @STATUS,
           @SALESPERID,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DEPOSIT: Cannot insert because primary key value not found in OP_DEPOSIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DEPOSIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DEPOSIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DEPOSIT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DEPOSIT 
 */

CREATE PROCEDURE SP_UpdProcOP_DEPOSIT
(
    @BATNBRDEPOSIT       numeric(10, 0),
    @SITEID              UD_IDSITE,
    @SALESPERSONTURN     tinyint,
    @OPDATE              smalldatetime,
    @DATEPROCE           smalldatetime,
    @STATUS              bit                       = NULL,
    @SALESPERID          char(20),
    @USERID              UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1               UD_USER1                  = NULL,
    @USER2               UD_USER2                  = NULL,
    @USER3               UD_USER6                  = NULL,
    @USER4               UD_USER7                  = NULL,
    @TSTAMP              timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSIT
       SET SALESPERSONTURN      = @SALESPERSONTURN,
           OPDATE               = @OPDATE,
           DATEPROCE            = @DATEPROCE,
           STATUS               = @STATUS,
           SALESPERID           = @SALESPERID,
           USERID               = @USERID,
           STKSEND              = @STKSEND,
           STKRECEIVE           = @STKRECEIVE,
           USER1                = @USER1,
           USER2                = @USER2,
           USER3                = @USER3,
           USER4                = @USER4
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DEPOSIT: Cannot update  in OP_DEPOSIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DEPOSIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DEPOSIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DEPOSIT >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DEPOSIT 
 */

CREATE PROCEDURE SP_DelProcOP_DEPOSIT
(
    @BATNBRDEPOSIT       numeric(10, 0),
    @SITEID              UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSIT
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DEPOSIT: Cannot delete because foreign keys still exist in OP_DEPOSIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DEPOSIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DEPOSIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DEPOSIT >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DEPOSIT 
 */

CREATE PROCEDURE SP_SelProcOP_DEPOSIT
(
    @BATNBRDEPOSIT       numeric(10, 0),
    @SITEID              UD_IDSITE)
AS
BEGIN
    SELECT BATNBRDEPOSIT,
           SITEID,
           SALESPERSONTURN,
           OPDATE,
           DATEPROCE,
           STATUS,
           SALESPERID,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSIT
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DEPOSIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DEPOSIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DEPOSIT >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DEPOSITDET 
 */

CREATE PROCEDURE SP_InsProcOP_DEPOSITDET
(
    @BATNBRDEPOSIT     UD_CORRELATIVOS,
    @SITEID            UD_IDSITE,
    @SEQUENCE          UD_REGISTRO,
    @CARDID            char(2)                    = NULL,
    @TOTAL             UD_MONTO,
    @CUSTIDSS          UD_CODCLIENT               = NULL,
    @CURYID            UD_CURYID,
    @DEPOSITTYPEID     char(1),
    @DATEPROCE         UD_FECHA,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                   = NULL,
    @USER2             UD_USER2                   = NULL,
    @USER3             UD_USER6                   = NULL,
    @USER4             UD_USER7                   = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSITDET(BATNBRDEPOSIT,
                              SITEID,
                              SEQUENCE,
                              CARDID,
                              TOTAL,
                              CUSTIDSS,
                              CURYID,
                              DEPOSITTYPEID,
                              DATEPROCE,
                              STKSEND,
                              STKRECEIVE,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@BATNBRDEPOSIT,
           @SITEID,
           @SEQUENCE,
           @CARDID,
           @TOTAL,
           @CUSTIDSS,
           @CURYID,
           @DEPOSITTYPEID,
           @DATEPROCE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DEPOSITDET: Cannot insert because primary key value not found in OP_DEPOSITDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DEPOSITDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DEPOSITDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DEPOSITDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DEPOSITDET 
 */

CREATE PROCEDURE SP_UpdProcOP_DEPOSITDET
(
    @BATNBRDEPOSIT     UD_CORRELATIVOS,
    @SITEID            UD_IDSITE,
    @SEQUENCE          UD_REGISTRO,
    @CARDID            char(2)                    = NULL,
    @TOTAL             UD_MONTO,
    @CUSTIDSS          UD_CODCLIENT               = NULL,
    @CURYID            UD_CURYID,
    @DEPOSITTYPEID     char(1),
    @DATEPROCE         UD_FECHA,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                   = NULL,
    @USER2             UD_USER2                   = NULL,
    @USER3             UD_USER6                   = NULL,
    @USER4             UD_USER7                   = NULL,
    @TSTAMP            timestamp                  = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSITDET
       SET CARDID             = @CARDID,
           TOTAL              = @TOTAL,
           CUSTIDSS           = @CUSTIDSS,
           CURYID             = @CURYID,
           DEPOSITTYPEID      = @DEPOSITTYPEID,
           DATEPROCE          = @DATEPROCE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID
       AND SEQUENCE      = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DEPOSITDET: Cannot update  in OP_DEPOSITDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DEPOSITDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DEPOSITDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DEPOSITDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DEPOSITDET 
 */

CREATE PROCEDURE SP_DelProcOP_DEPOSITDET
(
    @BATNBRDEPOSIT     UD_CORRELATIVOS,
    @SITEID            UD_IDSITE,
    @SEQUENCE          UD_REGISTRO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITDET
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID
       AND SEQUENCE      = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DEPOSITDET: Cannot delete because foreign keys still exist in OP_DEPOSITDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DEPOSITDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DEPOSITDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DEPOSITDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DEPOSITDET 
 */

CREATE PROCEDURE SP_SelProcOP_DEPOSITDET
(
    @BATNBRDEPOSIT     UD_CORRELATIVOS,
    @SITEID            UD_IDSITE,
    @SEQUENCE          UD_REGISTRO)
AS
BEGIN
    SELECT BATNBRDEPOSIT,
           SITEID,
           SEQUENCE,
           CARDID,
           TOTAL,
           CUSTIDSS,
           CURYID,
           DEPOSITTYPEID,
           DATEPROCE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSITDET
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID
       AND SEQUENCE      = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DEPOSITDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DEPOSITDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DEPOSITDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DEPOSITSITE 
 */

CREATE PROCEDURE SP_InsProcOP_DEPOSITSITE
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @DATEPROCE             smalldatetime,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @STATUS                bit                       = NULL,
    @USERID                UD_USERID,
    @STKSEND        bit=0,
    @STRECEIVE             bit,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSITSITE(SITEID,
                               BATNBRDEPOSITSITE,
                               DATEPROCE,
                               TOTALPEN,
                               TOTALUSD,
                               STATUS,
                               USERID,
                               STKSEND,
                               STRECEIVE,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@SITEID,
           @BATNBRDEPOSITSITE,
           @DATEPROCE,
           @TOTALPEN,
           @TOTALUSD,
           @STATUS,
           @USERID,
           @STKSEND,
           @STRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DEPOSITSITE: Cannot insert because primary key value not found in OP_DEPOSITSITE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DEPOSITSITE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DEPOSITSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DEPOSITSITE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DEPOSITSITE 
 */

CREATE PROCEDURE SP_UpdProcOP_DEPOSITSITE
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @DATEPROCE             smalldatetime,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @STATUS                bit                       = NULL,
    @USERID                UD_USERID,
    @STKSEND        bit=0,
    @STRECEIVE             bit,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSITSITE
       SET TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           STATUS                 = @STATUS,
           USERID                 = @USERID,
           STKSEND                = @STKSEND,
           STRECEIVE              = @STRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND DATEPROCE         = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DEPOSITSITE: Cannot update  in OP_DEPOSITSITE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DEPOSITSITE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DEPOSITSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DEPOSITSITE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DEPOSITSITE 
 */

CREATE PROCEDURE SP_DelProcOP_DEPOSITSITE
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @DATEPROCE             smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITSITE
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND DATEPROCE         = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DEPOSITSITE: Cannot delete because foreign keys still exist in OP_DEPOSITSITE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DEPOSITSITE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DEPOSITSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DEPOSITSITE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DEPOSITSITE 
 */

CREATE PROCEDURE SP_SelProcOP_DEPOSITSITE
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @DATEPROCE             smalldatetime)
AS
BEGIN
    SELECT SITEID,
           BATNBRDEPOSITSITE,
           DATEPROCE,
           TOTALPEN,
           TOTALUSD,
           STATUS,
           USERID,
           STKSEND,
           STRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSITSITE
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND DATEPROCE         = @DATEPROCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DEPOSITSITE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DEPOSITSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DEPOSITSITE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DEPOSITSITEDET 
 */

CREATE PROCEDURE SP_InsProcOP_DEPOSITSITEDET
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @DATEPROCE             smalldatetime,
    @SEQUENCE              smallint,
    @CUSTIDSS              UD_CODCLIENT,
    @DOCSUMMARYTYPEID      char(2)                   = NULL,
    @REFERENCE             numeric(10, 0),
    @DEPOSITPASSWORD       char(20),
    @DATEDEPOSIT           smalldatetime,
    @CURYID                char(3),
    @CURYRATE              UD_CURYRATE,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @DEBEHABER             UD_DEBEHABER,
    @STATUS                bit                       = NULL,
    @USERID                UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSITSITEDET(SITEID,
                                  BATNBRDEPOSITSITE,
                                  DATEPROCE,
                                  SEQUENCE,
                                  CUSTIDSS,
                                  DOCSUMMARYTYPEID,
                                  REFERENCE,
                                  DEPOSITPASSWORD,
                                  DATEDEPOSIT,
                                  CURYID,
                                  CURYRATE,
                                  TOTALPEN,
                                  TOTALUSD,
                                  DEBEHABER,
                                  STATUS,
                                  USERID,
                                  STKSEND,
                                  STKRECEIVE,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@SITEID,
           @BATNBRDEPOSITSITE,
           @DATEPROCE,
           @SEQUENCE,
           @CUSTIDSS,
           @DOCSUMMARYTYPEID,
           @REFERENCE,
           @DEPOSITPASSWORD,
           @DATEDEPOSIT,
           @CURYID,
           @CURYRATE,
           @TOTALPEN,
           @TOTALUSD,
           @DEBEHABER,
           @STATUS,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DEPOSITSITEDET: Cannot insert because primary key value not found in OP_DEPOSITSITEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DEPOSITSITEDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DEPOSITSITEDET 
 */

CREATE PROCEDURE SP_UpdProcOP_DEPOSITSITEDET
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @DATEPROCE             smalldatetime,
    @SEQUENCE              smallint,
    @CUSTIDSS              UD_CODCLIENT,
    @DOCSUMMARYTYPEID      char(2)                   = NULL,
    @REFERENCE             numeric(10, 0),
    @DEPOSITPASSWORD       char(20),
    @DATEDEPOSIT           smalldatetime,
    @CURYID                char(3),
    @CURYRATE              UD_CURYRATE,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @DEBEHABER             UD_DEBEHABER,
    @STATUS                bit                       = NULL,
    @USERID                UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSITSITEDET
       SET CUSTIDSS               = @CUSTIDSS,
           DOCSUMMARYTYPEID       = @DOCSUMMARYTYPEID,
           REFERENCE              = @REFERENCE,
           DEPOSITPASSWORD        = @DEPOSITPASSWORD,
           DATEDEPOSIT            = @DATEDEPOSIT,
           CURYID                 = @CURYID,
           CURYRATE               = @CURYRATE,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           DEBEHABER              = @DEBEHABER,
           STATUS                 = @STATUS,
           USERID                 = @USERID,
           STKSEND                = @STKSEND,
           STKRECEIVE             = @STKRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND DATEPROCE         = @DATEPROCE
       AND SEQUENCE          = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DEPOSITSITEDET: Cannot update  in OP_DEPOSITSITEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DEPOSITSITEDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DEPOSITSITEDET 
 */

CREATE PROCEDURE SP_DelProcOP_DEPOSITSITEDET
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @DATEPROCE             smalldatetime,
    @SEQUENCE              smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITSITEDET
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND DATEPROCE         = @DATEPROCE
       AND SEQUENCE          = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DEPOSITSITEDET: Cannot delete because foreign keys still exist in OP_DEPOSITSITEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DEPOSITSITEDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DEPOSITSITEDET 
 */

CREATE PROCEDURE SP_SelProcOP_DEPOSITSITEDET
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @DATEPROCE             smalldatetime,
    @SEQUENCE              smallint)
AS
BEGIN
    SELECT SITEID,
           BATNBRDEPOSITSITE,
           DATEPROCE,
           SEQUENCE,
           CUSTIDSS,
           DOCSUMMARYTYPEID,
           REFERENCE,
           DEPOSITPASSWORD,
           DATEDEPOSIT,
           CURYID,
           CURYRATE,
           TOTALPEN,
           TOTALUSD,
           DEBEHABER,
           STATUS,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSITSITEDET
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND DATEPROCE         = @DATEPROCE
       AND SEQUENCE          = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DEPOSITSITEDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DEPOSITTYPE 
 */

CREATE PROCEDURE SP_InsProcOP_DEPOSITTYPE
(
    @DEPOSITTYPEID        char(1),
    @DESCRDEPOSTITYPE     UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01               UD_USER1                  = NULL,
    @USER02               UD_USER2                  = NULL,
    @USER03               UD_USER6                  = NULL,
    @USER04               UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSITTYPE(DEPOSITTYPEID,
                               DESCRDEPOSTITYPE,
                               STKNEW,
                               STKMODIFY,
                               STKDELETE,
                               STKSEND,
                               STKRECEIVE,
                               USER01,
                               USER02,
                               USER03,
                               USER04 
                               )
    VALUES(@DEPOSITTYPEID,
           @DESCRDEPOSTITYPE,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DEPOSITTYPE: Cannot insert because primary key value not found in OP_DEPOSITTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DEPOSITTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DEPOSITTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DEPOSITTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DEPOSITTYPE 
 */

CREATE PROCEDURE SP_UpdProcOP_DEPOSITTYPE
(
    @DEPOSITTYPEID        char(1),
    @DESCRDEPOSTITYPE     UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01               UD_USER1                  = NULL,
    @USER02               UD_USER2                  = NULL,
    @USER03               UD_USER6                  = NULL,
    @USER04               UD_USER7                  = NULL,
    @TSTAMP               timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSITTYPE
       SET DESCRDEPOSTITYPE      = @DESCRDEPOSTITYPE,
           STKNEW                = @STKNEW,
           STKMODIFY             = @STKMODIFY,
           STKDELETE             = @STKDELETE,
           STKSEND               = @STKSEND,
           STKRECEIVE            = @STKRECEIVE,
           USER01                = @USER01,
           USER02                = @USER02,
           USER03                = @USER03,
           USER04                = @USER04
     WHERE DEPOSITTYPEID = @DEPOSITTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DEPOSITTYPE: Cannot update  in OP_DEPOSITTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DEPOSITTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DEPOSITTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DEPOSITTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DEPOSITTYPE 
 */

CREATE PROCEDURE SP_DelProcOP_DEPOSITTYPE
(
    @DEPOSITTYPEID        char(1))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITTYPE
     WHERE DEPOSITTYPEID = @DEPOSITTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DEPOSITTYPE: Cannot delete because foreign keys still exist in OP_DEPOSITTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DEPOSITTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DEPOSITTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DEPOSITTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DEPOSITTYPE 
 */

CREATE PROCEDURE SP_SelProcOP_DEPOSITTYPE
(
    @DEPOSITTYPEID        char(1))
AS
BEGIN
    SELECT DEPOSITTYPEID,
           DESCRDEPOSTITYPE,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_DEPOSITTYPE
     WHERE DEPOSITTYPEID = @DEPOSITTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DEPOSITTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DEPOSITTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DEPOSITTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DOCREFERENCE 
 */

CREATE PROCEDURE SP_InsProcOP_DOCREFERENCE
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          numeric(10, 0),
    @SITEID               char(5),
    @DOCTYPEIDREF         UD_DOCTYPEID,
    @NBRDOCUMENTREF       UD_CORRELATIVOS,
    @TYPEPAYMENTIDREF     UD_TYPEPAYMENTID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DOCREFERENCE(DOCTYPEID,
                                NBRDOCUMENT,
                                SITEID,
                                DOCTYPEIDREF,
                                NBRDOCUMENTREF,
                                TYPEPAYMENTIDREF,
                                STKNEW,
                                STKMODIFY,
                                STKDELETE,
                                STKSEND,
                                STKRECEIVE)
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SITEID,
           @DOCTYPEIDREF,
           @NBRDOCUMENTREF,
           @TYPEPAYMENTIDREF,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DOCREFERENCE: Cannot insert because primary key value not found in OP_DOCREFERENCE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DOCREFERENCE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DOCREFERENCE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DOCREFERENCE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DOCREFERENCE 
 */

CREATE PROCEDURE SP_UpdProcOP_DOCREFERENCE
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          numeric(10, 0),
    @SITEID               char(5),
    @DOCTYPEIDREF         UD_DOCTYPEID,
    @NBRDOCUMENTREF       UD_CORRELATIVOS,
    @TYPEPAYMENTIDREF     UD_TYPEPAYMENTID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DOCREFERENCE
       SET STKNEW                = @STKNEW,
           STKMODIFY             = @STKMODIFY,
           STKDELETE             = @STKDELETE,
           STKSEND               = @STKSEND,
           STKRECEIVE            = @STKRECEIVE
     WHERE DOCTYPEID        = @DOCTYPEID
       AND NBRDOCUMENT      = @NBRDOCUMENT
       AND SITEID           = @SITEID
       AND DOCTYPEIDREF     = @DOCTYPEIDREF
       AND NBRDOCUMENTREF   = @NBRDOCUMENTREF
       AND TYPEPAYMENTIDREF = @TYPEPAYMENTIDREF

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DOCREFERENCE: Cannot update  in OP_DOCREFERENCE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DOCREFERENCE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DOCREFERENCE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DOCREFERENCE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DOCREFERENCE 
 */

CREATE PROCEDURE SP_DelProcOP_DOCREFERENCE
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          numeric(10, 0),
    @SITEID               char(5),
    @DOCTYPEIDREF         UD_DOCTYPEID,
    @NBRDOCUMENTREF       UD_CORRELATIVOS,
    @TYPEPAYMENTIDREF     UD_TYPEPAYMENTID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DOCREFERENCE
     WHERE DOCTYPEID        = @DOCTYPEID
       AND NBRDOCUMENT      = @NBRDOCUMENT
       AND SITEID           = @SITEID
       AND DOCTYPEIDREF     = @DOCTYPEIDREF
       AND NBRDOCUMENTREF   = @NBRDOCUMENTREF
       AND TYPEPAYMENTIDREF = @TYPEPAYMENTIDREF

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DOCREFERENCE: Cannot delete because foreign keys still exist in OP_DOCREFERENCE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DOCREFERENCE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DOCREFERENCE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DOCREFERENCE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DOCREFERENCE 
 */

CREATE PROCEDURE SP_SelProcOP_DOCREFERENCE
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          numeric(10, 0),
    @SITEID               char(5),
    @DOCTYPEIDREF         UD_DOCTYPEID,
    @NBRDOCUMENTREF       UD_CORRELATIVOS,
    @TYPEPAYMENTIDREF     UD_TYPEPAYMENTID)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SITEID,
           DOCTYPEIDREF,
           NBRDOCUMENTREF,
           TYPEPAYMENTIDREF,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE
      FROM OP_DOCREFERENCE
     WHERE DOCTYPEID        = @DOCTYPEID
       AND NBRDOCUMENT      = @NBRDOCUMENT
       AND SITEID           = @SITEID
       AND DOCTYPEIDREF     = @DOCTYPEIDREF
       AND NBRDOCUMENTREF   = @NBRDOCUMENTREF
       AND TYPEPAYMENTIDREF = @TYPEPAYMENTIDREF

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DOCREFERENCE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DOCREFERENCE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DOCREFERENCE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DOCSERIES 
 */

CREATE PROCEDURE SP_InsProcOP_DOCSERIES
(
    @DOCTYPEID      UD_DOCTYPEID,
    @NBRSERIES      numeric(3, 0),
    @SITEID         UD_IDSITE,
    @NBRDOC         numeric(7, 0)            = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DOCSERIES(DOCTYPEID,
                             NBRSERIES,
                             SITEID,
                             NBRDOC,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2 
                             )
    VALUES(@DOCTYPEID,
           @NBRSERIES,
           @SITEID,
           @NBRDOC,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DOCSERIES: Cannot insert because primary key value not found in OP_DOCSERIES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DOCSERIES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DOCSERIES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DOCSERIES >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DOCSERIES 
 */

CREATE PROCEDURE SP_UpdProcOP_DOCSERIES
(
    @DOCTYPEID      UD_DOCTYPEID,
    @NBRSERIES      numeric(3, 0),
    @SITEID         UD_IDSITE,
    @NBRDOC         numeric(7, 0)            = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @TSTAMP         timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DOCSERIES
       SET NBRDOC          = @NBRDOC,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2
     WHERE DOCTYPEID = @DOCTYPEID
       AND NBRSERIES = @NBRSERIES
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DOCSERIES: Cannot update  in OP_DOCSERIES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DOCSERIES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DOCSERIES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DOCSERIES >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DOCSERIES 
 */

CREATE PROCEDURE SP_DelProcOP_DOCSERIES
(
    @DOCTYPEID      UD_DOCTYPEID,
    @NBRSERIES      numeric(3, 0),
    @SITEID         UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DOCSERIES
     WHERE DOCTYPEID = @DOCTYPEID
       AND NBRSERIES = @NBRSERIES
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DOCSERIES: Cannot delete because foreign keys still exist in OP_DOCSERIES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DOCSERIES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DOCSERIES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DOCSERIES >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DOCSERIES 
 */

CREATE PROCEDURE SP_SelProcOP_DOCSERIES
(
    @DOCTYPEID      UD_DOCTYPEID,
    @NBRSERIES      numeric(3, 0),
    @SITEID         UD_IDSITE)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRSERIES,
           SITEID,
           NBRDOC,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           TSTAMP
      FROM OP_DOCSERIES
     WHERE DOCTYPEID = @DOCTYPEID
       AND NBRSERIES = @NBRSERIES
       AND SITEID    = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DOCSERIES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DOCSERIES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DOCSERIES >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DOCSUMMARY 
 */

CREATE PROCEDURE SP_InsProcOP_DOCSUMMARY
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @DOCDATE              smalldatetime,
    @DUEDATE              smalldatetime,
    @DOCSTATUSID          char(2),
    @TAXID01              UD_TAXID,
    @TAXID02              UD_TAXID,
    @TERMID               char(2),
    @CURYTYPEID           char(6),
    @CURYRATE             UD_CURYRATE,
    @CURYID               char(3),
    @CUSTIDSS             char(20),
    @CUSTID               UD_CODCLIENT,
    @CUSTNAME             UD_NOMBRE                 = NULL,
    @TAXREGNBR            UD_CODCLIENT,
    @WEEKDAYPAG           char(12),
    @DAYTHENK             int,
    @TOTALPEN             numeric(11, 4),
    @TOTALUSD             numeric(11, 4),
    @SUBTOTALPEN          numeric(11, 4),
    @SUBTOTALUSD          numeric(11, 4),
    @TAXPEN               numeric(11, 4),
    @TAXUSD               numeric(11, 4),
    @STKITEM              bit                       = NULL,
    @STKPROCE             bit,
    @USERID               UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL,
    @USER3                UD_USER6                  = NULL,
    @USER4                UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DOCSUMMARY(SITEID,
                              DOCSUMMARYTYPEID,
                              NBRDOCSUMMARY,
                              DOCDATE,
                              DUEDATE,
                              DOCSTATUSID,
                              TAXID01,
                              TAXID02,
                              TERMID,
                              CURYTYPEID,
                              CURYRATE,
                              CURYID,
                              CUSTIDSS,
                              CUSTID,
                              CUSTNAME,
                              TAXREGNBR,
                              WEEKDAYPAG,
                              DAYTHENK,
                              TOTALPEN,
                              TOTALUSD,
                              SUBTOTALPEN,
                              SUBTOTALUSD,
                              TAXPEN,
                              TAXUSD,
                              STKITEM,
                              STKPROCE,
                              USERID,
                              STKNEW,
                              STKMODIFY,
                              STKDELETE,
                              STKSEND,
                              STKRECEIVE,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@SITEID,
           @DOCSUMMARYTYPEID,
           @NBRDOCSUMMARY,
           @DOCDATE,
           @DUEDATE,
           @DOCSTATUSID,
           @TAXID01,
           @TAXID02,
           @TERMID,
           @CURYTYPEID,
           @CURYRATE,
           @CURYID,
           @CUSTIDSS,
           @CUSTID,
           @CUSTNAME,
           @TAXREGNBR,
           @WEEKDAYPAG,
           @DAYTHENK,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKPROCE,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DOCSUMMARY: Cannot insert because primary key value not found in OP_DOCSUMMARY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DOCSUMMARY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DOCSUMMARY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DOCSUMMARY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DOCSUMMARY 
 */

CREATE PROCEDURE SP_UpdProcOP_DOCSUMMARY
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @DOCDATE              smalldatetime,
    @DUEDATE              smalldatetime,
    @DOCSTATUSID          char(2),
    @TAXID01              UD_TAXID,
    @TAXID02              UD_TAXID,
    @TERMID               char(2),
    @CURYTYPEID           char(6),
    @CURYRATE             UD_CURYRATE,
    @CURYID               char(3),
    @CUSTIDSS             char(20),
    @CUSTID               UD_CODCLIENT,
    @CUSTNAME             UD_NOMBRE                 = NULL,
    @TAXREGNBR            UD_CODCLIENT,
    @WEEKDAYPAG           char(12),
    @DAYTHENK             int,
    @TOTALPEN             numeric(11, 4),
    @TOTALUSD             numeric(11, 4),
    @SUBTOTALPEN          numeric(11, 4),
    @SUBTOTALUSD          numeric(11, 4),
    @TAXPEN               numeric(11, 4),
    @TAXUSD               numeric(11, 4),
    @STKITEM              bit                       = NULL,
    @STKPROCE             bit,
    @USERID               UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL,
    @USER3                UD_USER6                  = NULL,
    @USER4                UD_USER7                  = NULL,
    @TSTAMP               timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DOCSUMMARY
       SET DOCDATE               = @DOCDATE,
           DUEDATE               = @DUEDATE,
           DOCSTATUSID           = @DOCSTATUSID,
           TAXID01               = @TAXID01,
           TAXID02               = @TAXID02,
           TERMID                = @TERMID,
           CURYTYPEID            = @CURYTYPEID,
           CURYRATE              = @CURYRATE,
           CURYID                = @CURYID,
           CUSTIDSS              = @CUSTIDSS,
           CUSTID                = @CUSTID,
           CUSTNAME              = @CUSTNAME,
           TAXREGNBR             = @TAXREGNBR,
           WEEKDAYPAG            = @WEEKDAYPAG,
           DAYTHENK              = @DAYTHENK,
           TOTALPEN              = @TOTALPEN,
           TOTALUSD              = @TOTALUSD,
           SUBTOTALPEN           = @SUBTOTALPEN,
           SUBTOTALUSD           = @SUBTOTALUSD,
           TAXPEN                = @TAXPEN,
           TAXUSD                = @TAXUSD,
           STKITEM               = @STKITEM,
           STKPROCE              = @STKPROCE,
           USERID                = @USERID,
           STKNEW                = @STKNEW,
           STKMODIFY             = @STKMODIFY,
           STKDELETE             = @STKDELETE,
           STKSEND               = @STKSEND,
           STKRECEIVE            = @STKRECEIVE,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID           = @SITEID
       AND DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND NBRDOCSUMMARY    = @NBRDOCSUMMARY

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DOCSUMMARY: Cannot update  in OP_DOCSUMMARY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DOCSUMMARY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DOCSUMMARY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DOCSUMMARY >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DOCSUMMARY 
 */

CREATE PROCEDURE SP_DelProcOP_DOCSUMMARY
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DOCSUMMARY
     WHERE SITEID           = @SITEID
       AND DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND NBRDOCSUMMARY    = @NBRDOCSUMMARY

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DOCSUMMARY: Cannot delete because foreign keys still exist in OP_DOCSUMMARY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DOCSUMMARY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DOCSUMMARY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DOCSUMMARY >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DOCSUMMARY 
 */

CREATE PROCEDURE SP_SelProcOP_DOCSUMMARY
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10))
AS
BEGIN
    SELECT SITEID,
           DOCSUMMARYTYPEID,
           NBRDOCSUMMARY,
           DOCDATE,
           DUEDATE,
           DOCSTATUSID,
           TAXID01,
           TAXID02,
           TERMID,
           CURYTYPEID,
           CURYRATE,
           CURYID,
           CUSTIDSS,
           CUSTID,
           CUSTNAME,
           TAXREGNBR,
           WEEKDAYPAG,
           DAYTHENK,
           TOTALPEN,
           TOTALUSD,
           SUBTOTALPEN,
           SUBTOTALUSD,
           TAXPEN,
           TAXUSD,
           STKITEM,
           STKPROCE,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DOCSUMMARY
     WHERE SITEID           = @SITEID
       AND DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND NBRDOCSUMMARY    = @NBRDOCSUMMARY

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DOCSUMMARY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DOCSUMMARY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DOCSUMMARY >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DOCSUMMARYDET 
 */

CREATE PROCEDURE SP_InsProcOP_DOCSUMMARYDET
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             smallint,
    @CURYTYPEID           char(6),
    @CURYID               char(3),
    @TAXID02              UD_TAXID,
    @TAXID01              UD_TAXID,
    @DISCOUNT             numeric(12, 2)              = NULL,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @SLS_PRICE            numeric(8, 4),
    @QTY                  numeric(10, 3),
    @TOTALPEN             numeric(11, 4),
    @TOTALUSD             numeric(11, 4),
    @SUBTOTALPEN          numeric(11, 4),
    @SUBTOTALUSD          numeric(11, 4),
    @TAXPEN               numeric(11, 4),
    @TAXUSD               numeric(11, 4),
    @USERID               UD_USERID,
    @DOCDATE              smalldatetime,
    @INVTIDSOLOMON        char(20),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                    = NULL,
    @USER2                UD_USER2                    = NULL,
    @USER3                UD_USER6                    = NULL,
    @USER4                UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DOCSUMMARYDET(SITEID,
                                 DOCSUMMARYTYPEID,
                                 NBRDOCSUMMARY,
                                 SEQUENCE,
                                 CURYTYPEID,
                                 CURYID,
                                 TAXID02,
                                 TAXID01,
                                 DISCOUNT,
                                 INVTIDSKU,
                                 SLS_PRICE,
                                 QTY,
                                 TOTALPEN,
                                 TOTALUSD,
                                 SUBTOTALPEN,
                                 SUBTOTALUSD,
                                 TAXPEN,
                                 TAXUSD,
                                 USERID,
                                 DOCDATE,
                                 INVTIDSOLOMON,
                                 STKNEW,
                                 STKMODIFY,
                                 STKDELETE,
                                 STKSEND,
                                 STKRECEIVE,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @DOCSUMMARYTYPEID,
           @NBRDOCSUMMARY,
           @SEQUENCE,
           @CURYTYPEID,
           @CURYID,
           @TAXID02,
           @TAXID01,
           @DISCOUNT,
           @INVTIDSKU,
           @SLS_PRICE,
           @QTY,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @USERID,
           @DOCDATE,
           @INVTIDSOLOMON,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DOCSUMMARYDET: Cannot insert because primary key value not found in OP_DOCSUMMARYDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DOCSUMMARYDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DOCSUMMARYDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DOCSUMMARYDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DOCSUMMARYDET 
 */

CREATE PROCEDURE SP_UpdProcOP_DOCSUMMARYDET
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             smallint,
    @CURYTYPEID           char(6),
    @CURYID               char(3),
    @TAXID02              UD_TAXID,
    @TAXID01              UD_TAXID,
    @DISCOUNT             numeric(12, 2)              = NULL,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @SLS_PRICE            numeric(8, 4),
    @QTY                  numeric(10, 3),
    @TOTALPEN             numeric(11, 4),
    @TOTALUSD             numeric(11, 4),
    @SUBTOTALPEN          numeric(11, 4),
    @SUBTOTALUSD          numeric(11, 4),
    @TAXPEN               numeric(11, 4),
    @TAXUSD               numeric(11, 4),
    @USERID               UD_USERID,
    @DOCDATE              smalldatetime,
    @INVTIDSOLOMON        char(20),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                    = NULL,
    @USER2                UD_USER2                    = NULL,
    @USER3                UD_USER6                    = NULL,
    @USER4                UD_USER7                    = NULL,
    @TSTAMP               timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DOCSUMMARYDET
       SET CURYTYPEID            = @CURYTYPEID,
           CURYID                = @CURYID,
           TAXID02               = @TAXID02,
           TAXID01               = @TAXID01,
           DISCOUNT              = @DISCOUNT,
           INVTIDSKU             = @INVTIDSKU,
           SLS_PRICE             = @SLS_PRICE,
           QTY                   = @QTY,
           TOTALPEN              = @TOTALPEN,
           TOTALUSD              = @TOTALUSD,
           SUBTOTALPEN           = @SUBTOTALPEN,
           SUBTOTALUSD           = @SUBTOTALUSD,
           TAXPEN                = @TAXPEN,
           TAXUSD                = @TAXUSD,
           USERID                = @USERID,
           DOCDATE               = @DOCDATE,
           INVTIDSOLOMON         = @INVTIDSOLOMON,
           STKNEW                = @STKNEW,
           STKMODIFY             = @STKMODIFY,
           STKDELETE             = @STKDELETE,
           STKSEND               = @STKSEND,
           STKRECEIVE            = @STKRECEIVE,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID           = @SITEID
       AND DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND NBRDOCSUMMARY    = @NBRDOCSUMMARY
       AND SEQUENCE         = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DOCSUMMARYDET: Cannot update  in OP_DOCSUMMARYDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DOCSUMMARYDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DOCSUMMARYDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DOCSUMMARYDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DOCSUMMARYDET 
 */

CREATE PROCEDURE SP_DelProcOP_DOCSUMMARYDET
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DOCSUMMARYDET
     WHERE SITEID           = @SITEID
       AND DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND NBRDOCSUMMARY    = @NBRDOCSUMMARY
       AND SEQUENCE         = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DOCSUMMARYDET: Cannot delete because foreign keys still exist in OP_DOCSUMMARYDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DOCSUMMARYDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DOCSUMMARYDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DOCSUMMARYDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DOCSUMMARYDET 
 */

CREATE PROCEDURE SP_SelProcOP_DOCSUMMARYDET
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             smallint)
AS
BEGIN
    SELECT SITEID,
           DOCSUMMARYTYPEID,
           NBRDOCSUMMARY,
           SEQUENCE,
           CURYTYPEID,
           CURYID,
           TAXID02,
           TAXID01,
           DISCOUNT,
           INVTIDSKU,
           SLS_PRICE,
           QTY,
           TOTALPEN,
           TOTALUSD,
           SUBTOTALPEN,
           SUBTOTALUSD,
           TAXPEN,
           TAXUSD,
           USERID,
           DOCDATE,
           INVTIDSOLOMON,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DOCSUMMARYDET
     WHERE SITEID           = @SITEID
       AND DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND NBRDOCSUMMARY    = @NBRDOCSUMMARY
       AND SEQUENCE         = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DOCSUMMARYDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DOCSUMMARYDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DOCSUMMARYDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_DOCSUMMARYTYPE 
 */

CREATE PROCEDURE SP_InsProcOP_DOCSUMMARYTYPE
(
    @DOCSUMMARYTYPEID        char(2),
    @SITEID                  UD_IDSITE,
    @DOCTYPEID               UD_DOCTYPEID,
    @DESCRDOCSUMMARYTYPE     UD_DESCRIPCION,
    @NBRDOCSUMMARY           numeric(10, 0),
    @USERID                  UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                  = NULL,
    @USER2                   UD_USER2                  = NULL,
    @USER3                   UD_USER6                  = NULL,
    @USER4                   UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DOCSUMMARYTYPE(DOCSUMMARYTYPEID,
                                  SITEID,
                                  DOCTYPEID,
                                  DESCRDOCSUMMARYTYPE,
                                  NBRDOCSUMMARY,
                                  USERID,
                                  STKNEW,
                                  STKMODIFY,
                                  STKDELETE,
                                  STKSEND,
                                  STKRECEIVE,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@DOCSUMMARYTYPEID,
           @SITEID,
           @DOCTYPEID,
           @DESCRDOCSUMMARYTYPE,
           @NBRDOCSUMMARY,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_DOCSUMMARYTYPE: Cannot insert because primary key value not found in OP_DOCSUMMARYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_DOCSUMMARYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_DOCSUMMARYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_DOCSUMMARYTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_DOCSUMMARYTYPE 
 */

CREATE PROCEDURE SP_UpdProcOP_DOCSUMMARYTYPE
(
    @DOCSUMMARYTYPEID        char(2),
    @SITEID                  UD_IDSITE,
    @DOCTYPEID               UD_DOCTYPEID,
    @DESCRDOCSUMMARYTYPE     UD_DESCRIPCION,
    @NBRDOCSUMMARY           numeric(10, 0),
    @USERID                  UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                  = NULL,
    @USER2                   UD_USER2                  = NULL,
    @USER3                   UD_USER6                  = NULL,
    @USER4                   UD_USER7                  = NULL,
    @TSTAMP                  timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DOCSUMMARYTYPE
       SET DOCTYPEID                = @DOCTYPEID,
           DESCRDOCSUMMARYTYPE      = @DESCRDOCSUMMARYTYPE,
           NBRDOCSUMMARY            = @NBRDOCSUMMARY,
           USERID                   = @USERID,
           STKNEW                   = @STKNEW,
           STKMODIFY                = @STKMODIFY,
           STKDELETE                = @STKDELETE,
           STKSEND                  = @STKSEND,
           STKRECEIVE               = @STKRECEIVE,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND SITEID           = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_DOCSUMMARYTYPE: Cannot update  in OP_DOCSUMMARYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_DOCSUMMARYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_DOCSUMMARYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_DOCSUMMARYTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_DOCSUMMARYTYPE 
 */

CREATE PROCEDURE SP_DelProcOP_DOCSUMMARYTYPE
(
    @DOCSUMMARYTYPEID        char(2),
    @SITEID                  UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DOCSUMMARYTYPE
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND SITEID           = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_DOCSUMMARYTYPE: Cannot delete because foreign keys still exist in OP_DOCSUMMARYTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_DOCSUMMARYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_DOCSUMMARYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_DOCSUMMARYTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_DOCSUMMARYTYPE 
 */

CREATE PROCEDURE SP_SelProcOP_DOCSUMMARYTYPE
(
    @DOCSUMMARYTYPEID        char(2),
    @SITEID                  UD_IDSITE)
AS
BEGIN
    SELECT DOCSUMMARYTYPEID,
           SITEID,
           DOCTYPEID,
           DESCRDOCSUMMARYTYPE,
           NBRDOCSUMMARY,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DOCSUMMARYTYPE
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID
       AND SITEID           = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_DOCSUMMARYTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_DOCSUMMARYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_DOCSUMMARYTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_HISTRANSFUEL 
 */

CREATE PROCEDURE SP_InsProcOP_HISTRANSFUEL
(
    @NUMERO        char(4),
    @SOLES         char(6),
    @PRODUCTO      char(2),
    @PRECIO        char(4),
    @GALONES       char(8),
    @CARA          char(2),
    @FECHA         char(6),
    @HORA          char(4),
    @ESTADO        char(10)                 = NULL,
    @DOCUMENTO     char(10)                 = NULL,
    @DATEPROCE     smalldatetime)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_HISTRANSFUEL(NUMERO,
                                SOLES,
                                PRODUCTO,
                                PRECIO,
                                GALONES,
                                CARA,
                                FECHA,
                                HORA,
                                ESTADO,
                                DOCUMENTO,
                                DATEPROCE)
    VALUES(@NUMERO,
           @SOLES,
           @PRODUCTO,
           @PRECIO,
           @GALONES,
           @CARA,
           @FECHA,
           @HORA,
           @ESTADO,
           @DOCUMENTO,
           @DATEPROCE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_HISTRANSFUEL: Cannot insert because primary key value not found in OP_HISTRANSFUEL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_HISTRANSFUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_HISTRANSFUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_HISTRANSFUEL >>>'
go





/* 
 * PROCEDURE: SP_InsProcOP_LOADSALESORD 
 */

CREATE PROCEDURE SP_InsProcOP_LOADSALESORD
(
    @NBRLOADSALE        numeric(10, 0),
    @CURYID             UD_CURYID,
    @CUSTIDSS           UD_CODCLIENT                = NULL,
    @TAXREGNBR          UD_CODCLIENT                = NULL,
    @CUSTNAME           UD_NOMBRE                   = NULL,
    @ADDR1              UD_DIRECCION                = NULL,
    @DOCTYPEID          UD_DOCTYPEID,
    @SALESPERID         UD_SALESPERSONID,
    @USERID             UD_USERID,
    @OPTRTOTPEN         numeric(11, 4),
    @OPTRTAXPEN         numeric(9, 2),
    @DOCDATE            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @CURYRATE           UD_CAMBIO,
    @STKCHECK           bit,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER6                    = NULL,
    @USER4              UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_LOADSALESORD(NBRLOADSALE,
                                CURYID,
                                CUSTIDSS,
                                TAXREGNBR,
                                CUSTNAME,
                                ADDR1,
                                DOCTYPEID,
                                SALESPERID,
                                USERID,
                                OPTRTOTPEN,
                                OPTRTAXPEN,
                                DOCDATE,
                                DATEPROCESALES,
                                CURYRATE,
                                STKCHECK,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@NBRLOADSALE,
           @CURYID,
           @CUSTIDSS,
           @TAXREGNBR,
           @CUSTNAME,
           @ADDR1,
           @DOCTYPEID,
           @SALESPERID,
           @USERID,
           @OPTRTOTPEN,
           @OPTRTAXPEN,
           @DOCDATE,
           @DATEPROCESALES,
           @CURYRATE,
           @STKCHECK,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_LOADSALESORD: Cannot insert because primary key value not found in OP_LOADSALESORD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_LOADSALESORD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_LOADSALESORD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_LOADSALESORD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_LOADSALESORD 
 */

CREATE PROCEDURE SP_UpdProcOP_LOADSALESORD
(
    @NBRLOADSALE        numeric(10, 0),
    @CURYID             UD_CURYID,
    @CUSTIDSS           UD_CODCLIENT                = NULL,
    @TAXREGNBR          UD_CODCLIENT                = NULL,
    @CUSTNAME           UD_NOMBRE                   = NULL,
    @ADDR1              UD_DIRECCION                = NULL,
    @DOCTYPEID          UD_DOCTYPEID,
    @SALESPERID         UD_SALESPERSONID,
    @USERID             UD_USERID,
    @OPTRTOTPEN         numeric(11, 4),
    @OPTRTAXPEN         numeric(9, 2),
    @DOCDATE            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @CURYRATE           UD_CAMBIO,
    @STKCHECK           bit,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER6                    = NULL,
    @USER4              UD_USER7                    = NULL,
    @TSTAMP             timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_LOADSALESORD
       SET CURYID              = @CURYID,
           CUSTIDSS            = @CUSTIDSS,
           TAXREGNBR           = @TAXREGNBR,
           CUSTNAME            = @CUSTNAME,
           ADDR1               = @ADDR1,
           DOCTYPEID           = @DOCTYPEID,
           SALESPERID          = @SALESPERID,
           USERID              = @USERID,
           OPTRTOTPEN          = @OPTRTOTPEN,
           OPTRTAXPEN          = @OPTRTAXPEN,
           DOCDATE             = @DOCDATE,
           DATEPROCESALES      = @DATEPROCESALES,
           CURYRATE            = @CURYRATE,
           STKCHECK            = @STKCHECK,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4
     WHERE NBRLOADSALE = @NBRLOADSALE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_LOADSALESORD: Cannot update  in OP_LOADSALESORD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_LOADSALESORD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_LOADSALESORD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_LOADSALESORD >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_LOADSALESORD 
 */

CREATE PROCEDURE SP_DelProcOP_LOADSALESORD
(
    @NBRLOADSALE        numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_LOADSALESORD
     WHERE NBRLOADSALE = @NBRLOADSALE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_LOADSALESORD: Cannot delete because foreign keys still exist in OP_LOADSALESORD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_LOADSALESORD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_LOADSALESORD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_LOADSALESORD >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_LOADSALESORD 
 */

CREATE PROCEDURE SP_SelProcOP_LOADSALESORD
(
    @NBRLOADSALE        numeric(10, 0))
AS
BEGIN
    SELECT NBRLOADSALE,
           CURYID,
           CUSTIDSS,
           TAXREGNBR,
           CUSTNAME,
           ADDR1,
           DOCTYPEID,
           SALESPERID,
           USERID,
           OPTRTOTPEN,
           OPTRTAXPEN,
           DOCDATE,
           DATEPROCESALES,
           CURYRATE,
           STKCHECK,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_LOADSALESORD
     WHERE NBRLOADSALE = @NBRLOADSALE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_LOADSALESORD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_LOADSALESORD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_LOADSALESORD >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_LOADSALESORDDET 
 */

CREATE PROCEDURE SP_InsProcOP_LOADSALESORDDET
(
    @NBRLOADSALE     numeric(10, 0),
    @SEQUENCE        smallint,
    @INVTIDSKU       UD_IDCORPORATIVO,
    @DESCR           UD_DESCRIPCION,
    @TAXIGV          numeric(11, 4),
    @SLS_PRICE       numeric(11, 4),
    @QTY             numeric(10, 3),
    @OPTRTOTDET      numeric(11, 4),
    @STDCOSTPEN      numeric(8, 4),
    @STRCOSTUSD      numeric(8, 4),
    @USER1           UD_USER1                    = NULL,
    @USER2           UD_USER2                    = NULL,
    @USER3           UD_USER6                    = NULL,
    @USER4           UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_LOADSALESORDDET(NBRLOADSALE,
                                   SEQUENCE,
                                   INVTIDSKU,
                                   DESCR,
                                   TAXIGV,
                                   SLS_PRICE,
                                   QTY,
                                   OPTRTOTDET,
                                   STDCOSTPEN,
                                   STRCOSTUSD,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@NBRLOADSALE,
           @SEQUENCE,
           @INVTIDSKU,
           @DESCR,
           @TAXIGV,
           @SLS_PRICE,
           @QTY,
           @OPTRTOTDET,
           @STDCOSTPEN,
           @STRCOSTUSD,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_LOADSALESORDDET: Cannot insert because primary key value not found in OP_LOADSALESORDDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_LOADSALESORDDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_LOADSALESORDDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_LOADSALESORDDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_LOADSALESORDDET 
 */

CREATE PROCEDURE SP_UpdProcOP_LOADSALESORDDET
(
    @NBRLOADSALE     numeric(10, 0),
    @SEQUENCE        smallint,
    @INVTIDSKU       UD_IDCORPORATIVO,
    @DESCR           UD_DESCRIPCION,
    @TAXIGV          numeric(11, 4),
    @SLS_PRICE       numeric(11, 4),
    @QTY             numeric(10, 3),
    @OPTRTOTDET      numeric(11, 4),
    @STDCOSTPEN      numeric(8, 4),
    @STRCOSTUSD      numeric(8, 4),
    @USER1           UD_USER1                    = NULL,
    @USER2           UD_USER2                    = NULL,
    @USER3           UD_USER6                    = NULL,
    @USER4           UD_USER7                    = NULL,
    @TSTAMP          timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_LOADSALESORDDET
       SET INVTIDSKU        = @INVTIDSKU,
           DESCR            = @DESCR,
           TAXIGV           = @TAXIGV,
           SLS_PRICE        = @SLS_PRICE,
           QTY              = @QTY,
           OPTRTOTDET       = @OPTRTOTDET,
           STDCOSTPEN       = @STDCOSTPEN,
           STRCOSTUSD       = @STRCOSTUSD,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE NBRLOADSALE = @NBRLOADSALE
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_LOADSALESORDDET: Cannot update  in OP_LOADSALESORDDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_LOADSALESORDDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_LOADSALESORDDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_LOADSALESORDDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_LOADSALESORDDET 
 */

CREATE PROCEDURE SP_DelProcOP_LOADSALESORDDET
(
    @NBRLOADSALE     numeric(10, 0),
    @SEQUENCE        smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_LOADSALESORDDET
     WHERE NBRLOADSALE = @NBRLOADSALE
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_LOADSALESORDDET: Cannot delete because foreign keys still exist in OP_LOADSALESORDDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_LOADSALESORDDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_LOADSALESORDDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_LOADSALESORDDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_LOADSALESORDDET 
 */

CREATE PROCEDURE SP_SelProcOP_LOADSALESORDDET
(
    @NBRLOADSALE     numeric(10, 0),
    @SEQUENCE        smallint)
AS
BEGIN
    SELECT NBRLOADSALE,
           SEQUENCE,
           INVTIDSKU,
           DESCR,
           TAXIGV,
           SLS_PRICE,
           QTY,
           OPTRTOTDET,
           STDCOSTPEN,
           STRCOSTUSD,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_LOADSALESORDDET
     WHERE NBRLOADSALE = @NBRLOADSALE
       AND SEQUENCE    = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_LOADSALESORDDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_LOADSALESORDDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_LOADSALESORDDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_MODSALES 
 */

CREATE PROCEDURE SP_InsProcOP_MODSALES
(
    @MODSALESID     char(2),
    @DESCR          UD_DESCRIPCION,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_MODSALES(MODSALESID,
                            DESCR,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@MODSALESID,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_MODSALES: Cannot insert because primary key value not found in OP_MODSALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_MODSALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_MODSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_MODSALES >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_MODSALES 
 */

CREATE PROCEDURE SP_UpdProcOP_MODSALES
(
    @MODSALESID     char(2),
    @DESCR          UD_DESCRIPCION,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_MODSALES
       SET DESCR           = @DESCR,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE MODSALESID = @MODSALESID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_MODSALES: Cannot update  in OP_MODSALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_MODSALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_MODSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_MODSALES >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_MODSALES 
 */

CREATE PROCEDURE SP_DelProcOP_MODSALES
(
    @MODSALESID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_MODSALES
     WHERE MODSALESID = @MODSALESID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_MODSALES: Cannot delete because foreign keys still exist in OP_MODSALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_MODSALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_MODSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_MODSALES >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_MODSALES 
 */

CREATE PROCEDURE SP_SelProcOP_MODSALES
(
    @MODSALESID     char(2))
AS
BEGIN
    SELECT MODSALESID,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_MODSALES
     WHERE MODSALESID = @MODSALESID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_MODSALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_MODSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_MODSALES >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_PRCLVLCUST 
 */

CREATE PROCEDURE SP_InsProcOP_PRCLVLCUST
(
    @PRCLVLCUSTID           UD_PRCLVLCUSTID,
    @SITEID                 char(5),
    @CURYID                 UD_CURYID,
    @DESCR                  UD_DESCRIPCION,
    @STARTDATEPRICE         smalldatetime,
    @ENDDATEPRICE           smalldatetime,
    @STATUS                 bit,
    @APPROVA1               bit,
    @USERIDAPPROVA1         UD_USERID,
    @DATETIMEAUTORIZED1     smalldatetime,
    @APPROVA2               bit,
    @USERIDAPPROVA2         UD_USERID                  = NULL,
    @DATETIMEAUTORIZED2     smalldatetime,
    @STKPRICESEND           bit,
    @SITEORIG               UD_IDSITE,
    @STKPRICESS             bit,
    @TYPEPRCLVLCUST         char(1)                    = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                   = NULL,
    @USER2                  UD_USER2                   = NULL,
    @USER3                  UD_USER6                   = NULL,
    @USER4                  UD_USER7                   = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PRCLVLCUST(PRCLVLCUSTID,
                              SITEID,
                              CURYID,
                              DESCR,
                              STARTDATEPRICE,
                              ENDDATEPRICE,
                              STATUS,
                              APPROVA1,
                              USERIDAPPROVA1,
                              DATETIMEAUTORIZED1,
                              APPROVA2,
                              USERIDAPPROVA2,
                              DATETIMEAUTORIZED2,
                              STKPRICESEND,
                              SITEORIG,
                              STKPRICESS,
                              TYPEPRCLVLCUST,
                              STKNEW,
                              STKMODIFY,
                              STKDELETE,
                              STKSEND,
                              STKRECEIVE,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@PRCLVLCUSTID,
           @SITEID,
           @CURYID,
           @DESCR,
           @STARTDATEPRICE,
           @ENDDATEPRICE,
           @STATUS,
           @APPROVA1,
           @USERIDAPPROVA1,
           @DATETIMEAUTORIZED1,
           @APPROVA2,
           @USERIDAPPROVA2,
           @DATETIMEAUTORIZED2,
           @STKPRICESEND,
           @SITEORIG,
           @STKPRICESS,
           @TYPEPRCLVLCUST,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_PRCLVLCUST: Cannot insert because primary key value not found in OP_PRCLVLCUST '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_PRCLVLCUST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_PRCLVLCUST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_PRCLVLCUST >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_PRCLVLCUST 
 */

CREATE PROCEDURE SP_UpdProcOP_PRCLVLCUST
(
    @PRCLVLCUSTID           UD_PRCLVLCUSTID,
    @SITEID                 char(5),
    @CURYID                 UD_CURYID,
    @DESCR                  UD_DESCRIPCION,
    @STARTDATEPRICE         smalldatetime,
    @ENDDATEPRICE           smalldatetime,
    @STATUS                 bit,
    @APPROVA1               bit,
    @USERIDAPPROVA1         UD_USERID,
    @DATETIMEAUTORIZED1     smalldatetime,
    @APPROVA2               bit,
    @USERIDAPPROVA2         UD_USERID                  = NULL,
    @DATETIMEAUTORIZED2     smalldatetime,
    @STKPRICESEND           bit,
    @SITEORIG               UD_IDSITE,
    @STKPRICESS             bit,
    @TYPEPRCLVLCUST         char(1)                    = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                   = NULL,
    @USER2                  UD_USER2                   = NULL,
    @USER3                  UD_USER6                   = NULL,
    @USER4                  UD_USER7                   = NULL,
    @TSTAMP                 timestamp                  = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PRCLVLCUST
       SET CURYID                  = @CURYID,
           DESCR                   = @DESCR,
           STARTDATEPRICE          = @STARTDATEPRICE,
           ENDDATEPRICE            = @ENDDATEPRICE,
           STATUS                  = @STATUS,
           APPROVA1                = @APPROVA1,
           USERIDAPPROVA1          = @USERIDAPPROVA1,
           DATETIMEAUTORIZED1      = @DATETIMEAUTORIZED1,
           APPROVA2                = @APPROVA2,
           USERIDAPPROVA2          = @USERIDAPPROVA2,
           DATETIMEAUTORIZED2      = @DATETIMEAUTORIZED2,
           STKPRICESEND            = @STKPRICESEND,
           SITEORIG                = @SITEORIG,
           STKPRICESS              = @STKPRICESS,
           TYPEPRCLVLCUST          = @TYPEPRCLVLCUST,
           STKNEW                  = @STKNEW,
           STKMODIFY               = @STKMODIFY,
           STKDELETE               = @STKDELETE,
           STKSEND                 = @STKSEND,
           STKRECEIVE              = @STKRECEIVE,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SITEID       = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_PRCLVLCUST: Cannot update  in OP_PRCLVLCUST '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_PRCLVLCUST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_PRCLVLCUST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_PRCLVLCUST >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_PRCLVLCUST 
 */

CREATE PROCEDURE SP_DelProcOP_PRCLVLCUST
(
    @PRCLVLCUSTID           UD_PRCLVLCUSTID,
    @SITEID                 char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PRCLVLCUST
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SITEID       = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_PRCLVLCUST: Cannot delete because foreign keys still exist in OP_PRCLVLCUST '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_PRCLVLCUST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_PRCLVLCUST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_PRCLVLCUST >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_PRCLVLCUST 
 */

CREATE PROCEDURE SP_SelProcOP_PRCLVLCUST
(
    @PRCLVLCUSTID           UD_PRCLVLCUSTID,
    @SITEID                 char(5))
AS
BEGIN
    SELECT PRCLVLCUSTID,
           SITEID,
           CURYID,
           DESCR,
           STARTDATEPRICE,
           ENDDATEPRICE,
           STATUS,
           APPROVA1,
           USERIDAPPROVA1,
           DATETIMEAUTORIZED1,
           APPROVA2,
           USERIDAPPROVA2,
           DATETIMEAUTORIZED2,
           STKPRICESEND,
           SITEORIG,
           STKPRICESS,
           TYPEPRCLVLCUST,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PRCLVLCUST
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SITEID       = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_PRCLVLCUST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_PRCLVLCUST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_PRCLVLCUST >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE SP_InsProcOP_PRCLVLCUSTDET
(
    @PRCLVLCUSTID      UD_PRCLVLCUSTID,
    @SITEID            char(5),
    @INVTIDSKU         UD_IDCORPORATIVO,
    @DISCMTO           UD_MONTO,
    @SLS_PRICE         numeric(8, 4),
    @SLS_PRICECUST     UD_PRECIO,
    @DESCR             UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER6                    = NULL,
    @USER4             UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PRCLVLCUSTDET(PRCLVLCUSTID,
                                 SITEID,
                                 INVTIDSKU,
                                 DISCMTO,
                                 SLS_PRICE,
                                 SLS_PRICECUST,
                                 DESCR,
                                 STKNEW,
                                 STKMODIFY,
                                 STKDELETE,
                                 STKSEND,
                                 STKRECEIVE,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@PRCLVLCUSTID,
           @SITEID,
           @INVTIDSKU,
           @DISCMTO,
           @SLS_PRICE,
           @SLS_PRICECUST,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_PRCLVLCUSTDET: Cannot insert because primary key value not found in OP_PRCLVLCUSTDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_PRCLVLCUSTDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE SP_UpdProcOP_PRCLVLCUSTDET
(
    @PRCLVLCUSTID      UD_PRCLVLCUSTID,
    @SITEID            char(5),
    @INVTIDSKU         UD_IDCORPORATIVO,
    @DISCMTO           UD_MONTO,
    @SLS_PRICE         numeric(8, 4),
    @SLS_PRICECUST     UD_PRECIO,
    @DESCR             UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER6                    = NULL,
    @USER4             UD_USER7                    = NULL,
    @TSTAMP            timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PRCLVLCUSTDET
       SET DISCMTO            = @DISCMTO,
           SLS_PRICE          = @SLS_PRICE,
           SLS_PRICECUST      = @SLS_PRICECUST,
           DESCR              = @DESCR,
           STKNEW             = @STKNEW,
           STKMODIFY          = @STKMODIFY,
           STKDELETE          = @STKDELETE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SITEID       = @SITEID
       AND INVTIDSKU    = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_PRCLVLCUSTDET: Cannot update  in OP_PRCLVLCUSTDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_PRCLVLCUSTDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE SP_DelProcOP_PRCLVLCUSTDET
(
    @PRCLVLCUSTID      UD_PRCLVLCUSTID,
    @SITEID            char(5),
    @INVTIDSKU         UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PRCLVLCUSTDET
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SITEID       = @SITEID
       AND INVTIDSKU    = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_PRCLVLCUSTDET: Cannot delete because foreign keys still exist in OP_PRCLVLCUSTDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_PRCLVLCUSTDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE SP_SelProcOP_PRCLVLCUSTDET
(
    @PRCLVLCUSTID      UD_PRCLVLCUSTID,
    @SITEID            char(5),
    @INVTIDSKU         UD_IDCORPORATIVO)
AS
BEGIN
    SELECT PRCLVLCUSTID,
           SITEID,
           INVTIDSKU,
           DISCMTO,
           SLS_PRICE,
           SLS_PRICECUST,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PRCLVLCUSTDET
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SITEID       = @SITEID
       AND INVTIDSKU    = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_PRCLVLCUSTDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_PRINTERTYPE 
 */

CREATE PROCEDURE SP_InsProcOP_PRINTERTYPE
(
    @PRINTERTYPEID     char(2),
    @DESCRPRINTER      UD_DESCRIPCION,
    @JUMPLINES         tinyint                   = NULL,
    @IDCUTPAPER        varchar(70)               = NULL,
    @WIDELINES         tinyint                   = NULL,
    @IDOPENDRAWER      varchar(70)               = NULL,
    @USERID            UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PRINTERTYPE(PRINTERTYPEID,
                               DESCRPRINTER,
                               JUMPLINES,
                               IDCUTPAPER,
                               WIDELINES,
                               IDOPENDRAWER,
                               USERID,
                               STKNEW,
                               STKMODIFY,
                               STKDELETE,
                               STKSEND,
                               STKRECEIVE,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@PRINTERTYPEID,
           @DESCRPRINTER,
           @JUMPLINES,
           @IDCUTPAPER,
           @WIDELINES,
           @IDOPENDRAWER,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_PRINTERTYPE: Cannot insert because primary key value not found in OP_PRINTERTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_PRINTERTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_PRINTERTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_PRINTERTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_PRINTERTYPE 
 */

CREATE PROCEDURE SP_UpdProcOP_PRINTERTYPE
(
    @PRINTERTYPEID     char(2),
    @DESCRPRINTER      UD_DESCRIPCION,
    @JUMPLINES         tinyint                   = NULL,
    @IDCUTPAPER        varchar(70)               = NULL,
    @WIDELINES         tinyint                   = NULL,
    @IDOPENDRAWER      varchar(70)               = NULL,
    @USERID            UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL,
    @TSTAMP            timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PRINTERTYPE
       SET DESCRPRINTER       = @DESCRPRINTER,
           JUMPLINES          = @JUMPLINES,
           IDCUTPAPER         = @IDCUTPAPER,
           WIDELINES          = @WIDELINES,
           IDOPENDRAWER       = @IDOPENDRAWER,
           USERID             = @USERID,
           STKNEW             = @STKNEW,
           STKMODIFY          = @STKMODIFY,
           STKDELETE          = @STKDELETE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE PRINTERTYPEID = @PRINTERTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_PRINTERTYPE: Cannot update  in OP_PRINTERTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_PRINTERTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_PRINTERTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_PRINTERTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_PRINTERTYPE 
 */

CREATE PROCEDURE SP_DelProcOP_PRINTERTYPE
(
    @PRINTERTYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PRINTERTYPE
     WHERE PRINTERTYPEID = @PRINTERTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_PRINTERTYPE: Cannot delete because foreign keys still exist in OP_PRINTERTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_PRINTERTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_PRINTERTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_PRINTERTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_PRINTERTYPE 
 */

CREATE PROCEDURE SP_SelProcOP_PRINTERTYPE
(
    @PRINTERTYPEID     char(2))
AS
BEGIN
    SELECT PRINTERTYPEID,
           DESCRPRINTER,
           JUMPLINES,
           IDCUTPAPER,
           WIDELINES,
           IDOPENDRAWER,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PRINTERTYPE
     WHERE PRINTERTYPEID = @PRINTERTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_PRINTERTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_PRINTERTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_PRINTERTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_PROMOTIONCARD 
 */

CREATE PROCEDURE SP_InsProcOP_PROMOTIONCARD
(
    @PROMOTIONCARDID       char(20),
    @NAMECLIENT            UD_NOMBRE            = NULL,
    @PASSWORDMAGNETIC      char(30)             = NULL,
    @LASTNAME              UD_NOMBRE            = NULL,
    @PROMOTIONCARDTYPE     char(2),
    @USERID                UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1             = NULL,
    @USER2                 UD_USER2             = NULL,
    @USER3                 UD_USER6             = NULL,
    @USER4                 UD_USER7             = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PROMOTIONCARD(PROMOTIONCARDID,
                                 NAMECLIENT,
                                 PASSWORDMAGNETIC,
                                 LASTNAME,
                                 PROMOTIONCARDTYPE,
                                 USERID,
                                 STKNEW,
                                 STKMODIFY,
                                 STKDELETE,
                                 STKSEND,
                                 STKRECEIVE,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@PROMOTIONCARDID,
           @NAMECLIENT,
           @PASSWORDMAGNETIC,
           @LASTNAME,
           @PROMOTIONCARDTYPE,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_PROMOTIONCARD: Cannot insert because primary key value not found in OP_PROMOTIONCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_PROMOTIONCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_PROMOTIONCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_PROMOTIONCARD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_PROMOTIONCARD 
 */

CREATE PROCEDURE SP_UpdProcOP_PROMOTIONCARD
(
    @PROMOTIONCARDID       char(20),
    @NAMECLIENT            UD_NOMBRE            = NULL,
    @PASSWORDMAGNETIC      char(30)             = NULL,
    @LASTNAME              UD_NOMBRE            = NULL,
    @PROMOTIONCARDTYPE     char(2),
    @USERID                UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1             = NULL,
    @USER2                 UD_USER2             = NULL,
    @USER3                 UD_USER6             = NULL,
    @USER4                 UD_USER7             = NULL,
    @TSTAMP                timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PROMOTIONCARD
       SET NAMECLIENT             = @NAMECLIENT,
           PASSWORDMAGNETIC       = @PASSWORDMAGNETIC,
           LASTNAME               = @LASTNAME,
           PROMOTIONCARDTYPE      = @PROMOTIONCARDTYPE,
           USERID                 = @USERID,
           STKNEW                 = @STKNEW,
           STKMODIFY              = @STKMODIFY,
           STKDELETE              = @STKDELETE,
           STKSEND                = @STKSEND,
           STKRECEIVE             = @STKRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE PROMOTIONCARDID = @PROMOTIONCARDID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_PROMOTIONCARD: Cannot update  in OP_PROMOTIONCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_PROMOTIONCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_PROMOTIONCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_PROMOTIONCARD >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_PROMOTIONCARD 
 */

CREATE PROCEDURE SP_DelProcOP_PROMOTIONCARD
(
    @PROMOTIONCARDID       char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PROMOTIONCARD
     WHERE PROMOTIONCARDID = @PROMOTIONCARDID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_PROMOTIONCARD: Cannot delete because foreign keys still exist in OP_PROMOTIONCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_PROMOTIONCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_PROMOTIONCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_PROMOTIONCARD >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_PROMOTIONCARD 
 */

CREATE PROCEDURE SP_SelProcOP_PROMOTIONCARD
(
    @PROMOTIONCARDID       char(20))
AS
BEGIN
    SELECT PROMOTIONCARDID,
           NAMECLIENT,
           PASSWORDMAGNETIC,
           LASTNAME,
           PROMOTIONCARDTYPE,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PROMOTIONCARD
     WHERE PROMOTIONCARDID = @PROMOTIONCARDID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_PROMOTIONCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_PROMOTIONCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_PROMOTIONCARD >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_PROMOTIONCARDTYPE 
 */

CREATE PROCEDURE SP_InsProcOP_PROMOTIONCARDTYPE
(
    @PROMOTIONCARDTYPE      char(2),
    @DESCRPROMOTIONCARD     char(10)             = NULL,
    @PROMOTIONCARDID        char(20),
    @USERID                 UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1             = NULL,
    @USER2                  UD_USER2             = NULL,
    @USER3                  UD_USER6             = NULL,
    @USER4                  UD_USER7             = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PROMOTIONCARDTYPE(PROMOTIONCARDTYPE,
                                     DESCRPROMOTIONCARD,
                                     PROMOTIONCARDID,
                                     USERID,
                                     STKNEW,
                                     STKMODIFY,
                                     STKDELETE,
                                     STKSEND,
                                     STKRECEIVE,
                                     USER1,
                                     USER2,
                                     USER3,
                                     USER4 
                                     )
    VALUES(@PROMOTIONCARDTYPE,
           @DESCRPROMOTIONCARD,
           @PROMOTIONCARDID,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_PROMOTIONCARDTYPE: Cannot insert because primary key value not found in OP_PROMOTIONCARDTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_PROMOTIONCARDTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_PROMOTIONCARDTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_PROMOTIONCARDTYPE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_PROMOTIONCARDTYPE 
 */

CREATE PROCEDURE SP_UpdProcOP_PROMOTIONCARDTYPE
(
    @PROMOTIONCARDTYPE      char(2),
    @DESCRPROMOTIONCARD     char(10)             = NULL,
    @PROMOTIONCARDID        char(20),
    @USERID                 UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1             = NULL,
    @USER2                  UD_USER2             = NULL,
    @USER3                  UD_USER6             = NULL,
    @USER4                  UD_USER7             = NULL,
    @TSTAMP                 timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PROMOTIONCARDTYPE
       SET DESCRPROMOTIONCARD      = @DESCRPROMOTIONCARD,
           PROMOTIONCARDID         = @PROMOTIONCARDID,
           USERID                  = @USERID,
           STKNEW                  = @STKNEW,
           STKMODIFY               = @STKMODIFY,
           STKDELETE               = @STKDELETE,
           STKSEND                 = @STKSEND,
           STKRECEIVE              = @STKRECEIVE,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE PROMOTIONCARDTYPE = @PROMOTIONCARDTYPE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_PROMOTIONCARDTYPE: Cannot update  in OP_PROMOTIONCARDTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_PROMOTIONCARDTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_PROMOTIONCARDTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_PROMOTIONCARDTYPE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_PROMOTIONCARDTYPE 
 */

CREATE PROCEDURE SP_DelProcOP_PROMOTIONCARDTYPE
(
    @PROMOTIONCARDTYPE      char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PROMOTIONCARDTYPE
     WHERE PROMOTIONCARDTYPE = @PROMOTIONCARDTYPE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_PROMOTIONCARDTYPE: Cannot delete because foreign keys still exist in OP_PROMOTIONCARDTYPE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_PROMOTIONCARDTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_PROMOTIONCARDTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_PROMOTIONCARDTYPE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_PROMOTIONCARDTYPE 
 */

CREATE PROCEDURE SP_SelProcOP_PROMOTIONCARDTYPE
(
    @PROMOTIONCARDTYPE      char(2))
AS
BEGIN
    SELECT PROMOTIONCARDTYPE,
           DESCRPROMOTIONCARD,
           PROMOTIONCARDID,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PROMOTIONCARDTYPE
     WHERE PROMOTIONCARDTYPE = @PROMOTIONCARDTYPE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_PROMOTIONCARDTYPE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_PROMOTIONCARDTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_PROMOTIONCARDTYPE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_RESERVATIONCASH 
 */

CREATE PROCEDURE SP_InsProcOP_RESERVATIONCASH
(
    @DATEPROCE      smalldatetime,
    @SALESPOINT     char(10),
    @SALESPERID     char(20),
    @DATEDOC        smalldatetime,
    @STKCLSEDX      bit,
    @CURYID         char(3),
    @TOTALPEN       numeric(11, 4),
    @TOTALUSD       numeric(11, 4),
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_RESERVATIONCASH(DATEPROCE,
                                   SALESPOINT,
                                   SALESPERID,
                                   DATEDOC,
                                   STKCLSEDX,
                                   CURYID,
                                   TOTALPEN,
                                   TOTALUSD,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@DATEPROCE,
           @SALESPOINT,
           @SALESPERID,
           @DATEDOC,
           @STKCLSEDX,
           @CURYID,
           @TOTALPEN,
           @TOTALUSD,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_RESERVATIONCASH: Cannot insert because primary key value not found in OP_RESERVATIONCASH '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_RESERVATIONCASH') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_RESERVATIONCASH >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_RESERVATIONCASH >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_RESERVATIONCASH 
 */

CREATE PROCEDURE SP_UpdProcOP_RESERVATIONCASH
(
    @DATEPROCE      smalldatetime,
    @SALESPOINT     char(10),
    @SALESPERID     char(20),
    @DATEDOC        smalldatetime,
    @STKCLSEDX      bit,
    @CURYID         char(3),
    @TOTALPEN       numeric(11, 4),
    @TOTALUSD       numeric(11, 4),
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_RESERVATIONCASH
       SET STKCLSEDX       = @STKCLSEDX,
           CURYID          = @CURYID,
           TOTALPEN        = @TOTALPEN,
           TOTALUSD        = @TOTALUSD,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE DATEPROCE  = @DATEPROCE
       AND SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID
       AND DATEDOC    = @DATEDOC

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_RESERVATIONCASH: Cannot update  in OP_RESERVATIONCASH '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_RESERVATIONCASH') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_RESERVATIONCASH >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_RESERVATIONCASH >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_RESERVATIONCASH 
 */

CREATE PROCEDURE SP_DelProcOP_RESERVATIONCASH
(
    @DATEPROCE      smalldatetime,
    @SALESPOINT     char(10),
    @SALESPERID     char(20),
    @DATEDOC        smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_RESERVATIONCASH
     WHERE DATEPROCE  = @DATEPROCE
       AND SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID
       AND DATEDOC    = @DATEDOC

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_RESERVATIONCASH: Cannot delete because foreign keys still exist in OP_RESERVATIONCASH '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_RESERVATIONCASH') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_RESERVATIONCASH >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_RESERVATIONCASH >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_RESERVATIONCASH 
 */

CREATE PROCEDURE SP_SelProcOP_RESERVATIONCASH
(
    @DATEPROCE      smalldatetime,
    @SALESPOINT     char(10),
    @SALESPERID     char(20),
    @DATEDOC        smalldatetime)
AS
BEGIN
    SELECT DATEPROCE,
           SALESPOINT,
           SALESPERID,
           DATEDOC,
           STKCLSEDX,
           CURYID,
           TOTALPEN,
           TOTALUSD,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_RESERVATIONCASH
     WHERE DATEPROCE  = @DATEPROCE
       AND SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID
       AND DATEDOC    = @DATEDOC

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_RESERVATIONCASH') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_RESERVATIONCASH >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_RESERVATIONCASH >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SALES 
 */

CREATE PROCEDURE SP_InsProcOP_SALES
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           UD_CORRELATIVOS,
    @SITEID                char(5),
    @TYPEPAYMENTID         char(2),
    @STKINVENTORY          bit,
    @CURYID                UD_CURYID,
    @DOCSTATUSID           char(2),
    @TERMID                char(2),
    @SALESPERID            UD_SALESPERSONID,
    @USERID                UD_USERID,
    @DATEDOC               UD_FECHAHORA,
    @DATEPROCESALES        UD_FECHA,
    @PERPOST               UD_PERPOST,
    @TAXIGVID              char(2)                     = NULL,
    @TAXISCID              char(2)                     = NULL,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TAXISCPEN             numeric(11, 4),
    @TAXISCUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PORCENTDISCOUNT1      numeric(5, 2),
    @PORCENTDISCOUNT2      numeric(5, 2),
    @TOTDISCOUNTPEN        numeric(11, 4),
    @TOTDISCOUNTUSD        numeric(11, 4),
    @TOTRETURNEDPEN        numeric(11, 4),
    @TOTRETURNEDUSD        numeric(11, 4),
    @TOTCASHPEN            numeric(11, 4),
    @TOTCASHUSD            numeric(11, 4),
    @CUSTIDSS              UD_CODCLIENT                = NULL,
    @TAXREGNBR             UD_CODCLIENT                = NULL,
    @CUSTNAME              UD_NOMBRE                   = NULL,
    @PLACA                 UD_PLACA,
    @NBRBONUS              numeric(10, 0)              = NULL,
    @CURYTYPEID            UD_CURYTYPEID,
    @CURYRATE              UD_CAMBIO,
    @STKCLOSEDZ            bit,
    @STKCLOSEDX            bit,
    @PROMOTIONCARDID       char(20)                    = NULL,
    @POINTNUMBER           smallint,
    @SALESPOINT            char(10),
    @KILOMETRAJE           int                         = NULL,
    @BUSINESSTYPE          char(1),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                    = NULL,
    @USER2                 UD_USER2                    = NULL,
    @USER3                 UD_USER3                    = NULL,
    @USER4                 UD_USER5                    = NULL,
    @USER5                 UD_USER5                    = NULL,
    @USER6                 UD_USER6                    = NULL,
    @USER7                 UD_USER7                    = NULL,
    @USER8                 UD_USER8                    = NULL,
    @USER9                 UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALES(DOCTYPEID,
                         NBRDOCUMENT,
                         SITEID,
                         TYPEPAYMENTID,
                         STKINVENTORY,
                         CURYID,
                         DOCSTATUSID,
                         TERMID,
                         SALESPERID,
                         USERID,
                         DATEDOC,
                         DATEPROCESALES,
                         PERPOST,
                         TAXIGVID,
                         TAXISCID,
                         TOTALPEN,
                         TOTALUSD,
                         SUBTOTALPEN,
                         SUBTOTALUSD,
                         TAXIGVPEN,
                         TAXIGVUSD,
                         TAXISCPEN,
                         TAXISCUSD,
                         TOTALNOTAFFECTPEN,
                         TOTALNOTAFFECTUSD,
                         PORCENTDISCOUNT1,
                         PORCENTDISCOUNT2,
                         TOTDISCOUNTPEN,
                         TOTDISCOUNTUSD,
                         TOTRETURNEDPEN,
                         TOTRETURNEDUSD,
                         TOTCASHPEN,
                         TOTCASHUSD,
                         CUSTIDSS,
                         TAXREGNBR,
                         CUSTNAME,
                         PLACA,
                         NBRBONUS,
                         CURYTYPEID,
                         CURYRATE,
                         STKCLOSEDZ,
                         STKCLOSEDX,
                         PROMOTIONCARDID,
                         POINTNUMBER,
                         SALESPOINT,
                         KILOMETRAJE,
                         BUSINESSTYPE,
                         STKSEND,
                         STKRECEIVE,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SITEID,
           @TYPEPAYMENTID,
           @STKINVENTORY,
           @CURYID,
           @DOCSTATUSID,
           @TERMID,
           @SALESPERID,
           @USERID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @TAXIGVID,
           @TAXISCID,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAXIGVPEN,
           @TAXIGVUSD,
           @TAXISCPEN,
           @TAXISCUSD,
           @TOTALNOTAFFECTPEN,
           @TOTALNOTAFFECTUSD,
           @PORCENTDISCOUNT1,
           @PORCENTDISCOUNT2,
           @TOTDISCOUNTPEN,
           @TOTDISCOUNTUSD,
           @TOTRETURNEDPEN,
           @TOTRETURNEDUSD,
           @TOTCASHPEN,
           @TOTCASHUSD,
           @CUSTIDSS,
           @TAXREGNBR,
           @CUSTNAME,
           @PLACA,
           @NBRBONUS,
           @CURYTYPEID,
           @CURYRATE,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @PROMOTIONCARDID,
           @POINTNUMBER,
           @SALESPOINT,
           @KILOMETRAJE,
           @BUSINESSTYPE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SALES: Cannot insert because primary key value not found in OP_SALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SALES >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SALES 
 */

CREATE PROCEDURE SP_UpdProcOP_SALES
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           UD_CORRELATIVOS,
    @SITEID                char(5),
    @TYPEPAYMENTID         char(2),
    @STKINVENTORY          bit,
    @CURYID                UD_CURYID,
    @DOCSTATUSID           char(2),
    @TERMID                char(2),
    @SALESPERID            UD_SALESPERSONID,
    @USERID                UD_USERID,
    @DATEDOC               UD_FECHAHORA,
    @DATEPROCESALES        UD_FECHA,
    @PERPOST               UD_PERPOST,
    @TAXIGVID              char(2)                     = NULL,
    @TAXISCID              char(2)                     = NULL,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TAXISCPEN             numeric(11, 4),
    @TAXISCUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PORCENTDISCOUNT1      numeric(5, 2),
    @PORCENTDISCOUNT2      numeric(5, 2),
    @TOTDISCOUNTPEN        numeric(11, 4),
    @TOTDISCOUNTUSD        numeric(11, 4),
    @TOTRETURNEDPEN        numeric(11, 4),
    @TOTRETURNEDUSD        numeric(11, 4),
    @TOTCASHPEN            numeric(11, 4),
    @TOTCASHUSD            numeric(11, 4),
    @CUSTIDSS              UD_CODCLIENT                = NULL,
    @TAXREGNBR             UD_CODCLIENT                = NULL,
    @CUSTNAME              UD_NOMBRE                   = NULL,
    @PLACA                 UD_PLACA,
    @NBRBONUS              numeric(10, 0)              = NULL,
    @CURYTYPEID            UD_CURYTYPEID,
    @CURYRATE              UD_CAMBIO,
    @STKCLOSEDZ            bit,
    @STKCLOSEDX            bit,
    @PROMOTIONCARDID       char(20)                    = NULL,
    @POINTNUMBER           smallint,
    @SALESPOINT            char(10),
    @KILOMETRAJE           int                         = NULL,
    @BUSINESSTYPE          char(1),
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                    = NULL,
    @USER2                 UD_USER2                    = NULL,
    @USER3                 UD_USER3                    = NULL,
    @USER4                 UD_USER5                    = NULL,
    @USER5                 UD_USER5                    = NULL,
    @USER6                 UD_USER6                    = NULL,
    @USER7                 UD_USER7                    = NULL,
    @USER8                 UD_USER8                    = NULL,
    @USER9                 UD_USER9                    = NULL,
    @TSTAMP                timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALES
       SET TYPEPAYMENTID          = @TYPEPAYMENTID,
           STKINVENTORY           = @STKINVENTORY,
           CURYID                 = @CURYID,
           DOCSTATUSID            = @DOCSTATUSID,
           TERMID                 = @TERMID,
           SALESPERID             = @SALESPERID,
           USERID                 = @USERID,
           DATEDOC                = @DATEDOC,
           DATEPROCESALES         = @DATEPROCESALES,
           PERPOST                = @PERPOST,
           TAXIGVID               = @TAXIGVID,
           TAXISCID               = @TAXISCID,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           SUBTOTALPEN            = @SUBTOTALPEN,
           SUBTOTALUSD            = @SUBTOTALUSD,
           TAXIGVPEN              = @TAXIGVPEN,
           TAXIGVUSD              = @TAXIGVUSD,
           TAXISCPEN              = @TAXISCPEN,
           TAXISCUSD              = @TAXISCUSD,
           TOTALNOTAFFECTPEN      = @TOTALNOTAFFECTPEN,
           TOTALNOTAFFECTUSD      = @TOTALNOTAFFECTUSD,
           PORCENTDISCOUNT1       = @PORCENTDISCOUNT1,
           PORCENTDISCOUNT2       = @PORCENTDISCOUNT2,
           TOTDISCOUNTPEN         = @TOTDISCOUNTPEN,
           TOTDISCOUNTUSD         = @TOTDISCOUNTUSD,
           TOTRETURNEDPEN         = @TOTRETURNEDPEN,
           TOTRETURNEDUSD         = @TOTRETURNEDUSD,
           TOTCASHPEN             = @TOTCASHPEN,
           TOTCASHUSD             = @TOTCASHUSD,
           CUSTIDSS               = @CUSTIDSS,
           TAXREGNBR              = @TAXREGNBR,
           CUSTNAME               = @CUSTNAME,
           PLACA                  = @PLACA,
           NBRBONUS               = @NBRBONUS,
           CURYTYPEID             = @CURYTYPEID,
           CURYRATE               = @CURYRATE,
           STKCLOSEDZ             = @STKCLOSEDZ,
           STKCLOSEDX             = @STKCLOSEDX,
           PROMOTIONCARDID        = @PROMOTIONCARDID,
           POINTNUMBER            = @POINTNUMBER,
           SALESPOINT             = @SALESPOINT,
           KILOMETRAJE            = @KILOMETRAJE,
           BUSINESSTYPE           = @BUSINESSTYPE,
           STKSEND                = @STKSEND,
           STKRECEIVE             = @STKRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4,
           USER5                  = @USER5,
           USER6                  = @USER6,
           USER7                  = @USER7,
           USER8                  = @USER8,
           USER9                  = @USER9
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SALES: Cannot update  in OP_SALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SALES >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SALES 
 */

CREATE PROCEDURE SP_DelProcOP_SALES
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           UD_CORRELATIVOS,
    @SITEID                char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALES
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SALES: Cannot delete because foreign keys still exist in OP_SALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SALES >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SALES 
 */

CREATE PROCEDURE SP_SelProcOP_SALES
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           UD_CORRELATIVOS,
    @SITEID                char(5))
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SITEID,
           TYPEPAYMENTID,
           STKINVENTORY,
           CURYID,
           DOCSTATUSID,
           TERMID,
           SALESPERID,
           USERID,
           DATEDOC,
           DATEPROCESALES,
           PERPOST,
           TAXIGVID,
           TAXISCID,
           TOTALPEN,
           TOTALUSD,
           SUBTOTALPEN,
           SUBTOTALUSD,
           TAXIGVPEN,
           TAXIGVUSD,
           TAXISCPEN,
           TAXISCUSD,
           TOTALNOTAFFECTPEN,
           TOTALNOTAFFECTUSD,
           PORCENTDISCOUNT1,
           PORCENTDISCOUNT2,
           TOTDISCOUNTPEN,
           TOTDISCOUNTUSD,
           TOTRETURNEDPEN,
           TOTRETURNEDUSD,
           TOTCASHPEN,
           TOTCASHUSD,
           CUSTIDSS,
           TAXREGNBR,
           CUSTNAME,
           PLACA,
           NBRBONUS,
           CURYTYPEID,
           CURYRATE,
           STKCLOSEDZ,
           STKCLOSEDX,
           PROMOTIONCARDID,
           POINTNUMBER,
           SALESPOINT,
           KILOMETRAJE,
           BUSINESSTYPE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM OP_SALES
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SALES >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SALESCARD 
 */

CREATE PROCEDURE SP_InsProcOP_SALESCARD
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @SITEID             char(5),
    @SEQUENCE           smallint,
    @CARDID             char(2),
    @NBRCARD            char(20),
    @TOTCARDPEN         numeric(11, 4),
    @TOTCARDUSD         UD_MONTO,
    @CURYID             UD_CURYID,
    @DATEPROCESALES     smalldatetime,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01             UD_USER1                   = NULL,
    @USER02             UD_USER2                   = NULL,
    @USER03             UD_USER6                   = NULL,
    @USER04             UD_USER7                   = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESCARD(DOCTYPEID,
                             NBRDOCUMENT,
                             SITEID,
                             SEQUENCE,
                             CARDID,
                             NBRCARD,
                             TOTCARDPEN,
                             TOTCARDUSD,
                             CURYID,
                             DATEPROCESALES,
                             STKSEND,
                             STKRECEIVE,
                             USER01,
                             USER02,
                             USER03,
                             USER04 
                             )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SITEID,
           @SEQUENCE,
           @CARDID,
           @NBRCARD,
           @TOTCARDPEN,
           @TOTCARDUSD,
           @CURYID,
           @DATEPROCESALES,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SALESCARD: Cannot insert because primary key value not found in OP_SALESCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SALESCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SALESCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SALESCARD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SALESCARD 
 */

CREATE PROCEDURE SP_UpdProcOP_SALESCARD
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @SITEID             char(5),
    @SEQUENCE           smallint,
    @CARDID             char(2),
    @NBRCARD            char(20),
    @TOTCARDPEN         numeric(11, 4),
    @TOTCARDUSD         UD_MONTO,
    @CURYID             UD_CURYID,
    @DATEPROCESALES     smalldatetime,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01             UD_USER1                   = NULL,
    @USER02             UD_USER2                   = NULL,
    @USER03             UD_USER6                   = NULL,
    @USER04             UD_USER7                   = NULL,
    @TSTAMP             timestamp                  = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALESCARD
       SET CARDID              = @CARDID,
           NBRCARD             = @NBRCARD,
           TOTCARDPEN          = @TOTCARDPEN,
           TOTCARDUSD          = @TOTCARDUSD,
           CURYID              = @CURYID,
           DATEPROCESALES      = @DATEPROCESALES,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE,
           USER01              = @USER01,
           USER02              = @USER02,
           USER03              = @USER03,
           USER04              = @USER04
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SALESCARD: Cannot update  in OP_SALESCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SALESCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SALESCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SALESCARD >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SALESCARD 
 */

CREATE PROCEDURE SP_DelProcOP_SALESCARD
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @SITEID             char(5),
    @SEQUENCE           smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESCARD
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SALESCARD: Cannot delete because foreign keys still exist in OP_SALESCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SALESCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SALESCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SALESCARD >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SALESCARD 
 */

CREATE PROCEDURE SP_SelProcOP_SALESCARD
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @SITEID             char(5),
    @SEQUENCE           smallint)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SITEID,
           SEQUENCE,
           CARDID,
           NBRCARD,
           TOTCARDPEN,
           TOTCARDUSD,
           CURYID,
           DATEPROCESALES,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_SALESCARD
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID
       AND SEQUENCE    = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SALESCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SALESCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SALESCARD >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SALESDET 
 */

CREATE PROCEDURE SP_InsProcOP_SALESDET
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             char(5),
    @SEQUENCE           smallint,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DOCSTATUSID        char(2),
    @DATEDOC            UD_FECHAHORA,
    @DATEPROCESALES     smalldatetime,
    @PERPOST            UD_PERPOST,
    @NBRTURN            tinyint,
    @NBRSIDE            UD_CARA                     = NULL,
    @NBRTRANSACFUEL     char(4)                     = NULL,
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           UD_MONTO,
    @TOTALUSD           UD_MONTO,
    @TAXPEN             UD_MONTO,
    @TAXUSD             UD_MONTO,
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       UD_PRECIO,
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                UD_STOCK,
    @USERID             UD_USERID,
    @KIT                bit,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER3                    = NULL,
    @USER4              UD_USER5                    = NULL,
    @USER5              UD_USER5                    = NULL,
    @USER6              UD_USER6                    = NULL,
    @USER7              UD_USER7                    = NULL,
    @USER8              UD_USER8                    = NULL,
    @USER9              UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESDET(DOCTYPEID,
                            NBRDOCUMENT,
                            SITEID,
                            SEQUENCE,
                            INVTIDSKU,
                            CURYID,
                            DOCSTATUSID,
                            DATEDOC,
                            DATEPROCESALES,
                            PERPOST,
                            NBRTURN,
                            NBRSIDE,
                            NBRTRANSACFUEL,
                            PORCENTTAXIGV,
                            PORCENTTAXISC,
                            TOTALPEN,
                            TOTALUSD,
                            TAXPEN,
                            TAXUSD,
                            STKITEM,
                            STKFISI,
                            SLS_PRICE,
                            SLSPRICESALE,
                            STDCOSTPEN,
                            STDCOSTUSD,
                            INVTIDALTER,
                            DESCRINVENTORY,
                            QTY,
                            USERID,
                            KIT,
                            STKSEND,
                            STKRECEIVE,
                            USER1,
                            USER2,
                            USER3,
                            USER4,
                            USER5,
                            USER6,
                            USER7,
                            USER8,
                            USER9 
                            )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SITEID,
           @SEQUENCE,
           @INVTIDSKU,
           @CURYID,
           @DOCSTATUSID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @NBRTURN,
           @NBRSIDE,
           @NBRTRANSACFUEL,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @USERID,
           @KIT,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SALESDET: Cannot insert because primary key value not found in OP_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SALESDET 
 */

CREATE PROCEDURE SP_UpdProcOP_SALESDET
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             char(5),
    @SEQUENCE           smallint,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DOCSTATUSID        char(2),
    @DATEDOC            UD_FECHAHORA,
    @DATEPROCESALES     smalldatetime,
    @PERPOST            UD_PERPOST,
    @NBRTURN            tinyint,
    @NBRSIDE            UD_CARA                     = NULL,
    @NBRTRANSACFUEL     char(4)                     = NULL,
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           UD_MONTO,
    @TOTALUSD           UD_MONTO,
    @TAXPEN             UD_MONTO,
    @TAXUSD             UD_MONTO,
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       UD_PRECIO,
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                UD_STOCK,
    @USERID             UD_USERID,
    @KIT                bit,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER3                    = NULL,
    @USER4              UD_USER5                    = NULL,
    @USER5              UD_USER5                    = NULL,
    @USER6              UD_USER6                    = NULL,
    @USER7              UD_USER7                    = NULL,
    @USER8              UD_USER8                    = NULL,
    @USER9              UD_USER9                    = NULL,
    @TSTAMP             timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALESDET
       SET INVTIDSKU           = @INVTIDSKU,
           CURYID              = @CURYID,
           DOCSTATUSID         = @DOCSTATUSID,
           DATEDOC             = @DATEDOC,
           DATEPROCESALES      = @DATEPROCESALES,
           PERPOST             = @PERPOST,
           NBRTURN             = @NBRTURN,
           NBRSIDE             = @NBRSIDE,
           NBRTRANSACFUEL      = @NBRTRANSACFUEL,
           PORCENTTAXIGV       = @PORCENTTAXIGV,
           PORCENTTAXISC       = @PORCENTTAXISC,
           TOTALPEN            = @TOTALPEN,
           TOTALUSD            = @TOTALUSD,
           TAXPEN              = @TAXPEN,
           TAXUSD              = @TAXUSD,
           STKITEM             = @STKITEM,
           STKFISI             = @STKFISI,
           SLS_PRICE           = @SLS_PRICE,
           SLSPRICESALE        = @SLSPRICESALE,
           STDCOSTPEN          = @STDCOSTPEN,
           STDCOSTUSD          = @STDCOSTUSD,
           INVTIDALTER         = @INVTIDALTER,
           DESCRINVENTORY      = @DESCRINVENTORY,
           QTY                 = @QTY,
           USERID              = @USERID,
           KIT                 = @KIT,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4,
           USER5               = @USER5,
           USER6               = @USER6,
           USER7               = @USER7,
           USER8               = @USER8,
           USER9               = @USER9
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SALESDET: Cannot update  in OP_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SALESDET 
 */

CREATE PROCEDURE SP_DelProcOP_SALESDET
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             char(5),
    @SEQUENCE           smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SALESDET: Cannot delete because foreign keys still exist in OP_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SALESDET 
 */

CREATE PROCEDURE SP_SelProcOP_SALESDET
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             char(5),
    @SEQUENCE           smallint)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SITEID,
           SEQUENCE,
           INVTIDSKU,
           CURYID,
           DOCSTATUSID,
           DATEDOC,
           DATEPROCESALES,
           PERPOST,
           NBRTURN,
           NBRSIDE,
           NBRTRANSACFUEL,
           PORCENTTAXIGV,
           PORCENTTAXISC,
           TOTALPEN,
           TOTALUSD,
           TAXPEN,
           TAXUSD,
           STKITEM,
           STKFISI,
           SLS_PRICE,
           SLSPRICESALE,
           STDCOSTPEN,
           STDCOSTUSD,
           INVTIDALTER,
           DESCRINVENTORY,
           QTY,
           USERID,
           KIT,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM OP_SALESDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SITEID      = @SITEID
       AND SEQUENCE    = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SALESDETTMP 
 */

CREATE PROCEDURE SP_InsProcOP_SALESDETTMP
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             UD_IDSITE,
    @SEQUENCE           smallint,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DOCSTATUSID        char(2),
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERPOST            UD_PERPOST,
    @NBRTURN            tinyint,
    @NBRSIDE            UD_CARA                     = NULL,
    @NBRTRANSACFUEL     char(4)                     = NULL,
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(11, 4),
    @TOTALUSD           numeric(11, 4),
    @TAXPEN             numeric(11, 4),
    @TAXUSD             numeric(11, 4),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                numeric(10, 3),
    @USERID             UD_USERID,
    @KIT                bit,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER3                    = NULL,
    @USER4              UD_USER5                    = NULL,
    @USER5              UD_USER5                    = NULL,
    @USER6              UD_USER6                    = NULL,
    @USER7              UD_USER7                    = NULL,
    @USER8              UD_USER8                    = NULL,
    @USER9              UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESDETTMP(DOCTYPEID,
                               NBRDOCUMENT,
                               SITEID,
                               SEQUENCE,
                               INVTIDSKU,
                               CURYID,
                               DOCSTATUSID,
                               DATEDOC,
                               DATEPROCESALES,
                               PERPOST,
                               NBRTURN,
                               NBRSIDE,
                               NBRTRANSACFUEL,
                               PORCENTTAXIGV,
                               PORCENTTAXISC,
                               TOTALPEN,
                               TOTALUSD,
                               TAXPEN,
                               TAXUSD,
                               STKITEM,
                               STKFISI,
                               SLS_PRICE,
                               SLSPRICESALE,
                               STDCOSTPEN,
                               STDCOSTUSD,
                               INVTIDALTER,
                               DESCRINVENTORY,
                               QTY,
                               USERID,
                               KIT,
                               USER1,
                               USER2,
                               USER3,
                               USER4,
                               USER5,
                               USER6,
                               USER7,
                               USER8,
                               USER9 
                               )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SITEID,
           @SEQUENCE,
           @INVTIDSKU,
           @CURYID,
           @DOCSTATUSID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @NBRTURN,
           @NBRSIDE,
           @NBRTRANSACFUEL,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @USERID,
           @KIT,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SALESDETTMP: Cannot insert because primary key value not found in OP_SALESDETTMP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SALESDETTMP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SALESDETTMP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SALESDETTMP >>>'
go





/* 
 * PROCEDURE: SP_InsProcOP_SALESPERSON 
 */

CREATE PROCEDURE SP_InsProcOP_SALESPERSON
(
    @SALESPERID         UD_SALESPERSONID,
    @SALESPERNAME       UD_NOMBRE                   = NULL,
    @IDENTITYDOC        char(10),
    @PHONE              char(30)                    = NULL,
    @SEX                char(1),
    @INITIALDATE        UD_FECHA,
    @SITEID             UD_IDSITE,
    @ADDRESS1           UD_DIRECCION,
    @ADDRESS2           UD_DIRECCION                = NULL,
    @PASSWORD           char(12),
    @BIRTHDATE          smalldatetime,
    @BIRTHADDRESS1      UD_DIRECCION                = NULL,
    @BIRTHADDRESS2      UD_DIRECCION                = NULL,
    @STATUSPERSONID     char(2),
    @USERID             UD_USERID,
    @ACCESSUSERID       UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01             UD_USER1                    = NULL,
    @USER02             UD_USER2                    = NULL,
    @USER03             UD_USER6                    = NULL,
    @USER04             UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESPERSON(SALESPERID,
                               SALESPERNAME,
                               IDENTITYDOC,
                               PHONE,
                               SEX,
                               INITIALDATE,
                               SITEID,
                               ADDRESS1,
                               ADDRESS2,
                               PASSWORD,
                               BIRTHDATE,
                               BIRTHADDRESS1,
                               BIRTHADDRESS2,
                               STATUSPERSONID,
                               USERID,
                               ACCESSUSERID,
                               STKNEW,
                               STKMODIFY,
                               STKDELETE,
                               STKSEND,
                               STKRECEIVE,
                               USER01,
                               USER02,
                               USER03,
                               USER04 
                               )
    VALUES(@SALESPERID,
           @SALESPERNAME,
           @IDENTITYDOC,
           @PHONE,
           @SEX,
           @INITIALDATE,
           @SITEID,
           @ADDRESS1,
           @ADDRESS2,
           @PASSWORD,
           @BIRTHDATE,
           @BIRTHADDRESS1,
           @BIRTHADDRESS2,
           @STATUSPERSONID,
           @USERID,
           @ACCESSUSERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SALESPERSON: Cannot insert because primary key value not found in OP_SALESPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SALESPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SALESPERSON >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SALESPERSON 
 */

CREATE PROCEDURE SP_UpdProcOP_SALESPERSON
(
    @SALESPERID         UD_SALESPERSONID,
    @SALESPERNAME       UD_NOMBRE                   = NULL,
    @IDENTITYDOC        char(10),
    @PHONE              char(30)                    = NULL,
    @SEX                char(1),
    @INITIALDATE        UD_FECHA,
    @SITEID             UD_IDSITE,
    @ADDRESS1           UD_DIRECCION,
    @ADDRESS2           UD_DIRECCION                = NULL,
    @PASSWORD           char(12),
    @BIRTHDATE          smalldatetime,
    @BIRTHADDRESS1      UD_DIRECCION                = NULL,
    @BIRTHADDRESS2      UD_DIRECCION                = NULL,
    @STATUSPERSONID     char(2),
    @USERID             UD_USERID,
    @ACCESSUSERID       UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01             UD_USER1                    = NULL,
    @USER02             UD_USER2                    = NULL,
    @USER03             UD_USER6                    = NULL,
    @USER04             UD_USER7                    = NULL,
    @TSTAMP             timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALESPERSON
       SET SALESPERNAME        = @SALESPERNAME,
           IDENTITYDOC         = @IDENTITYDOC,
           PHONE               = @PHONE,
           SEX                 = @SEX,
           INITIALDATE         = @INITIALDATE,
           SITEID              = @SITEID,
           ADDRESS1            = @ADDRESS1,
           ADDRESS2            = @ADDRESS2,
           PASSWORD            = @PASSWORD,
           BIRTHDATE           = @BIRTHDATE,
           BIRTHADDRESS1       = @BIRTHADDRESS1,
           BIRTHADDRESS2       = @BIRTHADDRESS2,
           STATUSPERSONID      = @STATUSPERSONID,
           USERID              = @USERID,
           ACCESSUSERID        = @ACCESSUSERID,
           STKNEW              = @STKNEW,
           STKMODIFY           = @STKMODIFY,
           STKDELETE           = @STKDELETE,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE,
           USER01              = @USER01,
           USER02              = @USER02,
           USER03              = @USER03,
           USER04              = @USER04
     WHERE SALESPERID = @SALESPERID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SALESPERSON: Cannot update  in OP_SALESPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SALESPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SALESPERSON >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SALESPERSON 
 */

CREATE PROCEDURE SP_DelProcOP_SALESPERSON
(
    @SALESPERID         UD_SALESPERSONID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESPERSON
     WHERE SALESPERID = @SALESPERID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SALESPERSON: Cannot delete because foreign keys still exist in OP_SALESPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SALESPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SALESPERSON >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SALESPERSON 
 */

CREATE PROCEDURE SP_SelProcOP_SALESPERSON
(
    @SALESPERID         UD_SALESPERSONID)
AS
BEGIN
    SELECT SALESPERID,
           SALESPERNAME,
           IDENTITYDOC,
           PHONE,
           SEX,
           INITIALDATE,
           SITEID,
           ADDRESS1,
           ADDRESS2,
           PASSWORD,
           BIRTHDATE,
           BIRTHADDRESS1,
           BIRTHADDRESS2,
           STATUSPERSONID,
           USERID,
           ACCESSUSERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_SALESPERSON
     WHERE SALESPERID = @SALESPERID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SALESPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SALESPERSON >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SALESPOINTSETTING 
 */

CREATE PROCEDURE SP_InsProcOP_SALESPOINTSETTING
(
    @SALESPOINT           UD_PTOVTA,
    @TERMINALNAME         varchar(25),
    @ENDDATEACT           smalldatetime,
    @SERIALNUMBER         char(16),
    @STKTICKFAC           bit,
    @STKTICKBOL           bit,
    @CURYID               UD_CURYID,
    @STKLOADSALE          bit,
    @NBRLOADSALE          numeric(10, 0),
    @STKPRINTERFAC        bit,
    @STKPRINTEDBOL        bit,
    @TYPEDISPFAC          varchar(1),
    @DISPFAC              UD_DISP_SALIDA            = NULL,
    @TYPEDISPBOL          varchar(1),
    @DISPBOL              UD_DISP_SALIDA            = NULL,
    @TYPEDISPNC           varchar(1),
    @DISPNC               UD_DISP_SALIDA            = NULL,
    @TYPEDISPND           varchar(1),
    @DISPND               UD_DISP_SALIDA            = NULL,
    @TYPEDISORDER         varchar(1),
    @DISPORDER            UD_DISP_SALIDA,
    @TYPEDISPTICKFAC      varchar(1),
    @DISPTICKFAC          UD_DISP_SALIDA            = NULL,
    @NBRTICKFAC           numeric(10, 0),
    @TYPEDISPTICKBOL      varchar(1),
    @DISPTICKBOL          UD_DISP_SALIDA            = NULL,
    @NBRTICKBOL           numeric(10, 0),
    @DISPDISPLAY          UD_DISP_SALIDA            = NULL,
    @STKSALESPOINTFAC     bit,
    @STKZETAOK            bit,
    @STKTURNOK            bit,
    @STKJUMPAUTOMAT       bit,
    @QTYJUMPAUTOMAT       tinyint,
    @STKHOTKEYS           bit,
    @STKDELETEDOC         bit,
    @BUSINESSTYPE         char(1),
    @SITEID               UD_IDSITE,
    @BATNBRSALES          numeric(10, 0),
    @STKTOFORCEZ          bit,
    @PRINTERTYPEID        char(2)                   = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL,
    @USER3                UD_USER3                  = NULL,
    @USER4                UD_USER4                  = NULL,
    @USER5                UD_USER5                  = NULL,
    @USER6                UD_USER6                  = NULL,
    @USER7                UD_USER7                  = NULL,
    @USER8                UD_USER8                  = NULL,
    @USER9                UD_USER9                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESPOINTSETTING(SALESPOINT,
                                     TERMINALNAME,
                                     ENDDATEACT,
                                     SERIALNUMBER,
                                     STKTICKFAC,
                                     STKTICKBOL,
                                     CURYID,
                                     STKLOADSALE,
                                     NBRLOADSALE,
                                     STKPRINTERFAC,
                                     STKPRINTEDBOL,
                                     TYPEDISPFAC,
                                     DISPFAC,
                                     TYPEDISPBOL,
                                     DISPBOL,
                                     TYPEDISPNC,
                                     DISPNC,
                                     TYPEDISPND,
                                     DISPND,
                                     TYPEDISORDER,
                                     DISPORDER,
                                     TYPEDISPTICKFAC,
                                     DISPTICKFAC,
                                     NBRTICKFAC,
                                     TYPEDISPTICKBOL,
                                     DISPTICKBOL,
                                     NBRTICKBOL,
                                     DISPDISPLAY,
                                     STKSALESPOINTFAC,
                                     STKZETAOK,
                                     STKTURNOK,
                                     STKJUMPAUTOMAT,
                                     QTYJUMPAUTOMAT,
                                     STKHOTKEYS,
                                     STKDELETEDOC,
                                     BUSINESSTYPE,
                                     SITEID,
                                     BATNBRSALES,
                                     STKTOFORCEZ,
                                     PRINTERTYPEID,
                                     STKNEW,
                                     STKMODIFY,
                                     STKDELETE,
                                     STKSEND,
                                     STKRECEIVE,
                                     USER1,
                                     USER2,
                                     USER3,
                                     USER4,
                                     USER5,
                                     USER6,
                                     USER7,
                                     USER8,
                                     USER9 
                                     )
    VALUES(@SALESPOINT,
           @TERMINALNAME,
           @ENDDATEACT,
           @SERIALNUMBER,
           @STKTICKFAC,
           @STKTICKBOL,
           @CURYID,
           @STKLOADSALE,
           @NBRLOADSALE,
           @STKPRINTERFAC,
           @STKPRINTEDBOL,
           @TYPEDISPFAC,
           @DISPFAC,
           @TYPEDISPBOL,
           @DISPBOL,
           @TYPEDISPNC,
           @DISPNC,
           @TYPEDISPND,
           @DISPND,
           @TYPEDISORDER,
           @DISPORDER,
           @TYPEDISPTICKFAC,
           @DISPTICKFAC,
           @NBRTICKFAC,
           @TYPEDISPTICKBOL,
           @DISPTICKBOL,
           @NBRTICKBOL,
           @DISPDISPLAY,
           @STKSALESPOINTFAC,
           @STKZETAOK,
           @STKTURNOK,
           @STKJUMPAUTOMAT,
           @QTYJUMPAUTOMAT,
           @STKHOTKEYS,
           @STKDELETEDOC,
           @BUSINESSTYPE,
           @SITEID,
           @BATNBRSALES,
           @STKTOFORCEZ,
           @PRINTERTYPEID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SALESPOINTSETTING: Cannot insert because primary key value not found in OP_SALESPOINTSETTING '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SALESPOINTSETTING') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SALESPOINTSETTING >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SALESPOINTSETTING >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SALESPOINTSETTING 
 */

CREATE PROCEDURE SP_UpdProcOP_SALESPOINTSETTING
(
    @SALESPOINT           UD_PTOVTA,
    @TERMINALNAME         varchar(25),
    @ENDDATEACT           smalldatetime,
    @SERIALNUMBER         char(16),
    @STKTICKFAC           bit,
    @STKTICKBOL           bit,
    @CURYID               UD_CURYID,
    @STKLOADSALE          bit,
    @NBRLOADSALE          numeric(10, 0),
    @STKPRINTERFAC        bit,
    @STKPRINTEDBOL        bit,
    @TYPEDISPFAC          varchar(1),
    @DISPFAC              UD_DISP_SALIDA            = NULL,
    @TYPEDISPBOL          varchar(1),
    @DISPBOL              UD_DISP_SALIDA            = NULL,
    @TYPEDISPNC           varchar(1),
    @DISPNC               UD_DISP_SALIDA            = NULL,
    @TYPEDISPND           varchar(1),
    @DISPND               UD_DISP_SALIDA            = NULL,
    @TYPEDISORDER         varchar(1),
    @DISPORDER            UD_DISP_SALIDA,
    @TYPEDISPTICKFAC      varchar(1),
    @DISPTICKFAC          UD_DISP_SALIDA            = NULL,
    @NBRTICKFAC           numeric(10, 0),
    @TYPEDISPTICKBOL      varchar(1),
    @DISPTICKBOL          UD_DISP_SALIDA            = NULL,
    @NBRTICKBOL           numeric(10, 0),
    @DISPDISPLAY          UD_DISP_SALIDA            = NULL,
    @STKSALESPOINTFAC     bit,
    @STKZETAOK            bit,
    @STKTURNOK            bit,
    @STKJUMPAUTOMAT       bit,
    @QTYJUMPAUTOMAT       tinyint,
    @STKHOTKEYS           bit,
    @STKDELETEDOC         bit,
    @BUSINESSTYPE         char(1),
    @SITEID               UD_IDSITE,
    @BATNBRSALES          numeric(10, 0),
    @STKTOFORCEZ          bit,
    @PRINTERTYPEID        char(2)                   = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL,
    @USER3                UD_USER3                  = NULL,
    @USER4                UD_USER4                  = NULL,
    @USER5                UD_USER5                  = NULL,
    @USER6                UD_USER6                  = NULL,
    @USER7                UD_USER7                  = NULL,
    @USER8                UD_USER8                  = NULL,
    @USER9                UD_USER9                  = NULL,
    @TSTAMP               timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALESPOINTSETTING
       SET TERMINALNAME          = @TERMINALNAME,
           ENDDATEACT            = @ENDDATEACT,
           SERIALNUMBER          = @SERIALNUMBER,
           STKTICKFAC            = @STKTICKFAC,
           STKTICKBOL            = @STKTICKBOL,
           CURYID                = @CURYID,
           STKLOADSALE           = @STKLOADSALE,
           NBRLOADSALE           = @NBRLOADSALE,
           STKPRINTERFAC         = @STKPRINTERFAC,
           STKPRINTEDBOL         = @STKPRINTEDBOL,
           TYPEDISPFAC           = @TYPEDISPFAC,
           DISPFAC               = @DISPFAC,
           TYPEDISPBOL           = @TYPEDISPBOL,
           DISPBOL               = @DISPBOL,
           TYPEDISPNC            = @TYPEDISPNC,
           DISPNC                = @DISPNC,
           TYPEDISPND            = @TYPEDISPND,
           DISPND                = @DISPND,
           TYPEDISORDER          = @TYPEDISORDER,
           DISPORDER             = @DISPORDER,
           TYPEDISPTICKFAC       = @TYPEDISPTICKFAC,
           DISPTICKFAC           = @DISPTICKFAC,
           NBRTICKFAC            = @NBRTICKFAC,
           TYPEDISPTICKBOL       = @TYPEDISPTICKBOL,
           DISPTICKBOL           = @DISPTICKBOL,
           NBRTICKBOL            = @NBRTICKBOL,
           DISPDISPLAY           = @DISPDISPLAY,
           STKSALESPOINTFAC      = @STKSALESPOINTFAC,
           STKZETAOK             = @STKZETAOK,
           STKTURNOK             = @STKTURNOK,
           STKJUMPAUTOMAT        = @STKJUMPAUTOMAT,
           QTYJUMPAUTOMAT        = @QTYJUMPAUTOMAT,
           STKHOTKEYS            = @STKHOTKEYS,
           STKDELETEDOC          = @STKDELETEDOC,
           BUSINESSTYPE          = @BUSINESSTYPE,
           SITEID                = @SITEID,
           BATNBRSALES           = @BATNBRSALES,
           STKTOFORCEZ           = @STKTOFORCEZ,
           PRINTERTYPEID         = @PRINTERTYPEID,
           STKNEW                = @STKNEW,
           STKMODIFY             = @STKMODIFY,
           STKDELETE             = @STKDELETE,
           STKSEND               = @STKSEND,
           STKRECEIVE            = @STKRECEIVE,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4,
           USER5                 = @USER5,
           USER6                 = @USER6,
           USER7                 = @USER7,
           USER8                 = @USER8,
           USER9                 = @USER9
     WHERE SALESPOINT = @SALESPOINT

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SALESPOINTSETTING: Cannot update  in OP_SALESPOINTSETTING '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SALESPOINTSETTING') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SALESPOINTSETTING >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SALESPOINTSETTING >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SALESPOINTSETTING 
 */

CREATE PROCEDURE SP_DelProcOP_SALESPOINTSETTING
(
    @SALESPOINT           UD_PTOVTA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESPOINTSETTING
     WHERE SALESPOINT = @SALESPOINT

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SALESPOINTSETTING: Cannot delete because foreign keys still exist in OP_SALESPOINTSETTING '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SALESPOINTSETTING') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SALESPOINTSETTING >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SALESPOINTSETTING >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SALESPOINTSETTING 
 */

CREATE PROCEDURE SP_SelProcOP_SALESPOINTSETTING
(
    @SALESPOINT           UD_PTOVTA)
AS
BEGIN
    SELECT SALESPOINT,
           TERMINALNAME,
           ENDDATEACT,
           SERIALNUMBER,
           STKTICKFAC,
           STKTICKBOL,
           CURYID,
           STKLOADSALE,
           NBRLOADSALE,
           STKPRINTERFAC,
           STKPRINTEDBOL,
           TYPEDISPFAC,
           DISPFAC,
           TYPEDISPBOL,
           DISPBOL,
           TYPEDISPNC,
           DISPNC,
           TYPEDISPND,
           DISPND,
           TYPEDISORDER,
           DISPORDER,
           TYPEDISPTICKFAC,
           DISPTICKFAC,
           NBRTICKFAC,
           TYPEDISPTICKBOL,
           DISPTICKBOL,
           NBRTICKBOL,
           DISPDISPLAY,
           STKSALESPOINTFAC,
           STKZETAOK,
           STKTURNOK,
           STKJUMPAUTOMAT,
           QTYJUMPAUTOMAT,
           STKHOTKEYS,
           STKDELETEDOC,
           BUSINESSTYPE,
           SITEID,
           BATNBRSALES,
           STKTOFORCEZ,
           PRINTERTYPEID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM OP_SALESPOINTSETTING
     WHERE SALESPOINT = @SALESPOINT

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SALESPOINTSETTING') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SALESPOINTSETTING >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SALESPOINTSETTING >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SALESPOINTVSSIDE 
 */

CREATE PROCEDURE SP_InsProcOP_SALESPOINTVSSIDE
(
    @SIDE           UD_CARA,
    @SALESPOINT     UD_PTOVTA)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESPOINTVSSIDE(SIDE,
                                    SALESPOINT)
    VALUES(@SIDE,
           @SALESPOINT)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SALESPOINTVSSIDE: Cannot insert because primary key value not found in OP_SALESPOINTVSSIDE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SALESPOINTVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SALESPOINTVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SALESPOINTVSSIDE >>>'
go



/* 
 * PROCEDURE: SP_DelProcOP_SALESPOINTVSSIDE 
 */

CREATE PROCEDURE SP_DelProcOP_SALESPOINTVSSIDE
(
    @SIDE           UD_CARA,
    @SALESPOINT     UD_PTOVTA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESPOINTVSSIDE
     WHERE SIDE       = @SIDE
       AND SALESPOINT = @SALESPOINT

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SALESPOINTVSSIDE: Cannot delete because foreign keys still exist in OP_SALESPOINTVSSIDE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SALESPOINTVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SALESPOINTVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SALESPOINTVSSIDE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SALESPOINTVSSIDE 
 */

CREATE PROCEDURE SP_SelProcOP_SALESPOINTVSSIDE
(
    @SIDE           UD_CARA,
    @SALESPOINT     UD_PTOVTA)
AS
BEGIN
    SELECT SIDE,
           SALESPOINT
      FROM OP_SALESPOINTVSSIDE
     WHERE SIDE       = @SIDE
       AND SALESPOINT = @SALESPOINT

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SALESPOINTVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SALESPOINTVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SALESPOINTVSSIDE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SALESTMP 
 */

CREATE PROCEDURE SP_InsProcOP_SALESTMP
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @SITEID                UD_IDSITE,
    @TYPEPAYMENTID         char(2),
    @STKINVENTORY          bit,
    @CURYID                UD_CURYID,
    @DOCSTATUSID           char(2),
    @TERMID                char(2),
    @SALESPERID            UD_SALESPERSONID,
    @USERID                UD_USERID,
    @DATEDOC               smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @PERPOST               UD_PERPOST,
    @TAXIGVID              char(2)                     = NULL,
    @TAXISCID              char(2)                     = NULL,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TAXISCPEN             numeric(11, 4),
    @TAXISCUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PORCENTDISCOUNT1      numeric(5, 2),
    @PORCENTDISCOUNT2      numeric(5, 2),
    @TOTDISCOUNTPEN        numeric(11, 4),
    @TOTDISCOUNTUSD        numeric(11, 4),
    @TOTRETURNEDPEN        numeric(11, 4),
    @TOTRETURNEDUSD        numeric(11, 4),
    @TOTCASHPEN            numeric(11, 4),
    @TOTCASHUSD            numeric(11, 4),
    @CUSTIDSS              UD_CODCLIENT                = NULL,
    @TAXREGNBR             UD_CODCLIENT                = NULL,
    @CUSTNAME              UD_NOMBRE                   = NULL,
    @PLACA                 UD_PLACA,
    @NBRBONUS              numeric(10, 0)              = NULL,
    @CURYTYPEID            UD_CURYTYPEID,
    @CURYRATE              UD_CAMBIO,
    @STKCLOSEDZ            bit,
    @STKCLOSEDX            bit,
    @PROMOTIONCARDID       char(20)                    = NULL,
    @POINTNUMBER           smallint,
    @SALESPOINT            UD_PTOVTA,
    @KILOMETRAJE           int                         = NULL,
    @USER1                 UD_USER1                    = NULL,
    @USER2                 UD_USER2                    = NULL,
    @USER3                 UD_USER3                    = NULL,
    @USER4                 UD_USER5                    = NULL,
    @USER5                 UD_USER5                    = NULL,
    @USER6                 UD_USER6                    = NULL,
    @USER7                 UD_USER7                    = NULL,
    @USER8                 UD_USER8                    = NULL,
    @USER9                 UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESTMP(DOCTYPEID,
                            NBRDOCUMENT,
                            SITEID,
                            TYPEPAYMENTID,
                            STKINVENTORY,
                            CURYID,
                            DOCSTATUSID,
                            TERMID,
                            SALESPERID,
                            USERID,
                            DATEDOC,
                            DATEPROCESALES,
                            PERPOST,
                            TAXIGVID,
                            TAXISCID,
                            TOTALPEN,
                            TOTALUSD,
                            SUBTOTALPEN,
                            SUBTOTALUSD,
                            TAXIGVPEN,
                            TAXIGVUSD,
                            TAXISCPEN,
                            TAXISCUSD,
                            TOTALNOTAFFECTPEN,
                            TOTALNOTAFFECTUSD,
                            PORCENTDISCOUNT1,
                            PORCENTDISCOUNT2,
                            TOTDISCOUNTPEN,
                            TOTDISCOUNTUSD,
                            TOTRETURNEDPEN,
                            TOTRETURNEDUSD,
                            TOTCASHPEN,
                            TOTCASHUSD,
                            CUSTIDSS,
                            TAXREGNBR,
                            CUSTNAME,
                            PLACA,
                            NBRBONUS,
                            CURYTYPEID,
                            CURYRATE,
                            STKCLOSEDZ,
                            STKCLOSEDX,
                            PROMOTIONCARDID,
                            POINTNUMBER,
                            SALESPOINT,
                            KILOMETRAJE,
                            USER1,
                            USER2,
                            USER3,
                            USER4,
                            USER5,
                            USER6,
                            USER7,
                            USER8,
                            USER9 
                            )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SITEID,
           @TYPEPAYMENTID,
           @STKINVENTORY,
           @CURYID,
           @DOCSTATUSID,
           @TERMID,
           @SALESPERID,
           @USERID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @TAXIGVID,
           @TAXISCID,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAXIGVPEN,
           @TAXIGVUSD,
           @TAXISCPEN,
           @TAXISCUSD,
           @TOTALNOTAFFECTPEN,
           @TOTALNOTAFFECTUSD,
           @PORCENTDISCOUNT1,
           @PORCENTDISCOUNT2,
           @TOTDISCOUNTPEN,
           @TOTDISCOUNTUSD,
           @TOTRETURNEDPEN,
           @TOTRETURNEDUSD,
           @TOTCASHPEN,
           @TOTCASHUSD,
           @CUSTIDSS,
           @TAXREGNBR,
           @CUSTNAME,
           @PLACA,
           @NBRBONUS,
           @CURYTYPEID,
           @CURYRATE,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @PROMOTIONCARDID,
           @POINTNUMBER,
           @SALESPOINT,
           @KILOMETRAJE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SALESTMP: Cannot insert because primary key value not found in OP_SALESTMP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SALESTMP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SALESTMP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SALESTMP >>>'
go





/* 
 * PROCEDURE: SP_InsProcOP_SETUP 
 */

CREATE PROCEDURE SP_InsProcOP_SETUP
(
    @AUTOMATICSALES            bit,
    @CATEGORYFUEL              UD_CATEGORY,
    @CATEGORYLUBES             UD_CATEGORY,
    @BADGE                     bit,
    @MTOMAXSALES               numeric(11, 4),
    @ACCOUNTCARD               UD_ACCOUNTCARD,
    @DATEPROCESALES            smalldatetime,
    @CCLASSIDCASH              UD_CUSTCLASSID,
    @CCLASSIDANTICIPATE        UD_CUSTCLASSID,
    @CCLASSIDCREDLOCAL         UD_CUSTCLASSID,
    @CCLASSIDCREDCORPORATE     UD_CUSTCLASSID,
    @CCLASSIDOTHER             UD_CUSTCLASSID,
    @DOCTYPEIDTICKET           UD_DOCTYPEID,
    @DOCTYPEIDFAC              UD_DOCTYPEID,
    @DOCTYPEIDBOL              UD_DOCTYPEID,
    @DOCTYPEIDNC               UD_DOCTYPEID,
    @DOCTYPEIDND               UD_DOCTYPEID,
    @STKCORRELATIVE            bit,
    @STKENDBYDAY               bit,
    @ACCESSCLOSEDZ             bit,
    @REPORTPATH                UD_REPORTES               = NULL,
    @TRANSACFUELPATH           UD_REPORTES               = NULL,
    @TABLESTRANSACFUEL         UD_REPORTES               = NULL,
    @STKODBCSTRING             int,
    @STRINGCONECC              varchar(400),
    @UNITMEDFUEL               UD_STKUNITID              = NULL,
    @STKNEW                    bit,
    @STKMODIFY                 bit,
    @STKDELETE                 bit,
    @STKSEND                   bit,
    @STKRECEIVE                bit,
    @USER1                     UD_USER1                  = NULL,
    @USER2                     UD_USER2                  = NULL,
    @USER3                     UD_USER3                  = NULL,
    @USER4                     UD_USER5                  = NULL,
    @USER5                     UD_USER5                  = NULL,
    @USER6                     UD_USER6                  = NULL,
    @USER7                     UD_USER7                  = NULL,
    @USER8                     UD_USER8                  = NULL,
    @USER9                     UD_USER9                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SETUP(AUTOMATICSALES,
                         CATEGORYFUEL,
                         CATEGORYLUBES,
                         BADGE,
                         MTOMAXSALES,
                         ACCOUNTCARD,
                         DATEPROCESALES,
                         CCLASSIDCASH,
                         CCLASSIDANTICIPATE,
                         CCLASSIDCREDLOCAL,
                         CCLASSIDCREDCORPORATE,
                         CCLASSIDOTHER,
                         DOCTYPEIDTICKET,
                         DOCTYPEIDFAC,
                         DOCTYPEIDBOL,
                         DOCTYPEIDNC,
                         DOCTYPEIDND,
                         STKCORRELATIVE,
                         STKENDBYDAY,
                         ACCESSCLOSEDZ,
                         REPORTPATH,
                         TRANSACFUELPATH,
                         TABLESTRANSACFUEL,
                         STKODBCSTRING,
                         STRINGCONECC,
                         UNITMEDFUEL,
                         STKNEW,
                         STKMODIFY,
                         STKDELETE,
                         STKSEND,
                         STKRECEIVE,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@AUTOMATICSALES,
           @CATEGORYFUEL,
           @CATEGORYLUBES,
           @BADGE,
           @MTOMAXSALES,
           @ACCOUNTCARD,
           @DATEPROCESALES,
           @CCLASSIDCASH,
           @CCLASSIDANTICIPATE,
           @CCLASSIDCREDLOCAL,
           @CCLASSIDCREDCORPORATE,
           @CCLASSIDOTHER,
           @DOCTYPEIDTICKET,
           @DOCTYPEIDFAC,
           @DOCTYPEIDBOL,
           @DOCTYPEIDNC,
           @DOCTYPEIDND,
           @STKCORRELATIVE,
           @STKENDBYDAY,
           @ACCESSCLOSEDZ,
           @REPORTPATH,
           @TRANSACFUELPATH,
           @TABLESTRANSACFUEL,
           @STKODBCSTRING,
           @STRINGCONECC,
           @UNITMEDFUEL,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SETUP: Cannot insert because primary key value not found in OP_SETUP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SETUP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SETUP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SETUP >>>'
go





/* 
 * PROCEDURE: SP_InsProcOP_SETUP_GEN 
 */

CREATE PROCEDURE SP_InsProcOP_SETUP_GEN
(
    @SITEID                    UD_IDSITE,
    @AUTOMATICSALES            bit,
    @CATEGORYFUEL              UD_CATEGORY,
    @BADGE                     bit,
    @MTOMAXSALES               numeric(11, 4),
    @ACCOUNTCARD               UD_ACCOUNTCARD,
    @DATEPROCESALES            smalldatetime,
    @CCLASSIDCASH              UD_CUSTCLASSID,
    @CCLASSIDANTICIPATE        UD_CUSTCLASSID,
    @CCLASSIDCREDLOCAL         UD_CUSTCLASSID,
    @CCLASSIDCREDCORPORATE     UD_CUSTCLASSID,
    @CCLASSIDOTHER             UD_CUSTCLASSID,
    @DOCTYPEIDTICKET           UD_DOCTYPEID,
    @DOCTYPEIDFAC              UD_DOCTYPEID,
    @DOCTYPEIDBOL              UD_DOCTYPEID,
    @DOCTYPEIDNC               UD_DOCTYPEID,
    @DOCTYPEIDND               UD_DOCTYPEID,
    @STKCORRELATIVE            bit,
    @STKENDBYDAY               bit,
    @ACCESSCLOSEDZ             bit,
    @REPORTPATH                UD_REPORTES               = NULL,
    @TRANSACFUELPATH           UD_REPORTES               = NULL,
    @TABLESTRANSACFUEL         UD_REPORTES               = NULL,
    @STKODBCSTRING             int,
    @STRINGCONECC              varchar(400),
    @UNITMEDFUEL               UD_STKUNITID              = NULL,
    @CATEGORYLUBES             UD_CATEGORY,
    @STKNEW                    bit,
    @STKMODIFY                 bit,
    @STKDELETE                 bit,
    @STKSEND                   bit,
    @STKRECEIVE                bit,
    @USER1                     UD_USER1                  = NULL,
    @USER2                     UD_USER2                  = NULL,
    @USER3                     UD_USER3                  = NULL,
    @USER4                     UD_USER5                  = NULL,
    @USER5                     UD_USER5                  = NULL,
    @USER6                     UD_USER6                  = NULL,
    @USER7                     UD_USER7                  = NULL,
    @USER8                     UD_USER8                  = NULL,
    @USER9                     UD_USER9                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SETUP_GEN(SITEID,
                             AUTOMATICSALES,
                             CATEGORYFUEL,
                             BADGE,
                             MTOMAXSALES,
                             ACCOUNTCARD,
                             DATEPROCESALES,
                             CCLASSIDCASH,
                             CCLASSIDANTICIPATE,
                             CCLASSIDCREDLOCAL,
                             CCLASSIDCREDCORPORATE,
                             CCLASSIDOTHER,
                             DOCTYPEIDTICKET,
                             DOCTYPEIDFAC,
                             DOCTYPEIDBOL,
                             DOCTYPEIDNC,
                             DOCTYPEIDND,
                             STKCORRELATIVE,
                             STKENDBYDAY,
                             ACCESSCLOSEDZ,
                             REPORTPATH,
                             TRANSACFUELPATH,
                             TABLESTRANSACFUEL,
                             STKODBCSTRING,
                             STRINGCONECC,
                             UNITMEDFUEL,
                             CATEGORYLUBES,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9 
                             )
    VALUES(@SITEID,
           @AUTOMATICSALES,
           @CATEGORYFUEL,
           @BADGE,
           @MTOMAXSALES,
           @ACCOUNTCARD,
           @DATEPROCESALES,
           @CCLASSIDCASH,
           @CCLASSIDANTICIPATE,
           @CCLASSIDCREDLOCAL,
           @CCLASSIDCREDCORPORATE,
           @CCLASSIDOTHER,
           @DOCTYPEIDTICKET,
           @DOCTYPEIDFAC,
           @DOCTYPEIDBOL,
           @DOCTYPEIDNC,
           @DOCTYPEIDND,
           @STKCORRELATIVE,
           @STKENDBYDAY,
           @ACCESSCLOSEDZ,
           @REPORTPATH,
           @TRANSACFUELPATH,
           @TABLESTRANSACFUEL,
           @STKODBCSTRING,
           @STRINGCONECC,
           @UNITMEDFUEL,
           @CATEGORYLUBES,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SETUP_GEN: Cannot insert because primary key value not found in OP_SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SETUP_GEN 
 */

CREATE PROCEDURE SP_UpdProcOP_SETUP_GEN
(
    @SITEID                    UD_IDSITE,
    @AUTOMATICSALES            bit,
    @CATEGORYFUEL              UD_CATEGORY,
    @BADGE                     bit,
    @MTOMAXSALES               numeric(11, 4),
    @ACCOUNTCARD               UD_ACCOUNTCARD,
    @DATEPROCESALES            smalldatetime,
    @CCLASSIDCASH              UD_CUSTCLASSID,
    @CCLASSIDANTICIPATE        UD_CUSTCLASSID,
    @CCLASSIDCREDLOCAL         UD_CUSTCLASSID,
    @CCLASSIDCREDCORPORATE     UD_CUSTCLASSID,
    @CCLASSIDOTHER             UD_CUSTCLASSID,
    @DOCTYPEIDTICKET           UD_DOCTYPEID,
    @DOCTYPEIDFAC              UD_DOCTYPEID,
    @DOCTYPEIDBOL              UD_DOCTYPEID,
    @DOCTYPEIDNC               UD_DOCTYPEID,
    @DOCTYPEIDND               UD_DOCTYPEID,
    @STKCORRELATIVE            bit,
    @STKENDBYDAY               bit,
    @ACCESSCLOSEDZ             bit,
    @REPORTPATH                UD_REPORTES               = NULL,
    @TRANSACFUELPATH           UD_REPORTES               = NULL,
    @TABLESTRANSACFUEL         UD_REPORTES               = NULL,
    @STKODBCSTRING             int,
    @STRINGCONECC              varchar(400),
    @UNITMEDFUEL               UD_STKUNITID              = NULL,
    @CATEGORYLUBES             UD_CATEGORY,
    @STKNEW                    bit,
    @STKMODIFY                 bit,
    @STKDELETE                 bit,
    @STKSEND                   bit,
    @STKRECEIVE                bit,
    @USER1                     UD_USER1                  = NULL,
    @USER2                     UD_USER2                  = NULL,
    @USER3                     UD_USER3                  = NULL,
    @USER4                     UD_USER5                  = NULL,
    @USER5                     UD_USER5                  = NULL,
    @USER6                     UD_USER6                  = NULL,
    @USER7                     UD_USER7                  = NULL,
    @USER8                     UD_USER8                  = NULL,
    @USER9                     UD_USER9                  = NULL,
    @TSTAMP                    timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SETUP_GEN
       SET AUTOMATICSALES             = @AUTOMATICSALES,
           CATEGORYFUEL               = @CATEGORYFUEL,
           BADGE                      = @BADGE,
           MTOMAXSALES                = @MTOMAXSALES,
           ACCOUNTCARD                = @ACCOUNTCARD,
           DATEPROCESALES             = @DATEPROCESALES,
           CCLASSIDCASH               = @CCLASSIDCASH,
           CCLASSIDANTICIPATE         = @CCLASSIDANTICIPATE,
           CCLASSIDCREDLOCAL          = @CCLASSIDCREDLOCAL,
           CCLASSIDCREDCORPORATE      = @CCLASSIDCREDCORPORATE,
           CCLASSIDOTHER              = @CCLASSIDOTHER,
           DOCTYPEIDTICKET            = @DOCTYPEIDTICKET,
           DOCTYPEIDFAC               = @DOCTYPEIDFAC,
           DOCTYPEIDBOL               = @DOCTYPEIDBOL,
           DOCTYPEIDNC                = @DOCTYPEIDNC,
           DOCTYPEIDND                = @DOCTYPEIDND,
           STKCORRELATIVE             = @STKCORRELATIVE,
           STKENDBYDAY                = @STKENDBYDAY,
           ACCESSCLOSEDZ              = @ACCESSCLOSEDZ,
           REPORTPATH                 = @REPORTPATH,
           TRANSACFUELPATH            = @TRANSACFUELPATH,
           TABLESTRANSACFUEL          = @TABLESTRANSACFUEL,
           STKODBCSTRING              = @STKODBCSTRING,
           STRINGCONECC               = @STRINGCONECC,
           UNITMEDFUEL                = @UNITMEDFUEL,
           CATEGORYLUBES              = @CATEGORYLUBES,
           STKNEW                     = @STKNEW,
           STKMODIFY                  = @STKMODIFY,
           STKDELETE                  = @STKDELETE,
           STKSEND                    = @STKSEND,
           STKRECEIVE                 = @STKRECEIVE,
           USER1                      = @USER1,
           USER2                      = @USER2,
           USER3                      = @USER3,
           USER4                      = @USER4,
           USER5                      = @USER5,
           USER6                      = @USER6,
           USER7                      = @USER7,
           USER8                      = @USER8,
           USER9                      = @USER9
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SETUP_GEN: Cannot update  in OP_SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SETUP_GEN 
 */

CREATE PROCEDURE SP_DelProcOP_SETUP_GEN
(
    @SITEID                    UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SETUP_GEN
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SETUP_GEN: Cannot delete because foreign keys still exist in OP_SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SETUP_GEN 
 */

CREATE PROCEDURE SP_SelProcOP_SETUP_GEN
(
    @SITEID                    UD_IDSITE)
AS
BEGIN
    SELECT SITEID,
           AUTOMATICSALES,
           CATEGORYFUEL,
           BADGE,
           MTOMAXSALES,
           ACCOUNTCARD,
           DATEPROCESALES,
           CCLASSIDCASH,
           CCLASSIDANTICIPATE,
           CCLASSIDCREDLOCAL,
           CCLASSIDCREDCORPORATE,
           CCLASSIDOTHER,
           DOCTYPEIDTICKET,
           DOCTYPEIDFAC,
           DOCTYPEIDBOL,
           DOCTYPEIDNC,
           DOCTYPEIDND,
           STKCORRELATIVE,
           STKENDBYDAY,
           ACCESSCLOSEDZ,
           REPORTPATH,
           TRANSACFUELPATH,
           TABLESTRANSACFUEL,
           STKODBCSTRING,
           STRINGCONECC,
           UNITMEDFUEL,
           CATEGORYLUBES,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM OP_SETUP_GEN
     WHERE SITEID = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SIDE_FUEL 
 */

CREATE PROCEDURE SP_InsProcOP_SIDE_FUEL
(
    @SIDE           UD_CARA,
    @HOSENUMBER     tinyint,
    @INVTIDBARR     UD_CODIGOBARRA,
    @DESCR          UD_DESCRIPCION,
    @USERID         UD_USERID,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SIDE_FUEL(SIDE,
                             HOSENUMBER,
                             INVTIDBARR,
                             DESCR,
                             USERID,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@SIDE,
           @HOSENUMBER,
           @INVTIDBARR,
           @DESCR,
           @USERID,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SIDE_FUEL: Cannot insert because primary key value not found in OP_SIDE_FUEL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SIDE_FUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SIDE_FUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SIDE_FUEL >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SIDE_FUEL 
 */

CREATE PROCEDURE SP_UpdProcOP_SIDE_FUEL
(
    @SIDE           UD_CARA,
    @HOSENUMBER     tinyint,
    @INVTIDBARR     UD_CODIGOBARRA,
    @DESCR          UD_DESCRIPCION,
    @USERID         UD_USERID,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SIDE_FUEL
       SET INVTIDBARR      = @INVTIDBARR,
           DESCR           = @DESCR,
           USERID          = @USERID,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SIDE_FUEL: Cannot update  in OP_SIDE_FUEL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SIDE_FUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SIDE_FUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SIDE_FUEL >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SIDE_FUEL 
 */

CREATE PROCEDURE SP_DelProcOP_SIDE_FUEL
(
    @SIDE           UD_CARA,
    @HOSENUMBER     tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SIDE_FUEL
     WHERE SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SIDE_FUEL: Cannot delete because foreign keys still exist in OP_SIDE_FUEL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SIDE_FUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SIDE_FUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SIDE_FUEL >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SIDE_FUEL 
 */

CREATE PROCEDURE SP_SelProcOP_SIDE_FUEL
(
    @SIDE           UD_CARA,
    @HOSENUMBER     tinyint)
AS
BEGIN
    SELECT SIDE,
           HOSENUMBER,
           INVTIDBARR,
           DESCR,
           USERID,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_SIDE_FUEL
     WHERE SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SIDE_FUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SIDE_FUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SIDE_FUEL >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_STATUSPERSON 
 */

CREATE PROCEDURE SP_InsProcOP_STATUSPERSON
(
    @STATUSPERSONID        char(2),
    @DESCRSTATUSPERSON     UD_DESCRIPCION,
    @USERID                UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_STATUSPERSON(STATUSPERSONID,
                                DESCRSTATUSPERSON,
                                USERID,
                                STKNEW,
                                STKMODIFY,
                                STKDELETE,
                                STKSEND,
                                STKRECEIVE,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@STATUSPERSONID,
           @DESCRSTATUSPERSON,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_STATUSPERSON: Cannot insert because primary key value not found in OP_STATUSPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_STATUSPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_STATUSPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_STATUSPERSON >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_STATUSPERSON 
 */

CREATE PROCEDURE SP_UpdProcOP_STATUSPERSON
(
    @STATUSPERSONID        char(2),
    @DESCRSTATUSPERSON     UD_DESCRIPCION,
    @USERID                UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                 UD_USER1                  = NULL,
    @USER2                 UD_USER2                  = NULL,
    @USER3                 UD_USER6                  = NULL,
    @USER4                 UD_USER7                  = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_STATUSPERSON
       SET DESCRSTATUSPERSON      = @DESCRSTATUSPERSON,
           USERID                 = @USERID,
           STKNEW                 = @STKNEW,
           STKMODIFY              = @STKMODIFY,
           STKDELETE              = @STKDELETE,
           STKSEND                = @STKSEND,
           STKRECEIVE             = @STKRECEIVE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE STATUSPERSONID = @STATUSPERSONID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_STATUSPERSON: Cannot update  in OP_STATUSPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_STATUSPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_STATUSPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_STATUSPERSON >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_STATUSPERSON 
 */

CREATE PROCEDURE SP_DelProcOP_STATUSPERSON
(
    @STATUSPERSONID        char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_STATUSPERSON
     WHERE STATUSPERSONID = @STATUSPERSONID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_STATUSPERSON: Cannot delete because foreign keys still exist in OP_STATUSPERSON '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_STATUSPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_STATUSPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_STATUSPERSON >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_STATUSPERSON 
 */

CREATE PROCEDURE SP_SelProcOP_STATUSPERSON
(
    @STATUSPERSONID        char(2))
AS
BEGIN
    SELECT STATUSPERSONID,
           DESCRSTATUSPERSON,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_STATUSPERSON
     WHERE STATUSPERSONID = @STATUSPERSONID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_STATUSPERSON') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_STATUSPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_STATUSPERSON >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SUMMARYDAY 
 */

CREATE PROCEDURE SP_InsProcOP_SUMMARYDAY
(
    @BATNBRDIARYSUMMARY     numeric(10, 0),
    @SITEID                 UD_IDSITE,
    @DATEPROCE              smalldatetime,
    @CURYRATE               UD_CURYRATE,
    @ROUND                  numeric(11, 4),
    @TOTALDIFERENCE         numeric(11, 4),
    @OBSERVATION            varchar(100)              = NULL,
    @STATUS                 bit                       = NULL,
    @TOTALDEBE              numeric(11, 4),
    @TOTALHABER             numeric(11, 4),
    @USERID                 UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @USER3                  UD_USER6                  = NULL,
    @USER4                  UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SUMMARYDAY(BATNBRDIARYSUMMARY,
                              SITEID,
                              DATEPROCE,
                              CURYRATE,
                              ROUND,
                              TOTALDIFERENCE,
                              OBSERVATION,
                              STATUS,
                              TOTALDEBE,
                              TOTALHABER,
                              USERID,
                              STKSEND,
                              STKRECEIVE,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@BATNBRDIARYSUMMARY,
           @SITEID,
           @DATEPROCE,
           @CURYRATE,
           @ROUND,
           @TOTALDIFERENCE,
           @OBSERVATION,
           @STATUS,
           @TOTALDEBE,
           @TOTALHABER,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SUMMARYDAY: Cannot insert because primary key value not found in OP_SUMMARYDAY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SUMMARYDAY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SUMMARYDAY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SUMMARYDAY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SUMMARYDAY 
 */

CREATE PROCEDURE SP_UpdProcOP_SUMMARYDAY
(
    @BATNBRDIARYSUMMARY     numeric(10, 0),
    @SITEID                 UD_IDSITE,
    @DATEPROCE              smalldatetime,
    @CURYRATE               UD_CURYRATE,
    @ROUND                  numeric(11, 4),
    @TOTALDIFERENCE         numeric(11, 4),
    @OBSERVATION            varchar(100)              = NULL,
    @STATUS                 bit                       = NULL,
    @TOTALDEBE              numeric(11, 4),
    @TOTALHABER             numeric(11, 4),
    @USERID                 UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @USER3                  UD_USER6                  = NULL,
    @USER4                  UD_USER7                  = NULL,
    @TSTAMP                 timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SUMMARYDAY
       SET DATEPROCE               = @DATEPROCE,
           CURYRATE                = @CURYRATE,
           ROUND                   = @ROUND,
           TOTALDIFERENCE          = @TOTALDIFERENCE,
           OBSERVATION             = @OBSERVATION,
           STATUS                  = @STATUS,
           TOTALDEBE               = @TOTALDEBE,
           TOTALHABER              = @TOTALHABER,
           USERID                  = @USERID,
           STKSEND                 = @STKSEND,
           STKRECEIVE              = @STKRECEIVE,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE BATNBRDIARYSUMMARY = @BATNBRDIARYSUMMARY
       AND SITEID             = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SUMMARYDAY: Cannot update  in OP_SUMMARYDAY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SUMMARYDAY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SUMMARYDAY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SUMMARYDAY >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SUMMARYDAY 
 */

CREATE PROCEDURE SP_DelProcOP_SUMMARYDAY
(
    @BATNBRDIARYSUMMARY     numeric(10, 0),
    @SITEID                 UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SUMMARYDAY
     WHERE BATNBRDIARYSUMMARY = @BATNBRDIARYSUMMARY
       AND SITEID             = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SUMMARYDAY: Cannot delete because foreign keys still exist in OP_SUMMARYDAY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SUMMARYDAY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SUMMARYDAY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SUMMARYDAY >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SUMMARYDAY 
 */

CREATE PROCEDURE SP_SelProcOP_SUMMARYDAY
(
    @BATNBRDIARYSUMMARY     numeric(10, 0),
    @SITEID                 UD_IDSITE)
AS
BEGIN
    SELECT BATNBRDIARYSUMMARY,
           SITEID,
           DATEPROCE,
           CURYRATE,
           ROUND,
           TOTALDIFERENCE,
           OBSERVATION,
           STATUS,
           TOTALDEBE,
           TOTALHABER,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_SUMMARYDAY
     WHERE BATNBRDIARYSUMMARY = @BATNBRDIARYSUMMARY
       AND SITEID             = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SUMMARYDAY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SUMMARYDAY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SUMMARYDAY >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SUMMARYSALES 
 */

CREATE PROCEDURE SP_InsProcOP_SUMMARYSALES
(
    @DATEPROCE      smalldatetime,
    @COMMENT        UD_DESCRIPCION,
    @STATUS         bit                       = NULL,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SUMMARYSALES(DATEPROCE,
                                COMMENT,
                                STATUS,
                                USERID,
                                STKSEND,
                                STKRECEIVE,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@DATEPROCE,
           @COMMENT,
           @STATUS,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SUMMARYSALES: Cannot insert because primary key value not found in OP_SUMMARYSALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SUMMARYSALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SUMMARYSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SUMMARYSALES >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_SUMMARYSALES 
 */

CREATE PROCEDURE SP_UpdProcOP_SUMMARYSALES
(
    @DATEPROCE      smalldatetime,
    @COMMENT        UD_DESCRIPCION,
    @STATUS         bit                       = NULL,
    @USERID         UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SUMMARYSALES
       SET COMMENT         = @COMMENT,
           STATUS          = @STATUS,
           USERID          = @USERID,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE DATEPROCE = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_SUMMARYSALES: Cannot update  in OP_SUMMARYSALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_SUMMARYSALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_SUMMARYSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_SUMMARYSALES >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_SUMMARYSALES 
 */

CREATE PROCEDURE SP_DelProcOP_SUMMARYSALES
(
    @DATEPROCE      smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SUMMARYSALES
     WHERE DATEPROCE = @DATEPROCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_SUMMARYSALES: Cannot delete because foreign keys still exist in OP_SUMMARYSALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_SUMMARYSALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_SUMMARYSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_SUMMARYSALES >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_SUMMARYSALES 
 */

CREATE PROCEDURE SP_SelProcOP_SUMMARYSALES
(
    @DATEPROCE      smalldatetime)
AS
BEGIN
    SELECT DATEPROCE,
           COMMENT,
           STATUS,
           USERID,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_SUMMARYSALES
     WHERE DATEPROCE = @DATEPROCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_SUMMARYSALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_SUMMARYSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_SUMMARYSALES >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_SUMMARYSALESDET 
 */

CREATE PROCEDURE SP_InsProcOP_SUMMARYSALESDET
(
    @SITEID                  UD_IDSITE,
    @DATEPROCE               smalldatetime,
    @SEQUENCE                smallint,
    @DOCSUMMARYTYPEIDFAC     char(2),
    @REFERENCEFAC            char(15),
    @DOCSUMMARYTYPEIDNC      char(2)                   = NULL,
    @REFERENCENC             char(15)                  = NULL,
    @CLASSID                 UD_CATEGORY,
    @CLASSUBID               char(5)                   = NULL,
    @CUSTCLASSID             UD_CUSTCLASSID            = NULL,
    @CUSTIDSS                UD_CODCLIENT              = NULL,
    @TOTALBONUS              numeric(11, 4),
    @DIFFERENCE              numeric(11, 4),
    @USERID                  UD_USERID,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                   UD_USER1                  = NULL,
    @USER2                   UD_USER2                  = NULL,
    @USER3                   UD_USER6                  = NULL,
    @USER4                   UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SUMMARYSALESDET(SITEID,
                                   DATEPROCE,
                                   SEQUENCE,
                                   DOCSUMMARYTYPEIDFAC,
                                   REFERENCEFAC,
                                   DOCSUMMARYTYPEIDNC,
                                   REFERENCENC,
                                   CLASSID,
                                   CLASSUBID,
                                   CUSTCLASSID,
                                   CUSTIDSS,
                                   TOTALBONUS,
                                   DIFFERENCE,
                                   USERID,
                                   STKSEND,
                                   STKRECEIVE,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@SITEID,
           @DATEPROCE,
           @SEQUENCE,
           @DOCSUMMARYTYPEIDFAC,
           @REFERENCEFAC,
           @DOCSUMMARYTYPEIDNC,
           @REFERENCENC,
           @CLASSID,
           @CLASSUBID,
           @CUSTCLASSID,
           @CUSTIDSS,
           @TOTALBONUS,
           @DIFFERENCE,
           @USERID,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_SUMMARYSALESDET: Cannot insert because primary key value not found in OP_SUMMARYSALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_SUMMARYSALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_SUMMARYSALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_SUMMARYSALESDET >>>'
go





/* 
 * PROCEDURE: SP_InsProcOP_TICKETFORM 
 */

CREATE PROCEDURE SP_InsProcOP_TICKETFORM
(
    @HEAD1      varchar(40)            = NULL,
    @HEAD2      varchar(40)            = NULL,
    @HEAD3      varchar(40)            = NULL,
    @HEAD4      varchar(40)            = NULL,
    @HEAD5      varchar(40)            = NULL,
    @HEAD6      varchar(40)            = NULL,
    @HEAD7      varchar(40)            = NULL,
    @HEAD8      varchar(40)            = NULL,
    @HEAD9      varchar(40)            = NULL,
    @HEAD10     varchar(40)            = NULL,
    @LINES1     varchar(40)            = NULL,
    @LINES2     varchar(40)            = NULL,
    @LINES3     varchar(40)            = NULL,
    @LINES4     varchar(40)            = NULL,
    @FOOT1      varchar(40)            = NULL,
    @FOOT2      varchar(40)            = NULL,
    @FOOT3      varchar(40)            = NULL,
    @FOOT4      varchar(40)            = NULL,
    @FOOT5      varchar(40)            = NULL,
    @FOOT6      varchar(40)            = NULL,
    @FOOT7      varchar(40)            = NULL,
    @FOOT8      varchar(40)            = NULL,
    @FOOT9      varchar(40)            = NULL,
    @FOOT10     varchar(40)            = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_TICKETFORM(HEAD1,
                              HEAD2,
                              HEAD3,
                              HEAD4,
                              HEAD5,
                              HEAD6,
                              HEAD7,
                              HEAD8,
                              HEAD9,
                              HEAD10,
                              LINES1,
                              LINES2,
                              LINES3,
                              LINES4,
                              FOOT1,
                              FOOT2,
                              FOOT3,
                              FOOT4,
                              FOOT5,
                              FOOT6,
                              FOOT7,
                              FOOT8,
                              FOOT9,
                              FOOT10 
                              )
    VALUES(@HEAD1,
           @HEAD2,
           @HEAD3,
           @HEAD4,
           @HEAD5,
           @HEAD6,
           @HEAD7,
           @HEAD8,
           @HEAD9,
           @HEAD10,
           @LINES1,
           @LINES2,
           @LINES3,
           @LINES4,
           @FOOT1,
           @FOOT2,
           @FOOT3,
           @FOOT4,
           @FOOT5,
           @FOOT6,
           @FOOT7,
           @FOOT8,
           @FOOT9,
           @FOOT10 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_TICKETFORM: Cannot insert because primary key value not found in OP_TICKETFORM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_TICKETFORM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_TICKETFORM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_TICKETFORM >>>'
go





/* 
 * PROCEDURE: SP_InsProcOP_TRAN 
 */

CREATE PROCEDURE SP_InsProcOP_TRAN
(
    @NUMERO        char(4),
    @SOLES         char(6),
    @PRODUCTO      char(2),
    @PRECIO        char(4),
    @GALONES       char(8),
    @CARA          char(2),
    @FECHA         char(6),
    @HORA          char(4),
    @TURNO         char(1),
    @ESTADO        char(10),
    @DOCUMENTO     numeric(20, 0))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_TRAN(NUMERO,
                        SOLES,
                        PRODUCTO,
                        PRECIO,
                        GALONES,
                        CARA,
                        FECHA,
                        HORA,
                        TURNO,
                        ESTADO,
                        DOCUMENTO)
    VALUES(@NUMERO,
           @SOLES,
           @PRODUCTO,
           @PRECIO,
           @GALONES,
           @CARA,
           @FECHA,
           @HORA,
           @TURNO,
           @ESTADO,
           @DOCUMENTO)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_TRAN: Cannot insert because primary key value not found in OP_TRAN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_TRAN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_TRAN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_TRAN >>>'
go





/* 
 * PROCEDURE: SP_InsProcOP_TURNVSSIDE 
 */

CREATE PROCEDURE SP_InsProcOP_TURNVSSIDE
(
    @SITEID            UD_IDSITE,
    @DATEPROCE         UD_FECHA,
    @DATESALES         UD_FECHA,
    @NUMBERTURN        tinyint,
    @SALESPERID        UD_SALESPERSONID,
    @SIDE              UD_CARA,
    @STATUS            bit,
    @SALESPERIDOLD     char(20)                    = NULL,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER6                    = NULL,
    @USER4             UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_TURNVSSIDE(SITEID,
                              DATEPROCE,
                              DATESALES,
                              NUMBERTURN,
                              SALESPERID,
                              SIDE,
                              STATUS,
                              SALESPERIDOLD,
                              STKSEND,
                              STKRECEIVE,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@SITEID,
           @DATEPROCE,
           @DATESALES,
           @NUMBERTURN,
           @SALESPERID,
           @SIDE,
           @STATUS,
           @SALESPERIDOLD,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_TURNVSSIDE: Cannot insert because primary key value not found in OP_TURNVSSIDE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_TURNVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_TURNVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_TURNVSSIDE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_TURNVSSIDE 
 */

CREATE PROCEDURE SP_UpdProcOP_TURNVSSIDE
(
    @SITEID            UD_IDSITE,
    @DATEPROCE         UD_FECHA,
    @DATESALES         UD_FECHA,
    @NUMBERTURN        tinyint,
    @SALESPERID        UD_SALESPERSONID,
    @SIDE              UD_CARA,
    @STATUS            bit,
    @SALESPERIDOLD     char(20)                    = NULL,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER6                    = NULL,
    @USER4             UD_USER7                    = NULL,
    @TSTAMP            timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_TURNVSSIDE
       SET STATUS             = @STATUS,
           SALESPERIDOLD      = @SALESPERIDOLD,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE SITEID     = @SITEID
       AND DATEPROCE  = @DATEPROCE
       AND DATESALES  = @DATESALES
       AND NUMBERTURN = @NUMBERTURN
       AND SALESPERID = @SALESPERID
       AND SIDE       = @SIDE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_TURNVSSIDE: Cannot update  in OP_TURNVSSIDE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_TURNVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_TURNVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_TURNVSSIDE >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_TURNVSSIDE 
 */

CREATE PROCEDURE SP_DelProcOP_TURNVSSIDE
(
    @SITEID            UD_IDSITE,
    @DATEPROCE         UD_FECHA,
    @DATESALES         UD_FECHA,
    @NUMBERTURN        tinyint,
    @SALESPERID        UD_SALESPERSONID,
    @SIDE              UD_CARA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_TURNVSSIDE
     WHERE SITEID     = @SITEID
       AND DATEPROCE  = @DATEPROCE
       AND DATESALES  = @DATESALES
       AND NUMBERTURN = @NUMBERTURN
       AND SALESPERID = @SALESPERID
       AND SIDE       = @SIDE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_TURNVSSIDE: Cannot delete because foreign keys still exist in OP_TURNVSSIDE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_TURNVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_TURNVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_TURNVSSIDE >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_TURNVSSIDE 
 */

CREATE PROCEDURE SP_SelProcOP_TURNVSSIDE
(
    @SITEID            UD_IDSITE,
    @DATEPROCE         UD_FECHA,
    @DATESALES         UD_FECHA,
    @NUMBERTURN        tinyint,
    @SALESPERID        UD_SALESPERSONID,
    @SIDE              UD_CARA)
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           DATESALES,
           NUMBERTURN,
           SALESPERID,
           SIDE,
           STATUS,
           SALESPERIDOLD,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_TURNVSSIDE
     WHERE SITEID     = @SITEID
       AND DATEPROCE  = @DATEPROCE
       AND DATESALES  = @DATESALES
       AND NUMBERTURN = @NUMBERTURN
       AND SALESPERID = @SALESPERID
       AND SIDE       = @SIDE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_TURNVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_TURNVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_TURNVSSIDE >>>'
go


/* 
 * PROCEDURE: SP_InsProcOP_TURNVSSIDEDET 
 */

CREATE PROCEDURE SP_InsProcOP_TURNVSSIDEDET
(
    @USER1      UD_USER1             = NULL,
    @USER2      UD_USER2             = NULL,
    @USER3      UD_USER6             = NULL,
    @USER4      UD_USER7             = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_TURNVSSIDEDET(USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_TURNVSSIDEDET: Cannot insert because primary key value not found in OP_TURNVSSIDEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_TURNVSSIDEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_TURNVSSIDEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_TURNVSSIDEDET >>>'
go





/* 
 * PROCEDURE: SP_InsProcOP_ZETA 
 */

CREATE PROCEDURE SP_InsProcOP_ZETA
(
    @SALESPOINT         char(10),
    @DATEPROCESALES     smalldatetime,
    @DATEZETA           UD_FECHAHORA              = NULL,
    @TOTZETA            numeric(11, 4),
    @NBRZETA            smallint,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                  = NULL,
    @USER2              UD_USER2                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_ZETA(SALESPOINT,
                        DATEPROCESALES,
                        DATEZETA,
                        TOTZETA,
                        NBRZETA,
                        STKSEND,
                        STKRECEIVE,
                        USER1,
                        USER2 
                        )
    VALUES(@SALESPOINT,
           @DATEPROCESALES,
           @DATEZETA,
           @TOTZETA,
           @NBRZETA,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcOP_ZETA: Cannot insert because primary key value not found in OP_ZETA '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcOP_ZETA') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcOP_ZETA >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcOP_ZETA >>>'
go


/* 
 * PROCEDURE: SP_UpdProcOP_ZETA 
 */

CREATE PROCEDURE SP_UpdProcOP_ZETA
(
    @SALESPOINT         char(10),
    @DATEPROCESALES     smalldatetime,
    @DATEZETA           UD_FECHAHORA              = NULL,
    @TOTZETA            numeric(11, 4),
    @NBRZETA            smallint,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                  = NULL,
    @USER2              UD_USER2                  = NULL,
    @TSTAMP             timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_ZETA
       SET DATEZETA            = @DATEZETA,
           TOTZETA             = @TOTZETA,
           NBRZETA             = @NBRZETA,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE,
           USER1               = @USER1,
           USER2               = @USER2
     WHERE SALESPOINT     = @SALESPOINT
       AND DATEPROCESALES = @DATEPROCESALES

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcOP_ZETA: Cannot update  in OP_ZETA '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcOP_ZETA') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcOP_ZETA >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcOP_ZETA >>>'
go


/* 
 * PROCEDURE: SP_DelProcOP_ZETA 
 */

CREATE PROCEDURE SP_DelProcOP_ZETA
(
    @SALESPOINT         char(10),
    @DATEPROCESALES     smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_ZETA
     WHERE SALESPOINT     = @SALESPOINT
       AND DATEPROCESALES = @DATEPROCESALES

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcOP_ZETA: Cannot delete because foreign keys still exist in OP_ZETA '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcOP_ZETA') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcOP_ZETA >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcOP_ZETA >>>'
go


/* 
 * PROCEDURE: SP_SelProcOP_ZETA 
 */

CREATE PROCEDURE SP_SelProcOP_ZETA
(
    @SALESPOINT         char(10),
    @DATEPROCESALES     smalldatetime)
AS
BEGIN
    SELECT SALESPOINT,
           DATEPROCESALES,
           DATEZETA,
           TOTZETA,
           NBRZETA,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           TSTAMP
      FROM OP_ZETA
     WHERE SALESPOINT     = @SALESPOINT
       AND DATEPROCESALES = @DATEPROCESALES

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcOP_ZETA') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcOP_ZETA >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcOP_ZETA >>>'
go


/* 
 * PROCEDURE: SP_InsProcSE_ACCESSDETRIGHTS 
 */

CREATE PROCEDURE SP_InsProcSE_ACCESSDETRIGHTS
(
    @USERID           UD_USERID,
    @SCREENID         UD_SCREENID,
    @VIEWRIGHTS       bit,
    @INSERTRIGHTS     bit,
    @UPDATERIGHTS     bit,
    @DELETERIGHTS     bit,
    @PRINTRIGHTS      bit,
    @NULLRIGHTS       bit,
    @CLOSERIGHTS      bit,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1               = NULL,
    @USER2            UD_USER2               = NULL,
    @USER3            UD_USER6               = NULL,
    @USER4            UD_USER7               = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SE_ACCESSDETRIGHTS(USERID,
                                   SCREENID,
                                   VIEWRIGHTS,
                                   INSERTRIGHTS,
                                   UPDATERIGHTS,
                                   DELETERIGHTS,
                                   PRINTRIGHTS,
                                   NULLRIGHTS,
                                   CLOSERIGHTS,
                                   STKNEW,
                                   STKMODIFY,
                                   STKDELETE,
                                   STKSEND,
                                   STKRECEIVE,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@USERID,
           @SCREENID,
           @VIEWRIGHTS,
           @INSERTRIGHTS,
           @UPDATERIGHTS,
           @DELETERIGHTS,
           @PRINTRIGHTS,
           @NULLRIGHTS,
           @CLOSERIGHTS,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSE_ACCESSDETRIGHTS: Cannot insert because primary key value not found in SE_ACCESSDETRIGHTS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSE_ACCESSDETRIGHTS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSE_ACCESSDETRIGHTS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSE_ACCESSDETRIGHTS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSE_ACCESSDETRIGHTS 
 */

CREATE PROCEDURE SP_UpdProcSE_ACCESSDETRIGHTS
(
    @USERID           UD_USERID,
    @SCREENID         UD_SCREENID,
    @VIEWRIGHTS       bit,
    @INSERTRIGHTS     bit,
    @UPDATERIGHTS     bit,
    @DELETERIGHTS     bit,
    @PRINTRIGHTS      bit,
    @NULLRIGHTS       bit,
    @CLOSERIGHTS      bit,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1               = NULL,
    @USER2            UD_USER2               = NULL,
    @USER3            UD_USER6               = NULL,
    @USER4            UD_USER7               = NULL,
    @TSTAMP           timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE SE_ACCESSDETRIGHTS
       SET VIEWRIGHTS        = @VIEWRIGHTS,
           INSERTRIGHTS      = @INSERTRIGHTS,
           UPDATERIGHTS      = @UPDATERIGHTS,
           DELETERIGHTS      = @DELETERIGHTS,
           PRINTRIGHTS       = @PRINTRIGHTS,
           NULLRIGHTS        = @NULLRIGHTS,
           CLOSERIGHTS       = @CLOSERIGHTS,
           STKNEW            = @STKNEW,
           STKMODIFY         = @STKMODIFY,
           STKDELETE         = @STKDELETE,
           STKSEND           = @STKSEND,
           STKRECEIVE        = @STKRECEIVE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE USERID   = @USERID
       AND SCREENID = @SCREENID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSE_ACCESSDETRIGHTS: Cannot update  in SE_ACCESSDETRIGHTS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSE_ACCESSDETRIGHTS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSE_ACCESSDETRIGHTS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSE_ACCESSDETRIGHTS >>>'
go


/* 
 * PROCEDURE: SP_DelProcSE_ACCESSDETRIGHTS 
 */

CREATE PROCEDURE SP_DelProcSE_ACCESSDETRIGHTS
(
    @USERID           UD_USERID,
    @SCREENID         UD_SCREENID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_ACCESSDETRIGHTS
     WHERE USERID   = @USERID
       AND SCREENID = @SCREENID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSE_ACCESSDETRIGHTS: Cannot delete because foreign keys still exist in SE_ACCESSDETRIGHTS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSE_ACCESSDETRIGHTS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSE_ACCESSDETRIGHTS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSE_ACCESSDETRIGHTS >>>'
go


/* 
 * PROCEDURE: SP_SelProcSE_ACCESSDETRIGHTS 
 */

CREATE PROCEDURE SP_SelProcSE_ACCESSDETRIGHTS
(
    @USERID           UD_USERID,
    @SCREENID         UD_SCREENID)
AS
BEGIN
    SELECT USERID,
           SCREENID,
           VIEWRIGHTS,
           INSERTRIGHTS,
           UPDATERIGHTS,
           DELETERIGHTS,
           PRINTRIGHTS,
           NULLRIGHTS,
           CLOSERIGHTS,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_ACCESSDETRIGHTS
     WHERE USERID   = @USERID
       AND SCREENID = @SCREENID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSE_ACCESSDETRIGHTS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSE_ACCESSDETRIGHTS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSE_ACCESSDETRIGHTS >>>'
go


/* 
 * PROCEDURE: SP_InsProcSE_GROUPREC 
 */

CREATE PROCEDURE SP_InsProcSE_GROUPREC
(
    @GROUPID        UD_USERID,
    @GROUPNAME      UD_NOMBRE,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1             = NULL,
    @USER2          UD_USER2             = NULL,
    @USER3          UD_USER6             = NULL,
    @USER4          UD_USER7             = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SE_GROUPREC(GROUPID,
                            GROUPNAME,
                            STKNEW,
                            STKMODIFY,
                            STKDELETE,
                            STKSEND,
                            STKRECEIVE,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@GROUPID,
           @GROUPNAME,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSE_GROUPREC: Cannot insert because primary key value not found in SE_GROUPREC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSE_GROUPREC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSE_GROUPREC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSE_GROUPREC >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSE_GROUPREC 
 */

CREATE PROCEDURE SP_UpdProcSE_GROUPREC
(
    @GROUPID        UD_USERID,
    @GROUPNAME      UD_NOMBRE,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1             = NULL,
    @USER2          UD_USER2             = NULL,
    @USER3          UD_USER6             = NULL,
    @USER4          UD_USER7             = NULL,
    @TSTAMP         timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE SE_GROUPREC
       SET GROUPNAME       = @GROUPNAME,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE GROUPID = @GROUPID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSE_GROUPREC: Cannot update  in SE_GROUPREC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSE_GROUPREC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSE_GROUPREC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSE_GROUPREC >>>'
go


/* 
 * PROCEDURE: SP_DelProcSE_GROUPREC 
 */

CREATE PROCEDURE SP_DelProcSE_GROUPREC
(
    @GROUPID        UD_USERID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_GROUPREC
     WHERE GROUPID = @GROUPID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSE_GROUPREC: Cannot delete because foreign keys still exist in SE_GROUPREC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSE_GROUPREC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSE_GROUPREC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSE_GROUPREC >>>'
go


/* 
 * PROCEDURE: SP_SelProcSE_GROUPREC 
 */

CREATE PROCEDURE SP_SelProcSE_GROUPREC
(
    @GROUPID        UD_USERID)
AS
BEGIN
    SELECT GROUPID,
           GROUPNAME,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_GROUPREC
     WHERE GROUPID = @GROUPID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSE_GROUPREC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSE_GROUPREC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSE_GROUPREC >>>'
go


/* 
 * PROCEDURE: SP_InsProcSE_MODULE 
 */

CREATE PROCEDURE SP_InsProcSE_MODULE
(
    @MODULEID       UD_MODULOID,
    @MODULENAME     UD_NOMBRE,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1               = NULL,
    @USER2          UD_USER2               = NULL,
    @USER3          UD_USER6               = NULL,
    @USER4          UD_USER7               = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SE_MODULE(MODULEID,
                          MODULENAME,
                          STKNEW,
                          STKMODIFY,
                          STKDELETE,
                          STKSEND,
                          STKRECEIVE,
                          USER1,
                          USER2,
                          USER3,
                          USER4 
                          )
    VALUES(@MODULEID,
           @MODULENAME,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSE_MODULE: Cannot insert because primary key value not found in SE_MODULE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSE_MODULE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSE_MODULE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSE_MODULE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSE_MODULE 
 */

CREATE PROCEDURE SP_UpdProcSE_MODULE
(
    @MODULEID       UD_MODULOID,
    @MODULENAME     UD_NOMBRE,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1               = NULL,
    @USER2          UD_USER2               = NULL,
    @USER3          UD_USER6               = NULL,
    @USER4          UD_USER7               = NULL,
    @TSTAMP         timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE SE_MODULE
       SET MODULENAME      = @MODULENAME,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE MODULEID = @MODULEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSE_MODULE: Cannot update  in SE_MODULE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSE_MODULE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSE_MODULE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSE_MODULE >>>'
go


/* 
 * PROCEDURE: SP_DelProcSE_MODULE 
 */

CREATE PROCEDURE SP_DelProcSE_MODULE
(
    @MODULEID       UD_MODULOID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_MODULE
     WHERE MODULEID = @MODULEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSE_MODULE: Cannot delete because foreign keys still exist in SE_MODULE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSE_MODULE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSE_MODULE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSE_MODULE >>>'
go


/* 
 * PROCEDURE: SP_SelProcSE_MODULE 
 */

CREATE PROCEDURE SP_SelProcSE_MODULE
(
    @MODULEID       UD_MODULOID)
AS
BEGIN
    SELECT MODULEID,
           MODULENAME,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_MODULE
     WHERE MODULEID = @MODULEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSE_MODULE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSE_MODULE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSE_MODULE >>>'
go


/* 
 * PROCEDURE: SP_InsProcSE_SCREEN 
 */

CREATE PROCEDURE SP_InsProcSE_SCREEN
(
    @SCREENID       UD_SCREENID,
    @MODULEID       UD_MODULOID,
    @SCREENNAME     UD_NOMBRE,
    @SCREENTYPE     char(1),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1               = NULL,
    @USER2          UD_USER2               = NULL,
    @USER3          UD_USER6               = NULL,
    @USER4          UD_USER7               = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SE_SCREEN(SCREENID,
                          MODULEID,
                          SCREENNAME,
                          SCREENTYPE,
                          STKNEW,
                          STKMODIFY,
                          STKDELETE,
                          STKSEND,
                          STKRECEIVE,
                          USER1,
                          USER2,
                          USER3,
                          USER4 
                          )
    VALUES(@SCREENID,
           @MODULEID,
           @SCREENNAME,
           @SCREENTYPE,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSE_SCREEN: Cannot insert because primary key value not found in SE_SCREEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSE_SCREEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSE_SCREEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSE_SCREEN >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSE_SCREEN 
 */

CREATE PROCEDURE SP_UpdProcSE_SCREEN
(
    @SCREENID       UD_SCREENID,
    @MODULEID       UD_MODULOID,
    @SCREENNAME     UD_NOMBRE,
    @SCREENTYPE     char(1),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1               = NULL,
    @USER2          UD_USER2               = NULL,
    @USER3          UD_USER6               = NULL,
    @USER4          UD_USER7               = NULL,
    @TSTAMP         timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE SE_SCREEN
       SET MODULEID        = @MODULEID,
           SCREENNAME      = @SCREENNAME,
           SCREENTYPE      = @SCREENTYPE,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SCREENID = @SCREENID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSE_SCREEN: Cannot update  in SE_SCREEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSE_SCREEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSE_SCREEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSE_SCREEN >>>'
go


/* 
 * PROCEDURE: SP_DelProcSE_SCREEN 
 */

CREATE PROCEDURE SP_DelProcSE_SCREEN
(
    @SCREENID       UD_SCREENID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_SCREEN
     WHERE SCREENID = @SCREENID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSE_SCREEN: Cannot delete because foreign keys still exist in SE_SCREEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSE_SCREEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSE_SCREEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSE_SCREEN >>>'
go


/* 
 * PROCEDURE: SP_SelProcSE_SCREEN 
 */

CREATE PROCEDURE SP_SelProcSE_SCREEN
(
    @SCREENID       UD_SCREENID)
AS
BEGIN
    SELECT SCREENID,
           MODULEID,
           SCREENNAME,
           SCREENTYPE,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_SCREEN
     WHERE SCREENID = @SCREENID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSE_SCREEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSE_SCREEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSE_SCREEN >>>'
go


/* 
 * PROCEDURE: SP_InsProcSE_USERGROUP 
 */

CREATE PROCEDURE SP_InsProcSE_USERGROUP
(
    @USERID         UD_USERID,
    @GROUPID        UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          char(30)             = NULL,
    @USER2          char(30)             = NULL,
    @USER3          float                = NULL,
    @USER4          float                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SE_USERGROUP(USERID,
                             GROUPID,
                             STKNEW,
                             STKMODIFY,
                             STKDELETE,
                             STKSEND,
                             STKRECEIVE,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@USERID,
           @GROUPID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSE_USERGROUP: Cannot insert because primary key value not found in SE_USERGROUP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSE_USERGROUP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSE_USERGROUP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSE_USERGROUP >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSE_USERGROUP 
 */

CREATE PROCEDURE SP_UpdProcSE_USERGROUP
(
    @USERID         UD_USERID,
    @GROUPID        UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          char(30)             = NULL,
    @USER2          char(30)             = NULL,
    @USER3          float                = NULL,
    @USER4          float                = NULL,
    @TSTAMP         timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE SE_USERGROUP
       SET STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE USERID  = @USERID
       AND GROUPID = @GROUPID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSE_USERGROUP: Cannot update  in SE_USERGROUP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSE_USERGROUP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSE_USERGROUP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSE_USERGROUP >>>'
go


/* 
 * PROCEDURE: SP_DelProcSE_USERGROUP 
 */

CREATE PROCEDURE SP_DelProcSE_USERGROUP
(
    @USERID         UD_USERID,
    @GROUPID        UD_USERID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_USERGROUP
     WHERE USERID  = @USERID
       AND GROUPID = @GROUPID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSE_USERGROUP: Cannot delete because foreign keys still exist in SE_USERGROUP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSE_USERGROUP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSE_USERGROUP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSE_USERGROUP >>>'
go


/* 
 * PROCEDURE: SP_SelProcSE_USERGROUP 
 */

CREATE PROCEDURE SP_SelProcSE_USERGROUP
(
    @USERID         UD_USERID,
    @GROUPID        UD_USERID)
AS
BEGIN
    SELECT USERID,
           GROUPID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_USERGROUP
     WHERE USERID  = @USERID
       AND GROUPID = @GROUPID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSE_USERGROUP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSE_USERGROUP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSE_USERGROUP >>>'
go


/* 
 * PROCEDURE: SP_InsProcSE_USERREC 
 */

CREATE PROCEDURE SP_InsProcSE_USERREC
(
    @USERID           UD_USERID,
    @USERNAME         UD_NOMBRE,
    @EMAILADDRESS     varchar(40)              = NULL,
    @LOCATION         varchar(40),
    @PASSWORD         char(12),
    @PHONE            char(30)                 = NULL,
    @EXPIRED          smalldatetime,
    @STATUS           bit,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                 = NULL,
    @USER2            UD_USER2                 = NULL,
    @USER3            UD_USER6                 = NULL,
    @USER4            UD_USER7                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SE_USERREC(USERID,
                           USERNAME,
                           EMAILADDRESS,
                           LOCATION,
                           PASSWORD,
                           PHONE,
                           EXPIRED,
                           STATUS,
                           STKNEW,
                           STKMODIFY,
                           STKDELETE,
                           STKSEND,
                           STKRECEIVE,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@USERID,
           @USERNAME,
           @EMAILADDRESS,
           @LOCATION,
           @PASSWORD,
           @PHONE,
           @EXPIRED,
           @STATUS,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSE_USERREC: Cannot insert because primary key value not found in SE_USERREC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSE_USERREC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSE_USERREC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSE_USERREC >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSE_USERREC 
 */

CREATE PROCEDURE SP_UpdProcSE_USERREC
(
    @USERID           UD_USERID,
    @USERNAME         UD_NOMBRE,
    @EMAILADDRESS     varchar(40)              = NULL,
    @LOCATION         varchar(40),
    @PASSWORD         char(12),
    @PHONE            char(30)                 = NULL,
    @EXPIRED          smalldatetime,
    @STATUS           bit,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1            UD_USER1                 = NULL,
    @USER2            UD_USER2                 = NULL,
    @USER3            UD_USER6                 = NULL,
    @USER4            UD_USER7                 = NULL,
    @TSTAMP           timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE SE_USERREC
       SET USERNAME          = @USERNAME,
           EMAILADDRESS      = @EMAILADDRESS,
           LOCATION          = @LOCATION,
           PASSWORD          = @PASSWORD,
           PHONE             = @PHONE,
           EXPIRED           = @EXPIRED,
           STATUS            = @STATUS,
           STKNEW            = @STKNEW,
           STKMODIFY         = @STKMODIFY,
           STKDELETE         = @STKDELETE,
           STKSEND           = @STKSEND,
           STKRECEIVE        = @STKRECEIVE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE USERID = @USERID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSE_USERREC: Cannot update  in SE_USERREC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSE_USERREC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSE_USERREC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSE_USERREC >>>'
go


/* 
 * PROCEDURE: SP_DelProcSE_USERREC 
 */

CREATE PROCEDURE SP_DelProcSE_USERREC
(
    @USERID           UD_USERID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_USERREC
     WHERE USERID = @USERID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSE_USERREC: Cannot delete because foreign keys still exist in SE_USERREC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSE_USERREC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSE_USERREC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSE_USERREC >>>'
go


/* 
 * PROCEDURE: SP_SelProcSE_USERREC 
 */

CREATE PROCEDURE SP_SelProcSE_USERREC
(
    @USERID           UD_USERID)
AS
BEGIN
    SELECT USERID,
           USERNAME,
           EMAILADDRESS,
           LOCATION,
           PASSWORD,
           PHONE,
           EXPIRED,
           STATUS,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_USERREC
     WHERE USERID = @USERID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSE_USERREC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSE_USERREC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSE_USERREC >>>'
go


/* 
 * PROCEDURE: SP_InsProcSETUP 
 */

CREATE PROCEDURE SP_InsProcSETUP
(
    @TYPEPRICEUPDATE        UD_PRECLVID,
    @CUSTID                 UD_CODCLIENT,
    @QTYSALESPERSONTURN     tinyint,
    @SALESPERSONTURN        tinyint,
    @QTYSIDE                tinyint,
    @SOURCESITE             UD_IDSITE,
    @CURYORIG               UD_CURYID,
    @FLOATPRICE             UD_CANTDECIMAL,
    @FLOATCOST              UD_CANTDECIMAL,
    @FLOATQTY               UD_CANTDECIMAL,
    @FLOATOUTPUT            UD_CANTDECIMAL,
    @TAXVALUEIGV            UD_TAXID,
    @TAXID                  UD_TAXID,
    @SOURCECURYTYPEID       UD_CURYTYPEID,
    @DATEPROCE              smalldatetime,
    @TYPECONTROLLERFUEL     char(1)                   = NULL,
    @CURYTYPEIDSALES        UD_CURYTYPEID,
    @CENTRAL                bit,
    @SENDPATH               UD_DESCRIPCION,
    @RECEPPATH              UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @USER3                  UD_USER3                  = NULL,
    @USER4                  UD_USER5                  = NULL,
    @USER5                  UD_USER5                  = NULL,
    @USER6                  UD_USER6                  = NULL,
    @USER7                  UD_USER7                  = NULL,
    @USER8                  UD_USER8                  = NULL,
    @USER9                  UD_USER9                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SETUP(TYPEPRICEUPDATE,
                      CUSTID,
                      QTYSALESPERSONTURN,
                      SALESPERSONTURN,
                      QTYSIDE,
                      SOURCESITE,
                      CURYORIG,
                      FLOATPRICE,
                      FLOATCOST,
                      FLOATQTY,
                      FLOATOUTPUT,
                      TAXVALUEIGV,
                      TAXID,
                      SOURCECURYTYPEID,
                      DATEPROCE,
                      TYPECONTROLLERFUEL,
                      CURYTYPEIDSALES,
                      CENTRAL,
                      SENDPATH,
                      RECEPPATH,
                      STKNEW,
                      STKMODIFY,
                      STKDELETE,
                      STKSEND,
                      STKRECEIVE,
                      USER1,
                      USER2,
                      USER3,
                      USER4,
                      USER5,
                      USER6,
                      USER7,
                      USER8,
                      USER9 
                      )
    VALUES(@TYPEPRICEUPDATE,
           @CUSTID,
           @QTYSALESPERSONTURN,
           @SALESPERSONTURN,
           @QTYSIDE,
           @SOURCESITE,
           @CURYORIG,
           @FLOATPRICE,
           @FLOATCOST,
           @FLOATQTY,
           @FLOATOUTPUT,
           @TAXVALUEIGV,
           @TAXID,
           @SOURCECURYTYPEID,
           @DATEPROCE,
           @TYPECONTROLLERFUEL,
           @CURYTYPEIDSALES,
           @CENTRAL,
           @SENDPATH,
           @RECEPPATH,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSETUP: Cannot insert because primary key value not found in SETUP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSETUP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSETUP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSETUP >>>'
go





/* 
 * PROCEDURE: SP_InsProcSETUP_GEN 
 */

CREATE PROCEDURE SP_InsProcSETUP_GEN
(
    @SITEID                 UD_IDSITE,
    @TYPEPRICEUPDATE        UD_PRECLVID,
    @CUSTID                 UD_CODCLIENT,
    @QTYSALESPERSONTURN     tinyint,
    @SALESPERSONTURN        tinyint,
    @QTYSIDE                tinyint,
    @SOURCESITE             UD_IDSITE,
    @CURYORIG               UD_CURYID,
    @FLOATPRICE             UD_CANTDECIMAL,
    @FLOATCOST              UD_CANTDECIMAL,
    @FLOATQTY               UD_CANTDECIMAL,
    @FLOATOUTPUT            UD_CANTDECIMAL,
    @TAXVALUEIGV            UD_TAXID,
    @TAXID                  UD_TAXID,
    @SOURCECURYTYPEID       UD_CURYTYPEID,
    @DATEPROCE              smalldatetime,
    @TYPECONTROLLERFUEL     char(1)                   = NULL,
    @CURYTYPEIDSALES        UD_CURYTYPEID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @CENTRAL                bit,
    @SENDPATH               UD_DESCRIPCION,
    @RECEPPATH              UD_DESCRIPCION,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @USER3                  UD_USER3                  = NULL,
    @USER4                  UD_USER5                  = NULL,
    @USER5                  UD_USER5                  = NULL,
    @USER6                  UD_USER6                  = NULL,
    @USER7                  UD_USER7                  = NULL,
    @USER8                  UD_USER8                  = NULL,
    @USER9                  UD_USER9                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SETUP_GEN(SITEID,
                          TYPEPRICEUPDATE,
                          CUSTID,
                          QTYSALESPERSONTURN,
                          SALESPERSONTURN,
                          QTYSIDE,
                          SOURCESITE,
                          CURYORIG,
                          FLOATPRICE,
                          FLOATCOST,
                          FLOATQTY,
                          FLOATOUTPUT,
                          TAXVALUEIGV,
                          TAXID,
                          SOURCECURYTYPEID,
                          DATEPROCE,
                          TYPECONTROLLERFUEL,
                          CURYTYPEIDSALES,
                          STKNEW,
                          STKMODIFY,
                          STKDELETE,
                          STKSEND,
                          STKRECEIVE,
                          CENTRAL,
                          SENDPATH,
                          RECEPPATH,
                          USER1,
                          USER2,
                          USER3,
                          USER4,
                          USER5,
                          USER6,
                          USER7,
                          USER8,
                          USER9 
                          )
    VALUES(@SITEID,
           @TYPEPRICEUPDATE,
           @CUSTID,
           @QTYSALESPERSONTURN,
           @SALESPERSONTURN,
           @QTYSIDE,
           @SOURCESITE,
           @CURYORIG,
           @FLOATPRICE,
           @FLOATCOST,
           @FLOATQTY,
           @FLOATOUTPUT,
           @TAXVALUEIGV,
           @TAXID,
           @SOURCECURYTYPEID,
           @DATEPROCE,
           @TYPECONTROLLERFUEL,
           @CURYTYPEIDSALES,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @CENTRAL,
           @SENDPATH,
           @RECEPPATH,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSETUP_GEN: Cannot insert because primary key value not found in SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSETUP_GEN 
 */

CREATE PROCEDURE SP_UpdProcSETUP_GEN
(
    @SITEID                 UD_IDSITE,
    @TYPEPRICEUPDATE        UD_PRECLVID,
    @CUSTID                 UD_CODCLIENT,
    @QTYSALESPERSONTURN     tinyint,
    @SALESPERSONTURN        tinyint,
    @QTYSIDE                tinyint,
    @SOURCESITE             UD_IDSITE,
    @CURYORIG               UD_CURYID,
    @FLOATPRICE             UD_CANTDECIMAL,
    @FLOATCOST              UD_CANTDECIMAL,
    @FLOATQTY               UD_CANTDECIMAL,
    @FLOATOUTPUT            UD_CANTDECIMAL,
    @TAXVALUEIGV            UD_TAXID,
    @TAXID                  UD_TAXID,
    @SOURCECURYTYPEID       UD_CURYTYPEID,
    @DATEPROCE              smalldatetime,
    @TYPECONTROLLERFUEL     char(1)                   = NULL,
    @CURYTYPEIDSALES        UD_CURYTYPEID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @CENTRAL                bit,
    @SENDPATH               UD_DESCRIPCION,
    @RECEPPATH              UD_DESCRIPCION,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @USER3                  UD_USER3                  = NULL,
    @USER4                  UD_USER5                  = NULL,
    @USER5                  UD_USER5                  = NULL,
    @USER6                  UD_USER6                  = NULL,
    @USER7                  UD_USER7                  = NULL,
    @USER8                  UD_USER8                  = NULL,
    @USER9                  UD_USER9                  = NULL,
    @TSTAMP                 timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE SETUP_GEN
       SET TYPEPRICEUPDATE         = @TYPEPRICEUPDATE,
           CUSTID                  = @CUSTID,
           QTYSALESPERSONTURN      = @QTYSALESPERSONTURN,
           SALESPERSONTURN         = @SALESPERSONTURN,
           QTYSIDE                 = @QTYSIDE,
           SOURCESITE              = @SOURCESITE,
           CURYORIG                = @CURYORIG,
           FLOATPRICE              = @FLOATPRICE,
           FLOATCOST               = @FLOATCOST,
           FLOATQTY                = @FLOATQTY,
           FLOATOUTPUT             = @FLOATOUTPUT,
           TAXVALUEIGV             = @TAXVALUEIGV,
           TAXID                   = @TAXID,
           SOURCECURYTYPEID        = @SOURCECURYTYPEID,
           DATEPROCE               = @DATEPROCE,
           TYPECONTROLLERFUEL      = @TYPECONTROLLERFUEL,
           CURYTYPEIDSALES         = @CURYTYPEIDSALES,
           STKNEW                  = @STKNEW,
           STKMODIFY               = @STKMODIFY,
           STKDELETE               = @STKDELETE,
           STKSEND                 = @STKSEND,
           STKRECEIVE              = @STKRECEIVE,
           CENTRAL                 = @CENTRAL,
           SENDPATH                = @SENDPATH,
           RECEPPATH               = @RECEPPATH,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4,
           USER5                   = @USER5,
           USER6                   = @USER6,
           USER7                   = @USER7,
           USER8                   = @USER8,
           USER9                   = @USER9
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSETUP_GEN: Cannot update  in SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_DelProcSETUP_GEN 
 */

CREATE PROCEDURE SP_DelProcSETUP_GEN
(
    @SITEID                 UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SETUP_GEN
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSETUP_GEN: Cannot delete because foreign keys still exist in SETUP_GEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_SelProcSETUP_GEN 
 */

CREATE PROCEDURE SP_SelProcSETUP_GEN
(
    @SITEID                 UD_IDSITE)
AS
BEGIN
    SELECT SITEID,
           TYPEPRICEUPDATE,
           CUSTID,
           QTYSALESPERSONTURN,
           SALESPERSONTURN,
           QTYSIDE,
           SOURCESITE,
           CURYORIG,
           FLOATPRICE,
           FLOATCOST,
           FLOATQTY,
           FLOATOUTPUT,
           TAXVALUEIGV,
           TAXID,
           SOURCECURYTYPEID,
           DATEPROCE,
           TYPECONTROLLERFUEL,
           CURYTYPEIDSALES,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           CENTRAL,
           SENDPATH,
           RECEPPATH,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM SETUP_GEN
     WHERE SITEID = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSETUP_GEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSETUP_GEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSETUP_GEN >>>'
go


/* 
 * PROCEDURE: SP_InsProcSH_RECEPTIONS 
 */

CREATE PROCEDURE SP_InsProcSH_RECEPTIONS
(
    @SITEID            char(5),
    @TABLEID           char(3),
    @RECEPTIONDATE     smalldatetime,
    @DATEPROCE         smalldatetime,
    @USERID            UD_USERID,
    @FILENAME          UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SH_RECEPTIONS(SITEID,
                              TABLEID,
                              RECEPTIONDATE,
                              DATEPROCE,
                              USERID,
                              FILENAME)
    VALUES(@SITEID,
           @TABLEID,
           @RECEPTIONDATE,
           @DATEPROCE,
           @USERID,
           @FILENAME)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSH_RECEPTIONS: Cannot insert because primary key value not found in SH_RECEPTIONS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSH_RECEPTIONS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSH_RECEPTIONS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSH_RECEPTIONS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSH_RECEPTIONS 
 */

CREATE PROCEDURE SP_UpdProcSH_RECEPTIONS
(
    @SITEID            char(5),
    @TABLEID           char(3),
    @RECEPTIONDATE     smalldatetime,
    @DATEPROCE         smalldatetime,
    @USERID            UD_USERID,
    @FILENAME          UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    UPDATE SH_RECEPTIONS
       SET DATEPROCE          = @DATEPROCE,
           USERID             = @USERID,
           FILENAME           = @FILENAME
     WHERE SITEID        = @SITEID
       AND TABLEID       = @TABLEID
       AND RECEPTIONDATE = @RECEPTIONDATE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSH_RECEPTIONS: Cannot update  in SH_RECEPTIONS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSH_RECEPTIONS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSH_RECEPTIONS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSH_RECEPTIONS >>>'
go


/* 
 * PROCEDURE: SP_DelProcSH_RECEPTIONS 
 */

CREATE PROCEDURE SP_DelProcSH_RECEPTIONS
(
    @SITEID            char(5),
    @TABLEID           char(3),
    @RECEPTIONDATE     smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SH_RECEPTIONS
     WHERE SITEID        = @SITEID
       AND TABLEID       = @TABLEID
       AND RECEPTIONDATE = @RECEPTIONDATE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSH_RECEPTIONS: Cannot delete because foreign keys still exist in SH_RECEPTIONS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSH_RECEPTIONS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSH_RECEPTIONS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSH_RECEPTIONS >>>'
go


/* 
 * PROCEDURE: SP_SelProcSH_RECEPTIONS 
 */

CREATE PROCEDURE SP_SelProcSH_RECEPTIONS
(
    @SITEID            char(5),
    @TABLEID           char(3),
    @RECEPTIONDATE     smalldatetime)
AS
BEGIN
    SELECT SITEID,
           TABLEID,
           RECEPTIONDATE,
           DATEPROCE,
           USERID,
           FILENAME
      FROM SH_RECEPTIONS
     WHERE SITEID        = @SITEID
       AND TABLEID       = @TABLEID
       AND RECEPTIONDATE = @RECEPTIONDATE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSH_RECEPTIONS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSH_RECEPTIONS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSH_RECEPTIONS >>>'
go


/* 
 * PROCEDURE: SP_InsProcSH_SHIPPINGS 
 */

CREATE PROCEDURE SP_InsProcSH_SHIPPINGS
(
    @SITEID        char(5),
    @TABLEID       char(3),
    @SHIPDATE      smalldatetime,
    @DATEPROCE     smalldatetime,
    @USERID        UD_USERID,
    @FILENAME      UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SH_SHIPPINGS(SITEID,
                             TABLEID,
                             SHIPDATE,
                             DATEPROCE,
                             USERID,
                             FILENAME)
    VALUES(@SITEID,
           @TABLEID,
           @SHIPDATE,
           @DATEPROCE,
           @USERID,
           @FILENAME)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSH_SHIPPINGS: Cannot insert because primary key value not found in SH_SHIPPINGS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSH_SHIPPINGS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSH_SHIPPINGS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSH_SHIPPINGS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSH_SHIPPINGS 
 */

CREATE PROCEDURE SP_UpdProcSH_SHIPPINGS
(
    @SITEID        char(5),
    @TABLEID       char(3),
    @SHIPDATE      smalldatetime,
    @DATEPROCE     smalldatetime,
    @USERID        UD_USERID,
    @FILENAME      UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    UPDATE SH_SHIPPINGS
       SET SHIPDATE       = @SHIPDATE,
           DATEPROCE      = @DATEPROCE,
           USERID         = @USERID
     WHERE SITEID   = @SITEID
       AND TABLEID  = @TABLEID
       AND FILENAME = @FILENAME

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSH_SHIPPINGS: Cannot update  in SH_SHIPPINGS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSH_SHIPPINGS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSH_SHIPPINGS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSH_SHIPPINGS >>>'
go


/* 
 * PROCEDURE: SP_DelProcSH_SHIPPINGS 
 */

CREATE PROCEDURE SP_DelProcSH_SHIPPINGS
(
    @SITEID        char(5),
    @TABLEID       char(3),
    @FILENAME      UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SH_SHIPPINGS
     WHERE SITEID   = @SITEID
       AND TABLEID  = @TABLEID
       AND FILENAME = @FILENAME

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSH_SHIPPINGS: Cannot delete because foreign keys still exist in SH_SHIPPINGS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSH_SHIPPINGS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSH_SHIPPINGS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSH_SHIPPINGS >>>'
go


/* 
 * PROCEDURE: SP_SelProcSH_SHIPPINGS 
 */

CREATE PROCEDURE SP_SelProcSH_SHIPPINGS
(
    @SITEID        char(5),
    @TABLEID       char(3),
    @FILENAME      UD_DESCRIPCION)
AS
BEGIN
    SELECT SITEID,
           TABLEID,
           SHIPDATE,
           DATEPROCE,
           USERID,
           FILENAME
      FROM SH_SHIPPINGS
     WHERE SITEID   = @SITEID
       AND TABLEID  = @TABLEID
       AND FILENAME = @FILENAME

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSH_SHIPPINGS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSH_SHIPPINGS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSH_SHIPPINGS >>>'
go


/* 
 * PROCEDURE: SP_InsProcSH_SHIPPINGTABLES 
 */

CREATE PROCEDURE SP_InsProcSH_SHIPPINGTABLES
(
    @TABLEID                UD_TABLEID,
    @TABLENAME              UD_DESCRIPCION,
    @SEQUENCE               smallint,
    @TYPE                   char(1),
    @MODULEID               char(2),
    @DATEFIELD              UD_DESCRIPCION            = NULL,
    @CENTRAL_TO_STATION     bit,
    @STATION_TO_CENTRAL     bit,
    @PERSITE                bit,
    @COMMENTS               varchar(40)               = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE             bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SH_SHIPPINGTABLES(TABLEID,
                                  TABLENAME,
                                  SEQUENCE,
                                  TYPE,
                                  MODULEID,
                                  DATEFIELD,
                                  CENTRAL_TO_STATION,
                                  STATION_TO_CENTRAL,
                                  PERSITE,
                                  COMMENTS,
                                  STKNEW,
                                  STKMODIFY,
                                  STKDELETE,
                                  STKSEND,
                                  STKRECEIVE)
    VALUES(@TABLEID,
           @TABLENAME,
           @SEQUENCE,
           @TYPE,
           @MODULEID,
           @DATEFIELD,
           @CENTRAL_TO_STATION,
           @STATION_TO_CENTRAL,
           @PERSITE,
           @COMMENTS,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSH_SHIPPINGTABLES: Cannot insert because primary key value not found in SH_SHIPPINGTABLES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSH_SHIPPINGTABLES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSH_SHIPPINGTABLES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSH_SHIPPINGTABLES >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSH_SHIPPINGTABLES 
 */

CREATE PROCEDURE SP_UpdProcSH_SHIPPINGTABLES
(
    @TABLEID                UD_TABLEID,
    @TABLENAME              UD_DESCRIPCION,
    @SEQUENCE               smallint,
    @TYPE                   char(1),
    @MODULEID               char(2),
    @DATEFIELD              UD_DESCRIPCION            = NULL,
    @CENTRAL_TO_STATION     bit,
    @STATION_TO_CENTRAL     bit,
    @PERSITE                bit,
    @COMMENTS               varchar(40)               = NULL,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE             bit)
AS
BEGIN
    BEGIN TRAN

    UPDATE SH_SHIPPINGTABLES
       SET TABLENAME               = @TABLENAME,
           SEQUENCE                = @SEQUENCE,
           TYPE                    = @TYPE,
           MODULEID                = @MODULEID,
           DATEFIELD               = @DATEFIELD,
           CENTRAL_TO_STATION      = @CENTRAL_TO_STATION,
           STATION_TO_CENTRAL      = @STATION_TO_CENTRAL,
           PERSITE                 = @PERSITE,
           COMMENTS                = @COMMENTS,
           STKNEW                  = @STKNEW,
           STKMODIFY               = @STKMODIFY,
           STKDELETE               = @STKDELETE,
           STKSEND                 = @STKSEND,
           STKRECEIVE              = @STKRECEIVE
     WHERE TABLEID = @TABLEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSH_SHIPPINGTABLES: Cannot update  in SH_SHIPPINGTABLES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSH_SHIPPINGTABLES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSH_SHIPPINGTABLES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSH_SHIPPINGTABLES >>>'
go


/* 
 * PROCEDURE: SP_DelProcSH_SHIPPINGTABLES 
 */

CREATE PROCEDURE SP_DelProcSH_SHIPPINGTABLES
(
    @TABLEID                UD_TABLEID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SH_SHIPPINGTABLES
     WHERE TABLEID = @TABLEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSH_SHIPPINGTABLES: Cannot delete because foreign keys still exist in SH_SHIPPINGTABLES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSH_SHIPPINGTABLES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSH_SHIPPINGTABLES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSH_SHIPPINGTABLES >>>'
go


/* 
 * PROCEDURE: SP_SelProcSH_SHIPPINGTABLES 
 */

CREATE PROCEDURE SP_SelProcSH_SHIPPINGTABLES
(
    @TABLEID                UD_TABLEID)
AS
BEGIN
    SELECT TABLEID,
           TABLENAME,
           SEQUENCE,
           TYPE,
           MODULEID,
           DATEFIELD,
           CENTRAL_TO_STATION,
           STATION_TO_CENTRAL,
           PERSITE,
           COMMENTS,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE
      FROM SH_SHIPPINGTABLES
     WHERE TABLEID = @TABLEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSH_SHIPPINGTABLES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSH_SHIPPINGTABLES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSH_SHIPPINGTABLES >>>'
go


/* 
 * PROCEDURE: SP_InsProcSITE 
 */

CREATE PROCEDURE SP_InsProcSITE
(
    @SITEID             UD_IDSITE,
    @CITYID             char(3),
    @COUNTRYID          char(3),
    @NAME               UD_DESCRIPCION,
    @ADDR1              UD_DIRECCION,
    @ADDR2              UD_DIRECCION              = NULL,
    @FONO1              UD_TELEFONO               = NULL,
    @FONO2              UD_TELEFONO               = NULL,
    @FAX                UD_TELEFONO               = NULL,
    @STATUS             bit,
    @STARTDATEINVEN     smalldatetime             = NULL,
    @ENDDATEINVEN       smalldatetime             = NULL,
    @ADJUST             bit,
    @ATTN               varchar(40),
    @USERID             UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                  = NULL,
    @USER2              UD_USER2                  = NULL,
    @USER3              UD_USER3                  = NULL,
    @USER4              UD_USER5                  = NULL,
    @USER5              UD_USER5                  = NULL,
    @USER6              UD_USER6                  = NULL,
    @USER7              UD_USER7                  = NULL,
    @USER8              UD_USER8                  = NULL,
    @USER9              UD_USER9                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SITE(SITEID,
                     CITYID,
                     COUNTRYID,
                     NAME,
                     ADDR1,
                     ADDR2,
                     FONO1,
                     FONO2,
                     FAX,
                     STATUS,
                     STARTDATEINVEN,
                     ENDDATEINVEN,
                     ADJUST,
                     ATTN,
                     USERID,
                     STKNEW,
                     STKMODIFY,
                     STKDELETE,
                     STKSEND,
                     STKRECEIVE,
                     USER1,
                     USER2,
                     USER3,
                     USER4,
                     USER5,
                     USER6,
                     USER7,
                     USER8,
                     USER9 
                     )
    VALUES(@SITEID,
           @CITYID,
           @COUNTRYID,
           @NAME,
           @ADDR1,
           @ADDR2,
           @FONO1,
           @FONO2,
           @FAX,
           @STATUS,
           @STARTDATEINVEN,
           @ENDDATEINVEN,
           @ADJUST,
           @ATTN,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSITE: Cannot insert because primary key value not found in SITE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSITE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSITE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSITE 
 */

CREATE PROCEDURE SP_UpdProcSITE
(
    @SITEID             UD_IDSITE,
    @CITYID             char(3),
    @COUNTRYID          char(3),
    @NAME               UD_DESCRIPCION,
    @ADDR1              UD_DIRECCION,
    @ADDR2              UD_DIRECCION              = NULL,
    @FONO1              UD_TELEFONO               = NULL,
    @FONO2              UD_TELEFONO               = NULL,
    @FAX                UD_TELEFONO               = NULL,
    @STATUS             bit,
    @STARTDATEINVEN     smalldatetime             = NULL,
    @ENDDATEINVEN       smalldatetime             = NULL,
    @ADJUST             bit,
    @ATTN               varchar(40),
    @USERID             UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER1              UD_USER1                  = NULL,
    @USER2              UD_USER2                  = NULL,
    @USER3              UD_USER3                  = NULL,
    @USER4              UD_USER5                  = NULL,
    @USER5              UD_USER5                  = NULL,
    @USER6              UD_USER6                  = NULL,
    @USER7              UD_USER7                  = NULL,
    @USER8              UD_USER8                  = NULL,
    @USER9              UD_USER9                  = NULL,
    @TSTAMP             timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE SITE
       SET CITYID              = @CITYID,
           COUNTRYID           = @COUNTRYID,
           NAME                = @NAME,
           ADDR1               = @ADDR1,
           ADDR2               = @ADDR2,
           FONO1               = @FONO1,
           FONO2               = @FONO2,
           FAX                 = @FAX,
           STATUS              = @STATUS,
           STARTDATEINVEN      = @STARTDATEINVEN,
           ENDDATEINVEN        = @ENDDATEINVEN,
           ADJUST              = @ADJUST,
           ATTN                = @ATTN,
           USERID              = @USERID,
           STKNEW              = @STKNEW,
           STKMODIFY           = @STKMODIFY,
           STKDELETE           = @STKDELETE,
           STKSEND             = @STKSEND,
           STKRECEIVE          = @STKRECEIVE,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4,
           USER5               = @USER5,
           USER6               = @USER6,
           USER7               = @USER7,
           USER8               = @USER8,
           USER9               = @USER9
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSITE: Cannot update  in SITE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSITE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSITE >>>'
go


/* 
 * PROCEDURE: SP_DelProcSITE 
 */

CREATE PROCEDURE SP_DelProcSITE
(
    @SITEID             UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SITE
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSITE: Cannot delete because foreign keys still exist in SITE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSITE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSITE >>>'
go


/* 
 * PROCEDURE: SP_SelProcSITE 
 */

CREATE PROCEDURE SP_SelProcSITE
(
    @SITEID             UD_IDSITE)
AS
BEGIN
    SELECT SITEID,
           CITYID,
           COUNTRYID,
           NAME,
           ADDR1,
           ADDR2,
           FONO1,
           FONO2,
           FAX,
           STATUS,
           STARTDATEINVEN,
           ENDDATEINVEN,
           ADJUST,
           ATTN,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM SITE
     WHERE SITEID = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSITE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSITE >>>'
go


/* 
 * PROCEDURE: SP_InsProcSTATE 
 */

CREATE PROCEDURE SP_InsProcSTATE
(
    @STATEID        char(2),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO STATE(STATEID,
                      DESCR,
                      STKNEW,
                      STKMODIFY,
                      STKDELETE,
                      STKSEND,
                      STKRECEIVE 
                      )
    VALUES(@STATEID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSTATE: Cannot insert because primary key value not found in STATE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcSTATE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcSTATE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcSTATE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcSTATE 
 */

CREATE PROCEDURE SP_UpdProcSTATE
(
    @STATEID        char(2),
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE STATE
       SET DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE
     WHERE STATEID = @STATEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcSTATE: Cannot update  in STATE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcSTATE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcSTATE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcSTATE >>>'
go


/* 
 * PROCEDURE: SP_DelProcSTATE 
 */

CREATE PROCEDURE SP_DelProcSTATE
(
    @STATEID        char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM STATE
     WHERE STATEID = @STATEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcSTATE: Cannot delete because foreign keys still exist in STATE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcSTATE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcSTATE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcSTATE >>>'
go


/* 
 * PROCEDURE: SP_SelProcSTATE 
 */

CREATE PROCEDURE SP_SelProcSTATE
(
    @STATEID        char(2))
AS
BEGIN
    SELECT STATEID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           TSTAMP
      FROM STATE
     WHERE STATEID = @STATEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcSTATE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcSTATE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcSTATE >>>'
go


/* 
 * PROCEDURE: SP_InsProcTAXS 
 */

CREATE PROCEDURE SP_InsProcTAXS
(
    @TAXID          UD_TAXID,
    @DESCR          UD_DESCRIPCION,
    @VALUE          numeric(5, 2),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TAXS(TAXID,
                     DESCR,
                     VALUE,
                     STKNEW,
                     STKMODIFY,
                     STKDELETE,
                     STKSEND,
                     STKRECEIVE,
                     USER1,
                     USER2,
                     USER3,
                     USER4 
                     )
    VALUES(@TAXID,
           @DESCR,
           @VALUE,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTAXS: Cannot insert because primary key value not found in TAXS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTAXS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTAXS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTAXS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTAXS 
 */

CREATE PROCEDURE SP_UpdProcTAXS
(
    @TAXID          UD_TAXID,
    @DESCR          UD_DESCRIPCION,
    @VALUE          numeric(5, 2),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TAXS
       SET DESCR           = @DESCR,
           VALUE           = @VALUE,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE TAXID = @TAXID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTAXS: Cannot update  in TAXS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTAXS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTAXS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTAXS >>>'
go


/* 
 * PROCEDURE: SP_DelProcTAXS 
 */

CREATE PROCEDURE SP_DelProcTAXS
(
    @TAXID          UD_TAXID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TAXS
     WHERE TAXID = @TAXID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTAXS: Cannot delete because foreign keys still exist in TAXS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTAXS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTAXS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTAXS >>>'
go


/* 
 * PROCEDURE: SP_SelProcTAXS 
 */

CREATE PROCEDURE SP_SelProcTAXS
(
    @TAXID          UD_TAXID)
AS
BEGIN
    SELECT TAXID,
           DESCR,
           VALUE,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM TAXS
     WHERE TAXID = @TAXID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTAXS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTAXS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTAXS >>>'
go


/* 
 * PROCEDURE: SP_InsProcTERMS 
 */

CREATE PROCEDURE SP_InsProcTERMS
(
    @TERMID         char(2),
    @DUEINTRV       tinyint,
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TERMS(TERMID,
                      DUEINTRV,
                      DESCR,
                      STKNEW,
                      STKMODIFY,
                      STKDELETE,
                      STKSEND,
                      STKRECEIVE,
                      USER1,
                      USER2,
                      USER3,
                      USER4 
                      )
    VALUES(@TERMID,
           @DUEINTRV,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTERMS: Cannot insert because primary key value not found in TERMS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTERMS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTERMS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTERMS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTERMS 
 */

CREATE PROCEDURE SP_UpdProcTERMS
(
    @TERMID         char(2),
    @DUEINTRV       tinyint,
    @DESCR          UD_DESCRIPCION,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE     bit,
    @USER1          UD_USER1                  = NULL,
    @USER2          UD_USER2                  = NULL,
    @USER3          UD_USER6                  = NULL,
    @USER4          UD_USER7                  = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TERMS
       SET DUEINTRV        = @DUEINTRV,
           DESCR           = @DESCR,
           STKNEW          = @STKNEW,
           STKMODIFY       = @STKMODIFY,
           STKDELETE       = @STKDELETE,
           STKSEND         = @STKSEND,
           STKRECEIVE      = @STKRECEIVE,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE TERMID = @TERMID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTERMS: Cannot update  in TERMS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTERMS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTERMS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTERMS >>>'
go


/* 
 * PROCEDURE: SP_DelProcTERMS 
 */

CREATE PROCEDURE SP_DelProcTERMS
(
    @TERMID         char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TERMS
     WHERE TERMID = @TERMID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTERMS: Cannot delete because foreign keys still exist in TERMS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTERMS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTERMS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTERMS >>>'
go


/* 
 * PROCEDURE: SP_SelProcTERMS 
 */

CREATE PROCEDURE SP_SelProcTERMS
(
    @TERMID         char(2))
AS
BEGIN
    SELECT TERMID,
           DUEINTRV,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM TERMS
     WHERE TERMID = @TERMID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTERMS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTERMS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTERMS >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_AR_ALMACENES 
 */

CREATE PROCEDURE SP_InsProcTMP_AR_ALMACENES
(
    @CORRNBR     int,
    @SITEID      UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_AR_ALMACENES(CORRNBR,
                                 SITEID)
    VALUES(@CORRNBR,
           @SITEID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_AR_ALMACENES: Cannot insert because primary key value not found in TMP_AR_ALMACENES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_AR_ALMACENES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_AR_ALMACENES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_AR_ALMACENES >>>'
go



/* 
 * PROCEDURE: SP_DelProcTMP_AR_ALMACENES 
 */

CREATE PROCEDURE SP_DelProcTMP_AR_ALMACENES
(
    @CORRNBR     int,
    @SITEID      UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_AR_ALMACENES
     WHERE CORRNBR = @CORRNBR
       AND SITEID  = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_AR_ALMACENES: Cannot delete because foreign keys still exist in TMP_AR_ALMACENES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_AR_ALMACENES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_AR_ALMACENES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_AR_ALMACENES >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_AR_ALMACENES 
 */

CREATE PROCEDURE SP_SelProcTMP_AR_ALMACENES
(
    @CORRNBR     int,
    @SITEID      UD_IDSITE)
AS
BEGIN
    SELECT CORRNBR,
           SITEID
      FROM TMP_AR_ALMACENES
     WHERE CORRNBR = @CORRNBR
       AND SITEID  = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_AR_ALMACENES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_AR_ALMACENES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_AR_ALMACENES >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_AR_RESUMEN 
 */

CREATE PROCEDURE SP_InsProcTMP_AR_RESUMEN
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @SEQUENCE           smallint,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             UD_IDSITE,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DOCSTATUSID        char(2),
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERPOST            UD_PERPOST,
    @STKCLOSEDZ         bit,
    @STKCLOSEDX         bit,
    @NBRTURN            tinyint,
    @NBRSIDE            UD_CARA                     = NULL,
    @NBRTRANSACFUEL     char(4)                     = NULL,
    @DISCDSCTO1         numeric(5, 2),
    @DISCDSCTO2         numeric(5, 2),
    @DISCMTOPEN         numeric(11, 4),
    @DISCMTOUSD         numeric(11, 4),
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(18, 4),
    @TOTALUSD           numeric(18, 4),
    @TOTALPENORI        numeric(18, 4),
    @TOTALUSDORI        numeric(18, 4),
    @TAXPEN             numeric(18, 4),
    @TAXUSD             numeric(18, 4),
    @TAXPENORI          numeric(18, 4),
    @TAXUSDORI          numeric(18, 4),
    @SUBTOTALPEN        numeric(18, 4),
    @SUBTOTALUSD        numeric(18, 4),
    @NOAFECTOPEN        numeric(18, 4),
    @NOAFECTOUSD        numeric(18, 4),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                numeric(10, 3),
    @USERID             UD_USERID                   = NULL,
    @KIT                bit                         = NULL,
    @COMBUSTIBLE        bit                         = NULL,
    @PTOVTA             UD_PTOVTA                   = NULL,
    @STKUNITID          UD_STKUNITID)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_AR_RESUMEN(CORRNBR,
                               DOCTYPEID,
                               SEQUENCE,
                               NBRDOCUMENT,
                               SITEID,
                               INVTIDSKU,
                               CURYID,
                               DOCSTATUSID,
                               DATEDOC,
                               DATEPROCESALES,
                               PERPOST,
                               STKCLOSEDZ,
                               STKCLOSEDX,
                               NBRTURN,
                               NBRSIDE,
                               NBRTRANSACFUEL,
                               DISCDSCTO1,
                               DISCDSCTO2,
                               DISCMTOPEN,
                               DISCMTOUSD,
                               PORCENTTAXIGV,
                               PORCENTTAXISC,
                               TOTALPEN,
                               TOTALUSD,
                               TOTALPENORI,
                               TOTALUSDORI,
                               TAXPEN,
                               TAXUSD,
                               TAXPENORI,
                               TAXUSDORI,
                               SUBTOTALPEN,
                               SUBTOTALUSD,
                               NOAFECTOPEN,
                               NOAFECTOUSD,
                               STKITEM,
                               STKFISI,
                               SLS_PRICE,
                               SLSPRICESALE,
                               STDCOSTPEN,
                               STDCOSTUSD,
                               INVTIDALTER,
                               DESCRINVENTORY,
                               QTY,
                               USERID,
                               KIT,
                               COMBUSTIBLE,
                               PTOVTA,
                               STKUNITID)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @SEQUENCE,
           @NBRDOCUMENT,
           @SITEID,
           @INVTIDSKU,
           @CURYID,
           @DOCSTATUSID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @NBRTURN,
           @NBRSIDE,
           @NBRTRANSACFUEL,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTOPEN,
           @DISCMTOUSD,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TOTALPENORI,
           @TOTALUSDORI,
           @TAXPEN,
           @TAXUSD,
           @TAXPENORI,
           @TAXUSDORI,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @NOAFECTOPEN,
           @NOAFECTOUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @USERID,
           @KIT,
           @COMBUSTIBLE,
           @PTOVTA,
           @STKUNITID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_AR_RESUMEN: Cannot insert because primary key value not found in TMP_AR_RESUMEN '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_AR_RESUMEN') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_AR_RESUMEN >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_AR_RESUMEN >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_AR_SALES 
 */

CREATE PROCEDURE SP_InsProcTMP_AR_SALES
(
    @CORRNBR               int,
    @DOCTYPEID             UD_DOCTYPEID,
    @TIPODOCUMENTO         UD_DESCRIPCION,
    @SITEID                UD_IDSITE,
    @NAME                  UD_DESCRIPCION,
    @NBRDOCUMENT           numeric(10, 0),
    @DATEPROCESALES        smalldatetime,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PLACA                 UD_PLACA,
    @SELECTED              smallint)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_AR_SALES(CORRNBR,
                             DOCTYPEID,
                             TIPODOCUMENTO,
                             SITEID,
                             NAME,
                             NBRDOCUMENT,
                             DATEPROCESALES,
                             TOTALPEN,
                             TOTALUSD,
                             SUBTOTALPEN,
                             SUBTOTALUSD,
                             TAXIGVPEN,
                             TAXIGVUSD,
                             TOTALNOTAFFECTPEN,
                             TOTALNOTAFFECTUSD,
                             PLACA,
                             SELECTED)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @TIPODOCUMENTO,
           @SITEID,
           @NAME,
           @NBRDOCUMENT,
           @DATEPROCESALES,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAXIGVPEN,
           @TAXIGVUSD,
           @TOTALNOTAFFECTPEN,
           @TOTALNOTAFFECTUSD,
           @PLACA,
           @SELECTED)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_AR_SALES: Cannot insert because primary key value not found in TMP_AR_SALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_AR_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_AR_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_AR_SALES >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_AR_SALESDET 
 */

CREATE PROCEDURE SP_InsProcTMP_AR_SALESDET
(
    @CORRNBR             int,
    @SITEID              UD_IDSITE,
    @INVTIDSKU           UD_IDCORPORATIVO,
    @NAME                UD_DESCRIPCION              = NULL,
    @DESCR               UD_DESCR_INVENT             = NULL,
    @QTY                 numeric(10, 3),
    @SLSPRICESALEORI     numeric(8, 4),
    @SLSPRICESALE        numeric(8, 4),
    @TOTALPEN            numeric(11, 4),
    @TOTALPENORI         numeric(11, 4),
    @TOTALUSD            numeric(11, 4),
    @TOTALUSDORI         numeric(11, 4),
    @TAXPEN              numeric(11, 4),
    @TAXPENORI           numeric(11, 4),
    @TAXUSD              numeric(11, 4),
    @TAXUSORI            numeric(11, 4),
    @SUBTOTALPEN         numeric(11, 4),
    @SUBTOTALUSD         numeric(11, 4),
    @NOAFECTOPEN         numeric(11, 4),
    @NOAFECTOUSD         numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_AR_SALESDET(CORRNBR,
                                SITEID,
                                INVTIDSKU,
                                NAME,
                                DESCR,
                                QTY,
                                SLSPRICESALEORI,
                                SLSPRICESALE,
                                TOTALPEN,
                                TOTALPENORI,
                                TOTALUSD,
                                TOTALUSDORI,
                                TAXPEN,
                                TAXPENORI,
                                TAXUSD,
                                TAXUSORI,
                                SUBTOTALPEN,
                                SUBTOTALUSD,
                                NOAFECTOPEN,
                                NOAFECTOUSD)
    VALUES(@CORRNBR,
           @SITEID,
           @INVTIDSKU,
           @NAME,
           @DESCR,
           @QTY,
           @SLSPRICESALEORI,
           @SLSPRICESALE,
           @TOTALPEN,
           @TOTALPENORI,
           @TOTALUSD,
           @TOTALUSDORI,
           @TAXPEN,
           @TAXPENORI,
           @TAXUSD,
           @TAXUSORI,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @NOAFECTOPEN,
           @NOAFECTOUSD)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_AR_SALESDET: Cannot insert because primary key value not found in TMP_AR_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_AR_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_AR_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_AR_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_AR_SALESDET 
 */

CREATE PROCEDURE SP_UpdProcTMP_AR_SALESDET
(
    @CORRNBR             int,
    @SITEID              UD_IDSITE,
    @INVTIDSKU           UD_IDCORPORATIVO,
    @NAME                UD_DESCRIPCION              = NULL,
    @DESCR               UD_DESCR_INVENT             = NULL,
    @QTY                 numeric(10, 3),
    @SLSPRICESALEORI     numeric(8, 4),
    @SLSPRICESALE        numeric(8, 4),
    @TOTALPEN            numeric(11, 4),
    @TOTALPENORI         numeric(11, 4),
    @TOTALUSD            numeric(11, 4),
    @TOTALUSDORI         numeric(11, 4),
    @TAXPEN              numeric(11, 4),
    @TAXPENORI           numeric(11, 4),
    @TAXUSD              numeric(11, 4),
    @TAXUSORI            numeric(11, 4),
    @SUBTOTALPEN         numeric(11, 4),
    @SUBTOTALUSD         numeric(11, 4),
    @NOAFECTOPEN         numeric(11, 4),
    @NOAFECTOUSD         numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_AR_SALESDET
       SET NAME                 = @NAME,
           DESCR                = @DESCR,
           QTY                  = @QTY,
           SLSPRICESALEORI      = @SLSPRICESALEORI,
           SLSPRICESALE         = @SLSPRICESALE,
           TOTALPEN             = @TOTALPEN,
           TOTALPENORI          = @TOTALPENORI,
           TOTALUSD             = @TOTALUSD,
           TOTALUSDORI          = @TOTALUSDORI,
           TAXPEN               = @TAXPEN,
           TAXPENORI            = @TAXPENORI,
           TAXUSD               = @TAXUSD,
           TAXUSORI             = @TAXUSORI,
           SUBTOTALPEN          = @SUBTOTALPEN,
           SUBTOTALUSD          = @SUBTOTALUSD,
           NOAFECTOPEN          = @NOAFECTOPEN,
           NOAFECTOUSD          = @NOAFECTOUSD
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_AR_SALESDET: Cannot update  in TMP_AR_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_AR_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_AR_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_AR_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_AR_SALESDET 
 */

CREATE PROCEDURE SP_DelProcTMP_AR_SALESDET
(
    @CORRNBR             int,
    @SITEID              UD_IDSITE,
    @INVTIDSKU           UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_AR_SALESDET
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_AR_SALESDET: Cannot delete because foreign keys still exist in TMP_AR_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_AR_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_AR_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_AR_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_AR_SALESDET 
 */

CREATE PROCEDURE SP_SelProcTMP_AR_SALESDET
(
    @CORRNBR             int,
    @SITEID              UD_IDSITE,
    @INVTIDSKU           UD_IDCORPORATIVO)
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           INVTIDSKU,
           NAME,
           DESCR,
           QTY,
           SLSPRICESALEORI,
           SLSPRICESALE,
           TOTALPEN,
           TOTALPENORI,
           TOTALUSD,
           TOTALUSDORI,
           TAXPEN,
           TAXPENORI,
           TAXUSD,
           TAXUSORI,
           SUBTOTALPEN,
           SUBTOTALUSD,
           NOAFECTOPEN,
           NOAFECTOUSD
      FROM TMP_AR_SALESDET
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_AR_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_AR_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_AR_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_CORRNBRHEAD 
 */

CREATE PROCEDURE SP_InsProcTMP_CORRNBRHEAD
(
    @USERID      UD_USERID)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_CORRNBRHEAD(USERID)
    VALUES(@USERID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_CORRNBRHEAD: Cannot insert because primary key value not found in TMP_CORRNBRHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_CORRNBRHEAD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_CORRNBRHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_CORRNBRHEAD >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_CTO_PROM 
 */

CREATE PROCEDURE SP_InsProcTMP_CTO_PROM
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @NAME                 UD_DESCRIPCION,
    @CLASSID              UD_CATEGORY,
    @DESCR                UD_DESCRIPCION,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @DESCRARTICULO        UD_DESCRIPCION,
    @INICIAL              numeric(10, 3),
    @INICIALVAL           numeric(10, 4),
    @COMPRAS              numeric(10, 3),
    @AJUSTEINGRESO        numeric(10, 3),
    @BONIFICACION         numeric(10, 3),
    @TRANSFERING          numeric(10, 3),
    @OTROSING             numeric(10, 3),
    @VENTAS               numeric(10, 3),
    @AJUSTESAL            numeric(10, 3),
    @NOTACRED             numeric(10, 3),
    @TRANSFERSAL          numeric(10, 3),
    @OTRASSAL             numeric(10, 3),
    @COMPRASVAL           numeric(10, 4),
    @AJUSTEINGVALO        numeric(10, 4),
    @BONIFIVAL            numeric(10, 4),
    @TRANSINGVAL          numeric(10, 4),
    @OTROSINGVAL          numeric(10, 4),
    @VENTASVALOR          numeric(10, 4),
    @AJUSTESALIVAL        numeric(10, 4),
    @NOTACREDVAL          numeric(10, 4),
    @TRANSFERSALVALOR     numeric(10, 4),
    @OTROSSALVALOR        numeric(10, 4))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_CTO_PROM(CORRNBR,
                             SITEID,
                             NAME,
                             CLASSID,
                             DESCR,
                             INVTIDSKU,
                             DESCRARTICULO,
                             INICIAL,
                             INICIALVAL,
                             COMPRAS,
                             AJUSTEINGRESO,
                             BONIFICACION,
                             TRANSFERING,
                             OTROSING,
                             VENTAS,
                             AJUSTESAL,
                             NOTACRED,
                             TRANSFERSAL,
                             OTRASSAL,
                             COMPRASVAL,
                             AJUSTEINGVALO,
                             BONIFIVAL,
                             TRANSINGVAL,
                             OTROSINGVAL,
                             VENTASVALOR,
                             AJUSTESALIVAL,
                             NOTACREDVAL,
                             TRANSFERSALVALOR,
                             OTROSSALVALOR)
    VALUES(@CORRNBR,
           @SITEID,
           @NAME,
           @CLASSID,
           @DESCR,
           @INVTIDSKU,
           @DESCRARTICULO,
           @INICIAL,
           @INICIALVAL,
           @COMPRAS,
           @AJUSTEINGRESO,
           @BONIFICACION,
           @TRANSFERING,
           @OTROSING,
           @VENTAS,
           @AJUSTESAL,
           @NOTACRED,
           @TRANSFERSAL,
           @OTRASSAL,
           @COMPRASVAL,
           @AJUSTEINGVALO,
           @BONIFIVAL,
           @TRANSINGVAL,
           @OTROSINGVAL,
           @VENTASVALOR,
           @AJUSTESALIVAL,
           @NOTACREDVAL,
           @TRANSFERSALVALOR,
           @OTROSSALVALOR)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_CTO_PROM: Cannot insert because primary key value not found in TMP_CTO_PROM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_CTO_PROM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_CTO_PROM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_CTO_PROM >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_CUSTWARRANTY 
 */

CREATE PROCEDURE SP_InsProcTMP_CUSTWARRANTY
(
    @CORRNBR                int,
    @CUSTWARRANTYTYPEID     char(2),
    @TOTGAR                 numeric(11, 4),
    @DESCRGAR               UD_DESCRIPCION,
    @EXPIREDATE             smalldatetime,
    @CURYID                 UD_CURYID)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_CUSTWARRANTY(CORRNBR,
                                 CUSTWARRANTYTYPEID,
                                 TOTGAR,
                                 DESCRGAR,
                                 EXPIREDATE,
                                 CURYID)
    VALUES(@CORRNBR,
           @CUSTWARRANTYTYPEID,
           @TOTGAR,
           @DESCRGAR,
           @EXPIREDATE,
           @CURYID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_CUSTWARRANTY: Cannot insert because primary key value not found in TMP_CUSTWARRANTY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_CUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_CUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_CUSTWARRANTY >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_CUSTWARRANTY 
 */

CREATE PROCEDURE SP_UpdProcTMP_CUSTWARRANTY
(
    @CORRNBR                int,
    @CUSTWARRANTYTYPEID     char(2),
    @TOTGAR                 numeric(11, 4),
    @DESCRGAR               UD_DESCRIPCION,
    @EXPIREDATE             smalldatetime,
    @CURYID                 UD_CURYID)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_CUSTWARRANTY
       SET TOTGAR                  = @TOTGAR,
           DESCRGAR                = @DESCRGAR,
           EXPIREDATE              = @EXPIREDATE,
           CURYID                  = @CURYID
     WHERE CORRNBR            = @CORRNBR
       AND CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_CUSTWARRANTY: Cannot update  in TMP_CUSTWARRANTY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_CUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_CUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_CUSTWARRANTY >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_CUSTWARRANTY 
 */

CREATE PROCEDURE SP_DelProcTMP_CUSTWARRANTY
(
    @CORRNBR                int,
    @CUSTWARRANTYTYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_CUSTWARRANTY
     WHERE CORRNBR            = @CORRNBR
       AND CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_CUSTWARRANTY: Cannot delete because foreign keys still exist in TMP_CUSTWARRANTY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_CUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_CUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_CUSTWARRANTY >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_CUSTWARRANTY 
 */

CREATE PROCEDURE SP_SelProcTMP_CUSTWARRANTY
(
    @CORRNBR                int,
    @CUSTWARRANTYTYPEID     char(2))
AS
BEGIN
    SELECT CORRNBR,
           CUSTWARRANTYTYPEID,
           TOTGAR,
           DESCRGAR,
           EXPIREDATE,
           CURYID
      FROM TMP_CUSTWARRANTY
     WHERE CORRNBR            = @CORRNBR
       AND CUSTWARRANTYTYPEID = @CUSTWARRANTYTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_CUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_CUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_CUSTWARRANTY >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_IN_COMPETITIONPRICE 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_COMPETITIONPRICE
(
    @CORRNBR            int,
    @SITEID             UD_IDSITE,
    @DATEPROCE          smalldatetime,
    @INVTIDSKU          UD_CODIGOBARRA,
    @PRICESUGGESTED     numeric(8, 4),
    @PRICECURRENT       numeric(8, 4),
    @PRICEPREVIOUS      numeric(8, 4))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_COMPETITIONPRICE(CORRNBR,
                                        SITEID,
                                        DATEPROCE,
                                        INVTIDSKU,
                                        PRICESUGGESTED,
                                        PRICECURRENT,
                                        PRICEPREVIOUS)
    VALUES(@CORRNBR,
           @SITEID,
           @DATEPROCE,
           @INVTIDSKU,
           @PRICESUGGESTED,
           @PRICECURRENT,
           @PRICEPREVIOUS)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_COMPETITIONPRICE: Cannot insert because primary key value not found in TMP_IN_COMPETITIONPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_COMPETITIONPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_COMPETITIONPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_COMPETITIONPRICE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_IN_COMPETITIONPRICE 
 */

CREATE PROCEDURE SP_UpdProcTMP_IN_COMPETITIONPRICE
(
    @CORRNBR            int,
    @SITEID             UD_IDSITE,
    @DATEPROCE          smalldatetime,
    @INVTIDSKU          UD_CODIGOBARRA,
    @PRICESUGGESTED     numeric(8, 4),
    @PRICECURRENT       numeric(8, 4),
    @PRICEPREVIOUS      numeric(8, 4))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_IN_COMPETITIONPRICE
       SET PRICESUGGESTED      = @PRICESUGGESTED,
           PRICECURRENT        = @PRICECURRENT,
           PRICEPREVIOUS       = @PRICEPREVIOUS
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_IN_COMPETITIONPRICE: Cannot update  in TMP_IN_COMPETITIONPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_IN_COMPETITIONPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_IN_COMPETITIONPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_IN_COMPETITIONPRICE >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_IN_COMPETITIONPRICE 
 */

CREATE PROCEDURE SP_DelProcTMP_IN_COMPETITIONPRICE
(
    @CORRNBR            int,
    @SITEID             UD_IDSITE,
    @DATEPROCE          smalldatetime,
    @INVTIDSKU          UD_CODIGOBARRA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_IN_COMPETITIONPRICE
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_IN_COMPETITIONPRICE: Cannot delete because foreign keys still exist in TMP_IN_COMPETITIONPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_IN_COMPETITIONPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_IN_COMPETITIONPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_IN_COMPETITIONPRICE >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_IN_COMPETITIONPRICE 
 */

CREATE PROCEDURE SP_SelProcTMP_IN_COMPETITIONPRICE
(
    @CORRNBR            int,
    @SITEID             UD_IDSITE,
    @DATEPROCE          smalldatetime,
    @INVTIDSKU          UD_CODIGOBARRA)
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           DATEPROCE,
           INVTIDSKU,
           PRICESUGGESTED,
           PRICECURRENT,
           PRICEPREVIOUS
      FROM TMP_IN_COMPETITIONPRICE
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_IN_COMPETITIONPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_IN_COMPETITIONPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_IN_COMPETITIONPRICE >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_IN_COMPETITIONPRICEDET 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_COMPETITIONPRICEDET
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @DATEPROCE         smalldatetime,
    @COMPETITIONID     char(2),
    @INVTIDSKU         UD_CODIGOBARRA,
    @SLS_PRICE         numeric(8, 4))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_COMPETITIONPRICEDET(CORRNBR,
                                           SITEID,
                                           DATEPROCE,
                                           COMPETITIONID,
                                           INVTIDSKU,
                                           SLS_PRICE)
    VALUES(@CORRNBR,
           @SITEID,
           @DATEPROCE,
           @COMPETITIONID,
           @INVTIDSKU,
           @SLS_PRICE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_COMPETITIONPRICEDET: Cannot insert because primary key value not found in TMP_IN_COMPETITIONPRICEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_COMPETITIONPRICEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_COMPETITIONPRICEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_COMPETITIONPRICEDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_IN_COMPETITIONPRICEDET 
 */

CREATE PROCEDURE SP_UpdProcTMP_IN_COMPETITIONPRICEDET
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @DATEPROCE         smalldatetime,
    @COMPETITIONID     char(2),
    @INVTIDSKU         UD_CODIGOBARRA,
    @SLS_PRICE         numeric(8, 4))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_IN_COMPETITIONPRICEDET
       SET SLS_PRICE          = @SLS_PRICE
     WHERE CORRNBR       = @CORRNBR
       AND SITEID        = @SITEID
       AND DATEPROCE     = @DATEPROCE
       AND COMPETITIONID = @COMPETITIONID
       AND INVTIDSKU     = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_IN_COMPETITIONPRICEDET: Cannot update  in TMP_IN_COMPETITIONPRICEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_IN_COMPETITIONPRICEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_IN_COMPETITIONPRICEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_IN_COMPETITIONPRICEDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_IN_COMPETITIONPRICEDET 
 */

CREATE PROCEDURE SP_DelProcTMP_IN_COMPETITIONPRICEDET
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @DATEPROCE         smalldatetime,
    @COMPETITIONID     char(2),
    @INVTIDSKU         UD_CODIGOBARRA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_IN_COMPETITIONPRICEDET
     WHERE CORRNBR       = @CORRNBR
       AND SITEID        = @SITEID
       AND DATEPROCE     = @DATEPROCE
       AND COMPETITIONID = @COMPETITIONID
       AND INVTIDSKU     = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_IN_COMPETITIONPRICEDET: Cannot delete because foreign keys still exist in TMP_IN_COMPETITIONPRICEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_IN_COMPETITIONPRICEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_IN_COMPETITIONPRICEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_IN_COMPETITIONPRICEDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_IN_COMPETITIONPRICEDET 
 */

CREATE PROCEDURE SP_SelProcTMP_IN_COMPETITIONPRICEDET
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @DATEPROCE         smalldatetime,
    @COMPETITIONID     char(2),
    @INVTIDSKU         UD_CODIGOBARRA)
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           DATEPROCE,
           COMPETITIONID,
           INVTIDSKU,
           SLS_PRICE
      FROM TMP_IN_COMPETITIONPRICEDET
     WHERE CORRNBR       = @CORRNBR
       AND SITEID        = @SITEID
       AND DATEPROCE     = @DATEPROCE
       AND COMPETITIONID = @COMPETITIONID
       AND INVTIDSKU     = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_IN_COMPETITIONPRICEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_IN_COMPETITIONPRICEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_IN_COMPETITIONPRICEDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_IN_INTRAITEM 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_INTRAITEM
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @TYPEDOCID         UD_TYPEMOVEID,
    @SEQUENCE          smallint,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDBARR        UD_CODIGOBARRA              = NULL,
    @QTY               numeric(10, 3),
    @TAXIGV            numeric(4, 2)               = NULL,
    @STDCOSTPEN        numeric(8, 4),
    @STDCOSTUSD        numeric(8, 4),
    @STKFISI           bit,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @RLSED             bit,
    @TAXISC            numeric(4, 2),
    @USER1             UD_USER1                    = NULL,
    @USER2             UD_USER2                    = NULL,
    @USER3             UD_USER3                    = NULL,
    @USER4             UD_USER5                    = NULL,
    @USER5             UD_USER5                    = NULL,
    @USER6             UD_USER6                    = NULL,
    @USER7             UD_USER7                    = NULL,
    @USER8             UD_USER8                    = NULL,
    @USER9             UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_INTRAITEM(CORRNBR,
                                 SITEID,
                                 TYPEDOCID,
                                 SEQUENCE,
                                 INVTIDSKU,
                                 INVTIDBARR,
                                 QTY,
                                 TAXIGV,
                                 STDCOSTPEN,
                                 STDCOSTUSD,
                                 STKFISI,
                                 INTRDATE,
                                 INTRDATEPROCE,
                                 RLSED,
                                 TAXISC,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4,
                                 USER5,
                                 USER6,
                                 USER7,
                                 USER8,
                                 USER9 
                                 )
    VALUES(@CORRNBR,
           @SITEID,
           @TYPEDOCID,
           @SEQUENCE,
           @INVTIDSKU,
           @INVTIDBARR,
           @QTY,
           @TAXIGV,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @STKFISI,
           @INTRDATE,
           @INTRDATEPROCE,
           @RLSED,
           @TAXISC,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_INTRAITEM: Cannot insert because primary key value not found in TMP_IN_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_INTRAITEM >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_IN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_INVENTORYALTER
(
    @CORRNBR        int,
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL,
    @TSTAMP         binary(10)                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_INVENTORYALTER(CORRNBR,
                                      INVTIDBARR,
                                      INVTIDSKU,
                                      USER1,
                                      USER2,
                                      USER3,
                                      USER4,
                                      TSTAMP)
    VALUES(@CORRNBR,
           @INVTIDBARR,
           @INVTIDSKU,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @TSTAMP)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_INVENTORYALTER: Cannot insert because primary key value not found in TMP_IN_INVENTORYALTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_INVENTORYALTER >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_IN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_UpdProcTMP_IN_INVENTORYALTER
(
    @CORRNBR        int,
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL,
    @TSTAMP         binary(10)                  = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_IN_INVENTORYALTER
       SET INVTIDSKU       = @INVTIDSKU,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4,
           TSTAMP          = @TSTAMP
     WHERE CORRNBR    = @CORRNBR
       AND INVTIDBARR = @INVTIDBARR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_IN_INVENTORYALTER: Cannot update  in TMP_IN_INVENTORYALTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_IN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_IN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_IN_INVENTORYALTER >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_IN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_DelProcTMP_IN_INVENTORYALTER
(
    @CORRNBR        int,
    @INVTIDBARR     UD_CODIGOBARRA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_IN_INVENTORYALTER
     WHERE CORRNBR    = @CORRNBR
       AND INVTIDBARR = @INVTIDBARR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_IN_INVENTORYALTER: Cannot delete because foreign keys still exist in TMP_IN_INVENTORYALTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_IN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_IN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_IN_INVENTORYALTER >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_IN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_SelProcTMP_IN_INVENTORYALTER
(
    @CORRNBR        int,
    @INVTIDBARR     UD_CODIGOBARRA)
AS
BEGIN
    SELECT CORRNBR,
           INVTIDBARR,
           INVTIDSKU,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM TMP_IN_INVENTORYALTER
     WHERE CORRNBR    = @CORRNBR
       AND INVTIDBARR = @INVTIDBARR

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_IN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_IN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_IN_INVENTORYALTER >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_IN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_INVENTORYDAT
(
    @CORRNBR            int,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           numeric(10, 3),
    @STOCKMAX           numeric(10, 3),
    @STOCKSTAR          numeric(10, 3),
    @QTYAVAIL           numeric(10, 3),
    @STARTDATEINV       smalldatetime               = NULL,
    @ENDDATEINV         smalldatetime               = NULL,
    @LASTINVFIS         numeric(10, 3),
    @AVGCOSTPEN         numeric(8, 4),
    @AVGCOSTUSD         numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @LASTSTDCOSTPEN     numeric(8, 4),
    @LASTSTDCOSTUSD     numeric(8, 4),
    @SLS_PRICE          numeric(8, 4),
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID                 = NULL,
    @INVTIDSTK          char(20)                    = NULL,
    @INVTIDNOSTK        char(20)                    = NULL,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER3                    = NULL,
    @USER4              UD_USER5                    = NULL,
    @USER5              UD_USER5                    = NULL,
    @USER6              UD_USER6                    = NULL,
    @USER7              UD_USER7                    = NULL,
    @USER8              UD_USER8                    = NULL,
    @USER9              UD_USER9                    = NULL,
    @TSTAMP             binary(10)                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_INVENTORYDAT(CORRNBR,
                                    INVTIDSKU,
                                    SITEID,
                                    STOCKMIN,
                                    STOCKMAX,
                                    STOCKSTAR,
                                    QTYAVAIL,
                                    STARTDATEINV,
                                    ENDDATEINV,
                                    LASTINVFIS,
                                    AVGCOSTPEN,
                                    AVGCOSTUSD,
                                    STDCOSTPEN,
                                    STDCOSTUSD,
                                    LASTSTDCOSTPEN,
                                    LASTSTDCOSTUSD,
                                    SLS_PRICE,
                                    LASTDAYIN,
                                    LASTDAYOU,
                                    PRECLVID,
                                    INVTIDSTK,
                                    INVTIDNOSTK,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4,
                                    USER5,
                                    USER6,
                                    USER7,
                                    USER8,
                                    USER9,
                                    TSTAMP)
    VALUES(@CORRNBR,
           @INVTIDSKU,
           @SITEID,
           @STOCKMIN,
           @STOCKMAX,
           @STOCKSTAR,
           @QTYAVAIL,
           @STARTDATEINV,
           @ENDDATEINV,
           @LASTINVFIS,
           @AVGCOSTPEN,
           @AVGCOSTUSD,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @LASTSTDCOSTPEN,
           @LASTSTDCOSTUSD,
           @SLS_PRICE,
           @LASTDAYIN,
           @LASTDAYOU,
           @PRECLVID,
           @INVTIDSTK,
           @INVTIDNOSTK,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9,
           @TSTAMP)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_INVENTORYDAT: Cannot insert because primary key value not found in TMP_IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_INVENTORYDAT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_IN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_UpdProcTMP_IN_INVENTORYDAT
(
    @CORRNBR            int,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           numeric(10, 3),
    @STOCKMAX           numeric(10, 3),
    @STOCKSTAR          numeric(10, 3),
    @QTYAVAIL           numeric(10, 3),
    @STARTDATEINV       smalldatetime               = NULL,
    @ENDDATEINV         smalldatetime               = NULL,
    @LASTINVFIS         numeric(10, 3),
    @AVGCOSTPEN         numeric(8, 4),
    @AVGCOSTUSD         numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @LASTSTDCOSTPEN     numeric(8, 4),
    @LASTSTDCOSTUSD     numeric(8, 4),
    @SLS_PRICE          numeric(8, 4),
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID                 = NULL,
    @INVTIDSTK          char(20)                    = NULL,
    @INVTIDNOSTK        char(20)                    = NULL,
    @USER1              UD_USER1                    = NULL,
    @USER2              UD_USER2                    = NULL,
    @USER3              UD_USER3                    = NULL,
    @USER4              UD_USER5                    = NULL,
    @USER5              UD_USER5                    = NULL,
    @USER6              UD_USER6                    = NULL,
    @USER7              UD_USER7                    = NULL,
    @USER8              UD_USER8                    = NULL,
    @USER9              UD_USER9                    = NULL,
    @TSTAMP             binary(10)                  = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_IN_INVENTORYDAT
       SET STOCKMIN            = @STOCKMIN,
           STOCKMAX            = @STOCKMAX,
           STOCKSTAR           = @STOCKSTAR,
           QTYAVAIL            = @QTYAVAIL,
           STARTDATEINV        = @STARTDATEINV,
           ENDDATEINV          = @ENDDATEINV,
           LASTINVFIS          = @LASTINVFIS,
           AVGCOSTPEN          = @AVGCOSTPEN,
           AVGCOSTUSD          = @AVGCOSTUSD,
           STDCOSTPEN          = @STDCOSTPEN,
           STDCOSTUSD          = @STDCOSTUSD,
           LASTSTDCOSTPEN      = @LASTSTDCOSTPEN,
           LASTSTDCOSTUSD      = @LASTSTDCOSTUSD,
           SLS_PRICE           = @SLS_PRICE,
           LASTDAYIN           = @LASTDAYIN,
           LASTDAYOU           = @LASTDAYOU,
           PRECLVID            = @PRECLVID,
           INVTIDSTK           = @INVTIDSTK,
           INVTIDNOSTK         = @INVTIDNOSTK,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4,
           USER5               = @USER5,
           USER6               = @USER6,
           USER7               = @USER7,
           USER8               = @USER8,
           USER9               = @USER9,
           TSTAMP              = @TSTAMP
     WHERE CORRNBR   = @CORRNBR
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_IN_INVENTORYDAT: Cannot update  in TMP_IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_IN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_IN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_IN_INVENTORYDAT >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_IN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_DelProcTMP_IN_INVENTORYDAT
(
    @CORRNBR            int,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_IN_INVENTORYDAT
     WHERE CORRNBR   = @CORRNBR
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_IN_INVENTORYDAT: Cannot delete because foreign keys still exist in TMP_IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_IN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_IN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_IN_INVENTORYDAT >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_IN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_SelProcTMP_IN_INVENTORYDAT
(
    @CORRNBR            int,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE)
AS
BEGIN
    SELECT CORRNBR,
           INVTIDSKU,
           SITEID,
           STOCKMIN,
           STOCKMAX,
           STOCKSTAR,
           QTYAVAIL,
           STARTDATEINV,
           ENDDATEINV,
           LASTINVFIS,
           AVGCOSTPEN,
           AVGCOSTUSD,
           STDCOSTPEN,
           STDCOSTUSD,
           LASTSTDCOSTPEN,
           LASTSTDCOSTUSD,
           SLS_PRICE,
           LASTDAYIN,
           LASTDAYOU,
           PRECLVID,
           INVTIDSTK,
           INVTIDNOSTK,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM TMP_IN_INVENTORYDAT
     WHERE CORRNBR   = @CORRNBR
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_IN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_IN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_IN_INVENTORYDAT >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_IN_KITCOMPONENT 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_KITCOMPONENT
(
    @CORRNBR        int,
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     numeric(8, 4),
    @STDCOSTUSD     numeric(8, 4),
    @QTY            numeric(9, 3),
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_KITCOMPONENT(CORRNBR,
                                    KITID,
                                    INVTIDSKU,
                                    SEQUENCE,
                                    STDCOSTPEN,
                                    STDCOSTUSD,
                                    QTY,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@CORRNBR,
           @KITID,
           @INVTIDSKU,
           @SEQUENCE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @QTY,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_KITCOMPONENT: Cannot insert because primary key value not found in TMP_IN_KITCOMPONENT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_KITCOMPONENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_KITCOMPONENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_KITCOMPONENT >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_IN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_PRICELEVELTEMDET
(
    @CORRNBR          int                         = NULL,
    @SITEID           UD_IDSITE                   = NULL,
    @SEQUENCE         smallint                    = NULL,
    @INVTIDSKU        UD_IDCORPORATIVO,
    @DESCR            UD_DESCRIPCION,
    @SLS_PRICE        numeric(8, 4),
    @DISCMTO          numeric(11, 4),
    @NEWSLS_PRICE     numeric(8, 4),
    @STDCOST          numeric(8, 4),
    @USER1            UD_USER1                    = NULL,
    @USER2            UD_USER2                    = NULL,
    @USER3            UD_USER6                    = NULL,
    @USER4            UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_PRICELEVELTEMDET(CORRNBR,
                                        SITEID,
                                        SEQUENCE,
                                        INVTIDSKU,
                                        DESCR,
                                        SLS_PRICE,
                                        DISCMTO,
                                        NEWSLS_PRICE,
                                        STDCOST,
                                        USER1,
                                        USER2,
                                        USER3,
                                        USER4 
                                        )
    VALUES(@CORRNBR,
           @SITEID,
           @SEQUENCE,
           @INVTIDSKU,
           @DESCR,
           @SLS_PRICE,
           @DISCMTO,
           @NEWSLS_PRICE,
           @STDCOST,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_PRICELEVELTEMDET: Cannot insert because primary key value not found in TMP_IN_PRICELEVELTEMDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_PRICELEVELTEMDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_PRICELEVELTEMDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_PRICELEVELTEMDET >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_IN_VENDORINVT 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_VENDORINVT
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SITEID        UD_IDSITE,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @STDCOST       numeric(8, 4),
    @CURYID        UD_CURYID                   = NULL,
    @BUYDATE       smalldatetime               = NULL,
    @USER1         UD_USER1                    = NULL,
    @USER2         UD_USER2                    = NULL,
    @USER3         UD_USER6                    = NULL,
    @USER4         UD_USER7                    = NULL,
    @TSTAMP        binary(10)                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_VENDORINVT(CORRNBR,
                                  VENDIDSS,
                                  INVTIDSKU,
                                  SITEID,
                                  BUYCOD,
                                  DESCR,
                                  STDCOST,
                                  CURYID,
                                  BUYDATE,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4,
                                  TSTAMP)
    VALUES(@CORRNBR,
           @VENDIDSS,
           @INVTIDSKU,
           @SITEID,
           @BUYCOD,
           @DESCR,
           @STDCOST,
           @CURYID,
           @BUYDATE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @TSTAMP)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_VENDORINVT: Cannot insert because primary key value not found in TMP_IN_VENDORINVT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_VENDORINVT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_IN_VENDORINVT 
 */

CREATE PROCEDURE SP_UpdProcTMP_IN_VENDORINVT
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SITEID        UD_IDSITE,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @STDCOST       numeric(8, 4),
    @CURYID        UD_CURYID                   = NULL,
    @BUYDATE       smalldatetime               = NULL,
    @USER1         UD_USER1                    = NULL,
    @USER2         UD_USER2                    = NULL,
    @USER3         UD_USER6                    = NULL,
    @USER4         UD_USER7                    = NULL,
    @TSTAMP        binary(10)                  = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_IN_VENDORINVT
       SET BUYCOD         = @BUYCOD,
           DESCR          = @DESCR,
           STDCOST        = @STDCOST,
           CURYID         = @CURYID,
           BUYDATE        = @BUYDATE,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4,
           TSTAMP         = @TSTAMP
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_IN_VENDORINVT: Cannot update  in TMP_IN_VENDORINVT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_IN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_IN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_IN_VENDORINVT >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_IN_VENDORINVT 
 */

CREATE PROCEDURE SP_DelProcTMP_IN_VENDORINVT
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SITEID        UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_IN_VENDORINVT
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_IN_VENDORINVT: Cannot delete because foreign keys still exist in TMP_IN_VENDORINVT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_IN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_IN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_IN_VENDORINVT >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_IN_VENDORINVT 
 */

CREATE PROCEDURE SP_SelProcTMP_IN_VENDORINVT
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SITEID        UD_IDSITE)
AS
BEGIN
    SELECT CORRNBR,
           VENDIDSS,
           INVTIDSKU,
           SITEID,
           BUYCOD,
           DESCR,
           STDCOST,
           CURYID,
           BUYDATE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM TMP_IN_VENDORINVT
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_IN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_IN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_IN_VENDORINVT >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_IN_VENDORINVTREL 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_VENDORINVTREL
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @CURYID        UD_CURYID)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_VENDORINVTREL(CORRNBR,
                                     VENDIDSS,
                                     INVTIDSKU,
                                     BUYCOD,
                                     DESCR,
                                     CURYID)
    VALUES(@CORRNBR,
           @VENDIDSS,
           @INVTIDSKU,
           @BUYCOD,
           @DESCR,
           @CURYID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_IN_VENDORINVTREL: Cannot insert because primary key value not found in TMP_IN_VENDORINVTREL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_IN_VENDORINVTREL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_IN_VENDORINVTREL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_IN_VENDORINVTREL >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_IN_VENDORINVTREL 
 */

CREATE PROCEDURE SP_UpdProcTMP_IN_VENDORINVTREL
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @CURYID        UD_CURYID)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_IN_VENDORINVTREL
       SET BUYCOD         = @BUYCOD,
           DESCR          = @DESCR,
           CURYID         = @CURYID
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_IN_VENDORINVTREL: Cannot update  in TMP_IN_VENDORINVTREL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_IN_VENDORINVTREL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_IN_VENDORINVTREL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_IN_VENDORINVTREL >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_IN_VENDORINVTREL 
 */

CREATE PROCEDURE SP_DelProcTMP_IN_VENDORINVTREL
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_IN_VENDORINVTREL
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_IN_VENDORINVTREL: Cannot delete because foreign keys still exist in TMP_IN_VENDORINVTREL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_IN_VENDORINVTREL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_IN_VENDORINVTREL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_IN_VENDORINVTREL >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_IN_VENDORINVTREL 
 */

CREATE PROCEDURE SP_SelProcTMP_IN_VENDORINVTREL
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO)
AS
BEGIN
    SELECT CORRNBR,
           VENDIDSS,
           INVTIDSKU,
           BUYCOD,
           DESCR,
           CURYID
      FROM TMP_IN_VENDORINVTREL
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_IN_VENDORINVTREL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_IN_VENDORINVTREL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_IN_VENDORINVTREL >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_BONUS 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_BONUS
(
    @CORRNBR        int,
    @custidss       UD_CODCLIENT,
    @SITEID         UD_IDSITE,
    @NBRBONUS       numeric(10, 0),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @stkreceive     bit,
    @USER01         UD_USER1                  = NULL,
    @USER02         UD_USER2                  = NULL,
    @TSTAMP         binary(10)                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_BONUS(CORRNBR,
                             custidss,
                             SITEID,
                             NBRBONUS,
                             stknew,
                             stkmodify,
                             stkdelete,
                             stksend,
                             stkreceive,
                             USER01,
                             USER02,
                             TSTAMP)
    VALUES(@CORRNBR,
           @custidss,
           @SITEID,
           @NBRBONUS,
           @stknew,
           @stkmodify,
           @stkdelete,
           @stksend,
           @stkreceive,
           @USER01,
           @USER02,
           @TSTAMP)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_BONUS: Cannot insert because primary key value not found in TMP_OP_BONUS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_BONUS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_BONUS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_BONUS >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_OP_CCSALESPERSONDET 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_CCSALESPERSONDET
(
    @CORRNBR                 int,
    @SITEID                  UD_IDSITE,
    @DATEPROCE               smalldatetime,
    @SALESPERID              UD_SALESPERSONID,
    @CCSALESPERSONTYPEID     char(2),
    @TOTALPEN                numeric(11, 4),
    @TOTALUSD                numeric(11, 4),
    @OBS                     varchar(100))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_CCSALESPERSONDET(CORRNBR,
                                        SITEID,
                                        DATEPROCE,
                                        SALESPERID,
                                        CCSALESPERSONTYPEID,
                                        TOTALPEN,
                                        TOTALUSD,
                                        OBS)
    VALUES(@CORRNBR,
           @SITEID,
           @DATEPROCE,
           @SALESPERID,
           @CCSALESPERSONTYPEID,
           @TOTALPEN,
           @TOTALUSD,
           @OBS)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_CCSALESPERSONDET: Cannot insert because primary key value not found in TMP_OP_CCSALESPERSONDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_CCSALESPERSONDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_CCSALESPERSONDET 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_CCSALESPERSONDET
(
    @CORRNBR                 int,
    @SITEID                  UD_IDSITE,
    @DATEPROCE               smalldatetime,
    @SALESPERID              UD_SALESPERSONID,
    @CCSALESPERSONTYPEID     char(2),
    @TOTALPEN                numeric(11, 4),
    @TOTALUSD                numeric(11, 4),
    @OBS                     varchar(100))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_CCSALESPERSONDET
       SET TOTALPEN                 = @TOTALPEN,
           TOTALUSD                 = @TOTALUSD,
           OBS                      = @OBS
     WHERE CORRNBR             = @CORRNBR
       AND SITEID              = @SITEID
       AND DATEPROCE           = @DATEPROCE
       AND SALESPERID          = @SALESPERID
       AND CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_CCSALESPERSONDET: Cannot update  in TMP_OP_CCSALESPERSONDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_CCSALESPERSONDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_CCSALESPERSONDET 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_CCSALESPERSONDET
(
    @CORRNBR                 int,
    @SITEID                  UD_IDSITE,
    @DATEPROCE               smalldatetime,
    @SALESPERID              UD_SALESPERSONID,
    @CCSALESPERSONTYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_CCSALESPERSONDET
     WHERE CORRNBR             = @CORRNBR
       AND SITEID              = @SITEID
       AND DATEPROCE           = @DATEPROCE
       AND SALESPERID          = @SALESPERID
       AND CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_CCSALESPERSONDET: Cannot delete because foreign keys still exist in TMP_OP_CCSALESPERSONDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_CCSALESPERSONDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_CCSALESPERSONDET 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_CCSALESPERSONDET
(
    @CORRNBR                 int,
    @SITEID                  UD_IDSITE,
    @DATEPROCE               smalldatetime,
    @SALESPERID              UD_SALESPERSONID,
    @CCSALESPERSONTYPEID     char(2))
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           DATEPROCE,
           SALESPERID,
           CCSALESPERSONTYPEID,
           TOTALPEN,
           TOTALUSD,
           OBS
      FROM TMP_OP_CCSALESPERSONDET
     WHERE CORRNBR             = @CORRNBR
       AND SITEID              = @SITEID
       AND DATEPROCE           = @DATEPROCE
       AND SALESPERID          = @SALESPERID
       AND CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_CCSALESPERSONDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_CTRLFUELDET 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_CTRLFUELDET
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @DATEPROCE            smalldatetime,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @STOCKINITIAL         numeric(10, 3),
    @QTYIMPUT             numeric(10, 3),
    @QTYOUTPUT            numeric(10, 3),
    @QTYSALES             numeric(10, 3),
    @QTYAVAIL             numeric(10, 3),
    @STKUINITID           UD_STKUNITID,
    @VALUEUNIMED          numeric(4, 0),
    @QTYPHISICAL          numeric(10, 3),
    @QTYDIFFERENCE        numeric(10, 3),
    @PORCENTDIFERENCE     numeric(5, 2),
    @REMARK               UD_DESCRIPCION              = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_CTRLFUELDET(CORRNBR,
                                   SITEID,
                                   DATEPROCE,
                                   INVTIDSKU,
                                   STOCKINITIAL,
                                   QTYIMPUT,
                                   QTYOUTPUT,
                                   QTYSALES,
                                   QTYAVAIL,
                                   STKUINITID,
                                   VALUEUNIMED,
                                   QTYPHISICAL,
                                   QTYDIFFERENCE,
                                   PORCENTDIFERENCE,
                                   REMARK)
    VALUES(@CORRNBR,
           @SITEID,
           @DATEPROCE,
           @INVTIDSKU,
           @STOCKINITIAL,
           @QTYIMPUT,
           @QTYOUTPUT,
           @QTYSALES,
           @QTYAVAIL,
           @STKUINITID,
           @VALUEUNIMED,
           @QTYPHISICAL,
           @QTYDIFFERENCE,
           @PORCENTDIFERENCE,
           @REMARK)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_CTRLFUELDET: Cannot insert because primary key value not found in TMP_OP_CTRLFUELDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_CTRLFUELDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_CTRLFUELDET 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_CTRLFUELDET
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @DATEPROCE            smalldatetime,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @STOCKINITIAL         numeric(10, 3),
    @QTYIMPUT             numeric(10, 3),
    @QTYOUTPUT            numeric(10, 3),
    @QTYSALES             numeric(10, 3),
    @QTYAVAIL             numeric(10, 3),
    @STKUINITID           UD_STKUNITID,
    @VALUEUNIMED          numeric(4, 0),
    @QTYPHISICAL          numeric(10, 3),
    @QTYDIFFERENCE        numeric(10, 3),
    @PORCENTDIFERENCE     numeric(5, 2),
    @REMARK               UD_DESCRIPCION              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_CTRLFUELDET
       SET STOCKINITIAL          = @STOCKINITIAL,
           QTYIMPUT              = @QTYIMPUT,
           QTYOUTPUT             = @QTYOUTPUT,
           QTYSALES              = @QTYSALES,
           QTYAVAIL              = @QTYAVAIL,
           STKUINITID            = @STKUINITID,
           VALUEUNIMED           = @VALUEUNIMED,
           QTYPHISICAL           = @QTYPHISICAL,
           QTYDIFFERENCE         = @QTYDIFFERENCE,
           PORCENTDIFERENCE      = @PORCENTDIFERENCE,
           REMARK                = @REMARK
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_CTRLFUELDET: Cannot update  in TMP_OP_CTRLFUELDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_CTRLFUELDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_CTRLFUELDET 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_CTRLFUELDET
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @DATEPROCE            smalldatetime,
    @INVTIDSKU            UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_CTRLFUELDET
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_CTRLFUELDET: Cannot delete because foreign keys still exist in TMP_OP_CTRLFUELDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_CTRLFUELDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_CTRLFUELDET 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_CTRLFUELDET
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @DATEPROCE            smalldatetime,
    @INVTIDSKU            UD_IDCORPORATIVO)
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           DATEPROCE,
           INVTIDSKU,
           STOCKINITIAL,
           QTYIMPUT,
           QTYOUTPUT,
           QTYSALES,
           QTYAVAIL,
           STKUINITID,
           VALUEUNIMED,
           QTYPHISICAL,
           QTYDIFFERENCE,
           PORCENTDIFERENCE,
           REMARK
      FROM TMP_OP_CTRLFUELDET
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_CTRLFUELDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_CTRLFUELPRICE 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_CTRLFUELPRICE
(
    @CORRNBR       int,
    @SITEID        UD_IDSITE,
    @DATEPROCE     smalldatetime,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SLS_PRICE     numeric(8, 4),
    @CURYID        UD_CURYID,
    @QTYSALES      numeric(10, 3))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_CTRLFUELPRICE(CORRNBR,
                                     SITEID,
                                     DATEPROCE,
                                     INVTIDSKU,
                                     SLS_PRICE,
                                     CURYID,
                                     QTYSALES)
    VALUES(@CORRNBR,
           @SITEID,
           @DATEPROCE,
           @INVTIDSKU,
           @SLS_PRICE,
           @CURYID,
           @QTYSALES)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_CTRLFUELPRICE: Cannot insert because primary key value not found in TMP_OP_CTRLFUELPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_CTRLFUELPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_CTRLFUELPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_CTRLFUELPRICE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_CTRLFUELPRICE 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_CTRLFUELPRICE
(
    @CORRNBR       int,
    @SITEID        UD_IDSITE,
    @DATEPROCE     smalldatetime,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SLS_PRICE     numeric(8, 4),
    @CURYID        UD_CURYID,
    @QTYSALES      numeric(10, 3))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_CTRLFUELPRICE
       SET CURYID         = @CURYID,
           QTYSALES       = @QTYSALES
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU
       AND SLS_PRICE = @SLS_PRICE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_CTRLFUELPRICE: Cannot update  in TMP_OP_CTRLFUELPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_CTRLFUELPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_CTRLFUELPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_CTRLFUELPRICE >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_CTRLFUELPRICE 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_CTRLFUELPRICE
(
    @CORRNBR       int,
    @SITEID        UD_IDSITE,
    @DATEPROCE     smalldatetime,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SLS_PRICE     numeric(8, 4))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_CTRLFUELPRICE
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU
       AND SLS_PRICE = @SLS_PRICE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_CTRLFUELPRICE: Cannot delete because foreign keys still exist in TMP_OP_CTRLFUELPRICE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_CTRLFUELPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_CTRLFUELPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_CTRLFUELPRICE >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_CTRLFUELPRICE 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_CTRLFUELPRICE
(
    @CORRNBR       int,
    @SITEID        UD_IDSITE,
    @DATEPROCE     smalldatetime,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SLS_PRICE     numeric(8, 4))
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           DATEPROCE,
           INVTIDSKU,
           SLS_PRICE,
           CURYID,
           QTYSALES
      FROM TMP_OP_CTRLFUELPRICE
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND INVTIDSKU = @INVTIDSKU
       AND SLS_PRICE = @SLS_PRICE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_CTRLFUELPRICE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_CTRLFUELPRICE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_CTRLFUELPRICE >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_CTRLTURNDET 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_CTRLTURNDET
(
    @CORRNBR          int,
    @SITEID           UD_IDSITE,
    @DATEPROCES       smalldatetime,
    @NBRTUR           char(1),
    @SIDE             UD_CARA,
    @HOSENUMBER       tinyint,
    @INVTIDBARR       UD_CODIGOBARRA,
    @STARTREADING     numeric(13, 3),
    @ENDREADING       numeric(13, 3),
    @QTYSALES         numeric(10, 3),
    @QTYSERAF         numeric(10, 3),
    @QTYOTHER         numeric(10, 3),
    @TOTQTY           numeric(10, 3))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_CTRLTURNDET(CORRNBR,
                                   SITEID,
                                   DATEPROCES,
                                   NBRTUR,
                                   SIDE,
                                   HOSENUMBER,
                                   INVTIDBARR,
                                   STARTREADING,
                                   ENDREADING,
                                   QTYSALES,
                                   QTYSERAF,
                                   QTYOTHER,
                                   TOTQTY)
    VALUES(@CORRNBR,
           @SITEID,
           @DATEPROCES,
           @NBRTUR,
           @SIDE,
           @HOSENUMBER,
           @INVTIDBARR,
           @STARTREADING,
           @ENDREADING,
           @QTYSALES,
           @QTYSERAF,
           @QTYOTHER,
           @TOTQTY)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_CTRLTURNDET: Cannot insert because primary key value not found in TMP_OP_CTRLTURNDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_CTRLTURNDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_CTRLTURNDET 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_CTRLTURNDET
(
    @CORRNBR          int,
    @SITEID           UD_IDSITE,
    @DATEPROCES       smalldatetime,
    @NBRTUR           char(1),
    @SIDE             UD_CARA,
    @HOSENUMBER       tinyint,
    @INVTIDBARR       UD_CODIGOBARRA,
    @STARTREADING     numeric(13, 3),
    @ENDREADING       numeric(13, 3),
    @QTYSALES         numeric(10, 3),
    @QTYSERAF         numeric(10, 3),
    @QTYOTHER         numeric(10, 3),
    @TOTQTY           numeric(10, 3))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_CTRLTURNDET
       SET INVTIDBARR        = @INVTIDBARR,
           STARTREADING      = @STARTREADING,
           ENDREADING        = @ENDREADING,
           QTYSALES          = @QTYSALES,
           QTYSERAF          = @QTYSERAF,
           QTYOTHER          = @QTYOTHER,
           TOTQTY            = @TOTQTY
     WHERE CORRNBR    = @CORRNBR
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR
       AND SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_CTRLTURNDET: Cannot update  in TMP_OP_CTRLTURNDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_CTRLTURNDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_CTRLTURNDET 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_CTRLTURNDET
(
    @CORRNBR          int,
    @SITEID           UD_IDSITE,
    @DATEPROCES       smalldatetime,
    @NBRTUR           char(1),
    @SIDE             UD_CARA,
    @HOSENUMBER       tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_CTRLTURNDET
     WHERE CORRNBR    = @CORRNBR
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR
       AND SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_CTRLTURNDET: Cannot delete because foreign keys still exist in TMP_OP_CTRLTURNDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_CTRLTURNDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_CTRLTURNDET 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_CTRLTURNDET
(
    @CORRNBR          int,
    @SITEID           UD_IDSITE,
    @DATEPROCES       smalldatetime,
    @NBRTUR           char(1),
    @SIDE             UD_CARA,
    @HOSENUMBER       tinyint)
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           DATEPROCES,
           NBRTUR,
           SIDE,
           HOSENUMBER,
           INVTIDBARR,
           STARTREADING,
           ENDREADING,
           QTYSALES,
           QTYSERAF,
           QTYOTHER,
           TOTQTY
      FROM TMP_OP_CTRLTURNDET
     WHERE CORRNBR    = @CORRNBR
       AND SITEID     = @SITEID
       AND DATEPROCES = @DATEPROCES
       AND NBRTUR     = @NBRTUR
       AND SIDE       = @SIDE
       AND HOSENUMBER = @HOSENUMBER

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_CTRLTURNDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_CUST_LIMIT 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_CUST_LIMIT
(
    @CORRNBR            int,
    @SITEID             UD_IDSITE,
    @PORCENTLIMITED     numeric(5, 2),
    @CREDLIMITED        numeric(11, 4),
    @CURRBAL            numeric(9, 2),
    @PORCENTSURPLUS     numeric(5, 2),
    @MTOSURPLUS         numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_CUST_LIMIT(CORRNBR,
                                  SITEID,
                                  PORCENTLIMITED,
                                  CREDLIMITED,
                                  CURRBAL,
                                  PORCENTSURPLUS,
                                  MTOSURPLUS)
    VALUES(@CORRNBR,
           @SITEID,
           @PORCENTLIMITED,
           @CREDLIMITED,
           @CURRBAL,
           @PORCENTSURPLUS,
           @MTOSURPLUS)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_CUST_LIMIT: Cannot insert because primary key value not found in TMP_OP_CUST_LIMIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_CUST_LIMIT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_CUST_LIMIT 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_CUST_LIMIT
(
    @CORRNBR            int,
    @SITEID             UD_IDSITE,
    @PORCENTLIMITED     numeric(5, 2),
    @CREDLIMITED        numeric(11, 4),
    @CURRBAL            numeric(9, 2),
    @PORCENTSURPLUS     numeric(5, 2),
    @MTOSURPLUS         numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_CUST_LIMIT
       SET PORCENTLIMITED      = @PORCENTLIMITED,
           CREDLIMITED         = @CREDLIMITED,
           CURRBAL             = @CURRBAL,
           PORCENTSURPLUS      = @PORCENTSURPLUS,
           MTOSURPLUS          = @MTOSURPLUS
     WHERE CORRNBR = @CORRNBR
       AND SITEID  = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_CUST_LIMIT: Cannot update  in TMP_OP_CUST_LIMIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_CUST_LIMIT >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_CUST_LIMIT 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_CUST_LIMIT
(
    @CORRNBR            int,
    @SITEID             UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_CUST_LIMIT
     WHERE CORRNBR = @CORRNBR
       AND SITEID  = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_CUST_LIMIT: Cannot delete because foreign keys still exist in TMP_OP_CUST_LIMIT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_CUST_LIMIT >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_CUST_LIMIT 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_CUST_LIMIT
(
    @CORRNBR            int,
    @SITEID             UD_IDSITE)
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           PORCENTLIMITED,
           CREDLIMITED,
           CURRBAL,
           PORCENTSURPLUS,
           MTOSURPLUS
      FROM TMP_OP_CUST_LIMIT
     WHERE CORRNBR = @CORRNBR
       AND SITEID  = @SITEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_CUST_LIMIT >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_CUSTBADGE 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_CUSTBADGE
(
    @CORRNBR     int,
    @BADGE       varchar(12))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_CUSTBADGE(CORRNBR,
                                 BADGE)
    VALUES(@CORRNBR,
           @BADGE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_CUSTBADGE: Cannot insert because primary key value not found in TMP_OP_CUSTBADGE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_CUSTBADGE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_CUSTBADGE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_CUSTBADGE >>>'
go



/* 
 * PROCEDURE: SP_DelProcTMP_OP_CUSTBADGE 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_CUSTBADGE
(
    @CORRNBR     int,
    @BADGE       varchar(12))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_CUSTBADGE
     WHERE CORRNBR = @CORRNBR
       AND BADGE   = @BADGE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_CUSTBADGE: Cannot delete because foreign keys still exist in TMP_OP_CUSTBADGE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_CUSTBADGE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_CUSTBADGE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_CUSTBADGE >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_CUSTBADGE 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_CUSTBADGE
(
    @CORRNBR     int,
    @BADGE       varchar(12))
AS
BEGIN
    SELECT CORRNBR,
           BADGE
      FROM TMP_OP_CUSTBADGE
     WHERE CORRNBR = @CORRNBR
       AND BADGE   = @BADGE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_CUSTBADGE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_CUSTBADGE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_CUSTBADGE >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_CUSTCARD 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_CUSTCARD
(
    @CORRNBR           int,
    @CARDPASSWORD      varchar(60),
    @EXPIREDATE        smalldatetime             = NULL,
    @DESCRCUSTCARD     UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_CUSTCARD(CORRNBR,
                                CARDPASSWORD,
                                EXPIREDATE,
                                DESCRCUSTCARD)
    VALUES(@CORRNBR,
           @CARDPASSWORD,
           @EXPIREDATE,
           @DESCRCUSTCARD)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_CUSTCARD: Cannot insert because primary key value not found in TMP_OP_CUSTCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_CUSTCARD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_CUSTCARD 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_CUSTCARD
(
    @CORRNBR           int,
    @CARDPASSWORD      varchar(60),
    @EXPIREDATE        smalldatetime             = NULL,
    @DESCRCUSTCARD     UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_CUSTCARD
       SET EXPIREDATE         = @EXPIREDATE,
           DESCRCUSTCARD      = @DESCRCUSTCARD
     WHERE CORRNBR      = @CORRNBR
       AND CARDPASSWORD = @CARDPASSWORD

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_CUSTCARD: Cannot update  in TMP_OP_CUSTCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_CUSTCARD >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_CUSTCARD 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_CUSTCARD
(
    @CORRNBR           int,
    @CARDPASSWORD      varchar(60))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_CUSTCARD
     WHERE CORRNBR      = @CORRNBR
       AND CARDPASSWORD = @CARDPASSWORD

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_CUSTCARD: Cannot delete because foreign keys still exist in TMP_OP_CUSTCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_CUSTCARD >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_CUSTCARD 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_CUSTCARD
(
    @CORRNBR           int,
    @CARDPASSWORD      varchar(60))
AS
BEGIN
    SELECT CORRNBR,
           CARDPASSWORD,
           EXPIREDATE,
           DESCRCUSTCARD
      FROM TMP_OP_CUSTCARD
     WHERE CORRNBR      = @CORRNBR
       AND CARDPASSWORD = @CARDPASSWORD

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_CUSTCARD >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_DEPOSITDET 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_DEPOSITDET
(
    @CORRNBR           int,
    @BATNBRDEPOSIT     numeric(10, 0),
    @SITEID            UD_IDSITE,
    @SEQUENCE          smallint,
    @CARDID            char(2)                   = NULL,
    @TOTAL             numeric(11, 4),
    @CUSTIDSS          UD_CODCLIENT              = NULL,
    @CURYID            UD_CURYID,
    @DEPOSITTYPEID     char(1),
    @USER1             UD_USER1                  = NULL,
    @USER2             UD_USER2                  = NULL,
    @USER3             UD_USER6                  = NULL,
    @USER4             UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_DEPOSITDET(CORRNBR,
                                  BATNBRDEPOSIT,
                                  SITEID,
                                  SEQUENCE,
                                  CARDID,
                                  TOTAL,
                                  CUSTIDSS,
                                  CURYID,
                                  DEPOSITTYPEID,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@CORRNBR,
           @BATNBRDEPOSIT,
           @SITEID,
           @SEQUENCE,
           @CARDID,
           @TOTAL,
           @CUSTIDSS,
           @CURYID,
           @DEPOSITTYPEID,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_DEPOSITDET: Cannot insert because primary key value not found in TMP_OP_DEPOSITDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_DEPOSITDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_DEPOSITDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_DEPOSITDET >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_OP_DEPOSITSITEDET 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_DEPOSITSITEDET
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @DATEPROCE            smalldatetime,
    @SEQUENCE             smallint,
    @CUSTIDSS             UD_CODCLIENT              = NULL,
    @DOCSUMMARYTYPEID     char(2)                   = NULL,
    @REFERENCE            numeric(10, 0),
    @DEPOSITPASSWORD      char(20),
    @DATEDEPOSIT          smalldatetime,
    @CURYID               UD_CURYID,
    @CURYRATE             UD_CURYRATE,
    @TOTALPEN             numeric(11, 4),
    @TOTALUSD             numeric(11, 4),
    @DEBEHABER            UD_DEBEHABER,
    @STATUS               bit                       = NULL,
    @USERID               UD_USERID)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_DEPOSITSITEDET(CORRNBR,
                                      SITEID,
                                      DATEPROCE,
                                      SEQUENCE,
                                      CUSTIDSS,
                                      DOCSUMMARYTYPEID,
                                      REFERENCE,
                                      DEPOSITPASSWORD,
                                      DATEDEPOSIT,
                                      CURYID,
                                      CURYRATE,
                                      TOTALPEN,
                                      TOTALUSD,
                                      DEBEHABER,
                                      STATUS,
                                      USERID)
    VALUES(@CORRNBR,
           @SITEID,
           @DATEPROCE,
           @SEQUENCE,
           @CUSTIDSS,
           @DOCSUMMARYTYPEID,
           @REFERENCE,
           @DEPOSITPASSWORD,
           @DATEDEPOSIT,
           @CURYID,
           @CURYRATE,
           @TOTALPEN,
           @TOTALUSD,
           @DEBEHABER,
           @STATUS,
           @USERID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_DEPOSITSITEDET: Cannot insert because primary key value not found in TMP_OP_DEPOSITSITEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_DEPOSITSITEDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_DEPOSITSITEDET 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_DEPOSITSITEDET
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @DATEPROCE            smalldatetime,
    @SEQUENCE             smallint,
    @CUSTIDSS             UD_CODCLIENT              = NULL,
    @DOCSUMMARYTYPEID     char(2)                   = NULL,
    @REFERENCE            numeric(10, 0),
    @DEPOSITPASSWORD      char(20),
    @DATEDEPOSIT          smalldatetime,
    @CURYID               UD_CURYID,
    @CURYRATE             UD_CURYRATE,
    @TOTALPEN             numeric(11, 4),
    @TOTALUSD             numeric(11, 4),
    @DEBEHABER            UD_DEBEHABER,
    @STATUS               bit                       = NULL,
    @USERID               UD_USERID)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_DEPOSITSITEDET
       SET CUSTIDSS              = @CUSTIDSS,
           DOCSUMMARYTYPEID      = @DOCSUMMARYTYPEID,
           REFERENCE             = @REFERENCE,
           DEPOSITPASSWORD       = @DEPOSITPASSWORD,
           DATEDEPOSIT           = @DATEDEPOSIT,
           CURYID                = @CURYID,
           CURYRATE              = @CURYRATE,
           TOTALPEN              = @TOTALPEN,
           TOTALUSD              = @TOTALUSD,
           DEBEHABER             = @DEBEHABER,
           STATUS                = @STATUS,
           USERID                = @USERID
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND SEQUENCE  = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_DEPOSITSITEDET: Cannot update  in TMP_OP_DEPOSITSITEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_DEPOSITSITEDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_DEPOSITSITEDET 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_DEPOSITSITEDET
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @DATEPROCE            smalldatetime,
    @SEQUENCE             smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_DEPOSITSITEDET
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND SEQUENCE  = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_DEPOSITSITEDET: Cannot delete because foreign keys still exist in TMP_OP_DEPOSITSITEDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_DEPOSITSITEDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_DEPOSITSITEDET 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_DEPOSITSITEDET
(
    @CORRNBR              int,
    @SITEID               UD_IDSITE,
    @DATEPROCE            smalldatetime,
    @SEQUENCE             smallint)
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           DATEPROCE,
           SEQUENCE,
           CUSTIDSS,
           DOCSUMMARYTYPEID,
           REFERENCE,
           DEPOSITPASSWORD,
           DATEDEPOSIT,
           CURYID,
           CURYRATE,
           TOTALPEN,
           TOTALUSD,
           DEBEHABER,
           STATUS,
           USERID
      FROM TMP_OP_DEPOSITSITEDET
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE
       AND SEQUENCE  = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_DEPOSITSITEDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_HISTRANSFUEL 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_HISTRANSFUEL
(
    @CORRNBR       int,
    @NUMERO        char(4),
    @SOLES         char(6),
    @PRODUCTO      char(2),
    @PRECIO        char(4),
    @GALONES       char(8),
    @CARA          char(2),
    @FECHA         char(6),
    @HORA          char(4),
    @ESTADO        char(10)                 = NULL,
    @DOCUMENTO     char(10)                 = NULL,
    @DATEPROCE     smalldatetime            = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_HISTRANSFUEL(CORRNBR,
                                    NUMERO,
                                    SOLES,
                                    PRODUCTO,
                                    PRECIO,
                                    GALONES,
                                    CARA,
                                    FECHA,
                                    HORA,
                                    ESTADO,
                                    DOCUMENTO,
                                    DATEPROCE)
    VALUES(@CORRNBR,
           @NUMERO,
           @SOLES,
           @PRODUCTO,
           @PRECIO,
           @GALONES,
           @CARA,
           @FECHA,
           @HORA,
           @ESTADO,
           @DOCUMENTO,
           @DATEPROCE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_HISTRANSFUEL: Cannot insert because primary key value not found in TMP_OP_HISTRANSFUEL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_HISTRANSFUEL') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_HISTRANSFUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_HISTRANSFUEL >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_OP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_PRCLVLCUSTDET
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @DISCMTO           numeric(11, 4),
    @SLS_PRICE         numeric(8, 4),
    @SLS_PRICECUST     numeric(8, 4),
    @DESCR             UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_PRCLVLCUSTDET(CORRNBR,
                                     SITEID,
                                     INVTIDSKU,
                                     DISCMTO,
                                     SLS_PRICE,
                                     SLS_PRICECUST,
                                     DESCR)
    VALUES(@CORRNBR,
           @SITEID,
           @INVTIDSKU,
           @DISCMTO,
           @SLS_PRICE,
           @SLS_PRICECUST,
           @DESCR)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_PRCLVLCUSTDET: Cannot insert because primary key value not found in TMP_OP_PRCLVLCUSTDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_PRCLVLCUSTDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_PRCLVLCUSTDET
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @DISCMTO           numeric(11, 4),
    @SLS_PRICE         numeric(8, 4),
    @SLS_PRICECUST     numeric(8, 4),
    @DESCR             UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_PRCLVLCUSTDET
       SET DISCMTO            = @DISCMTO,
           SLS_PRICE          = @SLS_PRICE,
           SLS_PRICECUST      = @SLS_PRICECUST,
           DESCR              = @DESCR
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_PRCLVLCUSTDET: Cannot update  in TMP_OP_PRCLVLCUSTDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_PRCLVLCUSTDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_PRCLVLCUSTDET
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_PRCLVLCUSTDET
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_PRCLVLCUSTDET: Cannot delete because foreign keys still exist in TMP_OP_PRCLVLCUSTDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_PRCLVLCUSTDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_PRCLVLCUSTDET
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO)
AS
BEGIN
    SELECT CORRNBR,
           SITEID,
           INVTIDSKU,
           DISCMTO,
           SLS_PRICE,
           SLS_PRICECUST,
           DESCR
      FROM TMP_OP_PRCLVLCUSTDET
     WHERE CORRNBR   = @CORRNBR
       AND SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_PRCLVLCUSTDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_SALES 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_SALES
(
    @CORRNBR               int,
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRSIDE               UD_CARA,
    @NBRDOCUMENT           numeric(10, 0),
    @TYPEPAYMENTID         char(2),
    @STKINVENTORY          bit,
    @SITEID                UD_IDSITE,
    @CURYID                UD_CURYID,
    @DOCSTATUSID           char(2),
    @TERMID                char(2),
    @SALESPERID            UD_SALESPERSONID,
    @USERID                UD_USERID,
    @DATEDOC               smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @PERPOST               UD_PERPOST,
    @TAXIGVID              char(2)                     = NULL,
    @TAXISCID              char(2)                     = NULL,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TAXISCPEN             numeric(11, 4),
    @TAXISCUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PORCENTDISCOUNT1      numeric(5, 2),
    @PORCENTDISCOUNT2      numeric(5, 2),
    @TOTDISCOUNTPEN        numeric(11, 4),
    @TOTDISCOUNTUSD        numeric(11, 4),
    @TOTRETURNEDPEN        numeric(11, 4),
    @TOTRETURNEDUSD        numeric(11, 4),
    @TOTCASHPEN            numeric(11, 4),
    @TOTCASHUSD            numeric(11, 4),
    @CUSTIDSS              UD_CODCLIENT                = NULL,
    @TAXREGNBR             UD_CODCLIENT                = NULL,
    @CUSTNAME              UD_NOMBRE                   = NULL,
    @PLACA                 UD_PLACA,
    @NBRBONUS              numeric(10, 0),
    @CURYTYPEID            UD_CURYTYPEID,
    @CURYRATE              UD_CAMBIO,
    @STKCLOSEDZ            bit,
    @STKCLOSEDX            bit,
    @PROMOTIONCARDID       char(20)                    = NULL,
    @POINTNUMBER           smallint,
    @SALESPOINT            UD_PTOVTA,
    @TERMINALNAME          varchar(25),
    @KILOMETRAJE           int,
    @ADDRESS               UD_DIRECCION                = NULL,
    @TYPECUSTOMER          int                         = NULL,
    @DSCRTYPECUST          varchar(40)                 = NULL,
    @IDESTADO              char(1)                     = NULL,
    @ESTADO                varchar(50)                 = NULL,
    @MONEDACREDITO         UD_CURYID                   = NULL,
    @TIPODECAMBIO          UD_CURYTYPEID               = NULL,
    @MONTOTIPOCAMBIO       UD_CURYRATE                 = NULL,
    @DIASDEGRACIA          int                         = NULL,
    @LIMITECRED            numeric(11, 4),
    @DEUDACLIENTE          numeric(9, 2)               = NULL,
    @MTOSURPLUS            numeric(11, 4),
    @AFECTO                bit                         = NULL,
    @CARDID                char(2)                     = NULL,
    @NBRCARD               char(20)                    = NULL,
    @CURYIDCARD            UD_CURYID                   = NULL,
    @PAGO                  int                         = NULL,
    @DESCRCARD             UD_NOMBRE                   = NULL,
    @USER1                 UD_USER1                    = NULL,
    @USER2                 UD_USER2                    = NULL,
    @USER3                 UD_USER3                    = NULL,
    @USER4                 UD_USER5                    = NULL,
    @USER5                 UD_USER5                    = NULL,
    @USER6                 UD_USER6                    = NULL,
    @USER7                 UD_USER7                    = NULL,
    @USER8                 UD_USER8                    = NULL,
    @USER9                 UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_SALES(CORRNBR,
                             DOCTYPEID,
                             NBRSIDE,
                             NBRDOCUMENT,
                             TYPEPAYMENTID,
                             STKINVENTORY,
                             SITEID,
                             CURYID,
                             DOCSTATUSID,
                             TERMID,
                             SALESPERID,
                             USERID,
                             DATEDOC,
                             DATEPROCESALES,
                             PERPOST,
                             TAXIGVID,
                             TAXISCID,
                             TOTALPEN,
                             TOTALUSD,
                             SUBTOTALPEN,
                             SUBTOTALUSD,
                             TAXIGVPEN,
                             TAXIGVUSD,
                             TAXISCPEN,
                             TAXISCUSD,
                             TOTALNOTAFFECTPEN,
                             TOTALNOTAFFECTUSD,
                             PORCENTDISCOUNT1,
                             PORCENTDISCOUNT2,
                             TOTDISCOUNTPEN,
                             TOTDISCOUNTUSD,
                             TOTRETURNEDPEN,
                             TOTRETURNEDUSD,
                             TOTCASHPEN,
                             TOTCASHUSD,
                             CUSTIDSS,
                             TAXREGNBR,
                             CUSTNAME,
                             PLACA,
                             NBRBONUS,
                             CURYTYPEID,
                             CURYRATE,
                             STKCLOSEDZ,
                             STKCLOSEDX,
                             PROMOTIONCARDID,
                             POINTNUMBER,
                             SALESPOINT,
                             TERMINALNAME,
                             KILOMETRAJE,
                             ADDRESS,
                             TYPECUSTOMER,
                             DSCRTYPECUST,
                             IDESTADO,
                             ESTADO,
                             MONEDACREDITO,
                             TIPODECAMBIO,
                             MONTOTIPOCAMBIO,
                             DIASDEGRACIA,
                             LIMITECRED,
                             DEUDACLIENTE,
                             MTOSURPLUS,
                             AFECTO,
                             CARDID,
                             NBRCARD,
                             CURYIDCARD,
                             PAGO,
                             DESCRCARD,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9 
                             )
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @NBRSIDE,
           @NBRDOCUMENT,
           @TYPEPAYMENTID,
           @STKINVENTORY,
           @SITEID,
           @CURYID,
           @DOCSTATUSID,
           @TERMID,
           @SALESPERID,
           @USERID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @TAXIGVID,
           @TAXISCID,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAXIGVPEN,
           @TAXIGVUSD,
           @TAXISCPEN,
           @TAXISCUSD,
           @TOTALNOTAFFECTPEN,
           @TOTALNOTAFFECTUSD,
           @PORCENTDISCOUNT1,
           @PORCENTDISCOUNT2,
           @TOTDISCOUNTPEN,
           @TOTDISCOUNTUSD,
           @TOTRETURNEDPEN,
           @TOTRETURNEDUSD,
           @TOTCASHPEN,
           @TOTCASHUSD,
           @CUSTIDSS,
           @TAXREGNBR,
           @CUSTNAME,
           @PLACA,
           @NBRBONUS,
           @CURYTYPEID,
           @CURYRATE,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @PROMOTIONCARDID,
           @POINTNUMBER,
           @SALESPOINT,
           @TERMINALNAME,
           @KILOMETRAJE,
           @ADDRESS,
           @TYPECUSTOMER,
           @DSCRTYPECUST,
           @IDESTADO,
           @ESTADO,
           @MONEDACREDITO,
           @TIPODECAMBIO,
           @MONTOTIPOCAMBIO,
           @DIASDEGRACIA,
           @LIMITECRED,
           @DEUDACLIENTE,
           @MTOSURPLUS,
           @AFECTO,
           @CARDID,
           @NBRCARD,
           @CURYIDCARD,
           @PAGO,
           @DESCRCARD,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_SALES: Cannot insert because primary key value not found in TMP_OP_SALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_SALES >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_SALES 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_SALES
(
    @CORRNBR               int,
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRSIDE               UD_CARA,
    @NBRDOCUMENT           numeric(10, 0),
    @TYPEPAYMENTID         char(2),
    @STKINVENTORY          bit,
    @SITEID                UD_IDSITE,
    @CURYID                UD_CURYID,
    @DOCSTATUSID           char(2),
    @TERMID                char(2),
    @SALESPERID            UD_SALESPERSONID,
    @USERID                UD_USERID,
    @DATEDOC               smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @PERPOST               UD_PERPOST,
    @TAXIGVID              char(2)                     = NULL,
    @TAXISCID              char(2)                     = NULL,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TAXISCPEN             numeric(11, 4),
    @TAXISCUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PORCENTDISCOUNT1      numeric(5, 2),
    @PORCENTDISCOUNT2      numeric(5, 2),
    @TOTDISCOUNTPEN        numeric(11, 4),
    @TOTDISCOUNTUSD        numeric(11, 4),
    @TOTRETURNEDPEN        numeric(11, 4),
    @TOTRETURNEDUSD        numeric(11, 4),
    @TOTCASHPEN            numeric(11, 4),
    @TOTCASHUSD            numeric(11, 4),
    @CUSTIDSS              UD_CODCLIENT                = NULL,
    @TAXREGNBR             UD_CODCLIENT                = NULL,
    @CUSTNAME              UD_NOMBRE                   = NULL,
    @PLACA                 UD_PLACA,
    @NBRBONUS              numeric(10, 0),
    @CURYTYPEID            UD_CURYTYPEID,
    @CURYRATE              UD_CAMBIO,
    @STKCLOSEDZ            bit,
    @STKCLOSEDX            bit,
    @PROMOTIONCARDID       char(20)                    = NULL,
    @POINTNUMBER           smallint,
    @SALESPOINT            UD_PTOVTA,
    @TERMINALNAME          varchar(25),
    @KILOMETRAJE           int,
    @ADDRESS               UD_DIRECCION                = NULL,
    @TYPECUSTOMER          int                         = NULL,
    @DSCRTYPECUST          varchar(40)                 = NULL,
    @IDESTADO              char(1)                     = NULL,
    @ESTADO                varchar(50)                 = NULL,
    @MONEDACREDITO         UD_CURYID                   = NULL,
    @TIPODECAMBIO          UD_CURYTYPEID               = NULL,
    @MONTOTIPOCAMBIO       UD_CURYRATE                 = NULL,
    @DIASDEGRACIA          int                         = NULL,
    @LIMITECRED            numeric(11, 4),
    @DEUDACLIENTE          numeric(9, 2)               = NULL,
    @MTOSURPLUS            numeric(11, 4),
    @AFECTO                bit                         = NULL,
    @CARDID                char(2)                     = NULL,
    @NBRCARD               char(20)                    = NULL,
    @CURYIDCARD            UD_CURYID                   = NULL,
    @PAGO                  int                         = NULL,
    @DESCRCARD             UD_NOMBRE                   = NULL,
    @USER1                 UD_USER1                    = NULL,
    @USER2                 UD_USER2                    = NULL,
    @USER3                 UD_USER3                    = NULL,
    @USER4                 UD_USER5                    = NULL,
    @USER5                 UD_USER5                    = NULL,
    @USER6                 UD_USER6                    = NULL,
    @USER7                 UD_USER7                    = NULL,
    @USER8                 UD_USER8                    = NULL,
    @USER9                 UD_USER9                    = NULL,
    @TSTAMP                timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_SALES
       SET NBRSIDE                = @NBRSIDE,
           NBRDOCUMENT            = @NBRDOCUMENT,
           TYPEPAYMENTID          = @TYPEPAYMENTID,
           STKINVENTORY           = @STKINVENTORY,
           SITEID                 = @SITEID,
           CURYID                 = @CURYID,
           DOCSTATUSID            = @DOCSTATUSID,
           TERMID                 = @TERMID,
           SALESPERID             = @SALESPERID,
           USERID                 = @USERID,
           DATEDOC                = @DATEDOC,
           DATEPROCESALES         = @DATEPROCESALES,
           PERPOST                = @PERPOST,
           TAXIGVID               = @TAXIGVID,
           TAXISCID               = @TAXISCID,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           SUBTOTALPEN            = @SUBTOTALPEN,
           SUBTOTALUSD            = @SUBTOTALUSD,
           TAXIGVPEN              = @TAXIGVPEN,
           TAXIGVUSD              = @TAXIGVUSD,
           TAXISCPEN              = @TAXISCPEN,
           TAXISCUSD              = @TAXISCUSD,
           TOTALNOTAFFECTPEN      = @TOTALNOTAFFECTPEN,
           TOTALNOTAFFECTUSD      = @TOTALNOTAFFECTUSD,
           PORCENTDISCOUNT1       = @PORCENTDISCOUNT1,
           PORCENTDISCOUNT2       = @PORCENTDISCOUNT2,
           TOTDISCOUNTPEN         = @TOTDISCOUNTPEN,
           TOTDISCOUNTUSD         = @TOTDISCOUNTUSD,
           TOTRETURNEDPEN         = @TOTRETURNEDPEN,
           TOTRETURNEDUSD         = @TOTRETURNEDUSD,
           TOTCASHPEN             = @TOTCASHPEN,
           TOTCASHUSD             = @TOTCASHUSD,
           CUSTIDSS               = @CUSTIDSS,
           TAXREGNBR              = @TAXREGNBR,
           CUSTNAME               = @CUSTNAME,
           PLACA                  = @PLACA,
           NBRBONUS               = @NBRBONUS,
           CURYTYPEID             = @CURYTYPEID,
           CURYRATE               = @CURYRATE,
           STKCLOSEDZ             = @STKCLOSEDZ,
           STKCLOSEDX             = @STKCLOSEDX,
           PROMOTIONCARDID        = @PROMOTIONCARDID,
           POINTNUMBER            = @POINTNUMBER,
           SALESPOINT             = @SALESPOINT,
           TERMINALNAME           = @TERMINALNAME,
           KILOMETRAJE            = @KILOMETRAJE,
           ADDRESS                = @ADDRESS,
           TYPECUSTOMER           = @TYPECUSTOMER,
           DSCRTYPECUST           = @DSCRTYPECUST,
           IDESTADO               = @IDESTADO,
           ESTADO                 = @ESTADO,
           MONEDACREDITO          = @MONEDACREDITO,
           TIPODECAMBIO           = @TIPODECAMBIO,
           MONTOTIPOCAMBIO        = @MONTOTIPOCAMBIO,
           DIASDEGRACIA           = @DIASDEGRACIA,
           LIMITECRED             = @LIMITECRED,
           DEUDACLIENTE           = @DEUDACLIENTE,
           MTOSURPLUS             = @MTOSURPLUS,
           AFECTO                 = @AFECTO,
           CARDID                 = @CARDID,
           NBRCARD                = @NBRCARD,
           CURYIDCARD             = @CURYIDCARD,
           PAGO                   = @PAGO,
           DESCRCARD              = @DESCRCARD,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4,
           USER5                  = @USER5,
           USER6                  = @USER6,
           USER7                  = @USER7,
           USER8                  = @USER8,
           USER9                  = @USER9
     WHERE CORRNBR   = @CORRNBR
       AND DOCTYPEID = @DOCTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_SALES: Cannot update  in TMP_OP_SALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_SALES >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_SALES 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_SALES
(
    @CORRNBR               int,
    @DOCTYPEID             UD_DOCTYPEID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_SALES
     WHERE CORRNBR   = @CORRNBR
       AND DOCTYPEID = @DOCTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_SALES: Cannot delete because foreign keys still exist in TMP_OP_SALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_SALES >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_SALES 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_SALES
(
    @CORRNBR               int,
    @DOCTYPEID             UD_DOCTYPEID)
AS
BEGIN
    SELECT CORRNBR,
           DOCTYPEID,
           NBRSIDE,
           NBRDOCUMENT,
           TYPEPAYMENTID,
           STKINVENTORY,
           SITEID,
           CURYID,
           DOCSTATUSID,
           TERMID,
           SALESPERID,
           USERID,
           DATEDOC,
           DATEPROCESALES,
           PERPOST,
           TAXIGVID,
           TAXISCID,
           TOTALPEN,
           TOTALUSD,
           SUBTOTALPEN,
           SUBTOTALUSD,
           TAXIGVPEN,
           TAXIGVUSD,
           TAXISCPEN,
           TAXISCUSD,
           TOTALNOTAFFECTPEN,
           TOTALNOTAFFECTUSD,
           PORCENTDISCOUNT1,
           PORCENTDISCOUNT2,
           TOTDISCOUNTPEN,
           TOTDISCOUNTUSD,
           TOTRETURNEDPEN,
           TOTRETURNEDUSD,
           TOTCASHPEN,
           TOTCASHUSD,
           CUSTIDSS,
           TAXREGNBR,
           CUSTNAME,
           PLACA,
           NBRBONUS,
           CURYTYPEID,
           CURYRATE,
           STKCLOSEDZ,
           STKCLOSEDX,
           PROMOTIONCARDID,
           POINTNUMBER,
           SALESPOINT,
           TERMINALNAME,
           KILOMETRAJE,
           ADDRESS,
           TYPECUSTOMER,
           DSCRTYPECUST,
           IDESTADO,
           ESTADO,
           MONEDACREDITO,
           TIPODECAMBIO,
           MONTOTIPOCAMBIO,
           DIASDEGRACIA,
           LIMITECRED,
           DEUDACLIENTE,
           MTOSURPLUS,
           AFECTO,
           CARDID,
           NBRCARD,
           CURYIDCARD,
           PAGO,
           DESCRCARD,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM TMP_OP_SALES
     WHERE CORRNBR   = @CORRNBR
       AND DOCTYPEID = @DOCTYPEID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_SALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_SALES >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_SALESCARD 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_SALESCARD
(
    @CORRNBR        int,
    @SEQUENCE       smallint,
    @SITEID         UD_IDSITE,
    @CARDID         char(2),
    @NBRCARD        char(20),
    @curyid         UD_CURYID                 = NULL,
    @TOTCARDPEN     numeric(11, 4),
    @TOTCARDUSD     numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_SALESCARD(CORRNBR,
                                 SEQUENCE,
                                 SITEID,
                                 CARDID,
                                 NBRCARD,
                                 curyid,
                                 TOTCARDPEN,
                                 TOTCARDUSD)
    VALUES(@CORRNBR,
           @SEQUENCE,
           @SITEID,
           @CARDID,
           @NBRCARD,
           @curyid,
           @TOTCARDPEN,
           @TOTCARDUSD)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_SALESCARD: Cannot insert because primary key value not found in TMP_OP_SALESCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_SALESCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_SALESCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_SALESCARD >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_SALESCARD 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_SALESCARD
(
    @CORRNBR        int,
    @SEQUENCE       smallint,
    @SITEID         UD_IDSITE,
    @CARDID         char(2),
    @NBRCARD        char(20),
    @curyid         UD_CURYID                 = NULL,
    @TOTCARDPEN     numeric(11, 4),
    @TOTCARDUSD     numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_SALESCARD
       SET SITEID          = @SITEID,
           CARDID          = @CARDID,
           NBRCARD         = @NBRCARD,
           curyid          = @curyid,
           TOTCARDPEN      = @TOTCARDPEN,
           TOTCARDUSD      = @TOTCARDUSD
     WHERE CORRNBR  = @CORRNBR
       AND SEQUENCE = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_SALESCARD: Cannot update  in TMP_OP_SALESCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_SALESCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_SALESCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_SALESCARD >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_SALESCARD 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_SALESCARD
(
    @CORRNBR        int,
    @SEQUENCE       smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_SALESCARD
     WHERE CORRNBR  = @CORRNBR
       AND SEQUENCE = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_SALESCARD: Cannot delete because foreign keys still exist in TMP_OP_SALESCARD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_SALESCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_SALESCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_SALESCARD >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_SALESCARD 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_SALESCARD
(
    @CORRNBR        int,
    @SEQUENCE       smallint)
AS
BEGIN
    SELECT CORRNBR,
           SEQUENCE,
           SITEID,
           CARDID,
           NBRCARD,
           curyid,
           TOTCARDPEN,
           TOTCARDUSD
      FROM TMP_OP_SALESCARD
     WHERE CORRNBR  = @CORRNBR
       AND SEQUENCE = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_SALESCARD') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_SALESCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_SALESCARD >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_SALESCSTORE 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_SALESCSTORE
(
    @CORRNBR               int,
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @TYPEPAYMENTID         char(2),
    @STKINVENTORY          bit,
    @SITEID                UD_IDSITE,
    @CURYID                UD_CURYID,
    @TERMID                char(2),
    @SALESPERID            UD_SALESPERSONID,
    @USERID                UD_USERID,
    @DATEDOC               smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @PERPOST               UD_PERPOST,
    @TAXIGVID              char(2)                     = NULL,
    @TAXISCID              char(2)                     = NULL,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TAXISCPEN             numeric(11, 4),
    @TAXISCUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PORCENTDISCOUNT1      numeric(5, 2),
    @PORCENTDISCOUNT2      numeric(5, 2),
    @TOTDISCOUNTPEN        numeric(11, 4),
    @TOTDISCOUNTUSD        numeric(11, 4),
    @TOTRETURNEDPEN        numeric(11, 4),
    @TOTRETURNEDUSD        numeric(11, 4),
    @TOTCASHPEN            numeric(11, 4),
    @TOTCASHUSD            numeric(11, 4),
    @custidss              UD_CODCLIENT,
    @taxregnbr             UD_CODCLIENT,
    @CUSTNAME              UD_NOMBRE,
    @ADDR1                 UD_DESCRIPCION,
    @PLACA                 UD_PLACA,
    @NBRBONUS              numeric(10, 0)              = NULL,
    @CURYTYPEID            UD_CURYTYPEID,
    @CURYRATE              UD_CAMBIO,
    @PROMOTIONCARDID       char(20)                    = NULL,
    @POINTNUMBER           smallint,
    @SALESPOINT            UD_PTOVTA,
    @KILOMETRAJE           int                         = NULL,
    @STKSAVE               bit,
    @SALESTYPE             char(1),
    @PROCESSED             bit,
    @businesstype          char(1))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_SALESCSTORE(CORRNBR,
                                   DOCTYPEID,
                                   NBRDOCUMENT,
                                   TYPEPAYMENTID,
                                   STKINVENTORY,
                                   SITEID,
                                   CURYID,
                                   TERMID,
                                   SALESPERID,
                                   USERID,
                                   DATEDOC,
                                   DATEPROCESALES,
                                   PERPOST,
                                   TAXIGVID,
                                   TAXISCID,
                                   TOTALPEN,
                                   TOTALUSD,
                                   SUBTOTALPEN,
                                   SUBTOTALUSD,
                                   TAXIGVPEN,
                                   TAXIGVUSD,
                                   TAXISCPEN,
                                   TAXISCUSD,
                                   TOTALNOTAFFECTPEN,
                                   TOTALNOTAFFECTUSD,
                                   PORCENTDISCOUNT1,
                                   PORCENTDISCOUNT2,
                                   TOTDISCOUNTPEN,
                                   TOTDISCOUNTUSD,
                                   TOTRETURNEDPEN,
                                   TOTRETURNEDUSD,
                                   TOTCASHPEN,
                                   TOTCASHUSD,
                                   custidss,
                                   taxregnbr,
                                   CUSTNAME,
                                   ADDR1,
                                   PLACA,
                                   NBRBONUS,
                                   CURYTYPEID,
                                   CURYRATE,
                                   PROMOTIONCARDID,
                                   POINTNUMBER,
                                   SALESPOINT,
                                   KILOMETRAJE,
                                   STKSAVE,
                                   SALESTYPE,
                                   PROCESSED,
                                   businesstype)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @NBRDOCUMENT,
           @TYPEPAYMENTID,
           @STKINVENTORY,
           @SITEID,
           @CURYID,
           @TERMID,
           @SALESPERID,
           @USERID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @TAXIGVID,
           @TAXISCID,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAXIGVPEN,
           @TAXIGVUSD,
           @TAXISCPEN,
           @TAXISCUSD,
           @TOTALNOTAFFECTPEN,
           @TOTALNOTAFFECTUSD,
           @PORCENTDISCOUNT1,
           @PORCENTDISCOUNT2,
           @TOTDISCOUNTPEN,
           @TOTDISCOUNTUSD,
           @TOTRETURNEDPEN,
           @TOTRETURNEDUSD,
           @TOTCASHPEN,
           @TOTCASHUSD,
           @custidss,
           @taxregnbr,
           @CUSTNAME,
           @ADDR1,
           @PLACA,
           @NBRBONUS,
           @CURYTYPEID,
           @CURYRATE,
           @PROMOTIONCARDID,
           @POINTNUMBER,
           @SALESPOINT,
           @KILOMETRAJE,
           @STKSAVE,
           @SALESTYPE,
           @PROCESSED,
           @businesstype)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_SALESCSTORE: Cannot insert because primary key value not found in TMP_OP_SALESCSTORE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_SALESCSTORE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_SALESCSTORE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_SALESCSTORE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_SALESCSTORE 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_SALESCSTORE
(
    @CORRNBR               int,
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @TYPEPAYMENTID         char(2),
    @STKINVENTORY          bit,
    @SITEID                UD_IDSITE,
    @CURYID                UD_CURYID,
    @TERMID                char(2),
    @SALESPERID            UD_SALESPERSONID,
    @USERID                UD_USERID,
    @DATEDOC               smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @PERPOST               UD_PERPOST,
    @TAXIGVID              char(2)                     = NULL,
    @TAXISCID              char(2)                     = NULL,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TAXISCPEN             numeric(11, 4),
    @TAXISCUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PORCENTDISCOUNT1      numeric(5, 2),
    @PORCENTDISCOUNT2      numeric(5, 2),
    @TOTDISCOUNTPEN        numeric(11, 4),
    @TOTDISCOUNTUSD        numeric(11, 4),
    @TOTRETURNEDPEN        numeric(11, 4),
    @TOTRETURNEDUSD        numeric(11, 4),
    @TOTCASHPEN            numeric(11, 4),
    @TOTCASHUSD            numeric(11, 4),
    @custidss              UD_CODCLIENT,
    @taxregnbr             UD_CODCLIENT,
    @CUSTNAME              UD_NOMBRE,
    @ADDR1                 UD_DESCRIPCION,
    @PLACA                 UD_PLACA,
    @NBRBONUS              numeric(10, 0)              = NULL,
    @CURYTYPEID            UD_CURYTYPEID,
    @CURYRATE              UD_CAMBIO,
    @PROMOTIONCARDID       char(20)                    = NULL,
    @POINTNUMBER           smallint,
    @SALESPOINT            UD_PTOVTA,
    @KILOMETRAJE           int                         = NULL,
    @STKSAVE               bit,
    @SALESTYPE             char(1),
    @PROCESSED             bit,
    @businesstype          char(1))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_SALESCSTORE
       SET DOCTYPEID              = @DOCTYPEID,
           NBRDOCUMENT            = @NBRDOCUMENT,
           TYPEPAYMENTID          = @TYPEPAYMENTID,
           STKINVENTORY           = @STKINVENTORY,
           SITEID                 = @SITEID,
           CURYID                 = @CURYID,
           TERMID                 = @TERMID,
           SALESPERID             = @SALESPERID,
           USERID                 = @USERID,
           DATEDOC                = @DATEDOC,
           DATEPROCESALES         = @DATEPROCESALES,
           PERPOST                = @PERPOST,
           TAXIGVID               = @TAXIGVID,
           TAXISCID               = @TAXISCID,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           SUBTOTALPEN            = @SUBTOTALPEN,
           SUBTOTALUSD            = @SUBTOTALUSD,
           TAXIGVPEN              = @TAXIGVPEN,
           TAXIGVUSD              = @TAXIGVUSD,
           TAXISCPEN              = @TAXISCPEN,
           TAXISCUSD              = @TAXISCUSD,
           TOTALNOTAFFECTPEN      = @TOTALNOTAFFECTPEN,
           TOTALNOTAFFECTUSD      = @TOTALNOTAFFECTUSD,
           PORCENTDISCOUNT1       = @PORCENTDISCOUNT1,
           PORCENTDISCOUNT2       = @PORCENTDISCOUNT2,
           TOTDISCOUNTPEN         = @TOTDISCOUNTPEN,
           TOTDISCOUNTUSD         = @TOTDISCOUNTUSD,
           TOTRETURNEDPEN         = @TOTRETURNEDPEN,
           TOTRETURNEDUSD         = @TOTRETURNEDUSD,
           TOTCASHPEN             = @TOTCASHPEN,
           TOTCASHUSD             = @TOTCASHUSD,
           custidss               = @custidss,
           taxregnbr              = @taxregnbr,
           CUSTNAME               = @CUSTNAME,
           ADDR1                  = @ADDR1,
           PLACA                  = @PLACA,
           NBRBONUS               = @NBRBONUS,
           CURYTYPEID             = @CURYTYPEID,
           CURYRATE               = @CURYRATE,
           PROMOTIONCARDID        = @PROMOTIONCARDID,
           POINTNUMBER            = @POINTNUMBER,
           SALESPOINT             = @SALESPOINT,
           KILOMETRAJE            = @KILOMETRAJE,
           STKSAVE                = @STKSAVE,
           SALESTYPE              = @SALESTYPE,
           PROCESSED              = @PROCESSED,
           businesstype           = @businesstype
     WHERE CORRNBR = @CORRNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_SALESCSTORE: Cannot update  in TMP_OP_SALESCSTORE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_SALESCSTORE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_SALESCSTORE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_SALESCSTORE >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_SALESCSTORE 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_SALESCSTORE
(
    @CORRNBR               int)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_SALESCSTORE
     WHERE CORRNBR = @CORRNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_SALESCSTORE: Cannot delete because foreign keys still exist in TMP_OP_SALESCSTORE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_SALESCSTORE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_SALESCSTORE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_SALESCSTORE >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_SALESCSTORE 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_SALESCSTORE
(
    @CORRNBR               int)
AS
BEGIN
    SELECT CORRNBR,
           DOCTYPEID,
           NBRDOCUMENT,
           TYPEPAYMENTID,
           STKINVENTORY,
           SITEID,
           CURYID,
           TERMID,
           SALESPERID,
           USERID,
           DATEDOC,
           DATEPROCESALES,
           PERPOST,
           TAXIGVID,
           TAXISCID,
           TOTALPEN,
           TOTALUSD,
           SUBTOTALPEN,
           SUBTOTALUSD,
           TAXIGVPEN,
           TAXIGVUSD,
           TAXISCPEN,
           TAXISCUSD,
           TOTALNOTAFFECTPEN,
           TOTALNOTAFFECTUSD,
           PORCENTDISCOUNT1,
           PORCENTDISCOUNT2,
           TOTDISCOUNTPEN,
           TOTDISCOUNTUSD,
           TOTRETURNEDPEN,
           TOTRETURNEDUSD,
           TOTCASHPEN,
           TOTCASHUSD,
           custidss,
           taxregnbr,
           CUSTNAME,
           ADDR1,
           PLACA,
           NBRBONUS,
           CURYTYPEID,
           CURYRATE,
           PROMOTIONCARDID,
           POINTNUMBER,
           SALESPOINT,
           KILOMETRAJE,
           STKSAVE,
           SALESTYPE,
           PROCESSED,
           businesstype
      FROM TMP_OP_SALESCSTORE
     WHERE CORRNBR = @CORRNBR

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_SALESCSTORE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_SALESCSTORE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_SALESCSTORE >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_SALESDET 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_SALESDET
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @SEQUENCE           smallint,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             UD_IDSITE,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DOCSTATUSID        char(2),
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERPOST            UD_PERPOST,
    @STKCLOSEDZ         bit,
    @STKCLOSEDX         bit,
    @NBRTURN            tinyint,
    @NBRSIDE            UD_CARA                     = NULL,
    @NBRTRANSACFUEL     char(4)                     = NULL,
    @DISCDSCTO1         numeric(5, 2),
    @DISCDSCTO2         numeric(5, 2),
    @DISCMTOPEN         numeric(11, 4),
    @DISCMTOUSD         numeric(11, 4),
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(11, 4),
    @TOTALUSD           numeric(11, 4),
    @TAXPEN             numeric(11, 4),
    @TAXUSD             numeric(11, 4),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                numeric(10, 3),
    @USERID             UD_USERID                   = NULL,
    @KIT                bit                         = NULL,
    @COMBUSTIBLE        bit                         = NULL,
    @PTOVTA             UD_PTOVTA                   = NULL,
    @STKUNITID          UD_STKUNITID)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_SALESDET(CORRNBR,
                                DOCTYPEID,
                                SEQUENCE,
                                NBRDOCUMENT,
                                SITEID,
                                INVTIDSKU,
                                CURYID,
                                DOCSTATUSID,
                                DATEDOC,
                                DATEPROCESALES,
                                PERPOST,
                                STKCLOSEDZ,
                                STKCLOSEDX,
                                NBRTURN,
                                NBRSIDE,
                                NBRTRANSACFUEL,
                                DISCDSCTO1,
                                DISCDSCTO2,
                                DISCMTOPEN,
                                DISCMTOUSD,
                                PORCENTTAXIGV,
                                PORCENTTAXISC,
                                TOTALPEN,
                                TOTALUSD,
                                TAXPEN,
                                TAXUSD,
                                STKITEM,
                                STKFISI,
                                SLS_PRICE,
                                SLSPRICESALE,
                                STDCOSTPEN,
                                STDCOSTUSD,
                                INVTIDALTER,
                                DESCRINVENTORY,
                                QTY,
                                USERID,
                                KIT,
                                COMBUSTIBLE,
                                PTOVTA,
                                STKUNITID)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @SEQUENCE,
           @NBRDOCUMENT,
           @SITEID,
           @INVTIDSKU,
           @CURYID,
           @DOCSTATUSID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @NBRTURN,
           @NBRSIDE,
           @NBRTRANSACFUEL,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTOPEN,
           @DISCMTOUSD,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @USERID,
           @KIT,
           @COMBUSTIBLE,
           @PTOVTA,
           @STKUNITID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_SALESDET: Cannot insert because primary key value not found in TMP_OP_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_SALESDET 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_SALESDET
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @SEQUENCE           smallint,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             UD_IDSITE,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DOCSTATUSID        char(2),
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERPOST            UD_PERPOST,
    @STKCLOSEDZ         bit,
    @STKCLOSEDX         bit,
    @NBRTURN            tinyint,
    @NBRSIDE            UD_CARA                     = NULL,
    @NBRTRANSACFUEL     char(4)                     = NULL,
    @DISCDSCTO1         numeric(5, 2),
    @DISCDSCTO2         numeric(5, 2),
    @DISCMTOPEN         numeric(11, 4),
    @DISCMTOUSD         numeric(11, 4),
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(11, 4),
    @TOTALUSD           numeric(11, 4),
    @TAXPEN             numeric(11, 4),
    @TAXUSD             numeric(11, 4),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                numeric(10, 3),
    @USERID             UD_USERID                   = NULL,
    @KIT                bit                         = NULL,
    @COMBUSTIBLE        bit                         = NULL,
    @PTOVTA             UD_PTOVTA                   = NULL,
    @STKUNITID          UD_STKUNITID)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_SALESDET
       SET NBRDOCUMENT         = @NBRDOCUMENT,
           SITEID              = @SITEID,
           INVTIDSKU           = @INVTIDSKU,
           CURYID              = @CURYID,
           DOCSTATUSID         = @DOCSTATUSID,
           DATEDOC             = @DATEDOC,
           DATEPROCESALES      = @DATEPROCESALES,
           PERPOST             = @PERPOST,
           STKCLOSEDZ          = @STKCLOSEDZ,
           STKCLOSEDX          = @STKCLOSEDX,
           NBRTURN             = @NBRTURN,
           NBRSIDE             = @NBRSIDE,
           NBRTRANSACFUEL      = @NBRTRANSACFUEL,
           DISCDSCTO1          = @DISCDSCTO1,
           DISCDSCTO2          = @DISCDSCTO2,
           DISCMTOPEN          = @DISCMTOPEN,
           DISCMTOUSD          = @DISCMTOUSD,
           PORCENTTAXIGV       = @PORCENTTAXIGV,
           PORCENTTAXISC       = @PORCENTTAXISC,
           TOTALPEN            = @TOTALPEN,
           TOTALUSD            = @TOTALUSD,
           TAXPEN              = @TAXPEN,
           TAXUSD              = @TAXUSD,
           STKITEM             = @STKITEM,
           STKFISI             = @STKFISI,
           SLS_PRICE           = @SLS_PRICE,
           SLSPRICESALE        = @SLSPRICESALE,
           STDCOSTPEN          = @STDCOSTPEN,
           STDCOSTUSD          = @STDCOSTUSD,
           INVTIDALTER         = @INVTIDALTER,
           DESCRINVENTORY      = @DESCRINVENTORY,
           QTY                 = @QTY,
           USERID              = @USERID,
           KIT                 = @KIT,
           COMBUSTIBLE         = @COMBUSTIBLE,
           PTOVTA              = @PTOVTA,
           STKUNITID           = @STKUNITID
     WHERE CORRNBR   = @CORRNBR
       AND DOCTYPEID = @DOCTYPEID
       AND SEQUENCE  = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_SALESDET: Cannot update  in TMP_OP_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_SALESDET 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_SALESDET
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @SEQUENCE           smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_SALESDET
     WHERE CORRNBR   = @CORRNBR
       AND DOCTYPEID = @DOCTYPEID
       AND SEQUENCE  = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_SALESDET: Cannot delete because foreign keys still exist in TMP_OP_SALESDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_SALESDET 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_SALESDET
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @SEQUENCE           smallint)
AS
BEGIN
    SELECT CORRNBR,
           DOCTYPEID,
           SEQUENCE,
           NBRDOCUMENT,
           SITEID,
           INVTIDSKU,
           CURYID,
           DOCSTATUSID,
           DATEDOC,
           DATEPROCESALES,
           PERPOST,
           STKCLOSEDZ,
           STKCLOSEDX,
           NBRTURN,
           NBRSIDE,
           NBRTRANSACFUEL,
           DISCDSCTO1,
           DISCDSCTO2,
           DISCMTOPEN,
           DISCMTOUSD,
           PORCENTTAXIGV,
           PORCENTTAXISC,
           TOTALPEN,
           TOTALUSD,
           TAXPEN,
           TAXUSD,
           STKITEM,
           STKFISI,
           SLS_PRICE,
           SLSPRICESALE,
           STDCOSTPEN,
           STDCOSTUSD,
           INVTIDALTER,
           DESCRINVENTORY,
           QTY,
           USERID,
           KIT,
           COMBUSTIBLE,
           PTOVTA,
           STKUNITID
      FROM TMP_OP_SALESDET
     WHERE CORRNBR   = @CORRNBR
       AND DOCTYPEID = @DOCTYPEID
       AND SEQUENCE  = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_SALESDET >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_SALESDETCSTORE 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_SALESDETCSTORE
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint,
    @SITEID             UD_IDSITE,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERPOST            UD_PERPOST,
    @NBRTURN            tinyint,
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(11, 4),
    @TOTALUSD           numeric(11, 4),
    @TAXPEN             numeric(11, 4),
    @TAXUSD             numeric(11, 4),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                numeric(10, 3),
    @KIT                bit,
    @STKUNITID          UD_STKUNITID)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_SALESDETCSTORE(CORRNBR,
                                      DOCTYPEID,
                                      NBRDOCUMENT,
                                      SEQUENCE,
                                      SITEID,
                                      INVTIDSKU,
                                      CURYID,
                                      DATEDOC,
                                      DATEPROCESALES,
                                      PERPOST,
                                      NBRTURN,
                                      PORCENTTAXIGV,
                                      PORCENTTAXISC,
                                      TOTALPEN,
                                      TOTALUSD,
                                      TAXPEN,
                                      TAXUSD,
                                      STKITEM,
                                      STKFISI,
                                      SLS_PRICE,
                                      SLSPRICESALE,
                                      STDCOSTPEN,
                                      STDCOSTUSD,
                                      INVTIDALTER,
                                      DESCRINVENTORY,
                                      QTY,
                                      KIT,
                                      STKUNITID)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @NBRDOCUMENT,
           @SEQUENCE,
           @SITEID,
           @INVTIDSKU,
           @CURYID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @NBRTURN,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @KIT,
           @STKUNITID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_SALESDETCSTORE: Cannot insert because primary key value not found in TMP_OP_SALESDETCSTORE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_SALESDETCSTORE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_SALESDETCSTORE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_SALESDETCSTORE >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_OP_SALESDETCSTORE 
 */

CREATE PROCEDURE SP_UpdProcTMP_OP_SALESDETCSTORE
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint,
    @SITEID             UD_IDSITE,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERPOST            UD_PERPOST,
    @NBRTURN            tinyint,
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(11, 4),
    @TOTALUSD           numeric(11, 4),
    @TAXPEN             numeric(11, 4),
    @TAXUSD             numeric(11, 4),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                numeric(10, 3),
    @KIT                bit,
    @STKUNITID          UD_STKUNITID)
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_OP_SALESDETCSTORE
       SET SITEID              = @SITEID,
           INVTIDSKU           = @INVTIDSKU,
           CURYID              = @CURYID,
           DATEDOC             = @DATEDOC,
           DATEPROCESALES      = @DATEPROCESALES,
           PERPOST             = @PERPOST,
           NBRTURN             = @NBRTURN,
           PORCENTTAXIGV       = @PORCENTTAXIGV,
           PORCENTTAXISC       = @PORCENTTAXISC,
           TOTALPEN            = @TOTALPEN,
           TOTALUSD            = @TOTALUSD,
           TAXPEN              = @TAXPEN,
           TAXUSD              = @TAXUSD,
           STKITEM             = @STKITEM,
           STKFISI             = @STKFISI,
           SLS_PRICE           = @SLS_PRICE,
           SLSPRICESALE        = @SLSPRICESALE,
           STDCOSTPEN          = @STDCOSTPEN,
           STDCOSTUSD          = @STDCOSTUSD,
           INVTIDALTER         = @INVTIDALTER,
           DESCRINVENTORY      = @DESCRINVENTORY,
           QTY                 = @QTY,
           KIT                 = @KIT,
           STKUNITID           = @STKUNITID
     WHERE CORRNBR     = @CORRNBR
       AND DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_OP_SALESDETCSTORE: Cannot update  in TMP_OP_SALESDETCSTORE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_OP_SALESDETCSTORE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_OP_SALESDETCSTORE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_OP_SALESDETCSTORE >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_OP_SALESDETCSTORE 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_SALESDETCSTORE
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_SALESDETCSTORE
     WHERE CORRNBR     = @CORRNBR
       AND DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_SALESDETCSTORE: Cannot delete because foreign keys still exist in TMP_OP_SALESDETCSTORE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_SALESDETCSTORE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_SALESDETCSTORE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_SALESDETCSTORE >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_SALESDETCSTORE 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_SALESDETCSTORE
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint)
AS
BEGIN
    SELECT CORRNBR,
           DOCTYPEID,
           NBRDOCUMENT,
           SEQUENCE,
           SITEID,
           INVTIDSKU,
           CURYID,
           DATEDOC,
           DATEPROCESALES,
           PERPOST,
           NBRTURN,
           PORCENTTAXIGV,
           PORCENTTAXISC,
           TOTALPEN,
           TOTALUSD,
           TAXPEN,
           TAXUSD,
           STKITEM,
           STKFISI,
           SLS_PRICE,
           SLSPRICESALE,
           STDCOSTPEN,
           STDCOSTUSD,
           INVTIDALTER,
           DESCRINVENTORY,
           QTY,
           KIT,
           STKUNITID
      FROM TMP_OP_SALESDETCSTORE
     WHERE CORRNBR     = @CORRNBR
       AND DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_SALESDETCSTORE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_SALESDETCSTORE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_SALESDETCSTORE >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_OP_SALESDETNC 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_SALESDETNC
(
    @CORRNBR               int,
    @SEQUENCE              smallint,
    @SITEID                UD_IDSITE,
    @INVTIDSKU             UD_IDCORPORATIVO,
    @CURYID                UD_CURYID,
    @DATEDOC               smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @PORCENTTAXIGV         numeric(5, 2),
    @PORCENTTAXISC         numeric(5, 2),
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @TAXPEN                numeric(11, 4),
    @TAXUSD                numeric(11, 4),
    @STKITEM               bit,
    @STKFISI               bit,
    @SLS_PRICE             numeric(8, 4),
    @SLSPRICESALE          numeric(8, 4),
    @STDCOSTPEN            numeric(8, 4),
    @STDCOSTUSD            numeric(8, 4),
    @INVTIDALTER           UD_CODIGOBARRA,
    @DESCRINVENTORY        UD_DESCRIPCION,
    @QTY                   numeric(10, 3),
    @KIT                   bit,
    @cantidad_original     numeric(10, 3),
    @DEVUELTO              numeric(10, 3))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_SALESDETNC(CORRNBR,
                                  SEQUENCE,
                                  SITEID,
                                  INVTIDSKU,
                                  CURYID,
                                  DATEDOC,
                                  DATEPROCESALES,
                                  PORCENTTAXIGV,
                                  PORCENTTAXISC,
                                  TOTALPEN,
                                  TOTALUSD,
                                  TAXPEN,
                                  TAXUSD,
                                  STKITEM,
                                  STKFISI,
                                  SLS_PRICE,
                                  SLSPRICESALE,
                                  STDCOSTPEN,
                                  STDCOSTUSD,
                                  INVTIDALTER,
                                  DESCRINVENTORY,
                                  QTY,
                                  KIT,
                                  cantidad_original,
                                  DEVUELTO)
    VALUES(@CORRNBR,
           @SEQUENCE,
           @SITEID,
           @INVTIDSKU,
           @CURYID,
           @DATEDOC,
           @DATEPROCESALES,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @KIT,
           @cantidad_original,
           @DEVUELTO)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_SALESDETNC: Cannot insert because primary key value not found in TMP_OP_SALESDETNC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_SALESDETNC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_SALESDETNC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_SALESDETNC >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_OP_SALESNC 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_SALESNC
(
    @CORRNBR               int,
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           numeric(10, 0),
    @TYPEPAYMENTID         char(2),
    @STKINVENTORY          bit,
    @SITEID                UD_IDSITE,
    @CURYID                UD_CURYID,
    @TERMID                char(2),
    @SALESPERID            UD_SALESPERSONID,
    @USERID                UD_USERID,
    @DATEDOC               smalldatetime,
    @DATEPROCESALES        smalldatetime,
    @PERPOST               UD_PERPOST,
    @TAXIGVID              char(2)                     = NULL,
    @TAXISCID              char(2)                     = NULL,
    @TOTALPEN              numeric(11, 4),
    @TOTALUSD              numeric(11, 4),
    @SUBTOTALPEN           numeric(11, 4),
    @SUBTOTALUSD           numeric(11, 4),
    @TAXIGVPEN             numeric(11, 4),
    @TAXIGVUSD             numeric(11, 4),
    @TAXISCPEN             numeric(11, 4),
    @TAXISCUSD             numeric(11, 4),
    @TOTALNOTAFFECTPEN     numeric(11, 4),
    @TOTALNOTAFFECTUSD     numeric(11, 4),
    @PORCENTDISCOUNT1      numeric(5, 2),
    @PORCENTDISCOUNT2      numeric(5, 2),
    @TOTDISCOUNTPEN        numeric(11, 4),
    @TOTDISCOUNTUSD        numeric(11, 4),
    @TOTRETURNEDPEN        numeric(11, 4),
    @TOTRETURNEDUSD        numeric(11, 4),
    @TOTCASHPEN            numeric(11, 4),
    @TOTCASHUSD            numeric(11, 4),
    @custidss              UD_CODCLIENT,
    @taxregnbr             UD_CODCLIENT,
    @CUSTNAME              UD_NOMBRE,
    @ADDR1                 UD_DESCRIPCION,
    @PLACA                 UD_PLACA,
    @NBRBONUS              numeric(10, 0)              = NULL,
    @CURYTYPEID            UD_CURYTYPEID,
    @CURYRATE              UD_CAMBIO,
    @PROMOTIONCARDID       char(20)                    = NULL,
    @POINTNUMBER           smallint,
    @SALESPOINT            UD_PTOVTA,
    @KILOMETRAJE           int                         = NULL,
    @STKSAVE               bit,
    @SALESTYPE             char(1),
    @PROCESSED             bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_SALESNC(CORRNBR,
                               DOCTYPEID,
                               NBRDOCUMENT,
                               TYPEPAYMENTID,
                               STKINVENTORY,
                               SITEID,
                               CURYID,
                               TERMID,
                               SALESPERID,
                               USERID,
                               DATEDOC,
                               DATEPROCESALES,
                               PERPOST,
                               TAXIGVID,
                               TAXISCID,
                               TOTALPEN,
                               TOTALUSD,
                               SUBTOTALPEN,
                               SUBTOTALUSD,
                               TAXIGVPEN,
                               TAXIGVUSD,
                               TAXISCPEN,
                               TAXISCUSD,
                               TOTALNOTAFFECTPEN,
                               TOTALNOTAFFECTUSD,
                               PORCENTDISCOUNT1,
                               PORCENTDISCOUNT2,
                               TOTDISCOUNTPEN,
                               TOTDISCOUNTUSD,
                               TOTRETURNEDPEN,
                               TOTRETURNEDUSD,
                               TOTCASHPEN,
                               TOTCASHUSD,
                               custidss,
                               taxregnbr,
                               CUSTNAME,
                               ADDR1,
                               PLACA,
                               NBRBONUS,
                               CURYTYPEID,
                               CURYRATE,
                               PROMOTIONCARDID,
                               POINTNUMBER,
                               SALESPOINT,
                               KILOMETRAJE,
                               STKSAVE,
                               SALESTYPE,
                               PROCESSED)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @NBRDOCUMENT,
           @TYPEPAYMENTID,
           @STKINVENTORY,
           @SITEID,
           @CURYID,
           @TERMID,
           @SALESPERID,
           @USERID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @TAXIGVID,
           @TAXISCID,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAXIGVPEN,
           @TAXIGVUSD,
           @TAXISCPEN,
           @TAXISCUSD,
           @TOTALNOTAFFECTPEN,
           @TOTALNOTAFFECTUSD,
           @PORCENTDISCOUNT1,
           @PORCENTDISCOUNT2,
           @TOTDISCOUNTPEN,
           @TOTDISCOUNTUSD,
           @TOTRETURNEDPEN,
           @TOTRETURNEDUSD,
           @TOTCASHPEN,
           @TOTCASHUSD,
           @custidss,
           @taxregnbr,
           @CUSTNAME,
           @ADDR1,
           @PLACA,
           @NBRBONUS,
           @CURYTYPEID,
           @CURYRATE,
           @PROMOTIONCARDID,
           @POINTNUMBER,
           @SALESPOINT,
           @KILOMETRAJE,
           @STKSAVE,
           @SALESTYPE,
           @PROCESSED)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_SALESNC: Cannot insert because primary key value not found in TMP_OP_SALESNC '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_SALESNC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_SALESNC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_SALESNC >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_OP_TURNVSSIDE 
 */

CREATE PROCEDURE SP_InsProcTMP_OP_TURNVSSIDE
(
    @CORRNBR     int,
    @SIDE        UD_CARA)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_TURNVSSIDE(CORRNBR,
                                  SIDE)
    VALUES(@CORRNBR,
           @SIDE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_OP_TURNVSSIDE: Cannot insert because primary key value not found in TMP_OP_TURNVSSIDE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_OP_TURNVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_OP_TURNVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_OP_TURNVSSIDE >>>'
go



/* 
 * PROCEDURE: SP_DelProcTMP_OP_TURNVSSIDE 
 */

CREATE PROCEDURE SP_DelProcTMP_OP_TURNVSSIDE
(
    @CORRNBR     int,
    @SIDE        UD_CARA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_OP_TURNVSSIDE
     WHERE CORRNBR = @CORRNBR
       AND SIDE    = @SIDE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_OP_TURNVSSIDE: Cannot delete because foreign keys still exist in TMP_OP_TURNVSSIDE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_OP_TURNVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_OP_TURNVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_OP_TURNVSSIDE >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_OP_TURNVSSIDE 
 */

CREATE PROCEDURE SP_SelProcTMP_OP_TURNVSSIDE
(
    @CORRNBR     int,
    @SIDE        UD_CARA)
AS
BEGIN
    SELECT CORRNBR,
           SIDE
      FROM TMP_OP_TURNVSSIDE
     WHERE CORRNBR = @CORRNBR
       AND SIDE    = @SIDE

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_OP_TURNVSSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_OP_TURNVSSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_OP_TURNVSSIDE >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_REGVENTAS1 
 */

CREATE PROCEDURE SP_InsProcTMP_REGVENTAS1
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @DESCR              UD_DESCRIPCION,
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             UD_IDSITE,
    @NAME               UD_DESCRIPCION,
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @TAXREGNBR          UD_CODCLIENT              = NULL,
    @CUSTNAME           UD_NOMBRE                 = NULL,
    @VAL_AFEC           numeric(11, 4),
    @VAL_NAFEC          numeric(11, 4),
    @VAL_IGV            numeric(11, 4),
    @TOTAL              numeric(11, 4),
    @SALESPOINT         UD_PTOVTA,
    @SERIALNUMBER       char(16),
    @DOCSTATUSID        char(2))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_REGVENTAS1(CORRNBR,
                               DOCTYPEID,
                               DESCR,
                               NBRDOCUMENT,
                               SITEID,
                               NAME,
                               DATEDOC,
                               DATEPROCESALES,
                               TAXREGNBR,
                               CUSTNAME,
                               VAL_AFEC,
                               VAL_NAFEC,
                               VAL_IGV,
                               TOTAL,
                               SALESPOINT,
                               SERIALNUMBER,
                               DOCSTATUSID)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @DESCR,
           @NBRDOCUMENT,
           @SITEID,
           @NAME,
           @DATEDOC,
           @DATEPROCESALES,
           @TAXREGNBR,
           @CUSTNAME,
           @VAL_AFEC,
           @VAL_NAFEC,
           @VAL_IGV,
           @TOTAL,
           @SALESPOINT,
           @SERIALNUMBER,
           @DOCSTATUSID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_REGVENTAS1: Cannot insert because primary key value not found in TMP_REGVENTAS1 '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_REGVENTAS1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_REGVENTAS1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_REGVENTAS1 >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_REGVENTAS2 
 */

CREATE PROCEDURE SP_InsProcTMP_REGVENTAS2
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @DESCR              UD_DESCRIPCION,
    @NBRDOCUMENTINI     numeric(10, 0),
    @NBRDOCUMENTFIN     numeric(10, 0)            = NULL,
    @SITEID             UD_IDSITE,
    @NAME               UD_DESCRIPCION,
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @TAXREGNBR          UD_CODCLIENT              = NULL,
    @CUSTNAME           UD_NOMBRE                 = NULL,
    @VAL_AFEC           numeric(11, 4),
    @VAL_NAFEC          numeric(11, 4),
    @VAL_IGV            numeric(11, 4),
    @TOTAL              numeric(11, 4),
    @SALESPOINT         UD_PTOVTA,
    @SERIALNUMBER       char(16))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_REGVENTAS2(CORRNBR,
                               DOCTYPEID,
                               DESCR,
                               NBRDOCUMENTINI,
                               NBRDOCUMENTFIN,
                               SITEID,
                               NAME,
                               DATEDOC,
                               DATEPROCESALES,
                               TAXREGNBR,
                               CUSTNAME,
                               VAL_AFEC,
                               VAL_NAFEC,
                               VAL_IGV,
                               TOTAL,
                               SALESPOINT,
                               SERIALNUMBER)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @DESCR,
           @NBRDOCUMENTINI,
           @NBRDOCUMENTFIN,
           @SITEID,
           @NAME,
           @DATEDOC,
           @DATEPROCESALES,
           @TAXREGNBR,
           @CUSTNAME,
           @VAL_AFEC,
           @VAL_NAFEC,
           @VAL_IGV,
           @TOTAL,
           @SALESPOINT,
           @SERIALNUMBER)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_REGVENTAS2: Cannot insert because primary key value not found in TMP_REGVENTAS2 '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_REGVENTAS2') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_REGVENTAS2 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_REGVENTAS2 >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_RESUMEN_VENTAS 
 */

CREATE PROCEDURE SP_InsProcTMP_RESUMEN_VENTAS
(
    @CORRNBR         int,
    @IDSUMARY        varchar(20),
    @CONTCRED        tinyint,
    @DESCR           UD_DESCRIPCION,
    @NBRPHANTNC      numeric(10, 0)            = NULL,
    @MONTONCBASE     numeric(11, 4),
    @MONTONCEXTR     numeric(11, 4),
    @NBRPHANTFC      numeric(10, 0)            = NULL,
    @MONTOFCBASE     numeric(11, 4),
    @MONTOFCEXTR     numeric(11, 4),
    @VALES           numeric(11, 4),
    @DIFERENCIA      numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_RESUMEN_VENTAS(CORRNBR,
                                   IDSUMARY,
                                   CONTCRED,
                                   DESCR,
                                   NBRPHANTNC,
                                   MONTONCBASE,
                                   MONTONCEXTR,
                                   NBRPHANTFC,
                                   MONTOFCBASE,
                                   MONTOFCEXTR,
                                   VALES,
                                   DIFERENCIA)
    VALUES(@CORRNBR,
           @IDSUMARY,
           @CONTCRED,
           @DESCR,
           @NBRPHANTNC,
           @MONTONCBASE,
           @MONTONCEXTR,
           @NBRPHANTFC,
           @MONTOFCBASE,
           @MONTOFCEXTR,
           @VALES,
           @DIFERENCIA)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_RESUMEN_VENTAS: Cannot insert because primary key value not found in TMP_RESUMEN_VENTAS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_RESUMEN_VENTAS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_RESUMEN_VENTAS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_RESUMEN_VENTAS >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTMP_RESUMEN_VENTAS 
 */

CREATE PROCEDURE SP_UpdProcTMP_RESUMEN_VENTAS
(
    @CORRNBR         int,
    @IDSUMARY        varchar(20),
    @CONTCRED        tinyint,
    @DESCR           UD_DESCRIPCION,
    @NBRPHANTNC      numeric(10, 0)            = NULL,
    @MONTONCBASE     numeric(11, 4),
    @MONTONCEXTR     numeric(11, 4),
    @NBRPHANTFC      numeric(10, 0)            = NULL,
    @MONTOFCBASE     numeric(11, 4),
    @MONTOFCEXTR     numeric(11, 4),
    @VALES           numeric(11, 4),
    @DIFERENCIA      numeric(11, 4))
AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_RESUMEN_VENTAS
       SET CONTCRED         = @CONTCRED,
           DESCR            = @DESCR,
           NBRPHANTNC       = @NBRPHANTNC,
           MONTONCBASE      = @MONTONCBASE,
           MONTONCEXTR      = @MONTONCEXTR,
           NBRPHANTFC       = @NBRPHANTFC,
           MONTOFCBASE      = @MONTOFCBASE,
           MONTOFCEXTR      = @MONTOFCEXTR,
           VALES            = @VALES,
           DIFERENCIA       = @DIFERENCIA
     WHERE CORRNBR  = @CORRNBR
       AND IDSUMARY = @IDSUMARY

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTMP_RESUMEN_VENTAS: Cannot update  in TMP_RESUMEN_VENTAS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTMP_RESUMEN_VENTAS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTMP_RESUMEN_VENTAS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTMP_RESUMEN_VENTAS >>>'
go


/* 
 * PROCEDURE: SP_DelProcTMP_RESUMEN_VENTAS 
 */

CREATE PROCEDURE SP_DelProcTMP_RESUMEN_VENTAS
(
    @CORRNBR         int,
    @IDSUMARY        varchar(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_RESUMEN_VENTAS
     WHERE CORRNBR  = @CORRNBR
       AND IDSUMARY = @IDSUMARY

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTMP_RESUMEN_VENTAS: Cannot delete because foreign keys still exist in TMP_RESUMEN_VENTAS '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTMP_RESUMEN_VENTAS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTMP_RESUMEN_VENTAS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTMP_RESUMEN_VENTAS >>>'
go


/* 
 * PROCEDURE: SP_SelProcTMP_RESUMEN_VENTAS 
 */

CREATE PROCEDURE SP_SelProcTMP_RESUMEN_VENTAS
(
    @CORRNBR         int,
    @IDSUMARY        varchar(20))
AS
BEGIN
    SELECT CORRNBR,
           IDSUMARY,
           CONTCRED,
           DESCR,
           NBRPHANTNC,
           MONTONCBASE,
           MONTONCEXTR,
           NBRPHANTFC,
           MONTOFCBASE,
           MONTOFCEXTR,
           VALES,
           DIFERENCIA
      FROM TMP_RESUMEN_VENTAS
     WHERE CORRNBR  = @CORRNBR
       AND IDSUMARY = @IDSUMARY

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTMP_RESUMEN_VENTAS') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTMP_RESUMEN_VENTAS >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTMP_RESUMEN_VENTAS >>>'
go


/* 
 * PROCEDURE: SP_InsProcTMP_TURNSIDE 
 */

CREATE PROCEDURE SP_InsProcTMP_TURNSIDE
(
    @CORRNBR     int,
    @SIDE        UD_CARA)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_TURNSIDE(CORRNBR,
                             SIDE)
    VALUES(@CORRNBR,
           @SIDE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_TURNSIDE: Cannot insert because primary key value not found in TMP_TURNSIDE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_TURNSIDE') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_TURNSIDE >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_TURNSIDE >>>'
go





/* 
 * PROCEDURE: SP_InsProcTMP_VENTAS_VALES 
 */

CREATE PROCEDURE SP_InsProcTMP_VENTAS_VALES
(
    @CORRNBR            int,
    @DOCTYPEID          UD_DOCTYPEID,
    @MINDES             char(5),
    @NBRDOCUMENT        numeric(10, 0),
    @SITEID             UD_IDSITE,
    @NAME               UD_DESCRIPCION,
    @CUSTIDSS           UD_CODCLIENT,
    @CUSTNAME           UD_NOMBRE,
    @RUC                UD_CODCLIENT,
    @NBRBONUS           varchar(200)                = NULL,
    @PLACA              UD_PLACA,
    @Codigo             UD_IDCORPORATIVO,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @Cantidad           numeric(10, 3),
    @Precio             numeric(8, 4),
    @Total              numeric(11, 4),
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_VENTAS_VALES(CORRNBR,
                                 DOCTYPEID,
                                 MINDES,
                                 NBRDOCUMENT,
                                 SITEID,
                                 NAME,
                                 CUSTIDSS,
                                 CUSTNAME,
                                 RUC,
                                 NBRBONUS,
                                 PLACA,
                                 Codigo,
                                 DESCRINVENTORY,
                                 Cantidad,
                                 Precio,
                                 Total,
                                 DATEDOC,
                                 DATEPROCESALES)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @MINDES,
           @NBRDOCUMENT,
           @SITEID,
           @NAME,
           @CUSTIDSS,
           @CUSTNAME,
           @RUC,
           @NBRBONUS,
           @PLACA,
           @Codigo,
           @DESCRINVENTORY,
           @Cantidad,
           @Precio,
           @Total,
           @DATEDOC,
           @DATEPROCESALES)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTMP_VENTAS_VALES: Cannot insert because primary key value not found in TMP_VENTAS_VALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTMP_VENTAS_VALES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTMP_VENTAS_VALES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTMP_VENTAS_VALES >>>'
go





/* 
 * PROCEDURE: SP_InsProcTYPEPAYMENT 
 */

CREATE PROCEDURE SP_InsProcTYPEPAYMENT
(
    @TYPEPAYMENTID     UD_TYPEPAYMENTID,
    @DESCR             char(40),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01            UD_USER1                    = NULL,
    @USER02            UD_USER2                    = NULL,
    @USER03            UD_USER6                    = NULL,
    @USER04            UD_USER7                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TYPEPAYMENT(TYPEPAYMENTID,
                            DESCR,
                            STKNEW,
                            STKMODIFY,
                            STKDELETE,
                            STKSEND,
                            STKRECEIVE,
                            USER01,
                            USER02,
                            USER03,
                            USER04 
                            )
    VALUES(@TYPEPAYMENTID,
           @DESCR,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcTYPEPAYMENT: Cannot insert because primary key value not found in TYPEPAYMENT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcTYPEPAYMENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcTYPEPAYMENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcTYPEPAYMENT >>>'
go


/* 
 * PROCEDURE: SP_UpdProcTYPEPAYMENT 
 */

CREATE PROCEDURE SP_UpdProcTYPEPAYMENT
(
    @TYPEPAYMENTID     UD_TYPEPAYMENTID,
    @DESCR             char(40),
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01            UD_USER1                    = NULL,
    @USER02            UD_USER2                    = NULL,
    @USER03            UD_USER6                    = NULL,
    @USER04            UD_USER7                    = NULL,
    @TSTAMP            timestamp                   = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TYPEPAYMENT
       SET DESCR              = @DESCR,
           STKNEW             = @STKNEW,
           STKMODIFY          = @STKMODIFY,
           STKDELETE          = @STKDELETE,
           STKSEND            = @STKSEND,
           STKRECEIVE         = @STKRECEIVE,
           USER01             = @USER01,
           USER02             = @USER02,
           USER03             = @USER03,
           USER04             = @USER04
     WHERE TYPEPAYMENTID = @TYPEPAYMENTID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcTYPEPAYMENT: Cannot update  in TYPEPAYMENT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcTYPEPAYMENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcTYPEPAYMENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcTYPEPAYMENT >>>'
go


/* 
 * PROCEDURE: SP_DelProcTYPEPAYMENT 
 */

CREATE PROCEDURE SP_DelProcTYPEPAYMENT
(
    @TYPEPAYMENTID     UD_TYPEPAYMENTID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TYPEPAYMENT
     WHERE TYPEPAYMENTID = @TYPEPAYMENTID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcTYPEPAYMENT: Cannot delete because foreign keys still exist in TYPEPAYMENT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcTYPEPAYMENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcTYPEPAYMENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcTYPEPAYMENT >>>'
go


/* 
 * PROCEDURE: SP_SelProcTYPEPAYMENT 
 */

CREATE PROCEDURE SP_SelProcTYPEPAYMENT
(
    @TYPEPAYMENTID     UD_TYPEPAYMENTID)
AS
BEGIN
    SELECT TYPEPAYMENTID,
           DESCR,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM TYPEPAYMENT
     WHERE TYPEPAYMENTID = @TYPEPAYMENTID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcTYPEPAYMENT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcTYPEPAYMENT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcTYPEPAYMENT >>>'
go


/* 
 * PROCEDURE: SP_InsProcXDIAS_PAGO 
 */

CREATE PROCEDURE SP_InsProcXDIAS_PAGO
(
    @DIAPAGID        char(10),
    @COMBDIA1        int                    = NULL,
    @COMBDIA2        int,
    @COMBDIA3        int                    = NULL,
    @COMBDIA4        int                    = NULL,
    @DESCR           varchar(50)            = NULL,
    @D1LUNES         int                    = NULL,
    @D2MARTES        int                    = NULL,
    @D3MIERCOLES     int                    = NULL,
    @D4JUEVES        int                    = NULL,
    @D5VIERNES       int                    = NULL,
    @D6SABADO        int                    = NULL,
    @D7DOMINGO       int                    = NULL,
    @FECCREACION     datetime               = NULL,
    @FECULTACT       datetime               = NULL,
    @STATUSSEM       int                    = NULL,
    @USERID          UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01          UD_USER1               = NULL,
    @USER02          UD_USER2               = NULL,
    @USER03          UD_USER6               = NULL,
    @USER04          UD_USER7               = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO XDIAS_PAGO(DIAPAGID,
                           COMBDIA1,
                           COMBDIA2,
                           COMBDIA3,
                           COMBDIA4,
                           DESCR,
                           D1LUNES,
                           D2MARTES,
                           D3MIERCOLES,
                           D4JUEVES,
                           D5VIERNES,
                           D6SABADO,
                           D7DOMINGO,
                           FECCREACION,
                           FECULTACT,
                           STATUSSEM,
                           USERID,
                           STKNEW,
                           STKMODIFY,
                           STKDELETE,
                           STKSEND,
                           STKRECEIVE,
                           USER01,
                           USER02,
                           USER03,
                           USER04 
                           )
    VALUES(@DIAPAGID,
           @COMBDIA1,
           @COMBDIA2,
           @COMBDIA3,
           @COMBDIA4,
           @DESCR,
           @D1LUNES,
           @D2MARTES,
           @D3MIERCOLES,
           @D4JUEVES,
           @D5VIERNES,
           @D6SABADO,
           @D7DOMINGO,
           @FECCREACION,
           @FECULTACT,
           @STATUSSEM,
           @USERID,
           @STKNEW,
           @STKMODIFY,
           @STKDELETE,
           @STKSEND,
           @STKRECEIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcXDIAS_PAGO: Cannot insert because primary key value not found in XDIAS_PAGO '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcXDIAS_PAGO') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcXDIAS_PAGO >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcXDIAS_PAGO >>>'
go


/* 
 * PROCEDURE: SP_UpdProcXDIAS_PAGO 
 */

CREATE PROCEDURE SP_UpdProcXDIAS_PAGO
(
    @DIAPAGID        char(10),
    @COMBDIA1        int                    = NULL,
    @COMBDIA2        int,
    @COMBDIA3        int                    = NULL,
    @COMBDIA4        int                    = NULL,
    @DESCR           varchar(50)            = NULL,
    @D1LUNES         int                    = NULL,
    @D2MARTES        int                    = NULL,
    @D3MIERCOLES     int                    = NULL,
    @D4JUEVES        int                    = NULL,
    @D5VIERNES       int                    = NULL,
    @D6SABADO        int                    = NULL,
    @D7DOMINGO       int                    = NULL,
    @FECCREACION     datetime               = NULL,
    @FECULTACT       datetime               = NULL,
    @STATUSSEM       int                    = NULL,
    @USERID          UD_USERID,
    @STKNEW         bit=1,
    @STKMODIFY      bit=0,
    @STKDELETE      bit=0,
    @STKSEND        bit=0,
    @STKRECEIVE      bit=0,
    @USER01          UD_USER1               = NULL,
    @USER02          UD_USER2               = NULL,
    @USER03          UD_USER6               = NULL,
    @USER04          UD_USER7               = NULL,
    @TIMESTAMP       timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE XDIAS_PAGO
       SET COMBDIA1         = @COMBDIA1,
           COMBDIA2         = @COMBDIA2,
           COMBDIA3         = @COMBDIA3,
           COMBDIA4         = @COMBDIA4,
           DESCR            = @DESCR,
           D1LUNES          = @D1LUNES,
           D2MARTES         = @D2MARTES,
           D3MIERCOLES      = @D3MIERCOLES,
           D4JUEVES         = @D4JUEVES,
           D5VIERNES        = @D5VIERNES,
           D6SABADO         = @D6SABADO,
           D7DOMINGO        = @D7DOMINGO,
           FECCREACION      = @FECCREACION,
           FECULTACT        = @FECULTACT,
           STATUSSEM        = @STATUSSEM,
           USERID           = @USERID,
           STKNEW           = @STKNEW,
           STKMODIFY        = @STKMODIFY,
           STKDELETE        = @STKDELETE,
           STKSEND          = @STKSEND,
           STKRECEIVE       = @STKRECEIVE,
           USER01           = @USER01,
           USER02           = @USER02,
           USER03           = @USER03,
           USER04           = @USER04
     WHERE DIAPAGID = @DIAPAGID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'SP_UpdProcXDIAS_PAGO: Cannot update  in XDIAS_PAGO '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_UpdProcXDIAS_PAGO') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_UpdProcXDIAS_PAGO >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_UpdProcXDIAS_PAGO >>>'
go


/* 
 * PROCEDURE: SP_DelProcXDIAS_PAGO 
 */

CREATE PROCEDURE SP_DelProcXDIAS_PAGO
(
    @DIAPAGID        char(10))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM XDIAS_PAGO
     WHERE DIAPAGID = @DIAPAGID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SP_DelProcXDIAS_PAGO: Cannot delete because foreign keys still exist in XDIAS_PAGO '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcXDIAS_PAGO') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcXDIAS_PAGO >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcXDIAS_PAGO >>>'
go


/* 
 * PROCEDURE: SP_SelProcXDIAS_PAGO 
 */

CREATE PROCEDURE SP_SelProcXDIAS_PAGO
(
    @DIAPAGID        char(10))
AS
BEGIN
    SELECT DIAPAGID,
           COMBDIA1,
           COMBDIA2,
           COMBDIA3,
           COMBDIA4,
           DESCR,
           D1LUNES,
           D2MARTES,
           D3MIERCOLES,
           D4JUEVES,
           D5VIERNES,
           D6SABADO,
           D7DOMINGO,
           FECCREACION,
           FECULTACT,
           STATUSSEM,
           USERID,
           STKNEW,
           STKMODIFY,
           STKDELETE,
           STKSEND,
           STKRECEIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TIMESTAMP
      FROM XDIAS_PAGO
     WHERE DIAPAGID = @DIAPAGID

    RETURN(0)
END
go
IF OBJECT_ID('SP_SelProcXDIAS_PAGO') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_SelProcXDIAS_PAGO >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_SelProcXDIAS_PAGO >>>'
go


/* 
 * PROCEDURE: SP_InsProcXTMP_DIAS_SEMANA_PAGO 
 */

CREATE PROCEDURE SP_InsProcXTMP_DIAS_SEMANA_PAGO
(
    @DIA      int            = NULL,
    @SPID     int            = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO XTMP_DIAS_SEMANA_PAGO(DIA,
                                      SPID)
    VALUES(@DIA,
           @SPID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcXTMP_DIAS_SEMANA_PAGO: Cannot insert because primary key value not found in XTMP_DIAS_SEMANA_PAGO '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
go
IF OBJECT_ID('SP_InsProcXTMP_DIAS_SEMANA_PAGO') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_InsProcXTMP_DIAS_SEMANA_PAGO >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_InsProcXTMP_DIAS_SEMANA_PAGO >>>'
go





