GLTranRevenueSums.sql 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. IF OBJECT_ID ('dbo.GLTranRevenueSumsWithPeriod','V') IS NOT NULL DROP VIEW dbo.GLTranRevenueSumsWithPeriod
  2. GO
  3. CREATE VIEW dbo.GLTranRevenueSumsWithPeriod
  4. AS
  5. SELECT t.CompanyID,
  6. t.ProjectID,
  7. c.ContractCD,
  8. c.NoteID,
  9. cust.BAccountID as CustomerID,
  10. cust.AcctCD as CustomerCD,
  11. cust.AcctName as CustomerName,
  12. br.BranchID as BranchKey,
  13. br.BranchCD as BranchCD,
  14. ba.AcctName as BranchName,
  15. dbo.GetTopContractUserDefinedFieldDescription(t.CompanyID, c.NoteID, 'AttributeCategory') as JobCategory,
  16. dbo.GetTopContractUserDefinedFieldDescription(t.CompanyID, c.NoteID, 'AttributejobStatus') as JobStatus,
  17. dbo.GetTopContractUserDefinedFieldDescription(t.CompanyID, c.NoteID, 'AttributeProjectGRP') as ProjectGroup,
  18. dbo.GetContractUserDefinedField(t.CompanyID, c.NoteID, 'AttributeESTCompl', 0) as EstimatedComplete,
  19. dbo.GetContractUserDefinedField(t.CompanyID, c.NoteID, 'AttributePO', 0) as CustomerPO,
  20. dbo.GetRevisedBudgetAmount(t.CompanyID,t.ProjectID,'I') as RevisedBudgetedincome,
  21. dbo.GetRevisedBudgetAmount(t.CompanyID,t.ProjectID,'E') as RevisedBudgetedExpense,
  22. dbo.GetTransactions(t.CompanyID, t.ProjectID, 1, 1,0) as TotalExpenses,
  23. c.status as ProjectStatus,
  24. c.CreatedDateTime as ProjectCreatedDateTime,
  25. c.LastModifiedDateTime as ProjectLastModifiedDateTime,
  26. bAExec.AcctCD as ExecutiveNumber,
  27. bAExec.AcctName as ExecutiveName,
  28. execEmp.userID as ExecutivePKID,
  29. lmu.PKID as LastModifiedByKey,
  30. lmu.Username as LastModifiedUserName,
  31. lmu.FullName as LastModifiedName,
  32. t.FinPeriodID,
  33. SUM(CASE
  34. WHEN a.AccountCD = '4001' THEN COALESCE(t.CreditAmt, 0) - COALESCE(t.DebitAmt, 0)
  35. ELSE 0
  36. END) AS BilledRevenue4001, -- "Billed Revenue - 4001"
  37. SUM(CASE
  38. WHEN a.AccountCD = '4002' THEN COALESCE(t.CreditAmt, 0) - COALESCE(t.DebitAmt, 0)
  39. ELSE 0
  40. END) AS TMUnbilledRevenue4002, -- "T&M Unbilled Revenue - 4002"
  41. SUM(CASE
  42. WHEN a.AccountCD = '4000' THEN COALESCE(t.CreditAmt, 0) - COALESCE(t.DebitAmt, 0)
  43. ELSE 0
  44. END) AS LumpSumWIPRevenue4000, -- "Lump Sum WIP Revenue - 4000"
  45. SUM(CASE
  46. WHEN a.AccountCD = '4003' THEN COALESCE(t.CreditAmt, 0) - COALESCE(t.DebitAmt, 0)
  47. ELSE 0
  48. END) AS BillingsLumpSum4003, -- "Billings Lump Sum - 4003"
  49. SUM(CASE
  50. WHEN TRY_CAST(a.AccountCD AS INT) BETWEEN 5000 AND 5999 THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0)
  51. ELSE 0
  52. END) AS TotalExpensesCOGS, -- "Total Expenses - COGS"
  53. SUM(CASE WHEN a.AccountCD = '4007' THEN COALESCE(t.CreditAmt, 0) - COALESCE(t.DebitAmt, 0) ELSE 0 END) AS ClientDiscounts4007,
  54. SUM(CASE WHEN a.AccountCD = '4500' THEN COALESCE(t.CreditAmt, 0) - COALESCE(t.DebitAmt, 0) ELSE 0 END) AS OtherIncome4500,
  55. SUM(CASE WHEN a.AccountCD = '4502' THEN COALESCE(t.CreditAmt, 0) - COALESCE(t.DebitAmt, 0) ELSE 0 END) AS Refunds4502,
  56. SUM(CASE WHEN a.AccountCD = '5000' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCLabor5000,
  57. SUM(CASE WHEN a.AccountCD = '5001' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCLaborBurden5001,
  58. SUM(CASE WHEN a.AccountCD = '5010' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCSubcontractor5010,
  59. SUM(CASE WHEN a.AccountCD = '5020' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCMaterials5020,
  60. SUM(CASE WHEN a.AccountCD = '5030' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCEquipmentInternal5030,
  61. SUM(CASE WHEN a.AccountCD = '5040' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCEquipmentExternal5040,
  62. SUM(CASE WHEN a.AccountCD = '5041' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCConsultant5041,
  63. SUM(CASE WHEN a.AccountCD = '5050' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCTravel5050,
  64. SUM(CASE WHEN a.AccountCD = '5055' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCOther5055,
  65. SUM(CASE WHEN a.AccountCD = '5056' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS JCNonAllocating5056,
  66. SUM(CASE WHEN a.AccountCD = '5099' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS WIPCOGS5099,
  67. SUM(CASE WHEN a.AccountCD = '5100' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS ShopSuppliesIndirect5100,
  68. SUM(CASE WHEN a.AccountCD = '5101' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS ShopExpensesSmallTools5101,
  69. SUM(CASE WHEN a.AccountCD = '5102' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS ShopExpenseUniforms5102,
  70. SUM(CASE WHEN a.AccountCD = '5103' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS ShopIndirectLabor5103,
  71. SUM(CASE WHEN a.AccountCD = '5104' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS DirectLabor5104,
  72. SUM(CASE WHEN a.AccountCD = '5105' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS IndirectQuotingCosts5105,
  73. SUM(CASE WHEN a.AccountCD = '5108' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS PassThroughCost5108,
  74. SUM(CASE WHEN a.AccountCD = '5109' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS PurchasePriceVariance5109,
  75. SUM(CASE WHEN a.AccountCD = '5110' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS WarrantyWork5110,
  76. SUM(CASE WHEN a.AccountCD = '5111' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS EstSubcontractorCosts5111,
  77. SUM(CASE WHEN a.AccountCD = '5115' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS CatTravelExpenses5115,
  78. SUM(CASE WHEN a.AccountCD = '5201' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS DiscountsGiven5201,
  79. SUM(CASE WHEN a.AccountCD = '5202' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS DiscountsTaken5202,
  80. SUM(CASE WHEN a.AccountCD = '5210' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS ClientBillingExpense5210,
  81. SUM(CASE WHEN a.AccountCD = '5300' THEN COALESCE(t.DebitAmt, 0) - COALESCE(t.CreditAmt, 0) ELSE 0 END) AS BadDebt5300
  82. FROM GLTran t
  83. left JOIN Contract c ON t.CompanyID = c.CompanyID AND t.ProjectID = c.ContractID
  84. left JOIN Account a ON t.CompanyID = a.CompanyID AND t.AccountID = a.AccountID
  85. LEFT join BAccount cust on cust.CompanyID = c.CompanyID and cust.BAccountID = c.CustomerID
  86. LEFT JOIN Branch br on br.CompanyID = c.CompanyID and br.BranchID = c.DefaultBranchID
  87. LEFT join BACCount ba on ba.CompanyID = br.CompanyID and ba.BAccountID = br.BACCountID
  88. left JOIN ContractKvExt execNote on execNote.CompanyID = c.CompanyID and execNote.RecordID =c.NoteID and execNote.FieldName = 'AttributePROJECTEXE'
  89. left join BAccount bAExec on BAExec.CompanyID = execNote.CompanyID and BAExec.AcctCD = execNote.ValueString
  90. left join EPEmployee execEmp on BAExec.CompanyID = execEmp.CompanyID and BAExec.BAccountID = execEmp.BAccountID
  91. left join Users lmu on lmu.CompanyID = c.CompanyID and lmu.PKID = c.LastModifiedByID
  92. WHERE (a.AccountCD IN ('4001', '4002', '4000', '4003', '4007', '4500', '4502')
  93. OR TRY_CAST(a.AccountCD AS INT) BETWEEN 5000 AND 5999)
  94. GROUP BY t.CompanyID,
  95. t.ProjectID,
  96. c.ContractCD,
  97. c.CreatedDateTime,
  98. c.LastModifiedDateTime,
  99. c.NoteID,
  100. c.status,
  101. t.FinPeriodID,
  102. cust.BAccountID,
  103. cust.AcctCD,
  104. cust.AcctName,
  105. br.BranchID,
  106. br.BranchCD,
  107. ba.AcctName,
  108. bAExec.AcctCD,
  109. bAExec.AcctName,
  110. execEmp.userID,
  111. lmu.PKID,
  112. lmu.Username,
  113. lmu.FullName