CREATE VIEW dbo.TArrangementLinesView
AS
SELECT     ArrangementLines.ArrangementLineNumber, ArrangementLines.[Date], ArrangementLines.PacketItemNumber, 
           ArrangementLines.[Time], ArrangementLines.DepartureTime, ArrangementLines.Price, ArrangementLines.TotalPrice, 
           ArrangementLines.Pieces, ArrangementLines.WareText, /*ArrangementLines.Departments,*/ ArrangementLines.FK_ExDepartment, 
           ArrangementLines.FK_PacketChargeDepartment, ArrangementLines.PlaceText, ArrangementLines.WareNumber, 
           ArrangementLines.HallNumber, ArrangementLines.Priority, ArrangementLines.FK_HallNumber, ArrangementLines.FK_HallLineNumber,
           ArrangementReservationsXtab.ReferenceNumber, Ware.SortNumber AS WareSortNumber, ArrangementLines.Note, 
           WareGroups.SortNumber AS GroupSortNumber, WareGroups.WareGroupNumber, WareGroups.Text AS WareGroupName,
           /* Ware.DepartmentService AS PossibleDeps,*/
           Ware.WareName AS RealWareName, Ware.PLUNumber
           , Departments.DepartmentNumber AS ExDepartment,
           dbo.GetArrangementLineDepartmentStr(ArrangementLines.ArrangementLineNumber,0,0) AS Departments,
           dbo.GetArrangementLineDepartmentStr(ArrangementLines.ArrangementLineNumber,1,1) AS PKDepartments,
           ArrangementLines.ReadOnly,
		   ArrangementLines.IsPiecesUserModified,
		   ArrangementLines.PiecesModifiedDiff,
		   ArrangementLines.IsUserModified,
		   ArrangementLines.WareHasRule,
		   ArrangementLines.ArrangementLineUniqueID,
		   ArrangementLineUniqueXTAB.KeyPinCode
FROM   ArrangementReservationsXtab WITH (NOLOCK) INNER JOIN ArrangementLines WITH (NOLOCK) ON 
           ArrangementReservationsXtab.ArrangementLineNumber = ArrangementLines.ArrangementLineNumber LEFT OUTER JOIN
           Ware WITH (NOLOCK) ON ArrangementLines.WareNumber = Ware.WareNumber LEFT OUTER JOIN
           WareGroups WITH (NOLOCK) ON Ware.WareGroupNumber = WareGroups.WareGroupNumber 
           LEFT OUTER JOIN Departments WITH (NOLOCK) ON DEPARTMENTS.PK_Department = ArrangementLines.FK_ExDepartment
           LEFT OUTER JOIN ArrangementRoomXTab WITH (NOLOCK) ON ArrangementLines.ArrangementLineNumber = ArrangementRoomXTab.ArrangementLineNumber
		   LEFT OUTER JOIN ArrangementLineUniqueXTAB WITH (NOLOCK) ON ArrangementLines.ArrangementLineUniqueID = ArrangementLineUniqueXTAB.FK_ArrangementLineUniqueID
WHERE (ArrangementReservationsXTab.GuestNumber = 0 OR (ArrangementReservationsXTab.GuestNumber > 0 AND ArrangementRoomXTab.ArrangementLineNumber IS NOT NULL))



