DROP PROCEDURE IN_INTRAHEADInsProc
GO
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