CREATE PROCEDURE DBO.KitchenUnprinted @BeginDate char(8), @EndDate char(8) AS
SET NOCOUNT ON
SELECT T.FolioNumber As FolioNr, T.TransactionNumber AS TransNr, T.DateStamp, T.TextOverwrite, T.Pieces as Pcs, dbo.Printers.Name AS PrinterName
, dbo.PERSONEL.UserIDCode, KSX.Note
FROM dbo.TRANSACTIONS T WITH (NOLOCK)
  INNER JOIN dbo.Ware WITH (NOLOCK) ON T.WareNumber = dbo.Ware.WareNumber
  INNER JOIN dbo.FOLIOS WITH (NOLOCK) ON T.FolioNumber = dbo.FOLIOS.FolioNumber
  INNER JOIN dbo.Printers WITH (NOLOCK) ON dbo.Ware.PrinterNumber = dbo.Printers.PrinterNumber
  INNER JOIN dbo.PERSONEL WITH (NOLOCK) ON T.UserIDNumber = dbo.PERSONEL.UserIDNumber
  LEFT JOIN dbo.TransactionKitchenStatementXTab KSX WITH (NOLOCK) ON T.TransactionNumber = KSX.TransactionNumber
  LEFT JOIN dbo.InvoiceTransactionXtab ITX WITH (NOLOCK) ON ITX.FkTransaction = T.TransactionNumber
  LEFT JOIN dbo.Invoice I WITH (NOLOCK) ON I.PkInvoice = ITX.FkInvoice AND I.CancelDate IS NULL
WHERE (KSX.KitchenStatementNumber IS NULL) AND (I.PkInvoice IS NULL) AND (T.ShowOnBillFlag = 1) AND
  ((T.MovedDeleted IS NULL) OR (T.MovedDeleted=0)) AND (CONVERT(varchar, T.DateStamp, 112) >= @BeginDate) AND (CONVERT(varchar, T.DateStamp, 112) <= @EndDate)
ORDER BY T.DateStamp

