UtilityFunctions.sql 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. CREATE OR ALTER FUNCTION dbo.GetProjectOpenInvoices(@CompanyID int,
  2. @ContractID int) RETURNS decimal(19,
  3. 6) AS
  4. BEGIN
  5. DECLARE @OpenInvoices decimal(19,
  6. 6);
  7. select
  8. @OpenInvoices = SUM(case when AR.DocType = 'INV' then AR.CuryOrigDocAmt else 0 end) - SUM(case when AR.DocType = 'CRM' then AR.CuryOrigDocAmt else 0 end)
  9. from
  10. Contract con
  11. left join ARInvoice pmc on
  12. con.CompanyID = pmc.CompanyID
  13. and con.ContractID = pmc.ProjectID
  14. left join ARRegister AR on
  15. pmc.CompanyID = AR.CompanyID
  16. and pmc.DocType = AR.DocType
  17. and pmc.RefNbr = AR.RefNbr
  18. where
  19. con.CompanyID = @CompanyID
  20. and con.ContractID = @ContractID
  21. and ( AR.Status = 'N'
  22. or AR.Status = 'C') RETURN IsNull(@OpenInvoices,
  23. 0.00)
  24. END
  25. GO
  26. CREATE OR ALTER FUNCTION dbo.SumReleasedTransactions(
  27. @CompanyID int,
  28. @projectID int,
  29. @released int
  30. )
  31. RETURNS decimal(19,6)
  32. AS
  33. BEGIN
  34. DECLARE @BudAmount decimal(19,6);
  35. SELECT @BudAmount = ISNULL(SUM(p.amount),0)
  36. FROM PMTran p
  37. join PMAccountGroup pg on pg.CompanyID = p.CompanyID and p.AccountGroupID = pg.GroupID
  38. WHERE p.CompanyID = @CompanyID
  39. AND p.ProjectID = @projectID
  40. and p.Released = @released
  41. RETURN @BudAmount;
  42. END;
  43. GO
  44. CREATE OR ALTER FUNCTION dbo.GetTransactions(
  45. @CompanyID int,
  46. @projectID int,
  47. @expense int,
  48. @released int,
  49. @allocated int
  50. )
  51. RETURNS decimal(19,6)
  52. AS
  53. BEGIN
  54. DECLARE @BudAmount decimal(19,6);
  55. SELECT @BudAmount = ISNULL(SUM(p.amount),0)
  56. FROM PMTran p
  57. join PMAccountGroup pg on pg.CompanyID = p.CompanyID and p.AccountGroupID = pg.GroupID
  58. WHERE p.CompanyID = @CompanyID
  59. AND p.ProjectID = @projectID
  60. and pg.IsExpense = @expense
  61. and p.Released = @released
  62. and P.Allocated = @allocated
  63. RETURN @BudAmount;
  64. END;
  65. GO
  66. CREATE OR ALTER FUNCTION dbo.GetRevisedBudgetAmount(
  67. @CompanyID int,
  68. @projectID int,
  69. @type varchar(50)
  70. )
  71. RETURNS decimal(19,6)
  72. AS
  73. BEGIN
  74. DECLARE @BudAmount decimal(19,6);
  75. -- Using a common table expression (CTE) to split the @type string into individual characters
  76. WITH TypeList AS (
  77. SELECT value AS [Type]
  78. FROM STRING_SPLIT(@type, ',')
  79. )
  80. SELECT @BudAmount = ISNULL(SUM(p.RevisedAmount),0)
  81. FROM PMBudget p
  82. WHERE p.CompanyID = @CompanyID
  83. AND p.ProjectID = @projectID
  84. AND p.[Type] IN (SELECT [Type] FROM TypeList) -- Use the split values directly in the IN clause
  85. RETURN @BudAmount;
  86. END;
  87. GO
  88. CREATE OR ALTER FUNCTION dbo.GetTopContractUserDefinedFieldDescription
  89. (
  90. @companyID INT,
  91. @recordID UNIQUEIDENTIFIER,
  92. @attributeName NVARCHAR(128)
  93. )
  94. RETURNS NVARCHAR(MAX)
  95. AS
  96. BEGIN
  97. DECLARE @result NVARCHAR(MAX);
  98. -- Static SQL query (no dynamic SQL)
  99. SELECT top 1 @result = cd.Description
  100. FROM ContractKvExt c
  101. left join CSAttributeDetail cd on cd.CompanyID = c.CompanyID and cd.AttributeID = SUBSTRING(c.FieldName, 10, LEN(c.FieldName) - 9) and cd.ValueID = c.ValueString
  102. WHERE c.FieldName = @attributeName
  103. AND c.CompanyID = @companyID
  104. AND c.RecordID = @recordID;
  105. RETURN @result;
  106. END;
  107. GO
  108. CREATE OR ALTER FUNCTION dbo.ProjectRental
  109. (
  110. @companyID INT,
  111. @contractID INT
  112. )
  113. RETURNS BIT
  114. AS
  115. BEGIN
  116. -- Return TRUE if 'rental' exists in the description, FALSE otherwise
  117. IF EXISTS (
  118. SELECT 1
  119. FROM Contract c
  120. JOIN ContractKvExt e ON c.CompanyID = e.CompanyID AND c.NoteID = e.RecordID
  121. JOIN CSAttributeDetail d ON d.CompanyID = e.CompanyID AND e.ValueString = d.ValueID
  122. WHERE c.CompanyID = @companyID
  123. AND c.ContractID = @contractID
  124. AND e.FieldName = 'AttributeCATEGORY'
  125. AND CHARINDEX('rental', LOWER(d.Description)) > 0
  126. )
  127. BEGIN
  128. RETURN 1; -- TRUE
  129. END
  130. RETURN 0; -- FALSE
  131. END;
  132. GO
  133. CREATE OR ALTER FUNCTION dbo.GetContractUserDefinedField
  134. (
  135. @companyID INT,
  136. @recordID UNIQUEIDENTIFIER,
  137. @attributeName NVARCHAR(128),
  138. @isANumber BIT
  139. )
  140. RETURNS NVARCHAR(MAX)
  141. AS
  142. BEGIN
  143. DECLARE @result NVARCHAR(MAX);
  144. -- Static SQL query (no dynamic SQL)
  145. SELECT @result = ValueString
  146. FROM ContractKvExt
  147. WHERE FieldName = @attributeName
  148. AND CompanyID = @companyID
  149. AND RecordID = @recordID;
  150. -- If isANumber is true, check if ValueString is a valid number and clean it up
  151. IF @isANumber = 1
  152. BEGIN
  153. -- Remove any special characters except digits and '.'
  154. IF ISNUMERIC(@result) = 0
  155. BEGIN
  156. -- If not numeric, set to NULL
  157. SET @result = NULL;
  158. END
  159. ELSE
  160. BEGIN
  161. -- Strip out any characters except digits and '.'
  162. WHILE PATINDEX('%[^0-9.]%', @result) > 0
  163. BEGIN
  164. SET @result = STUFF(@result, PATINDEX('%[^0-9.]%', @result), 1, '');
  165. END
  166. END
  167. END
  168. RETURN @result;
  169. END;
  170. GO
  171. CREATE or ALTER FUNCTION dbo.CountCustomerContracts(
  172. @beginDate DATETIME,
  173. @endDate DATETIME,
  174. @companyId INT,
  175. @customerId INT
  176. )
  177. RETURNS INT
  178. AS
  179. BEGIN
  180. DECLARE @contractCount INT;
  181. SELECT @contractCount = COUNT(*)
  182. FROM Contract
  183. WHERE CompanyId = @companyId
  184. AND CustomerId = @customerId
  185. AND CreatedDateTime <= @endDate
  186. AND CreatedDateTime >= @beginDate
  187. RETURN @contractCount
  188. END;
  189. GO
  190. CREATE OR ALTER FUNCTION dbo.GetBudgetAmount(
  191. @CompanyID int,
  192. @projectID int,
  193. @type varchar(50), -- Use varchar to handle multiple characters
  194. @targetDate DateTime
  195. )
  196. RETURNS decimal(19,6)
  197. AS
  198. BEGIN
  199. DECLARE @BudAmount decimal(19,6);
  200. -- Using a common table expression (CTE) to split the @type string into individual characters
  201. WITH TypeList AS (
  202. SELECT value AS [Type]
  203. FROM STRING_SPLIT(@type, ',')
  204. )
  205. SELECT @BudAmount = ISNULL(SUM(p.Amount),0)
  206. FROM PMBudget p
  207. WHERE p.CompanyID = @CompanyID
  208. AND p.ProjectID = @projectID
  209. AND p.[Type] IN (SELECT [Type] FROM TypeList) -- Use the split values directly in the IN clause
  210. AND p.CreatedDateTime >= @targetDate; -- Filter by the current year
  211. RETURN @BudAmount;
  212. END;
  213. GO
  214. CREATE OR Alter FUNCTION dbo.SumProjectInvoiceAmt(
  215. @companyId INT,
  216. @projectId INT,
  217. @beginDate DATETIME,
  218. @endDate DATETIME
  219. )
  220. RETURNS DECIMAL(18, 2)
  221. AS
  222. BEGIN
  223. DECLARE @totalOrigDocAmt DECIMAL(18, 2);
  224. SELECT @totalOrigDocAmt = ISNULL(SUM(i.CuryLineTotal), 0.00)
  225. FROM ARRegister a
  226. join ARInvoice i on a.CompanyID = i.CompanyID and a.DocType = i.DocType and a.RefNbr = i.RefNbr
  227. WHERE a.CompanyID = @companyId
  228. AND i.ProjectID = @projectId
  229. AND a.DocType = 'INV'
  230. AND a.DocDate >= @beginDate
  231. AND a.DocDate <= @endDate;
  232. RETURN @totalOrigDocAmt;
  233. END;
  234. GO
  235. CREATE OR Alter FUNCTION dbo.SumCustomerInvoiceAmt(
  236. @companyId INT,
  237. @customerId INT,
  238. @beginDate DATETIME,
  239. @endDate DATETIME
  240. )
  241. RETURNS DECIMAL(18, 2)
  242. AS
  243. BEGIN
  244. DECLARE @totalOrigDocAmt DECIMAL(18, 2);
  245. SELECT @totalOrigDocAmt = ISNULL(SUM(i.CuryLineTotal), 0.00)
  246. FROM ARRegister a
  247. join ARInvoice i on a.CompanyID = i.CompanyID and a.DocType = i.DocType and a.RefNbr = i.RefNbr
  248. WHERE a.CompanyID = @companyId
  249. AND a.CustomerID = @customerId
  250. AND a.DocType = 'INV'
  251. AND a.DocDate >= @beginDate
  252. AND a.DocDate <= @endDate;
  253. RETURN @totalOrigDocAmt;
  254. END;
  255. GO
  256. CREATE OR ALTER FUNCTION dbo.SumProjectInvoicePayments(
  257. @companyId INT,
  258. @projectId INT,
  259. @startDate DATETIME,
  260. @endDate DATETIME
  261. )
  262. RETURNS DECIMAL(18, 2)
  263. AS
  264. BEGIN
  265. DECLARE @totalCuryAdjdAmt DECIMAL(18, 2);
  266. SELECT @totalCuryAdjdAmt = ISNULL(SUM(a.CuryAdjdAmt),0.00)
  267. FROM ARAdjust a
  268. join ARInvoice i on a.CompanyID = i.CompanyID and a.AdjdRefNbr = i.RefNbr and a.AdjdDocType = i.DocType
  269. WHERE a.CompanyID = @companyId
  270. AND a.AdjdDocType = 'INV'
  271. AND a.AdjgDocType = 'PMT'
  272. AND i.ProjectID = @projectID
  273. AND a.AdjgDocDate >= @startDate
  274. AND a.AdjgDocDate <= @endDate;
  275. RETURN @totalCuryAdjdAmt;
  276. END;
  277. GO
  278. CREATE OR ALTER FUNCTION dbo.SumCustomerInvoicePayments(
  279. @companyId INT,
  280. @customerId INT,
  281. @startDate DATETIME,
  282. @endDate DATETIME
  283. )
  284. RETURNS DECIMAL(18, 2)
  285. AS
  286. BEGIN
  287. DECLARE @totalCuryAdjdAmt DECIMAL(18, 2);
  288. SELECT @totalCuryAdjdAmt = ISNULL(SUM(a.CuryAdjdAmt),0.00)
  289. FROM ARAdjust a
  290. WHERE a.CompanyID = @companyId
  291. AND a.AdjdDocType = 'INV'
  292. AND a.AdjgDocType = 'PMT'
  293. AND a.CustomerID = @customerId
  294. AND a.AdjgDocDate >= @startDate
  295. AND a.AdjgDocDate <= @endDate;
  296. RETURN @totalCuryAdjdAmt;
  297. END;
  298. GO
  299. CREATE OR ALTER FUNCTION dbo.GetFirstDayOfPeriod(
  300. @inputDate DATETIME,
  301. @periodType VARCHAR(10)
  302. )
  303. RETURNS DATETIME
  304. AS
  305. BEGIN
  306. DECLARE @firstDay DATETIME;
  307. IF @periodType = 'WEEK'
  308. BEGIN
  309. -- Get the first day of the week (assuming Sunday as the first day)
  310. SET @firstDay = DATEADD(day, -DATEDIFF(day, 0, @inputDate) % 7, CAST(@inputDate AS DATE));
  311. END
  312. ELSE IF @periodType = 'MONTH'
  313. BEGIN
  314. -- Get the first day of the month
  315. SET @firstDay = DATEFROMPARTS(YEAR(@inputDate), MONTH(@inputDate), 1);
  316. END
  317. ELSE IF @periodType = 'QUARTER'
  318. BEGIN
  319. DECLARE @inputMonth INT;
  320. DECLARE @quarterStartMonth INT;
  321. SET @inputMonth = MONTH(@inputDate);
  322. -- Determine the starting month of the quarter for the given date
  323. IF @inputMonth IN (1, 2, 3)
  324. SET @quarterStartMonth = 1;
  325. ELSE IF @inputMonth IN (4, 5, 6)
  326. SET @quarterStartMonth = 4;
  327. ELSE IF @inputMonth IN (7, 8, 9)
  328. SET @quarterStartMonth = 7;
  329. ELSE
  330. SET @quarterStartMonth = 10;
  331. -- Construct the first day of the quarter for the given date
  332. SET @firstDay = DATEFROMPARTS(YEAR(@inputDate), @quarterStartMonth, 1);
  333. END
  334. ELSE IF @periodType = 'YEAR'
  335. BEGIN
  336. -- Get the first day of the year
  337. SET @firstDay = DATEFROMPARTS(YEAR(@inputDate), 1, 1);
  338. END
  339. RETURN CAST(@firstDay AS DATETIME);
  340. END;
  341. GO
  342. CREATE or ALTER FUNCTION dbo.GetCustomerRevenueByProjectDate(@CompanyID int, @CustomerID int, @targetDate DATETIME)
  343. RETURNS decimal(19,6)
  344. AS
  345. BEGIN
  346. DECLARE @RevAmount decimal(19,6);
  347. SELECT @RevAmount = SUM(p.Amount)
  348. FROM PMTran p
  349. join Contract c on p.CompanyID = c.CompanyID and p.ProjectID = c.ContractID
  350. join PMAccountGroup pg on pg.CompanyID = p.CompanyID and p.AccountGroupID = pg.GroupID
  351. WHERE p.CompanyID = @CompanyID
  352. AND c.CustomerID = @CustomerID
  353. and pg.[Type] = 'I'
  354. and pg.GroupID <> 25
  355. and c.CreatedDateTime >= @targetDate;
  356. RETURN @RevAmount;
  357. END;
  358. GO
  359. CREATE or ALTER FUNCTION dbo.GetCustomerRevenueByProjectBilled(@CompanyID int, @CustomerID int, @targetDate DATETIME)
  360. RETURNS decimal(19,6)
  361. AS
  362. BEGIN
  363. DECLARE @RevAmount decimal(19,6);
  364. SELECT @RevAmount = SUM(p.Amount)
  365. FROM PMTran p
  366. join Contract c on p.CompanyID = c.CompanyID and p.ProjectID = c.ContractID
  367. join PMAccountGroup pg on pg.CompanyID = p.CompanyID and p.AccountGroupID = pg.GroupID
  368. WHERE p.CompanyID = @CompanyID
  369. AND c.CustomerID = @CustomerID
  370. and c.ContractID in (SELECT a.ProjectID from ARInvoice a where a.CompanyID= @CompanyID and a.DocType = 'INV' and a.InvoiceDate > @targetDate group by a.ProjectID)
  371. and pg.[Type] = 'I'
  372. and pg.GroupID <> 25
  373. and c.CreatedDateTime >= @targetDate;
  374. RETURN @RevAmount;
  375. END;
  376. GO
  377. CREATE or ALTER FUNCTION dbo.GetCustomerRevenueByProjectPaid(@CompanyID int, @CustomerID int, @targetDate DATETIME)
  378. RETURNS decimal(19,6)
  379. AS
  380. BEGIN
  381. DECLARE @RevAmount decimal(19,6);
  382. SELECT @RevAmount = SUM(p.Amount)
  383. FROM PMTran p
  384. join Contract c on p.CompanyID = c.CompanyID and p.ProjectID = c.ContractID
  385. join PMAccountGroup pg on pg.CompanyID = p.CompanyID and p.AccountGroupID = pg.GroupID
  386. WHERE p.CompanyID = @CompanyID
  387. AND c.CustomerID = @CustomerID
  388. and c.ContractID in (
  389. SELECT i.ProjectID
  390. FROM ARAdjust a
  391. join ARInvoice i on a.CompanyID = i.CompanyID and a.AdjdDocType = i.DocType and a.AdjdRefNbr = i.RefNbr
  392. WHERE a.CompanyID = @CompanyID
  393. AND a.AdjdDocType = 'INV'
  394. AND a.AdjgDocType = 'PMT'
  395. AND a.AdjgDocDate >= @targetDate
  396. group by i.ProjectID )
  397. and pg.[Type] = 'I'
  398. and pg.GroupID <> 25
  399. and c.CreatedDateTime >= @targetDate;
  400. RETURN @RevAmount;
  401. END;