IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[qryMMSBankingClose_SaveAndAuthorise_MxC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [qryMMSBankingClose_SaveAndAuthorise_MxC]
GO

CREATE PROCEDURE [qryMMSBankingClose_SaveAndAuthorise_MxC]
	@Eftpos money,
	@Withhold money,
	@Rebank money,
	@PettyCash money,
	@SpecialDeposits money,
	@Vouchers money,
	@Cheques money,
	@Catering money,
	@CashOut money,
	@VoucherCheques money,
	@GiftCertificateSold money,
	@GiftCertificateRedeemed money,
	@CashVariance money,	
	@BankingCloseID bigint,
	@Username varchar(100),
	@Comments varchar(500),
	@CashupDeposit money,
	@CashupPreviousDeposit money,
	@LoyaltyCard money,
	@NonSales money,		
	@PaidIn money,
	@EntityId bigint,
	@TransactionDate datetime
AS
BEGIN

    UPDATE tbBankingClose SET
	TransactionDate = @TransactionDate,
	Eftpos = @Eftpos,
	Withhold = @Withhold,
	Rebank = @Rebank,
	PettyCash = @PettyCash,
	SpecialDeposits = @SpecialDeposits,
	Vouchers = @Vouchers,
	Cheques = @Cheques,
	Catering = @Catering,
	CashOut = @CashOut,
	VoucherCheques = @VoucherCheques,
	GiftCertificateSold = @GiftCertificateSold,
	GiftCertificateRedeemed = @GiftCertificateRedeemed,
	CashVariance= @CashVariance,
	AuthorisedByUsername = @Username,
	Authorised = 1,
	Comments = @Comments,
	CashupDeposit = @CashupDeposit,
	CashupPreviousDeposit = @CashupPreviousDeposit,
	LoyaltyCard = @LoyaltyCard,
	NonSales = @NonSales,
	PaidIn = @PaidIn
	WHERE
	BankingCloseID = @BankingCloseID

    Declare @BankingDate As DateTime
	Set @BankingDate = (Select BankingDate From tbBankingClose WHERE BankingCloseID = @BankingCloseID)
	
	EXEC dbo.MX_OutsideSalesNameFix_Workaround @EntityID, @BankingDate
	
	IF Exists (SELECT ShowRebank FROM tbBankingSettings WHERE (ShowRebank =1) or (ShowWithhold=1))  
	-- If rebank or withhold are editable then .....
		BEGIN
			DECLARE @DefaultStoreFloat FLOAT
			SET @DefaultStoreFloat = @Withhold - @Rebank
			EXEC qryMMSEntityFloat_SetValue @EntityID, @DefaultStoreFloat, 1
		END

	-- EFTPOS Recon	
	EXEC dbo.qryMMSBankingEftposRecon_InsertOrUpdateAmountDeposit @EntityID, @BankingDate, @BankingCloseID, @Eftpos, @CashOut

	-- Cheque Recon
	EXEC dbo.qryMMSBankingChequeRecon_UpdateAmountDeposit @BankingCloseID, @Cheques
	-- PettyCash Recon
	EXEC dbo.qryMMSBankingPettyCashRecon_UpdateAmountDeposit @BankingCloseID, @PettyCash
	
	-- Voucher Recon  
    EXEC dbo.qryMMSBankingVoucherRecon_InsertOrUpdateAmountDeposit @EntityID, @BankingDate, @BankingCloseID    

	Declare @EndOfMonthDate As DateTime
	SET @EndOfMonthDate = DATEADD(D, CASE DATENAME(DW,EOMonth(@BankingDate))
       WHEN 'Sunday' THEN -1
       ELSE 0
       END, EOMonth(@BankingDate))
       
    IF (@BankingDate = @EndOfMonthDate)
    BEGIN
    Declare @ApplicablePeriod DateTime 
    SELECT @ApplicablePeriod = DATEADD(month, DATEDIFF(month, 0, @BankingDate), 0)  
    if not exists (select * from tbEndOfMonthStatusCheck with (nolock) where EntityId = @EntityId and ApplicablePeriod = @ApplicablePeriod)
       BEGIN
          Insert into tbEndOfMonthStatusCheck (EntityId, ApplicablePeriod, DayFinalizeStatus)
          values(@EntityId, @ApplicablePeriod, 1)
       END
     ELSE
       BEGIN
        Update tbEndOfMonthStatusCheck set DayFinalizeStatus = 1 Where EntityId = @EntityId and ApplicablePeriod = @ApplicablePeriod
       END
   END

END

GO

