/*
 *ER/Studio 5.5 SQL Code Generation
 * Company :      CHEVRON LUBRICANTS SA
 * Project :      Model
 * Author :       PERSIESWS02
 *
 * Date Created : Thursday, November 07, 2002 18:22:46
 * Target DBMS : Microsoft SQL Server 7.x
 */

USE master
go
CREATE DATABASE sies
ON (NAME='sies_data', FILENAME='d:\data\sies.mdf', SIZE=50)
LOG ON (NAME='sies_log', FILENAME='d:\data\sies_log_log.ldf', SIZE=5)

go
USE sies
go

CREATE DEFAULT DF_CERO AS 0
go

CREATE DEFAULT DF_CORRELATIVO AS 1
go

CREATE DEFAULT DF_ESPACIO AS ' '
go

CREATE DEFAULT DF_FECHA AS GETDATE()
go

CREATE DEFAULT DF_PORCENT AS 0
go

CREATE DEFAULT DF_REGISTRO AS 1
go

CREATE DEFAULT DF_TURNOPERSONAL AS 1
go

CREATE DEFAULT DF_TYPE_DISP AS 0
go

CREATE DEFAULT DF_VALORPRECIO AS 0
go

CREATE DEFAULT df_Valorstock AS 0
go

EXEC sp_addtype UD_ACCOUNTBACK, "char(10)","NOT NULL"
go
EXEC sp_bindefault DF_ESPACIO, UD_ACCOUNTBACK
go

EXEC sp_addtype UD_ACCOUNTBANK, "char(10)","NULL"
go
EXEC sp_bindefault DF_ESPACIO, UD_ACCOUNTBANK
go

EXEC sp_addtype UD_ACCOUNTCARD, "char(15)","NULL"
go

EXEC sp_addtype UD_CAMBIO, "numeric(6,4)","NOT NULL"
go

EXEC sp_addtype UD_CANTDECIMAL, tinyint,"NOT NULL"
go

EXEC sp_addtype UD_CARA, "char(2)","NOT NULL"
go

EXEC sp_addtype UD_CATEGORY, "char(5)","NOT NULL"
go

EXEC sp_addtype UD_CODCLIENT, "char(20)","NOT NULL"
go

EXEC sp_addtype UD_CODIGOBARRA, "char(20)","NOT NULL"
go

EXEC sp_addtype UD_CORR_CONTOMET, "numeric(10,0)","NOT NULL"
go

EXEC sp_addtype UD_CORREL_DEPOSIT, tinyint,"NOT NULL"
go

EXEC sp_addtype UD_CORRELATIVOS, "numeric(10,0)","NOT NULL"
go
EXEC sp_bindefault DF_CORRELATIVO, UD_CORRELATIVOS
go

EXEC sp_addtype UD_COSTO, "numeric(8,4)","NOT NULL"
go
EXEC sp_bindefault DF_VALORPRECIO, UD_COSTO
go

EXEC sp_addtype UD_CURYID, "char(3)","NOT NULL"
go

EXEC sp_addtype UD_CURYRATE, "numeric(6,4)","NOT NULL"
go

EXEC sp_addtype UD_CURYTYPEID, "char(6)","NOT NULL"
go

EXEC sp_addtype UD_DEBE_HABER, "char(1)","NOT NULL"
go

EXEC sp_addtype UD_DESCRIPCION, "varchar(40)","NOT NULL"
go

EXEC sp_addtype UD_DEUDA, "numeric(9,2)","NOT NULL"
go
EXEC sp_bindefault DF_CERO, UD_DEUDA
go

EXEC sp_addtype UD_DIRECCION, "varchar(40)","NOT NULL"
go

EXEC sp_addtype UD_DISP_SALIDA, "varchar(10)","NOT NULL"
go

EXEC sp_addtype UD_DOCSTATUS, "char(2)","NOT NULL"
go

EXEC sp_addtype UD_DOCTYPEID, "char(3)","NOT NULL"
go

EXEC sp_addtype UD_FECHA, smalldatetime,"NOT NULL"
go
EXEC sp_bindefault DF_FECHA, UD_FECHA
go

EXEC sp_addtype UD_IDCORPORATIVO, "char(20)","NOT NULL"
go

EXEC sp_addtype UD_IDSITE, "char(5)","NOT NULL"
go

EXEC sp_addtype UD_IDVENDEDOR, "char(10)","NOT NULL"
go

EXEC sp_addtype UD_IDVENDOR, "char(20)","NOT NULL"
go

EXEC sp_addtype UD_IMPUESTO, "numeric(9,2)","NOT NULL"
go
EXEC sp_bindefault DF_VALORPRECIO, UD_IMPUESTO
go

EXEC sp_addtype UD_KITID, "char(20)","NOT NULL"
go

EXEC sp_addtype UD_MODULOID, "char(2)","NOT NULL"
go

EXEC sp_addtype UD_MONTO, "numeric(9,2)","NOT NULL"
go
EXEC sp_bindefault DF_VALORPRECIO, UD_MONTO
go

EXEC sp_addtype UD_NOMBRE, "varchar(40)","NULL"
go

EXEC sp_addtype UD_PERPOST, "char(6)","NOT NULL"
go

EXEC sp_addtype UD_PLACA, "varchar(10)","NOT NULL"
go

EXEC sp_addtype UD_PORCENTAJE, "numeric(5,2)","NOT NULL"
go
EXEC sp_bindefault DF_PORCENT, UD_PORCENTAJE
go

EXEC sp_addtype UD_PRCLVLCUSTID, "char(10)","NOT NULL"
go

EXEC sp_addtype UD_PRECIO, "numeric(8,4)","NOT NULL"
go
EXEC sp_bindefault DF_VALORPRECIO, UD_PRECIO
go

EXEC sp_addtype UD_PRECLVID, "char(5)","NOT NULL"
go

EXEC sp_addtype UD_PTOVTA, "char(10)","NOT NULL"
go

EXEC sp_addtype UD_REGISTRO, smallint,"NOT NULL"
go
EXEC sp_bindefault DF_REGISTRO, UD_REGISTRO
go

EXEC sp_addtype UD_SCREENID, "char(7)","NOT NULL"
go

EXEC sp_addtype UD_STATUSPERSONID, "char(2)","NOT NULL"
go

EXEC sp_addtype UD_STKUNITID, "char(5)","NOT NULL"
go

EXEC sp_addtype UD_STOCK, "numeric(10,3)","NOT NULL"
go
EXEC sp_bindefault df_Valorstock, UD_STOCK
go

EXEC sp_addtype UD_TAXID, "char(2)","NOT NULL"
go

EXEC sp_addtype UD_TELEFONO, "char(10)","NOT NULL"
go

EXEC sp_addtype UD_TOTZETA, "numeric(10,2)","NOT NULL"
go
EXEC sp_bindefault DF_CERO, UD_TOTZETA
go

EXEC sp_addtype UD_TURNOSPERSONAL, tinyint,"NOT NULL"
go
EXEC sp_bindefault DF_TURNOPERSONAL, UD_TURNOSPERSONAL
go

EXEC sp_addtype UD_TYPE_DISP_SALIDA, "varchar(1)","NOT NULL"
go
EXEC sp_bindefault DF_TYPE_DISP, UD_TYPE_DISP_SALIDA
go

EXEC sp_addtype UD_TYPEDOC, "char(5)","NOT NULL"
go

EXEC sp_addtype UD_TYPEMOVEID, "char(5)","NOT NULL"
go

EXEC sp_addtype UD_USER1, "varchar(30)","NULL"
go

EXEC sp_addtype UD_USER2, "varchar(30)","NULL"
go

EXEC sp_addtype UD_USER3, "varchar(30)","NULL"
go

EXEC sp_addtype UD_USER4, smalldatetime,"NULL"
go

EXEC sp_addtype UD_USER5, smalldatetime,"NULL"
go

EXEC sp_addtype UD_USER6, float,"NULL"
go

EXEC sp_addtype UD_USER7, float,"NULL"
go

EXEC sp_addtype UD_USER8, smallint,"NULL"
go

EXEC sp_addtype UD_USER9, smallint,"NULL"
go

EXEC sp_addtype UD_USERID, "char(20)","NOT NULL"
go

/* 
 * TABLE: FACTUDET 
 */

CREATE TABLE FACTUDET(
    CDTIPODOCU    nvarchar(3)      NULL,
    NRODOCUM      float            NULL,
    NROCOMANDA    float            NULL,
    NROITEM       float            NULL,
    CDARTICULO    nvarchar(20)     NULL,
    CDALTERNAT    nvarchar(20)     NULL,
    DSARTICULO    nvarchar(45)     NULL,
    CANTIDAD      float            NULL,
    CDMONEDA      nvarchar(1)      NULL,
    MTOCOSTO      float            NULL,
    MTOPRECIO     float            NULL,
    SUBTOTAL      float            NULL,
    IGV           float            NULL,
    TOTAL         float            NULL,
    PORCIGV       float            NULL,
    PORCSERVIC    float            NULL,
    MTOSERVIC     float            NULL,
    PORCDCTOI1    float            NULL,
    PORCDCTOI2    float            NULL,
    PORCDCTOI3    float            NULL,
    MTODSCTOI     float            NULL,
    FECDOCUM      smalldatetime    NULL,
    CANTNCRED     float            NULL,
    TOMAINVFIS    bit              DEFAULT (0) NULL,
    LIQCONSIG     bit              NOT NULL,
    CIERREZ       bit              NOT NULL,
    FECPROCESO    smalldatetime    NULL,
    CDPROVEE      nvarchar(8)      NULL,
    FLAGCOPIA     bit              NOT NULL
) 
go


IF OBJECT_ID('FACTUDET') IS NOT NULL
    PRINT '<<< CREATED TABLE FACTUDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE FACTUDET >>>'
go

/* 
 * TABLE: FACTURA 
 */

CREATE TABLE FACTURA(
    CDTIPODOCU        nvarchar(3)      NULL,
    NRODOCUM          float            NULL,
    NROGUIA           float            NULL,
    NROPROFORM        float            NULL,
    SUBTOTAL          float            NULL,
    IGV               float            NULL,
    TOTAL             float            NULL,
    MTOGTOFIN         float            NULL,
    FECDOCUM          smalldatetime    NULL,
    MTOPAGEFECS       float            NULL,
    MTOPAGEFECD       float            NULL,
    MTOPAGTAR1S       float            NULL,
    MTOPAGTAR1D       float            NULL,
    MTOPAGTAR2S       float            NULL,
    MTOPAGTAR2D       float            NULL,
    MTOPAGTAR3S       float            NULL,
    MTOPAGTAR3D       float            NULL,
    MTOPAGTAR4S       float            NULL,
    MTOPAGTAR4D       float            NULL,
    MTOPAGCHEQS       float            NULL,
    MTOPAGCHEQD       float            NULL,
    MTOPAGCREDS       float            NULL,
    MTOPAGCREDD       float            NULL,
    MTOPAGCREDITOS    float            NULL,
    MTOPAGCREDITOD    float            NULL,
    MTOVUELTS         float            NULL,
    MTOVUELTD         float            NULL,
    CDTARJCRE1        nvarchar(5)      NULL,
    NROTARJCR1        nvarchar(20)     NULL,
    CDTARJCRE2        nvarchar(5)      NULL,
    NROTARJCR2        nvarchar(20)     NULL,
    CDTARJCRE3        nvarchar(5)      NULL,
    NROTARJCR3        nvarchar(20)     NULL,
    CDTARJCRE4        nvarchar(5)      NULL,
    NROTARJCR4        nvarchar(20)     NULL,
    CDBANCO           nvarchar(5)      NULL,
    NROCTABANC        nvarchar(20)     NULL,
    NROCHQBANC        nvarchar(20)     NULL,
    ESTADO            nvarchar(10)     NULL,
    CDMONEDA          nvarchar(1)      NULL,
    CAMBIO            float            NULL,
    CAMBIOC           float            NULL,
    TIPCREDFAC        nvarchar(5)      NULL,
    TIPCREDLET        nvarchar(5)      NULL,
    CDCLIENTE         nvarchar(15)     NULL,
    NOMCLIENT         nvarchar(50)     NULL,
    DIRCLIENT         nvarchar(60)     NULL,
    RUCCLIENT         nvarchar(15)     NULL,
    CDUSUARIO         nvarchar(8)      NULL,
    CDVENDEDOR        nvarchar(15)     NULL,
    PORCSERVIC        float            NULL,
    MTOSERVICI        float            NULL,
    MTODSCTO          float            NULL,
    PORCDCTO1         float            NULL,
    PORCDCTO2         float            NULL,
    PORCDCTO3         float            NULL,
    CDALMACEN         nvarchar(2)      NULL,
    NROPTOVTA         nvarchar(10)     NULL,
    NROORDENC         nvarchar(15)     NULL,
    TIPOFACTUR        nvarchar(2)      NULL,
    CIERREZ           bit              NOT NULL,
    FECPROCESO        smalldatetime    NULL,
    CODTRANSP         nvarchar(15)     NULL,
    DOPARTIDA         nvarchar(45)     NULL,
    DODESTINO         nvarchar(45)     NULL,
    PLACA             nvarchar(10)     NULL,
    CDUSERANUL        nvarchar(8)      NULL,
    FECANULDOC        smalldatetime    NULL,
    TOTANUL           float            NULL,
    STOANUL           float            NULL,
    IGVANUL           float            NULL,
    CDPRECIO1         nvarchar(3)      NULL,
    CDPRECIO2         nvarchar(3)      NULL,
    IMPRECONSUMO      bit              NOT NULL,
    MTOPROPINA        float            NULL,
    MONPROPINA        nvarchar(1)      NULL,
    NROREFERENCIA     float            NULL,
    FLAGCOPIA         bit              NOT NULL
) 
go


IF OBJECT_ID('FACTURA') IS NOT NULL
    PRINT '<<< CREATED TABLE FACTURA >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE FACTURA >>>'
go

/* 
 * TABLE: IN_CLUSTER 
 */

CREATE TABLE IN_CLUSTER(
    CLASSUBID           char(5)          NOT NULL,
    CLASSID             char(5)          NOT NULL,
    INTRDATEPHISICAL    smalldatetime    NOT NULL
) 
go


IF OBJECT_ID('IN_CLUSTER') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_CLUSTER >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_CLUSTER >>>'
go

/* 
 * TABLE: IN_INVENTHISTOPHISI 
 */

CREATE TABLE IN_INVENTHISTOPHISI(
    SITEID              char(5)           NOT NULL,
    INVTIDSKU           char(20)          NOT NULL,
    INTRDATEPHISICAL    smalldatetime     NOT NULL,
    CLASSID             char(5)           NOT NULL,
    CLASSUBID           char(5)           NOT NULL,
    STOCKPHISICAL       numeric(10, 3)    DEFAULT (0) NOT NULL,
    QTYAVAIL            numeric(10, 3)    DEFAULT (0) NOT NULL
) 
go


IF OBJECT_ID('IN_INVENTHISTOPHISI') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INVENTHISTOPHISI >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INVENTHISTOPHISI >>>'
go

/* 
 * TABLE: IN_SETUP 
 */

CREATE TABLE IN_SETUP(
    PERPOST              UD_PERPOST       NOT NULL,
    INTYPETRANSFERIN     UD_TYPEDOC       NOT NULL,
    INTYPETRANSFEROUT    UD_TYPEDOC       NOT NULL,
    TSTAMP               timestamp        NOT NULL,
    REPORTPATH           char(50)         NOT NULL,
    INTYPESALES          UD_TYPEDOC       NOT NULL,
    INTYPEDESIN          UD_TYPEDOC       NOT NULL,
    INTYPEDESOUT         UD_TYPEDOC       NOT NULL,
    INTYPECREDMEM        UD_TYPEDOC       NOT NULL,
    INTYPEBONUS          UD_TYPEDOC       NOT NULL,
    INTYPEFISIN          UD_TYPEDOC       NOT NULL,
    INTYPEFISOUT         UD_TYPEDOC       NOT NULL,
    INTYPETRBUYSTORE     UD_TYPEDOC       NOT NULL,
    INTYPETRBUYFUELS     UD_TYPEDOC       NOT NULL,
    BATNBRTYPEDOC        char(10)         NOT NULL,
    BATNBRTYPEPRICE      char(10)         NOT NULL,
    STKFISI              bit              NOT NULL,
    INTYPEQTYINI         UD_TYPEDOC       NOT NULL,
    STKSTOCK             bit              DEFAULT (0) NOT NULL,
    DOCTYPEIDFIS         UD_DOCTYPEID     DEFAULT (' ') NOT NULL,
    IDVENDORSS           UD_IDVENDOR      DEFAULT (' ') NOT NULL,
    USER1                varchar(30)      NULL,
    USER2                varchar(30)      NULL,
    USER3                varchar(30)      NULL,
    USER4                smalldatetime    NULL,
    USER5                smalldatetime    NULL,
    USER6                float            NULL,
    USER7                float            NULL,
    USER8                smallint         NULL,
    USER9                smallint         NULL
) 
go


IF OBJECT_ID('IN_SETUP') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_SETUP >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_SETUP >>>'
go

/* 
 * TABLE: OP_SALESPOINTVSCARA 
 */

CREATE TABLE OP_SALESPOINTVSCARA(
    SALESPOINT    char(10)    NOT NULL,
    CARA          UD_CARA     NOT NULL
) 
go


IF OBJECT_ID('OP_SALESPOINTVSCARA') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_SALESPOINTVSCARA >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_SALESPOINTVSCARA >>>'
go

/* 
 * TABLE: OP_SETUP 
 */

CREATE TABLE OP_SETUP(
    AUTOMATICSALES    bit              NOT NULL,
    CATEGORYFUEL      char(5)          NOT NULL,
    PLACA             bit              NOT NULL,
    MTOMAXSALES       numeric(9, 2)    DEFAULT (0) NOT NULL,
    ACCOUNTCARD       char(15)         NOT NULL,
    DATEPROCESALES    smalldatetime    DEFAULT (getdate()) NOT NULL,
    USER1             varchar(30)      NULL,
    USER2             varchar(30)      NULL,
    USER3             varchar(30)      NULL,
    USER4             smalldatetime    NULL,
    USER5             smalldatetime    NULL,
    USER6             float            NULL,
    USER7             float            NULL,
    USER8             smallint         NULL,
    USER9             smallint         NULL,
    TSTAMP            timestamp        NULL
) 
go


IF OBJECT_ID('OP_SETUP') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_SETUP >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_SETUP >>>'
go

/* 
 * TABLE: SETUP 
 */

CREATE TABLE SETUP(
    TYPEPRICEUPDATE       char(5)          NOT NULL,
    CUSTID                char(20)         NOT NULL,
    QTYSALESPERSONTURN    tinyint          NOT NULL,
    SALESPERSONTURN       tinyint          NOT NULL,
    QTYCARAS              tinyint          NOT NULL,
    SOURCESITE            char(5)          NOT NULL,
    CURYORIG              char(3)          NOT NULL,
    FLOATPRICE            tinyint          NOT NULL,
    FLOATCOST             tinyint          NOT NULL,
    FLOATQTY              tinyint          NOT NULL,
    FLOATOUTPUT           tinyint          NOT NULL,
    TAXVALUEIGV           char(2)          NOT NULL,
    TAXID                 char(2)          NOT NULL,
    TSTAMP                timestamp        NULL,
    SOURCECURYTYPEID      char(6)          NOT NULL,
    DATEPROCE             smalldatetime    DEFAULT (getdate()) NOT NULL,
    USER1                 varchar(30)      NULL,
    USER2                 varchar(30)      NULL,
    USER3                 varchar(30)      NULL,
    USER4                 smalldatetime    NULL,
    USER5                 smalldatetime    NULL,
    USER6                 float            NULL,
    USER7                 float            NULL,
    USER8                 smallint         NULL,
    USER9                 smallint         NULL
) 
go


IF OBJECT_ID('SETUP') IS NOT NULL
    PRINT '<<< CREATED TABLE SETUP >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE SETUP >>>'
go

/* 
 * TABLE: TMP_CORRNBRHEAD 
 */

CREATE TABLE TMP_CORRNBRHEAD(
    CORRNBR    int          IDENTITY(1,1),
    USERID     UD_USERID    NOT NULL
) 
go


IF OBJECT_ID('TMP_CORRNBRHEAD') IS NOT NULL
    PRINT '<<< CREATED TABLE TMP_CORRNBRHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TMP_CORRNBRHEAD >>>'
go

/* 
 * TABLE: TMP_IN_INTRAITEM 
 */

CREATE TABLE TMP_IN_INTRAITEM(
    CORRNBR          int                 NOT NULL,
    SITEID           UD_IDSITE           NOT NULL,
    TYPEDOCID        UD_TYPEMOVEID       NOT NULL,
    SEQUENCE         smallint            NOT NULL,
    INVTIDSKU        UD_IDCORPORATIVO    NOT NULL,
    INVTIDBARR       UD_CODIGOBARRA      NULL,
    QTY              UD_STOCK            NOT NULL,
    TAXIGV           numeric(4, 2)       NULL,
    STDCOSTPEN       UD_COSTO            NOT NULL,
    STDCOSTUSD       UD_COSTO            NOT NULL,
    STKFISI          bit                 NOT NULL,
    INTRDATE         smalldatetime       NOT NULL,
    INTRDATEPROCE    smalldatetime       NOT NULL,
    RLSED            bit                 NOT NULL,
    TAXISC           numeric(4, 2)       DEFAULT (0) NOT 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
) 
go


IF OBJECT_ID('TMP_IN_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED TABLE TMP_IN_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TMP_IN_INTRAITEM >>>'
go

/* 
 * TABLE: TMP_IN_KITCOMPONENT 
 */

CREATE TABLE TMP_IN_KITCOMPONENT(
    CORRNBR       int                 NOT NULL,
    KITID         UD_KITID            NOT NULL,
    INVTIDSKU     UD_IDCORPORATIVO    NOT NULL,
    SEQUENCE      tinyint             NOT NULL,
    STDCOSTPEN    UD_COSTO            NOT NULL,
    STDCOSTUSD    UD_COSTO            NOT NULL,
    QTY           numeric(9, 3)       NOT NULL,
    USER1         UD_USER1            NULL,
    USER2         UD_USER2            NULL,
    USER3         UD_USER6            NULL,
    USER4         UD_USER7            NULL,
    TSTAMP        timestamp           NULL
) 
go


IF OBJECT_ID('TMP_IN_KITCOMPONENT') IS NOT NULL
    PRINT '<<< CREATED TABLE TMP_IN_KITCOMPONENT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TMP_IN_KITCOMPONENT >>>'
go

/* 
 * TABLE: TMP_OP_SALESDET 
 */

CREATE TABLE TMP_OP_SALESDET(
    CORRNBR           int               NOT NULL,
    DOCTYPEID         char(3)           NOT NULL,
    NBRDOCUMENT       numeric(10, 0)    NOT NULL,
    SEQUENCE          smallint          NOT NULL,
    SITEID            char(5)           NOT NULL,
    INVTIDSKU         char(20)          NOT NULL,
    CURYID            char(3)           NOT NULL,
    DOCSTATUSID       char(2)           NOT NULL,
    DATEDOC           smalldatetime     NOT NULL,
    DATEPROCESALES    smalldatetime     NOT NULL,
    PERSPOT           char(6)           NOT NULL,
    STKCLOSEDZ        bit               NOT NULL,
    STKCLOSEDX        bit               NOT NULL,
    NBRTURN           tinyint           NOT NULL,
    NBRCARA           char(2)           NULL,
    NBRTRANSACFUEL    char(4)           NULL,
    DISCDSCTO1        numeric(5, 2)     NOT NULL,
    DISCDSCTO2        numeric(5, 2)     NOT NULL,
    DISCMTOPEN        numeric(9, 2)     NOT NULL,
    DISCMTOUSD        numeric(9, 2)     NOT NULL,
    PORCENTTAXIGV     numeric(5, 2)     NOT NULL,
    PORCENTTAXISC     numeric(5, 2)     NOT NULL,
    TOTALPEN          numeric(9, 2)     NOT NULL,
    TOTALUSD          numeric(9, 2)     NOT NULL,
    TAXPEN            numeric(9, 2)     NOT NULL,
    TAXUSD            numeric(9, 2)     NOT NULL,
    STKITEM           bit               NOT NULL,
    STKFISI           bit               NOT NULL,
    SLS_PRICE         numeric(8, 4)     NOT NULL,
    SLSPRICESALE      numeric(8, 4)     NOT NULL,
    STDCOSTPEN        numeric(8, 4)     NOT NULL,
    STDCOSTUSD        numeric(8, 4)     NOT NULL,
    INVTIDALTER       char(20)          NOT NULL,
    DESCRINVENTORY    varchar(40)       NOT NULL,
    QTY               numeric(10, 3)    NOT NULL,
    USER1             varchar(30)       NULL,
    USER2             varchar(30)       NULL,
    USER3             varchar(30)       NULL,
    USER4             smalldatetime     NULL,
    USER5             smalldatetime     NULL,
    USER6             float             NULL,
    USER7             float             NULL,
    USER8             smallint          NULL,
    USER9             smallint          NULL,
    TSTAMP            timestamp         NULL,
    SALESPOINT        char(10)          NOT NULL,
    SALESPERID        char(10)          NOT NULL
) 
go


IF OBJECT_ID('TMP_OP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED TABLE TMP_OP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TMP_OP_SALESDET >>>'
go

/* 
 * TABLE: AR_CCDOC 
 */

CREATE TABLE AR_CCDOC(
    DOCTYPEID         char(3)           NOT NULL,
    NBRDOCUMENT       numeric(10, 0)    DEFAULT (1) NOT NULL,
    CURYTYPEID        char(6)           NOT NULL,
    DOCDATE           smalldatetime     DEFAULT (getdate()) NOT NULL,
    DATEPROCESALES    smalldatetime     DEFAULT (getdate()) NOT NULL,
    DUEDATE           smalldatetime     DEFAULT (getdate()) NOT NULL,
    TOTALPEN          numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTALUSD          numeric(9, 2)     DEFAULT (0) NOT NULL,
    PAIDMENTPEN       numeric(9, 2)     DEFAULT (0) NOT NULL,
    PAIDMENTUSD       numeric(9, 2)     DEFAULT (0) NOT NULL,
    DOCVALPEN         numeric(9, 2)     DEFAULT (0) NOT NULL,
    DOCVALUSD         numeric(9, 2)     DEFAULT (0) NOT NULL,
    MODSALESID        char(2)           NOT NULL,
    CUSTIDSS          char(20)          NOT NULL,
    TAXREGNBR         char(20)          NULL,
    DOCSTATUSID       char(2)           NOT NULL,
    CURYRATE          numeric(6, 4)     NOT NULL,
    CURYID            char(3)           NOT NULL,
    WEEKDAYPAG        char(12)          NOT NULL,
    DAYTHENK          int               NOT NULL,
    USER1             varchar(30)       NULL,
    USER2             varchar(30)       NULL,
    USER3             float             NULL,
    USER4             float             NULL,
    TSTAMP            timestamp         NULL,
    CONSTRAINT PK202 PRIMARY KEY NONCLUSTERED (DOCTYPEID, NBRDOCUMENT)
) 
go


IF OBJECT_ID('AR_CCDOC') IS NOT NULL
    PRINT '<<< CREATED TABLE AR_CCDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE AR_CCDOC >>>'
go

/* 
 * TABLE: AR_CCDOCDET 
 */

CREATE TABLE AR_CCDOCDET(
    DOCTYPEID            char(3)           NOT NULL,
    NBRDOCUMENT          numeric(10, 0)    DEFAULT (1) NOT NULL,
    SEQUENCE             smallint          DEFAULT (1) NOT NULL,
    CURYTYPEID           char(6)           NOT NULL,
    DOCSTATUSID          char(2)           NOT NULL,
    DOCDATE              smalldatetime     DEFAULT (getdate()) NOT NULL,
    DATEPROCEPAIDMENT    smalldatetime     DEFAULT (getdate()) NOT NULL,
    DATEPROCESALES       smalldatetime     DEFAULT (getdate()) NOT NULL,
    BANKID               char(2)           NOT NULL,
    CHEQNUMBER           char(15)          NOT NULL,
    NBRBANKACCOUNT       char(15)          NOT NULL,
    TOTALPEN             numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTALUSD             numeric(9, 2)     DEFAULT (0) NOT NULL,
    PAIDMENTPEN          numeric(9, 2)     DEFAULT (0) NOT NULL,
    PAIDMENTUSD          numeric(9, 2)     DEFAULT (0) NOT NULL,
    CURYID               char(3)           NOT NULL,
    CURYRATE             numeric(6, 4)     NOT NULL,
    USER1                varchar(30)       NULL,
    USER2                varchar(30)       NULL,
    USER3                float             NULL,
    USER4                float             NULL,
    TSTAMP               timestamp         NULL,
    CONSTRAINT PK200 PRIMARY KEY NONCLUSTERED (DOCTYPEID, NBRDOCUMENT, SEQUENCE)
) 
go


IF OBJECT_ID('AR_CCDOCDET') IS NOT NULL
    PRINT '<<< CREATED TABLE AR_CCDOCDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE AR_CCDOCDET >>>'
go

/* 
 * TABLE: BANK 
 */

CREATE TABLE BANK(
    BANKID         char(2)        NOT NULL,
    DESCR          varchar(40)    NOT NULL,
    BANKACCOUNT    char(10)       DEFAULT (' ') NOT NULL,
    USER01         varchar(30)    NULL,
    USER02         varchar(30)    NULL,
    USER03         float          NULL,
    USER04         float          NULL,
    TSTAMP         timestamp      NULL,
    CONSTRAINT PK_BANCK PRIMARY KEY NONCLUSTERED (BANKID)
) 
go


IF OBJECT_ID('BANK') IS NOT NULL
    PRINT '<<< CREATED TABLE BANK >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE BANK >>>'
go

/* 
 * TABLE: CITY 
 */

CREATE TABLE CITY(
    CITYID     char(3)           NOT NULL,
    STATEID    char(2)           NOT NULL,
    DESCR      UD_DESCRIPCION    NOT NULL,
    TSTAMP     timestamp         NULL,
    CONSTRAINT PK__CITY PRIMARY KEY NONCLUSTERED (CITYID)
) 
go


IF OBJECT_ID('CITY') IS NOT NULL
    PRINT '<<< CREATED TABLE CITY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CITY >>>'
go

/* 
 * TABLE: COUNTRY 
 */

CREATE TABLE COUNTRY(
    COUNTRYID    char(3)           NOT NULL,
    DESCR        UD_DESCRIPCION    NOT NULL,
    TSTAMP       timestamp         NULL,
    CONSTRAINT PK__COUNTRY PRIMARY KEY NONCLUSTERED (COUNTRYID)
) 
go


IF OBJECT_ID('COUNTRY') IS NOT NULL
    PRINT '<<< CREATED TABLE COUNTRY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE COUNTRY >>>'
go

/* 
 * TABLE: CURRENCY 
 */

CREATE TABLE CURRENCY(
    CURYID        UD_CURYID         NOT NULL,
    DESCR         UD_DESCRIPCION    NOT NULL,
    DESCRMONEY    char(5)           NOT NULL,
    USER1         UD_USER1          NULL,
    USER2         UD_USER2          NULL,
    USER3         UD_USER6          NULL,
    USER4         UD_USER7          NULL,
    TSTAMP        timestamp         NULL,
    CONSTRAINT PK_CURRENCY PRIMARY KEY NONCLUSTERED (CURYID)
) 
go


IF OBJECT_ID('CURRENCY') IS NOT NULL
    PRINT '<<< CREATED TABLE CURRENCY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CURRENCY >>>'
go

/* 
 * TABLE: CURYRATE 
 */

CREATE TABLE CURYRATE(
    CURYTYPEID    UD_CURYTYPEID    NOT NULL,
    FROMCURYID    UD_CURYID        NOT NULL,
    TOCURYID      UD_CURYID        NOT NULL,
    CURYDATE      smalldatetime    NOT NULL,
    RATE          numeric(6, 4)    NOT NULL,
    OPERADOR      char(1)          NULL,
    USER1         UD_USER1         NULL,
    USER2         UD_USER2         NULL,
    USER3         UD_USER6         NULL,
    USER4         UD_USER7         NULL,
    TSTAMP        timestamp        NULL,
    CONSTRAINT PK_CURYRATE PRIMARY KEY NONCLUSTERED (CURYTYPEID, FROMCURYID, TOCURYID, CURYDATE)
) 
go


IF OBJECT_ID('CURYRATE') IS NOT NULL
    PRINT '<<< CREATED TABLE CURYRATE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CURYRATE >>>'
go

/* 
 * TABLE: CURYRATETYPE 
 */

CREATE TABLE CURYRATETYPE(
    CURYTYPEID    UD_CURYTYPEID    NOT NULL,
    DESCR         char(30)         NULL,
    USER4         UD_USER7         NULL,
    USER1         UD_USER1         NULL,
    USER2         UD_USER2         NULL,
    USER3         UD_USER6         NULL,
    TSTAMP        timestamp        NULL,
    CONSTRAINT PK_CURYRATETYPE PRIMARY KEY NONCLUSTERED (CURYTYPEID)
) 
go


IF OBJECT_ID('CURYRATETYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE CURYRATETYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CURYRATETYPE >>>'
go

/* 
 * TABLE: CUST_LIMIT 
 */

CREATE TABLE CUST_LIMIT(
    CUSTIDSS       char(20)         NOT NULL,
    SITEID         char(5)          NOT NULL,
    CREDLIMITED    numeric(9, 2)    DEFAULT (0) NOT NULL,
    USER1          varchar(30)      NULL,
    USER2          varchar(30)      NULL,
    USER3          float            NULL,
    USER4          float            NULL,
    TSTAMP         timestamp        NULL,
    CONSTRAINT PK102 PRIMARY KEY NONCLUSTERED (CUSTIDSS, SITEID)
) 
go


IF OBJECT_ID('CUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED TABLE CUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CUST_LIMIT >>>'
go

/* 
 * TABLE: CUSTOMER 
 */

CREATE TABLE CUSTOMER(
    CUSTIDSS        char(20)         NOT NULL,
    CUSTSTATUSID    char(1)          NOT NULL,
    CUSTID          char(20)         NOT NULL,
    TAXREGNBR       char(20)         NULL,
    CUSTNAME        varchar(40)      NULL,
    ADDR1           varchar(40)      NULL,
    ADDR2           varchar(40)      NULL,
    PHONE           char(10)         NULL,
    FAX             char(10)         NULL,
    CUSTZONEID      char(2)          NOT NULL,
    COUNTRYID       char(3)          NOT NULL,
    STATEID         char(2)          NOT NULL,
    CITYID          char(3)          NOT NULL,
    CUSTCLASSID     char(2)          NOT NULL,
    TAXID1          char(2)          NOT NULL,
    TAXID2          char(2)          NOT NULL,
    CURYTYPEID      char(6)          NOT NULL,
    TERMID          char(2)          NOT NULL,
    MODSALESID      char(2)          NOT NULL,
    SALESPERID      char(10)         NULL,
    PRCLVLCUSTID    char(10)         NULL,
    DATEORIG        datetime         NULL,
    DATEPROC        datetime         NOT NULL,
    WEEKDAYPAG      char(12)         NOT NULL,
    DAYTHENK        int              NOT NULL,
    CURYID          char(3)          NULL,
    CURRBAL         numeric(9, 2)    DEFAULT (0) NOT NULL,
    USER1           varchar(30)      NULL,
    USER2           varchar(30)      NULL,
    USER3           varchar(30)      NULL,
    USER4           smalldatetime    NULL,
    USER5           smalldatetime    NULL,
    USER6           float            NULL,
    USER7           float            NULL,
    USER8           smallint         NULL,
    USER9           smallint         NULL,
    TSTAMP          timestamp        NULL,
    CONSTRAINT PK_CUSTOMER PRIMARY KEY NONCLUSTERED (CUSTIDSS)
) 
go


IF OBJECT_ID('CUSTOMER') IS NOT NULL
    PRINT '<<< CREATED TABLE CUSTOMER >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CUSTOMER >>>'
go

/* 
 * TABLE: CUSTSTATUS 
 */

CREATE TABLE CUSTSTATUS(
    CUSTSTATUSID    char(1)        NOT NULL,
    DESCRCUST       varchar(40)    NOT NULL,
    USER1           varchar(30)    NULL,
    USER2           varchar(30)    NULL,
    USER3           float          NULL,
    USER4           float          NULL,
    TSTAMP          timestamp      NULL,
    CONSTRAINT PK_CUSTSTATUS PRIMARY KEY NONCLUSTERED (CUSTSTATUSID)
) 
go


IF OBJECT_ID('CUSTSTATUS') IS NOT NULL
    PRINT '<<< CREATED TABLE CUSTSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CUSTSTATUS >>>'
go

/* 
 * TABLE: CUSTWARRANTY 
 */

CREATE TABLE CUSTWARRANTY(
    CUSTIDSS              char(20)         NOT NULL,
    CUSTGARANTIATYPEID    char(2)          NOT NULL,
    TOTGAR                numeric(9, 2)    DEFAULT (0) NOT NULL,
    DESCRGAR              varchar(40)      NOT NULL,
    EXPIREDATE            smalldatetime    DEFAULT (getdate()) NOT NULL,
    CURYID                char(3)          NOT NULL,
    USER1                 varchar(30)      NULL,
    USER2                 varchar(30)      NULL,
    USER3                 float            NULL,
    USER4                 float            NULL,
    TSTAMP                timestamp        NULL,
    CONSTRAINT PK_CUSTGARANTIA PRIMARY KEY NONCLUSTERED (CUSTIDSS, CUSTGARANTIATYPEID)
) 
go


IF OBJECT_ID('CUSTWARRANTY') IS NOT NULL
    PRINT '<<< CREATED TABLE CUSTWARRANTY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CUSTWARRANTY >>>'
go

/* 
 * TABLE: CUSTWARRANTYTYPE 
 */

CREATE TABLE CUSTWARRANTYTYPE(
    CUSTGARANTIATYPEID    char(2)        NOT NULL,
    DECRCUSTGARANTIA      binary(20)     NOT NULL,
    USER1                 varchar(30)    NULL,
    USER2                 varchar(30)    NULL,
    TSTAMP                timestamp      NULL,
    CONSTRAINT PK_CUSTGARANTIATYPE PRIMARY KEY NONCLUSTERED (CUSTGARANTIATYPEID)
) 
go


IF OBJECT_ID('CUSTWARRANTYTYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE CUSTWARRANTYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE CUSTWARRANTYTYPE >>>'
go

/* 
 * TABLE: DOCTYPE 
 */

CREATE TABLE DOCTYPE(
    DOCTYPEID    UD_DOCTYPEID      NOT NULL,
    DESCR        UD_DESCRIPCION    NOT NULL,
    MINDES       char(5)           NOT NULL,
    USER1        UD_USER1          NULL,
    USER2        UD_USER2          NULL,
    USER3        UD_USER6          NULL,
    USER4        UD_USER7          NULL,
    TSTAMP       timestamp         NULL,
    CONSTRAINT PK_DOCTYPE PRIMARY KEY NONCLUSTERED (DOCTYPEID)
) 
go


IF OBJECT_ID('DOCTYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE DOCTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE DOCTYPE >>>'
go

/* 
 * TABLE: IN_CATEGORY 
 */

CREATE TABLE IN_CATEGORY(
    CLASSID         char(5)        NOT NULL,
    DESCR           varchar(40)    NOT NULL,
    BUSSINESTYPE    char(1)        NOT NULL,
    USER1           varchar(30)    NULL,
    USER2           varchar(30)    NULL,
    USER3           float          NULL,
    USER4           float          NULL,
    TSTAMP          timestamp      NULL,
    CONSTRAINT PK_IN_CATEGORY PRIMARY KEY NONCLUSTERED (CLASSID)
) 
go


IF OBJECT_ID('IN_CATEGORY') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_CATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_CATEGORY >>>'
go

/* 
 * TABLE: IN_CLASSVENDOR 
 */

CREATE TABLE IN_CLASSVENDOR(
    CLASSVENDORID    char(2)           NOT NULL,
    DESCR            UD_DESCRIPCION    NOT NULL,
    USER1            UD_USER1          NULL,
    USER2            UD_USER2          NULL,
    USER3            UD_USER6          NULL,
    USER4            UD_USER7          NULL,
    TSTAMP           timestamp         NULL,
    CONSTRAINT PK__IN_CLASSVENDOR PRIMARY KEY NONCLUSTERED (CLASSVENDORID)
) 
go


IF OBJECT_ID('IN_CLASSVENDOR') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_CLASSVENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_CLASSVENDOR >>>'
go

/* 
 * TABLE: IN_DOCSTATUS 
 */

CREATE TABLE IN_DOCSTATUS(
    DOCSTATUSID    char(2)           NOT NULL,
    DESCR          UD_DESCRIPCION    NOT NULL,
    USER1          UD_USER1          NULL,
    USER2          UD_USER2          NULL,
    USER3          UD_USER6          NULL,
    USER4          UD_USER7          NULL,
    TSTAMP         timestamp         NULL,
    CONSTRAINT PK__IN_DOCSTATUS PRIMARY KEY NONCLUSTERED (DOCSTATUSID)
) 
go


IF OBJECT_ID('IN_DOCSTATUS') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_DOCSTATUS >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_DOCSTATUS >>>'
go

/* 
 * TABLE: IN_INTRAHEAD 
 */

CREATE TABLE IN_INTRAHEAD(
    SITEID           UD_IDSITE       NOT NULL,
    BATNBR           char(10)        NOT NULL,
    TYPEDOCID        UD_TYPEDOC      NOT NULL,
    VENDIDSS         UD_IDVENDOR     NOT NULL,
    DOCTYPEID        UD_DOCTYPEID    NOT NULL,
    DOCSTATUSID      char(2)         NOT NULL,
    REFNBR           char(15)        NOT NULL,
    CURYRATE         UD_CAMBIO       NOT NULL,
    TOCURYID         char(3)         NOT NULL,
    FROMCURYID       char(3)         NOT NULL,
    SITEPROCES       UD_IDSITE       NOT NULL,
    NBRORDER         char(10)        NULL,
    DATENBRORDER     UD_FECHA        NULL,
    USERIDORDER      UD_USERID       NULL,
    INTRDATE         UD_FECHA        NOT NULL,
    INTRDATEPROCE    UD_FECHA        NOT NULL,
    SITEDEST         UD_IDSITE       NOT NULL,
    INTRTOTPEN       UD_MONTO        NOT NULL,
    INTRTAXPEN       UD_IMPUESTO     NOT NULL,
    INTRTOTUSD       UD_MONTO        NOT NULL,
    INTRTAXUSD       UD_IMPUESTO     NOT NULL,
    PERPOST          UD_PERPOST      NOT NULL,
    RLSED            bit             NOT NULL,
    DISASSEMBLE      bit             DEFAULT (0) NOT NULL,
    INTRSUBTOTPEN    UD_MONTO        NOT NULL,
    INTRSUBTOTUSD    UD_MONTO        NOT 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,
    CONSTRAINT PK__IN_INTRAHEAD PRIMARY KEY NONCLUSTERED (SITEID, BATNBR, TYPEDOCID)
) 
go


IF OBJECT_ID('IN_INTRAHEAD') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INTRAHEAD >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INTRAHEAD >>>'
go

/* 
 * TABLE: IN_INTRAITEM 
 */

CREATE TABLE IN_INTRAITEM(
    SITEID           UD_IDSITE           NOT NULL,
    BATNBR           char(10)            NOT NULL,
    TYPEDOCID        UD_TYPEDOC          NOT NULL,
    SEQUENCE         smallint            NOT NULL,
    INVTIDSKU        UD_IDCORPORATIVO    NOT NULL,
    INVTIDBARR       UD_CODIGOBARRA      NULL,
    QTY              UD_STOCK            NOT NULL,
    TAXIGV           numeric(4, 2)       NULL,
    STDCOSTPEN       UD_COSTO            NOT NULL,
    STDCOSTUSD       UD_COSTO            NOT NULL,
    STKFISI          bit                 NOT NULL,
    INTRDATE         UD_FECHA            NOT NULL,
    INTRDATEPROCE    UD_FECHA            NOT NULL,
    RLSED            bit                 NOT NULL,
    TAXISC           numeric(4, 2)       DEFAULT (0) NOT 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,
    BUYCOD           UD_CODIGOBARRA      NULL,
    DOCSTATUSID      char(2)             NOT NULL,
    CONSTRAINT PK__IN_INTRAITEM PRIMARY KEY NONCLUSTERED (SITEID, BATNBR, TYPEDOCID, SEQUENCE)
) 
go


IF OBJECT_ID('IN_INTRAITEM') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INTRAITEM >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INTRAITEM >>>'
go

/* 
 * TABLE: IN_INVENTORY 
 */

CREATE TABLE IN_INVENTORY(
    INVTIDSKU      char(20)          NOT NULL,
    TAXISC         char(2)           NULL,
    TAXIGV         char(2)           NOT NULL,
    CLASSID        char(5)           NOT NULL,
    CLASSUBID      char(5)           NOT NULL,
    INVTYPEID      char(5)           NOT NULL,
    STKUNITID      char(5)           NOT NULL,
    DESCR          varchar(40)       NOT NULL,
    WTSTK1         numeric(7, 3)     NOT NULL,
    STKFUELS       bit               NOT NULL,
    STKSTORE       bit               NOT NULL,
    STKOTHER       bit               NOT NULL,
    STKITEM        bit               NOT NULL,
    KIT            bit               NOT NULL,
    DISCDSCTO1     numeric(5, 2)     DEFAULT (0) NOT NULL,
    DISCDSCTO2     numeric(5, 2)     DEFAULT (0) NOT NULL,
    DISCMTO        numeric(12, 2)    NOT NULL,
    STKRECEPT      bit               NOT NULL,
    STKSALES       bit               NOT NULL,
    STATUS         bit               NOT NULL,
    CONSIGNMENT    bit               NOT NULL,
    DISASSEMBLE    bit               NOT NULL,
    USER1          varchar(30)       NULL,
    USER2          varchar(30)       NULL,
    USER3          varchar(30)       NULL,
    USER4          smalldatetime     NULL,
    USER5          smalldatetime     NULL,
    USER6          float             NULL,
    USER7          float             NULL,
    USER8          smallint          NULL,
    USER9          smallint          NULL,
    TSTAMP         timestamp         NULL,
    CONSTRAINT PK_IN_INVENTORY PRIMARY KEY NONCLUSTERED (INVTIDSKU)
) 
go


IF OBJECT_ID('IN_INVENTORY') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INVENTORY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INVENTORY >>>'
go

/* 
 * TABLE: IN_INVENTORYALTER 
 */

CREATE TABLE IN_INVENTORYALTER(
    INVTIDBARR    UD_CODIGOBARRA      NOT NULL,
    INVTIDSKU     UD_IDCORPORATIVO    NOT NULL,
    USER1         UD_USER1            NULL,
    USER2         UD_USER2            NULL,
    USER3         UD_USER6            NULL,
    USER4         UD_USER7            NULL,
    TSTAMP        timestamp           NULL,
    CONSTRAINT PK_IN_INVENTORYALTER PRIMARY KEY NONCLUSTERED (INVTIDBARR)
) 
go


IF OBJECT_ID('IN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INVENTORYALTER >>>'
go

/* 
 * TABLE: IN_INVENTORYDAT 
 */

CREATE TABLE IN_INVENTORYDAT(
    INVTIDSKU         UD_IDCORPORATIVO    NOT NULL,
    SITEID            UD_IDSITE           NOT NULL,
    STOCKMIN          UD_STOCK            NOT NULL,
    STOCKMAX          UD_STOCK            NOT NULL,
    STOCKSTAR         UD_STOCK            NOT NULL,
    QTYAVAIL          UD_STOCK            NOT NULL,
    STARTDATEINV      UD_FECHA            NULL,
    ENDDATEINV        UD_FECHA            NULL,
    LASTINVFIS        UD_STOCK            NULL,
    AVGCOSTPEN        UD_COSTO            NOT NULL,
    AVGCOSTUSD        UD_COSTO            NOT NULL,
    STDCOSTPEN        UD_COSTO            NOT NULL,
    STDCOSTUSD        UD_COSTO            NOT NULL,
    LASTSTDCOSTPEN    UD_COSTO            NOT NULL,
    LASTSTDCOSTUSD    UD_COSTO            NOT NULL,
    SLS_PRICE         UD_PRECIO           NOT NULL,
    LASTDAYIN         smalldatetime       NULL,
    LASTDAYOU         smalldatetime       NULL,
    PRECLVID          UD_PRECLVID         NULL,
    INVTIDSTK         char(20)            NOT 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            timestamp           NULL,
    CONSTRAINT PK_IN_INVENTORYDAT PRIMARY KEY NONCLUSTERED (INVTIDSKU, SITEID)
) 
go


IF OBJECT_ID('IN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INVENTORYDAT >>>'
go

/* 
 * TABLE: IN_INVENTORYDES 
 */

CREATE TABLE IN_INVENTORYDES(
    INVTIDSKU       UD_IDCORPORATIVO    NOT NULL,
    INVTIDSKUDES    UD_IDCORPORATIVO    NOT NULL,
    WTSTK2          tinyint             NOT NULL,
    USER1           UD_USER1            NULL,
    USER2           UD_USER2            NULL,
    USER3           UD_USER6            NULL,
    USER4           UD_USER7            NULL,
    TSTAMP          timestamp           NULL,
    CONSTRAINT PK_IN_INVENTORYDES PRIMARY KEY NONCLUSTERED (INVTIDSKU)
) 
go


IF OBJECT_ID('IN_INVENTORYDES') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INVENTORYDES >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INVENTORYDES >>>'
go

/* 
 * TABLE: IN_INVENTORYTYPE 
 */

CREATE TABLE IN_INVENTORYTYPE(
    INVTYPEID    char(5)           NOT NULL,
    DESCR        UD_DESCRIPCION    NOT NULL,
    USER1        UD_USER1          NULL,
    USER2        UD_USER2          NULL,
    USER3        UD_USER6          NULL,
    USER4        UD_USER7          NULL,
    TSTAMP       timestamp         NULL,
    CONSTRAINT PK_IN_INVENTORYTYPE PRIMARY KEY NONCLUSTERED (INVTYPEID)
) 
go


IF OBJECT_ID('IN_INVENTORYTYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INVENTORYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INVENTORYTYPE >>>'
go

/* 
 * TABLE: IN_INVENTORYUNITMED 
 */

CREATE TABLE IN_INVENTORYUNITMED(
    STKUNITID    UD_STKUNITID      NOT NULL,
    DESCR        UD_DESCRIPCION    NOT NULL,
    STKDIVIDE    bit               NOT NULL,
    USER1        UD_USER1          NULL,
    USER2        UD_USER2          NULL,
    USER3        UD_USER6          NULL,
    USER4        UD_USER7          NULL,
    TSTAMP       timestamp         NULL,
    CONSTRAINT PK_IN_INVENTORYUNIT PRIMARY KEY NONCLUSTERED (STKUNITID)
) 
go


IF OBJECT_ID('IN_INVENTORYUNITMED') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INVENTORYUNITMED >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INVENTORYUNITMED >>>'
go

/* 
 * TABLE: IN_INVENTPHISICAL 
 */

CREATE TABLE IN_INVENTPHISICAL(
    SITEID           UD_IDSITE           NOT NULL,
    INVTIDSKU        UD_IDCORPORATIVO    NOT NULL,
    CLASSUBID        char(5)             NOT NULL,
    CLASSID          char(5)             NOT NULL,
    STOCKPHISICAL    UD_STOCK            NOT NULL,
    CONSTRAINT PK_IN_INVENTPHISICAL PRIMARY KEY NONCLUSTERED (SITEID, INVTIDSKU)
) 
go


IF OBJECT_ID('IN_INVENTPHISICAL') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_INVENTPHISICAL >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_INVENTPHISICAL >>>'
go

/* 
 * TABLE: IN_KDXHIST 
 */

CREATE TABLE IN_KDXHIST(
    YEARHIST     smallint            NOT NULL,
    SITEID       UD_IDSITE           NOT NULL,
    INVTIDSKU    UD_IDCORPORATIVO    NOT NULL,
    QTYEND00     UD_STOCK            NOT NULL,
    QTYEND01     UD_STOCK            NOT NULL,
    QTYEND02     UD_STOCK            NOT NULL,
    QTYEND03     UD_STOCK            NOT NULL,
    QTYEND04     UD_STOCK            NOT NULL,
    QTYEND05     UD_STOCK            NOT NULL,
    QTYEND06     UD_STOCK            NOT NULL,
    QTYEND07     UD_STOCK            NOT NULL,
    QTYEND08     UD_STOCK            NOT NULL,
    QTYEND09     UD_STOCK            NOT NULL,
    QTYEND10     UD_STOCK            NOT NULL,
    QTYEND11     UD_STOCK            NOT NULL,
    QTYEND12     UD_STOCK            NOT NULL,
    QTYEND13     UD_STOCK            NOT NULL,
    AVGPEN00     UD_COSTO            NOT NULL,
    AVGPEN01     UD_COSTO            NOT NULL,
    AVGPEN02     UD_COSTO            NOT NULL,
    AVGPEN03     UD_COSTO            NOT NULL,
    AVGPEN04     UD_COSTO            NOT NULL,
    AVGPEN06     UD_COSTO            NOT NULL,
    AVGPEN05     UD_COSTO            NOT NULL,
    AVGPEN07     UD_COSTO            NOT NULL,
    AVGPEN08     UD_COSTO            NOT NULL,
    AVGPEN09     UD_COSTO            NOT NULL,
    AVGPEN10     UD_COSTO            NOT NULL,
    AVGPEN11     UD_COSTO            NOT NULL,
    AVGPEN12     UD_COSTO            NOT NULL,
    AVGPEN13     UD_COSTO            NOT NULL,
    AVGUSD00     UD_COSTO            NOT NULL,
    AVGUSD01     UD_COSTO            NOT NULL,
    AVGUSD02     UD_COSTO            NOT NULL,
    AVGUSD03     UD_COSTO            NOT NULL,
    AVGUSD04     UD_COSTO            NOT NULL,
    AVGUSD05     UD_COSTO            NOT NULL,
    AVGUSD06     UD_COSTO            NOT NULL,
    AVGUSD07     UD_COSTO            NOT NULL,
    AVGUSD08     UD_COSTO            NOT NULL,
    AVGUSD09     UD_COSTO            NOT NULL,
    AVGUSD10     UD_COSTO            NOT NULL,
    AVGUSD11     UD_COSTO            NOT NULL,
    AVGUSD12     UD_COSTO            NOT NULL,
    AVGUSD13     UD_COSTO            NOT NULL,
    USER1        UD_USER1            NULL,
    USER2        UD_USER2            NULL,
    USER3        UD_USER6            NULL,
    USER4        UD_USER7            NULL,
    TSTAMP       timestamp           NULL,
    CONSTRAINT PK__IN_KDXHIST PRIMARY KEY CLUSTERED (YEARHIST, SITEID, INVTIDSKU)
) 
go


IF OBJECT_ID('IN_KDXHIST') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_KDXHIST >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_KDXHIST >>>'
go

/* 
 * TABLE: IN_KIT 
 */

CREATE TABLE IN_KIT(
    KITID         UD_KITID          NOT NULL,
    STKUNITID     UD_STKUNITID      NOT NULL,
    CURYID        UD_CURYID         NOT NULL,
    TAXID         UD_TAXID          NOT NULL,
    DESCR         UD_DESCRIPCION    NOT NULL,
    MTOCTOREPS    UD_COSTO          NOT NULL,
    STDCOSTUSD    UD_COSTO          NOT NULL,
    USER1         UD_USER1          NULL,
    USER2         UD_USER2          NULL,
    USER3         UD_USER6          NULL,
    USER4         UD_USER7          NULL,
    TSTAMP        timestamp         NULL,
    CONSTRAINT PK_IN_KIT PRIMARY KEY NONCLUSTERED (KITID)
) 
go


IF OBJECT_ID('IN_KIT') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_KIT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_KIT >>>'
go

/* 
 * TABLE: IN_KITCOMPONENT 
 */

CREATE TABLE IN_KITCOMPONENT(
    KITID         UD_KITID            NOT NULL,
    INVTIDSKU     UD_IDCORPORATIVO    NOT NULL,
    SEQUENCE      tinyint             NOT NULL,
    STDCOSTPEN    UD_COSTO            NOT NULL,
    STDCOSTUSD    UD_COSTO            NOT NULL,
    QTY           numeric(9, 3)       NOT NULL,
    USER1         UD_USER1            NULL,
    USER2         UD_USER2            NULL,
    USER3         UD_USER6            NULL,
    USER4         UD_USER7            NULL,
    TSTAMP        timestamp           NULL,
    CONSTRAINT PK_KITCOMPONENT PRIMARY KEY NONCLUSTERED (KITID, INVTIDSKU)
) 
go


IF OBJECT_ID('IN_KITCOMPONENT') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_KITCOMPONENT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_KITCOMPONENT >>>'
go

/* 
 * TABLE: IN_PRICELEVELTEM 
 */

CREATE TABLE IN_PRICELEVELTEM(
    BATNBRPRICELEVELTEM    numeric(10, 0)    DEFAULT (1) NOT NULL,
    SITEID                 char(5)           NOT NULL,
    USERIDAUTORIZED01      char(20)          NOT NULL,
    DATETIMEAUTORIZED01    smalldatetime     NOT NULL,
    USERIDAUTORIZED02      char(20)          NOT NULL,
    DATETIMEAUTORIZED02    smalldatetime     DEFAULT (getdate()) NOT NULL,
    PRECLVID               char(5)           NOT NULL,
    STKPRICESS             bit               NOT NULL,
    STARTDATEPRICE         smalldatetime     NOT NULL,
    ENDDATEPRICE           smalldatetime     NOT NULL,
    STARTTIMEPRICE         tinyint           NOT NULL,
    ENDTIMEPRICE           tinyint           NOT NULL,
    STATUS                 bit               NOT NULL,
    STKPRICESEND           bit               NOT NULL,
    USER1                  UD_USER1          NULL,
    USER2                  UD_USER2          NULL,
    USER3                  UD_USER6          NULL,
    USER4                  UD_USER7          NULL,
    TSTAMP                 timestamp         NULL,
    CONSTRAINT PK103 PRIMARY KEY NONCLUSTERED (BATNBRPRICELEVELTEM, SITEID)
) 
go


IF OBJECT_ID('IN_PRICELEVELTEM') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_PRICELEVELTEM >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_PRICELEVELTEM >>>'
go

/* 
 * TABLE: IN_PRICELEVELTEMDET 
 */

CREATE TABLE IN_PRICELEVELTEMDET(
    BATNBRPRICELEVELTEM    numeric(10, 0)    DEFAULT (1) NOT NULL,
    SITEID                 char(5)           NOT NULL,
    SEQUENCE               smallint          DEFAULT (1) NOT NULL,
    INVTIDSKU              char(20)          NULL,
    DESCR                  varchar(40)       NOT NULL,
    SLS_PRICE              numeric(8, 4)     DEFAULT (0) NOT NULL,
    USER1                  varchar(30)       NULL,
    USER2                  varchar(30)       NULL,
    USER3                  float             NULL,
    USER4                  float             NULL,
    TSTAMP                 timestamp         NULL,
    CONSTRAINT PK_IN_PRICELEVELTEM PRIMARY KEY NONCLUSTERED (BATNBRPRICELEVELTEM, SITEID, SEQUENCE)
) 
go


IF OBJECT_ID('IN_PRICELEVELTEMDET') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_PRICELEVELTEMDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_PRICELEVELTEMDET >>>'
go

/* 
 * TABLE: IN_PRICELEVELTYPE 
 */

CREATE TABLE IN_PRICELEVELTYPE(
    PRECLVID      UD_PRECLVID       NOT NULL,
    CURYID        UD_CURYID         NOT NULL,
    CURYTYPEID    char(6)           NOT NULL,
    DESCR         UD_DESCRIPCION    NOT NULL,
    USER1         UD_USER1          NULL,
    USER2         UD_USER2          NULL,
    USER3         UD_USER6          NULL,
    USER4         UD_USER7          NULL,
    TSTAMP        timestamp         NULL,
    CONSTRAINT PK_IN_PRICELEVELTYP PRIMARY KEY NONCLUSTERED (PRECLVID)
) 
go


IF OBJECT_ID('IN_PRICELEVELTYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_PRICELEVELTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_PRICELEVELTYPE >>>'
go

/* 
 * TABLE: IN_SUBCATEGORY 
 */

CREATE TABLE IN_SUBCATEGORY(
    CLASSUBID            char(5)          NOT NULL,
    CLASSID              char(5)          NOT NULL,
    DESCR                varchar(40)      NOT NULL,
    TYPEDOCFISIN         char(5)          NOT NULL,
    TYPEDOCOUT           char(5)          NOT NULL,
    PORCENTDIFFERENCE    numeric(5, 2)    DEFAULT (0) NOT NULL,
    USER1                varchar(30)      NULL,
    USER2                varchar(30)      NULL,
    USER3                float            NULL,
    USER4                float            NULL,
    TSTAMP               timestamp        NULL,
    CONSTRAINT PK_IN_SUBCATEGORY PRIMARY KEY NONCLUSTERED (CLASSUBID, CLASSID)
) 
go


IF OBJECT_ID('IN_SUBCATEGORY') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_SUBCATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_SUBCATEGORY >>>'
go

/* 
 * TABLE: IN_TYPEDOC 
 */

CREATE TABLE IN_TYPEDOC(
    TYPEDOCID      UD_TYPEDOC        NOT NULL,
    DESCR          UD_DESCRIPCION    NOT NULL,
    INOUT          bit               NOT NULL,
    STKVALUE       bit               NOT NULL,
    STKCOSPRICE    bit               NOT NULL,
    STKAVGCOST     bit               NOT NULL,
    PRECLVLID      UD_PRECLVID       NULL,
    STKBUY         bit               NOT NULL,
    STKVENDOR      bit               NOT NULL,
    STKAVERAGE     bit               NOT NULL,
    DESCR_ABR      char(12)          NOT NULL,
    USER1          UD_USER1          NULL,
    USER2          UD_USER2          NULL,
    USER3          UD_USER6          NULL,
    USER4          UD_USER7          NULL,
    TSTAMP         timestamp         NULL,
    CONSTRAINT PK_IN_TYPEDOC PRIMARY KEY NONCLUSTERED (TYPEDOCID)
) 
go


IF OBJECT_ID('IN_TYPEDOC') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_TYPEDOC >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_TYPEDOC >>>'
go

/* 
 * TABLE: IN_VENDOR 
 */

CREATE TABLE IN_VENDOR(
    VENDIDSS         char(20)         NOT NULL,
    CITYID           char(3)          NOT NULL,
    COUNTRYID        char(3)          NOT NULL,
    CLASSVENDORID    char(2)          NOT NULL,
    TERMID           char(2)          NOT NULL,
    VENDORID         char(20)         NOT NULL,
    DESCR            varchar(40)      NOT NULL,
    CONTACT          varchar(40)      NULL,
    TAXREGNBR        varchar(20)      NULL,
    ADDRES           varchar(40)      NOT NULL,
    ADDRES2          varchar(40)      NULL,
    FONO1            char(10)         NULL,
    FONO2            char(10)         NULL,
    FAX              char(10)         NULL,
    MAIL             varchar(40)      NULL,
    STATUS           char(1)          NOT NULL,
    CURRBAL          numeric(9, 2)    DEFAULT (0) NOT NULL,
    CURYID           char(3)          NOT NULL,
    CURYTYPEID       char(6)          NOT NULL,
    TAXID            char(2)          NOT NULL,
    USER1            varchar(30)      NULL,
    USER2            varchar(30)      NULL,
    USER3            float            NULL,
    USER4            float            NULL,
    TSTAMP           timestamp        NULL,
    CONSTRAINT PK__IN_VENDOR PRIMARY KEY NONCLUSTERED (VENDIDSS)
) 
go


IF OBJECT_ID('IN_VENDOR') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_VENDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_VENDOR >>>'
go

/* 
 * TABLE: IN_VENDORINVT 
 */

CREATE TABLE IN_VENDORINVT(
    VENDIDSS     char(20)         NOT NULL,
    INVTIDSKU    char(20)         NOT NULL,
    SITEID       char(5)          NOT NULL,
    BUYCOD       varchar(20)      NOT NULL,
    DESCR        varchar(40)      NOT NULL,
    STDCOST      numeric(8, 4)    DEFAULT (0) NOT NULL,
    CURYID       char(3)          NOT NULL,
    BUYDATE      smalldatetime    NULL,
    USER1        varchar(30)      NULL,
    USER2        varchar(30)      NULL,
    USER3        float            NULL,
    USER4        float            NULL,
    TSTAMP       timestamp        NULL
) 
go


IF OBJECT_ID('IN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED TABLE IN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE IN_VENDORINVT >>>'
go

/* 
 * TABLE: INTRAMOV 
 */

CREATE TABLE INTRAMOV(
    SITEID           UD_IDSITE           NOT NULL,
    INVTIDSKU        UD_IDCORPORATIVO    NOT NULL,
    BATNBR           char(10)            NOT NULL,
    INTRDATE         UD_FECHA            NOT NULL,
    INTRDATEPROCE    UD_FECHA            NOT NULL,
    TYPEDOCID        UD_TYPEDOC          NOT NULL,
    CURYRATE         UD_CAMBIO           NOT NULL,
    CURYDATE         smalldatetime       NOT NULL,
    PERPOST          UD_PERPOST          NOT NULL,
    SWTINOUT         bit                 NOT NULL,
    QTY              UD_STOCK            NOT NULL,
    STDCOSTUSD       UD_COSTO            NULL,
    STDCOSTPEN       UD_COSTO            NULL,
    COMPONENT        bit                 NOT NULL,
    SLSPRICE         UD_PRECIO           NOT NULL,
    REFNBR           char(15)            NULL,
    DOCTYPEID        UD_DOCTYPEID        NULL,
    STKFISI          bit                 DEFAULT (0) NOT 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,
    CONSTRAINT PK_INTRAMOV PRIMARY KEY NONCLUSTERED (SITEID, INVTIDSKU, BATNBR)
) 
go


IF OBJECT_ID('INTRAMOV') IS NOT NULL
    PRINT '<<< CREATED TABLE INTRAMOV >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE INTRAMOV >>>'
go

/* 
 * TABLE: OP_CARA_FUEL 
 */

CREATE TABLE OP_CARA_FUEL(
    CARA          char(2)        NOT NULL,
    INVTIDBARR    char(20)       NULL,
    DESCR         varchar(40)    NOT NULL,
    USER1         varchar(30)    NULL,
    USER2         varchar(30)    NULL,
    USER3         float          NULL,
    USER4         float          NULL,
    TSTAMP        timestamp      NULL,
    CONSTRAINT PK_OP_CARA_FUEL PRIMARY KEY NONCLUSTERED (CARA)
) 
go


IF OBJECT_ID('OP_CARA_FUEL') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CARA_FUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CARA_FUEL >>>'
go

/* 
 * TABLE: OP_CARD 
 */

CREATE TABLE OP_CARD(
    CARDID         char(2)        NOT NULL,
    ACCOUNT        char(12)       NOT NULL,
    DESCR          varchar(40)    NOT NULL,
    NUMBERORDER    tinyint        NOT NULL,
    USER01         varchar(30)    NULL,
    USER02         varchar(30)    NULL,
    USER03         float          NULL,
    USER04         float          NULL,
    TSTAMP         timestamp      NULL,
    CONSTRAINT PK_OP_CARD PRIMARY KEY NONCLUSTERED (CARDID)
) 
go


IF OBJECT_ID('OP_CARD') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CARD >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CARD >>>'
go

/* 
 * TABLE: OP_CARD_MONT 
 */

CREATE TABLE OP_CARD_MONT(
    SITEID       char(5)          NOT NULL,
    CARDID       char(2)          NOT NULL,
    DATE         smalldatetime    DEFAULT (getdate()) NOT NULL,
    DATEPROCE    smalldatetime    DEFAULT (getdate()) NOT NULL,
    CURYID       char(3)          NOT NULL,
    TOTALCARD    numeric(9, 2)    DEFAULT (0) NOT NULL,
    STATUS       bit              NULL,
    USER1        varchar(30)      NULL,
    USER2        varchar(30)      NULL,
    USER3        float            NULL,
    USER4        float            NULL,
    TSTAMP       timestamp        NULL,
    CONSTRAINT PK_OP_CARD_MONT PRIMARY KEY NONCLUSTERED (SITEID, CARDID, DATE)
) 
go


IF OBJECT_ID('OP_CARD_MONT') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CARD_MONT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CARD_MONT >>>'
go

/* 
 * TABLE: OP_CCSALESPERSON 
 */

CREATE TABLE OP_CCSALESPERSON(
    SITEID        char(5)          NOT NULL,
    SALESPERID    char(10)         NOT NULL,
    CURYID        char(3)          NOT NULL,
    TOTAL         numeric(9, 2)    DEFAULT (0) NOT NULL,
    USER1         varchar(30)      NULL,
    USER2         varchar(30)      NULL,
    USER3         float            NULL,
    USER4         float            NULL,
    TSTAMP        timestamp        NULL,
    CONSTRAINT PK_OP_REMAIN_EXCEDED PRIMARY KEY NONCLUSTERED (SITEID, SALESPERID)
) 
go


IF OBJECT_ID('OP_CCSALESPERSON') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CCSALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CCSALESPERSON >>>'
go

/* 
 * TABLE: OP_CCSALESPERSONDET 
 */

CREATE TABLE OP_CCSALESPERSONDET(
    SITEID                 char(5)          NOT NULL,
    SALESPERID             char(10)         NOT NULL,
    SEQUENCE               smallint         DEFAULT (1) NOT NULL,
    CCSALESPERSONTYPEID    char(2)          NOT NULL,
    DATEPROCE              smalldatetime    DEFAULT (getdate()) NOT NULL,
    OBS                    char(100)        NULL,
    USER1                  varchar(30)      NULL,
    USER2                  varchar(30)      NULL,
    USER3                  float            NULL,
    USER4                  float            NULL,
    TSTAMP                 timestamp        NULL,
    CONSTRAINT PK_OP_CCSALESPERSONDET PRIMARY KEY NONCLUSTERED (SITEID, SALESPERID, SEQUENCE)
) 
go


IF OBJECT_ID('OP_CCSALESPERSONDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CCSALESPERSONDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CCSALESPERSONDET >>>'
go

/* 
 * TABLE: OP_CCSALESPERSONTYPE 
 */

CREATE TABLE OP_CCSALESPERSONTYPE(
    CCSALESPERSONTYPEID       char(2)        NOT NULL,
    DESCRCCSALESPERSONTYPE    varchar(40)    NOT NULL,
    DEBE                      char(1)        NOT NULL,
    HABER                     char(1)        NOT NULL,
    USER1                     varchar(30)    NULL,
    USER2                     varchar(30)    NULL,
    USER3                     float          NULL,
    USER4                     float          NULL,
    TSTAMP                    timestamp      NULL,
    CONSTRAINT PK_OP_CCSALESPERSONTYPE PRIMARY KEY NONCLUSTERED (CCSALESPERSONTYPEID)
) 
go


IF OBJECT_ID('OP_CCSALESPERSONTYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CCSALESPERSONTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CCSALESPERSONTYPE >>>'
go

/* 
 * TABLE: OP_CORELATIVE 
 */

CREATE TABLE OP_CORELATIVE(
    TYPECORELATIVE    char(20)          NOT NULL,
    NBRCORELATIVE     numeric(10, 0)    DEFAULT (1) NOT NULL,
    USER01            varchar(30)       NULL,
    USER02            varchar(30)       NULL,
    USER03            float             NULL,
    USER04            float             NULL,
    TSTAMP            timestamp         NULL,
    CONSTRAINT PK_OP_CORELATIVE PRIMARY KEY NONCLUSTERED (TYPECORELATIVE)
) 
go


IF OBJECT_ID('OP_CORELATIVE') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CORELATIVE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CORELATIVE >>>'
go

/* 
 * TABLE: OP_CTRLFUEL 
 */

CREATE TABLE OP_CTRLFUEL(
    SITEID              char(5)           NOT NULL,
    BATNBRCTRLFUEL      numeric(10, 0)    DEFAULT (1) NOT NULL,
    DATEPROCE           smalldatetime     DEFAULT (getdate()) NOT NULL,
    INVTIDSKU           char(20)          NOT NULL,
    STOCKINITIAL        numeric(10, 3)    DEFAULT (0) NOT NULL,
    QTYIMPUT            numeric(10, 3)    DEFAULT (0) NOT NULL,
    QTYOUTPUT           numeric(10, 3)    DEFAULT (0) NOT NULL,
    QTYSALES            numeric(10, 3)    DEFAULT (0) NOT NULL,
    QTYAVAIL            numeric(10, 3)    DEFAULT (0) NOT NULL,
    STKUNITID           char(5)           NOT NULL,
    VALUEUNIMED         numeric(7, 4)     NOT NULL,
    QTYDIFFERENCE       numeric(10, 3)    DEFAULT (0) NOT NULL,
    PORCENTDIFERENCE    numeric(5, 2)     DEFAULT (0) NOT NULL,
    REMARK              varchar(40)       NOT NULL,
    STATUS              bit               NULL,
    USER1               varchar(30)       NULL,
    USER2               varchar(30)       NULL,
    USER3               float             NULL,
    USER4               float             NULL,
    TSTAMP              timestamp         NULL,
    CONSTRAINT PK_OP_CTRLFUEL PRIMARY KEY NONCLUSTERED (SITEID, BATNBRCTRLFUEL)
) 
go


IF OBJECT_ID('OP_CTRLFUEL') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CTRLFUEL >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CTRLFUEL >>>'
go

/* 
 * TABLE: OP_CTRLFUELDET 
 */

CREATE TABLE OP_CTRLFUELDET(
    SITEID            char(5)           NOT NULL,
    BATNBRCTRLFUEL    numeric(10, 0)    DEFAULT (1) NOT NULL,
    SEQUENCE          tinyint           NOT NULL,
    CURYID            char(3)           NOT NULL,
    QTYSALES          numeric(10, 3)    DEFAULT (0) NOT NULL,
    SLS_PRICE         numeric(8, 4)     DEFAULT (0) NOT NULL,
    STATUS            bit               NULL,
    USER1             varchar(30)       NULL,
    USER2             varchar(30)       NULL,
    USER3             float             NULL,
    USER4             float             NULL,
    TSTAMP            timestamp         NULL,
    CONSTRAINT PK_OP_CTRLFUELDET PRIMARY KEY NONCLUSTERED (SITEID, BATNBRCTRLFUEL, SEQUENCE)
) 
go


IF OBJECT_ID('OP_CTRLFUELDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CTRLFUELDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CTRLFUELDET >>>'
go

/* 
 * TABLE: OP_CTRLTURN 
 */

CREATE TABLE OP_CTRLTURN(
    BATNBRTURN    numeric(10, 0)    NOT NULL,
    SALESPERID    char(10)          NOT NULL,
    SITEID        char(5)           NOT NULL,
    DATEPROCES    smalldatetime     DEFAULT (getdate()) NOT NULL,
    NBRTUR        char(1)           NOT NULL,
    USER01        varchar(30)       NULL,
    USER02        varchar(30)       NULL,
    USER03        float             NULL,
    USER04        float             NULL,
    TSTAMP        timestamp         NULL,
    CONSTRAINT PK_OP_CTRLTURN PRIMARY KEY NONCLUSTERED (BATNBRTURN)
) 
go


IF OBJECT_ID('OP_CTRLTURN') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CTRLTURN >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CTRLTURN >>>'
go

/* 
 * TABLE: OP_CTRLTURNDET 
 */

CREATE TABLE OP_CTRLTURNDET(
    BATNBRTURN      numeric(10, 0)    NOT NULL,
    CARA            char(2)           NOT NULL,
    INVTIDSKU       char(20)          NOT NULL,
    STARTREADING    numeric(13, 3)    NOT NULL,
    ENDREADING      numeric(13, 3)    NOT NULL,
    QTYSALES        numeric(10, 3)    DEFAULT (0) NOT NULL,
    QTYSERAF        numeric(10, 3)    DEFAULT (0) NOT NULL,
    QTYOTHER        numeric(10, 3)    DEFAULT (0) NOT NULL,
    TOTQTY          numeric(10, 3)    DEFAULT (0) NOT NULL,
    USER1           varchar(30)       NULL,
    USER2           varchar(30)       NULL,
    USER3           float             NULL,
    USER4           float             NULL,
    TSTAMP          timestamp         NULL,
    CONSTRAINT PK_OP_CTRLTURNDET PRIMARY KEY NONCLUSTERED (BATNBRTURN)
) 
go


IF OBJECT_ID('OP_CTRLTURNDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CTRLTURNDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CTRLTURNDET >>>'
go

/* 
 * TABLE: OP_CUSTCARD 
 */

CREATE TABLE OP_CUSTCARD(
    CUSTIDSS         char(20)         NOT NULL,
    EXPIREDATE       smalldatetime    DEFAULT (getdate()) NOT NULL,
    DESCRCUSTCARD    varchar(40)      NOT NULL,
    CARDPASSWORD     char(10)         NOT NULL,
    USER1            varchar(30)      NULL,
    USER2            varchar(30)      NULL,
    USER3            float            NULL,
    USER4            float            NULL,
    TSTAMP           timestamp        NULL,
    CONSTRAINT PK_OP_CUSTCARD PRIMARY KEY NONCLUSTERED (CUSTIDSS)
) 
go


IF OBJECT_ID('OP_CUSTCARD') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CUSTCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CUSTCARD >>>'
go

/* 
 * TABLE: OP_CUSTCLASS 
 */

CREATE TABLE OP_CUSTCLASS(
    CUSTCLASSID    char(2)        NOT NULL,
    DESCR          varchar(40)    NOT NULL,
    USER1          varchar(30)    NULL,
    USER2          varchar(30)    NULL,
    USER3          float          NULL,
    USER4          float          NULL,
    TSTAMP         timestamp      NULL,
    CONSTRAINT PK_OP_CUSTCLASS PRIMARY KEY NONCLUSTERED (CUSTCLASSID)
) 
go


IF OBJECT_ID('OP_CUSTCLASS') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CUSTCLASS >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CUSTCLASS >>>'
go

/* 
 * TABLE: OP_CUSTZONE 
 */

CREATE TABLE OP_CUSTZONE(
    CUSTZONEID    char(2)        NOT NULL,
    DESCR         varchar(40)    NOT NULL,
    USER1         varchar(30)    NULL,
    USER2         varchar(30)    NULL,
    USER3         float          NULL,
    USER4         float          NULL,
    TSTAMP        timestamp      NULL,
    CONSTRAINT PK_OP_CUSTZONE PRIMARY KEY NONCLUSTERED (CUSTZONEID)
) 
go


IF OBJECT_ID('OP_CUSTZONE') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_CUSTZONE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_CUSTZONE >>>'
go

/* 
 * TABLE: OP_DEFINITIONPD 
 */

CREATE TABLE OP_DEFINITIONPD(
    DEFINITIONID    char(2)        NOT NULL,
    CUSTIDSS        char(20)       NULL,
    CLASSID         char(5)        NULL,
    CLASSUBID       char(5)        NULL,
    INVTIDSKU       char(20)       NULL,
    DEBE            char(1)        NOT NULL,
    HABER           char(1)        NOT NULL,
    USER1           varchar(30)    NULL,
    USER2           varchar(30)    NULL,
    USER3           float          NULL,
    USER4           float          NULL,
    TSTAMP          timestamp      NULL,
    CONSTRAINT PK95 PRIMARY KEY NONCLUSTERED (DEFINITIONID)
) 
go


IF OBJECT_ID('OP_DEFINITIONPD') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DEFINITIONPD >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DEFINITIONPD >>>'
go

/* 
 * TABLE: OP_DEPOSIT 
 */

CREATE TABLE OP_DEPOSIT(
    BATNBRDEPOSIT      tinyint          NOT NULL,
    SITEID             char(5)          NOT NULL,
    CURYID             char(3)          NULL,
    SALESPERSONTURN    tinyint          NOT NULL,
    DEPOSITTYPEID      char(1)          NOT NULL,
    SALESPERID         char(10)         NOT NULL,
    OPDATE             smalldatetime    DEFAULT (getdate()) NOT NULL,
    DATEPROCE          smalldatetime    DEFAULT (getdate()) NOT NULL,
    TOTPEN             numeric(9, 2)    DEFAULT (0) NOT NULL,
    TOTUSD             numeric(9, 2)    DEFAULT (0) NOT NULL,
    STATUS             bit              NULL,
    USER1              varchar(30)      NULL,
    USER2              varchar(30)      NULL,
    USER3              float            NULL,
    USER4              float            NULL,
    TSTAMP             timestamp        NULL,
    CONSTRAINT PK_OP_DEPOSIT PRIMARY KEY NONCLUSTERED (BATNBRDEPOSIT, SITEID)
) 
go


IF OBJECT_ID('OP_DEPOSIT') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DEPOSIT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DEPOSIT >>>'
go

/* 
 * TABLE: OP_DEPOSITDET 
 */

CREATE TABLE OP_DEPOSITDET(
    BATNBRDEPOSIT    tinyint          NOT NULL,
    SITEID           char(5)          NOT NULL,
    SEQUENCE         smallint         DEFAULT (1) NOT NULL,
    CARDID           char(2)          NULL,
    TOTCARD          numeric(9, 2)    DEFAULT (0) NULL,
    CUSTIDSS         char(20)         NULL,
    MTOBONUS         numeric(9, 2)    DEFAULT (0) NOT NULL,
    MTOCASH          numeric(9, 2)    DEFAULT (0) NOT NULL,
    USER1            varchar(30)      NULL,
    USER2            varchar(30)      NULL,
    USER3            float            NULL,
    USER4            float            NULL,
    TSTAMP           timestamp        NULL,
    CONSTRAINT PK_OP_DEPOSITDET PRIMARY KEY NONCLUSTERED (BATNBRDEPOSIT, SITEID, SEQUENCE)
) 
go


IF OBJECT_ID('OP_DEPOSITDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DEPOSITDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DEPOSITDET >>>'
go

/* 
 * TABLE: OP_DEPOSITSITE 
 */

CREATE TABLE OP_DEPOSITSITE(
    SITEID               char(5)           NOT NULL,
    BATNBRDEPOSITSITE    numeric(10, 0)    DEFAULT (1) NOT NULL,
    CUSTIDSS             char(20)          NULL,
    DATEPROCE            smalldatetime     DEFAULT (getdate()) NOT NULL,
    TOTALPEN             numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTALUSD             numeric(9, 2)     DEFAULT (0) NOT NULL,
    STATUS               bit               NULL,
    USER1                varchar(30)       NULL,
    USER2                varchar(30)       NULL,
    USER3                float             NULL,
    USER4                float             NULL,
    TSTAMP               timestamp         NULL,
    CONSTRAINT PK_OP_DEPOSITSITE PRIMARY KEY NONCLUSTERED (SITEID, BATNBRDEPOSITSITE)
) 
go


IF OBJECT_ID('OP_DEPOSITSITE') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DEPOSITSITE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DEPOSITSITE >>>'
go

/* 
 * TABLE: OP_DEPOSITSITEDET 
 */

CREATE TABLE OP_DEPOSITSITEDET(
    SITEID               char(5)           NOT NULL,
    BATNBRDEPOSITSITE    numeric(10, 0)    DEFAULT (1) NOT NULL,
    SEQUENCE             smallint          DEFAULT (1) NOT NULL,
    DOCSUMMARYTYPEID     char(2)           NULL,
    REFERENCE            numeric(10, 0)    DEFAULT (0) NOT NULL,
    DEPOSITPASSWORD      char(20)          NOT NULL,
    DATEDEPOSIT          smalldatetime     DEFAULT (getdate()) NOT NULL,
    CURYID               char(3)           NOT NULL,
    CURYRATE             numeric(6, 4)     NOT NULL,
    TOTALPEN             numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTALUSD             numeric(9, 2)     DEFAULT (0) NOT NULL,
    DEBE                 char(1)           NOT NULL,
    HABER                char(1)           NOT NULL,
    STATUS               bit               NULL,
    USER1                varchar(30)       NULL,
    USER2                varchar(30)       NULL,
    USER3                float             NULL,
    USER4                float             NULL,
    TSTAMP               timestamp         NULL,
    CONSTRAINT PK93 PRIMARY KEY NONCLUSTERED (SITEID, BATNBRDEPOSITSITE, SEQUENCE)
) 
go


IF OBJECT_ID('OP_DEPOSITSITEDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DEPOSITSITEDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DEPOSITSITEDET >>>'
go

/* 
 * TABLE: OP_DEPOSITTYPE 
 */

CREATE TABLE OP_DEPOSITTYPE(
    DEPOSITTYPEID       char(1)        NOT NULL,
    DESCRDEPOSTITYPE    varchar(40)    NOT NULL,
    USER01              varchar(30)    NULL,
    USER02              varchar(30)    NULL,
    USER03              float          NULL,
    USER04              float          NULL,
    TSTAMP              timestamp      NULL,
    CONSTRAINT PK_OP_DEPOSITTYPE PRIMARY KEY NONCLUSTERED (DEPOSITTYPEID)
) 
go


IF OBJECT_ID('OP_DEPOSITTYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DEPOSITTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DEPOSITTYPE >>>'
go

/* 
 * TABLE: OP_DOCSUMMARY 
 */

CREATE TABLE OP_DOCSUMMARY(
    SITEID              char(5)          NOT NULL,
    DOCSUMMARYTYPEID    char(2)          NOT NULL,
    NBRDOCSUMMARY       char(10)         NOT NULL,
    DOCDATE             smalldatetime    DEFAULT (getdate()) NOT NULL,
    DUEDATE             smalldatetime    DEFAULT (getdate()) NOT NULL,
    DOCSTATUSID         char(2)          NOT NULL,
    TAXID01             char(2)          NOT NULL,
    TAXID02             char(2)          NOT NULL,
    TERMID              char(2)          NOT NULL,
    CURYTYPEID          char(6)          NOT NULL,
    CURYRATE            numeric(6, 4)    NOT NULL,
    CURYID              char(3)          NOT NULL,
    CUSTIDSS            char(20)         NOT NULL,
    CUSTID              char(20)         NOT NULL,
    CUSTNAME            varchar(40)      NULL,
    TAXREGNBR           char(20)         NOT NULL,
    WEEKDAYPAG          char(12)         NOT NULL,
    DAYTHENK            int              NOT NULL,
    TOTALPEN            numeric(9, 2)    DEFAULT (0) NOT NULL,
    TOTALUSD            numeric(9, 2)    DEFAULT (0) NOT NULL,
    SUBTOTALPEN         numeric(9, 2)    DEFAULT (0) NOT NULL,
    SUBTOTALUSD         numeric(9, 2)    DEFAULT (0) NOT NULL,
    TAXPEN              numeric(9, 2)    DEFAULT (0) NOT NULL,
    TAXUSD              numeric(9, 2)    DEFAULT (0) NOT NULL,
    STKITEM             bit              NULL,
    STKPROCE            bit              NOT NULL,
    USER1               varchar(30)      NULL,
    USER2               varchar(30)      NULL,
    USER3               float            NULL,
    USER4               float            NULL,
    TSTAMP              timestamp        NULL,
    CONSTRAINT PK100 PRIMARY KEY NONCLUSTERED (SITEID, DOCSUMMARYTYPEID, NBRDOCSUMMARY)
) 
go


IF OBJECT_ID('OP_DOCSUMMARY') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DOCSUMMARY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DOCSUMMARY >>>'
go

/* 
 * TABLE: OP_DOCSUMMARYDET 
 */

CREATE TABLE OP_DOCSUMMARYDET(
    SITEID              char(5)           NOT NULL,
    DOCSUMMARYTYPEID    char(2)           NOT NULL,
    NBRDOCSUMMARY       char(10)          NOT NULL,
    SEQUENCE            smallint          DEFAULT (1) NOT NULL,
    CURYTYPEID          char(6)           NOT NULL,
    CURYID              char(3)           NOT NULL,
    TAXID02             char(2)           NOT NULL,
    TAXID01             char(2)           NOT NULL,
    DISCOUNT            numeric(12, 2)    NULL,
    INVTIDSKU           char(20)          NOT NULL,
    SLS_PRICE           numeric(8, 4)     DEFAULT (0) NOT NULL,
    QTY                 numeric(10, 3)    DEFAULT (0) NOT NULL,
    TOTALPEN            numeric(9, 2)     DEFAULT (0) NOT NULL,
    SUBTOTALPEN         numeric(9, 2)     DEFAULT (0) NOT NULL,
    TAX1PEN             numeric(9, 2)     DEFAULT (0) NOT NULL,
    USER1               varchar(30)       NULL,
    USER2               varchar(30)       NULL,
    USER3               float             NULL,
    USER4               float             NULL,
    TSTAMP              timestamp         NULL,
    CONSTRAINT PK101 PRIMARY KEY NONCLUSTERED (SITEID, DOCSUMMARYTYPEID, NBRDOCSUMMARY, SEQUENCE)
) 
go


IF OBJECT_ID('OP_DOCSUMMARYDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DOCSUMMARYDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DOCSUMMARYDET >>>'
go

/* 
 * TABLE: OP_DOCSUMMARYTYPE 
 */

CREATE TABLE OP_DOCSUMMARYTYPE(
    DOCSUMMARYTYPEID       char(2)        NOT NULL,
    DESCRDOCSUMMARYTYPE    varchar(40)    NOT NULL,
    USER1                  varchar(30)    NULL,
    USER2                  varchar(30)    NULL,
    USER3                  float          NULL,
    USER4                  float          NULL,
    TSTAMP                 timestamp      NULL,
    CONSTRAINT PK91 PRIMARY KEY NONCLUSTERED (DOCSUMMARYTYPEID)
) 
go


IF OBJECT_ID('OP_DOCSUMMARYTYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_DOCSUMMARYTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_DOCSUMMARYTYPE >>>'
go

/* 
 * TABLE: OP_LOADSALESORD 
 */

CREATE TABLE OP_LOADSALESORD(
    NBRLOADSALE       numeric(10, 0)    DEFAULT (1) NOT NULL,
    CURYID            char(3)           NOT NULL,
    SALESPERID        char(10)          NOT NULL,
    CUSTIDSS          char(20)          NULL,
    TAXREGNBR         char(20)          NULL,
    CUSTNAME          varchar(40)       NULL,
    ADDR1             varchar(40)       NULL,
    DOCTYPEID         char(3)           NOT NULL,
    DOCSTATUSID       char(2)           NOT NULL,
    USERID            char(20)          NOT NULL,
    OPTRTOTPEN        numeric(9, 2)     DEFAULT (0) NOT NULL,
    OPTRTAXPEN        numeric(9, 2)     DEFAULT (0) NOT NULL,
    DOCDATE           smalldatetime     DEFAULT (getdate()) NOT NULL,
    DATEPROCESALES    smalldatetime     DEFAULT (getdate()) NOT NULL,
    CURYRATE          numeric(6, 4)     NOT NULL,
    STKCHECK          bit               NOT NULL,
    USER1             varchar(30)       NULL,
    USER2             varchar(30)       NULL,
    USER3             float             NULL,
    USER4             float             NULL,
    TSTAMP            timestamp         NULL,
    CONSTRAINT PK_OP_LOADSALESORD PRIMARY KEY NONCLUSTERED (NBRLOADSALE)
) 
go


IF OBJECT_ID('OP_LOADSALESORD') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_LOADSALESORD >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_LOADSALESORD >>>'
go

/* 
 * TABLE: OP_LOADSALESORDDET 
 */

CREATE TABLE OP_LOADSALESORDDET(
    NBRLOADSALE    numeric(10, 0)    DEFAULT (1) NOT NULL,
    SEQUENCE       smallint          IDENTITY(1,1),
    INVTIDSKU      char(20)          NOT NULL,
    DESCR          varchar(40)       NOT NULL,
    STKUNITID      char(5)           NULL,
    TAXIGV         numeric(9, 2)     DEFAULT (0) NOT NULL,
    SLS_PRICE      numeric(8, 4)     DEFAULT (0) NOT NULL,
    QTY            numeric(10, 3)    DEFAULT (0) NOT NULL,
    OPTRTOTDET     numeric(9, 2)     DEFAULT (0) NOT NULL,
    STDCOSTPEN     numeric(8, 4)     DEFAULT (0) NOT NULL,
    STRCOSTUSD     numeric(8, 4)     DEFAULT (0) NOT NULL,
    DISCDSCTO1     numeric(5, 2)     DEFAULT (0) NOT NULL,
    DISCDSCTO2     numeric(5, 2)     DEFAULT (0) NOT NULL,
    DISCMTO        numeric(12, 2)    NOT NULL,
    USER1          varchar(30)       NULL,
    USER2          varchar(30)       NULL,
    USER3          float             NULL,
    USER4          float             NULL,
    TSTAMP         timestamp         NULL,
    CONSTRAINT PK_OP_LOADSALESORDDET PRIMARY KEY NONCLUSTERED (NBRLOADSALE)
) 
go


IF OBJECT_ID('OP_LOADSALESORDDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_LOADSALESORDDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_LOADSALESORDDET >>>'
go

/* 
 * TABLE: OP_MODSALES 
 */

CREATE TABLE OP_MODSALES(
    MODSALESID    char(2)        NOT NULL,
    DESCR         varchar(40)    NOT NULL,
    USER1         varchar(30)    NULL,
    USER2         varchar(30)    NULL,
    USER3         float          NULL,
    USER4         float          NULL,
    TSTAMP        timestamp      NULL,
    CONSTRAINT PK_MODSALES PRIMARY KEY NONCLUSTERED (MODSALESID)
) 
go


IF OBJECT_ID('OP_MODSALES') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_MODSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_MODSALES >>>'
go

/* 
 * TABLE: OP_PRCLVLCUST 
 */

CREATE TABLE OP_PRCLVLCUST(
    PRCLVLCUSTID          char(10)         NOT NULL,
    CUSTIDSS              char(20)         NULL,
    SITEID                char(5)          NOT NULL,
    CURYID                char(3)          NOT NULL,
    DESCR                 varchar(40)      NOT NULL,
    STARTDATEPRICE        smalldatetime    DEFAULT (getdate()) NOT NULL,
    ENDDATEPRICE          smalldatetime    DEFAULT (getdate()) NOT NULL,
    STATUS                bit              NOT NULL,
    APPROVA1              bit              DEFAULT (0) NOT NULL,
    USERIDAPPROVA1        char(20)         NOT NULL,
    DATETIMEAUTORIZED1    smalldatetime    NOT NULL,
    APPROVA2              bit              NOT NULL,
    USERIDAPPROVA2        char(20)         NOT NULL,
    DATETIMEAUTORIZED2    smalldatetime    DEFAULT (getdate()) NOT NULL,
    STKPRICESEND          bit              NOT NULL,
    SITEORIG              char(5)          NOT NULL,
    STKPRICESS            bit              NOT NULL,
    USER1                 varchar(30)      NULL,
    USER2                 varchar(30)      NULL,
    USER3                 float            NULL,
    USER4                 float            NULL,
    TSTAMP                timestamp        NULL,
    CONSTRAINT PK_PRCLVLCUST PRIMARY KEY NONCLUSTERED (PRCLVLCUSTID)
) 
go


IF OBJECT_ID('OP_PRCLVLCUST') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_PRCLVLCUST >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_PRCLVLCUST >>>'
go

/* 
 * TABLE: OP_PRCLVLCUSTDET 
 */

CREATE TABLE OP_PRCLVLCUSTDET(
    PRCLVLCUSTID     char(10)         NOT NULL,
    SEQUENCE         smallint         DEFAULT (1) NOT NULL,
    STATUS           bit              NOT NULL,
    DISCDSCTO        numeric(5, 2)    DEFAULT (0) NOT NULL,
    DISCMTO          numeric(9, 2)    DEFAULT (0) NOT NULL,
    SLS_PRICE        numeric(8, 4)    DEFAULT (0) NOT NULL,
    SLS_PRICECUST    numeric(8, 4)    DEFAULT (0) NOT NULL,
    INVTIDSKU        char(20)         NOT NULL,
    USER1            varchar(30)      NULL,
    USER2            varchar(30)      NULL,
    USER3            float            NULL,
    USER4            float            NULL,
    TSTAMP           timestamp        NULL,
    CONSTRAINT PK_OP_PRCLVLCUSTDET PRIMARY KEY NONCLUSTERED (PRCLVLCUSTID, SEQUENCE)
) 
go


IF OBJECT_ID('OP_PRCLVLCUSTDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_PRCLVLCUSTDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_PRCLVLCUSTDET >>>'
go

/* 
 * TABLE: OP_PROMOTIONCARD 
 */

CREATE TABLE OP_PROMOTIONCARD(
    PROMOTIONCARDID      char(20)       NOT NULL,
    NAMECLIENT           varchar(40)    NULL,
    PASSWORDMAGNETIC     char(30)       NULL,
    LASTNAME             varchar(40)    NULL,
    USER1                varchar(30)    NULL,
    USER2                varchar(30)    NULL,
    USER3                float          NULL,
    USER4                float          NULL,
    TSTAMP               timestamp      NULL,
    PROMOTIONCARDTYPE    char(2)        NOT NULL,
    CONSTRAINT PK97 PRIMARY KEY NONCLUSTERED (PROMOTIONCARDID)
) 
go


IF OBJECT_ID('OP_PROMOTIONCARD') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_PROMOTIONCARD >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_PROMOTIONCARD >>>'
go

/* 
 * TABLE: OP_PROMOTIONCARDTYPE 
 */

CREATE TABLE OP_PROMOTIONCARDTYPE(
    PROMOTIONCARDTYPE     char(2)     NOT NULL,
    DESCRPROMOTIONCARD    char(10)    NULL,
    CONSTRAINT PK98 PRIMARY KEY NONCLUSTERED (PROMOTIONCARDTYPE)
) 
go


IF OBJECT_ID('OP_PROMOTIONCARDTYPE') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_PROMOTIONCARDTYPE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_PROMOTIONCARDTYPE >>>'
go

/* 
 * TABLE: OP_SALES 
 */

CREATE TABLE OP_SALES(
    DOCTYPEID           char(3)           NOT NULL,
    NBRDOCUMENT         numeric(10, 0)    DEFAULT (1) NOT NULL,
    STKINVENTORY        bit               NOT NULL,
    SITEID              char(5)           NOT NULL,
    CURYID              char(3)           NOT NULL,
    DOCSTATUSID         char(2)           NOT NULL,
    TERMID              char(2)           NOT NULL,
    MODSALESID          char(2)           NOT NULL,
    SALESPOINT          char(10)          NOT NULL,
    SALESPERID          char(10)          NOT NULL,
    USERID              char(20)          NOT NULL,
    DATEDOC             smalldatetime     DEFAULT (getdate()) NOT NULL,
    DATEPROCESALES      smalldatetime     DEFAULT (getdate()) NOT NULL,
    PERPOST             char(6)           NOT NULL,
    TAXIGVID            char(2)           NULL,
    TAXISCID            char(2)           NULL,
    TOTALPEN            numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTALUSD            numeric(9, 2)     DEFAULT (0) NOT NULL,
    SUBTOTALPEN         numeric(9, 2)     DEFAULT (0) NOT NULL,
    SUBTOTALUSD         numeric(9, 2)     DEFAULT (0) NOT NULL,
    TAX1PEN             numeric(9, 2)     DEFAULT (0) NOT NULL,
    TAX1USD             numeric(9, 2)     DEFAULT (0) NOT NULL,
    TAX2PEN             numeric(9, 2)     DEFAULT (0) NOT NULL,
    TAX2USD             numeric(9, 2)     DEFAULT (0) NOT NULL,
    PORCENTDISCOUNT1    numeric(5, 2)     DEFAULT (0) NOT NULL,
    PORCENTDISCOUNT2    numeric(5, 2)     DEFAULT (0) NOT NULL,
    TOTDISCOUNTPEN      numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTDISCOUNTUSD      numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTRETURNEDPEN      numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTRETURNEDUSD      numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTCASHPEN          numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTCASHUSD          numeric(9, 2)     DEFAULT (0) NOT NULL,
    CARDCREDITID1       char(2)           NULL,
    CARDCREDITID2       char(2)           NULL,
    NBRCARD1            char(20)          NULL,
    NBRCARD2            char(20)          NULL,
    TOTCARD1PEN         numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTCARD1USD         numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTCARD2PEN         numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTCARD2USD         numeric(9, 2)     DEFAULT (0) NOT NULL,
    CUSTIDSS            char(20)          NULL,
    TAXREGNBR           char(20)          NULL,
    NAME                varchar(40)       NULL,
    PLACA               varchar(10)       NOT NULL,
    NBRBONUS            int               DEFAULT (0) NOT NULL,
    CURYTYPEID          char(6)           NOT NULL,
    CURYRATE            numeric(6, 4)     NOT NULL,
    STKCLOSEDZ          bit               NOT NULL,
    STKCLOSEDX          bit               NOT NULL,
    PROMOTIONCARDID     char(20)          NULL,
    POINTNUMBER         smallint          DEFAULT (0) NOT NULL,
    USER1               varchar(30)       NULL,
    USER2               varchar(30)       NULL,
    USER3               varchar(30)       NULL,
    USER4               smalldatetime     NULL,
    USER5               smalldatetime     NULL,
    USER6               float             NULL,
    USER7               float             NULL,
    USER8               smallint          NULL,
    USER9               smallint          NULL,
    TSTAMP              timestamp         NULL,
    CONSTRAINT PK_OP_SALES PRIMARY KEY NONCLUSTERED (DOCTYPEID, NBRDOCUMENT)
) 
go


IF OBJECT_ID('OP_SALES') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_SALES >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_SALES >>>'
go

/* 
 * TABLE: OP_SALESDET 
 */

CREATE TABLE OP_SALESDET(
    DOCTYPEID         char(3)           NOT NULL,
    NBRDOCUMENT       numeric(10, 0)    DEFAULT (1) NOT NULL,
    SEQUENCE          smallint          DEFAULT (1) NOT NULL,
    SITEID            char(5)           NOT NULL,
    INVTIDSKU         char(20)          NOT NULL,
    CURYID            char(3)           NOT NULL,
    DOCSTATUSID       char(2)           NOT NULL,
    DATEDOC           smalldatetime     DEFAULT (getdate()) NOT NULL,
    DATEPROCESALES    smalldatetime     DEFAULT (getdate()) NOT NULL,
    PERSPOT           char(6)           NOT NULL,
    STKCLOSEDZ        bit               NOT NULL,
    STKCLOSEDX        bit               NOT NULL,
    NBRTURN           tinyint           NOT NULL,
    NBRCARA           char(2)           NULL,
    NBRTRANSACFUEL    char(4)           NULL,
    DISCDSCTO1        numeric(5, 2)     DEFAULT (0) NOT NULL,
    DISCDSCTO2        numeric(5, 2)     DEFAULT (0) NOT NULL,
    DISCMTOPEN        numeric(9, 2)     DEFAULT (0) NOT NULL,
    DISCMTOUSD        numeric(9, 2)     DEFAULT (0) NOT NULL,
    PORCENTTAXIGV     numeric(5, 2)     DEFAULT (0) NOT NULL,
    PORCENTTAXISC     numeric(5, 2)     DEFAULT (0) NOT NULL,
    TOTALPEN          numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTALUSD          numeric(9, 2)     DEFAULT (0) NOT NULL,
    TAXPEN            numeric(9, 2)     DEFAULT (0) NOT NULL,
    TAXUSD            numeric(9, 2)     DEFAULT (0) NOT NULL,
    STKITEM           bit               NOT NULL,
    STKFISI           bit               NOT NULL,
    SLS_PRICE         numeric(8, 4)     DEFAULT (0) NOT NULL,
    SLSPRICESALE      numeric(8, 4)     DEFAULT (0) NOT NULL,
    STDCOSTPEN        numeric(8, 4)     DEFAULT (0) NOT NULL,
    STDCOSTUSD        numeric(8, 4)     DEFAULT (0) NOT NULL,
    INVTIDALTER       char(20)          NOT NULL,
    DESCRINVENTORY    varchar(40)       NOT NULL,
    QTY               numeric(10, 3)    DEFAULT (0) NOT NULL,
    USER1             varchar(30)       NULL,
    USER2             varchar(30)       NULL,
    USER3             varchar(30)       NULL,
    USER4             smalldatetime     NULL,
    USER5             smalldatetime     NULL,
    USER6             float             NULL,
    USER7             float             NULL,
    USER8             smallint          NULL,
    USER9             smallint          NULL,
    TSTAMP            timestamp         NULL,
    SALESPOINT        char(10)          NOT NULL,
    SALESPERID        char(10)          NOT NULL,
    CONSTRAINT PK_OP_SALESDET PRIMARY KEY NONCLUSTERED (DOCTYPEID, NBRDOCUMENT, SEQUENCE)
) 
go


IF OBJECT_ID('OP_SALESDET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_SALESDET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_SALESDET >>>'
go

/* 
 * TABLE: OP_SALESPERSON 
 */

CREATE TABLE OP_SALESPERSON(
    SALESPERID        char(10)         NOT NULL,
    SALESPERNAME      varchar(40)      NULL,
    IDENTITYDOC       char(10)         NOT NULL,
    PHONE             char(10)         NULL,
    SEX               char(1)          NOT NULL,
    INITIALDATE       smalldatetime    DEFAULT (getdate()) NOT NULL,
    SITEID            char(5)          NOT NULL,
    ADDRESS1          varchar(40)      NOT NULL,
    ADDRESS2          varchar(40)      NULL,
    PASSWORD          char(15)         NOT NULL,
    BIRTHDATE         smalldatetime    DEFAULT (getdate()) NOT NULL,
    BIRTHADDRESS1     varchar(40)      NULL,
    BIRTHADDRESS2     varchar(40)      NULL,
    STATUSPERSONID    char(10)         NOT NULL,
    USER01            varchar(30)      NULL,
    USER02            varchar(30)      NULL,
    USER03            float            NULL,
    USER04            float            NULL,
    TSTAMP            timestamp        NULL,
    CONSTRAINT PK_OP_SALESPERSON PRIMARY KEY NONCLUSTERED (SALESPERID)
) 
go


IF OBJECT_ID('OP_SALESPERSON') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_SALESPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_SALESPERSON >>>'
go

/* 
 * TABLE: OP_SALESPOINTSETING 
 */

CREATE TABLE OP_SALESPOINTSETING(
    SALESPOINT           char(10)          NOT NULL,
    SALESPERID           char(10)          NOT NULL,
    ENDDATEACT           smalldatetime     DEFAULT (getdate()) NOT NULL,
    SERIALNUMBER         char(16)          NOT NULL,
    TOTZETA              numeric(10, 2)    DEFAULT (0) NOT NULL,
    NBRZETA              smallint          IDENTITY(1,1),
    PRECLVID             char(5)           NOT NULL,
    SITEID               char(5)           NOT NULL,
    STKTICKFAC           bit               NOT NULL,
    STKTICKBOL           bit               NOT NULL,
    CURYID               char(3)           NOT NULL,
    STKLOADSALE          bit               NOT NULL,
    NBRLOADSALE          numeric(10, 0)    DEFAULT (1) NOT NULL,
    STKPRINTERFAC        bit               NOT NULL,
    STKPRINTEDBOL        bit               NOT NULL,
    TYPEDISPFAC          varchar(1)        DEFAULT (0) NOT NULL,
    DISPFAC              varchar(10)       NOT NULL,
    NBRFAC               numeric(10, 0)    DEFAULT (1) NOT NULL,
    TYPEDISPBOL          varchar(1)        DEFAULT (0) NOT NULL,
    DISPBOL              varchar(10)       NOT NULL,
    NBRBOL               numeric(10, 0)    DEFAULT (1) NOT NULL,
    TYPEDISPNC           varchar(1)        DEFAULT (0) NOT NULL,
    DISPNC               varchar(10)       NOT NULL,
    NBRNC                numeric(10, 0)    DEFAULT (1) NOT NULL,
    TYPEDISPND           varchar(1)        DEFAULT (0) NOT NULL,
    DISPND               varchar(10)       NOT NULL,
    NBRND                numeric(10, 0)    DEFAULT (1) NOT NULL,
    TYPEDISORDER         varchar(1)        DEFAULT (0) NOT NULL,
    DISPORDER            varchar(10)       NOT NULL,
    NBRORDER             numeric(10, 0)    DEFAULT (1) NOT NULL,
    TYPEDISPTICKFAC      varchar(1)        DEFAULT (0) NOT NULL,
    DISPTICKFAC          varchar(10)       NOT NULL,
    NBRTICKFAC           numeric(10, 0)    DEFAULT (1) NOT NULL,
    TYPEDISPTICKBOL      varchar(1)        DEFAULT (0) NOT NULL,
    DISPTICKBOL          varchar(10)       NOT NULL,
    NBRTICKBOL           numeric(10, 0)    DEFAULT (1) NOT NULL,
    TERMINALNAME         varchar(20)       NOT NULL,
    DISPDISPLAY          varchar(10)       NOT NULL,
    STKSALESPERSONID     bit               NOT NULL,
    STKSALESPOINTFAC     bit               NOT NULL,
    STKZETAOK            bit               NOT NULL,
    STKJUMPAUTOMAT       bit               NOT NULL,
    QTYJUNPAUTOMAT       tinyint           DEFAULT (0) NOT NULL,
    STKHOTKEYS           bit               NOT NULL,
    TYPEPRINTERTICKET    char(18)          NOT NULL,
    STKDELETEDOC         bit               NOT NULL,
    [STK PRICE_COST]     bit               NOT NULL,
    STKDESCFACTUR        bit               NOT NULL,
    USER1                varchar(30)       NULL,
    USER2                varchar(30)       NULL,
    USER3                varchar(30)       NULL,
    USER4                smalldatetime     NULL,
    USER5                smalldatetime     NULL,
    USER6                float             NULL,
    USER7                float             NULL,
    USER8                smallint          NULL,
    USER9                smallint          NULL,
    TSTAMP               timestamp         NULL,
    CONSTRAINT PK_OP_SALESPOINTSETING PRIMARY KEY NONCLUSTERED (SALESPOINT, SALESPERID)
) 
go


IF OBJECT_ID('OP_SALESPOINTSETING') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_SALESPOINTSETING >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_SALESPOINTSETING >>>'
go

/* 
 * TABLE: OP_STATUSPERSON 
 */

CREATE TABLE OP_STATUSPERSON(
    STATUSPERSONID       char(10)       NOT NULL,
    DESCRSTATUSPERSON    varchar(40)    NOT NULL,
    USER1                varchar(30)    NULL,
    USER2                varchar(30)    NULL,
    USER3                float          NULL,
    USER4                float          NULL,
    TSTAMP               timestamp      NULL,
    CONSTRAINT PK92 PRIMARY KEY NONCLUSTERED (STATUSPERSONID)
) 
go


IF OBJECT_ID('OP_STATUSPERSON') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_STATUSPERSON >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_STATUSPERSON >>>'
go

/* 
 * TABLE: OP_SUMMARYDAY 
 */

CREATE TABLE OP_SUMMARYDAY(
    BATNBRDIARYSUMMARY    numeric(10, 0)    DEFAULT (1) NOT NULL,
    SITEID                char(5)           NOT NULL,
    DATEPROCE             smalldatetime     DEFAULT (getdate()) NOT NULL,
    CURYRATE              numeric(6, 4)     NOT NULL,
    ROUND                 numeric(9, 2)     DEFAULT (0) NOT NULL,
    TOTALDIFERENCE        numeric(9, 2)     DEFAULT (0) NOT NULL,
    OBSERVATION           varchar(100)      NULL,
    STATUS                bit               DEFAULT (0) NULL,
    DEBE                  numeric(9, 2)     DEFAULT (0) NOT NULL,
    HABER                 numeric(9, 2)     DEFAULT (0) NOT NULL,
    USER1                 varchar(30)       NULL,
    USER2                 varchar(30)       NULL,
    USER3                 float             NULL,
    USER4                 float             NULL,
    TSTAMP                timestamp         NULL,
    CONSTRAINT PK_OP_DIARYSUMMARY PRIMARY KEY NONCLUSTERED (BATNBRDIARYSUMMARY, SITEID)
) 
go


IF OBJECT_ID('OP_SUMMARYDAY') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_SUMMARYDAY >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_SUMMARYDAY >>>'
go

/* 
 * TABLE: OP_SUMMARYSALES 
 */

CREATE TABLE OP_SUMMARYSALES(
    SITEID              char(5)          NOT NULL,
    DATEPROCE           smalldatetime    DEFAULT (getdate()) NOT NULL,
    DOCSUMMARYTYPEID    char(2)          NOT NULL,
    CLASSID             char(5)          NOT NULL,
    CUSTIDSS            char(20)         NULL,
    CUSTCLASSID         char(2)          NULL,
    DEFINITIONID        char(2)          NOT NULL,
    CLASSUBID           char(5)          NULL,
    REFERENCE           char(15)         NULL,
    TOTALBONUS          numeric(9, 2)    DEFAULT (0) NOT NULL,
    DIFFERENCE          numeric(9, 2)    DEFAULT (0) NOT NULL,
    COMMENT             varchar(40)      NOT NULL,
    DEBE                char(1)          NOT NULL,
    HABER               char(1)          NOT NULL,
    TOTALDEBE           numeric(9, 2)    DEFAULT (0) NOT NULL,
    TOTALHABER          numeric(9, 2)    DEFAULT (0) NOT NULL,
    TOTALPEN            numeric(9, 2)    DEFAULT (0) NOT NULL,
    TOTALUSD            numeric(9, 2)    DEFAULT (0) NOT NULL,
    STATUS              bit              NULL,
    USER1               varchar(30)      NULL,
    USER2               varchar(30)      NULL,
    USER3               float            NULL,
    USER4               float            NULL,
    TSTAMP              timestamp        NULL,
    CONSTRAINT PK_OP_SUMMARYSALES PRIMARY KEY NONCLUSTERED (SITEID, DATEPROCE)
) 
go


IF OBJECT_ID('OP_SUMMARYSALES') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_SUMMARYSALES >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_SUMMARYSALES >>>'
go

/* 
 * TABLE: OP_TURNVSCARA 
 */

CREATE TABLE OP_TURNVSCARA(
    SITEID              char(5)          NOT NULL,
    BATNBRTURNPERSON    tinyint          DEFAULT (1) NOT NULL,
    DATEPROCE           smalldatetime    DEFAULT (getdate()) NOT NULL,
    NUMBERTURN          tinyint          NOT NULL,
    SALESPERID          char(10)         NOT NULL,
    USER1               varchar(30)      NULL,
    USER2               varchar(30)      NULL,
    USER3               float            NULL,
    USER4               float            NULL,
    TSTAMP              timestamp        NULL,
    CONSTRAINT PK_OP_TURNVSCARA PRIMARY KEY NONCLUSTERED (SITEID, BATNBRTURNPERSON, DATEPROCE, NUMBERTURN)
) 
go


IF OBJECT_ID('OP_TURNVSCARA') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_TURNVSCARA >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_TURNVSCARA >>>'
go

/* 
 * TABLE: OP_TURNVSCARADET 
 */

CREATE TABLE OP_TURNVSCARADET(
    SITEID              char(5)          NOT NULL,
    BATNBRTURNPERSON    tinyint          DEFAULT (1) NOT NULL,
    DATEPROCE           smalldatetime    DEFAULT (getdate()) NOT NULL,
    NUMBERTURN          tinyint          NOT NULL,
    CARA                char(2)          NOT NULL,
    USER1               varchar(30)      NULL,
    USER2               varchar(30)      NULL,
    USER3               float            NULL,
    USER4               float            NULL,
    TSTAMP              timestamp        NULL,
    CONSTRAINT PK_OP_TURNVSCARADET PRIMARY KEY NONCLUSTERED (SITEID, BATNBRTURNPERSON, DATEPROCE, NUMBERTURN)
) 
go


IF OBJECT_ID('OP_TURNVSCARADET') IS NOT NULL
    PRINT '<<< CREATED TABLE OP_TURNVSCARADET >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE OP_TURNVSCARADET >>>'
go

/* 
 * TABLE: SE_ACCESSDETRIGHTS 
 */

CREATE TABLE SE_ACCESSDETRIGHTS(
    USERID          UD_USERID      NOT NULL,
    SCREENID        UD_SCREENID    NOT NULL,
    VIEWRIGHTS      bit            DEFAULT (0) NOT NULL,
    INSERTRIGHTS    bit            DEFAULT (0) NOT NULL,
    UPDATERIGHTS    bit            DEFAULT (0) NOT NULL,
    DELETERIGHTS    bit            DEFAULT (0) NOT NULL,
    PRINTRIGHTS     bit            DEFAULT (0) NOT NULL,
    NULLRIGHTS      bit            DEFAULT (0) NOT NULL,
    CLOSERIGHTS     bit            DEFAULT (0) NOT NULL,
    USER1           UD_USER1       NULL,
    USER2           UD_USER2       NULL,
    USER3           UD_USER6       NULL,
    USER4           UD_USER7       NULL,
    TSTAMP          timestamp      NULL,
    CONSTRAINT PK__SE_ACCESSDETRIGH PRIMARY KEY NONCLUSTERED (USERID, SCREENID)
) 
go


IF OBJECT_ID('SE_ACCESSDETRIGHTS') IS NOT NULL
    PRINT '<<< CREATED TABLE SE_ACCESSDETRIGHTS >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE SE_ACCESSDETRIGHTS >>>'
go

/* 
 * TABLE: SE_GROUPREC 
 */

CREATE TABLE SE_GROUPREC(
    GROUPID      UD_USERID    NOT NULL,
    GROUPNAME    UD_NOMBRE    NOT NULL,
    USER1        UD_USER1     NULL,
    USER2        UD_USER2     NULL,
    USER3        UD_USER6     NULL,
    USER4        UD_USER7     NULL,
    TSTAMP       timestamp    NULL,
    CONSTRAINT PK__SE_GROUPREC PRIMARY KEY NONCLUSTERED (GROUPID)
) 
go


IF OBJECT_ID('SE_GROUPREC') IS NOT NULL
    PRINT '<<< CREATED TABLE SE_GROUPREC >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE SE_GROUPREC >>>'
go

/* 
 * TABLE: SE_MODULE 
 */

CREATE TABLE SE_MODULE(
    MODULEID      UD_MODULOID    NOT NULL,
    MODULENAME    UD_NOMBRE      NOT NULL,
    USER1         UD_USER1       NULL,
    USER2         UD_USER2       NULL,
    USER3         UD_USER6       NULL,
    USER4         UD_USER7       NULL,
    TSTAMP        timestamp      NULL,
    CONSTRAINT PK__SE_MODULE PRIMARY KEY NONCLUSTERED (MODULEID)
) 
go


IF OBJECT_ID('SE_MODULE') IS NOT NULL
    PRINT '<<< CREATED TABLE SE_MODULE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE SE_MODULE >>>'
go

/* 
 * TABLE: SE_SCREEN 
 */

CREATE TABLE SE_SCREEN(
    SCREENID      UD_SCREENID    NOT NULL,
    MODULEID      UD_MODULOID    NOT NULL,
    SCREENNAME    UD_NOMBRE      NOT NULL,
    SCREENTYPE    char(1)        NOT NULL,
    USER1         UD_USER1       NULL,
    USER2         UD_USER2       NULL,
    USER3         UD_USER6       NULL,
    USER4         UD_USER7       NULL,
    TSTAMP        timestamp      NULL,
    CONSTRAINT PK__SE_SCREEN PRIMARY KEY NONCLUSTERED (SCREENID)
) 
go


IF OBJECT_ID('SE_SCREEN') IS NOT NULL
    PRINT '<<< CREATED TABLE SE_SCREEN >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE SE_SCREEN >>>'
go

/* 
 * TABLE: SE_USERGROUP 
 */

CREATE TABLE SE_USERGROUP(
    USERID     UD_USERID    NOT NULL,
    GROUPID    UD_USERID    NOT NULL,
    USER1      char(30)     NULL,
    USER2      char(30)     NULL,
    USER3      float        NULL,
    USER4      float        NULL,
    TSTAMP     timestamp    NULL,
    CONSTRAINT PK__SE_USERGROUP PRIMARY KEY NONCLUSTERED (USERID, GROUPID)
) 
go


IF OBJECT_ID('SE_USERGROUP') IS NOT NULL
    PRINT '<<< CREATED TABLE SE_USERGROUP >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE SE_USERGROUP >>>'
go

/* 
 * TABLE: SE_USERREC 
 */

CREATE TABLE SE_USERREC(
    USERID          UD_USERID        NOT NULL,
    USERNAME        UD_NOMBRE        NOT NULL,
    EMAILADDRESS    varchar(40)      NULL,
    LOCATION        varchar(40)      NOT NULL,
    PASSWORD        char(12)         NOT NULL,
    PHONE           char(30)         NULL,
    EXPIRED         smalldatetime    NOT NULL,
    STATUS          bit              DEFAULT (1) NOT NULL,
    USER1           UD_USER1         NULL,
    USER2           UD_USER2         NULL,
    USER3           UD_USER6         NULL,
    USER4           UD_USER7         NULL,
    TSTAMP          timestamp        NULL,
    CONSTRAINT PK__SE_USERREC PRIMARY KEY NONCLUSTERED (USERID)
) 
go


IF OBJECT_ID('SE_USERREC') IS NOT NULL
    PRINT '<<< CREATED TABLE SE_USERREC >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE SE_USERREC >>>'
go

/* 
 * TABLE: SITE 
 */

CREATE TABLE SITE(
    SITEID            UD_IDSITE         NOT NULL,
    CITYID            char(3)           NOT NULL,
    COUNTRYID         char(3)           NOT NULL,
    NAME              UD_DESCRIPCION    NOT NULL,
    ADDR1             UD_DIRECCION      NOT NULL,
    ADDR2             UD_DIRECCION      NULL,
    FONO1             UD_TELEFONO       NULL,
    FONO2             UD_TELEFONO       NULL,
    FAX               UD_TELEFONO       NULL,
    STATUS            bit               DEFAULT (1) NOT NULL,
    STARTDATEINVEN    smalldatetime     NULL,
    ENDDATEINVEN      smalldatetime     NULL,
    ADJUST            bit               DEFAULT (0) NOT NULL,
    ATTN              varchar(40)       NOT 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,
    CONSTRAINT PK__SITE PRIMARY KEY NONCLUSTERED (SITEID)
) 
go


IF OBJECT_ID('SITE') IS NOT NULL
    PRINT '<<< CREATED TABLE SITE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE SITE >>>'
go

/* 
 * TABLE: STATE 
 */

CREATE TABLE STATE(
    STATEID    char(2)           NOT NULL,
    DESCR      UD_DESCRIPCION    NOT NULL,
    TSTAMP     timestamp         NULL,
    CONSTRAINT PK__STATE PRIMARY KEY NONCLUSTERED (STATEID)
) 
go


IF OBJECT_ID('STATE') IS NOT NULL
    PRINT '<<< CREATED TABLE STATE >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE STATE >>>'
go

/* 
 * TABLE: TAXS 
 */

CREATE TABLE TAXS(
    TAXID     UD_TAXID          NOT NULL,
    DESCR     UD_DESCRIPCION    NOT NULL,
    VALUE     numeric(5, 2)     NOT NULL,
    STATUS    bit               NOT NULL,
    USER1     UD_USER1          NULL,
    USER2     UD_USER2          NULL,
    USER3     UD_USER6          NULL,
    USER4     UD_USER7          NULL,
    TSTAMP    timestamp         NULL,
    CONSTRAINT PK_TAXS PRIMARY KEY NONCLUSTERED (TAXID)
) 
go


IF OBJECT_ID('TAXS') IS NOT NULL
    PRINT '<<< CREATED TABLE TAXS >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TAXS >>>'
go

/* 
 * TABLE: TERMS 
 */

CREATE TABLE TERMS(
    TERMID      char(2)           NOT NULL,
    DUEINTRV    tinyint           NOT NULL,
    DESCR       UD_DESCRIPCION    NOT NULL,
    USER1       UD_USER1          NULL,
    USER2       UD_USER2          NULL,
    USER3       UD_USER6          NULL,
    USER4       UD_USER7          NULL,
    TSTAMP      timestamp         NULL,
    CONSTRAINT PK__TERMS PRIMARY KEY NONCLUSTERED (TERMID)
) 
go


IF OBJECT_ID('TERMS') IS NOT NULL
    PRINT '<<< CREATED TABLE TERMS >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TERMS >>>'
go

/* 
 * TABLE: TMP_IN_INVENTORYALTER 
 */

CREATE TABLE TMP_IN_INVENTORYALTER(
    CORRNBR       int                 NOT NULL,
    INVTIDBARR    UD_CODIGOBARRA      NOT NULL,
    INVTIDSKU     UD_IDCORPORATIVO    NOT NULL,
    USER1         UD_USER1            NULL,
    USER2         UD_USER2            NULL,
    USER3         UD_USER6            NULL,
    USER4         UD_USER7            NULL,
    TSTAMP        binary(10)          NULL,
    CONSTRAINT PK_TMP_IN_INVENTORYALTER PRIMARY KEY NONCLUSTERED (CORRNBR, INVTIDBARR)
) 
go


IF OBJECT_ID('TMP_IN_INVENTORYALTER') IS NOT NULL
    PRINT '<<< CREATED TABLE TMP_IN_INVENTORYALTER >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TMP_IN_INVENTORYALTER >>>'
go

/* 
 * TABLE: TMP_IN_INVENTORYDAT 
 */

CREATE TABLE TMP_IN_INVENTORYDAT(
    CORRNBR           int                 NOT NULL,
    INVTIDSKU         UD_IDCORPORATIVO    NOT NULL,
    SITEID            UD_IDSITE           NOT NULL,
    STOCKMIN          UD_STOCK            NOT NULL,
    STOCKMAX          UD_STOCK            NOT NULL,
    STOCKSTAR         UD_STOCK            NOT NULL,
    QTYAVAIL          UD_STOCK            NOT NULL,
    STARTDATEINV      smalldatetime       NULL,
    ENDDATEINV        smalldatetime       NULL,
    LASTINVFIS        UD_STOCK            NOT NULL,
    AVGCOSTPEN        UD_COSTO            NOT NULL,
    AVGCOSTUSD        UD_COSTO            NOT NULL,
    STDCOSTPEN        UD_COSTO            NOT NULL,
    STDCOSTUSD        UD_COSTO            NOT NULL,
    LASTSTDCOSTPEN    UD_COSTO            NOT NULL,
    LASTSTDCOSTUSD    UD_COSTO            NOT NULL,
    SLS_PRICE         UD_COSTO            NOT NULL,
    LASTDAYIN         smalldatetime       NULL,
    LASTDAYOU         smalldatetime       NULL,
    PRECLVID          UD_PRECLVID         NULL,
    INVTIDSTK         char(20)            NOT 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,
    CONSTRAINT PK_TMP_IN_INVENTORYDAT PRIMARY KEY NONCLUSTERED (CORRNBR, INVTIDSKU, SITEID)
) 
go


IF OBJECT_ID('TMP_IN_INVENTORYDAT') IS NOT NULL
    PRINT '<<< CREATED TABLE TMP_IN_INVENTORYDAT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TMP_IN_INVENTORYDAT >>>'
go

/* 
 * TABLE: TMP_IN_VENDORINVT 
 */

CREATE TABLE TMP_IN_VENDORINVT(
    CORRNBR      int                 NOT NULL,
    VENDIDSS     UD_IDVENDOR         NOT NULL,
    INVTIDSKU    UD_IDCORPORATIVO    NOT NULL,
    SITEID       UD_IDSITE           NOT NULL,
    BUYCOD       varchar(20)         NOT NULL,
    DESCR        UD_DESCRIPCION      NOT NULL,
    STDCOST      UD_COSTO            NOT NULL,
    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,
    CONSTRAINT PK_TMP_IN_VENDORINVT PRIMARY KEY NONCLUSTERED (CORRNBR, VENDIDSS, INVTIDSKU, SITEID)
) 
go


IF OBJECT_ID('TMP_IN_VENDORINVT') IS NOT NULL
    PRINT '<<< CREATED TABLE TMP_IN_VENDORINVT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TMP_IN_VENDORINVT >>>'
go

/* 
 * TABLE: TMP_IN_VENDORINVTREL 
 */

CREATE TABLE TMP_IN_VENDORINVTREL(
    CORRNBR      int                 NOT NULL,
    VENDIDSS     UD_IDVENDOR         NOT NULL,
    INVTIDSKU    UD_IDCORPORATIVO    NOT NULL,
    BUYCOD       varchar(20)         NOT NULL,
    DESCR        UD_DESCRIPCION      NOT NULL,
    CURYID       UD_CURYID           NOT NULL,
    CONSTRAINT PK_TMP_IN_VENDORINVTREL PRIMARY KEY NONCLUSTERED (CORRNBR, VENDIDSS, INVTIDSKU)
) 
go


IF OBJECT_ID('TMP_IN_VENDORINVTREL') IS NOT NULL
    PRINT '<<< CREATED TABLE TMP_IN_VENDORINVTREL >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TMP_IN_VENDORINVTREL >>>'
go

/* 
 * TABLE: TYPEPAINMENT 
 */

CREATE TABLE TYPEPAINMENT(
    TYPEPAINMENTID    char(2)      NOT NULL,
    DESCR             char(40)     NOT NULL,
    TSTAMP            timestamp    NULL,
    CONSTRAINT PK_TYPEPAINMENT PRIMARY KEY NONCLUSTERED (TYPEPAINMENTID)
) 
go


IF OBJECT_ID('TYPEPAINMENT') IS NOT NULL
    PRINT '<<< CREATED TABLE TYPEPAINMENT >>>'
ELSE
    PRINT '<<< FAILED CREATING TABLE TYPEPAINMENT >>>'
go

/* 
 * VIEW: VW_ADJUST 
 */

CREATE VIEW VW_ADJUST
AS
SELECT SITEID,ADJUST FROM SITE WHERE SITEID=(SELECT SOURCESITE FROM SETUP)
go

IF OBJECT_ID('VW_ADJUST') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_ADJUST >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_ADJUST >>>'
go

/* 
 * VIEW: VW_AJUSTEAUTOMATICO 
 */

CREATE VIEW dbo.VW_AJUSTEAUTOMATICO
AS
SELECT IN_INVENTPHISICAL.SITEID, 
    IN_INVENTPHISICAL.INVTIDSKU, 
    IN_INVENTPHISICAL.STOCKPHISICAL, 
    IN_INVENTORYDAT.QTYAVAIL, 
    IN_SUBCATEGORY.CLASSUBID, 
    IN_SUBCATEGORY.CLASSID
FROM IN_INVENTPHISICAL INNER JOIN
    IN_INVENTORYDAT ON 
    IN_INVENTPHISICAL.INVTIDSKU = IN_INVENTORYDAT.INVTIDSKU
     AND 
    IN_INVENTPHISICAL.SITEID = IN_INVENTORYDAT.SITEID AND 
    IN_INVENTPHISICAL.STOCKPHISICAL <> IN_INVENTORYDAT.QTYAVAIL
     INNER JOIN
    IN_SUBCATEGORY ON 
    IN_INVENTPHISICAL.CLASSUBID = IN_SUBCATEGORY.CLASSUBID
go

IF OBJECT_ID('VW_AJUSTEAUTOMATICO') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_AJUSTEAUTOMATICO >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_AJUSTEAUTOMATICO >>>'
go

/* 
 * VIEW: VW_AJUSTECANTIDAD 
 */


CREATE VIEW VW_AJUSTECANTIDAD 
AS
SELECT SITEID, INVTIDSKU, SUM(INTRAMOV.QTY) AS CANTIDAD
FROM IN_TYPEDOC INNER JOIN
    INTRAMOV ON 
    IN_TYPEDOC.TYPEDOCID = INTRAMOV.TYPEDOCID
WHERE STKFISI=1 AND IN_TYPEDOC.INOUT = 1 
GROUP BY SITEID, INVTIDSKU
UNION
SELECT SITEID, INVTIDSKU, SUM(INTRAMOV.QTY)*-1 AS CANTIDAD
FROM IN_TYPEDOC INNER JOIN
    INTRAMOV ON 
    IN_TYPEDOC.TYPEDOCID = INTRAMOV.TYPEDOCID
WHERE  STKFISI=1 AND IN_TYPEDOC.INOUT = 0
GROUP BY SITEID, INVTIDSKU
go

IF OBJECT_ID('VW_AJUSTECANTIDAD') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_AJUSTECANTIDAD >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_AJUSTECANTIDAD >>>'
go

/* 
 * VIEW: VW_ARTDISPONIBLES_CLUSTER 
 */

CREATE VIEW dbo.VW_ARTDISPONIBLES_CLUSTER
AS
SELECT INVTIDSKU, DESCR, CLASSUBID, CLASSID
FROM IN_INVENTORY
WHERE (CLASSUBID IN
        (SELECT CLASSUBID
      FROM IN_cluster)) AND (CLASSID IN
        (SELECT CLASSID
      FROM IN_cluster)) AND (INVTIDSKU NOT IN
        (SELECT INVTIDSKU
      FROM IN_INVENTPHISICAL))
go

IF OBJECT_ID('VW_ARTDISPONIBLES_CLUSTER') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_ARTDISPONIBLES_CLUSTER >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_ARTDISPONIBLES_CLUSTER >>>'
go

/* 
 * VIEW: vw_articulos_toma 
 */

CREATE VIEW dbo.vw_articulos_toma
AS
SELECT IN_INVENTORY.DESCR, IN_INVENTPHISICAL.INVTIDSKU, 
    IN_INVENTORY.CLASSUBID, 
    IN_SUBCATEGORY.DESCR AS nsubcat, 
    IN_SUBCATEGORY.CLASSID, 
    IN_CATEGORY.DESCR AS ncat
FROM IN_INVENTPHISICAL INNER JOIN
    IN_INVENTORY ON 
    IN_INVENTPHISICAL.INVTIDSKU = IN_INVENTORY.INVTIDSKU INNER
     JOIN
    IN_SUBCATEGORY ON 
    IN_INVENTORY.CLASSUBID = IN_SUBCATEGORY.CLASSUBID AND
     IN_INVENTORY.CLASSID = IN_SUBCATEGORY.CLASSID INNER
     JOIN
    IN_CATEGORY ON 
    IN_SUBCATEGORY.CLASSID = IN_CATEGORY.CLASSID
go

IF OBJECT_ID('vw_articulos_toma') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_articulos_toma >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_articulos_toma >>>'
go

/* 
 * VIEW: Vw_ARTSELECCIONADOS_CLUSTER 
 */

CREATE VIEW dbo.Vw_ARTSELECCIONADOS_CLUSTER
AS
SELECT IN_INVENTPHISICAL.INVTIDSKU, IN_INVENTORY.DESCR, 
    IN_INVENTPHISICAL.CLASSUBID, 
    IN_INVENTPHISICAL.CLASSID, 
    IN_SUBCATEGORY.DESCR AS NSCAT, 
    IN_CATEGORY.DESCR AS NCAT, 
    IN_INVENTPHISICAL.STOCKPHISICAL
FROM IN_INVENTPHISICAL INNER JOIN
    IN_INVENTORY ON 
    IN_INVENTPHISICAL.INVTIDSKU = IN_INVENTORY.INVTIDSKU INNER
     JOIN
    IN_SUBCATEGORY ON 
    IN_INVENTORY.CLASSUBID = IN_SUBCATEGORY.CLASSUBID AND
     IN_INVENTORY.CLASSID = IN_SUBCATEGORY.CLASSID INNER
     JOIN
    IN_CATEGORY ON 
    IN_SUBCATEGORY.CLASSID = IN_CATEGORY.CLASSID
go

IF OBJECT_ID('Vw_ARTSELECCIONADOS_CLUSTER') IS NOT NULL
    PRINT '<<< CREATED VIEW Vw_ARTSELECCIONADOS_CLUSTER >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW Vw_ARTSELECCIONADOS_CLUSTER >>>'
go

/* 
 * VIEW: VW_BuscarArticulos 
 */

Create View VW_BuscarArticulos
as
  Select INVTIDSKU,DESCR from in_inventory
go

IF OBJECT_ID('VW_BuscarArticulos') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_BuscarArticulos >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_BuscarArticulos >>>'
go

/* 
 * VIEW: VW_CATEGORYSUBCATEGORY 
 */

/************************************************************************************************************************/
CREATE VIEW  VW_CATEGORYSUBCATEGORY
AS 
SELECT IN_SUBCATEGORY.CLASSUBID, 
    IN_SUBCATEGORY.DESCR, IN_CATEGORY.CLASSID, 
    IN_CATEGORY.DESCR AS DESCRCATEGORY
FROM IN_CATEGORY INNER JOIN
    IN_SUBCATEGORY ON 
    IN_CATEGORY.CLASSID = IN_SUBCATEGORY.CLASSID
go

IF OBJECT_ID('VW_CATEGORYSUBCATEGORY') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_CATEGORYSUBCATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_CATEGORYSUBCATEGORY >>>'
go

/* 
 * VIEW: VW_catseleccionadas_cluster 
 */

create view VW_catseleccionadas_cluster
as
SELECT IN_CLUSTER.CLASSID, IN_CATEGORY.DESCR,
	(select count(invtidsku) from in_inventory where classid=in_cluster.classid)-	
	(select count(invtidsku) from in_inventphisical where classid=in_cluster.classid) as dif
	FROM IN_CLUSTER LEFT OUTER JOIN
    IN_CATEGORY ON 
    IN_CLUSTER.CLASSID = IN_CATEGORY.CLASSID
GROUP BY IN_CLUSTER.CLASSID, IN_CATEGORY.DESCR
go

IF OBJECT_ID('VW_catseleccionadas_cluster') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_catseleccionadas_cluster >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_catseleccionadas_cluster >>>'
go

/* 
 * VIEW: VW_CUSERGROUPS 
 */

CREATE VIEW VW_CUSERGROUPS AS
SELECT USERID,USERNAME,GROUPID,GROUPNAME
FROM SE_GROUPREC 
CROSS JOIN SE_USERREC
go

IF OBJECT_ID('VW_CUSERGROUPS') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_CUSERGROUPS >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_CUSERGROUPS >>>'
go

/* 
 * VIEW: VW_DEPARTAMENTODISTRITO 
 */

CREATE VIEW VW_DEPARTAMENTODISTRITO
AS
  SELECT CITY.CITYID, STATE.STATEID, STATE.DESCR, 
         CITY.DESCR AS descrcity
  FROM CITY INNER JOIN
         STATE ON CITY.STATEID = STATE.STATEID
go

IF OBJECT_ID('VW_DEPARTAMENTODISTRITO') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_DEPARTAMENTODISTRITO >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_DEPARTAMENTODISTRITO >>>'
go

/* 
 * VIEW: vw_derechosgrupos 
 */

CREATE VIEW dbo.vw_derechosgrupos
AS
SELECT SE_ACCESSDETRIGHTS.SCREENID, 
    SE_SCREEN.SCREENNAME, 
    SE_GROUPREC.GROUPID AS USERID, 
    SE_ACCESSDETRIGHTS.VIEWRIGHTS, 
    SE_ACCESSDETRIGHTS.INSERTRIGHTS, 
    SE_ACCESSDETRIGHTS.UPDATERIGHTS, 
    SE_ACCESSDETRIGHTS.DELETERIGHTS, 
    SE_ACCESSDETRIGHTS.PRINTRIGHTS, 
    SE_ACCESSDETRIGHTS.NULLRIGHTS, 
    SE_ACCESSDETRIGHTS.CLOSERIGHTS
FROM SE_ACCESSDETRIGHTS INNER JOIN
    SE_SCREEN ON 
    SE_SCREEN.SCREENID = SE_ACCESSDETRIGHTS.SCREENID INNER
     JOIN
    SE_GROUPREC ON 
    SE_ACCESSDETRIGHTS.USERID = SE_GROUPREC.GROUPID
go

IF OBJECT_ID('vw_derechosgrupos') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_derechosgrupos >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_derechosgrupos >>>'
go

/* 
 * VIEW: vw_derechosusuarios 
 */

CREATE VIEW dbo.vw_derechosusuarios
AS
SELECT SE_ACCESSDETRIGHTS.SCREENID, 
    SE_SCREEN.SCREENNAME, SE_USERREC.USERID, 
    SE_ACCESSDETRIGHTS.VIEWRIGHTS, 
    SE_ACCESSDETRIGHTS.INSERTRIGHTS, 
    SE_ACCESSDETRIGHTS.UPDATERIGHTS, 
    SE_ACCESSDETRIGHTS.DELETERIGHTS, 
    SE_ACCESSDETRIGHTS.PRINTRIGHTS, 
    SE_ACCESSDETRIGHTS.NULLRIGHTS, 
    SE_ACCESSDETRIGHTS.CLOSERIGHTS, 
    SE_SCREEN.SCREENTYPE
FROM SE_ACCESSDETRIGHTS INNER JOIN
    SE_SCREEN ON 
    SE_SCREEN.SCREENID = SE_ACCESSDETRIGHTS.SCREENID INNER
     JOIN
    SE_USERREC ON 
    SE_ACCESSDETRIGHTS.USERID = SE_USERREC.USERID
go

IF OBJECT_ID('vw_derechosusuarios') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_derechosusuarios >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_derechosusuarios >>>'
go

/* 
 * VIEW: vw_derechosusuariosxgrupos 
 */

CREATE VIEW dbo.vw_derechosusuariosxgrupos
AS
SELECT SE_ACCESSDETRIGHTS.SCREENID, 
    SE_SCREEN.SCREENNAME, SE_USERGROUP.USERID, 
    SE_ACCESSDETRIGHTS.VIEWRIGHTS, 
    SE_ACCESSDETRIGHTS.INSERTRIGHTS, 
    SE_ACCESSDETRIGHTS.UPDATERIGHTS, 
    SE_ACCESSDETRIGHTS.DELETERIGHTS, 
    SE_ACCESSDETRIGHTS.PRINTRIGHTS, 
    SE_ACCESSDETRIGHTS.NULLRIGHTS, 
    SE_ACCESSDETRIGHTS.CLOSERIGHTS, 
    SE_SCREEN.SCREENTYPE
FROM SE_ACCESSDETRIGHTS INNER JOIN
    SE_SCREEN ON 
    SE_ACCESSDETRIGHTS.SCREENID = SE_SCREEN.SCREENID INNER
     JOIN
    SE_USERGROUP INNER JOIN
    SE_GROUPREC ON 
    SE_USERGROUP.GROUPID = SE_GROUPREC.GROUPID ON 
    SE_ACCESSDETRIGHTS.USERID = SE_GROUPREC.GROUPID
go

IF OBJECT_ID('vw_derechosusuariosxgrupos') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_derechosusuariosxgrupos >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_derechosusuariosxgrupos >>>'
go

/* 
 * VIEW: vw_in_ris_detalle_all 
 */

CREATE VIEW dbo.vw_in_ris_detalle_all
AS
SELECT IN_INTRAHEAD.SITEID, SITE.NAME AS sitename, 
    IN_INTRAHEAD.TYPEDOCID, 
    IN_TYPEDOC.DESCR AS typedocdescr, 
    IN_INTRAHEAD.BATNBR, IN_INTRAHEAD.DOCTYPEID, 
    DOCTYPE.MINDES AS doctypedescr, IN_INTRAHEAD.REFNBR, 
    IN_INTRAHEAD.INTRDATE, IN_INTRAHEAD.INTRDATEPROCE, 
    IN_INTRAHEAD.FROMCURYID, CURRENCY.DESCRMONEY, 
    IN_VENDOR.TAXREGNBR, 
    IN_VENDOR.DESCR AS nproveedor, 
    IN_INTRAHEAD.CURYRATE, IN_INTRAITEM.INVTIDSKU, 
    IN_INVENTORY.DESCR AS nproducto, 
    IN_INVENTORY.STKUNITID, IN_INTRAITEM.STDCOSTPEN, 
    IN_INTRAITEM.STDCOSTUSD, IN_INTRAITEM.QTY, 
    IN_INTRAITEM.RLSED, IN_INVENTORY.CLASSUBID, 
    IN_SUBCATEGORY.DESCR AS NSUBCAT, 
    IN_SUBCATEGORY.CLASSID, 
    IN_CATEGORY.DESCR AS NCAT, 
    CASE IN_INTRAITEM.RLSED WHEN 1 THEN 'SI' WHEN 0 THEN 'NO'
     END AS EMITIDO
FROM IN_INTRAHEAD INNER JOIN
    SITE ON IN_INTRAHEAD.SITEID = SITE.SITEID INNER JOIN
    DOCTYPE ON 
    IN_INTRAHEAD.DOCTYPEID = DOCTYPE.DOCTYPEID INNER JOIN
    IN_TYPEDOC ON 
    IN_INTRAHEAD.TYPEDOCID = IN_TYPEDOC.TYPEDOCID INNER
     JOIN
    CURRENCY ON 
    IN_INTRAHEAD.FROMCURYID = CURRENCY.CURYID INNER JOIN
    IN_VENDOR ON 
    IN_INTRAHEAD.VENDIDSS = IN_VENDOR.VENDIDSS INNER JOIN
    IN_INTRAITEM ON 
    IN_INTRAHEAD.SITEID = IN_INTRAITEM.SITEID AND 
    IN_INTRAHEAD.BATNBR = IN_INTRAITEM.BATNBR AND 
    IN_INTRAHEAD.TYPEDOCID = IN_INTRAITEM.TYPEDOCID INNER
     JOIN
    IN_INVENTORY ON 
    IN_INTRAITEM.INVTIDSKU = IN_INVENTORY.INVTIDSKU INNER JOIN
    IN_SUBCATEGORY ON 
    IN_INVENTORY.CLASSUBID = IN_SUBCATEGORY.CLASSUBID AND
     IN_INVENTORY.CLASSID = IN_SUBCATEGORY.CLASSID INNER
     JOIN
    IN_CATEGORY ON 
    IN_SUBCATEGORY.CLASSID = IN_CATEGORY.CLASSID
go

IF OBJECT_ID('vw_in_ris_detalle_all') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_in_ris_detalle_all >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_in_ris_detalle_all >>>'
go

/* 
 * VIEW: vw_in_ris_resumendoc 
 */

CREATE VIEW dbo.vw_in_ris_resumendoc
AS
SELECT IN_INTRAHEAD.SITEID, SITE.NAME AS sitename, 
    IN_INTRAHEAD.TYPEDOCID, 
    IN_TYPEDOC.DESCR AS typedocdescr, 
    IN_INTRAHEAD.BATNBR, IN_INTRAHEAD.DOCTYPEID, 
    DOCTYPE.MINDES AS doctypedescr, IN_INTRAHEAD.REFNBR, 
    IN_INTRAHEAD.INTRDATE, IN_INTRAHEAD.INTRDATEPROCE, 
    IN_INTRAHEAD.FROMCURYID, CURRENCY.DESCRMONEY, 
    IN_VENDOR.TAXREGNBR, 
    IN_VENDOR.DESCR AS nproveedor, IN_INTRAHEAD.RLSED, 
    IN_INTRAHEAD.CURYRATE, IN_INTRAHEAD.INTRTAXPEN, 
    IN_INTRAHEAD.INTRTOTPEN, IN_INTRAHEAD.INTRTAXUSD, 
    IN_INTRAHEAD.INTRTOTUSD, 
    CASE IN_INTRAHEAD.RLSED WHEN 1 THEN 'SI' WHEN 0 THEN 'NO'
     END AS EMITIDO
FROM IN_INTRAHEAD INNER JOIN
    SITE ON IN_INTRAHEAD.SITEID = SITE.SITEID INNER JOIN
    DOCTYPE ON 
    IN_INTRAHEAD.DOCTYPEID = DOCTYPE.DOCTYPEID INNER JOIN
    IN_TYPEDOC ON 
    IN_INTRAHEAD.TYPEDOCID = IN_TYPEDOC.TYPEDOCID INNER
     JOIN
    CURRENCY ON 
    IN_INTRAHEAD.FROMCURYID = CURRENCY.CURYID INNER JOIN
    IN_VENDOR ON 
    IN_INTRAHEAD.VENDIDSS = IN_VENDOR.VENDIDSS
go

IF OBJECT_ID('vw_in_ris_resumendoc') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_in_ris_resumendoc >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_in_ris_resumendoc >>>'
go

/* 
 * VIEW: vw_ingresoscompraproveedor 
 */

CREATE VIEW dbo.vw_ingresoscompraproveedor
AS
SELECT IN_INTRAHEAD.SITEID, IN_INTRAHEAD.BATNBR, 
    IN_INTRAHEAD.TYPEDOCID, IN_INTRAHEAD.VENDIDSS, 
    IN_INTRAHEAD.DOCTYPEID, IN_INTRAHEAD.DOCSTATUSID, 
    IN_INTRAHEAD.REFNBR, IN_INTRAHEAD.CURYRATE, 
    IN_INTRAHEAD.SITEPROCES, IN_INTRAHEAD.NBRORDER, 
    IN_INTRAHEAD.DATENBRORDER, 
    IN_INTRAHEAD.USERIDORDER, IN_INTRAHEAD.INTRDATE, 
    IN_INTRAHEAD.INTRDATEPROCE, IN_INTRAHEAD.SITEDEST, 
    IN_INTRAHEAD.INTRTOTPEN, IN_INTRAHEAD.INTRTAXPEN, 
    IN_INTRAHEAD.INTRTOTUSD, IN_INTRAHEAD.INTRTAXUSD, 
    IN_INTRAHEAD.PERPOST, IN_TYPEDOC.STKBUY, 
    IN_TYPEDOC.DESCR, IN_VENDOR.DESCR AS NPROVEEDOR, 
    IN_VENDOR.TAXREGNBR AS RUC, SITE.NAME, 
    DOCTYPE.MINDES AS tdocumento, 
    CURRENCY.DESCRMONEY, 
    IN_INTRAHEAD.FROMCURYID
FROM IN_INTRAHEAD INNER JOIN
    SITE ON IN_INTRAHEAD.SITEID = SITE.SITEID INNER JOIN
    IN_VENDOR ON 
    IN_INTRAHEAD.VENDIDSS = IN_VENDOR.VENDIDSS INNER JOIN
    IN_TYPEDOC ON 
    IN_INTRAHEAD.TYPEDOCID = IN_TYPEDOC.TYPEDOCID INNER
     JOIN
    DOCTYPE ON 
    IN_INTRAHEAD.DOCTYPEID = DOCTYPE.DOCTYPEID INNER JOIN
    CURRENCY ON 
    IN_INTRAHEAD.FROMCURYID = CURRENCY.CURYID
go

IF OBJECT_ID('vw_ingresoscompraproveedor') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_ingresoscompraproveedor >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_ingresoscompraproveedor >>>'
go

/* 
 * VIEW: VW_KARDEXFS210 
 */

Create View VW_KARDEXFS210
as
SELECT INTRAMOV.SITEID,IN_INVENTORY.CLASSID, 
    INTRAMOV.INTRDATEPROCE,INTRAMOV.BATNBR, 
    DOCTYPE.MINDES,INTRAMOV.REFNBR, 
    INTRAMOV.TYPEDOCID, IN_TYPEDOC.DESCR, 
    SUM(CASE SWTINOUT WHEN  1 THEN QTY WHEN 0 THEN
    (QTY * - 1) END) AS QTYRESULT
FROM INTRAMOV INNER JOIN
    DOCTYPE ON 
    INTRAMOV.DOCTYPEID = DOCTYPE.DOCTYPEID INNER JOIN
    IN_TYPEDOC ON 
    INTRAMOV.TYPEDOCID = IN_TYPEDOC.TYPEDOCID INNER JOIN
    SITE ON INTRAMOV.SITEID = SITE.SITEID INNER JOIN
    IN_INVENTORY ON 
    INTRAMOV.INVTIDSKU = IN_INVENTORY.INVTIDSKU
GROUP BY INTRAMOV.SITEID,IN_INVENTORY.CLASSID, 
    INTRAMOV.INTRDATEPROCE,INTRAMOV.BATNBR, 
    DOCTYPE.MINDES,INTRAMOV.REFNBR, 
    INTRAMOV.TYPEDOCID, IN_TYPEDOC.DESCR
go

IF OBJECT_ID('VW_KARDEXFS210') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_KARDEXFS210 >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_KARDEXFS210 >>>'
go

/* 
 * VIEW: VW_KARDEXFS210SUBCATEG 
 */

Create View VW_KARDEXFS210SUBCATEG
as
SELECT INTRAMOV.SITEID,IN_INVENTORY.CLASSUBID, 
    INTRAMOV.INTRDATEPROCE,INTRAMOV.BATNBR, 
    DOCTYPE.MINDES,INTRAMOV.REFNBR, 
    INTRAMOV.TYPEDOCID, IN_TYPEDOC.DESCR, 
    SUM(CASE SWTINOUT WHEN  1 THEN QTY WHEN 0 THEN
    (QTY * - 1) END) AS QTYRESULT
FROM INTRAMOV INNER JOIN
    DOCTYPE ON 
    INTRAMOV.DOCTYPEID = DOCTYPE.DOCTYPEID INNER JOIN
    IN_TYPEDOC ON 
    INTRAMOV.TYPEDOCID = IN_TYPEDOC.TYPEDOCID INNER JOIN
    SITE ON INTRAMOV.SITEID = SITE.SITEID INNER JOIN
    IN_INVENTORY ON 
    INTRAMOV.INVTIDSKU = IN_INVENTORY.INVTIDSKU
GROUP BY INTRAMOV.SITEID,IN_INVENTORY.CLASSUBID, 
    INTRAMOV.INTRDATEPROCE,INTRAMOV.BATNBR, 
    DOCTYPE.MINDES,INTRAMOV.REFNBR, 
    INTRAMOV.TYPEDOCID, IN_TYPEDOC.DESCR
/***************************************************************************************************************************/
go

IF OBJECT_ID('VW_KARDEXFS210SUBCATEG') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_KARDEXFS210SUBCATEG >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_KARDEXFS210SUBCATEG >>>'
go

/* 
 * VIEW: VW_LISTAPRECIOS 
 */

CREATE VIEW VW_LISTAPRECIOS
AS
SELECT CAT.CLASSID, CAT.DESCR AS DESCR_CAT, 
    IN_SUBCATEGORY.DESCR AS DESCR_SUBCAT, 
    TAXS.VALUE AS valueigv, TYPE.INVTYPEID, 
    TYPE.DESCR AS DESCR_TYPE, INV.INVTIDSKU, INV.DESCR, 
    INV.STKUNITID, SITE.SITEID, SITE.NAME, DAT.PRECLVID, 
    DAT.SLS_PRICE, 
    IN_PRICELEVELTYPE.DESCR AS nombre_lista, 
    IN_PRICELEVELTYPE.curyid AS moneda_lista, DAT.QTYAVAIL, 
    INV.TAXIGV, IN_SUBCATEGORY.CLASSUBID
FROM SITE INNER JOIN
    IN_INVENTORY INV INNER JOIN
    IN_INVENTORYDAT DAT ON 
    INV.INVTIDSKU = DAT.INVTIDSKU INNER JOIN
    IN_INVENTORYTYPE TYPE ON 
    INV.INVTYPEID = TYPE.INVTYPEID ON 
    SITE.SITEID = DAT.SITEID INNER JOIN
    IN_PRICELEVELTYPE ON 
    DAT.PRECLVID = IN_PRICELEVELTYPE.PRECLVID INNER JOIN
    IN_SUBCATEGORY INNER JOIN
    IN_CATEGORY CAT ON 
    IN_SUBCATEGORY.CLASSID = CAT.CLASSID ON 
    INV.CLASSUBID = IN_SUBCATEGORY.CLASSUBID LEFT OUTER JOIN
    TAXS ON INV.TAXIGV = TAXS.TAXID
go

IF OBJECT_ID('VW_LISTAPRECIOS') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_LISTAPRECIOS >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_LISTAPRECIOS >>>'
go

/* 
 * VIEW: VW_ListarArticulos 
 */

Create View VW_ListarArticulos
AS
  SELECT INVTIDSKU, DESCR, TAXISC, TAXIGV, CLASSUBID, 
    INVTYPEID, STKUNITID, CLASSID, WTSTK1, STKFUELS, 
    STKSTORE, STKOTHER, STKITEM, KIT, DISCDSCTO1, 
    DISCDSCTO2, DISCMTO, STKRECEPT, STKSALES, STATUS, 
    CONSIGNMENT, DISASSEMBLE
  FROM IN_INVENTORY
/*************************************************************************************************************************/
go

IF OBJECT_ID('VW_ListarArticulos') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_ListarArticulos >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_ListarArticulos >>>'
go

/* 
 * VIEW: VW_LISTASTOCK 
 */

CREATE VIEW VW_LISTASTOCK
AS
SELECT CAT.CLASSID, CAT.DESCR AS DESCR_CAT, 
    IN_SUBCATEGORY.DESCR AS DESCR_SUBCAT, 
    INV.INVTIDSKU, INV.DESCR, INV.STKUNITID, SITE.SITEID, 
    SITE.NAME, DAT.QTYAVAIL, 
    IN_SUBCATEGORY.CLASSUBID
FROM SITE INNER JOIN
    IN_INVENTORY INV INNER JOIN
    IN_INVENTORYDAT DAT ON 
    INV.INVTIDSKU = DAT.INVTIDSKU ON 
    SITE.SITEID = DAT.SITEID INNER JOIN
    IN_SUBCATEGORY INNER JOIN
    IN_CATEGORY CAT ON 
    IN_SUBCATEGORY.CLASSID = CAT.CLASSID ON 
    INV.CLASSUBID = IN_SUBCATEGORY.CLASSUBID
go

IF OBJECT_ID('VW_LISTASTOCK') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_LISTASTOCK >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_LISTASTOCK >>>'
go

/* 
 * VIEW: VW_PRODUCTOSXPROVEEDOR 
 */

CREATE VIEW dbo.VW_PRODUCTOSXPROVEEDOR
AS
SELECT IN_VENDORINVT.VENDIDSS, 
    IN_VENDOR.DESCR AS NPROVEEDOR, 
    IN_VENDORINVT.INVTIDSKU, 
    IN_INVENTORY.DESCR AS NPRODUCTO, 
    IN_INVENTORYDAT.SITEID, IN_INVENTORYDAT.QTYAVAIL, 
    IN_INVENTORYDAT.SLS_PRICE, 
    IN_INVENTORYDAT.PRECLVID, IN_INVENTORY.TAXIGV, 
    TAXS.VALUE AS VALUEIGV, IN_INVENTORY.STKUNITID, 
    SITE.NAME
FROM IN_VENDORINVT INNER JOIN
    IN_INVENTORY ON 
    IN_VENDORINVT.INVTIDSKU = IN_INVENTORY.INVTIDSKU INNER
     JOIN
    IN_VENDOR ON 
    IN_VENDORINVT.VENDIDSS = IN_VENDOR.VENDIDSS INNER JOIN
    IN_INVENTORYDAT ON 
    IN_INVENTORY.INVTIDSKU = IN_INVENTORYDAT.INVTIDSKU INNER
     JOIN
    SITE ON 
    IN_INVENTORYDAT.SITEID = SITE.SITEID LEFT OUTER JOIN
    TAXS ON IN_INVENTORY.TAXIGV = TAXS.TAXID
go

IF OBJECT_ID('VW_PRODUCTOSXPROVEEDOR') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_PRODUCTOSXPROVEEDOR >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_PRODUCTOSXPROVEEDOR >>>'
go

/* 
 * VIEW: VW_REPORTELISTAPRECIOS 
 */

CREATE VIEW VW_REPORTELISTAPRECIOS 
AS
   SELECT PRECLVID,In_PriceLevelType.DESCR,IN_PRICELEVELTYPE.CURYTYPEID AS TIPOCAMBIO,curyratetype.DESCR as descrtipocambio,
          currency.curyid as [codcurr], currency.descr as [desccurr] 
          FROM (In_PriceLevelType inner join currency on In_PriceLevelType.curyid=currency.curyid)
          LEFT JOIN curyratetype ON  In_PriceLevelType.CURYTYPEID=curyratetype.CURYTYPEID
go

IF OBJECT_ID('VW_REPORTELISTAPRECIOS') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_REPORTELISTAPRECIOS >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_REPORTELISTAPRECIOS >>>'
go

/* 
 * VIEW: VW_REPORTETIPOMOVIMIENTO 
 */

/************************************************************************************************************************/
CREATE VIEW VW_REPORTETIPOMOVIMIENTO 
AS
 SELECT TYPEDOCID, DESCR,CASE INOUT WHEN 1 THEN 'I' WHEN 0 THEN 'S' END AS TIPO,
        CASE STKVALUE WHEN 1 THEN 'SI' WHEN 0 THEN 'NO' END AS VALORIZADO, 
        [TIPOVAL] = CASE  WHEN STKVALUE= 1  AND STKCOSPRICE=1  THEN 'COSTO' 
                          WHEN STKVALUE= 1  AND STKCOSPRICE=0  THEN 'PRECIO'  END,  
        [TIPOCOSTO]= CASE WHEN STKAVGCOST=0 AND STKVALUE= 1  AND STKCOSPRICE=1  THEN 'REPOSICION' 
                          WHEN STKAVGCOST=1 AND STKVALUE= 1  AND STKCOSPRICE=1  THEN 'PROMEDIO' END                       
        ,PRECLVLID
 FROM IN_TYPEDOC
go

IF OBJECT_ID('VW_REPORTETIPOMOVIMIENTO') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_REPORTETIPOMOVIMIENTO >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_REPORTETIPOMOVIMIENTO >>>'
go

/* 
 * VIEW: vw_se_screen 
 */

CREATE VIEW dbo.vw_se_screen
AS
SELECT SCREENID, 
    SCREENID + '-' + MODULEID + '-' + SCREENTYPE + '-' + SCREENNAME
     AS screen, MODULEID, SCREENNAME, SCREENTYPE
FROM SE_SCREEN
go

IF OBJECT_ID('vw_se_screen') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_se_screen >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_se_screen >>>'
go

/* 
 * VIEW: VW_SITECATEGORY 
 */

CREATE VIEW VW_SITECATEGORY
AS
select SITE.SITEID,IN_CATEGORY.CLASSID,NULL AS [INTRDATEPROCE],"" AS [BATNBR],"" AS [MINDES],"" AS [REFNBR],
       "" AS [TIPO],"Total Ventas Expresadas en Cantidad " AS [DESCR] FROM SITE CROSS JOIN IN_CATEGORY 
/*************************************************************************************************************************/
go

IF OBJECT_ID('VW_SITECATEGORY') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_SITECATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_SITECATEGORY >>>'
go

/* 
 * VIEW: VW_SITESUBCATEGORY 
 */

CREATE VIEW VW_SITESUBCATEGORY
AS
select SITE.SITEID,IN_SUBCATEGORY.CLASSUBID,NULL AS [INTRDATEPROCE],"" AS [BATNBR],"" AS [MINDES],"" AS [REFNBR],
       "" AS [TIPO],"Total Ventas Expresadas en Cantidad " AS [DESCR] FROM SITE CROSS JOIN IN_SUBCATEGORY 

/************************************************************************************************************************/
go

IF OBJECT_ID('VW_SITESUBCATEGORY') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_SITESUBCATEGORY >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_SITESUBCATEGORY >>>'
go

/* 
 * VIEW: vw_subcatseleccionadas_cluster 
 */

CREATE VIEW dbo.vw_subcatseleccionadas_cluster
AS
SELECT IN_CLUSTER.CLASSUBID, 
    IN_SUBCATEGORY.DESCR,
	(select count(invtidsku) from in_inventory where classubid=in_cluster.classubid)-	
	(select count(invtidsku) from in_inventphisical where classubid=in_cluster.classubid) as dif
FROM IN_CLUSTER INNER JOIN
    IN_SUBCATEGORY ON 
    IN_CLUSTER.CLASSUBID = IN_SUBCATEGORY.CLASSUBID
go

IF OBJECT_ID('vw_subcatseleccionadas_cluster') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_subcatseleccionadas_cluster >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_subcatseleccionadas_cluster >>>'
go

/* 
 * VIEW: VW_USUARIOSGRUPOS 
 */

CREATE VIEW dbo.VW_USUARIOSGRUPOS
AS
SELECT g.GROUPID, g.GROUPNAME, ug.USERID, 
    SE_USERREC.USERNAME
FROM SE_USERGROUP ug INNER JOIN
    SE_GROUPREC g ON ug.GROUPID = g.GROUPID INNER JOIN
    SE_USERREC ON ug.USERID = SE_USERREC.USERID
go

IF OBJECT_ID('VW_USUARIOSGRUPOS') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_USUARIOSGRUPOS >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_USUARIOSGRUPOS >>>'
go

/* 
 * VIEW: vw_valorinventarioFISICO 
 */

CREATE VIEW dbo.vw_valorinventarioFISICO
AS
SELECT IN_INVENTPHISICAL.SITEID, SITE.NAME AS sitename, 
    IN_INVENTPHISICAL.CLASSUBID, 
    IN_SUBCATEGORY.DESCR AS nsubcat, 
    IN_INVENTPHISICAL.CLASSID, 
    IN_CATEGORY.DESCR AS ncat, 
    IN_INVENTPHISICAL.INVTIDSKU, 
    IN_INVENTORY.DESCR AS narticulo, 
    IN_INVENTPHISICAL.STOCKPHISICAL, 
    IN_INVENTORYDAT.AVGCOSTPEN, 
    IN_INVENTORYDAT.AVGCOSTUSD, 
    IN_INVENTORYDAT.STDCOSTPEN, 
    IN_INVENTORYDAT.STDCOSTUSD
FROM IN_INVENTORY INNER JOIN
    IN_INVENTPHISICAL INNER JOIN
    IN_INVENTORYDAT ON 
    IN_INVENTPHISICAL.INVTIDSKU = IN_INVENTORYDAT.INVTIDSKU
     ON 
    IN_INVENTORY.INVTIDSKU = IN_INVENTPHISICAL.INVTIDSKU INNER
     JOIN
    IN_CATEGORY INNER JOIN
    IN_SUBCATEGORY ON 
    IN_CATEGORY.CLASSID = IN_SUBCATEGORY.CLASSID ON 
    IN_INVENTPHISICAL.CLASSUBID = IN_SUBCATEGORY.CLASSUBID
     INNER JOIN
    SITE ON IN_INVENTPHISICAL.SITEID = SITE.SITEID
go

IF OBJECT_ID('vw_valorinventarioFISICO') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_valorinventarioFISICO >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_valorinventarioFISICO >>>'
go

/* 
 * VIEW: VWREPORTDIFCATEG 
 */

CREATE VIEW VWREPORTDIFCATEG
AS
SELECT IN_INVENTORYDAT.SITEID,IN_INVENTORYDAT.INVTIDSKU,IN_INVENTORY.CLASSID,ISNULL(ENDDATEINV,'') AS [ENDDATEINV],
       LASTINVFIS,AVGCOSTPEN
       FROM (IN_INVENTORY INNER JOIN IN_INVENTORYDAT ON IN_INVENTORY.INVTIDSKU=IN_INVENTORYDAT.INVTIDSKU) LEFT JOIN
             IN_CATEGORY ON IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID
go

IF OBJECT_ID('VWREPORTDIFCATEG') IS NOT NULL
    PRINT '<<< CREATED VIEW VWREPORTDIFCATEG >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VWREPORTDIFCATEG >>>'
go

/* 
 * VIEW: VWREPORTDIFCATEGRESUMIDO 
 */

CREATE VIEW VWREPORTDIFCATEGRESUMIDO
AS
SELECT IN_INVENTORYDAT.SITEID,IN_INVENTORYDAT.INVTIDSKU,IN_INVENTORY.CLASSID,ISNULL(ENDDATEINV,'') AS [ENDDATEINV],
       QTYAVAIL,AVGCOSTPEN
       FROM (IN_INVENTORY INNER JOIN IN_INVENTORYDAT ON IN_INVENTORY.INVTIDSKU=IN_INVENTORYDAT.INVTIDSKU) LEFT JOIN
             IN_CATEGORY ON IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID
go

IF OBJECT_ID('VWREPORTDIFCATEGRESUMIDO') IS NOT NULL
    PRINT '<<< CREATED VIEW VWREPORTDIFCATEGRESUMIDO >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VWREPORTDIFCATEGRESUMIDO >>>'
go

/* 
 * VIEW: VWREPORTDIFSUBCATEG 
 */

CREATE VIEW VWREPORTDIFSUBCATEG
AS
SELECT IN_INVENTORYDAT.SITEID,IN_INVENTORYDAT.INVTIDSKU,IN_INVENTORY.CLASSUBID,ISNULL(ENDDATEINV,'') AS [ENDDATEINV],
       LASTINVFIS,AVGCOSTPEN
       FROM (IN_INVENTORY INNER JOIN IN_INVENTORYDAT ON IN_INVENTORY.INVTIDSKU=IN_INVENTORYDAT.INVTIDSKU) LEFT JOIN
             IN_SUBCATEGORY ON IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID
go

IF OBJECT_ID('VWREPORTDIFSUBCATEG') IS NOT NULL
    PRINT '<<< CREATED VIEW VWREPORTDIFSUBCATEG >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VWREPORTDIFSUBCATEG >>>'
go

/* 
 * VIEW: VWREPORTDIFSUBCATEGRESUMIDO 
 */

CREATE VIEW VWREPORTDIFSUBCATEGRESUMIDO
AS
SELECT IN_INVENTORYDAT.SITEID,IN_INVENTORYDAT.INVTIDSKU,IN_INVENTORY.CLASSUBID,ISNULL(ENDDATEINV,'') AS [ENDDATEINV],
       QTYAVAIL,AVGCOSTPEN
       FROM (IN_INVENTORY INNER JOIN IN_INVENTORYDAT ON IN_INVENTORY.INVTIDSKU=IN_INVENTORYDAT.INVTIDSKU) LEFT JOIN
             IN_SUBCATEGORY ON IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID
go

IF OBJECT_ID('VWREPORTDIFSUBCATEGRESUMIDO') IS NOT NULL
    PRINT '<<< CREATED VIEW VWREPORTDIFSUBCATEGRESUMIDO >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VWREPORTDIFSUBCATEGRESUMIDO >>>'
go

/* 
 * VIEW: VW_AJUSTECANTIDAD_SUMA 
 */

CREATE VIEW VW_AJUSTECANTIDAD_SUMA
AS
SELECT SITEID,INVTIDSKU,SUM(CANTIDAD) AS CANTIDAD FROM VW_AJUSTECANTIDAD 
GROUP BY SITEID,INVTIDSKU
go

IF OBJECT_ID('VW_AJUSTECANTIDAD_SUMA') IS NOT NULL
    PRINT '<<< CREATED VIEW VW_AJUSTECANTIDAD_SUMA >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW VW_AJUSTECANTIDAD_SUMA >>>'
go

/* 
 * VIEW: vw_catdisponibles_cluster 
 */

CREATE VIEW dbo.vw_catdisponibles_cluster
AS
SELECT CLASSID, DESCR, 0 AS dif
FROM IN_CATEGORY
WHERE (CLASSID NOT IN
        (SELECT classid
      FROM vw_catseleccionadas_cluster))
go

IF OBJECT_ID('vw_catdisponibles_cluster') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_catdisponibles_cluster >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_catdisponibles_cluster >>>'
go

/* 
 * VIEW: vw_derechos 
 */

CREATE VIEW dbo.vw_derechos
AS
SELECT screenid, userid, viewrights, insertrights, updaterights, 
    deleterights, printrights, NULLRIGHTS, CLOSERIGHTS, 
    screentype
FROM vw_derechosusuarios
UNION
SELECT screenid, userid, viewrights, insertrights, updaterights, 
    deleterights, printrights, NULLRIGHTS, CLOSERIGHTS, 
    screentype
FROM vw_derechosusuariosxgrupos
go

IF OBJECT_ID('vw_derechos') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_derechos >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_derechos >>>'
go

/* 
 * VIEW: vw_gruposposibles 
 */

CREATE VIEW dbo.vw_gruposposibles
AS
SELECT GROUPID, GROUPNAME, USERID, USERNAME
FROM VW_CUSERGROUPS A
WHERE GROUPID NOT IN
        (SELECT GROUPID
      FROM SE_USERGROUP
      WHERE A.USERID = USERID)
go

IF OBJECT_ID('vw_gruposposibles') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_gruposposibles >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_gruposposibles >>>'
go

/* 
 * VIEW: vw_subcatdisponibles_cluster 
 */

CREATE VIEW dbo.vw_subcatdisponibles_cluster
AS
SELECT CLASSUBID, DESCR, 0 AS dif
FROM IN_SUBCATEGORY
WHERE (CLASSUBID NOT IN
        (SELECT classubid
      FROM vw_subcatseleccionadas_cluster))
go

IF OBJECT_ID('vw_subcatdisponibles_cluster') IS NOT NULL
    PRINT '<<< CREATED VIEW vw_subcatdisponibles_cluster >>>'
ELSE
    PRINT '<<< FAILED CREATING VIEW vw_subcatdisponibles_cluster >>>'
go

/* 
 * INDEX: IDX_IN_INVENTORY__DESCR 
 */

CREATE UNIQUE INDEX IDX_IN_INVENTORY__DESCR ON IN_INVENTORY(DESCR)
go
IF EXISTS (SELECT * FROM sysindexes WHERE id=OBJECT_ID('IN_INVENTORY') AND name='IDX_IN_INVENTORY__DESCR')
    PRINT '<<< CREATED INDEX IN_INVENTORY.IDX_IN_INVENTORY__DESCR >>>'
ELSE
    PRINT '<<< FAILED CREATING INDEX IN_INVENTORY.IDX_IN_INVENTORY__DESCR >>>'
go

/* 
 * INDEX: IX_IN_INVENTORY 
 */

CREATE UNIQUE INDEX IX_IN_INVENTORY ON IN_INVENTORY(CLASSID, CLASSUBID, INVTIDSKU)
go
IF EXISTS (SELECT * FROM sysindexes WHERE id=OBJECT_ID('IN_INVENTORY') AND name='IX_IN_INVENTORY')
    PRINT '<<< CREATED INDEX IN_INVENTORY.IX_IN_INVENTORY >>>'
ELSE
    PRINT '<<< FAILED CREATING INDEX IN_INVENTORY.IX_IN_INVENTORY >>>'
go

/* 
 * INDEX: INDX_INVTIDSKU 
 */

CREATE UNIQUE INDEX INDX_INVTIDSKU ON IN_INVENTORY(INVTIDSKU)
go
IF EXISTS (SELECT * FROM sysindexes WHERE id=OBJECT_ID('IN_INVENTORY') AND name='INDX_INVTIDSKU')
    PRINT '<<< CREATED INDEX IN_INVENTORY.INDX_INVTIDSKU >>>'
ELSE
    PRINT '<<< FAILED CREATING INDEX IN_INVENTORY.INDX_INVTIDSKU >>>'
go

/* 
 * INDEX: IDX_IN_INVENTORY__INVTIDSKU 
 */

CREATE UNIQUE INDEX IDX_IN_INVENTORY__INVTIDSKU ON IN_INVENTORY(CLASSID, CLASSUBID, INVTIDSKU)
go
IF EXISTS (SELECT * FROM sysindexes WHERE id=OBJECT_ID('IN_INVENTORY') AND name='IDX_IN_INVENTORY__INVTIDSKU')
    PRINT '<<< CREATED INDEX IN_INVENTORY.IDX_IN_INVENTORY__INVTIDSKU >>>'
ELSE
    PRINT '<<< FAILED CREATING INDEX IN_INVENTORY.IDX_IN_INVENTORY__INVTIDSKU >>>'
go

ALTER TABLE IN_INVENTHISTOPHISI ADD CONSTRAINT RefSITE85 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE AR_CCDOC ADD CONSTRAINT FK__AR_CCDOC__4D01216C 
    FOREIGN KEY (DOCTYPEID,NBRDOCUMENT)
    REFERENCES OP_SALES(DOCTYPEID,NBRDOCUMENT)
go

ALTER TABLE AR_CCDOC ADD CONSTRAINT FK__AR_CCDOC__CURYID__4DF545A5 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE AR_CCDOC ADD CONSTRAINT FK__AR_CCDOC__CURYTY__51C5D689 
    FOREIGN KEY (CURYTYPEID)
    REFERENCES CURYRATETYPE(CURYTYPEID)
go

ALTER TABLE AR_CCDOC ADD CONSTRAINT FK__AR_CCDOC__CUSTID__4FDD8E17 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE AR_CCDOC ADD CONSTRAINT FK__AR_CCDOC__DOCSTA__4EE969DE 
    FOREIGN KEY (DOCSTATUSID)
    REFERENCES IN_DOCSTATUS(DOCSTATUSID)
go

ALTER TABLE AR_CCDOC ADD CONSTRAINT FK__AR_CCDOC__MODSAL__50D1B250 
    FOREIGN KEY (MODSALESID)
    REFERENCES OP_MODSALES(MODSALESID)
go


ALTER TABLE AR_CCDOCDET ADD CONSTRAINT FK__AR_CCDOCD__BANKI__47484816 
    FOREIGN KEY (BANKID)
    REFERENCES BANK(BANKID)
go

ALTER TABLE AR_CCDOCDET ADD CONSTRAINT FK__AR_CCDOCD__CURYI__465423DD 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE AR_CCDOCDET ADD CONSTRAINT FK__AR_CCDOCD__CURYT__4A24B4C1 
    FOREIGN KEY (CURYTYPEID)
    REFERENCES CURYRATETYPE(CURYTYPEID)
go

ALTER TABLE AR_CCDOCDET ADD CONSTRAINT FK__AR_CCDOCD__DOCST__49309088 
    FOREIGN KEY (DOCSTATUSID)
    REFERENCES IN_DOCSTATUS(DOCSTATUSID)
go

ALTER TABLE AR_CCDOCDET ADD CONSTRAINT FK__AR_CCDOCDET__483C6C4F 
    FOREIGN KEY (DOCTYPEID,NBRDOCUMENT)
    REFERENCES AR_CCDOC(DOCTYPEID,NBRDOCUMENT)
go


ALTER TABLE CITY ADD CONSTRAINT FK_STATE_CITY 
    FOREIGN KEY (STATEID)
    REFERENCES STATE(STATEID)
go


ALTER TABLE CURYRATE ADD CONSTRAINT FK_CURRENCY_CURYRATE__FROMCURY 
    FOREIGN KEY (FROMCURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE CURYRATE ADD CONSTRAINT FK_CURRENCY_CURYRATE__TOCURY 
    FOREIGN KEY (TOCURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE CURYRATE ADD CONSTRAINT FK_CURYRATETYPE_CURYRATE 
    FOREIGN KEY (CURYTYPEID)
    REFERENCES CURYRATETYPE(CURYTYPEID)
go


ALTER TABLE CUST_LIMIT ADD CONSTRAINT FK__CUST_LIMI__CUSTI__0E0FCABA 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE CUST_LIMIT ADD CONSTRAINT FK_SITE__CUST_LIMIT 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE CUSTOMER ADD CONSTRAINT FK__CUSTOMER__CUSTST__4A59BEEB 
    FOREIGN KEY (CUSTSTATUSID)
    REFERENCES CUSTSTATUS(CUSTSTATUSID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_CITY__CUSTOMER 
    FOREIGN KEY (CITYID)
    REFERENCES CITY(CITYID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_COUNTRY__CUSTOMER 
    FOREIGN KEY (COUNTRYID)
    REFERENCES COUNTRY(COUNTRYID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_CURRENCY__CUSTOMER 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_CURYRATETYPE__CUSTOMER 
    FOREIGN KEY (CURYTYPEID)
    REFERENCES CURYRATETYPE(CURYTYPEID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_MODSALES__CUSTOMER 
    FOREIGN KEY (MODSALESID)
    REFERENCES OP_MODSALES(MODSALESID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_OP_CUSTCLASS__CUSTOMER 
    FOREIGN KEY (CUSTCLASSID)
    REFERENCES OP_CUSTCLASS(CUSTCLASSID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_OP_CUSTZONE__CUSTOMER 
    FOREIGN KEY (CUSTZONEID)
    REFERENCES OP_CUSTZONE(CUSTZONEID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_OP_PRCLVLCUST__CUSTOMER 
    FOREIGN KEY (PRCLVLCUSTID)
    REFERENCES OP_PRCLVLCUST(PRCLVLCUSTID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_OP_SALESPERSON__CUSTOMER 
    FOREIGN KEY (SALESPERID)
    REFERENCES OP_SALESPERSON(SALESPERID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_STATE__CUSTOMER 
    FOREIGN KEY (STATEID)
    REFERENCES STATE(STATEID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_TAXS__CUSTOMER_TAXID1 
    FOREIGN KEY (TAXID1)
    REFERENCES TAXS(TAXID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_TAXS__CUSTOMER_TAXID2 
    FOREIGN KEY (TAXID2)
    REFERENCES TAXS(TAXID)
go

ALTER TABLE CUSTOMER ADD CONSTRAINT FK_TERMS__CUSTOMER 
    FOREIGN KEY (TERMID)
    REFERENCES TERMS(TERMID)
go


ALTER TABLE CUSTWARRANTY ADD CONSTRAINT FK_CURRENCY__CUSTWARRANTY 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE CUSTWARRANTY ADD CONSTRAINT FK_CUSTOMER__CUSTGARANTIA 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE CUSTWARRANTY ADD CONSTRAINT FK_CUSTWARRANTYTYPE__CUSTWARRANTY 
    FOREIGN KEY (CUSTGARANTIATYPEID)
    REFERENCES CUSTWARRANTYTYPE(CUSTGARANTIATYPEID)
go


ALTER TABLE IN_INTRAHEAD ADD CONSTRAINT FK_DOCTYPE_IN_INTRAHEAD 
    FOREIGN KEY (DOCTYPEID)
    REFERENCES DOCTYPE(DOCTYPEID)
go

ALTER TABLE IN_INTRAHEAD ADD CONSTRAINT FK_IN_DOCSTATUS_IN_INTRAHEAD 
    FOREIGN KEY (DOCSTATUSID)
    REFERENCES IN_DOCSTATUS(DOCSTATUSID)
go

ALTER TABLE IN_INTRAHEAD ADD CONSTRAINT FK_IN_TYPEDOC_IN_INTRAHEAD 
    FOREIGN KEY (TYPEDOCID)
    REFERENCES IN_TYPEDOC(TYPEDOCID)
go

ALTER TABLE IN_INTRAHEAD ADD CONSTRAINT FK_IN_VENDOR_IN_INTRAHEAD 
    FOREIGN KEY (VENDIDSS)
    REFERENCES IN_VENDOR(VENDIDSS)
go

ALTER TABLE IN_INTRAHEAD ADD CONSTRAINT FK_SITE_IN_INTRAHEAD 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE IN_INTRAITEM ADD CONSTRAINT FK_IN_INTRAHEAD_IN_INTRAITEM 
    FOREIGN KEY (SITEID,BATNBR,TYPEDOCID)
    REFERENCES IN_INTRAHEAD(SITEID,BATNBR,TYPEDOCID)
go

ALTER TABLE IN_INTRAITEM ADD CONSTRAINT FK_IN_INVENTORY_IN_INTRAITEM 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE IN_INTRAITEM ADD CONSTRAINT RefIN_DOCSTATUS88 
    FOREIGN KEY (DOCSTATUSID)
    REFERENCES IN_DOCSTATUS(DOCSTATUSID)
go


ALTER TABLE IN_INVENTORY ADD CONSTRAINT FK_TAXS_IN_INVENTORY___ISC 
    FOREIGN KEY (TAXISC)
    REFERENCES TAXS(TAXID)
go


ALTER TABLE IN_INVENTORYALTER ADD CONSTRAINT FK_IN_INVENTORY_IN_INVENTORYALTER 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go


ALTER TABLE IN_INVENTORYDAT ADD CONSTRAINT FK_IN_INVENTORY_IN_INVENTORYDAT 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE IN_INVENTORYDAT ADD CONSTRAINT FK_IN_PRICELEVELTYPE_IN_INVENTORYDAT 
    FOREIGN KEY (PRECLVID)
    REFERENCES IN_PRICELEVELTYPE(PRECLVID)
go

ALTER TABLE IN_INVENTORYDAT ADD CONSTRAINT FK_SITE_IN_INVENTORYDAT 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE IN_INVENTORYDES ADD CONSTRAINT FK_IN_INVENTORY__IN_INVENTORYDES_SOURCE 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE IN_INVENTORYDES ADD CONSTRAINT FK_IN_INVENTORY__IN_INVENTORYDES_TARGET 
    FOREIGN KEY (INVTIDSKUDES)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go


ALTER TABLE IN_INVENTPHISICAL ADD CONSTRAINT FK_SITE_IN_INVENTPHISICAL 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE IN_KDXHIST ADD CONSTRAINT FK_IN_INVENTORY_IN_KDXHIST 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE IN_KDXHIST ADD CONSTRAINT FK_SITE_IN_KDXHIST 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE IN_KIT ADD CONSTRAINT FK_CURRENCY_IN_KIT 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE IN_KIT ADD CONSTRAINT FK_IN_INVENTORYUNITMED_IN_KIT 
    FOREIGN KEY (STKUNITID)
    REFERENCES IN_INVENTORYUNITMED(STKUNITID)
go

ALTER TABLE IN_KIT ADD CONSTRAINT FK_TAXS_IN_KIT 
    FOREIGN KEY (TAXID)
    REFERENCES TAXS(TAXID)
go


ALTER TABLE IN_KITCOMPONENT ADD CONSTRAINT FK_IN_INVENTORY_IN_KITCOMPONENT 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE IN_KITCOMPONENT ADD CONSTRAINT FK_IN_KIT_IN_KITCOMPONENT 
    FOREIGN KEY (KITID)
    REFERENCES IN_KIT(KITID)
go


ALTER TABLE IN_PRICELEVELTEM ADD CONSTRAINT FK_IN_PRICELEVELTYPE__IN_PRICELEVELTEM 
    FOREIGN KEY (PRECLVID)
    REFERENCES IN_PRICELEVELTYPE(PRECLVID)
go

ALTER TABLE IN_PRICELEVELTEM ADD CONSTRAINT FK_SE_USERREC__IN_PRICELEVELTEM_USER01 
    FOREIGN KEY (USERIDAUTORIZED01)
    REFERENCES SE_USERREC(USERID)
go

ALTER TABLE IN_PRICELEVELTEM ADD CONSTRAINT FK_SE_USERREC__IN_PRICELEVELTEM_USER02 
    FOREIGN KEY (USERIDAUTORIZED02)
    REFERENCES SE_USERREC(USERID)
go

ALTER TABLE IN_PRICELEVELTEM ADD CONSTRAINT FK_SITE__IN_PRICELEVELTEM 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go

ALTER TABLE IN_PRICELEVELTEM ADD CONSTRAINT FK_SITE__IN_PRICELEVELTEM_SITEID 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE IN_PRICELEVELTEMDET ADD CONSTRAINT FK__IN_PRICELEVELTEM__76384129 
    FOREIGN KEY (BATNBRPRICELEVELTEM,SITEID)
    REFERENCES IN_PRICELEVELTEM(BATNBRPRICELEVELTEM,SITEID)
go

ALTER TABLE IN_PRICELEVELTEMDET ADD CONSTRAINT FK_IN_INVENTORY_IN_PRICELEVELTEM 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go


ALTER TABLE IN_PRICELEVELTYPE ADD CONSTRAINT FK_CURRENCY_IN_PRICELEVELTYPE 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE IN_PRICELEVELTYPE ADD CONSTRAINT FK_CURYRATETYPE_IN_PRICELEVELTYPE 
    FOREIGN KEY (CURYTYPEID)
    REFERENCES CURYRATETYPE(CURYTYPEID)
go


ALTER TABLE IN_SUBCATEGORY ADD CONSTRAINT FK_IN_CATEGORY_IN_SUBCATEGORY 
    FOREIGN KEY (CLASSID)
    REFERENCES IN_CATEGORY(CLASSID)
go


ALTER TABLE IN_VENDOR ADD CONSTRAINT FK_CITY_IN_VENDOR 
    FOREIGN KEY (CITYID)
    REFERENCES CITY(CITYID)
go

ALTER TABLE IN_VENDOR ADD CONSTRAINT FK_COUNTRY_IN_VENDOR 
    FOREIGN KEY (COUNTRYID)
    REFERENCES COUNTRY(COUNTRYID)
go

ALTER TABLE IN_VENDOR ADD CONSTRAINT FK_IN_CLASSVENDOR_IN_VENDOR 
    FOREIGN KEY (CLASSVENDORID)
    REFERENCES IN_CLASSVENDOR(CLASSVENDORID)
go

ALTER TABLE IN_VENDOR ADD CONSTRAINT FK_TERMS_IN_VENDOR 
    FOREIGN KEY (TERMID)
    REFERENCES TERMS(TERMID)
go


ALTER TABLE INTRAMOV ADD CONSTRAINT FK_IN_INVENTORY_INTRAMOV 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE INTRAMOV ADD CONSTRAINT FK_SITE_INTRAMOV 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_CARA_FUEL ADD CONSTRAINT FK_IN_INVENTORYALTER__OP_CARA_FUEL 
    FOREIGN KEY (INVTIDBARR)
    REFERENCES IN_INVENTORYALTER(INVTIDBARR)
go


ALTER TABLE OP_CARD_MONT ADD CONSTRAINT FK_CURRENCY__OP_CARD_MONT 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_CARD_MONT ADD CONSTRAINT FK_OP_CARD__OP_CARD_MONT 
    FOREIGN KEY (CARDID)
    REFERENCES OP_CARD(CARDID)
go

ALTER TABLE OP_CARD_MONT ADD CONSTRAINT FK_SITE__OP_CARD_MONT 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_CCSALESPERSON ADD CONSTRAINT FK__OP_CCSALE__SALES__361DBC14 
    FOREIGN KEY (SALESPERID)
    REFERENCES OP_SALESPERSON(SALESPERID)
go

ALTER TABLE OP_CCSALESPERSON ADD CONSTRAINT FK_CURRENCY__OP_REMAIN_EXCEDED 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_CCSALESPERSON ADD CONSTRAINT FK_SITE__OP_REMAIN_EXCEDED 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_CCSALESPERSONDET ADD CONSTRAINT FK__OP_CCSALE__CCSAL__7DD962F1 
    FOREIGN KEY (CCSALESPERSONTYPEID)
    REFERENCES OP_CCSALESPERSONTYPE(CCSALESPERSONTYPEID)
go

ALTER TABLE OP_CCSALESPERSONDET ADD CONSTRAINT FK__OP_CCSALESPERSON__7CE53EB8 
    FOREIGN KEY (SITEID,SALESPERID)
    REFERENCES OP_CCSALESPERSON(SITEID,SALESPERID)
go


ALTER TABLE OP_CTRLFUEL ADD CONSTRAINT FK_IN_INVENTORY__OP_CTRLFUEL 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE OP_CTRLFUEL ADD CONSTRAINT FK_IN_INVENTORYUNITMED__OP_CTRLFUEL 
    FOREIGN KEY (STKUNITID)
    REFERENCES IN_INVENTORYUNITMED(STKUNITID)
go

ALTER TABLE OP_CTRLFUEL ADD CONSTRAINT FK_SITE__OP_CTRLFUEL 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_CTRLFUELDET ADD CONSTRAINT FK__OP_CTRLFU__CURYI__4C0CFD33 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_CTRLFUELDET ADD CONSTRAINT FK__OP_CTRLFUELDET__4B18D8FA 
    FOREIGN KEY (SITEID,BATNBRCTRLFUEL)
    REFERENCES OP_CTRLFUEL(SITEID,BATNBRCTRLFUEL)
go


ALTER TABLE OP_CTRLTURN ADD CONSTRAINT FK_SALESPERSON_CTRLTURN 
    FOREIGN KEY (SALESPERID)
    REFERENCES OP_SALESPERSON(SALESPERID)
go

ALTER TABLE OP_CTRLTURN ADD CONSTRAINT FK_SITE__OP_CTRLTURN 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_CTRLTURNDET ADD CONSTRAINT FK__OP_CTRLTU__INVTI__577EAFDF 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE OP_CTRLTURNDET ADD CONSTRAINT FK_CTRLTURN_CTRLTURNDET 
    FOREIGN KEY (BATNBRTURN)
    REFERENCES OP_CTRLTURN(BATNBRTURN)
go

ALTER TABLE OP_CTRLTURNDET ADD CONSTRAINT FK_OP_CARA_FUEL__OP_CTRLTURNDET 
    FOREIGN KEY (CARA)
    REFERENCES OP_CARA_FUEL(CARA)
go


ALTER TABLE OP_CUSTCARD ADD CONSTRAINT FK_CUSTOMER__OP_CUSTCARD 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go


ALTER TABLE OP_DEFINITIONPD ADD CONSTRAINT FK__OP_DEFINI__CLASS__7AFCF646 
    FOREIGN KEY (CLASSID)
    REFERENCES IN_CATEGORY(CLASSID)
go

ALTER TABLE OP_DEFINITIONPD ADD CONSTRAINT FK__OP_DEFINI__CUSTI__7BF11A7F 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE OP_DEFINITIONPD ADD CONSTRAINT FK__OP_DEFINI__INVTI__7A08D20D 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go


ALTER TABLE OP_DEPOSIT ADD CONSTRAINT FK__OP_DEPOSI__CURYI__188D592D 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_DEPOSIT ADD CONSTRAINT FK_OP_DEPOSITTYPE__OP_DEPOSIT 
    FOREIGN KEY (DEPOSITTYPEID)
    REFERENCES OP_DEPOSITTYPE(DEPOSITTYPEID)
go

ALTER TABLE OP_DEPOSIT ADD CONSTRAINT FK_OP_SALESPERSON__OP_DEPOSIT 
    FOREIGN KEY (SALESPERID)
    REFERENCES OP_SALESPERSON(SALESPERID)
go

ALTER TABLE OP_DEPOSIT ADD CONSTRAINT FK_SITE__OP_DEPOSIT 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_DEPOSITDET ADD CONSTRAINT FK_CUSTOMER__OP_DEPOSITDET 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE OP_DEPOSITDET ADD CONSTRAINT FK_OP_CARD__OP_DEPOSITDET 
    FOREIGN KEY (CARDID)
    REFERENCES OP_CARD(CARDID)
go

ALTER TABLE OP_DEPOSITDET ADD CONSTRAINT FK_OP_DEPOSIT__OP_DEPOSITDET 
    FOREIGN KEY (BATNBRDEPOSIT,SITEID)
    REFERENCES OP_DEPOSIT(BATNBRDEPOSIT,SITEID)
go


ALTER TABLE OP_DEPOSITSITE ADD CONSTRAINT FK_CUSTOMER__OP_DEPOSITSITE 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE OP_DEPOSITSITE ADD CONSTRAINT FK_SITE__OP_DEPOSITSITE 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_DEPOSITSITEDET ADD CONSTRAINT FK__OP_DEPOSI__CURYI__7820899B 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_DEPOSITSITEDET ADD CONSTRAINT FK__OP_DEPOSITSITEDE__772C6562 
    FOREIGN KEY (SITEID,BATNBRDEPOSITSITE)
    REFERENCES OP_DEPOSITSITE(SITEID,BATNBRDEPOSITSITE)
go

ALTER TABLE OP_DEPOSITSITEDET ADD CONSTRAINT FK_OP_DOCSUMMARYTYPE__OP_DEPOSITSITEDET 
    FOREIGN KEY (DOCSUMMARYTYPEID)
    REFERENCES OP_DOCSUMMARYTYPE(DOCSUMMARYTYPEID)
go


ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK__OP_DOCSUM__CURYI__00B5CF9C 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK__OP_DOCSUM__CURYT__029E180E 
    FOREIGN KEY (CURYTYPEID)
    REFERENCES CURYRATETYPE(CURYTYPEID)
go

ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK__OP_DOCSUM__CUSTI__01A9F3D5 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK__OP_DOCSUM__DOCST__066EA8F2 
    FOREIGN KEY (DOCSTATUSID)
    REFERENCES IN_DOCSTATUS(DOCSTATUSID)
go

ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK__OP_DOCSUM__SITEI__0762CD2B 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go

ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK__OP_DOCSUM__TERMI__03923C47 
    FOREIGN KEY (TERMID)
    REFERENCES TERMS(TERMID)
go

ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK_OP_DOCSUMMARYTYPE__OP_DOCSUMMARY 
    FOREIGN KEY (DOCSUMMARYTYPEID)
    REFERENCES OP_DOCSUMMARYTYPE(DOCSUMMARYTYPEID)
go

ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK_TAXS__OP_DOCSUMMARY_TAXID01 
    FOREIGN KEY (TAXID01)
    REFERENCES TAXS(TAXID)
go

ALTER TABLE OP_DOCSUMMARY ADD CONSTRAINT FK_TAXS__OP_DOCSUMMARY_TAXID02 
    FOREIGN KEY (TAXID02)
    REFERENCES TAXS(TAXID)
go


ALTER TABLE OP_DOCSUMMARYDET ADD CONSTRAINT FK__OP_DOCSUM__CURYI__0C278248 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_DOCSUMMARYDET ADD CONSTRAINT FK__OP_DOCSUM__CURYT__0D1BA681 
    FOREIGN KEY (CURYTYPEID)
    REFERENCES CURYRATETYPE(CURYTYPEID)
go

ALTER TABLE OP_DOCSUMMARYDET ADD CONSTRAINT FK__OP_DOCSUM__INVTI__094B159D 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE OP_DOCSUMMARYDET ADD CONSTRAINT FK__OP_DOCSUMMARYDET__0856F164 
    FOREIGN KEY (SITEID,DOCSUMMARYTYPEID,NBRDOCSUMMARY)
    REFERENCES OP_DOCSUMMARY(SITEID,DOCSUMMARYTYPEID,NBRDOCSUMMARY)
go

ALTER TABLE OP_DOCSUMMARYDET ADD CONSTRAINT FK_TAXS__OP_DOCSUMMARYDET__TAX01 
    FOREIGN KEY (TAXID01)
    REFERENCES TAXS(TAXID)
go

ALTER TABLE OP_DOCSUMMARYDET ADD CONSTRAINT FK_TAXS__OP_DOCSUMMARYDET__TAXID02 
    FOREIGN KEY (TAXID02)
    REFERENCES TAXS(TAXID)
go


ALTER TABLE OP_LOADSALESORD ADD CONSTRAINT FK_CURRENCY__OP_LOADSALESORD 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_LOADSALESORD ADD CONSTRAINT FK_CUSTOMER__OP_LOADSALESORD 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE OP_LOADSALESORD ADD CONSTRAINT FK_DOCTYPE__OP_LOADSALESORD 
    FOREIGN KEY (DOCTYPEID)
    REFERENCES DOCTYPE(DOCTYPEID)
go

ALTER TABLE OP_LOADSALESORD ADD CONSTRAINT FK_IN_DOCSTATUS__OP_LOADSALESORD 
    FOREIGN KEY (DOCSTATUSID)
    REFERENCES IN_DOCSTATUS(DOCSTATUSID)
go

ALTER TABLE OP_LOADSALESORD ADD CONSTRAINT FK_OP_SALESPERSON__OP_LOADSALESORD 
    FOREIGN KEY (SALESPERID)
    REFERENCES OP_SALESPERSON(SALESPERID)
go


ALTER TABLE OP_LOADSALESORDDET ADD CONSTRAINT FK_IN_INVENTORY__OP_LOADSALESORDDET 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE OP_LOADSALESORDDET ADD CONSTRAINT FK_IN_INVENTORYUNITMED__OP_LOADSALESORDDET 
    FOREIGN KEY (STKUNITID)
    REFERENCES IN_INVENTORYUNITMED(STKUNITID)
go

ALTER TABLE OP_LOADSALESORDDET ADD CONSTRAINT FK_OP_LOADSALESORD__OP_LOADSALESORDDET 
    FOREIGN KEY (NBRLOADSALE)
    REFERENCES OP_LOADSALESORD(NBRLOADSALE)
go


ALTER TABLE OP_PRCLVLCUST ADD CONSTRAINT FK_CURRENCY__OP_PRCLVLCUST 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_PRCLVLCUST ADD CONSTRAINT FK_CUSTOMER__OP_PRCLVLCUST 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE OP_PRCLVLCUST ADD CONSTRAINT FK_SE_USERREC__OP_PRCLVLCUST_USERIDAPPROVA1 
    FOREIGN KEY (USERIDAPPROVA1)
    REFERENCES SE_USERREC(USERID)
go

ALTER TABLE OP_PRCLVLCUST ADD CONSTRAINT FK_SE_USERREC__OP_PRCLVLCUST_USERIDAPPROVA2 
    FOREIGN KEY (USERIDAPPROVA2)
    REFERENCES SE_USERREC(USERID)
go

ALTER TABLE OP_PRCLVLCUST ADD CONSTRAINT FK_SITE__OP_PRCLVLCUST 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_PRCLVLCUSTDET ADD CONSTRAINT FK_IN_INVENTORY__OP_PRCLVLCUSTDET 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE OP_PRCLVLCUSTDET ADD CONSTRAINT FK_OP_PRCLVLCUST__OP_PRCLVLCUSTDET 
    FOREIGN KEY (PRCLVLCUSTID)
    REFERENCES OP_PRCLVLCUST(PRCLVLCUSTID)
go


ALTER TABLE OP_PROMOTIONCARD ADD CONSTRAINT FK__OP_PROMOT__PROMO__7ECD872A 
    FOREIGN KEY (PROMOTIONCARDTYPE)
    REFERENCES OP_PROMOTIONCARDTYPE(PROMOTIONCARDTYPE)
go


ALTER TABLE OP_SALES ADD CONSTRAINT FK__OP_SALES__CURYID__2BA02DA1 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK__OP_SALES__PROMOT__3064E2BE 
    FOREIGN KEY (PROMOTIONCARDID)
    REFERENCES OP_PROMOTIONCARD(PROMOTIONCARDID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_CURYRATETYPE__OP_SALES 
    FOREIGN KEY (CURYTYPEID)
    REFERENCES CURYRATETYPE(CURYTYPEID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_CUSTOMER__OP_SALES 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_DOCTYPE__OP_SALES 
    FOREIGN KEY (DOCTYPEID)
    REFERENCES DOCTYPE(DOCTYPEID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_IN_DOCSTATUS__OP_SALES 
    FOREIGN KEY (DOCSTATUSID)
    REFERENCES IN_DOCSTATUS(DOCSTATUSID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_MODSALES__OP_SALES 
    FOREIGN KEY (MODSALESID)
    REFERENCES OP_MODSALES(MODSALESID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_OP_CARD_OP_SALES1 
    FOREIGN KEY (CARDCREDITID1)
    REFERENCES OP_CARD(CARDID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_OP_CARD_OP_SALES2 
    FOREIGN KEY (CARDCREDITID2)
    REFERENCES OP_CARD(CARDID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_OP_SALESPERSON__OP_SALES 
    FOREIGN KEY (SALESPERID)
    REFERENCES OP_SALESPERSON(SALESPERID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_SITE__OP_SALES 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go

ALTER TABLE OP_SALES ADD CONSTRAINT FK_TERMS__OP_SALES 
    FOREIGN KEY (TERMID)
    REFERENCES TERMS(TERMID)
go


ALTER TABLE OP_SALESDET ADD CONSTRAINT FK_CURRENCY__OP_SALESDET 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_SALESDET ADD CONSTRAINT FK_IN_DOCSTATUS__OP_SALESDET 
    FOREIGN KEY (DOCSTATUSID)
    REFERENCES IN_DOCSTATUS(DOCSTATUSID)
go

ALTER TABLE OP_SALESDET ADD CONSTRAINT FK_IN_INVENTORY__OP_SALESDET 
    FOREIGN KEY (INVTIDSKU)
    REFERENCES IN_INVENTORY(INVTIDSKU)
go

ALTER TABLE OP_SALESDET ADD CONSTRAINT FK_OP_SALES__OP_SALESDET 
    FOREIGN KEY (DOCTYPEID,NBRDOCUMENT)
    REFERENCES OP_SALES(DOCTYPEID,NBRDOCUMENT)
go

ALTER TABLE OP_SALESDET ADD CONSTRAINT FK_OP_SALESPOINTSETING__OP_SALESDET 
    FOREIGN KEY (SALESPOINT,SALESPERID)
    REFERENCES OP_SALESPOINTSETING(SALESPOINT,SALESPERID)
go

ALTER TABLE OP_SALESDET ADD CONSTRAINT FK_SITE__OP_SALESDET 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_SALESPERSON ADD CONSTRAINT FK__OP_SALESP__STATU__52B9FAC2 
    FOREIGN KEY (STATUSPERSONID)
    REFERENCES OP_STATUSPERSON(STATUSPERSONID)
go


ALTER TABLE OP_SALESPOINTSETING ADD CONSTRAINT FK_CURRENCY__SALESPOINTSETING 
    FOREIGN KEY (CURYID)
    REFERENCES CURRENCY(CURYID)
go

ALTER TABLE OP_SALESPOINTSETING ADD CONSTRAINT FK_IN_PRICELEVELTYPE__SALESPOINTSETING 
    FOREIGN KEY (PRECLVID)
    REFERENCES IN_PRICELEVELTYPE(PRECLVID)
go

ALTER TABLE OP_SALESPOINTSETING ADD CONSTRAINT FK_OP_SALESPERSON__SALESPOINTSETING 
    FOREIGN KEY (SALESPERID)
    REFERENCES OP_SALESPERSON(SALESPERID)
go

ALTER TABLE OP_SALESPOINTSETING ADD CONSTRAINT FK_SITE__SALESPOINTSETING 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_SUMMARYDAY ADD CONSTRAINT FK_SITE__OP_SUMMARYDAY 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_SUMMARYSALES ADD CONSTRAINT FK__OP_SUMMAR__CLASS__446BDB6B 
    FOREIGN KEY (CLASSID)
    REFERENCES IN_CATEGORY(CLASSID)
go

ALTER TABLE OP_SUMMARYSALES ADD CONSTRAINT FK__OP_SUMMAR__CUSTC__4377B732 
    FOREIGN KEY (CUSTCLASSID)
    REFERENCES OP_CUSTCLASS(CUSTCLASSID)
go

ALTER TABLE OP_SUMMARYSALES ADD CONSTRAINT FK__OP_SUMMAR__CUSTI__428392F9 
    FOREIGN KEY (CUSTIDSS)
    REFERENCES CUSTOMER(CUSTIDSS)
go

ALTER TABLE OP_SUMMARYSALES ADD CONSTRAINT FK__OP_SUMMAR__DEFIN__418F6EC0 
    FOREIGN KEY (DEFINITIONID)
    REFERENCES OP_DEFINITIONPD(DEFINITIONID)
go

ALTER TABLE OP_SUMMARYSALES ADD CONSTRAINT FK__OP_SUMMAR__SITEI__409B4A87 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go

ALTER TABLE OP_SUMMARYSALES ADD CONSTRAINT FK_OP_DOCSUMMARYTYPE__OP_SUMMARYSALES 
    FOREIGN KEY (DOCSUMMARYTYPEID)
    REFERENCES OP_DOCSUMMARYTYPE(DOCSUMMARYTYPEID)
go


ALTER TABLE OP_TURNVSCARA ADD CONSTRAINT FK_OP_SALESPERSON__OP_TURNVSCARA 
    FOREIGN KEY (SALESPERID)
    REFERENCES OP_SALESPERSON(SALESPERID)
go

ALTER TABLE OP_TURNVSCARA ADD CONSTRAINT FK_SITE__OP_TURNVSCARA 
    FOREIGN KEY (SITEID)
    REFERENCES SITE(SITEID)
go


ALTER TABLE OP_TURNVSCARADET ADD CONSTRAINT FK_OP_CARA_FUEL__OP_TURNVSCARADET 
    FOREIGN KEY (CARA)
    REFERENCES OP_CARA_FUEL(CARA)
go

ALTER TABLE OP_TURNVSCARADET ADD CONSTRAINT FK_OP_TURNVSCARA__OP_TURNVSCARADET 
    FOREIGN KEY (SITEID,BATNBRTURNPERSON,DATEPROCE,NUMBERTURN)
    REFERENCES OP_TURNVSCARA(SITEID,BATNBRTURNPERSON,DATEPROCE,NUMBERTURN)
go


ALTER TABLE SE_ACCESSDETRIGHTS ADD CONSTRAINT FK_SE_SCREEN_SE_ACCESSDETRIGHTS 
    FOREIGN KEY (SCREENID)
    REFERENCES SE_SCREEN(SCREENID)
go


ALTER TABLE SE_SCREEN ADD CONSTRAINT FK_SE_MODULE_SE_SCREEN 
    FOREIGN KEY (MODULEID)
    REFERENCES SE_MODULE(MODULEID)
go


ALTER TABLE SE_USERGROUP ADD CONSTRAINT FK_SE_GROUPREC_SE_USERGROUP 
    FOREIGN KEY (GROUPID)
    REFERENCES SE_GROUPREC(GROUPID)
go

ALTER TABLE SE_USERGROUP ADD CONSTRAINT FK_SE_USERREC_SE_USERGROUP 
    FOREIGN KEY (USERID)
    REFERENCES SE_USERREC(USERID)
go


ALTER TABLE SITE ADD CONSTRAINT FK_CITY_SITE 
    FOREIGN KEY (CITYID)
    REFERENCES CITY(CITYID)
go

ALTER TABLE SITE ADD CONSTRAINT FK_COUNTRY_SITE 
    FOREIGN KEY (COUNTRYID)
    REFERENCES COUNTRY(COUNTRYID)
go



/* 
 * PROCEDURE: actualiza_stock_toma_proc 
 */


CREATE PROCEDURE actualiza_stock_toma_proc
    @id     ud_idcorporativo,
    @stock  UD_stock        

AS
BEGIN
    BEGIN TRAN

    UPDATE in_inventphisical
	SET  stockphisical= @stock 
	WHERE INVTIDSKU=@id

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'no se puede actualizar stock fisico '
        ROLLBACK TRAN
        RETURN(1)
    END
    COMMIT TRAN
    RETURN(0)
END
go
IF OBJECT_ID('actualiza_stock_toma_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE actualiza_stock_toma_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE actualiza_stock_toma_proc >>>'
go


/* 
 * PROCEDURE: AGREGARTODOSLOSDERECHOS_PROC 
 */

CREATE PROC AGREGARTODOSLOSDERECHOS_PROC
@USERID UD_USERID
AS
 INSERT INTO SE_ACCESSDETRIGHTS (USERID,
                                   SCREENID,
                                   VIEWRIGHTS,
                                   INSERTRIGHTS,
                                   UPDATERIGHTS,
                                   DELETERIGHTS,
                                   PRINTRIGHTS,
                                   NULLRIGHTS,
                                   CLOSERIGHTS)
SELECT @USERID,SCREENID,1,0,0,0,1,0,0 FROM SE_SCREEN WHERE SCREENID NOT IN (SELECT SCREENID FROM SE_ACCESSDETRIGHTS WHERE USERID=@USERID)
go
IF OBJECT_ID('AGREGARTODOSLOSDERECHOS_PROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE AGREGARTODOSLOSDERECHOS_PROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE AGREGARTODOSLOSDERECHOS_PROC >>>'
go


/* 
 * PROCEDURE: ConsultaParaReporteIngresoProc 
 */

CREATE PROCEDURE ConsultaParaReporteIngresoProc
( @SITEID     UD_IDSITE,
  @BATNBR     CHAR(10),
  @VENDIDSS   UD_IDVENDOR,
  @CURYID     UD_CURYID
)

AS
BEGIN
      DECLARE @MONEDABASE CHAR(3)
      SET @MONEDABASE =(SELECT CURYORIG FROM SETUP) 

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint
	DECLARE @ND_RESULTADOSETUP Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)
	SET @ND_RESULTADOSETUP=(select Floatoutput from setup)	
/*******************************************************************************************************/
      
      SELECT SEQUENCE,IN_INTRAITEM.INVTIDSKU,IN_VENDORINVT.BUYCOD,IN_INVENTORY.CLASSID,
             IN_INVENTORY.CLASSUBID,IN_INVENTORY.DESCR,IN_INVENTORYUNITMED.DESCR AS UNIT_MED,
             QTY,IN_INTRAITEM.STDCOSTPEN,INTRDATEPROCE,
             CASE @CURYID
			WHEN @MONEDABASE THEN IN_INTRAITEM.STDCOSTPEN
			ELSE IN_INTRAITEM.STDCOSTUSD
			END as Costo,
             CASE @CURYID
			WHEN @MONEDABASE THEN ROUND(QTY*IN_INTRAITEM.STDCOSTPEN,@ND_RESULTADOSETUP )
			ELSE ROUND(QTY*IN_INTRAITEM.STDCOSTUSD,@ND_RESULTADOSETUP)
			END as TOTAL

      FROM   IN_INTRAITEM,IN_INVENTORY,IN_VENDORINVT,IN_INVENTORYUNITMED
      WHERE  IN_INTRAITEM.SITEID=@SITEID AND
             BATNBR=@BATNBR AND IN_INTRAITEM.INVTIDSKU=IN_INVENTORY.INVTIDSKU AND
             IN_INTRAITEM.INVTIDSKU=IN_VENDORINVT.INVTIDSKU AND VENDIDSS=@VENDIDSS AND
	     IN_INVENTORY.STKUNITID=IN_INVENTORYUNITMED.STKUNITID 
END
go
IF OBJECT_ID('ConsultaParaReporteIngresoProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE ConsultaParaReporteIngresoProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE ConsultaParaReporteIngresoProc >>>'
go


/* 
 * PROCEDURE: in_actualiza_stock_fintomainventario_proc 
 */

create proc in_actualiza_stock_fintomainventario_proc
as
declare @almacen ud_idsite
declare @inicioinventario smalldatetime

SELECT @ALMACEN =SOURCESITE FROM SETUP

select @inicioinventario=(select startdateinven from site where siteid=@almacen) --OBTENER fecha de inicio de inventario

begin tran



IF (@@error!=0)
	begin
		rollback
		return(1)
	end
else
	commit

return(0)
go
IF OBJECT_ID('in_actualiza_stock_fintomainventario_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_actualiza_stock_fintomainventario_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_actualiza_stock_fintomainventario_proc >>>'
go


/* 
 * PROCEDURE: IN_ActualizaKitComponentProc 
 */

CREATE PROCEDURE IN_ActualizaKitComponentProc
(@KITID1	UD_KITID,
 @CORRNBR1	INT
)
AS
BEGIN
	DECLARE	@CORRNBR	INT									
	DECLARE	@KITID          UD_KITID
	DECLARE	@INVTIDSKU      UD_IDCORPORATIVO
	DECLARE	@SEQUENCE       tinyint
	DECLARE	@STDCOSTPEN     UD_COSTO
	DECLARE	@STDCOSTUSD     UD_COSTO
	DECLARE	@QTY            numeric(9, 3)
	
	
	SET @KITID=@KITID1
	SET @CORRNBR=@CORRNBR1

	BEGIN TRAN

	--ELIMINO DATOS DE LA TABLA DETALLE DE FORMULAS (IN_KITCOMPONENT) PARA LUEGO ACTUALIZARLA
	DELETE
	FROM		IN_KITCOMPONENT
	WHERE	KITID=@KITID

	IF (@@error!=0)
    	BEGIN
        	ROLLBACK TRAN
        	RETURN(1) 
	END
	-----------------------------------------------------------------------

	--PROCEDE A INSERTAR LOS DATOS EN LA TABLA DETALLE DE FORMULA	
	DECLARE CUR_KITCOMPONENT CURSOR 
	FOR	SELECT 	KITID, INVTIDSKU, SEQUENCE, STDCOSTPEN, STDCOSTUSD,QTY  
        		FROM   	TMP_IN_KITCOMPONENT
		WHERE 	KITID=@KITID AND CORRNBR=@CORRNBR 

    	OPEN CUR_KITCOMPONENT
    	FETCH NEXT	FROM 	CUR_KITCOMPONENT
		 	INTO  	@KITID, @INVTIDSKU, @SEQUENCE, @STDCOSTPEN, @STDCOSTUSD,@QTY
                      		

      	WHILE @@FETCH_STATUS=0
	 BEGIN
	
		INSERT INTO IN_KITCOMPONENT(KITID,INVTIDSKU,SEQUENCE,STDCOSTPEN,STDCOSTUSD,QTY)
    		VALUES(@KITID,@INVTIDSKU,@SEQUENCE,@STDCOSTPEN,@STDCOSTUSD,@QTY)

		IF (@@ERROR!=0)
          		BEGIN       
            			CLOSE CUR_KITCOMPONENT
            			DEALLOCATE CUR_KITCOMPONENT
	    		ROLLBACK
            			RETURN(1)  
          		END

	   	FETCH NEXT	FROM 	CUR_KITCOMPONENT
			 	INTO  	@KITID, @INVTIDSKU, @SEQUENCE, @STDCOSTPEN, @STDCOSTUSD,@QTY
	END

	--ELIMINO DATOS DE LA TABLA TEMPORAL DETALLE DE FORMULAS (TMP_IN_KITCOMPONENT) PARA LUEGO ACTUALIZARLA
	DELETE
	FROM		TMP_IN_KITCOMPONENT
	WHERE	KITID=@KITID

	IF (@@error!=0)
    	BEGIN
        	ROLLBACK TRAN
        	RETURN(1) 
	END


  CLOSE CUR_KITCOMPONENT
  DEALLOCATE CUR_KITCOMPONENT   
  COMMIT TRAN
  RETURN (0)         
END
go
IF OBJECT_ID('IN_ActualizaKitComponentProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_ActualizaKitComponentProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_ActualizaKitComponentProc >>>'
go


/* 
 * PROCEDURE: in_actualizatoma_art_proc 
 */


create proc in_actualizatoma_art_proc
@lista_del varchar(7950),
@lista_ins varchar(7950)
as

declare @cadena_ins varchar(8000)
declare @cadena_del varchar(8000)
declare @fechainicio as smalldatetime		--fecha inicion de inventario
declare @siteorig  ud_idsite			--site que inicia inventario
set @siteorig=(select sourcesite from setup)	--obtener el almacen original
set @fechainicio= (select startdateinven from site where siteid=@siteorig ) --obtener la fecha de inicio de inventario del site

begin tran
--------------------------------------------------------
--eliminar los productos a in_inventphisical

if len(@lista_del)>2
	begin
		set @cadena_del='delete in_inventphisical where INVTIDSKU in ('+ @lista_del +')'
		exec (@cadena_del)
	end

--------------------------------------------------------
--agregar los productos a in_inventphisical

if len(@lista_ins)>2
begin
	set @cadena_ins=' insert into in_inventphisical (siteid,invtidsku, classid,classubid,stockphisical) '+
		" ( select '"+@siteorig+"' ,invtidsku,classid,classubid,0 from in_inventory "+
		' where invtidsku in ('+@lista_ins+') ' +
		' and (invtidsku not in (select invtidsku from in_INVENTPHISICAL) ) )'

	exec (@cadena_ins)
end

if (@@error!=0)
	rollback
else
	commit
go
IF OBJECT_ID('in_actualizatoma_art_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_actualizatoma_art_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_actualizatoma_art_proc >>>'
go


/* 
 * PROCEDURE: in_actualizatoma_CAT_proc 
 */


create proc in_actualizatoma_CAT_proc
@lista varchar(8000)
as

declare @cadena_ins varchar(7950)
declare @cadena_del varchar(7950)
declare @fechainicio as smalldatetime		--fecha inicion de inventario
declare @siteorig  ud_idsite	--site que inicia inventario
declare @fi varchar(30)		--fecha inicio de inventario en texto
set @siteorig=(select sourcesite from setup)	--obtener el almacen original
set @fechainicio= (select startdateinven from site where siteid=@siteorig ) --obtener la fecha de inicio de inventario del site
set @fi=( select cast(@fechainicio as varchar(30) ) )

begin tran
--------------------------------------------------------
--agregar las subcategorias nuevas a in_cluster
set @cadena_ins='insert into in_cluster (classid,classubid,intrdatephisical) '+
" select classid,classubid,'"+@fi+"' from in_subcategory where classid in ("+@lista+") "+
' and (classid not in (select classid from in_cluster)      ) '

EXEC (@CADENA_INS)

--------------------------------------------------------
--eliminar las subcategorias que se han deseleccionados del in_cluster
set @cadena_del='delete in_cluster where classid in ( '+
" select classid from in_cluster  where classid not in ("+@lista+") )"

EXEC (@CADENA_DEL)
--------------------------------------------------------

--agregar los productos a in_inventphisical
insert into in_inventphisical (siteid,invtidsku, classid,classubid,stockphisical) 
		select @siteorig ,invtidsku,classid,classubid,0 from in_inventory 
		where classid in (select classid from in_cluster)
		and (classid not in (select classid from in_INVENTPHISICAL)      )
-------------------------------------------------------
--eliminar los productos deseleccionados de in_inventphisical
delete in_inventphisical 
      where classid in (select classid 
			from in_INVENTPHISICAL 
			where classid not in ( select classid 
			from in_cluster) )

 if (@@error!=0)
	rollback
else
	commit
go
IF OBJECT_ID('in_actualizatoma_CAT_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_actualizatoma_CAT_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_actualizatoma_CAT_proc >>>'
go


/* 
 * PROCEDURE: in_actualizatoma_proc 
 */

create proc in_actualizatoma_proc
@lista varchar(8000)
as

SET @LISTA="'0101','0201','0202'"

declare @cadena_ins varchar(7950)
declare @cadena_del varchar(7950)

declare @fechainicio as smalldatetime		--fecha inicion de inventario
declare @siteorig  ud_idsite	--site que inicia inventario
declare @fi varchar(30)		--fecha inicio de inventario en texto

set @siteorig=(select sourcesite from setup)	--obtener el almacen original
set @fechainicio= (select startdateinven from site where siteid=@siteorig ) --obtener la fecha de inicio de inventario del site

set @fi=( select cast(@fechainicio as varchar(28) ) )

--------------------------------------------------------
--agregar las subcategorias nuevas
set @cadena_ins='insert into in_cluster (classid,classubid,intrdatephisical) '+
		" select classid,classubid,'"+@fi+"' from in_subcategory where classubid in ("+@lista+") "+
		' and (classubid not in (select classubid from in_cluster)      )'
--------------------------------------------------------
--eliminar las subcategorias que se han deseleccionados

set @cadena_del='delete in_cluster where classubid in ( '+
		" select classubid from in_cluster  where classubid not in ("+@lista+") )"

EXEC (@CADENA_DEL)
EXEC (@CADENA_ins)
go
IF OBJECT_ID('in_actualizatoma_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_actualizatoma_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_actualizatoma_proc >>>'
go


/* 
 * PROCEDURE: in_actualizatoma_SCAT_proc 
 */


create proc in_actualizatoma_SCAT_proc
@lista varchar(8000)
as

declare @cadena_ins varchar(7950)
declare @cadena_del varchar(7950)
declare @fechainicio as smalldatetime		--fecha inicion de inventario
declare @siteorig  ud_idsite	--site que inicia inventario
declare @fi  varchar(30)	--site que inicia inventario

set @siteorig=(select sourcesite from setup)	--obtener el almacen original
set @fechainicio= (select startdateinven from site where siteid=@siteorig ) --obtener la fecha de inicio de inventario del site
set @fi=( select cast(@fechainicio as varchar(30) ) )

begin tran
--------------------------------------------------------
--agregar las subcategorias nuevas a in_cluster
set @cadena_ins='insert into in_cluster (classid,classubid,intrdatephisical) '+
" select classid,classubid,'"+@fi+"' from in_subcategory where classubid in ("+@lista+") "+
' and (classubid not in (select classubid from in_cluster)      )'

EXEC (@CADENA_INS)

--------------------------------------------------------
--eliminar las subcategorias que se han deseleccionados del in_cluster
set @cadena_del='delete in_cluster where classubid in ( '+
" select classubid from in_cluster  where classubid not in ("+@lista+") )"
EXEC (@CADENA_DEL)
--------------------------------------------------------

--agregar los productos a in_inventphisical
insert into in_inventphisical (siteid,invtidsku, classid,classubid,stockphisical) 
		select @siteorig ,invtidsku,classid,classubid,0 from in_inventory 
		where classubid in (select classubid from in_cluster)
		and (classubid not in (select classubid from in_INVENTPHISICAL)      )
-------------------------------------------------------
--eliminar los productos deseleccionados de in_inventphisical
delete in_inventphisical 
      where classubid in (select classubid 
			from in_INVENTPHISICAL 
			where classubid not in ( select classubid 
			from in_cluster) )

 if (@@error!=0)
	rollback
else
	commit
go
IF OBJECT_ID('in_actualizatoma_SCAT_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_actualizatoma_SCAT_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_actualizatoma_SCAT_proc >>>'
go


/* 
 * PROCEDURE: IN_AGREGAR_SCAT_PROC 
 */

CREATE PROC IN_AGREGAR_SCAT_PROC
@SCAT varCHAR(8000)
AS
--agregar las subcategorias nuevas
declare @fechainicio as smalldatetime		--fecha inicion de inventario
declare @siteorig  ud_idsite	--site que inicia inventario

set @siteorig=(select sourcesite from setup)	--obtener el almacen original
set @fechainicio= (select startdateinven from site where siteid=@siteorig ) --obtener la fecha de inicio de inventario del site

declare @shit varchar(8000)
set @shit='insert into in_cluster (classid,classubid,intrdatephisical) '+
		" select classid,classubid,'"+cast(@fechainicio as char(30) )+"'" +
		' from in_subcategory where classubid in ('+@SCAT+')'
		
exec (@shit)
--from in_subcategory where classubid =@SCAT
go
IF OBJECT_ID('IN_AGREGAR_SCAT_PROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_AGREGAR_SCAT_PROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_AGREGAR_SCAT_PROC >>>'
go


/* 
 * PROCEDURE: in_agregarinventariohistorico_proc 
 */

create proc in_agregarinventariohistorico_proc
as
declare @almacen ud_idsite
declare @inicioinventario smalldatetime

SELECT @ALMACEN =SOURCESITE FROM SETUP

select @inicioinventario=(select startdateinven from site where siteid=@almacen) --OBTENER fecha de inicio de inventario

begin tran

insert into in_inventhistophisi (SITEID,INVTIDSKU,CLASSID,CLASSUBID,STOCKPHISICAL,QTYAVAIL,INTRDATEPHISICAL)
select SITEID,INVTIDSKU,CLASSID,CLASSUBID,STOCKPHISICAL,QTYAVAIL,@inicioinventario from VW_AJUSTEAUTOMATICO where siteid=@almacen

truncate table in_inventphisical
truncate table in_cluster

IF (@@error!=0)
	begin
		rollback
		return(1)
	end
else
	commit

return(0)
go
IF OBJECT_ID('in_agregarinventariohistorico_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_agregarinventariohistorico_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_agregarinventariohistorico_proc >>>'
go


/* 
 * PROCEDURE: IN_AnularDocumentoProc 
 */

CREATE PROCEDURE IN_AnularDocumentoProc
(@SITEID UD_IDSITE,
@BATNBR char(10)
)
AS
BEGIN
    DECLARE   	@ERROR	integer
   -- DECLARE	@DATEPROCE	SMALLDATETIME  

    BEGIN TRAN
   
   -- OBTENIENDO LA FECHA DE PROCESO COMO FECHA DE ANULACION
   -- SET @DATEPROCE=(SELECT DATEPROCE FROM SETUP)

   --ACTUALIZO EL ESTADO DEL DOCUMENTO EN IN_INTRAHEAD
  
    UPDATE IN_INTRAHEAD
    SET DOCSTATUSID='AN'
    WHERE SITEID=@SITEID AND BATNBR=@BATNBR
          
    SET @ERROR=@@error
     
          IF (@ERROR!=0 OR @@ROWCOUNT=0 )
          BEGIN       
            ROLLBACK
            RETURN(1)  
          END         

    --ACTUALIZO EL ESTADO DEL DOCUMENTO EN IN_INTRAITEM
    UPDATE IN_INTRAITEM
    SET DOCSTATUSID='AN'
    WHERE SITEID=@SITEID AND BATNBR=@BATNBR
          
    SET @ERROR=@@error
     
          IF (@ERROR!=0 OR @@ROWCOUNT=0 )
          BEGIN       
            ROLLBACK
            RETURN(1)  
          END         

  COMMIT TRAN
  RETURN (0)

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


/* 
 * PROCEDURE: IN_APERTURAAlMACENESProc 
 */

CREATE PROCEDURE IN_APERTURAAlMACENESProc
@INVTIDSKU          UD_IDCORPORATIVO
AS
BEGIN
   DECLARE @SITEID UD_IDSITE
   DECLARE @ERROR  INTEGER

   DECLARE APERTURAALMACENES cursor 
   FOR Select siteid from site where status=1 and siteid Not in( Select siteid from in_inventorydat 
                                                                 where INVTIDSKU =@INVTIDSKU)
   OPEN APERTURAALMACENES
   FETCH NEXT FROM APERTURAALMACENES INTO @SITEID

   WHILE @@FETCH_STATUS = 0
    BEGIN
       INSERT INTO IN_INVENTORYDAT (INVTIDSKU,SITEID,STARTDATEINV) VALUES
                                    (@INVTIDSKU,@SITEID,GETDATE())

       SET @ERROR=@@error     
       IF (@ERROR!=0)
       BEGIN       
          CLOSE APERTURAALMACENES
          DEALLOCATE APERTURAALMACENES
          RETURN(1)  
       END
       FETCH NEXT FROM APERTURAALMACENES INTO @SITEID   
    END      
   CLOSE APERTURAALMACENES
   DEALLOCATE APERTURAALMACENES
   RETURN(0)      

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


/* 
 * PROCEDURE: IN_APERTURAKDXHISTProc 
 */

create procedure IN_APERTURAKDXHISTProc
@INVTIDSKU       UD_IDCORPORATIVO
AS
BEGIN
   DECLARE @SITEID UD_IDSITE
   DECLARE @ERROR  INTEGER
   
   DECLARE APERTURAKDXHIST cursor 
   FOR Select siteid from site where status=1 and siteid Not in( Select siteid from IN_KDXHIST 
                               where INVTIDSKU =@INVTIDSKU)
   
   OPEN APERTURAKDXHIST
   FETCH NEXT FROM APERTURAKDXHIST INTO @SITEID

   WHILE @@FETCH_STATUS = 0
    BEGIN
       INSERT INTO IN_KDXHIST (YEARHIST,SITEID,INVTIDSKU) VALUES
                              (YEAR(GETDATE()),@SITEID,@INVTIDSKU)

       SET @ERROR=@@error     
       IF (@ERROR!=0)
       BEGIN       
          CLOSE APERTURAKDXHIST
          DEALLOCATE APERTURAKDXHIST
          RETURN(1)  
       END
       FETCH NEXT FROM APERTURAKDXHIST INTO @SITEID   
    END      
   CLOSE APERTURAKDXHIST
   DEALLOCATE APERTURAKDXHIST
   RETURN(0)     
end
/*********************************************************************************************************************************/
go
IF OBJECT_ID('IN_APERTURAKDXHISTProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_APERTURAKDXHISTProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_APERTURAKDXHISTProc >>>'
go


/* 
 * PROCEDURE: IN_ArticulosPorProveedorAlmacenProc 
 */

CREATE Procedure IN_ArticulosPorProveedorAlmacenProc
(@VENDIDSS     	UD_IDVENDOR,
 @SITEID       	UD_IDSITE,
 @DISASSEMBLE	BIT
)
AS
BEGIN   

	SELECT  in_inventory.INVTIDSKU,in_inventory.descr,QTYAVAIL,BUYCOD AS [Cd.Compra], STDCOST AS Costo  
    	FROM 	(((in_vendorinvt INNER JOIN in_vendor ON in_vendorinvt.VENDIDSS=in_vendor.VENDIDSS) INNER JOIN 
           	currency ON in_vendorinvt.curyid=currency.curyid) INNER JOIN in_inventory ON in_vendorinvt.INVTIDSKU=
           	in_inventory.INVTIDSKU) INNER JOIN in_INVENTORYDAT ON in_INVENTORYDAT.INVTIDSKU=in_inventory.INVTIDSKU
	WHERE in_vendorinvt.VENDIDSS =@VENDIDSS AND IN_INVENTORYDAT.SITEID=@SITEID AND IN_VENDORINVT.SITEID=@SITEID
		AND in_inventory.DISASSEMBLE=@DISASSEMBLE 

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


/* 
 * PROCEDURE: in_borrar_cluster 
 */


create proc in_borrar_cluster
as
truncate table in_cluster
go
IF OBJECT_ID('in_borrar_cluster') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_borrar_cluster >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_borrar_cluster >>>'
go


/* 
 * PROCEDURE: IN_BucaArticuloEntmpDetalleFormulaProc 
 */

CREATE PROCEDURE IN_BucaArticuloEntmpDetalleFormulaProc
(   @INVTIDSKU UD_IDCORPORATIVO,
    @CORRNBR INT)

AS
BEGIN
      SELECT 	INVTIDSKU
      FROM 	TMP_IN_KITCOMPONENT
      WHERE 	INVTIDSKU=@INVTIDSKU AND
	   	CORRNBR=@CORRNBR

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


/* 
 * PROCEDURE: IN_BucaArticuloProc 
 */

CREATE PROCEDURE IN_BucaArticuloProc
(   @INVTIDSKU UD_IDCORPORATIVO,
    @CORRNBR INT)

AS
BEGIN
      SELECT INVTIDSKU
      FROM TMP_IN_INTRAITEM
      WHERE INVTIDSKU=@INVTIDSKU AND
	    CORRNBR=@CORRNBR

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


/* 
 * PROCEDURE: IN_BuscaArticuloProc 
 */

CREATE PROCEDURE IN_BuscaArticuloProc
(   @INVTIDSKU UD_IDCORPORATIVO,
    @CORRNBR INT)

AS
BEGIN
      SELECT 	INVTIDSKU
      FROM 	TMP_IN_INTRAITEM
      WHERE 	INVTIDSKU=@INVTIDSKU AND
	   	 CORRNBR=@CORRNBR

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


/* 
 * PROCEDURE: IN_BuscaNroDocumentoProc 
 */

CREATE PROCEDURE IN_BuscaNroDocumentoProc
(   @VENDIDSS 	UD_IDVENDOR,
    @REFNBR 	CHAR(15)
)
AS
BEGIN
      SELECT 	REFNBR
      FROM 	IN_INTRAHEAD
      WHERE 	VENDIDSS=@VENDIDSS AND
	   	REFNBR=@REFNBR

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


/* 
 * PROCEDURE: IN_BuscarMovimientoProc 
 */

CREATE PROCEDURE IN_BuscarMovimientoProc
(@SITEID	UD_IDSITE,
 @FECHAINICIAL	SMALLDATETIME,
 @FECHAFINAL 	SMALLDATETIME,
 @INOUT		BIT,
 @ESTADODOC	INT
)
AS
	BEGIN
		IF (@SITEID<>'')
		  BEGIN
			IF (@ESTADODOC=0)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE SITEID=@SITEID AND INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT
				ORDER BY SITEID,BATNBR
			END
	
			IF (@ESTADODOC=1)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE SITEID=@SITEID AND INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND RLSED=1
				ORDER BY SITEID,BATNBR
			END
	
			IF (@ESTADODOC=2)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE SITEID=@SITEID AND INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND RLSED=0 AND DOCSTATUSID<>'AN'
				ORDER BY SITEID,BATNBR
			END
			
			IF (@ESTADODOC=3)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE SITEID=@SITEID AND INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND DOCSTATUSID='AN'
				ORDER BY SITEID,BATNBR
			END
		  END
		ELSE 
		  BEGIN
			IF (@ESTADODOC=0)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT
				ORDER BY SITEID,BATNBR
			END
	
			IF (@ESTADODOC=1)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND RLSED=1
				ORDER BY SITEID,BATNBR
			END
	
			IF (@ESTADODOC=2)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND RLSED=0 AND DOCSTATUSID<>'AN'
				ORDER BY SITEID,BATNBR
			END

			IF (@ESTADODOC=3)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT  AND DOCSTATUSID='AN'
				ORDER BY SITEID,BATNBR
			END

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


/* 
 * PROCEDURE: IN_CalculaTotales_2Proc 
 */

CREATE PROCEDURE IN_CalculaTotales_2Proc
( @CURYID 	UD_CURYID,
  @SITEID	UD_IDSITE,
  @BATNBR	char(10)
)
AS 
BEGIN

      DECLARE @MONEDABASE 	char(3)
      SET @MONEDABASE =(SELECT CURYORIG FROM SETUP) 

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint
	DECLARE @ND_RESULTADOSETUP	Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)
	SET @ND_RESULTADOSETUP=(Select Floatoutput from setup)
/*******************************************************************************************************/
   IF @CURYID=@MONEDABASE
      SELECT ROUND(SUM(STDCOSTPEN*QTY),@ND_RESULTADOSETUP) AS VALVENTA,
	     (ROUND(SUM(STDCOSTPEN*QTY*TAXIGV/100),@ND_RESULTADOSETUP)) AS IGV 
      FROM IN_INTRAITEM
      WHERE SITEID=@SITEID AND BATNBR=@BATNBR

   ELSE
      SELECT ROUND(SUM(STDCOSTUSD*QTY),@ND_RESULTADOSETUP) AS VALVENTA, 
	     (ROUND(SUM(STDCOSTUSD*QTY*TAXIGV/100),@ND_RESULTADOSETUP)) AS IGV
      FROM IN_INTRAITEM
      WHERE SITEID=@SITEID AND BATNBR=@BATNBR


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


/* 
 * PROCEDURE: IN_CalculaTotalesProc 
 */

CREATE PROCEDURE IN_CalculaTotalesProc
( @CURYID 	UD_CURYID,
  @CORRNBR	INT)
AS 
BEGIN

      DECLARE @MONEDABASE 	char(3)
      SET @MONEDABASE =(SELECT CURYORIG FROM SETUP) 

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint
	DECLARE @ND_RESULTADOSETUP	Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)
	SET @ND_RESULTADOSETUP=(Select Floatoutput from setup)
/*******************************************************************************************************/

   IF @CURYID=@MONEDABASE
      SELECT ROUND(SUM(STDCOSTPEN*QTY),@ND_RESULTADOSETUP) AS VALVENTA,
	     (ROUND(SUM(STDCOSTPEN*QTY*TAXIGV/100),@ND_RESULTADOSETUP)) AS IGV
      FROM TMP_IN_INTRAITEM
      WHERE CORRNBR=@CORRNBR
   ELSE
      SELECT ROUND(SUM(STDCOSTUSD*QTY),@ND_RESULTADOSETUP) AS VALVENTA, 
	     (ROUND(SUM(STDCOSTUSD*QTY*TAXIGV/100),@ND_RESULTADOSETUP)) AS IGV
      FROM TMP_IN_INTRAITEM
      WHERE CORRNBR=@CORRNBR
END
go
IF OBJECT_ID('IN_CalculaTotalesProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_CalculaTotalesProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_CalculaTotalesProc >>>'
go


/* 
 * PROCEDURE: IN_CargaInventoryAlterDataTempProc 
 */

Create Procedure IN_CargaInventoryAlterDataTempProc
(@CORRNBR            int,     
 @INVTIDSKU          UD_IDCORPORATIVO)  
as
 BEGIN TRAN

 INSERT INTO TMP_IN_INVENTORYALTER SELECT @CORRNBR,IN_INVENTORYALTER.* FROM IN_INVENTORYALTER WHERE 
             INVTIDSKU=@INVTIDSKU 
 IF (@@error!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)    
    END

 COMMIT TRAN
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_CargaInventoryAlterDataTempProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_CargaInventoryAlterDataTempProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_CargaInventoryAlterDataTempProc >>>'
go


/* 
 * PROCEDURE: IN_CargaInventoryDataTempProc 
 */

Create Procedure IN_CargaInventoryDataTempProc 
(@CORRNBR            int,     
 @INVTIDSKU          UD_IDCORPORATIVO)  
as
 BEGIN TRAN

 INSERT INTO TMP_IN_INVENTORYDAT SELECT @CORRNBR,IN_INVENTORYDAT.* FROM IN_INVENTORYDAT WHERE 
             INVTIDSKU=@INVTIDSKU 
 IF (@@error!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)    
    END

 COMMIT TRAN
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_CargaInventoryDataTempProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_CargaInventoryDataTempProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_CargaInventoryDataTempProc >>>'
go


/* 
 * PROCEDURE: IN_CargaInventoryVendorInvTempProc 
 */

Create Procedure IN_CargaInventoryVendorInvTempProc 
(@CORRNBR            int,     
 @INVTIDSKU          UD_IDCORPORATIVO)  
as
 BEGIN TRAN

 INSERT INTO TMP_IN_VENDORINVT SELECT @CORRNBR,IN_VENDORINVT.* FROM IN_VENDORINVT WHERE 
             INVTIDSKU=@INVTIDSKU 
 IF (@@error!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)    
    END

 COMMIT TRAN
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_CargaInventoryVendorInvTempProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_CargaInventoryVendorInvTempProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_CargaInventoryVendorInvTempProc >>>'
go


/* 
 * PROCEDURE: IN_CargaTempDataIN_VENDORINVTProc 
 */

Create Procedure IN_CargaTempDataIN_VENDORINVTProc  
(@CORRNBR int)
as
DECLARE   @VENDIDSS           UD_IDVENDOR
DECLARE   @INVTIDSKU          UD_IDCORPORATIVO
DECLARE   @SITEID             UD_IDSITE
DECLARE   @BUYCOD             varchar(20)
DECLARE   @DESCR              UD_DESCRIPCION 
DECLARE   @CURYID             UD_CURYID
DECLARE   @STDCOST            UD_COSTO
DECLARE   @BUYDATE            smalldatetime
DECLARE   @USER1              UD_USER1
DECLARE   @USER2              UD_USER2
DECLARE   @USER3              UD_USER6
DECLARE   @USER4              UD_USER7
DECLARE   @ERROR              INTEGER
    
DECLARE InventoryVendor cursor 
   FOR SELECT VENDIDSS, INVTIDSKU,SITEID, BUYCOD, DESCR, CURYID, 
              STDCOST, BUYDATE, USER1, USER2, USER3, USER4
        FROM TMP_IN_VENDORINVT where CORRNBR=@CORRNBR

   OPEN InventoryVendor       
       FETCH NEXT FROM InventoryVendor INTO  @VENDIDSS,@INVTIDSKU,@SITEID,@BUYCOD,@DESCR,
                       @CURYID,@STDCOST,@BUYDATE,@USER1,@USER2,@USER3,@USER4
                                                   
 WHILE @@FETCH_STATUS = 0
    BEGIN    
     INSERT INTO IN_VENDORINVT (VENDIDSS, INVTIDSKU,SITEID,BUYCOD, DESCR, CURYID,
                                STDCOST, BUYDATE, USER1, USER2, USER3, USER4) values 
			       (@VENDIDSS,@INVTIDSKU,@SITEID,@BUYCOD,@DESCR,
                                @CURYID,@STDCOST,@BUYDATE,@USER1,@USER2,@USER3,@USER4)
	
     SET @ERROR=@@error
     
       IF (@ERROR!=0)
       BEGIN       
          CLOSE InventoryVendor 
          DEALLOCATE InventoryVendor
          RETURN(1)  
       END 

       FETCH NEXT FROM InventoryVendor INTO  @VENDIDSS,@INVTIDSKU,@SITEID,@BUYCOD,@DESCR,
                       @CURYID,@STDCOST,@BUYDATE,@USER1,@USER2,@USER3,@USER4
       
    END

  CLOSE InventoryVendor 
  DEALLOCATE InventoryVendor
  RETURN (0)
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_CargaTempDataIN_VENDORINVTProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_CargaTempDataIN_VENDORINVTProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_CargaTempDataIN_VENDORINVTProc >>>'
go


/* 
 * PROCEDURE: IN_CargaTempDataInventoryAlterProc 
 */

Create Procedure IN_CargaTempDataInventoryAlterProc  
(@CORRNBR int)
as
DECLARE   @INVTIDSKU          UD_IDCORPORATIVO
DECLARE   @INVTIDBARR         UD_CODIGOBARRA
DECLARE   @USER1              UD_USER1
DECLARE   @USER2              UD_USER2
DECLARE   @USER3              UD_USER6
DECLARE   @USER4              UD_USER7
DECLARE   @ERROR              INTEGER
    
DECLARE InventoryAlter cursor 
    FOR SELECT INVTIDBARR,INVTIDSKU,USER1, USER2, USER3, USER4 
         FROM TMP_IN_INVENTORYALTER where CORRNBR=@CORRNBR

 OPEN InventoryAlter         
       FETCH NEXT FROM InventoryAlter INTO  @INVTIDBARR,@INVTIDSKU,@USER1,@USER2,
                      @USER3,@USER4
                                                   
 WHILE @@FETCH_STATUS = 0
    BEGIN    
     INSERT INTO IN_INVENTORYALTER (INVTIDBARR,INVTIDSKU,USER1, USER2, USER3, USER4) values 
                                   (@INVTIDBARR,@INVTIDSKU,@USER1,@USER2,
                                   @USER3,@USER4)
     SET @ERROR=@@error
     
       IF (@ERROR!=0)
       BEGIN       
          CLOSE InventoryAlter 
          DEALLOCATE InventoryAlter     
          RETURN(1)  
       END 

       FETCH NEXT FROM InventoryAlter INTO  @INVTIDBARR,@INVTIDSKU,@USER1,@USER2,
                       @USER3,@USER4
       
    END

  CLOSE InventoryAlter
  DEALLOCATE InventoryAlter    
  RETURN (0)
go
IF OBJECT_ID('IN_CargaTempDataInventoryAlterProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_CargaTempDataInventoryAlterProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_CargaTempDataInventoryAlterProc >>>'
go


/* 
 * PROCEDURE: IN_CargaTempDataInventoryProc 
 */

Create Procedure IN_CargaTempDataInventoryProc  
(@CORRNBR int)
as
DECLARE   @INVTIDSKU          UD_IDCORPORATIVO
DECLARE   @SITEID             UD_IDSITE
DECLARE   @PRECLVID           UD_PRECLVID
DECLARE   @STOCKMIN           UD_STOCK
DECLARE   @STOCKMAX           UD_STOCK
DECLARE   @STOCKSTAR          UD_STOCK
DECLARE   @QTYAVAIL           UD_STOCK
DECLARE   @STARTDATEINV       smalldatetime
DECLARE   @ENDDATEINV         smalldatetime
DECLARE   @LASTINVFIS         UD_STOCK
DECLARE   @AVGCOSTPEN         UD_COSTO
DECLARE   @AVGCOSTUSD         UD_COSTO
DECLARE   @STDCOSTPEN         UD_COSTO
DECLARE   @STDCOSTUSD         UD_COSTO
DECLARE   @LASTSTDCOSTPEN     UD_COSTO
DECLARE   @LASTSTDCOSTUSD     UD_COSTO
DECLARE   @SLS_PRICE          UD_COSTO
DECLARE   @INVTIDSTK          CHAR(20)
DECLARE   @INVTIDNOSTK        CHAR(20)
DECLARE   @USER1              UD_USER1
DECLARE   @USER2              UD_USER2
DECLARE   @USER3              UD_USER3
DECLARE   @USER4              UD_USER5
DECLARE   @USER5              UD_USER5
DECLARE   @USER6              UD_USER6
DECLARE   @USER7              UD_USER7
DECLARE   @USER8              UD_USER8
DECLARE   @USER9              UD_USER9 
DECLARE   @ERROR              INTEGER
    
DECLARE InventoryData cursor 
    FOR SELECT INVTIDSKU, SITEID,PRECLVID, STOCKMIN, STOCKMAX, 
             STOCKSTAR, QTYAVAIL, STARTDATEINV, ENDDATEINV, 
             LASTINVFIS, AVGCOSTPEN, AVGCOSTUSD, STDCOSTPEN, 
             STDCOSTUSD, LASTSTDCOSTPEN, LASTSTDCOSTUSD, 
             SLS_PRICE,INVTIDSTK,INVTIDNOSTK,USER1, USER2, USER3, USER4, USER5, USER6, 
             USER7, USER8, USER9
         FROM TMP_IN_INVENTORYDAT where CORRNBR=@CORRNBR

 OPEN InventoryData         
       FETCH NEXT FROM InventoryData INTO  @INVTIDSKU,@SITEID,@PRECLVID,@STOCKMIN,
                      @STOCKMAX,@STOCKSTAR,@QTYAVAIL,@STARTDATEINV,@ENDDATEINV,
                      @LASTINVFIS,@AVGCOSTPEN,@AVGCOSTUSD,@STDCOSTPEN,@STDCOSTUSD,
		      @LASTSTDCOSTPEN,@LASTSTDCOSTUSD,@SLS_PRICE,@INVTIDSTK,@INVTIDNOSTK,
                      @USER1,@USER2,@USER3,@USER4,@USER5,@USER6,@USER7,@USER8,@USER9
                                                   
 WHILE @@FETCH_STATUS = 0
    BEGIN    
     INSERT INTO IN_INVENTORYDAT (INVTIDSKU, SITEID, STOCKMIN, STOCKMAX, 
				   STOCKSTAR, QTYAVAIL, STARTDATEINV, ENDDATEINV, 
				   LASTINVFIS, AVGCOSTPEN, AVGCOSTUSD, STDCOSTPEN, 
				   STDCOSTUSD, LASTSTDCOSTPEN, LASTSTDCOSTUSD, 
			           SLS_PRICE,PRECLVID,INVTIDSTK,INVTIDNOSTK,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,@PRECLVID,
                                   @INVTIDSTK,@INVTIDNOSTK,@USER1,@USER2,@USER3,@USER4,@USER5,@USER6,@USER7,@USER8,@USER9)
     SET @ERROR=@@error
     
       IF (@ERROR!=0)
       BEGIN       
          CLOSE InventoryData
          DEALLOCATE InventoryData    
          RETURN(1)  
       END 

      FETCH NEXT FROM InventoryData INTO  @INVTIDSKU,@SITEID,@PRECLVID,@STOCKMIN,
                      @STOCKMAX,@STOCKSTAR,@QTYAVAIL,@STARTDATEINV,@ENDDATEINV,
                      @LASTINVFIS,@AVGCOSTPEN,@AVGCOSTUSD,@STDCOSTPEN,@STDCOSTUSD,
		      @LASTSTDCOSTPEN,@LASTSTDCOSTUSD,@SLS_PRICE,@INVTIDSTK,@INVTIDNOSTK,
		      @USER1,@USER2,@USER3,@USER4,@USER5,@USER6,@USER7,@USER8,@USER9
       
    END

  CLOSE InventoryData
  DEALLOCATE InventoryData     
  RETURN (0)

/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_CargaTempDataInventoryProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_CargaTempDataInventoryProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_CargaTempDataInventoryProc >>>'
go


/* 
 * PROCEDURE: IN_CopiaKitComponentProc 
 */

CREATE PROCEDURE IN_CopiaKitComponentProc
(@KITID1	UD_KITID,
 @CORRNBR1	INT
)
AS
BEGIN
	DECLARE	@CORRNBR	INT									
	DECLARE	@KITID          UD_KITID
	DECLARE	@INVTIDSKU      UD_IDCORPORATIVO
	DECLARE	@SEQUENCE       tinyint
	DECLARE	@STDCOSTPEN     UD_COSTO
	DECLARE	@STDCOSTUSD     UD_COSTO
	DECLARE	@QTY            numeric(9, 3)
	
	
	SET @KITID=@KITID1
	SET @CORRNBR=@CORRNBR1

	BEGIN TRAN
	
	DECLARE CUR_KITCOMPONENT CURSOR 
	FOR	SELECT 	KITID, INVTIDSKU, SEQUENCE, STDCOSTPEN, STDCOSTUSD,QTY  
        	FROM   	IN_KITCOMPONENT
		WHERE 	KITID=@KITID 

    	OPEN CUR_KITCOMPONENT
    	FETCH NEXT	FROM 	CUR_KITCOMPONENT
		 	INTO  	@KITID, @INVTIDSKU, @SEQUENCE, @STDCOSTPEN, @STDCOSTUSD,@QTY
                      		

      	WHILE @@FETCH_STATUS=0
        BEGIN
	
		INSERT INTO TMP_IN_KITCOMPONENT(CORRNBR,KITID,INVTIDSKU,SEQUENCE,STDCOSTPEN,STDCOSTUSD,QTY)
    		VALUES(@CORRNBR,@KITID,@INVTIDSKU,@SEQUENCE,@STDCOSTPEN,@STDCOSTUSD,@QTY)

		IF (@@ERROR!=0)
          	BEGIN       
            		CLOSE CUR_KITCOMPONENT
            		DEALLOCATE CUR_KITCOMPONENT
	    		ROLLBACK
            		RETURN(1)  
          	END

	   	FETCH NEXT	FROM 	CUR_KITCOMPONENT
			 	INTO  	@KITID, @INVTIDSKU, @SEQUENCE, @STDCOSTPEN, @STDCOSTUSD,@QTY
	END

  CLOSE CUR_KITCOMPONENT
  DEALLOCATE CUR_KITCOMPONENT   
  COMMIT TRAN
  RETURN (0)         
END
go
IF OBJECT_ID('IN_CopiaKitComponentProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_CopiaKitComponentProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_CopiaKitComponentProc >>>'
go


/* 
 * PROCEDURE: IN_eliminar_art_cluster_PROC 
 */


create proc IN_eliminar_art_cluster_PROC 
@id ud_idcorporativo
as
begin tran
	delete in_inventphisical where INVTIDSKU=@id
	IF (@@error!=0)
	BEGIN
        	RAISERROR  20002 ' Cannot delete '
	        ROLLBACK TRAN
	        RETURN(1)
    
    	END
COMMIT TRAN
RETURN(0)
go
IF OBJECT_ID('IN_eliminar_art_cluster_PROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_eliminar_art_cluster_PROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_eliminar_art_cluster_PROC >>>'
go


/* 
 * PROCEDURE: IN_EmisionDeDocumentoProc 
 */

CREATE PROCEDURE IN_EmisionDeDocumentoProc
(@SITEID1 UD_IDSITE,
@BATNBR1 char(10)
)
AS
BEGIN
	DECLARE	@ERROR		INTEGER 	-- PARA EL MANEJO DE ERRORES
	DECLARE @SWTINOUT 	BIT		-- FLAG PARA INGRESO(1) O SALIDA(0)
	DECLARE @DESENSAMBLE	BIT		-- 1 SI ES MOVIMIENTO POR DESENSAMBLE
	DECLARE @ID_ING_DES	UD_TYPEMOVEID	-- CODIGO DEL INGRESO POR DESENSAMBLE
	DECLARE @ID_SAL_DES	UD_TYPEMOVEID	-- CODIGO DE LA SALIDA POR DESENSAMBLE
	DECLARE	@COD_PROVEEDOR	UD_IDVENDOR	-- CODIGO DEL PROVEEDOR

 	BEGIN TRAN   
	
	SELECT @ERROR=0

	-- SE VERIFICA SI ES UN INGRESO O UNA SALIDA --
	SELECT 	@SWTINOUT=IN_TYPEDOC.INOUT,@DESENSAMBLE=IN_INTRAHEAD.DISASSEMBLE,@COD_PROVEEDOR=IN_INTRAHEAD.VENDIDSS
    	FROM 	IN_TYPEDOC,IN_INTRAHEAD
    	WHERE 	IN_TYPEDOC.TYPEDOCID=IN_INTRAHEAD.TYPEDOCID AND  SITEID=@SITEID1 AND BATNBR=@BATNBR1


	SELECT 	@ID_ING_DES=INTYPEDESIN,@ID_SAL_DES=INTYPEDESOUT
	FROM 	IN_SETUP

/****** LLAMADO AL PROCEDIMIENTO DE EMISION ******/
 
	SET EXEC @ERROR=IN_EmitirProc @SITEID1,@BATNBR1
    
	IF (@ERROR!=0)
    	BEGIN
		ROLLBACK
       		RETURN(1)               
    	END 

/****** LLAMADO AL PROCEDIMIENTO QUE GENERA LOS DOCUMENTOS POR DESENSAMBLE  ******/
 	IF(@DESENSAMBLE=1)
	BEGIN
		SET EXEC @ERROR=IN_GenerarDocumentosPorDesensambleProc @SITEID1,@BATNBR1,@ID_ING_DES,@ID_SAL_DES,@SWTINOUT,@COD_PROVEEDOR
	
		IF (@ERROR!=0)
    		BEGIN
			ROLLBACK
       			RETURN(1)               
    		END 
	END
	
	COMMIT TRAN	
  	RETURN (0)

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


/* 
 * PROCEDURE: IN_EmitirProc 
 */

CREATE PROCEDURE IN_EmitirProc
(@SITEID1 UD_IDSITE,
@BATNBR1 char(10)
)
AS
BEGIN
    
    DECLARE   @SITEID            UD_IDSITE
    DECLARE   @BATNBR            char(10)
    DECLARE   @TYPEDOCID         UD_TYPEMOVEID
    DECLARE   @INVTIDSKU         UD_IDCORPORATIVO
    DECLARE   @QTY               UD_STOCK
    DECLARE   @STDCOSTPEN        UD_COSTO
    DECLARE   @STDCOSTUSD        UD_COSTO
    DECLARE   @INTRDATE          smalldatetime
    DECLARE   @INTRDATEPROCE     smalldatetime
    DECLARE   @CURYRATE		 numeric(6,4)
    DECLARE   @PERPOST           char(6) 
    DECLARE   @SWTINOUT		 bit    
    DECLARE   @COMPONENT         bit
    DECLARE   @SLSPRICE          numeric(9, 2)  
    DECLARE   @REFNBR		 char(15)
    DECLARE   @ERROR             integer
    DECLARE   @DOCTYPEID	 UD_DOCTYPEID

/***** PARA ACTUALIZAR VENDORINVT***********/
    DECLARE   @VENDIDSS          UD_IDVENDOR
    DECLARE   @CURYID 		 char(3)
    DECLARE   @MONEDABASE	 char(3)    -- almacena la moneda base
/*******************************************/
    DECLARE   @STKFISI           bit
    DECLARE   @DATEPROCE	 smalldatetime
    DECLARE   @STKSTOCK		 BIT
    DECLARE   @VALOR		 UD_STOCK
    DECLARE   @DESCRIPCION	 VARCHAR(40) -- DESCRIPCION DEL ARTICULO
    DECLARE   @MENSAJE		 VARCHAR(80) -- MENSAJE EN CASO DE STOCK INSUFICIENTE
/*******************************************************************************************************/
    	-- VERIFICAR LA TOMA DE INVENTARIO
   	SET @STKFISI=(SELECT STKFISI FROM IN_SETUP) -- PARA CONTROLAR LA TOMA DE INVENTARIO
	SET @STKSTOCK=(SELECT STKSTOCK FROM IN_SETUP) -- PARA LA VALIDACION DE STOCK NEGATIVO
/*******************************************************************************************************/
	-- CARGO LA FECHA DE PROCESO COMO FECHA DE EMISION
	SET @DATEPROCE=(SELECT DATEPROCE FROM SETUP)
	SET @MONEDABASE =(SELECT CURYORIG FROM SETUP)
/*******************************************************************************************************/
    SET @BATNBR=@BATNBR1   
    SET @SITEID=@SITEID1
    SET @SLSPRICE=0
    SET @COMPONENT=0	
   
    SELECT @CURYRATE=CURYRATE,@PERPOST=PERPOST,@REFNBR=REFNBR,@DOCTYPEID=DOCTYPEID,@VENDIDSS=VENDIDSS,@CURYID=FROMCURYID
    FROM IN_INTRAHEAD 
    WHERE SITEID=@SITEID AND BATNBR=@BATNBR 

    SELECT @SWTINOUT=IN_TYPEDOC.INOUT
    FROM IN_TYPEDOC,IN_INTRAHEAD
    WHERE IN_TYPEDOC.TYPEDOCID=IN_INTRAHEAD.TYPEDOCID AND  SITEID=@SITEID AND BATNBR=@BATNBR

  
    --ACTUALIZO EL ESTADO DEL DOCUMENTO EN IN_INTRAHEAD 
    UPDATE IN_INTRAHEAD
    SET DOCSTATUSID='OK',
	RLSED=1,INTRDATEPROCE=@DATEPROCE
    WHERE SITEID=@SITEID AND BATNBR=@BATNBR
          
    SET @ERROR=@@error
     
          IF (@ERROR!=0 OR @@ROWCOUNT=0 )
          BEGIN       
            RETURN(1)  
          END         

    --ACTUALIZO EL ESTADO DEL DOCUMENTO EN IN_INTRAITEM 
    UPDATE IN_INTRAITEM
    SET RLSED=1,INTRDATEPROCE=@DATEPROCE, DOCSTATUSID='OK'
    WHERE SITEID=@SITEID AND BATNBR=@BATNBR
          
    SET @ERROR=@@error
     
          IF (@ERROR!=0 OR @@ROWCOUNT=0 )
          BEGIN       
            RETURN(1)  
          END         
     /*----------------------------------------------------------------------------------------- */

    -- SE DECLARA UN CURSOR PARA IN_INTRAITEM Y REALIZAR LA ACUALIZACION DE INTRAMOV
    DECLARE INTRAITEM cursor 


    FOR SELECT TYPEDOCID, INVTIDSKU, 
	       QTY, STDCOSTPEN, STDCOSTUSD, 
  	       INTRDATE, INTRDATEPROCE
        FROM   IN_INTRAITEM WHERE SITEID=@SITEID AND BATNBR=@BATNBR 

    OPEN INTRAITEM
    FETCH NEXT FROM INTRAITEM INTO  @TYPEDOCID, @INVTIDSKU,
                      		    @QTY, @STDCOSTPEN, @STDCOSTUSD,
                      		    @INTRDATE, @INTRDATEPROCE

      WHILE @@FETCH_STATUS=0
        BEGIN
			-- SE VERIFICA SI ES UNA SALIDA Y SI NO SE PERMITE STOCK NEGATIVO
			IF (@SWTINOUT=0 AND @STKSTOCK=0)
				BEGIN
					SET @VALOR =(SELECT QTYAVAIL-@QTY FROM IN_INVENTORYDAT WHERE SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU)
					IF (@VALOR<0)
						BEGIN
							SET @DESCRIPCION=(SELECT DESCR FROM IN_INVENTORY WHERE INVTIDSKU=@INVTIDSKU)
							SET @MENSAJE='Stock insuficiente en:' + ' ' + LTRIM(RTRIM(@INVTIDSKU)) + ' ' +  LTRIM(RTRIM(@DESCRIPCION))
							RAISERROR 20000  @MENSAJE
							RETURN(1)	
						END
				END
			
	  	        --ACTUALIZANDO LA TABLA IN_INVENTORYDAT CON LAS NUEVAS CANTIDADES INGRESADAS EN EL DOCUMENTO
			IF (@STDCOSTPEN<>0)  -- SI EL COSTO ES CERO INDICA QUE NO ES VALORIZADO
	  			BEGIN
					UPDATE  IN_INVENTORYDAT
	  				SET QTYAVAIL= CASE @SWTINOUT 
						WHEN 1 THEN QTYAVAIL + @QTY
						WHEN 0 THEN QTYAVAIL - @QTY END,
						STDCOSTPEN=@STDCOSTPEN, STDCOSTUSD=@STDCOSTUSD,LASTSTDCOSTPEN=@STDCOSTPEN, LASTSTDCOSTUSD=@STDCOSTUSD
	  				WHERE SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU
			
				END
			ELSE
	  			BEGIN
					UPDATE  IN_INVENTORYDAT
	  				SET QTYAVAIL=CASE @SWTINOUT 
						WHEN 1 THEN QTYAVAIL + @QTY
						WHEN 0 THEN QTYAVAIL - @QTY END
			  		WHERE SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU   
				END
         				

			-- MANEJO DEL ERROR
              		SET @ERROR=@@error
     
              		IF (@ERROR!=0 OR @@ROWCOUNT=0)
               		BEGIN       
              			CLOSE INTRAITEM
               			DEALLOCATE INTRAITEM
               			RETURN(1)  
               		END


          		---- ACTUALIZO LA TABLA IN_VENDORINVT
        		IF (@STDCOSTPEN<>0 AND @VENDIDSS<>'00000000000')
        			BEGIN
					IF (@CURYID=@MONEDABASE)
	 					BEGIN
                        				UPDATE IN_VENDORINVT
	           					SET CURYID=@CURYID,STDCOST=@STDCOSTPEN
        	        				WHERE VENDIDSS=@VENDIDSS AND INVTIDSKU= @INVTIDSKU AND SITEID=@SITEID
	 					END
					ELSE
	 					BEGIN
                        				UPDATE IN_VENDORINVT
	           					SET CURYID=@CURYID,STDCOST=@STDCOSTUSD
        	        				WHERE VENDIDSS=@VENDIDSS AND INVTIDSKU= @INVTIDSKU AND SITEID=@SITEID
	 					END
	 
					IF (@@ERROR!=0 OR @@ROWCOUNT=0)
               					BEGIN       
                 					CLOSE INTRAITEM
                 					DEALLOCATE INTRAITEM
                 					RETURN(1)  
              					END         

       				END         
--------------------------------------------------------------------

  	  --INSERTANDO EN LA TABLA INTRAMOV
          INSERT INTO INTRAMOV (SITEID,INVTIDSKU,BATNBR,INTRDATE,INTRDATEPROCE,
                                TYPEDOCID,CURYRATE,CURYDATE,PERPOST,SWTINOUT,
				QTY,STDCOSTPEN,STDCOSTUSD,COMPONENT,SLSPRICE,
				REFNBR,DOCTYPEID,STKFISI
                               )
          VALUES(@SITEID,@INVTIDSKU,@BATNBR,@INTRDATE,@INTRDATEPROCE,
                 @TYPEDOCID,@CURYRATE,@INTRDATE,@PERPOST,@SWTINOUT,
		 @QTY,@STDCOSTPEN,@STDCOSTUSD,@COMPONENT,@SLSPRICE,
		 @REFNBR,@DOCTYPEID,@STKFISI 
                 )
 	
	  SET @ERROR=@@error
     
          IF (@ERROR!=0)
          BEGIN       
            CLOSE INTRAITEM
            DEALLOCATE INTRAITEM
            RAISERROR 20000 'Documento ya fué Emitido'
            RETURN(1)  
          END         
	-------------------------------------------------------         
    	  FETCH NEXT FROM INTRAITEM INTO  @TYPEDOCID, @INVTIDSKU,
                      		    @QTY, @STDCOSTPEN, @STDCOSTUSD,
                      		    @INTRDATE, @INTRDATEPROCE
        
      END /* Termina while */




  CLOSE INTRAITEM
  DEALLOCATE INTRAITEM   
  RETURN (0)

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


/* 
 * PROCEDURE: in_fininventario_proc 
 */


create proc in_fininventario_proc
--inicia la toma de inventario
as
declare @siteorig  ud_idsite	--site que inicia inventario
set @siteorig=(select sourcesite from setup)
begin tran
	update in_setup set stkfisi=0
	IF (@@error!=0)
	    BEGIN
	        RAISERROR  20001 'Error Desactivando Inventario en tabla in_setup'
	        ROLLBACK TRAN
	        RETURN(1)
	END
    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_GenerarDocumentosPorDesensambleProc 
 */

CREATE PROCEDURE IN_GenerarDocumentosPorDesensambleProc
(@SITEID1 	UD_IDSITE,
 @BATNBR1 	char(10),
 @TYPEDOCID1	UD_TYPEMOVEID,  -- CODIGO DEL INGRESO POR DESENSAMBLE
 @TYPEDOCID2	UD_TYPEMOVEID,  -- CODIGO DE LA SALIDA POR DESENSAMBLE
 @INOUT		BIT,		-- 1 SI LOS DOCUMENTOS A GENERAR PROVIENEN DE UN INGRESO Y 0 SI PROVIENEN DE UNA SALIDA
 @VENDIDSS	UD_IDVENDOR		-- ULILIZADO PARA GENERAR EL DETALLE DEL DOCUMENTO2
)
AS
BEGIN

	DECLARE	@BATNBR		CHAR(10)
	DECLARE @BATNBR_NEXT 	char(10)
	DECLARE @SITEID 	UD_IDSITE
	DECLARE @TYPEDOCID	UD_TYPEMOVEID 
	DECLARE @NUM_BATNBR NUMERIC(10,0)
   	DECLARE @ERROR Integer
   	DECLARE @TSTAMP timestamp
   	DECLARE @ROWCOUNT Integer
	SELECT @ERROR=0   

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)
/**********************************************************************************/
/************************* PROCESO DE DOCUMENTO1 **********************************/
/**********************************************************************************/

   	SELECT @BATNBR=BATNBRTYPEDOC FROM IN_SETUP
   	SELECT @NUM_BATNBR=cast(@BATNBR AS NUMERIC(10,0))+1
   	SELECT @BATNBR_NEXT =replicate('0',10-len(@NUM_BATNBR))+cast(@NUM_BATNBR as char(10))


	IF (@INOUT=1)	 
		SET @TYPEDOCID=@TYPEDOCID2
	ELSE
		SET @TYPEDOCID=@TYPEDOCID1		

	SET @SITEID=@SITEID1	 	

  /*** ACTUALIZACION DEL CORRELATIVO DE TIPO DE MOVIMIENTO ***/
    	UPDATE IN_SETUP
    	SET    BATNBRTYPEDOC=@BATNBR_NEXT    
	WHERE  BATNBRTYPEDOC=@BATNBR

	IF (@@error!=0 OR @@ROWCOUNT=0 )
    	BEGIN
        	RETURN(1)
    	END
  /*************** GENERANDO CABECERA DE DOCUMENTO1 **************************/
	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)
		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
		FROM	IN_INTRAHEAD
		WHERE	SITEID=@SITEID AND BATNBR=@BATNBR1

	IF (@@error!=0 OR @@ROWCOUNT=0 )
    	BEGIN
        	RETURN(1)
    	END
  /*************** GENERANDO DETALLE DE DOCUMENTO1 **************************/
	INSERT 	INTO 	IN_INTRAITEM(SITEID,BATNBR,TYPEDOCID,SEQUENCE,INVTIDSKU,INVTIDBARR,QTY,TAXIGV,
			STDCOSTPEN,STDCOSTUSD,STKFISI,INTRDATE,INTRDATEPROCE,RLSED,BUYCOD,DOCSTATUSID)
		SELECT 	@SITEID,@BATNBR,@TYPEDOCID,SEQUENCE,INVTIDSKU,INVTIDBARR,QTY,TAXIGV,
			STDCOSTPEN,STDCOSTUSD,STKFISI,INTRDATE,INTRDATEPROCE,RLSED,BUYCOD,DOCSTATUSID
		FROM	IN_INTRAITEM
		WHERE	SITEID=@SITEID AND BATNBR=@BATNBR1

	IF (@@error!=0 OR @@ROWCOUNT=0 )
    	BEGIN
        	RETURN(1)
    	END
 /*************** EMITIENDO DOCUMENTO1 GENERADO **************************/
        SET EXEC @ERROR=IN_EmitirProc @SITEID,@BATNBR
    
	IF (@ERROR!=0)
    	BEGIN
       		RETURN(1)               
    	END 
/*************************   FIN DE DOCUMENTO1 ************************************/

/**********************************************************************************/
/************************* PROCESO DE DOCUMENTO2 **********************************/
/**********************************************************************************/

   	SELECT @BATNBR=BATNBRTYPEDOC FROM IN_SETUP
   	SELECT @NUM_BATNBR=cast(@BATNBR AS NUMERIC(10,0))+1
   	SELECT @BATNBR_NEXT =replicate('0',10-len(@NUM_BATNBR))+cast(@NUM_BATNBR as char(10))
	
	
	IF (@INOUT=1)	 
		SET @TYPEDOCID=@TYPEDOCID1
	ELSE
		SET @TYPEDOCID=@TYPEDOCID2
	
	SET @SITEID=@SITEID1	 	

 /*** ACTUALIZACION DEL CORRELATIVO DE TIPO DE MOVIMIENTO ***/

    	UPDATE IN_SETUP
    	SET    BATNBRTYPEDOC=@BATNBR_NEXT    
	WHERE  BATNBRTYPEDOC=@BATNBR

	IF (@@error!=0 OR @@ROWCOUNT=0 )
    	BEGIN
        	RETURN(1)
    	END
  /*************** GENERANDO CABECERA DE DOCUMENTO2 **************************/
       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)
		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
		FROM	IN_INTRAHEAD
		WHERE	SITEID=@SITEID AND BATNBR=@BATNBR1

	IF (@@error!=0 OR @@ROWCOUNT=0 )
    	BEGIN
        	RETURN(1)
    	END
  /*************** GENERANDO DETALLE DE DOCUMENTO2 **************************/
	INSERT 	INTO 	IN_INTRAITEM(SITEID,BATNBR,TYPEDOCID,SEQUENCE,INVTIDSKU,INVTIDBARR,QTY,TAXIGV,
			STDCOSTPEN,STDCOSTUSD,STKFISI,INTRDATE,INTRDATEPROCE,RLSED,BUYCOD,DOCSTATUSID)	
	  SELECT TOP 1  @SITEID,@BATNBR,@TYPEDOCID,SEQUENCE,INVTIDSKUDES,IN_INVENTORYALTER.INVTIDBARR,ROUND(QTY*WTSTK2,@ND_STOCKSETUP),ROUND(TAXIGV/(WTSTK2),@ND_COSTOSETUP),
			ROUND(STDCOSTPEN/(WTSTK2),@ND_COSTOSETUP),ROUND(STDCOSTUSD/(WTSTK2),@ND_COSTOSETUP),STKFISI,INTRDATE,INTRDATEPROCE,RLSED,IN_VENDORINVT.BUYCOD,
                        DOCSTATUSID
		FROM	(((IN_INTRAITEM INNER JOIN IN_INVENTORYDES ON IN_INTRAITEM.INVTIDSKU=IN_INVENTORYDES.INVTIDSKU)
                        LEFT JOIN IN_INVENTORYALTER ON IN_INVENTORYDES.INVTIDSKUDES=IN_INVENTORYALTER.INVTIDSKU) 
                        LEFT JOIN IN_VENDORINVT ON IN_INVENTORYDES.INVTIDSKUDES=IN_VENDORINVT.INVTIDSKU)
		WHERE	SITEID=@SITEID AND BATNBR=@BATNBR1 AND IN_VENDORINVT.VENDIDSS=@VENDIDSS
       
	IF (@@error!=0 OR @@ROWCOUNT=0 )
    	BEGIN
        	RETURN(1)
    	END
 /*************** EMITIENDO DOCUMENTO2 GENERADO **************************/
        SET EXEC @ERROR=IN_EmitirProc @SITEID,@BATNBR
    
	IF (@ERROR!=0)
    	BEGIN
       		RETURN(1)               
    	END 

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


/* 
 * PROCEDURE: IN_GrabaDetalleMovimientoProc 
 */

CREATE PROCEDURE IN_GrabaDetalleMovimientoProc
(@CORRNBR int,
@BATNBR1 char(10),
@VENDIDSS UD_IDVENDOR,
@CURYID char(3),
@DOCSTATUSID char(2),
@INTRDATE1	smalldatetime,
@INTRDATEPROCE1	smalldatetime
)
AS
BEGIN    
    DECLARE   @SITEID            UD_IDSITE
    DECLARE   @BATNBR            char(10)
    DECLARE   @TYPEDOCID         UD_TYPEMOVEID
    DECLARE   @SEQUENCE          smallint
    DECLARE   @INVTIDSKU         UD_IDCORPORATIVO
    DECLARE   @INVTIDBARR        UD_CODIGOBARRA
    DECLARE   @QTY               UD_STOCK
    DECLARE   @TAXIGV            numeric(5, 2)
    DECLARE   @STDCOSTPEN        UD_COSTO
    DECLARE   @STDCOSTUSD        UD_COSTO
    DECLARE   @STKFISI           bit
    DECLARE   @INTRDATE          smalldatetime
    DECLARE   @INTRDATEPROCE     smalldatetime
    DECLARE   @RLSED             bit
    DECLARE   @TAXISC		 numeric(5,2)
    DECLARE   @USER1             UD_USER1
    DECLARE   @USER2             UD_USER2
    DECLARE   @USER3             UD_USER3
    DECLARE   @USER4             UD_USER5
    DECLARE   @USER5             UD_USER5
    DECLARE   @USER6             UD_USER6
    DECLARE   @USER7             UD_USER7
    DECLARE   @USER8             UD_USER8
    DECLARE   @USER9             UD_USER9
    DECLARE   @ERROR             integer

   --VARIABLE PARA IN_VENDORINVT
    DECLARE   @MONEDABASE		char(3)    -- almacena la moneda base
    DECLARE   @CAMPO  		char(10)  --para distinguir el campo de costo a tomar 

    SET @BATNBR=@BATNBR1
    SET @INTRDATE=@INTRDATE1
    SET @INTRDATEPROCE=@INTRDATEPROCE1   
    SET @MONEDABASE =(SELECT CURYORIG FROM SETUP) 

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)	
/*******************************************************************************************************/
    	-- VERIFICAR LA TOMA DE INVENTARIO
   	SET @STKFISI=(SELECT STKFISI FROM IN_SETUP)

/*******************************************************************************************************/

    DECLARE INTRAITEM cursor 


    FOR SELECT SITEID, @BATNBR,TYPEDOCID, SEQUENCE, INVTIDSKU, 
               INVTIDBARR, QTY, TAXIGV, STDCOSTPEN, STDCOSTUSD, 
               @STKFISI, @INTRDATE, @INTRDATEPROCE, RLSED, TAXISC,  
               USER1, USER2, USER3, USER4, USER5, USER6, 
               USER7, USER8, USER9
        FROM TMP_IN_INTRAITEM WHERE CORRNBR=@CORRNBR

    OPEN INTRAITEM
    FETCH NEXT FROM INTRAITEM INTO  @SITEID, @BATNBR, @TYPEDOCID, @SEQUENCE, @INVTIDSKU,
                      		    @INVTIDBARR, @QTY, @TAXIGV, @STDCOSTPEN, @STDCOSTUSD,
                      		    @STKFISI, @INTRDATE, @INTRDATEPROCE, @RLSED, @TAXISC, @USER1,
				    @USER2, @USER3, @USER4, @USER5, @USER6,
		      		    @USER7, @USER8, @USER9  
      IF (@@FETCH_STATUS!=0)
          BEGIN       
            CLOSE INTRAITEM
            DEALLOCATE INTRAITEM
            RAISERROR 20000  'No tiene registros en Detalle'
            RETURN(1)  
          END         
     

      WHILE @@FETCH_STATUS=0
        BEGIN    

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


          SET @ERROR=@@error
     
          IF (@ERROR!=0)
          BEGIN       
        CLOSE INTRAITEM
            DEALLOCATE INTRAITEM
            RETURN(1)  
          END         
             

            ----------------------------------------------------------------------

    	  FETCH NEXT FROM INTRAITEM INTO  @SITEID, @BATNBR, @TYPEDOCID, @SEQUENCE, @INVTIDSKU,
                      		    @INVTIDBARR, @QTY, @TAXIGV, @STDCOSTPEN, @STDCOSTUSD,
                      		    @STKFISI, @INTRDATE, @INTRDATEPROCE, @RLSED, @TAXISC, @USER1,
				    @USER2, @USER3, @USER4, @USER5, @USER6,
		      		    @USER7, @USER8, @USER9

        
      END /* Termina while */



  CLOSE INTRAITEM
  DEALLOCATE INTRAITEM
  RETURN (0)

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


/* 
 * PROCEDURE: IN_GrabaRelacionProvArtProc 
 */

CREATE PROCEDURE IN_GrabaRelacionProvArtProc
(@CORRNBR int,
 @SITEID UD_IDSITE)
as
begin
DECLARE   @VENDIDSS           UD_IDVENDOR
DECLARE   @INVTIDSKU          UD_IDCORPORATIVO
DECLARE   @BUYCOD             varchar(20)
DECLARE   @DESCR              UD_DESCRIPCION 
DECLARE   @CURYID             UD_CURYID
DECLARE   @ERROR Integer
    
   DECLARE ProvArt cursor 
   FOR SELECT VENDIDSS, INVTIDSKU, BUYCOD, DESCR, CURYID               
              FROM TMP_IN_VENDORINVTREL where CORRNBR=@CORRNBR

   OPEN ProvArt
       FETCH NEXT FROM ProvArt INTO  @VENDIDSS,@INVTIDSKU,@BUYCOD,@DESCR,
                       @CURYID
                                                   
   WHILE @@FETCH_STATUS = 0
     BEGIN    
      INSERT INTO IN_VENDORINVT (VENDIDSS, INVTIDSKU, BUYCOD, DESCR, CURYID,SITEID) values 
		   	        (@VENDIDSS,@INVTIDSKU,@BUYCOD,@DESCR,@CURYID,@SITEID)
	
      SET @ERROR=@@error
     
       IF (@ERROR!=0)
       BEGIN       
          CLOSE ProvArt
          DEALLOCATE ProvArt
          RETURN(1)  
       END 

       FETCH NEXT FROM ProvArt INTO  @VENDIDSS,@INVTIDSKU,@BUYCOD,@DESCR,
                                     @CURYID
       
    END

  CLOSE ProvArt
  DEALLOCATE ProvArt
  RETURN (0)
end
go
IF OBJECT_ID('IN_GrabaRelacionProvArtProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_GrabaRelacionProvArtProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_GrabaRelacionProvArtProc >>>'
go


/* 
 * PROCEDURE: IN_GrabarMovimientoProc 
 */

CREATE PROCEDURE IN_GrabarMovimientoProc
(
    @SITEID            UD_IDSITE,
    @TYPEDOCID         UD_TYPEMOVEID,
    @VENDIDSS          UD_IDVENDOR,
    @DOCTYPEID         UD_DOCTYPEID,
    @DOCSTATUSID       char(2),
    @REFNBR            char(15),
    @CURYRATE          numeric(6, 4),
    @TOCURYID          char(3),
    @FROMCURYID        char(3),
    @SITEPROCES        UD_IDSITE,
    @NBRORDER          char(15)                 = NULL,
    @DATENBRORDER      smalldatetime            = NULL,
    @USERIDORDER       UD_USERID                = NULL,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @SITEDEST          UD_IDSITE,
    @INTRTOTPEN        numeric(9, 2),
    @INTRTAXPEN        numeric(9, 2),
    @INTRTOTUSD        numeric(9, 2),
    @INTRTAXUSD        numeric(9, 2),
    @PERPOST           char(6),
    @RLSED             bit,
    @DISASSEMBLE       bit,
    @INTRSUBTOTPEN     UD_MONTO,
    @INTRSUBTOTUSD     UD_MONTO,
    @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,
    @CORRNBR           int,
    @BAT 	CHAR(10) =' '	 OUTPUT	 )
AS
BEGIN
   
   DECLARE @BATNBR char(10)
   DECLARE @BATNBR_NEXT char(10)
   DECLARE @NUM_BATNBR NUMERIC(10,0)
   DECLARE @ERROR Integer
   DECLARE @TSTAMP timestamp
   DECLARE @ROWCOUNT Integer
   SELECT @ERROR=0   


    BEGIN TRAN
   
   --set TRANSACTION  isolation LEVEL read committed 

   SELECT @BATNBR=BATNBRTYPEDOC from in_setup
-- SELECT @TSTAMP=TIMESTAMP from in_setup
   SELECT @NUM_BATNBR=cast(@BATNBR AS NUMERIC(10,0))+1
   SELECT @BATNBR_NEXT =replicate('0',10-len(@NUM_BATNBR))+cast(@NUM_BATNBR as char(10))


  /*** ACTUALIZACION DEL CORRELATIVO DE TIPO DE MOVIMIENTO ***/
    	UPDATE IN_SETUP
    	SET    BATNBRTYPEDOC=@BATNBR_NEXT    
	WHERE  BATNBRTYPEDOC=@BATNBR

     IF (@@error!=0 OR @@ROWCOUNT=0 )
    BEGIN
        ROLLBACK TRAN
        RETURN(1)
    
    END
    

    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,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,@USER1,@USER2,@USER3,@USER4,@USER5,@USER6,@USER7,@USER8,@USER9 
           )
 
    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'IN_INTRAHEADInsProc: Cannot insert because primary key value not found in IN_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    /*** Llamada al store de grabado de detalle ******/
    SET EXEC @ERROR=IN_GrabaDetalleMovimientoProc @CORRNBR,@BATNBR,@VENDIDSS,@FROMCURYID,@DOCSTATUSID,@INTRDATE, @INTRDATEPROCE

    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END 

   /*** ACTUALIZACION DEL CORRELATIVO DE TIPO DE MOVIMIENTO
    	UPDATE IN_SETUP
    	SET    BATNBRTYPEDOC=@BATNBR_NEXT    
	WHERE  BATNBRTYPEDOC=@BATNBR

     IF (@@error!=0 OR @@ROWCOUNT=0 )
    BEGIN
        ROLLBACK TRAN
        RETURN(1)
    
    END ***/
   SET @BAT=@BATNBR
   SELECT @BATNBR as NUMEROMOVIMIENTO
    COMMIT TRAN
    RETURN(0)
END
go
IF OBJECT_ID('IN_GrabarMovimientoProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_GrabarMovimientoProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_GrabarMovimientoProc >>>'
go


/* 
 * PROCEDURE: IN_GrabarProvArtAlmProc 
 */

create procedure IN_GrabarProvArtAlmProc
(  @VENDIDSS           UD_IDVENDOR,
   @INVTIDSKU          UD_IDCORPORATIVO,
   @DESCR              UD_DESCRIPCION, 
   @CURYID             UD_CURYID,
   @SITEID             UD_IDSITE)  
as
begin    
    BEGIN TRAN
    INSERT INTO IN_VENDORINVT   (VENDIDSS, INVTIDSKU, BUYCOD, DESCR, CURYID,SITEID) values 
              	   	        (@VENDIDSS,@INVTIDSKU,@INVTIDSKU,@DESCR,@CURYID,@SITEID)
    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Código ya fue Registrado'
        ROLLBACK TRAN
        RETURN(1)    
    END    
    COMMIT TRAN    
    RETURN(0)
end
go
IF OBJECT_ID('IN_GrabarProvArtAlmProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_GrabarProvArtAlmProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_GrabarProvArtAlmProc >>>'
go


/* 
 * PROCEDURE: in_inicioinventario_proc 
 */

create proc in_inicioinventario_proc
--inicia la toma de inventario
as
declare @siteorig  ud_idsite	--site que inicia inventario
set @siteorig=(select sourcesite from setup)
begin tran
	update site 	set startdateinven=getdate(),ADJUST=0  where siteid=@siteorig
	
	IF (@@error!=0) or @@rowcount=0
	    BEGIN
        	RAISERROR  20001 'Error Iniciando Inventario en tabla site'
	        ROLLBACK TRAN
        	RETURN(1)
    	END
	update in_setup set stkfisi=1
	IF (@@error!=0)
	    BEGIN
	        RAISERROR  20001 'Error Iniciando Inventario en tabla in_setup'
	        ROLLBACK TRAN
	        RETURN(1)
	END
    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INTRAHEADDelProc 
 */

CREATE PROCEDURE IN_INTRAHEADDelProc
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEMOVEID)
AS
BEGIN
    BEGIN TRAN

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

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INTRAHEADInsProc 
 */

CREATE PROCEDURE IN_INTRAHEADInsProc
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEMOVEID,
    @VENDIDSS          UD_IDVENDOR,
    @DOCTYPEID         UD_DOCTYPEID,
    @DOCSTATUSID       char(2),
    @REFNBR            char(10),
    @CURYRATE          numeric(6, 4),
    @TOCURYID          char(3),
    @FROMCURYID        char(3),
    @SITEPROCES        UD_IDSITE,
    @NBRORDER          char(10)                 = NULL,
    @DATENBRORDER      smalldatetime            = NULL,
    @USERIDORDER       UD_USERID                = NULL,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @SITEDEST          UD_IDSITE,
    @INTRTOTPEN        numeric(9, 2),
    @INTRTAXPEN        numeric(9, 2),
    @INTRTOTUSD        numeric(9, 2),
    @INTRTAXUSD        numeric(9, 2),
    @PERPOST           char(6),
    @RLSED             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,
                             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,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INTRAHEADSelProc 
 */

CREATE PROCEDURE IN_INTRAHEADSelProc
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEMOVEID)
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,
           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('IN_INTRAHEADSelProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_INTRAHEADSelProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_INTRAHEADSelProc >>>'
go


/* 
 * PROCEDURE: IN_INTRAHEADUpdProc 
 */

CREATE PROCEDURE IN_INTRAHEADUpdProc
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEMOVEID,
    @VENDIDSS          UD_IDVENDOR,
    @DOCTYPEID         UD_DOCTYPEID,
    @DOCSTATUSID       char(2),
    @REFNBR            char(10),
    @CURYRATE          numeric(6, 4),
    @TOCURYID          char(3),
    @FROMCURYID        char(3),
    @SITEPROCES        UD_IDSITE,
    @NBRORDER          char(10)                 = NULL,
    @DATENBRORDER      smalldatetime            = NULL,
    @USERIDORDER       UD_USERID                = NULL,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @SITEDEST          UD_IDSITE,
    @INTRTOTPEN        numeric(9, 2),
    @INTRTAXPEN        numeric(9, 2),
    @INTRTOTUSD        numeric(9, 2),
    @INTRTAXUSD        numeric(9, 2),
    @PERPOST           char(6),
    @RLSED             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)
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,
           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 'IN_INTRAHEADUpdProc: Cannot update  in IN_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INTRAITEMDelProc 
 */

CREATE PROCEDURE IN_INTRAITEMDelProc
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEMOVEID,
    @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 'IN_INTRAITEMDelProc: Cannot delete because foreign keys still exist in IN_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INTRAITEMInsProc 
 */

CREATE PROCEDURE IN_INTRAITEMInsProc
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEMOVEID,
    @SEQUENCE          smallint,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDBARR        UD_CODIGOBARRA,
    @QTY               UD_STOCK,
    @TAXIGV            numeric(4, 2)               = NULL,
    @STDCOSTPEN        UD_COSTO                    = NULL,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STKFISI           bit,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @RLSED             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_INTRAITEM(SITEID,
                             BATNBR,
                             TYPEDOCID,
                             SEQUENCE,
                             INVTIDSKU,
                             INVTIDBARR,
                             QTY,
                             TAXIGV,
                             STDCOSTPEN,
                             STDCOSTUSD,
                             STKFISI,
                             INTRDATE,
                             INTRDATEPROCE,
                             RLSED,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9 
                             )
    VALUES(@SITEID,
           @BATNBR,
           @TYPEDOCID,
           @SEQUENCE,
           @INVTIDSKU,
           @INVTIDBARR,
           @QTY,
           @TAXIGV,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @STKFISI,
           @INTRDATE,
           @INTRDATEPROCE,
           @RLSED,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INTRAITEMSelProc 
 */

CREATE PROCEDURE IN_INTRAITEMSelProc
(   @SITEID     UD_IDSITE,
    @BATNBR     CHAR(10), 
    @VENDIDSS   UD_IDVENDOR,
    @CURYID     UD_CURYID)
AS
BEGIN
      DECLARE @MONEDABASE CHAR(3)
      SET @MONEDABASE =(SELECT CURYORIG FROM SETUP) 

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)	
/*******************************************************************************************************/

      SELECT [SEQUENCE] AS Item,
             IN_INTRAITEM.INVTIDSKU AS Codigo,
	     IN_VENDORINVT.BUYCOD AS Cod_Compra, 
             IN_INVENTORY.DESCR AS Descripcion,
             QTY as Cantidad,
             CASE @CURYID
			WHEN @MONEDABASE THEN STDCOSTPEN
			ELSE STDCOSTUSD
			END as Costo,
             CASE @CURYID
			WHEN @MONEDABASE THEN ROUND(QTY*STDCOSTPEN,@ND_COSTOSETUP)
			ELSE ROUND(QTY*STDCOSTUSD,@ND_COSTOSETUP)
			END as Total,
	IN_INTRAITEM.TAXIGV AS IGV,IN_INTRAITEM.TAXISC AS ISC
      FROM IN_INTRAITEM, IN_INVENTORY, IN_VENDORINVT 
      WHERE IN_INTRAITEM.INVTIDSKU = IN_INVENTORY.INVTIDSKU AND 
      	    IN_INTRAITEM.SITEID=@SITEID AND IN_INTRAITEM.INVTIDSKU=IN_VENDORINVT.INVTIDSKU AND 
            VENDIDSS=@VENDIDSS AND BATNBR=@BATNBR 

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


/* 
 * PROCEDURE: IN_INTRAITEMUpdProc 
 */

CREATE PROCEDURE IN_INTRAITEMUpdProc
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEMOVEID,
    @SEQUENCE          smallint,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDBARR        UD_CODIGOBARRA,
    @QTY               UD_STOCK,
    @TAXIGV            numeric(4, 2)               = NULL,
    @STDCOSTPEN        UD_COSTO                    = NULL,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STKFISI           bit,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @RLSED             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)
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,
           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
       AND SEQUENCE  = @SEQUENCE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INVENTORYALTERDelProc 
 */

CREATE PROCEDURE IN_INVENTORYALTERDelProc
(
  @INVTIDSKU UD_IDCORPORATIVO)
AS
BEGIN
DECLARE @ERROR INTEGER    
    DELETE
        FROM IN_INVENTORYALTER
        WHERE INVTIDSKU = @INVTIDSKU

    SET @ERROR=@@error
    
    IF (@ERROR!=0)
    BEGIN       
        RETURN(1)    
    END

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


/* 
 * PROCEDURE: IN_INVENTORYALTERInsProc 
 */

CREATE PROCEDURE IN_INVENTORYALTERInsProc
(
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO            = NULL,
    @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,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@INVTIDBARR,
           @INVTIDSKU,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INVENTORYALTERSelProc 
 */

CREATE PROCEDURE IN_INVENTORYALTERSelProc
(
    @INVTIDBARR     UD_CODIGOBARRA)
AS
BEGIN
    SELECT INVTIDBARR,
           INVTIDSKU,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYALTER
     WHERE INVTIDBARR = @INVTIDBARR

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


/* 
 * PROCEDURE: IN_INVENTORYALTERUpdProc 
 */

CREATE PROCEDURE IN_INVENTORYALTERUpdProc
(
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO            = NULL,
    @USER1          UD_USER1                    = NULL,
    @USER2          UD_USER2                    = NULL,
    @USER3          UD_USER6                    = NULL,
    @USER4          UD_USER7                    = NULL,
    @TSTAMP         timestamp)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTORYALTER
       SET INVTIDSKU       = @INVTIDSKU,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE INVTIDBARR = @INVTIDBARR

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_INVENTORYDATDelProc 
 */

CREATE PROCEDURE IN_INVENTORYDATDelProc
(
  @INVTIDSKU UD_IDCORPORATIVO)
AS
BEGIN
DECLARE @ERROR INTEGER    
    DELETE
        FROM IN_INVENTORYDAT
        WHERE INVTIDSKU = @INVTIDSKU

    SET @ERROR=@@error
    
    IF (@ERROR!=0)
    BEGIN       
        RETURN(1)    
    END

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


/* 
 * PROCEDURE: IN_INVENTORYDESDelProc 
 */

CREATE PROCEDURE IN_INVENTORYDESDelProc
(
    @INVTIDSKU        UD_IDCORPORATIVO)
AS
BEGIN
DECLARE @ERROR INTEGER      
    DELETE
      FROM IN_INVENTORYDES
    WHERE INVTIDSKU = @INVTIDSKU

    SET @ERROR=@@error
    
    IF (@ERROR!=0)
    BEGIN       
        RETURN(1)    
    END

    RETURN(0)
END

/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_INVENTORYDESDelProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_INVENTORYDESDelProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_INVENTORYDESDelProc >>>'
go


/* 
 * PROCEDURE: IN_INVENTORYDESInsProc 
 */

CREATE PROCEDURE IN_INVENTORYDESInsProc
(   @INVTIDSKU        UD_IDCORPORATIVO,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint)
AS
BEGIN
    DECLARE @ERROR Integer
    SELECT @ERROR=0   
    INSERT INTO IN_INVENTORYDES(INVTIDSKU,
                                INVTIDSKUDES,
                                WTSTK2)
    VALUES(@INVTIDSKU,
           @INVTIDSKUDES,
           @WTSTK2)


    SET @ERROR=@@error
    
    IF (@ERROR!=0)
    BEGIN       
        RETURN(1)    
    END

    RETURN(0)

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


/* 
 * PROCEDURE: IN_INVENTORYDESUpdProc 
 */

CREATE PROCEDURE IN_INVENTORYDESUpdProc
(   @INVTIDSKU        UD_IDCORPORATIVO,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint)
AS
BEGIN
    DECLARE @ERROR Integer
    SELECT @ERROR=0   

    UPDATE IN_INVENTORYDES SET INVTIDSKUDES=@INVTIDSKUDES,
                               WTSTK2=@WTSTK2
           WHERE INVTIDSKU=@INVTIDSKU
    SET @ERROR=@@error
    
    IF (@ERROR!=0)
    BEGIN       
        RETURN(1)    
    END

    RETURN(0)

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


/* 
 * PROCEDURE: IN_KARDEXGENERAL_CATEG_NOVALOPROC 
 */

CREATE PROCEDURE IN_KARDEXGENERAL_CATEG_NOVALOPROC
@MONEDASOLICITADA UD_CURYID,     -- Recibe la Moneda Solicitada por el Usuario  
@FechaDia CHAR(2),               -- Recibe el Día de la Fecha Desde
@FechaMes CHAR(2),               -- Recibe el Mes de la Fecha Desde  
@Fechaanio CHAR(4),              -- Recibe el Año de la Fecha Desde 
@FechaDiaIni char(2),          
@FechaMesIni char(2),  
@FechaAnioIni char(4),   
@FechaMesHistorica char(2),    
@FechaDiaHasta char(2),
@FechaMesHasta char(2),
@FechaAnioHasta char(4),
@CadenaAlmacenes  varchar(8000), -- Recibe los almacenes seleccionado
@CadenaCategorias varchar(8000),  -- Recibe las Categorias Seleccionadas
@TODOSALAMACENES TINYINT,         -- Es 1 si todos los almacenes fueron seleccionados , de lo contrario es 0 
@TODOSCATEGORIAS TINYINT         -- Es 1 si todas las categorias fueron seleccionadas , de lo contrairo es 0
AS
BEGIN
/**Variables para los Tipos de Movimientos Ingresos**/
DECLARE @TIPOCOMPRAFUELS UD_TYPEDOC
DECLARE @TIPOCOMPRASTORE UD_TYPEDOC
DECLARE @TIPOAJUSTEING UD_TYPEDOC
DECLARE @TIPOBONIFICACION UD_TYPEDOC
DECLARE @TIPOTRANSFERING UD_TYPEDOC
/**Variables para los Tipos de Movimientos Salidas **/
DECLARE @TIPOVENTAS UD_TYPEDOC
DECLARE @TIPOAJUSTESAL UD_TYPEDOC
DECLARE @TIPONTACREDITO UD_TYPEDOC
DECLARE @TIPOTRANSFERSAL UD_TYPEDOC 
/*******************************************/
DECLARE @INTRAMOVMONBASE char(10)
DECLARE @INTRAMOVMONDEST char(10)
DECLARE @MONEDAPROCESO char(10)
DECLARE @KDXHISTMONBASE char(6)
DECLARE @KDXHISTMONDEST char(6)
DECLARE @MONEDAKDXHIST char(6)
/*******************************************/
DECLARE @FechaDesde smalldatetime
DECLARE @FechaInicial smalldatetime
DECLARE @FechaHasta smalldatetime
DECLARE @SALDOINICIAL varchar(8000)
DECLARE @INGNOVAL varchar(8000)
DECLARE @SALNOVAL varchar(8000)
DECLARE @INGSALVALORIZADA VARCHAR(8000) 
DECLARE @KARDEXGENERAL VARCHAR(8000)
/*Variables para armar el criterio de Seleccion*/
DECLARE @CRITERIOSELECCION varchar(8000)
DECLARE @CRITERIOSELECCIONVAL varchar(8000)
/*******************************************/
/*Variables para el Manejo de los Decimales*/
/*******************************************/
DECLARE @ND_COSTOSETUP Tinyint
DECLARE @ND_STOCKSETUP Tinyint
DECLARE @ND_STOCK CHAR(1) 
DECLARE @ND_COSTO CHAR(1) 
  

  /* set @FechaInicial=Cast(@FechaMesIni + '-' + @FechaDiaIni + '-' + @FechaAnioIni as smalldatetime)*/
  /* set @FechaDesde= Cast(@FechaMes + '-' +@FechaDia + '-' + @Fechaanio as smalldatetime)*/
  /* set @FechaHasta = Cast(@FechaMesHasta + '-' + @FechaDiaHasta + '-' + @FechaanioHasta as smalldatetime)*/

   set @FechaInicial=Cast(@FechaDiaIni + '-' + @FechaMesIni + '-' + @FechaAnioIni as smalldatetime)
   set @FechaDesde= Cast(@FechaDia + '-' + @FechaMes + '-' + @Fechaanio as smalldatetime)
   set @FechaHasta = Cast(@FechaDiaHasta + '-' + @FechaMesHasta + '-' + @FechaanioHasta as smalldatetime)



  /*Nomnbres de las Columnas que contienen a Moneda Base y Destino */
  set @INTRAMOVMONBASE='STDCOSTPEN' 
  set @INTRAMOVMONDEST='STDCOSTUSD'  
  set @KDXHISTMONBASE ='AVGPEN'
  set @KDXHISTMONDEST ='AVGUSD'
 
  /*Tipo de Movimientos*/
  SET @TIPOCOMPRAFUELS=(SELECT INTYPETRBUYFUELS FROM IN_SETUP) 
  SET @TIPOCOMPRASTORE=(SELECT INTYPETRBUYSTORE FROM IN_SETUP)
  SET @TIPOAJUSTEING=(SELECT INTYPEFISIN FROM IN_SETUP)
  SET @TIPOBONIFICACION=(SELECT INTYPEBONUS FROM IN_SETUP)
  SET @TIPOTRANSFERING=(SELECT INTYPETRANSFERIN FROM IN_SETUP) 
  SET @TIPOVENTAS =(SELECT INTYPESALES FROM IN_SETUP)
  SET @TIPOAJUSTESAL =(SELECT INTYPEFISOUT FROM IN_SETUP)
  SET @TIPONTACREDITO =(SELECT INTYPECREDMEM FROM IN_SETUP)
  SET @TIPOTRANSFERSAL =(SELECT INTYPETRANSFEROUT FROM IN_SETUP)

  /*Establecer los decimales */
  SET @ND_COSTOSETUP=(Select Floatcost from setup)
  SET @ND_STOCKSETUP=(Select Floatqty from setup)
  set @ND_COSTO=RTRIM(LTRIM(STR(@ND_COSTOSETUP)))
  set @ND_STOCK=RTRIM(LTRIM(STR(@ND_STOCKSETUP)))


 /* Para Crear la cadena de Filtros */
   if  @TODOSALAMACENES =1 and @TODOSCATEGORIAS =1   -- Todos los almacenes y todas las Categorias
       begin
           set  @CRITERIOSELECCION= " "
           set  @CRITERIOSELECCIONVAL= " "  
       end
   else
       begin
          if @TODOSALAMACENES =1 and @TODOSCATEGORIAS =0  -- Todos los Almacenes y solo algunas categorias
             begin
               set @CRITERIOSELECCION= " IN_INVENTORY.CLASSID in (" + RTRIM(LTRIM(@CadenaCategorias)) + ") and"
               set @CRITERIOSELECCIONVAL=@CRITERIOSELECCION 
             end
          else             
             begin
               if @TODOSALAMACENES =0 and @TODOSCATEGORIAS =1  -- Algunos Almacenes y Todas las Categorias
                  begin
                    set @CRITERIOSELECCION= " SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and "
                    set @CRITERIOSELECCIONVAL= " INTRAMOV.SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and "
                  end
               else 
                  begin
                    if @TODOSALAMACENES =0 and @TODOSCATEGORIAS =0  -- Algunos Almacenes y Algunas Categorias
                      begin 
                          set @CRITERIOSELECCION= " SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and IN_INVENTORY.CLASSID in (" +
                                                    RTRIM(LTRIM(@CadenaCategorias)) +") and "
                          set @CRITERIOSELECCIONVAL = " INTRAMOV.SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and IN_INVENTORY.CLASSID in (" +
                                                    RTRIM(LTRIM(@CadenaCategorias)) +") and "
                      end
                  end
             end
       end           
   
   
  SET @SALDOINICIAL= " SELECT in_kdxhist.siteid,IN_INVENTORY.CLASSID,in_kdxhist.INVTIDSKU,SUM(ROUND(QTYEND" + @FechaMesHistorica +
                     "," + @ND_STOCK + ")) AS [Inicial],0 as [QTYRESULT],0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL]," + 
                     "0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL]," +
                     "0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 as [TRANSFERVAL],0 as [OTROS]," +
                     "0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL],0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL]," +
                     "0 AS [OTROSSAL] " +
                     " from (in_kdxhist inner join IN_INVENTORY on IN_KDXHIST.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                     " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +
                     " WHERE " + @CRITERIOSELECCION + " yearhist=" +  RTRIM(LTRIM(STR(year(@FechaDesde)))) + 
                     " GROUP BY in_kdxhist.siteid,IN_INVENTORY.CLASSID,in_kdxhist.INVTIDSKU "  +
                     " UNION ALL " + 
                     " SELECT INTRAMOV.SITEID,IN_INVENTORY.CLASSID,INTRAMOV.INVTIDSKU,0 as [Inicial], " + 
                     " SUM(CASE SWTINOUT WHEN 1 THEN QTY WHEN 0 THEN ROUND((QTY * - 1)," + @ND_STOCK +") END) AS [QTYRESULT], " + 
                     " 0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL]," +
                     " 0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 AS [TRANSFERVAL]," + 
                     " 0 as [OTROS],0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL]," + 
                     " 0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL],0 AS [OTROSSAL] " +           
                     " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU)" +
                     " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +
                     " where " + @CRITERIOSELECCION + " intrdateproce >='" + RTRIM(ltrim(Cast(@FechaInicial as char(100)))) +
                     "' AND intrdateproce < '" + 
                     RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' GROUP BY  INTRAMOV.SITEID,IN_INVENTORY.CLASSID," +
                     "INTRAMOV.INVTIDSKU " 
                     

   if @MONEDASOLICITADA=(select CURYORIG from SETUP)
     begin
       SET @MONEDAPROCESO=@INTRAMOVMONBASE
       SET @MONEDAKDXHIST=RTRIM(LTRIM(@KDXHISTMONBASE))
     end
   else   
     begin
       SET @MONEDAPROCESO=@INTRAMOVMONDEST
       SET @MONEDAKDXHIST=RTRIM(LTRIM(@KDXHISTMONDEST))
     end
  
   
  SET @INGNOVAL = " UNION ALL SELECT siteid,IN_INVENTORY.classid,INTRAMOV.INVTIDSKU,0 AS [Inicial],0 AS [QTYRESULT]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRAFUELS + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0  END) AS [COMPRASFUELS]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRAFUELS + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [COMPRASFUELSVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRASTORE + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0  END) AS [COMPRASSTORE]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRASTORE + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [COMPRASSTOREVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOAJUSTEING + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [AJUSTEING]," +  
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOBONIFICACION + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [BONIF]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOBONIFICACION + " THEN ROUND(QTY," + @ND_STOCK + ") * " +
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [BONIFVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERING + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [TRANSFER]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERING + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [TRASNFERVAL]," +	                   
                  " [OTROS]= SUM(CASE WHEN (TYPEDOCID <> " + @TIPOCOMPRAFUELS + " AND TYPEDOCID <> " + @TIPOCOMPRASTORE + 
                  " AND TYPEDOCID <> " + @TIPOAJUSTEING + " AND " +
                  " TYPEDOCID <> " + @TIPOBONIFICACION + " AND TYPEDOCID <> " + @TIPOTRANSFERING + " AND SWTINOUT=1)" +
                  " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END),0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL]," +
                  " 0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL],0 AS [OTROSSAL]" +                  
                  " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                  " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +                    
                  " where " + @CRITERIOSELECCION + " INTRDATEPROCE BETWEEN '" + 
                    RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) +
                  "' GROUP BY SITEID,IN_INVENTORY.CLASSID,INTRAMOV.INVTIDSKU " 
   
   SET @SALNOVAL = " UNION ALL SELECT siteid,IN_INVENTORY.classid,INTRAMOV.INVTIDSKU,0 AS [Inicial],0 AS [QTYRESULT]," +
                   " 0 AS [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL],0 AS [AJUSTEING]," + 
                   " 0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 AS [TRANSFERVAL],0 as [OTROS]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOVENTAS + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [VENTAS]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOAJUSTESAL + " THEN ROUND(QTY," + @ND_STOCK +") ELSE 0 END) AS [AJUSTESAL]," +  
                   " SUM(CASE TYPEDOCID WHEN " + @TIPONTACREDITO + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [NTACREDITO]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPONTACREDITO + " THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAPROCESO  + "," +
                   @ND_COSTO + ") ELSE 0 END) AS [NTACREDITOVAL]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERSAL + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [TRANSFERSAL]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERSAL + " THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAPROCESO  + "," +
                   @ND_COSTO + ") ELSE 0 END) AS [TRANSFERSALVAL]," +
                   " [OTROSSAL]= SUM(CASE WHEN (TYPEDOCID <> " + @TIPOVENTAS + " AND TYPEDOCID <> " + @TIPOAJUSTESAL + " AND " +
                   " TYPEDOCID <> " + @TIPONTACREDITO + " AND TYPEDOCID <> " + @TIPOTRANSFERSAL + " AND SWTINOUT=0)" +
                   " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) " +               
                   " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                   " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +                  
                   " where " + @CRITERIOSELECCION + " INTRDATEPROCE BETWEEN '" +
                     RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) + 
                   "' GROUP BY SITEID,IN_INVENTORY.CLASSID,INTRAMOV.INVTIDSKU "

   
   /*Crear Tabla Temporal para Almacenar los resultados obtenidos de las cadenas @SALDOINICIAL + @INGNOVAL + @SALNOVAL + @INGSALVALORIZADA */
   /*y Crear un Resumen */

   Create Table #TMP_KARDEXGENERAL (SITEID CHAR(5),CLASSID CHAR(5),INVTIDSKU CHAR(20),INICIAL NUMERIC(10,3),QTYRESULT  NUMERIC(10,3),
                 COMPRASFUELS NUMERIC(10,3),COMPRASFUELSVAL NUMERIC(10,4),COMPRASSTORE NUMERIC(10,3),COMPRASSTOREVAL NUMERIC(10,4),
                 AJUSTEING NUMERIC(10,3),BONIF NUMERIC(10,3),BONIFVAL NUMERIC(10,4),TRANSFER NUMERIC(10,3),TRANSFERVAL NUMERIC(10,4),
                 OTROS NUMERIC(10,3),VENTAS NUMERIC(10,3),AJUSTESAL NUMERIC(10,3),NTACREDITO NUMERIC(10,3),NTACREDITOVAL NUMERIC(10,4),
                 TRANSFERSAL  NUMERIC(10,3),TRANSFERSALVAL NUMERIC(10,4),OTROSSAL NUMERIC(10,3)) 
   
   INSERT INTO #TMP_KARDEXGENERAL exec(@SALDOINICIAL + @INGNOVAL + @SALNOVAL)
   
   SET @KARDEXGENERAL=" SELECT #TMP_KARDEXGENERAL.SITEID,SITE.NAME,#TMP_KARDEXGENERAL.CLASSID,IN_CATEGORY.DESCR,#TMP_KARDEXGENERAL.INVTIDSKU," + 
                 " IN_INVENTORY.DESCR as [DESCRARTICULO],SUM(INICIAL+QTYRESULT)AS [INICIAL]," +                                
                 " SUM(COMPRASFUELS+COMPRASSTORE) AS [COMPRAS],SUM(AJUSTEING) AS [AJUSTEINGRESO]," +
                 " SUM(BONIF) AS [BONIFICACION],SUM(TRANSFER) AS [TRANSFERING],SUM(OTROS) AS [OTROSING],SUM(VENTAS) AS [VENTAS]," +
                 " SUM(AJUSTESAL) AS [AJUSTESAL],SUM(NTACREDITO) AS [NOTACRED],SUM(TRANSFERSAL) AS [TRANSFERSAL],SUM(OTROSSAL) AS [OTRASSAL] " + 
                 " FROM (((#TMP_KARDEXGENERAL INNER JOIN IN_KDXHIST ON (#TMP_KARDEXGENERAL.SITEID=IN_KDXHIST.SITEID AND " + 
                 " #TMP_KARDEXGENERAL.INVTIDSKU=IN_KDXHIST.INVTIDSKU) AND YEARHIST=" + RTRIM(LTRIM(STR(year(@FechaDesde)))) + 
		 " ) INNER JOIN SITE ON " +
                 " #TMP_KARDEXGENERAL.SITEID=SITE.SITEID) INNER JOIN IN_INVENTORY ON #TMP_KARDEXGENERAL.INVTIDSKU=" + 
                 " IN_INVENTORY.INVTIDSKU) INNER JOIN IN_CATEGORY ON IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +                
                 " GROUP BY #TMP_KARDEXGENERAL.SITEID,SITE.NAME,#TMP_KARDEXGENERAL.CLASSID,IN_CATEGORY.DESCR, " +
                 " #TMP_KARDEXGENERAL.INVTIDSKU,IN_INVENTORY.DESCR"
   EXEC (@KARDEXGENERAL)
   DROP TABLE #TMP_KARDEXGENERAL
END
go
IF OBJECT_ID('IN_KARDEXGENERAL_CATEG_NOVALOPROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_KARDEXGENERAL_CATEG_NOVALOPROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_KARDEXGENERAL_CATEG_NOVALOPROC >>>'
go


/* 
 * PROCEDURE: IN_KARDEXGENERAL_CATEG_VALOPROC 
 */

CREATE PROCEDURE IN_KARDEXGENERAL_CATEG_VALOPROC
@MONEDASOLICITADA UD_CURYID,     -- Recibe la Moneda Solicitada por el Usuario  
@FechaDia CHAR(2),               -- Recibe el Día de la Fecha Desde
@FechaMes CHAR(2),               -- Recibe el Mes de la Fecha Desde  
@Fechaanio CHAR(4),              -- Recibe el Año de la Fecha Desde 
@FechaDiaIni char(2),            
@FechaMesIni char(2),  
@FechaAnioIni char(4),   
@FechaMesHistorica char(2),    
@FechaDiaHasta char(2),
@FechaMesHasta char(2),
@FechaAnioHasta char(4),
@CadenaAlmacenes  varchar(8000), -- Recibe los almacenes seleccionado
@CadenaCategorias varchar(8000),  -- Recibe las Categorias Seleccionadas
@TODOSALAMACENES TINYINT,         -- Es 1 si todos los almacenes fueron seleccionados , de lo contrario es 0 
@TODOSCATEGORIAS TINYINT         -- Es 1 si todas las categorias fueron seleccionadas , de lo contrairo es 0
AS
BEGIN
/**Variables para los Tipos de Movimientos Ingresos**/
DECLARE @TIPOCOMPRAFUELS UD_TYPEDOC
DECLARE @TIPOCOMPRASTORE UD_TYPEDOC
DECLARE @TIPOAJUSTEING UD_TYPEDOC
DECLARE @TIPOBONIFICACION UD_TYPEDOC
DECLARE @TIPOTRANSFERING UD_TYPEDOC
/**Variables para los Tipos de Movimientos Salidas **/
DECLARE @TIPOVENTAS UD_TYPEDOC
DECLARE @TIPOAJUSTESAL UD_TYPEDOC
DECLARE @TIPONTACREDITO UD_TYPEDOC
DECLARE @TIPOTRANSFERSAL UD_TYPEDOC 
/*******************************************/
DECLARE @INTRAMOVMONBASE char(10)
DECLARE @INTRAMOVMONDEST char(10)
DECLARE @MONEDAPROCESO char(10)
DECLARE @KDXHISTMONBASE char(6)
DECLARE @KDXHISTMONDEST char(6)
DECLARE @MONEDAKDXHIST char(6)
/*******************************************/
DECLARE @FechaDesde smalldatetime
DECLARE @FechaInicial smalldatetime
DECLARE @FechaHasta smalldatetime
DECLARE @SALDOINICIAL varchar(8000)
DECLARE @INGNOVAL varchar(8000)
DECLARE @SALNOVAL varchar(8000)
DECLARE @INGSALVALORIZADA VARCHAR(8000) 
DECLARE @KARDEXGENERAL VARCHAR(8000)
/*Variables para armar el criterio de Seleccion*/
DECLARE @CRITERIOSELECCION varchar(8000)
DECLARE @CRITERIOSELECCIONVAL varchar(8000)
/*******************************************/
/*Variables para el Manejo de los Decimales*/
/*******************************************/
DECLARE @ND_COSTOSETUP Tinyint
DECLARE @ND_STOCKSETUP Tinyint
DECLARE @ND_STOCK CHAR(1) 
DECLARE @ND_COSTO CHAR(1) 
  

 /*  set @FechaInicial=Cast(@FechaMesIni + '-' + @FechaDiaIni + '-' + @FechaAnioIni as smalldatetime)*/
 /*  set @FechaDesde= Cast(@FechaMes + '-' +@FechaDia + '-' + @Fechaanio as smalldatetime)*/
 /*  set @FechaHasta = Cast(@FechaMesHasta + '-' + @FechaDiaHasta + '-' + @FechaanioHasta as smalldatetime)*/

  set @FechaInicial=Cast(@FechaDiaIni + '-' + @FechaMesIni + '-' + @FechaAnioIni as smalldatetime)
  set @FechaDesde= Cast(@FechaDia + '-' + @FechaMes + '-' + @Fechaanio as smalldatetime)
  set @FechaHasta = Cast(@FechaDiaHasta + '-' + @FechaMesHasta + '-' + @FechaanioHasta as smalldatetime)


  /*Nomnbres de las Columnas que contienen a Moneda Base y Destino */
  set @INTRAMOVMONBASE='STDCOSTPEN' 
  set @INTRAMOVMONDEST='STDCOSTUSD'  
  set @KDXHISTMONBASE ='AVGPEN'
  set @KDXHISTMONDEST ='AVGUSD'
 
  /*Tipo de Movimientos*/
  SET @TIPOCOMPRAFUELS=(SELECT INTYPETRBUYFUELS FROM IN_SETUP) 
  SET @TIPOCOMPRASTORE=(SELECT INTYPETRBUYSTORE FROM IN_SETUP)
  SET @TIPOAJUSTEING=(SELECT INTYPEFISIN FROM IN_SETUP)
  SET @TIPOBONIFICACION=(SELECT INTYPEBONUS FROM IN_SETUP)
  SET @TIPOTRANSFERING=(SELECT INTYPETRANSFERIN FROM IN_SETUP) 
  SET @TIPOVENTAS =(SELECT INTYPESALES FROM IN_SETUP)
  SET @TIPOAJUSTESAL =(SELECT INTYPEFISOUT FROM IN_SETUP)
  SET @TIPONTACREDITO =(SELECT INTYPECREDMEM FROM IN_SETUP)
  SET @TIPOTRANSFERSAL =(SELECT INTYPETRANSFEROUT FROM IN_SETUP)

  /*Establecer los decimales */
  SET @ND_COSTOSETUP=(Select Floatcost from setup)
  SET @ND_STOCKSETUP=(Select Floatqty from setup)
  set @ND_COSTO=RTRIM(LTRIM(STR(@ND_COSTOSETUP)))
  set @ND_STOCK=RTRIM(LTRIM(STR(@ND_STOCKSETUP)))


  /* Para Crear la cadena de Filtros */
   if  @TODOSALAMACENES =1 and @TODOSCATEGORIAS =1   -- Todos los almacenes y todas las Categorias
       begin
           set  @CRITERIOSELECCION= " "
           set  @CRITERIOSELECCIONVAL= " "  
       end
   else
       begin
          if @TODOSALAMACENES =1 and @TODOSCATEGORIAS =0  -- Todos los Almacenes y solo algunas categorias
             begin
               set @CRITERIOSELECCION= " IN_INVENTORY.CLASSID in (" + RTRIM(LTRIM(@CadenaCategorias)) + ") and"
               set @CRITERIOSELECCIONVAL=@CRITERIOSELECCION 
             end
          else             
             begin
               if @TODOSALAMACENES =0 and @TODOSCATEGORIAS =1  -- Algunos Almacenes y Todas las Categorias
                  begin
                    set @CRITERIOSELECCION= " SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and "
                    set @CRITERIOSELECCIONVAL= " INTRAMOV.SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and "
                  end
               else 
                  begin
                    if @TODOSALAMACENES =0 and @TODOSCATEGORIAS =0  -- Algunos Almacenes y Algunas Categorias
                      begin 
                          set @CRITERIOSELECCION= " SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and IN_INVENTORY.CLASSID in (" +
                                                    RTRIM(LTRIM(@CadenaCategorias)) +") and "
                          set @CRITERIOSELECCIONVAL = " INTRAMOV.SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and IN_INVENTORY.CLASSID in (" +
                                                    RTRIM(LTRIM(@CadenaCategorias)) +") and "
                      end
                  end
             end
       end           
   
   
  SET @SALDOINICIAL= " SELECT in_kdxhist.siteid,IN_INVENTORY.CLASSID,in_kdxhist.INVTIDSKU,SUM(ROUND(QTYEND" + @FechaMesHistorica +
                     "," + @ND_STOCK + ")) AS [Inicial],0 as [QTYRESULT],0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL]," + 
                     "0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL]," +
                     "0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 as [TRANSFERVAL],0 as [OTROS]," +
                     "0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL],0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL]," +
                     "0 AS [OTROSSAL],0 as [AJUSTEINGVAL],0 as [OTROSINGVAL],0 as [VENTASVAL]," +
                     "0 as [AJUSTESALVAL],0 as [OTROSSALVAL]" +   
                     " from (in_kdxhist inner join IN_INVENTORY on IN_KDXHIST.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                     " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +
                     " WHERE " + @CRITERIOSELECCION + " yearhist=" +  RTRIM(LTRIM(STR(year(@FechaDesde)))) + 
                     " GROUP BY in_kdxhist.siteid,IN_INVENTORY.CLASSID,in_kdxhist.INVTIDSKU "  +
                     " UNION ALL " + 
                     " SELECT INTRAMOV.SITEID,IN_INVENTORY.CLASSID,INTRAMOV.INVTIDSKU,0 as [Inicial], " + 
                     " SUM(CASE SWTINOUT WHEN 1 THEN QTY WHEN 0 THEN ROUND((QTY * - 1)," + @ND_STOCK +") END) AS [QTYRESULT], " + 
                     " 0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL]," +
                     " 0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 AS [TRANSFERVAL]," + 
                     " 0 as [OTROS],0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL]," + 
                     " 0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL],0 AS [OTROSSAL],0 as [AJUSTEINGVAL]," +
                     " 0 as [OTROSINGVAL],0 as [VENTASVAL], 0 as [AJUSTESALVAL],0 as [OTROSSALVAL]" +           
                     " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU)" +
                     " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +
                " where " + @CRITERIOSELECCION + " intrdateproce >='" + RTRIM(ltrim(Cast(@FechaInicial as char(100)))) +
                     "' AND intrdateproce < '" + 
                     RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' GROUP BY  INTRAMOV.SITEID,IN_INVENTORY.CLASSID," +
                     "INTRAMOV.INVTIDSKU " 
                     

   if @MONEDASOLICITADA=(select CURYORIG from SETUP)
     begin
       SET @MONEDAPROCESO=@INTRAMOVMONBASE
       SET @MONEDAKDXHIST=RTRIM(LTRIM(@KDXHISTMONBASE))
     end
   else   
     begin
       SET @MONEDAPROCESO=@INTRAMOVMONDEST
       SET @MONEDAKDXHIST=RTRIM(LTRIM(@KDXHISTMONDEST))
     end
     
  /*************************************************/
   
  SET @INGNOVAL = " UNION ALL SELECT siteid,IN_INVENTORY.classid,INTRAMOV.INVTIDSKU,0 AS [Inicial],0 AS [QTYRESULT]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRAFUELS + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0  END) AS [COMPRASFUELS]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRAFUELS + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [COMPRASFUELSVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRASTORE + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0  END) AS [COMPRASSTORE]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRASTORE + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [COMPRASSTOREVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOAJUSTEING + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [AJUSTEING]," +  
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOBONIFICACION + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [BONIF]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOBONIFICACION + " THEN ROUND(QTY," + @ND_STOCK + ") * " +
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [BONIFVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERING + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [TRANSFER]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERING + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [TRASNFERVAL]," +	                   
                  " [OTROS]= SUM(CASE WHEN (TYPEDOCID <> " + @TIPOCOMPRAFUELS + " AND TYPEDOCID <> " + @TIPOCOMPRASTORE + 
                  " AND TYPEDOCID <> " + @TIPOAJUSTEING + " AND " +
                  " TYPEDOCID <> " + @TIPOBONIFICACION + " AND TYPEDOCID <> " + @TIPOTRANSFERING + " AND SWTINOUT=1)" +
                  " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END),0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL]," +
                  " 0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL],0 AS [OTROSSAL], 0 as [AJUSTEINGVAL],0 as [OTROSINGVAL]," +
                  " 0 as [VENTASVAL], 0 as [AJUSTESALVAL],0 as [OTROSSALVAL] " +                  
                  " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                  " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +                    
                  " where " + @CRITERIOSELECCION + " INTRDATEPROCE BETWEEN '" + 
                    RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) +
                  "' GROUP BY SITEID,IN_INVENTORY.CLASSID,INTRAMOV.INVTIDSKU " 
   
   SET @SALNOVAL = " UNION ALL SELECT siteid,IN_INVENTORY.classid,INTRAMOV.INVTIDSKU,0 AS [Inicial],0 AS [QTYRESULT]," +
                   " 0 AS [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL],0 AS [AJUSTEING]," + 
                   " 0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 AS [TRANSFERVAL],0 as [OTROS]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOVENTAS + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [VENTAS]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOAJUSTESAL + " THEN ROUND(QTY," + @ND_STOCK +") ELSE 0 END) AS [AJUSTESAL]," +  
                   " SUM(CASE TYPEDOCID WHEN " + @TIPONTACREDITO + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [NTACREDITO]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPONTACREDITO + " THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAPROCESO  + "," +
                   @ND_COSTO + ") ELSE 0 END) AS [NTACREDITOVAL]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERSAL + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [TRANSFERSAL]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERSAL + " THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAPROCESO  + "," +
                   @ND_COSTO + ") ELSE 0 END) AS [TRANSFERSALVAL]," +
                   " [OTROSSAL]= SUM(CASE WHEN (TYPEDOCID <> " + @TIPOVENTAS + " AND TYPEDOCID <> " + @TIPOAJUSTESAL + " AND " +
                   " TYPEDOCID <> " + @TIPONTACREDITO + " AND TYPEDOCID <> " + @TIPOTRANSFERSAL + " AND SWTINOUT=0)" +
                   " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END),0 as [AJUSTEINGVAL],0 as [OTROSINGVAL],0 as [VENTASVAL]," +
                   " 0 as [AJUSTESALVAL],0 as [OTROSSALVAL] " +               
                   " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                   " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +                  
                   " where " + @CRITERIOSELECCION + " INTRDATEPROCE BETWEEN '" +
                     RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) + 
                   "' GROUP BY SITEID,IN_INVENTORY.CLASSID,INTRAMOV.INVTIDSKU "

  SET @INGSALVALORIZADA=" UNION ALL select intramov.siteid,in_inventory.classid,in_inventory.INVTIDSKU,0 as [Inicial],0 as [QTYRESULT]," +
                        "0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL],0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 AS [TRANSFER]," +
                        "0 AS [TRANSFERVAL],0 AS [OTROS],0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL],0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL]," +
                        "0 AS [OTROSSAL],[AJUSTEINGVAL]=SUM(CASE WHEN TYPEDOCID=" + @TIPOAJUSTEING  + " THEN " + 			           		               
                        " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                        
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END)," +
      "[OTROSINGVAL]=SUM(CASE WHEN (TYPEDOCID <> " + @TIPOCOMPRAFUELS + " AND TYPEDOCID <> " + @TIPOCOMPRASTORE + " AND TYPEDOCID <> " + @TIPOAJUSTEING + " AND " +
                        " TYPEDOCID <> " + @TIPOBONIFICACION + " AND TYPEDOCID <> " + @TIPOTRANSFERING + " AND SWTINOUT=1) THEN " +
                        " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                       
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END)," +
       "[VENTASVAL]=SUM(CASE WHEN TYPEDOCID=" + @TIPOVENTAS + " THEN " + 
                        " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                       
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
  		" END ELSE 0 END)," +
       "[AJUSTESALVAL]=SUM(CASE WHEN TYPEDOCID=" + @TIPOAJUSTESAL + " THEN " +  
                      " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                         
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END)," +
       "[OTROSSALVAL]=SUM(CASE WHEN (TYPEDOCID <>" + @TIPOVENTAS + " AND TYPEDOCID <>" + @TIPOAJUSTESAL + " AND " +
                        " TYPEDOCID <>" + @TIPONTACREDITO + " AND TYPEDOCID <>" + @TIPOTRANSFERSAL + " AND SWTINOUT=0) THEN " +
                        " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                         
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END) " + 
        " from ((intramov inner join in_kdxhist " +
        " on intramov.INVTIDSKU=in_kdxhist.INVTIDSKU and intramov.siteid=in_kdxhist.siteid) inner join in_inventory " +
        " on intramov.INVTIDSKU=in_inventory.INVTIDSKU) inner join in_category " + 
        " on in_inventory.classid=in_category.classid  WHERE YEAR(INTRDATEPROCE)=YEARHIST and " + @CRITERIOSELECCIONVAL +
        " INTRDATEPROCE BETWEEN '" + RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) +                              
        "' GROUP BY intramov.siteid,in_inventory.classid,in_inventory.INVTIDSKU "
   
   /*Crear Tabla Temporal para Almacenar los resultados obtenidos de las cadenas @SALDOINICIAL + @INGNOVAL + @SALNOVAL + @INGSALVALORIZADA */
   /*y Crear un Resumen */

   Create Table #TMP_KARDEXGENERAL (SITEID CHAR(5),CLASSID CHAR(5),INVTIDSKU CHAR(20),INICIAL NUMERIC(10,3),QTYRESULT  NUMERIC(10,3),
                 COMPRASFUELS NUMERIC(10,3),COMPRASFUELSVAL NUMERIC(10,4),COMPRASSTORE NUMERIC(10,3),COMPRASSTOREVAL NUMERIC(10,4),
                 AJUSTEING NUMERIC(10,3),BONIF NUMERIC(10,3),BONIFVAL NUMERIC(10,4),TRANSFER NUMERIC(10,3),TRANSFERVAL NUMERIC(10,4),
                 OTROS NUMERIC(10,3),VENTAS NUMERIC(10,3),AJUSTESAL NUMERIC(10,3),NTACREDITO NUMERIC(10,3),NTACREDITOVAL NUMERIC(10,4),
                 TRANSFERSAL  NUMERIC(10,3),TRANSFERSALVAL NUMERIC(10,4),OTROSSAL NUMERIC(10,3),AJUSTEINGVAL NUMERIC(10,4),
                 OTROSINGVAL NUMERIC(10,4),VENTASVAL NUMERIC(10,4) ,AJUSTESALVAL NUMERIC(10,4) ,OTROSSALVAL NUMERIC(10,4)) 
   

   INSERT INTO #TMP_KARDEXGENERAL exec(@SALDOINICIAL + @INGNOVAL + @SALNOVAL + @INGSALVALORIZADA)
   
   SET @KARDEXGENERAL=" SELECT #TMP_KARDEXGENERAL.SITEID,SITE.NAME,#TMP_KARDEXGENERAL.CLASSID,IN_CATEGORY.DESCR,#TMP_KARDEXGENERAL.INVTIDSKU," + 
                 " IN_INVENTORY.DESCR as [DESCRARTICULO],SUM(INICIAL+QTYRESULT)AS [INICIAL]," +               
                 " ROUND(SUM(INICIAL+QTYRESULT) * ROUND(" +  @MONEDAKDXHIST + @FechaMes + "," + @ND_COSTO + ")," + @ND_COSTO + ") as [INICIALVAL]," +                      
                 " SUM(COMPRASFUELS+COMPRASSTORE) AS [COMPRAS],SUM(AJUSTEING) AS [AJUSTEINGRESO]," +
                 " SUM(BONIF) AS [BONIFICACION],SUM(TRANSFER) AS [TRANSFERING],SUM(OTROS) AS [OTROSING],SUM(VENTAS) AS [VENTAS]," +
                 " SUM(AJUSTESAL) AS [AJUSTESAL],SUM(NTACREDITO) AS [NOTACRED],SUM(TRANSFERSAL) AS [TRANSFERSAL],SUM(OTROSSAL) AS [OTRASSAL]," + 
                 " SUM(COMPRASFUELSVAL+COMPRASSTOREVAL) AS [COMPRASVAL],SUM(AJUSTEINGVAL) AS [AJUSTEINGVALO],SUM(BONIFVAL) AS [BONIFIVAL]," +
                 " SUM(TRANSFERVAL) AS [TRANSINGVAL],SUM(OTROSINGVAL) AS [OTROSINGVAL],SUM(VENTASVAL) AS [VENTASVALOR]," +
                 " SUM(AJUSTESALVAL) AS [AJUSTESALIVAL],SUM(NTACREDITOVAL) AS [NOTACREDVAL],SUM(TRANSFERSALVAL) AS [TRANSFERSALVALOR]," +
                 " SUM(OTROSSALVAL) AS [OTROSSALVALOR] " + 
                 " FROM (((#TMP_KARDEXGENERAL INNER JOIN IN_KDXHIST ON (#TMP_KARDEXGENERAL.SITEID=IN_KDXHIST.SITEID AND " + 
                 " #TMP_KARDEXGENERAL.INVTIDSKU=IN_KDXHIST.INVTIDSKU) AND YEARHIST=" + RTRIM(LTRIM(STR(year(@FechaDesde)))) + 
		 " ) INNER JOIN SITE ON " +
                 " #TMP_KARDEXGENERAL.SITEID=SITE.SITEID) INNER JOIN IN_INVENTORY ON #TMP_KARDEXGENERAL.INVTIDSKU=" + 
                 " IN_INVENTORY.INVTIDSKU) INNER JOIN IN_CATEGORY ON IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +                
                 " GROUP BY #TMP_KARDEXGENERAL.SITEID,SITE.NAME,#TMP_KARDEXGENERAL.CLASSID,IN_CATEGORY.DESCR, " +
                 " #TMP_KARDEXGENERAL.INVTIDSKU,IN_INVENTORY.DESCR,IN_KDXHIST." +  @MONEDAKDXHIST + @FechaMes 

   EXEC (@KARDEXGENERAL)
  
   DROP TABLE #TMP_KARDEXGENERAL
END
go
IF OBJECT_ID('IN_KARDEXGENERAL_CATEG_VALOPROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_KARDEXGENERAL_CATEG_VALOPROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_KARDEXGENERAL_CATEG_VALOPROC >>>'
go


/* 
 * PROCEDURE: IN_KARDEXGENERAL_SUBCATEG_NOVALOPROC 
 */

CREATE PROCEDURE IN_KARDEXGENERAL_SUBCATEG_NOVALOPROC
@MONEDASOLICITADA UD_CURYID,     -- Recibe la Moneda Solicitada por el Usuario  
@FechaDia CHAR(2),               -- Recibe el Día de la Fecha Desde
@FechaMes CHAR(2),               -- Recibe el Mes de la Fecha Desde  
@Fechaanio CHAR(4),              -- Recibe el Año de la Fecha Desde 
@FechaDiaIni char(2),          
@FechaMesIni char(2),  
@FechaAnioIni char(4),   
@FechaMesHistorica char(2),    
@FechaDiaHasta char(2),
@FechaMesHasta char(2),
@FechaAnioHasta char(4),
@CadenaAlmacenes  varchar(8000), -- Recibe los almacenes seleccionado
@CadenaSubCategorias varchar(8000),  -- Recibe las Categorias Seleccionadas
@TODOSALAMACENES TINYINT,         -- Es 1 si todos los almacenes fueron seleccionados , de lo contrario es 0 
@TODOSCATEGORIAS TINYINT         -- Es 1 si todas las categorias fueron seleccionadas , de lo contrairo es 0
AS
BEGIN
/**Variables para los Tipos de Movimientos Ingresos**/
DECLARE @TIPOCOMPRAFUELS UD_TYPEDOC
DECLARE @TIPOCOMPRASTORE UD_TYPEDOC
DECLARE @TIPOAJUSTEING UD_TYPEDOC
DECLARE @TIPOBONIFICACION UD_TYPEDOC
DECLARE @TIPOTRANSFERING UD_TYPEDOC
/**Variables para los Tipos de Movimientos Salidas **/
DECLARE @TIPOVENTAS UD_TYPEDOC
DECLARE @TIPOAJUSTESAL UD_TYPEDOC
DECLARE @TIPONTACREDITO UD_TYPEDOC
DECLARE @TIPOTRANSFERSAL UD_TYPEDOC 
/*******************************************/
DECLARE @INTRAMOVMONBASE char(10)
DECLARE @INTRAMOVMONDEST char(10)
DECLARE @MONEDAPROCESO char(10)
DECLARE @KDXHISTMONBASE char(6)
DECLARE @KDXHISTMONDEST char(6)
DECLARE @MONEDAKDXHIST char(6)
/*******************************************/
DECLARE @FechaDesde smalldatetime
DECLARE @FechaInicial smalldatetime
DECLARE @FechaHasta smalldatetime
DECLARE @SALDOINICIAL varchar(8000)
DECLARE @INGNOVAL varchar(8000)
DECLARE @SALNOVAL varchar(8000)
DECLARE @INGSALVALORIZADA VARCHAR(8000) 
DECLARE @KARDEXGENERAL VARCHAR(8000)
/*Variables para armar el criterio de Seleccion*/
DECLARE @CRITERIOSELECCION varchar(8000)
DECLARE @CRITERIOSELECCIONVAL varchar(8000)
/*******************************************/
/*Variables para el Manejo de los Decimales*/
/*******************************************/
DECLARE @ND_COSTOSETUP Tinyint
DECLARE @ND_STOCKSETUP Tinyint
DECLARE @ND_STOCK CHAR(1) 
DECLARE @ND_COSTO CHAR(1) 
  
   
 /* set @FechaInicial=Cast(@FechaMesIni + '-' + @FechaDiaIni + '-' + @FechaAnioIni as smalldatetime)*/
 /*  set @FechaDesde= Cast(@FechaMes + '-' +@FechaDia + '-' + @Fechaanio as smalldatetime)*/
 /*  set @FechaHasta = Cast(@FechaMesHasta + '-' + @FechaDiaHasta + '-' + @FechaanioHasta as smalldatetime) */

   set @FechaInicial=Cast(@FechaDiaIni + '-' + @FechaMesIni + '-' + @FechaAnioIni as smalldatetime)
   set @FechaDesde= Cast(@FechaDia + '-' + @FechaMes + '-' + @Fechaanio as smalldatetime)
   set @FechaHasta = Cast(@FechaDiaHasta + '-' + @FechaMesHasta + '-' + @FechaanioHasta as smalldatetime)



  /*Nomnbres de las Columnas que contienen a Moneda Base y Destino */
  set @INTRAMOVMONBASE='STDCOSTPEN' 
  set @INTRAMOVMONDEST='STDCOSTUSD'  
  set @KDXHISTMONBASE ='AVGPEN'
  set @KDXHISTMONDEST ='AVGUSD'
 
  /*Tipo de Movimientos*/
  SET @TIPOCOMPRAFUELS=(SELECT INTYPETRBUYFUELS FROM IN_SETUP) 
  SET @TIPOCOMPRASTORE=(SELECT INTYPETRBUYSTORE FROM IN_SETUP)
  SET @TIPOAJUSTEING=(SELECT INTYPEFISIN FROM IN_SETUP)
  SET @TIPOBONIFICACION=(SELECT INTYPEBONUS FROM IN_SETUP)
  SET @TIPOTRANSFERING=(SELECT INTYPETRANSFERIN FROM IN_SETUP) 
  SET @TIPOVENTAS =(SELECT INTYPESALES FROM IN_SETUP)
  SET @TIPOAJUSTESAL =(SELECT INTYPEFISOUT FROM IN_SETUP)
  SET @TIPONTACREDITO =(SELECT INTYPECREDMEM FROM IN_SETUP)
  SET @TIPOTRANSFERSAL =(SELECT INTYPETRANSFEROUT FROM IN_SETUP)

  /*Establecer los decimales */
  SET @ND_COSTOSETUP=(Select Floatcost from setup)
  SET @ND_STOCKSETUP=(Select Floatqty from setup)
  set @ND_COSTO=RTRIM(LTRIM(STR(@ND_COSTOSETUP)))
  set @ND_STOCK=RTRIM(LTRIM(STR(@ND_STOCKSETUP)))


  /* Para Crear la cadena de Filtros */
   if  @TODOSALAMACENES =1 and @TODOSCATEGORIAS =1   -- Todos los almacenes y todas las Categorias
       begin
           set  @CRITERIOSELECCION= " "
           set  @CRITERIOSELECCIONVAL= " "  
       end
   else
       begin
          if @TODOSALAMACENES =1 and @TODOSCATEGORIAS =0  -- Todos los Almacenes y solo algunas categorias
             begin
               set @CRITERIOSELECCION= " IN_INVENTORY.CLASSUBID in (" + RTRIM(LTRIM(@CadenaSubCategorias)) + ") and"
               set @CRITERIOSELECCIONVAL=@CRITERIOSELECCION 
             end
          else             
             begin
               if @TODOSALAMACENES =0 and @TODOSCATEGORIAS =1  -- Algunos Almacenes y Todas las Categorias
                  begin
                    set @CRITERIOSELECCION= " SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and "
                    set @CRITERIOSELECCIONVAL= " INTRAMOV.SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and "
                  end
               else 
                  begin
                    if @TODOSALAMACENES =0 and @TODOSCATEGORIAS =0  -- Algunos Almacenes y Algunas Categorias
                      begin 
                          set @CRITERIOSELECCION= " SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and IN_INVENTORY.CLASSUBID in (" +
                                                    RTRIM(LTRIM(@CadenaSubCategorias)) +") and "
                          set @CRITERIOSELECCIONVAL = " INTRAMOV.SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and IN_INVENTORY.CLASSUBID in (" +
                                                    RTRIM(LTRIM(@CadenaSubCategorias)) +") and "
                      end
                  end
             end
       end           
   
   
  SET @SALDOINICIAL= " SELECT in_kdxhist.siteid,IN_INVENTORY.CLASSUBID,in_kdxhist.INVTIDSKU,SUM(ROUND(QTYEND" + @FechaMesHistorica +
                     "," + @ND_STOCK + ")) AS [Inicial],0 as [QTYRESULT],0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL]," + 
                     "0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL]," +
                     "0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 as [TRANSFERVAL],0 as [OTROS]," +
                     "0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL],0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL]," +
                     "0 AS [OTROSSAL] " +
                     " from (in_kdxhist inner join IN_INVENTORY on IN_KDXHIST.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                     " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +
                     " WHERE " + @CRITERIOSELECCION + " yearhist=" +  RTRIM(LTRIM(STR(year(@FechaDesde)))) + 
                     " GROUP BY in_kdxhist.siteid,IN_INVENTORY.CLASSUBID,in_kdxhist.INVTIDSKU "  +
                     " UNION ALL " + 
                     " SELECT INTRAMOV.SITEID,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU,0 as [Inicial], " + 
                     " SUM(CASE SWTINOUT WHEN 1 THEN QTY WHEN 0 THEN ROUND((QTY * - 1)," + @ND_STOCK +") END) AS [QTYRESULT], " + 
                     " 0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL]," +
                     " 0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 AS [TRANSFERVAL]," + 
                     " 0 as [OTROS],0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL]," + 
                     " 0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL],0 AS [OTROSSAL] " +           
                     " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU)" +
                     " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +
                     " where " + @CRITERIOSELECCION + " intrdateproce >='" + RTRIM(ltrim(Cast(@FechaInicial as char(100)))) +
                     "' AND intrdateproce < '" + 
  RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' GROUP BY  INTRAMOV.SITEID,IN_INVENTORY.CLASSUBID," +
                     "INTRAMOV.INVTIDSKU " 
                     

   if @MONEDASOLICITADA=(select CURYORIG from SETUP)
     begin
       SET @MONEDAPROCESO=@INTRAMOVMONBASE
       SET @MONEDAKDXHIST=RTRIM(LTRIM(@KDXHISTMONBASE))
     end
   else   
     begin
       SET @MONEDAPROCESO=@INTRAMOVMONDEST
       SET @MONEDAKDXHIST=RTRIM(LTRIM(@KDXHISTMONDEST))
     end
  
   
  SET @INGNOVAL = " UNION ALL SELECT siteid,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU,0 AS [Inicial],0 AS [QTYRESULT]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRAFUELS + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0  END) AS [COMPRASFUELS]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRAFUELS + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [COMPRASFUELSVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRASTORE + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0  END) AS [COMPRASSTORE]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRASTORE + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [COMPRASSTOREVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOAJUSTEING + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [AJUSTEING]," +  
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOBONIFICACION + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [BONIF]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOBONIFICACION + " THEN ROUND(QTY," + @ND_STOCK + ") * " +
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [BONIFVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERING + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [TRANSFER]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERING + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [TRASNFERVAL]," +	                   
                  " [OTROS]= SUM(CASE WHEN (TYPEDOCID <> " + @TIPOCOMPRAFUELS + " AND TYPEDOCID <> " + @TIPOCOMPRASTORE + 
                  " AND TYPEDOCID <> " + @TIPOAJUSTEING + " AND " +
                  " TYPEDOCID <> " + @TIPOBONIFICACION + " AND TYPEDOCID <> " + @TIPOTRANSFERING + " AND SWTINOUT=1)" +
                  " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END),0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL]," +
                  " 0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL],0 AS [OTROSSAL]" +                  
                  " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                  " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +                    
                  " where " + @CRITERIOSELECCION + " INTRDATEPROCE BETWEEN '" + 
                    RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) +
                  "' GROUP BY SITEID,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU " 
   
   SET @SALNOVAL = " UNION ALL SELECT siteid,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU,0 AS [Inicial],0 AS [QTYRESULT]," +
                   " 0 AS [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL],0 AS [AJUSTEING]," + 
                   " 0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 AS [TRANSFERVAL],0 as [OTROS]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOVENTAS + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [VENTAS]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOAJUSTESAL + " THEN ROUND(QTY," + @ND_STOCK +") ELSE 0 END) AS [AJUSTESAL]," +  
                   " SUM(CASE TYPEDOCID WHEN " + @TIPONTACREDITO + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [NTACREDITO]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPONTACREDITO + " THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAPROCESO  + "," +
                   @ND_COSTO + ") ELSE 0 END) AS [NTACREDITOVAL]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERSAL + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [TRANSFERSAL]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERSAL + " THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAPROCESO  + "," +
                   @ND_COSTO + ") ELSE 0 END) AS [TRANSFERSALVAL]," +
                   " [OTROSSAL]= SUM(CASE WHEN (TYPEDOCID <> " + @TIPOVENTAS + " AND TYPEDOCID <> " + @TIPOAJUSTESAL + " AND " +
                   " TYPEDOCID <> " + @TIPONTACREDITO + " AND TYPEDOCID <> " + @TIPOTRANSFERSAL + " AND SWTINOUT=0)" +
                   " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) " +               
                   " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                   " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +                  
                   " where " + @CRITERIOSELECCION + " INTRDATEPROCE BETWEEN '" +
                     RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) + 
                   "' GROUP BY SITEID,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU "

   
   /*Crear Tabla Temporal para Almacenar los resultados obtenidos de las cadenas @SALDOINICIAL + @INGNOVAL + @SALNOVAL + @INGSALVALORIZADA */
   /*y Crear un Resumen */

   Create Table #TMP_KARDEXGENERAL (SITEID CHAR(5),CLASSUBID CHAR(5),INVTIDSKU CHAR(20),INICIAL NUMERIC(10,3),QTYRESULT  NUMERIC(10,3),
                 COMPRASFUELS NUMERIC(10,3),COMPRASFUELSVAL NUMERIC(10,4),COMPRASSTORE NUMERIC(10,3),COMPRASSTOREVAL NUMERIC(10,4),
                 AJUSTEING NUMERIC(10,3),BONIF NUMERIC(10,3),BONIFVAL NUMERIC(10,4),TRANSFER NUMERIC(10,3),TRANSFERVAL NUMERIC(10,4),
                 OTROS NUMERIC(10,3),VENTAS NUMERIC(10,3),AJUSTESAL NUMERIC(10,3),NTACREDITO NUMERIC(10,3),NTACREDITOVAL NUMERIC(10,4),
                 TRANSFERSAL  NUMERIC(10,3),TRANSFERSALVAL NUMERIC(10,4),OTROSSAL NUMERIC(10,3)) 
   
   INSERT INTO #TMP_KARDEXGENERAL exec(@SALDOINICIAL + @INGNOVAL + @SALNOVAL)
   
   SET @KARDEXGENERAL=" SELECT #TMP_KARDEXGENERAL.SITEID,SITE.NAME,#TMP_KARDEXGENERAL.CLASSUBID,IN_SUBCATEGORY.DESCR,#TMP_KARDEXGENERAL.INVTIDSKU," + 
                 " IN_INVENTORY.DESCR as [DESCRARTICULO],SUM(INICIAL+QTYRESULT)AS [INICIAL]," +                                
                 " SUM(COMPRASFUELS+COMPRASSTORE) AS [COMPRAS],SUM(AJUSTEING) AS [AJUSTEINGRESO]," +
                 " SUM(BONIF) AS [BONIFICACION],SUM(TRANSFER) AS [TRANSFERING],SUM(OTROS) AS [OTROSING],SUM(VENTAS) AS [VENTAS]," +
                 " SUM(AJUSTESAL) AS [AJUSTESAL],SUM(NTACREDITO) AS [NOTACRED],SUM(TRANSFERSAL) AS [TRANSFERSAL],SUM(OTROSSAL) AS [OTRASSAL] " + 
                 " FROM (((#TMP_KARDEXGENERAL INNER JOIN IN_KDXHIST ON (#TMP_KARDEXGENERAL.SITEID=IN_KDXHIST.SITEID AND " + 
                 " #TMP_KARDEXGENERAL.INVTIDSKU=IN_KDXHIST.INVTIDSKU) AND YEARHIST=" + RTRIM(LTRIM(STR(year(@FechaDesde)))) + 
		 " ) INNER JOIN SITE ON " +
                 " #TMP_KARDEXGENERAL.SITEID=SITE.SITEID) INNER JOIN IN_INVENTORY ON #TMP_KARDEXGENERAL.INVTIDSKU=" + 
                 " IN_INVENTORY.INVTIDSKU) INNER JOIN IN_SUBCATEGORY ON IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +                
                 " GROUP BY #TMP_KARDEXGENERAL.SITEID,SITE.NAME,#TMP_KARDEXGENERAL.CLASSUBID,IN_SUBCATEGORY.DESCR, " +
                 " #TMP_KARDEXGENERAL.INVTIDSKU,IN_INVENTORY.DESCR"
   EXEC (@KARDEXGENERAL)
   DROP TABLE #TMP_KARDEXGENERAL
END
go
IF OBJECT_ID('IN_KARDEXGENERAL_SUBCATEG_NOVALOPROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_KARDEXGENERAL_SUBCATEG_NOVALOPROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_KARDEXGENERAL_SUBCATEG_NOVALOPROC >>>'
go


/* 
 * PROCEDURE: IN_KARDEXGENERAL_SUBCATEG_VALOPROC 
 */

CREATE PROCEDURE IN_KARDEXGENERAL_SUBCATEG_VALOPROC
@MONEDASOLICITADA UD_CURYID,     -- Recibe la Moneda Solicitada por el Usuario  
@FechaDia CHAR(2),               -- Recibe el Día de la Fecha Desde
@FechaMes CHAR(2),               -- Recibe el Mes de la Fecha Desde  
@Fechaanio CHAR(4),              -- Recibe el Año de la Fecha Desde 
@FechaDiaIni char(2),          
@FechaMesIni char(2),  
@FechaAnioIni char(4),   
@FechaMesHistorica char(2),    
@FechaDiaHasta char(2),
@FechaMesHasta char(2),
@FechaAnioHasta char(4),
@CadenaAlmacenes  varchar(8000), -- Recibe los almacenes seleccionado
@CadenaSubCategorias varchar(8000),  -- Recibe las Categorias Seleccionadas
@TODOSALAMACENES TINYINT,         -- Es 1 si todos los almacenes fueron seleccionados , de lo contrario es 0 
@TODOSCATEGORIAS TINYINT         -- Es 1 si todas las categorias fueron seleccionadas , de lo contrairo es 0
AS
BEGIN
/**Variables para los Tipos de Movimientos Ingresos**/
DECLARE @TIPOCOMPRAFUELS UD_TYPEDOC
DECLARE @TIPOCOMPRASTORE UD_TYPEDOC
DECLARE @TIPOAJUSTEING UD_TYPEDOC
DECLARE @TIPOBONIFICACION UD_TYPEDOC
DECLARE @TIPOTRANSFERING UD_TYPEDOC
/**Variables para los Tipos de Movimientos Salidas **/
DECLARE @TIPOVENTAS UD_TYPEDOC
DECLARE @TIPOAJUSTESAL UD_TYPEDOC
DECLARE @TIPONTACREDITO UD_TYPEDOC
DECLARE @TIPOTRANSFERSAL UD_TYPEDOC 
/*******************************************/
DECLARE @INTRAMOVMONBASE char(10)
DECLARE @INTRAMOVMONDEST char(10)
DECLARE @MONEDAPROCESO char(10)
DECLARE @KDXHISTMONBASE char(6)
DECLARE @KDXHISTMONDEST char(6)
DECLARE @MONEDAKDXHIST char(6)
/*******************************************/
DECLARE @FechaDesde smalldatetime
DECLARE @FechaInicial smalldatetime
DECLARE @FechaHasta smalldatetime
DECLARE @SALDOINICIAL varchar(8000)
DECLARE @INGNOVAL varchar(8000)
DECLARE @SALNOVAL varchar(8000)
DECLARE @INGSALVALORIZADA VARCHAR(8000) 
DECLARE @KARDEXGENERAL VARCHAR(8000)
/*Variables para armar el criterio de Seleccion*/
DECLARE @CRITERIOSELECCION varchar(8000)
DECLARE @CRITERIOSELECCIONVAL varchar(8000)
/*******************************************/
/*Variables para el Manejo de los Decimales*/
/*******************************************/
DECLARE @ND_COSTOSETUP Tinyint
DECLARE @ND_STOCKSETUP Tinyint
DECLARE @ND_STOCK CHAR(1) 
DECLARE @ND_COSTO CHAR(1) 
  

 /* set @FechaInicial=Cast(@FechaMesIni + '-' + @FechaDiaIni + '-' + @FechaAnioIni as smalldatetime)*/
 /* set @FechaDesde= Cast(@FechaMes + '-' +@FechaDia + '-' + @Fechaanio as smalldatetime)*/
 /* set @FechaHasta = Cast(@FechaMesHasta + '-' + @FechaDiaHasta + '-' + @FechaanioHasta as smalldatetime)*/

   set @FechaInicial=Cast(@FechaDiaIni + '-' + @FechaMesIni + '-' + @FechaAnioIni as smalldatetime)
   set @FechaDesde= Cast(@FechaDia + '-' + @FechaMes + '-' + @Fechaanio as smalldatetime)
   set @FechaHasta = Cast(@FechaDiaHasta + '-' + @FechaMesHasta + '-' + @FechaanioHasta as smalldatetime)



  /*Nomnbres de las Columnas que contienen a Moneda Base y Destino */
  set @INTRAMOVMONBASE='STDCOSTPEN' 
  set @INTRAMOVMONDEST='STDCOSTUSD'  
  set @KDXHISTMONBASE ='AVGPEN'
  set @KDXHISTMONDEST ='AVGUSD'
 
  /*Tipo de Movimientos*/
  SET @TIPOCOMPRAFUELS=(SELECT INTYPETRBUYFUELS FROM IN_SETUP) 
  SET @TIPOCOMPRASTORE=(SELECT INTYPETRBUYSTORE FROM IN_SETUP)
  SET @TIPOAJUSTEING=(SELECT INTYPEFISIN FROM IN_SETUP)
  SET @TIPOBONIFICACION=(SELECT INTYPEBONUS FROM IN_SETUP)
  SET @TIPOTRANSFERING=(SELECT INTYPETRANSFERIN FROM IN_SETUP) 
  SET @TIPOVENTAS =(SELECT INTYPESALES FROM IN_SETUP)
  SET @TIPOAJUSTESAL =(SELECT INTYPEFISOUT FROM IN_SETUP)
  SET @TIPONTACREDITO =(SELECT INTYPECREDMEM FROM IN_SETUP)
  SET @TIPOTRANSFERSAL =(SELECT INTYPETRANSFEROUT FROM IN_SETUP)

  /*Establecer los decimales */
  SET @ND_COSTOSETUP=(Select Floatcost from setup)
  SET @ND_STOCKSETUP=(Select Floatqty from setup)
  set @ND_COSTO=RTRIM(LTRIM(STR(@ND_COSTOSETUP)))
  set @ND_STOCK=RTRIM(LTRIM(STR(@ND_STOCKSETUP)))


  /* Para Crear la cadena de Filtros */
   if  @TODOSALAMACENES =1 and @TODOSCATEGORIAS =1   -- Todos los almacenes y todas las Categorias
       begin
           set  @CRITERIOSELECCION= " "
           set  @CRITERIOSELECCIONVAL= " "  
       end
   else
       begin
          if @TODOSALAMACENES =1 and @TODOSCATEGORIAS =0  -- Todos los Almacenes y solo algunas categorias
             begin
               set @CRITERIOSELECCION= " IN_INVENTORY.CLASSUBID in (" + RTRIM(LTRIM(@CadenaSubCategorias)) + ") and"
               set @CRITERIOSELECCIONVAL=@CRITERIOSELECCION 
             end
          else             
             begin
               if @TODOSALAMACENES =0 and @TODOSCATEGORIAS =1  -- Algunos Almacenes y Todas las Categorias
                  begin
                    set @CRITERIOSELECCION= " SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and "
                    set @CRITERIOSELECCIONVAL= " INTRAMOV.SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and "
                  end
               else 
                  begin
                    if @TODOSALAMACENES =0 and @TODOSCATEGORIAS =0  -- Algunos Almacenes y Algunas Categorias
                      begin 
                          set @CRITERIOSELECCION= " SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and IN_INVENTORY.CLASSUBID in (" +
                                                    RTRIM(LTRIM(@CadenaSubCategorias)) +") and "
                          set @CRITERIOSELECCIONVAL = " INTRAMOV.SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and IN_INVENTORY.CLASSUBID in (" +
                                                    RTRIM(LTRIM(@CadenaSubCategorias)) +") and "
                      end
                  end
             end
       end           
   
   
  SET @SALDOINICIAL= " SELECT in_kdxhist.siteid,IN_INVENTORY.CLASSUBID,in_kdxhist.INVTIDSKU,SUM(ROUND(QTYEND" + @FechaMesHistorica +
                     "," + @ND_STOCK + ")) AS [Inicial],0 as [QTYRESULT],0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL]," + 
                     "0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL]," +
                     "0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 as [TRANSFERVAL],0 as [OTROS]," +
                     "0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL],0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL]," +
                     "0 AS [OTROSSAL],0 as [AJUSTEINGVAL],0 as [OTROSINGVAL],0 as [VENTASVAL]," +
                     "0 as [AJUSTESALVAL],0 as [OTROSSALVAL]" +   
                     " from (in_kdxhist inner join IN_INVENTORY on IN_KDXHIST.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                     " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +
                     " WHERE " + @CRITERIOSELECCION + " yearhist=" +  RTRIM(LTRIM(STR(year(@FechaDesde)))) + 
                     " GROUP BY in_kdxhist.siteid,IN_INVENTORY.CLASSUBID,in_kdxhist.INVTIDSKU "  +
                     " UNION ALL " + 
                     " SELECT INTRAMOV.SITEID,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU,0 as [Inicial], " + 
                     " SUM(CASE SWTINOUT WHEN 1 THEN QTY WHEN 0 THEN ROUND((QTY * - 1)," + @ND_STOCK +") END) AS [QTYRESULT], " + 
                     " 0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL]," +
                     " 0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 AS [TRANSFERVAL]," + 
                     " 0 as [OTROS],0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL]," + 
                     " 0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL],0 AS [OTROSSAL],0 as [AJUSTEINGVAL]," +
                     " 0 as [OTROSINGVAL],0 as [VENTASVAL], 0 as [AJUSTESALVAL],0 as [OTROSSALVAL]" +           
                     " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU)" +
                     " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +
                     " where " + @CRITERIOSELECCION + " intrdateproce >='" + RTRIM(ltrim(Cast(@FechaInicial as char(100)))) +
                     "' AND intrdateproce < '" + 
                     RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' GROUP BY  INTRAMOV.SITEID,IN_INVENTORY.CLASSUBID," +
                     "INTRAMOV.INVTIDSKU " 
                     

   if @MONEDASOLICITADA=(select CURYORIG from SETUP)
     begin
       SET @MONEDAPROCESO=@INTRAMOVMONBASE
       SET @MONEDAKDXHIST=RTRIM(LTRIM(@KDXHISTMONBASE))
     end
   else   
     begin
       SET @MONEDAPROCESO=@INTRAMOVMONDEST
       SET @MONEDAKDXHIST=RTRIM(LTRIM(@KDXHISTMONDEST))
     end
     
  /*************************************************/
   
  SET @INGNOVAL = " UNION ALL SELECT siteid,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU,0 AS [Inicial],0 AS [QTYRESULT]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRAFUELS + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0  END) AS [COMPRASFUELS]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRAFUELS + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [COMPRASFUELSVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRASTORE + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0  END) AS [COMPRASSTORE]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOCOMPRASTORE + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [COMPRASSTOREVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOAJUSTEING + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [AJUSTEING]," +  
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOBONIFICACION + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [BONIF]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOBONIFICACION + " THEN ROUND(QTY," + @ND_STOCK + ") * " +
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [BONIFVAL]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERING + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [TRANSFER]," +
                  " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERING + " THEN ROUND(QTY," + @ND_STOCK + ") * " + 
                  " ROUND(" + @MONEDAPROCESO + "," + @ND_COSTO + ") ELSE 0 END) AS [TRASNFERVAL]," +	                   
                  " [OTROS]= SUM(CASE WHEN (TYPEDOCID <> " + @TIPOCOMPRAFUELS + " AND TYPEDOCID <> " + @TIPOCOMPRASTORE + 
                  " AND TYPEDOCID <> " + @TIPOAJUSTEING + " AND " +
                  " TYPEDOCID <> " + @TIPOBONIFICACION + " AND TYPEDOCID <> " + @TIPOTRANSFERING + " AND SWTINOUT=1)" +
                  " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END),0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL]," +
                  " 0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL],0 AS [OTROSSAL], 0 as [AJUSTEINGVAL],0 as [OTROSINGVAL]," +
                  " 0 as [VENTASVAL], 0 as [AJUSTESALVAL],0 as [OTROSSALVAL] " +                  
                  " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                  " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +                    
                  " where " + @CRITERIOSELECCION + " INTRDATEPROCE BETWEEN '" + 
                    RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) +
                  "' GROUP BY SITEID,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU " 
   
   SET @SALNOVAL = " UNION ALL SELECT siteid,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU,0 AS [Inicial],0 AS [QTYRESULT]," +
                   " 0 AS [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL],0 AS [AJUSTEING]," + 
                   " 0 AS [BONIF],0 AS [BONIFVAL],0 as [TRANSFER],0 AS [TRANSFERVAL],0 as [OTROS]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOVENTAS + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [VENTAS]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOAJUSTESAL + " THEN ROUND(QTY," + @ND_STOCK +") ELSE 0 END) AS [AJUSTESAL]," +  
                   " SUM(CASE TYPEDOCID WHEN " + @TIPONTACREDITO + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [NTACREDITO]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPONTACREDITO + " THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAPROCESO  + "," +
                   @ND_COSTO + ") ELSE 0 END) AS [NTACREDITOVAL]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERSAL + " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END) AS [TRANSFERSAL]," +
                   " SUM(CASE TYPEDOCID WHEN " + @TIPOTRANSFERSAL + " THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAPROCESO  + "," +
                   @ND_COSTO + ") ELSE 0 END) AS [TRANSFERSALVAL]," +
                   " [OTROSSAL]= SUM(CASE WHEN (TYPEDOCID <> " + @TIPOVENTAS + " AND TYPEDOCID <> " + @TIPOAJUSTESAL + " AND " +
                   " TYPEDOCID <> " + @TIPONTACREDITO + " AND TYPEDOCID <> " + @TIPOTRANSFERSAL + " AND SWTINOUT=0)" +
                   " THEN ROUND(QTY," + @ND_STOCK + ") ELSE 0 END),0 as [AJUSTEINGVAL],0 as [OTROSINGVAL],0 as [VENTASVAL]," +
                   " 0 as [AJUSTESALVAL],0 as [OTROSSALVAL] " +               
                   " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +
                   " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +                  
                   " where " + @CRITERIOSELECCION + " INTRDATEPROCE BETWEEN '" +
                     RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) + 
                   "' GROUP BY SITEID,IN_INVENTORY.CLASSUBID,INTRAMOV.INVTIDSKU "

  SET @INGSALVALORIZADA=" UNION ALL select intramov.siteid,in_inventory.CLASSUBID,in_inventory.INVTIDSKU,0 as [Inicial],0 as [QTYRESULT]," +
                        "0 as [COMPRASFUELS],0 AS [COMPRASFUELSVAL],0 AS [COMPRASSTORE],0 AS [COMPRASSTOREVAL],0 AS [AJUSTEING],0 AS [BONIF],0 AS [BONIFVAL],0 AS [TRANSFER]," +
                        "0 AS [TRANSFERVAL],0 AS [OTROS],0 AS [VENTAS],0 AS [AJUSTESAL],0 AS [NTACREDITO],0 AS [NTACREDITOVAL],0 AS [TRANSFERSAL],0 AS [TRANSFERSALVAL]," +
                        "0 AS [OTROSSAL],[AJUSTEINGVAL]=SUM(CASE WHEN TYPEDOCID=" + @TIPOAJUSTEING  + " THEN " + 			           		               
                        " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                        
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END)," +
      "[OTROSINGVAL]=SUM(CASE WHEN (TYPEDOCID <> " + @TIPOCOMPRAFUELS + " AND TYPEDOCID <> " + @TIPOCOMPRASTORE + " AND TYPEDOCID <> " + @TIPOAJUSTEING + " AND " +
                        " TYPEDOCID <> " + @TIPOBONIFICACION + " AND TYPEDOCID <> " + @TIPOTRANSFERING + " AND SWTINOUT=1) THEN " +
                        " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                       
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END)," +
       "[VENTASVAL]=SUM(CASE WHEN TYPEDOCID=" + @TIPOVENTAS + " THEN " + 
                        " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                       
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END)," +
       "[AJUSTESALVAL]=SUM(CASE WHEN TYPEDOCID=" + @TIPOAJUSTESAL + " THEN " +  
                      " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                         
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END)," +
       "[OTROSSALVAL]=SUM(CASE WHEN (TYPEDOCID <>" + @TIPOVENTAS + " AND TYPEDOCID <>" + @TIPOAJUSTESAL + " AND " +
                        " TYPEDOCID <>" + @TIPONTACREDITO + " AND TYPEDOCID <>" + @TIPOTRANSFERSAL + " AND SWTINOUT=0) THEN " +
                        " CASE WHEN MONTH(INTRDATEPROCE)=1 THEN  ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "01," + @ND_COSTO +") WHEN MONTH(INTRDATEPROCE)=2 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "02," + @ND_COSTO + ") " +                                         
                        " WHEN MONTH(INTRDATEPROCE)=3 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "03," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=4 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "04," + @ND_COSTO + ") " +		
                        " WHEN MONTH(INTRDATEPROCE)=5 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "05," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=6 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "06," + @ND_COSTO + ") " +         
    			" WHEN MONTH(INTRDATEPROCE)=7 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "07," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=8 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "08," + @ND_COSTO + ") " +                					  	 		  		          
         		" WHEN MONTH(INTRDATEPROCE)=9 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "09," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=10 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "10," + @ND_COSTO + ") " +            			
 			" WHEN MONTH(INTRDATEPROCE)=11 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "11," + @ND_COSTO + ") WHEN MONTH(INTRDATEPROCE)=12 THEN ROUND(QTY," + @ND_STOCK + ") * ROUND(" + @MONEDAKDXHIST + "12," + @ND_COSTO + ") " +            			             				    			 					  			
           		" END ELSE 0 END) " + 
        " from ((intramov inner join in_kdxhist " +
        " on intramov.INVTIDSKU=in_kdxhist.INVTIDSKU and intramov.siteid=in_kdxhist.siteid) inner join in_inventory " +
        " on intramov.INVTIDSKU=in_inventory.INVTIDSKU) inner join IN_SUBCATEGORY " + 
        " on in_inventory.CLASSUBID=IN_SUBCATEGORY.CLASSUBID  WHERE YEAR(INTRDATEPROCE)=YEARHIST and " + @CRITERIOSELECCIONVAL +
        " INTRDATEPROCE BETWEEN '" + RTRIM(ltrim(Cast(@FechaDesde as char(100)))) + "' AND '" + RTRIM(ltrim(Cast(@FechaHasta as char(100)))) +                              
        "' GROUP BY intramov.siteid,in_inventory.CLASSUBID,in_inventory.INVTIDSKU "
   
   /*Crear Tabla Temporal para Almacenar los resultados obtenidos de las cadenas @SALDOINICIAL + @INGNOVAL + @SALNOVAL + @INGSALVALORIZADA */
   /*y Crear un Resumen */

   Create Table #TMP_KARDEXGENERAL (SITEID CHAR(5),CLASSUBID CHAR(5),INVTIDSKU CHAR(20),INICIAL NUMERIC(10,3),QTYRESULT  NUMERIC(10,3),
                 COMPRASFUELS NUMERIC(10,3),COMPRASFUELSVAL NUMERIC(10,4),COMPRASSTORE NUMERIC(10,3),COMPRASSTOREVAL NUMERIC(10,4),
                 AJUSTEING NUMERIC(10,3),BONIF NUMERIC(10,3),BONIFVAL NUMERIC(10,4),TRANSFER NUMERIC(10,3),TRANSFERVAL NUMERIC(10,4),
                 OTROS NUMERIC(10,3),VENTAS NUMERIC(10,3),AJUSTESAL NUMERIC(10,3),NTACREDITO NUMERIC(10,3),NTACREDITOVAL NUMERIC(10,4),
                 TRANSFERSAL  NUMERIC(10,3),TRANSFERSALVAL NUMERIC(10,4),OTROSSAL NUMERIC(10,3),AJUSTEINGVAL NUMERIC(10,4),
                 OTROSINGVAL NUMERIC(10,4),VENTASVAL NUMERIC(10,4) ,AJUSTESALVAL NUMERIC(10,4) ,OTROSSALVAL NUMERIC(10,4)) 
   

   INSERT INTO #TMP_KARDEXGENERAL exec(@SALDOINICIAL + @INGNOVAL + @SALNOVAL + @INGSALVALORIZADA)
   
   SET @KARDEXGENERAL=" SELECT #TMP_KARDEXGENERAL.SITEID,SITE.NAME,#TMP_KARDEXGENERAL.CLASSUBID,IN_SUBCATEGORY.DESCR,#TMP_KARDEXGENERAL.INVTIDSKU," + 
                 " IN_INVENTORY.DESCR as [DESCRARTICULO],SUM(INICIAL+QTYRESULT)AS [INICIAL]," +               
                 " ROUND(SUM(INICIAL+QTYRESULT) * ROUND(" +  @MONEDAKDXHIST + @FechaMes + "," + @ND_COSTO + ")," + @ND_COSTO + ") as [INICIALVAL]," +                      
                 " SUM(COMPRASFUELS+COMPRASSTORE) AS [COMPRAS],SUM(AJUSTEING) AS [AJUSTEINGRESO]," +
                 " SUM(BONIF) AS [BONIFICACION],SUM(TRANSFER) AS [TRANSFERING],SUM(OTROS) AS [OTROSING],SUM(VENTAS) AS [VENTAS]," +
                 " SUM(AJUSTESAL) AS [AJUSTESAL],SUM(NTACREDITO) AS [NOTACRED],SUM(TRANSFERSAL) AS [TRANSFERSAL],SUM(OTROSSAL) AS [OTRASSAL]," + 
                 " SUM(COMPRASFUELSVAL+COMPRASSTOREVAL) AS [COMPRASVAL],SUM(AJUSTEINGVAL) AS [AJUSTEINGVALO],SUM(BONIFVAL) AS [BONIFIVAL]," +
                 " SUM(TRANSFERVAL) AS [TRANSINGVAL],SUM(OTROSINGVAL) AS [OTROSINGVAL],SUM(VENTASVAL) AS [VENTASVALOR]," +
                 " SUM(AJUSTESALVAL) AS [AJUSTESALIVAL],SUM(NTACREDITOVAL) AS [NOTACREDVAL],SUM(TRANSFERSALVAL) AS [TRANSFERSALVALOR]," +
                 " SUM(OTROSSALVAL) AS [OTROSSALVALOR] " + 
                 " FROM (((#TMP_KARDEXGENERAL INNER JOIN IN_KDXHIST ON (#TMP_KARDEXGENERAL.SITEID=IN_KDXHIST.SITEID AND " + 
                 " #TMP_KARDEXGENERAL.INVTIDSKU=IN_KDXHIST.INVTIDSKU) AND YEARHIST=" + RTRIM(LTRIM(STR(year(@FechaDesde)))) + 
		 " ) INNER JOIN SITE ON " +
                 " #TMP_KARDEXGENERAL.SITEID=SITE.SITEID) INNER JOIN IN_INVENTORY ON #TMP_KARDEXGENERAL.INVTIDSKU=" + 
                 " IN_INVENTORY.INVTIDSKU) INNER JOIN IN_SUBCATEGORY ON IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +                
                 " GROUP BY #TMP_KARDEXGENERAL.SITEID,SITE.NAME,#TMP_KARDEXGENERAL.CLASSUBID,IN_SUBCATEGORY.DESCR, " +
                 " #TMP_KARDEXGENERAL.INVTIDSKU,IN_INVENTORY.DESCR,IN_KDXHIST." +  @MONEDAKDXHIST + @FechaMes

   EXEC (@KARDEXGENERAL)
   DROP TABLE #TMP_KARDEXGENERAL
END
go
IF OBJECT_ID('IN_KARDEXGENERAL_SUBCATEG_VALOPROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_KARDEXGENERAL_SUBCATEG_VALOPROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_KARDEXGENERAL_SUBCATEG_VALOPROC >>>'
go


/* 
 * PROCEDURE: IN_LIMPIARTEMPORALES 
 */

CREATE PROCEDURE IN_LIMPIARTEMPORALES
@CORRNBR         INT
AS
  DECLARE @ERROR INTEGER    

  DELETE FROM TMP_IN_INVENTORYALTER WHERE CORRNBR=@CORRNBR
  DELETE FROM TMP_IN_INVENTORYDAT WHERE CORRNBR=@CORRNBR
  DELETE FROM TMP_IN_VENDORINVT WHERE CORRNBR=@CORRNBR

  SET @ERROR=@@error
    
  IF (@ERROR!=0)
   BEGIN       
      RETURN(1)    
   END

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


/* 
 * PROCEDURE: IN_LimpiarTempProvArtProc 
 */

CREATE PROCEDURE IN_LimpiarTempProvArtProc
(@CORRNBR int)
as
begin
  declare @ERROR INTEGER
  delete from TMP_IN_VENDORINVTREL where corrnbr=@CORRNBR
  SET @ERROR=@@error
    
  IF (@ERROR!=0)
   BEGIN       
      RETURN(1)    
   END

  RETURN(0)  
end 
/*********************************************************************************************************************************/
go
IF OBJECT_ID('IN_LimpiarTempProvArtProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_LimpiarTempProvArtProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_LimpiarTempProvArtProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleArticulosPorProveedorProc 
 */

CREATE Procedure IN_MostrarDetalleArticulosPorProveedorProc
(@VENDIDSS     UD_IDVENDOR)
AS
Begin   
    Select  in_inventory.INVTIDSKU,in_inventory.descr,BUYCOD as [Cd.Compra], STDCOST as Costo  
           from ((in_vendorinvt inner join in_vendor on in_vendorinvt.VENDIDSS=in_vendor.VENDIDSS) inner join 
           currency on in_vendorinvt.curyid=currency.curyid) inner join in_inventory on in_vendorinvt.INVTIDSKU=
           in_inventory.INVTIDSKU  where in_vendorinvt.VENDIDSS =@VENDIDSS
    return(0)
end

/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleArticulosPorProveedorProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleArticulosPorProveedorProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleArticulosPorProveedorProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleIN_INVENTORYALTERProc 
 */

Create Procedure IN_MostrarDetalleIN_INVENTORYALTERProc
(@INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
   Select INVTIDBARR as [Código Alternativo] from IN_INVENTORYALTER where INVTIDSKU =@INVTIDSKU
   RETURN(0)
END
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleIN_INVENTORYALTERProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleIN_INVENTORYALTERProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleIN_INVENTORYALTERProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleIN_INVENTORYDES 
 */

Create Procedure IN_MostrarDetalleIN_INVENTORYDES
(@INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
   select INVTIDSKUDES,in_inventorydes.WTSTK2 from in_inventorydes inner join in_inventory on 
          in_inventorydes.INVTIDSKU = in_inventory.INVTIDSKU 
          where in_inventory.INVTIDSKU=@INVTIDSKU
   RETURN(0) 
END
go
IF OBJECT_ID('IN_MostrarDetalleIN_INVENTORYDES') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleIN_INVENTORYDES >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleIN_INVENTORYDES >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleIN_INVENTORYDESProc 
 */

/*************************************************************************************************************************/
Create Procedure IN_MostrarDetalleIN_INVENTORYDESProc
(@INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
   select INVTIDSKUDES,in_inventorydes.WTSTK2 from in_inventorydes inner join in_inventory on 
          in_inventorydes.INVTIDSKU = in_inventory.INVTIDSKU 
          where in_inventory.INVTIDSKU=@INVTIDSKU
   RETURN(0) 
END
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleIN_INVENTORYDESProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleIN_INVENTORYDESProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleIN_INVENTORYDESProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleIN_INVENTORYVENDORINVTProc 
 */

Create Procedure IN_MostrarDetalleIN_INVENTORYVENDORINVTProc
(@INVTIDSKU       UD_IDCORPORATIVO)
AS
Begin   
    Select in_vendorinvt.VENDIDSS as [Cd],in_vendor.DESCR as [Proveedor],[Name] as Almacén,
           BUYCOD as [Cd.Compra],in_vendorinvt.descr as [Descripción],
           STDCOST as Costo,currency.curyid,currency.descr as Moneda,BUYDATE as [Ult.Compra] 
           from ((in_vendorinvt inner join in_vendor on in_vendorinvt.VENDIDSS=in_vendor.VENDIDSS) inner join 
           currency on in_vendorinvt.curyid=currency.curyid) inner join site on 
           in_vendorinvt.siteid=site.siteid 
           where INVTIDSKU =@INVTIDSKU
    return(0)
end
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleIN_INVENTORYVENDORINVTProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleIN_INVENTORYVENDORINVTProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleIN_INVENTORYVENDORINVTProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleIN_SOLINVENTORYProc 
 */

Create Procedure IN_MostrarDetalleIN_SOLINVENTORYProc
(@INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
   select INVTIDSTK,INVTIDNOSTK from 
          IN_SOLINVENTORY where INVTIDSKU=@INVTIDSKU  
   RETURN(0) 
END
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleIN_SOLINVENTORYProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleIN_SOLINVENTORYProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleIN_SOLINVENTORYProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleInventoryDatProc 
 */

Create Procedure IN_MostrarDetalleInventoryDatProc
(@INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
     Select in_inventorydat.siteid as [Cd],Name as [Almacén],
            StockMax as [Stk.Max],StockMin as [Stk.Min],sls_price as [P.Venta],
            in_inventorydat.PRECLVID,IN_PRICELEVELTYPE.DESCR as [Tipo_Precio],CURRENCY.DESCR as [Moneda],
            lastdayin as [Ult.Ingreso],lastdayou As [Ult.Salida],avgcostpen,avgcostusd,stdcostpen,stdcostusd,stockstar,STARTDATEINV,qtyavail,
            ENDDATEINV,lastinvfis,INVTIDSTK as [Cd_Inventariable],INVTIDNOSTK as [Cd_No_Inventariable] 
            from ((in_inventorydat inner join site on in_inventorydat.siteid=site.siteid) LEFT JOIN 
            IN_PRICELEVELTYPE on in_inventorydat.PRECLVID=IN_PRICELEVELTYPE.PRECLVID) LEFT join currency
            on  IN_PRICELEVELTYPE.curyid=currency.curyid
            where INVTIDSKU =@INVTIDSKU
    RETURN(0)
END 
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleInventoryDatProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleInventoryDatProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleInventoryDatProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleTempInventoryAlterProc 
 */

Create Procedure IN_MostrarDetalleTempInventoryAlterProc
(@INVTIDSKU       UD_IDCORPORATIVO,
 @CORRNBR         integer)
as
begin 
      Select INVTIDBARR as [Código Alternativo] from TMP_IN_INVENTORYALTER where 
             INVTIDSKU =@INVTIDSKU
              and corrnbr=@CORRNBR

      RETURN(0)
end
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleTempInventoryAlterProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleTempInventoryAlterProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleTempInventoryAlterProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleTempInventoryDatProc 
 */

Create Procedure IN_MostrarDetalleTempInventoryDatProc
(@INVTIDSKU       UD_IDCORPORATIVO,
 @CORRNBR         integer)
as 
begin
    Select tmp_in_inventorydat.siteid as [Cd],Name as [Almacén],
           StockMax as [Stk.Max],StockMin as [Stk.Min],sls_price as [P.Venta],
           tmp_in_inventorydat.PRECLVID,IN_PRICELEVELTYPE.DESCR as [Tipo_Precio],CURRENCY.DESCR as [Moneda],
           lastdayin as [Ult.Ingreso],lastdayou As [Ult.Salida],avgcostpen,avgcostusd,stdcostpen,
           stdcostusd,stockstar,STARTDATEINV,qtyavail,ENDDATEINV,lastinvfis,INVTIDSTK as [Cd_Inventariable],
           INVTIDNOSTK as [Cd_NoInventariable]  
           from ((tmp_in_inventorydat inner join site on tmp_in_inventorydat.siteid=site.siteid) left join 
                IN_PRICELEVELTYPE on tmp_in_inventorydat.PRECLVID=IN_PRICELEVELTYPE.PRECLVID) left join currency
                on  IN_PRICELEVELTYPE.curyid=currency.curyid
                where INVTIDSKU =@INVTIDSKU and corrnbr=@CORRNBR
     RETURN(0)
end
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleTempInventoryDatProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleTempInventoryDatProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleTempInventoryDatProc >>>'
go


/* 
 * PROCEDURE: IN_MostrarDetalleTEMPINVENTORYVENDORINVTProc 
 */

Create Procedure IN_MostrarDetalleTEMPINVENTORYVENDORINVTProc
(@INVTIDSKU       UD_IDCORPORATIVO,
 @CORRNBR         integer)
AS
Begin
     Select tmp_in_vendorinvt.VENDIDSS as [Cd],in_vendor.DESCR as [Proveedor],[NAME] as Almacén,BUYCOD as [Cd.Compra],
            TMP_in_vendorinvt.descr as [Descripción],STDCOST as Costo,currency.curyid,currency.descr as Moneda,
            BUYDATE as [Ult.Compra] 
     from ((TMP_in_vendorinvt inner join in_vendor on TMP_in_vendorinvt.VENDIDSS=in_vendor.VENDIDSS) inner join 
            currency on TMP_in_vendorinvt.curyid=currency.curyid) INNER JOIN SITE ON
            TMP_in_vendorinvt.SITEID=SITE.SITEID
            where INVTIDSKU =@INVTIDSKU AND 
            CORRNBR=@CORRNBR
     return(0)
end
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_MostrarDetalleTEMPINVENTORYVENDORINVTProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_MostrarDetalleTEMPINVENTORYVENDORINVTProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_MostrarDetalleTEMPINVENTORYVENDORINVTProc >>>'
go


/* 
 * PROCEDURE: IN_NoPertenecealosAlmacenesProc 
 */

Create Procedure IN_NoPertenecealosAlmacenesProc
(@INVTIDSKU       UD_IDCORPORATIVO)
as
  Select siteid,[name] from site where status=1 and siteid Not in( Select siteid from in_inventorydat 
                       where INVTIDSKU =@INVTIDSKU)

/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_NoPertenecealosAlmacenesProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_NoPertenecealosAlmacenesProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_NoPertenecealosAlmacenesProc >>>'
go


/* 
 * PROCEDURE: IN_ObtenerDescripcionaInventoryProc 
 */

Create Procedure IN_ObtenerDescripcionaInventoryProc
(@INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
   select descr from in_inventory where in_inventory.INVTIDSKU=@INVTIDSKU
   RETURN(0)
END
/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_ObtenerDescripcionaInventoryProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_ObtenerDescripcionaInventoryProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_ObtenerDescripcionaInventoryProc >>>'
go


/* 
 * PROCEDURE: IN_ObtenerSaldodelPeriodo 
 */

CREATE PROCEDURE IN_ObtenerSaldodelPeriodo
(@INVTIDSKU       UD_IDCORPORATIVO,
 @SITEID          UD_IDSITE)
AS
BEGIN
   DECLARE @MES  SMALLINT 
   DECLARE @ANIO SMALLINT

   SELECT @ANIO=YEAR(DATEPROCE),@MES=MONTH(DATEPROCE) FROM SETUP      
   SELECT CASE @MES -1 
           WHEN  0  THEN 
              QTYEND00  
           WHEN  1  THEN 
              QTYEND01
           WHEN  2  THEN 
              QTYEND02
 	   WHEN  3  THEN 	
              QTYEND03
           WHEN  4  THEN 
              QTYEND04  
           WHEN  5  THEN 
              QTYEND05
           WHEN  6  THEN 
              QTYEND06
 	   WHEN  7  THEN 	
              QTYEND07
           WHEN  8  THEN 
              QTYEND08
           WHEN  9  THEN 
              QTYEND09
 	   WHEN  10  THEN 	
              QTYEND10
       	   WHEN  11  THEN 	
              QTYEND11
       	   WHEN  12  THEN 	
              QTYEND12         
           END AS [SALDO]
    FROM  IN_KDXHIST                      
    WHERE YEARHIST=@ANIO AND SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU            
      
END
             
/*********************************************************************************************************************************/
go
IF OBJECT_ID('IN_ObtenerSaldodelPeriodo') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_ObtenerSaldodelPeriodo >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_ObtenerSaldodelPeriodo >>>'
go


/* 
 * PROCEDURE: IN_ObtieneCostosTotalesEnFormulasProc 
 */

CREATE PROCEDURE IN_ObtieneCostosTotalesEnFormulasProc
( @KITID	UD_KITID,
  @CORRNBR	INT,
  @TABLA	BIT		 --1 SI ES IN_KITCOMPONENT, 0  SI ES TMP_IN_KITCOMPONENT 
)
AS
BEGIN 

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)	
/*******************************************************************************************************/

	IF (@TABLA=1)
		BEGIN
			SELECT 	ROUND(SUM(STDCOSTPEN*QTY),@ND_COSTOSETUP) AS COSTOPEN ,SUM(STDCOSTUSD*QTY) AS COSTOUSD
			FROM 		IN_KITCOMPONENT
			WHERE 	IN_KITCOMPONENT.KITID=@KITID
		END
	ELSE
		BEGIN
			SELECT 	ROUND(SUM(STDCOSTPEN*QTY),@ND_COSTOSETUP) AS COSTOPEN ,SUM(STDCOSTUSD*QTY) AS COSTOUSD
			FROM 		TMP_IN_KITCOMPONENT
			WHERE 	TMP_IN_KITCOMPONENT.KITID=@KITID AND CORRNBR=@CORRNBR
		END

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


/* 
 * PROCEDURE: IN_ObtieneDetalleDeFormulaProc 
 */

CREATE PROCEDURE IN_ObtieneDetalleDeFormulaProc
( @KITID	UD_KITID
)
AS
BEGIN

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)	
/*******************************************************************************************************/


	SELECT 	IN_KITCOMPONENT.SEQUENCE AS Registro,IN_KITCOMPONENT.INVTIDSKU AS Codigo,IN_INVENTORY.DESCR AS Descripcion,
			IN_INVENTORY.STKUNITID AS Unidad,IN_KITCOMPONENT.QTY AS Cantidad,IN_KITCOMPONENT.STDCOSTPEN AS Costo_Soles,
			IN_KITCOMPONENT.STDCOSTUSD AS Costo_Dolares,ROUND((IN_KITCOMPONENT.QTY*IN_KITCOMPONENT.STDCOSTPEN),@ND_COSTOSETUP) as Total_Soles,
			ROUND((IN_KITCOMPONENT.QTY*IN_KITCOMPONENT.STDCOSTUSD),@ND_COSTOSETUP) as Total_Dolares
	FROM 		IN_KITCOMPONENT JOIN IN_INVENTORY
	ON		IN_KITCOMPONENT.INVTIDSKU=IN_INVENTORY.INVTIDSKU
	WHERE	IN_KITCOMPONENT.KITID=@KITID
	ORDER BY	IN_KITCOMPONENT.SEQUENCE ASC

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


/* 
 * PROCEDURE: IN_ObtieneSecuenciaProc 
 */

CREATE PROCEDURE IN_ObtieneSecuenciaProc
( @CORRNBR  int)
AS
BEGIN

    BEGIN TRAN
    DECLARE @VALOR INTEGER	
    SET  SELECT @VALOR = COUNT(*)+1 FROM TMP_IN_INTRAITEM WHERE CORRNBR=@CORRNBR
   

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'ERROR AL CONSULTAR TMP_IN_INTRAITEM'
        ROLLBACK TRAN
        RETURN(-1)
    END

    COMMIT TRAN
    RETURN(@VALOR)

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


/* 
 * PROCEDURE: IN_PertenecealosAlmacenesProc 
 */

Create Procedure IN_PertenecealosAlmacenesProc
(@INVTIDSKU       UD_IDCORPORATIVO)
as
  Select siteid,[name] from site where siteid in( Select siteid from in_inventorydat 
                       where INVTIDSKU =@INVTIDSKU)

/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_PertenecealosAlmacenesProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_PertenecealosAlmacenesProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_PertenecealosAlmacenesProc >>>'
go


/* 
 * PROCEDURE: IN_PRICELEVELTYPESelProc 
 */

CREATE PROCEDURE IN_PRICELEVELTYPESelProc
(
    @PRECLVID     UD_PRECLVID)
AS
BEGIN
       SELECT PRECLVID,In_PriceLevelType.DESCR,currency.curyid as [codcurr], currency.descr as [desccurr]
       FROM In_PriceLevelType inner join currency
        on In_PriceLevelType.curyid=currency.curyid
       WHERE PRECLVID = @PRECLVID

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


/* 
 * PROCEDURE: IN_PROVEEDOR_ARTICULO_PROC 
 */

CREATE PROCEDURE IN_PROVEEDOR_ARTICULO_PROC
( @VENDIDSS		UD_IDVENDOR,
  @CODIGO		UD_IDCORPORATIVO,
  @DISASSEMBLE		BIT
)
AS

	SELECT  IN_INVENTORY.INVTIDSKU,
		IN_INVENTORY.DESCR,
		IN_INVENTORYALTER.INVTIDBARR,
		IN_VENDORINVT.BUYCOD,
		IN_VENDORINVT.VENDIDSS,
		IN_VENDORINVT.CURYID,
		IN_VENDORINVT.STDCOST
	FROM 	IN_INVENTORY 
		LEFT JOIN IN_INVENTORYALTER
		ON IN_INVENTORY.INVTIDSKU=IN_INVENTORYALTER.INVTIDSKU 
		LEFT JOIN IN_VENDORINVT ON IN_INVENTORY.INVTIDSKU=IN_VENDORINVT.INVTIDSKU
        WHERE	IN_VENDORINVT.VENDIDSS=@VENDIDSS AND IN_INVENTORY.DISASSEMBLE=@DISASSEMBLE AND  
		(IN_INVENTORY.INVTIDSKU=@CODIGO OR IN_INVENTORYALTER.INVTIDBARR=@CODIGO OR IN_VENDORINVT.BUYCOD=@CODIGO)
go
IF OBJECT_ID('IN_PROVEEDOR_ARTICULO_PROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_PROVEEDOR_ARTICULO_PROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_PROVEEDOR_ARTICULO_PROC >>>'
go


/* 
 * PROCEDURE: IN_REPORTEDIFERENCIAPROC 
 */

Create Procedure IN_REPORTEDIFERENCIAPROC 
@CATEGOSUBCATEG char(3),   -- CAT es por Categoría/SUB es por SubCategoría
@CadenaCategorias varchar(8000),
@CadenaSubCategorias varchar(8000),
@TODOSCATEGORIAS TINYINT,    -- Si es 1 son todas las Categorias 
@TODOSSUBCATEGORIAS TINYINT,  -- Si es 1 son todas las SubCategorías   
@DIERENTEDECERO TINYINT ------- Si es 1 es con el campo [Diferencia] tiene que ser diferente de cero
as
BEGIN
/*Declarar variables para Tipos de Movimientos*/
  DECLARE @TIPOVENTAS UD_TYPEDOC 
  DECLARE @TIPODEVOLU UD_TYPEDOC
  DECLARE @SOURCESITE UD_IDSITE 
/*Declarar Variables para Estabalecer Decimales*/
  DECLARE @ND_COSTOSETUP Tinyint
  DECLARE @ND_STOCKSETUP Tinyint
  DECLARE @ND_COSTO CHAR(1)
  DECLARE @ND_STOCK CHAR(1) 
/*Declarar Variable para Almacenar la Fecha de la Toma de Iventario */
  DECLARE @FECHADEINVENTARIO smalldatetime
  DECLARE @ALMACENORIGEN UD_IDSITE
/*Declarar Variable para armar Cadenas */
  DECLARE @CADENATEMP1 Varchar(8000)
  DECLARE @CADENATEMP2 Varchar(8000)
  DECLARE @CADENATEMP3 Varchar(8000)
  DECLARE @CADENATEMP4 Varchar(8000)
/*Cadena de Selección*******************/

  DECLARE @CRITERIOSELECCION varchar(8000)
  DECLARE @CRITERIOSELECCION2 varchar(8000)
  DECLARE @CRITERIOSELECCION3 varchar(8000)
/**********************************************/
   
  Select @TIPOVENTAS=INTYPESALES from in_setup 
  Select @TIPODEVOLU=INTYPEBONUS from in_setup 
  Select @ALMACENORIGEN=SOURCESITE from setup
  Select @FECHADEINVENTARIO=STARTDATEINVEN from site where siteid = @ALMACENORIGEN
  Select @ND_COSTOSETUP=Floatcost from setup
  Select @ND_STOCKSETUP=Floatqty from setup
  set @ND_COSTO=RTRIM(LTRIM(STR(@ND_COSTOSETUP)))
  set @ND_STOCK=RTRIM(LTRIM(STR(@ND_STOCKSETUP)))
  
  /*Genera las Columnas de Stock Inicial,Ingresos,Salidas,Ventas y Devolu agrupada por Almacen ,Categoría y Categoría */
  /*Generar un Join entre las Vista VWREPORTDIFCATEG (IN_INVENTORY CON IN_INVENTORYDAT) y INTRAMOV*/ 
  
  if @CATEGOSUBCATEG='CAT'
    BEGIN
      /*Armar Criterio de Seleccion*/

      if @TODOSCATEGORIAS=1 
        begin
         set @CRITERIOSELECCION =" "       
         set @CRITERIOSELECCION2=" "
         set @CRITERIOSELECCION3=" "
           -- Preguntar si solo se quiere mostrar los saldos diferentes de cero
              if @DIERENTEDECERO= 1 
                 set @CRITERIOSELECCION3=" where stkteorico-STOCKPHISICAL <> 0 "                            
        end
      else
        begin 
          set @CRITERIOSELECCION= " AND CLASSID IN (" + @CadenaCategorias + ")" 
          set @CRITERIOSELECCION2= " AND VWREPORTDIFCATEG.CLASSID IN (" + @CadenaCategorias + ")" 
          set @CRITERIOSELECCION3= " where IN_INVENTPHISICAL.CLASSID IN (" + @CadenaCategorias + ")" 
            -- Preguntar si solo se quiere mostrar los saldos diferentes de cero 
             if @DIERENTEDECERO= 1 
                set @CRITERIOSELECCION3=@CRITERIOSELECCION3 + " and (stkteorico-STOCKPHISICAL) <> 0 "
        end 



      SET @CADENATEMP1 = " SELECT VWREPORTDIFCATEG.SITEID,VWREPORTDIFCATEG.INVTIDSKU,CLASSID,VWREPORTDIFCATEG.ENDDATEINV, " +
                         " [INGRESOS]= SUM (CASE WHEN TYPEDOCID <> " + @TIPODEVOLU + " AND SWTINOUT=1 THEN ROUND(QTY," + @ND_STOCK + 
                         " ) ELSE 0 END)," +
                         " [SALIDAS]=SUM (CASE WHEN TYPEDOCID <> " + @TIPOVENTAS + " AND SWTINOUT=0 THEN ROUND(QTY," + @ND_STOCK + 
                         " ) ELSE 0 END)," +
                         " [VENTAS]=SUM (CASE WHEN TYPEDOCID = " + @TIPOVENTAS + " AND SWTINOUT=0 THEN ROUND(QTY," + @ND_STOCK + 
                         " ) ELSE 0 END)," +
                         " [DEVOLU]=SUM(CASE WHEN TYPEDOCID= " + @TIPODEVOLU + " AND SWTINOUT=1 THEN ROUND(QTY," + @ND_STOCK+ 
                         " ) ELSE 0 END) " + 
                         " FROM INTRAMOV RIGHT JOIN VWREPORTDIFCATEG ON VWREPORTDIFCATEG.SITEID=INTRAMOV.SITEID AND " +
                         " VWREPORTDIFCATEG.INVTIDSKU=INTRAMOV.INVTIDSKU " +
                         " WHERE INTRDATEPROCE BETWEEN ENDDATEINV AND '" + RTRIM(ltrim(Cast(@FECHADEINVENTARIO as char(100))))
                         + "' AND STKFISI=0 AND VWREPORTDIFCATEG.SITEID='" + 
                         @ALMACENORIGEN + "' " + @CRITERIOSELECCION +  
                         " GROUP BY VWREPORTDIFCATEG.SITEID,VWREPORTDIFCATEG.INVTIDSKU,CLASSID,ENDDATEINV"
    
     CREATE TABLE #TMPREPORTEDIF0 (SITEID CHAR(5),INVTIDSKU CHAR(20),CLASSID CHAR(5),ENDDATEINV SMALLDATETIME,
                                   INGRESOS NUMERIC(10,3),SALIDAS NUMERIC(10,3),
                                   VENTAS NUMERIC(10,3),DEVOLU NUMERIC(10,3)) 
   
     INSERT INTO #TMPREPORTEDIF0 EXEC (@CADENATEMP1) 

   /************************************************************************************************************************************/
     set @CADENATEMP4=" SELECT  VWREPORTDIFCATEG.SITEID,VWREPORTDIFCATEG.INVTIDSKU,VWREPORTDIFCATEG.CLASSID," +
                      " VWREPORTDIFCATEG.ENDDATEINV,INGRESOS,SALIDAS,VENTAS,DEVOLU " +
                      " FROM #TMPREPORTEDIF0 RIGHT JOIN VWREPORTDIFCATEG ON " +
                      " VWREPORTDIFCATEG.SITEID=#TMPREPORTEDIF0.SITEID AND " +
                      " VWREPORTDIFCATEG.INVTIDSKU=#TMPREPORTEDIF0.INVTIDSKU " +
                      " where VWREPORTDIFCATEG.SITEID ='" +  @ALMACENORIGEN + "' " + @CRITERIOSELECCION2
 
     CREATE TABLE #TMPREPORTEDIF (SITEID CHAR(5),INVTIDSKU CHAR(20),CLASSID CHAR(5),ENDDATEINV SMALLDATETIME,INGRESOS NUMERIC(10,3),SALIDAS NUMERIC(10,3),
                                  VENTAS NUMERIC(10,3),DEVOLU NUMERIC(10,3)) 
     INSERT INTO #TMPREPORTEDIF EXEC (@CADENATEMP4)               
   
   /*Obtener el Stock Teórico(ingresos + Devolu - salidas- ventas),el costo promedio,y la fecha del ultimo inventario de cadaartículo*/

     SET @CADENATEMP2=" SELECT VWREPORTDIFCATEG.SITEID,VWREPORTDIFCATEG.INVTIDSKU,VWREPORTDIFCATEG.CLASSID,VWREPORTDIFCATEG.ENDDATEINV," +
                      " ROUND(VWREPORTDIFCATEG.LASTINVFIS," + @ND_STOCK + ") AS [StockInicial]," + 
                      " ROUND(VWREPORTDIFCATEG.AVGCOSTPEN," + @ND_COSTO + ") AS [CostoProm]," +
                      " ROUND(INGRESOS," + @ND_STOCK + ") as [Ingresos],ROUND(SALIDAS," + @ND_STOCK + ") as [Salidas]," +
                      " ROUND(VENTAS," + @ND_STOCK + ") as [Ventas]," + 
                      " ROUND(DEVOLU," + @ND_STOCK + ") as [Devolu]," + 
                      " (ROUND(ISNULL(VWREPORTDIFCATEG.LASTINVFIS,0)," + @ND_STOCK + ") + ROUND(ISNULL(INGRESOS,0)," + @ND_STOCK + 
                      ") + ROUND(ISNULL(DEVOLU,0)," + @ND_STOCK + ") - ROUND(ISNULL(SALIDAS,0)," + @ND_STOCK + ")- " +
                      " ROUND(ISNULL(VENTAS,0)," + @ND_STOCK + ")) AS [STOCKTEORICO]" +
                      " FROM (VWREPORTDIFCATEG INNER JOIN #TMPREPORTEDIF ON  VWREPORTDIFCATEG.SITEID=#TMPREPORTEDIF.SITEID AND " +
                      " VWREPORTDIFCATEG.INVTIDSKU=#TMPREPORTEDIF.INVTIDSKU) "        

     CREATE TABLE #TMPREPORTEDIF2 (SITEID CHAR(5),INVTIDSKU CHAR(20),CLASSID CHAR(5),FECHAULTINV SMALLDATETIME,STOCKINI NUMERIC(10,3),
                        COSTOPROM NUMERIC(8,4),INGRESOS NUMERIC(10,3),SALIDAS NUMERIC(10,3),VENTAS NUMERIC(10,3),
                        DEVOLU NUMERIC(10,3),stkteorico NUMERIC(10,3)) 
   
     INSERT INTO #TMPREPORTEDIF2 EXEC (@CADENATEMP2) 

    /*Realizar un Right Join para que puedan salir Todos los articulos de IN_INVENTPHISICAL (aunque no tenga movimientos(intramov))
      y no figure en el in_inventorydat */   
         
     set @CADENATEMP3 = " SELECT CASE WHEN isnull(FECHAULTINV,'')='' THEN NULL ELSE FECHAULTINV END AS [FECHAINV]," +
                     " IN_INVENTPHISICAL.SITEID,NAME,IN_INVENTPHISICAL.CLASSID,IN_CATEGORY.DESCR," +
                     " IN_INVENTPHISICAL.INVTIDSKU,IN_INVENTORY.DESCR AS [DESCRARTICULO],isnull(FECHAULTINV,''),isnull(STOCKINI,0) as [StockIni]," +
                     " isnull(Ingresos,0) as [Ingresos],isnull(Salidas,0) as [Salidas],isnull(Ventas,0) as [Ventas], " +
                     " isnull(Devolu,0) as Devolu,isnull(stkteorico,0) as [Teorico],isnull(STOCKPHISICAL,0) as [Fisico], " +
                     " isnull((stkteorico-STOCKPHISICAL),0) as [Diferencia],isnull(CostoProm,0) as [CostoProm], " +
                     " round((isnull(round(stkteorico," + @ND_STOCK + "),0)- isnull(round(STOCKPHISICAL," + @ND_STOCK + "),0)) * " +
                     " isnull(round(CostoProm," + @ND_COSTO + "),0)," + @ND_STOCK + ") as [Total]" +                                		           		                                                       
                     " FROM  (((#TMPREPORTEDIF2 RIGHT JOIN IN_INVENTPHISICAL ON " +
                     " #TMPREPORTEDIF2.SITEID=IN_INVENTPHISICAL.SITEID AND " +
                     " #TMPREPORTEDIF2.INVTIDSKU=IN_INVENTPHISICAL.INVTIDSKU) left join " +
                     " site on IN_INVENTPHISICAL.SITEID=SITE.SITEID) LEFT JOIN IN_CATEGORY " +
                     " on IN_INVENTPHISICAL.CLASSID=IN_CATEGORY.CLASSID) LEFT JOIN IN_INVENTORY " +  
                     " on IN_INVENTPHISICAL.INVTIDSKU=IN_INVENTORY.INVTIDSKU " + @CRITERIOSELECCION3 
                                           
      exec (@CADENATEMP3)                 
  END
  ELSE
    BEGIN 
       /******************************************************************************************************************************/
       --Inicio de Reporte por SubCategoría   
       /*Armar Criterio de Seleccion*/
       if @TODOSSUBCATEGORIAS=1 
          begin
            set @CRITERIOSELECCION =" "       
            set @CRITERIOSELECCION2=" "
            set @CRITERIOSELECCION3=" "
               -- Preguntar si solo se quiere mostrar los saldos diferentes de cero
                  if @DIERENTEDECERO= 1 
                     set @CRITERIOSELECCION3=" where stkteorico-STOCKPHISICAL <> 0 "                            
          end
       else
          begin 
            set @CRITERIOSELECCION= " AND CLASSUBID IN (" + @CadenaSubCategorias + ")" 
            set @CRITERIOSELECCION2= " AND VWREPORTDIFSUBCATEG.CLASSUBID IN (" + @CadenaSubCategorias + ")" 
            set @CRITERIOSELECCION3= " where IN_INVENTPHISICAL.CLASSUBID IN (" + @CadenaSubCategorias + ")" 
                -- Preguntar si solo se quiere mostrar los saldos diferentes de cero 
                  if @DIERENTEDECERO= 1 
                    set @CRITERIOSELECCION3=@CRITERIOSELECCION3 + " and (stkteorico-STOCKPHISICAL) <> 0 "
           end 

       SET @CADENATEMP1 = " SELECT VWREPORTDIFSUBCATEG.SITEID,VWREPORTDIFSUBCATEG.INVTIDSKU,CLASSUBID,VWREPORTDIFSUBCATEG.ENDDATEINV, " +
                          " [INGRESOS]= SUM (CASE WHEN TYPEDOCID <> " + @TIPODEVOLU + " AND SWTINOUT=1 THEN ROUND(QTY," + @ND_STOCK + 
                          " ) ELSE 0 END)," +
                          " [SALIDAS]=SUM (CASE WHEN TYPEDOCID <> " + @TIPOVENTAS + " AND SWTINOUT=0 THEN ROUND(QTY," + @ND_STOCK + 
                          " ) ELSE 0 END)," +
                          " [VENTAS]=SUM (CASE WHEN TYPEDOCID = " + @TIPOVENTAS + " AND SWTINOUT=0 THEN ROUND(QTY," + @ND_STOCK + 
                          " ) ELSE 0 END)," +
                          " [DEVOLU]=SUM(CASE WHEN TYPEDOCID= " + @TIPODEVOLU + " AND SWTINOUT=1 THEN ROUND(QTY," + @ND_STOCK+ 
                          " ) ELSE 0 END) " + 
                          " FROM INTRAMOV RIGHT JOIN VWREPORTDIFSUBCATEG ON VWREPORTDIFSUBCATEG.SITEID=INTRAMOV.SITEID AND " +
                          " VWREPORTDIFSUBCATEG.INVTIDSKU=INTRAMOV.INVTIDSKU " +
                          " WHERE INTRDATEPROCE BETWEEN ENDDATEINV AND '" + RTRIM(ltrim(Cast(@FECHADEINVENTARIO as char(100))))
                          + "' AND STKFISI=0  AND  VWREPORTDIFSUBCATEG.SITEID='" + 
                          @ALMACENORIGEN + "' " + @CRITERIOSELECCION +  
                          " GROUP BY VWREPORTDIFSUBCATEG.SITEID,VWREPORTDIFSUBCATEG.INVTIDSKU,CLASSUBID,ENDDATEINV"
    
        CREATE TABLE #TMPREPORTEDIF0SB (SITEID CHAR(5),INVTIDSKU CHAR(20),CLASSUBID CHAR(5),ENDDATEINV SMALLDATETIME,
                                      INGRESOS NUMERIC(10,3),SALIDAS NUMERIC(10,3),
                                      VENTAS NUMERIC(10,3),DEVOLU NUMERIC(10,3)) 
   
        INSERT INTO #TMPREPORTEDIF0SB EXEC (@CADENATEMP1) 

     /************************************************************************************************************************************/
      SET @CADENATEMP4=" SELECT VWREPORTDIFSUBCATEG.SITEID,VWREPORTDIFSUBCATEG.INVTIDSKU,VWREPORTDIFSUBCATEG.CLASSUBID," +
                       " VWREPORTDIFSUBCATEG.ENDDATEINV,INGRESOS,SALIDAS,VENTAS,DEVOLU " +
                       " FROM #TMPREPORTEDIF0SB RIGHT JOIN VWREPORTDIFSUBCATEG ON " +
                       " VWREPORTDIFSUBCATEG.SITEID=#TMPREPORTEDIF0SB.SITEID AND " +
                       " VWREPORTDIFSUBCATEG.INVTIDSKU=#TMPREPORTEDIF0SB.INVTIDSKU " +
                       " where VWREPORTDIFSUBCATEG.SITEID ='" +  @ALMACENORIGEN + "' " + @CRITERIOSELECCION2
  
      CREATE TABLE #TMPREPORTEDIFSB (SITEID CHAR(5),INVTIDSKU CHAR(20),CLASSUBID CHAR(5),ENDDATEINV SMALLDATETIME,INGRESOS NUMERIC(10,3),SALIDAS NUMERIC(10,3),
                                     VENTAS NUMERIC(10,3),DEVOLU NUMERIC(10,3)) 
      INSERT INTO #TMPREPORTEDIFSB EXEC (@CADENATEMP4)               
    
      /*Obtener el Stock Teórico(ingresos + Devolu - salidas- ventas),el costo promedio,y la fecha del ultimo inventario de cadaartículo*/

      SET @CADENATEMP2=" SELECT VWREPORTDIFSUBCATEG.SITEID,VWREPORTDIFSUBCATEG.INVTIDSKU,VWREPORTDIFSUBCATEG.CLASSUBID,VWREPORTDIFSUBCATEG.ENDDATEINV," +
                       " ROUND(VWREPORTDIFSUBCATEG.LASTINVFIS," + @ND_STOCK + ") AS [StockInicial]," + 
                       " ROUND(VWREPORTDIFSUBCATEG.AVGCOSTPEN," + @ND_COSTO + ") AS [CostoProm]," +
                       " ROUND(INGRESOS," + @ND_STOCK + ") as [Ingresos],ROUND(SALIDAS," + @ND_STOCK + ") as [Salidas]," +
                       " ROUND(VENTAS," + @ND_STOCK + ") as [Ventas]," + 
                       " ROUND(DEVOLU," + @ND_STOCK + ") as [Devolu]," + 
                       " (ROUND(ISNULL(VWREPORTDIFSUBCATEG.LASTINVFIS,0)," + @ND_STOCK + ") + ROUND(ISNULL(INGRESOS,0)," + @ND_STOCK + 
                       ") + ROUND(ISNULL(DEVOLU,0)," + @ND_STOCK + ") - ROUND(ISNULL(SALIDAS,0)," + @ND_STOCK + ")- " +
                       " ROUND(ISNULL(VENTAS,0)," + @ND_STOCK + ")) AS [STOCKTEORICO]" +
                       " FROM (VWREPORTDIFSUBCATEG INNER JOIN #TMPREPORTEDIFSB ON  VWREPORTDIFSUBCATEG.SITEID=#TMPREPORTEDIFSB.SITEID AND " +
                       " VWREPORTDIFSUBCATEG.INVTIDSKU=#TMPREPORTEDIFSB.INVTIDSKU) "        

      CREATE TABLE #TMPREPORTEDIFSB2 (SITEID CHAR(5),INVTIDSKU CHAR(20),CLASSUBID CHAR(5),FECHAULTINV SMALLDATETIME,STOCKINI NUMERIC(10,3),
                       COSTOPROM NUMERIC(8,4),INGRESOS NUMERIC(10,3),SALIDAS NUMERIC(10,3),VENTAS NUMERIC(10,3),
                       DEVOLU NUMERIC(10,3),stkteorico NUMERIC(10,3)) 
   
      INSERT INTO #TMPREPORTEDIFSB2 EXEC (@CADENATEMP2) 

     /*Realizar un Right Join para que puedan salir Todos los articulos de IN_INVENTPHISICAL (aunque no tenga movimientos(intramov))
       y no figure en el in_inventorydat */   
         
      set @CADENATEMP3 = " SELECT CASE WHEN isnull(FECHAULTINV,'')='' THEN NULL ELSE FECHAULTINV END AS [FECHAINV], " +
                         " IN_INVENTPHISICAL.SITEID,NAME,IN_INVENTPHISICAL.CLASSUBID,IN_SUBCATEGORY.DESCR," +
                         " IN_INVENTPHISICAL.INVTIDSKU,IN_INVENTORY.DESCR AS [DESCRARTICULO],isnull(FECHAULTINV,''),isnull(STOCKINI,0) as [StockIni]," +
                         " isnull(Ingresos,0) as [Ingresos],isnull(Salidas,0) as [Salidas],isnull(Ventas,0) as [Ventas], " +
                         " isnull(Devolu,0) as Devolu,isnull(stkteorico,0) as [Teorico],isnull(STOCKPHISICAL,0) as [Fisico], " +
                         " isnull((stkteorico-STOCKPHISICAL),0) as [Diferencia],isnull(CostoProm,0) as [CostoProm], " +
                         " round((isnull(round(stkteorico," + @ND_STOCK + "),0)- isnull(round(STOCKPHISICAL," + @ND_STOCK + "),0)) * " +
                         " isnull(round(CostoProm," + @ND_COSTO + "),0)," + @ND_STOCK + ") as [Total]" +                                		           		                                                       
                         " FROM  (((#TMPREPORTEDIFSB2 RIGHT JOIN IN_INVENTPHISICAL ON " +
                         " #TMPREPORTEDIFSB2.SITEID=IN_INVENTPHISICAL.SITEID AND " +
                         " #TMPREPORTEDIFSB2.INVTIDSKU=IN_INVENTPHISICAL.INVTIDSKU) left join " +
                         " site on IN_INVENTPHISICAL.SITEID=SITE.SITEID) LEFT JOIN IN_SUBCATEGORY " +
                         " on IN_INVENTPHISICAL.CLASSUBID=IN_SUBCATEGORY.CLASSUBID) LEFT JOIN IN_INVENTORY " +  
                         " on IN_INVENTPHISICAL.INVTIDSKU=IN_INVENTORY.INVTIDSKU " + @CRITERIOSELECCION3 
                                           
       exec (@CADENATEMP3)
       --Fin de Reporte por SubCategoría
    END
End
go
IF OBJECT_ID('IN_REPORTEDIFERENCIAPROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_REPORTEDIFERENCIAPROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_REPORTEDIFERENCIAPROC >>>'
go


/* 
 * PROCEDURE: IN_REPORTEDIFERENCIARESUMIDOPROC 
 */

Create Procedure IN_REPORTEDIFERENCIARESUMIDOPROC 
@CATEGOSUBCATEG char(3),   -- CAT es por Categoría/SUB es por SubCategoría
@CadenaCategorias varchar(8000),
@CadenaSubCategorias varchar(8000),
@TODOSCATEGORIAS TINYINT,    -- Si es 1 son todas las Categorias 
@TODOSSUBCATEGORIAS TINYINT,  -- Si es 1 son todas las SubCategorías   
@DIERENTEDECERO TINYINT ------- Si es 1 es con el campo [Diferencia] tiene que ser diferente de cero
as
BEGIN
/*Declarar Variables para Estabalecer Decimales*/
  DECLARE @ND_COSTOSETUP Tinyint
  DECLARE @ND_STOCKSETUP Tinyint
  DECLARE @ND_COSTO CHAR(1)
  DECLARE @ND_STOCK CHAR(1) 
/*Declarar Variable para armar Cadenas */
  DECLARE @CADENATEMP1 Varchar(8000)
  DECLARE @ALMACENORIGEN UD_IDSITE
/*Cadena de Selección*******************/
  DECLARE @CRITERIOSELECCION varchar(8000)
/**********************************************/
  Select @ALMACENORIGEN=SOURCESITE from setup
  Select @ND_COSTOSETUP=Floatcost from setup
  Select @ND_STOCKSETUP=Floatqty from setup
  set @ND_COSTO=RTRIM(LTRIM(STR(@ND_COSTOSETUP)))
  set @ND_STOCK=RTRIM(LTRIM(STR(@ND_STOCKSETUP)))
  
  /*Genera las Columnas de Stock Inicial,Ingresos,Salidas,Ventas y Devolu agrupada por Almacen ,Categoría y Categoría */
  /*Generar un Join entre las Vista VWREPORTDIFCATEG (IN_INVENTORY CON IN_INVENTORYDAT) e INTRAMOV*/ 
  
  if @CATEGOSUBCATEG='CAT'
    BEGIN
      /*Armar Criterio de Seleccion*/

      if @TODOSCATEGORIAS=1 
        begin
         set @CRITERIOSELECCION=" where IN_INVENTPHISICAL.SITEID='" + @ALMACENORIGEN + "'"
           -- Preguntar si solo se quiere mostrar los saldos diferentes de cero
              if @DIERENTEDECERO= 1 
                 set @CRITERIOSELECCION=@CRITERIOSELECCION +  " and isnull(QTYAVAIL,0)-STOCKPHISICAL <> 0 "                            
        end
      else
        begin 
          set @CRITERIOSELECCION= " where IN_INVENTPHISICAL.CLASSID IN (" + @CadenaCategorias + ") and " +
                                   " IN_INVENTPHISICAL.SITEID='" + @ALMACENORIGEN + "'"  
            -- Preguntar si solo se quiere mostrar los saldos diferentes de cero 
             if @DIERENTEDECERO= 1 
                set @CRITERIOSELECCION=@CRITERIOSELECCION + " and (isnull(QTYAVAIL,0)-STOCKPHISICAL) <> 0 "
        end 
 
     set @CADENATEMP1 = " SELECT CASE WHEN isnull(ENDDATEINV,'')='' THEN NULL ELSE ENDDATEINV END AS [FECHAINV]," +
                     " IN_INVENTPHISICAL.SITEID,NAME,IN_INVENTPHISICAL.CLASSID,IN_CATEGORY.DESCR," +
                     " IN_INVENTPHISICAL.INVTIDSKU,IN_INVENTORY.DESCR AS [DESCRARTICULO],isnull(QTYAVAIL,0) as [Teorico]," +
                     " isnull(STOCKPHISICAL,0) as [Fisico]," +
                     " round(round(isnull(QTYAVAIL,0)," + @ND_STOCK + ")- round(isnull(STOCKPHISICAL,0)," + 
                       @ND_STOCK + ")," + @ND_STOCK + ") as [Diferencia]," +
                     " isnull(AVGCOSTPEN,0) as [CostoProm], " +
                     " round((isnull(round(QTYAVAIL," + @ND_STOCK + "),0)- isnull(round(STOCKPHISICAL," + @ND_STOCK + "),0)) * " +
                     " isnull(round(AVGCOSTPEN," + @ND_COSTO + "),0)," + @ND_STOCK + ") as [Total]" +                                		           		                                                       
                     " FROM  (((VWREPORTDIFCATEGRESUMIDO RIGHT JOIN IN_INVENTPHISICAL ON " +
                     " VWREPORTDIFCATEGRESUMIDO.SITEID=IN_INVENTPHISICAL.SITEID AND " +
                     " VWREPORTDIFCATEGRESUMIDO.INVTIDSKU=IN_INVENTPHISICAL.INVTIDSKU) left join " +
                     " site on IN_INVENTPHISICAL.SITEID=SITE.SITEID) LEFT JOIN IN_CATEGORY " +
                     " on IN_INVENTPHISICAL.CLASSID=IN_CATEGORY.CLASSID) LEFT JOIN IN_INVENTORY " +  
                     " on IN_INVENTPHISICAL.INVTIDSKU=IN_INVENTORY.INVTIDSKU " + @CRITERIOSELECCION      
     exec (@CADENATEMP1) 
                

  END
  ELSE
    BEGIN 
      /******************************************************************************************************************************/
      --Inicio de Reporte por SubCategoría   
      /*Armar Criterio de Seleccion*/
    if @TODOSSUBCATEGORIAS=1     
      begin
         set @CRITERIOSELECCION=" where IN_INVENTPHISICAL.SITEID='" + @ALMACENORIGEN + "'"
           -- Preguntar si solo se quiere mostrar los saldos diferentes de cero
              if @DIERENTEDECERO= 1 
                 set @CRITERIOSELECCION=@CRITERIOSELECCION +  " and isnull(QTYAVAIL,0)-STOCKPHISICAL <> 0 "                            
        end
      else
        begin 
          set @CRITERIOSELECCION= " where IN_INVENTPHISICAL.CLASSUBID IN (" + @CadenaSubCategorias + ") and " +
                                  " IN_INVENTPHISICAL.SITEID='" + @ALMACENORIGEN + "'"  
            -- Preguntar si solo se quiere mostrar los saldos diferentes de cero 
             if @DIERENTEDECERO= 1 
                set @CRITERIOSELECCION=@CRITERIOSELECCION + " and (isnull(QTYAVAIL,0)-STOCKPHISICAL) <> 0 "
      end 

     /*Realizar un Right Join para que puedan salir Todos los articulos de IN_INVENTPHISICAL (aunque no tenga movimientos(intramov))
       y no figure en el in_inventorydat */   
     set @CADENATEMP1 = " SELECT CASE WHEN isnull(ENDDATEINV,'')='' THEN NULL ELSE ENDDATEINV END AS [FECHAINV]," +
                        " IN_INVENTPHISICAL.SITEID,NAME,IN_INVENTPHISICAL.CLASSUBID,IN_SUBCATEGORY.DESCR," +
                        " IN_INVENTPHISICAL.INVTIDSKU,IN_INVENTORY.DESCR AS [DESCRARTICULO],isnull(QTYAVAIL,0) as [Teorico]," +
                        " isnull(STOCKPHISICAL,0) as [Fisico]," +
                        " round(round(isnull(QTYAVAIL,0)," + @ND_STOCK + ")- round(isnull(STOCKPHISICAL,0)," + 
                         @ND_STOCK + ")," + @ND_STOCK + ") as [Diferencia]," +
                        " isnull(AVGCOSTPEN,0) as [CostoProm], " +
                        " round((isnull(round(QTYAVAIL," + @ND_STOCK + "),0)- isnull(round(STOCKPHISICAL," + @ND_STOCK + "),0)) * " +
                        " isnull(round(AVGCOSTPEN," + @ND_COSTO + "),0)," + @ND_STOCK + ") as [Total]" +                                		           		                                                       
                        " FROM  (((VWREPORTDIFSUBCATEGRESUMIDO RIGHT JOIN IN_INVENTPHISICAL ON " +
                        " VWREPORTDIFSUBCATEGRESUMIDO.SITEID=IN_INVENTPHISICAL.SITEID AND " +
                        " VWREPORTDIFSUBCATEGRESUMIDO.INVTIDSKU=IN_INVENTPHISICAL.INVTIDSKU) left join " +
                        " site on IN_INVENTPHISICAL.SITEID=SITE.SITEID) LEFT JOIN IN_SUBCATEGORY " +
                        " on IN_INVENTPHISICAL.CLASSUBID=IN_SUBCATEGORY.CLASSUBID) LEFT JOIN IN_INVENTORY " +  
                        " on IN_INVENTPHISICAL.INVTIDSKU=IN_INVENTORY.INVTIDSKU " --+ @CRITERIOSELECCION      
     exec (@CADENATEMP1)            
   
      --Fin de Reporte por SubCategoría
    END
End
go
IF OBJECT_ID('IN_REPORTEDIFERENCIARESUMIDOPROC') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_REPORTEDIFERENCIARESUMIDOPROC >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_REPORTEDIFERENCIARESUMIDOPROC >>>'
go


/* 
 * PROCEDURE: IN_REPORTEFS210Proc 
 */

Create procedure IN_REPORTEFS210Proc
@FechaDia CHAR(2),
@FechaMes CHAR(2),
@Fechaanio CHAR(4),
@FechaDiaIni char(2),   
@FechaMesIni char(2),  
@FechaAnioIni char(4),   
@FechaMesHistorica char(2),
@FechaDiaHasta char(2),
@FechaMesHasta char(2),
@FechaAnioHasta char(4),
@CadenaAlmacenes  varchar(8000),
@CadenaCategorias varchar(8000),
@CadenaSubCategorias varchar(8000),
@CATEGOSUBCATEG char(3),
@TODOSCATEGORIAS TINYINT,    -- Si es 1 son todas las Categorias 
@TODOSSUBCATEGORIAS TINYINT  -- Si es 1 son todas las SubCategorías   
as  
  begin
   DECLARE @cade varchar(8000)
   DECLARE @FechaDesde smalldatetime
   DECLARE @FechaInicial smalldatetime
   DECLARE @FechaHasta smalldatetime
  /*Variable para almacenar el criterio de selección */ 
   DECLARE @CRITERIOSELECCION varchar(8000) 
  /*Variable para Cargar el Tipo  de Movimiento Ventas*/
   DECLARE @TIPO UD_TYPEMOVEID  
  /*Variables para el Manejo de los Decimales*/

   DECLARE @ND_STOCKSETUP Tinyint
   DECLARE @ND_STOCK CHAR(1)
   SET @ND_STOCKSETUP=(Select Floatqty from setup)
   SET @ND_STOCK=RTRIM(LTRIM(STR(@ND_STOCKSETUP)))

  /*******************************************/
  
  /*set @FechaInicial=Cast(@FechaMesIni + '-' + @FechaDiaIni + '-' + @FechaAnioIni as smalldatetime)*/
  /*set @FechaDesde= Cast(@FechaMes + '-' +@FechaDia + '-' + @Fechaanio as smalldatetime)*/
  /*set @FechaHasta = Cast(@FechaMesHasta + '-' + @FechaDiaHasta + '-' + @FechaanioHasta as smalldatetime)*/

  set @FechaInicial=Cast(@FechaDiaIni + '-' + @FechaMesIni + '-' + @FechaAnioIni as smalldatetime)
  set @FechaDesde= Cast(@FechaDia + '-' + @FechaMes + '-' + @Fechaanio as smalldatetime)
  set @FechaHasta = Cast(@FechaDiaHasta + '-' + @FechaMesHasta + '-' + @FechaanioHasta as smalldatetime)
    
  if @CATEGOSUBCATEG='CAT' 
    begin
      /*Armar la Cadena de Criterios*/
      if @TODOSCATEGORIAS=1 
         set @CRITERIOSELECCION=" "          
      else
         set @CRITERIOSELECCION=" IN_INVENTORY.CLASSID in (" + RTRIM(LTRIM(@CadenaCategorias)) + ") and "

      select @cade=" SELECT in_kdxhist.siteid,IN_INVENTORY.CLASSID,SUM(ROUND(QTYEND" + @FechaMesHistorica +
               ","+ @ND_STOCK + ")) AS [Inicial],0 as [QTYRESULT]" +
               " from (in_kdxhist inner join IN_INVENTORY on IN_KDXHIST.INVTIDSKU=IN_INVENTORY.INVTIDSKU)" +
               " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +
               " WHERE SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and " + @CRITERIOSELECCION +                
               " yearhist=" + RTRIM(LTRIM(STR(year(@FechaDesde)))) + " GROUP BY IN_INVENTORY.CLASSID,in_kdxhist.siteid " +  
               " UNION  ALL " +     
               " SELECT INTRAMOV.SITEID,IN_INVENTORY.CLASSID,0 as [Inicial],SUM(CASE SWTINOUT WHEN 1 THEN ROUND(QTY," + @ND_STOCK +
               ") WHEN 0 THEN (ROUND(QTY," + @ND_STOCK + ") * - 1) END) AS QTYRESULT " +
               " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +  
               " inner join in_category on IN_INVENTORY.CLASSID=IN_CATEGORY.CLASSID " +
               " where SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and " + @CRITERIOSELECCION +
               " intrdateproce >='" + rtrim(ltrim(Cast(@FechaInicial as char(100)))) + "' and  " +
	       " intrdateproce <'" + rtrim(ltrim(Cast(@FechaDesde as char(100)))) + "' " +    
               " GROUP BY IN_INVENTORY.CLASSID,INTRAMOV.SITEID"   
       /*Se crea Tabla Temporal para Almacenar datos Obtenidos */

      Create Table #TMP_KARDEX
        (SITEID  char(5),
         CLASSID CHAR(5),
         INICIAL numeric(7,3),
         QTYRESULT numeric(7,3))  
      insert into #TMP_KARDEX exec(@cade)

      /*Se crea un Resumen de los Datos Creados en #TMP_KARDEXRESUMEN*/ 
  
      select #TMP_KARDEX.SITEID,SITE.NAME,#TMP_KARDEX.CLASSID,IN_CATEGORY.DESCR as [Descripcion],
             SUM(ROUND(INICIAL,@ND_STOCKSETUP))+ SUM (ROUND(QTYRESULT,@ND_STOCKSETUP)) AS [SALDO INICIAL] into #TMP_KARDEXRESUMEN 
             from (#TMP_KARDEX INNER JOIN SITE ON (#TMP_KARDEX.SITEID=SITE.SITEID))
                   INNER JOIN IN_CATEGORY ON #TMP_KARDEX.CLASSID=IN_CATEGORY.CLASSID              
             GROUP BY #TMP_KARDEX.SITEID,SITE.NAME,#TMP_KARDEX.CLASSID,IN_CATEGORY.DESCR

      /*Seleccionar el Typo de Movimientos de Inventarios (Por ventas)y excluirlo del Listado */ 
  
      Select @TIPO=INTYPESALES from in_setup 
      select siteid,classid,SUM (CASE TYPEDOCID
                          WHEN @TIPO THEN                          
                             ROUND(QTYRESULT * -1,@ND_STOCKSETUP) 
                          ELSE
                             0                    
                          END)AS [QTYRESULT] INTO #VW_KARDEXFS210VENTAS 
                       from VW_KARDEXFS210  where
                       INTRDATEPROCE BETWEEN @FechaDesde AND @FechaHasta
                       GROUP BY SITEID,CLASSID         

      /*Guardar en la Tabla #TMPVENTAS el Total de Ventas Expresadas en Cantidad */  
       select VW_SITECATEGORY.*,ROUND(ISNULL(QTYRESULT,0),@ND_STOCKSETUP) AS [QTYRESULT]into #TMPVENTAS 
                            from VW_SITECATEGORY LEFT JOIN #VW_KARDEXFS210VENTAS ON 
                                 VW_SITECATEGORY.SITEID = #VW_KARDEXFS210VENTAS.SITEID AND
                                 VW_SITECATEGORY.CLASSID = #VW_KARDEXFS210VENTAS.CLASSID
       
      /* Realiza el Union para Juntar la Vista  VW_KARDEXFS210 con el Total de Ventas  */ 
 
       SELECT #TMP_KARDEXRESUMEN.*,VW_KARDEXFS210.* FROM VW_KARDEXFS210 INNER JOIN #TMP_KARDEXRESUMEN ON
                                                   (VW_KARDEXFS210.SITEID=#TMP_KARDEXRESUMEN.SITEID AND 
   	       		                            VW_KARDEXFS210.CLASSID=#TMP_KARDEXRESUMEN.CLASSID) AND 
                       	  		            INTRDATEPROCE BETWEEN @FechaDesde AND @FechaHasta AND
                                                    TYPEDOCID <> @TIPO               
       UNION ALL 
       SELECT #TMP_KARDEXRESUMEN.*,#TMPVENTAS.* FROM #TMP_KARDEXRESUMEN INNER JOIN #TMPVENTAS ON 
                                                     #TMP_KARDEXRESUMEN.SITEID=#TMPVENTAS.SITEID AND
                                                     #TMP_KARDEXRESUMEN.CLASSID=#TMPVENTAS.CLASSID    
     end  
   else     
     begin
          /*Armar la Cadena de Criterio*/
          if @TODOSSUBCATEGORIAS=1 
            set @CRITERIOSELECCION=" "          
          else
            set @CRITERIOSELECCION=" IN_INVENTORY.CLASSUBID in (" + RTRIM(LTRIM(@CadenaSubCategorias)) + ") and "

          /*Si el Reporte es por SubCategoria */
          select @cade=" SELECT in_kdxhist.siteid,IN_INVENTORY.CLASSUBID,SUM(ROUND(QTYEND" + @FechaMesHistorica +
                 "," + @ND_STOCK + ")) AS [Inicial],0 as [QTYRESULT]" +
                 " from (in_kdxhist inner join IN_INVENTORY on IN_KDXHIST.INVTIDSKU=IN_INVENTORY.INVTIDSKU)" +
                 " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +
                 " WHERE SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and " + @CRITERIOSELECCION + " yearhist=" +  
                 + RTRIM(LTRIM(STR(year(@FechaDesde)))) + " GROUP BY IN_INVENTORY.CLASSUBID,in_kdxhist.siteid " +  
                 " UNION  ALL " +     
                 " SELECT INTRAMOV.SITEID,IN_INVENTORY.CLASSUBID,0 as [Inicial],SUM(CASE SWTINOUT WHEN 1 THEN ROUND(QTY," +
                 @ND_STOCK + ") WHEN 0 THEN " +
                 " (ROUND(QTY," + @ND_STOCK + ") * - 1) END) AS QTYRESULT " +
                 " from (INTRAMOV inner join IN_INVENTORY on INTRAMOV.INVTIDSKU=IN_INVENTORY.INVTIDSKU) " +  
                 " inner join IN_SUBCATEGORY on IN_INVENTORY.CLASSUBID=IN_SUBCATEGORY.CLASSUBID " +
                 " where SITEID in (" + RTRIM(LTRIM(@CadenaAlmacenes)) + ") and " + @CRITERIOSELECCION +                 
                 " intrdateproce >='" + rtrim(ltrim(Cast(@FechaInicial as char(100)))) + "' and  " +
	         " intrdateproce <'" + rtrim(ltrim(Cast(@FechaDesde as char(100)))) + "' " +    
                 " GROUP BY IN_INVENTORY.CLASSUBID,INTRAMOV.SITEID"   

       /*Se crea Tabla Temporal para Almacenar datos Obtenidos */

      Create Table #TMP_KARDEXSUBCAT
        (SITEID  char(5),
         CLASSUBID CHAR(5),
         INICIAL numeric(7,3),
         QTYRESULT numeric(7,3))  
   
      insert into #TMP_KARDEXSUBCAT exec(@cade)
      /*Se crea un Resumen de los Datos Creados en #TMP_KARDEXRESUMENSUBCAT */ 
      
      select #TMP_KARDEXSUBCAT.SITEID,SITE.NAME,#TMP_KARDEXSUBCAT.CLASSUBID,IN_SUBCATEGORY.DESCR as [Descripcion],
             SUM(ROUND(INICIAL,@ND_STOCKSETUP))+SUM (ROUND(QTYRESULT,@ND_STOCKSETUP)) AS [SALDO INICIAL] into #TMP_KARDEXRESUMENSUBCAT 
             from #TMP_KARDEXSUBCAT INNER JOIN SITE ON #TMP_KARDEXSUBCAT.SITEID=SITE.SITEID
                   INNER JOIN IN_SUBCATEGORY ON #TMP_KARDEXSUBCAT.CLASSUBID=IN_SUBCATEGORY.CLASSUBID                   
             GROUP BY #TMP_KARDEXSUBCAT.SITEID,SITE.NAME,
                      #TMP_KARDEXSUBCAT.CLASSUBID,IN_SUBCATEGORY.DESCR
     
      /*Seleccionar el Typo de Movimientos de Inventarios (Por ventas)y excluirlo del Listado */ 
  
      Select @TIPO=INTYPESALES from in_setup 
      select siteid,CLASSUBID,SUM (CASE TYPEDOCID
                          WHEN @TIPO THEN                          
                             ROUND(QTYRESULT,@ND_STOCKSETUP)
                          ELSE
                             0                    
                          END)AS [QTYRESULT] INTO #VW_KARDEXFS210VENTASSUBCAT 
                       from VW_KARDEXFS210SUBCATEG  where
                       INTRDATEPROCE BETWEEN @FechaDesde AND @FechaHasta
                       GROUP BY SITEID,CLASSUBID      

      /*Guardar en la Tabla #TMPVENTASSUBCAT el Total de Ventas Expresadas en Cantidad */  

      select VW_SITESUBCATEGORY.*,ROUND(ISNULL(QTYRESULT,0),@ND_STOCKSETUP) AS [QTYRESULT]into #TMPVENTASSUBCAT 
                               from VW_SITESUBCATEGORY LEFT JOIN #VW_KARDEXFS210VENTASSUBCAT ON 
                                    VW_SITESUBCATEGORY.SITEID =  #VW_KARDEXFS210VENTASSUBCAT.SITEID AND
                                    VW_SITESUBCATEGORY.CLASSUBID = #VW_KARDEXFS210VENTASSUBCAT.CLASSUBID
           
      /* Realiza el Union para Juntar la Vista  VW_KARDEXFS210SUBCATEG con el Total de Ventas  */ 
 
       SELECT #TMP_KARDEXRESUMENSUBCAT.*,VW_KARDEXFS210SUBCATEG.* 
					FROM VW_KARDEXFS210SUBCATEG INNER JOIN #TMP_KARDEXRESUMENSUBCAT ON
                                            (VW_KARDEXFS210SUBCATEG.SITEID=#TMP_KARDEXRESUMENSUBCAT.SITEID AND 
   	       		                     VW_KARDEXFS210SUBCATEG.CLASSUBID=#TMP_KARDEXRESUMENSUBCAT.CLASSUBID) AND 
                       	  		     INTRDATEPROCE BETWEEN @FechaDesde AND @FechaHasta AND
                                             TYPEDOCID <> @TIPO 
       UNION ALL  
       SELECT #TMP_KARDEXRESUMENSUBCAT.*,#TMPVENTASSUBCAT.* 
                                            FROM #TMP_KARDEXRESUMENSUBCAT INNER JOIN #TMPVENTASSUBCAT ON 
                                            #TMP_KARDEXRESUMENSUBCAT.SITEID=#TMPVENTASSUBCAT.SITEID AND
                                            #TMP_KARDEXRESUMENSUBCAT.CLASSUBID=#TMPVENTASSUBCAT.CLASSUBID    
        
     end 
end
go
IF OBJECT_ID('IN_REPORTEFS210Proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_REPORTEFS210Proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_REPORTEFS210Proc >>>'
go


/* 
 * PROCEDURE: in_RIS_DETALLE_ALL_proc 
 */


CREATE PROC in_RIS_DETALLE_ALL_proc
@MONEDA		UD_CURYID,	--EXPRESADO EN QUE MONEDA
@SITE		varCHAR(255),	-- SITES A EXAMINAR
@TODOS_MOV	TINYINT	,	--TODOS LOS MOVIMIENTOS
@TIPOMOV    	VARCHAR(255)='',--SOLO ESTOS MOVIMIENTOS
@FECHA_INI	VARCHAR(10),	--
@FECHA_FIN	VARCHAR(10),	--
@TCONDICION	CHAR(3)='',--TIPO CONDICION CATegoria, SCA SUB CATERIGORIA, (ART) PRODUCTO DOC documento
@TODOS		TINYINT=1,--TODOS LOS elementos del tipo de condicion
@CONDICION	VARCHAR(255)=''	--solo los elementos que cumplan la condicion
AS

DECLARE @CADENA VARCHAR(1000)	--CADENA A EJECUTAR
declare @moneda_base ud_curyid
set @moneda_base=(select curyorig from setup)

IF @MONEDA=@MONEDA_BASE	--si se ha solicitado la misma moneda base
	SET @CADENA=	' select vw_in_ris_detalle_all.*,STDCOSTPEN as COSTO,qty*STDCOSTPEN as tcosto '+
			' from   vw_in_ris_detalle_all '+
			" WHERE SITEID IN ("+@SITE+") AND (INTRDATEPROCE BETWEEN '"+@FECHA_INI+"' AND '"+@FECHA_FIN+"') "
ELSE	
	SET @CADENA=	' select vw_in_ris_detalle_all.*,stdcostusd as costo,qty*stdcostusd as tcosto '+
			' from   vw_in_ris_detalle_all '+
			" WHERE SITEID IN ("+@SITE+") AND (INTRDATEPROCE BETWEEN '"+@FECHA_INI+"' AND '"+@FECHA_FIN+"') "

IF @TODOS_MOV=0	SET @CADENA=@CADENA +' AND TYPEDOCID IN ('+@TIPOMOV+') '  --ALGUNOS MOVIMIENTOS 

IF @TCONDICION='CAT'	--CATEGORIA
BEGIN 
	IF @TODOS=0 SET @CADENA=@CADENA +' AND CLASSID IN ('+@CONDICION+') ' 	--ALGUNAS  CATEGORIAS
	SET @CADENA = @CADENA+' ORDER BY SITEID,CLASSID,TYPEDOCDESCR '	
END

IF @TCONDICION='SCA'	--SUB CATEGORIA
BEGIN 
	IF @TODOS=0 SET @CADENA=@CADENA +' AND CLASSID IN ('+@CONDICION+') ' 	--ALGUNAS  SUB CATEGORIAS

	SET @CADENA = @CADENA+' ORDER BY SITEID,CLASSUBID,TYPEDOCDESCR '	
END

IF @TCONDICION='ART'	--SITE POR ARTICULO
BEGIN 
	IF @TODOS=0 SET @CADENA=@CADENA +' AND CLASSUBID IN ('+@CONDICION+') ' 	--ALGUNOS ARTICULOS

	SET @CADENA = @CADENA+' ORDER BY SITEID,TYPEDOCDESCR,INVTIDSKU '	
END

IF @TCONDICION='ARS'	--ARTICULO POR SITE
BEGIN 
	IF @TODOS=0 SET @CADENA=@CADENA +' AND INVTIDSKU IN ('+@CONDICION+') ' 	--ALGUNAS  ARTICULOS 

	SET @CADENA = @CADENA+' ORDER BY  INVTIDSKU,SITEID,TYPEDOCDESCR '	
END

IF @TCONDICION='DOC'	--DOCUMENTO DETALLE
	SET @CADENA = @CADENA+' ORDER BY  SITEID,TYPEDOCDESCR,INTRDATE '	

exec (@cadena)
go
IF OBJECT_ID('in_RIS_DETALLE_ALL_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_RIS_DETALLE_ALL_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_RIS_DETALLE_ALL_proc >>>'
go


/* 
 * PROCEDURE: in_RIS_DOCUMENTO_RES_proc 
 */


CREATE PROC in_RIS_DOCUMENTO_RES_proc
@MONEDA		UD_CURYID,	--EXPRESADO EN QUE MONEDA
@SITE		varCHAR(255),	-- SITES A EXAMINAR
@TODOS_MOV	TINYINT	,	--TODOS LOS MOVIMIENTOS
@TIPOMOV    	VARCHAR(255)='',	--SOLO ESTOS MOVIMIENTOS
@FECHA_INI	VARCHAR(10),
@FECHA_FIN	VARCHAR(10)
AS

DECLARE @CADENA VARCHAR(1000)	--CADENA A EJECUTAR
declare @moneda_base ud_curyid
--leer la moneda base de setup
set @moneda_base=(select curyorig from setup)

IF @MONEDA=@moneda_base	--si se ha solicitado la misma moneda base
SET @CADENA=	' select vw_in_ris_resumendoc.*,intrtotpen-intrtaxpen as tventa,intrtaxpen as igv,intrtotpen as tcompras '+
		' from vw_in_ris_resumendoc '+
		" WHERE SITEID IN ("+@SITE+") AND (INTRDATEPROCE BETWEEN '"+@FECHA_INI+"' AND '"+ @FECHA_FIN+"') "
ELSE	
SET @CADENA=	' select vw_in_ris_resumendoc.*,intrtotusd-intrtaxusd as tventa,intrtaxusd as igv,intrtotusd as tcompras '+
		' from vw_in_ris_resumendoc '+
		" WHERE SITEID IN ("+@SITE+") AND (INTRDATEPROCE BETWEEN '"+@FECHA_INI+"' AND '"+ 	@FECHA_FIN+"') "

IF @TODOS_MOV=1		--TODOS LOS MOVIMIENTOS, 
	SET 	@CADENA=@CADENA +
		' ORDER BY SITEID,TYPEDOCDESCR,INTRDATE '
ELSE			--SOLO ALGUNOS TIPOS DE MOVIMIENTOS
	SET 	@CADENA=@CADENA +
		' AND TYPEDOCID IN ('+@TIPOMOV+') ' +
		' ORDER BY SITEID,TYPEDOCDESCR,INTRDATE '
exec (@cadena)
go
IF OBJECT_ID('in_RIS_DOCUMENTO_RES_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE in_RIS_DOCUMENTO_RES_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE in_RIS_DOCUMENTO_RES_proc >>>'
go


/* 
 * PROCEDURE: IN_SgteItemEnDetalleFormulaProc 
 */

CREATE PROCEDURE IN_SgteItemEnDetalleFormulaProc
( @CORRNBR  int)
AS
BEGIN

    BEGIN TRAN
    DECLARE @VALOR INTEGER	
    SET  SELECT @VALOR = COUNT(*)+1 FROM TMP_IN_KITCOMPONENT WHERE CORRNBR=@CORRNBR
   

    IF (@@error!=0)
    BEGIN
        ROLLBACK TRAN
        RETURN(-1)
    END

    COMMIT TRAN
    RETURN(@VALOR)

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


/* 
 * PROCEDURE: IN_SOLINVENTORYInsProc 
 */

CREATE PROCEDURE IN_SOLINVENTORYInsProc
(
    @INVTIDSKU       UD_IDCORPORATIVO,
    @INVTIDSTK       char(20),
    @INVTIDNOSTK     char(20)                    = NULL)
AS
BEGIN
    DECLARE @ERROR INTEGER

    INSERT INTO IN_SOLINVENTORY(INVTIDSKU,INVTIDSTK,INVTIDNOSTK)
    VALUES(@INVTIDSKU,
           @INVTIDSTK,
           @INVTIDNOSTK)

    SET @ERROR=@@error
    
    IF (@ERROR!=0)
    BEGIN       
        RETURN(1)    
    END

    RETURN(0)

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


/* 
 * PROCEDURE: IN_SOLINVENTORYUpdProc 
 */

CREATE PROCEDURE IN_SOLINVENTORYUpdProc
(
    @INVTIDSKU       UD_IDCORPORATIVO,
    @INVTIDSTK       char(20),
    @INVTIDNOSTK     char(20)                    = NULL)
AS
BEGIN
    DECLARE @ERROR INTEGER    

    UPDATE IN_SOLINVENTORY
       SET INVTIDSTK        = @INVTIDSTK,
           INVTIDNOSTK      = @INVTIDNOSTK
     WHERE INVTIDSKU = @INVTIDSKU

    SET @ERROR=@@error
    
    IF (@ERROR!=0)
    BEGIN       
        RETURN(1)    
    END

    RETURN(0)

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


/* 
 * PROCEDURE: IN_SUBCATEGORYSelProc 
 */

CREATE PROCEDURE IN_SUBCATEGORYSelProc
(
    @CLASSID       char(5))
AS
BEGIN
    SELECT CLASSUBID,DESCR,
           CLASSID           
     FROM IN_SUBCATEGORY
     WHERE CLASSID   = @CLASSID

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


/* 
 * PROCEDURE: IN_TypeDocInsProc 
 */

CREATE PROCEDURE IN_TypeDocInsProc
(
    @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),
    @USER1           UD_USER1                  = NULL,
    @USER2           UD_USER2                  = NULL,
    @USER3           UD_USER6                  = NULL,
    @USER4           UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    IF(@PRECLVLID='')	 SET @PRECLVLID=NULL

    INSERT INTO IN_TYPEDOC(TYPEDOCID,
                           DESCR,
                           INOUT,
                           STKVALUE,
                           STKCOSPRICE,
                           STKAVGCOST,
                           PRECLVLID,
                           STKBUY,
                           STKVENDOR,
                           STKAVERAGE,
                           DESCR_ABR,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@TYPEDOCID,
           @DESCR,
           @INOUT,
           @STKVALUE,
           @STKCOSPRICE,
           @STKAVGCOST,
           @PRECLVLID,
           @STKBUY,
           @STKVENDOR,
           @STKAVERAGE,
           @DESCR_ABR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_TypeDocUpdProc 
 */

CREATE PROCEDURE IN_TypeDocUpdProc
(
    @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),
    @USER1           UD_USER1                  = NULL,
    @USER2           UD_USER2                  = NULL,
    @USER3           UD_USER6                  = NULL,
    @USER4           UD_USER7                  = NULL,
    @TSTAMP          timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    IF(@PRECLVLID='')	 SET @PRECLVLID=NULL

    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,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE TYPEDOCID = @TYPEDOCID

    IF (@@error!=0)
    BEGIN
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: IN_VENDORINVTDelProc 
 */

CREATE PROCEDURE IN_VENDORINVTDelProc
(
  @INVTIDSKU UD_IDCORPORATIVO)
AS
BEGIN
DECLARE @ERROR INTEGER    
     DELETE
     FROM IN_VENDORINVT
          WHERE INVTIDSKU = @INVTIDSKU

    SET @ERROR=@@error
    
    IF (@ERROR!=0)
    BEGIN       
        RETURN(1)    
    END

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


/* 
 * PROCEDURE: listacomprasproveedor_ALL_proc 
 */

CREATE PROC  listacomprasproveedor_ALL_proc
@FECHA_INI 		VARCHAR(30), 		--FECHA INICIAL, NO ES TIPO SMALLDATETIME POR PROBLEMAS DE CONVERSION
@FECHA_FIN  		VARCHAR(30), 		--FECHA FINAL, NO ES TIPO SMALLDATETIME POR PROBLEMAS DE CONVERSION
@SITE 			varchar(300),		--ALMACEN
@TODASLASMONEDAS	tinyint,		--1 TODAS LAS MONEDAS,0  SOLO ALGUNA MONEDA
@TODOS_prov 		tinyint,		--TODOS LOS PROVEEDORES
@SOLOMONEDA 		ud_curyid,		--SOLOS LOS DOCUMENTOS QUE TENGAN ESTA MONEDA
@CONDICION 		varchar(300)		--SOLO LOS PROVEEDORS QUE ESTEN EN ESTE PARAMETRO
AS

DECLARE @VFECHA_INI SMALLDATETIME
DECLARE @VFECHA_FIN SMALLDATETIME
DECLARE @CADENA VARCHAR(8000)			--CADENA DE EJECUCION


SET @VFECHA_INI=CAST(@FECHA_INI AS SMALLDATETIME) 
SET @VFECHA_FIN=CAST(@FECHA_FIN AS SMALLDATETIME)

SET @CADENA="select * from vw_ingresoscompraproveedor WHERE (INTRDATEPROCE BETWEEN '"+
			@FECHA_INI+
			"' AND '"+ 
			@FECHA_FIN+"') "+
			" AND SITEID IN ("+@SITE+") "

IF @TODASLASMONEDAS=1 AND @TODOS_PROV=1 	--TODAS LAS MONEDAS TODOS LOS PROVEEDORES
		SET @CADENA=@CADENA +' ORDER BY SITEID,NPROVEEDOR,INTRDATE '

IF @TODASLASMONEDAS=0 AND @TODOS_PROV=1 	--SOLO ALGUNA MONEDA TODOS LOS PROVEEDORES
		SET @CADENA=@CADENA +
		" AND FROMCURYID='"+ @SOLOMONEDA +"' "+
		' ORDER BY SITEID,NPROVEEDOR,INTRDATE '

IF @TODASLASMONEDAS=1 AND @TODOS_PROV=0 	--TODAS LAS MONEDAS ALGUNOS PROVEEDORES
		SET @CADENA=@CADENA +	
		" AND VENDIDSS IN (" + @CONDICION +") "+
		' ORDER BY SITEID,NPROVEEDOR,INTRDATE '

IF @TODASLASMONEDAS=0 AND @TODOS_PROV=0 	--SOLO UNA MONEDA  ALGUNOS PROVEEDORES
		SET @CADENA=@CADENA +
		" AND FROMCURYID='"+ @SOLOMONEDA +"' AND (VENDIDSS IN ("+@CONDICION+")) "+
		' ORDER BY SITEID,NPROVEEDOR,INTRDATE '
EXEC (@CADENA)
go
IF OBJECT_ID('listacomprasproveedor_ALL_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE listacomprasproveedor_ALL_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE listacomprasproveedor_ALL_proc >>>'
go


/* 
 * PROCEDURE: listap_ALL_SP 
 */


create proc listap_ALL_SP
@idlista  	UD_PRECLVID,		--codigo tipo lista de precios
@tipomoneda  	UD_CURYID,		--codigo de moneda a mostrar
@SITE 		varCHAR(8000),		--almacenes que se desean mostrar
@TCONDICION 	CHAR(3),		--tipo de condicion, CAT categoria SCA subcategoria, TIp tipo, ART articulo
@todas 		integer,			--si de desean ver todos los elementos del tipo de condicion 
@condicion 	varchar(8000)='' 	--condicion si solo se desean ver algunos item
	as
SET @CONDICION=LTRIM(@CONDICION)
declare @tipoc 		numeric(6,4)	--TIPO DE CAMBIO PARA LOS CALCULOS
declare @tipoc_actual	numeric(6,4)	--tipo de cambio referencial
declare @Ta		VARCHAR(10)	--tipo de cambio referencial EN TEXTO
DECLARE @TMONEDAL 	UD_CURYID	--TIPO MONEDA LISTA
DECLARE @OPERADOR 	CHAR(1)		--OPERADOR PARA LA CONVERSION
DECLARE @TTIPOC 	CHAR(6)		--TIPO LISTA  DE TIPO DE CAMBIO
DECLARE @CONSULTA	VARCHAR(8000)	--CONSULTA QUE SE VA A EJECUTAR
declare @tc 		Varchar(10)	--TIPO DE CAMBIO PARA EL CALCULO EN CADENA
DECLARE @VISTA		VARCHAR(25)	--NOMBRE DE LA VISTA

SET @CONDICION=RTRIM(@CONDICION)
SET @TMONEDAL= (SELECT CURYID FROM IN_PRICELEVELTYPE WHERE PRECLVID=@IDLISTA)	--SABER LA MONEDA DEL TIPO DE LISTA

SET @TTIPOC=(SELECT CURYTYPEID  FROM IN_PRICELEVELTYPE WHERE PRECLVID=@IDLISTA)


IF @TMONEDAL=@TIPOMONEDA 
	BEGIN
		SET @TIPOC=1	--SI SE PIDE EL MISMO LA CONSULTA EN EL MISMO TIPO DE MONEDA DE LA LSITA NO SE NECESITA CONVERTIR
	
		SET @TIPOC_aCTUAL=(SELECT  TOP 1 RATE FROM CURYRATE 
			WHERE  FROMCURYID=@TMONEDAL AND CURYTYPEID=@TTIPOC 
			ORDER BY CURYDATE DESC )	-- OBTENER EL TIPO DE CAMBIO
	END
ELSE
	BEGIN 
		SET @TIPOC=(SELECT  TOP 1 RATE FROM CURYRATE 
			WHERE TOCURYID=@TIPOMONEDA AND FROMCURYID=@TMONEDAL AND CURYTYPEID=@TTIPOC 
			ORDER BY CURYDATE DESC )	-- OBTENER EL TIPO DE CAMBIO
	
		SET @OPERADOR=(SELECT  TOP 1 OPERADOR FROM CURYRATE 	--SI SE PIDEN DIFERENTES TIPOS DE MONEDA SE NECESITA OBTENER TIPO CAMBIO
			WHERE TOCURYID=@TIPOMONEDA AND FROMCURYID=@TMONEDAL AND CURYTYPEID=@TTIPOC 	--BUSCAR EL DATO DE TIPOS LISTA DE PRECIOS
			ORDER BY CURYDATE DESC )	-- OBTENER EL TIPO DE CAMBIO	
		set @tipoc_actual=@TIPOC  --se busca el tipo de cambio solo para presentacion, se calcula con @tipoc			
	END
SET @Ta=LTRIM(Rtrim(str(@tipoc_actual,3,2)))
set @tc=LTRIM(Rtrim(str(@tipoc,3,2)))

IF @TCONDICION='PRO' 
	SET @VISTA='VW_PRODUCTOSXPROVEEDOR'
ELSE
	SET @VISTA='VW_LISTAPRECIOS'

IF @OPERADOR='/'
	SET @CONSULTA =	'SELECT *,'+@TC+' AS TIPOCAMBIO, '+ 
			'CAST( ROUND(SLS_PRICE/' +@TC+',2) AS NUMERIC(10,2) ) AS PVENTA, '+
		 	'CAST( ROUND( (SLS_PRICE/'+@TC+')*(VALUEIGV/100),2) AS NUMERIC(10,2) )AS TAX, '+
			'CAST( ROUND(SLS_PRICE/'+@TC+',2)-ROUND( (SLS_PRICE/'+@TC+')*(VALUEIGV/100),2)  AS NUMERIC(10,2) )  AS VVENTA, '+ @Ta+' as tipoactual ' +
			"FROM "+@VISTA+" WHERE PRECLVID='" + @IDLISTA + "' AND SITEID IN (" +@SITE+ ") "
ELSE
	SET @CONSULTA =	'SELECT *,'+@TC+' AS TIPOCAMBIO, '+ 
			'CAST( ROUND(SLS_PRICE*' +@TC+',2) AS NUMERIC(10,2) ) AS PVENTA, '+
		 	'CAST( ROUND( (SLS_PRICE*'+@TC+')*(VALUEIGV/100),2) AS NUMERIC(10,2) )AS TAX, '+
			'CAST( ROUND(SLS_PRICE*'+@TC+',2)-ROUND( (SLS_PRICE*'+@TC+')*(VALUEIGV/100),2)  AS NUMERIC(10,2) )  AS VVENTA, '+ @Ta+' as tipoactual ' +
			"FROM "+@VISTA + " WHERE PRECLVID='" + @IDLISTA + "' AND SITEID IN (" +@SITE+ ") "

if @TCONDICION="CAT"	--POR CATEGORIA
	IF @TODAS=1	--SIN CONDICION
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by siteid,classid,descr'
			EXEC (@CONSULTA)		
		END	
	ELSE
		BEGIN	
			SET @CONSULTA =@CONSULTA +' and classid IN ('+@condicion+') '+
						  ' order by siteid,classid,descr'
			EXEC (@CONSULTA)
		END

	if @TCONDICION="SCA"	--POR SUB CATEGORIA
		IF @TODAS=1
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by siteid,classid,classubid,descr'				
			EXEC (@CONSULTA)		
		END	
	ELSE
		BEGIN
			SET @CONSULTA =@CONSULTA +' AND CLASSUBID IN (' +@CONDICION+')'+
						  ' order by siteid,classid,classubid,descr'				
		EXEC (@CONSULTA)		
		END	
	if @TCONDICION="TIP"	--POR SUB CATEGORIA
		IF @TODAS=1
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by siteid,invtypeid,descr'
			EXEC (@CONSULTA)		
		END	
		
	ELSE
		BEGIN
			SET @CONSULTA =@CONSULTA +' AND invtypeid in ('+@condicion+')'+
						  ' order by siteid,invtypeid,descr'
			EXEC (@CONSULTA)		
		END	
	if @TCONDICION="ART"	--POR SITE X ARTICULO
		IF @TODAS=1
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by siteid,descr'
			EXEC (@CONSULTA)		
		END	
	ELSE
		BEGIN
			SET @CONSULTA =@CONSULTA +' AND invtidsku in ('+@condicion+')' +
						  ' order by siteid,descr '
			EXEC (@CONSULTA)		
		END	

	if @TCONDICION="ARS"	-- ARTICULO X SITE
		IF @TODAS=1
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by invtidsku,SITEID'
			EXEC (@CONSULTA)		
		END	
	ELSE
		BEGIN
			SET @CONSULTA =@CONSULTA +' AND invtidsku in ('+@condicion+')' +
						  ' order by invtidsku,SITEID '
			EXEC (@CONSULTA)		
		END	

	if @TCONDICION="PRO"	-- PROVEEDOR
		IF @TODAS=1
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by VENDIDSS,SITEID,NPRODUCTO'
			EXEC (@CONSULTA)		
		END	
	ELSE
		BEGIN
			SET @CONSULTA =@CONSULTA +' AND vendidss in ('+@condicion+') ' +
						  ' order by VENDIDSS,SITEID,NPRODUCTO '
			EXEC (@CONSULTA)		
		END
go
IF OBJECT_ID('listap_ALL_SP') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE listap_ALL_SP >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE listap_ALL_SP >>>'
go


/* 
 * PROCEDURE: listastock_ALL_proc 
 */


create proc listastock_ALL_proc
@SITE 		varCHAR(8000),		--almacenes que se desean mostrar
@TCONDICION 	CHAR(3),		--tipo de condicion, CAT categoria SCA subcategoria, ART articulo
@todas 		integer=1,		--si de desean ver todos los elementos del tipo de condicion 
@condicion 	varchar(8000)='', 	--condicion si solo se desean ver algunos item
@solostock	tinyint=1			--solo si desean productos con  stock
	as
SET @CONDICION=LTRIM(@CONDICION)
DECLARE @CONSULTA	VARCHAR(8000)	--CONSULTA QUE SE VA A EJECUTAR

IF @solostock=1
	SET @CONSULTA =	"SELECT * FROM VW_LISTASTOCK WHERE QTYAVAIL<>0 and SITEID IN (" +@SITE+ ") "
ELSE
	SET @CONSULTA =	"SELECT * FROM VW_LISTASTOCK WHERE SITEID IN (" +@SITE+ ") "

if @TCONDICION="CAT"	--POR CATEGORIA
	IF @TODAS=1	--SIN CONDICION
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by siteid,classid,descr'				
			EXEC (@CONSULTA)		
		END	
	ELSE
		BEGIN	
			SET @CONSULTA =@CONSULTA +' and classid IN ('+@condicion+') '+
						  ' order by siteid,classid,descr'
			EXEC (@CONSULTA)
		END

if @TCONDICION="SCA"	--POR SUB CATEGORIA
	IF @TODAS=1
	BEGIN
		SET @CONSULTA =@CONSULTA +' order by siteid,classid,classubid,descr'				
		EXEC (@CONSULTA)		
	END	
ELSE
	BEGIN
		SET @CONSULTA =@CONSULTA +' AND CLASSUBID IN (' +@CONDICION+')'+
					  ' order by siteid,classid,classubid,descr'				
	EXEC (@CONSULTA)		
	END	
if @TCONDICION="ART"	--POR ARTICULO
	IF @TODAS=1
	BEGIN
		SET @CONSULTA =@CONSULTA +' order by siteid,descr'
		EXEC (@CONSULTA)		
	END	
ELSE
	BEGIN
		SET @CONSULTA =@CONSULTA +' AND invtidsku in ('+@condicion+')' +
					  ' order by siteid,descr '
		EXEC (@CONSULTA)		
	END
go
IF OBJECT_ID('listastock_ALL_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE listastock_ALL_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE listastock_ALL_proc >>>'
go


/* 
 * PROCEDURE: ObtenerCorrelativo 
 */

Create Procedure ObtenerCorrelativo
(
@USERID UD_USERID,
@CORRELATIVO INTEGER OUTPUT
)

AS     
  BEGIN
   BEGIN TRAN
    insert into TMP_CORRNBRHEAD(userid) values (@USERID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'ObtenerCorrelativoProcUtil: Error al Insertar'
        ROLLBACK TRAN
        RETURN(1)
    
    END
     
    COMMIT TRAN
    SET @Correlativo=(SELECT @@IDENTITY )
  END
go
IF OBJECT_ID('ObtenerCorrelativo') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE ObtenerCorrelativo >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE ObtenerCorrelativo >>>'
go


/* 
 * PROCEDURE: ObtenerCorrelativoProcUtil 
 */

CREATE Procedure ObtenerCorrelativoProcUtil
(@USERID UD_USERID,
 @corre integer=0 output
)
AS     
  BEGIN
   BEGIN TRAN
    insert into TMP_CORRNBRHEAD(userid) values (@USERID)
    Set @corre=@@identity
    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'ObtenerCorrelativoProcUtil: Error al Obtener Correlativo'
        ROLLBACK TRAN
        RETURN(1)    
    END
    SELECT @@IDENTITY as Correlativo          
    COMMIT TRAN    

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


/* 
 * PROCEDURE: ObtenerImpuestosProc 
 */

CREATE PROC ObtenerImpuestosProc
(	@INVTIDSKU       UD_IDCORPORATIVO
)
AS
BEGIN
	DECLARE @IMPUESTO1	NUMERIC(5,2) -- ALMACENARA EL PORCENTAJE DE IGV
	DECLARE	@IMPUESTO2	NUMERIC(5,2) -- ALMACENARA EL PORCENTAJE DEL ISC
	
	SET @IMPUESTO1=	(SELECT VALUE
			FROM IN_INVENTORY JOIN TAXS 
			ON IN_INVENTORY.TAXIGV=TAXS.TAXID
			WHERE INVTIDSKU=@INVTIDSKU)

	SET @IMPUESTO2=	(SELECT VALUE
			FROM IN_INVENTORY JOIN TAXS 
			ON IN_INVENTORY.TAXISC=TAXS.TAXID
			WHERE INVTIDSKU=@INVTIDSKU)

	SELECT @IMPUESTO1 AS IMPUESTO1,@IMPUESTO2 AS IMPUESTO2
END
go
IF OBJECT_ID('ObtenerImpuestosProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE ObtenerImpuestosProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE ObtenerImpuestosProc >>>'
go


/* 
 * PROCEDURE: obtiene_tipocambio_proc 
 */

create proc obtiene_tipocambio_proc
@idmoneda ud_curyid,
@fecha smalldatetime,
@tipocambio numeric(4,2)=0 output


as
declare @TTCAMBIO ud_curytypeid


set @TTCAMBIO= (select SOURCECURYTYPEID FROM SETUP )

SELECT RATE,CURYDATE,OPERADOR FROM CURYRATE WHERE CURYTYPEID=@TTCAMBIO AND FROMCURYID=@idmoneda AND CURYDATE=
								(SELECT MAX(CURYDATE) FROM CURYRATE WHERE CURYDATE<=@FECHA)
go
IF OBJECT_ID('obtiene_tipocambio_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE obtiene_tipocambio_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE obtiene_tipocambio_proc >>>'
go


/* 
 * PROCEDURE: ObtieneMontoCambioProc 
 */

CREATE PROC ObtieneMontoCambioProc(
	@COSTO		UD_COSTO,
	@CAMBIO		numeric(6,4),
	@OPERADOR 	CHAR(1)
)
AS

	DECLARE @COST	UD_COSTO

	IF (@OPERADOR='/')
		SET @COST=@COSTO/@CAMBIO
	ELSE
		SET @COST=@COSTO*@CAMBIO

	SELECT @COST AS COSTO
go
IF OBJECT_ID('ObtieneMontoCambioProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE ObtieneMontoCambioProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE ObtieneMontoCambioProc >>>'
go


/* 
 * PROCEDURE: ObtieneTipoCambioProc 
 */

CREATE PROC ObtieneTipoCambioProc(
	@VENDIDSS	UD_IDVENDOR,
	@IDMONEDA 	UD_CURYID,
	@FECHA 		smalldatetime,
	@TIPOCAMBIO numeric(4,2)=0 output
)
AS

	DECLARE @TTCAMBIO UD_CURYTYPEID
	SET @TTCAMBIO= (SELECT CURYTYPEID FROM IN_VENDOR WHERE VENDIDSS=@VENDIDSS)

	SELECT RATE,CURYDATE,OPERADOR FROM CURYRATE WHERE CURYTYPEID=@TTCAMBIO AND FROMCURYID=@idmoneda AND CURYDATE=@FECHA
go
IF OBJECT_ID('ObtieneTipoCambioProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE ObtieneTipoCambioProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE ObtieneTipoCambioProc >>>'
go


/* 
 * PROCEDURE: SE_BORRARDERECHOSProc 
 */

CREATE PROCEDURE SE_BORRARDERECHOSProc
(    @USERID           UD_USERID )
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_ACCESSDETRIGHTS
     WHERE USERID   = @USERID

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

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


/* 
 * PROCEDURE: SE_borrargruposdelusuarioProc 
 */

CREATE PROCEDURE SE_borrargruposdelusuarioProc
(
    @USERID         UD_USERID
  )
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_USERGROUP
     WHERE USERID  = @USERID
    
    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SE_USERGROUPDelProc: Cannot delete because foreign keys still exist in SE_USERGROUP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: SE_borrarusuariosdelgrupoProc 
 */


CREATE PROCEDURE SE_borrarusuariosdelgrupoProc
(
    @groupID         UD_USERID
  )
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_USERGROUP
     WHERE groupid  = @groupid
    
    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'SE_USERGROUPDelProc: Cannot delete because foreign keys still exist in SE_USERGROUP '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcAR_CCDOC 
 */



/* 
 * PROCEDURE: sp_DelProcAR_CCDOC 
 */

CREATE PROCEDURE sp_DelProcAR_CCDOC
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS)
AS
BEGIN
    BEGIN TRAN

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

    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_DelProcAR_CCDOCDET 
 */



/* 
 * PROCEDURE: sp_DelProcAR_CCDOCDET 
 */

CREATE PROCEDURE sp_DelProcAR_CCDOCDET
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           UD_CORRELATIVOS,
    @SEQUENCE              UD_REGISTRO)
AS
BEGIN
    BEGIN TRAN

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

    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_DelProcBANK 
 */



/* 
 * 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_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_DelProcCLASSCUST 
 */

CREATE PROCEDURE SP_DelProcCLASSCUST
(
    @CLASSCUSTID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CLASSCUST
     WHERE CLASSCUSTID = @CLASSCUSTID

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

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('SP_DelProcCLASSCUST') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE SP_DelProcCLASSCUST >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE SP_DelProcCLASSCUST >>>'
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_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_DelProcCURYRATE 
 */

CREATE PROCEDURE SP_DelProcCURYRATE
(
    @CURYTYPEID     char(6),
    @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_DelProcCURYRATETYPE 
 */

CREATE PROCEDURE SP_DelProcCURYRATETYPE
(
    @CURYTYPEID     char(6))
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_DelProcCUST_LIMIT 
 */



/* 
 * PROCEDURE: sp_DelProcCUST_LIMIT 
 */

CREATE PROCEDURE sp_DelProcCUST_LIMIT
(
    @CUSTIDSS        UD_CODCLIENT,
    @SITEID          UD_IDSITE)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUST_LIMIT
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

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

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('sp_DelProcCUST_LIMIT') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcCUST_LIMIT >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcCUST_LIMIT >>>'
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_DelProcCUSTSTATUS 
 */



/* 
 * 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_DelProcCUSTWARRANTY 
 */



/* 
 * PROCEDURE: sp_DelProcCUSTWARRANTY 
 */

CREATE PROCEDURE sp_DelProcCUSTWARRANTY
(
    @CUSTIDSS               UD_CODCLIENT,
    @CUSTGARANTIATYPEID     char(2))
AS
BEGIN
    BEGIN TRAN


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

    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_DelProcCUSTWARRANTYTYPE 
 */



/* 
 * PROCEDURE: sp_DelProcCUSTWARRANTYTYPE 
 */

CREATE PROCEDURE sp_DelProcCUSTWARRANTYTYPE
(
    @CUSTGARANTIATYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUSTWARRANTYTYPE
     WHERE CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

    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_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_DelProcIN_CATEGORY 
 */

CREATE PROCEDURE SP_DelProcIN_CATEGORY
(
    @CLASSID     char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_CATEGORY
     WHERE CLASSID = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'No se puede Eliminar categoria '
        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_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_DelProcIN_CLUSTER 
 */



/* 
 * 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_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_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_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_DelProcIN_INVENTHISTOPHISI 
 */

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

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

    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_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_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_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_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_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_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_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_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_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_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_DelProcIN_PRICELEVELTEM 
 */

CREATE PROCEDURE SP_DelProcIN_PRICELEVELTEM
(
    @INVTIDSKU             UD_IDCORPORATIVO,
    @SITEID                UD_IDSITE,
    @CORRNBR               char(10))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_PRICELEVELTEM
     WHERE INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID
       AND CORRNBR   = @CORRNBR

    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_DelProcIN_PRICELEVELTEMDET 
 */



/* 
 * PROCEDURE: sp_DelProcIN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE sp_DelProcIN_PRICELEVELTEMDET
(
    @BATNBRPRICELEVELTEM     UD_CORRELATIVOS,
    @SITEID                  UD_IDSITE,
    @SEQUENCE                UD_REGISTRO)
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_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_DelProcIN_SOLINVENTORY 
 */

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

    DELETE
      FROM IN_SOLINVENTORY
     WHERE INVTIDSKU = @INVTIDSKU

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: SP_DelProcIN_SUBCATEGORY 
 */

CREATE PROCEDURE SP_DelProcIN_SUBCATEGORY
(
    @CLASSUBID     char(5),
    @CLASSID       char(5))
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_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_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_DelProcIN_VENDORINVT 
 */

CREATE PROCEDURE SP_DelProcIN_VENDORINVT
(
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

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

    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_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_DelProcOP_CARA_FUEL 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CARA_FUEL 
 */

CREATE PROCEDURE sp_DelProcOP_CARA_FUEL
(
    @CARA           UD_CARA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CARA_FUEL
     WHERE CARA = @CARA

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CARD 
 */



/* 
 * 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_DelProcOP_CARD_MONT 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CARD_MONT 
 */

CREATE PROCEDURE sp_DelProcOP_CARD_MONT
(
    @SITEID        UD_IDSITE,
    @CARDID        char(2),
    @DATE          UD_FECHA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CARD_MONT
     WHERE SITEID = @SITEID
       AND CARDID = @CARDID
       AND DATE   = @DATE

    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_DelProcOP_CCSALESPERSON 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CCSALESPERSON 
 */

CREATE PROCEDURE sp_DelProcOP_CCSALESPERSON
(
    @SITEID         UD_IDSITE,
    @SALESPERID     UD_IDVENDEDOR)
AS
BEGIN
    BEGIN TRAN

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

    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_DelProcOP_CCSALESPERSONDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CCSALESPERSONDET 
 */

CREATE PROCEDURE sp_DelProcOP_CCSALESPERSONDET
(
    @SITEID                  UD_IDSITE,
    @SALESPERID              UD_IDVENDEDOR,
    @SEQUENCE                UD_REGISTRO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CCSALESPERSONDET
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID
       AND SEQUENCE   = @SEQUENCE

    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_DelProcOP_CCSALESPERSONTYPE 
 */



/* 
 * 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_DelProcOP_CORELATIVE 
 */



/* 
 * 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_DelProcOP_CTRLFUEL 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CTRLFUEL 
 */

CREATE PROCEDURE sp_DelProcOP_CTRLFUEL
(
    @SITEID               UD_IDSITE,
    @BATNBRCTRLFUEL       UD_CORRELATIVOS)
AS
BEGIN
    BEGIN TRAN

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

    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_DelProcOP_CTRLFUELDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CTRLFUELDET 
 */

CREATE PROCEDURE sp_DelProcOP_CTRLFUELDET
(
    @SITEID             UD_IDSITE,
    @BATNBRCTRLFUEL     UD_CORRELATIVOS,
    @SEQUENCE           tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLFUELDET
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL
       AND SEQUENCE       = @SEQUENCE

    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_DelProcOP_CTRLTURN 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CTRLTURN 
 */

CREATE PROCEDURE sp_DelProcOP_CTRLTURN
(
    @BATNBRTURN     UD_CORR_CONTOMET)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLTURN
     WHERE BATNBRTURN = @BATNBRTURN

    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_DelProcOP_CTRLTURNDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CTRLTURNDET 
 */

CREATE PROCEDURE sp_DelProcOP_CTRLTURNDET
(
    @BATNBRTURN       UD_CORR_CONTOMET)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLTURNDET
     WHERE BATNBRTURN = @BATNBRTURN

    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_DelProcOP_CUSTCARD 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CUSTCARD 
 */

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

    DELETE
      FROM OP_CUSTCARD
     WHERE CUSTIDSS = @CUSTIDSS

    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_DelProcOP_CUSTCLASS 
 */



/* 
 * PROCEDURE: sp_DelProcOP_CUSTCLASS 
 */

CREATE PROCEDURE sp_DelProcOP_CUSTCLASS
(
    @CUSTCLASSID     char(2))
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_DelProcOP_CUSTZONE 
 */



/* 
 * 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_DelProcOP_DEFINITIONPD 
 */



/* 
 * 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_DelProcOP_DEPOSIT 
 */



/* 
 * PROCEDURE: sp_DelProcOP_DEPOSIT 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSIT
(
    @BATNBRDEPOSIT       UD_CORREL_DEPOSIT,
    @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_DelProcOP_DEPOSITDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_DEPOSITDET 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSITDET
(
    @BATNBRDEPOSIT     UD_CORREL_DEPOSIT,
    @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_DelProcOP_DEPOSITSITE 
 */



/* 
 * PROCEDURE: sp_DelProcOP_DEPOSITSITE 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSITSITE
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     UD_CORRELATIVOS)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITSITE
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE

    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_DelProcOP_DEPOSITSITEDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_DEPOSITSITEDET 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSITSITEDET
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     UD_CORRELATIVOS,
    @SEQUENCE              UD_REGISTRO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITSITEDET
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       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_DelProcOP_DEPOSITTYPE 
 */



/* 
 * 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_DelProcOP_DOCSUMMARY 
 */



/* 
 * PROCEDURE: sp_DelProcOP_DOCSUMMARY 
 */

CREATE PROCEDURE sp_DelProcOP_DOCSUMMARY
(
    @SITEID               UD_IDSITE,
    @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_DelProcOP_DOCSUMMARYDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_DOCSUMMARYDET 
 */

CREATE PROCEDURE sp_DelProcOP_DOCSUMMARYDET
(
    @SITEID               UD_IDSITE,
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             UD_REGISTRO)
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_DelProcOP_DOCSUMMARYTYPE 
 */



/* 
 * PROCEDURE: sp_DelProcOP_DOCSUMMARYTYPE 
 */

CREATE PROCEDURE sp_DelProcOP_DOCSUMMARYTYPE
(
    @DOCSUMMARYTYPEID        char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DOCSUMMARYTYPE
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID

    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_DelProcOP_LOADSALESORD 
 */



/* 
 * PROCEDURE: sp_DelProcOP_LOADSALESORD 
 */

CREATE PROCEDURE sp_DelProcOP_LOADSALESORD
(
    @NBRLOADSALE        UD_CORRELATIVOS)
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_DelProcOP_LOADSALESORDDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_LOADSALESORDDET 
 */

CREATE PROCEDURE sp_DelProcOP_LOADSALESORDDET
(
    @NBRLOADSALE     UD_CORRELATIVOS)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_LOADSALESORDDET
     WHERE NBRLOADSALE = @NBRLOADSALE

    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_DelProcOP_MODSALES 
 */



/* 
 * 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_DelProcOP_PRCLVLCUST 
 */



/* 
 * PROCEDURE: sp_DelProcOP_PRCLVLCUST 
 */

CREATE PROCEDURE sp_DelProcOP_PRCLVLCUST
(
    @PRCLVLCUSTID           UD_PRCLVLCUSTID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PRCLVLCUST
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID

    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_DelProcOP_PRCLVLCUSTDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE sp_DelProcOP_PRCLVLCUSTDET
(
    @PRCLVLCUSTID      UD_PRCLVLCUSTID,
    @SEQUENCE          UD_REGISTRO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PRCLVLCUSTDET
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SEQUENCE     = @SEQUENCE

    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_DelProcOP_PROMOTIONCARD 
 */



/* 
 * 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_DelProcOP_PROMOTIONCARDTYPE 
 */



/* 
 * 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_DelProcOP_SALES 
 */



/* 
 * PROCEDURE: sp_DelProcOP_SALES 
 */

CREATE PROCEDURE sp_DelProcOP_SALES
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          UD_CORRELATIVOS)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALES
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

    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_DelProcOP_SALESDET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_SALESDET 
 */

CREATE PROCEDURE sp_DelProcOP_SALESDET
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @SEQUENCE           UD_REGISTRO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       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_DelProcOP_SALESPERSON 
 */



/* 
 * PROCEDURE: sp_DelProcOP_SALESPERSON 
 */

CREATE PROCEDURE sp_DelProcOP_SALESPERSON
(
    @SALESPERID         UD_IDVENDEDOR)
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_DelProcOP_SALESPOINTSETING 
 */



/* 
 * PROCEDURE: sp_DelProcOP_SALESPOINTSETING 
 */

CREATE PROCEDURE sp_DelProcOP_SALESPOINTSETING
(
    @SALESPOINT            UD_PTOVTA,
    @SALESPERID            UD_IDVENDEDOR)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESPOINTSETING
     WHERE SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_STATUSPERSON 
 */



/* 
 * PROCEDURE: sp_DelProcOP_STATUSPERSON 
 */

CREATE PROCEDURE sp_DelProcOP_STATUSPERSON
(
    @STATUSPERSONID        char(10))
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_DelProcOP_SUMMARYDAY 
 */



/* 
 * PROCEDURE: sp_DelProcOP_SUMMARYDAY 
 */

CREATE PROCEDURE sp_DelProcOP_SUMMARYDAY
(
    @BATNBRDIARYSUMMARY     UD_CORRELATIVOS,
    @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_DelProcOP_SUMMARYSALES 
 */



/* 
 * PROCEDURE: sp_DelProcOP_SUMMARYSALES 
 */

CREATE PROCEDURE sp_DelProcOP_SUMMARYSALES
(
    @SITEID               UD_IDSITE,
    @DATEPROCE            UD_FECHA)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SUMMARYSALES
     WHERE SITEID    = @SITEID
       AND 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_DelProcOP_TURNVSCARA 
 */



/* 
 * PROCEDURE: sp_DelProcOP_TURNVSCARA 
 */

CREATE PROCEDURE sp_DelProcOP_TURNVSCARA
(
    @SITEID               UD_IDSITE,
    @BATNBRTURNPERSON     UD_TURNOSPERSONAL,
    @DATEPROCE            UD_FECHA,
    @NUMBERTURN           tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_TURNVSCARA
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_TURNVSCARADET 
 */



/* 
 * PROCEDURE: sp_DelProcOP_TURNVSCARADET 
 */

CREATE PROCEDURE sp_DelProcOP_TURNVSCARADET
(
    @SITEID               UD_IDSITE,
    @BATNBRTURNPERSON     UD_TURNOSPERSONAL,
    @DATEPROCE            UD_FECHA,
    @NUMBERTURN           tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_TURNVSCARADET
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('sp_DelProcOP_TURNVSCARADET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_TURNVSCARADET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_TURNVSCARADET >>>'
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_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  'No se puede Eliminar porque tiene registros relacionados'
        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_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_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_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_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_DelProcSITE 
 */

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

    DELETE
      FROM SITE
     WHERE SITEID = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'No se puede Eliminar el Almacén'
        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_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_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_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_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_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_DelProcTMP_IN_VENDORINVT 
 */

CREATE PROCEDURE SP_DelProcTMP_IN_VENDORINVT
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TMP_IN_VENDORINVT
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU

    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_DelProcTMP_IN_VENDORINVTREL 
 */



/* 
 * 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_DelProcTYPEPAINMENT 
 */

CREATE PROCEDURE sp_DelProcTYPEPAINMENT
(
    @TYPEPAINMENTID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TYPEPAINMENT
     WHERE TYPEPAINMENTID = @TYPEPAINMENTID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_InsProcAR_CCDOC 
 */

/* 
 * PROCEDURE: sp_InsProcAR_CCDOC 
 */

CREATE PROCEDURE sp_InsProcAR_CCDOC
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @CURYTYPEID         UD_CURYTYPEID,
    @DOCDATE            UD_FECHA,
    @DATEPROCESALES     UD_FECHA,
    @DUEDATE            UD_FECHA,
    @TOTALPEN           UD_MONTO,
    @TOTALUSD           UD_MONTO,
    @PAIDMENTPEN        UD_MONTO,
    @PAIDMENTUSD        UD_MONTO,
    @DOCVALPEN          UD_MONTO,
    @DOCVALUSD          UD_MONTO,
    @MODSALESID         char(2),
    @CUSTIDSS           UD_CODCLIENT,
    @TAXREGNBR          UD_CODCLIENT               = NULL,
    @DOCSTATUSID        char(2),
    @CURYRATE           UD_CAMBIO,
    @CURYID             UD_CURYID,
    @WEEKDAYPAG         char(12),
    @DAYTHENK           int,
    @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,
                         CURYTYPEID,
                         DOCDATE,
                         DATEPROCESALES,
                         DUEDATE,
                         TOTALPEN,
                         TOTALUSD,
                         PAIDMENTPEN,
                         PAIDMENTUSD,
                         DOCVALPEN,
                         DOCVALUSD,
                         MODSALESID,
                         CUSTIDSS,
                         TAXREGNBR,
                         DOCSTATUSID,
                         CURYRATE,
                         CURYID,
                         WEEKDAYPAG,
                         DAYTHENK,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @CURYTYPEID,
           @DOCDATE,
           @DATEPROCESALES,
           @DUEDATE,
           @TOTALPEN,
           @TOTALUSD,
           @PAIDMENTPEN,
           @PAIDMENTUSD,
           @DOCVALPEN,
           @DOCVALUSD,
           @MODSALESID,
           @CUSTIDSS,
           @TAXREGNBR,
           @DOCSTATUSID,
           @CURYRATE,
           @CURYID,
           @WEEKDAYPAG,
           @DAYTHENK,
           @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_InsProcAR_CCDOCDET 
 */



/* 
 * PROCEDURE: sp_InsProcAR_CCDOCDET 
 */

CREATE PROCEDURE sp_InsProcAR_CCDOCDET
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           UD_CORRELATIVOS,
    @SEQUENCE              UD_REGISTRO,
    @CURYTYPEID            UD_CURYTYPEID,
    @DOCSTATUSID           char(2),
    @DOCDATE               UD_FECHA,
    @DATEPROCEPAIDMENT     UD_FECHA,
    @DATEPROCESALES        UD_FECHA,
    @BANKID                char(2),
    @CHEQNUMBER            char(15),
    @NBRBANKACCOUNT        char(15),
    @TOTALPEN              UD_MONTO,
    @TOTALUSD              UD_MONTO,
    @PAIDMENTPEN           UD_MONTO,
    @PAIDMENTUSD           UD_MONTO,
    @CURYID                UD_CURYID,
    @CURYRATE              UD_CAMBIO,
    @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,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SEQUENCE,
           @CURYTYPEID,
           @DOCSTATUSID,
           @DOCDATE,
           @DATEPROCEPAIDMENT,
           @DATEPROCESALES,
           @BANKID,
           @CHEQNUMBER,
           @NBRBANKACCOUNT,
           @TOTALPEN,
           @TOTALUSD,
           @PAIDMENTPEN,
           @PAIDMENTUSD,
           @CURYID,
           @CURYRATE,
           @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_InsProcBANK 
 */



/* 
 * PROCEDURE: sp_InsProcBANK 
 */

CREATE PROCEDURE sp_InsProcBANK
(
    @BANKID          char(2),
    @DESCR           UD_NOMBRE,
    @BANKACCOUNT     UD_ACCOUNTBANK,
    @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,
                     BANKACCOUNT,
                     USER01,
                     USER02,
                     USER03,
                     USER04 
                     )
    VALUES(@BANKID,
           @DESCR,
           @BANKACCOUNT,
           @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_InsProcCITY 
 */

CREATE PROCEDURE SP_InsProcCITY
(
    @CITYID      char(3),
    @STATEID     char(2),
    @DESCR       UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CITY(CITYID,
                     STATEID,
                     DESCR 
                     )
    VALUES(@CITYID,
           @STATEID,
           @DESCR 
           )

    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_InsProcCLASSCUST 
 */

CREATE PROCEDURE SP_InsProcCLASSCUST
(
    @CLASSCUSTID     char(2),
    @DESCR           char(2))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CLASSCUST(CLASSCUSTID,
                          DESCR 
                          )
    VALUES(@CLASSCUSTID,
           @DESCR 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: SP_InsProcCOUNTRY 
 */

CREATE PROCEDURE SP_InsProcCOUNTRY
(
    @COUNTRYID     char(3),
    @DESCR         UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO COUNTRY(COUNTRYID,
                        DESCR 
                        )
    VALUES(@COUNTRYID,
           @DESCR 
           )

    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_InsProcCURRENCY 
 */

CREATE PROCEDURE SP_InsProcCURRENCY
(
    @CURYID         UD_CURYID,
    @DESCR          UD_DESCRIPCION,
    @DESCRMONEY     char(5),
    @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,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@CURYID,
           @DESCR,
           @DESCRMONEY,
           @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_InsProcCURYRATE 
 */

CREATE PROCEDURE SP_InsProcCURYRATE
(
    @CURYTYPEID     char(6),
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime,
    @RATE           numeric(6, 4),
    @OPERADOR       char(1)                  = NULL,
    @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,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@CURYTYPEID,
           @FROMCURYID,
           @TOCURYID,
           @CURYDATE,
           @RATE,
           @OPERADOR,
           @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_InsProcCURYRATETYPE 
 */

CREATE PROCEDURE SP_InsProcCURYRATETYPE
(
    @CURYTYPEID     char(6),
    @DESCR          char(30)             = NULL,
    @USER4          UD_USER7             = NULL,
    @USER1          UD_USER1             = NULL,
    @USER2          UD_USER2             = NULL,
    @USER3          UD_USER6             = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CURYRATETYPE(CURYTYPEID,
                             DESCR,
                             USER4,
                             USER1,
                             USER2,
                             USER3 
                             )
    VALUES(@CURYTYPEID,
           @DESCR,
           @USER4,
           @USER1,
           @USER2,
           @USER3 
           )

    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_InsProcCUST_LIMIT 
 */



/* 
 * PROCEDURE: sp_InsProcCUST_LIMIT 
 */

CREATE PROCEDURE sp_InsProcCUST_LIMIT
(
    @CUSTIDSS        UD_CODCLIENT,
    @SITEID          UD_IDSITE,
    @CREDLIMITED     UD_MONTO,
    @USER1           UD_USER1                = NULL,
    @USER2           UD_USER2                = NULL,
    @USER3           UD_USER6                = NULL,
    @USER4           UD_USER7                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUST_LIMIT(CUSTIDSS,
                           SITEID,
                           CREDLIMITED,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@CUSTIDSS,
           @SITEID,
           @CREDLIMITED,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: SP_InsProcCUSTOMER 
 */

CREATE PROCEDURE SP_InsProcCUSTOMER
(
    @CUSTIDSS         UD_CODCLIENT,
    @CUSTID           UD_CODCLIENT,
    @NAME             UD_NOMBRE                = NULL,
    @ADDR1            UD_DIRECCION,
    @ADDR2            UD_DIRECCION,
    @TAXREGNBR        UD_CODCLIENT,
    @DATEORIG         datetime,
    @DATEPROC         datetime,
    @PHONE            char(10),
    @FAX              char(10),
    @ZONECUSTOMER     char(2),
    @SLSPERID         UD_IDVENDEDOR,
    @FORMPAG          char(2),
    @TYPEPAG          char(2),
    @TERMSID          char(2),
    @CURYID           UD_CURYID,
    @CRLMT            numeric(9, 2),
    @CLASSCUSTID      char(2),
    @PRCLVLCUSTID     UD_PRECLVID,
    @WEEKDAYPAG       char(12),
    @THANKDAY         int)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTOMER(CUSTIDSS,
                         CUSTID,
                         NAME,
                         ADDR1,
                         ADDR2,
                         TAXREGNBR,
                         DATEORIG,
                         DATEPROC,
                         PHONE,
                         FAX,
                         ZONECUSTOMER,
                         SLSPERID,
                         FORMPAG,
                         TYPEPAG,
                         TERMSID,
                         CURYID,
                         CRLMT,
                         CLASSCUSTID,
                         PRCLVLCUSTID,
                         WEEKDAYPAG,
                         THANKDAY 
                         )
    VALUES(@CUSTIDSS,
           @CUSTID,
           @NAME,
           @ADDR1,
           @ADDR2,
           @TAXREGNBR,
           @DATEORIG,
           @DATEPROC,
           @PHONE,
           @FAX,
           @ZONECUSTOMER,
           @SLSPERID,
           @FORMPAG,
           @TYPEPAG,
           @TERMSID,
           @CURYID,
           @CRLMT,
           @CLASSCUSTID,
           @PRCLVLCUSTID,
           @WEEKDAYPAG,
           @THANKDAY 
           )

    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_InsProcCUSTSTATUS 
 */



/* 
 * PROCEDURE: sp_InsProcCUSTSTATUS 
 */

CREATE PROCEDURE sp_InsProcCUSTSTATUS
(
    @CUSTSTATUSID     char(1),
    @DESCRCUST        UD_DESCRIPCION,
    @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,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@CUSTSTATUSID,
           @DESCRCUST,
           @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_InsProcCUSTWARRANTY 
 */



/* 
 * PROCEDURE: sp_InsProcCUSTWARRANTY 
 */

CREATE PROCEDURE sp_InsProcCUSTWARRANTY
(
    @CUSTIDSS               UD_CODCLIENT,
    @CUSTGARANTIATYPEID     char(2),
    @TOTGAR                 UD_MONTO,
    @DESCRGAR               UD_DESCRIPCION,
    @EXPIREDATE             UD_FECHA,
    @CURYID                 UD_CURYID,
    @USER1                  UD_USER1                  = NULL,
    @USER2                  UD_USER2                  = NULL,
    @USER3                  UD_USER6                  = NULL,
    @USER4                  UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTWARRANTY(CUSTIDSS,
                             CUSTGARANTIATYPEID,
                             TOTGAR,
                             DESCRGAR,
                             EXPIREDATE,
                             CURYID,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CUSTIDSS,
           @CUSTGARANTIATYPEID,
           @TOTGAR,
           @DESCRGAR,
           @EXPIREDATE,
           @CURYID,
           @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_InsProcCUSTWARRANTYTYPE 
 */



/* 
 * PROCEDURE: sp_InsProcCUSTWARRANTYTYPE 
 */

CREATE PROCEDURE sp_InsProcCUSTWARRANTYTYPE
(
    @CUSTGARANTIATYPEID     char(2),
    @DECRCUSTGARANTIA       binary(20),
    @USER1                  UD_USER1              = NULL,
    @USER2                  UD_USER2              = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTWARRANTYTYPE(CUSTGARANTIATYPEID,
                                 DECRCUSTGARANTIA,
                                 USER1,
                                 USER2 
                                 )
    VALUES(@CUSTGARANTIATYPEID,
           @DECRCUSTGARANTIA,
           @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_InsProcDOCTYPE 
 */

CREATE PROCEDURE SP_InsProcDOCTYPE
(
    @DOCTYPEID     UD_DOCTYPEID,
    @DESCR         UD_DESCRIPCION,
    @MINDES        char(5),
    @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,
                        USER1,
                        USER2,
                        USER3,
                        USER4 
                        )
    VALUES(@DOCTYPEID,
           @DESCR,
           @MINDES,
           @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_InsProcIN_CATEGORY 
 */

CREATE PROCEDURE SP_InsProcIN_CATEGORY
(
    @CLASSID     char(5),
    @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 IN_CATEGORY(CLASSID,
                            DESCR,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CLASSID,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Código de Categoría ya Existe '
        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_InsProcIN_CLASSVENDOR 
 */

CREATE PROCEDURE SP_InsProcIN_CLASSVENDOR
(
    @CLASSVENDORID     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 IN_CLASSVENDOR(CLASSVENDORID,
                               DESCR,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@CLASSVENDORID,
           @DESCR,
           @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_InsProcIN_CLUSTER 
 */



/* 
 * 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_InsProcIN_DOCSTATUS 
 */

CREATE PROCEDURE SP_InsProcIN_DOCSTATUS
(
    @DOCSTATUSID     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 IN_DOCSTATUS(DOCSTATUSID,
                             DESCR,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@DOCSTATUSID,
           @DESCR,
           @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_InsProcIN_INTRAHEAD 
 */

CREATE PROCEDURE SP_InsProcIN_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @VENDIDSS          UD_IDVENDOR,
    @DOCTYPEID         UD_DOCTYPEID,
    @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,
    @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,
                             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,
           @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_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                    = NULL,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STKFISI           bit,
    @INTRDATE          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @RLSED             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,
    @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,
                             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,
           @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_InsProcIN_INVENTHISTOPHISI 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTHISTOPHISI
(
    @SITEID               UD_IDSITE,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @CLASSID              char(5),
    @CLASSUBID            char(5),
    @STOCKPHISICAL        UD_STOCK,
    @QTYAVAIAL            UD_STOCK,
    @INTRDATEPHISICAL     smalldatetime,
    @INTRDATEADJUST       smalldatetime)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTHISTOPHISI(SITEID,
                                    INVTIDSKU,
                                    CLASSID,
                                    CLASSUBID,
                                    STOCKPHISICAL,
                                    QTYAVAIAL,
                                    INTRDATEPHISICAL,
                                    INTRDATEADJUST)
    VALUES(@SITEID,
           @INVTIDSKU,
           @CLASSID,
           @CLASSUBID,
           @STOCKPHISICAL,
           @QTYAVAIAL,
           @INTRDATEPHISICAL,
           @INTRDATEADJUST)

    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_InsProcIN_INVENTORY 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORY
(
    @INVTIDSKU       UD_IDCORPORATIVO,
    @TAXISC          UD_TAXID                    = NULL,
    @TAXIGV          UD_TAXID,
    @CLASSUBID       char(5),
    @INVTYPEID       char(5),
    @STKUNITID       UD_STKUNITID,
    @CLASSID         char(5),
    @DESCR           varchar(40),
    @WTSTK1          numeric(7, 3),
    @STKFUELS        bit,
    @STKSTORE        bit,
    @STKOTHER        bit,
    @STKITEM         bit,
    @KIT             bit,
    @DISCDSCTO1      UD_PORCENTAJE,
    @DISCDSCTO2      UD_PORCENTAJE,
    @DISCMTO         numeric(12, 2),
    @STKRECEPT       bit,
    @STKSALES        bit,
    @STATUS          bit,
    @CONSIGNMENT     bit,
    @DISASSEMBLE     bit,
    @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 IN_INVENTORY(INVTIDSKU,
                             TAXISC,
                             TAXIGV,
                             CLASSUBID,
                             INVTYPEID,
                             STKUNITID,
                             CLASSID,
                             DESCR,
                             WTSTK1,
                             STKFUELS,
                             STKSTORE,
                             STKOTHER,
                             STKITEM,
                             KIT,
                             DISCDSCTO1,
                             DISCDSCTO2,
                             DISCMTO,
                             STKRECEPT,
                             STKSALES,
                             STATUS,
                             CONSIGNMENT,
                             DISASSEMBLE,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9 
                             )
    VALUES(@INVTIDSKU,
           @TAXISC,
           @TAXIGV,
           @CLASSUBID,
           @INVTYPEID,
           @STKUNITID,
           @CLASSID,
           @DESCR,
           @WTSTK1,
           @STKFUELS,
           @STKSTORE,
           @STKOTHER,
           @STKITEM,
           @KIT,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTO,
           @STKRECEPT,
           @STKSALES,
           @STATUS,
           @CONSIGNMENT,
           @DISASSEMBLE,
           @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_InsProcIN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYALTER
(
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @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,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@INVTIDBARR,
           @INVTIDSKU,
           @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_InsProcIN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYDAT
(
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @STOCKSTAR          UD_STOCK,
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       UD_FECHA                    = NULL,
    @ENDDATEINV         UD_FECHA                    = NULL,
    @LASTINVFIS         UD_STOCK                    = NULL,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         UD_COSTO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @LASTSTDCOSTPEN     UD_COSTO,
    @LASTSTDCOSTUSD     UD_COSTO,
    @SLS_PRICE          UD_PRECIO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID,
    @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,
                                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,
           @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_InsProcIN_INVENTORYDES 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYDES
(
    @INVTIDSKU        UD_IDCORPORATIVO,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint,
    @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,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@INVTIDSKU,
           @INVTIDSKUDES,
           @WTSTK2,
           @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_InsProcIN_INVENTORYTYPE 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYTYPE
(
    @INVTYPEID     char(5),
    @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 IN_INVENTORYTYPE(INVTYPEID,
                                 DESCR,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@INVTYPEID,
           @DESCR,
           @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_InsProcIN_INVENTORYUNITMED 
 */

CREATE PROCEDURE SP_InsProcIN_INVENTORYUNITMED
(
    @STKUNITID     UD_STKUNITID,
    @DESCR         UD_DESCRIPCION,
    @STKDIVIDE 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,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@STKUNITID,
           @DESCR,
           @STKDIVIDE,
           @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_InsProcIN_INVENTPHISICAL 
 */

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

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

    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_InsProcIN_KDXHIST 
 */

CREATE PROCEDURE SP_InsProcIN_KDXHIST
(
    @YEARHIST      smallint,
    @SITEID        UD_IDSITE,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @QTYEND00      UD_STOCK,
    @QTYEND01      UD_STOCK,
    @QTYEND02      UD_STOCK,
    @QTYEND03      UD_STOCK,
    @QTYEND04      UD_STOCK,
    @QTYEND05      UD_STOCK,
    @QTYEND06      UD_STOCK,
    @QTYEND07      UD_STOCK,
    @QTYEND08      UD_STOCK,
    @QTYEND09      UD_STOCK,
    @QTYEND10      UD_STOCK,
    @QTYEND11      UD_STOCK,
    @QTYEND12      UD_STOCK,
    @QTYEND13      UD_STOCK,
    @AVGPEN00      UD_COSTO,
    @AVGPEN01      UD_COSTO,
    @AVGPEN02      UD_COSTO,
    @AVGPEN03      UD_COSTO,
    @AVGPEN04      UD_COSTO,
    @AVGPEN06      UD_COSTO,
    @AVGPEN05      UD_COSTO,
    @AVGPEN07      UD_COSTO,
    @AVGPEN08      UD_COSTO,
    @AVGPEN09      UD_COSTO,
    @AVGPEN10      UD_COSTO,
    @AVGPEN11      UD_COSTO,
    @AVGPEN12      UD_COSTO,
    @AVGPEN13      UD_COSTO,
    @AVGUSD00      UD_COSTO,
    @AVGUSD01      UD_COSTO,
    @AVGUSD02      UD_COSTO,
    @AVGUSD03      UD_COSTO,
    @AVGUSD04      UD_COSTO,
    @AVGUSD05      UD_COSTO,
    @AVGUSD06      UD_COSTO,
    @AVGUSD07      UD_COSTO,
    @AVGUSD08      UD_COSTO,
    @AVGUSD09      UD_COSTO,
    @AVGUSD10      UD_COSTO,
    @AVGUSD11      UD_COSTO,
    @AVGUSD12      UD_COSTO,
    @AVGUSD13      UD_COSTO,
    @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,
                           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,
           @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_InsProcIN_KIT 
 */

CREATE PROCEDURE SP_InsProcIN_KIT
(
    @KITID          UD_KITID,
    @STKUNITID      UD_STKUNITID,
    @CURYID         UD_CURYID,
    @TAXID          UD_TAXID,
    @DESCR          UD_DESCRIPCION,
    @MTOCTOREPS     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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,
                       USER1,
                       USER2,
                       USER3,
                       USER4 
                       )
    VALUES(@KITID,
           @STKUNITID,
           @CURYID,
           @TAXID,
           @DESCR,
           @MTOCTOREPS,
           @STDCOSTUSD,
           @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_InsProcIN_KITCOMPONENT 
 */

CREATE PROCEDURE SP_InsProcIN_KITCOMPONENT
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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 IN_KITCOMPONENT(KITID,
                                INVTIDSKU,
                                SEQUENCE,
                                STDCOSTPEN,
                                STDCOSTUSD,
                                QTY,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@KITID,
           @INVTIDSKU,
           @SEQUENCE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @QTY,
           @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_InsProcIN_PRICELEVELTEM 
 */

CREATE PROCEDURE SP_InsProcIN_PRICELEVELTEM
(
    @INVTIDSKU             UD_IDCORPORATIVO,
    @SITEID                UD_IDSITE,
    @CORRNBR               char(10),
    @PRECLVID              UD_PRECLVID,
    @CURYID                UD_CURYID,
    @DESCR                 UD_DESCRIPCION,
    @SLSPRICE              numeric(6, 2),
    @STARTDATEPRICE        smalldatetime,
    @ENDDATEPRICE          smalldatetime,
    @STARTTIMEPRICE        tinyint,
    @ENDTIMEPRICE          tinyint,
    @STATUS                bit,
    @STKPRICESEND          bit,
    @USERIDAUTORIZED       char(10),
    @DATETIMEAUTORIZED     smalldatetime,
    @SITEORIG              UD_IDSITE,
    @STKPRICESS            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_PRICELEVELTEM(INVTIDSKU,
                                 SITEID,
                                 CORRNBR,
                                 PRECLVID,
                                 CURYID,
                                 DESCR,
                                 SLSPRICE,
                                 STARTDATEPRICE,
                                 ENDDATEPRICE,
                                 STARTTIMEPRICE,
                                 ENDTIMEPRICE,
                                 STATUS,
                                 STKPRICESEND,
                                 USERIDAUTORIZED,
                                 DATETIMEAUTORIZED,
                                 SITEORIG,
                                 STKPRICESS,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4,
                                 USER5,
                                 USER6,
                                 USER7,
                                 USER8,
                                 USER9 
                                 )
    VALUES(@INVTIDSKU,
           @SITEID,
           @CORRNBR,
           @PRECLVID,
           @CURYID,
           @DESCR,
           @SLSPRICE,
           @STARTDATEPRICE,
           @ENDDATEPRICE,
           @STARTTIMEPRICE,
           @ENDTIMEPRICE,
           @STATUS,
           @STKPRICESEND,
           @USERIDAUTORIZED,
           @DATETIMEAUTORIZED,
           @SITEORIG,
           @STKPRICESS,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    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_InsProcIN_PRICELEVELTEMDET 
 */



/* 
 * PROCEDURE: sp_InsProcIN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE sp_InsProcIN_PRICELEVELTEMDET
(
    @BATNBRPRICELEVELTEM     UD_CORRELATIVOS,
    @SITEID                  UD_IDSITE,
    @SEQUENCE                UD_REGISTRO,
    @INVTIDSKU               UD_IDCORPORATIVO            = NULL,
    @DESCR                   UD_DESCRIPCION,
    @SLS_PRICE               UD_PRECIO,
    @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,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@BATNBRPRICELEVELTEM,
           @SITEID,
           @SEQUENCE,
           @INVTIDSKU,
           @DESCR,
           @SLS_PRICE,
           @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_InsProcIN_PRICELEVELTYPE 
 */

CREATE PROCEDURE SP_InsProcIN_PRICELEVELTYPE
(
    @PRECLVID       UD_PRECLVID,
    @CURYID         UD_CURYID,
    @CURYTYPEID     char(6),
    @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 IN_PRICELEVELTYPE(PRECLVID,
                                  CURYID,
                                  CURYTYPEID,
                                  DESCR,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@PRECLVID,
           @CURYID,
           @CURYTYPEID,
           @DESCR,
           @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_InsProcIN_SETUP 
 */

CREATE PROCEDURE SP_InsProcIN_SETUP
(
    @PERPOST               UD_PERPOST,
    @INTYPETRANSFERIN      UD_TYPEDOC,
    @INTYPETRANSFEROUT     UD_TYPEDOC,
    @RUTAREPORTES          char(50),
    @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)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_SETUP(PERPOST,
                         INTYPETRANSFERIN,
                         INTYPETRANSFEROUT,
                         RUTAREPORTES,
                         INTYPESALES,
                         INTYPEDESIN,
                         INTYPEDESOUT,
                         INTYPECREDMEM,
                         INTYPEBONUS,
                         INTYPEFISIN,
                         INTYPEFISOUT,
                         INTYPETRBUYSTORE,
                         INTYPETRBUYFUELS,
                         BATNBRTYPEDOC,
                         BATNBRTYPEPRICE,
                         STKFISI,
                         INTYPEQTYINI)
    VALUES(@PERPOST,
           @INTYPETRANSFERIN,
           @INTYPETRANSFEROUT,
           @RUTAREPORTES,
           @INTYPESALES,
           @INTYPEDESIN,
           @INTYPEDESOUT,
           @INTYPECREDMEM,
           @INTYPEBONUS,
           @INTYPEFISIN,
           @INTYPEFISOUT,
           @INTYPETRBUYSTORE,
           @INTYPETRBUYFUELS,
           @BATNBRTYPEDOC,
           @BATNBRTYPEPRICE,
           @STKFISI,
           @INTYPEQTYINI)

    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_SUBCATEGORY 
 */

CREATE PROCEDURE SP_InsProcIN_SUBCATEGORY
(
    @CLASSUBID     char(5),
    @CLASSID       char(5),
    @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 IN_SUBCATEGORY(CLASSUBID,
                               CLASSID,
                               DESCR,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@CLASSUBID,
           @CLASSID,
           @DESCR,
           @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_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),
    @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,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@TYPEDOCID,
           @DESCR,
           @INOUT,
           @STKVALUE,
           @STKCOSPRICE,
           @STKAVGCOST,
           @PRECLVLID,
           @STKBUY,
           @STKVENDOR,
           @STKAVERAGE,
           @DESCR_ABR,
           @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_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	UD_CURYID,
    @CURYTYPEID	UD_CURYTYPEID,
    @TAXID	UD_TAXID,	
    @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,
                          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,
           @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_InsProcIN_VENDORINVT 
 */

CREATE PROCEDURE SP_InsProcIN_VENDORINVT
(
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @STDCOST       UD_COSTO,
    @CURYID        UD_CURYID,
    @BUYDATE       smalldatetime               = NULL,
    @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,
                              BUYCOD,
                              DESCR,
                              STDCOST,
                              CURYID,
                              BUYDATE,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@VENDIDSS,
           @INVTIDSKU,
           @BUYCOD,
           @DESCR,
           @STDCOST,
           @CURYID,
           @BUYDATE,
           @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_InsProcINTRAMOV 
 */

CREATE PROCEDURE SP_InsProcINTRAMOV
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @BATNBR            char(10),
    @INTRDATE          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @TYPEDOCID         UD_TYPEDOC,
    @CURYRATE          UD_CAMBIO,
    @CURYDATE          smalldatetime,
    @PERPOST           UD_PERPOST,
    @SWTINOUT          bit,
    @QTY               UD_STOCK,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STDCOSTPEN        UD_COSTO                    = NULL,
    @COMPONENT         bit,
    @SLSPRICE          UD_PRECIO,
    @REFNBR            char(15)                    = NULL,
    @DOCTYPEID         UD_DOCTYPEID                = 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 INTRAMOV(SITEID,
                         INVTIDSKU,
                         BATNBR,
                         INTRDATE,
                         INTRDATEPROCE,
                         TYPEDOCID,
                         CURYRATE,
                         CURYDATE,
                         PERPOST,
                         SWTINOUT,
                         QTY,
                         STDCOSTUSD,
                         STDCOSTPEN,
                         COMPONENT,
                         SLSPRICE,
                         REFNBR,
                         DOCTYPEID,
                         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,
           @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_InsProcOP_CARA_FUEL 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CARA_FUEL 
 */

CREATE PROCEDURE sp_InsProcOP_CARA_FUEL
(
    @CARA           UD_CARA,
    @INVTIDBARR     UD_CODIGOBARRA            = NULL,
    @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_CARA_FUEL(CARA,
                             INVTIDBARR,
                             DESCR,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CARA,
           @INVTIDBARR,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_InsProcOP_CARD 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CARD 
 */

CREATE PROCEDURE sp_InsProcOP_CARD
(
    @CARDID          char(2),
    @ACCOUNT         char(12),
    @DESCR           UD_NOMBRE,
    @NUMBERORDER     tinyint,
    @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,
                        USER01,
                        USER02,
                        USER03,
                        USER04 
                        )
    VALUES(@CARDID,
           @ACCOUNT,
           @DESCR,
           @NUMBERORDER,
           @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_InsProcOP_CARD_MONT 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CARD_MONT 
 */

CREATE PROCEDURE sp_InsProcOP_CARD_MONT
(
    @SITEID        UD_IDSITE,
    @CARDID        char(2),
    @DATE          UD_FECHA,
    @DATEPROCE     UD_FECHA,
    @CURYID        UD_CURYID,
    @TOTALCARD     UD_MONTO,
    @STATUS        bit                  = NULL,
    @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,
                             DATE,
                             DATEPROCE,
                             CURYID,
                             TOTALCARD,
                             STATUS,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@SITEID,
           @CARDID,
           @DATE,
           @DATEPROCE,
           @CURYID,
           @TOTALCARD,
           @STATUS,
           @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_InsProcOP_CCSALESPERSON 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CCSALESPERSON 
 */

CREATE PROCEDURE sp_InsProcOP_CCSALESPERSON
(
    @SITEID         UD_IDSITE,
    @SALESPERID     UD_IDVENDEDOR,
    @CURYID         UD_CURYID,
    @TOTAL          UD_MONTO,
    @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,
                                 SALESPERID,
                                 CURYID,
                                 TOTAL,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @SALESPERID,
           @CURYID,
           @TOTAL,
           @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_InsProcOP_CCSALESPERSONDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CCSALESPERSONDET 
 */

CREATE PROCEDURE sp_InsProcOP_CCSALESPERSONDET
(
    @SITEID                  UD_IDSITE,
    @SALESPERID              UD_IDVENDEDOR,
    @SEQUENCE                UD_REGISTRO,
    @CCSALESPERSONTYPEID     char(2),
    @DATEPROCE               UD_FECHA,
    @OBS                     char(100)                = NULL,
    @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,
                                    SALESPERID,
                                    SEQUENCE,
                                    CCSALESPERSONTYPEID,
                                    DATEPROCE,
                                    OBS,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@SITEID,
           @SALESPERID,
           @SEQUENCE,
           @CCSALESPERSONTYPEID,
           @DATEPROCE,
           @OBS,
           @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_InsProcOP_CCSALESPERSONTYPE 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CCSALESPERSONTYPE 
 */

CREATE PROCEDURE sp_InsProcOP_CCSALESPERSONTYPE
(
    @CCSALESPERSONTYPEID        char(2),
    @DESCRCCSALESPERSONTYPE     UD_DESCRIPCION,
    @DEBE                       UD_DEBE_HABER,
    @HABER                      UD_DEBE_HABER,
    @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,
                                     DEBE,
                                     HABER,
                                     USER1,
                                     USER2,
                                     USER3,
                                     USER4 
                                     )
    VALUES(@CCSALESPERSONTYPEID,
           @DESCRCCSALESPERSONTYPE,
           @DEBE,
           @HABER,
           @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_InsProcOP_CORELATIVE 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CORELATIVE 
 */

CREATE PROCEDURE sp_InsProcOP_CORELATIVE
(
    @TYPECORELATIVE     char(20),
    @NBRCORELATIVE      UD_CORRELATIVOS,
    @USER01             UD_USER1                   = NULL,
    @USER02             UD_USER2                   = NULL,
    @USER03             UD_USER6                   = NULL,
    @USER04             UD_USER7                   = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CORELATIVE(TYPECORELATIVE,
                              NBRCORELATIVE,
                              USER01,
                              USER02,
                              USER03,
                              USER04 
                              )
    VALUES(@TYPECORELATIVE,
           @NBRCORELATIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    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_InsProcOP_CTRLFUEL 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CTRLFUEL 
 */

CREATE PROCEDURE sp_InsProcOP_CTRLFUEL
(
    @SITEID               UD_IDSITE,
    @BATNBRCTRLFUEL       UD_CORRELATIVOS,
    @DATEPROCE            UD_FECHA,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @STOCKINITIAL         UD_STOCK,
    @QTYIMPUT             UD_STOCK,
    @QTYOUTPUT            UD_STOCK,
    @QTYSALES             UD_STOCK,
    @QTYAVAIL             UD_STOCK,
    @STKUNITID            UD_STKUNITID,
    @VALUEUNIMED          numeric(7, 4),
    @QTYDIFFERENCE        UD_STOCK,
    @PORCENTDIFERENCE     UD_PORCENTAJE,
    @REMARK               UD_DESCRIPCION,
    @STATUS               bit                         = NULL,
    @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,
                            BATNBRCTRLFUEL,
                            DATEPROCE,
                            INVTIDSKU,
                            STOCKINITIAL,
                            QTYIMPUT,
                            QTYOUTPUT,
                            QTYSALES,
                            QTYAVAIL,
                            STKUNITID,
                            VALUEUNIMED,
                            QTYDIFFERENCE,
                            PORCENTDIFERENCE,
                            REMARK,
                            STATUS,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@SITEID,
           @BATNBRCTRLFUEL,
           @DATEPROCE,
           @INVTIDSKU,
           @STOCKINITIAL,
           @QTYIMPUT,
           @QTYOUTPUT,
           @QTYSALES,
           @QTYAVAIL,
           @STKUNITID,
           @VALUEUNIMED,
           @QTYDIFFERENCE,
           @PORCENTDIFERENCE,
           @REMARK,
           @STATUS,
           @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_InsProcOP_CTRLFUELDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CTRLFUELDET 
 */

CREATE PROCEDURE sp_InsProcOP_CTRLFUELDET
(
    @SITEID             UD_IDSITE,
    @BATNBRCTRLFUEL     UD_CORRELATIVOS,
    @SEQUENCE           tinyint,
    @CURYID             UD_CURYID,
    @QTYSALES           UD_STOCK,
    @SLS_PRICE          UD_PRECIO,
    @STATUS             bit                        = NULL,
    @USER1              UD_USER1                   = NULL,
    @USER2              UD_USER2                   = NULL,
    @USER3              UD_USER6                   = NULL,
    @USER4              UD_USER7                   = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLFUELDET(SITEID,
                               BATNBRCTRLFUEL,
                               SEQUENCE,
                               CURYID,
                               QTYSALES,
                               SLS_PRICE,
                               STATUS,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@SITEID,
           @BATNBRCTRLFUEL,
           @SEQUENCE,
           @CURYID,
           @QTYSALES,
           @SLS_PRICE,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    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_InsProcOP_CTRLTURN 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CTRLTURN 
 */

CREATE PROCEDURE sp_InsProcOP_CTRLTURN
(
    @BATNBRTURN     UD_CORR_CONTOMET,
    @SALESPERID     UD_IDVENDEDOR,
    @SITEID         UD_IDSITE,
    @DATEPROCES     UD_FECHA,
    @NBRTUR         char(1),
    @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,
                            SALESPERID,
                            SITEID,
                            DATEPROCES,
                            NBRTUR,
                            USER01,
                            USER02,
                            USER03,
                            USER04 
                            )
    VALUES(@BATNBRTURN,
           @SALESPERID,
           @SITEID,
           @DATEPROCES,
           @NBRTUR,
           @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_InsProcOP_CTRLTURNDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CTRLTURNDET 
 */

CREATE PROCEDURE sp_InsProcOP_CTRLTURNDET
(
    @BATNBRTURN       UD_CORR_CONTOMET,
    @CARA             UD_CARA,
    @INVTIDSKU        UD_IDCORPORATIVO,
    @STARTREADING     numeric(13, 3),
    @ENDREADING       numeric(13, 3),
    @QTYSALES         UD_STOCK,
    @QTYSERAF         UD_STOCK,
    @QTYOTHER         UD_STOCK,
    @TOTQTY           UD_STOCK,
    @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,
                               CARA,
                               INVTIDSKU,
                               STARTREADING,
                               ENDREADING,
                               QTYSALES,
                               QTYSERAF,
                               QTYOTHER,
                               TOTQTY,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@BATNBRTURN,
           @CARA,
           @INVTIDSKU,
           @STARTREADING,
           @ENDREADING,
           @QTYSALES,
           @QTYSERAF,
           @QTYOTHER,
           @TOTQTY,
           @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_InsProcOP_CUSTCARD 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CUSTCARD 
 */

CREATE PROCEDURE sp_InsProcOP_CUSTCARD
(
    @CUSTIDSS          UD_CODCLIENT,
    @EXPIREDATE        UD_FECHA,
    @DESCRCUSTCARD     UD_DESCRIPCION,
    @CARDPASSWORD      char(10),
    @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,
                            EXPIREDATE,
                            DESCRCUSTCARD,
                            CARDPASSWORD,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CUSTIDSS,
           @EXPIREDATE,
           @DESCRCUSTCARD,
           @CARDPASSWORD,
           @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_InsProcOP_CUSTCLASS 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CUSTCLASS 
 */

CREATE PROCEDURE sp_InsProcOP_CUSTCLASS
(
    @CUSTCLASSID     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_CUSTCLASS(CUSTCLASSID,
                             DESCR,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CUSTCLASSID,
           @DESCR,
           @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_InsProcOP_CUSTZONE 
 */



/* 
 * PROCEDURE: sp_InsProcOP_CUSTZONE 
 */

CREATE PROCEDURE sp_InsProcOP_CUSTZONE
(
    @CUSTZONEID     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_CUSTZONE(CUSTZONEID,
                            DESCR,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CUSTZONEID,
           @DESCR,
           @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_InsProcOP_DEFINITIONPD 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DEFINITIONPD 
 */

CREATE PROCEDURE sp_InsProcOP_DEFINITIONPD
(
    @DEFINITIONID     char(2),
    @CUSTIDSS         UD_CODCLIENT                = NULL,
    @CLASSID          UD_CATEGORY                 = NULL,
    @CLASSUBID        char(5)                     = NULL,
    @INVTIDSKU        UD_IDCORPORATIVO            = NULL,
    @DEBE             UD_DEBE_HABER,
    @HABER            UD_DEBE_HABER,
    @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,
                                DEBE,
                                HABER,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@DEFINITIONID,
           @CUSTIDSS,
           @CLASSID,
           @CLASSUBID,
           @INVTIDSKU,
           @DEBE,
           @HABER,
           @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_InsProcOP_DEPOSIT 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DEPOSIT 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSIT
(
    @BATNBRDEPOSIT       UD_CORREL_DEPOSIT,
    @SITEID              UD_IDSITE,
    @CURYID              UD_CURYID                    = NULL,
    @SALESPERSONTURN     tinyint,
    @DEPOSITTYPEID       char(1),
    @SALESPERID          UD_IDVENDEDOR,
    @OPDATE              UD_FECHA,
    @DATEPROCE           UD_FECHA,
    @TOTPEN              UD_MONTO,
    @TOTUSD              UD_MONTO,
    @STATUS              bit                          = NULL,
    @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,
                           CURYID,
                           SALESPERSONTURN,
                           DEPOSITTYPEID,
                           SALESPERID,
                           OPDATE,
                           DATEPROCE,
                           TOTPEN,
                           TOTUSD,
                           STATUS,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@BATNBRDEPOSIT,
           @SITEID,
           @CURYID,
           @SALESPERSONTURN,
           @DEPOSITTYPEID,
           @SALESPERID,
           @OPDATE,
           @DATEPROCE,
           @TOTPEN,
           @TOTUSD,
           @STATUS,
           @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_InsProcOP_DEPOSITDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DEPOSITDET 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSITDET
(
    @BATNBRDEPOSIT     UD_CORREL_DEPOSIT,
    @SITEID            UD_IDSITE,
    @SEQUENCE          UD_REGISTRO,
    @CARDID            char(2)                      = NULL,
    @TOTCARD           UD_MONTO                     = NULL,
    @CUSTIDSS          UD_CODCLIENT                 = NULL,
    @MTOBONUS          UD_MONTO,
    @MTOCASH           UD_MONTO,
    @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,
                              TOTCARD,
                              CUSTIDSS,
                              MTOBONUS,
                              MTOCASH,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@BATNBRDEPOSIT,
           @SITEID,
           @SEQUENCE,
           @CARDID,
           @TOTCARD,
           @CUSTIDSS,
           @MTOBONUS,
           @MTOCASH,
           @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_InsProcOP_DEPOSITSITE 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DEPOSITSITE 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSITSITE
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     UD_CORRELATIVOS,
    @CUSTIDSS              UD_CODCLIENT               = NULL,
    @DATEPROCE             UD_FECHA,
    @TOTALPEN              UD_MONTO,
    @TOTALUSD              UD_MONTO,
    @STATUS                bit                        = NULL,
    @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,
                               CUSTIDSS,
                               DATEPROCE,
                               TOTALPEN,
                               TOTALUSD,
                               STATUS,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@SITEID,
           @BATNBRDEPOSITSITE,
           @CUSTIDSS,
           @DATEPROCE,
           @TOTALPEN,
           @TOTALUSD,
           @STATUS,
           @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_InsProcOP_DEPOSITSITEDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DEPOSITSITEDET 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSITSITEDET
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     UD_CORRELATIVOS,
    @SEQUENCE              UD_REGISTRO,
    @DOCSUMMARYTYPEID      char(2)                    = NULL,
    @REFERENCE             numeric(10, 0),
    @DEPOSITPASSWORD       char(20),
    @DATEDEPOSIT           UD_FECHA,
    @CURYID                UD_CURYID,
    @CURYRATE              UD_CURYRATE,
    @TOTALPEN              UD_MONTO,
    @TOTALUSD              UD_MONTO,
    @DEBE                  UD_DEBE_HABER,
    @HABER                 UD_DEBE_HABER,
    @STATUS                bit                        = NULL,
    @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,
                                  SEQUENCE,
                                  DOCSUMMARYTYPEID,
                                  REFERENCE,
                                  DEPOSITPASSWORD,
                                  DATEDEPOSIT,
                                  CURYID,
                                  CURYRATE,
                                  TOTALPEN,
                                  TOTALUSD,
                                  DEBE,
                                  HABER,
                                  STATUS,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@SITEID,
           @BATNBRDEPOSITSITE,
           @SEQUENCE,
           @DOCSUMMARYTYPEID,
           @REFERENCE,
           @DEPOSITPASSWORD,
           @DATEDEPOSIT,
           @CURYID,
           @CURYRATE,
           @TOTALPEN,
           @TOTALUSD,
           @DEBE,
           @HABER,
           @STATUS,
           @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_InsProcOP_DEPOSITTYPE 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DEPOSITTYPE 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSITTYPE
(
    @DEPOSITTYPEID        char(1),
    @DESCRDEPOSTITYPE     UD_DESCRIPCION,
    @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,
                               USER01,
                               USER02,
                               USER03,
                               USER04 
                               )
    VALUES(@DEPOSITTYPEID,
           @DESCRDEPOSTITYPE,
           @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_InsProcOP_DOCSUMMARY 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DOCSUMMARY 
 */

CREATE PROCEDURE sp_InsProcOP_DOCSUMMARY
(
    @SITEID               UD_IDSITE,
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @DOCDATE              UD_FECHA,
    @DUEDATE              UD_FECHA,
    @DOCSTATUSID          char(2),
    @TAXID01              UD_TAXID,
    @TAXID02              UD_TAXID,
    @TERMID               char(2),
    @CURYTYPEID           UD_CURYTYPEID,
    @CURYRATE             UD_CURYRATE,
    @CURYID               UD_CURYID,
    @CUSTIDSS             UD_CODCLIENT,
    @CUSTID               UD_CODCLIENT,
    @CUSTNAME             UD_NOMBRE                = NULL,
    @TAXREGNBR            UD_CODCLIENT,
    @WEEKDAYPAG           char(12),
    @DAYTHENK             int,
    @TOTALPEN             UD_MONTO,
    @TOTALUSD             UD_MONTO,
    @SUBTOTALPEN          UD_MONTO,
    @SUBTOTALUSD          UD_MONTO,
    @TAXPEN               UD_MONTO,
    @TAXUSD               UD_MONTO,
    @STKITEM              bit                      = NULL,
    @STKPROCE             bit,
    @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,
                              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,
           @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_InsProcOP_DOCSUMMARYDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DOCSUMMARYDET 
 */

CREATE PROCEDURE sp_InsProcOP_DOCSUMMARYDET
(
    @SITEID               UD_IDSITE,
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             UD_REGISTRO,
    @CURYTYPEID           UD_CURYTYPEID,
    @CURYID               UD_CURYID,
    @TAXID02              UD_TAXID,
    @TAXID01              UD_TAXID,
    @DISCOUNT             numeric(12, 2)              = NULL,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @SLS_PRICE            UD_PRECIO,
    @QTY                  UD_STOCK,
    @TOTALPEN             UD_MONTO,
    @SUBTOTALPEN          UD_MONTO,
    @TAX1PEN              UD_MONTO,
    @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,
                                 SUBTOTALPEN,
                                 TAX1PEN,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @DOCSUMMARYTYPEID,
           @NBRDOCSUMMARY,
           @SEQUENCE,
           @CURYTYPEID,
           @CURYID,
           @TAXID02,
           @TAXID01,
           @DISCOUNT,
           @INVTIDSKU,
           @SLS_PRICE,
           @QTY,
           @TOTALPEN,
           @SUBTOTALPEN,
           @TAX1PEN,
           @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_InsProcOP_DOCSUMMARYTYPE 
 */



/* 
 * PROCEDURE: sp_InsProcOP_DOCSUMMARYTYPE 
 */

CREATE PROCEDURE sp_InsProcOP_DOCSUMMARYTYPE
(
    @DOCSUMMARYTYPEID        char(2),
    @DESCRDOCSUMMARYTYPE     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_DOCSUMMARYTYPE(DOCSUMMARYTYPEID,
                                  DESCRDOCSUMMARYTYPE,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@DOCSUMMARYTYPEID,
           @DESCRDOCSUMMARYTYPE,
           @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_InsProcOP_LOADSALESORD 
 */



/* 
 * PROCEDURE: sp_InsProcOP_LOADSALESORD 
 */

CREATE PROCEDURE sp_InsProcOP_LOADSALESORD
(
    @NBRLOADSALE        UD_CORRELATIVOS,
    @CURYID             UD_CURYID,
    @SALESPERID         UD_IDVENDEDOR,
    @CUSTIDSS           UD_CODCLIENT               = NULL,
    @TAXREGNBR          UD_CODCLIENT               = NULL,
    @CUSTNAME           UD_NOMBRE                  = NULL,
    @ADDR1              UD_DIRECCION               = NULL,
    @DOCTYPEID          UD_DOCTYPEID,
    @DOCSTATUSID        char(2),
    @USERID             UD_USERID,
    @OPTRTOTPEN         UD_MONTO,
    @OPTRTAXPEN         UD_IMPUESTO,
    @DOCDATE            UD_FECHA,
    @DATEPROCESALES     UD_FECHA,
    @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,
                                SALESPERID,
                                CUSTIDSS,
                                TAXREGNBR,
                                CUSTNAME,
                                ADDR1,
                                DOCTYPEID,
                                DOCSTATUSID,
                                USERID,
                                OPTRTOTPEN,
                                OPTRTAXPEN,
                                DOCDATE,
                                DATEPROCESALES,
                                CURYRATE,
                                STKCHECK,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@NBRLOADSALE,
           @CURYID,
           @SALESPERID,
           @CUSTIDSS,
           @TAXREGNBR,
           @CUSTNAME,
           @ADDR1,
           @DOCTYPEID,
           @DOCSTATUSID,
           @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_InsProcOP_LOADSALESORDDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_LOADSALESORDDET 
 */

CREATE PROCEDURE sp_InsProcOP_LOADSALESORDDET
(
    @NBRLOADSALE     UD_CORRELATIVOS,
    @INVTIDSKU       UD_IDCORPORATIVO,
    @DESCR           UD_DESCRIPCION,
    @STKUNITID       UD_STKUNITID                = NULL,
    @TAXIGV          UD_IMPUESTO,
    @SLS_PRICE       UD_PRECIO,
    @QTY             UD_STOCK,
    @OPTRTOTDET      UD_MONTO,
    @STDCOSTPEN      UD_COSTO,
    @STRCOSTUSD      UD_COSTO,
    @DISCDSCTO1      UD_PORCENTAJE,
    @DISCDSCTO2      UD_PORCENTAJE,
    @DISCMTO         numeric(12, 2),
    @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,
                                   INVTIDSKU,
                                   DESCR,
                                   STKUNITID,
                                   TAXIGV,
                                   SLS_PRICE,
                                   QTY,
                                   OPTRTOTDET,
                                   STDCOSTPEN,
                                   STRCOSTUSD,
                                   DISCDSCTO1,
                                   DISCDSCTO2,
                                   DISCMTO,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@NBRLOADSALE,
           @INVTIDSKU,
           @DESCR,
           @STKUNITID,
           @TAXIGV,
           @SLS_PRICE,
           @QTY,
           @OPTRTOTDET,
           @STDCOSTPEN,
           @STRCOSTUSD,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTO,
           @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_InsProcOP_MODSALES 
 */



/* 
 * 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_InsProcOP_PRCLVLCUST 
 */



/* 
 * PROCEDURE: sp_InsProcOP_PRCLVLCUST 
 */

CREATE PROCEDURE sp_InsProcOP_PRCLVLCUST
(
    @PRCLVLCUSTID           UD_PRCLVLCUSTID,
    @CUSTIDSS               UD_CODCLIENT               = NULL,
    @SITEID                 UD_IDSITE,
    @CURYID                 UD_CURYID,
    @DESCR                  UD_DESCRIPCION,
    @STARTDATEPRICE         UD_FECHA,
    @ENDDATEPRICE           UD_FECHA,
    @STATUS                 bit,
    @APPROVA1               bit,
    @USERIDAPPROVA1         UD_USERID,
    @DATETIMEAUTORIZED1     smalldatetime,
    @APPROVA2               bit,
    @USERIDAPPROVA2         UD_USERID,
    @DATETIMEAUTORIZED2     UD_FECHA,
    @STKPRICESEND           bit,
    @SITEORIG               UD_IDSITE,
    @STKPRICESS             bit,
    @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,
                              CUSTIDSS,
                              SITEID,
                              CURYID,
                              DESCR,
                              STARTDATEPRICE,
                              ENDDATEPRICE,
                              STATUS,
                              APPROVA1,
                              USERIDAPPROVA1,
                              DATETIMEAUTORIZED1,
                              APPROVA2,
                              USERIDAPPROVA2,
                              DATETIMEAUTORIZED2,
                              STKPRICESEND,
                              SITEORIG,
                              STKPRICESS,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@PRCLVLCUSTID,
           @CUSTIDSS,
           @SITEID,
           @CURYID,
           @DESCR,
           @STARTDATEPRICE,
           @ENDDATEPRICE,
           @STATUS,
           @APPROVA1,
           @USERIDAPPROVA1,
           @DATETIMEAUTORIZED1,
           @APPROVA2,
           @USERIDAPPROVA2,
           @DATETIMEAUTORIZED2,
           @STKPRICESEND,
           @SITEORIG,
           @STKPRICESS,
           @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_InsProcOP_PRCLVLCUSTDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE sp_InsProcOP_PRCLVLCUSTDET
(
    @PRCLVLCUSTID      UD_PRCLVLCUSTID,
    @SEQUENCE          UD_REGISTRO,
    @STATUS            bit,
    @DISCDSCTO         UD_PORCENTAJE,
    @DISCMTO           UD_MONTO,
    @SLS_PRICE         UD_PRECIO,
    @SLS_PRICECUST     UD_PRECIO,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @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,
                                 SEQUENCE,
                                 STATUS,
                                 DISCDSCTO,
                                 DISCMTO,
                                 SLS_PRICE,
                                 SLS_PRICECUST,
                                 INVTIDSKU,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@PRCLVLCUSTID,
           @SEQUENCE,
           @STATUS,
           @DISCDSCTO,
           @DISCMTO,
           @SLS_PRICE,
           @SLS_PRICECUST,
           @INVTIDSKU,
           @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_InsProcOP_PROMOTIONCARD 
 */



/* 
 * PROCEDURE: sp_InsProcOP_PROMOTIONCARD 
 */

CREATE PROCEDURE sp_InsProcOP_PROMOTIONCARD
(
    @PROMOTIONCARDID       char(20),
    @NAMECLIENT            UD_NOMBRE            = NULL,
    @PASSWORDMAGNETIC      char(30)             = NULL,
    @LASTNAME              UD_NOMBRE            = NULL,
    @USER1                 UD_USER1             = NULL,
    @USER2                 UD_USER2             = NULL,
    @USER3                 UD_USER6             = NULL,
    @USER4                 UD_USER7             = NULL,
    @PROMOTIONCARDTYPE     char(2))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PROMOTIONCARD(PROMOTIONCARDID,
                                 NAMECLIENT,
                                 PASSWORDMAGNETIC,
                                 LASTNAME,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4,
                                 PROMOTIONCARDTYPE)
    VALUES(@PROMOTIONCARDID,
           @NAMECLIENT,
           @PASSWORDMAGNETIC,
           @LASTNAME,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @PROMOTIONCARDTYPE)

    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_InsProcOP_PROMOTIONCARDTYPE 
 */



/* 
 * PROCEDURE: sp_InsProcOP_PROMOTIONCARDTYPE 
 */

CREATE PROCEDURE sp_InsProcOP_PROMOTIONCARDTYPE
(
    @PROMOTIONCARDTYPE      char(2),
    @DESCRPROMOTIONCARD     char(10)            = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PROMOTIONCARDTYPE(PROMOTIONCARDTYPE,
                                     DESCRPROMOTIONCARD)
    VALUES(@PROMOTIONCARDTYPE,
           @DESCRPROMOTIONCARD)

    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_InsProcOP_SALES 
 */



/* 
 * PROCEDURE: sp_InsProcOP_SALES 
 */

CREATE PROCEDURE sp_InsProcOP_SALES
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          UD_CORRELATIVOS,
    @STKINVENTORY         bit,
    @SITEID               UD_IDSITE,
    @CURYID               UD_CURYID,
    @DOCSTATUSID          char(2),
    @TERMID               char(2),
    @MODSALESID           char(2),
    @SALESPOINT           UD_PTOVTA,
    @SALESPERID           UD_IDVENDEDOR,
    @USERID               UD_USERID,
    @DATEDOC              UD_FECHA,
    @DATEPROCESALES       UD_FECHA,
    @PERPOST              UD_PERPOST,
    @TAXIGVID             char(2)                    = NULL,
    @TAXISCID             char(2)                    = NULL,
    @TOTALPEN             UD_MONTO,
    @TOTALUSD             UD_MONTO,
    @SUBTOTALPEN          UD_MONTO,
    @SUBTOTALUSD          UD_MONTO,
    @TAX1PEN              UD_MONTO,
    @TAX1USD              UD_MONTO,
    @TAX2PEN              UD_MONTO,
    @TAX2USD              UD_MONTO,
    @PORCENTDISCOUNT1     UD_PORCENTAJE,
    @PORCENTDISCOUNT2     UD_PORCENTAJE,
    @TOTDISCOUNTPEN       UD_MONTO,
    @TOTDISCOUNTUSD       UD_MONTO,
    @TOTRETURNEDPEN       UD_MONTO,
    @TOTRETURNEDUSD       UD_MONTO,
    @TOTCASHPEN           UD_MONTO,
    @TOTCASHUSD           UD_MONTO,
    @CARDCREDITID1        char(2)                    = NULL,
    @CARDCREDITID2        char(2)                    = NULL,
    @NBRCARD1             char(20)                   = NULL,
    @NBRCARD2             char(20)                   = NULL,
    @TOTCARD1PEN          UD_MONTO,
    @TOTCARD1USD          UD_MONTO,
    @TOTCARD2PEN          UD_MONTO,
    @TOTCARD2USD          UD_MONTO,
    @CUSTIDSS             UD_CODCLIENT               = NULL,
    @TAXREGNBR            UD_CODCLIENT               = NULL,
    @NAME                 UD_NOMBRE                  = NULL,
    @PLACA                UD_PLACA,
    @NBRBONUS             int,
    @CURYTYPEID           UD_CURYTYPEID,
    @CURYRATE             UD_CAMBIO,
    @STKCLOSEDZ           bit,
    @STKCLOSEDX           bit,
    @PROMOTIONCARDID      char(20)                   = NULL,
    @POINTNUMBER          smallint,
    @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,
                         STKINVENTORY,
                         SITEID,
                         CURYID,
                         DOCSTATUSID,
                         TERMID,
                         MODSALESID,
                         SALESPOINT,
                         SALESPERID,
                         USERID,
                         DATEDOC,
                         DATEPROCESALES,
                         PERPOST,
                         TAXIGVID,
                         TAXISCID,
                         TOTALPEN,
                         TOTALUSD,
                         SUBTOTALPEN,
                         SUBTOTALUSD,
                         TAX1PEN,
                         TAX1USD,
                         TAX2PEN,
                         TAX2USD,
                         PORCENTDISCOUNT1,
                         PORCENTDISCOUNT2,
                         TOTDISCOUNTPEN,
                         TOTDISCOUNTUSD,
                         TOTRETURNEDPEN,
                         TOTRETURNEDUSD,
                         TOTCASHPEN,
                         TOTCASHUSD,
                         CARDCREDITID1,
                         CARDCREDITID2,
                         NBRCARD1,
                         NBRCARD2,
                         TOTCARD1PEN,
                         TOTCARD1USD,
                         TOTCARD2PEN,
                         TOTCARD2USD,
                         CUSTIDSS,
                         TAXREGNBR,
                         NAME,
                         PLACA,
                         NBRBONUS,
                         CURYTYPEID,
                         CURYRATE,
                         STKCLOSEDZ,
                         STKCLOSEDX,
                         PROMOTIONCARDID,
                         POINTNUMBER,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @STKINVENTORY,
           @SITEID,
           @CURYID,
           @DOCSTATUSID,
           @TERMID,
           @MODSALESID,
           @SALESPOINT,
           @SALESPERID,
           @USERID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @TAXIGVID,
           @TAXISCID,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAX1PEN,
           @TAX1USD,
           @TAX2PEN,
           @TAX2USD,
           @PORCENTDISCOUNT1,
           @PORCENTDISCOUNT2,
           @TOTDISCOUNTPEN,
           @TOTDISCOUNTUSD,
           @TOTRETURNEDPEN,
           @TOTRETURNEDUSD,
           @TOTCASHPEN,
           @TOTCASHUSD,
           @CARDCREDITID1,
           @CARDCREDITID2,
           @NBRCARD1,
           @NBRCARD2,
           @TOTCARD1PEN,
           @TOTCARD1USD,
           @TOTCARD2PEN,
           @TOTCARD2USD,
           @CUSTIDSS,
           @TAXREGNBR,
           @NAME,
           @PLACA,
           @NBRBONUS,
           @CURYTYPEID,
           @CURYRATE,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @PROMOTIONCARDID,
           @POINTNUMBER,
           @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_InsProcOP_SALESDET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_SALESDET 
 */

CREATE PROCEDURE sp_InsProcOP_SALESDET
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @SEQUENCE           UD_REGISTRO,
    @SITEID             UD_IDSITE,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DOCSTATUSID        char(2),
    @DATEDOC            UD_FECHA,
    @DATEPROCESALES     UD_FECHA,
    @PERSPOT            UD_PERPOST,
    @STKCLOSEDZ         bit,
    @STKCLOSEDX         bit,
    @NBRTURN            tinyint,
    @NBRCARA            UD_CARA                     = NULL,
    @NBRTRANSACFUEL     char(4)                     = NULL,
    @DISCDSCTO1         UD_PORCENTAJE,
    @DISCDSCTO2         UD_PORCENTAJE,
    @DISCMTOPEN         UD_MONTO,
    @DISCMTOUSD         UD_MONTO,
    @PORCENTTAXIGV      UD_PORCENTAJE,
    @PORCENTTAXISC      UD_PORCENTAJE,
    @TOTALPEN           UD_MONTO,
    @TOTALUSD           UD_MONTO,
    @TAXPEN             UD_MONTO,
    @TAXUSD             UD_MONTO,
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          UD_PRECIO,
    @SLSPRICESALE       UD_PRECIO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                UD_STOCK,
    @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,
    @SALESPOINT         UD_PTOVTA,
    @SALESPERID         UD_IDVENDEDOR)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESDET(DOCTYPEID,
                            NBRDOCUMENT,
                            SEQUENCE,
                            SITEID,
                            INVTIDSKU,
                            CURYID,
                            DOCSTATUSID,
                            DATEDOC,
                            DATEPROCESALES,
                            PERSPOT,
                            STKCLOSEDZ,
                            STKCLOSEDX,
                            NBRTURN,
                            NBRCARA,
                            NBRTRANSACFUEL,
                            DISCDSCTO1,
                            DISCDSCTO2,
                            DISCMTOPEN,
                            DISCMTOUSD,
                            PORCENTTAXIGV,
                            PORCENTTAXISC,
                            TOTALPEN,
                            TOTALUSD,
                            TAXPEN,
                            TAXUSD,
                            STKITEM,
                            STKFISI,
                            SLS_PRICE,
                            SLSPRICESALE,
                            STDCOSTPEN,
                            STDCOSTUSD,
                            INVTIDALTER,
                            DESCRINVENTORY,
                            QTY,
                            USER1,
                            USER2,
                            USER3,
                            USER4,
                            USER5,
                            USER6,
                            USER7,
                            USER8,
                            USER9,
                            SALESPOINT,
                            SALESPERID)
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SEQUENCE,
           @SITEID,
           @INVTIDSKU,
           @CURYID,
           @DOCSTATUSID,
           @DATEDOC,
@DATEPROCESALES,
           @PERSPOT,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @NBRTURN,
           @NBRCARA,
           @NBRTRANSACFUEL,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTOPEN,
           @DISCMTOUSD,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9,
           @SALESPOINT,
           @SALESPERID)

    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_InsProcOP_SALESPERSON 
 */



/* 
 * PROCEDURE: sp_InsProcOP_SALESPERSON 
 */

CREATE PROCEDURE sp_InsProcOP_SALESPERSON
(
    @SALESPERID         UD_IDVENDEDOR,
    @SALESPERNAME       UD_NOMBRE                = NULL,
    @IDENTITYDOC        char(10),
    @PHONE              char(10)                 = NULL,
    @SEX                char(1),
    @INITIALDATE        UD_FECHA,
    @SITEID             UD_IDSITE,
    @ADDRESS1           UD_DIRECCION,
    @ADDRESS2           UD_DIRECCION             = NULL,
    @PASSWORD           char(15),
    @BIRTHDATE          UD_FECHA,
    @BIRTHADDRESS1      UD_DIRECCION             = NULL,
    @BIRTHADDRESS2      UD_DIRECCION             = NULL,
    @STATUSPERSONID     char(10),
    @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,
                               USER01,
                               USER02,
                               USER03,
                               USER04 
                               )
    VALUES(@SALESPERID,
           @SALESPERNAME,
           @IDENTITYDOC,
           @PHONE,
           @SEX,
           @INITIALDATE,
           @SITEID,
           @ADDRESS1,
           @ADDRESS2,
           @PASSWORD,
           @BIRTHDATE,
           @BIRTHADDRESS1,
           @BIRTHADDRESS2,
           @STATUSPERSONID,
           @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_InsProcOP_SALESPOINTSETING 
 */



/* 
 * PROCEDURE: sp_InsProcOP_SALESPOINTSETING 
 */

CREATE PROCEDURE sp_InsProcOP_SALESPOINTSETING
(
    @SALESPOINT            UD_PTOVTA,
    @SALESPERID            UD_IDVENDEDOR,
    @ENDDATEACT            UD_FECHA,
    @SERIALNUMBER          char(16),
    @TOTZETA               UD_TOTZETA,
    @PRECLVID              UD_PRECLVID,
    @SITEID                UD_IDSITE,
    @STKTICKFAC            bit,
    @STKTICKBOL            bit,
    @CURYID                UD_CURYID,
    @STKLOADSALE           bit,
    @NBRLOADSALE           UD_CORRELATIVOS,
    @STKPRINTERFAC         bit,
    @STKPRINTEDBOL         bit,
    @TYPEDISPFAC           UD_TYPE_DISP_SALIDA,
    @DISPFAC               UD_DISP_SALIDA,
    @NBRFAC                UD_CORRELATIVOS,
    @TYPEDISPBOL           UD_TYPE_DISP_SALIDA,
    @DISPBOL               UD_DISP_SALIDA,
    @NBRBOL                UD_CORRELATIVOS,
    @TYPEDISPNC            UD_TYPE_DISP_SALIDA,
    @DISPNC                UD_DISP_SALIDA,
    @NBRNC                 UD_CORRELATIVOS,
    @TYPEDISPND            UD_TYPE_DISP_SALIDA,
    @DISPND                UD_DISP_SALIDA,
    @NBRND                 UD_CORRELATIVOS,
    @TYPEDISORDER          UD_TYPE_DISP_SALIDA,
    @DISPORDER             UD_DISP_SALIDA,
    @NBRORDER              UD_CORRELATIVOS,
    @TYPEDISPTICKFAC       UD_TYPE_DISP_SALIDA,
    @DISPTICKFAC           UD_DISP_SALIDA,
    @NBRTICKFAC            UD_CORRELATIVOS,
    @TYPEDISPTICKBOL       UD_TYPE_DISP_SALIDA,
    @DISPTICKBOL           UD_DISP_SALIDA,
    @NBRTICKBOL            UD_CORRELATIVOS,
    @TERMINALNAME          varchar(20),
    @DISPDISPLAY           UD_DISP_SALIDA,
    @STKSALESPERSONID      bit,
    @STKSALESPOINTFAC      bit,
    @STKZETAOK             bit,
    @STKJUMPAUTOMAT        bit,
    @QTYJUNPAUTOMAT        tinyint,
    @STKHOTKEYS            bit,
    @TYPEPRINTERTICKET     char(18),
    @STKDELETEDOC          bit,
    @STK_PRICE_COST        bit,
    @STKDESCFACTUR         bit,
    @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_SALESPOINTSETING(SALESPOINT,
                                    SALESPERID,
                                    ENDDATEACT,
                                    SERIALNUMBER,
                                    TOTZETA,
                                    PRECLVID,
                                    SITEID,
                                    STKTICKFAC,
                                    STKTICKBOL,
                                    CURYID,
                                    STKLOADSALE,
                                    NBRLOADSALE,
                                    STKPRINTERFAC,
                                    STKPRINTEDBOL,
                                    TYPEDISPFAC,
                                    DISPFAC,
                                    NBRFAC,
                                    TYPEDISPBOL,
                                    DISPBOL,
                                    NBRBOL,
                                    TYPEDISPNC,
                                    DISPNC,
                                    NBRNC,
                                    TYPEDISPND,
                                    DISPND,
                                    NBRND,
                                    TYPEDISORDER,
                                    DISPORDER,
                                    NBRORDER,
                                    TYPEDISPTICKFAC,
                                    DISPTICKFAC,
                                    NBRTICKFAC,
                                    TYPEDISPTICKBOL,
                                    DISPTICKBOL,
                                    NBRTICKBOL,
                                    TERMINALNAME,
                                    DISPDISPLAY,
                                    STKSALESPERSONID,
                                    STKSALESPOINTFAC,
                                    STKZETAOK,
                                    STKJUMPAUTOMAT,
                                    QTYJUNPAUTOMAT,
                                    STKHOTKEYS,
                                    TYPEPRINTERTICKET,
                                    STKDELETEDOC,
                                    [STK PRICE_COST],
                                    STKDESCFACTUR,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4,
                                    USER5,
                                    USER6,
                                    USER7,
                                    USER8,
                                    USER9 
                                    )
    VALUES(@SALESPOINT,
           @SALESPERID,
           @ENDDATEACT,
           @SERIALNUMBER,
           @TOTZETA,
           @PRECLVID,
           @SITEID,
           @STKTICKFAC,
           @STKTICKBOL,
           @CURYID,
           @STKLOADSALE,
           @NBRLOADSALE,
           @STKPRINTERFAC,
           @STKPRINTEDBOL,
           @TYPEDISPFAC,
           @DISPFAC,
           @NBRFAC,
           @TYPEDISPBOL,
           @DISPBOL,
           @NBRBOL,
           @TYPEDISPNC,
           @DISPNC,
           @NBRNC,
           @TYPEDISPND,
           @DISPND,
           @NBRND,
           @TYPEDISORDER,
           @DISPORDER,
           @NBRORDER,
           @TYPEDISPTICKFAC,
           @DISPTICKFAC,
           @NBRTICKFAC,
           @TYPEDISPTICKBOL,
           @DISPTICKBOL,
           @NBRTICKBOL,
           @TERMINALNAME,
           @DISPDISPLAY,
           @STKSALESPERSONID,
           @STKSALESPOINTFAC,
           @STKZETAOK,
           @STKJUMPAUTOMAT,
           @QTYJUNPAUTOMAT,
           @STKHOTKEYS,
           @TYPEPRINTERTICKET,
           @STKDELETEDOC,
           @STK_PRICE_COST,
           @STKDESCFACTUR,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_InsProcOP_SETUP 
 */



/* 
 * PROCEDURE: sp_InsProcOP_SETUP 
 */

CREATE PROCEDURE sp_InsProcOP_SETUP
(
    @AUTOMATICSALES     bit,
    @CATEGORYFUEL       UD_CATEGORY,
    @PLACA              bit,
    @MTOMAXSALES        UD_MONTO,
    @ACCOUNTCARD        UD_ACCOUNTCARD,
    @DATEPROCESALES     UD_FECHA,
    @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,
                         PLACA,
                         MTOMAXSALES,
                         ACCOUNTCARD,
                         DATEPROCESALES,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@AUTOMATICSALES,
           @CATEGORYFUEL,
           @PLACA,
           @MTOMAXSALES,
           @ACCOUNTCARD,
           @DATEPROCESALES,
           @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_STATUSPERSON 
 */






/* 
 * PROCEDURE: sp_InsProcOP_STATUSPERSON 
 */

CREATE PROCEDURE sp_InsProcOP_STATUSPERSON
(
    @STATUSPERSONID        char(10),
    @DESCRSTATUSPERSON     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_STATUSPERSON(STATUSPERSONID,
                                DESCRSTATUSPERSON,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@STATUSPERSONID,
           @DESCRSTATUSPERSON,
           @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_InsProcOP_SUMMARYDAY 
 */



/* 
 * PROCEDURE: sp_InsProcOP_SUMMARYDAY 
 */

CREATE PROCEDURE sp_InsProcOP_SUMMARYDAY
(
    @BATNBRDIARYSUMMARY     UD_CORRELATIVOS,
    @SITEID                 UD_IDSITE,
    @DATEPROCE              UD_FECHA,
    @CURYRATE               UD_CURYRATE,
    @ROUND                  UD_MONTO,
    @TOTALDIFERENCE         UD_MONTO,
    @OBSERVATION            varchar(100)               = NULL,
    @STATUS                 bit                        = NULL,
    @DEBE                   UD_MONTO,
    @HABER                  UD_MONTO,
    @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,
                              DEBE,
                              HABER,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@BATNBRDIARYSUMMARY,
           @SITEID,
           @DATEPROCE,
           @CURYRATE,
           @ROUND,
           @TOTALDIFERENCE,
           @OBSERVATION,
           @STATUS,
           @DEBE,
           @HABER,
           @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_InsProcOP_SUMMARYSALES 
 */



/* 
 * PROCEDURE: sp_InsProcOP_SUMMARYSALES 
 */

CREATE PROCEDURE sp_InsProcOP_SUMMARYSALES
(
    @SITEID               UD_IDSITE,
    @DATEPROCE            UD_FECHA,
    @DOCSUMMARYTYPEID     char(2),
    @CLASSID              UD_CATEGORY,
    @CUSTIDSS             UD_CODCLIENT              = NULL,
    @CUSTCLASSID          char(2)                   = NULL,
    @DEFINITIONID         char(2),
    @CLASSUBID            char(5)                   = NULL,
    @REFERENCE            char(15)                  = NULL,
    @TOTALBONUS           UD_MONTO,
    @DIFFERENCE           UD_MONTO,
    @COMMENT              UD_DESCRIPCION,
    @DEBE                 UD_DEBE_HABER,
    @HABER                UD_DEBE_HABER,
    @TOTALDEBE            UD_MONTO,
    @TOTALHABER           UD_MONTO,
    @TOTALPEN             UD_MONTO,
    @TOTALUSD             UD_MONTO,
    @STATUS               bit                       = NULL,
    @USER1                UD_USER1                  = NULL,
    @USER2                UD_USER2                  = NULL,
    @USER3                UD_USER6                  = NULL,
    @USER4                UD_USER7                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SUMMARYSALES(SITEID,
                                DATEPROCE,
                                DOCSUMMARYTYPEID,
                                CLASSID,
                                CUSTIDSS,
                                CUSTCLASSID,
                                DEFINITIONID,
                                CLASSUBID,
                                REFERENCE,
                                TOTALBONUS,
                                DIFFERENCE,
                                COMMENT,
                                DEBE,
                                HABER,
                                TOTALDEBE,
                                TOTALHABER,
                                TOTALPEN,
                                TOTALUSD,
                                STATUS,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@SITEID,
           @DATEPROCE,
           @DOCSUMMARYTYPEID,
           @CLASSID,
           @CUSTIDSS,
           @CUSTCLASSID,
           @DEFINITIONID,
           @CLASSUBID,
           @REFERENCE,
           @TOTALBONUS,
           @DIFFERENCE,
           @COMMENT,
           @DEBE,
           @HABER,
           @TOTALDEBE,
           @TOTALHABER,
           @TOTALPEN,
           @TOTALUSD,
           @STATUS,
           @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_InsProcOP_TURNVSCARA 
 */



/* 
 * PROCEDURE: sp_InsProcOP_TURNVSCARA 
 */

CREATE PROCEDURE sp_InsProcOP_TURNVSCARA
(
    @SITEID               UD_IDSITE,
    @BATNBRTURNPERSON     UD_TURNOSPERSONAL,
    @DATEPROCE            UD_FECHA,
    @NUMBERTURN           tinyint,
    @SALESPERID           UD_IDVENDEDOR,
    @USER1                UD_USER1                     = NULL,
    @USER2                UD_USER2                     = NULL,
    @USER3                UD_USER6                     = NULL,
    @USER4                UD_USER7                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_TURNVSCARA(SITEID,
                              BATNBRTURNPERSON,
                              DATEPROCE,
                              NUMBERTURN,
                              SALESPERID,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@SITEID,
           @BATNBRTURNPERSON,
           @DATEPROCE,
           @NUMBERTURN,
           @SALESPERID,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_InsProcOP_TURNVSCARADET 
 */



/* 
 * PROCEDURE: sp_InsProcOP_TURNVSCARADET 
 */

CREATE PROCEDURE sp_InsProcOP_TURNVSCARADET
(
    @SITEID               UD_IDSITE,
    @BATNBRTURNPERSON     UD_TURNOSPERSONAL,
    @DATEPROCE            UD_FECHA,
    @NUMBERTURN           tinyint,
    @CARA                 UD_CARA,
    @USER1                UD_USER1                     = NULL,
    @USER2                UD_USER2                     = NULL,
    @USER3                UD_USER6                     = NULL,
    @USER4                UD_USER7                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_TURNVSCARADET(SITEID,
                                 BATNBRTURNPERSON,
                                 DATEPROCE,
                                 NUMBERTURN,
                                 CARA,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @BATNBRTURNPERSON,
           @DATEPROCE,
           @NUMBERTURN,
           @CARA,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

END
go
IF OBJECT_ID('sp_InsProcOP_TURNVSCARADET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_TURNVSCARADET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_TURNVSCARADET >>>'
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,
    @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,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@USERID,
           @SCREENID,
           @VIEWRIGHTS,
           @INSERTRIGHTS,
           @UPDATERIGHTS,
           @DELETERIGHTS,
           @PRINTRIGHTS,
           @NULLRIGHTS,
           @CLOSERIGHTS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_InsProcSE_ACCESSDETRIGHTS: llave primaria duplicada en 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_InsProcSE_GROUPREC 
 */

CREATE PROCEDURE SP_InsProcSE_GROUPREC
(
    @GROUPID       UD_USERID,
    @GROUPNAME     UD_NOMBRE,
    @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,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@GROUPID,
           @GROUPNAME,
           @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_InsProcSE_MODULE 
 */

CREATE PROCEDURE SP_InsProcSE_MODULE
(
    @MODULEID       UD_MODULOID,
    @MODULENAME     UD_NOMBRE,
    @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,
                          USER1,
                          USER2,
                          USER3,
                          USER4 
                          )
    VALUES(@MODULEID,
           @MODULENAME,
           @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_InsProcSE_SCREEN 
 */

CREATE PROCEDURE SP_InsProcSE_SCREEN
(
    @SCREENID       UD_SCREENID,
    @MODULEID       UD_MODULOID,
    @SCREENNAME     UD_NOMBRE,
    @SCREENTYPE     char(1),
    @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,
                          USER1,
                          USER2,
                          USER3,
                          USER4 
                          )
    VALUES(@SCREENID,
           @MODULEID,
           @SCREENNAME,
           @SCREENTYPE,
           @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_InsProcSE_USERGROUP 
 */

CREATE PROCEDURE SP_InsProcSE_USERGROUP
(
    @USERID      UD_USERID,
    @GROUPID     UD_USERID,
    @USER1       char(30)             = NULL,
    @USER2       char(30)             = NULL,
    @USER3       float                = NULL,
    @USER4       float                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SE_USERGROUP(USERID,
                             GROUPID,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@USERID,
           @GROUPID,
           @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_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            = NULL,
    @STATUS           bit,
    @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,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@USERID,
           @USERNAME,
           @EMAILADDRESS,
           @LOCATION,
           @PASSWORD,
           @PHONE,
           @EXPIRED,
           @STATUS,
           @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_InsProcSETUP 
 */

CREATE PROCEDURE SP_InsProcSETUP
(
    @TYPEPRICEUPDATE        UD_PRECLVID,
    @QTYSALESPERSONTURN     tinyint,
    @QTYCARAS               tinyint,
    @SERVERNAME             char(40),
    @DBNAME                 char(40),
    @SITEORIG               UD_IDSITE,
    @CURYORIG               UD_CURYID,
    @FLOATPRICE             UD_CANTDECIMAL,
    @FLOATCOST              UD_CANTDECIMAL,
    @FLOATQTY               UD_CANTDECIMAL,
    @FLOATOUTPUT            UD_CANTDECIMAL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SETUP(TYPEPRICEUPDATE,
                      QTYSALESPERSONTURN,
                      QTYCARAS,
                      SERVERNAME,
                      DBNAME,
                      SITEORIG,
                      CURYORIG,
                      FLOATPRICE,
                      FLOATCOST,
                      FLOATQTY,
                      FLOATOUTPUT)
    VALUES(@TYPEPRICEUPDATE,
           @QTYSALESPERSONTURN,
           @QTYCARAS,
           @SERVERNAME,
           @DBNAME,
           @SITEORIG,
           @CURYORIG,
           @FLOATPRICE,
           @FLOATCOST,
           @FLOATQTY,
           @FLOATOUTPUT)

    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_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),
    @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,
                     USER1,
                     USER2,
                     USER3,
                     USER4,
                     USER5,
                     USER6,
                     USER7,
                     USER8,
                     USER9 
                     )
    VALUES(@SITEID,
           @CITYID,
           @COUNTRYID,
           @NAME,
           @ADDR1,
           @ADDR2,
           @FONO1,
           @FONO2,
           @FAX,
           @STATUS,
           @STARTDATEINVEN,
           @ENDDATEINVEN,
           @ADJUST,
           @ATTN,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Código de Almacén ya Existe'
        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_InsProcSTATE 
 */

CREATE PROCEDURE SP_InsProcSTATE
(
    @STATEID     char(2),
    @DESCR       UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO STATE(STATEID,
                      DESCR 
                      )
    VALUES(@STATEID,
           @DESCR 
           )

    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_InsProcTAXS 
 */

CREATE PROCEDURE SP_InsProcTAXS
(
    @TAXID      UD_TAXID,
    @DESCR      UD_DESCRIPCION,
    @VALUE      numeric(5, 2),
    @STATUS     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,
                     STATUS,
                     USER1,
                     USER2,
                     USER3,
                     USER4 
                     )
    VALUES(@TAXID,
           @DESCR,
           @VALUE,
           @STATUS,
           @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_InsProcTERMS 
 */

CREATE PROCEDURE SP_InsProcTERMS
(
    @TERMID       char(2),
    @DUEINTRV     tinyint,
    @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 TERMS(TERMID,
                      DUEINTRV,
                      DESCR,
                      USER1,
                      USER2,
                      USER3,
                      USER4 
                      )
    VALUES(@TERMID,
           @DUEINTRV,
           @DESCR,
           @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_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_IN_INTRAITEM 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_INTRAITEM
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @TYPEDOCID         UD_TYPEMOVEID,
    @SEQUENCE          smallint,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDBARR        UD_CODIGOBARRA,
    @QTY               UD_STOCK,
    @TAXIGV            numeric(4, 2)               = NULL,
    @STDCOSTPEN        UD_COSTO,
    @STDCOSTUSD        UD_COSTO,
    @STKFISI           bit,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @RLSED             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 TMP_IN_INTRAITEM(CORRNBR,
                                 SITEID,
                                 TYPEDOCID,
                                 SEQUENCE,
                                 INVTIDSKU,
                                 INVTIDBARR,
                                 QTY,
                                 TAXIGV,
                                 STDCOSTPEN,
                                 STDCOSTUSD,
                                 STKFISI,
                                 INTRDATE,
                                 INTRDATEPROCE,
                                 RLSED,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4,
                                 USER5,
                                 USER6,
                                 USER7,
                                 USER8,
                                 USER9 
                                 )
    VALUES(@CORRNBR,
           @SITEID,
           @TYPEDOCID,
           @SEQUENCE,
           @INVTIDSKU,
           @INVTIDBARR,
           @QTY,
           @TAXIGV,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @STKFISI,
           @INTRDATE,
           @INTRDATEPROCE,
           @RLSED,
           @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_InsProcTMP_IN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_INVENTORYDAT
(
    @CORRNBR            int,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @STOCKSTAR          UD_STOCK,
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       smalldatetime               = NULL,
    @ENDDATEINV         smalldatetime               = NULL,
    @LASTINVFIS         UD_STOCK,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         UD_COSTO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @LASTSTDCOSTPEN     UD_COSTO,
    @LASTSTDCOSTUSD     UD_COSTO,
    @SLS_PRICE          UD_COSTO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID,
    @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,
                                    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,
           @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_InsProcTMP_IN_KITCOMPONENT 
 */



/* 
 * PROCEDURE: sp_InsProcTMP_IN_KITCOMPONENT 
 */

CREATE PROCEDURE sp_InsProcTMP_IN_KITCOMPONENT
(
    @CORRNBR        int,
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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_VENDORINVT 
 */

CREATE PROCEDURE SP_InsProcTMP_IN_VENDORINVT
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @CURYID        UD_CURYID,
    @STDCOST       UD_COSTO,
    @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,
                                  BUYCOD,
                                  DESCR,
                                  CURYID,
                                  STDCOST,
                                  BUYDATE,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4,
                                  TSTAMP)
    VALUES(@CORRNBR,
           @VENDIDSS,
           @INVTIDSKU,
           @BUYCOD,
           @DESCR,
           @CURYID,
           @STDCOST,
           @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_InsProcTMP_IN_VENDORINVTREL 
 */



/* 
 * 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_InsProcTYPEPAINMENT 
 */

CREATE PROCEDURE sp_InsProcTYPEPAINMENT
(
    @TYPEPAINMENTID     char(2),
    @DESCR              char(40))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TYPEPAINMENT(TYPEPAINMENTID,
                             DESCR 
                             )
    VALUES(@TYPEPAINMENTID,
           @DESCR 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: SP_InsProcZONECUSTOMER 
 */

CREATE PROCEDURE SP_InsProcZONECUSTOMER
(
    @ZONECUSTOMERID     char(2),
    @DESCR              char(40))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO ZONECUSTOMER(ZONECUSTOMERID,
                             DESCR 
                             )
    VALUES(@ZONECUSTOMERID,
           @DESCR 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcAR_CCDOC 
 */



/* 
 * PROCEDURE: sp_SelProcAR_CCDOC 
 */

CREATE PROCEDURE sp_SelProcAR_CCDOC
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           CURYTYPEID,
           DOCDATE,
           DATEPROCESALES,
           DUEDATE,
           TOTALPEN,
           TOTALUSD,
           PAIDMENTPEN,
           PAIDMENTUSD,
           DOCVALPEN,
           DOCVALUSD,
           MODSALESID,
           CUSTIDSS,
           TAXREGNBR,
           DOCSTATUSID,
           CURYRATE,
           CURYID,
           WEEKDAYPAG,
           DAYTHENK,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM AR_CCDOC
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

    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_SelProcAR_CCDOCDET 
 */



/* 
 * PROCEDURE: sp_SelProcAR_CCDOCDET 
 */

CREATE PROCEDURE sp_SelProcAR_CCDOCDET
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           UD_CORRELATIVOS,
    @SEQUENCE              UD_REGISTRO)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SEQUENCE,
           CURYTYPEID,
           DOCSTATUSID,
           DOCDATE,
           DATEPROCEPAIDMENT,
           DATEPROCESALES,
           BANKID,
           CHEQNUMBER,
           NBRBANKACCOUNT,
           TOTALPEN,
           TOTALUSD,
           PAIDMENTPEN,
           PAIDMENTUSD,
           CURYID,
           CURYRATE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM AR_CCDOCDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

    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_SelProcBANK 
 */



/* 
 * PROCEDURE: sp_SelProcBANK 
 */

CREATE PROCEDURE sp_SelProcBANK
(
    @BANKID          char(2))
AS
BEGIN
    SELECT BANKID,
           DESCR,
           BANKACCOUNT,
           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_SelProcCITY 
 */

CREATE PROCEDURE SP_SelProcCITY
(
    @CITYID      char(3))
AS
BEGIN
    SELECT CITYID,
           STATEID,
           DESCR,
           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_SelProcCLASSCUST 
 */

CREATE PROCEDURE SP_SelProcCLASSCUST
(
    @CLASSCUSTID     char(2))
AS
BEGIN
    SELECT CLASSCUSTID,
           DESCR,
           TSTAMP
      FROM CLASSCUST
     WHERE CLASSCUSTID = @CLASSCUSTID

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


/* 
 * PROCEDURE: SP_SelProcCOUNTRY 
 */

CREATE PROCEDURE SP_SelProcCOUNTRY
(
    @COUNTRYID     char(3))
AS
BEGIN
    SELECT COUNTRYID,
           DESCR,
           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_SelProcCURRENCY 
 */

CREATE PROCEDURE SP_SelProcCURRENCY
(
    @CURYID         UD_CURYID)
AS
BEGIN
    SELECT CURYID,
           DESCR,
           DESCRMONEY,
           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_SelProcCURYRATE 
 */

CREATE PROCEDURE SP_SelProcCURYRATE
(
    @CURYTYPEID     char(6),
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime)
AS
BEGIN
    SELECT CURYTYPEID,
           FROMCURYID,
           TOCURYID,
           CURYDATE,
           RATE,
           OPERADOR,
           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_SelProcCURYRATETYPE 
 */

CREATE PROCEDURE SP_SelProcCURYRATETYPE
(
    @CURYTYPEID     char(6))
AS
BEGIN
    SELECT CURYTYPEID,
           DESCR,
           USER4,
           USER1,
           USER2,
           USER3,
           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_SelProcCUST_LIMIT 
 */



/* 
 * PROCEDURE: sp_SelProcCUST_LIMIT 
 */

CREATE PROCEDURE sp_SelProcCUST_LIMIT
(
    @CUSTIDSS        UD_CODCLIENT,
    @SITEID          UD_IDSITE)
AS
BEGIN
    SELECT CUSTIDSS,
           SITEID,
           CREDLIMITED,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CUST_LIMIT
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

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


/* 
 * PROCEDURE: SP_SelProcCUSTOMER 
 */

CREATE PROCEDURE SP_SelProcCUSTOMER
(
    @CUSTIDSS         UD_CODCLIENT)
AS
BEGIN
    SELECT CUSTIDSS,
           CUSTID,
           NAME,
           ADDR1,
           ADDR2,
           TAXREGNBR,
           DATEORIG,
           DATEPROC,
           PHONE,
           FAX,
           ZONECUSTOMER,
           SLSPERID,
           FORMPAG,
           TYPEPAG,
           TERMSID,
           CURYID,
           CRLMT,
           CLASSCUSTID,
           PRCLVLCUSTID,
           WEEKDAYPAG,
           THANKDAY,
           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_SelProcCUSTSTATUS 
 */



/* 
 * PROCEDURE: sp_SelProcCUSTSTATUS 
 */

CREATE PROCEDURE sp_SelProcCUSTSTATUS
(
    @CUSTSTATUSID     char(1))
AS
BEGIN
    SELECT CUSTSTATUSID,
           DESCRCUST,
           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_SelProcCUSTWARRANTY 
 */



/* 
 * PROCEDURE: sp_SelProcCUSTWARRANTY 
 */

CREATE PROCEDURE sp_SelProcCUSTWARRANTY
(
    @CUSTIDSS               UD_CODCLIENT,
    @CUSTGARANTIATYPEID     char(2))
AS
BEGIN
    SELECT CUSTIDSS,
           CUSTGARANTIATYPEID,
           TOTGAR,
           DESCRGAR,
           EXPIREDATE,
           CURYID,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CUSTWARRANTY
     WHERE CUSTIDSS           = @CUSTIDSS
       AND CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

    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_SelProcCUSTWARRANTYTYPE 
 */



/* 
 * PROCEDURE: sp_SelProcCUSTWARRANTYTYPE 
 */

CREATE PROCEDURE sp_SelProcCUSTWARRANTYTYPE
(
    @CUSTGARANTIATYPEID     char(2))
AS
BEGIN
    SELECT CUSTGARANTIATYPEID,
           DECRCUSTGARANTIA,
           USER1,
           USER2,
           TSTAMP
      FROM CUSTWARRANTYTYPE
     WHERE CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

    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_SelProcDOCTYPE 
 */

CREATE PROCEDURE SP_SelProcDOCTYPE
(
    @DOCTYPEID     UD_DOCTYPEID)
AS
BEGIN
    SELECT DOCTYPEID,
           DESCR,
           MINDES,
           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_SelProcIN_CATEGORY 
 */

CREATE PROCEDURE SP_SelProcIN_CATEGORY
(
    @CLASSID     char(5))
AS
BEGIN
    SELECT CLASSID,
           DESCR,
           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_SelProcIN_CLASSVENDOR 
 */

CREATE PROCEDURE SP_SelProcIN_CLASSVENDOR
(
    @CLASSVENDORID     char(2))
AS
BEGIN
    SELECT CLASSVENDORID,
           DESCR,
           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_SelProcIN_CLUSTER 
 */



/* 
 * 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_SelProcIN_DOCSTATUS 
 */

CREATE PROCEDURE SP_SelProcIN_DOCSTATUS
(
    @DOCSTATUSID     char(2))
AS
BEGIN
    SELECT DOCSTATUSID,
           DESCR,
           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_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,
           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_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,
           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_SelProcIN_INVENTHISTOPHISI 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTHISTOPHISI
(
    @SITEID               UD_IDSITE,
    @INVTIDSKU            UD_IDCORPORATIVO)
AS
BEGIN
    SELECT SITEID,
           INVTIDSKU,
           CLASSID,
           CLASSUBID,
           STOCKPHISICAL,
           QTYAVAIAL,
           INTRDATEPHISICAL,
           INTRDATEADJUST
      FROM IN_INVENTHISTOPHISI
     WHERE SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    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_SelProcIN_INVENTORY 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORY
(
    @INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
    SELECT INVTIDSKU,
           TAXISC,
           TAXIGV,
           CLASSUBID,
           INVTYPEID,
           STKUNITID,
           CLASSID,
           DESCR,
           WTSTK1,
           STKFUELS,
           STKSTORE,
           STKOTHER,
           STKITEM,
           KIT,
           DISCDSCTO1,
           DISCDSCTO2,
           DISCMTO,
           STKRECEPT,
           STKSALES,
           STATUS,
           CONSIGNMENT,
           DISASSEMBLE,
           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_SelProcIN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYALTER
(
    @INVTIDBARR     UD_CODIGOBARRA)
AS
BEGIN
    SELECT INVTIDBARR,
           INVTIDSKU,
           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_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,
           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_SelProcIN_INVENTORYDES 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYDES
(
    @INVTIDSKU        UD_IDCORPORATIVO)
AS
BEGIN
    SELECT INVTIDSKU,
           INVTIDSKUDES,
           WTSTK2,
           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_SelProcIN_INVENTORYTYPE 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYTYPE
(
    @INVTYPEID     char(5))
AS
BEGIN
    SELECT INVTYPEID,
           DESCR,
           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_SelProcIN_INVENTORYUNITMED 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTORYUNITMED
(
    @STKUNITID     UD_STKUNITID)
AS
BEGIN
    SELECT STKUNITID,
           DESCR,
           STKDIVIDE,
           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_SelProcIN_INVENTPHISICAL 
 */

CREATE PROCEDURE SP_SelProcIN_INVENTPHISICAL
(
    @SITEID               UD_IDSITE,
    @INVTIDSKU            UD_IDCORPORATIVO)
AS
BEGIN
    SELECT SITEID,
           INVTIDSKU,
           CLASSUBID,
           CLASSID,
           STOCKPHISICAL,
           QTYAVAIL,
           INTRDATEPHISICAL
      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_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,
           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_SelProcIN_KIT 
 */

CREATE PROCEDURE SP_SelProcIN_KIT
(
    @KITID          UD_KITID)
AS
BEGIN
    SELECT KITID,
           STKUNITID,
           CURYID,
           TAXID,
           DESCR,
           MTOCTOREPS,
           STDCOSTUSD,
           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_SelProcIN_KITCOMPONENT 
 */

CREATE PROCEDURE SP_SelProcIN_KITCOMPONENT
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO)
AS
BEGIN
    SELECT KITID,
           INVTIDSKU,
           SEQUENCE,
           STDCOSTPEN,
           STDCOSTUSD,
           QTY,
           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_SelProcIN_PRICELEVELTEM 
 */

CREATE PROCEDURE SP_SelProcIN_PRICELEVELTEM
(
    @INVTIDSKU             UD_IDCORPORATIVO,
    @SITEID                UD_IDSITE,
    @CORRNBR               char(10))
AS
BEGIN
    SELECT INVTIDSKU,
           SITEID,
           CORRNBR,
           PRECLVID,
           CURYID,
           DESCR,
           SLSPRICE,
           STARTDATEPRICE,
           ENDDATEPRICE,
           STARTTIMEPRICE,
           ENDTIMEPRICE,
           STATUS,
           STKPRICESEND,
           USERIDAUTORIZED,
           DATETIMEAUTORIZED,
           SITEORIG,
           STKPRICESS,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM IN_PRICELEVELTEM
     WHERE INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID
       AND CORRNBR   = @CORRNBR

    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_SelProcIN_PRICELEVELTEMDET 
 */



/* 
 * PROCEDURE: sp_SelProcIN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE sp_SelProcIN_PRICELEVELTEMDET
(
    @BATNBRPRICELEVELTEM     UD_CORRELATIVOS,
    @SITEID                  UD_IDSITE,
    @SEQUENCE                UD_REGISTRO)
AS
BEGIN
    SELECT BATNBRPRICELEVELTEM,
           SITEID,
           SEQUENCE,
           INVTIDSKU,
           DESCR,
           SLS_PRICE,
           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_SelProcIN_PRICELEVELTYPE 
 */

CREATE PROCEDURE SP_SelProcIN_PRICELEVELTYPE
(
    @PRECLVID       UD_PRECLVID)
AS
BEGIN
    SELECT PRECLVID,
           CURYID,
           CURYTYPEID,
           DESCR,
           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_SelProcIN_SOLINVENTORY 
 */

CREATE PROCEDURE SP_SelProcIN_SOLINVENTORY
(
    @INVTIDSKU       UD_IDCORPORATIVO)
AS
BEGIN
    SELECT INVTIDSKU,
           SITEID,	
           INVTIDSTK,
           INVTIDNOSTK,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_SOLINVENTORY
     WHERE INVTIDSKU = @INVTIDSKU

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


/* 
 * PROCEDURE: SP_SelProcIN_SUBCATEGORY 
 */

Create Procedure SP_SelProcIN_SUBCATEGORY
(
    @CLASSID       char(5))
AS
BEGIN
    SELECT CLASSUBID,DESCR,
           CLASSID           
     FROM IN_SUBCATEGORY
     WHERE 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_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,
           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_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,
           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_SelProcIN_VENDORINVT 
 */

CREATE PROCEDURE SP_SelProcIN_VENDORINVT
(
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO)
AS
BEGIN
    SELECT VENDIDSS,
           INVTIDSKU,
           BUYCOD,
           DESCR,
           STDCOST,
           CURYID,
           BUYDATE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_VENDORINVT
     WHERE VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU

    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_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,
           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_SelProcOP_CARA_FUEL 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CARA_FUEL 
 */

CREATE PROCEDURE sp_SelProcOP_CARA_FUEL
(
    @CARA           UD_CARA)
AS
BEGIN
    SELECT CARA,
           INVTIDBARR,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CARA_FUEL
     WHERE CARA = @CARA

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


/* 
 * PROCEDURE: sp_SelProcOP_CARD 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CARD 
 */

CREATE PROCEDURE sp_SelProcOP_CARD
(
    @CARDID          char(2))
AS
BEGIN
    SELECT CARDID,
           ACCOUNT,
           DESCR,
           NUMBERORDER,
           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_SelProcOP_CARD_MONT 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CARD_MONT 
 */

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

    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_SelProcOP_CCSALESPERSON 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CCSALESPERSON 
 */

CREATE PROCEDURE sp_SelProcOP_CCSALESPERSON
(
    @SITEID         UD_IDSITE,
    @SALESPERID     UD_IDVENDEDOR)
AS
BEGIN
    SELECT SITEID,
           SALESPERID,
           CURYID,
           TOTAL,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCSALESPERSON
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID

    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_SelProcOP_CCSALESPERSONDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CCSALESPERSONDET 
 */

CREATE PROCEDURE sp_SelProcOP_CCSALESPERSONDET
(
    @SITEID                  UD_IDSITE,
    @SALESPERID              UD_IDVENDEDOR,
    @SEQUENCE                UD_REGISTRO)
AS
BEGIN
    SELECT SITEID,
           SALESPERID,
           SEQUENCE,
           CCSALESPERSONTYPEID,
           DATEPROCE,
           OBS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCSALESPERSONDET
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID
       AND SEQUENCE   = @SEQUENCE

    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_SelProcOP_CCSALESPERSONTYPE 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CCSALESPERSONTYPE 
 */

CREATE PROCEDURE sp_SelProcOP_CCSALESPERSONTYPE
(
    @CCSALESPERSONTYPEID        char(2))
AS
BEGIN
    SELECT CCSALESPERSONTYPEID,
           DESCRCCSALESPERSONTYPE,
           DEBE,
           HABER,
           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_SelProcOP_CORELATIVE 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CORELATIVE 
 */

CREATE PROCEDURE sp_SelProcOP_CORELATIVE
(
    @TYPECORELATIVE     char(20))
AS
BEGIN
    SELECT TYPECORELATIVE,
           NBRCORELATIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      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_SelProcOP_CTRLFUEL 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CTRLFUEL 
 */

CREATE PROCEDURE sp_SelProcOP_CTRLFUEL
(
    @SITEID               UD_IDSITE,
    @BATNBRCTRLFUEL       UD_CORRELATIVOS)
AS
BEGIN
    SELECT SITEID,
           BATNBRCTRLFUEL,
           DATEPROCE,
           INVTIDSKU,
           STOCKINITIAL,
           QTYIMPUT,
           QTYOUTPUT,
           QTYSALES,
           QTYAVAIL,
           STKUNITID,
           VALUEUNIMED,
           QTYDIFFERENCE,
           PORCENTDIFERENCE,
           REMARK,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLFUEL
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL

    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_SelProcOP_CTRLFUELDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CTRLFUELDET 
 */

CREATE PROCEDURE sp_SelProcOP_CTRLFUELDET
(
    @SITEID             UD_IDSITE,
    @BATNBRCTRLFUEL     UD_CORRELATIVOS,
    @SEQUENCE           tinyint)
AS
BEGIN
    SELECT SITEID,
           BATNBRCTRLFUEL,
           SEQUENCE,
           CURYID,
           QTYSALES,
           SLS_PRICE,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLFUELDET
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL
       AND SEQUENCE       = @SEQUENCE

    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_SelProcOP_CTRLTURN 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CTRLTURN 
 */

CREATE PROCEDURE sp_SelProcOP_CTRLTURN
(
    @BATNBRTURN     UD_CORR_CONTOMET)
AS
BEGIN
    SELECT BATNBRTURN,
           SALESPERID,
           SITEID,
           DATEPROCES,
           NBRTUR,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_CTRLTURN
     WHERE BATNBRTURN = @BATNBRTURN

    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_SelProcOP_CTRLTURNDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CTRLTURNDET 
 */

CREATE PROCEDURE sp_SelProcOP_CTRLTURNDET
(
    @BATNBRTURN       UD_CORR_CONTOMET)
AS
BEGIN
    SELECT BATNBRTURN,
           CARA,
           INVTIDSKU,
           STARTREADING,
           ENDREADING,
           QTYSALES,
           QTYSERAF,
           QTYOTHER,
           TOTQTY,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLTURNDET
     WHERE BATNBRTURN = @BATNBRTURN

    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_SelProcOP_CUSTCARD 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CUSTCARD 
 */

CREATE PROCEDURE sp_SelProcOP_CUSTCARD
(
    @CUSTIDSS          UD_CODCLIENT)
AS
BEGIN
    SELECT CUSTIDSS,
           EXPIREDATE,
           DESCRCUSTCARD,
           CARDPASSWORD,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUSTCARD
     WHERE CUSTIDSS = @CUSTIDSS

    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_SelProcOP_CUSTCLASS 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CUSTCLASS 
 */

CREATE PROCEDURE sp_SelProcOP_CUSTCLASS
(
    @CUSTCLASSID     char(2))
AS
BEGIN
    SELECT CUSTCLASSID,
           DESCR,
           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_SelProcOP_CUSTZONE 
 */



/* 
 * PROCEDURE: sp_SelProcOP_CUSTZONE 
 */

CREATE PROCEDURE sp_SelProcOP_CUSTZONE
(
    @CUSTZONEID     char(2))
AS
BEGIN
    SELECT CUSTZONEID,
           DESCR,
           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_SelProcOP_DEFINITIONPD 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DEFINITIONPD 
 */

CREATE PROCEDURE sp_SelProcOP_DEFINITIONPD
(
    @DEFINITIONID     char(2))
AS
BEGIN
    SELECT DEFINITIONID,
           CUSTIDSS,
           CLASSID,
           CLASSUBID,
           INVTIDSKU,
           DEBE,
           HABER,
           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_SelProcOP_DEPOSIT 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DEPOSIT 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSIT
(
    @BATNBRDEPOSIT       UD_CORREL_DEPOSIT,
    @SITEID              UD_IDSITE)
AS
BEGIN
    SELECT BATNBRDEPOSIT,
           SITEID,
           CURYID,
           SALESPERSONTURN,
           DEPOSITTYPEID,
           SALESPERID,
           OPDATE,
           DATEPROCE,
           TOTPEN,
           TOTUSD,
           STATUS,
           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_SelProcOP_DEPOSITDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DEPOSITDET 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSITDET
(
    @BATNBRDEPOSIT     UD_CORREL_DEPOSIT,
    @SITEID            UD_IDSITE,
    @SEQUENCE          UD_REGISTRO)
AS
BEGIN
    SELECT BATNBRDEPOSIT,
           SITEID,
           SEQUENCE,
           CARDID,
           TOTCARD,
           CUSTIDSS,
           MTOBONUS,
           MTOCASH,
           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_SelProcOP_DEPOSITSITE 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DEPOSITSITE 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSITSITE
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     UD_CORRELATIVOS)
AS
BEGIN
    SELECT SITEID,
           BATNBRDEPOSITSITE,
           CUSTIDSS,
           DATEPROCE,
           TOTALPEN,
           TOTALUSD,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSITSITE
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE

    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_SelProcOP_DEPOSITSITEDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DEPOSITSITEDET 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSITSITEDET
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     UD_CORRELATIVOS,
    @SEQUENCE              UD_REGISTRO)
AS
BEGIN
    SELECT SITEID,
           BATNBRDEPOSITSITE,
           SEQUENCE,
           DOCSUMMARYTYPEID,
           REFERENCE,
           DEPOSITPASSWORD,
           DATEDEPOSIT,
           CURYID,
           CURYRATE,
           TOTALPEN,
           TOTALUSD,
           DEBE,
           HABER,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSITSITEDET
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       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_SelProcOP_DEPOSITTYPE 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DEPOSITTYPE 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSITTYPE
(
    @DEPOSITTYPEID        char(1))
AS
BEGIN
    SELECT DEPOSITTYPEID,
           DESCRDEPOSTITYPE,
           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_SelProcOP_DOCSUMMARY 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DOCSUMMARY 
 */

CREATE PROCEDURE sp_SelProcOP_DOCSUMMARY
(
    @SITEID               UD_IDSITE,
    @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,
           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_SelProcOP_DOCSUMMARYDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DOCSUMMARYDET 
 */

CREATE PROCEDURE sp_SelProcOP_DOCSUMMARYDET
(
    @SITEID               UD_IDSITE,
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             UD_REGISTRO)
AS
BEGIN
    SELECT SITEID,
           DOCSUMMARYTYPEID,
           NBRDOCSUMMARY,
           SEQUENCE,
           CURYTYPEID,
           CURYID,
           TAXID02,
           TAXID01,
           DISCOUNT,
           INVTIDSKU,
           SLS_PRICE,
           QTY,
           TOTALPEN,
           SUBTOTALPEN,
           TAX1PEN,
           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_SelProcOP_DOCSUMMARYTYPE 
 */



/* 
 * PROCEDURE: sp_SelProcOP_DOCSUMMARYTYPE 
 */

CREATE PROCEDURE sp_SelProcOP_DOCSUMMARYTYPE
(
    @DOCSUMMARYTYPEID        char(2))
AS
BEGIN
    SELECT DOCSUMMARYTYPEID,
           DESCRDOCSUMMARYTYPE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DOCSUMMARYTYPE
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID

    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_SelProcOP_LOADSALESORD 
 */



/* 
 * PROCEDURE: sp_SelProcOP_LOADSALESORD 
 */

CREATE PROCEDURE sp_SelProcOP_LOADSALESORD
(
    @NBRLOADSALE        UD_CORRELATIVOS)
AS
BEGIN
    SELECT NBRLOADSALE,
           CURYID,
           SALESPERID,
           CUSTIDSS,
           TAXREGNBR,
           CUSTNAME,
           ADDR1,
           DOCTYPEID,
           DOCSTATUSID,
           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_SelProcOP_LOADSALESORDDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_LOADSALESORDDET 
 */

CREATE PROCEDURE sp_SelProcOP_LOADSALESORDDET
(
    @NBRLOADSALE     UD_CORRELATIVOS)
AS
BEGIN
    SELECT NBRLOADSALE,
           SEQUENCE,
           INVTIDSKU,
           DESCR,
           STKUNITID,
           TAXIGV,
           SLS_PRICE,
           QTY,
           OPTRTOTDET,
           STDCOSTPEN,
           STRCOSTUSD,
           DISCDSCTO1,
           DISCDSCTO2,
           DISCMTO,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_LOADSALESORDDET
     WHERE NBRLOADSALE = @NBRLOADSALE

    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_SelProcOP_MODSALES 
 */



/* 
 * 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_SelProcOP_PRCLVLCUST 
 */



/* 
 * PROCEDURE: sp_SelProcOP_PRCLVLCUST 
 */

CREATE PROCEDURE sp_SelProcOP_PRCLVLCUST
(
    @PRCLVLCUSTID           UD_PRCLVLCUSTID)
AS
BEGIN
    SELECT PRCLVLCUSTID,
           CUSTIDSS,
           SITEID,
           CURYID,
           DESCR,
           STARTDATEPRICE,
           ENDDATEPRICE,
           STATUS,
           APPROVA1,
           USERIDAPPROVA1,
           DATETIMEAUTORIZED1,
           APPROVA2,
           USERIDAPPROVA2,
           DATETIMEAUTORIZED2,
           STKPRICESEND,
           SITEORIG,
           STKPRICESS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PRCLVLCUST
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID

    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_SelProcOP_PRCLVLCUSTDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE sp_SelProcOP_PRCLVLCUSTDET
(
    @PRCLVLCUSTID      UD_PRCLVLCUSTID,
    @SEQUENCE          UD_REGISTRO)
AS
BEGIN
    SELECT PRCLVLCUSTID,
           SEQUENCE,
           STATUS,
           DISCDSCTO,
           DISCMTO,
           SLS_PRICE,
           SLS_PRICECUST,
           INVTIDSKU,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PRCLVLCUSTDET
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SEQUENCE     = @SEQUENCE

    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_SelProcOP_PROMOTIONCARD 
 */



/* 
 * PROCEDURE: sp_SelProcOP_PROMOTIONCARD 
 */

CREATE PROCEDURE sp_SelProcOP_PROMOTIONCARD
(
    @PROMOTIONCARDID       char(20))
AS
BEGIN
    SELECT PROMOTIONCARDID,
           NAMECLIENT,
           PASSWORDMAGNETIC,
           LASTNAME,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP,
           PROMOTIONCARDTYPE
      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_SelProcOP_PROMOTIONCARDTYPE 
 */



/* 
 * PROCEDURE: sp_SelProcOP_PROMOTIONCARDTYPE 
 */

CREATE PROCEDURE sp_SelProcOP_PROMOTIONCARDTYPE
(
    @PROMOTIONCARDTYPE      char(2))
AS
BEGIN
    SELECT PROMOTIONCARDTYPE,
           DESCRPROMOTIONCARD
      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_SelProcOP_SALES 
 */



/* 
 * PROCEDURE: sp_SelProcOP_SALES 
 */

CREATE PROCEDURE sp_SelProcOP_SALES
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          UD_CORRELATIVOS)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           STKINVENTORY,
           SITEID,
           CURYID,
           DOCSTATUSID,
           TERMID,
           MODSALESID,
           SALESPOINT,
           SALESPERID,
           USERID,
           DATEDOC,
           DATEPROCESALES,
           PERPOST,
           TAXIGVID,
           TAXISCID,
           TOTALPEN,
           TOTALUSD,
           SUBTOTALPEN,
           SUBTOTALUSD,
           TAX1PEN,
           TAX1USD,
           TAX2PEN,
           TAX2USD,
           PORCENTDISCOUNT1,
           PORCENTDISCOUNT2,
           TOTDISCOUNTPEN,
           TOTDISCOUNTUSD,
           TOTRETURNEDPEN,
           TOTRETURNEDUSD,
           TOTCASHPEN,
           TOTCASHUSD,
           CARDCREDITID1,
           CARDCREDITID2,
           NBRCARD1,
           NBRCARD2,
           TOTCARD1PEN,
           TOTCARD1USD,
           TOTCARD2PEN,
           TOTCARD2USD,
           CUSTIDSS,
           TAXREGNBR,
           NAME,
           PLACA,
           NBRBONUS,
           CURYTYPEID,
           CURYRATE,
           STKCLOSEDZ,
           STKCLOSEDX,
           PROMOTIONCARDID,
           POINTNUMBER,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM OP_SALES
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

    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_SelProcOP_SALESDET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_SALESDET 
 */

CREATE PROCEDURE sp_SelProcOP_SALESDET
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @SEQUENCE           UD_REGISTRO)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SEQUENCE,
           SITEID,
           INVTIDSKU,
           CURYID,
           DOCSTATUSID,
           DATEDOC,
           DATEPROCESALES,
           PERSPOT,
           STKCLOSEDZ,
           STKCLOSEDX,
           NBRTURN,
           NBRCARA,
           NBRTRANSACFUEL,
           DISCDSCTO1,
           DISCDSCTO2,
           DISCMTOPEN,
           DISCMTOUSD,
           PORCENTTAXIGV,
           PORCENTTAXISC,
           TOTALPEN,
           TOTALUSD,
           TAXPEN,
           TAXUSD,
           STKITEM,
           STKFISI,
           SLS_PRICE,
           SLSPRICESALE,
           STDCOSTPEN,
           STDCOSTUSD,
           INVTIDALTER,
           DESCRINVENTORY,
           QTY,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP,
           SALESPOINT,
           SALESPERID
      FROM OP_SALESDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       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_SelProcOP_SALESPERSON 
 */



/* 
 * PROCEDURE: sp_SelProcOP_SALESPERSON 
 */

CREATE PROCEDURE sp_SelProcOP_SALESPERSON
(
    @SALESPERID         UD_IDVENDEDOR)
AS
BEGIN
    SELECT SALESPERID,
           SALESPERNAME,
           IDENTITYDOC,
           PHONE,
           SEX,
           INITIALDATE,
           SITEID,
           ADDRESS1,
           ADDRESS2,
           PASSWORD,
           BIRTHDATE,
           BIRTHADDRESS1,
           BIRTHADDRESS2,
           STATUSPERSONID,
           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_SelProcOP_SALESPOINTSETING 
 */



/* 
 * PROCEDURE: sp_SelProcOP_SALESPOINTSETING 
 */

CREATE PROCEDURE sp_SelProcOP_SALESPOINTSETING
(
    @SALESPOINT            UD_PTOVTA,
    @SALESPERID            UD_IDVENDEDOR)
AS
BEGIN
    SELECT SALESPOINT,
           SALESPERID,
           ENDDATEACT,
           SERIALNUMBER,
           TOTZETA,
           NBRZETA,
           PRECLVID,
           SITEID,
           STKTICKFAC,
           STKTICKBOL,
           CURYID,
           STKLOADSALE,
           NBRLOADSALE,
           STKPRINTERFAC,
           STKPRINTEDBOL,
           TYPEDISPFAC,
           DISPFAC,
           NBRFAC,
           TYPEDISPBOL,
           DISPBOL,
           NBRBOL,
           TYPEDISPNC,
           DISPNC,
           NBRNC,
           TYPEDISPND,
           DISPND,
           NBRND,
           TYPEDISORDER,
           DISPORDER,
           NBRORDER,
           TYPEDISPTICKFAC,
           DISPTICKFAC,
           NBRTICKFAC,
           TYPEDISPTICKBOL,
           DISPTICKBOL,
           NBRTICKBOL,
           TERMINALNAME,
           DISPDISPLAY,
           STKSALESPERSONID,
           STKSALESPOINTFAC,
           STKZETAOK,
           STKJUMPAUTOMAT,
           QTYJUNPAUTOMAT,
           STKHOTKEYS,
           TYPEPRINTERTICKET,
           STKDELETEDOC,
           [STK PRICE_COST],
           STKDESCFACTUR,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM OP_SALESPOINTSETING
     WHERE SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID

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


/* 
 * PROCEDURE: sp_SelProcOP_STATUSPERSON 
 */



/* 
 * PROCEDURE: sp_SelProcOP_STATUSPERSON 
 */

CREATE PROCEDURE sp_SelProcOP_STATUSPERSON
(
    @STATUSPERSONID        char(10))
AS
BEGIN
    SELECT STATUSPERSONID,
           DESCRSTATUSPERSON,
           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_SelProcOP_SUMMARYDAY 
 */



/* 
 * PROCEDURE: sp_SelProcOP_SUMMARYDAY 
 */

CREATE PROCEDURE sp_SelProcOP_SUMMARYDAY
(
    @BATNBRDIARYSUMMARY     UD_CORRELATIVOS,
    @SITEID                 UD_IDSITE)
AS
BEGIN
    SELECT BATNBRDIARYSUMMARY,
           SITEID,
           DATEPROCE,
           CURYRATE,
           ROUND,
           TOTALDIFERENCE,
           OBSERVATION,
           STATUS,
           DEBE,
           HABER,
           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_SelProcOP_SUMMARYSALES 
 */



/* 
 * PROCEDURE: sp_SelProcOP_SUMMARYSALES 
 */

CREATE PROCEDURE sp_SelProcOP_SUMMARYSALES
(
    @SITEID               UD_IDSITE,
    @DATEPROCE            UD_FECHA)
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           DOCSUMMARYTYPEID,
           CLASSID,
           CUSTIDSS,
           CUSTCLASSID,
           DEFINITIONID,
           CLASSUBID,
           REFERENCE,
           TOTALBONUS,
           DIFFERENCE,
           COMMENT,
           DEBE,
           HABER,
           TOTALDEBE,
           TOTALHABER,
           TOTALPEN,
           TOTALUSD,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_SUMMARYSALES
     WHERE SITEID    = @SITEID
       AND 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_SelProcOP_TURNVSCARA 
 */



/* 
 * PROCEDURE: sp_SelProcOP_TURNVSCARA 
 */

CREATE PROCEDURE sp_SelProcOP_TURNVSCARA
(
    @SITEID               UD_IDSITE,
    @BATNBRTURNPERSON     UD_TURNOSPERSONAL,
    @DATEPROCE            UD_FECHA,
    @NUMBERTURN           tinyint)
AS
BEGIN
    SELECT SITEID,
           BATNBRTURNPERSON,
           DATEPROCE,
           NUMBERTURN,
           SALESPERID,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_TURNVSCARA
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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


/* 
 * PROCEDURE: sp_SelProcOP_TURNVSCARADET 
 */



/* 
 * PROCEDURE: sp_SelProcOP_TURNVSCARADET 
 */

CREATE PROCEDURE sp_SelProcOP_TURNVSCARADET
(
    @SITEID               UD_IDSITE,
    @BATNBRTURNPERSON     UD_TURNOSPERSONAL,
    @DATEPROCE            UD_FECHA,
    @NUMBERTURN           tinyint)
AS
BEGIN
    SELECT SITEID,
           BATNBRTURNPERSON,
           DATEPROCE,
           NUMBERTURN,
           CARA,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_TURNVSCARADET
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

    RETURN(0)
END
go
IF OBJECT_ID('sp_SelProcOP_TURNVSCARADET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcOP_TURNVSCARADET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcOP_TURNVSCARADET >>>'
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,
           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_SelProcSE_GROUPREC 
 */

CREATE PROCEDURE SP_SelProcSE_GROUPREC
(
    @GROUPID       UD_USERID)
AS
BEGIN
    SELECT GROUPID,
           GROUPNAME,
           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_SelProcSE_MODULE 
 */

CREATE PROCEDURE SP_SelProcSE_MODULE
(
    @MODULEID       UD_MODULOID)
AS
BEGIN
    SELECT MODULEID,
           MODULENAME,
           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_SelProcSE_SCREEN 
 */

CREATE PROCEDURE SP_SelProcSE_SCREEN
(
    @SCREENID       UD_SCREENID)
AS
BEGIN
    SELECT SCREENID,
           MODULEID,
           SCREENNAME,
           SCREENTYPE,
           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_SelProcSE_USERGROUP 
 */

CREATE PROCEDURE SP_SelProcSE_USERGROUP
(
    @USERID      UD_USERID,
    @GROUPID     UD_USERID)
AS
BEGIN
    SELECT USERID,
           GROUPID,
           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_SelProcSE_USERREC 
 */

CREATE PROCEDURE SP_SelProcSE_USERREC
(
    @USERID           UD_USERID)
AS
BEGIN
    SELECT USERID,
           USERNAME,
           EMAILADDRESS,
           LOCATION,
           PASSWORD,
           PHONE,
           EXPIRED,
           STATUS,
           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_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,
           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_SelProcSTATE 
 */

CREATE PROCEDURE SP_SelProcSTATE
(
    @STATEID     char(2))
AS
BEGIN
    SELECT STATEID,
           DESCR,
           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_SelProcTAXS 
 */

CREATE PROCEDURE SP_SelProcTAXS
(
    @TAXID      UD_TAXID)
AS
BEGIN
    SELECT TAXID,
           DESCR,
           VALUE,
           STATUS,
           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_SelProcTERMS 
 */

CREATE PROCEDURE SP_SelProcTERMS
(
    @TERMID       char(2))
AS
BEGIN
    SELECT TERMID,
           DUEINTRV,
           DESCR,
           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_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_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,
           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_SelProcTMP_IN_VENDORINVT 
 */

CREATE PROCEDURE SP_SelProcTMP_IN_VENDORINVT
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO)
AS
BEGIN
    SELECT CORRNBR,
           VENDIDSS,
           INVTIDSKU,
           BUYCOD,
           DESCR,
           CURYID,
           STDCOST,
           BUYDATE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM TMP_IN_VENDORINVT
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU

    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_SelProcTMP_IN_VENDORINVTREL 
 */



/* 
 * 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_SelProcTYPEPAINMENT 
 */

CREATE PROCEDURE SP_SelProcTYPEPAINMENT
(
    @TYPEPAINMENTID     char(2))
AS
BEGIN
    SELECT TYPEPAINMENTID,
           DESCR,
           TSTAMP
      FROM TYPEPAINMENT
     WHERE TYPEPAINMENTID = @TYPEPAINMENTID

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


/* 
 * PROCEDURE: SP_SelProcZONECUSTOMER 
 */

CREATE PROCEDURE SP_SelProcZONECUSTOMER
(
    @ZONECUSTOMERID     char(2))
AS
BEGIN
    SELECT ZONECUSTOMERID,
           DESCR,
           TSTAMP
      FROM ZONECUSTOMER
     WHERE ZONECUSTOMERID = @ZONECUSTOMERID

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


/* 
 * PROCEDURE: sp_UpdProcAR_CCDOC 
 */



/* 
 * PROCEDURE: sp_UpdProcAR_CCDOC 
 */

CREATE PROCEDURE sp_UpdProcAR_CCDOC
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @CURYTYPEID         UD_CURYTYPEID,
    @DOCDATE            UD_FECHA,
    @DATEPROCESALES     UD_FECHA,
    @DUEDATE            UD_FECHA,
    @TOTALPEN           UD_MONTO,
    @TOTALUSD           UD_MONTO,
    @PAIDMENTPEN        UD_MONTO,
    @PAIDMENTUSD        UD_MONTO,
    @DOCVALPEN          UD_MONTO,
    @DOCVALUSD          UD_MONTO,
    @MODSALESID         char(2),
    @CUSTIDSS           UD_CODCLIENT,
    @TAXREGNBR          UD_CODCLIENT               = NULL,
    @DOCSTATUSID        char(2),
    @CURYRATE           UD_CAMBIO,
    @CURYID             UD_CURYID,
    @WEEKDAYPAG         char(12),
    @DAYTHENK           int,
    @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 CURYTYPEID          = @CURYTYPEID,
           DOCDATE             = @DOCDATE,
           DATEPROCESALES      = @DATEPROCESALES,
           DUEDATE             = @DUEDATE,
           TOTALPEN            = @TOTALPEN,
           TOTALUSD            = @TOTALUSD,
           PAIDMENTPEN         = @PAIDMENTPEN,
           PAIDMENTUSD         = @PAIDMENTUSD,
           DOCVALPEN           = @DOCVALPEN,
           DOCVALUSD           = @DOCVALUSD,
           MODSALESID          = @MODSALESID,
           CUSTIDSS            = @CUSTIDSS,
           TAXREGNBR           = @TAXREGNBR,
           DOCSTATUSID         = @DOCSTATUSID,
           CURYRATE            = @CURYRATE,
           CURYID              = @CURYID,
           WEEKDAYPAG          = @WEEKDAYPAG,
           DAYTHENK            = @DAYTHENK,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

    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_UpdProcAR_CCDOCDET 
 */



/* 
 * PROCEDURE: sp_UpdProcAR_CCDOCDET 
 */

CREATE PROCEDURE sp_UpdProcAR_CCDOCDET
(
    @DOCTYPEID             UD_DOCTYPEID,
    @NBRDOCUMENT           UD_CORRELATIVOS,
    @SEQUENCE              UD_REGISTRO,
    @CURYTYPEID            UD_CURYTYPEID,
    @DOCSTATUSID           char(2),
    @DOCDATE               UD_FECHA,
    @DATEPROCEPAIDMENT     UD_FECHA,
    @DATEPROCESALES        UD_FECHA,
    @BANKID                char(2),
    @CHEQNUMBER            char(15),
    @NBRBANKACCOUNT        char(15),
    @TOTALPEN              UD_MONTO,
    @TOTALUSD              UD_MONTO,
    @PAIDMENTPEN           UD_MONTO,
    @PAIDMENTUSD           UD_MONTO,
    @CURYID                UD_CURYID,
    @CURYRATE              UD_CAMBIO,
    @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,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

    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_UpdProcBANK 
 */



/* 
 * PROCEDURE: sp_UpdProcBANK 
 */

CREATE PROCEDURE sp_UpdProcBANK
(
    @BANKID          char(2),
    @DESCR           UD_NOMBRE,
    @BANKACCOUNT     UD_ACCOUNTBANK,
    @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,
           BANKACCOUNT      = @BANKACCOUNT,
           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_UpdProcCITY 
 */

CREATE PROCEDURE SP_UpdProcCITY
(
    @CITYID      char(3),
    @STATEID     char(2),
    @DESCR       UD_DESCRIPCION,
    @TSTAMP      timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CITY
       SET STATEID      = @STATEID,
           DESCR        = @DESCR
     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_UpdProcCLASSCUST 
 */

CREATE PROCEDURE SP_UpdProcCLASSCUST
(
    @CLASSCUSTID     char(2),
    @DESCR           char(2),
    @TSTAMP          timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CLASSCUST
       SET DESCR            = @DESCR
     WHERE CLASSCUSTID = @CLASSCUSTID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: SP_UpdProcCOUNTRY 
 */

CREATE PROCEDURE SP_UpdProcCOUNTRY
(
    @COUNTRYID     char(3),
    @DESCR         UD_DESCRIPCION,
    @TSTAMP        timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE COUNTRY
       SET DESCR          = @DESCR
     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_UpdProcCURRENCY 
 */

CREATE PROCEDURE SP_UpdProcCURRENCY
(
    @CURYID         UD_CURYID,
    @DESCR          UD_DESCRIPCION,
    @DESCRMONEY     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 CURRENCY
       SET DESCR           = @DESCR,
           DESCRMONEY      = @DESCRMONEY,
           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_UpdProcCURYRATE 
 */

CREATE PROCEDURE SP_UpdProcCURYRATE
(
    @CURYTYPEID     char(6),
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime,
    @RATE           numeric(6, 4),
    @OPERADOR       char(1)                  = NULL,
    @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,
           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_UpdProcCURYRATETYPE 
 */

CREATE PROCEDURE SP_UpdProcCURYRATETYPE
(
    @CURYTYPEID     char(6),
    @DESCR          char(30)             = NULL,
    @USER4          UD_USER7             = NULL,
    @USER1          UD_USER1             = NULL,
    @USER2          UD_USER2             = NULL,
    @USER3          UD_USER6             = NULL,
    @TSTAMP         timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CURYRATETYPE
       SET DESCR           = @DESCR,
           USER4           = @USER4,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3
     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_UpdProcCUST_LIMIT 
 */



/* 
 * PROCEDURE: sp_UpdProcCUST_LIMIT 
 */

CREATE PROCEDURE sp_UpdProcCUST_LIMIT
(
    @CUSTIDSS        UD_CODCLIENT,
    @SITEID          UD_IDSITE,
    @CREDLIMITED     UD_MONTO,
    @USER1           UD_USER1                = NULL,
    @USER2           UD_USER2                = NULL,
    @USER3           UD_USER6                = NULL,
    @USER4           UD_USER7                = NULL,
    @TSTAMP          timestamp               = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUST_LIMIT
       SET CREDLIMITED      = @CREDLIMITED,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: SP_UpdProcCUSTOMER 
 */

CREATE PROCEDURE SP_UpdProcCUSTOMER
(
    @CUSTIDSS         UD_CODCLIENT,
    @CUSTID           UD_CODCLIENT,
    @NAME             UD_NOMBRE                = NULL,
    @ADDR1            UD_DIRECCION,
    @ADDR2            UD_DIRECCION,
    @TAXREGNBR        UD_CODCLIENT,
    @DATEORIG         datetime,
    @DATEPROC         datetime,
    @PHONE            char(10),
    @FAX              char(10),
    @ZONECUSTOMER     char(2),
    @SLSPERID         UD_IDVENDEDOR,
    @FORMPAG          char(2),
    @TYPEPAG          char(2),
    @TERMSID          char(2),
    @CURYID           UD_CURYID,
    @CRLMT            numeric(9, 2),
    @CLASSCUSTID      char(2),
    @PRCLVLCUSTID     UD_PRECLVID,
    @WEEKDAYPAG       char(12),
    @THANKDAY         int,
    @TSTAMP           timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTOMER
       SET CUSTID            = @CUSTID,
           NAME              = @NAME,
           ADDR1             = @ADDR1,
           ADDR2             = @ADDR2,
           TAXREGNBR         = @TAXREGNBR,
           DATEORIG          = @DATEORIG,
           DATEPROC          = @DATEPROC,
           PHONE             = @PHONE,
           FAX               = @FAX,
           ZONECUSTOMER      = @ZONECUSTOMER,
           SLSPERID          = @SLSPERID,
           FORMPAG           = @FORMPAG,
           TYPEPAG           = @TYPEPAG,
           TERMSID           = @TERMSID,
           CURYID            = @CURYID,
           CRLMT             = @CRLMT,
           CLASSCUSTID       = @CLASSCUSTID,
           PRCLVLCUSTID      = @PRCLVLCUSTID,
           WEEKDAYPAG        = @WEEKDAYPAG,
           THANKDAY          = @THANKDAY
     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_UpdProcCUSTSTATUS 
 */



/* 
 * PROCEDURE: sp_UpdProcCUSTSTATUS 
 */

CREATE PROCEDURE sp_UpdProcCUSTSTATUS
(
    @CUSTSTATUSID     char(1),
    @DESCRCUST        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 CUSTSTATUS
       SET DESCRCUST         = @DESCRCUST,
           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_UpdProcCUSTWARRANTY 
 */



/* 
 * PROCEDURE: sp_UpdProcCUSTWARRANTY 
 */

CREATE PROCEDURE sp_UpdProcCUSTWARRANTY
(
    @CUSTIDSS               UD_CODCLIENT,
    @CUSTGARANTIATYPEID     char(2),
    @TOTGAR                 UD_MONTO,
    @DESCRGAR               UD_DESCRIPCION,
    @EXPIREDATE             UD_FECHA,
    @CURYID                 UD_CURYID,
    @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,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE CUSTIDSS           = @CUSTIDSS
       AND CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

    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_UpdProcCUSTWARRANTYTYPE 
 */



/* 
 * PROCEDURE: sp_UpdProcCUSTWARRANTYTYPE 
 */

CREATE PROCEDURE sp_UpdProcCUSTWARRANTYTYPE
(
    @CUSTGARANTIATYPEID     char(2),
    @DECRCUSTGARANTIA       binary(20),
    @USER1                  UD_USER1              = NULL,
    @USER2                  UD_USER2              = NULL,
    @TSTAMP                 timestamp             = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTWARRANTYTYPE
       SET DECRCUSTGARANTIA        = @DECRCUSTGARANTIA,
           USER1                   = @USER1,
           USER2                   = @USER2
     WHERE CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

    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_UpdProcDOCTYPE 
 */

CREATE PROCEDURE SP_UpdProcDOCTYPE
(
    @DOCTYPEID     UD_DOCTYPEID,
    @DESCR         UD_DESCRIPCION,
    @MINDES        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 DOCTYPE
       SET DESCR          = @DESCR,
           MINDES         = @MINDES,
           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_UpdProcIN_CATEGORY 
 */

CREATE PROCEDURE SP_UpdProcIN_CATEGORY
(
    @CLASSID     char(5),
    @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 IN_CATEGORY
       SET DESCR        = @DESCR,
           USER1        = @USER1,
           USER2        = @USER2,
           USER3        = @USER3,
           USER4        = @USER4
     WHERE CLASSID = @CLASSID

    IF (@@error!=0) OR (@@rowcount=0)
    BEGIN
        RAISERROR  20001 'No se puede Actualizar la Categoría'
        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_UpdProcIN_CLASSVENDOR 
 */

CREATE PROCEDURE SP_UpdProcIN_CLASSVENDOR
(
    @CLASSVENDORID     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 IN_CLASSVENDOR
       SET DESCR              = @DESCR,
           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_UpdProcIN_CLUSTER 
 */



/* 
 * 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_UpdProcIN_DOCSTATUS 
 */

CREATE PROCEDURE SP_UpdProcIN_DOCSTATUS
(
    @DOCSTATUSID     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 IN_DOCSTATUS
       SET DESCR            = @DESCR,
           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_UpdProcIN_INTRAHEAD 
 */

CREATE PROCEDURE SP_UpdProcIN_INTRAHEAD
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @VENDIDSS          UD_IDVENDOR,
    @DOCTYPEID         UD_DOCTYPEID,
    @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,
    @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,
           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_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                    = NULL,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STKFISI           bit,
    @INTRDATE          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @RLSED             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,
    @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,
           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_UpdProcIN_INVENTHISTOPHISI 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTHISTOPHISI
(
    @SITEID               UD_IDSITE,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @CLASSID              char(5),
    @CLASSUBID            char(5),
    @STOCKPHISICAL        UD_STOCK,
    @QTYAVAIAL            UD_STOCK,
    @INTRDATEPHISICAL     smalldatetime,
    @INTRDATEADJUST       smalldatetime)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_INVENTHISTOPHISI
       SET CLASSID               = @CLASSID,
           CLASSUBID             = @CLASSUBID,
           STOCKPHISICAL         = @STOCKPHISICAL,
           QTYAVAIAL             = @QTYAVAIAL,
           INTRDATEPHISICAL      = @INTRDATEPHISICAL,
           INTRDATEADJUST        = @INTRDATEADJUST
     WHERE SITEID    = @SITEID
       AND INVTIDSKU = @INVTIDSKU

    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_UpdProcIN_INVENTORY 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORY
(
    @INVTIDSKU       UD_IDCORPORATIVO,
    @TAXISC          UD_TAXID                    = NULL,
    @TAXIGV          UD_TAXID,
    @CLASSUBID       char(5),
    @INVTYPEID       char(5),
    @STKUNITID       UD_STKUNITID,
    @CLASSID         char(5),
    @DESCR           varchar(40),
    @WTSTK1          numeric(7, 3),
    @STKFUELS        bit,
    @STKSTORE        bit,
    @STKOTHER        bit,
    @STKITEM         bit,
    @KIT             bit,
    @DISCDSCTO1      UD_PORCENTAJE,
    @DISCDSCTO2      UD_PORCENTAJE,
    @DISCMTO         numeric(12, 2),
    @STKRECEPT       bit,
    @STKSALES        bit,
    @STATUS          bit,
    @CONSIGNMENT     bit,
    @DISASSEMBLE     bit,
    @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 IN_INVENTORY
       SET TAXISC           = @TAXISC,
           TAXIGV           = @TAXIGV,
           CLASSUBID        = @CLASSUBID,
           INVTYPEID        = @INVTYPEID,
           STKUNITID        = @STKUNITID,
           CLASSID          = @CLASSID,
           DESCR            = @DESCR,
           WTSTK1           = @WTSTK1,
           STKFUELS         = @STKFUELS,
           STKSTORE         = @STKSTORE,
           STKOTHER         = @STKOTHER,
           STKITEM          = @STKITEM,
           KIT              = @KIT,
           DISCDSCTO1       = @DISCDSCTO1,
           DISCDSCTO2       = @DISCDSCTO2,
           DISCMTO          = @DISCMTO,
           STKRECEPT        = @STKRECEPT,
           STKSALES         = @STKSALES,
           STATUS           = @STATUS,
           CONSIGNMENT      = @CONSIGNMENT,
           DISASSEMBLE      = @DISASSEMBLE,
           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_UpdProcIN_INVENTORYALTER 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYALTER
(
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @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,
           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_UpdProcIN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYDAT
(
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @STOCKSTAR          UD_STOCK,
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       UD_FECHA                    = NULL,
    @ENDDATEINV         UD_FECHA                    = NULL,
    @LASTINVFIS         UD_STOCK                    = NULL,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         UD_COSTO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @LASTSTDCOSTPEN     UD_COSTO,
    @LASTSTDCOSTUSD     UD_COSTO,
    @SLS_PRICE          UD_PRECIO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID,
    @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,
           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_UpdProcIN_INVENTORYDES 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYDES
(
    @INVTIDSKU        UD_IDCORPORATIVO,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint,
    @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,
           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_UpdProcIN_INVENTORYTYPE 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYTYPE
(
    @INVTYPEID     char(5),
    @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 IN_INVENTORYTYPE
       SET DESCR          = @DESCR,
           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_UpdProcIN_INVENTORYUNITMED 
 */

CREATE PROCEDURE SP_UpdProcIN_INVENTORYUNITMED
(
    @STKUNITID     UD_STKUNITID,
    @DESCR         UD_DESCRIPCION,
    @STKDIVIDE 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,           
           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_UpdProcIN_INVENTPHISICAL 
 */

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

    UPDATE IN_INVENTPHISICAL
       SET CLASSUBID             = @CLASSUBID,
           CLASSID               = @CLASSID,
           STOCKPHISICAL         = @STOCKPHISICAL,
           QTYAVAIL              = @QTYAVAIL,
           INTRDATEPHISICAL      = @INTRDATEPHISICAL
     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_UpdProcIN_KDXHIST 
 */

CREATE PROCEDURE SP_UpdProcIN_KDXHIST
(
    @YEARHIST      smallint,
    @SITEID        UD_IDSITE,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @QTYEND00      UD_STOCK,
    @QTYEND01      UD_STOCK,
    @QTYEND02      UD_STOCK,
    @QTYEND03      UD_STOCK,
    @QTYEND04      UD_STOCK,
    @QTYEND05      UD_STOCK,
    @QTYEND06      UD_STOCK,
    @QTYEND07      UD_STOCK,
    @QTYEND08      UD_STOCK,
    @QTYEND09      UD_STOCK,
    @QTYEND10      UD_STOCK,
    @QTYEND11      UD_STOCK,
    @QTYEND12      UD_STOCK,
    @QTYEND13      UD_STOCK,
    @AVGPEN00      UD_COSTO,
    @AVGPEN01      UD_COSTO,
    @AVGPEN02      UD_COSTO,
    @AVGPEN03      UD_COSTO,
    @AVGPEN04      UD_COSTO,
    @AVGPEN06      UD_COSTO,
    @AVGPEN05      UD_COSTO,
    @AVGPEN07      UD_COSTO,
    @AVGPEN08      UD_COSTO,
    @AVGPEN09      UD_COSTO,
    @AVGPEN10      UD_COSTO,
    @AVGPEN11      UD_COSTO,
    @AVGPEN12      UD_COSTO,
    @AVGPEN13      UD_COSTO,
    @AVGUSD00      UD_COSTO,
    @AVGUSD01      UD_COSTO,
    @AVGUSD02      UD_COSTO,
    @AVGUSD03      UD_COSTO,
    @AVGUSD04      UD_COSTO,
    @AVGUSD05      UD_COSTO,
    @AVGUSD06      UD_COSTO,
    @AVGUSD07      UD_COSTO,
    @AVGUSD08      UD_COSTO,
    @AVGUSD09      UD_COSTO,
    @AVGUSD10      UD_COSTO,
    @AVGUSD11      UD_COSTO,
    @AVGUSD12      UD_COSTO,
    @AVGUSD13      UD_COSTO,
    @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,
           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_UpdProcIN_KIT 
 */

CREATE PROCEDURE SP_UpdProcIN_KIT
(
    @KITID          UD_KITID,
    @STKUNITID      UD_STKUNITID,
    @CURYID         UD_CURYID,
    @TAXID          UD_TAXID,
    @DESCR          UD_DESCRIPCION,
    @MTOCTOREPS     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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,
           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_UpdProcIN_KITCOMPONENT 
 */

CREATE PROCEDURE SP_UpdProcIN_KITCOMPONENT
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @QTY            numeric(9, 3),
    @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,
           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_UpdProcIN_PRICELEVELTEM 
 */

CREATE PROCEDURE SP_UpdProcIN_PRICELEVELTEM
(
    @INVTIDSKU             UD_IDCORPORATIVO,
    @SITEID                UD_IDSITE,
    @CORRNBR               char(10),
    @PRECLVID              UD_PRECLVID,
    @CURYID                UD_CURYID,
    @DESCR                 UD_DESCRIPCION,
    @SLSPRICE              numeric(6, 2),
    @STARTDATEPRICE        smalldatetime,
    @ENDDATEPRICE          smalldatetime,
    @STARTTIMEPRICE        tinyint,
    @ENDTIMEPRICE          tinyint,
    @STATUS                bit,
    @STKPRICESEND          bit,
    @USERIDAUTORIZED       char(10),
    @DATETIMEAUTORIZED     smalldatetime,
    @SITEORIG              UD_IDSITE,
    @STKPRICESS            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_PRICELEVELTEM
       SET PRECLVID               = @PRECLVID,
           CURYID                 = @CURYID,
           DESCR                  = @DESCR,
           SLSPRICE               = @SLSPRICE,
           STARTDATEPRICE         = @STARTDATEPRICE,
           ENDDATEPRICE           = @ENDDATEPRICE,
           STARTTIMEPRICE         = @STARTTIMEPRICE,
           ENDTIMEPRICE           = @ENDTIMEPRICE,
           STATUS                 = @STATUS,
           STKPRICESEND           = @STKPRICESEND,
           USERIDAUTORIZED        = @USERIDAUTORIZED,
           DATETIMEAUTORIZED      = @DATETIMEAUTORIZED,
           SITEORIG               = @SITEORIG,
           STKPRICESS             = @STKPRICESS,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4,
           USER5                  = @USER5,
           USER6                  = @USER6,
           USER7                  = @USER7,
           USER8                  = @USER8,
           USER9                  = @USER9
     WHERE INVTIDSKU = @INVTIDSKU
       AND SITEID    = @SITEID
       AND CORRNBR   = @CORRNBR

    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_UpdProcIN_PRICELEVELTEMDET 
 */



/* 
 * PROCEDURE: sp_UpdProcIN_PRICELEVELTEMDET 
 */

CREATE PROCEDURE sp_UpdProcIN_PRICELEVELTEMDET
(
    @BATNBRPRICELEVELTEM     UD_CORRELATIVOS,
    @SITEID                  UD_IDSITE,
    @SEQUENCE                UD_REGISTRO,
    @INVTIDSKU               UD_IDCORPORATIVO            = NULL,
    @DESCR                   UD_DESCRIPCION,
    @SLS_PRICE               UD_PRECIO,
    @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,
           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_UpdProcIN_PRICELEVELTYPE 
 */

CREATE PROCEDURE SP_UpdProcIN_PRICELEVELTYPE
(
    @PRECLVID       UD_PRECLVID,
    @CURYID         UD_CURYID,
    @CURYTYPEID     char(6),
    @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 IN_PRICELEVELTYPE
       SET CURYID          = @CURYID,
           CURYTYPEID      = @CURYTYPEID,
           DESCR           = @DESCR,
           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_UpdProcIN_SUBCATEGORY 
 */

CREATE PROCEDURE SP_UpdProcIN_SUBCATEGORY
(
    @CLASSUBID     char(5),
    @CLASSID       char(5),
    @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 IN_SUBCATEGORY
       SET DESCR          = @DESCR,
           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_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),
    @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,
           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_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	UD_CURYID,
    @CURYTYPEID  UD_CURYTYPEID,
    @TAXID	UD_TAXID,
    @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,
           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_UpdProcIN_VENDORINVT 
 */

CREATE PROCEDURE SP_UpdProcIN_VENDORINVT
(
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @STDCOST       UD_COSTO,
    @CURYID        UD_CURYID,
    @BUYDATE       smalldatetime               = NULL,
    @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,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4
     WHERE VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU

    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_UpdProcINTRAMOV 
 */

CREATE PROCEDURE SP_UpdProcINTRAMOV
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @BATNBR            char(10),
    @INTRDATE          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @TYPEDOCID         UD_TYPEDOC,
    @CURYRATE          UD_CAMBIO,
    @CURYDATE          smalldatetime,
    @PERPOST           UD_PERPOST,
    @SWTINOUT          bit,
    @QTY               UD_STOCK,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STDCOSTPEN        UD_COSTO                    = NULL,
    @COMPONENT         bit,
    @SLSPRICE          UD_PRECIO,
    @REFNBR            char(15)                    = NULL,
    @DOCTYPEID         UD_DOCTYPEID                = 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 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,
           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_UpdProcOP_CARA_FUEL 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CARA_FUEL 
 */

CREATE PROCEDURE sp_UpdProcOP_CARA_FUEL
(
    @CARA           UD_CARA,
    @INVTIDBARR     UD_CODIGOBARRA            = NULL,
    @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_CARA_FUEL
       SET INVTIDBARR      = @INVTIDBARR,
           DESCR           = @DESCR,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE CARA = @CARA

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcOP_CARD 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CARD 
 */

CREATE PROCEDURE sp_UpdProcOP_CARD
(
    @CARDID          char(2),
    @ACCOUNT         char(12),
    @DESCR           UD_NOMBRE,
    @NUMBERORDER     tinyint,
    @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,
           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_UpdProcOP_CARD_MONT 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CARD_MONT 
 */

CREATE PROCEDURE sp_UpdProcOP_CARD_MONT
(
    @SITEID        UD_IDSITE,
    @CARDID        char(2),
    @DATE          UD_FECHA,
    @DATEPROCE     UD_FECHA,
    @CURYID        UD_CURYID,
    @TOTALCARD     UD_MONTO,
    @STATUS        bit                  = NULL,
    @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 DATEPROCE      = @DATEPROCE,
           CURYID         = @CURYID,
           TOTALCARD      = @TOTALCARD,
           STATUS         = @STATUS,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4
     WHERE SITEID = @SITEID
       AND CARDID = @CARDID
       AND DATE   = @DATE

    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_UpdProcOP_CCSALESPERSON 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CCSALESPERSON 
 */

CREATE PROCEDURE sp_UpdProcOP_CCSALESPERSON
(
    @SITEID         UD_IDSITE,
    @SALESPERID     UD_IDVENDEDOR,
    @CURYID         UD_CURYID,
    @TOTAL          UD_MONTO,
    @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 CURYID          = @CURYID,
           TOTAL           = @TOTAL,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID

    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_UpdProcOP_CCSALESPERSONDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CCSALESPERSONDET 
 */

CREATE PROCEDURE sp_UpdProcOP_CCSALESPERSONDET
(
    @SITEID                  UD_IDSITE,
    @SALESPERID              UD_IDVENDEDOR,
    @SEQUENCE                UD_REGISTRO,
    @CCSALESPERSONTYPEID     char(2),
    @DATEPROCE               UD_FECHA,
    @OBS                     char(100)                = NULL,
    @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 CCSALESPERSONTYPEID      = @CCSALESPERSONTYPEID,
           DATEPROCE                = @DATEPROCE,
           OBS                      = @OBS,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID
       AND SEQUENCE   = @SEQUENCE

    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_UpdProcOP_CCSALESPERSONTYPE 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CCSALESPERSONTYPE 
 */

CREATE PROCEDURE sp_UpdProcOP_CCSALESPERSONTYPE
(
    @CCSALESPERSONTYPEID        char(2),
    @DESCRCCSALESPERSONTYPE     UD_DESCRIPCION,
    @DEBE                       UD_DEBE_HABER,
    @HABER                      UD_DEBE_HABER,
    @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,
           DEBE                        = @DEBE,
           HABER                       = @HABER,
           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_UpdProcOP_CORELATIVE 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CORELATIVE 
 */

CREATE PROCEDURE sp_UpdProcOP_CORELATIVE
(
    @TYPECORELATIVE     char(20),
    @NBRCORELATIVE      UD_CORRELATIVOS,
    @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_CORELATIVE
       SET NBRCORELATIVE       = @NBRCORELATIVE,
           USER01              = @USER01,
           USER02              = @USER02,
           USER03              = @USER03,
           USER04              = @USER04
     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_UpdProcOP_CTRLFUEL 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CTRLFUEL 
 */

CREATE PROCEDURE sp_UpdProcOP_CTRLFUEL
(
    @SITEID               UD_IDSITE,
    @BATNBRCTRLFUEL       UD_CORRELATIVOS,
    @DATEPROCE            UD_FECHA,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @STOCKINITIAL         UD_STOCK,
    @QTYIMPUT             UD_STOCK,
    @QTYOUTPUT            UD_STOCK,
    @QTYSALES             UD_STOCK,
    @QTYAVAIL             UD_STOCK,
    @STKUNITID            UD_STKUNITID,
    @VALUEUNIMED          numeric(7, 4),
    @QTYDIFFERENCE        UD_STOCK,
    @PORCENTDIFERENCE     UD_PORCENTAJE,
    @REMARK               UD_DESCRIPCION,
    @STATUS               bit                         = NULL,
    @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 DATEPROCE             = @DATEPROCE,
           INVTIDSKU             = @INVTIDSKU,
           STOCKINITIAL          = @STOCKINITIAL,
           QTYIMPUT              = @QTYIMPUT,
           QTYOUTPUT             = @QTYOUTPUT,
           QTYSALES              = @QTYSALES,
           QTYAVAIL              = @QTYAVAIL,
           STKUNITID             = @STKUNITID,
           VALUEUNIMED           = @VALUEUNIMED,
           QTYDIFFERENCE         = @QTYDIFFERENCE,
           PORCENTDIFERENCE      = @PORCENTDIFERENCE,
           REMARK                = @REMARK,
           STATUS                = @STATUS,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL

    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_UpdProcOP_CTRLFUELDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CTRLFUELDET 
 */

CREATE PROCEDURE sp_UpdProcOP_CTRLFUELDET
(
    @SITEID             UD_IDSITE,
    @BATNBRCTRLFUEL     UD_CORRELATIVOS,
    @SEQUENCE           tinyint,
    @CURYID             UD_CURYID,
    @QTYSALES           UD_STOCK,
    @SLS_PRICE          UD_PRECIO,
    @STATUS             bit                        = NULL,
    @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_CTRLFUELDET
       SET CURYID              = @CURYID,
           QTYSALES            = @QTYSALES,
           SLS_PRICE           = @SLS_PRICE,
           STATUS              = @STATUS,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL
       AND SEQUENCE       = @SEQUENCE

    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_UpdProcOP_CTRLTURN 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CTRLTURN 
 */

CREATE PROCEDURE sp_UpdProcOP_CTRLTURN
(
    @BATNBRTURN     UD_CORR_CONTOMET,
    @SALESPERID     UD_IDVENDEDOR,
    @SITEID         UD_IDSITE,
    @DATEPROCES     UD_FECHA,
    @NBRTUR         char(1),
    @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,
           SITEID          = @SITEID,
           DATEPROCES      = @DATEPROCES,
           NBRTUR          = @NBRTUR,
           USER01          = @USER01,
           USER02          = @USER02,
           USER03          = @USER03,
           USER04          = @USER04
     WHERE BATNBRTURN = @BATNBRTURN

    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_UpdProcOP_CTRLTURNDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CTRLTURNDET 
 */

CREATE PROCEDURE sp_UpdProcOP_CTRLTURNDET
(
    @BATNBRTURN       UD_CORR_CONTOMET,
    @CARA             UD_CARA,
    @INVTIDSKU        UD_IDCORPORATIVO,
    @STARTREADING     numeric(13, 3),
    @ENDREADING       numeric(13, 3),
    @QTYSALES         UD_STOCK,
    @QTYSERAF         UD_STOCK,
    @QTYOTHER         UD_STOCK,
    @TOTQTY           UD_STOCK,
    @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 CARA              = @CARA,
           INVTIDSKU         = @INVTIDSKU,
           STARTREADING      = @STARTREADING,
           ENDREADING        = @ENDREADING,
           QTYSALES          = @QTYSALES,
           QTYSERAF          = @QTYSERAF,
           QTYOTHER          = @QTYOTHER,
           TOTQTY            = @TOTQTY,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE BATNBRTURN = @BATNBRTURN

    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_UpdProcOP_CUSTCARD 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CUSTCARD 
 */

CREATE PROCEDURE sp_UpdProcOP_CUSTCARD
(
    @CUSTIDSS          UD_CODCLIENT,
    @EXPIREDATE        UD_FECHA,
    @DESCRCUSTCARD     UD_DESCRIPCION,
    @CARDPASSWORD      char(10),
    @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,
           CARDPASSWORD       = @CARDPASSWORD,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE CUSTIDSS = @CUSTIDSS

    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_UpdProcOP_CUSTCLASS 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CUSTCLASS 
 */

CREATE PROCEDURE sp_UpdProcOP_CUSTCLASS
(
    @CUSTCLASSID     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_CUSTCLASS
       SET DESCR            = @DESCR,
           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_UpdProcOP_CUSTZONE 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_CUSTZONE 
 */

CREATE PROCEDURE sp_UpdProcOP_CUSTZONE
(
    @CUSTZONEID     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_CUSTZONE
       SET DESCR           = @DESCR,
           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_UpdProcOP_DEFINITIONPD 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DEFINITIONPD 
 */

CREATE PROCEDURE sp_UpdProcOP_DEFINITIONPD
(
    @DEFINITIONID     char(2),
    @CUSTIDSS         UD_CODCLIENT                = NULL,
    @CLASSID          UD_CATEGORY                 = NULL,
    @CLASSUBID        char(5)                     = NULL,
    @INVTIDSKU        UD_IDCORPORATIVO            = NULL,
    @DEBE             UD_DEBE_HABER,
    @HABER            UD_DEBE_HABER,
    @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,
           DEBE              = @DEBE,
           HABER             = @HABER,
           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_UpdProcOP_DEPOSIT 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSIT 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSIT
(
    @BATNBRDEPOSIT       UD_CORREL_DEPOSIT,
    @SITEID              UD_IDSITE,
    @CURYID              UD_CURYID                    = NULL,
    @SALESPERSONTURN     tinyint,
    @DEPOSITTYPEID       char(1),
    @SALESPERID          UD_IDVENDEDOR,
    @OPDATE              UD_FECHA,
    @DATEPROCE           UD_FECHA,
    @TOTPEN              UD_MONTO,
    @TOTUSD              UD_MONTO,
    @STATUS              bit                          = NULL,
    @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 CURYID               = @CURYID,
           SALESPERSONTURN      = @SALESPERSONTURN,
           DEPOSITTYPEID        = @DEPOSITTYPEID,
           SALESPERID           = @SALESPERID,
           OPDATE               = @OPDATE,
           DATEPROCE            = @DATEPROCE,
           TOTPEN               = @TOTPEN,
           TOTUSD               = @TOTUSD,
           STATUS               = @STATUS,
           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_UpdProcOP_DEPOSITDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSITDET 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSITDET
(
    @BATNBRDEPOSIT     UD_CORREL_DEPOSIT,
    @SITEID            UD_IDSITE,
    @SEQUENCE          UD_REGISTRO,
    @CARDID            char(2)                      = NULL,
    @TOTCARD           UD_MONTO                     = NULL,
    @CUSTIDSS          UD_CODCLIENT                 = NULL,
    @MTOBONUS          UD_MONTO,
    @MTOCASH           UD_MONTO,
    @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,
           TOTCARD            = @TOTCARD,
           CUSTIDSS           = @CUSTIDSS,
           MTOBONUS           = @MTOBONUS,
           MTOCASH            = @MTOCASH,
           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_UpdProcOP_DEPOSITSITE 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSITSITE 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSITSITE
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     UD_CORRELATIVOS,
    @CUSTIDSS              UD_CODCLIENT               = NULL,
    @DATEPROCE             UD_FECHA,
    @TOTALPEN              UD_MONTO,
    @TOTALUSD              UD_MONTO,
    @STATUS                bit                        = NULL,
    @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 CUSTIDSS               = @CUSTIDSS,
           DATEPROCE              = @DATEPROCE,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           STATUS                 = @STATUS,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE

    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_UpdProcOP_DEPOSITSITEDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSITSITEDET 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSITSITEDET
(
    @SITEID                UD_IDSITE,
    @BATNBRDEPOSITSITE     UD_CORRELATIVOS,
    @SEQUENCE              UD_REGISTRO,
    @DOCSUMMARYTYPEID      char(2)                    = NULL,
    @REFERENCE             numeric(10, 0),
    @DEPOSITPASSWORD       char(20),
    @DATEDEPOSIT           UD_FECHA,
    @CURYID                UD_CURYID,
    @CURYRATE              UD_CURYRATE,
    @TOTALPEN              UD_MONTO,
    @TOTALUSD              UD_MONTO,
    @DEBE                  UD_DEBE_HABER,
    @HABER                 UD_DEBE_HABER,
    @STATUS                bit                        = NULL,
    @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 DOCSUMMARYTYPEID       = @DOCSUMMARYTYPEID,
           REFERENCE              = @REFERENCE,
           DEPOSITPASSWORD        = @DEPOSITPASSWORD,
           DATEDEPOSIT            = @DATEDEPOSIT,
           CURYID                 = @CURYID,
           CURYRATE               = @CURYRATE,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           DEBE                   = @DEBE,
           HABER                  = @HABER,
           STATUS                 = @STATUS,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       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_UpdProcOP_DEPOSITTYPE 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSITTYPE 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSITTYPE
(
    @DEPOSITTYPEID        char(1),
    @DESCRDEPOSTITYPE     UD_DESCRIPCION,
    @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,
           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_UpdProcOP_DOCSUMMARY 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DOCSUMMARY 
 */

CREATE PROCEDURE sp_UpdProcOP_DOCSUMMARY
(
    @SITEID               UD_IDSITE,
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @DOCDATE              UD_FECHA,
    @DUEDATE              UD_FECHA,
    @DOCSTATUSID          char(2),
    @TAXID01              UD_TAXID,
    @TAXID02              UD_TAXID,
    @TERMID               char(2),
    @CURYTYPEID           UD_CURYTYPEID,
    @CURYRATE             UD_CURYRATE,
    @CURYID               UD_CURYID,
    @CUSTIDSS             UD_CODCLIENT,
    @CUSTID               UD_CODCLIENT,
    @CUSTNAME             UD_NOMBRE                = NULL,
    @TAXREGNBR            UD_CODCLIENT,
    @WEEKDAYPAG           char(12),
    @DAYTHENK             int,
    @TOTALPEN             UD_MONTO,
    @TOTALUSD             UD_MONTO,
    @SUBTOTALPEN          UD_MONTO,
    @SUBTOTALUSD          UD_MONTO,
    @TAXPEN               UD_MONTO,
    @TAXUSD               UD_MONTO,
    @STKITEM              bit                      = NULL,
    @STKPROCE             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_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,
           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_UpdProcOP_DOCSUMMARYDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DOCSUMMARYDET 
 */

CREATE PROCEDURE sp_UpdProcOP_DOCSUMMARYDET
(
    @SITEID               UD_IDSITE,
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             UD_REGISTRO,
    @CURYTYPEID           UD_CURYTYPEID,
    @CURYID               UD_CURYID,
    @TAXID02              UD_TAXID,
    @TAXID01              UD_TAXID,
    @DISCOUNT             numeric(12, 2)              = NULL,
    @INVTIDSKU            UD_IDCORPORATIVO,
    @SLS_PRICE            UD_PRECIO,
    @QTY                  UD_STOCK,
    @TOTALPEN             UD_MONTO,
    @SUBTOTALPEN          UD_MONTO,
    @TAX1PEN              UD_MONTO,
    @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,
           SUBTOTALPEN           = @SUBTOTALPEN,
           TAX1PEN               = @TAX1PEN,
           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_UpdProcOP_DOCSUMMARYTYPE 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_DOCSUMMARYTYPE 
 */

CREATE PROCEDURE sp_UpdProcOP_DOCSUMMARYTYPE
(
    @DOCSUMMARYTYPEID        char(2),
    @DESCRDOCSUMMARYTYPE     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_DOCSUMMARYTYPE
       SET DESCRDOCSUMMARYTYPE      = @DESCRDOCSUMMARYTYPE,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID

    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_UpdProcOP_LOADSALESORD 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_LOADSALESORD 
 */

CREATE PROCEDURE sp_UpdProcOP_LOADSALESORD
(
    @NBRLOADSALE        UD_CORRELATIVOS,
    @CURYID             UD_CURYID,
    @SALESPERID         UD_IDVENDEDOR,
    @CUSTIDSS           UD_CODCLIENT               = NULL,
    @TAXREGNBR          UD_CODCLIENT               = NULL,
    @CUSTNAME           UD_NOMBRE                  = NULL,
    @ADDR1              UD_DIRECCION               = NULL,
    @DOCTYPEID          UD_DOCTYPEID,
    @DOCSTATUSID        char(2),
    @USERID             UD_USERID,
    @OPTRTOTPEN         UD_MONTO,
    @OPTRTAXPEN         UD_IMPUESTO,
    @DOCDATE            UD_FECHA,
    @DATEPROCESALES     UD_FECHA,
    @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,
           SALESPERID          = @SALESPERID,
           CUSTIDSS            = @CUSTIDSS,
           TAXREGNBR           = @TAXREGNBR,
           CUSTNAME            = @CUSTNAME,
           ADDR1               = @ADDR1,
           DOCTYPEID           = @DOCTYPEID,
           DOCSTATUSID         = @DOCSTATUSID,
           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_UpdProcOP_LOADSALESORDDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_LOADSALESORDDET 
 */

CREATE PROCEDURE sp_UpdProcOP_LOADSALESORDDET
(
    @NBRLOADSALE     UD_CORRELATIVOS,
    @SEQUENCE        UD_REGISTRO,
    @INVTIDSKU       UD_IDCORPORATIVO,
    @DESCR           UD_DESCRIPCION,
    @STKUNITID       UD_STKUNITID                = NULL,
    @TAXIGV          UD_IMPUESTO,
    @SLS_PRICE       UD_PRECIO,
    @QTY             UD_STOCK,
    @OPTRTOTDET      UD_MONTO,
    @STDCOSTPEN      UD_COSTO,
    @STRCOSTUSD      UD_COSTO,
    @DISCDSCTO1      UD_PORCENTAJE,
    @DISCDSCTO2      UD_PORCENTAJE,
    @DISCMTO         numeric(12, 2),
    @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 SEQUENCE         = @SEQUENCE,
           INVTIDSKU        = @INVTIDSKU,
           DESCR            = @DESCR,
           STKUNITID        = @STKUNITID,
           TAXIGV           = @TAXIGV,
           SLS_PRICE        = @SLS_PRICE,
           QTY              = @QTY,
           OPTRTOTDET       = @OPTRTOTDET,
           STDCOSTPEN       = @STDCOSTPEN,
           STRCOSTUSD       = @STRCOSTUSD,
           DISCDSCTO1       = @DISCDSCTO1,
           DISCDSCTO2       = @DISCDSCTO2,
           DISCMTO          = @DISCMTO,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE NBRLOADSALE = @NBRLOADSALE

    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_UpdProcOP_MODSALES 
 */



/* 
 * 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_UpdProcOP_PRCLVLCUST 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_PRCLVLCUST 
 */

CREATE PROCEDURE sp_UpdProcOP_PRCLVLCUST
(
    @PRCLVLCUSTID           UD_PRCLVLCUSTID,
    @CUSTIDSS               UD_CODCLIENT               = NULL,
    @SITEID                 UD_IDSITE,
    @CURYID                 UD_CURYID,
    @DESCR                  UD_DESCRIPCION,
    @STARTDATEPRICE         UD_FECHA,
    @ENDDATEPRICE           UD_FECHA,
    @STATUS                 bit,
    @APPROVA1               bit,
    @USERIDAPPROVA1         UD_USERID,
    @DATETIMEAUTORIZED1     smalldatetime,
    @APPROVA2               bit,
    @USERIDAPPROVA2         UD_USERID,
    @DATETIMEAUTORIZED2     UD_FECHA,
    @STKPRICESEND           bit,
    @SITEORIG               UD_IDSITE,
    @STKPRICESS             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_PRCLVLCUST
       SET CUSTIDSS                = @CUSTIDSS,
           SITEID                  = @SITEID,
           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,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID

    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_UpdProcOP_PRCLVLCUSTDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_PRCLVLCUSTDET 
 */

CREATE PROCEDURE sp_UpdProcOP_PRCLVLCUSTDET
(
    @PRCLVLCUSTID      UD_PRCLVLCUSTID,
    @SEQUENCE          UD_REGISTRO,
    @STATUS            bit,
    @DISCDSCTO         UD_PORCENTAJE,
    @DISCMTO           UD_MONTO,
    @SLS_PRICE         UD_PRECIO,
    @SLS_PRICECUST     UD_PRECIO,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @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 STATUS             = @STATUS,
           DISCDSCTO          = @DISCDSCTO,
           DISCMTO            = @DISCMTO,
           SLS_PRICE          = @SLS_PRICE,
           SLS_PRICECUST      = @SLS_PRICECUST,
           INVTIDSKU          = @INVTIDSKU,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SEQUENCE     = @SEQUENCE

    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_UpdProcOP_PROMOTIONCARD 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_PROMOTIONCARD 
 */

CREATE PROCEDURE sp_UpdProcOP_PROMOTIONCARD
(
    @PROMOTIONCARDID       char(20),
    @NAMECLIENT            UD_NOMBRE            = NULL,
    @PASSWORDMAGNETIC      char(30)             = NULL,
    @LASTNAME              UD_NOMBRE            = NULL,
    @USER1                 UD_USER1             = NULL,
    @USER2                 UD_USER2             = NULL,
    @USER3                 UD_USER6             = NULL,
    @USER4                 UD_USER7             = NULL,
    @TSTAMP                timestamp            = NULL,
    @PROMOTIONCARDTYPE     char(2))
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PROMOTIONCARD
       SET NAMECLIENT             = @NAMECLIENT,
           PASSWORDMAGNETIC       = @PASSWORDMAGNETIC,
           LASTNAME               = @LASTNAME,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4,
           PROMOTIONCARDTYPE      = @PROMOTIONCARDTYPE
     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_UpdProcOP_PROMOTIONCARDTYPE 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_PROMOTIONCARDTYPE 
 */

CREATE PROCEDURE sp_UpdProcOP_PROMOTIONCARDTYPE
(
    @PROMOTIONCARDTYPE      char(2),
    @DESCRPROMOTIONCARD     char(10)            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PROMOTIONCARDTYPE
       SET DESCRPROMOTIONCARD      = @DESCRPROMOTIONCARD
     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_UpdProcOP_SALES 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_SALES 
 */

CREATE PROCEDURE sp_UpdProcOP_SALES
(
    @DOCTYPEID            UD_DOCTYPEID,
    @NBRDOCUMENT          UD_CORRELATIVOS,
    @STKINVENTORY         bit,
    @SITEID               UD_IDSITE,
    @CURYID               UD_CURYID,
    @DOCSTATUSID          char(2),
    @TERMID               char(2),
    @MODSALESID           char(2),
    @SALESPOINT           UD_PTOVTA,
    @SALESPERID           UD_IDVENDEDOR,
    @USERID               UD_USERID,
    @DATEDOC              UD_FECHA,
    @DATEPROCESALES       UD_FECHA,
    @PERPOST              UD_PERPOST,
    @TAXIGVID             char(2)                    = NULL,
    @TAXISCID             char(2)                    = NULL,
    @TOTALPEN             UD_MONTO,
    @TOTALUSD             UD_MONTO,
    @SUBTOTALPEN          UD_MONTO,
    @SUBTOTALUSD          UD_MONTO,
    @TAX1PEN              UD_MONTO,
    @TAX1USD              UD_MONTO,
    @TAX2PEN              UD_MONTO,
    @TAX2USD              UD_MONTO,
    @PORCENTDISCOUNT1     UD_PORCENTAJE,
    @PORCENTDISCOUNT2     UD_PORCENTAJE,
    @TOTDISCOUNTPEN       UD_MONTO,
    @TOTDISCOUNTUSD       UD_MONTO,
    @TOTRETURNEDPEN       UD_MONTO,
    @TOTRETURNEDUSD       UD_MONTO,
    @TOTCASHPEN           UD_MONTO,
    @TOTCASHUSD           UD_MONTO,
    @CARDCREDITID1        char(2)                    = NULL,
    @CARDCREDITID2        char(2)                    = NULL,
    @NBRCARD1             char(20)                   = NULL,
    @NBRCARD2             char(20)                   = NULL,
    @TOTCARD1PEN          UD_MONTO,
    @TOTCARD1USD          UD_MONTO,
    @TOTCARD2PEN          UD_MONTO,
    @TOTCARD2USD          UD_MONTO,
    @CUSTIDSS             UD_CODCLIENT               = NULL,
    @TAXREGNBR            UD_CODCLIENT               = NULL,
    @NAME                 UD_NOMBRE                  = NULL,
    @PLACA                UD_PLACA,
    @NBRBONUS             int,
    @CURYTYPEID           UD_CURYTYPEID,
    @CURYRATE             UD_CAMBIO,
    @STKCLOSEDZ           bit,
    @STKCLOSEDX           bit,
    @PROMOTIONCARDID      char(20)                   = NULL,
    @POINTNUMBER          smallint,
    @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 STKINVENTORY          = @STKINVENTORY,
           SITEID                = @SITEID,
           CURYID                = @CURYID,
           DOCSTATUSID           = @DOCSTATUSID,
           TERMID                = @TERMID,
           MODSALESID            = @MODSALESID,
           SALESPOINT            = @SALESPOINT,
           SALESPERID            = @SALESPERID,
           USERID                = @USERID,
           DATEDOC               = @DATEDOC,
           DATEPROCESALES        = @DATEPROCESALES,
           PERPOST               = @PERPOST,
           TAXIGVID              = @TAXIGVID,
           TAXISCID              = @TAXISCID,
           TOTALPEN              = @TOTALPEN,
           TOTALUSD              = @TOTALUSD,
           SUBTOTALPEN           = @SUBTOTALPEN,
           SUBTOTALUSD           = @SUBTOTALUSD,
           TAX1PEN               = @TAX1PEN,
           TAX1USD               = @TAX1USD,
           TAX2PEN               = @TAX2PEN,
           TAX2USD               = @TAX2USD,
           PORCENTDISCOUNT1      = @PORCENTDISCOUNT1,
           PORCENTDISCOUNT2      = @PORCENTDISCOUNT2,
           TOTDISCOUNTPEN        = @TOTDISCOUNTPEN,
           TOTDISCOUNTUSD        = @TOTDISCOUNTUSD,
           TOTRETURNEDPEN        = @TOTRETURNEDPEN,
           TOTRETURNEDUSD        = @TOTRETURNEDUSD,
           TOTCASHPEN            = @TOTCASHPEN,
           TOTCASHUSD            = @TOTCASHUSD,
           CARDCREDITID1         = @CARDCREDITID1,
           CARDCREDITID2         = @CARDCREDITID2,
           NBRCARD1              = @NBRCARD1,
           NBRCARD2              = @NBRCARD2,
           TOTCARD1PEN           = @TOTCARD1PEN,
           TOTCARD1USD           = @TOTCARD1USD,
           TOTCARD2PEN           = @TOTCARD2PEN,
           TOTCARD2USD           = @TOTCARD2USD,
           CUSTIDSS              = @CUSTIDSS,
           TAXREGNBR             = @TAXREGNBR,
           NAME                  = @NAME,
           PLACA                 = @PLACA,
           NBRBONUS              = @NBRBONUS,
           CURYTYPEID            = @CURYTYPEID,
           CURYRATE              = @CURYRATE,
           STKCLOSEDZ            = @STKCLOSEDZ,
           STKCLOSEDX            = @STKCLOSEDX,
           PROMOTIONCARDID       = @PROMOTIONCARDID,
           POINTNUMBER           = @POINTNUMBER,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4,
           USER5                 = @USER5,
           USER6                 = @USER6,
           USER7                 = @USER7,
           USER8                 = @USER8,
           USER9                 = @USER9
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

    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_UpdProcOP_SALESDET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_SALESDET 
 */

CREATE PROCEDURE sp_UpdProcOP_SALESDET
(
    @DOCTYPEID          UD_DOCTYPEID,
    @NBRDOCUMENT        UD_CORRELATIVOS,
    @SEQUENCE           UD_REGISTRO,
    @SITEID             UD_IDSITE,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @CURYID             UD_CURYID,
    @DOCSTATUSID        char(2),
    @DATEDOC            UD_FECHA,
    @DATEPROCESALES     UD_FECHA,
    @PERSPOT            UD_PERPOST,
    @STKCLOSEDZ         bit,
    @STKCLOSEDX         bit,
    @NBRTURN            tinyint,
    @NBRCARA            UD_CARA                     = NULL,
    @NBRTRANSACFUEL     char(4)                     = NULL,
    @DISCDSCTO1         UD_PORCENTAJE,
    @DISCDSCTO2         UD_PORCENTAJE,
    @DISCMTOPEN         UD_MONTO,
    @DISCMTOUSD         UD_MONTO,
    @PORCENTTAXIGV      UD_PORCENTAJE,
    @PORCENTTAXISC      UD_PORCENTAJE,
    @TOTALPEN           UD_MONTO,
    @TOTALUSD           UD_MONTO,
    @TAXPEN             UD_MONTO,
    @TAXUSD             UD_MONTO,
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          UD_PRECIO,
    @SLSPRICESALE       UD_PRECIO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @INVTIDALTER        UD_CODIGOBARRA,
    @DESCRINVENTORY     UD_DESCRIPCION,
    @QTY                UD_STOCK,
    @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,
    @SALESPOINT         UD_PTOVTA,
    @SALESPERID         UD_IDVENDEDOR)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALESDET
       SET SITEID              = @SITEID,
           INVTIDSKU           = @INVTIDSKU,
           CURYID              = @CURYID,
           DOCSTATUSID         = @DOCSTATUSID,
           DATEDOC             = @DATEDOC,
           DATEPROCESALES      = @DATEPROCESALES,
           PERSPOT             = @PERSPOT,
           STKCLOSEDZ          = @STKCLOSEDZ,
           STKCLOSEDX          = @STKCLOSEDX,
           NBRTURN             = @NBRTURN,
           NBRCARA             = @NBRCARA,
           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,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4,
           USER5               = @USER5,
           USER6               = @USER6,
           USER7               = @USER7,
           USER8               = @USER8,
           USER9               = @USER9,
           SALESPOINT          = @SALESPOINT,
           SALESPERID   = @SALESPERID
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       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_UpdProcOP_SALESPERSON 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_SALESPERSON 
 */

CREATE PROCEDURE sp_UpdProcOP_SALESPERSON
(
    @SALESPERID         UD_IDVENDEDOR,
    @SALESPERNAME       UD_NOMBRE                = NULL,
    @IDENTITYDOC        char(10),
    @PHONE              char(10)                 = NULL,
    @SEX                char(1),
    @INITIALDATE        UD_FECHA,
    @SITEID             UD_IDSITE,
    @ADDRESS1           UD_DIRECCION,
    @ADDRESS2           UD_DIRECCION             = NULL,
    @PASSWORD           char(15),
    @BIRTHDATE          UD_FECHA,
    @BIRTHADDRESS1      UD_DIRECCION             = NULL,
    @BIRTHADDRESS2      UD_DIRECCION             = NULL,
    @STATUSPERSONID     char(10),
    @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,
           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_UpdProcOP_SALESPOINTSETING 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_SALESPOINTSETING 
 */

CREATE PROCEDURE sp_UpdProcOP_SALESPOINTSETING
(
    @SALESPOINT            UD_PTOVTA,
    @SALESPERID            UD_IDVENDEDOR,
    @ENDDATEACT            UD_FECHA,
    @SERIALNUMBER          char(16),
    @TOTZETA               UD_TOTZETA,
    @NBRZETA               smallint,
    @PRECLVID              UD_PRECLVID,
    @SITEID                UD_IDSITE,
    @STKTICKFAC            bit,
    @STKTICKBOL            bit,
    @CURYID                UD_CURYID,
    @STKLOADSALE           bit,
    @NBRLOADSALE           UD_CORRELATIVOS,
    @STKPRINTERFAC         bit,
    @STKPRINTEDBOL         bit,
    @TYPEDISPFAC           UD_TYPE_DISP_SALIDA,
    @DISPFAC               UD_DISP_SALIDA,
    @NBRFAC                UD_CORRELATIVOS,
    @TYPEDISPBOL           UD_TYPE_DISP_SALIDA,
    @DISPBOL               UD_DISP_SALIDA,
    @NBRBOL                UD_CORRELATIVOS,
    @TYPEDISPNC            UD_TYPE_DISP_SALIDA,
    @DISPNC                UD_DISP_SALIDA,
    @NBRNC                 UD_CORRELATIVOS,
    @TYPEDISPND            UD_TYPE_DISP_SALIDA,
    @DISPND                UD_DISP_SALIDA,
    @NBRND                 UD_CORRELATIVOS,
    @TYPEDISORDER          UD_TYPE_DISP_SALIDA,
    @DISPORDER             UD_DISP_SALIDA,
    @NBRORDER              UD_CORRELATIVOS,
    @TYPEDISPTICKFAC       UD_TYPE_DISP_SALIDA,
    @DISPTICKFAC           UD_DISP_SALIDA,
    @NBRTICKFAC            UD_CORRELATIVOS,
    @TYPEDISPTICKBOL       UD_TYPE_DISP_SALIDA,
    @DISPTICKBOL           UD_DISP_SALIDA,
    @NBRTICKBOL            UD_CORRELATIVOS,
    @TERMINALNAME          varchar(20),
    @DISPDISPLAY           UD_DISP_SALIDA,
    @STKSALESPERSONID      bit,
    @STKSALESPOINTFAC      bit,
    @STKZETAOK             bit,
    @STKJUMPAUTOMAT        bit,
    @QTYJUNPAUTOMAT        tinyint,
    @STKHOTKEYS            bit,
    @TYPEPRINTERTICKET     char(18),
    @STKDELETEDOC          bit,
    @STK_PRICE_COST        bit,
    @STKDESCFACTUR         bit,
    @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_SALESPOINTSETING
       SET ENDDATEACT             = @ENDDATEACT,
           SERIALNUMBER           = @SERIALNUMBER,
           TOTZETA                = @TOTZETA,
           NBRZETA                = @NBRZETA,
           PRECLVID               = @PRECLVID,
           SITEID                 = @SITEID,
           STKTICKFAC             = @STKTICKFAC,
           STKTICKBOL             = @STKTICKBOL,
           CURYID                 = @CURYID,
           STKLOADSALE            = @STKLOADSALE,
           NBRLOADSALE            = @NBRLOADSALE,
           STKPRINTERFAC          = @STKPRINTERFAC,
           STKPRINTEDBOL          = @STKPRINTEDBOL,
           TYPEDISPFAC            = @TYPEDISPFAC,
           DISPFAC                = @DISPFAC,
           NBRFAC                 = @NBRFAC,
           TYPEDISPBOL            = @TYPEDISPBOL,
           DISPBOL                = @DISPBOL,
           NBRBOL                 = @NBRBOL,
           TYPEDISPNC             = @TYPEDISPNC,
           DISPNC                 = @DISPNC,
           NBRNC                  = @NBRNC,
           TYPEDISPND             = @TYPEDISPND,
           DISPND                 = @DISPND,
           NBRND = @NBRND,
           TYPEDISORDER           = @TYPEDISORDER,
           DISPORDER              = @DISPORDER,
           NBRORDER               = @NBRORDER,
           TYPEDISPTICKFAC        = @TYPEDISPTICKFAC,
           DISPTICKFAC            = @DISPTICKFAC,
           NBRTICKFAC             = @NBRTICKFAC,
           TYPEDISPTICKBOL        = @TYPEDISPTICKBOL,
           DISPTICKBOL            = @DISPTICKBOL,
           NBRTICKBOL             = @NBRTICKBOL,
           TERMINALNAME           = @TERMINALNAME,
           DISPDISPLAY            = @DISPDISPLAY,
           STKSALESPERSONID       = @STKSALESPERSONID,
           STKSALESPOINTFAC       = @STKSALESPOINTFAC,
           STKZETAOK              = @STKZETAOK,
           STKJUMPAUTOMAT         = @STKJUMPAUTOMAT,
           QTYJUNPAUTOMAT         = @QTYJUNPAUTOMAT,
           STKHOTKEYS             = @STKHOTKEYS,
           TYPEPRINTERTICKET      = @TYPEPRINTERTICKET,
           STKDELETEDOC           = @STKDELETEDOC,
           [STK PRICE_COST]       = @STK_PRICE_COST,
           STKDESCFACTUR          = @STKDESCFACTUR,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4,
           USER5                  = @USER5,
           USER6                  = @USER6,
           USER7                  = @USER7,
           USER8                  = @USER8,
           USER9                  = @USER9
     WHERE SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcOP_STATUSPERSON 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_STATUSPERSON 
 */

CREATE PROCEDURE sp_UpdProcOP_STATUSPERSON
(
    @STATUSPERSONID        char(10),
    @DESCRSTATUSPERSON     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_STATUSPERSON
       SET DESCRSTATUSPERSON      = @DESCRSTATUSPERSON,
           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_UpdProcOP_SUMMARYDAY 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_SUMMARYDAY 
 */

CREATE PROCEDURE sp_UpdProcOP_SUMMARYDAY
(
    @BATNBRDIARYSUMMARY     UD_CORRELATIVOS,
    @SITEID                 UD_IDSITE,
    @DATEPROCE              UD_FECHA,
    @CURYRATE               UD_CURYRATE,
    @ROUND                  UD_MONTO,
    @TOTALDIFERENCE         UD_MONTO,
    @OBSERVATION            varchar(100)               = NULL,
    @STATUS                 bit                        = NULL,
    @DEBE                   UD_MONTO,
    @HABER                  UD_MONTO,
    @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,
           DEBE                    = @DEBE,
           HABER                   = @HABER,
           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_UpdProcOP_SUMMARYSALES 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_SUMMARYSALES 
 */

CREATE PROCEDURE sp_UpdProcOP_SUMMARYSALES
(
    @SITEID               UD_IDSITE,
    @DATEPROCE            UD_FECHA,
    @DOCSUMMARYTYPEID     char(2),
    @CLASSID              UD_CATEGORY,
    @CUSTIDSS             UD_CODCLIENT              = NULL,
    @CUSTCLASSID          char(2)                   = NULL,
    @DEFINITIONID         char(2),
    @CLASSUBID            char(5)                   = NULL,
    @REFERENCE            char(15)                  = NULL,
    @TOTALBONUS           UD_MONTO,
    @DIFFERENCE           UD_MONTO,
    @COMMENT              UD_DESCRIPCION,
    @DEBE                 UD_DEBE_HABER,
    @HABER                UD_DEBE_HABER,
    @TOTALDEBE            UD_MONTO,
    @TOTALHABER           UD_MONTO,
    @TOTALPEN             UD_MONTO,
    @TOTALUSD             UD_MONTO,
    @STATUS               bit                       = NULL,
    @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 DOCSUMMARYTYPEID      = @DOCSUMMARYTYPEID,
           CLASSID               = @CLASSID,
           CUSTIDSS              = @CUSTIDSS,
           CUSTCLASSID           = @CUSTCLASSID,
           DEFINITIONID          = @DEFINITIONID,
           CLASSUBID             = @CLASSUBID,
           REFERENCE             = @REFERENCE,
           TOTALBONUS            = @TOTALBONUS,
           DIFFERENCE            = @DIFFERENCE,
           COMMENT               = @COMMENT,
           DEBE                  = @DEBE,
           HABER                 = @HABER,
           TOTALDEBE             = @TOTALDEBE,
           TOTALHABER            = @TOTALHABER,
           TOTALPEN              = @TOTALPEN,
           TOTALUSD              = @TOTALUSD,
           STATUS                = @STATUS,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID    = @SITEID
       AND 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_UpdProcOP_TURNVSCARA 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_TURNVSCARA 
 */

CREATE PROCEDURE sp_UpdProcOP_TURNVSCARA
(
    @SITEID               UD_IDSITE,
    @BATNBRTURNPERSON     UD_TURNOSPERSONAL,
    @DATEPROCE            UD_FECHA,
    @NUMBERTURN           tinyint,
    @SALESPERID           UD_IDVENDEDOR,
    @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_TURNVSCARA
       SET SALESPERID            = @SALESPERID,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcOP_TURNVSCARADET 
 */



/* 
 * PROCEDURE: sp_UpdProcOP_TURNVSCARADET 
 */

CREATE PROCEDURE sp_UpdProcOP_TURNVSCARADET
(
    @SITEID               UD_IDSITE,
    @BATNBRTURNPERSON     UD_TURNOSPERSONAL,
    @DATEPROCE            UD_FECHA,
    @NUMBERTURN           tinyint,
    @CARA                 UD_CARA,
    @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_TURNVSCARADET
       SET CARA                  = @CARA,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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

    COMMIT TRAN

    RETURN(0)
END
go
IF OBJECT_ID('sp_UpdProcOP_TURNVSCARADET') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_UpdProcOP_TURNVSCARADET >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_UpdProcOP_TURNVSCARADET >>>'
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,
    @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,
           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_UpdProcSE_GROUPREC 
 */

CREATE PROCEDURE SP_UpdProcSE_GROUPREC
(
    @GROUPID       UD_USERID,
    @GROUPNAME     UD_NOMBRE,
    @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,
           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_UpdProcSE_MODULE 
 */

CREATE PROCEDURE SP_UpdProcSE_MODULE
(
    @MODULEID       UD_MODULOID,
    @MODULENAME     UD_NOMBRE,
    @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,
           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_UpdProcSE_SCREEN 
 */

CREATE PROCEDURE SP_UpdProcSE_SCREEN
(
    @SCREENID       UD_SCREENID,
    @MODULEID       UD_MODULOID,
    @SCREENNAME     UD_NOMBRE,
    @SCREENTYPE     char(1),
    @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,
           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_UpdProcSE_USERGROUP 
 */

CREATE PROCEDURE SP_UpdProcSE_USERGROUP
(
    @USERID      UD_USERID,
    @GROUPID     UD_USERID,
    @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 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_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            = NULL,
    @STATUS           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_USERREC
       SET USERNAME          = @USERNAME,
           EMAILADDRESS      = @EMAILADDRESS,
           LOCATION          = @LOCATION,
           PASSWORD          = @PASSWORD,
           PHONE             = @PHONE,
           EXPIRED           = @EXPIRED,
           STATUS            = @STATUS,
           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_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),
    @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,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4,
           USER5               = @USER5,
           USER6               = @USER6,
           USER7               = @USER7,
           USER8               = @USER8,
           USER9               = @USER9
     WHERE SITEID = @SITEID

    IF (@@error!=0 OR @@ROWCOUNT=0)
    BEGIN
        RAISERROR  20001 'No se puede Actulizar el Almacén'
        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_UpdProcSTATE 
 */

CREATE PROCEDURE SP_UpdProcSTATE
(
    @STATEID     char(2),
    @DESCR       UD_DESCRIPCION,
    @TSTAMP      timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE STATE
       SET DESCR        = @DESCR
     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_UpdProcTAXS 
 */

CREATE PROCEDURE SP_UpdProcTAXS
(
    @TAXID      UD_TAXID,
    @DESCR      UD_DESCRIPCION,
    @VALUE      numeric(5, 2),
    @STATUS     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,
           STATUS      = @STATUS,
           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_UpdProcTERMS 
 */

CREATE PROCEDURE SP_UpdProcTERMS
(
    @TERMID       char(2),
    @DUEINTRV     tinyint,
    @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 TERMS
       SET DUEINTRV      = @DUEINTRV,
           DESCR         = @DESCR,
           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_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_UpdProcTMP_IN_INVENTORYDAT 
 */

CREATE PROCEDURE SP_UpdProcTMP_IN_INVENTORYDAT
(
    @CORRNBR            int,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @STOCKSTAR          UD_STOCK,
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       smalldatetime               = NULL,
    @ENDDATEINV         smalldatetime               = NULL,
    @LASTINVFIS         UD_STOCK,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         UD_COSTO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @LASTSTDCOSTPEN     UD_COSTO,
    @LASTSTDCOSTUSD     UD_COSTO,
    @SLS_PRICE          UD_COSTO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID,
    @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,
           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_UpdProcTMP_IN_VENDORINVT 
 */

CREATE PROCEDURE SP_UpdProcTMP_IN_VENDORINVT
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @CURYID        UD_CURYID,
    @STDCOST       UD_COSTO,
    @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,
           CURYID         = @CURYID,
           STDCOST        = @STDCOST,
           BUYDATE        = @BUYDATE,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4,
           TSTAMP         = @TSTAMP
     WHERE CORRNBR   = @CORRNBR
       AND VENDIDSS  = @VENDIDSS
       AND INVTIDSKU = @INVTIDSKU

    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_UpdProcTMP_IN_VENDORINVTREL 
 */



/* 
 * 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_UpdProcTYPEPAINMENT 
 */

CREATE PROCEDURE SP_UpdProcTYPEPAINMENT
(
    @TYPEPAINMENTID     char(2),
    @DESCR              char(40),
    @TSTAMP             timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TYPEPAINMENT
       SET DESCR               = @DESCR
     WHERE TYPEPAINMENTID = @TYPEPAINMENTID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: TEMP_IN_INVENTORYALTERInsProc 
 */

CREATE PROCEDURE TEMP_IN_INVENTORYALTERInsProc
(
    @CORRNBR        INT,
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO)

AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_INVENTORYALTER(CORRNBR,INVTIDBARR,INVTIDSKU)
    VALUES(@CORRNBR,
           @INVTIDBARR,
           @INVTIDSKU)

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: TEMP_IN_INVENTORYDATReplicaProc 
 */

CREATE PROCEDURE TEMP_IN_INVENTORYDATReplicaProc
(
   @CORRNBR            int,     
   @INVTIDSKU          UD_IDCORPORATIVO,
   @SITEID             UD_IDSITE,
   @STOCKMIN           UD_STOCK,
   @STOCKMAX           UD_STOCK,
   @INVTIDSTK          char(20),
   @INVTIDNOSTK        char(20))

AS
BEGIN
    BEGIN TRAN

    if exists(select * from TMP_IN_INVENTORYDAT where CORRNBR=@CORRNBR and 
              INVTIDSKU=@INVTIDSKU and SITEID=@SITEID) 
       UPDATE TMP_IN_INVENTORYDAT SET  STOCKMIN=@STOCKMIN,
                                       STOCKMAX=@STOCKMAX,
                                       INVTIDSTK=@INVTIDSTK,
                                       INVTIDNOSTK=@INVTIDNOSTK                                           
       WHERE CORRNBR=@CORRNBR and 
             INVTIDSKU=@INVTIDSKU and SITEID=@SITEID       

    else      
       /*Si el registro no está en el Temporal entonces se Inserta*/
       INSERT INTO TMP_IN_INVENTORYDAT(CORRNBR,INVTIDSKU,SITEID,STOCKMIN,STOCKMAX,STARTDATEINV,
                                       INVTIDSTK,INVTIDNOSTK)
                   
       VALUES(@CORRNBR,
              @INVTIDSKU,
              @SITEID,
              @STOCKMIN,
              @STOCKMAX,GETDATE(),@INVTIDSTK,@INVTIDNOSTK)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'TEMP_IN_INVENTORYDATReplicaProc: Cannot insert because primary key value not found in IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END
    COMMIT TRAN
END
/*************************************************************************************************************************/
go
IF OBJECT_ID('TEMP_IN_INVENTORYDATReplicaProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE TEMP_IN_INVENTORYDATReplicaProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE TEMP_IN_INVENTORYDATReplicaProc >>>'
go


/* 
 * PROCEDURE: TEMP_IN_INVENTORYDATUpdProc 
 */

CREATE PROCEDURE TEMP_IN_INVENTORYDATUpdProc
(
    @CORRNBR            int,     
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @PRECLVID		UD_PRECLVID,
    @SLS_PRICE          UD_COSTO,
    @INVTIDSTK          char(20),
    @INVTIDNOSTK        char(20))
    
AS
BEGIN
    BEGIN TRAN

       UPDATE TMP_IN_INVENTORYDAT SET  STOCKMIN=@STOCKMIN,
                                       STOCKMAX=@STOCKMAX,
			               PRECLVID=@PRECLVID,
  				       SLS_PRICE=@SLS_PRICE,
                                       INVTIDSTK=@INVTIDSTK,
                                       INVTIDNOSTK=@INVTIDNOSTK 
	
       WHERE CORRNBR=@CORRNBR and 
             INVTIDSKU=@INVTIDSKU and SITEID=@SITEID       

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

/*************************************************************************************************************************/
go
IF OBJECT_ID('TEMP_IN_INVENTORYDATUpdProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE TEMP_IN_INVENTORYDATUpdProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE TEMP_IN_INVENTORYDATUpdProc >>>'
go


/* 
 * PROCEDURE: TEMP_IN_VENDORINVTnsProc 
 */

CREATE PROCEDURE TEMP_IN_VENDORINVTnsProc
(
    @CORRNBR        INT,
    @VENDIDSS       UD_IDVENDOR,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SITEID         UD_IDSITE,
    @BUYCOD         VARCHAR(20),
    @DESCR          UD_DESCRIPCION,
    @CURYID         UD_CURYID,
    @STDCOST        UD_COSTO )

AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_IN_VENDORINVT(CORRNBR,VENDIDSS,INVTIDSKU,SITEID,BUYCOD,DESCR,CURYID,STDCOST)
           VALUES(@CORRNBR,@VENDIDSS,@INVTIDSKU,@SITEID,@BUYCOD,@DESCR,@CURYID,@STDCOST)

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

    COMMIT TRAN

END

/***************************************************************************************************************************/
go
IF OBJECT_ID('TEMP_IN_VENDORINVTnsProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE TEMP_IN_VENDORINVTnsProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE TEMP_IN_VENDORINVTnsProc >>>'
go


/* 
 * PROCEDURE: TEMP_IN_VENDORINVTRELInsProc 
 */

CREATE PROCEDURE TEMP_IN_VENDORINVTRELInsProc
(
    @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 'Artículo ya fue registrado'
        ROLLBACK TRAN
        RETURN(1)    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: TEMP_IN_VENDORINVTUpdProc 
 */

CREATE PROCEDURE TEMP_IN_VENDORINVTUpdProc
(
    @CORRNBR        INT,
    @VENDIDSS       UD_IDVENDOR,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SITEID         UD_IDSITE,
    @BUYCOD         VARCHAR(20),
    @DESCR          UD_DESCRIPCION,
    @CURYID         UD_CURYID,
    @STDCOST        UD_COSTO )

AS
BEGIN
    BEGIN TRAN

    UPDATE TMP_IN_VENDORINVT SET BUYCOD=@BUYCOD,DESCR=@DESCR,CURYID=@CURYID,STDCOST=@STDCOST
           WHERE CORRNBR=@CORRNBR AND VENDIDSS=@VENDIDSS AND INVTIDSKU=@INVTIDSKU
                 AND SITEID=@SITEID 

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'TEMP_IN_VENDORINVTUpdProc: Cannot update in TMP_IN_VENDORINVT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: Tmp_In_IntraItemDelProc 
 */

CREATE PROCEDURE Tmp_In_IntraItemDelProc
(   @INVTIDSKU UD_IDCORPORATIVO,
    @CORRNBR INT)

AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM  TMP_IN_INTRAITEM
      WHERE INVTIDSKU=@INVTIDSKU AND
	    CORRNBR=@CORRNBR


    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'Error al Eliminar'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: Tmp_In_IntraItemDelTodoProc 
 */

CREATE PROCEDURE Tmp_In_IntraItemDelTodoProc
(
  @CORRNBR  INT)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM Tmp_In_IntraItem
     WHERE CORRNBR=@CORRNBR

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: TMP_IN_INTRAITEMInsProc 
 */

CREATE PROCEDURE TMP_IN_INTRAITEMInsProc
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,

    @TYPEDOCID         UD_TYPEMOVEID,
    @SEQUENCE          smallint,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @INVTIDBARR        UD_CODIGOBARRA,
    @QTY               UD_STOCK,
    @TAXIGV            numeric(5, 2)               = NULL,
    @STDCOSTPEN        UD_COSTO                    = NULL,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STKFISI           bit,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE    smalldatetime,
    @RLSED             bit,
    @TAXISC	      NUMERIC(5,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 'TMP_IN_INTRAITEMInsProc: Cannot insert because primary key value not found in IN_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: TMP_IN_INTRAITEMSelProc 
 */

CREATE PROCEDURE TMP_IN_INTRAITEMSelProc
(   @VENDIDSS UD_IDVENDOR,
    @CORRNBR  INT,
    @CURYID     UD_CURYID)

AS
BEGIN
      DECLARE @MONEDABASE CHAR(3)
      SET @MONEDABASE =(SELECT CURYORIG FROM SETUP) 

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint
	DECLARE @ND_RESULTADOSETUP Tinyint 

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)
	SET @ND_RESULTADOSETUP=(select Floatoutput from setup)	
/*******************************************************************************************************/

      	SELECT	 [SEQUENCE] AS Item,
           		       	TMP_IN_INTRAITEM.INVTIDSKU AS Codigo,
	       		IN_VENDORINVT.BUYCOD AS Cod_Compra, 
                    		IN_INVENTORY.DESCR AS Descripcion,
                    		QTY as Cantidad,
                    		CASE @CURYID
				WHEN @MONEDABASE THEN STDCOSTPEN
				ELSE STDCOSTUSD
				END as Costo,
                    		CASE @CURYID
				WHEN @MONEDABASE THEN ROUND(QTY*STDCOSTPEN,@ND_RESULTADOSETUP)
				ELSE ROUND(QTY*STDCOSTUSD,@ND_RESULTADOSETUP)
				END as Total,
			TMP_IN_INTRAITEM.TAXIGV AS IGV,TMP_IN_INTRAITEM.TAXISC AS ISC
	FROM 		TMP_IN_INTRAITEM,IN_INVENTORY,IN_VENDORINVT
	WHERE 	TMP_IN_INTRAITEM.INVTIDSKU = IN_INVENTORY.INVTIDSKU AND
	   	 	CORRNBR=@CORRNBR AND TMP_IN_INTRAITEM.INVTIDSKU=IN_VENDORINVT.INVTIDSKU
	  		 AND VENDIDSS=@VENDIDSS

      	ORDER BY	SEQUENCE ASC

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


/* 
 * PROCEDURE: TMP_IN_INTRAITEMUpdProc 
 */

CREATE PROCEDURE TMP_IN_INTRAITEMUpdProc
(
    @INVTIDSKU         	UD_IDCORPORATIVO,
    @SEQUENCE		SMALLINT,
    @QTY              	UD_STOCK,
    @COSTO        	UD_COSTO                    = NULL,
    @CORRNBR           	int,
    @CURYID		UD_CURYID)
AS
BEGIN
    BEGIN TRAN
     
      DECLARE @MONEDABASE CHAR(3)
      SET @MONEDABASE =(SELECT CURYORIG FROM SETUP)


    UPDATE TMP_IN_INTRAITEM
    SET 	SEQUENCE = @SEQUENCE,
           	QTY  = @QTY,
		STDCOSTPEN= CASE @CURYID
				WHEN @MONEDABASE THEN @COSTO
				ELSE STDCOSTPEN
				END,
		STDCOSTUSD= CASE @CURYID
				WHEN @MONEDABASE THEN STDCOSTUSD
				ELSE @COSTO
				END
	
    WHERE INVTIDSKU=@INVTIDSKU AND CORRNBR=@CORRNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20001 'No se puede actualizar la tabla TMP_IN_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: TMP_IN_KitComponentDelProc 
 */

CREATE PROCEDURE TMP_IN_KitComponentDelProc
(   @INVTIDSKU UD_IDCORPORATIVO,
    @CORRNBR INT)

AS
BEGIN
    BEGIN TRAN

    	DELETE
    	FROM  	TMP_IN_KITCOMPONENT
	WHERE 	INVTIDSKU=@INVTIDSKU AND
	    	CORRNBR=@CORRNBR


    IF (@@error!=0)
    BEGIN
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: TMP_IN_KitComponentDelTodoProc 
 */

CREATE PROCEDURE TMP_IN_KitComponentDelTodoProc
(
  @CORRNBR  INT)
AS
BEGIN
    BEGIN TRAN

	DELETE
      	FROM 	TMP_IN_KITCOMPONENT
     	WHERE 	CORRNBR=@CORRNBR

    IF (@@error!=0)
    BEGIN
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: TMP_IN_KitComponentInsProc 
 */

CREATE PROCEDURE TMP_IN_KitComponentInsProc
(   @CORRNBR	    INT,
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: TMP_IN_KitComponentSelProc 
 */

CREATE PROCEDURE TMP_IN_KitComponentSelProc
( @KITID	UD_KITID,
  @CORRNBR	INT
)
AS
BEGIN


/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint
	DECLARE @ND_RESULTADOSETUP Tinyint 

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)
	SET @ND_RESULTADOSETUP=(select Floatoutput from setup)	
/*******************************************************************************************************/


	SELECT 	TMP_IN_KITCOMPONENT.SEQUENCE AS Registro,TMP_IN_KITCOMPONENT.INVTIDSKU AS Codigo,IN_INVENTORY.DESCR AS Descripcion,
			IN_INVENTORY.STKUNITID AS Unidad,TMP_IN_KITCOMPONENT.QTY AS Cantidad,TMP_IN_KITCOMPONENT.STDCOSTPEN AS Costo_Soles,
			TMP_IN_KITCOMPONENT.STDCOSTUSD AS Costo_Dolares,ROUND((TMP_IN_KITCOMPONENT.QTY*TMP_IN_KITCOMPONENT.STDCOSTPEN),@ND_RESULTADOSETUP) as Total_Soles,
			ROUND((TMP_IN_KITCOMPONENT.QTY*TMP_IN_KITCOMPONENT.STDCOSTUSD),@ND_RESULTADOSETUP) as Total_Dolares
	FROM 		TMP_IN_KITCOMPONENT JOIN IN_INVENTORY
	ON		TMP_IN_KITCOMPONENT.INVTIDSKU=IN_INVENTORY.INVTIDSKU
	WHERE	TMP_IN_KITCOMPONENT.KITID=@KITID AND TMP_IN_KITCOMPONENT.CORRNBR=@CORRNBR
	ORDER BY	TMP_IN_KITCOMPONENT.SEQUENCE ASC

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


/* 
 * PROCEDURE: TMP_IN_KitComponentUpdProc 
 */

CREATE PROCEDURE TMP_IN_KitComponentUpdProc
(
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @QTY            numeric(9, 3),
    @CORRNBR	    INT
)
AS
BEGIN
    BEGIN TRAN

    	UPDATE 	TMP_IN_KITCOMPONENT
       	SET 	SEQUENCE= @SEQUENCE,
           	QTY= @QTY
     	WHERE 	INVTIDSKU = @INVTIDSKU AND CORRNBR=@CORRNBR

    IF (@@error!=0)
    BEGIN
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: valorinventariofisico_proc 
 */


create proc valorinventariofisico_proc
@tipomoneda  	UD_CURYID,		--codigo de moneda a mostrar
@SITE 		varCHAR(8000),		--almacenes que se desean mostrar
@TCONDICION 	CHAR(3),		--tipo de condicion, CAT categoria SCA subcategoria, TIp tipo, ART articulo
@todas 		integer,		--si de desean ver todos los elementos del tipo de condicion 
@condicion 	varchar(8000)='', 	--condicion si solo se desean ver algunos item
@costo		char(3)			--(pro)medio o (rep)osicion
as
DECLARE @CONSULTA	VARCHAR(8000)	--CONSULTA QUE SE VA A EJECUTAR
DECLARE @MONEDAORIG	UD_CURYID	--MONEDA ORIGINAL

SET @MONEDAORIG=(SELECT CURYORIG FROM SETUP)

if @COSTO='PRO'				--COSTO PROMEDIO
	IF @MONEDAORIG=@TIPOMONEDA	-- COSTO PROMEDIO MONEDA NACIONAL
		SET @CONSULTA =	'SELECT VI.*, avgcostpen as costo,ROUND(avgcostpen*stockphisical,2) as stotal' + 
				' FROM vw_valorinventariofisico VI  where siteid in ('+@site +') '
	ELSE		-- COSTO PROMEDIO MONEDA ENTRANJERA
		SET @CONSULTA =	'SELECT VI.*, avgcostUSD as costo,ROUND(avgcostUSD*stockphisical,2) as stotal' + 
				' FROM vw_valorinventariofisico VI  where siteid in ('+@site +') '

ELSE					-- COSTO REPOSICION
	IF @MONEDAORIG=@TIPOMONEDA	-- COSTO REPOSICION MONEDA NACIONAL
		SET @CONSULTA =	'SELECT VI.*, STDcostpen as costo,ROUND(STDcostpen*stockphisical,2) as stotal' + 
				' FROM vw_valorinventariofisico VI where siteid in ('+@site +') '
	ELSE		-- COSTO REPOSICION MONEDA EXTRANEJRA
		SET @CONSULTA =	'SELECT VI.*, STDcostUSD as costo,ROUND(STDcostUSD*stockphisical,2) as stotal' + 
				' FROM vw_valorinventariofisico VI where siteid in ('+@site +') '	

if @TCONDICION="CAT"	--POR CATEGORIA
	IF @TODAS=1	--SIN CONDICION
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by siteid,classid,NARTICULO'
		END	
	ELSE
		BEGIN	
			SET @CONSULTA =@CONSULTA +' and classid IN ('+@condicion+') '+
						  ' order by siteid,classid,narticulo'
		END

	if @TCONDICION="SCA"	--POR SUB CATEGORIA
		IF @TODAS=1
		BEGIN
			SET @CONSULTA =@CONSULTA +' order by siteid,classid,classubid,narticulo'				
		END	
	ELSE
		BEGIN
			SET @CONSULTA =@CONSULTA +' AND CLASSUBID IN (' +@CONDICION+')'+
						  ' order by siteid,classid,classubid,narticulo'				
		END	
EXEC (@CONSULTA)
go
IF OBJECT_ID('valorinventariofisico_proc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE valorinventariofisico_proc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE valorinventariofisico_proc >>>'
go


/* 
 * PROCEDURE: IN_AJUSTEAUTOMATICO 
 */


CREATE PROC IN_AJUSTEAUTOMATICO
@USUARIO UD_USERID	--ID DE USUARIO
AS
declare @tmoving ud_typedoc	--tipo movimiento de ingreso
declare @tmovsal ud_typedoc	--tipo movimiento de salidas
declare @NDOCT_ing int	--NUMERO CORRELATIVO TEMPORAL ingreso
declare @NDOCT_sal int		--NUMERO CORRELATIVO TEMPORAL SALIDA
declare @NDOC_ing char(10)		--NUMERO DOCUMENTO INGRESO
declare @NDOC_sal char(10)		--NUMERO DOCUMENTO SALIDA
DECLARE @ALMACEN UD_IDSITE
DECLARE @PROVEEDOR UD_IDVENDOR		--CODIGO PROVEEDOR
DECLARE @MONEDA_ORI UD_CURYID		--CODIGO MONEDA
DECLARE @IMPUESTO NUMERIC(4,2)		--CODIGO MONEDA
DECLARE @FECHAPROCESO SMALLDATETIME 	
DECLARE @inicioinventario SMALLDATETIME 
DECLARE @SEC_INGRESO INTEGER		--SECUENCE INGRESO
DECLARE @SEC_SALIDAS INTEGER		--SECUENCE SALIDA
DECLARE @PERIODO_PROCESO UD_PERPOST	--PERIODO DE PROCESO
DECLARE @DOCTYPE 	UD_DOCTYPEID	--PERIODO DE PROCESO
declare @ref char(10)
declare @tipo_cambio ud_cambio
declare @ttcambio CHAR(6)

declare @error integer
declare @sec integer			--NUMERO DE SEQUENCIA


------------------------------------------------------------------------------------------
--variables para uso del cursor
DECLARE  @CORRNBR           int
DECLARE  @SITEID            UD_IDSITE
DECLARE  @TYPEDOCID         UD_TYPEMOVEID
DECLARE  @SEQUENCE          smallint
DECLARE  @INVTIDSKU         UD_IDCORPORATIVO
DECLARE  @INVTIDBARR        UD_CODIGOBARRA
DECLARE  @QTY               UD_STOCK
DECLARE  @TAXIGV            numeric(4, 2)          
DECLARE  @STDCOSTPEN        UD_COSTO
DECLARE  @STDCOSTUSD        UD_COSTO
DECLARE  @STKFISI           bit
DECLARE  @INTRDATE          smalldatetime
DECLARE  @INTRDATEPROCE     smalldatetimE
DECLARE  @rlsed		    bit

------------------------------------------------------------------------------------------

--OBTENER CORRELATIVO INTERNO
EXEC ObtenerCorrelativoProcUtil @USUARIO, @NDOCT_ing output

EXEC ObtenerCorrelativoProcUtil @USUARIO, @NDOCT_SAL output

---------------------------------------------
--OBTENER LOS TIPOS DE MOVIMIENTOS PARA AJUSTE
SELECT @DOCTYPE= DOCTYPEIDFIS ,@PERIODO_PROCESO=PERPOST,@tmoving=INTYPEFISIN,@tmovsal=INTYPEFISOUT FROM IN_SETUP
set @ref=(select cast(year(DATEPROCE) as char(4))+'-'+cast(month(DATEPROCE) as char(2))+'-'+cast(day(DATEPROCE) as char(2)) from Setup)

--PROVEEDOR 0000
SET @PROVEEDOR='00000000000'

--OBTENER SITE POR DEFECTO
--OBTENER VALOR DEL IMPUESTP
---OBTENER MONEDA ORIGINAL
SELECT @TTCAMBIO=SOURCECURYTYPEID,@MONEDA_ORI=CURYORIG, @IMPUESTO=TAXVALUE,@ALMACEN =SOURCESITE,@FECHAPROCESO=DATEPROCE FROM SETUP

---------------------------------------------
--OBTENER La fehca de inventario
select @inicioinventario=(select startdateinven-1 from site where siteid=@almacen) --OBTENER fecha de inicio de inventario


------------------------------------------------------------------------------------
SET @TIPO_CAMBIO=(SELECT RATE FROM CURYRATE WHERE CURYTYPEID=@TTCAMBIO AND FROMCURYID=@MONEDA_ORI AND CURYDATE=(SELECT MAX(CURYDATE) FROM CURYRATE))

SET @SEC_INGRESO =1
SET @SEC_SALIDAS =1

------------------------------------------------------------------------------------
--llenar tmp_in_intraitem

 DECLARE tempo cursor FOR SELECT 
	CASE WHEN STOCKPHISICAL<QTYAVAIL THEN  @NDOCT_ING				--CORRENBR
	     WHEN STOCKPHISICAL>QTYAVAIL THEN  @NDOCT_SAL END AS CORRNBR,
	@ALMACEN AS SITEID,								--ALMACEN
	CASE WHEN STOCKPHISICAL<QTYAVAIL THEN  @tmoving					--TYPEDOCID
	     WHEN STOCKPHISICAL>QTYAVAIL THEN  @tmovSAL END as typedocid,
	0 AS SECUENCE,									--secuence
	INVTIDSKU ,									--INVTIDSKU
	NULL AS INVTIDBARR,								--CODIG DE BARRA	
	CASE WHEN STOCKPHISICAL<QTYAVAIL THEN  QTYAVAIL-STOCKPHISICAL   		--QTY
	     WHEN STOCKPHISICAL>QTYAVAIL THEN  STOCKPHISICAL-QTYAVAIL END  AS QTY, 
	NULL AS TAXIGV,									--TAXIGV
	0 AS STDCOSTPEN,								--STDCOSTPEN
	0 AS STDCOSTUSD ,								--STDCOSTUSD
	1 AS STKFISI,									--STKFISI
	@FECHAPROCESO AS INTRDATE,							--fechadocumento
	@FECHAPROCESO AS INTRDATEPROC,							--fechaproceso
	0 AS RLSED								--emitido
 FROM VW_AJUSTEAUTOMATICO

    OPEN tempo
    FETCH NEXT FROM tempo into	@CORRNBR,@SITEID,@TYPEDOCID,@SEQUENCE,@INVTIDSKU,@INVTIDBARR,@QTY,@TAXIGV,
				@STDCOSTPEN,@STDCOSTUSD,@STKFISI,@INTRDATE,@INTRDATEPROCE,@RLSED

      WHILE @@FETCH_STATUS=0
        BEGIN    

	if @CORRNBR=@NDOCT_ING  set @sec=@SEC_INGRESO 
	if @CORRNBR=@NDOCT_sal set @sec=@SEC_salidas 

------------------------------------------------------------------------------------
--grabar movimiento EN TABLA TEMPORAL

	 EXEC SP_InsProcTMP_IN_INTRAITEM 	@CORRNBR,@SITEID,@TYPEDOCID,@sec,@INVTIDSKU,@INVTIDBARR,
						@QTY,@TAXIGV,@STDCOSTPEN,@STDCOSTUSD,@STKFISI,@INTRDATE,
						@INTRDATEPROCE,@RLSED
------------------------------------------------------------------------------------

	if @CORRNBR=@NDOCT_ING	 SET @SEC_INGRESO =@SEC_INGRESO+1
	if @CORRNBR=@NDOCT_sal	 sET @SEC_SALIDAS =@SEC_SALIDAS+1
	
          SET @ERROR=@@error
     
          IF (@ERROR!=0)
          BEGIN       
            CLOSE TEMPO
            DEALLOCATE TEMPO
            RETURN(1)  
          END         
	
	    FETCH NEXT FROM tempo  into	@CORRNBR,@SITEID,@TYPEDOCID,@SEQUENCE,@INVTIDSKU,@INVTIDBARR,@QTY,@TAXIGV,
					@STDCOSTPEN,@STDCOSTUSD,@STKFISI,@INTRDATE,@INTRDATEPROCE,@RLSED
end

CLOSE TEMPO
DEALLOCATE TEMPO

------------------------------------------------------------------------------------
--grabar movimiento ingreso
if @sec_ingreso>1	--si hay alguna linea grabar ingreso
begin 

	exec IN_GrabarMovimientoProc 
		@almacen,
		@tmoving,         
		@proveedor,
		@DOCTYPE ,
		'OK',
		@ref,
		@TIPO_CAMBIO,
		@MONEDA_ORI,
		@MONEDA_ORI,
		@ALMACEN,
		null, 
		null,
		null,
		@FECHAPROCESO,
		@inicioinventario,
		@ALMACEN,
		0,
		0,
		0,
		0,
		@PERIODO_PROCESO,
		0,
		0,
		NULL, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
		@NDOCT_ING,  
		@NDOC_ing output    

		exec IN_EmitirProc @almacen,@ndoc_ing --EMITIRr movimiento INGRESO
end
------------------------------------------------------------------------------------
--grabar movimiento salida

if @sec_Salidas>1		--si hay alguna linea de salida grabar salida 
begin
	exec IN_GrabarMovimientoProc 
		@almacen,
	    	@tmovsal,         
		@proveedor,
	    	@DOCTYPE ,
		'OK',
		@ref,
	    	@TIPO_CAMBIO,
		@MONEDA_ORI,
		@MONEDA_ORI,
		@ALMACEN,
		null  , 
		null,
		null,
		@FECHAPROCESO,
		@inicioinventario,
		@ALMACEN,
		0,
		0,
		0,
		0,
		@PERIODO_PROCESO,
		0,
		0,
		NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
		@NDOCT_sal,  
		@NDOC_sal output  

		exec IN_EmitirProc @almacen,@ndoc_sal 		--EMITIRr movimiento SALIDA

end

------------------------------------------------------------------------------------
--actualizando flag de ajuste
UPDATE  SITE SET ADJUST=1 WHERE SITEID=@ALMACEN

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


/* 
 * PROCEDURE: IN_INVENTORYInsProc 
 */

CREATE PROCEDURE IN_INVENTORYInsProc
(
    @INVTIDSKU       UD_IDCORPORATIVO, 
    @DESCR           varchar(40),
    @WTSTK1          numeric(7, 3),
    @STKFUELS        bit,
    @STKSTORE        bit,
    @STKOTHER        bit, 
    @STKITEM         bit,   
    @KIT             bit,
    @STKUNITID       UD_STKUNITID,
    @CLASSID         char(5),        
    @CLASSUBID       char(5), 
    @INVTYPEID       char(5),    
    @TAXIGV          UD_TAXID,
    @TAXISC          UD_TAXID                    = NULL,
    @DISCDSCTO1      UD_PORCENTAJE,
    @DISCDSCTO2      UD_PORCENTAJE,
    @DISCMTO         numeric(12, 2),
    @STKRECEPT       bit,
    @STKSALES        bit, 
    @STATUS          bit,
    @CONSIGNMENT     bit,
    @DISASSEMBLE     bit,
    @CORRNBR         INT,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint)
AS
BEGIN
   DECLARE @ERROR Integer
   DECLARE @MONEDABASE UD_CURYID  
   SELECT @ERROR=0   

   if RTRIM(LTRIM(@TAXISC))=''
      set @TAXISC=null   
    
   BEGIN TRAN
   

   INSERT INTO IN_INVENTORY(INVTIDSKU,DESCR,WTSTK1,STKFUELS,STKSTORE,STKOTHER,STKITEM,KIT,STKUNITID,
               CLASSID,CLASSUBID,INVTYPEID,TAXIGV,TAXISC,DISCDSCTO1,DISCDSCTO2,DISCMTO,STKRECEPT,STKSALES, 
               STATUS,CONSIGNMENT,DISASSEMBLE)

   VALUES  (@INVTIDSKU,@DESCR,@WTSTK1,@STKFUELS,@STKSTORE,@STKOTHER,@STKITEM,@KIT,@STKUNITID,
             @CLASSID,@CLASSUBID,@INVTYPEID,@TAXIGV,@TAXISC,@DISCDSCTO1,@DISCDSCTO2,@DISCMTO,@STKRECEPT,@STKSALES, 
             @STATUS,@CONSIGNMENT,@DISASSEMBLE)

    IF (@@error!=0)
    BEGIN
        RAISERROR 20000 'Código de Artículo ya Existe'
        ROLLBACK TRAN
        RETURN(1)    
    END


    /*Borrar los datos de la tabla InventoryAlter */

    set exec @Error=IN_INVENTORYALTERDelProc @INVTIDSKU 
   
    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END
   
 
    /*Recorrer el Temporal de IventoryAlter y pasarlo a la Tabla Inventory Alter */  
   
    set exec @Error=IN_CargaTempDataInventoryAlterProc @CORRNBR
    
    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END

    
    /*Grabar en la Tabla Inventory_Des si el Flag de Desembsamble =1 */
    if @DISASSEMBLE=1 
       begin  
         set exec @Error=IN_INVENTORYDESInsProc @INVTIDSKU,@INVTIDSKUDES,@WTSTK2     
         IF (@ERROR!=0)
            BEGIN
              ROLLBACK TRAN
              RETURN(1)               
            END
        end
 
    /*Borrar la Tabla Inventorydat (Recibe Código de Artículo + Código de Almacen)*/    
                          
    set EXEC @ERROR=IN_INVENTORYDATDelProc @INVTIDSKU

    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END               

    /*Recorrer el Temporal pasando el identificador/correlativo para Grabar en InventoryDat*/
           
    set EXEC @ERROR=IN_CargaTempDataInventoryProc @CORRNBR                                                  
         
    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END 
        
    /*Generar en Inventorydat los almacenes que están con Status=1 y no están en InventoryDat  */
     
    SET EXEC @ERROR=IN_APERTURAAlMACENESProc @INVTIDSKU  
    if (@ERROR!=0)
    BEGIN 
      ROLLBACK TRAN
      RETURN(1)  
    END
     
    /*Borrar los datos de la tabla IN_VENDORINVT */
    set exec @Error=IN_VENDORINVTDelProc @INVTIDSKU
    
    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END     
    
    /*Recorrer el Temporal de IN_VENDORINVT y pasarlo a la Tabla IN_VENDORINVT*/

    set exec @Error=IN_CargaTempDataIN_VENDORINVTProc @CORRNBR          
     
    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END    
         
    /*Grabar en la Tabla IN_KIT*/

    IF @KIT=1
      BEGIN    
       select @MONEDABASE=CURYORIG from SETUP
       insert into IN_KIT(KITID,STKUNITID,CURYID,TAXID,DESCR) VALUES
                   (@INVTIDSKU,@STKUNITID,@MONEDABASE,@TAXIGV,@DESCR)

        IF (@@ERROR!=0)
          BEGIN
           RAISERROR  20000 'No se puede Grabar Artículo como Fórmula' 
           ROLLBACK TRAN
           RETURN(1)
         END
      END   

    /*Grabar en IN_KDXHIST el artículo por cada almacen con status= 1 */                
            
    SET EXEC @ERROR=IN_APERTURAKDXHISTProc @INVTIDSKU     
    if (@ERROR!=0)
    BEGIN
      ROLLBACK TRAN
      RETURN(1)  
    END
                                     
    COMMIT TRAN

    set exec @Error=IN_LIMPIARTEMPORALES @CORRNBR
    if (@ERROR!=0)
      BEGIN
       RETURN(1)               
     END    

END

/*************************************************************************************************************************/
go
IF OBJECT_ID('IN_INVENTORYInsProc') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE IN_INVENTORYInsProc >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE IN_INVENTORYInsProc >>>'
go


/* 
 * PROCEDURE: IN_INVENTORYUpdProc 
 */

CREATE PROCEDURE IN_INVENTORYUpdProc
(
    @INVTIDSKU       UD_IDCORPORATIVO,
    @DESCR           varchar(40),
    @WTSTK1          numeric(7, 3),
    @STKFUELS        bit,
    @STKSTORE        bit,
    @STKOTHER        bit, 
    @STKITEM         bit,   
    @KIT             bit,
    @STKUNITID       UD_STKUNITID,
    @CLASSID         char(5),        
    @CLASSUBID       char(5), 
    @INVTYPEID       char(5),    
    @TAXIGV          UD_TAXID,
    @TAXISC          UD_TAXID                    = NULL,
    @DISCDSCTO1      UD_PORCENTAJE,
    @DISCDSCTO2      UD_PORCENTAJE,
    @DISCMTO         numeric(12, 2),
    @STKRECEPT       bit,
    @STKSALES        bit, 
    @STATUS          bit,
    @CONSIGNMENT     bit,
    @DISASSEMBLE     bit,
    @CORRNBR         INT,
    @INVTIDSKUDES    UD_IDCORPORATIVO,
    @WTSTK2          tinyint)
AS
BEGIN
   DECLARE @ERROR Integer
   DECLARE @MONEDABASE UD_CURYID  
   SELECT @ERROR=0   
   if RTRIM(LTRIM(@TAXISC))=''
      set @TAXISC=null 

   BEGIN TRAN

   UPDATE IN_INVENTORY
       SET DESCR=@DESCR,WTSTK1=@WTSTK1,STKFUELS=@STKFUELS,STKSTORE=@STKSTORE,STKOTHER=@STKOTHER, 
           STKITEM=@STKITEM,KIT=@KIT,STKUNITID=@STKUNITID,CLASSID=@CLASSID,CLASSUBID=@CLASSUBID,
           INVTYPEID=@INVTYPEID,TAXIGV=@TAXIGV,TAXISC=@TAXISC,DISCDSCTO1=@DISCDSCTO1,
           DISCDSCTO2=@DISCDSCTO2,DISCMTO=@DISCMTO,STKRECEPT=@STKRECEPT,STKSALES=@STKSALES,
           STATUS=@STATUS,CONSIGNMENT=@CONSIGNMENT,DISASSEMBLE=@DISASSEMBLE
   WHERE INVTIDSKU = @INVTIDSKU

   IF (@@error!=0) or (@@rowcount=0)
   BEGIN
        RAISERROR  20001 'No se puede Actualizar el Maestro de Artículos'
        ROLLBACK TRAN
        RETURN(1)
    
   END
   
   /*Borrar los datos de la tabla InventoryAlter */

    set exec @Error=IN_INVENTORYALTERDelProc @INVTIDSKU 
   
    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END
   
 
   /*Recorrer el Temporal de IventoryAlter y pasarlo a la Tabla Inventory Alter */  
   
    set exec @Error=IN_CargaTempDataInventoryAlterProc @CORRNBR
    
    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END

   /*Tratamiento de la Tabla InventoryDes */   
    
    if @DISASSEMBLE=0   
      begin  
        set exec @Error=IN_INVENTORYDESDelProc @INVTIDSKU   
        IF (@ERROR!=0)
          BEGIN
           ROLLBACK TRAN
           RETURN(1)               
          END      
      end 
    else        
      begin 
        /*Si existe Actualizo*/
        if exists (select * from in_inventorydes where INVTIDSKU=@INVTIDSKU)        
         begin
           set exec @Error=IN_INVENTORYDESUpdProc @INVTIDSKU,@INVTIDSKUDES,@WTSTK2     
           IF (@ERROR!=0)
              BEGIN
              ROLLBACK TRAN
              RETURN(1)               
           END          
         end
        else
         begin
           set exec @Error=IN_INVENTORYDESInsProc @INVTIDSKU,@INVTIDSKUDES,@WTSTK2    
           IF (@ERROR!=0)
               BEGIN
               ROLLBACK TRAN
               RETURN(1)               
           END
         end 
      end
   /*Borrar los datos de la Tabla InventoryDat */ 
      
      set EXEC @ERROR=IN_INVENTORYDATDelProc @INVTIDSKU

      IF (@ERROR!=0)
      BEGIN
        ROLLBACK TRAN
        RETURN(1)               
      END   
         
   /*Recorrer el Temporal de IventoryDat y pasarlo a la Tabla InventoryDat */       
     set EXEC @ERROR=IN_CargaTempDataInventoryProc @CORRNBR                                                  
          
      IF (@ERROR!=0)
      BEGIN
        ROLLBACK TRAN
        RETURN(1)               
      END
   
   /*Borrar los datos de la tabla IN_VENDORINVT */
     set exec @Error=IN_VENDORINVTDelProc @INVTIDSKU
    
     IF (@ERROR!=0)
     BEGIN
       ROLLBACK TRAN
       RETURN(1)               
     END     
    
   /*Recorrer el Temporal de IN_VENDORINVT y pasarlo a la Tabla IN_VENDORINVT*/

     set exec @Error=IN_CargaTempDataIN_VENDORINVTProc @CORRNBR          
     
     IF (@ERROR!=0)
     BEGIN
        ROLLBACK TRAN
        RETURN(1)               
     END    
    
   /*Grabar o Eliminar en la Tabla IN_KIT*/

     IF @KIT=0
      BEGIN
        --Verificar si estaba grabado en IN_KITCOMPONENT
        Delete from IN_KIT where KITID=@INVTIDSKU        
        IF (@@ERROR!=0)
          BEGIN
           RAISERROR  20002 'No se puede modificar porque Artículo Tiene Componentes relacionados'
           ROLLBACK TRAN
           RETURN(1)
         END
      END
     ELSE
      BEGIN    
         if not exists(select KITID from in_kit where KITID=@INVTIDSKU)
          begin 
            select @MONEDABASE=CURYORIG from SETUP
            insert into IN_KIT(KITID,STKUNITID,CURYID,TAXID,DESCR) VALUES
                              (@INVTIDSKU,@STKUNITID,@MONEDABASE,@TAXIGV,@DESCR)
            IF (@@ERROR!=0)
                BEGIN
                  RAISERROR  20000 'No se puede Grabar Artículo como Fórmula' 
                  ROLLBACK TRAN
                  RETURN(1)
                END
          end
      END

   COMMIT TRAN

   set exec @Error=IN_LIMPIARTEMPORALES @CORRNBR
    if (@ERROR!=0)
      BEGIN
       RETURN(1)               
     END  

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


/* 
 * PROCEDURE: sp_InsProcFACTUDET1 
 */

CREATE PROCEDURE sp_InsProcFACTUDET1
(
    @CDTIPODOCU     nvarchar(3)              = NULL,
    @NRODOCUM       float                    = NULL,
    @NROCOMANDA     float                    = NULL,
    @NROITEM        float                    = NULL,
    @CDARTICULO     nvarchar(20)             = NULL,
    @CDALTERNAT     nvarchar(20)             = NULL,
    @DSARTICULO     nvarchar(45)             = NULL,
    @CANTIDAD       float                    = NULL,
    @CDMONEDA       nvarchar(1)              = NULL,
    @MTOCOSTO       float                    = NULL,
    @MTOPRECIO      float                    = NULL,
    @SUBTOTAL       float                    = NULL,
    @IGV            float                    = NULL,
    @TOTAL          float                    = NULL,
    @PORCIGV        float                    = NULL,
    @PORCSERVIC     float                    = NULL,
    @MTOSERVIC      float                    = NULL,
    @PORCDCTOI1     float                    = NULL,
    @PORCDCTOI2     float                    = NULL,
    @PORCDCTOI3     float                    = NULL,
    @MTODSCTOI      float                    = NULL,
    @FECDOCUM       smalldatetime            = NULL,
    @CANTNCRED      float                    = NULL,
    @TOMAINVFIS     bit                      = NULL,
    @LIQCONSIG      bit,
    @CIERREZ        bit,
    @FECPROCESO     smalldatetime            = NULL,
    @CDPROVEE       nvarchar(8)              = NULL,
    @FLAGCOPIA      bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO FACTUDET(CDTIPODOCU,
                         NRODOCUM,
                         NROCOMANDA,
                         NROITEM,
                         CDARTICULO,
                         CDALTERNAT,
                         DSARTICULO,
                         CANTIDAD,
                         CDMONEDA,
                         MTOCOSTO,
                         MTOPRECIO,
                         SUBTOTAL,
                         IGV,
                         TOTAL,
                         PORCIGV,
                         PORCSERVIC,
                         MTOSERVIC,
                         PORCDCTOI1,
                         PORCDCTOI2,
                         PORCDCTOI3,
                         MTODSCTOI,
                         FECDOCUM,
                         CANTNCRED,
                         TOMAINVFIS,
                         LIQCONSIG,
                         CIERREZ,
                         FECPROCESO,
                         CDPROVEE,
                         FLAGCOPIA)
    VALUES(@CDTIPODOCU,
           @NRODOCUM,
           @NROCOMANDA,
           @NROITEM,
           @CDARTICULO,
           @CDALTERNAT,
           @DSARTICULO,
           @CANTIDAD,
           @CDMONEDA,
           @MTOCOSTO,
           @MTOPRECIO,
           @SUBTOTAL,
           @IGV,
           @TOTAL,
           @PORCIGV,
           @PORCSERVIC,
           @MTOSERVIC,
           @PORCDCTOI1,
           @PORCDCTOI2,
           @PORCDCTOI3,
           @MTODSCTOI,
           @FECDOCUM,
           @CANTNCRED,
           @TOMAINVFIS,
           @LIQCONSIG,
           @CIERREZ,
           @FECPROCESO,
           @CDPROVEE,
           @FLAGCOPIA)

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

    COMMIT TRAN

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





/* 
 * PROCEDURE: sp_InsProcFACTURA1 
 */

CREATE PROCEDURE sp_InsProcFACTURA1
(
    @CDTIPODOCU         nvarchar(3)              = NULL,
    @NRODOCUM           float                    = NULL,
    @NROGUIA            float                    = NULL,
    @NROPROFORM         float                    = NULL,
    @SUBTOTAL           float                    = NULL,
    @IGV                float                    = NULL,
    @TOTAL              float                    = NULL,
    @MTOGTOFIN          float                    = NULL,
    @FECDOCUM           smalldatetime            = NULL,
    @MTOPAGEFECS        float                    = NULL,
    @MTOPAGEFECD        float                    = NULL,
    @MTOPAGTAR1S        float                    = NULL,
    @MTOPAGTAR1D        float                    = NULL,
    @MTOPAGTAR2S        float                    = NULL,
    @MTOPAGTAR2D        float                    = NULL,
    @MTOPAGTAR3S        float                    = NULL,
    @MTOPAGTAR3D        float                    = NULL,
    @MTOPAGTAR4S        float                    = NULL,
    @MTOPAGTAR4D        float                    = NULL,
    @MTOPAGCHEQS        float                    = NULL,
    @MTOPAGCHEQD        float                    = NULL,
    @MTOPAGCREDS        float                    = NULL,
    @MTOPAGCREDD        float                    = NULL,
    @MTOPAGCREDITOS     float                    = NULL,
    @MTOPAGCREDITOD     float                    = NULL,
    @MTOVUELTS          float                    = NULL,
    @MTOVUELTD          float                    = NULL,
    @CDTARJCRE1         nvarchar(5)              = NULL,
    @NROTARJCR1         nvarchar(20)             = NULL,
    @CDTARJCRE2         nvarchar(5)              = NULL,
    @NROTARJCR2         nvarchar(20)             = NULL,
    @CDTARJCRE3         nvarchar(5)              = NULL,
    @NROTARJCR3         nvarchar(20)             = NULL,
    @CDTARJCRE4         nvarchar(5)              = NULL,
    @NROTARJCR4         nvarchar(20)             = NULL,
    @CDBANCO            nvarchar(5)              = NULL,
    @NROCTABANC         nvarchar(20)             = NULL,
    @NROCHQBANC         nvarchar(20)             = NULL,
    @ESTADO             nvarchar(10)             = NULL,
    @CDMONEDA           nvarchar(1)              = NULL,
    @CAMBIO             float                    = NULL,
    @CAMBIOC            float                    = NULL,
    @TIPCREDFAC         nvarchar(5)              = NULL,
    @TIPCREDLET         nvarchar(5)              = NULL,
    @CDCLIENTE          nvarchar(15)             = NULL,
    @NOMCLIENT          nvarchar(50)             = NULL,
    @DIRCLIENT          nvarchar(60)             = NULL,
    @RUCCLIENT          nvarchar(15)             = NULL,
    @CDUSUARIO          nvarchar(8)              = NULL,
    @CDVENDEDOR         nvarchar(15)             = NULL,
    @PORCSERVIC         float                    = NULL,
    @MTOSERVICI         float                    = NULL,
    @MTODSCTO           float                    = NULL,
    @PORCDCTO1          float                    = NULL,
    @PORCDCTO2          float                    = NULL,
    @PORCDCTO3          float                    = NULL,
    @CDALMACEN          nvarchar(2)              = NULL,
    @NROPTOVTA          nvarchar(10)             = NULL,
    @NROORDENC          nvarchar(15)             = NULL,
    @TIPOFACTUR         nvarchar(2)              = NULL,
    @CIERREZ            bit,
    @FECPROCESO         smalldatetime            = NULL,
    @CODTRANSP          nvarchar(15)             = NULL,
    @DOPARTIDA          nvarchar(45)             = NULL,
    @DODESTINO          nvarchar(45)             = NULL,
    @PLACA              nvarchar(10)             = NULL,
    @CDUSERANUL         nvarchar(8)              = NULL,
    @FECANULDOC         smalldatetime            = NULL,
    @TOTANUL            float                    = NULL,
    @STOANUL            float                    = NULL,
    @IGVANUL            float                    = NULL,
    @CDPRECIO1          nvarchar(3)              = NULL,
    @CDPRECIO2          nvarchar(3)              = NULL,
    @IMPRECONSUMO       bit,
    @MTOPROPINA         float                    = NULL,
    @MONPROPINA         nvarchar(1)              = NULL,
    @NROREFERENCIA      float                    = NULL,
    @FLAGCOPIA          bit)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO FACTURA(CDTIPODOCU,
                        NRODOCUM,
                        NROGUIA,
                        NROPROFORM,
                        SUBTOTAL,
                        IGV,
                        TOTAL,
                        MTOGTOFIN,
                        FECDOCUM,
                        MTOPAGEFECS,
                        MTOPAGEFECD,
                        MTOPAGTAR1S,
                        MTOPAGTAR1D,
                        MTOPAGTAR2S,
                        MTOPAGTAR2D,
                        MTOPAGTAR3S,
                        MTOPAGTAR3D,
                        MTOPAGTAR4S,
                        MTOPAGTAR4D,
                        MTOPAGCHEQS,
                        MTOPAGCHEQD,
                        MTOPAGCREDS,
                        MTOPAGCREDD,
                        MTOPAGCREDITOS,
                        MTOPAGCREDITOD,
                        MTOVUELTS,
                        MTOVUELTD,
                        CDTARJCRE1,
                        NROTARJCR1,
                        CDTARJCRE2,
                        NROTARJCR2,
                        CDTARJCRE3,
                        NROTARJCR3,
                        CDTARJCRE4,
                        NROTARJCR4,
                        CDBANCO,
                        NROCTABANC,
                        NROCHQBANC,
                        ESTADO,
                        CDMONEDA,
                        CAMBIO,
                        CAMBIOC,
                        TIPCREDFAC,
                        TIPCREDLET,
                        CDCLIENTE,
                        NOMCLIENT,
                        DIRCLIENT,
                        RUCCLIENT,
                        CDUSUARIO,
                        CDVENDEDOR,
                        PORCSERVIC,
                        MTOSERVICI,
                        MTODSCTO,
                        PORCDCTO1,
                        PORCDCTO2,
                        PORCDCTO3,
                        CDALMACEN,
                        NROPTOVTA,
                        NROORDENC,
                        TIPOFACTUR,
                        CIERREZ,
                        FECPROCESO,
                        CODTRANSP,
                        DOPARTIDA,
                        DODESTINO,
                        PLACA,
                        CDUSERANUL,
                        FECANULDOC,
                        TOTANUL,
                        STOANUL,
                        IGVANUL,
                        CDPRECIO1,
                        CDPRECIO2,
                        IMPRECONSUMO,
                        MTOPROPINA,
                        MONPROPINA,
                        NROREFERENCIA,
                        FLAGCOPIA)
    VALUES(@CDTIPODOCU,
           @NRODOCUM,
           @NROGUIA,
           @NROPROFORM,
           @SUBTOTAL,
           @IGV,
           @TOTAL,
           @MTOGTOFIN,
           @FECDOCUM,
           @MTOPAGEFECS,
           @MTOPAGEFECD,
           @MTOPAGTAR1S,
           @MTOPAGTAR1D,
           @MTOPAGTAR2S,
           @MTOPAGTAR2D,
           @MTOPAGTAR3S,
           @MTOPAGTAR3D,
           @MTOPAGTAR4S,
           @MTOPAGTAR4D,
           @MTOPAGCHEQS,
           @MTOPAGCHEQD,
           @MTOPAGCREDS,
           @MTOPAGCREDD,
           @MTOPAGCREDITOS,
           @MTOPAGCREDITOD,
           @MTOVUELTS,
           @MTOVUELTD,
           @CDTARJCRE1,
           @NROTARJCR1,
           @CDTARJCRE2,
           @NROTARJCR2,
           @CDTARJCRE3,
           @NROTARJCR3,
           @CDTARJCRE4,
           @NROTARJCR4,
           @CDBANCO,
           @NROCTABANC,
           @NROCHQBANC,
           @ESTADO,
           @CDMONEDA,
           @CAMBIO,
           @CAMBIOC,
           @TIPCREDFAC,
           @TIPCREDLET,
           @CDCLIENTE,
           @NOMCLIENT,
           @DIRCLIENT,
           @RUCCLIENT,
           @CDUSUARIO,
           @CDVENDEDOR,
           @PORCSERVIC,
           @MTOSERVICI,
           @MTODSCTO,
           @PORCDCTO1,
           @PORCDCTO2,
           @PORCDCTO3,
           @CDALMACEN,
           @NROPTOVTA,
           @NROORDENC,
           @TIPOFACTUR,
           @CIERREZ,
           @FECPROCESO,
           @CODTRANSP,
           @DOPARTIDA,
           @DODESTINO,
           @PLACA,
           @CDUSERANUL,
           @FECANULDOC,
           @TOTANUL,
           @STOANUL,
           @IGVANUL,
           @CDPRECIO1,
           @CDPRECIO2,
           @IMPRECONSUMO,
           @MTOPROPINA,
           @MONPROPINA,
           @NROREFERENCIA,
           @FLAGCOPIA)

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

    COMMIT TRAN

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





/* 
 * PROCEDURE: sp_InsProcIN_CLUSTER1 
 */

CREATE PROCEDURE sp_InsProcIN_CLUSTER1
(
    @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_CLUSTER1: 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_CLUSTER1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_CLUSTER1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_CLUSTER1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcIN_INVENTHISTOPHISI1 
 */

CREATE PROCEDURE sp_InsProcIN_INVENTHISTOPHISI1
(
    @SITEID               char(5),
    @INVTIDSKU            char(20),
    @INTRDATEPHISICAL     smalldatetime,
    @CLASSID              char(5),
    @CLASSUBID            char(5),
    @STOCKPHISICAL        numeric(10, 3),
    @QTYAVAIL             numeric(10, 3))
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INVENTHISTOPHISI1: 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_INVENTHISTOPHISI1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INVENTHISTOPHISI1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INVENTHISTOPHISI1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcIN_SETUP1 
 */

CREATE PROCEDURE sp_InsProcIN_SETUP1
(
    @PERPOST               UD_PERPOST,
    @INTYPETRANSFERIN      UD_TYPEDOC,
    @INTYPETRANSFEROUT     UD_TYPEDOC,
    @REPORTPATH            char(50),
    @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,
    @USER1                 varchar(30)              = NULL,
    @USER2                 varchar(30)              = NULL,
    @USER3                 varchar(30)              = NULL,
    @USER4                 smalldatetime            = NULL,
    @USER5                 smalldatetime            = NULL,
    @USER6                 float                    = NULL,
    @USER7                 float                    = NULL,
    @USER8                 smallint                 = NULL,
    @USER9                 smallint                 = 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,
                         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,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_SETUP1: 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_SETUP1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_SETUP1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_SETUP1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcOP_SALESPOINTVSCARA1 
 */

CREATE PROCEDURE sp_InsProcOP_SALESPOINTVSCARA1
(
    @SALESPOINT     char(10),
    @CARA           UD_CARA)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESPOINTVSCARA(SALESPOINT,
                                    CARA)
    VALUES(@SALESPOINT,
           @CARA)

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

    COMMIT TRAN

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





/* 
 * PROCEDURE: sp_InsProcOP_SETUP1 
 */

CREATE PROCEDURE sp_InsProcOP_SETUP1
(
    @AUTOMATICSALES     bit,
    @CATEGORYFUEL       char(5),
    @PLACA              bit,
    @MTOMAXSALES        numeric(9, 2),
    @ACCOUNTCARD        char(15),
    @DATEPROCESALES     smalldatetime,
    @USER1              varchar(30)              = NULL,
    @USER2              varchar(30)              = NULL,
    @USER3              varchar(30)              = NULL,
    @USER4              smalldatetime            = NULL,
    @USER5              smalldatetime            = NULL,
    @USER6              float                    = NULL,
    @USER7              float                    = NULL,
    @USER8              smallint                 = NULL,
    @USER9              smallint                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SETUP(AUTOMATICSALES,
                         CATEGORYFUEL,
                         PLACA,
                         MTOMAXSALES,
                         ACCOUNTCARD,
                         DATEPROCESALES,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@AUTOMATICSALES,
           @CATEGORYFUEL,
           @PLACA,
           @MTOMAXSALES,
           @ACCOUNTCARD,
           @DATEPROCESALES,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_SETUP1: 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_SETUP1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_SETUP1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_SETUP1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcSETUP1 
 */

CREATE PROCEDURE sp_InsProcSETUP1
(
    @TYPEPRICEUPDATE        char(5),
    @CUSTID                 char(20),
    @QTYSALESPERSONTURN     tinyint,
    @SALESPERSONTURN        tinyint,
    @QTYCARAS               tinyint,
    @SOURCESITE             char(5),
    @CURYORIG               char(3),
    @FLOATPRICE             tinyint,
    @FLOATCOST              tinyint,
    @FLOATQTY               tinyint,
    @FLOATOUTPUT            tinyint,
    @TAXVALUEIGV            char(2),
    @TAXID                  char(2),
    @SOURCECURYTYPEID       char(6),
    @DATEPROCE              smalldatetime,
    @USER1                  varchar(30)              = NULL,
    @USER2                  varchar(30)              = NULL,
    @USER3                  varchar(30)              = NULL,
    @USER4                  smalldatetime            = NULL,
    @USER5                  smalldatetime            = NULL,
    @USER6                  float                    = NULL,
    @USER7                  float                    = NULL,
    @USER8                  smallint                 = NULL,
    @USER9                  smallint                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SETUP(TYPEPRICEUPDATE,
                      CUSTID,
                      QTYSALESPERSONTURN,
                      SALESPERSONTURN,
                      QTYCARAS,
                      SOURCESITE,
                      CURYORIG,
                      FLOATPRICE,
                      FLOATCOST,
                      FLOATQTY,
                      FLOATOUTPUT,
                      TAXVALUEIGV,
                      TAXID,
                      SOURCECURYTYPEID,
                      DATEPROCE,
                      USER1,
                      USER2,
                      USER3,
                      USER4,
                      USER5,
                      USER6,
                      USER7,
                      USER8,
                      USER9)
    VALUES(@TYPEPRICEUPDATE,
           @CUSTID,
           @QTYSALESPERSONTURN,
           @SALESPERSONTURN,
           @QTYCARAS,
           @SOURCESITE,
           @CURYORIG,
           @FLOATPRICE,
           @FLOATCOST,
           @FLOATQTY,
           @FLOATOUTPUT,
           @TAXVALUEIGV,
           @TAXID,
           @SOURCECURYTYPEID,
           @DATEPROCE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9)

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

    COMMIT TRAN

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





/* 
 * PROCEDURE: sp_InsProcTMP_CORRNBRHEAD1 
 */

CREATE PROCEDURE sp_InsProcTMP_CORRNBRHEAD1
(
    @USERID      UD_USERID)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_CORRNBRHEAD(USERID)
    VALUES(@USERID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcTMP_CORRNBRHEAD1: 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_CORRNBRHEAD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcTMP_CORRNBRHEAD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcTMP_CORRNBRHEAD1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcTMP_IN_INTRAITEM1 
 */

CREATE PROCEDURE sp_InsProcTMP_IN_INTRAITEM1
(
    @CORRNBR           int,
    @SITEID            UD_IDSITE,
    @TYPEDOCID         UD_TYPEMOVEID,
    @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),
    @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_INTRAITEM1: 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_INTRAITEM1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcTMP_IN_INTRAITEM1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcTMP_IN_INTRAITEM1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcTMP_IN_KITCOMPONENT1 
 */

CREATE PROCEDURE sp_InsProcTMP_IN_KITCOMPONENT1
(
    @CORRNBR        int,
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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_KITCOMPONENT1: 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_KITCOMPONENT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcTMP_IN_KITCOMPONENT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcTMP_IN_KITCOMPONENT1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcTMP_OP_SALESDET1 
 */

CREATE PROCEDURE sp_InsProcTMP_OP_SALESDET1
(
    @CORRNBR            int,
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint,
    @SITEID             char(5),
    @INVTIDSKU          char(20),
    @CURYID             char(3),
    @DOCSTATUSID        char(2),
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERSPOT            char(6),
    @STKCLOSEDZ         bit,
    @STKCLOSEDX         bit,
    @NBRTURN            tinyint,
    @NBRCARA            char(2)                   = NULL,
    @NBRTRANSACFUEL     char(4)                   = NULL,
    @DISCDSCTO1         numeric(5, 2),
    @DISCDSCTO2         numeric(5, 2),
    @DISCMTOPEN         numeric(9, 2),
    @DISCMTOUSD         numeric(9, 2),
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(9, 2),
    @TOTALUSD           numeric(9, 2),
    @TAXPEN             numeric(9, 2),
    @TAXUSD             numeric(9, 2),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        char(20),
    @DESCRINVENTORY     varchar(40),
    @QTY                numeric(10, 3),
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              varchar(30)               = NULL,
    @USER4              smalldatetime             = NULL,
    @USER5              smalldatetime             = NULL,
    @USER6              float                     = NULL,
    @USER7              float                     = NULL,
    @USER8              smallint                  = NULL,
    @USER9              smallint                  = NULL,
    @SALESPOINT         char(10),
    @SALESPERID         char(10))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TMP_OP_SALESDET(CORRNBR,
                                DOCTYPEID,
                                NBRDOCUMENT,
                                SEQUENCE,
                                SITEID,
                                INVTIDSKU,
                                CURYID,
                                DOCSTATUSID,
                                DATEDOC,
                                DATEPROCESALES,
                                PERSPOT,
                                STKCLOSEDZ,
                                STKCLOSEDX,
                                NBRTURN,
                                NBRCARA,
                                NBRTRANSACFUEL,
                                DISCDSCTO1,
                                DISCDSCTO2,
                                DISCMTOPEN,
                                DISCMTOUSD,
                                PORCENTTAXIGV,
                                PORCENTTAXISC,
                                TOTALPEN,
                                TOTALUSD,
                                TAXPEN,
                                TAXUSD,
                                STKITEM,
                                STKFISI,
                                SLS_PRICE,
                                SLSPRICESALE,
                                STDCOSTPEN,
                                STDCOSTUSD,
                                INVTIDALTER,
                                DESCRINVENTORY,
                                QTY,
                                USER1,
                                USER2,
                                USER3,
                                USER4,
                                USER5,
                                USER6,
                                USER7,
                                USER8,
                                USER9,
                                SALESPOINT,
                                SALESPERID)
    VALUES(@CORRNBR,
           @DOCTYPEID,
           @NBRDOCUMENT,
           @SEQUENCE,
           @SITEID,
           @INVTIDSKU,
           @CURYID,
           @DOCSTATUSID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERSPOT,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @NBRTURN,
           @NBRCARA,
           @NBRTRANSACFUEL,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTOPEN,
           @DISCMTOUSD,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9,
           @SALESPOINT,
           @SALESPERID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcTMP_OP_SALESDET1: 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_SALESDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcTMP_OP_SALESDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcTMP_OP_SALESDET1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcAR_CCDOC1 
 */

CREATE PROCEDURE sp_InsProcAR_CCDOC1
(
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0),
    @CURYTYPEID         char(6),
    @DOCDATE            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @DUEDATE            smalldatetime,
    @TOTALPEN           numeric(9, 2),
    @TOTALUSD           numeric(9, 2),
    @PAIDMENTPEN        numeric(9, 2),
    @PAIDMENTUSD        numeric(9, 2),
    @DOCVALPEN          numeric(9, 2),
    @DOCVALUSD          numeric(9, 2),
    @MODSALESID         char(2),
    @CUSTIDSS           char(20),
    @TAXREGNBR          char(20)                  = NULL,
    @DOCSTATUSID        char(2),
    @CURYRATE           numeric(6, 4),
    @CURYID             char(3),
    @WEEKDAYPAG         char(12),
    @DAYTHENK           int,
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              float                     = NULL,
    @USER4              float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO AR_CCDOC(DOCTYPEID,
                         NBRDOCUMENT,
                         CURYTYPEID,
                         DOCDATE,
                         DATEPROCESALES,
                         DUEDATE,
                         TOTALPEN,
                         TOTALUSD,
                         PAIDMENTPEN,
                         PAIDMENTUSD,
                         DOCVALPEN,
                         DOCVALUSD,
                         MODSALESID,
                         CUSTIDSS,
                         TAXREGNBR,
                         DOCSTATUSID,
                         CURYRATE,
                         CURYID,
                         WEEKDAYPAG,
                         DAYTHENK,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @CURYTYPEID,
           @DOCDATE,
           @DATEPROCESALES,
           @DUEDATE,
           @TOTALPEN,
           @TOTALUSD,
           @PAIDMENTPEN,
           @PAIDMENTUSD,
           @DOCVALPEN,
           @DOCVALUSD,
           @MODSALESID,
           @CUSTIDSS,
           @TAXREGNBR,
           @DOCSTATUSID,
           @CURYRATE,
           @CURYID,
           @WEEKDAYPAG,
           @DAYTHENK,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcAR_CCDOC1: 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_CCDOC1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcAR_CCDOC1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcAR_CCDOC1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcAR_CCDOC1 
 */

CREATE PROCEDURE sp_UpdProcAR_CCDOC1
(
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0),
    @CURYTYPEID         char(6),
    @DOCDATE            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @DUEDATE            smalldatetime,
    @TOTALPEN           numeric(9, 2),
    @TOTALUSD           numeric(9, 2),
    @PAIDMENTPEN        numeric(9, 2),
    @PAIDMENTUSD        numeric(9, 2),
    @DOCVALPEN          numeric(9, 2),
    @DOCVALUSD          numeric(9, 2),
    @MODSALESID         char(2),
    @CUSTIDSS           char(20),
    @TAXREGNBR          char(20)                  = NULL,
    @DOCSTATUSID        char(2),
    @CURYRATE           numeric(6, 4),
    @CURYID             char(3),
    @WEEKDAYPAG         char(12),
    @DAYTHENK           int,
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              float                     = NULL,
    @USER4              float                     = NULL,
    @TSTAMP             timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE AR_CCDOC
       SET CURYTYPEID          = @CURYTYPEID,
           DOCDATE             = @DOCDATE,
           DATEPROCESALES      = @DATEPROCESALES,
           DUEDATE             = @DUEDATE,
           TOTALPEN            = @TOTALPEN,
           TOTALUSD            = @TOTALUSD,
           PAIDMENTPEN         = @PAIDMENTPEN,
           PAIDMENTUSD         = @PAIDMENTUSD,
           DOCVALPEN           = @DOCVALPEN,
           DOCVALUSD           = @DOCVALUSD,
           MODSALESID          = @MODSALESID,
           CUSTIDSS            = @CUSTIDSS,
           TAXREGNBR           = @TAXREGNBR,
           DOCSTATUSID         = @DOCSTATUSID,
           CURYRATE            = @CURYRATE,
           CURYID              = @CURYID,
           WEEKDAYPAG          = @WEEKDAYPAG,
           DAYTHENK            = @DAYTHENK,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcAR_CCDOC1 
 */

CREATE PROCEDURE sp_DelProcAR_CCDOC1
(
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcAR_CCDOC1: 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_CCDOC1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcAR_CCDOC1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcAR_CCDOC1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcAR_CCDOC1 
 */

CREATE PROCEDURE sp_SelProcAR_CCDOC1
(
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0))
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           CURYTYPEID,
           DOCDATE,
           DATEPROCESALES,
           DUEDATE,
           TOTALPEN,
           TOTALUSD,
           PAIDMENTPEN,
           PAIDMENTUSD,
           DOCVALPEN,
           DOCVALUSD,
           MODSALESID,
           CUSTIDSS,
           TAXREGNBR,
           DOCSTATUSID,
           CURYRATE,
           CURYID,
           WEEKDAYPAG,
           DAYTHENK,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM AR_CCDOC
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

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


/* 
 * PROCEDURE: sp_InsProcAR_CCDOCDET1 
 */

CREATE PROCEDURE sp_InsProcAR_CCDOCDET1
(
    @DOCTYPEID             char(3),
    @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(9, 2),
    @TOTALUSD              numeric(9, 2),
    @PAIDMENTPEN           numeric(9, 2),
    @PAIDMENTUSD           numeric(9, 2),
    @CURYID                char(3),
    @CURYRATE              numeric(6, 4),
    @USER1                 varchar(30)               = NULL,
    @USER2                 varchar(30)               = NULL,
    @USER3                 float                     = NULL,
    @USER4                 float                     = 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,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SEQUENCE,
           @CURYTYPEID,
           @DOCSTATUSID,
           @DOCDATE,
           @DATEPROCEPAIDMENT,
           @DATEPROCESALES,
           @BANKID,
           @CHEQNUMBER,
           @NBRBANKACCOUNT,
           @TOTALPEN,
           @TOTALUSD,
           @PAIDMENTPEN,
           @PAIDMENTUSD,
           @CURYID,
           @CURYRATE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcAR_CCDOCDET1: 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_CCDOCDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcAR_CCDOCDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcAR_CCDOCDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcAR_CCDOCDET1 
 */

CREATE PROCEDURE sp_UpdProcAR_CCDOCDET1
(
    @DOCTYPEID             char(3),
    @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(9, 2),
    @TOTALUSD              numeric(9, 2),
    @PAIDMENTPEN           numeric(9, 2),
    @PAIDMENTUSD           numeric(9, 2),
    @CURYID                char(3),
    @CURYRATE              numeric(6, 4),
    @USER1                 varchar(30)               = NULL,
    @USER2                 varchar(30)               = NULL,
    @USER3                 float                     = NULL,
    @USER4                 float                     = 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,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcAR_CCDOCDET1 
 */

CREATE PROCEDURE sp_DelProcAR_CCDOCDET1
(
    @DOCTYPEID             char(3),
    @NBRDOCUMENT           numeric(10, 0),
    @SEQUENCE              smallint)
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcAR_CCDOCDET1: 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_CCDOCDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcAR_CCDOCDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcAR_CCDOCDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcAR_CCDOCDET1 
 */

CREATE PROCEDURE sp_SelProcAR_CCDOCDET1
(
    @DOCTYPEID             char(3),
    @NBRDOCUMENT           numeric(10, 0),
    @SEQUENCE              smallint)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SEQUENCE,
           CURYTYPEID,
           DOCSTATUSID,
           DOCDATE,
           DATEPROCEPAIDMENT,
           DATEPROCESALES,
           BANKID,
           CHEQNUMBER,
           NBRBANKACCOUNT,
           TOTALPEN,
           TOTALUSD,
           PAIDMENTPEN,
           PAIDMENTUSD,
           CURYID,
           CURYRATE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM AR_CCDOCDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

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


/* 
 * PROCEDURE: sp_InsProcBANK1 
 */

CREATE PROCEDURE sp_InsProcBANK1
(
    @BANKID          char(2),
    @DESCR           varchar(40),
    @BANKACCOUNT     char(10),
    @USER01          varchar(30)            = NULL,
    @USER02          varchar(30)            = NULL,
    @USER03          float                  = NULL,
    @USER04          float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO BANK(BANKID,
                     DESCR,
                     BANKACCOUNT,
                     USER01,
                     USER02,
                     USER03,
                     USER04 
                     )
    VALUES(@BANKID,
           @DESCR,
           @BANKACCOUNT,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcBANK1 
 */

CREATE PROCEDURE sp_UpdProcBANK1
(
    @BANKID          char(2),
    @DESCR           varchar(40),
    @BANKACCOUNT     char(10),
    @USER01          varchar(30)            = NULL,
    @USER02          varchar(30)            = NULL,
    @USER03          float                  = NULL,
    @USER04          float                  = NULL,
    @TSTAMP          timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE BANK
       SET DESCR            = @DESCR,
           BANKACCOUNT      = @BANKACCOUNT,
           USER01           = @USER01,
           USER02           = @USER02,
           USER03           = @USER03,
           USER04           = @USER04
     WHERE BANKID = @BANKID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcBANK1 
 */

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

    DELETE
      FROM BANK
     WHERE BANKID = @BANKID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcBANK1 
 */

CREATE PROCEDURE sp_SelProcBANK1
(
    @BANKID          char(2))
AS
BEGIN
    SELECT BANKID,
           DESCR,
           BANKACCOUNT,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM BANK
     WHERE BANKID = @BANKID

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


/* 
 * PROCEDURE: sp_InsProcCITY1 
 */

CREATE PROCEDURE sp_InsProcCITY1
(
    @CITYID      char(3),
    @STATEID     char(2),
    @DESCR       UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CITY(CITYID,
                     STATEID,
                     DESCR 
                     )
    VALUES(@CITYID,
           @STATEID,
           @DESCR 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCITY1 
 */

CREATE PROCEDURE sp_UpdProcCITY1
(
    @CITYID      char(3),
    @STATEID     char(2),
    @DESCR       UD_DESCRIPCION,
    @TSTAMP      timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CITY
       SET STATEID      = @STATEID,
           DESCR        = @DESCR
     WHERE CITYID = @CITYID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCITY1 
 */

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

    DELETE
      FROM CITY
     WHERE CITYID = @CITYID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCITY1 
 */

CREATE PROCEDURE sp_SelProcCITY1
(
    @CITYID      char(3))
AS
BEGIN
    SELECT CITYID,
           STATEID,
           DESCR,
           TSTAMP
      FROM CITY
     WHERE CITYID = @CITYID

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


/* 
 * PROCEDURE: sp_InsProcCOUNTRY1 
 */

CREATE PROCEDURE sp_InsProcCOUNTRY1
(
    @COUNTRYID     char(3),
    @DESCR         UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO COUNTRY(COUNTRYID,
                        DESCR 
                        )
    VALUES(@COUNTRYID,
           @DESCR 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCOUNTRY1 
 */

CREATE PROCEDURE sp_UpdProcCOUNTRY1
(
    @COUNTRYID     char(3),
    @DESCR         UD_DESCRIPCION,
    @TSTAMP        timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE COUNTRY
       SET DESCR          = @DESCR
     WHERE COUNTRYID = @COUNTRYID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCOUNTRY1 
 */

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

    DELETE
      FROM COUNTRY
     WHERE COUNTRYID = @COUNTRYID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCOUNTRY1 
 */

CREATE PROCEDURE sp_SelProcCOUNTRY1
(
    @COUNTRYID     char(3))
AS
BEGIN
    SELECT COUNTRYID,
           DESCR,
           TSTAMP
      FROM COUNTRY
     WHERE COUNTRYID = @COUNTRYID

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


/* 
 * PROCEDURE: sp_InsProcCURRENCY1 
 */

CREATE PROCEDURE sp_InsProcCURRENCY1
(
    @CURYID         UD_CURYID,
    @DESCR          UD_DESCRIPCION,
    @DESCRMONEY     char(5),
    @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,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@CURYID,
           @DESCR,
           @DESCRMONEY,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCURRENCY1 
 */

CREATE PROCEDURE sp_UpdProcCURRENCY1
(
    @CURYID         UD_CURYID,
    @DESCR          UD_DESCRIPCION,
    @DESCRMONEY     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 CURRENCY
       SET DESCR           = @DESCR,
           DESCRMONEY      = @DESCRMONEY,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE CURYID = @CURYID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCURRENCY1 
 */

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

    DELETE
      FROM CURRENCY
     WHERE CURYID = @CURYID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCURRENCY1 
 */

CREATE PROCEDURE sp_SelProcCURRENCY1
(
    @CURYID         UD_CURYID)
AS
BEGIN
    SELECT CURYID,
           DESCR,
           DESCRMONEY,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CURRENCY
     WHERE CURYID = @CURYID

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


/* 
 * PROCEDURE: sp_InsProcCURYRATE1 
 */

CREATE PROCEDURE sp_InsProcCURYRATE1
(
    @CURYTYPEID     UD_CURYTYPEID,
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime,
    @RATE           numeric(6, 4),
    @OPERADOR       char(1)                  = NULL,
    @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,
                         USER1,
                         USER2,
                         USER3,
                         USER4 
                         )
    VALUES(@CURYTYPEID,
           @FROMCURYID,
           @TOCURYID,
           @CURYDATE,
           @RATE,
           @OPERADOR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCURYRATE1 
 */

CREATE PROCEDURE sp_UpdProcCURYRATE1
(
    @CURYTYPEID     UD_CURYTYPEID,
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime,
    @RATE           numeric(6, 4),
    @OPERADOR       char(1)                  = NULL,
    @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,
           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_UpdProcCURYRATE1: Cannot update  in CURYRATE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCURYRATE1 
 */

CREATE PROCEDURE sp_DelProcCURYRATE1
(
    @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_DelProcCURYRATE1: Cannot delete because foreign keys still exist in CURYRATE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCURYRATE1 
 */

CREATE PROCEDURE sp_SelProcCURYRATE1
(
    @CURYTYPEID     UD_CURYTYPEID,
    @FROMCURYID     UD_CURYID,
    @TOCURYID       UD_CURYID,
    @CURYDATE       smalldatetime)
AS
BEGIN
    SELECT CURYTYPEID,
           FROMCURYID,
           TOCURYID,
           CURYDATE,
           RATE,
           OPERADOR,
           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_SelProcCURYRATE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcCURYRATE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcCURYRATE1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcCURYRATETYPE1 
 */

CREATE PROCEDURE sp_InsProcCURYRATETYPE1
(
    @CURYTYPEID     UD_CURYTYPEID,
    @DESCR          char(30)                 = NULL,
    @USER4          UD_USER7                 = NULL,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CURYRATETYPE(CURYTYPEID,
                             DESCR,
                             USER4,
                             USER1,
                             USER2,
                             USER3 
                             )
    VALUES(@CURYTYPEID,
           @DESCR,
           @USER4,
           @USER1,
           @USER2,
           @USER3 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCURYRATETYPE1 
 */

CREATE PROCEDURE sp_UpdProcCURYRATETYPE1
(
    @CURYTYPEID     UD_CURYTYPEID,
    @DESCR          char(30)                 = NULL,
    @USER4          UD_USER7                 = NULL,
    @USER1          UD_USER1                 = NULL,
    @USER2          UD_USER2                 = NULL,
    @USER3          UD_USER6                 = NULL,
    @TSTAMP         timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CURYRATETYPE
       SET DESCR           = @DESCR,
           USER4           = @USER4,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3
     WHERE CURYTYPEID = @CURYTYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCURYRATETYPE1 
 */

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

    DELETE
      FROM CURYRATETYPE
     WHERE CURYTYPEID = @CURYTYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCURYRATETYPE1 
 */

CREATE PROCEDURE sp_SelProcCURYRATETYPE1
(
    @CURYTYPEID     UD_CURYTYPEID)
AS
BEGIN
    SELECT CURYTYPEID,
           DESCR,
           USER4,
           USER1,
           USER2,
           USER3,
           TSTAMP
      FROM CURYRATETYPE
     WHERE CURYTYPEID = @CURYTYPEID

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


/* 
 * PROCEDURE: sp_InsProcCUST_LIMIT1 
 */

CREATE PROCEDURE sp_InsProcCUST_LIMIT1
(
    @CUSTIDSS        char(20),
    @SITEID          char(5),
    @CREDLIMITED     numeric(9, 2),
    @USER1           varchar(30)              = NULL,
    @USER2           varchar(30)              = NULL,
    @USER3           float                    = NULL,
    @USER4           float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUST_LIMIT(CUSTIDSS,
                           SITEID,
                           CREDLIMITED,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@CUSTIDSS,
           @SITEID,
           @CREDLIMITED,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCUST_LIMIT1 
 */

CREATE PROCEDURE sp_UpdProcCUST_LIMIT1
(
    @CUSTIDSS        char(20),
    @SITEID          char(5),
    @CREDLIMITED     numeric(9, 2),
    @USER1           varchar(30)              = NULL,
    @USER2           varchar(30)              = NULL,
    @USER3           float                    = NULL,
    @USER4           float                    = NULL,
    @TSTAMP          timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUST_LIMIT
       SET CREDLIMITED      = @CREDLIMITED,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCUST_LIMIT1 
 */

CREATE PROCEDURE sp_DelProcCUST_LIMIT1
(
    @CUSTIDSS        char(20),
    @SITEID          char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUST_LIMIT
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCUST_LIMIT1 
 */

CREATE PROCEDURE sp_SelProcCUST_LIMIT1
(
    @CUSTIDSS        char(20),
    @SITEID          char(5))
AS
BEGIN
    SELECT CUSTIDSS,
           SITEID,
           CREDLIMITED,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CUST_LIMIT
     WHERE CUSTIDSS = @CUSTIDSS
       AND SITEID   = @SITEID

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


/* 
 * PROCEDURE: sp_InsProcCUSTOMER1 
 */

CREATE PROCEDURE sp_InsProcCUSTOMER1
(
    @CUSTIDSS         char(20),
    @CUSTSTATUSID     char(1),
    @CUSTID           char(20),
    @TAXREGNBR        char(20)                 = NULL,
    @CUSTNAME         varchar(40)              = NULL,
    @ADDR1            varchar(40)              = NULL,
    @ADDR2            varchar(40)              = NULL,
    @PHONE            char(10)                 = NULL,
    @FAX              char(10)                 = NULL,
    @CUSTZONEID       char(2),
    @COUNTRYID        char(3),
    @STATEID          char(2),
    @CITYID           char(3),
    @CUSTCLASSID      char(2),
    @TAXID1           char(2),
    @TAXID2           char(2),
    @CURYTYPEID       char(6),
    @TERMID           char(2),
    @MODSALESID       char(2),
    @SALESPERID       char(10)                 = NULL,
    @PRCLVLCUSTID     char(10)                 = NULL,
    @DATEORIG         datetime                 = NULL,
    @DATEPROC         datetime,
    @WEEKDAYPAG       char(12),
    @DAYTHENK         int,
    @CURYID           char(3)                  = NULL,
    @CURRBAL          numeric(9, 2),
    @USER1            varchar(30)              = NULL,
    @USER2            varchar(30)              = NULL,
    @USER3            varchar(30)              = NULL,
    @USER4            smalldatetime            = NULL,
    @USER5            smalldatetime            = NULL,
    @USER6            float                    = NULL,
    @USER7            float                    = NULL,
    @USER8            smallint                 = NULL,
    @USER9            smallint                 = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTOMER(CUSTIDSS,
                         CUSTSTATUSID,
                         CUSTID,
                         TAXREGNBR,
                         CUSTNAME,
                         ADDR1,
                         ADDR2,
                         PHONE,
                         FAX,
                         CUSTZONEID,
                         COUNTRYID,
                         STATEID,
                         CITYID,
                         CUSTCLASSID,
                         TAXID1,
                         TAXID2,
                         CURYTYPEID,
                         TERMID,
                         MODSALESID,
                         SALESPERID,
                         PRCLVLCUSTID,
                         DATEORIG,
                         DATEPROC,
                         WEEKDAYPAG,
                         DAYTHENK,
                         CURYID,
                         CURRBAL,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@CUSTIDSS,
           @CUSTSTATUSID,
           @CUSTID,
           @TAXREGNBR,
           @CUSTNAME,
           @ADDR1,
           @ADDR2,
           @PHONE,
           @FAX,
           @CUSTZONEID,
           @COUNTRYID,
           @STATEID,
           @CITYID,
           @CUSTCLASSID,
           @TAXID1,
           @TAXID2,
           @CURYTYPEID,
           @TERMID,
           @MODSALESID,
           @SALESPERID,
           @PRCLVLCUSTID,
           @DATEORIG,
           @DATEPROC,
           @WEEKDAYPAG,
           @DAYTHENK,
           @CURYID,
           @CURRBAL,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCUSTOMER1 
 */

CREATE PROCEDURE sp_UpdProcCUSTOMER1
(
    @CUSTIDSS         char(20),
    @CUSTSTATUSID     char(1),
    @CUSTID           char(20),
    @TAXREGNBR        char(20)                 = NULL,
    @CUSTNAME         varchar(40)              = NULL,
    @ADDR1            varchar(40)              = NULL,
    @ADDR2            varchar(40)              = NULL,
    @PHONE            char(10)                 = NULL,
    @FAX              char(10)                 = NULL,
    @CUSTZONEID       char(2),
    @COUNTRYID        char(3),
    @STATEID          char(2),
    @CITYID           char(3),
    @CUSTCLASSID      char(2),
    @TAXID1           char(2),
    @TAXID2           char(2),
    @CURYTYPEID       char(6),
    @TERMID           char(2),
    @MODSALESID       char(2),
    @SALESPERID       char(10)                 = NULL,
    @PRCLVLCUSTID     char(10)                 = NULL,
    @DATEORIG         datetime                 = NULL,
    @DATEPROC         datetime,
    @WEEKDAYPAG       char(12),
    @DAYTHENK         int,
    @CURYID           char(3)                  = NULL,
    @CURRBAL          numeric(9, 2),
    @USER1            varchar(30)              = NULL,
    @USER2            varchar(30)              = NULL,
    @USER3            varchar(30)              = NULL,
    @USER4            smalldatetime            = NULL,
    @USER5            smalldatetime            = NULL,
    @USER6            float                    = NULL,
    @USER7            float                    = NULL,
    @USER8            smallint                 = NULL,
    @USER9            smallint                 = NULL,
    @TSTAMP           timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTOMER
       SET CUSTSTATUSID      = @CUSTSTATUSID,
           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,
           TERMID            = @TERMID,
           MODSALESID        = @MODSALESID,
           SALESPERID        = @SALESPERID,
           PRCLVLCUSTID      = @PRCLVLCUSTID,
           DATEORIG          = @DATEORIG,
           DATEPROC          = @DATEPROC,
           WEEKDAYPAG        = @WEEKDAYPAG,
           DAYTHENK          = @DAYTHENK,
           CURYID            = @CURYID,
           CURRBAL           = @CURRBAL,
           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_UpdProcCUSTOMER1: Cannot update  in CUSTOMER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCUSTOMER1 
 */

CREATE PROCEDURE sp_DelProcCUSTOMER1
(
    @CUSTIDSS         char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUSTOMER
     WHERE CUSTIDSS = @CUSTIDSS

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCUSTOMER1 
 */

CREATE PROCEDURE sp_SelProcCUSTOMER1
(
    @CUSTIDSS         char(20))
AS
BEGIN
    SELECT CUSTIDSS,
           CUSTSTATUSID,
           CUSTID,
           TAXREGNBR,
           CUSTNAME,
           ADDR1,
           ADDR2,
           PHONE,
           FAX,
           CUSTZONEID,
           COUNTRYID,
           STATEID,
           CITYID,
           CUSTCLASSID,
           TAXID1,
           TAXID2,
           CURYTYPEID,
           TERMID,
           MODSALESID,
           SALESPERID,
           PRCLVLCUSTID,
           DATEORIG,
           DATEPROC,
           WEEKDAYPAG,
           DAYTHENK,
           CURYID,
           CURRBAL,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM CUSTOMER
     WHERE CUSTIDSS = @CUSTIDSS

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


/* 
 * PROCEDURE: sp_InsProcCUSTSTATUS1 
 */

CREATE PROCEDURE sp_InsProcCUSTSTATUS1
(
    @CUSTSTATUSID     char(1),
    @DESCRCUST        varchar(40),
    @USER1            varchar(30)            = NULL,
    @USER2            varchar(30)            = NULL,
    @USER3            float                  = NULL,
    @USER4            float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTSTATUS(CUSTSTATUSID,
                           DESCRCUST,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@CUSTSTATUSID,
           @DESCRCUST,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCUSTSTATUS1 
 */

CREATE PROCEDURE sp_UpdProcCUSTSTATUS1
(
    @CUSTSTATUSID     char(1),
    @DESCRCUST        varchar(40),
    @USER1            varchar(30)            = NULL,
    @USER2            varchar(30)            = NULL,
    @USER3            float                  = NULL,
    @USER4            float                  = NULL,
    @TSTAMP           timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTSTATUS
       SET DESCRCUST         = @DESCRCUST,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE CUSTSTATUSID = @CUSTSTATUSID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCUSTSTATUS1 
 */

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

    DELETE
      FROM CUSTSTATUS
     WHERE CUSTSTATUSID = @CUSTSTATUSID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCUSTSTATUS1 
 */

CREATE PROCEDURE sp_SelProcCUSTSTATUS1
(
    @CUSTSTATUSID     char(1))
AS
BEGIN
    SELECT CUSTSTATUSID,
           DESCRCUST,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CUSTSTATUS
     WHERE CUSTSTATUSID = @CUSTSTATUSID

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


/* 
 * PROCEDURE: sp_InsProcCUSTWARRANTY1 
 */

CREATE PROCEDURE sp_InsProcCUSTWARRANTY1
(
    @CUSTIDSS               char(20),
    @CUSTGARANTIATYPEID     char(2),
    @TOTGAR                 numeric(9, 2),
    @DESCRGAR               varchar(40),
    @EXPIREDATE             smalldatetime,
    @CURYID                 char(3),
    @USER1                  varchar(30)              = NULL,
    @USER2                  varchar(30)              = NULL,
    @USER3                  float                    = NULL,
    @USER4                  float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTWARRANTY(CUSTIDSS,
                             CUSTGARANTIATYPEID,
                             TOTGAR,
                             DESCRGAR,
                             EXPIREDATE,
                             CURYID,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CUSTIDSS,
           @CUSTGARANTIATYPEID,
           @TOTGAR,
           @DESCRGAR,
           @EXPIREDATE,
           @CURYID,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCUSTWARRANTY1 
 */

CREATE PROCEDURE sp_UpdProcCUSTWARRANTY1
(
    @CUSTIDSS               char(20),
    @CUSTGARANTIATYPEID     char(2),
    @TOTGAR                 numeric(9, 2),
    @DESCRGAR               varchar(40),
    @EXPIREDATE             smalldatetime,
    @CURYID                 char(3),
    @USER1                  varchar(30)              = NULL,
    @USER2                  varchar(30)              = NULL,
    @USER3                  float                    = NULL,
    @USER4                  float                    = NULL,
    @TSTAMP                 timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTWARRANTY
       SET TOTGAR                  = @TOTGAR,
           DESCRGAR                = @DESCRGAR,
           EXPIREDATE              = @EXPIREDATE,
           CURYID                  = @CURYID,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE CUSTIDSS           = @CUSTIDSS
       AND CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCUSTWARRANTY1 
 */

CREATE PROCEDURE sp_DelProcCUSTWARRANTY1
(
    @CUSTIDSS               char(20),
    @CUSTGARANTIATYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

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

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCUSTWARRANTY1 
 */

CREATE PROCEDURE sp_SelProcCUSTWARRANTY1
(
    @CUSTIDSS               char(20),
    @CUSTGARANTIATYPEID     char(2))
AS
BEGIN
    SELECT CUSTIDSS,
           CUSTGARANTIATYPEID,
           TOTGAR,
           DESCRGAR,
           EXPIREDATE,
           CURYID,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM CUSTWARRANTY
     WHERE CUSTIDSS           = @CUSTIDSS
       AND CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

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


/* 
 * PROCEDURE: sp_InsProcCUSTWARRANTYTYPE1 
 */

CREATE PROCEDURE sp_InsProcCUSTWARRANTYTYPE1
(
    @CUSTGARANTIATYPEID     char(2),
    @DECRCUSTGARANTIA       binary(20),
    @USER1                  varchar(30)            = NULL,
    @USER2                  varchar(30)            = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO CUSTWARRANTYTYPE(CUSTGARANTIATYPEID,
                                 DECRCUSTGARANTIA,
                                 USER1,
                                 USER2 
                                 )
    VALUES(@CUSTGARANTIATYPEID,
           @DECRCUSTGARANTIA,
           @USER1,
           @USER2 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcCUSTWARRANTYTYPE1 
 */

CREATE PROCEDURE sp_UpdProcCUSTWARRANTYTYPE1
(
    @CUSTGARANTIATYPEID     char(2),
    @DECRCUSTGARANTIA       binary(20),
    @USER1                  varchar(30)            = NULL,
    @USER2                  varchar(30)            = NULL,
    @TSTAMP                 timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE CUSTWARRANTYTYPE
       SET DECRCUSTGARANTIA        = @DECRCUSTGARANTIA,
           USER1                   = @USER1,
           USER2                   = @USER2
     WHERE CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcCUSTWARRANTYTYPE1 
 */

CREATE PROCEDURE sp_DelProcCUSTWARRANTYTYPE1
(
    @CUSTGARANTIATYPEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM CUSTWARRANTYTYPE
     WHERE CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcCUSTWARRANTYTYPE1 
 */

CREATE PROCEDURE sp_SelProcCUSTWARRANTYTYPE1
(
    @CUSTGARANTIATYPEID     char(2))
AS
BEGIN
    SELECT CUSTGARANTIATYPEID,
           DECRCUSTGARANTIA,
           USER1,
           USER2,
           TSTAMP
      FROM CUSTWARRANTYTYPE
     WHERE CUSTGARANTIATYPEID = @CUSTGARANTIATYPEID

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


/* 
 * PROCEDURE: sp_InsProcDOCTYPE1 
 */

CREATE PROCEDURE sp_InsProcDOCTYPE1
(
    @DOCTYPEID     UD_DOCTYPEID,
    @DESCR         UD_DESCRIPCION,
    @MINDES        char(5),
    @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,
                        USER1,
                        USER2,
                        USER3,
                        USER4 
                        )
    VALUES(@DOCTYPEID,
           @DESCR,
           @MINDES,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcDOCTYPE1 
 */

CREATE PROCEDURE sp_UpdProcDOCTYPE1
(
    @DOCTYPEID     UD_DOCTYPEID,
    @DESCR         UD_DESCRIPCION,
    @MINDES        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 DOCTYPE
       SET DESCR          = @DESCR,
           MINDES         = @MINDES,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4
     WHERE DOCTYPEID = @DOCTYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcDOCTYPE1 
 */

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

    DELETE
      FROM DOCTYPE
     WHERE DOCTYPEID = @DOCTYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcDOCTYPE1 
 */

CREATE PROCEDURE sp_SelProcDOCTYPE1
(
    @DOCTYPEID     UD_DOCTYPEID)
AS
BEGIN
    SELECT DOCTYPEID,
           DESCR,
           MINDES,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM DOCTYPE
     WHERE DOCTYPEID = @DOCTYPEID

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


/* 
 * PROCEDURE: sp_InsProcIN_CATEGORY1 
 */

CREATE PROCEDURE sp_InsProcIN_CATEGORY1
(
    @CLASSID          char(5),
    @DESCR            varchar(40),
    @BUSSINESTYPE     char(1),
    @USER1            varchar(30)            = NULL,
    @USER2            varchar(30)            = NULL,
    @USER3            float                  = NULL,
    @USER4            float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_CATEGORY(CLASSID,
                            DESCR,
                            BUSSINESTYPE,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CLASSID,
           @DESCR,
           @BUSSINESTYPE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_CATEGORY1: 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_CATEGORY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_CATEGORY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_CATEGORY1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_CATEGORY1 
 */

CREATE PROCEDURE sp_UpdProcIN_CATEGORY1
(
    @CLASSID          char(5),
    @DESCR            varchar(40),
    @BUSSINESTYPE     char(1),
    @USER1            varchar(30)            = NULL,
    @USER2            varchar(30)            = NULL,
    @USER3            float                  = NULL,
    @USER4            float                  = NULL,
    @TSTAMP           timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_CATEGORY
       SET DESCR             = @DESCR,
           BUSSINESTYPE      = @BUSSINESTYPE,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE CLASSID = @CLASSID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_CATEGORY1 
 */

CREATE PROCEDURE sp_DelProcIN_CATEGORY1
(
    @CLASSID          char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_CATEGORY
     WHERE CLASSID = @CLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_CATEGORY1: 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_CATEGORY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_CATEGORY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_CATEGORY1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_CATEGORY1 
 */

CREATE PROCEDURE sp_SelProcIN_CATEGORY1
(
    @CLASSID          char(5))
AS
BEGIN
    SELECT CLASSID,
           DESCR,
           BUSSINESTYPE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_CATEGORY
     WHERE CLASSID = @CLASSID

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


/* 
 * PROCEDURE: sp_InsProcIN_CLASSVENDOR1 
 */

CREATE PROCEDURE sp_InsProcIN_CLASSVENDOR1
(
    @CLASSVENDORID     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 IN_CLASSVENDOR(CLASSVENDORID,
                               DESCR,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@CLASSVENDORID,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_CLASSVENDOR1: 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_CLASSVENDOR1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_CLASSVENDOR1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_CLASSVENDOR1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_CLASSVENDOR1 
 */

CREATE PROCEDURE sp_UpdProcIN_CLASSVENDOR1
(
    @CLASSVENDORID     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 IN_CLASSVENDOR
       SET DESCR              = @DESCR,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE CLASSVENDORID = @CLASSVENDORID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_CLASSVENDOR1 
 */

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

    DELETE
      FROM IN_CLASSVENDOR
     WHERE CLASSVENDORID = @CLASSVENDORID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_CLASSVENDOR1: 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_CLASSVENDOR1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_CLASSVENDOR1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_CLASSVENDOR1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_CLASSVENDOR1 
 */

CREATE PROCEDURE sp_SelProcIN_CLASSVENDOR1
(
    @CLASSVENDORID     char(2))
AS
BEGIN
    SELECT CLASSVENDORID,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_CLASSVENDOR
     WHERE CLASSVENDORID = @CLASSVENDORID

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


/* 
 * PROCEDURE: sp_InsProcIN_DOCSTATUS1 
 */

CREATE PROCEDURE sp_InsProcIN_DOCSTATUS1
(
    @DOCSTATUSID     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 IN_DOCSTATUS(DOCSTATUSID,
                             DESCR,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@DOCSTATUSID,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_DOCSTATUS1: 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_DOCSTATUS1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_DOCSTATUS1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_DOCSTATUS1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_DOCSTATUS1 
 */

CREATE PROCEDURE sp_UpdProcIN_DOCSTATUS1
(
    @DOCSTATUSID     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 IN_DOCSTATUS
       SET DESCR            = @DESCR,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE DOCSTATUSID = @DOCSTATUSID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_DOCSTATUS1 
 */

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

    DELETE
      FROM IN_DOCSTATUS
     WHERE DOCSTATUSID = @DOCSTATUSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_DOCSTATUS1: 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_DOCSTATUS1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_DOCSTATUS1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_DOCSTATUS1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_DOCSTATUS1 
 */

CREATE PROCEDURE sp_SelProcIN_DOCSTATUS1
(
    @DOCSTATUSID     char(2))
AS
BEGIN
    SELECT DOCSTATUSID,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_DOCSTATUS
     WHERE DOCSTATUSID = @DOCSTATUSID

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


/* 
 * PROCEDURE: sp_InsProcIN_INTRAHEAD1 
 */

CREATE PROCEDURE sp_InsProcIN_INTRAHEAD1
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @VENDIDSS          UD_IDVENDOR,
    @DOCTYPEID         UD_DOCTYPEID,
    @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     UD_MONTO,
    @INTRSUBTOTUSD     UD_MONTO,
    @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,
                             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,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INTRAHEAD1: 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_INTRAHEAD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INTRAHEAD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INTRAHEAD1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INTRAHEAD1 
 */

CREATE PROCEDURE sp_UpdProcIN_INTRAHEAD1
(
    @SITEID            UD_IDSITE,
    @BATNBR            char(10),
    @TYPEDOCID         UD_TYPEDOC,
    @VENDIDSS          UD_IDVENDOR,
    @DOCTYPEID         UD_DOCTYPEID,
    @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     UD_MONTO,
    @INTRSUBTOTUSD     UD_MONTO,
    @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,
           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_INTRAHEAD1: Cannot update  in IN_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INTRAHEAD1 
 */

CREATE PROCEDURE sp_DelProcIN_INTRAHEAD1
(
    @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_INTRAHEAD1: 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_INTRAHEAD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INTRAHEAD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INTRAHEAD1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INTRAHEAD1 
 */

CREATE PROCEDURE sp_SelProcIN_INTRAHEAD1
(
    @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,
           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_INTRAHEAD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcIN_INTRAHEAD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcIN_INTRAHEAD1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcIN_INTRAITEM1 
 */

CREATE PROCEDURE sp_InsProcIN_INTRAITEM1
(
    @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          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @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,
    @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,
                             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,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9,
           @BUYCOD,
           @DOCSTATUSID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INTRAITEM1: 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_INTRAITEM1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INTRAITEM1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INTRAITEM1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INTRAITEM1 
 */

CREATE PROCEDURE sp_UpdProcIN_INTRAITEM1
(
    @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          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @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,
    @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,
           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_INTRAITEM1: Cannot update  in IN_INTRAITEM '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INTRAITEM1 
 */

CREATE PROCEDURE sp_DelProcIN_INTRAITEM1
(
    @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_INTRAITEM1: 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_INTRAITEM1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INTRAITEM1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INTRAITEM1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INTRAITEM1 
 */

CREATE PROCEDURE sp_SelProcIN_INTRAITEM1
(
    @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,
           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_INTRAITEM1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcIN_INTRAITEM1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcIN_INTRAITEM1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcIN_INVENTORY1 
 */

CREATE PROCEDURE sp_InsProcIN_INVENTORY1
(
    @INVTIDSKU       char(20),
    @TAXISC          char(2)                   = NULL,
    @TAXIGV          char(2),
    @CLASSID         char(5),
    @CLASSUBID       char(5),
    @INVTYPEID       char(5),
    @STKUNITID       char(5),
    @DESCR           varchar(40),
    @WTSTK1          numeric(7, 3),
    @STKFUELS        bit,
    @STKSTORE        bit,
    @STKOTHER        bit,
    @STKITEM         bit,
    @KIT             bit,
    @DISCDSCTO1      numeric(5, 2),
    @DISCDSCTO2      numeric(5, 2),
    @DISCMTO         numeric(12, 2),
    @STKRECEPT       bit,
    @STKSALES        bit,
    @STATUS          bit,
    @CONSIGNMENT     bit,
    @DISASSEMBLE     bit,
    @USER1           varchar(30)               = NULL,
    @USER2           varchar(30)               = NULL,
    @USER3           varchar(30)               = NULL,
    @USER4           smalldatetime             = NULL,
    @USER5           smalldatetime             = NULL,
    @USER6           float                     = NULL,
    @USER7           float                     = NULL,
    @USER8           smallint                  = NULL,
    @USER9           smallint                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_INVENTORY(INVTIDSKU,
                             TAXISC,
                             TAXIGV,
                             CLASSID,
                             CLASSUBID,
                             INVTYPEID,
                             STKUNITID,
                             DESCR,
                             WTSTK1,
                             STKFUELS,
                             STKSTORE,
                             STKOTHER,
                             STKITEM,
                             KIT,
                             DISCDSCTO1,
                             DISCDSCTO2,
                             DISCMTO,
                             STKRECEPT,
                             STKSALES,
                             STATUS,
                             CONSIGNMENT,
                             DISASSEMBLE,
                             USER1,
                             USER2,
                             USER3,
                             USER4,
                             USER5,
                             USER6,
                             USER7,
                             USER8,
                             USER9 
                             )
    VALUES(@INVTIDSKU,
           @TAXISC,
           @TAXIGV,
           @CLASSID,
           @CLASSUBID,
           @INVTYPEID,
           @STKUNITID,
           @DESCR,
           @WTSTK1,
           @STKFUELS,
           @STKSTORE,
           @STKOTHER,
           @STKITEM,
           @KIT,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTO,
           @STKRECEPT,
           @STKSALES,
           @STATUS,
           @CONSIGNMENT,
           @DISASSEMBLE,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INVENTORY1: 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_INVENTORY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INVENTORY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INVENTORY1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INVENTORY1 
 */

CREATE PROCEDURE sp_UpdProcIN_INVENTORY1
(
    @INVTIDSKU       char(20),
    @TAXISC          char(2)                   = NULL,
    @TAXIGV          char(2),
    @CLASSID         char(5),
    @CLASSUBID       char(5),
    @INVTYPEID       char(5),
    @STKUNITID       char(5),
    @DESCR           varchar(40),
    @WTSTK1          numeric(7, 3),
    @STKFUELS        bit,
    @STKSTORE        bit,
    @STKOTHER        bit,
    @STKITEM         bit,
    @KIT             bit,
    @DISCDSCTO1      numeric(5, 2),
    @DISCDSCTO2      numeric(5, 2),
    @DISCMTO         numeric(12, 2),
    @STKRECEPT       bit,
    @STKSALES        bit,
    @STATUS          bit,
    @CONSIGNMENT     bit,
    @DISASSEMBLE     bit,
    @USER1           varchar(30)               = NULL,
    @USER2           varchar(30)               = NULL,
    @USER3           varchar(30)               = NULL,
    @USER4           smalldatetime             = NULL,
    @USER5           smalldatetime             = NULL,
    @USER6           float                     = NULL,
    @USER7           float                     = NULL,
    @USER8           smallint                  = NULL,
    @USER9           smallint                  = 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,
           DISCDSCTO1       = @DISCDSCTO1,
           DISCDSCTO2       = @DISCDSCTO2,
           DISCMTO          = @DISCMTO,
           STKRECEPT        = @STKRECEPT,
           STKSALES         = @STKSALES,
           STATUS           = @STATUS,
           CONSIGNMENT      = @CONSIGNMENT,
           DISASSEMBLE      = @DISASSEMBLE,
           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_INVENTORY1: Cannot update  in IN_INVENTORY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INVENTORY1 
 */

CREATE PROCEDURE sp_DelProcIN_INVENTORY1
(
    @INVTIDSKU       char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_INVENTORY
     WHERE INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_INVENTORY1: 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_INVENTORY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INVENTORY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INVENTORY1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INVENTORY1 
 */

CREATE PROCEDURE sp_SelProcIN_INVENTORY1
(
    @INVTIDSKU       char(20))
AS
BEGIN
    SELECT INVTIDSKU,
           TAXISC,
           TAXIGV,
           CLASSID,
           CLASSUBID,
           INVTYPEID,
           STKUNITID,
           DESCR,
           WTSTK1,
           STKFUELS,
           STKSTORE,
           STKOTHER,
           STKITEM,
           KIT,
           DISCDSCTO1,
           DISCDSCTO2,
           DISCMTO,
           STKRECEPT,
           STKSALES,
           STATUS,
           CONSIGNMENT,
           DISASSEMBLE,
           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_INVENTORY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcIN_INVENTORY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcIN_INVENTORY1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcIN_INVENTORYALTER1 
 */

CREATE PROCEDURE sp_InsProcIN_INVENTORYALTER1
(
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @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,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@INVTIDBARR,
           @INVTIDSKU,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INVENTORYALTER1: 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_INVENTORYALTER1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INVENTORYALTER1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INVENTORYALTER1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INVENTORYALTER1 
 */

CREATE PROCEDURE sp_UpdProcIN_INVENTORYALTER1
(
    @INVTIDBARR     UD_CODIGOBARRA,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @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,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE INVTIDBARR = @INVTIDBARR

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INVENTORYALTER1 
 */

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

    DELETE
      FROM IN_INVENTORYALTER
     WHERE INVTIDBARR = @INVTIDBARR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_INVENTORYALTER1: 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_INVENTORYALTER1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INVENTORYALTER1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INVENTORYALTER1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INVENTORYALTER1 
 */

CREATE PROCEDURE sp_SelProcIN_INVENTORYALTER1
(
    @INVTIDBARR     UD_CODIGOBARRA)
AS
BEGIN
    SELECT INVTIDBARR,
           INVTIDSKU,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYALTER
     WHERE INVTIDBARR = @INVTIDBARR

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


/* 
 * PROCEDURE: sp_InsProcIN_INVENTORYDAT1 
 */

CREATE PROCEDURE sp_InsProcIN_INVENTORYDAT1
(
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @STOCKSTAR          UD_STOCK,
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       UD_FECHA                    = NULL,
    @ENDDATEINV         UD_FECHA                    = NULL,
    @LASTINVFIS         UD_STOCK                    = NULL,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         UD_COSTO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @LASTSTDCOSTPEN     UD_COSTO,
    @LASTSTDCOSTUSD     UD_COSTO,
    @SLS_PRICE          UD_PRECIO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID                 = NULL,
    @INVTIDSTK          char(20),
    @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)
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,
                                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,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INVENTORYDAT1: 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_INVENTORYDAT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INVENTORYDAT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INVENTORYDAT1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INVENTORYDAT1 
 */

CREATE PROCEDURE sp_UpdProcIN_INVENTORYDAT1
(
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @STOCKSTAR          UD_STOCK,
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       UD_FECHA                    = NULL,
    @ENDDATEINV         UD_FECHA                    = NULL,
    @LASTINVFIS         UD_STOCK                    = NULL,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         UD_COSTO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @LASTSTDCOSTPEN     UD_COSTO,
    @LASTSTDCOSTUSD     UD_COSTO,
    @SLS_PRICE          UD_PRECIO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID                 = NULL,
    @INVTIDSTK          char(20),
    @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             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,
           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_INVENTORYDAT1: Cannot update  in IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INVENTORYDAT1 
 */

CREATE PROCEDURE sp_DelProcIN_INVENTORYDAT1
(
    @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_INVENTORYDAT1: 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_INVENTORYDAT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INVENTORYDAT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INVENTORYDAT1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INVENTORYDAT1 
 */

CREATE PROCEDURE sp_SelProcIN_INVENTORYDAT1
(
    @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,
           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_INVENTORYDAT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcIN_INVENTORYDAT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcIN_INVENTORYDAT1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcIN_INVENTORYDES1 
 */

CREATE PROCEDURE sp_InsProcIN_INVENTORYDES1
(
    @INVTIDSKU        UD_IDCORPORATIVO,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint,
    @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,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@INVTIDSKU,
           @INVTIDSKUDES,
           @WTSTK2,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INVENTORYDES1: 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_INVENTORYDES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INVENTORYDES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INVENTORYDES1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INVENTORYDES1 
 */

CREATE PROCEDURE sp_UpdProcIN_INVENTORYDES1
(
    @INVTIDSKU        UD_IDCORPORATIVO,
    @INVTIDSKUDES     UD_IDCORPORATIVO,
    @WTSTK2           tinyint,
    @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,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE INVTIDSKU = @INVTIDSKU

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INVENTORYDES1 
 */

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

    DELETE
      FROM IN_INVENTORYDES
     WHERE INVTIDSKU = @INVTIDSKU

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_INVENTORYDES1: 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_INVENTORYDES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INVENTORYDES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INVENTORYDES1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INVENTORYDES1 
 */

CREATE PROCEDURE sp_SelProcIN_INVENTORYDES1
(
    @INVTIDSKU        UD_IDCORPORATIVO)
AS
BEGIN
    SELECT INVTIDSKU,
           INVTIDSKUDES,
           WTSTK2,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYDES
     WHERE INVTIDSKU = @INVTIDSKU

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


/* 
 * PROCEDURE: sp_InsProcIN_INVENTORYTYPE1 
 */

CREATE PROCEDURE sp_InsProcIN_INVENTORYTYPE1
(
    @INVTYPEID     char(5),
    @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 IN_INVENTORYTYPE(INVTYPEID,
                                 DESCR,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@INVTYPEID,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INVENTORYTYPE1: 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_INVENTORYTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INVENTORYTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INVENTORYTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INVENTORYTYPE1 
 */

CREATE PROCEDURE sp_UpdProcIN_INVENTORYTYPE1
(
    @INVTYPEID     char(5),
    @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 IN_INVENTORYTYPE
       SET DESCR          = @DESCR,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4
     WHERE INVTYPEID = @INVTYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INVENTORYTYPE1 
 */

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

    DELETE
      FROM IN_INVENTORYTYPE
     WHERE INVTYPEID = @INVTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_INVENTORYTYPE1: 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_INVENTORYTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INVENTORYTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INVENTORYTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INVENTORYTYPE1 
 */

CREATE PROCEDURE sp_SelProcIN_INVENTORYTYPE1
(
    @INVTYPEID     char(5))
AS
BEGIN
    SELECT INVTYPEID,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYTYPE
     WHERE INVTYPEID = @INVTYPEID

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


/* 
 * PROCEDURE: sp_InsProcIN_INVENTORYUNITMED1 
 */

CREATE PROCEDURE sp_InsProcIN_INVENTORYUNITMED1
(
    @STKUNITID     UD_STKUNITID,
    @DESCR         UD_DESCRIPCION,
    @STKDIVIDE     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,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@STKUNITID,
           @DESCR,
           @STKDIVIDE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INVENTORYUNITMED1: 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_INVENTORYUNITMED1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INVENTORYUNITMED1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INVENTORYUNITMED1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INVENTORYUNITMED1 
 */

CREATE PROCEDURE sp_UpdProcIN_INVENTORYUNITMED1
(
    @STKUNITID     UD_STKUNITID,
    @DESCR         UD_DESCRIPCION,
    @STKDIVIDE     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,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4
     WHERE STKUNITID = @STKUNITID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INVENTORYUNITMED1 
 */

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

    DELETE
      FROM IN_INVENTORYUNITMED
     WHERE STKUNITID = @STKUNITID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_INVENTORYUNITMED1: 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_INVENTORYUNITMED1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INVENTORYUNITMED1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INVENTORYUNITMED1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INVENTORYUNITMED1 
 */

CREATE PROCEDURE sp_SelProcIN_INVENTORYUNITMED1
(
    @STKUNITID     UD_STKUNITID)
AS
BEGIN
    SELECT STKUNITID,
           DESCR,
           STKDIVIDE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_INVENTORYUNITMED
     WHERE STKUNITID = @STKUNITID

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


/* 
 * PROCEDURE: sp_InsProcIN_INVENTPHISICAL1 
 */

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

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_INVENTPHISICAL1: 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_INVENTPHISICAL1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_INVENTPHISICAL1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_INVENTPHISICAL1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_INVENTPHISICAL1 
 */

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

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

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_INVENTPHISICAL1 
 */

CREATE PROCEDURE sp_DelProcIN_INVENTPHISICAL1
(
    @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_INVENTPHISICAL1: 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_INVENTPHISICAL1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_INVENTPHISICAL1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_INVENTPHISICAL1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_INVENTPHISICAL1 
 */

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

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


/* 
 * PROCEDURE: sp_InsProcIN_KDXHIST1 
 */

CREATE PROCEDURE sp_InsProcIN_KDXHIST1
(
    @YEARHIST      smallint,
    @SITEID        UD_IDSITE,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @QTYEND00      UD_STOCK,
    @QTYEND01      UD_STOCK,
    @QTYEND02      UD_STOCK,
    @QTYEND03      UD_STOCK,
    @QTYEND04      UD_STOCK,
    @QTYEND05      UD_STOCK,
    @QTYEND06      UD_STOCK,
    @QTYEND07      UD_STOCK,
    @QTYEND08      UD_STOCK,
    @QTYEND09      UD_STOCK,
    @QTYEND10      UD_STOCK,
    @QTYEND11      UD_STOCK,
    @QTYEND12      UD_STOCK,
    @QTYEND13      UD_STOCK,
    @AVGPEN00      UD_COSTO,
    @AVGPEN01      UD_COSTO,
    @AVGPEN02      UD_COSTO,
    @AVGPEN03      UD_COSTO,
    @AVGPEN04      UD_COSTO,
    @AVGPEN06      UD_COSTO,
    @AVGPEN05      UD_COSTO,
    @AVGPEN07      UD_COSTO,
    @AVGPEN08      UD_COSTO,
    @AVGPEN09      UD_COSTO,
    @AVGPEN10      UD_COSTO,
    @AVGPEN11      UD_COSTO,
    @AVGPEN12      UD_COSTO,
    @AVGPEN13      UD_COSTO,
    @AVGUSD00      UD_COSTO,
    @AVGUSD01      UD_COSTO,
    @AVGUSD02      UD_COSTO,
    @AVGUSD03      UD_COSTO,
    @AVGUSD04      UD_COSTO,
    @AVGUSD05      UD_COSTO,
    @AVGUSD06      UD_COSTO,
    @AVGUSD07      UD_COSTO,
    @AVGUSD08      UD_COSTO,
    @AVGUSD09      UD_COSTO,
    @AVGUSD10      UD_COSTO,
    @AVGUSD11      UD_COSTO,
    @AVGUSD12      UD_COSTO,
    @AVGUSD13      UD_COSTO,
    @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,
                           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,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_KDXHIST1: 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_KDXHIST1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_KDXHIST1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_KDXHIST1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_KDXHIST1 
 */

CREATE PROCEDURE sp_UpdProcIN_KDXHIST1
(
    @YEARHIST      smallint,
    @SITEID        UD_IDSITE,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @QTYEND00      UD_STOCK,
    @QTYEND01      UD_STOCK,
    @QTYEND02      UD_STOCK,
    @QTYEND03      UD_STOCK,
    @QTYEND04      UD_STOCK,
    @QTYEND05      UD_STOCK,
    @QTYEND06      UD_STOCK,
    @QTYEND07      UD_STOCK,
    @QTYEND08      UD_STOCK,
    @QTYEND09      UD_STOCK,
    @QTYEND10      UD_STOCK,
    @QTYEND11      UD_STOCK,
    @QTYEND12      UD_STOCK,
    @QTYEND13      UD_STOCK,
    @AVGPEN00      UD_COSTO,
    @AVGPEN01      UD_COSTO,
    @AVGPEN02      UD_COSTO,
    @AVGPEN03      UD_COSTO,
    @AVGPEN04      UD_COSTO,
    @AVGPEN06      UD_COSTO,
    @AVGPEN05      UD_COSTO,
    @AVGPEN07      UD_COSTO,
    @AVGPEN08      UD_COSTO,
    @AVGPEN09      UD_COSTO,
    @AVGPEN10      UD_COSTO,
    @AVGPEN11      UD_COSTO,
    @AVGPEN12      UD_COSTO,
    @AVGPEN13      UD_COSTO,
    @AVGUSD00      UD_COSTO,
    @AVGUSD01      UD_COSTO,
    @AVGUSD02      UD_COSTO,
    @AVGUSD03      UD_COSTO,
    @AVGUSD04      UD_COSTO,
    @AVGUSD05      UD_COSTO,
    @AVGUSD06      UD_COSTO,
    @AVGUSD07      UD_COSTO,
    @AVGUSD08      UD_COSTO,
    @AVGUSD09      UD_COSTO,
    @AVGUSD10      UD_COSTO,
    @AVGUSD11      UD_COSTO,
    @AVGUSD12      UD_COSTO,
    @AVGUSD13      UD_COSTO,
    @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,
           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_KDXHIST1: Cannot update  in IN_KDXHIST '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_KDXHIST1 
 */

CREATE PROCEDURE sp_DelProcIN_KDXHIST1
(
    @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_KDXHIST1: 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_KDXHIST1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_KDXHIST1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_KDXHIST1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_KDXHIST1 
 */

CREATE PROCEDURE sp_SelProcIN_KDXHIST1
(
    @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,
           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_KDXHIST1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcIN_KDXHIST1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcIN_KDXHIST1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcIN_KIT1 
 */

CREATE PROCEDURE sp_InsProcIN_KIT1
(
    @KITID          UD_KITID,
    @STKUNITID      UD_STKUNITID,
    @CURYID         UD_CURYID,
    @TAXID          UD_TAXID,
    @DESCR          UD_DESCRIPCION,
    @MTOCTOREPS     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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,
                       USER1,
                       USER2,
                       USER3,
                       USER4 
                       )
    VALUES(@KITID,
           @STKUNITID,
           @CURYID,
           @TAXID,
           @DESCR,
           @MTOCTOREPS,
           @STDCOSTUSD,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_KIT1: 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_KIT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_KIT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_KIT1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_KIT1 
 */

CREATE PROCEDURE sp_UpdProcIN_KIT1
(
    @KITID          UD_KITID,
    @STKUNITID      UD_STKUNITID,
    @CURYID         UD_CURYID,
    @TAXID          UD_TAXID,
    @DESCR          UD_DESCRIPCION,
    @MTOCTOREPS     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE KITID = @KITID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_KIT1 
 */

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

    DELETE
      FROM IN_KIT
     WHERE KITID = @KITID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_KIT1: 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_KIT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_KIT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_KIT1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_KIT1 
 */

CREATE PROCEDURE sp_SelProcIN_KIT1
(
    @KITID          UD_KITID)
AS
BEGIN
    SELECT KITID,
           STKUNITID,
           CURYID,
           TAXID,
           DESCR,
           MTOCTOREPS,
           STDCOSTUSD,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_KIT
     WHERE KITID = @KITID

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


/* 
 * PROCEDURE: sp_InsProcIN_KITCOMPONENT1 
 */

CREATE PROCEDURE sp_InsProcIN_KITCOMPONENT1
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @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 IN_KITCOMPONENT(KITID,
                                INVTIDSKU,
                                SEQUENCE,
                                STDCOSTPEN,
                                STDCOSTUSD,
                                QTY,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@KITID,
           @INVTIDSKU,
           @SEQUENCE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @QTY,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_KITCOMPONENT1: 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_KITCOMPONENT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_KITCOMPONENT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_KITCOMPONENT1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_KITCOMPONENT1 
 */

CREATE PROCEDURE sp_UpdProcIN_KITCOMPONENT1
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO,
    @SEQUENCE       tinyint,
    @STDCOSTPEN     UD_COSTO,
    @STDCOSTUSD     UD_COSTO,
    @QTY            numeric(9, 3),
    @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,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE KITID     = @KITID
       AND INVTIDSKU = @INVTIDSKU

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_KITCOMPONENT1 
 */

CREATE PROCEDURE sp_DelProcIN_KITCOMPONENT1
(
    @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_KITCOMPONENT1: 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_KITCOMPONENT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_KITCOMPONENT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_KITCOMPONENT1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_KITCOMPONENT1 
 */

CREATE PROCEDURE sp_SelProcIN_KITCOMPONENT1
(
    @KITID          UD_KITID,
    @INVTIDSKU      UD_IDCORPORATIVO)
AS
BEGIN
    SELECT KITID,
           INVTIDSKU,
           SEQUENCE,
           STDCOSTPEN,
           STDCOSTUSD,
           QTY,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_KITCOMPONENT
     WHERE KITID     = @KITID
       AND INVTIDSKU = @INVTIDSKU

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


/* 
 * PROCEDURE: sp_InsProcIN_PRICELEVELTEM1 
 */

CREATE PROCEDURE sp_InsProcIN_PRICELEVELTEM1
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  char(5),
    @USERIDAUTORIZED01       char(20),
    @DATETIMEAUTORIZED01     smalldatetime,
    @USERIDAUTORIZED02       char(20),
    @DATETIMEAUTORIZED02     smalldatetime,
    @PRECLVID                char(5),
    @STKPRICESS              bit,
    @STARTDATEPRICE          smalldatetime,
    @ENDDATEPRICE            smalldatetime,
    @STARTTIMEPRICE          tinyint,
    @ENDTIMEPRICE            tinyint,
    @STATUS                  bit,
    @STKPRICESEND            bit,
    @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,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@BATNBRPRICELEVELTEM,
           @SITEID,
           @USERIDAUTORIZED01,
           @DATETIMEAUTORIZED01,
           @USERIDAUTORIZED02,
           @DATETIMEAUTORIZED02,
           @PRECLVID,
           @STKPRICESS,
           @STARTDATEPRICE,
           @ENDDATEPRICE,
           @STARTTIMEPRICE,
           @ENDTIMEPRICE,
           @STATUS,
           @STKPRICESEND,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_PRICELEVELTEM1: 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_PRICELEVELTEM1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_PRICELEVELTEM1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_PRICELEVELTEM1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_PRICELEVELTEM1 
 */

CREATE PROCEDURE sp_UpdProcIN_PRICELEVELTEM1
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  char(5),
    @USERIDAUTORIZED01       char(20),
    @DATETIMEAUTORIZED01     smalldatetime,
    @USERIDAUTORIZED02       char(20),
    @DATETIMEAUTORIZED02     smalldatetime,
    @PRECLVID                char(5),
    @STKPRICESS              bit,
    @STARTDATEPRICE          smalldatetime,
    @ENDDATEPRICE            smalldatetime,
    @STARTTIMEPRICE          tinyint,
    @ENDTIMEPRICE            tinyint,
    @STATUS                  bit,
    @STKPRICESEND            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_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,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE BATNBRPRICELEVELTEM = @BATNBRPRICELEVELTEM
       AND SITEID              = @SITEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_PRICELEVELTEM1 
 */

CREATE PROCEDURE sp_DelProcIN_PRICELEVELTEM1
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  char(5))
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_PRICELEVELTEM1: 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_PRICELEVELTEM1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_PRICELEVELTEM1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_PRICELEVELTEM1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_PRICELEVELTEM1 
 */

CREATE PROCEDURE sp_SelProcIN_PRICELEVELTEM1
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  char(5))
AS
BEGIN
    SELECT BATNBRPRICELEVELTEM,
           SITEID,
           USERIDAUTORIZED01,
           DATETIMEAUTORIZED01,
           USERIDAUTORIZED02,
           DATETIMEAUTORIZED02,
           PRECLVID,
           STKPRICESS,
           STARTDATEPRICE,
           ENDDATEPRICE,
           STARTTIMEPRICE,
           ENDTIMEPRICE,
           STATUS,
           STKPRICESEND,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_PRICELEVELTEM
     WHERE BATNBRPRICELEVELTEM = @BATNBRPRICELEVELTEM
       AND SITEID              = @SITEID

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


/* 
 * PROCEDURE: sp_InsProcIN_PRICELEVELTEMDET1 
 */

CREATE PROCEDURE sp_InsProcIN_PRICELEVELTEMDET1
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  char(5),
    @SEQUENCE                smallint,
    @INVTIDSKU               char(20)                  = NULL,
    @DESCR                   varchar(40),
    @SLS_PRICE               numeric(8, 4),
    @USER1                   varchar(30)               = NULL,
    @USER2                   varchar(30)               = NULL,
    @USER3                   float                     = NULL,
    @USER4                   float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_PRICELEVELTEMDET(BATNBRPRICELEVELTEM,
                                    SITEID,
                                    SEQUENCE,
                                    INVTIDSKU,
                                    DESCR,
                                    SLS_PRICE,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@BATNBRPRICELEVELTEM,
           @SITEID,
           @SEQUENCE,
           @INVTIDSKU,
           @DESCR,
           @SLS_PRICE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_PRICELEVELTEMDET1: 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_PRICELEVELTEMDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_PRICELEVELTEMDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_PRICELEVELTEMDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_PRICELEVELTEMDET1 
 */

CREATE PROCEDURE sp_UpdProcIN_PRICELEVELTEMDET1
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  char(5),
    @SEQUENCE                smallint,
    @INVTIDSKU               char(20)                  = NULL,
    @DESCR                   varchar(40),
    @SLS_PRICE               numeric(8, 4),
    @USER1                   varchar(30)               = NULL,
    @USER2                   varchar(30)               = NULL,
    @USER3                   float                     = NULL,
    @USER4                   float                     = NULL,
    @TSTAMP                  timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_PRICELEVELTEMDET
       SET INVTIDSKU                = @INVTIDSKU,
           DESCR                    = @DESCR,
           SLS_PRICE                = @SLS_PRICE,
           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_PRICELEVELTEMDET1: Cannot update  in IN_PRICELEVELTEMDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_PRICELEVELTEMDET1 
 */

CREATE PROCEDURE sp_DelProcIN_PRICELEVELTEMDET1
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  char(5),
    @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_PRICELEVELTEMDET1: 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_PRICELEVELTEMDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_PRICELEVELTEMDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_PRICELEVELTEMDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_PRICELEVELTEMDET1 
 */

CREATE PROCEDURE sp_SelProcIN_PRICELEVELTEMDET1
(
    @BATNBRPRICELEVELTEM     numeric(10, 0),
    @SITEID                  char(5),
    @SEQUENCE                smallint)
AS
BEGIN
    SELECT BATNBRPRICELEVELTEM,
           SITEID,
           SEQUENCE,
           INVTIDSKU,
           DESCR,
           SLS_PRICE,
           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_PRICELEVELTEMDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcIN_PRICELEVELTEMDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcIN_PRICELEVELTEMDET1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcIN_PRICELEVELTYPE1 
 */

CREATE PROCEDURE sp_InsProcIN_PRICELEVELTYPE1
(
    @PRECLVID       UD_PRECLVID,
    @CURYID         UD_CURYID,
    @CURYTYPEID     char(6),
    @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 IN_PRICELEVELTYPE(PRECLVID,
                                  CURYID,
                                  CURYTYPEID,
                                  DESCR,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@PRECLVID,
           @CURYID,
           @CURYTYPEID,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_PRICELEVELTYPE1: 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_PRICELEVELTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_PRICELEVELTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_PRICELEVELTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_PRICELEVELTYPE1 
 */

CREATE PROCEDURE sp_UpdProcIN_PRICELEVELTYPE1
(
    @PRECLVID       UD_PRECLVID,
    @CURYID         UD_CURYID,
    @CURYTYPEID     char(6),
    @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 IN_PRICELEVELTYPE
       SET CURYID          = @CURYID,
           CURYTYPEID      = @CURYTYPEID,
           DESCR           = @DESCR,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE PRECLVID = @PRECLVID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_PRICELEVELTYPE1 
 */

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

    DELETE
      FROM IN_PRICELEVELTYPE
     WHERE PRECLVID = @PRECLVID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_PRICELEVELTYPE1: 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_PRICELEVELTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_PRICELEVELTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_PRICELEVELTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_PRICELEVELTYPE1 
 */

CREATE PROCEDURE sp_SelProcIN_PRICELEVELTYPE1
(
    @PRECLVID       UD_PRECLVID)
AS
BEGIN
    SELECT PRECLVID,
           CURYID,
           CURYTYPEID,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_PRICELEVELTYPE
     WHERE PRECLVID = @PRECLVID

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


/* 
 * PROCEDURE: sp_InsProcIN_SUBCATEGORY1 
 */

CREATE PROCEDURE sp_InsProcIN_SUBCATEGORY1
(
    @CLASSUBID             char(5),
    @CLASSID               char(5),
    @DESCR                 varchar(40),
    @TYPEDOCFISIN          char(5),
    @TYPEDOCOUT            char(5),
    @PORCENTDIFFERENCE     numeric(5, 2),
    @USER1                 varchar(30)              = NULL,
    @USER2                 varchar(30)              = NULL,
    @USER3                 float                    = NULL,
    @USER4                 float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO IN_SUBCATEGORY(CLASSUBID,
                               CLASSID,
                               DESCR,
                               TYPEDOCFISIN,
                               TYPEDOCOUT,
                               PORCENTDIFFERENCE,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@CLASSUBID,
           @CLASSID,
           @DESCR,
           @TYPEDOCFISIN,
           @TYPEDOCOUT,
           @PORCENTDIFFERENCE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_SUBCATEGORY1: 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_SUBCATEGORY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_SUBCATEGORY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_SUBCATEGORY1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_SUBCATEGORY1 
 */

CREATE PROCEDURE sp_UpdProcIN_SUBCATEGORY1
(
    @CLASSUBID             char(5),
    @CLASSID               char(5),
    @DESCR                 varchar(40),
    @TYPEDOCFISIN          char(5),
    @TYPEDOCOUT            char(5),
    @PORCENTDIFFERENCE     numeric(5, 2),
    @USER1                 varchar(30)              = NULL,
    @USER2                 varchar(30)              = NULL,
    @USER3                 float                    = NULL,
    @USER4                 float                    = NULL,
    @TSTAMP                timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE IN_SUBCATEGORY
       SET DESCR                  = @DESCR,
           TYPEDOCFISIN           = @TYPEDOCFISIN,
           TYPEDOCOUT             = @TYPEDOCOUT,
           PORCENTDIFFERENCE      = @PORCENTDIFFERENCE,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE CLASSUBID = @CLASSUBID
       AND CLASSID   = @CLASSID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_SUBCATEGORY1 
 */

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

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_SUBCATEGORY1: 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_SUBCATEGORY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_SUBCATEGORY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_SUBCATEGORY1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_SUBCATEGORY1 
 */

CREATE PROCEDURE sp_SelProcIN_SUBCATEGORY1
(
    @CLASSUBID             char(5),
    @CLASSID               char(5))
AS
BEGIN
    SELECT CLASSUBID,
           CLASSID,
           DESCR,
           TYPEDOCFISIN,
           TYPEDOCOUT,
           PORCENTDIFFERENCE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_SUBCATEGORY
     WHERE CLASSUBID = @CLASSUBID
       AND CLASSID   = @CLASSID

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


/* 
 * PROCEDURE: sp_InsProcIN_TYPEDOC1 
 */

CREATE PROCEDURE sp_InsProcIN_TYPEDOC1
(
    @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),
    @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,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@TYPEDOCID,
           @DESCR,
           @INOUT,
           @STKVALUE,
           @STKCOSPRICE,
           @STKAVGCOST,
           @PRECLVLID,
           @STKBUY,
           @STKVENDOR,
           @STKAVERAGE,
           @DESCR_ABR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_TYPEDOC1: 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_TYPEDOC1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_TYPEDOC1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_TYPEDOC1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_TYPEDOC1 
 */

CREATE PROCEDURE sp_UpdProcIN_TYPEDOC1
(
    @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),
    @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,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE TYPEDOCID = @TYPEDOCID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_TYPEDOC1 
 */

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

    DELETE
      FROM IN_TYPEDOC
     WHERE TYPEDOCID = @TYPEDOCID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_TYPEDOC1: 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_TYPEDOC1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_TYPEDOC1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_TYPEDOC1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_TYPEDOC1 
 */

CREATE PROCEDURE sp_SelProcIN_TYPEDOC1
(
    @TYPEDOCID       UD_TYPEDOC)
AS
BEGIN
    SELECT TYPEDOCID,
           DESCR,
           INOUT,
           STKVALUE,
           STKCOSPRICE,
           STKAVGCOST,
           PRECLVLID,
           STKBUY,
           STKVENDOR,
           STKAVERAGE,
           DESCR_ABR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_TYPEDOC
     WHERE TYPEDOCID = @TYPEDOCID

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


/* 
 * PROCEDURE: sp_InsProcIN_VENDOR1 
 */

CREATE PROCEDURE sp_InsProcIN_VENDOR1
(
    @VENDIDSS          char(20),
    @CITYID            char(3),
    @COUNTRYID         char(3),
    @CLASSVENDORID     char(2),
    @TERMID            char(2),
    @VENDORID          char(20),
    @DESCR             varchar(40),
    @CONTACT           varchar(40)              = NULL,
    @TAXREGNBR         varchar(20)              = NULL,
    @ADDRES            varchar(40),
    @ADDRES2           varchar(40)              = NULL,
    @FONO1             char(10)                 = NULL,
    @FONO2             char(10)                 = NULL,
    @FAX               char(10)                 = NULL,
    @MAIL              varchar(40)              = NULL,
    @STATUS            char(1),
    @CURRBAL           numeric(9, 2),
    @CURYID            char(3),
    @CURYTYPEID        char(6),
    @TAXID             char(2),
    @USER1             varchar(30)              = NULL,
    @USER2             varchar(30)              = NULL,
    @USER3             float                    = NULL,
    @USER4             float                    = 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,
                          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,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_VENDOR1: 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_VENDOR1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_VENDOR1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_VENDOR1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcIN_VENDOR1 
 */

CREATE PROCEDURE sp_UpdProcIN_VENDOR1
(
    @VENDIDSS          char(20),
    @CITYID            char(3),
    @COUNTRYID         char(3),
    @CLASSVENDORID     char(2),
    @TERMID            char(2),
    @VENDORID          char(20),
    @DESCR             varchar(40),
    @CONTACT           varchar(40)              = NULL,
    @TAXREGNBR         varchar(20)              = NULL,
    @ADDRES            varchar(40),
    @ADDRES2           varchar(40)              = NULL,
    @FONO1             char(10)                 = NULL,
    @FONO2             char(10)                 = NULL,
    @FAX               char(10)                 = NULL,
    @MAIL              varchar(40)              = NULL,
    @STATUS            char(1),
    @CURRBAL           numeric(9, 2),
    @CURYID            char(3),
    @CURYTYPEID        char(6),
    @TAXID             char(2),
    @USER1             varchar(30)              = NULL,
    @USER2             varchar(30)              = NULL,
    @USER3             float                    = NULL,
    @USER4             float                    = 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,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE VENDIDSS = @VENDIDSS

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcIN_VENDOR1 
 */

CREATE PROCEDURE sp_DelProcIN_VENDOR1
(
    @VENDIDSS          char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM IN_VENDOR
     WHERE VENDIDSS = @VENDIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcIN_VENDOR1: 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_VENDOR1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcIN_VENDOR1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcIN_VENDOR1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcIN_VENDOR1 
 */

CREATE PROCEDURE sp_SelProcIN_VENDOR1
(
    @VENDIDSS          char(20))
AS
BEGIN
    SELECT VENDIDSS,
           CITYID,
           COUNTRYID,
           CLASSVENDORID,
           TERMID,
           VENDORID,
           DESCR,
           CONTACT,
           TAXREGNBR,
           ADDRES,
           ADDRES2,
           FONO1,
           FONO2,
           FAX,
           MAIL,
           STATUS,
           CURRBAL,
           CURYID,
           CURYTYPEID,
           TAXID,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM IN_VENDOR
     WHERE VENDIDSS = @VENDIDSS

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


/* 
 * PROCEDURE: sp_InsProcIN_VENDORINVT1 
 */

CREATE PROCEDURE sp_InsProcIN_VENDORINVT1
(
    @VENDIDSS      char(20),
    @INVTIDSKU     char(20),
    @SITEID        char(5),
    @BUYCOD        varchar(20),
    @DESCR         varchar(40),
    @STDCOST       numeric(8, 4),
    @CURYID        char(3),
    @BUYDATE       smalldatetime            = NULL,
    @USER1         varchar(30)              = NULL,
    @USER2         varchar(30)              = NULL,
    @USER3         float                    = NULL,
    @USER4         float                    = NULL)
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcIN_VENDORINVT1: 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_VENDORINVT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcIN_VENDORINVT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcIN_VENDORINVT1 >>>'
go





/* 
 * PROCEDURE: sp_InsProcINTRAMOV1 
 */

CREATE PROCEDURE sp_InsProcINTRAMOV1
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @BATNBR            char(10),
    @INTRDATE          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @TYPEDOCID         UD_TYPEDOC,
    @CURYRATE          UD_CAMBIO,
    @CURYDATE          smalldatetime,
    @PERPOST           UD_PERPOST,
    @SWTINOUT          bit,
    @QTY               UD_STOCK,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STDCOSTPEN        UD_COSTO                    = NULL,
    @COMPONENT         bit,
    @SLSPRICE          UD_PRECIO,
    @REFNBR            char(15)                    = NULL,
    @DOCTYPEID         UD_DOCTYPEID                = NULL,
    @STKFISI           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 INTRAMOV(SITEID,
                         INVTIDSKU,
                         BATNBR,
                         INTRDATE,
                         INTRDATEPROCE,
                         TYPEDOCID,
                         CURYRATE,
                         CURYDATE,
                         PERPOST,
                         SWTINOUT,
                         QTY,
                         STDCOSTUSD,
                         STDCOSTPEN,
                         COMPONENT,
                         SLSPRICE,
                         REFNBR,
                         DOCTYPEID,
                         STKFISI,
                         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,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcINTRAMOV1 
 */

CREATE PROCEDURE sp_UpdProcINTRAMOV1
(
    @SITEID            UD_IDSITE,
    @INVTIDSKU         UD_IDCORPORATIVO,
    @BATNBR            char(10),
    @INTRDATE          UD_FECHA,
    @INTRDATEPROCE     UD_FECHA,
    @TYPEDOCID         UD_TYPEDOC,
    @CURYRATE          UD_CAMBIO,
    @CURYDATE          smalldatetime,
    @PERPOST           UD_PERPOST,
    @SWTINOUT          bit,
    @QTY               UD_STOCK,
    @STDCOSTUSD        UD_COSTO                    = NULL,
    @STDCOSTPEN        UD_COSTO                    = NULL,
    @COMPONENT         bit,
    @SLSPRICE          UD_PRECIO,
    @REFNBR            char(15)                    = NULL,
    @DOCTYPEID         UD_DOCTYPEID                = NULL,
    @STKFISI           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 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,
           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_UpdProcINTRAMOV1: Cannot update  in INTRAMOV '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcINTRAMOV1 
 */

CREATE PROCEDURE sp_DelProcINTRAMOV1
(
    @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_DelProcINTRAMOV1: Cannot delete because foreign keys still exist in INTRAMOV '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcINTRAMOV1 
 */

CREATE PROCEDURE sp_SelProcINTRAMOV1
(
    @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,
           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_SelProcINTRAMOV1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcINTRAMOV1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcINTRAMOV1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcOP_CARA_FUEL1 
 */

CREATE PROCEDURE sp_InsProcOP_CARA_FUEL1
(
    @CARA           char(2),
    @INVTIDBARR     char(20)               = NULL,
    @DESCR          varchar(40),
    @USER1          varchar(30)            = NULL,
    @USER2          varchar(30)            = NULL,
    @USER3          float                  = NULL,
    @USER4          float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CARA_FUEL(CARA,
                             INVTIDBARR,
                             DESCR,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CARA,
           @INVTIDBARR,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcOP_CARA_FUEL1 
 */

CREATE PROCEDURE sp_UpdProcOP_CARA_FUEL1
(
    @CARA           char(2),
    @INVTIDBARR     char(20)               = NULL,
    @DESCR          varchar(40),
    @USER1          varchar(30)            = NULL,
    @USER2          varchar(30)            = NULL,
    @USER3          float                  = NULL,
    @USER4          float                  = NULL,
    @TSTAMP         timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CARA_FUEL
       SET INVTIDBARR      = @INVTIDBARR,
           DESCR           = @DESCR,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE CARA = @CARA

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CARA_FUEL1 
 */

CREATE PROCEDURE sp_DelProcOP_CARA_FUEL1
(
    @CARA           char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CARA_FUEL
     WHERE CARA = @CARA

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcOP_CARA_FUEL1 
 */

CREATE PROCEDURE sp_SelProcOP_CARA_FUEL1
(
    @CARA           char(2))
AS
BEGIN
    SELECT CARA,
           INVTIDBARR,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CARA_FUEL
     WHERE CARA = @CARA

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


/* 
 * PROCEDURE: sp_InsProcOP_CARD1 
 */

CREATE PROCEDURE sp_InsProcOP_CARD1
(
    @CARDID          char(2),
    @ACCOUNT         char(12),
    @DESCR           varchar(40),
    @NUMBERORDER     tinyint,
    @USER01          varchar(30)            = NULL,
    @USER02          varchar(30)            = NULL,
    @USER03          float                  = NULL,
    @USER04          float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CARD(CARDID,
                        ACCOUNT,
                        DESCR,
                        NUMBERORDER,
                        USER01,
                        USER02,
                        USER03,
                        USER04 
                        )
    VALUES(@CARDID,
           @ACCOUNT,
           @DESCR,
           @NUMBERORDER,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CARD1: 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_CARD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CARD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CARD1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CARD1 
 */

CREATE PROCEDURE sp_UpdProcOP_CARD1
(
    @CARDID          char(2),
    @ACCOUNT         char(12),
    @DESCR           varchar(40),
    @NUMBERORDER     tinyint,
    @USER01          varchar(30)            = NULL,
    @USER02          varchar(30)            = NULL,
    @USER03          float                  = NULL,
    @USER04          float                  = NULL,
    @TSTAMP          timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CARD
       SET ACCOUNT          = @ACCOUNT,
           DESCR            = @DESCR,
           NUMBERORDER      = @NUMBERORDER,
           USER01           = @USER01,
           USER02           = @USER02,
           USER03           = @USER03,
           USER04           = @USER04
     WHERE CARDID = @CARDID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CARD1 
 */

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

    DELETE
      FROM OP_CARD
     WHERE CARDID = @CARDID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CARD1: 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_CARD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CARD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CARD1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CARD1 
 */

CREATE PROCEDURE sp_SelProcOP_CARD1
(
    @CARDID          char(2))
AS
BEGIN
    SELECT CARDID,
           ACCOUNT,
           DESCR,
           NUMBERORDER,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_CARD
     WHERE CARDID = @CARDID

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


/* 
 * PROCEDURE: sp_InsProcOP_CARD_MONT1 
 */

CREATE PROCEDURE sp_InsProcOP_CARD_MONT1
(
    @SITEID        char(5),
    @CARDID        char(2),
    @DATE          smalldatetime,
    @DATEPROCE     smalldatetime,
    @CURYID        char(3),
    @TOTALCARD     numeric(9, 2),
    @STATUS        bit                      = NULL,
    @USER1         varchar(30)              = NULL,
    @USER2         varchar(30)              = NULL,
    @USER3         float                    = NULL,
    @USER4         float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CARD_MONT(SITEID,
                             CARDID,
                             DATE,
                             DATEPROCE,
                             CURYID,
                             TOTALCARD,
                             STATUS,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@SITEID,
           @CARDID,
           @DATE,
           @DATEPROCE,
           @CURYID,
           @TOTALCARD,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CARD_MONT1: 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_MONT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CARD_MONT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CARD_MONT1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CARD_MONT1 
 */

CREATE PROCEDURE sp_UpdProcOP_CARD_MONT1
(
    @SITEID        char(5),
    @CARDID        char(2),
    @DATE          smalldatetime,
    @DATEPROCE     smalldatetime,
    @CURYID        char(3),
    @TOTALCARD     numeric(9, 2),
    @STATUS        bit                      = NULL,
    @USER1         varchar(30)              = NULL,
    @USER2         varchar(30)              = NULL,
    @USER3         float                    = NULL,
    @USER4         float                    = NULL,
    @TSTAMP        timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CARD_MONT
       SET DATEPROCE      = @DATEPROCE,
           CURYID         = @CURYID,
           TOTALCARD      = @TOTALCARD,
           STATUS         = @STATUS,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4
     WHERE SITEID = @SITEID
       AND CARDID = @CARDID
       AND DATE   = @DATE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CARD_MONT1 
 */

CREATE PROCEDURE sp_DelProcOP_CARD_MONT1
(
    @SITEID        char(5),
    @CARDID        char(2),
    @DATE          smalldatetime)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CARD_MONT
     WHERE SITEID = @SITEID
       AND CARDID = @CARDID
       AND DATE   = @DATE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CARD_MONT1: 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_MONT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CARD_MONT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CARD_MONT1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CARD_MONT1 
 */

CREATE PROCEDURE sp_SelProcOP_CARD_MONT1
(
    @SITEID        char(5),
    @CARDID        char(2),
    @DATE          smalldatetime)
AS
BEGIN
    SELECT SITEID,
           CARDID,
           DATE,
           DATEPROCE,
           CURYID,
           TOTALCARD,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CARD_MONT
     WHERE SITEID = @SITEID
       AND CARDID = @CARDID
       AND DATE   = @DATE

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


/* 
 * PROCEDURE: sp_InsProcOP_CCSALESPERSON1 
 */

CREATE PROCEDURE sp_InsProcOP_CCSALESPERSON1
(
    @SITEID         char(5),
    @SALESPERID     char(10),
    @CURYID         char(3),
    @TOTAL          numeric(9, 2),
    @USER1          varchar(30)              = NULL,
    @USER2          varchar(30)              = NULL,
    @USER3          float                    = NULL,
    @USER4          float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CCSALESPERSON(SITEID,
                                 SALESPERID,
                                 CURYID,
                                 TOTAL,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @SALESPERID,
           @CURYID,
           @TOTAL,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CCSALESPERSON1: 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_CCSALESPERSON1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CCSALESPERSON1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CCSALESPERSON1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CCSALESPERSON1 
 */

CREATE PROCEDURE sp_UpdProcOP_CCSALESPERSON1
(
    @SITEID         char(5),
    @SALESPERID     char(10),
    @CURYID         char(3),
    @TOTAL          numeric(9, 2),
    @USER1          varchar(30)              = NULL,
    @USER2          varchar(30)              = NULL,
    @USER3          float                    = NULL,
    @USER4          float                    = NULL,
    @TSTAMP         timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CCSALESPERSON
       SET CURYID          = @CURYID,
           TOTAL           = @TOTAL,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CCSALESPERSON1 
 */

CREATE PROCEDURE sp_DelProcOP_CCSALESPERSON1
(
    @SITEID         char(5),
    @SALESPERID     char(10))
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CCSALESPERSON1: 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_CCSALESPERSON1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CCSALESPERSON1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CCSALESPERSON1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CCSALESPERSON1 
 */

CREATE PROCEDURE sp_SelProcOP_CCSALESPERSON1
(
    @SITEID         char(5),
    @SALESPERID     char(10))
AS
BEGIN
    SELECT SITEID,
           SALESPERID,
           CURYID,
           TOTAL,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCSALESPERSON
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID

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


/* 
 * PROCEDURE: sp_InsProcOP_CCSALESPERSONDET1 
 */

CREATE PROCEDURE sp_InsProcOP_CCSALESPERSONDET1
(
    @SITEID                  char(5),
    @SALESPERID              char(10),
    @SEQUENCE                smallint,
    @CCSALESPERSONTYPEID     char(2),
    @DATEPROCE               smalldatetime,
    @OBS                     char(100)                = NULL,
    @USER1                   varchar(30)              = NULL,
    @USER2                   varchar(30)              = NULL,
    @USER3                   float                    = NULL,
    @USER4                   float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CCSALESPERSONDET(SITEID,
                                    SALESPERID,
                                    SEQUENCE,
                                    CCSALESPERSONTYPEID,
                                    DATEPROCE,
                                    OBS,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4 
                                    )
    VALUES(@SITEID,
           @SALESPERID,
           @SEQUENCE,
           @CCSALESPERSONTYPEID,
           @DATEPROCE,
           @OBS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CCSALESPERSONDET1: 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_CCSALESPERSONDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CCSALESPERSONDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CCSALESPERSONDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CCSALESPERSONDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_CCSALESPERSONDET1
(
    @SITEID                  char(5),
    @SALESPERID              char(10),
    @SEQUENCE                smallint,
    @CCSALESPERSONTYPEID     char(2),
    @DATEPROCE               smalldatetime,
    @OBS                     char(100)                = NULL,
    @USER1                   varchar(30)              = NULL,
    @USER2                   varchar(30)              = NULL,
    @USER3                   float                    = NULL,
    @USER4                   float                    = NULL,
    @TSTAMP                  timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CCSALESPERSONDET
       SET CCSALESPERSONTYPEID      = @CCSALESPERSONTYPEID,
           DATEPROCE                = @DATEPROCE,
           OBS                      = @OBS,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID
       AND SEQUENCE   = @SEQUENCE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CCSALESPERSONDET1 
 */

CREATE PROCEDURE sp_DelProcOP_CCSALESPERSONDET1
(
    @SITEID                  char(5),
    @SALESPERID              char(10),
    @SEQUENCE                smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CCSALESPERSONDET
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID
       AND SEQUENCE   = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CCSALESPERSONDET1: 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_CCSALESPERSONDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CCSALESPERSONDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CCSALESPERSONDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CCSALESPERSONDET1 
 */

CREATE PROCEDURE sp_SelProcOP_CCSALESPERSONDET1
(
    @SITEID                  char(5),
    @SALESPERID              char(10),
    @SEQUENCE                smallint)
AS
BEGIN
    SELECT SITEID,
           SALESPERID,
           SEQUENCE,
           CCSALESPERSONTYPEID,
           DATEPROCE,
           OBS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCSALESPERSONDET
     WHERE SITEID     = @SITEID
       AND SALESPERID = @SALESPERID
       AND SEQUENCE   = @SEQUENCE

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


/* 
 * PROCEDURE: sp_InsProcOP_CCSALESPERSONTYPE1 
 */

CREATE PROCEDURE sp_InsProcOP_CCSALESPERSONTYPE1
(
    @CCSALESPERSONTYPEID        char(2),
    @DESCRCCSALESPERSONTYPE     varchar(40),
    @DEBE                       char(1),
    @HABER                      char(1),
    @USER1                      varchar(30)            = NULL,
    @USER2                      varchar(30)            = NULL,
    @USER3                      float                  = NULL,
    @USER4                      float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CCSALESPERSONTYPE(CCSALESPERSONTYPEID,
                                     DESCRCCSALESPERSONTYPE,
                                     DEBE,
                                     HABER,
                                     USER1,
                                     USER2,
                                     USER3,
                                     USER4 
                                     )
    VALUES(@CCSALESPERSONTYPEID,
           @DESCRCCSALESPERSONTYPE,
           @DEBE,
           @HABER,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CCSALESPERSONTYPE1: 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_CCSALESPERSONTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CCSALESPERSONTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CCSALESPERSONTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CCSALESPERSONTYPE1 
 */

CREATE PROCEDURE sp_UpdProcOP_CCSALESPERSONTYPE1
(
    @CCSALESPERSONTYPEID        char(2),
    @DESCRCCSALESPERSONTYPE     varchar(40),
    @DEBE                       char(1),
    @HABER                      char(1),
    @USER1                      varchar(30)            = NULL,
    @USER2                      varchar(30)            = NULL,
    @USER3                      float                  = NULL,
    @USER4                      float                  = NULL,
    @TSTAMP                     timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CCSALESPERSONTYPE
       SET DESCRCCSALESPERSONTYPE      = @DESCRCCSALESPERSONTYPE,
           DEBE                        = @DEBE,
           HABER                       = @HABER,
           USER1                       = @USER1,
           USER2                       = @USER2,
           USER3                       = @USER3,
           USER4                       = @USER4
     WHERE CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CCSALESPERSONTYPE1 
 */

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

    DELETE
      FROM OP_CCSALESPERSONTYPE
     WHERE CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CCSALESPERSONTYPE1: 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_CCSALESPERSONTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CCSALESPERSONTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CCSALESPERSONTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CCSALESPERSONTYPE1 
 */

CREATE PROCEDURE sp_SelProcOP_CCSALESPERSONTYPE1
(
    @CCSALESPERSONTYPEID        char(2))
AS
BEGIN
    SELECT CCSALESPERSONTYPEID,
           DESCRCCSALESPERSONTYPE,
           DEBE,
           HABER,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CCSALESPERSONTYPE
     WHERE CCSALESPERSONTYPEID = @CCSALESPERSONTYPEID

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


/* 
 * PROCEDURE: sp_InsProcOP_CORELATIVE1 
 */

CREATE PROCEDURE sp_InsProcOP_CORELATIVE1
(
    @TYPECORELATIVE     char(20),
    @NBRCORELATIVE      numeric(10, 0),
    @USER01             varchar(30)               = NULL,
    @USER02             varchar(30)               = NULL,
    @USER03             float                     = NULL,
    @USER04             float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CORELATIVE(TYPECORELATIVE,
                              NBRCORELATIVE,
                              USER01,
                              USER02,
                              USER03,
                              USER04 
                              )
    VALUES(@TYPECORELATIVE,
           @NBRCORELATIVE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CORELATIVE1: 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_CORELATIVE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CORELATIVE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CORELATIVE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CORELATIVE1 
 */

CREATE PROCEDURE sp_UpdProcOP_CORELATIVE1
(
    @TYPECORELATIVE     char(20),
    @NBRCORELATIVE      numeric(10, 0),
    @USER01             varchar(30)               = NULL,
    @USER02             varchar(30)               = NULL,
    @USER03             float                     = NULL,
    @USER04             float                     = NULL,
    @TSTAMP             timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CORELATIVE
       SET NBRCORELATIVE       = @NBRCORELATIVE,
           USER01              = @USER01,
           USER02              = @USER02,
           USER03              = @USER03,
           USER04              = @USER04
     WHERE TYPECORELATIVE = @TYPECORELATIVE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CORELATIVE1 
 */

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

    DELETE
      FROM OP_CORELATIVE
     WHERE TYPECORELATIVE = @TYPECORELATIVE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CORELATIVE1: 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_CORELATIVE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CORELATIVE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CORELATIVE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CORELATIVE1 
 */

CREATE PROCEDURE sp_SelProcOP_CORELATIVE1
(
    @TYPECORELATIVE     char(20))
AS
BEGIN
    SELECT TYPECORELATIVE,
           NBRCORELATIVE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_CORELATIVE
     WHERE TYPECORELATIVE = @TYPECORELATIVE

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


/* 
 * PROCEDURE: sp_InsProcOP_CTRLFUEL1 
 */

CREATE PROCEDURE sp_InsProcOP_CTRLFUEL1
(
    @SITEID               char(5),
    @BATNBRCTRLFUEL       numeric(10, 0),
    @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),
    @STKUNITID            char(5),
    @VALUEUNIMED          numeric(7, 4),
    @QTYDIFFERENCE        numeric(10, 3),
    @PORCENTDIFERENCE     numeric(5, 2),
    @REMARK               varchar(40),
    @STATUS               bit                       = NULL,
    @USER1                varchar(30)               = NULL,
    @USER2                varchar(30)               = NULL,
    @USER3                float                     = NULL,
    @USER4                float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLFUEL(SITEID,
                            BATNBRCTRLFUEL,
                            DATEPROCE,
                            INVTIDSKU,
                            STOCKINITIAL,
                            QTYIMPUT,
                            QTYOUTPUT,
                            QTYSALES,
                            QTYAVAIL,
                            STKUNITID,
                            VALUEUNIMED,
                            QTYDIFFERENCE,
                            PORCENTDIFERENCE,
                            REMARK,
                            STATUS,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@SITEID,
           @BATNBRCTRLFUEL,
           @DATEPROCE,
           @INVTIDSKU,
           @STOCKINITIAL,
           @QTYIMPUT,
           @QTYOUTPUT,
           @QTYSALES,
           @QTYAVAIL,
           @STKUNITID,
           @VALUEUNIMED,
           @QTYDIFFERENCE,
           @PORCENTDIFERENCE,
           @REMARK,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CTRLFUEL1: 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_CTRLFUEL1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CTRLFUEL1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CTRLFUEL1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CTRLFUEL1 
 */

CREATE PROCEDURE sp_UpdProcOP_CTRLFUEL1
(
    @SITEID               char(5),
    @BATNBRCTRLFUEL       numeric(10, 0),
    @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),
    @STKUNITID            char(5),
    @VALUEUNIMED          numeric(7, 4),
    @QTYDIFFERENCE        numeric(10, 3),
    @PORCENTDIFERENCE     numeric(5, 2),
    @REMARK               varchar(40),
    @STATUS               bit                       = NULL,
    @USER1                varchar(30)               = NULL,
    @USER2                varchar(30)               = NULL,
    @USER3                float                     = NULL,
    @USER4                float                     = NULL,
    @TSTAMP               timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLFUEL
       SET DATEPROCE             = @DATEPROCE,
           INVTIDSKU             = @INVTIDSKU,
           STOCKINITIAL          = @STOCKINITIAL,
           QTYIMPUT              = @QTYIMPUT,
           QTYOUTPUT             = @QTYOUTPUT,
           QTYSALES              = @QTYSALES,
           QTYAVAIL              = @QTYAVAIL,
           STKUNITID             = @STKUNITID,
           VALUEUNIMED           = @VALUEUNIMED,
           QTYDIFFERENCE         = @QTYDIFFERENCE,
           PORCENTDIFERENCE      = @PORCENTDIFERENCE,
           REMARK                = @REMARK,
           STATUS                = @STATUS,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CTRLFUEL1 
 */

CREATE PROCEDURE sp_DelProcOP_CTRLFUEL1
(
    @SITEID               char(5),
    @BATNBRCTRLFUEL       numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CTRLFUEL1: 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_CTRLFUEL1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CTRLFUEL1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CTRLFUEL1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CTRLFUEL1 
 */

CREATE PROCEDURE sp_SelProcOP_CTRLFUEL1
(
    @SITEID               char(5),
    @BATNBRCTRLFUEL       numeric(10, 0))
AS
BEGIN
    SELECT SITEID,
           BATNBRCTRLFUEL,
           DATEPROCE,
           INVTIDSKU,
           STOCKINITIAL,
           QTYIMPUT,
           QTYOUTPUT,
           QTYSALES,
           QTYAVAIL,
           STKUNITID,
           VALUEUNIMED,
           QTYDIFFERENCE,
           PORCENTDIFERENCE,
           REMARK,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLFUEL
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL

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


/* 
 * PROCEDURE: sp_InsProcOP_CTRLFUELDET1 
 */

CREATE PROCEDURE sp_InsProcOP_CTRLFUELDET1
(
    @SITEID             char(5),
    @BATNBRCTRLFUEL     numeric(10, 0),
    @SEQUENCE           tinyint,
    @CURYID             char(3),
    @QTYSALES           numeric(10, 3),
    @SLS_PRICE          numeric(8, 4),
    @STATUS             bit                       = NULL,
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              float                     = NULL,
    @USER4              float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLFUELDET(SITEID,
                               BATNBRCTRLFUEL,
                               SEQUENCE,
                               CURYID,
                               QTYSALES,
                               SLS_PRICE,
                               STATUS,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@SITEID,
           @BATNBRCTRLFUEL,
           @SEQUENCE,
           @CURYID,
           @QTYSALES,
           @SLS_PRICE,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CTRLFUELDET1: 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_CTRLFUELDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CTRLFUELDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CTRLFUELDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CTRLFUELDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_CTRLFUELDET1
(
    @SITEID             char(5),
    @BATNBRCTRLFUEL     numeric(10, 0),
    @SEQUENCE           tinyint,
    @CURYID             char(3),
    @QTYSALES           numeric(10, 3),
    @SLS_PRICE          numeric(8, 4),
    @STATUS             bit                       = NULL,
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              float                     = NULL,
    @USER4              float                     = NULL,
    @TSTAMP             timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLFUELDET
       SET CURYID              = @CURYID,
           QTYSALES            = @QTYSALES,
           SLS_PRICE           = @SLS_PRICE,
           STATUS              = @STATUS,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL
       AND SEQUENCE       = @SEQUENCE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CTRLFUELDET1 
 */

CREATE PROCEDURE sp_DelProcOP_CTRLFUELDET1
(
    @SITEID             char(5),
    @BATNBRCTRLFUEL     numeric(10, 0),
    @SEQUENCE           tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLFUELDET
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL
       AND SEQUENCE       = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CTRLFUELDET1: 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_CTRLFUELDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CTRLFUELDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CTRLFUELDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CTRLFUELDET1 
 */

CREATE PROCEDURE sp_SelProcOP_CTRLFUELDET1
(
    @SITEID             char(5),
    @BATNBRCTRLFUEL     numeric(10, 0),
    @SEQUENCE           tinyint)
AS
BEGIN
    SELECT SITEID,
           BATNBRCTRLFUEL,
           SEQUENCE,
           CURYID,
           QTYSALES,
           SLS_PRICE,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLFUELDET
     WHERE SITEID         = @SITEID
       AND BATNBRCTRLFUEL = @BATNBRCTRLFUEL
       AND SEQUENCE       = @SEQUENCE

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


/* 
 * PROCEDURE: sp_InsProcOP_CTRLTURN1 
 */

CREATE PROCEDURE sp_InsProcOP_CTRLTURN1
(
    @BATNBRTURN     numeric(10, 0),
    @SALESPERID     char(10),
    @SITEID         char(5),
    @DATEPROCES     smalldatetime,
    @NBRTUR         char(1),
    @USER01         varchar(30)               = NULL,
    @USER02         varchar(30)               = NULL,
    @USER03         float                     = NULL,
    @USER04         float                     = NULL)
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CTRLTURN1: 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_CTRLTURN1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CTRLTURN1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CTRLTURN1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CTRLTURN1 
 */

CREATE PROCEDURE sp_UpdProcOP_CTRLTURN1
(
    @BATNBRTURN     numeric(10, 0),
    @SALESPERID     char(10),
    @SITEID         char(5),
    @DATEPROCES     smalldatetime,
    @NBRTUR         char(1),
    @USER01         varchar(30)               = NULL,
    @USER02         varchar(30)               = NULL,
    @USER03         float                     = NULL,
    @USER04         float                     = NULL,
    @TSTAMP         timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLTURN
       SET SALESPERID      = @SALESPERID,
           SITEID          = @SITEID,
           DATEPROCES      = @DATEPROCES,
           NBRTUR          = @NBRTUR,
           USER01          = @USER01,
           USER02          = @USER02,
           USER03          = @USER03,
           USER04          = @USER04
     WHERE BATNBRTURN = @BATNBRTURN

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CTRLTURN1 
 */

CREATE PROCEDURE sp_DelProcOP_CTRLTURN1
(
    @BATNBRTURN     numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLTURN
     WHERE BATNBRTURN = @BATNBRTURN

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CTRLTURN1: 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_CTRLTURN1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CTRLTURN1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CTRLTURN1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CTRLTURN1 
 */

CREATE PROCEDURE sp_SelProcOP_CTRLTURN1
(
    @BATNBRTURN     numeric(10, 0))
AS
BEGIN
    SELECT BATNBRTURN,
           SALESPERID,
           SITEID,
           DATEPROCES,
           NBRTUR,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_CTRLTURN
     WHERE BATNBRTURN = @BATNBRTURN

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


/* 
 * PROCEDURE: sp_InsProcOP_CTRLTURNDET1 
 */

CREATE PROCEDURE sp_InsProcOP_CTRLTURNDET1
(
    @BATNBRTURN       numeric(10, 0),
    @CARA             char(2),
    @INVTIDSKU        char(20),
    @STARTREADING     numeric(13, 3),
    @ENDREADING       numeric(13, 3),
    @QTYSALES         numeric(10, 3),
    @QTYSERAF         numeric(10, 3),
    @QTYOTHER         numeric(10, 3),
    @TOTQTY           numeric(10, 3),
    @USER1            varchar(30)               = NULL,
    @USER2            varchar(30)               = NULL,
    @USER3            float                     = NULL,
    @USER4            float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CTRLTURNDET(BATNBRTURN,
                               CARA,
                               INVTIDSKU,
                               STARTREADING,
                               ENDREADING,
                               QTYSALES,
                               QTYSERAF,
                               QTYOTHER,
                               TOTQTY,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@BATNBRTURN,
           @CARA,
           @INVTIDSKU,
           @STARTREADING,
           @ENDREADING,
           @QTYSALES,
           @QTYSERAF,
           @QTYOTHER,
           @TOTQTY,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CTRLTURNDET1: 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_CTRLTURNDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CTRLTURNDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CTRLTURNDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CTRLTURNDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_CTRLTURNDET1
(
    @BATNBRTURN       numeric(10, 0),
    @CARA             char(2),
    @INVTIDSKU        char(20),
    @STARTREADING     numeric(13, 3),
    @ENDREADING       numeric(13, 3),
    @QTYSALES         numeric(10, 3),
    @QTYSERAF         numeric(10, 3),
    @QTYOTHER         numeric(10, 3),
    @TOTQTY           numeric(10, 3),
    @USER1            varchar(30)               = NULL,
    @USER2            varchar(30)               = NULL,
    @USER3            float                     = NULL,
    @USER4            float                     = NULL,
    @TSTAMP           timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CTRLTURNDET
       SET CARA              = @CARA,
           INVTIDSKU         = @INVTIDSKU,
           STARTREADING      = @STARTREADING,
           ENDREADING        = @ENDREADING,
           QTYSALES          = @QTYSALES,
           QTYSERAF          = @QTYSERAF,
           QTYOTHER          = @QTYOTHER,
           TOTQTY            = @TOTQTY,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE BATNBRTURN = @BATNBRTURN

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CTRLTURNDET1 
 */

CREATE PROCEDURE sp_DelProcOP_CTRLTURNDET1
(
    @BATNBRTURN       numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CTRLTURNDET
     WHERE BATNBRTURN = @BATNBRTURN

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CTRLTURNDET1: 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_CTRLTURNDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CTRLTURNDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CTRLTURNDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CTRLTURNDET1 
 */

CREATE PROCEDURE sp_SelProcOP_CTRLTURNDET1
(
    @BATNBRTURN       numeric(10, 0))
AS
BEGIN
    SELECT BATNBRTURN,
           CARA,
           INVTIDSKU,
           STARTREADING,
           ENDREADING,
           QTYSALES,
           QTYSERAF,
           QTYOTHER,
           TOTQTY,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CTRLTURNDET
     WHERE BATNBRTURN = @BATNBRTURN

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


/* 
 * PROCEDURE: sp_InsProcOP_CUSTCARD1 
 */

CREATE PROCEDURE sp_InsProcOP_CUSTCARD1
(
    @CUSTIDSS          char(20),
    @EXPIREDATE        smalldatetime,
    @DESCRCUSTCARD     varchar(40),
    @CARDPASSWORD      char(10),
    @USER1             varchar(30)              = NULL,
    @USER2             varchar(30)              = NULL,
    @USER3             float                    = NULL,
    @USER4             float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUSTCARD(CUSTIDSS,
                            EXPIREDATE,
                            DESCRCUSTCARD,
                            CARDPASSWORD,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CUSTIDSS,
           @EXPIREDATE,
           @DESCRCUSTCARD,
           @CARDPASSWORD,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CUSTCARD1: 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_CUSTCARD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CUSTCARD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CUSTCARD1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CUSTCARD1 
 */

CREATE PROCEDURE sp_UpdProcOP_CUSTCARD1
(
    @CUSTIDSS          char(20),
    @EXPIREDATE        smalldatetime,
    @DESCRCUSTCARD     varchar(40),
    @CARDPASSWORD      char(10),
    @USER1             varchar(30)              = NULL,
    @USER2             varchar(30)              = NULL,
    @USER3             float                    = NULL,
    @USER4             float                    = NULL,
    @TSTAMP            timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUSTCARD
       SET EXPIREDATE         = @EXPIREDATE,
           DESCRCUSTCARD      = @DESCRCUSTCARD,
           CARDPASSWORD       = @CARDPASSWORD,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE CUSTIDSS = @CUSTIDSS

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CUSTCARD1 
 */

CREATE PROCEDURE sp_DelProcOP_CUSTCARD1
(
    @CUSTIDSS          char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUSTCARD
     WHERE CUSTIDSS = @CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CUSTCARD1: 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_CUSTCARD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CUSTCARD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CUSTCARD1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CUSTCARD1 
 */

CREATE PROCEDURE sp_SelProcOP_CUSTCARD1
(
    @CUSTIDSS          char(20))
AS
BEGIN
    SELECT CUSTIDSS,
           EXPIREDATE,
           DESCRCUSTCARD,
           CARDPASSWORD,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUSTCARD
     WHERE CUSTIDSS = @CUSTIDSS

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


/* 
 * PROCEDURE: sp_InsProcOP_CUSTCLASS1 
 */

CREATE PROCEDURE sp_InsProcOP_CUSTCLASS1
(
    @CUSTCLASSID     char(2),
    @DESCR           varchar(40),
    @USER1           varchar(30)            = NULL,
    @USER2           varchar(30)            = NULL,
    @USER3           float                  = NULL,
    @USER4           float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUSTCLASS(CUSTCLASSID,
                             DESCR,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@CUSTCLASSID,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CUSTCLASS1: 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_CUSTCLASS1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CUSTCLASS1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CUSTCLASS1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CUSTCLASS1 
 */

CREATE PROCEDURE sp_UpdProcOP_CUSTCLASS1
(
    @CUSTCLASSID     char(2),
    @DESCR           varchar(40),
    @USER1           varchar(30)            = NULL,
    @USER2           varchar(30)            = NULL,
    @USER3           float                  = NULL,
    @USER4           float                  = NULL,
    @TSTAMP          timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUSTCLASS
       SET DESCR            = @DESCR,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE CUSTCLASSID = @CUSTCLASSID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CUSTCLASS1 
 */

CREATE PROCEDURE sp_DelProcOP_CUSTCLASS1
(
    @CUSTCLASSID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUSTCLASS
     WHERE CUSTCLASSID = @CUSTCLASSID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CUSTCLASS1: 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_CUSTCLASS1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CUSTCLASS1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CUSTCLASS1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CUSTCLASS1 
 */

CREATE PROCEDURE sp_SelProcOP_CUSTCLASS1
(
    @CUSTCLASSID     char(2))
AS
BEGIN
    SELECT CUSTCLASSID,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUSTCLASS
     WHERE CUSTCLASSID = @CUSTCLASSID

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


/* 
 * PROCEDURE: sp_InsProcOP_CUSTZONE1 
 */

CREATE PROCEDURE sp_InsProcOP_CUSTZONE1
(
    @CUSTZONEID     char(2),
    @DESCR          varchar(40),
    @USER1          varchar(30)            = NULL,
    @USER2          varchar(30)            = NULL,
    @USER3          float                  = NULL,
    @USER4          float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_CUSTZONE(CUSTZONEID,
                            DESCR,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@CUSTZONEID,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_CUSTZONE1: 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_CUSTZONE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_CUSTZONE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_CUSTZONE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_CUSTZONE1 
 */

CREATE PROCEDURE sp_UpdProcOP_CUSTZONE1
(
    @CUSTZONEID     char(2),
    @DESCR          varchar(40),
    @USER1          varchar(30)            = NULL,
    @USER2          varchar(30)            = NULL,
    @USER3          float                  = NULL,
    @USER4          float                  = NULL,
    @TSTAMP         timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_CUSTZONE
       SET DESCR           = @DESCR,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE CUSTZONEID = @CUSTZONEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_CUSTZONE1 
 */

CREATE PROCEDURE sp_DelProcOP_CUSTZONE1
(
    @CUSTZONEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_CUSTZONE
     WHERE CUSTZONEID = @CUSTZONEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_CUSTZONE1: 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_CUSTZONE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_CUSTZONE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_CUSTZONE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_CUSTZONE1 
 */

CREATE PROCEDURE sp_SelProcOP_CUSTZONE1
(
    @CUSTZONEID     char(2))
AS
BEGIN
    SELECT CUSTZONEID,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_CUSTZONE
     WHERE CUSTZONEID = @CUSTZONEID

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


/* 
 * PROCEDURE: sp_InsProcOP_DEFINITIONPD1 
 */

CREATE PROCEDURE sp_InsProcOP_DEFINITIONPD1
(
    @DEFINITIONID     char(2),
    @CUSTIDSS         char(20)               = NULL,
    @CLASSID          char(5)                = NULL,
    @CLASSUBID        char(5)                = NULL,
    @INVTIDSKU        char(20)               = NULL,
    @DEBE             char(1),
    @HABER            char(1),
    @USER1            varchar(30)            = NULL,
    @USER2            varchar(30)            = NULL,
    @USER3            float                  = NULL,
    @USER4            float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEFINITIONPD(DEFINITIONID,
                                CUSTIDSS,
                                CLASSID,
                                CLASSUBID,
                                INVTIDSKU,
                                DEBE,
                                HABER,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@DEFINITIONID,
           @CUSTIDSS,
           @CLASSID,
           @CLASSUBID,
           @INVTIDSKU,
           @DEBE,
           @HABER,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DEFINITIONPD1: 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_DEFINITIONPD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DEFINITIONPD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DEFINITIONPD1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DEFINITIONPD1 
 */

CREATE PROCEDURE sp_UpdProcOP_DEFINITIONPD1
(
    @DEFINITIONID     char(2),
    @CUSTIDSS         char(20)               = NULL,
    @CLASSID          char(5)                = NULL,
    @CLASSUBID        char(5)                = NULL,
    @INVTIDSKU        char(20)               = NULL,
    @DEBE             char(1),
    @HABER            char(1),
    @USER1            varchar(30)            = NULL,
    @USER2            varchar(30)            = NULL,
    @USER3            float                  = NULL,
    @USER4            float                  = NULL,
    @TSTAMP           timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEFINITIONPD
       SET CUSTIDSS          = @CUSTIDSS,
           CLASSID           = @CLASSID,
           CLASSUBID         = @CLASSUBID,
           INVTIDSKU         = @INVTIDSKU,
           DEBE              = @DEBE,
           HABER             = @HABER,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE DEFINITIONID = @DEFINITIONID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DEFINITIONPD1 
 */

CREATE PROCEDURE sp_DelProcOP_DEFINITIONPD1
(
    @DEFINITIONID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEFINITIONPD
     WHERE DEFINITIONID = @DEFINITIONID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_DEFINITIONPD1: 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_DEFINITIONPD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DEFINITIONPD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DEFINITIONPD1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DEFINITIONPD1 
 */

CREATE PROCEDURE sp_SelProcOP_DEFINITIONPD1
(
    @DEFINITIONID     char(2))
AS
BEGIN
    SELECT DEFINITIONID,
           CUSTIDSS,
           CLASSID,
           CLASSUBID,
           INVTIDSKU,
           DEBE,
           HABER,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEFINITIONPD
     WHERE DEFINITIONID = @DEFINITIONID

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


/* 
 * PROCEDURE: sp_InsProcOP_DEPOSIT1 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSIT1
(
    @BATNBRDEPOSIT       tinyint,
    @SITEID              char(5),
    @CURYID              char(3)                  = NULL,
    @SALESPERSONTURN     tinyint,
    @DEPOSITTYPEID       char(1),
    @SALESPERID          char(10),
    @OPDATE              smalldatetime,
    @DATEPROCE           smalldatetime,
    @TOTPEN              numeric(9, 2),
    @TOTUSD              numeric(9, 2),
    @STATUS              bit                      = NULL,
    @USER1               varchar(30)              = NULL,
    @USER2               varchar(30)              = NULL,
    @USER3               float                    = NULL,
    @USER4               float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSIT(BATNBRDEPOSIT,
                           SITEID,
                           CURYID,
                           SALESPERSONTURN,
                           DEPOSITTYPEID,
                           SALESPERID,
                           OPDATE,
                           DATEPROCE,
                           TOTPEN,
                           TOTUSD,
                           STATUS,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@BATNBRDEPOSIT,
           @SITEID,
           @CURYID,
           @SALESPERSONTURN,
           @DEPOSITTYPEID,
           @SALESPERID,
           @OPDATE,
           @DATEPROCE,
           @TOTPEN,
           @TOTUSD,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DEPOSIT1: 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_DEPOSIT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DEPOSIT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DEPOSIT1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSIT1 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSIT1
(
    @BATNBRDEPOSIT       tinyint,
    @SITEID              char(5),
    @CURYID              char(3)                  = NULL,
    @SALESPERSONTURN     tinyint,
    @DEPOSITTYPEID       char(1),
    @SALESPERID          char(10),
    @OPDATE              smalldatetime,
    @DATEPROCE           smalldatetime,
    @TOTPEN              numeric(9, 2),
    @TOTUSD              numeric(9, 2),
    @STATUS              bit                      = NULL,
    @USER1               varchar(30)              = NULL,
    @USER2               varchar(30)              = NULL,
    @USER3               float                    = NULL,
    @USER4               float                    = NULL,
    @TSTAMP              timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSIT
       SET CURYID               = @CURYID,
           SALESPERSONTURN      = @SALESPERSONTURN,
           DEPOSITTYPEID        = @DEPOSITTYPEID,
           SALESPERID           = @SALESPERID,
           OPDATE               = @OPDATE,
           DATEPROCE            = @DATEPROCE,
           TOTPEN               = @TOTPEN,
           TOTUSD               = @TOTUSD,
           STATUS               = @STATUS,
           USER1                = @USER1,
           USER2                = @USER2,
           USER3                = @USER3,
           USER4                = @USER4
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DEPOSIT1 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSIT1
(
    @BATNBRDEPOSIT       tinyint,
    @SITEID              char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSIT
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_DEPOSIT1: 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_DEPOSIT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DEPOSIT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DEPOSIT1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DEPOSIT1 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSIT1
(
    @BATNBRDEPOSIT       tinyint,
    @SITEID              char(5))
AS
BEGIN
    SELECT BATNBRDEPOSIT,
           SITEID,
           CURYID,
           SALESPERSONTURN,
           DEPOSITTYPEID,
           SALESPERID,
           OPDATE,
           DATEPROCE,
           TOTPEN,
           TOTUSD,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSIT
     WHERE BATNBRDEPOSIT = @BATNBRDEPOSIT
       AND SITEID        = @SITEID

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


/* 
 * PROCEDURE: sp_InsProcOP_DEPOSITDET1 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSITDET1
(
    @BATNBRDEPOSIT     tinyint,
    @SITEID            char(5),
    @SEQUENCE          smallint,
    @CARDID            char(2)                  = NULL,
    @TOTCARD           numeric(9, 2)            = NULL,
    @CUSTIDSS          char(20)                 = NULL,
    @MTOBONUS          numeric(9, 2),
    @MTOCASH           numeric(9, 2),
    @USER1             varchar(30)              = NULL,
    @USER2             varchar(30)              = NULL,
    @USER3             float                    = NULL,
    @USER4             float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSITDET(BATNBRDEPOSIT,
                              SITEID,
                              SEQUENCE,
                              CARDID,
                              TOTCARD,
                              CUSTIDSS,
                              MTOBONUS,
                              MTOCASH,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@BATNBRDEPOSIT,
           @SITEID,
           @SEQUENCE,
           @CARDID,
           @TOTCARD,
           @CUSTIDSS,
           @MTOBONUS,
           @MTOCASH,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DEPOSITDET1: 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_DEPOSITDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DEPOSITDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DEPOSITDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSITDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSITDET1
(
    @BATNBRDEPOSIT     tinyint,
    @SITEID            char(5),
    @SEQUENCE          smallint,
    @CARDID            char(2)                  = NULL,
    @TOTCARD           numeric(9, 2)            = NULL,
    @CUSTIDSS          char(20)                 = NULL,
    @MTOBONUS          numeric(9, 2),
    @MTOCASH           numeric(9, 2),
    @USER1             varchar(30)              = NULL,
    @USER2             varchar(30)              = NULL,
    @USER3             float                    = NULL,
    @USER4             float                    = NULL,
    @TSTAMP            timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSITDET
       SET CARDID             = @CARDID,
           TOTCARD            = @TOTCARD,
           CUSTIDSS           = @CUSTIDSS,
           MTOBONUS           = @MTOBONUS,
           MTOCASH            = @MTOCASH,
           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_DEPOSITDET1: Cannot update  in OP_DEPOSITDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DEPOSITDET1 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSITDET1
(
    @BATNBRDEPOSIT     tinyint,
    @SITEID            char(5),
    @SEQUENCE          smallint)
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_DEPOSITDET1: 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_DEPOSITDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DEPOSITDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DEPOSITDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DEPOSITDET1 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSITDET1
(
    @BATNBRDEPOSIT     tinyint,
    @SITEID            char(5),
    @SEQUENCE          smallint)
AS
BEGIN
    SELECT BATNBRDEPOSIT,
           SITEID,
           SEQUENCE,
           CARDID,
           TOTCARD,
           CUSTIDSS,
           MTOBONUS,
           MTOCASH,
           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_DEPOSITDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcOP_DEPOSITDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcOP_DEPOSITDET1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcOP_DEPOSITSITE1 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSITSITE1
(
    @SITEID                char(5),
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @CUSTIDSS              char(20)                  = NULL,
    @DATEPROCE             smalldatetime,
    @TOTALPEN              numeric(9, 2),
    @TOTALUSD              numeric(9, 2),
    @STATUS                bit                       = NULL,
    @USER1                 varchar(30)               = NULL,
    @USER2                 varchar(30)               = NULL,
    @USER3                 float                     = NULL,
    @USER4                 float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSITSITE(SITEID,
                               BATNBRDEPOSITSITE,
                               CUSTIDSS,
                               DATEPROCE,
                               TOTALPEN,
                               TOTALUSD,
                               STATUS,
                               USER1,
                               USER2,
                               USER3,
                               USER4 
                               )
    VALUES(@SITEID,
           @BATNBRDEPOSITSITE,
           @CUSTIDSS,
           @DATEPROCE,
           @TOTALPEN,
           @TOTALUSD,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DEPOSITSITE1: 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_DEPOSITSITE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DEPOSITSITE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DEPOSITSITE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSITSITE1 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSITSITE1
(
    @SITEID                char(5),
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @CUSTIDSS              char(20)                  = NULL,
    @DATEPROCE             smalldatetime,
    @TOTALPEN              numeric(9, 2),
    @TOTALUSD              numeric(9, 2),
    @STATUS                bit                       = NULL,
    @USER1                 varchar(30)               = NULL,
    @USER2                 varchar(30)               = NULL,
    @USER3                 float                     = NULL,
    @USER4                 float                     = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSITSITE
       SET CUSTIDSS               = @CUSTIDSS,
           DATEPROCE              = @DATEPROCE,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           STATUS                 = @STATUS,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DEPOSITSITE1 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSITSITE1
(
    @SITEID                char(5),
    @BATNBRDEPOSITSITE     numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITSITE
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_DEPOSITSITE1: 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_DEPOSITSITE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DEPOSITSITE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DEPOSITSITE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DEPOSITSITE1 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSITSITE1
(
    @SITEID                char(5),
    @BATNBRDEPOSITSITE     numeric(10, 0))
AS
BEGIN
    SELECT SITEID,
           BATNBRDEPOSITSITE,
           CUSTIDSS,
           DATEPROCE,
           TOTALPEN,
           TOTALUSD,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSITSITE
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE

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


/* 
 * PROCEDURE: sp_InsProcOP_DEPOSITSITEDET1 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSITSITEDET1
(
    @SITEID                char(5),
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @SEQUENCE              smallint,
    @DOCSUMMARYTYPEID      char(2)                   = NULL,
    @REFERENCE             numeric(10, 0),
    @DEPOSITPASSWORD       char(20),
    @DATEDEPOSIT           smalldatetime,
    @CURYID                char(3),
    @CURYRATE              numeric(6, 4),
    @TOTALPEN              numeric(9, 2),
    @TOTALUSD              numeric(9, 2),
    @DEBE                  char(1),
    @HABER                 char(1),
    @STATUS                bit                       = NULL,
    @USER1                 varchar(30)               = NULL,
    @USER2                 varchar(30)               = NULL,
    @USER3                 float                     = NULL,
    @USER4                 float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSITSITEDET(SITEID,
                                  BATNBRDEPOSITSITE,
                                  SEQUENCE,
                                  DOCSUMMARYTYPEID,
                                  REFERENCE,
                                  DEPOSITPASSWORD,
                                  DATEDEPOSIT,
                                  CURYID,
                                  CURYRATE,
                                  TOTALPEN,
                                  TOTALUSD,
                                  DEBE,
                                  HABER,
                                  STATUS,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@SITEID,
           @BATNBRDEPOSITSITE,
           @SEQUENCE,
           @DOCSUMMARYTYPEID,
           @REFERENCE,
           @DEPOSITPASSWORD,
           @DATEDEPOSIT,
           @CURYID,
           @CURYRATE,
           @TOTALPEN,
           @TOTALUSD,
           @DEBE,
           @HABER,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DEPOSITSITEDET1: 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_DEPOSITSITEDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DEPOSITSITEDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DEPOSITSITEDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSITSITEDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSITSITEDET1
(
    @SITEID                char(5),
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @SEQUENCE              smallint,
    @DOCSUMMARYTYPEID      char(2)                   = NULL,
    @REFERENCE             numeric(10, 0),
    @DEPOSITPASSWORD       char(20),
    @DATEDEPOSIT           smalldatetime,
    @CURYID                char(3),
    @CURYRATE              numeric(6, 4),
    @TOTALPEN              numeric(9, 2),
    @TOTALUSD              numeric(9, 2),
    @DEBE                  char(1),
    @HABER                 char(1),
    @STATUS                bit                       = NULL,
    @USER1                 varchar(30)               = NULL,
    @USER2                 varchar(30)               = NULL,
    @USER3                 float                     = NULL,
    @USER4                 float                     = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSITSITEDET
       SET DOCSUMMARYTYPEID       = @DOCSUMMARYTYPEID,
           REFERENCE              = @REFERENCE,
           DEPOSITPASSWORD        = @DEPOSITPASSWORD,
           DATEDEPOSIT            = @DATEDEPOSIT,
           CURYID                 = @CURYID,
           CURYRATE               = @CURYRATE,
           TOTALPEN               = @TOTALPEN,
           TOTALUSD               = @TOTALUSD,
           DEBE                   = @DEBE,
           HABER                  = @HABER,
           STATUS                 = @STATUS,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND SEQUENCE          = @SEQUENCE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DEPOSITSITEDET1 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSITSITEDET1
(
    @SITEID                char(5),
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @SEQUENCE              smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITSITEDET
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND SEQUENCE          = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_DEPOSITSITEDET1: 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_DEPOSITSITEDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DEPOSITSITEDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DEPOSITSITEDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DEPOSITSITEDET1 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSITSITEDET1
(
    @SITEID                char(5),
    @BATNBRDEPOSITSITE     numeric(10, 0),
    @SEQUENCE              smallint)
AS
BEGIN
    SELECT SITEID,
           BATNBRDEPOSITSITE,
           SEQUENCE,
           DOCSUMMARYTYPEID,
           REFERENCE,
           DEPOSITPASSWORD,
           DATEDEPOSIT,
           CURYID,
           CURYRATE,
           TOTALPEN,
           TOTALUSD,
           DEBE,
           HABER,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DEPOSITSITEDET
     WHERE SITEID            = @SITEID
       AND BATNBRDEPOSITSITE = @BATNBRDEPOSITSITE
       AND SEQUENCE          = @SEQUENCE

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


/* 
 * PROCEDURE: sp_InsProcOP_DEPOSITTYPE1 
 */

CREATE PROCEDURE sp_InsProcOP_DEPOSITTYPE1
(
    @DEPOSITTYPEID        char(1),
    @DESCRDEPOSTITYPE     varchar(40),
    @USER01               varchar(30)            = NULL,
    @USER02               varchar(30)            = NULL,
    @USER03               float                  = NULL,
    @USER04               float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DEPOSITTYPE(DEPOSITTYPEID,
                               DESCRDEPOSTITYPE,
                               USER01,
                               USER02,
                               USER03,
                               USER04 
                               )
    VALUES(@DEPOSITTYPEID,
           @DESCRDEPOSTITYPE,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DEPOSITTYPE1: 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_DEPOSITTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DEPOSITTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DEPOSITTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DEPOSITTYPE1 
 */

CREATE PROCEDURE sp_UpdProcOP_DEPOSITTYPE1
(
    @DEPOSITTYPEID        char(1),
    @DESCRDEPOSTITYPE     varchar(40),
    @USER01               varchar(30)            = NULL,
    @USER02               varchar(30)            = NULL,
    @USER03               float                  = NULL,
    @USER04               float                  = NULL,
    @TSTAMP               timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DEPOSITTYPE
       SET DESCRDEPOSTITYPE      = @DESCRDEPOSTITYPE,
           USER01                = @USER01,
           USER02                = @USER02,
           USER03                = @USER03,
           USER04                = @USER04
     WHERE DEPOSITTYPEID = @DEPOSITTYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DEPOSITTYPE1 
 */

CREATE PROCEDURE sp_DelProcOP_DEPOSITTYPE1
(
    @DEPOSITTYPEID        char(1))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DEPOSITTYPE
     WHERE DEPOSITTYPEID = @DEPOSITTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_DEPOSITTYPE1: 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_DEPOSITTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DEPOSITTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DEPOSITTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DEPOSITTYPE1 
 */

CREATE PROCEDURE sp_SelProcOP_DEPOSITTYPE1
(
    @DEPOSITTYPEID        char(1))
AS
BEGIN
    SELECT DEPOSITTYPEID,
           DESCRDEPOSTITYPE,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_DEPOSITTYPE
     WHERE DEPOSITTYPEID = @DEPOSITTYPEID

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


/* 
 * PROCEDURE: sp_InsProcOP_DOCSUMMARY1 
 */

CREATE PROCEDURE sp_InsProcOP_DOCSUMMARY1
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @DOCDATE              smalldatetime,
    @DUEDATE              smalldatetime,
    @DOCSTATUSID          char(2),
    @TAXID01              char(2),
    @TAXID02              char(2),
    @TERMID               char(2),
    @CURYTYPEID           char(6),
    @CURYRATE             numeric(6, 4),
    @CURYID               char(3),
    @CUSTIDSS             char(20),
    @CUSTID               char(20),
    @CUSTNAME             varchar(40)              = NULL,
    @TAXREGNBR            char(20),
    @WEEKDAYPAG           char(12),
    @DAYTHENK             int,
    @TOTALPEN             numeric(9, 2),
    @TOTALUSD             numeric(9, 2),
    @SUBTOTALPEN          numeric(9, 2),
    @SUBTOTALUSD          numeric(9, 2),
    @TAXPEN               numeric(9, 2),
    @TAXUSD               numeric(9, 2),
    @STKITEM              bit                      = NULL,
    @STKPROCE             bit,
    @USER1                varchar(30)              = NULL,
    @USER2                varchar(30)              = NULL,
    @USER3                float                    = NULL,
    @USER4                float                    = 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,
                              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,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DOCSUMMARY1: 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_DOCSUMMARY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DOCSUMMARY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DOCSUMMARY1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DOCSUMMARY1 
 */

CREATE PROCEDURE sp_UpdProcOP_DOCSUMMARY1
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @DOCDATE              smalldatetime,
    @DUEDATE              smalldatetime,
    @DOCSTATUSID          char(2),
    @TAXID01              char(2),
    @TAXID02              char(2),
    @TERMID               char(2),
    @CURYTYPEID           char(6),
    @CURYRATE             numeric(6, 4),
    @CURYID               char(3),
    @CUSTIDSS             char(20),
    @CUSTID               char(20),
    @CUSTNAME             varchar(40)              = NULL,
    @TAXREGNBR            char(20),
    @WEEKDAYPAG           char(12),
    @DAYTHENK             int,
    @TOTALPEN             numeric(9, 2),
    @TOTALUSD             numeric(9, 2),
    @SUBTOTALPEN          numeric(9, 2),
    @SUBTOTALUSD          numeric(9, 2),
    @TAXPEN               numeric(9, 2),
    @TAXUSD               numeric(9, 2),
    @STKITEM              bit                      = NULL,
    @STKPROCE             bit,
    @USER1                varchar(30)              = NULL,
    @USER2                varchar(30)              = NULL,
    @USER3                float                    = NULL,
    @USER4                float                    = 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,
           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_DOCSUMMARY1: Cannot update  in OP_DOCSUMMARY '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DOCSUMMARY1 
 */

CREATE PROCEDURE sp_DelProcOP_DOCSUMMARY1
(
    @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_DOCSUMMARY1: 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_DOCSUMMARY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DOCSUMMARY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DOCSUMMARY1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DOCSUMMARY1 
 */

CREATE PROCEDURE sp_SelProcOP_DOCSUMMARY1
(
    @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,
           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_DOCSUMMARY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcOP_DOCSUMMARY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcOP_DOCSUMMARY1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcOP_DOCSUMMARYDET1 
 */

CREATE PROCEDURE sp_InsProcOP_DOCSUMMARYDET1
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             smallint,
    @CURYTYPEID           char(6),
    @CURYID               char(3),
    @TAXID02              char(2),
    @TAXID01              char(2),
    @DISCOUNT             numeric(12, 2)            = NULL,
    @INVTIDSKU            char(20),
    @SLS_PRICE            numeric(8, 4),
    @QTY                  numeric(10, 3),
    @TOTALPEN             numeric(9, 2),
    @SUBTOTALPEN          numeric(9, 2),
    @TAX1PEN              numeric(9, 2),
    @USER1                varchar(30)               = NULL,
    @USER2                varchar(30)               = NULL,
    @USER3                float                     = NULL,
    @USER4                float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DOCSUMMARYDET(SITEID,
                                 DOCSUMMARYTYPEID,
                                 NBRDOCSUMMARY,
                                 SEQUENCE,
                                 CURYTYPEID,
                                 CURYID,
                                 TAXID02,
                                 TAXID01,
                                 DISCOUNT,
                                 INVTIDSKU,
                                 SLS_PRICE,
                                 QTY,
                                 TOTALPEN,
                                 SUBTOTALPEN,
                                 TAX1PEN,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @DOCSUMMARYTYPEID,
           @NBRDOCSUMMARY,
           @SEQUENCE,
           @CURYTYPEID,
           @CURYID,
           @TAXID02,
           @TAXID01,
           @DISCOUNT,
           @INVTIDSKU,
           @SLS_PRICE,
           @QTY,
           @TOTALPEN,
           @SUBTOTALPEN,
           @TAX1PEN,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DOCSUMMARYDET1: 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_DOCSUMMARYDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DOCSUMMARYDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DOCSUMMARYDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DOCSUMMARYDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_DOCSUMMARYDET1
(
    @SITEID               char(5),
    @DOCSUMMARYTYPEID     char(2),
    @NBRDOCSUMMARY        char(10),
    @SEQUENCE             smallint,
    @CURYTYPEID           char(6),
    @CURYID               char(3),
    @TAXID02              char(2),
    @TAXID01              char(2),
    @DISCOUNT             numeric(12, 2)            = NULL,
    @INVTIDSKU            char(20),
    @SLS_PRICE            numeric(8, 4),
    @QTY                  numeric(10, 3),
    @TOTALPEN             numeric(9, 2),
    @SUBTOTALPEN          numeric(9, 2),
    @TAX1PEN              numeric(9, 2),
    @USER1                varchar(30)               = NULL,
    @USER2                varchar(30)               = NULL,
    @USER3                float                     = NULL,
    @USER4                float                     = 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,
           SUBTOTALPEN           = @SUBTOTALPEN,
           TAX1PEN               = @TAX1PEN,
           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_DOCSUMMARYDET1: Cannot update  in OP_DOCSUMMARYDET '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DOCSUMMARYDET1 
 */

CREATE PROCEDURE sp_DelProcOP_DOCSUMMARYDET1
(
    @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_DOCSUMMARYDET1: 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_DOCSUMMARYDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DOCSUMMARYDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DOCSUMMARYDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DOCSUMMARYDET1 
 */

CREATE PROCEDURE sp_SelProcOP_DOCSUMMARYDET1
(
    @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,
           SUBTOTALPEN,
           TAX1PEN,
           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_DOCSUMMARYDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcOP_DOCSUMMARYDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcOP_DOCSUMMARYDET1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcOP_DOCSUMMARYTYPE1 
 */

CREATE PROCEDURE sp_InsProcOP_DOCSUMMARYTYPE1
(
    @DOCSUMMARYTYPEID        char(2),
    @DESCRDOCSUMMARYTYPE     varchar(40),
    @USER1                   varchar(30)            = NULL,
    @USER2                   varchar(30)            = NULL,
    @USER3                   float                  = NULL,
    @USER4                   float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_DOCSUMMARYTYPE(DOCSUMMARYTYPEID,
                                  DESCRDOCSUMMARYTYPE,
                                  USER1,
                                  USER2,
                                  USER3,
                                  USER4 
                                  )
    VALUES(@DOCSUMMARYTYPEID,
           @DESCRDOCSUMMARYTYPE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_DOCSUMMARYTYPE1: 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_DOCSUMMARYTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_DOCSUMMARYTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_DOCSUMMARYTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_DOCSUMMARYTYPE1 
 */

CREATE PROCEDURE sp_UpdProcOP_DOCSUMMARYTYPE1
(
    @DOCSUMMARYTYPEID        char(2),
    @DESCRDOCSUMMARYTYPE     varchar(40),
    @USER1                   varchar(30)            = NULL,
    @USER2                   varchar(30)            = NULL,
    @USER3                   float                  = NULL,
    @USER4                   float                  = NULL,
    @TSTAMP                  timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_DOCSUMMARYTYPE
       SET DESCRDOCSUMMARYTYPE      = @DESCRDOCSUMMARYTYPE,
           USER1                    = @USER1,
           USER2                    = @USER2,
           USER3                    = @USER3,
           USER4                    = @USER4
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_DOCSUMMARYTYPE1 
 */

CREATE PROCEDURE sp_DelProcOP_DOCSUMMARYTYPE1
(
    @DOCSUMMARYTYPEID        char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_DOCSUMMARYTYPE
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_DOCSUMMARYTYPE1: 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_DOCSUMMARYTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_DOCSUMMARYTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_DOCSUMMARYTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_DOCSUMMARYTYPE1 
 */

CREATE PROCEDURE sp_SelProcOP_DOCSUMMARYTYPE1
(
    @DOCSUMMARYTYPEID        char(2))
AS
BEGIN
    SELECT DOCSUMMARYTYPEID,
           DESCRDOCSUMMARYTYPE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_DOCSUMMARYTYPE
     WHERE DOCSUMMARYTYPEID = @DOCSUMMARYTYPEID

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


/* 
 * PROCEDURE: sp_InsProcOP_LOADSALESORD1 
 */

CREATE PROCEDURE sp_InsProcOP_LOADSALESORD1
(
    @NBRLOADSALE        numeric(10, 0),
    @CURYID             char(3),
    @SALESPERID         char(10),
    @CUSTIDSS           char(20)                  = NULL,
    @TAXREGNBR          char(20)                  = NULL,
    @CUSTNAME           varchar(40)               = NULL,
    @ADDR1              varchar(40)               = NULL,
    @DOCTYPEID          char(3),
    @DOCSTATUSID        char(2),
    @USERID             char(20),
    @OPTRTOTPEN         numeric(9, 2),
    @OPTRTAXPEN         numeric(9, 2),
    @DOCDATE            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @CURYRATE           numeric(6, 4),
    @STKCHECK           bit,
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              float                     = NULL,
    @USER4              float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_LOADSALESORD(NBRLOADSALE,
                                CURYID,
                                SALESPERID,
                                CUSTIDSS,
                                TAXREGNBR,
                                CUSTNAME,
                                ADDR1,
                                DOCTYPEID,
                                DOCSTATUSID,
                                USERID,
                                OPTRTOTPEN,
                                OPTRTAXPEN,
                                DOCDATE,
                                DATEPROCESALES,
                                CURYRATE,
                                STKCHECK,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@NBRLOADSALE,
           @CURYID,
           @SALESPERID,
           @CUSTIDSS,
           @TAXREGNBR,
           @CUSTNAME,
           @ADDR1,
           @DOCTYPEID,
           @DOCSTATUSID,
           @USERID,
           @OPTRTOTPEN,
           @OPTRTAXPEN,
           @DOCDATE,
           @DATEPROCESALES,
           @CURYRATE,
           @STKCHECK,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_LOADSALESORD1: 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_LOADSALESORD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_LOADSALESORD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_LOADSALESORD1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_LOADSALESORD1 
 */

CREATE PROCEDURE sp_UpdProcOP_LOADSALESORD1
(
    @NBRLOADSALE        numeric(10, 0),
    @CURYID             char(3),
    @SALESPERID         char(10),
    @CUSTIDSS           char(20)                  = NULL,
    @TAXREGNBR          char(20)                  = NULL,
    @CUSTNAME           varchar(40)               = NULL,
    @ADDR1              varchar(40)               = NULL,
    @DOCTYPEID          char(3),
    @DOCSTATUSID        char(2),
    @USERID             char(20),
    @OPTRTOTPEN         numeric(9, 2),
    @OPTRTAXPEN         numeric(9, 2),
    @DOCDATE            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @CURYRATE           numeric(6, 4),
    @STKCHECK           bit,
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              float                     = NULL,
    @USER4              float                     = NULL,
    @TSTAMP             timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_LOADSALESORD
       SET CURYID              = @CURYID,
           SALESPERID          = @SALESPERID,
           CUSTIDSS            = @CUSTIDSS,
           TAXREGNBR           = @TAXREGNBR,
           CUSTNAME            = @CUSTNAME,
           ADDR1               = @ADDR1,
           DOCTYPEID           = @DOCTYPEID,
           DOCSTATUSID         = @DOCSTATUSID,
           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_LOADSALESORD1: Cannot update  in OP_LOADSALESORD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_LOADSALESORD1 
 */

CREATE PROCEDURE sp_DelProcOP_LOADSALESORD1
(
    @NBRLOADSALE        numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_LOADSALESORD
     WHERE NBRLOADSALE = @NBRLOADSALE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_LOADSALESORD1: 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_LOADSALESORD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_LOADSALESORD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_LOADSALESORD1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_LOADSALESORD1 
 */

CREATE PROCEDURE sp_SelProcOP_LOADSALESORD1
(
    @NBRLOADSALE        numeric(10, 0))
AS
BEGIN
    SELECT NBRLOADSALE,
           CURYID,
           SALESPERID,
           CUSTIDSS,
           TAXREGNBR,
           CUSTNAME,
           ADDR1,
           DOCTYPEID,
           DOCSTATUSID,
           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_LOADSALESORD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcOP_LOADSALESORD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcOP_LOADSALESORD1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcOP_LOADSALESORDDET1 
 */

CREATE PROCEDURE sp_InsProcOP_LOADSALESORDDET1
(
    @NBRLOADSALE     numeric(10, 0),
    @INVTIDSKU       char(20),
    @DESCR           varchar(40),
    @STKUNITID       char(5)                   = NULL,
    @TAXIGV          numeric(9, 2),
    @SLS_PRICE       numeric(8, 4),
    @QTY             numeric(10, 3),
    @OPTRTOTDET      numeric(9, 2),
    @STDCOSTPEN      numeric(8, 4),
    @STRCOSTUSD      numeric(8, 4),
    @DISCDSCTO1      numeric(5, 2),
    @DISCDSCTO2      numeric(5, 2),
    @DISCMTO         numeric(12, 2),
    @USER1           varchar(30)               = NULL,
    @USER2           varchar(30)               = NULL,
    @USER3           float                     = NULL,
    @USER4           float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_LOADSALESORDDET(NBRLOADSALE,
                                   INVTIDSKU,
                                   DESCR,
                                   STKUNITID,
                                   TAXIGV,
                                   SLS_PRICE,
                                   QTY,
                                   OPTRTOTDET,
                                   STDCOSTPEN,
                                   STRCOSTUSD,
                                   DISCDSCTO1,
                                   DISCDSCTO2,
                                   DISCMTO,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@NBRLOADSALE,
           @INVTIDSKU,
           @DESCR,
           @STKUNITID,
           @TAXIGV,
           @SLS_PRICE,
           @QTY,
           @OPTRTOTDET,
           @STDCOSTPEN,
           @STRCOSTUSD,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTO,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_LOADSALESORDDET1: 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_LOADSALESORDDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_LOADSALESORDDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_LOADSALESORDDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_LOADSALESORDDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_LOADSALESORDDET1
(
    @NBRLOADSALE     numeric(10, 0),
    @SEQUENCE        smallint,
    @INVTIDSKU       char(20),
    @DESCR           varchar(40),
    @STKUNITID       char(5)                   = NULL,
    @TAXIGV          numeric(9, 2),
    @SLS_PRICE       numeric(8, 4),
    @QTY             numeric(10, 3),
    @OPTRTOTDET      numeric(9, 2),
    @STDCOSTPEN      numeric(8, 4),
    @STRCOSTUSD      numeric(8, 4),
    @DISCDSCTO1      numeric(5, 2),
    @DISCDSCTO2      numeric(5, 2),
    @DISCMTO         numeric(12, 2),
    @USER1           varchar(30)               = NULL,
    @USER2           varchar(30)               = NULL,
    @USER3           float                     = NULL,
    @USER4           float                     = NULL,
    @TSTAMP          timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_LOADSALESORDDET
       SET SEQUENCE         = @SEQUENCE,
           INVTIDSKU        = @INVTIDSKU,
           DESCR            = @DESCR,
           STKUNITID        = @STKUNITID,
           TAXIGV           = @TAXIGV,
           SLS_PRICE        = @SLS_PRICE,
           QTY              = @QTY,
           OPTRTOTDET       = @OPTRTOTDET,
           STDCOSTPEN       = @STDCOSTPEN,
           STRCOSTUSD       = @STRCOSTUSD,
           DISCDSCTO1       = @DISCDSCTO1,
           DISCDSCTO2       = @DISCDSCTO2,
           DISCMTO          = @DISCMTO,
           USER1            = @USER1,
           USER2            = @USER2,
           USER3            = @USER3,
           USER4            = @USER4
     WHERE NBRLOADSALE = @NBRLOADSALE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_LOADSALESORDDET1 
 */

CREATE PROCEDURE sp_DelProcOP_LOADSALESORDDET1
(
    @NBRLOADSALE     numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_LOADSALESORDDET
     WHERE NBRLOADSALE = @NBRLOADSALE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_LOADSALESORDDET1: 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_LOADSALESORDDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_LOADSALESORDDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_LOADSALESORDDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_LOADSALESORDDET1 
 */

CREATE PROCEDURE sp_SelProcOP_LOADSALESORDDET1
(
    @NBRLOADSALE     numeric(10, 0))
AS
BEGIN
    SELECT NBRLOADSALE,
           SEQUENCE,
           INVTIDSKU,
           DESCR,
           STKUNITID,
           TAXIGV,
           SLS_PRICE,
           QTY,
           OPTRTOTDET,
           STDCOSTPEN,
           STRCOSTUSD,
           DISCDSCTO1,
           DISCDSCTO2,
           DISCMTO,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_LOADSALESORDDET
     WHERE NBRLOADSALE = @NBRLOADSALE

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


/* 
 * PROCEDURE: sp_InsProcOP_MODSALES1 
 */

CREATE PROCEDURE sp_InsProcOP_MODSALES1
(
    @MODSALESID     char(2),
    @DESCR          varchar(40),
    @USER1          varchar(30)            = NULL,
    @USER2          varchar(30)            = NULL,
    @USER3          float                  = NULL,
    @USER4          float                  = 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_MODSALES1: 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_MODSALES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_MODSALES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_MODSALES1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_MODSALES1 
 */

CREATE PROCEDURE sp_UpdProcOP_MODSALES1
(
    @MODSALESID     char(2),
    @DESCR          varchar(40),
    @USER1          varchar(30)            = NULL,
    @USER2          varchar(30)            = NULL,
    @USER3          float                  = NULL,
    @USER4          float                  = 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_MODSALES1: Cannot update  in OP_MODSALES '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_MODSALES1 
 */

CREATE PROCEDURE sp_DelProcOP_MODSALES1
(
    @MODSALESID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_MODSALES
     WHERE MODSALESID = @MODSALESID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_MODSALES1: 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_MODSALES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_MODSALES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_MODSALES1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_MODSALES1 
 */

CREATE PROCEDURE sp_SelProcOP_MODSALES1
(
    @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_MODSALES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcOP_MODSALES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcOP_MODSALES1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcOP_PRCLVLCUST1 
 */

CREATE PROCEDURE sp_InsProcOP_PRCLVLCUST1
(
    @PRCLVLCUSTID           char(10),
    @CUSTIDSS               char(20)                 = NULL,
    @SITEID                 char(5),
    @CURYID                 char(3),
    @DESCR                  varchar(40),
    @STARTDATEPRICE         smalldatetime,
    @ENDDATEPRICE           smalldatetime,
    @STATUS                 bit,
    @APPROVA1               bit,
    @USERIDAPPROVA1         char(20),
    @DATETIMEAUTORIZED1     smalldatetime,
    @APPROVA2               bit,
    @USERIDAPPROVA2         char(20),
    @DATETIMEAUTORIZED2     smalldatetime,
    @STKPRICESEND           bit,
    @SITEORIG               char(5),
    @STKPRICESS             bit,
    @USER1                  varchar(30)              = NULL,
    @USER2                  varchar(30)              = NULL,
    @USER3                  float                    = NULL,
    @USER4                  float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PRCLVLCUST(PRCLVLCUSTID,
                              CUSTIDSS,
                              SITEID,
                              CURYID,
                              DESCR,
                              STARTDATEPRICE,
                              ENDDATEPRICE,
                              STATUS,
                              APPROVA1,
                              USERIDAPPROVA1,
                              DATETIMEAUTORIZED1,
                              APPROVA2,
                              USERIDAPPROVA2,
                              DATETIMEAUTORIZED2,
                              STKPRICESEND,
                              SITEORIG,
                              STKPRICESS,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@PRCLVLCUSTID,
           @CUSTIDSS,
           @SITEID,
           @CURYID,
           @DESCR,
           @STARTDATEPRICE,
           @ENDDATEPRICE,
           @STATUS,
           @APPROVA1,
           @USERIDAPPROVA1,
           @DATETIMEAUTORIZED1,
           @APPROVA2,
           @USERIDAPPROVA2,
           @DATETIMEAUTORIZED2,
           @STKPRICESEND,
           @SITEORIG,
           @STKPRICESS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_PRCLVLCUST1: 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_PRCLVLCUST1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_PRCLVLCUST1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_PRCLVLCUST1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_PRCLVLCUST1 
 */

CREATE PROCEDURE sp_UpdProcOP_PRCLVLCUST1
(
    @PRCLVLCUSTID           char(10),
    @CUSTIDSS               char(20)                 = NULL,
    @SITEID                 char(5),
    @CURYID                 char(3),
    @DESCR                  varchar(40),
    @STARTDATEPRICE         smalldatetime,
    @ENDDATEPRICE           smalldatetime,
    @STATUS                 bit,
    @APPROVA1               bit,
    @USERIDAPPROVA1         char(20),
    @DATETIMEAUTORIZED1     smalldatetime,
    @APPROVA2               bit,
    @USERIDAPPROVA2         char(20),
    @DATETIMEAUTORIZED2     smalldatetime,
    @STKPRICESEND           bit,
    @SITEORIG               char(5),
    @STKPRICESS             bit,
    @USER1                  varchar(30)              = NULL,
    @USER2                  varchar(30)              = NULL,
    @USER3                  float                    = NULL,
    @USER4                  float                    = NULL,
    @TSTAMP                 timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PRCLVLCUST
       SET CUSTIDSS                = @CUSTIDSS,
           SITEID                  = @SITEID,
           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,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_PRCLVLCUST1 
 */

CREATE PROCEDURE sp_DelProcOP_PRCLVLCUST1
(
    @PRCLVLCUSTID           char(10))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PRCLVLCUST
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_PRCLVLCUST1: 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_PRCLVLCUST1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_PRCLVLCUST1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_PRCLVLCUST1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_PRCLVLCUST1 
 */

CREATE PROCEDURE sp_SelProcOP_PRCLVLCUST1
(
    @PRCLVLCUSTID           char(10))
AS
BEGIN
    SELECT PRCLVLCUSTID,
           CUSTIDSS,
           SITEID,
           CURYID,
           DESCR,
           STARTDATEPRICE,
           ENDDATEPRICE,
           STATUS,
           APPROVA1,
           USERIDAPPROVA1,
           DATETIMEAUTORIZED1,
           APPROVA2,
           USERIDAPPROVA2,
           DATETIMEAUTORIZED2,
           STKPRICESEND,
           SITEORIG,
           STKPRICESS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PRCLVLCUST
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID

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


/* 
 * PROCEDURE: sp_InsProcOP_PRCLVLCUSTDET1 
 */

CREATE PROCEDURE sp_InsProcOP_PRCLVLCUSTDET1
(
    @PRCLVLCUSTID      char(10),
    @SEQUENCE          smallint,
    @STATUS            bit,
    @DISCDSCTO         numeric(5, 2),
    @DISCMTO           numeric(9, 2),
    @SLS_PRICE         numeric(8, 4),
    @SLS_PRICECUST     numeric(8, 4),
    @INVTIDSKU         char(20),
    @USER1             varchar(30)              = NULL,
    @USER2             varchar(30)              = NULL,
    @USER3             float                    = NULL,
    @USER4             float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PRCLVLCUSTDET(PRCLVLCUSTID,
                                 SEQUENCE,
                                 STATUS,
                                 DISCDSCTO,
                                 DISCMTO,
                                 SLS_PRICE,
                                 SLS_PRICECUST,
                                 INVTIDSKU,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@PRCLVLCUSTID,
           @SEQUENCE,
           @STATUS,
           @DISCDSCTO,
           @DISCMTO,
           @SLS_PRICE,
           @SLS_PRICECUST,
           @INVTIDSKU,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_PRCLVLCUSTDET1: 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_PRCLVLCUSTDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_PRCLVLCUSTDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_PRCLVLCUSTDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_PRCLVLCUSTDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_PRCLVLCUSTDET1
(
    @PRCLVLCUSTID      char(10),
    @SEQUENCE          smallint,
    @STATUS            bit,
    @DISCDSCTO         numeric(5, 2),
    @DISCMTO           numeric(9, 2),
    @SLS_PRICE         numeric(8, 4),
    @SLS_PRICECUST     numeric(8, 4),
    @INVTIDSKU         char(20),
    @USER1             varchar(30)              = NULL,
    @USER2             varchar(30)              = NULL,
    @USER3             float                    = NULL,
    @USER4             float                    = NULL,
    @TSTAMP            timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PRCLVLCUSTDET
       SET STATUS             = @STATUS,
           DISCDSCTO          = @DISCDSCTO,
           DISCMTO            = @DISCMTO,
           SLS_PRICE          = @SLS_PRICE,
           SLS_PRICECUST      = @SLS_PRICECUST,
           INVTIDSKU          = @INVTIDSKU,
           USER1              = @USER1,
           USER2              = @USER2,
           USER3              = @USER3,
           USER4              = @USER4
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SEQUENCE     = @SEQUENCE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_PRCLVLCUSTDET1 
 */

CREATE PROCEDURE sp_DelProcOP_PRCLVLCUSTDET1
(
    @PRCLVLCUSTID      char(10),
    @SEQUENCE          smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PRCLVLCUSTDET
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SEQUENCE     = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_PRCLVLCUSTDET1: 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_PRCLVLCUSTDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_PRCLVLCUSTDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_PRCLVLCUSTDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_PRCLVLCUSTDET1 
 */

CREATE PROCEDURE sp_SelProcOP_PRCLVLCUSTDET1
(
    @PRCLVLCUSTID      char(10),
    @SEQUENCE          smallint)
AS
BEGIN
    SELECT PRCLVLCUSTID,
           SEQUENCE,
           STATUS,
           DISCDSCTO,
           DISCMTO,
           SLS_PRICE,
           SLS_PRICECUST,
           INVTIDSKU,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_PRCLVLCUSTDET
     WHERE PRCLVLCUSTID = @PRCLVLCUSTID
       AND SEQUENCE     = @SEQUENCE

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


/* 
 * PROCEDURE: sp_InsProcOP_PROMOTIONCARD1 
 */

CREATE PROCEDURE sp_InsProcOP_PROMOTIONCARD1
(
    @PROMOTIONCARDID       char(20),
    @NAMECLIENT            varchar(40)            = NULL,
    @PASSWORDMAGNETIC      char(30)               = NULL,
    @LASTNAME              varchar(40)            = NULL,
    @USER1                 varchar(30)            = NULL,
    @USER2                 varchar(30)            = NULL,
    @USER3                 float                  = NULL,
    @USER4                 float                  = NULL,
    @PROMOTIONCARDTYPE     char(2))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PROMOTIONCARD(PROMOTIONCARDID,
                                 NAMECLIENT,
                                 PASSWORDMAGNETIC,
                                 LASTNAME,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4,
                                 PROMOTIONCARDTYPE)
    VALUES(@PROMOTIONCARDID,
           @NAMECLIENT,
           @PASSWORDMAGNETIC,
           @LASTNAME,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @PROMOTIONCARDTYPE)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_PROMOTIONCARD1: 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_PROMOTIONCARD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_PROMOTIONCARD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_PROMOTIONCARD1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_PROMOTIONCARD1 
 */

CREATE PROCEDURE sp_UpdProcOP_PROMOTIONCARD1
(
    @PROMOTIONCARDID       char(20),
    @NAMECLIENT            varchar(40)            = NULL,
    @PASSWORDMAGNETIC      char(30)               = NULL,
    @LASTNAME              varchar(40)            = NULL,
    @USER1                 varchar(30)            = NULL,
    @USER2                 varchar(30)            = NULL,
    @USER3                 float                  = NULL,
    @USER4                 float                  = NULL,
    @TSTAMP                timestamp              = NULL,
    @PROMOTIONCARDTYPE     char(2))
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PROMOTIONCARD
       SET NAMECLIENT             = @NAMECLIENT,
           PASSWORDMAGNETIC       = @PASSWORDMAGNETIC,
           LASTNAME               = @LASTNAME,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4,
           PROMOTIONCARDTYPE      = @PROMOTIONCARDTYPE
     WHERE PROMOTIONCARDID = @PROMOTIONCARDID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_PROMOTIONCARD1 
 */

CREATE PROCEDURE sp_DelProcOP_PROMOTIONCARD1
(
    @PROMOTIONCARDID       char(20))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PROMOTIONCARD
     WHERE PROMOTIONCARDID = @PROMOTIONCARDID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_PROMOTIONCARD1: 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_PROMOTIONCARD1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_PROMOTIONCARD1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_PROMOTIONCARD1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_PROMOTIONCARD1 
 */

CREATE PROCEDURE sp_SelProcOP_PROMOTIONCARD1
(
    @PROMOTIONCARDID       char(20))
AS
BEGIN
    SELECT PROMOTIONCARDID,
           NAMECLIENT,
           PASSWORDMAGNETIC,
           LASTNAME,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP,
           PROMOTIONCARDTYPE
      FROM OP_PROMOTIONCARD
     WHERE PROMOTIONCARDID = @PROMOTIONCARDID

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


/* 
 * PROCEDURE: sp_InsProcOP_PROMOTIONCARDTYPE1 
 */

CREATE PROCEDURE sp_InsProcOP_PROMOTIONCARDTYPE1
(
    @PROMOTIONCARDTYPE      char(2),
    @DESCRPROMOTIONCARD     char(10)            = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_PROMOTIONCARDTYPE(PROMOTIONCARDTYPE,
                                     DESCRPROMOTIONCARD)
    VALUES(@PROMOTIONCARDTYPE,
           @DESCRPROMOTIONCARD)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_PROMOTIONCARDTYPE1: 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_PROMOTIONCARDTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_PROMOTIONCARDTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_PROMOTIONCARDTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_PROMOTIONCARDTYPE1 
 */

CREATE PROCEDURE sp_UpdProcOP_PROMOTIONCARDTYPE1
(
    @PROMOTIONCARDTYPE      char(2),
    @DESCRPROMOTIONCARD     char(10)            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_PROMOTIONCARDTYPE
       SET DESCRPROMOTIONCARD      = @DESCRPROMOTIONCARD
     WHERE PROMOTIONCARDTYPE = @PROMOTIONCARDTYPE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_PROMOTIONCARDTYPE1 
 */

CREATE PROCEDURE sp_DelProcOP_PROMOTIONCARDTYPE1
(
    @PROMOTIONCARDTYPE      char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_PROMOTIONCARDTYPE
     WHERE PROMOTIONCARDTYPE = @PROMOTIONCARDTYPE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_PROMOTIONCARDTYPE1: 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_PROMOTIONCARDTYPE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_PROMOTIONCARDTYPE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_PROMOTIONCARDTYPE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_PROMOTIONCARDTYPE1 
 */

CREATE PROCEDURE sp_SelProcOP_PROMOTIONCARDTYPE1
(
    @PROMOTIONCARDTYPE      char(2))
AS
BEGIN
    SELECT PROMOTIONCARDTYPE,
           DESCRPROMOTIONCARD
      FROM OP_PROMOTIONCARDTYPE
     WHERE PROMOTIONCARDTYPE = @PROMOTIONCARDTYPE

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


/* 
 * PROCEDURE: sp_InsProcOP_SALES1 
 */

CREATE PROCEDURE sp_InsProcOP_SALES1
(
    @DOCTYPEID            char(3),
    @NBRDOCUMENT          numeric(10, 0),
    @STKINVENTORY         bit,
    @SITEID               char(5),
    @CURYID               char(3),
    @DOCSTATUSID          char(2),
    @TERMID               char(2),
    @MODSALESID           char(2),
    @SALESPOINT           char(10),
    @SALESPERID           char(10),
    @USERID               char(20),
    @DATEDOC              smalldatetime,
    @DATEPROCESALES       smalldatetime,
    @PERPOST              char(6),
    @TAXIGVID             char(2)                   = NULL,
    @TAXISCID             char(2)                   = NULL,
    @TOTALPEN             numeric(9, 2),
    @TOTALUSD             numeric(9, 2),
    @SUBTOTALPEN          numeric(9, 2),
    @SUBTOTALUSD          numeric(9, 2),
    @TAX1PEN              numeric(9, 2),
    @TAX1USD              numeric(9, 2),
    @TAX2PEN              numeric(9, 2),
    @TAX2USD              numeric(9, 2),
    @PORCENTDISCOUNT1     numeric(5, 2),
    @PORCENTDISCOUNT2     numeric(5, 2),
    @TOTDISCOUNTPEN       numeric(9, 2),
    @TOTDISCOUNTUSD       numeric(9, 2),
    @TOTRETURNEDPEN       numeric(9, 2),
    @TOTRETURNEDUSD       numeric(9, 2),
    @TOTCASHPEN           numeric(9, 2),
    @TOTCASHUSD           numeric(9, 2),
    @CARDCREDITID1        char(2)                   = NULL,
    @CARDCREDITID2        char(2)                   = NULL,
    @NBRCARD1             char(20)                  = NULL,
    @NBRCARD2             char(20)                  = NULL,
    @TOTCARD1PEN          numeric(9, 2),
    @TOTCARD1USD          numeric(9, 2),
    @TOTCARD2PEN          numeric(9, 2),
    @TOTCARD2USD          numeric(9, 2),
    @CUSTIDSS             char(20)                  = NULL,
    @TAXREGNBR            char(20)                  = NULL,
    @NAME                 varchar(40)               = NULL,
    @PLACA                varchar(10),
    @NBRBONUS             int,
    @CURYTYPEID           char(6),
    @CURYRATE             numeric(6, 4),
    @STKCLOSEDZ           bit,
    @STKCLOSEDX           bit,
    @PROMOTIONCARDID      char(20)                  = NULL,
    @POINTNUMBER          smallint,
    @USER1                varchar(30)               = NULL,
    @USER2                varchar(30)               = NULL,
    @USER3                varchar(30)               = NULL,
    @USER4                smalldatetime             = NULL,
    @USER5                smalldatetime             = NULL,
    @USER6                float                     = NULL,
    @USER7                float                     = NULL,
    @USER8                smallint                  = NULL,
    @USER9                smallint                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALES(DOCTYPEID,
                         NBRDOCUMENT,
                         STKINVENTORY,
                         SITEID,
                         CURYID,
                         DOCSTATUSID,
                         TERMID,
                         MODSALESID,
                         SALESPOINT,
                         SALESPERID,
                         USERID,
                         DATEDOC,
                         DATEPROCESALES,
                         PERPOST,
                         TAXIGVID,
                         TAXISCID,
                         TOTALPEN,
                         TOTALUSD,
                         SUBTOTALPEN,
                         SUBTOTALUSD,
                         TAX1PEN,
                         TAX1USD,
                         TAX2PEN,
                         TAX2USD,
                         PORCENTDISCOUNT1,
                         PORCENTDISCOUNT2,
                         TOTDISCOUNTPEN,
                         TOTDISCOUNTUSD,
                         TOTRETURNEDPEN,
                         TOTRETURNEDUSD,
                         TOTCASHPEN,
                         TOTCASHUSD,
                         CARDCREDITID1,
                         CARDCREDITID2,
                         NBRCARD1,
                         NBRCARD2,
                         TOTCARD1PEN,
                         TOTCARD1USD,
                         TOTCARD2PEN,
                         TOTCARD2USD,
                         CUSTIDSS,
                         TAXREGNBR,
                         NAME,
                         PLACA,
                         NBRBONUS,
                         CURYTYPEID,
                         CURYRATE,
                         STKCLOSEDZ,
                         STKCLOSEDX,
                         PROMOTIONCARDID,
                         POINTNUMBER,
                         USER1,
                         USER2,
                         USER3,
                         USER4,
                         USER5,
                         USER6,
                         USER7,
                         USER8,
                         USER9 
                         )
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @STKINVENTORY,
           @SITEID,
           @CURYID,
           @DOCSTATUSID,
           @TERMID,
           @MODSALESID,
           @SALESPOINT,
           @SALESPERID,
           @USERID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERPOST,
           @TAXIGVID,
           @TAXISCID,
           @TOTALPEN,
           @TOTALUSD,
           @SUBTOTALPEN,
           @SUBTOTALUSD,
           @TAX1PEN,
           @TAX1USD,
           @TAX2PEN,
           @TAX2USD,
           @PORCENTDISCOUNT1,
           @PORCENTDISCOUNT2,
           @TOTDISCOUNTPEN,
           @TOTDISCOUNTUSD,
           @TOTRETURNEDPEN,
           @TOTRETURNEDUSD,
           @TOTCASHPEN,
           @TOTCASHUSD,
           @CARDCREDITID1,
           @CARDCREDITID2,
           @NBRCARD1,
           @NBRCARD2,
           @TOTCARD1PEN,
           @TOTCARD1USD,
           @TOTCARD2PEN,
           @TOTCARD2USD,
           @CUSTIDSS,
           @TAXREGNBR,
           @NAME,
           @PLACA,
           @NBRBONUS,
           @CURYTYPEID,
           @CURYRATE,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @PROMOTIONCARDID,
           @POINTNUMBER,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_SALES1: 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_SALES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_SALES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_SALES1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_SALES1 
 */

CREATE PROCEDURE sp_UpdProcOP_SALES1
(
    @DOCTYPEID            char(3),
    @NBRDOCUMENT          numeric(10, 0),
    @STKINVENTORY         bit,
    @SITEID               char(5),
    @CURYID               char(3),
    @DOCSTATUSID          char(2),
    @TERMID               char(2),
    @MODSALESID           char(2),
    @SALESPOINT           char(10),
    @SALESPERID           char(10),
    @USERID               char(20),
    @DATEDOC              smalldatetime,
    @DATEPROCESALES       smalldatetime,
    @PERPOST              char(6),
    @TAXIGVID             char(2)                   = NULL,
    @TAXISCID             char(2)                   = NULL,
    @TOTALPEN             numeric(9, 2),
    @TOTALUSD             numeric(9, 2),
    @SUBTOTALPEN          numeric(9, 2),
    @SUBTOTALUSD          numeric(9, 2),
    @TAX1PEN              numeric(9, 2),
    @TAX1USD              numeric(9, 2),
    @TAX2PEN              numeric(9, 2),
    @TAX2USD              numeric(9, 2),
    @PORCENTDISCOUNT1     numeric(5, 2),
    @PORCENTDISCOUNT2     numeric(5, 2),
    @TOTDISCOUNTPEN       numeric(9, 2),
    @TOTDISCOUNTUSD       numeric(9, 2),
    @TOTRETURNEDPEN       numeric(9, 2),
    @TOTRETURNEDUSD       numeric(9, 2),
    @TOTCASHPEN           numeric(9, 2),
    @TOTCASHUSD           numeric(9, 2),
    @CARDCREDITID1        char(2)                   = NULL,
    @CARDCREDITID2        char(2)                   = NULL,
    @NBRCARD1             char(20)                  = NULL,
    @NBRCARD2             char(20)                  = NULL,
    @TOTCARD1PEN          numeric(9, 2),
    @TOTCARD1USD          numeric(9, 2),
    @TOTCARD2PEN          numeric(9, 2),
    @TOTCARD2USD          numeric(9, 2),
    @CUSTIDSS             char(20)                  = NULL,
    @TAXREGNBR            char(20)                  = NULL,
    @NAME                 varchar(40)               = NULL,
    @PLACA                varchar(10),
    @NBRBONUS             int,
    @CURYTYPEID           char(6),
    @CURYRATE             numeric(6, 4),
    @STKCLOSEDZ           bit,
    @STKCLOSEDX           bit,
    @PROMOTIONCARDID      char(20)                  = NULL,
    @POINTNUMBER          smallint,
    @USER1                varchar(30)               = NULL,
    @USER2                varchar(30)               = NULL,
    @USER3                varchar(30)               = NULL,
    @USER4                smalldatetime             = NULL,
    @USER5                smalldatetime             = NULL,
    @USER6                float                     = NULL,
    @USER7                float                     = NULL,
    @USER8                smallint                  = NULL,
    @USER9                smallint                  = NULL,
    @TSTAMP               timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALES
       SET STKINVENTORY          = @STKINVENTORY,
           SITEID                = @SITEID,
           CURYID                = @CURYID,
           DOCSTATUSID           = @DOCSTATUSID,
           TERMID                = @TERMID,
           MODSALESID            = @MODSALESID,
           SALESPOINT            = @SALESPOINT,
           SALESPERID            = @SALESPERID,
           USERID                = @USERID,
           DATEDOC               = @DATEDOC,
           DATEPROCESALES        = @DATEPROCESALES,
           PERPOST               = @PERPOST,
           TAXIGVID              = @TAXIGVID,
           TAXISCID              = @TAXISCID,
           TOTALPEN              = @TOTALPEN,
           TOTALUSD              = @TOTALUSD,
           SUBTOTALPEN           = @SUBTOTALPEN,
           SUBTOTALUSD           = @SUBTOTALUSD,
           TAX1PEN               = @TAX1PEN,
           TAX1USD               = @TAX1USD,
           TAX2PEN               = @TAX2PEN,
           TAX2USD               = @TAX2USD,
           PORCENTDISCOUNT1      = @PORCENTDISCOUNT1,
           PORCENTDISCOUNT2      = @PORCENTDISCOUNT2,
           TOTDISCOUNTPEN        = @TOTDISCOUNTPEN,
           TOTDISCOUNTUSD        = @TOTDISCOUNTUSD,
           TOTRETURNEDPEN        = @TOTRETURNEDPEN,
           TOTRETURNEDUSD        = @TOTRETURNEDUSD,
           TOTCASHPEN            = @TOTCASHPEN,
           TOTCASHUSD            = @TOTCASHUSD,
           CARDCREDITID1         = @CARDCREDITID1,
           CARDCREDITID2         = @CARDCREDITID2,
           NBRCARD1              = @NBRCARD1,
           NBRCARD2              = @NBRCARD2,
           TOTCARD1PEN           = @TOTCARD1PEN,
           TOTCARD1USD           = @TOTCARD1USD,
           TOTCARD2PEN           = @TOTCARD2PEN,
           TOTCARD2USD           = @TOTCARD2USD,
           CUSTIDSS              = @CUSTIDSS,
           TAXREGNBR             = @TAXREGNBR,
           NAME                  = @NAME,
           PLACA                 = @PLACA,
           NBRBONUS              = @NBRBONUS,
           CURYTYPEID            = @CURYTYPEID,
           CURYRATE              = @CURYRATE,
           STKCLOSEDZ            = @STKCLOSEDZ,
           STKCLOSEDX            = @STKCLOSEDX,
           PROMOTIONCARDID       = @PROMOTIONCARDID,
           POINTNUMBER           = @POINTNUMBER,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4,
           USER5                 = @USER5,
           USER6                 = @USER6,
           USER7                 = @USER7,
           USER8                 = @USER8,
           USER9                 = @USER9
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_SALES1 
 */

CREATE PROCEDURE sp_DelProcOP_SALES1
(
    @DOCTYPEID            char(3),
    @NBRDOCUMENT          numeric(10, 0))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALES
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_SALES1: 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_SALES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_SALES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_SALES1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_SALES1 
 */

CREATE PROCEDURE sp_SelProcOP_SALES1
(
    @DOCTYPEID            char(3),
    @NBRDOCUMENT          numeric(10, 0))
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           STKINVENTORY,
           SITEID,
           CURYID,
           DOCSTATUSID,
           TERMID,
           MODSALESID,
           SALESPOINT,
           SALESPERID,
           USERID,
           DATEDOC,
           DATEPROCESALES,
           PERPOST,
           TAXIGVID,
           TAXISCID,
           TOTALPEN,
           TOTALUSD,
           SUBTOTALPEN,
           SUBTOTALUSD,
           TAX1PEN,
           TAX1USD,
           TAX2PEN,
           TAX2USD,
           PORCENTDISCOUNT1,
           PORCENTDISCOUNT2,
           TOTDISCOUNTPEN,
           TOTDISCOUNTUSD,
           TOTRETURNEDPEN,
           TOTRETURNEDUSD,
           TOTCASHPEN,
           TOTCASHUSD,
           CARDCREDITID1,
           CARDCREDITID2,
           NBRCARD1,
           NBRCARD2,
           TOTCARD1PEN,
           TOTCARD1USD,
           TOTCARD2PEN,
           TOTCARD2USD,
           CUSTIDSS,
           TAXREGNBR,
           NAME,
           PLACA,
           NBRBONUS,
           CURYTYPEID,
           CURYRATE,
           STKCLOSEDZ,
           STKCLOSEDX,
           PROMOTIONCARDID,
           POINTNUMBER,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM OP_SALES
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT

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


/* 
 * PROCEDURE: sp_InsProcOP_SALESDET1 
 */

CREATE PROCEDURE sp_InsProcOP_SALESDET1
(
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint,
    @SITEID             char(5),
    @INVTIDSKU          char(20),
    @CURYID             char(3),
    @DOCSTATUSID        char(2),
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERSPOT            char(6),
    @STKCLOSEDZ         bit,
    @STKCLOSEDX         bit,
    @NBRTURN            tinyint,
    @NBRCARA            char(2)                   = NULL,
    @NBRTRANSACFUEL     char(4)                   = NULL,
    @DISCDSCTO1         numeric(5, 2),
    @DISCDSCTO2         numeric(5, 2),
    @DISCMTOPEN         numeric(9, 2),
    @DISCMTOUSD         numeric(9, 2),
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(9, 2),
    @TOTALUSD           numeric(9, 2),
    @TAXPEN             numeric(9, 2),
    @TAXUSD             numeric(9, 2),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        char(20),
    @DESCRINVENTORY     varchar(40),
    @QTY                numeric(10, 3),
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              varchar(30)               = NULL,
    @USER4              smalldatetime             = NULL,
    @USER5              smalldatetime             = NULL,
    @USER6              float                     = NULL,
    @USER7              float                     = NULL,
    @USER8              smallint                  = NULL,
    @USER9              smallint                  = NULL,
    @SALESPOINT         char(10),
    @SALESPERID         char(10))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESDET(DOCTYPEID,
                            NBRDOCUMENT,
                            SEQUENCE,
                            SITEID,
                            INVTIDSKU,
                            CURYID,
                            DOCSTATUSID,
                            DATEDOC,
                            DATEPROCESALES,
                            PERSPOT,
                            STKCLOSEDZ,
                            STKCLOSEDX,
                            NBRTURN,
                            NBRCARA,
                            NBRTRANSACFUEL,
                            DISCDSCTO1,
                            DISCDSCTO2,
                            DISCMTOPEN,
                            DISCMTOUSD,
                            PORCENTTAXIGV,
                            PORCENTTAXISC,
                            TOTALPEN,
                            TOTALUSD,
                            TAXPEN,
                            TAXUSD,
                            STKITEM,
                            STKFISI,
                            SLS_PRICE,
                            SLSPRICESALE,
                            STDCOSTPEN,
                            STDCOSTUSD,
                            INVTIDALTER,
                            DESCRINVENTORY,
                            QTY,
                            USER1,
                            USER2,
                            USER3,
                            USER4,
                            USER5,
                            USER6,
                            USER7,
                            USER8,
                            USER9,
                            SALESPOINT,
                            SALESPERID)
    VALUES(@DOCTYPEID,
           @NBRDOCUMENT,
           @SEQUENCE,
           @SITEID,
           @INVTIDSKU,
           @CURYID,
           @DOCSTATUSID,
           @DATEDOC,
           @DATEPROCESALES,
           @PERSPOT,
           @STKCLOSEDZ,
           @STKCLOSEDX,
           @NBRTURN,
           @NBRCARA,
           @NBRTRANSACFUEL,
           @DISCDSCTO1,
           @DISCDSCTO2,
           @DISCMTOPEN,
           @DISCMTOUSD,
           @PORCENTTAXIGV,
           @PORCENTTAXISC,
           @TOTALPEN,
           @TOTALUSD,
           @TAXPEN,
           @TAXUSD,
           @STKITEM,
           @STKFISI,
           @SLS_PRICE,
           @SLSPRICESALE,
           @STDCOSTPEN,
           @STDCOSTUSD,
           @INVTIDALTER,
           @DESCRINVENTORY,
           @QTY,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9,
           @SALESPOINT,
           @SALESPERID)

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_SALESDET1: 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_SALESDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_SALESDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_SALESDET1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_SALESDET1 
 */

CREATE PROCEDURE sp_UpdProcOP_SALESDET1
(
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint,
    @SITEID             char(5),
    @INVTIDSKU          char(20),
    @CURYID             char(3),
    @DOCSTATUSID        char(2),
    @DATEDOC            smalldatetime,
    @DATEPROCESALES     smalldatetime,
    @PERSPOT            char(6),
    @STKCLOSEDZ         bit,
    @STKCLOSEDX         bit,
    @NBRTURN            tinyint,
    @NBRCARA            char(2)                   = NULL,
    @NBRTRANSACFUEL     char(4)                   = NULL,
    @DISCDSCTO1         numeric(5, 2),
    @DISCDSCTO2         numeric(5, 2),
    @DISCMTOPEN         numeric(9, 2),
    @DISCMTOUSD         numeric(9, 2),
    @PORCENTTAXIGV      numeric(5, 2),
    @PORCENTTAXISC      numeric(5, 2),
    @TOTALPEN           numeric(9, 2),
    @TOTALUSD           numeric(9, 2),
    @TAXPEN             numeric(9, 2),
    @TAXUSD             numeric(9, 2),
    @STKITEM            bit,
    @STKFISI            bit,
    @SLS_PRICE          numeric(8, 4),
    @SLSPRICESALE       numeric(8, 4),
    @STDCOSTPEN         numeric(8, 4),
    @STDCOSTUSD         numeric(8, 4),
    @INVTIDALTER        char(20),
    @DESCRINVENTORY     varchar(40),
    @QTY                numeric(10, 3),
    @USER1              varchar(30)               = NULL,
    @USER2              varchar(30)               = NULL,
    @USER3              varchar(30)               = NULL,
    @USER4              smalldatetime             = NULL,
    @USER5              smalldatetime             = NULL,
    @USER6              float                     = NULL,
    @USER7              float                     = NULL,
    @USER8              smallint                  = NULL,
    @USER9              smallint                  = NULL,
    @TSTAMP             timestamp                 = NULL,
    @SALESPOINT         char(10),
    @SALESPERID         char(10))
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALESDET
       SET SITEID              = @SITEID,
           INVTIDSKU           = @INVTIDSKU,
           CURYID              = @CURYID,
           DOCSTATUSID         = @DOCSTATUSID,
           DATEDOC             = @DATEDOC,
           DATEPROCESALES      = @DATEPROCESALES,
           PERSPOT             = @PERSPOT,
           STKCLOSEDZ          = @STKCLOSEDZ,
           STKCLOSEDX          = @STKCLOSEDX,
           NBRTURN             = @NBRTURN,
           NBRCARA             = @NBRCARA,
           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,
           USER1               = @USER1,
           USER2               = @USER2,
           USER3               = @USER3,
           USER4               = @USER4,
           USER5               = @USER5,
           USER6               = @USER6,
           USER7               = @USER7,
           USER8               = @USER8,
           USER9               = @USER9,
           SALESPOINT          = @SALESPOINT,
           SALESPERID          = @SALESPERID
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_SALESDET1 
 */

CREATE PROCEDURE sp_DelProcOP_SALESDET1
(
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_SALESDET1: 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_SALESDET1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_SALESDET1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_SALESDET1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_SALESDET1 
 */

CREATE PROCEDURE sp_SelProcOP_SALESDET1
(
    @DOCTYPEID          char(3),
    @NBRDOCUMENT        numeric(10, 0),
    @SEQUENCE           smallint)
AS
BEGIN
    SELECT DOCTYPEID,
           NBRDOCUMENT,
           SEQUENCE,
           SITEID,
           INVTIDSKU,
           CURYID,
           DOCSTATUSID,
           DATEDOC,
           DATEPROCESALES,
           PERSPOT,
           STKCLOSEDZ,
           STKCLOSEDX,
           NBRTURN,
           NBRCARA,
           NBRTRANSACFUEL,
           DISCDSCTO1,
           DISCDSCTO2,
           DISCMTOPEN,
           DISCMTOUSD,
           PORCENTTAXIGV,
           PORCENTTAXISC,
           TOTALPEN,
           TOTALUSD,
           TAXPEN,
           TAXUSD,
           STKITEM,
           STKFISI,
           SLS_PRICE,
           SLSPRICESALE,
           STDCOSTPEN,
           STDCOSTUSD,
           INVTIDALTER,
           DESCRINVENTORY,
           QTY,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP,
           SALESPOINT,
           SALESPERID
      FROM OP_SALESDET
     WHERE DOCTYPEID   = @DOCTYPEID
       AND NBRDOCUMENT = @NBRDOCUMENT
       AND SEQUENCE    = @SEQUENCE

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


/* 
 * PROCEDURE: sp_InsProcOP_SALESPERSON1 
 */

CREATE PROCEDURE sp_InsProcOP_SALESPERSON1
(
    @SALESPERID         char(10),
    @SALESPERNAME       varchar(40)              = NULL,
    @IDENTITYDOC        char(10),
    @PHONE              char(10)                 = NULL,
    @SEX                char(1),
    @INITIALDATE        smalldatetime,
    @SITEID             char(5),
    @ADDRESS1           varchar(40),
    @ADDRESS2           varchar(40)              = NULL,
    @PASSWORD           char(15),
    @BIRTHDATE          smalldatetime,
    @BIRTHADDRESS1      varchar(40)              = NULL,
    @BIRTHADDRESS2      varchar(40)              = NULL,
    @STATUSPERSONID     char(10),
    @USER01             varchar(30)              = NULL,
    @USER02             varchar(30)              = NULL,
    @USER03             float                    = NULL,
    @USER04             float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESPERSON(SALESPERID,
                               SALESPERNAME,
                               IDENTITYDOC,
                               PHONE,
                               SEX,
                               INITIALDATE,
                               SITEID,
                               ADDRESS1,
                               ADDRESS2,
                               PASSWORD,
                               BIRTHDATE,
                               BIRTHADDRESS1,
                               BIRTHADDRESS2,
                               STATUSPERSONID,
                               USER01,
                               USER02,
                               USER03,
                               USER04 
                               )
    VALUES(@SALESPERID,
           @SALESPERNAME,
           @IDENTITYDOC,
           @PHONE,
           @SEX,
           @INITIALDATE,
           @SITEID,
           @ADDRESS1,
           @ADDRESS2,
           @PASSWORD,
           @BIRTHDATE,
           @BIRTHADDRESS1,
           @BIRTHADDRESS2,
           @STATUSPERSONID,
           @USER01,
           @USER02,
           @USER03,
           @USER04 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_SALESPERSON1: 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_SALESPERSON1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_SALESPERSON1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_SALESPERSON1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_SALESPERSON1 
 */

CREATE PROCEDURE sp_UpdProcOP_SALESPERSON1
(
    @SALESPERID         char(10),
    @SALESPERNAME       varchar(40)              = NULL,
    @IDENTITYDOC        char(10),
    @PHONE              char(10)                 = NULL,
    @SEX                char(1),
    @INITIALDATE        smalldatetime,
    @SITEID             char(5),
    @ADDRESS1           varchar(40),
    @ADDRESS2           varchar(40)              = NULL,
    @PASSWORD           char(15),
    @BIRTHDATE          smalldatetime,
    @BIRTHADDRESS1      varchar(40)              = NULL,
    @BIRTHADDRESS2      varchar(40)              = NULL,
    @STATUSPERSONID     char(10),
    @USER01             varchar(30)              = NULL,
    @USER02             varchar(30)              = NULL,
    @USER03             float                    = NULL,
    @USER04             float                    = 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,
           USER01              = @USER01,
           USER02              = @USER02,
           USER03              = @USER03,
           USER04              = @USER04
     WHERE SALESPERID = @SALESPERID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_SALESPERSON1 
 */

CREATE PROCEDURE sp_DelProcOP_SALESPERSON1
(
    @SALESPERID         char(10))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESPERSON
     WHERE SALESPERID = @SALESPERID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_SALESPERSON1: 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_SALESPERSON1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_SALESPERSON1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_SALESPERSON1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_SALESPERSON1 
 */

CREATE PROCEDURE sp_SelProcOP_SALESPERSON1
(
    @SALESPERID         char(10))
AS
BEGIN
    SELECT SALESPERID,
           SALESPERNAME,
           IDENTITYDOC,
           PHONE,
           SEX,
           INITIALDATE,
           SITEID,
           ADDRESS1,
           ADDRESS2,
           PASSWORD,
           BIRTHDATE,
           BIRTHADDRESS1,
           BIRTHADDRESS2,
           STATUSPERSONID,
           USER01,
           USER02,
           USER03,
           USER04,
           TSTAMP
      FROM OP_SALESPERSON
     WHERE SALESPERID = @SALESPERID

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


/* 
 * PROCEDURE: sp_InsProcOP_SALESPOINTSETING1 
 */

CREATE PROCEDURE sp_InsProcOP_SALESPOINTSETING1
(
    @SALESPOINT            char(10),
    @SALESPERID            char(10),
    @ENDDATEACT            smalldatetime,
    @SERIALNUMBER          char(16),
    @TOTZETA               numeric(10, 2),
    @PRECLVID              char(5),
    @SITEID                char(5),
    @STKTICKFAC            bit,
    @STKTICKBOL            bit,
    @CURYID                char(3),
    @STKLOADSALE           bit,
    @NBRLOADSALE           numeric(10, 0),
    @STKPRINTERFAC         bit,
    @STKPRINTEDBOL         bit,
    @TYPEDISPFAC           varchar(1),
    @DISPFAC               varchar(10),
    @NBRFAC                numeric(10, 0),
    @TYPEDISPBOL           varchar(1),
    @DISPBOL               varchar(10),
    @NBRBOL                numeric(10, 0),
    @TYPEDISPNC            varchar(1),
    @DISPNC                varchar(10),
    @NBRNC                 numeric(10, 0),
    @TYPEDISPND            varchar(1),
    @DISPND                varchar(10),
    @NBRND                 numeric(10, 0),
    @TYPEDISORDER          varchar(1),
    @DISPORDER             varchar(10),
    @NBRORDER              numeric(10, 0),
    @TYPEDISPTICKFAC       varchar(1),
    @DISPTICKFAC           varchar(10),
    @NBRTICKFAC            numeric(10, 0),
    @TYPEDISPTICKBOL       varchar(1),
    @DISPTICKBOL           varchar(10),
    @NBRTICKBOL            numeric(10, 0),
    @TERMINALNAME          varchar(20),
    @DISPDISPLAY           varchar(10),
    @STKSALESPERSONID      bit,
    @STKSALESPOINTFAC      bit,
    @STKZETAOK             bit,
    @STKJUMPAUTOMAT        bit,
    @QTYJUNPAUTOMAT        tinyint,
    @STKHOTKEYS            bit,
    @TYPEPRINTERTICKET     char(18),
    @STKDELETEDOC          bit,
    @STK_PRICE_COST        bit,
    @STKDESCFACTUR         bit,
    @USER1                 varchar(30)               = NULL,
    @USER2                 varchar(30)               = NULL,
    @USER3                 varchar(30)               = NULL,
    @USER4                 smalldatetime             = NULL,
    @USER5                 smalldatetime             = NULL,
    @USER6                 float                     = NULL,
    @USER7                 float                     = NULL,
    @USER8                 smallint                  = NULL,
    @USER9                 smallint                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SALESPOINTSETING(SALESPOINT,
                                    SALESPERID,
                                    ENDDATEACT,
                                    SERIALNUMBER,
                                    TOTZETA,
                                    PRECLVID,
                                    SITEID,
                                    STKTICKFAC,
                                    STKTICKBOL,
                                    CURYID,
                                    STKLOADSALE,
                                    NBRLOADSALE,
                                    STKPRINTERFAC,
                                    STKPRINTEDBOL,
                                    TYPEDISPFAC,
                                    DISPFAC,
                                    NBRFAC,
                                    TYPEDISPBOL,
                                    DISPBOL,
                                    NBRBOL,
                                    TYPEDISPNC,
                                    DISPNC,
                                    NBRNC,
                                    TYPEDISPND,
                                    DISPND,
                                    NBRND,
                                    TYPEDISORDER,
                                    DISPORDER,
                                    NBRORDER,
                                    TYPEDISPTICKFAC,
                                    DISPTICKFAC,
                                    NBRTICKFAC,
                                    TYPEDISPTICKBOL,
                                    DISPTICKBOL,
                                    NBRTICKBOL,
                                    TERMINALNAME,
                                    DISPDISPLAY,
                                    STKSALESPERSONID,
                                    STKSALESPOINTFAC,
                                    STKZETAOK,
                                    STKJUMPAUTOMAT,
                                    QTYJUNPAUTOMAT,
                                    STKHOTKEYS,
                                    TYPEPRINTERTICKET,
                                    STKDELETEDOC,
                                    [STK PRICE_COST],
                                    STKDESCFACTUR,
                                    USER1,
                                    USER2,
                                    USER3,
                                    USER4,
                                    USER5,
                                    USER6,
                                    USER7,
                                    USER8,
                                    USER9 
                                    )
    VALUES(@SALESPOINT,
           @SALESPERID,
           @ENDDATEACT,
           @SERIALNUMBER,
           @TOTZETA,
           @PRECLVID,
           @SITEID,
           @STKTICKFAC,
           @STKTICKBOL,
           @CURYID,
           @STKLOADSALE,
           @NBRLOADSALE,
           @STKPRINTERFAC,
           @STKPRINTEDBOL,
           @TYPEDISPFAC,
           @DISPFAC,
           @NBRFAC,
           @TYPEDISPBOL,
           @DISPBOL,
           @NBRBOL,
           @TYPEDISPNC,
           @DISPNC,
           @NBRNC,
           @TYPEDISPND,
           @DISPND,
           @NBRND,
           @TYPEDISORDER,
           @DISPORDER,
           @NBRORDER,
           @TYPEDISPTICKFAC,
           @DISPTICKFAC,
           @NBRTICKFAC,
           @TYPEDISPTICKBOL,
           @DISPTICKBOL,
           @NBRTICKBOL,
           @TERMINALNAME,
           @DISPDISPLAY,
           @STKSALESPERSONID,
           @STKSALESPOINTFAC,
           @STKZETAOK,
           @STKJUMPAUTOMAT,
           @QTYJUNPAUTOMAT,
           @STKHOTKEYS,
           @TYPEPRINTERTICKET,
           @STKDELETEDOC,
           @STK_PRICE_COST,
           @STKDESCFACTUR,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcOP_SALESPOINTSETING1 
 */

CREATE PROCEDURE sp_UpdProcOP_SALESPOINTSETING1
(
    @SALESPOINT            char(10),
    @SALESPERID            char(10),
    @ENDDATEACT            smalldatetime,
    @SERIALNUMBER          char(16),
    @TOTZETA               numeric(10, 2),
    @NBRZETA               smallint,
    @PRECLVID              char(5),
    @SITEID                char(5),
    @STKTICKFAC            bit,
    @STKTICKBOL            bit,
    @CURYID                char(3),
    @STKLOADSALE           bit,
    @NBRLOADSALE           numeric(10, 0),
    @STKPRINTERFAC         bit,
    @STKPRINTEDBOL         bit,
    @TYPEDISPFAC           varchar(1),
    @DISPFAC               varchar(10),
    @NBRFAC                numeric(10, 0),
    @TYPEDISPBOL           varchar(1),
    @DISPBOL               varchar(10),
    @NBRBOL                numeric(10, 0),
    @TYPEDISPNC            varchar(1),
    @DISPNC                varchar(10),
    @NBRNC                 numeric(10, 0),
    @TYPEDISPND            varchar(1),
    @DISPND                varchar(10),
    @NBRND                 numeric(10, 0),
    @TYPEDISORDER          varchar(1),
    @DISPORDER             varchar(10),
    @NBRORDER              numeric(10, 0),
    @TYPEDISPTICKFAC       varchar(1),
    @DISPTICKFAC           varchar(10),
    @NBRTICKFAC            numeric(10, 0),
    @TYPEDISPTICKBOL       varchar(1),
    @DISPTICKBOL           varchar(10),
    @NBRTICKBOL            numeric(10, 0),
    @TERMINALNAME          varchar(20),
    @DISPDISPLAY           varchar(10),
    @STKSALESPERSONID      bit,
    @STKSALESPOINTFAC      bit,
    @STKZETAOK             bit,
    @STKJUMPAUTOMAT        bit,
    @QTYJUNPAUTOMAT        tinyint,
    @STKHOTKEYS            bit,
    @TYPEPRINTERTICKET     char(18),
    @STKDELETEDOC          bit,
    @STK_PRICE_COST        bit,
    @STKDESCFACTUR         bit,
    @USER1                 varchar(30)               = NULL,
    @USER2                 varchar(30)               = NULL,
    @USER3                 varchar(30)               = NULL,
    @USER4                 smalldatetime             = NULL,
    @USER5                 smalldatetime             = NULL,
    @USER6                 float                     = NULL,
    @USER7                 float                     = NULL,
    @USER8                 smallint                  = NULL,
    @USER9                 smallint                  = NULL,
    @TSTAMP                timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SALESPOINTSETING
       SET ENDDATEACT             = @ENDDATEACT,
           SERIALNUMBER           = @SERIALNUMBER,
           TOTZETA                = @TOTZETA,
           NBRZETA                = @NBRZETA,
           PRECLVID               = @PRECLVID,
           SITEID                 = @SITEID,
           STKTICKFAC             = @STKTICKFAC,
           STKTICKBOL             = @STKTICKBOL,
           CURYID                 = @CURYID,
           STKLOADSALE            = @STKLOADSALE,
           NBRLOADSALE            = @NBRLOADSALE,
           STKPRINTERFAC          = @STKPRINTERFAC,
           STKPRINTEDBOL          = @STKPRINTEDBOL,
           TYPEDISPFAC            = @TYPEDISPFAC,
           DISPFAC                = @DISPFAC,
           NBRFAC                 = @NBRFAC,
           TYPEDISPBOL            = @TYPEDISPBOL,
           DISPBOL                = @DISPBOL,
           NBRBOL                 = @NBRBOL,
           TYPEDISPNC             = @TYPEDISPNC,
           DISPNC                 = @DISPNC,
           NBRNC                  = @NBRNC,
           TYPEDISPND             = @TYPEDISPND,
           DISPND                 = @DISPND,
           NBRND                  = @NBRND,
           TYPEDISORDER           = @TYPEDISORDER,
           DISPORDER              = @DISPORDER,
           NBRORDER               = @NBRORDER,
           TYPEDISPTICKFAC        = @TYPEDISPTICKFAC,
           DISPTICKFAC            = @DISPTICKFAC,
           NBRTICKFAC             = @NBRTICKFAC,
           TYPEDISPTICKBOL        = @TYPEDISPTICKBOL,
           DISPTICKBOL            = @DISPTICKBOL,
           NBRTICKBOL             = @NBRTICKBOL,
           TERMINALNAME           = @TERMINALNAME,
           DISPDISPLAY            = @DISPDISPLAY,
           STKSALESPERSONID       = @STKSALESPERSONID,
           STKSALESPOINTFAC       = @STKSALESPOINTFAC,
           STKZETAOK              = @STKZETAOK,
           STKJUMPAUTOMAT         = @STKJUMPAUTOMAT,
           QTYJUNPAUTOMAT         = @QTYJUNPAUTOMAT,
           STKHOTKEYS             = @STKHOTKEYS,
           TYPEPRINTERTICKET      = @TYPEPRINTERTICKET,
           STKDELETEDOC           = @STKDELETEDOC,
           [STK PRICE_COST]       = @STK_PRICE_COST,
           STKDESCFACTUR          = @STKDESCFACTUR,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4,
           USER5                  = @USER5,
           USER6                  = @USER6,
           USER7                  = @USER7,
           USER8                  = @USER8,
           USER9                  = @USER9
     WHERE SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_SALESPOINTSETING1 
 */

CREATE PROCEDURE sp_DelProcOP_SALESPOINTSETING1
(
    @SALESPOINT            char(10),
    @SALESPERID            char(10))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SALESPOINTSETING
     WHERE SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcOP_SALESPOINTSETING1 
 */

CREATE PROCEDURE sp_SelProcOP_SALESPOINTSETING1
(
    @SALESPOINT            char(10),
    @SALESPERID            char(10))
AS
BEGIN
    SELECT SALESPOINT,
           SALESPERID,
           ENDDATEACT,
           SERIALNUMBER,
           TOTZETA,
           NBRZETA,
           PRECLVID,
           SITEID,
           STKTICKFAC,
           STKTICKBOL,
           CURYID,
           STKLOADSALE,
           NBRLOADSALE,
           STKPRINTERFAC,
           STKPRINTEDBOL,
           TYPEDISPFAC,
           DISPFAC,
           NBRFAC,
           TYPEDISPBOL,
           DISPBOL,
           NBRBOL,
           TYPEDISPNC,
           DISPNC,
           NBRNC,
           TYPEDISPND,
           DISPND,
           NBRND,
           TYPEDISORDER,
           DISPORDER,
           NBRORDER,
           TYPEDISPTICKFAC,
           DISPTICKFAC,
           NBRTICKFAC,
           TYPEDISPTICKBOL,
           DISPTICKBOL,
           NBRTICKBOL,
           TERMINALNAME,
           DISPDISPLAY,
           STKSALESPERSONID,
           STKSALESPOINTFAC,
           STKZETAOK,
           STKJUMPAUTOMAT,
           QTYJUNPAUTOMAT,
           STKHOTKEYS,
           TYPEPRINTERTICKET,
           STKDELETEDOC,
           [STK PRICE_COST],
           STKDESCFACTUR,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM OP_SALESPOINTSETING
     WHERE SALESPOINT = @SALESPOINT
       AND SALESPERID = @SALESPERID

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


/* 
 * PROCEDURE: sp_InsProcOP_STATUSPERSON1 
 */

CREATE PROCEDURE sp_InsProcOP_STATUSPERSON1
(
    @STATUSPERSONID        char(10),
    @DESCRSTATUSPERSON     varchar(40),
    @USER1                 varchar(30)            = NULL,
    @USER2                 varchar(30)            = NULL,
    @USER3                 float                  = NULL,
    @USER4                 float                  = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_STATUSPERSON(STATUSPERSONID,
                                DESCRSTATUSPERSON,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@STATUSPERSONID,
           @DESCRSTATUSPERSON,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_STATUSPERSON1: 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_STATUSPERSON1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_STATUSPERSON1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_STATUSPERSON1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_STATUSPERSON1 
 */

CREATE PROCEDURE sp_UpdProcOP_STATUSPERSON1
(
    @STATUSPERSONID        char(10),
    @DESCRSTATUSPERSON     varchar(40),
    @USER1                 varchar(30)            = NULL,
    @USER2                 varchar(30)            = NULL,
    @USER3                 float                  = NULL,
    @USER4                 float                  = NULL,
    @TSTAMP                timestamp              = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_STATUSPERSON
       SET DESCRSTATUSPERSON      = @DESCRSTATUSPERSON,
           USER1                  = @USER1,
           USER2                  = @USER2,
           USER3                  = @USER3,
           USER4                  = @USER4
     WHERE STATUSPERSONID = @STATUSPERSONID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_STATUSPERSON1 
 */

CREATE PROCEDURE sp_DelProcOP_STATUSPERSON1
(
    @STATUSPERSONID        char(10))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_STATUSPERSON
     WHERE STATUSPERSONID = @STATUSPERSONID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_STATUSPERSON1: 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_STATUSPERSON1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_STATUSPERSON1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_STATUSPERSON1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_STATUSPERSON1 
 */

CREATE PROCEDURE sp_SelProcOP_STATUSPERSON1
(
    @STATUSPERSONID        char(10))
AS
BEGIN
    SELECT STATUSPERSONID,
           DESCRSTATUSPERSON,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_STATUSPERSON
     WHERE STATUSPERSONID = @STATUSPERSONID

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


/* 
 * PROCEDURE: sp_InsProcOP_SUMMARYDAY1 
 */

CREATE PROCEDURE sp_InsProcOP_SUMMARYDAY1
(
    @BATNBRDIARYSUMMARY     numeric(10, 0),
    @SITEID                 char(5),
    @DATEPROCE              smalldatetime,
    @CURYRATE               numeric(6, 4),
    @ROUND                  numeric(9, 2),
    @TOTALDIFERENCE         numeric(9, 2),
    @OBSERVATION            varchar(100)              = NULL,
    @STATUS                 bit                       = NULL,
    @DEBE                   numeric(9, 2),
    @HABER                  numeric(9, 2),
    @USER1                  varchar(30)               = NULL,
    @USER2                  varchar(30)               = NULL,
    @USER3                  float                     = NULL,
    @USER4                  float                     = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SUMMARYDAY(BATNBRDIARYSUMMARY,
                              SITEID,
                              DATEPROCE,
                              CURYRATE,
                              ROUND,
                              TOTALDIFERENCE,
                              OBSERVATION,
                              STATUS,
                              DEBE,
                              HABER,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@BATNBRDIARYSUMMARY,
           @SITEID,
           @DATEPROCE,
           @CURYRATE,
           @ROUND,
           @TOTALDIFERENCE,
           @OBSERVATION,
           @STATUS,
           @DEBE,
           @HABER,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_SUMMARYDAY1: 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_SUMMARYDAY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_SUMMARYDAY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_SUMMARYDAY1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_SUMMARYDAY1 
 */

CREATE PROCEDURE sp_UpdProcOP_SUMMARYDAY1
(
    @BATNBRDIARYSUMMARY     numeric(10, 0),
    @SITEID                 char(5),
    @DATEPROCE              smalldatetime,
    @CURYRATE               numeric(6, 4),
    @ROUND                  numeric(9, 2),
    @TOTALDIFERENCE         numeric(9, 2),
    @OBSERVATION            varchar(100)              = NULL,
    @STATUS                 bit                       = NULL,
    @DEBE                   numeric(9, 2),
    @HABER                  numeric(9, 2),
    @USER1                  varchar(30)               = NULL,
    @USER2                  varchar(30)               = NULL,
    @USER3                  float                     = NULL,
    @USER4                  float                     = NULL,
    @TSTAMP                 timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SUMMARYDAY
       SET DATEPROCE               = @DATEPROCE,
           CURYRATE                = @CURYRATE,
           ROUND                   = @ROUND,
           TOTALDIFERENCE          = @TOTALDIFERENCE,
           OBSERVATION             = @OBSERVATION,
           STATUS                  = @STATUS,
           DEBE                    = @DEBE,
           HABER                   = @HABER,
           USER1                   = @USER1,
           USER2                   = @USER2,
           USER3                   = @USER3,
           USER4                   = @USER4
     WHERE BATNBRDIARYSUMMARY = @BATNBRDIARYSUMMARY
       AND SITEID             = @SITEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_SUMMARYDAY1 
 */

CREATE PROCEDURE sp_DelProcOP_SUMMARYDAY1
(
    @BATNBRDIARYSUMMARY     numeric(10, 0),
    @SITEID                 char(5))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_SUMMARYDAY
     WHERE BATNBRDIARYSUMMARY = @BATNBRDIARYSUMMARY
       AND SITEID             = @SITEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_SUMMARYDAY1: 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_SUMMARYDAY1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_SUMMARYDAY1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_SUMMARYDAY1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_SUMMARYDAY1 
 */

CREATE PROCEDURE sp_SelProcOP_SUMMARYDAY1
(
    @BATNBRDIARYSUMMARY     numeric(10, 0),
    @SITEID                 char(5))
AS
BEGIN
    SELECT BATNBRDIARYSUMMARY,
           SITEID,
           DATEPROCE,
           CURYRATE,
           ROUND,
           TOTALDIFERENCE,
           OBSERVATION,
           STATUS,
           DEBE,
           HABER,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_SUMMARYDAY
     WHERE BATNBRDIARYSUMMARY = @BATNBRDIARYSUMMARY
       AND SITEID             = @SITEID

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


/* 
 * PROCEDURE: sp_InsProcOP_SUMMARYSALES1 
 */

CREATE PROCEDURE sp_InsProcOP_SUMMARYSALES1
(
    @SITEID               char(5),
    @DATEPROCE            smalldatetime,
    @DOCSUMMARYTYPEID     char(2),
    @CLASSID              char(5),
    @CUSTIDSS             char(20)                 = NULL,
    @CUSTCLASSID          char(2)                  = NULL,
    @DEFINITIONID         char(2),
    @CLASSUBID            char(5)                  = NULL,
    @REFERENCE            char(15)                 = NULL,
    @TOTALBONUS           numeric(9, 2),
    @DIFFERENCE           numeric(9, 2),
    @COMMENT              varchar(40),
    @DEBE                 char(1),
    @HABER                char(1),
    @TOTALDEBE            numeric(9, 2),
    @TOTALHABER           numeric(9, 2),
    @TOTALPEN             numeric(9, 2),
    @TOTALUSD             numeric(9, 2),
    @STATUS               bit                      = NULL,
    @USER1                varchar(30)              = NULL,
    @USER2                varchar(30)              = NULL,
    @USER3                float                    = NULL,
    @USER4                float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_SUMMARYSALES(SITEID,
                                DATEPROCE,
                                DOCSUMMARYTYPEID,
                                CLASSID,
                                CUSTIDSS,
                                CUSTCLASSID,
                                DEFINITIONID,
                                CLASSUBID,
                                REFERENCE,
                                TOTALBONUS,
                                DIFFERENCE,
                                COMMENT,
                                DEBE,
                                HABER,
                                TOTALDEBE,
                                TOTALHABER,
                                TOTALPEN,
                                TOTALUSD,
                                STATUS,
                                USER1,
                                USER2,
                                USER3,
                                USER4 
                                )
    VALUES(@SITEID,
           @DATEPROCE,
           @DOCSUMMARYTYPEID,
           @CLASSID,
           @CUSTIDSS,
           @CUSTCLASSID,
           @DEFINITIONID,
           @CLASSUBID,
           @REFERENCE,
           @TOTALBONUS,
           @DIFFERENCE,
           @COMMENT,
           @DEBE,
           @HABER,
           @TOTALDEBE,
           @TOTALHABER,
           @TOTALPEN,
           @TOTALUSD,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcOP_SUMMARYSALES1: 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_SUMMARYSALES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcOP_SUMMARYSALES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcOP_SUMMARYSALES1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcOP_SUMMARYSALES1 
 */

CREATE PROCEDURE sp_UpdProcOP_SUMMARYSALES1
(
    @SITEID               char(5),
    @DATEPROCE            smalldatetime,
    @DOCSUMMARYTYPEID     char(2),
    @CLASSID              char(5),
    @CUSTIDSS             char(20)                 = NULL,
    @CUSTCLASSID          char(2)                  = NULL,
    @DEFINITIONID         char(2),
    @CLASSUBID            char(5)                  = NULL,
    @REFERENCE            char(15)                 = NULL,
    @TOTALBONUS           numeric(9, 2),
    @DIFFERENCE           numeric(9, 2),
    @COMMENT              varchar(40),
    @DEBE                 char(1),
    @HABER                char(1),
    @TOTALDEBE            numeric(9, 2),
    @TOTALHABER           numeric(9, 2),
    @TOTALPEN             numeric(9, 2),
    @TOTALUSD             numeric(9, 2),
    @STATUS               bit                      = NULL,
    @USER1                varchar(30)              = NULL,
    @USER2                varchar(30)              = NULL,
    @USER3                float                    = NULL,
    @USER4                float                    = NULL,
    @TSTAMP               timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_SUMMARYSALES
       SET DOCSUMMARYTYPEID      = @DOCSUMMARYTYPEID,
           CLASSID               = @CLASSID,
           CUSTIDSS              = @CUSTIDSS,
           CUSTCLASSID           = @CUSTCLASSID,
           DEFINITIONID          = @DEFINITIONID,
           CLASSUBID             = @CLASSUBID,
           REFERENCE             = @REFERENCE,
           TOTALBONUS            = @TOTALBONUS,
           DIFFERENCE            = @DIFFERENCE,
           COMMENT               = @COMMENT,
           DEBE                  = @DEBE,
           HABER                 = @HABER,
           TOTALDEBE             = @TOTALDEBE,
           TOTALHABER            = @TOTALHABER,
           TOTALPEN              = @TOTALPEN,
           TOTALUSD              = @TOTALUSD,
           STATUS                = @STATUS,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_SUMMARYSALES1 
 */

CREATE PROCEDURE sp_DelProcOP_SUMMARYSALES1
(
    @SITEID               char(5),
    @DATEPROCE            smalldatetime)
AS
BEGIN
    BEGIN TRAN

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

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcOP_SUMMARYSALES1: 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_SUMMARYSALES1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcOP_SUMMARYSALES1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcOP_SUMMARYSALES1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcOP_SUMMARYSALES1 
 */

CREATE PROCEDURE sp_SelProcOP_SUMMARYSALES1
(
    @SITEID               char(5),
    @DATEPROCE            smalldatetime)
AS
BEGIN
    SELECT SITEID,
           DATEPROCE,
           DOCSUMMARYTYPEID,
           CLASSID,
           CUSTIDSS,
           CUSTCLASSID,
           DEFINITIONID,
           CLASSUBID,
           REFERENCE,
           TOTALBONUS,
           DIFFERENCE,
           COMMENT,
           DEBE,
           HABER,
           TOTALDEBE,
           TOTALHABER,
           TOTALPEN,
           TOTALUSD,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_SUMMARYSALES
     WHERE SITEID    = @SITEID
       AND DATEPROCE = @DATEPROCE

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


/* 
 * PROCEDURE: sp_InsProcOP_TURNVSCARA1 
 */

CREATE PROCEDURE sp_InsProcOP_TURNVSCARA1
(
    @SITEID               char(5),
    @BATNBRTURNPERSON     tinyint,
    @DATEPROCE            smalldatetime,
    @NUMBERTURN           tinyint,
    @SALESPERID           char(10),
    @USER1                varchar(30)              = NULL,
    @USER2                varchar(30)              = NULL,
    @USER3                float                    = NULL,
    @USER4                float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_TURNVSCARA(SITEID,
                              BATNBRTURNPERSON,
                              DATEPROCE,
                              NUMBERTURN,
                              SALESPERID,
                              USER1,
                              USER2,
                              USER3,
                              USER4 
                              )
    VALUES(@SITEID,
           @BATNBRTURNPERSON,
           @DATEPROCE,
           @NUMBERTURN,
           @SALESPERID,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcOP_TURNVSCARA1 
 */

CREATE PROCEDURE sp_UpdProcOP_TURNVSCARA1
(
    @SITEID               char(5),
    @BATNBRTURNPERSON     tinyint,
    @DATEPROCE            smalldatetime,
    @NUMBERTURN           tinyint,
    @SALESPERID           char(10),
    @USER1                varchar(30)              = NULL,
    @USER2                varchar(30)              = NULL,
    @USER3                float                    = NULL,
    @USER4                float                    = NULL,
    @TSTAMP               timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_TURNVSCARA
       SET SALESPERID            = @SALESPERID,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_TURNVSCARA1 
 */

CREATE PROCEDURE sp_DelProcOP_TURNVSCARA1
(
    @SITEID               char(5),
    @BATNBRTURNPERSON     tinyint,
    @DATEPROCE            smalldatetime,
    @NUMBERTURN           tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_TURNVSCARA
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcOP_TURNVSCARA1 
 */

CREATE PROCEDURE sp_SelProcOP_TURNVSCARA1
(
    @SITEID               char(5),
    @BATNBRTURNPERSON     tinyint,
    @DATEPROCE            smalldatetime,
    @NUMBERTURN           tinyint)
AS
BEGIN
    SELECT SITEID,
           BATNBRTURNPERSON,
           DATEPROCE,
           NUMBERTURN,
           SALESPERID,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_TURNVSCARA
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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


/* 
 * PROCEDURE: sp_InsProcOP_TURNVSCARADET1 
 */

CREATE PROCEDURE sp_InsProcOP_TURNVSCARADET1
(
    @SITEID               char(5),
    @BATNBRTURNPERSON     tinyint,
    @DATEPROCE            smalldatetime,
    @NUMBERTURN           tinyint,
    @CARA                 char(2),
    @USER1                varchar(30)              = NULL,
    @USER2                varchar(30)              = NULL,
    @USER3                float                    = NULL,
    @USER4                float                    = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO OP_TURNVSCARADET(SITEID,
                                 BATNBRTURNPERSON,
                                 DATEPROCE,
                                 NUMBERTURN,
                                 CARA,
                                 USER1,
                                 USER2,
                                 USER3,
                                 USER4 
                                 )
    VALUES(@SITEID,
           @BATNBRTURNPERSON,
           @DATEPROCE,
           @NUMBERTURN,
           @CARA,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcOP_TURNVSCARADET1 
 */

CREATE PROCEDURE sp_UpdProcOP_TURNVSCARADET1
(
    @SITEID               char(5),
    @BATNBRTURNPERSON     tinyint,
    @DATEPROCE            smalldatetime,
    @NUMBERTURN           tinyint,
    @CARA                 char(2),
    @USER1                varchar(30)              = NULL,
    @USER2                varchar(30)              = NULL,
    @USER3                float                    = NULL,
    @USER4                float                    = NULL,
    @TSTAMP               timestamp                = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE OP_TURNVSCARADET
       SET CARA                  = @CARA,
           USER1                 = @USER1,
           USER2                 = @USER2,
           USER3                 = @USER3,
           USER4                 = @USER4
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcOP_TURNVSCARADET1 
 */

CREATE PROCEDURE sp_DelProcOP_TURNVSCARADET1
(
    @SITEID               char(5),
    @BATNBRTURNPERSON     tinyint,
    @DATEPROCE            smalldatetime,
    @NUMBERTURN           tinyint)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM OP_TURNVSCARADET
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcOP_TURNVSCARADET1 
 */

CREATE PROCEDURE sp_SelProcOP_TURNVSCARADET1
(
    @SITEID               char(5),
    @BATNBRTURNPERSON     tinyint,
    @DATEPROCE            smalldatetime,
    @NUMBERTURN           tinyint)
AS
BEGIN
    SELECT SITEID,
           BATNBRTURNPERSON,
           DATEPROCE,
           NUMBERTURN,
           CARA,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM OP_TURNVSCARADET
     WHERE SITEID           = @SITEID
       AND BATNBRTURNPERSON = @BATNBRTURNPERSON
       AND DATEPROCE        = @DATEPROCE
       AND NUMBERTURN       = @NUMBERTURN

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


/* 
 * PROCEDURE: sp_InsProcSE_ACCESSDETRIGHTS1 
 */

CREATE PROCEDURE sp_InsProcSE_ACCESSDETRIGHTS1
(
    @USERID           UD_USERID,
    @SCREENID         UD_SCREENID,
    @VIEWRIGHTS       bit,
    @INSERTRIGHTS     bit,
    @UPDATERIGHTS     bit,
    @DELETERIGHTS     bit,
    @PRINTRIGHTS      bit,
    @NULLRIGHTS       bit,
    @CLOSERIGHTS      bit,
    @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,
                                   USER1,
                                   USER2,
                                   USER3,
                                   USER4 
                                   )
    VALUES(@USERID,
           @SCREENID,
           @VIEWRIGHTS,
           @INSERTRIGHTS,
           @UPDATERIGHTS,
           @DELETERIGHTS,
           @PRINTRIGHTS,
           @NULLRIGHTS,
           @CLOSERIGHTS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcSE_ACCESSDETRIGHTS1: 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_ACCESSDETRIGHTS1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcSE_ACCESSDETRIGHTS1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcSE_ACCESSDETRIGHTS1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcSE_ACCESSDETRIGHTS1 
 */

CREATE PROCEDURE sp_UpdProcSE_ACCESSDETRIGHTS1
(
    @USERID           UD_USERID,
    @SCREENID         UD_SCREENID,
    @VIEWRIGHTS       bit,
    @INSERTRIGHTS     bit,
    @UPDATERIGHTS     bit,
    @DELETERIGHTS     bit,
    @PRINTRIGHTS      bit,
    @NULLRIGHTS       bit,
    @CLOSERIGHTS      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_ACCESSDETRIGHTS
       SET VIEWRIGHTS        = @VIEWRIGHTS,
           INSERTRIGHTS      = @INSERTRIGHTS,
           UPDATERIGHTS      = @UPDATERIGHTS,
           DELETERIGHTS      = @DELETERIGHTS,
           PRINTRIGHTS       = @PRINTRIGHTS,
           NULLRIGHTS        = @NULLRIGHTS,
           CLOSERIGHTS       = @CLOSERIGHTS,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE USERID   = @USERID
       AND SCREENID = @SCREENID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcSE_ACCESSDETRIGHTS1 
 */

CREATE PROCEDURE sp_DelProcSE_ACCESSDETRIGHTS1
(
    @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_ACCESSDETRIGHTS1: 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_ACCESSDETRIGHTS1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcSE_ACCESSDETRIGHTS1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcSE_ACCESSDETRIGHTS1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcSE_ACCESSDETRIGHTS1 
 */

CREATE PROCEDURE sp_SelProcSE_ACCESSDETRIGHTS1
(
    @USERID           UD_USERID,
    @SCREENID         UD_SCREENID)
AS
BEGIN
    SELECT USERID,
           SCREENID,
           VIEWRIGHTS,
           INSERTRIGHTS,
           UPDATERIGHTS,
           DELETERIGHTS,
           PRINTRIGHTS,
           NULLRIGHTS,
           CLOSERIGHTS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_ACCESSDETRIGHTS
     WHERE USERID   = @USERID
       AND SCREENID = @SCREENID

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


/* 
 * PROCEDURE: sp_InsProcSE_GROUPREC1 
 */

CREATE PROCEDURE sp_InsProcSE_GROUPREC1
(
    @GROUPID       UD_USERID,
    @GROUPNAME     UD_NOMBRE,
    @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,
                            USER1,
                            USER2,
                            USER3,
                            USER4 
                            )
    VALUES(@GROUPID,
           @GROUPNAME,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcSE_GROUPREC1: 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_GROUPREC1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcSE_GROUPREC1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcSE_GROUPREC1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcSE_GROUPREC1 
 */

CREATE PROCEDURE sp_UpdProcSE_GROUPREC1
(
    @GROUPID       UD_USERID,
    @GROUPNAME     UD_NOMBRE,
    @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,
           USER1          = @USER1,
           USER2          = @USER2,
           USER3          = @USER3,
           USER4          = @USER4
     WHERE GROUPID = @GROUPID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcSE_GROUPREC1 
 */

CREATE PROCEDURE sp_DelProcSE_GROUPREC1
(
    @GROUPID       UD_USERID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_GROUPREC
     WHERE GROUPID = @GROUPID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcSE_GROUPREC1: 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_GROUPREC1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcSE_GROUPREC1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcSE_GROUPREC1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcSE_GROUPREC1 
 */

CREATE PROCEDURE sp_SelProcSE_GROUPREC1
(
    @GROUPID       UD_USERID)
AS
BEGIN
    SELECT GROUPID,
           GROUPNAME,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_GROUPREC
     WHERE GROUPID = @GROUPID

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


/* 
 * PROCEDURE: sp_InsProcSE_MODULE1 
 */

CREATE PROCEDURE sp_InsProcSE_MODULE1
(
    @MODULEID       UD_MODULOID,
    @MODULENAME     UD_NOMBRE,
    @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,
                          USER1,
                          USER2,
                          USER3,
                          USER4 
                          )
    VALUES(@MODULEID,
           @MODULENAME,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcSE_MODULE1: 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_MODULE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcSE_MODULE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcSE_MODULE1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcSE_MODULE1 
 */

CREATE PROCEDURE sp_UpdProcSE_MODULE1
(
    @MODULEID       UD_MODULOID,
    @MODULENAME     UD_NOMBRE,
    @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,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE MODULEID = @MODULEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcSE_MODULE1 
 */

CREATE PROCEDURE sp_DelProcSE_MODULE1
(
    @MODULEID       UD_MODULOID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_MODULE
     WHERE MODULEID = @MODULEID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcSE_MODULE1: 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_MODULE1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcSE_MODULE1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcSE_MODULE1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcSE_MODULE1 
 */

CREATE PROCEDURE sp_SelProcSE_MODULE1
(
    @MODULEID       UD_MODULOID)
AS
BEGIN
    SELECT MODULEID,
           MODULENAME,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_MODULE
     WHERE MODULEID = @MODULEID

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


/* 
 * PROCEDURE: sp_InsProcSE_SCREEN1 
 */

CREATE PROCEDURE sp_InsProcSE_SCREEN1
(
    @SCREENID       UD_SCREENID,
    @MODULEID       UD_MODULOID,
    @SCREENNAME     UD_NOMBRE,
    @SCREENTYPE     char(1),
    @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,
                          USER1,
                          USER2,
                          USER3,
                          USER4 
                          )
    VALUES(@SCREENID,
           @MODULEID,
           @SCREENNAME,
           @SCREENTYPE,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcSE_SCREEN1: 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_SCREEN1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcSE_SCREEN1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcSE_SCREEN1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcSE_SCREEN1 
 */

CREATE PROCEDURE sp_UpdProcSE_SCREEN1
(
    @SCREENID       UD_SCREENID,
    @MODULEID       UD_MODULOID,
    @SCREENNAME     UD_NOMBRE,
    @SCREENTYPE     char(1),
    @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,
           USER1           = @USER1,
           USER2           = @USER2,
           USER3           = @USER3,
           USER4           = @USER4
     WHERE SCREENID = @SCREENID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcSE_SCREEN1 
 */

CREATE PROCEDURE sp_DelProcSE_SCREEN1
(
    @SCREENID       UD_SCREENID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_SCREEN
     WHERE SCREENID = @SCREENID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcSE_SCREEN1: 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_SCREEN1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcSE_SCREEN1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcSE_SCREEN1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcSE_SCREEN1 
 */

CREATE PROCEDURE sp_SelProcSE_SCREEN1
(
    @SCREENID       UD_SCREENID)
AS
BEGIN
    SELECT SCREENID,
           MODULEID,
           SCREENNAME,
           SCREENTYPE,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_SCREEN
     WHERE SCREENID = @SCREENID

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


/* 
 * PROCEDURE: sp_InsProcSE_USERGROUP1 
 */

CREATE PROCEDURE sp_InsProcSE_USERGROUP1
(
    @USERID      UD_USERID,
    @GROUPID     UD_USERID,
    @USER1       char(30)             = NULL,
    @USER2       char(30)             = NULL,
    @USER3       float                = NULL,
    @USER4       float                = NULL)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO SE_USERGROUP(USERID,
                             GROUPID,
                             USER1,
                             USER2,
                             USER3,
                             USER4 
                             )
    VALUES(@USERID,
           @GROUPID,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcSE_USERGROUP1: 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_USERGROUP1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcSE_USERGROUP1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcSE_USERGROUP1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcSE_USERGROUP1 
 */

CREATE PROCEDURE sp_UpdProcSE_USERGROUP1
(
    @USERID      UD_USERID,
    @GROUPID     UD_USERID,
    @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 USER1        = @USER1,
           USER2        = @USER2,
           USER3        = @USER3,
           USER4        = @USER4
     WHERE USERID  = @USERID
       AND GROUPID = @GROUPID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcSE_USERGROUP1 
 */

CREATE PROCEDURE sp_DelProcSE_USERGROUP1
(
    @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_USERGROUP1: 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_USERGROUP1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcSE_USERGROUP1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcSE_USERGROUP1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcSE_USERGROUP1 
 */

CREATE PROCEDURE sp_SelProcSE_USERGROUP1
(
    @USERID      UD_USERID,
    @GROUPID     UD_USERID)
AS
BEGIN
    SELECT USERID,
           GROUPID,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_USERGROUP
     WHERE USERID  = @USERID
       AND GROUPID = @GROUPID

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


/* 
 * PROCEDURE: sp_InsProcSE_USERREC1 
 */

CREATE PROCEDURE sp_InsProcSE_USERREC1
(
    @USERID           UD_USERID,
    @USERNAME         UD_NOMBRE,
    @EMAILADDRESS     varchar(40)              = NULL,
    @LOCATION         varchar(40),
    @PASSWORD         char(12),
    @PHONE            char(30)                 = NULL,
    @EXPIRED          smalldatetime,
    @STATUS           bit,
    @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,
                           USER1,
                           USER2,
                           USER3,
                           USER4 
                           )
    VALUES(@USERID,
           @USERNAME,
           @EMAILADDRESS,
           @LOCATION,
           @PASSWORD,
           @PHONE,
           @EXPIRED,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'sp_InsProcSE_USERREC1: 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_USERREC1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcSE_USERREC1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcSE_USERREC1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcSE_USERREC1 
 */

CREATE PROCEDURE sp_UpdProcSE_USERREC1
(
    @USERID           UD_USERID,
    @USERNAME         UD_NOMBRE,
    @EMAILADDRESS     varchar(40)              = NULL,
    @LOCATION         varchar(40),
    @PASSWORD         char(12),
    @PHONE            char(30)                 = NULL,
    @EXPIRED          smalldatetime,
    @STATUS           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_USERREC
       SET USERNAME          = @USERNAME,
           EMAILADDRESS      = @EMAILADDRESS,
           LOCATION          = @LOCATION,
           PASSWORD          = @PASSWORD,
           PHONE             = @PHONE,
           EXPIRED           = @EXPIRED,
           STATUS            = @STATUS,
           USER1             = @USER1,
           USER2             = @USER2,
           USER3             = @USER3,
           USER4             = @USER4
     WHERE USERID = @USERID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcSE_USERREC1 
 */

CREATE PROCEDURE sp_DelProcSE_USERREC1
(
    @USERID           UD_USERID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM SE_USERREC
     WHERE USERID = @USERID

    IF (@@error!=0)
    BEGIN
        RAISERROR  20002 'sp_DelProcSE_USERREC1: 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_USERREC1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcSE_USERREC1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcSE_USERREC1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcSE_USERREC1 
 */

CREATE PROCEDURE sp_SelProcSE_USERREC1
(
    @USERID           UD_USERID)
AS
BEGIN
    SELECT USERID,
           USERNAME,
           EMAILADDRESS,
           LOCATION,
           PASSWORD,
           PHONE,
           EXPIRED,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM SE_USERREC
     WHERE USERID = @USERID

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


/* 
 * PROCEDURE: sp_InsProcSITE1 
 */

CREATE PROCEDURE sp_InsProcSITE1
(
    @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),
    @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,
                     USER1,
                     USER2,
                     USER3,
                     USER4,
                     USER5,
                     USER6,
                     USER7,
                     USER8,
                     USER9 
                     )
    VALUES(@SITEID,
           @CITYID,
           @COUNTRYID,
           @NAME,
           @ADDR1,
           @ADDR2,
           @FONO1,
           @FONO2,
           @FAX,
           @STATUS,
           @STARTDATEINVEN,
           @ENDDATEINVEN,
           @ADJUST,
           @ATTN,
           @USER1,
           @USER2,
           @USER3,
           @USER4,
           @USER5,
           @USER6,
           @USER7,
           @USER8,
           @USER9 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcSITE1 
 */

CREATE PROCEDURE sp_UpdProcSITE1
(
    @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),
    @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,
           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_UpdProcSITE1: Cannot update  in SITE '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcSITE1 
 */

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

    DELETE
      FROM SITE
     WHERE SITEID = @SITEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcSITE1 
 */

CREATE PROCEDURE sp_SelProcSITE1
(
    @SITEID             UD_IDSITE)
AS
BEGIN
    SELECT SITEID,
           CITYID,
           COUNTRYID,
           NAME,
           ADDR1,
           ADDR2,
           FONO1,
           FONO2,
           FAX,
           STATUS,
           STARTDATEINVEN,
           ENDDATEINVEN,
           ADJUST,
           ATTN,
           USER1,
           USER2,
           USER3,
           USER4,
           USER5,
           USER6,
           USER7,
           USER8,
           USER9,
           TSTAMP
      FROM SITE
     WHERE SITEID = @SITEID

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


/* 
 * PROCEDURE: sp_InsProcSTATE1 
 */

CREATE PROCEDURE sp_InsProcSTATE1
(
    @STATEID     char(2),
    @DESCR       UD_DESCRIPCION)
AS
BEGIN
    BEGIN TRAN

    INSERT INTO STATE(STATEID,
                      DESCR 
                      )
    VALUES(@STATEID,
           @DESCR 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcSTATE1 
 */

CREATE PROCEDURE sp_UpdProcSTATE1
(
    @STATEID     char(2),
    @DESCR       UD_DESCRIPCION,
    @TSTAMP      timestamp                 = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE STATE
       SET DESCR        = @DESCR
     WHERE STATEID = @STATEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcSTATE1 
 */

CREATE PROCEDURE sp_DelProcSTATE1
(
    @STATEID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM STATE
     WHERE STATEID = @STATEID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcSTATE1 
 */

CREATE PROCEDURE sp_SelProcSTATE1
(
    @STATEID     char(2))
AS
BEGIN
    SELECT STATEID,
           DESCR,
           TSTAMP
      FROM STATE
     WHERE STATEID = @STATEID

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


/* 
 * PROCEDURE: sp_InsProcTAXS1 
 */

CREATE PROCEDURE sp_InsProcTAXS1
(
    @TAXID      UD_TAXID,
    @DESCR      UD_DESCRIPCION,
    @VALUE      numeric(5, 2),
    @STATUS     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,
                     STATUS,
                     USER1,
                     USER2,
                     USER3,
                     USER4 
                     )
    VALUES(@TAXID,
           @DESCR,
           @VALUE,
           @STATUS,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcTAXS1 
 */

CREATE PROCEDURE sp_UpdProcTAXS1
(
    @TAXID      UD_TAXID,
    @DESCR      UD_DESCRIPCION,
    @VALUE      numeric(5, 2),
    @STATUS     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,
           STATUS      = @STATUS,
           USER1       = @USER1,
           USER2       = @USER2,
           USER3       = @USER3,
           USER4       = @USER4
     WHERE TAXID = @TAXID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcTAXS1 
 */

CREATE PROCEDURE sp_DelProcTAXS1
(
    @TAXID      UD_TAXID)
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TAXS
     WHERE TAXID = @TAXID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcTAXS1 
 */

CREATE PROCEDURE sp_SelProcTAXS1
(
    @TAXID      UD_TAXID)
AS
BEGIN
    SELECT TAXID,
           DESCR,
           VALUE,
           STATUS,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM TAXS
     WHERE TAXID = @TAXID

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


/* 
 * PROCEDURE: sp_InsProcTERMS1 
 */

CREATE PROCEDURE sp_InsProcTERMS1
(
    @TERMID       char(2),
    @DUEINTRV     tinyint,
    @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 TERMS(TERMID,
                      DUEINTRV,
                      DESCR,
                      USER1,
                      USER2,
                      USER3,
                      USER4 
                      )
    VALUES(@TERMID,
           @DUEINTRV,
           @DESCR,
           @USER1,
           @USER2,
           @USER3,
           @USER4 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcTERMS1 
 */

CREATE PROCEDURE sp_UpdProcTERMS1
(
    @TERMID       char(2),
    @DUEINTRV     tinyint,
    @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 TERMS
       SET DUEINTRV      = @DUEINTRV,
           DESCR         = @DESCR,
           USER1         = @USER1,
           USER2         = @USER2,
           USER3         = @USER3,
           USER4         = @USER4
     WHERE TERMID = @TERMID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcTERMS1 
 */

CREATE PROCEDURE sp_DelProcTERMS1
(
    @TERMID       char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TERMS
     WHERE TERMID = @TERMID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcTERMS1 
 */

CREATE PROCEDURE sp_SelProcTERMS1
(
    @TERMID       char(2))
AS
BEGIN
    SELECT TERMID,
           DUEINTRV,
           DESCR,
           USER1,
           USER2,
           USER3,
           USER4,
           TSTAMP
      FROM TERMS
     WHERE TERMID = @TERMID

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


/* 
 * PROCEDURE: sp_InsProcTMP_IN_INVENTORYALTER1 
 */

CREATE PROCEDURE sp_InsProcTMP_IN_INVENTORYALTER1
(
    @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_INVENTORYALTER1: 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_INVENTORYALTER1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcTMP_IN_INVENTORYALTER1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcTMP_IN_INVENTORYALTER1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcTMP_IN_INVENTORYALTER1 
 */

CREATE PROCEDURE sp_UpdProcTMP_IN_INVENTORYALTER1
(
    @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_INVENTORYALTER1: Cannot update  in TMP_IN_INVENTORYALTER '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcTMP_IN_INVENTORYALTER1 
 */

CREATE PROCEDURE sp_DelProcTMP_IN_INVENTORYALTER1
(
    @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_INVENTORYALTER1: 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_INVENTORYALTER1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcTMP_IN_INVENTORYALTER1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcTMP_IN_INVENTORYALTER1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcTMP_IN_INVENTORYALTER1 
 */

CREATE PROCEDURE sp_SelProcTMP_IN_INVENTORYALTER1
(
    @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_INVENTORYALTER1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcTMP_IN_INVENTORYALTER1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcTMP_IN_INVENTORYALTER1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcTMP_IN_INVENTORYDAT1 
 */

CREATE PROCEDURE sp_InsProcTMP_IN_INVENTORYDAT1
(
    @CORRNBR            int,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @STOCKSTAR          UD_STOCK,
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       smalldatetime               = NULL,
    @ENDDATEINV         smalldatetime               = NULL,
    @LASTINVFIS         UD_STOCK,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         UD_COSTO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @LASTSTDCOSTPEN     UD_COSTO,
    @LASTSTDCOSTUSD     UD_COSTO,
    @SLS_PRICE          UD_COSTO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID                 = NULL,
    @INVTIDSTK          char(20),
    @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_INVENTORYDAT1: 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_INVENTORYDAT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcTMP_IN_INVENTORYDAT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcTMP_IN_INVENTORYDAT1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcTMP_IN_INVENTORYDAT1 
 */

CREATE PROCEDURE sp_UpdProcTMP_IN_INVENTORYDAT1
(
    @CORRNBR            int,
    @INVTIDSKU          UD_IDCORPORATIVO,
    @SITEID             UD_IDSITE,
    @STOCKMIN           UD_STOCK,
    @STOCKMAX           UD_STOCK,
    @STOCKSTAR          UD_STOCK,
    @QTYAVAIL           UD_STOCK,
    @STARTDATEINV       smalldatetime               = NULL,
    @ENDDATEINV         smalldatetime               = NULL,
    @LASTINVFIS         UD_STOCK,
    @AVGCOSTPEN         UD_COSTO,
    @AVGCOSTUSD         UD_COSTO,
    @STDCOSTPEN         UD_COSTO,
    @STDCOSTUSD         UD_COSTO,
    @LASTSTDCOSTPEN     UD_COSTO,
    @LASTSTDCOSTUSD     UD_COSTO,
    @SLS_PRICE          UD_COSTO,
    @LASTDAYIN          smalldatetime               = NULL,
    @LASTDAYOU          smalldatetime               = NULL,
    @PRECLVID           UD_PRECLVID                 = NULL,
    @INVTIDSTK          char(20),
    @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_INVENTORYDAT1: Cannot update  in TMP_IN_INVENTORYDAT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcTMP_IN_INVENTORYDAT1 
 */

CREATE PROCEDURE sp_DelProcTMP_IN_INVENTORYDAT1
(
    @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_INVENTORYDAT1: 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_INVENTORYDAT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcTMP_IN_INVENTORYDAT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcTMP_IN_INVENTORYDAT1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcTMP_IN_INVENTORYDAT1 
 */

CREATE PROCEDURE sp_SelProcTMP_IN_INVENTORYDAT1
(
    @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_INVENTORYDAT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcTMP_IN_INVENTORYDAT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcTMP_IN_INVENTORYDAT1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcTMP_IN_VENDORINVT1 
 */

CREATE PROCEDURE sp_InsProcTMP_IN_VENDORINVT1
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SITEID        UD_IDSITE,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @STDCOST       UD_COSTO,
    @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_VENDORINVT1: 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_VENDORINVT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcTMP_IN_VENDORINVT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcTMP_IN_VENDORINVT1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcTMP_IN_VENDORINVT1 
 */

CREATE PROCEDURE sp_UpdProcTMP_IN_VENDORINVT1
(
    @CORRNBR       int,
    @VENDIDSS      UD_IDVENDOR,
    @INVTIDSKU     UD_IDCORPORATIVO,
    @SITEID        UD_IDSITE,
    @BUYCOD        varchar(20),
    @DESCR         UD_DESCRIPCION,
    @STDCOST       UD_COSTO,
    @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_VENDORINVT1: Cannot update  in TMP_IN_VENDORINVT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcTMP_IN_VENDORINVT1 
 */

CREATE PROCEDURE sp_DelProcTMP_IN_VENDORINVT1
(
    @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_VENDORINVT1: 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_VENDORINVT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcTMP_IN_VENDORINVT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcTMP_IN_VENDORINVT1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcTMP_IN_VENDORINVT1 
 */

CREATE PROCEDURE sp_SelProcTMP_IN_VENDORINVT1
(
    @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_VENDORINVT1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcTMP_IN_VENDORINVT1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcTMP_IN_VENDORINVT1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcTMP_IN_VENDORINVTREL1 
 */

CREATE PROCEDURE sp_InsProcTMP_IN_VENDORINVTREL1
(
    @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_VENDORINVTREL1: 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_VENDORINVTREL1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_InsProcTMP_IN_VENDORINVTREL1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_InsProcTMP_IN_VENDORINVTREL1 >>>'
go


/* 
 * PROCEDURE: sp_UpdProcTMP_IN_VENDORINVTREL1 
 */

CREATE PROCEDURE sp_UpdProcTMP_IN_VENDORINVTREL1
(
    @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_VENDORINVTREL1: Cannot update  in TMP_IN_VENDORINVTREL '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcTMP_IN_VENDORINVTREL1 
 */

CREATE PROCEDURE sp_DelProcTMP_IN_VENDORINVTREL1
(
    @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_VENDORINVTREL1: 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_VENDORINVTREL1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_DelProcTMP_IN_VENDORINVTREL1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_DelProcTMP_IN_VENDORINVTREL1 >>>'
go


/* 
 * PROCEDURE: sp_SelProcTMP_IN_VENDORINVTREL1 
 */

CREATE PROCEDURE sp_SelProcTMP_IN_VENDORINVTREL1
(
    @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_VENDORINVTREL1') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE sp_SelProcTMP_IN_VENDORINVTREL1 >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE sp_SelProcTMP_IN_VENDORINVTREL1 >>>'
go


/* 
 * PROCEDURE: sp_InsProcTYPEPAINMENT1 
 */

CREATE PROCEDURE sp_InsProcTYPEPAINMENT1
(
    @TYPEPAINMENTID     char(2),
    @DESCR              char(40))
AS
BEGIN
    BEGIN TRAN

    INSERT INTO TYPEPAINMENT(TYPEPAINMENTID,
                             DESCR 
                             )
    VALUES(@TYPEPAINMENTID,
           @DESCR 
           )

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_UpdProcTYPEPAINMENT1 
 */

CREATE PROCEDURE sp_UpdProcTYPEPAINMENT1
(
    @TYPEPAINMENTID     char(2),
    @DESCR              char(40),
    @TSTAMP             timestamp            = NULL)
AS
BEGIN
    BEGIN TRAN

    UPDATE TYPEPAINMENT
       SET DESCR               = @DESCR
     WHERE TYPEPAINMENTID = @TYPEPAINMENTID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_DelProcTYPEPAINMENT1 
 */

CREATE PROCEDURE sp_DelProcTYPEPAINMENT1
(
    @TYPEPAINMENTID     char(2))
AS
BEGIN
    BEGIN TRAN

    DELETE
      FROM TYPEPAINMENT
     WHERE TYPEPAINMENTID = @TYPEPAINMENTID

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

    COMMIT TRAN

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


/* 
 * PROCEDURE: sp_SelProcTYPEPAINMENT1 
 */

CREATE PROCEDURE sp_SelProcTYPEPAINMENT1
(
    @TYPEPAINMENTID     char(2))
AS
BEGIN
    SELECT TYPEPAINMENTID,
           DESCR,
           TSTAMP
      FROM TYPEPAINMENT
     WHERE TYPEPAINMENTID = @TYPEPAINMENTID

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


