-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLMtoATLAS Agreement Export.sql
284 lines (260 loc) · 9.25 KB
/
SLMtoATLAS Agreement Export.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
--NOTES
--v2 build. This will pull the necessary values from an SLM SQL. Note the CID,USERID you use as this is default to 1,1 respectively.
--Review Legal Organization. When Org values are missing, this script will default the value to ROOT.
--Ensure that all NULL values are removed from the output document prior to using in upload.
--20240812 Changes
---Corrected Master Agreement column pull --Line 126
---Added support for Custom Agreement Types --Lines 144 and 220-221
---Added additional fields for import available in Snow Atlas
---20240923 - Reverted SQL query due to incorrect upload
USE [SnowLicenseManager]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
BEGIN
declare @CID INT = 1
declare @UserID INT = 1
declare @Parameters NVARCHAR(MAX) = NULL
declare @FromWeb bit = 0
set nocount on;
set dateformat ymd
-- Get user language
declare @UserLang nvarchar(10)
set @UserLang = dbo.GetUserLanguage(@CID, @UserID)
declare @IsAdmin int
set @IsAdmin = dbo.CheckObjectAccess(@CID, @UserID, 'FULLACCESS');
-- Get user currency
declare @UserCurrency nvarchar(10)
set @UserCurrency = dbo.GetUserCurrency(@CID, @UserID)
declare @UserCurrencyRate numeric(18,4)
set @UserCurrencyRate = dbo.GetUserCurrencyrate(@CID,@UserID)
declare @cfColumns nvarchar(max);
set @cfColumns = dbo.GetCustomFieldTableColumns(@cid, 4, 'cf', 1, @UserID, 1);
if @Parameters IS NOT NULL
set @Parameters = replace(@Parameters,'@CID',convert(nvarchar,@CID))
else
set @Parameters = ''
declare @lang nvarchar(10);
set @lang = (select [Language] from tblSystemUser where UserID = @UserID);
declare @q nvarchar(max);
declare @qp nvarchar(max)
set @qp = N'@CID int, @UserID int, @lang nvarchar(10), @UserCurrencyRate numeric(18,4), @IsAdmin int, @UserCurrency nvarchar(10)'
set @q = N'
;
with lc as -- licensecount
(
select
l.CID,
l.ContractID,
count(*) as LicensePurchaseCount,
sum(isnull(cpl.licensequantity,0)) as Licensequantity,
sum(isnull(cpl.licensepurchasecost,0)*uc.basecurrencyrate ) as Licensepurchasecost,
sum(isnull(cpl.currentmaintenancecost,0)) as currentmaintenancecost,
sum(isnull(cpl.currentsupportcost,0)) as currentsupportcost,
sum(isnull(cpl.accumulatedsupportcost,0)) as totalsupportcost,
sum(isnull(cpl.accumulatedmaintenancecost,0)) as totalmaintenancecost,
sum(isnull(l.purchasevaluebase,0)*uc.basecurrencyrate) as licensecost,
sum(isnull(cpl.TotalCost,0)) as totalcost,
stuff(( select distinct '',['' + InvoiceReference + '']'' from tbllicense where ContractID = l.ContractID and cid = l.cid and invoicereference is not null and InvoiceReference <> ''''
for xml path ('''')),1,1, '''') as InvoiceReferences
from
dbo.tblLicense l
OUTER APPLY (SELECT MAX(uc1.basecurrencyrate) basecurrencyrate from tblCurrency uc1 where
l.cid = uc1.cid
and @usercurrency = uc1.CurrencyName
and l.PurchaseDate >= uc1.ValidFrom
and l.PurchaseDate <= isnull(uc1.ValidTo,''21000101'')) uc
left outer join dbo.tblCostsPerLicenseSummary cpl on l.cid = cpl.cid and l.licenseid = cpl.licenseid
group by
l.CID,
l.ContractID
),
cc as -- computercount
(
select
cc.CID,
cc.ContractID,
count(*) as ComputerCount,
sum(isnull(ci.purchasevaluebase,0)*uc.basecurrencyrate) as PurchaseValueBase
from
dbo.tblContractComputers cc
left outer join tblcomputerinfo ci on cc.cid = ci.cid and cc.computerid = ci.computerid
OUTER APPLY (SELECT MAX(uc1.basecurrencyrate) basecurrencyrate from tblCurrency uc1 where
ci.cid = uc1.cid
and @usercurrency = uc1.CurrencyName
and ci.PurchaseDate >= uc1.ValidFrom
and ci.PurchaseDate <= isnull(uc1.ValidTo,''21000101'')) uc
group by
cc.CID,
cc.ContractID
),
oc as -- objectcount
(
select
co.CID,
co.ContractID,
count(*) as ObjectCount
from
dbo.tblContractObjects co
group by
co.CID,
co.ContractID
)'
set @q = @q + N'
select
c.ContractID,
CASE
WHEN vr.VendorName IS NULL OR vr.VendorName = '''' THEN
CASE
WHEN c.Manufacturer IS NULL OR c.Manufacturer = '''' THEN ''TBD''
ELSE c.Manufacturer
END
ELSE vr.VendorName
END AS Manufacturer,
c.[Name] as ''Agreement Name'',
c.AssignedID As ''Agreement Number'',
CASE
WHEN c.IsValidForAll = 0 THEN ''NO''
WHEN c.IsValidForAll = 1 THEN ''YES''
ELSE ''''
END AS ''Searchable outside period'',
ISNULL(c.ManufacturerLink,'''') AS ''Website (manufacturer)'',
ISNULL(c.ManufacturerPhone,'''') AS ''Phone (manufacturer)'',
ISNULL(c.ManufacturerContact,'''') AS ''Contact (manufacturer)'',
ISNULL(c.ManufacturerContactEmail,'''') AS ''Contact email (manufacturer)'',
ISNULL(c.ManufacturerContactPhone,'''') AS ''Contact phone (manufacturer)'',
ISNULL(c.LocalContact,'''') AS ''Local Contact'',
ISNULL(c.LocalContactDepartment,'''') AS ''Department (local contact)'',
ISNULL(c.LocalContactPhone,'''') AS ''Contact phone (local contact)'',
ISNULL(c.LocalContactEmail,'''') AS ''Contact email (local contact)'',
CASE
WHEN c.RenewalDays IS NULL OR c.RenewalDays = 0 THEN ''''
ELSE CONVERT(varchar, c.RenewalDays)
END AS ''Renewal days'',
ISNULL(c.WarningDayLimit,'''') AS ''Warning Days'',
ISNULL(c.CriticalDayLimit,'''') AS ''Critical days'',
ISNULL(c.Description,'''') AS ''Description'',
ISNULL((SELECT mc.AssignedID FROM tblContract mc WHERE mc.ContractID = c.MasterContractID ),'''') as ''Master Agreement Number'',
CASE
WHEN c.ContractType = 0 THEN ''SOFTWARE''
WHEN c.ContractType = 1 THEN ''MAINTENANCE''
WHEN c.ContractType = 2 THEN ''SUPPORT''
WHEN c.ContractType = 3 THEN ''PURCHASE''
WHEN c.ContractType = 4 THEN ''CUSTOM''
WHEN c.ContractType = 5 THEN ''ORACLE''
ELSE ISNULL(ct.TypeName, ''Unknown'')
END AS ''Agreement Type'',
CASE
WHEN c.IsUpgradable = 0 THEN ''NO''
WHEN c.IsUpgradable = 1 THEN ''YES''
ELSE ''NULL''
END AS ''Activate Automatic Upgrade'',
CASE
WHEN c.IsSubscription = 0 THEN ''NO''
WHEN c.IsSubscription = 1 THEN ''YES''
ELSE ''NULL''
END AS ''Subscription Agreement'',
ISNULL(CONVERT(varchar(10), cp2.ValidFrom, 23), '''') as ''Valid From'', -- Convert to YYYY-MM-DD
ISNULL(CONVERT(varchar(10), cp2.ValidTo, 23), '''') as ''Valid To'', -- Convert to YYYY-MM-DD
c.AlertOnExpiration as ''Alert On Expiration'',
''ROOT'' as LegalOrganisation, --USE if Organization is being defaulted to ROOT
--COALESCE(o.FriendlyName, ''ROOT'') as ''Legal Organisation'', --This will default missing values to ROOT
CASE
WHEN c.RestrictOrganization = 0 THEN ''NO''
WHEN c.RestrictOrganization = 1 THEN ''YES''
ELSE ''NULL''
END AS ''Restrict Organisation''
' + @cfColumns + ' --Working to Remove NULL values from output
FROM
dbo.tblContract c
LEFT OUTER JOIN dbo.tblContractPeriod cp ON
cp.CID = c.CID
AND cp.ContractID = c.ContractID
LEFT OUTER JOIN dbo.tblContractPeriod cp2 ON
cp2.CID = c.CID
AND cp2.ContractID = c.ContractID
AND cp2.ValidTo =
(
SELECT TOP 1
MAX(cpm.ValidTo)
FROM
dbo.tblContractPeriod cpm
WHERE
cpm.CID = c.CID
AND cpm.ContractID = cp2.ContractID
)
LEFT OUTER JOIN [dbo].[tblVendorAssignments] va ON
va.CID = c.CID
AND va.ElementID = c.ContractID
AND va.ElementType = 1
LEFT OUTER JOIN [dbo].[tblVendorRepository] vr ON
vr.CID = c.[CID]
AND vr.VendorID = va.VendorID
LEFT OUTER JOIN [dbo].[tblOrganization] o ON
o.[CID] = c.CID
AND c.[OrgChecksum] = o.[OrgChecksum]
LEFT OUTER JOIN [dbo].[tblParameterTranslation] pt ON
pt.ParameterType = 7
AND pt.ParameterValue = c.ContractType
AND pt.[Language] = @lang
LEFT OUTER JOIN [dbo].[tblContractCustomTypes] cct ON
cct.CID = c.CID
AND cct.TypeID = c.ContractType
left outer join dbo.' + dbo.GenerateCustomFieldTemporaryName(@cid, 4) + N' cf on
cf.ElementID = c.ContractID
and cf.UserID = @UserID
left outer join lc on
lc.CID = c.CID
and lc.ContractID = c.ContractID
left outer join cc on
cc.CID = c.CID
and cc.ContractID = c.ContractID
left outer join oc on
oc.CID = c.CID
and oc.ContractID = c.ContractID
LEFT JOIN dbo.tblContractCustomTypes ct
ON c.ContractType = ct.TypeID
WHERE
c.CID = @CID
AND
(
IsNull(c.RestrictOrganization, 0) = 0
OR c.ContractID IN
(
SELECT
c1.[ContractID]
FROM
[dbo].[tblContract] c1
INNER JOIN [dbo].[tblSystemUserOrgDefinition] def ON
def.CID = c.CID
AND def.UserID = @UserID
AND def.OrgChecksum = c.OrgChecksum
WHERE
c1.CID = c.CID
AND IsNull(c.RestrictOrganization, 0) > 0
)
)
AND
(
ISNULL(c.RestrictToGroups, 0) = 0
OR @IsAdmin = 1
OR EXISTS
(
SELECT
1
FROM
[dbo].[tblContractGroups] cg
INNER JOIN[dbo].[tblSystemUserGroups] sug ON
sug.UserID = @UserID
AND cg.GroupID = sug.GroupID
WHERE
cg.CID = @CID
AND cg.[ContractID] = c.[ContractID]
)
)
'
+ isnull(@parameters, N'') + N';'
exec sp_executesql @q, @qp, @CID, @UserID, @lang, @UserCurrencyRate, @IsAdmin, @UserCurrency;
END