ALTER TABLE Trn_MudOrdenConc ADD FechaReg SMALLDATETIME,CdUsuario VARCHAR(11)
GO
ALTER TABLE Trn_MudOrdenOper ADD TipoReg INT DEFAULT(0) NOT NULL,FechaReg SMALLDATETIME,CdUsuario VARCHAR(11)
,Descripcion VARCHAR(500)
GO
ALTER TABLE Trn_MudInventDet ADD TipoIngreso VARCHAR(10),FechaReg SMALLDATETIME,CdUsuario VARCHAR(11)
GO
CREATE TABLE Trn_MudOrdenNov ( 
TipDoc VARCHAR(3) DEFAULT ('OSM') NOT NULL,
NumOrden INT DEFAULT ((0)) NOT NULL,
IdCia CHAR(2) NOT NULL,
Item INT DEFAULT ((0)) NOT NULL,
Fecha SMALLDATETIME NOT NULL,
TipoNov VARCHAR(10) NOT NULL,
Descripcion VARCHAR(500),
EstadoBod INT DEFAULT ((0)) NOT NULL,
CdBodAnt VARCHAR(4),
CdBodega VARCHAR(4),
FechaIngBod SMALLDATETIME,
FechaRetBod SMALLDATETIME,
CdConcepto VARCHAR(4),
ValorTotal MONEY DEFAULT ((0)),
Referencia VARCHAR(50),
IdUsuario VARCHAR(11) NOT NULL,
FechaCrea SMALLDATETIME NOT NULL
 CONSTRAINT PK_Trn_MudOrdenNov PRIMARY KEY CLUSTERED (TipDoc,NumOrden,IdCia,Item),
 CONSTRAINT CK_Trn_MudOrdenNovIdCia CHECK ((len([IdCia])>(0))),
 CONSTRAINT CK_Trn_MudOrdenNovIdUsuario CHECK ((len([IdUsuario])>(0))),
 CONSTRAINT CK_Trn_MudOrdenNovTipDoc CHECK ((len([TipDoc])>(0))),
 CONSTRAINT CK_Trn_MudOrdenNovTipoNov CHECK ((len([TipoNov])>(0))))
GO
INSERT INTO  Sys_Roles (IdObj,IdRole,Funcion)
 VALUES ('FRMDOSM','CER','Permitir Cerrar Orden')
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsMudOrdenConc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsMudOrdenConc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryMudInventario_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryMudInventario_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryMudOrdenOper_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryMudOrdenOper_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryMudOrdenOperRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryMudOrdenOperRel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryMudOrdenServ_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryMudOrdenServ_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryMudOrdenServRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryMudOrdenServRel]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenServRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecDespacho,O.IdTipoServ AS CdTipoServ,TipoServicio,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia
,O.IdOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino
,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest,O.VrServicio,O.VrEmpaque,O.VrBodega,O.VrOtros,O.VrDescuento,O.VrImpuesto,O.VrNeto,O.VrDeclarado,O.VrSeguro
,O.IdVend,VN.RazonSocial AS NomVendedor,O.NomContacto,O.TelsContacto,O.emlContacto,O.ContactoDest,O.emlContDest,O.Servicios,O.FormaPago,O.Referencia,O.TipInv,O.NumInvent,O.IdCiaInv,O.TipRem,O.NumRemesa,O.IdCiaRem
,O.EstFactura,O.TipCausac,O.Causacion,O.CdCiaCau,O.EstCumplido,O.FechaCump,O.TipoRuta,O.Volumen,EstadoBod,O.CdBodega,B.Bodega AS NomBodega,FechaIngBod,FechaRetBod,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
--Detalles de orden
,D.Item,D.IdConcepto AS CdConcepto,D.Descripcion,CD.Concepto,D.ValorTotal,D.TarifaIva,D.TipoConc,D.TipoRubro,D.CdCuenta,D.NitTercero,NT.RazonSocial AS NomTercero
,D.Referencia AS DetReferencia,D.NumDocRef,D.TipoReg,D.TarifSeguro,D.CodTarSeg,D.PlacaVehic,D.TarifaPago,D.NumDocFac,D.IndFactItem,D.FechaReg,D.CdUsuario AS DetCdUsuario,UC.Usuario AS DetUsuario,TS.Transporte,TS.Bodega,TS.Empaque,TS.Personal
--datos de facturas
,FC.FacNumero,FC.FacFecha,ISNULL(FC.FacValor,0) AS FacVrTotal,ISNULL(FC.FacCosto,0) AS FacVrCosto,ISNULL(FC.FacImpuesto,0) AS FacVrIva,ISNULL(FC.FacSeguro,0) AS FacVrSeguro
--datos del tercero
,T.Codigo AS TercCod,T.TipoId,T.Dv,T.Direccion,T.IdLocal AS TercCdLocal,L.Localidad AS TercLocalidad,T.Telefono AS TercTelefono,T.TelMovil AS TercTerCelular,T.e_mail AS TercEmail
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupo,GrupoClie,CLI.IdPlazo AS CdPlazo,Plazo
,V.NumVeh,V.TipoAfil,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,V.Modelo,V.IdPoseedor AS NitPoseedor,NP.RazonSocial AS Poseedor
,V.IdConductor AS CedConductor,CDT.RazonSocial AS Conductor,V.NumSoat,V.VigSoat
,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,U.Usuario
 FROM Trn_MudOrdenServ AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie
 INNER JOIN Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN MudServicios AS TS ON O.IdTipoServ=TS.IdTipoServ
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_MudOrdenConc AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto 
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona 
 LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo LEFT JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia 
 LEFT JOIN AlmBodegas AS B ON O.CdBodega=B.IdBodega
 LEFT JOIN Vehiculos AS V ON D.PlacaVehic=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Marcas AS M ON V.IdMarca=M.IdMarca LEFT JOIN TiposCol AS CLR ON V.IdColor=CLR.IdColor
 LEFT JOIN Terceros AS NP ON V.IdPoseedor=NP.IdTercero LEFT JOIN Terceros AS CDT ON V.IdConductor=CDT.IdTercero
 LEFT JOIN adm_Usuarios AS UC ON D.CdUsuario=UC.IdUsuario
 --Facturas item por item
 LEFT JOIN (SELECT TipRem AS FacTipOrd,Remesa AS FacNumOrden,IdCiaRem AS FacIdCiaOrd,ItemRem AS FacItemOrd,MAX(FecRemesa) AS FacFecha
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN (Cantidad*VrUnitario)*-1 ELSE Cantidad*VrUnitario END) AS FacValor
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN (Cantidad*VrCosto)*-1 ELSE Cantidad*VrCosto END) AS FacCosto
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN VrImpuesto*-1 ELSE VrImpuesto END) AS FacImpuesto
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN VrSeguroRem*-1 ELSE VrSeguroRem END) AS FacSeguro
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN VrDeclMcia ELSE 0 END) AS FacDeclarado
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Factura ELSE 0 END) AS FacNumero
 FROM Trn_TraFacRemesas WHERE TipRem='OSM' GROUP BY TipRem,Remesa,IdCiaRem,ItemRem) AS FC ON D.TipDoc=FC.FacTipOrd AND D.NumOrden=FC.FacNumOrden AND D.IdCia=FC.FacIdCiaOrd AND D.Item=FC.FacItemOrd
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenOperRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
SELECT O.TipDoc,O.NumOrden,O.IdCia,O.FecDespacho,O.IdCliente,D.Item,D.IdOperario,NomOperario,D.IdTipoOper,TipoOperario
,D.CdConcepto,C.Concepto,C.IdCuenta,C.TipoConc,D.TarifaCargo,D.PlacaVehic,D.Descripcion,D.TipoReg,D.FechaReg,D.CdUsuario,Usuario
,V.NumVeh,V.IdMarca AS CdMarca,M.Marca,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.Modelo,V.IdColor AS CdColor,NomColor,V.Config
,V.IdPoseedor AS CdPoseedor,T.RazonSocial AS Poseedor,V.TipoAfil
FROM Trn_MudOrdenServ AS O INNER JOIN Trn_MudOrdenOper AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
INNER JOIN TiposOperarios AS TP ON D.IdTipoOper=TP.IdOper LEFT JOIN TraConcCausac AS C ON D.CdConcepto=C.IdConcepto
LEFT JOIN Vehiculos AS V ON D.PlacaVehic=V.IdVehiculo
LEFT JOIN Marcas AS M ON V.IdMarca=M.IdMarca LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor
LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh LEFT JOIN Terceros AS T ON V.IdPoseedor=T.IdTercero
LEFT JOIN adm_Usuarios AS U ON D.CdUsuario=U.IdUsuario 
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenServ_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecDespacho,O.IdTipoServ AS CdTipoServ,TipoServicio,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia
,O.IdOrigen,LO.Localidad AS CiuOrigen,O.IdDestino,LD.Localidad AS CiuDestino,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest
,O.VrServicio,O.VrEmpaque,O.VrBodega,O.VrOtros,O.VrDescuento,O.VrImpuesto,O.VrNeto,O.VrDeclarado,O.VrSeguro,O.IdVend,VN.RazonSocial AS NomVendedor
,O.NomContacto,O.TelsContacto,O.emlContacto,O.ContactoDest,O.emlContDest,O.Servicios,O.FormaPago,O.Referencia,O.TipInv,O.NumInvent,O.IdCiaInv,O.TipRem,O.NumRemesa,O.IdCiaRem
,O.EstFactura,O.TipFact,O.NumFactura,O.CdCiaFact,O.TipCausac,O.Causacion,O.CdCiaCau,EstCumplido,FechaCump,TipoRuta,O.Volumen,EstadoBod,O.CdBodega,FechaIngBod,FechaRetBod,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
,D.Item,D.IdConcepto AS CdConcepto,D.Descripcion,CD.Concepto,D.ValorTotal,D.TarifaIva,D.TipoConc,D.TipoRubro,D.CdCuenta,D.NitTercero,NT.RazonSocial AS NomTercero
,D.Referencia AS DetReferencia,D.NumDocRef,D.NumDocFac,D.IndFactItem,D.TipoReg,D.TarifSeguro,D.CodTarSeg,D.PlacaVehic,D.TarifaPago,D.FechaReg,D.CdUsuario AS DetCdUsuario,UC.Usuario AS DetUsuario,TS.Transporte,TS.Bodega,TS.Empaque,TS.Personal
,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,U.Usuario
,T.Codigo AS TercCod,T.TipoId,T.Dv,T.Direccion,T.IdLocal AS TercCdLocal,L.Localidad AS TercLocalidad,T.Telefono AS TercTelefono,T.TelMovil AS TercTerCelular,T.e_mail AS TercEmail
 FROM Trn_MudOrdenServ AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie
 INNER JOIN Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN MudServicios AS TS ON O.IdTipoServ=TS.IdTipoServ
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_MudOrdenConc AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto 
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN adm_Usuarios AS UC ON D.CdUsuario=UC.IdUsuario
 WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudInventario_Cr] @pmTipDoc VARCHAR(3),@pmNumInventIni INT,@pmNumInventFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.TipDoc,TipoDoc,M.NumInvent,M.IdCia AS CdCia,Compania,M.Fecha,M.FecDespacho,M.IdTipoServ AS CdTipoServ,TipoServicio,M.IdCliente,T.RazonSocial AS NomCliente,M.IdAgencia AS CdAgencia,Agencia
,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest
,M.IdOrigen,LO.Localidad AS CiuOrigen,DPO.Departamento AS DptoOrigen,M.IdDestino,LD.Localidad AS CiuDestino,DPD.Departamento AS DptoDestino,M.pVehiculo,M.CdConductor,NC.RazonSocial AS Conductor
,M.nRemolque,M.TipoAfiVehic,M.NitEmpresa,M.NomEmpresa,CajasEmp,CajasClie,M.VrDeclarado,M.VrServicio,M.NumCotizac,M.NumPlanilla,NumActa,M.TipOsm,M.NumOrden,M.IdCiaOsm,M.NomContacto,M.TelsContacto
,M.emlContacto,M.Responsable,M.FormaPago,M.Remitente AS ContactoCargue,M.Destinatario AS ContactoDestino,M.Anulado,M.FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,ED.Estado
,M.OrigenAdd,M.TimeSys AS FechaCrea,M.FecUpdate,M.IdCiaCrea AS CdCiaCrea,M.IdUsuario AS CdUsuario,U.Usuario
,D.Item,D.IdArticulo AS CdArticulo,D.Descripcion,D.Cantidad,D.CantSalida,D.IdEstado AS CdEstadoArt,EA.Estado AS EstadoArt,D.Observacion AS DetObserv,Articulo,MA.IdSeccion AS CdSeccion,Seccion,EA.NColor AS NColorEst
,D.TipoIngreso,D.FechaReg,D.CdUsuario AS DetCdUsuario,UD.Usuario AS DetUsuario
FROM Trn_MudInventario AS M INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN Terceros AS T ON M.IdCliente=T.IdTercero
INNER JOIN MudServicios AS TS ON M.IdTipoServ=TS.IdTipoServ INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON M.TipDoc=TD.IdDoc
INNER JOIN Localidades AS LO ON M.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
INNER JOIN Localidades AS LD ON M.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
INNER JOIN Trn_MudInventDet AS D ON M.TipDoc=D.TipDoc AND M.NumInvent=D.NumInvent AND M.IdCia=D.IdCia
INNER JOIN MudArticulos AS MA ON D.IdArticulo=MA.IdArticulo INNER JOIN MudSecciones AS SC ON MA.IdSeccion=SC.IdSeccion
INNER JOIN MudEstados AS EA ON D.IdEstado=EA.IdEstado
LEFT JOIN Terceros AS NC ON M.CdConductor=NC.IdTercero LEFT JOIN Agencias AS A ON M.IdAgencia=A.IdAgencia 
LEFT JOIN adm_Usuarios AS UD ON D.CdUsuario=UD.IdUsuario 
WHERE M.TipDoc=@pmTipDoc AND M.NumInvent BETWEEN @pmNumInventIni AND @pmNumInventFin AND M.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenOper_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc,O.NumOrden,O.IdCia,O.Item,O.IdOperario,NomOperario,O.IdTipoOper,TipoOperario
,O.CdConcepto,C.Concepto,C.IdCuenta,C.TipoConc,TarifaCargo,O.PlacaVehic,O.Descripcion,O.TipoReg,O.FechaReg,O.CdUsuario,Usuario
 FROM Trn_MudOrdenOper AS O INNER JOIN TiposOperarios AS TP ON O.IdTipoOper=TP.IdOper
 LEFT JOIN Terceros AS T ON O.IdOperario=T.IdTercero
 LEFT JOIN TraConcCausac AS C ON O.CdConcepto=C.IdConcepto
 LEFT JOIN adm_Usuarios AS U ON O.CdUsuario=U.IdUsuario
 WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudOrdenConc] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDescripcion VARCHAR(500),@pmValorTotal MONEY,@pmTarifaIva DECIMAL(14,4),@pmTipoConc VARCHAR(10),@pmTipoRubro VARCHAR(20)
,@pmTipoReg INT,@pmCdCuenta VARCHAR(16),@pmNitTercero VARCHAR(16),@pmReferencia VARCHAR(50),@pmNumDocRef VARCHAR(20),@pmCodTarIva VARCHAR(4),@pmIndFactItem INT,@pmNumDocFac VARCHAR(20),@pmTarifSeguro DECIMAL(14,4),@pmCodTarSeg VARCHAR(4),@pmPlacaVehic VARCHAR(10),@pmTarifaPago MONEY
,@pmFechaReg SMALLDATETIME,@pmCdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MudOrdenConc (TipDoc,NumOrden,IdCia,Item,IdConcepto,Descripcion,ValorTotal,TarifaIva,TipoConc,TipoRubro,TipoReg,CdCuenta,NitTercero,Referencia,NumDocRef,CodTarIva,IndFactItem,NumDocFac,TarifSeguro,CodTarSeg,PlacaVehic,TarifaPago,FechaReg,CdUsuario)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmItem,@pmIdConcepto,@pmDescripcion,@pmValorTotal,@pmTarifaIva,@pmTipoConc,@pmTipoRubro,@pmTipoReg,@pmCdCuenta,@pmNitTercero,@pmReferencia,@pmNumDocRef,@pmCodTarIva,@pmIndFactItem,@pmNumDocFac,@pmTarifSeguro,@pmCodTarSeg,@pmPlacaVehic,@pmTarifaPago,@pmFechaReg,@pmCdUsuario)
GO
ALTER TABLE Trn_MudOrdenNov ADD 
 CONSTRAINT FK_Trn_MudOrdenNov_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_Trn_MudOrdenNov_Trn_MudOrdenServ FOREIGN KEY (TipDoc,NumOrden,IdCia) REFERENCES [dbo].[Trn_MudOrdenServ] (TipDoc,NumOrden,IdCia)
