CREATE PROCEDURE dbo.GetNumberOfChargedDeparturePackage @AProductNumber INT, @APackageNumber INT, @AReferenceNumber INT, @AGuestNumber INT, @APackageDebAccount INT, @ADate DATE, @ANumberOfCharged INT OUTPUT AS
SET NOCOUNT ON
/*
  Created by Kent Johansen, june 2018

*/

SELECT @ANumberOfCharged = COUNT(TransactionNumber) 
FROM Transactions WITH (NOLOCK) 
INNER JOIN Personel WITH (NOLOCK) ON Personel.UserIdNumber = Transactions.UserIdNumber
  AND ((Personel.UserTypeNumber = 4 AND MovedDeleted = 0) OR (Personel.UserTypeNumber = 3 AND (MovedDeleted = 0 OR (MovedDeleted = 1 AND MovedFolioNr > 0))))
INNER JOIN Folios WITH (NOLOCK) ON Folios.FolioNumber = Transactions.FolioNumber
  AND Folios.ReferenceNumber = @AReferenceNumber
  AND Folios.GuestNumber IN (0, @AGuestNumber)
WHERE ProductNumber = @AProductNumber 
AND PackageNumber = @APackageNumber
AND Transactions.GuestNumber = @AGuestNumber
AND AccountNumber = @APackageDebAccount 
AND Debit = 1
AND OriginalDate >= @ADate 
AND OriginalDate < DATEADD(DAY, 1, @ADate)