exec sp_executesql N'DECLARE @FoundZone BIT 

                        SELECT @FoundZone = template.eomtemplateid 
                        FROM   tbendofmonthtemplate template WITH(NOLOCK)
                               JOIN tbentityzone entityZone WITH(NOLOCK) 
                                 ON entityZone.entityid = @p0 
                                    AND entityZone.zoneid = template.zoneid 

                        IF ( @FoundZone = 1 ) 
                          BEGIN 
                              SELECT 
                                     template.eomtemplateId as EndOfMonthTemplateId,
                                     section.EndOfMonthSectionId as Id,
                                     section.EOMSectionKey,
                                     section.EOMNameTranslationToken,
                                     section.EOMNotPassedTranslationToken,
                                     section.EOMPassedTranslationToken,
                                     section.EOMExternalDataWebService,
                                     section.EOMMMSPageURL,
                                     section.EOMMobilePageLinkURL
                              FROM   tbendofmonthtemplate template WITH(NOLOCK)
                                     JOIN tbendofmonthtemplatesectionmap map WITH(NOLOCK)
                                       ON map.eomtemplateid = template.eomtemplateid 
                                     JOIN tbendofmonthsection section WITH(NOLOCK)
                                       ON section.endofmonthsectionid = map.eomsectionid 
                                     JOIN tbentityzone entityZone WITH(NOLOCK)
                                       ON entityZone.entityid = @p0 
                                          AND entityZone.zoneid = template.zoneid 
                                     ORDER BY map.EOMSectionOrder ASC
                          END 
                        ELSE 
                          BEGIN 
                              SELECT 
                                     defaultTemplate.eomtemplateId as EndOfMonthTemplateId,
                                     section.EndOfMonthSectionId as Id,
                                     section.EOMSectionKey,
                                     section.EOMNameTranslationToken,
                                     section.EOMNotPassedTranslationToken,
                                     section.EOMPassedTranslationToken,
                                     section.EOMExternalDataWebService,
                                     section.EOMMMSPageURL,
                                     section.EOMMobilePageLinkURL                                     
                              FROM   tbendofmonthtemplate defaultTemplate WITH(NOLOCK)
                                     JOIN tbendofmonthtemplatesectionmap defaultMap WITH(NOLOCK)
                                       ON defaultMap.eomtemplateid = defaultTemplate.eomtemplateid 
                                     JOIN tbendofmonthsection section WITH(NOLOCK)
                                       ON section.endofmonthsectionid = defaultMap.eomsectionid 
                              WHERE  defaultTemplate.eomtemplatename = ''Default'' 
                              ORDER BY defaultMap.EOMSectionOrder ASC
                          END ',N'@p0 bigint',@p0=902
go
exec sp_executesql N'SELECT 
	                        e.ExternalApiId,
	                        e.Description,
	                        e.ApiUri,
	                        e.UserName,
	                        e.Password
                        FROM tbExternalApi e WITH(NOLOCK)
                        LEFT JOIN tbEndOfMonthSection m WITH(NOLOCK) ON m.ExternalGETApiId = e.ExternalApiId
                        WHERE  m.EOMSectionKey = @p0 OR 
                            (e.Description = @p0 and m.EOMSectionKey IS NULL)',N'@p0 nvarchar(4000)',@p0=N'SupplierInvoices'
go
exec sp_executesql N'
                SELECT e.EndOfMonthStatusCheckExpenseId,
	                e.EndOfMonthStatusCheckId,
	                e.ExpenseCategoryGLAccountCode,
	                e.Comments,
	                e.Amount,
	                e.TransferToStoreEntityId,
	                c.GLAccountName CategoryDisplay,
	                NULL TransferToStoreDisplay
                FROM tbEndOfMonthStatusCheckExpense e WITH(NOLOCK)
                JOIN tbEndOfMonthStatusCheck s WITH(NOLOCK) ON e.EndOfMonthStatusCheckId = s.EndOfMonthStatusCheckId
	                AND s.EntityId = @p0
                LEFT JOIN LedgerChartOfAccounts c WITH(NOLOCK) ON e.ExpenseCategoryGLAccountCode = c.GLAccountNumber
                WHERE e.EndOfMonthStatusCheckId = @p1
                 AND (e.TransferToStoreEntityId IS NULL OR e.TransferToStoreEntityId <= 0)',N'@p0 bigint,@p1 bigint',@p0=902,@p1=1055
go
exec sp_executesql N'
                SELECT e.EndOfMonthStatusCheckExpenseId,
	                e.EndOfMonthStatusCheckId,
	                e.ExpenseCategoryGLAccountCode,
	                e.Comments,
	                e.Amount,
	                e.TransferToStoreEntityId,
	                c.GLAccountName CategoryDisplay,
	                store.Entity TransferToStoreDisplay
                FROM tbEndOfMonthStatusCheckExpense e WITH(NOLOCK)
                JOIN tbEndOfMonthStatusCheck s WITH(NOLOCK) ON e.EndOfMonthStatusCheckId = s.EndOfMonthStatusCheckId
	                AND s.EntityId = @p0
                LEFT JOIN LedgerChartOfAccounts c WITH(NOLOCK) ON e.ExpenseCategoryGLAccountCode = c.GLAccountNumber
                JOIN tbEntity store WITH(NOLOCK) ON e.TransferToStoreEntityId = store.EntityID
                WHERE e.EndOfMonthStatusCheckId = @p1
                ',N'@p0 bigint,@p1 bigint',@p0=902,@p1=1055
go
exec sp_executesql N'SELECT 
	                        e.ExternalApiId,
	                        e.Description,
	                        e.ApiUri,
	                        e.UserName,
	                        e.Password
                        FROM tbExternalApi e WITH(NOLOCK)
                        LEFT JOIN tbEndOfMonthSection m WITH(NOLOCK) ON m.ExternalGETApiId = e.ExternalApiId
                        WHERE  m.EOMSectionKey = @p0 OR 
                            (e.Description = @p0 and m.EOMSectionKey IS NULL)',N'@p0 nvarchar(4000)',@p0=N'UtilityThreshold'
go
exec sp_executesql N'
                SELECT a.EndOfMonthStatusCheckAccrualsAndReversalsId,
	                a.EndOfMonthStatusCheckId,
	                a.Action,
	                a.Measure,
	                a.Amount,
	                a.Account as AccrualCategoryGLAccountCode,
	                c.GLAccountName as CategoryDisplay,
                    a.ShowNextMonth
                FROM tbEndOfMonthStatusCheckAccrualsAndReversals a WITH(NOLOCK)
                JOIN tbEndOfMonthStatusCheck s WITH(NOLOCK) ON a.EndOfMonthStatusCheckId = s.EndOfMonthStatusCheckId
	                AND s.EntityId = @p0
                LEFT JOIN LedgerChartOfAccounts c WITH(NOLOCK) ON a.Account = c.GLAccountNumber
                WHERE a.EndOfMonthStatusCheckId = @p1
                ',N'@p0 bigint,@p1 bigint',@p0=902,@p1=1055
go
exec sp_executesql N'SELECT this_.EndOfMonthStatusCheckUtilityThresholdId as EndOfMon1_83_0_, this_.EndOfMonthStatusCheckId as EndOfMon2_83_0_, this_.ExternalIdentifier as External3_83_0_, this_.Reason as Reason83_0_, this_.AllowAccrual as AllowAcc5_83_0_, this_.AccrualAmount as AccrualA6_83_0_, this_.NoPaymentReason as NoPaymen7_83_0_ FROM dbo.tbEndOfMonthStatusCheckUtilityThreshold this_ WHERE this_.EndOfMonthStatusCheckId = @p0',N'@p0 bigint',@p0=1055
go
