1
|
ALTER PROCEDURE [dbo].[sp_DataLoad_di_Customer_import] (
|
2
|
@ProcessId varchar(100), @OrganizationID varchar(100), @TenantId varchar(50)
|
3
|
)
|
4
|
AS
|
5
|
BEGIN
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
SET NOCOUNT ON;
|
13
|
|
14
|
Select * into
|
15
|
|
16
|
Declare @defaultBuyingGroup1Id varchar(50) = '';
|
17
|
Select top 1 @defaultBuyingGroup1Id = buying_group_id_one from mst_buying_group_one where tenant_id = @TenantId order by is_default desc
|
18
|
|
19
|
IF((select count(1) from
|
20
|
BEGIN
|
21
|
|
22
|
|
23
|
UPDATE country set country.country_code = x.country_code
|
24
|
FROM
|
25
|
(
|
26
|
select country_code, tenant_id from
|
27
|
|
28
|
group by country_code, tenant_id
|
29
|
) x
|
30
|
where country.country_code = x.country_code and country.tenant_id = x.tenant_id
|
31
|
|
32
|
and x.tenant_id = @TenantId
|
33
|
|
34
|
INSERT into country (country_id, country_code, country_desc, status, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
35
|
SELECT NEWID(), x.country_code, x.country_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
36
|
FROM
|
37
|
(
|
38
|
select country_code, created_by, updated_by, tenant_id from
|
39
|
|
40
|
group by country_code, created_by, updated_by, tenant_id
|
41
|
) x
|
42
|
WHERE NOT EXISTS(SELECT 1 FROM country WHERE country.country_code=x.country_code and country.tenant_id=x.tenant_id)
|
43
|
|
44
|
and x.tenant_id = @TenantId
|
45
|
|
46
|
|
47
|
UPDATE country set country.country_code = x.billing_country_code
|
48
|
FROM
|
49
|
(
|
50
|
select billing_country_code, tenant_id from
|
51
|
|
52
|
group by billing_country_code, tenant_id
|
53
|
) x
|
54
|
where country.country_code = x.billing_country_code and country.tenant_id = x.tenant_id
|
55
|
|
56
|
and x.tenant_id = @TenantId
|
57
|
|
58
|
INSERT into country (country_id, country_code, country_desc, status, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
59
|
SELECT NEWID(), x.billing_country_code, x.billing_country_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
60
|
FROM
|
61
|
(
|
62
|
select billing_country_code, created_by, updated_by, tenant_id from
|
63
|
|
64
|
group by billing_country_code, created_by, updated_by, tenant_id
|
65
|
) x
|
66
|
WHERE NOT EXISTS(SELECT 1 FROM country WHERE country.country_code=x.billing_country_code and country.tenant_id=x.tenant_id)
|
67
|
|
68
|
and x.tenant_id = @TenantId
|
69
|
|
70
|
UPDATE country set country.country_code = x.shipping_country_code
|
71
|
FROM
|
72
|
(
|
73
|
select shipping_country_code, tenant_id from
|
74
|
|
75
|
group by shipping_country_code, tenant_id
|
76
|
) x
|
77
|
where country.country_code = x.shipping_country_code and country.tenant_id = x.tenant_id
|
78
|
|
79
|
and x.tenant_id = @TenantId
|
80
|
|
81
|
INSERT into country (country_id, country_code, country_desc, status, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
82
|
SELECT NEWID(), x.shipping_country_code, x.shipping_country_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
83
|
FROM
|
84
|
(
|
85
|
select shipping_country_code, created_by, updated_by, tenant_id from
|
86
|
|
87
|
group by shipping_country_code, created_by, updated_by, tenant_id
|
88
|
) x
|
89
|
WHERE NOT EXISTS(SELECT 1 FROM country WHERE country.country_code=x.shipping_country_code and country.tenant_id=x.tenant_id)
|
90
|
|
91
|
and x.tenant_id = @TenantId
|
92
|
|
93
|
|
94
|
|
95
|
UPDATE mst_region SET mst_region.region_code = x.region_code, mst_region.country_id = c.country_id
|
96
|
FROM
|
97
|
(
|
98
|
select di.region_code, di.country_code, di.tenant_id, di.org_id from
|
99
|
|
100
|
group by di.region_code, di.country_code, di.tenant_id, di.org_id
|
101
|
) x
|
102
|
, Country c
|
103
|
where mst_region.region_code = x.region_code and mst_region.org_id = x.org_id and c.country_code = x.country_code and c.tenant_id = x.tenant_id
|
104
|
|
105
|
and x.org_id = @OrganizationID
|
106
|
|
107
|
INSERT into mst_region (region_id, region_code, region_desc, status, country_id, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
108
|
SELECT NEWID(), x.region_code, x.region_code, 'Active', c.country_id, GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.org_id
|
109
|
from
|
110
|
(
|
111
|
select di.region_code, di.country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id from
|
112
|
|
113
|
group by di.region_code, di.country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id
|
114
|
) x
|
115
|
inner join country c on c.country_code = x.country_code and c.tenant_id = x.tenant_id
|
116
|
where NOT EXISTS(SELECT 1 FROM mst_region WHERE mst_region.region_code=x.region_code and mst_region.org_id=x.org_id AND mst_region.tenant_id = x.tenant_id)
|
117
|
|
118
|
and x.org_id = @OrganizationID
|
119
|
|
120
|
|
121
|
|
122
|
UPDATE mst_region SET mst_region.region_code = x.billing_region_code, mst_region.country_id = c.country_id
|
123
|
FROM
|
124
|
(
|
125
|
select di.billing_region_code, di.billing_country_code, di.tenant_id, di.org_id from
|
126
|
|
127
|
group by di.billing_region_code, di.billing_country_code, di.tenant_id, di.org_id
|
128
|
) x
|
129
|
, Country c
|
130
|
where mst_region.region_code = x.billing_region_code and mst_region.org_id = x.org_id and c.country_code = x.billing_country_code and c.tenant_id = x.tenant_id
|
131
|
|
132
|
and x.org_id = @OrganizationID
|
133
|
|
134
|
INSERT into mst_region (region_id, region_code, region_desc, status, country_id, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
135
|
SELECT NEWID(), x.billing_region_code, x.billing_region_code, 'Active', c.country_id, GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.org_id
|
136
|
from
|
137
|
(
|
138
|
select di.billing_region_code, di.billing_country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id from
|
139
|
|
140
|
group by di.billing_region_code, di.billing_country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id
|
141
|
) x
|
142
|
inner join country c on c.country_code = x.billing_country_code and c.tenant_id = x.tenant_id
|
143
|
where NOT EXISTS(SELECT 1 FROM mst_region WHERE mst_region.region_code=x.billing_region_code and mst_region.org_id=x.org_id AND mst_region.tenant_id = x.tenant_id)
|
144
|
|
145
|
and x.org_id = @OrganizationID
|
146
|
|
147
|
UPDATE mst_region SET mst_region.region_code = x.shipping_region_code, mst_region.country_id = c.country_id
|
148
|
FROM
|
149
|
(
|
150
|
select di.shipping_region_code, di.shipping_country_code, di.tenant_id, di.org_id from
|
151
|
|
152
|
group by di.shipping_region_code, di.shipping_country_code, di.tenant_id, di.org_id
|
153
|
) x
|
154
|
, Country c
|
155
|
where mst_region.region_code = x.shipping_region_code and mst_region.org_id = x.org_id and c.country_code = x.shipping_country_code and c.tenant_id = x.tenant_id
|
156
|
|
157
|
and x.org_id = @OrganizationID
|
158
|
|
159
|
INSERT into mst_region (region_id, region_code, region_desc, status, country_id, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
160
|
SELECT NEWID(), x.shipping_region_code, x.shipping_region_code, 'Active', c.country_id, GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.org_id
|
161
|
from
|
162
|
(
|
163
|
select di.shipping_region_code, di.shipping_country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id from
|
164
|
|
165
|
group by di.shipping_region_code, di.shipping_country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id
|
166
|
) x
|
167
|
inner join country c on c.country_code = x.shipping_country_code and c.tenant_id = x.tenant_id
|
168
|
where NOT EXISTS(SELECT 1 FROM mst_region WHERE mst_region.region_code=x.shipping_region_code and mst_region.org_id=x.org_id AND mst_region.tenant_id = x.tenant_id)
|
169
|
|
170
|
and x.org_id = @OrganizationID
|
171
|
|
172
|
|
173
|
|
174
|
UPDATE state SET state.state_code = x.state_code, state.country_id = c.country_id, state.region_id = r.region_id
|
175
|
FROM
|
176
|
(
|
177
|
select di.state_code, di.region_code, di.country_code, di.tenant_id, di.org_id from
|
178
|
|
179
|
group by di.state_code, di.region_code, di.country_code, di.tenant_id, di.org_id
|
180
|
) x
|
181
|
, Country c, mst_region r
|
182
|
where state.state_code = x.state_code and state.org_id = x.org_id and c.country_code = x.country_code and c.tenant_id = x.tenant_id and r.region_code = x.region_code and r.org_id = x.org_id
|
183
|
|
184
|
and x.org_id = @OrganizationID
|
185
|
|
186
|
INSERT into state (state_id, state_code, state_desc, status, country_id, created_date, created_by, updated_date, updated_by, tenant_id, org_id, region_id)
|
187
|
SELECT NEWID(), x.state_code, x.state_code, 'Active', c.country_id, GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.org_id, r.region_id
|
188
|
from
|
189
|
(
|
190
|
select di.state_code, di.region_code, di.country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id from
|
191
|
|
192
|
group by di.state_code, di.region_code, di.country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id
|
193
|
) x
|
194
|
inner join country c on c.country_code = x.country_code and c.tenant_id = x.tenant_id
|
195
|
inner join mst_region r on r.region_code = x.region_code and r.org_id = x.org_id
|
196
|
where NOT EXISTS(SELECT 1 FROM state WHERE state.state_code=x.state_code and state.org_id=x.org_id)
|
197
|
|
198
|
and x.org_id = @OrganizationID
|
199
|
|
200
|
|
201
|
UPDATE state SET state.state_code = x.billing_state_code, state.country_id = c.country_id, state.region_id = r.region_id
|
202
|
FROM
|
203
|
(
|
204
|
select di.billing_state_code, di.billing_region_code, di.billing_country_code, di.tenant_id, di.org_id from
|
205
|
|
206
|
group by di.billing_state_code, di.billing_region_code, di.billing_country_code, di.tenant_id, di.org_id
|
207
|
) x
|
208
|
, Country c, mst_region r
|
209
|
where state.state_code = x.billing_state_code and state.org_id = x.org_id and c.country_code = x.billing_country_code and c.tenant_id = x.tenant_id and r.region_code = x.billing_region_code and r.org_id = x.org_id
|
210
|
|
211
|
and x.org_id = @OrganizationID
|
212
|
|
213
|
INSERT into state (state_id, state_code, state_desc, status, country_id, created_date, created_by, updated_date, updated_by, tenant_id, org_id, region_id)
|
214
|
SELECT NEWID(), x.billing_state_code, x.billing_state_code, 'Active', c.country_id, GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.org_id, r.region_id
|
215
|
from
|
216
|
(
|
217
|
select di.billing_state_code, di.billing_region_code, di.billing_country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id from
|
218
|
|
219
|
group by di.billing_state_code, di.billing_region_code, di.billing_country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id
|
220
|
) x
|
221
|
inner join country c on c.country_code = x.billing_country_code and c.tenant_id = x.tenant_id
|
222
|
inner join mst_region r on r.region_code = x.billing_region_code and r.org_id = x.org_id
|
223
|
where NOT EXISTS(SELECT 1 FROM state WHERE state.state_code=x.billing_state_code and state.org_id=x.org_id)
|
224
|
|
225
|
and x.org_id = @OrganizationID
|
226
|
|
227
|
UPDATE state SET state.state_code = x.shipping_state_code, state.country_id = c.country_id, state.region_id = r.region_id
|
228
|
FROM
|
229
|
(
|
230
|
select di.shipping_state_code, di.shipping_region_code, di.shipping_country_code, di.tenant_id, di.org_id from
|
231
|
|
232
|
group by di.shipping_state_code, di.shipping_region_code, di.shipping_country_code, di.tenant_id, di.org_id
|
233
|
) x
|
234
|
, Country c, mst_region r
|
235
|
where state.state_code = x.shipping_state_code and state.org_id = x.org_id and c.country_code = x.shipping_country_code and c.tenant_id = x.tenant_id and r.region_code = x.shipping_region_code and r.org_id = x.org_id
|
236
|
|
237
|
and x.org_id = @OrganizationID
|
238
|
|
239
|
INSERT into state (state_id, state_code, state_desc, status, country_id, created_date, created_by, updated_date, updated_by, tenant_id, org_id, region_id)
|
240
|
SELECT NEWID(), x.shipping_state_code, x.shipping_state_code, 'Active', c.country_id, GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.org_id, r.region_id
|
241
|
from
|
242
|
(
|
243
|
select di.shipping_state_code, di.shipping_region_code, di.shipping_country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id from
|
244
|
|
245
|
group by di.shipping_state_code, di.shipping_region_code, di.shipping_country_code, di.created_by, di.updated_by, di.tenant_id, di.org_id
|
246
|
) x
|
247
|
inner join country c on c.country_code = x.shipping_country_code and c.tenant_id = x.tenant_id
|
248
|
inner join mst_region r on r.region_code = x.shipping_region_code and r.org_id = x.org_id
|
249
|
where NOT EXISTS(SELECT 1 FROM state WHERE state.state_code=x.shipping_state_code and state.org_id=x.org_id)
|
250
|
|
251
|
and x.org_id = @OrganizationID
|
252
|
|
253
|
|
254
|
|
255
|
UPDATE chain SET chain.chain_code = x.chain_code
|
256
|
FROM
|
257
|
(
|
258
|
select di.chain_code, di.tenant_id, di.org_id from
|
259
|
|
260
|
group by di.chain_code, di.tenant_id, di.org_id
|
261
|
) x
|
262
|
where chain.chain_code = x.chain_code and chain.tenant_id = x.tenant_id
|
263
|
|
264
|
and x.tenant_id = @TenantId and x.org_id=@OrganizationID
|
265
|
|
266
|
INSERT into chain (chain_id, org_id, chain_code, chain_desc, chain_short_desc, status, created_date, created_by, updated_date, updated_by, tenant_id)
|
267
|
SELECT NEWID(), x.org_id, x.chain_code, x.chain_code, x.chain_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id
|
268
|
from
|
269
|
(
|
270
|
select di.chain_code, di.created_by, di.updated_by, di.tenant_id, di.org_id from
|
271
|
where IsNull(di.chain_code,'') <> ''
|
272
|
group by di.chain_code, di.created_by, di.updated_by, di.tenant_id, di.org_id
|
273
|
) x
|
274
|
WHERE NOT EXISTS(SELECT 1 FROM chain WHERE chain.chain_code = x.chain_code and chain.tenant_id = x.tenant_id)
|
275
|
|
276
|
and x.tenant_id = @TenantId
|
277
|
|
278
|
and x.org_id=@OrganizationID
|
279
|
|
280
|
|
281
|
|
282
|
|
283
|
UPDATE channel SET channel_code = x.channel_code
|
284
|
FROM
|
285
|
(
|
286
|
select di.channel_code, di.org_id, di.tenant_id from
|
287
|
|
288
|
group by di.channel_code, di.org_id, di.tenant_id
|
289
|
) x
|
290
|
where channel.channel_code = x.channel_code and channel.tenant_id = x.tenant_id
|
291
|
|
292
|
and x.org_id = @OrganizationID
|
293
|
|
294
|
INSERT into channel (channel_id, org_id, channel_code, channel_desc, channel_short_desc, sector_id, sub_channel, status, created_date, created_by, updated_date, updated_by, tenant_id)
|
295
|
SELECT NEWID(), x.org_id, x.channel_code, x.channel_code, x.channel_code, 'NA', 'NA', 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id
|
296
|
FROM
|
297
|
(
|
298
|
select di.channel_code, di.created_by, di.updated_by, di.org_id, di.tenant_id from
|
299
|
where IsNull(di.channel_code,'') <> ''
|
300
|
group by di.channel_code, di.org_id, di.created_by, di.updated_by, di.tenant_id
|
301
|
) x
|
302
|
WHERE NOT EXISTS(SELECT 1 FROM channel WHERE channel.channel_code = x.channel_code and channel.tenant_id = x.tenant_id)
|
303
|
|
304
|
and x.org_id = @OrganizationID
|
305
|
|
306
|
|
307
|
|
308
|
UPDATE cluster SET cluster.cluster_code = x.cluster_code
|
309
|
FROM
|
310
|
(
|
311
|
select di.cluster_code, di.channel_code, di.org_id, di.tenant_id from
|
312
|
|
313
|
group by di.cluster_code, di.channel_code, di.org_id, di.tenant_id
|
314
|
) x
|
315
|
inner join channel c on c.channel_code = x.channel_code and c.tenant_id = x.tenant_id
|
316
|
where cluster.cluster_code = x.cluster_code and cluster.channel_id = c.channel_id and cluster.tenant_id = x.tenant_id
|
317
|
|
318
|
and x.org_id = @OrganizationID
|
319
|
|
320
|
INSERT into cluster (cluster_id, channel_id, org_id, cluster_code, cluster_desc, cluster_short_desc, status, created_date, created_by, updated_date, updated_by, tenant_id)
|
321
|
SELECT NEWID(), c.channel_id, x.tenant_id, x.cluster_code, x.cluster_code, x.cluster_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id
|
322
|
from
|
323
|
(
|
324
|
select di.cluster_code, di.org_id, di.created_by, di.updated_by, di.tenant_id, di.channel_code from
|
325
|
where IsNull(di.cluster_code,'') <> ''
|
326
|
group by di.cluster_code, di.org_id, di.created_by, di.updated_by, di.tenant_id, di.channel_code
|
327
|
) x
|
328
|
inner join channel c on c.channel_code = x.channel_code and c.tenant_id = x.tenant_id
|
329
|
where NOT EXISTS(SELECT 1 FROM cluster WHERE cluster.cluster_code=x.cluster_code and cluster.channel_id = c.channel_id and cluster.tenant_id = x.tenant_id)
|
330
|
|
331
|
and x.org_id = @OrganizationID
|
332
|
|
333
|
|
334
|
|
335
|
UPDATE mst_branch set branch_code = x.branch_code
|
336
|
FROM
|
337
|
(
|
338
|
select di.branch_code, di.org_id from
|
339
|
|
340
|
group by di.branch_code, di.org_id
|
341
|
) x
|
342
|
where mst_branch.branch_code = x.branch_code and mst_branch.org_id = x.org_id
|
343
|
|
344
|
and x.org_id = @OrganizationID
|
345
|
|
346
|
INSERT into mst_branch (branch_id, branch_code, branch_desc, status, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
347
|
SELECT NEWID(), x.branch_code, x.branch_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.org_id
|
348
|
from
|
349
|
(
|
350
|
select di.branch_code, di.created_by, di.updated_by, di.org_id, di.tenant_id from
|
351
|
|
352
|
group by di.branch_code, di.created_by, di.updated_by, di.org_id, di.tenant_id
|
353
|
) x
|
354
|
where NOT EXISTS(SELECT 1 FROM mst_branch WHERE mst_branch.branch_code = x.branch_code and mst_branch.org_id=x.org_id)
|
355
|
|
356
|
and x.org_id = @OrganizationID
|
357
|
|
358
|
|
359
|
|
360
|
UPDATE mst_area SET mst_area.area_code = x.area_code
|
361
|
FROM
|
362
|
(
|
363
|
select di.area_code, di.org_id from
|
364
|
|
365
|
group by di.area_code, di.org_id
|
366
|
) x
|
367
|
where mst_area.area_code = x.area_code and mst_area.org_id = x.org_id
|
368
|
|
369
|
and x.org_id = @OrganizationID
|
370
|
|
371
|
INSERT into mst_area (area_id, area_code, area_description, status, branch_id, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
372
|
SELECT NEWID(), x.area_code, x.area_code, 'Active', b.branch_id, GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.org_id
|
373
|
FROM
|
374
|
(
|
375
|
select di.area_code, di.branch_code, di.created_by, di.updated_by, di.org_id, di.tenant_id from
|
376
|
|
377
|
group by di.area_code, di.branch_code, di.created_by, di.updated_by, di.org_id, di.tenant_id
|
378
|
) x
|
379
|
inner join mst_branch b on b.branch_code = x.branch_code and b.org_id = x.org_id
|
380
|
where NOT EXISTS(SELECT 1 FROM mst_area WHERE mst_area.area_code=x.area_code and mst_area.org_id=x.org_id and mst_area.branch_id = b.branch_id)
|
381
|
|
382
|
and x.org_id = @OrganizationID
|
383
|
|
384
|
|
385
|
|
386
|
UPDATE mst_terms_code set mst_terms_code.terms_code = x.terms_code
|
387
|
FROM
|
388
|
(
|
389
|
select di.terms_code, di.tenant_id from
|
390
|
|
391
|
group by di.terms_code, di.tenant_id
|
392
|
) x
|
393
|
where mst_terms_code.terms_code = x.terms_code and mst_terms_code.tenant_id = x.tenant_id
|
394
|
|
395
|
and x.tenant_id = @TenantId
|
396
|
|
397
|
INSERT into mst_terms_code (terms_id, terms_code, terms_description, terms_days, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id)
|
398
|
SELECT NEWID(), x.terms_code, x.terms_code, x.terms_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
399
|
FROM
|
400
|
(
|
401
|
select di.terms_code, di.created_by, di.updated_by, di.tenant_id from
|
402
|
|
403
|
group by di.terms_code, di.created_by, di.updated_by, di.tenant_id
|
404
|
) x
|
405
|
where NOT EXISTS(SELECT 1 FROM mst_terms_code WHERE mst_terms_code.terms_code=x.terms_code and mst_terms_code.tenant_id=x.tenant_id)
|
406
|
|
407
|
and x.tenant_id = @TenantId
|
408
|
|
409
|
|
410
|
|
411
|
UPDATE mst_price_code set mst_price_code.price_code = x.price_code
|
412
|
FROM
|
413
|
(
|
414
|
select di.price_code, di.org_id, di.tenant_id from
|
415
|
|
416
|
group by di.price_code, di.org_id, di.tenant_id
|
417
|
) x
|
418
|
where mst_price_code.price_code = x.price_code and mst_price_code.org_id = x.org_id and mst_price_code.tenant_id = x.tenant_id
|
419
|
|
420
|
and x.org_id = @OrganizationID
|
421
|
|
422
|
INSERT into mst_price_code (price_id, price_code, price_description, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id)
|
423
|
SELECT NEWID(), x.price_code, x.price_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.org_id, x.tenant_id
|
424
|
from
|
425
|
(
|
426
|
select di.price_code, di.created_by, di.updated_by, di.org_id, di.tenant_id from
|
427
|
|
428
|
group by di.price_code, di.created_by, di.updated_by, di.org_id, di.tenant_id
|
429
|
) x
|
430
|
WHERE NOT EXISTS (SELECT 1 FROM mst_price_code WHERE mst_price_code.price_code=x.price_code and mst_price_code.org_id=x.org_id and mst_price_code.tenant_id=x.tenant_id)
|
431
|
|
432
|
and x.org_id = @OrganizationID
|
433
|
|
434
|
|
435
|
|
436
|
UPDATE mst_invoice_discount set mst_invoice_discount.invoice_discount_code = x.invoice_discount_code
|
437
|
FROM
|
438
|
(
|
439
|
select di.invoice_discount_code, di.tenant_id from
|
440
|
|
441
|
group by di.invoice_discount_code, di.tenant_id
|
442
|
) x
|
443
|
where mst_invoice_discount.invoice_discount_code = x.invoice_discount_code and mst_invoice_discount.tenant_id = x.tenant_id
|
444
|
|
445
|
and x.tenant_id = @TenantId
|
446
|
|
447
|
INSERT into mst_invoice_discount(invoice_discount_id, invoice_discount_code, invoice_description, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id)
|
448
|
SELECT NEWID(), x.invoice_discount_code, x.invoice_discount_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
449
|
from
|
450
|
(
|
451
|
select di.invoice_discount_code, di.created_by, di.updated_by, di.tenant_id from
|
452
|
|
453
|
group by di.invoice_discount_code, di.created_by, di.updated_by, di.tenant_id
|
454
|
) x
|
455
|
WHERE NOT EXISTS(SELECT 1 FROM mst_invoice_discount WHERE mst_invoice_discount.invoice_discount_code=x.invoice_discount_code and mst_invoice_discount.tenant_id=x.tenant_id)
|
456
|
|
457
|
and x.tenant_id = @TenantId
|
458
|
|
459
|
|
460
|
|
461
|
UPDATE mst_buying_group_one set mst_buying_group_one.buying_group_one_code = x.buying_group_one_code
|
462
|
FROM
|
463
|
(
|
464
|
select di.buying_group_one_code, di.tenant_id from
|
465
|
|
466
|
group by di.buying_group_one_code, di.tenant_id
|
467
|
) x
|
468
|
where mst_buying_group_one.buying_group_one_code = x.buying_group_one_code and mst_buying_group_one.tenant_id = x.tenant_id
|
469
|
|
470
|
and x.tenant_id = @TenantId
|
471
|
|
472
|
INSERT into mst_buying_group_one(buying_group_id_one, buying_group_one_code, buying_group_one_desc, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id)
|
473
|
SELECT NEWID(), x.buying_group_one_code, x.buying_group_one_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
474
|
from
|
475
|
(
|
476
|
select di.buying_group_one_code, di.created_by, di.updated_by, di.tenant_id from
|
477
|
where IsNull(di.buying_group_one_code,'') <>''
|
478
|
group by di.buying_group_one_code, di.created_by, di.updated_by, di.tenant_id
|
479
|
) x
|
480
|
WHERE NOT EXISTS(SELECT 1 FROM mst_buying_group_one WHERE mst_buying_group_one.buying_group_one_code=x.buying_group_one_code and mst_buying_group_one.tenant_id=x.tenant_id)
|
481
|
|
482
|
and x.tenant_id = @TenantId
|
483
|
|
484
|
|
485
|
|
486
|
UPDATE mst_buying_group_two set mst_buying_group_two.buying_group_two_code = x.buying_group_two_code
|
487
|
FROM
|
488
|
(
|
489
|
select di.buying_group_two_code, di.tenant_id from
|
490
|
|
491
|
group by di.buying_group_two_code, di.tenant_id
|
492
|
) x
|
493
|
where mst_buying_group_two.buying_group_two_code = x.buying_group_two_code and mst_buying_group_two.tenant_id = x.tenant_id
|
494
|
|
495
|
and x.tenant_id = @TenantId
|
496
|
|
497
|
INSERT into mst_buying_group_two(buying_group_id_two, buying_group_two_code, buying_group_two_desc, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id)
|
498
|
SELECT NEWID(), x.buying_group_two_code, x.buying_group_two_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
499
|
from
|
500
|
(
|
501
|
select di.buying_group_two_code, di.created_by, di.updated_by, di.tenant_id from
|
502
|
where IsNull(di.buying_group_two_code,'') <> ''
|
503
|
group by di.buying_group_two_code, di.created_by, di.updated_by, di.tenant_id
|
504
|
) x
|
505
|
WHERE NOT EXISTS(SELECT 1 FROM mst_buying_group_two WHERE mst_buying_group_two.buying_group_two_code=x.buying_group_two_code and mst_buying_group_two.tenant_id=x.tenant_id)
|
506
|
|
507
|
and x.tenant_id = @TenantId
|
508
|
|
509
|
|
510
|
|
511
|
UPDATE mst_buying_group_three set mst_buying_group_three.buying_group_three_code = x.buying_group_three_code
|
512
|
FROM
|
513
|
(
|
514
|
select di.buying_group_three_code, di.tenant_id from
|
515
|
|
516
|
group by di.buying_group_three_code, di.tenant_id
|
517
|
) x
|
518
|
where mst_buying_group_three.buying_group_three_code = x.buying_group_three_code and mst_buying_group_three.tenant_id = x.tenant_id
|
519
|
|
520
|
and x.tenant_id = @TenantId
|
521
|
|
522
|
INSERT into mst_buying_group_three(buying_group_id_three, buying_group_three_code, buying_group_three_desc, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id)
|
523
|
SELECT NEWID(), x.buying_group_three_code, x.buying_group_three_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
524
|
from
|
525
|
(
|
526
|
select di.buying_group_three_code, di.created_by, di.updated_by, di.tenant_id from
|
527
|
where IsNull(di.buying_group_three_code,'') <> ''
|
528
|
group by di.buying_group_three_code, di.created_by, di.updated_by, di.tenant_id
|
529
|
) x
|
530
|
WHERE NOT EXISTS(SELECT 1 FROM mst_buying_group_three WHERE mst_buying_group_three.buying_group_three_code=x.buying_group_three_code and mst_buying_group_three.tenant_id=x.tenant_id)
|
531
|
|
532
|
and x.tenant_id = @TenantId
|
533
|
|
534
|
|
535
|
|
536
|
|
537
|
|
538
|
|
539
|
update member set member.member_cd = x.person_in_charge
|
540
|
FROM
|
541
|
(
|
542
|
select di.person_in_charge, di.org_id from
|
543
|
|
544
|
group by di.person_in_charge, di.org_id
|
545
|
) x
|
546
|
where member.member_cd = x.person_in_charge and member.org_id = x.org_id
|
547
|
|
548
|
and x.org_id = @OrganizationID
|
549
|
|
550
|
INSERT into member (member_id, member_cd, org_id, first_name, created_date, created_by, updated_date, updated_by, status)
|
551
|
SELECT NEWID(), x.person_in_charge, x.org_id, x.person_in_charge, GETDATE(), x.created_by, GETDATE(), x.updated_by, 'Active'
|
552
|
FROM
|
553
|
(
|
554
|
select di.person_in_charge, di.created_by, di.updated_by, di.org_id, di.tenant_id from
|
555
|
|
556
|
group by di.person_in_charge, di.created_by, di.updated_by, di.org_id, di.tenant_id
|
557
|
) x
|
558
|
WHERE NOT EXISTS(SELECT 1 FROM member WHERE member.member_cd=x.person_in_charge and member.org_id=x.org_id)
|
559
|
|
560
|
and x.org_id = @OrganizationID
|
561
|
|
562
|
|
563
|
|
564
|
|
565
|
|
566
|
INSERT into member_branch (id, org_id, tenant_id, member_id, branch_id, status, created_date, created_by, updated_date, updated_by)
|
567
|
SELECT NEWID() [id], di.org_id [org_id], di.tenant_id [tenant_id], m.member_id [member_id], b.branch_id [branch_id],
|
568
|
'Active' [status], GetDate() [created_date], di.created_by [created_by], GetDate() [updated_date], di.updated_by [updated_by]
|
569
|
from
|
570
|
left join member m on m.member_cd = di.person_in_charge
|
571
|
left join mst_branch b on b.branch_code = di.branch_code
|
572
|
where
|
573
|
m.org_id = di.org_id and b.org_id = di.org_id
|
574
|
and Not Exists (Select 1 from member_branch mb where mb.member_id = m.member_id and mb.branch_id = b.branch_id)
|
575
|
Group By di.org_id, di.tenant_id, m.member_id, b.branch_id, di.created_by, di.updated_by
|
576
|
|
577
|
|
578
|
|
579
|
|
580
|
Declare @salesmanLevelParent varchar(50) = '', @groupCheck int = 0, @hierarchyId varchar(50) = '', @salesmanLevel varchar(50) =''
|
581
|
|
582
|
|
583
|
|
584
|
|
585
|
|
586
|
Select @salesmanLevelParent = l2.hierarchy_level_id, @salesmanLevel = l.hierarchy_level_id
|
587
|
from org_hierarchy_level l
|
588
|
left join org_hierarchy_level l2 on l2.hierarchy_level = (l.hierarchy_level - 1)
|
589
|
where l.hierarchy_type = 'Salesman' and l.tenant_id = @TenantId
|
590
|
and l2.tenant_id = @TenantId
|
591
|
|
592
|
|
593
|
Select @groupCheck = Count(hierarchy_group_id) from org_hierarchy_group g
|
594
|
left join org_hierarchy_level l on l.hierarchy_level_id = g.hierarchy_level_id
|
595
|
where l.hierarchy_type = 'Salesman' and g.org_id = @OrganizationID
|
596
|
|
597
|
|
598
|
IF (@groupCheck <> 1)
|
599
|
BEGIN
|
600
|
SET @hierarchyId = 'Imported-Salesman-Group-' + @OrganizationID;
|
601
|
|
602
|
Insert into org_hierarchy_group (hierarchy_group_id, hierarchy_level_id, hierarchy_group_name, hierarchy_group_parent, status, created_by, created_date, updated_by, updated_date, org_id, tenant_id)
|
603
|
Select 'Imported-Salesman-Group-' + @OrganizationID [hierarchy_group_id], @salesmanLevel [hierarchy_level_id], 'Imported-Salesman-Group-'+@OrganizationID [hierarchy_group_name],
|
604
|
Max(g.hierarchy_group_id) [hierarchy_group_parent], 'Active' [status], 'System' [created_by], GetDate() [created_date], 'System' [updated_by], GetDate() [updated_date], @OrganizationID [org_id], @TenantId [tenant_id]
|
605
|
from org_hierarchy_group g
|
606
|
where g.org_id = @OrganizationID and g.tenant_id = @TenantId
|
607
|
and g.hierarchy_level_id = @salesmanLevelParent
|
608
|
and Not Exists (Select 1 from org_hierarchy_group db where db.org_id = @OrganizationID and db.tenant_id = @TenantId and db.hierarchy_level_id = @salesmanLevel and db.hierarchy_group_name = 'Imported-Salesman-Group-' + @OrganizationID)
|
609
|
Group By g.hierarchy_level_id
|
610
|
END
|
611
|
ELSE
|
612
|
BEGIN
|
613
|
Select @hierarchyId = IsNull(Max(hierarchy_group_id), '') from org_hierarchy_group g
|
614
|
left join org_hierarchy_level l on l.hierarchy_level_id = g.hierarchy_level_id
|
615
|
where l.hierarchy_type = 'Salesman' and g.org_id = @OrganizationID
|
616
|
END
|
617
|
|
618
|
|
619
|
Insert into org_hierarchy_group_member (hierarchy_member_id, hierarchy_group_id, member_id, status, created_by, created_date, updated_by, updated_date, org_id, tenant_id)
|
620
|
Select NewId() [hierarchy_member_id],
|
621
|
@hierarchyId [hierarchy_group_id],
|
622
|
m.member_id [member_id], 'Active' [status], 'System' [created_by], GetDate() [created_date], 'System' [updated_by], GetDate() [updated_date], @OrganizationID [org_id], @TenantId [tenant_id]
|
623
|
from
|
624
|
Left Join member m on m.member_cd = di.person_in_charge and m.org_id = di.org_id
|
625
|
where
|
626
|
Not Exists (Select 1 from org_hierarchy_group_member gm where gm.member_id = m.member_id and gm.org_id = @OrganizationID and gm.tenant_id = @TenantId)
|
627
|
and m.org_id = @OrganizationID
|
628
|
Group By m.member_id
|
629
|
|
630
|
|
631
|
exec web_spUpdateOrgHierarchyMapping
|
632
|
@OrgId = @OrganizationID,
|
633
|
@TenantId = @TenantId,
|
634
|
@LoginId = 'System',
|
635
|
@HierarchyGroupId = @hierarchyId,
|
636
|
@Task = 'Insert',
|
637
|
@DeleteKey = '',
|
638
|
@NewParentId = '',
|
639
|
@OldParentId = ''
|
640
|
|
641
|
|
642
|
|
643
|
|
644
|
UPDATE mst_customer_class set mst_customer_class.customer_class_code = x.customer_class_code
|
645
|
FROM
|
646
|
(
|
647
|
select customer_class_code, org_id, tenant_id from
|
648
|
|
649
|
group by customer_class_code, org_id, tenant_id
|
650
|
) x
|
651
|
where mst_customer_class.customer_class_code = x.customer_class_code and mst_customer_class.tenant_id = x.tenant_id
|
652
|
|
653
|
and x.org_id = @OrganizationID
|
654
|
|
655
|
INSERT into mst_customer_class (customer_class_id, customer_class_code, customer_class_description, status, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
656
|
SELECT NEWID(), x.customer_class_code, x.customer_class_code, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
657
|
FROM
|
658
|
(
|
659
|
select customer_class_code, org_id, created_by, updated_by, tenant_id from
|
660
|
|
661
|
where IsNull(customer_class_code, '') <> ''
|
662
|
group by customer_class_code, org_id, created_by, updated_by, tenant_id
|
663
|
) x
|
664
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_class WHERE mst_customer_class.customer_class_code=x.customer_class_code and mst_customer_class.tenant_id=x.tenant_id)
|
665
|
|
666
|
and x.org_id = @OrganizationID
|
667
|
|
668
|
|
669
|
|
670
|
UPDATE mst_outlet_type set mst_outlet_type.outlet_type_code = x.outlet_type
|
671
|
FROM
|
672
|
(
|
673
|
select outlet_type, tenant_id from
|
674
|
|
675
|
group by outlet_type, tenant_id
|
676
|
) x
|
677
|
where mst_outlet_type.outlet_type_code = x.outlet_type and mst_outlet_type.tenant_id = x.tenant_id
|
678
|
|
679
|
and x.tenant_id = @TenantId
|
680
|
|
681
|
INSERT into mst_outlet_type (outlet_type_id, outlet_type_code, outlet_type_desc, status, created_date, created_by, updated_date, updated_by, tenant_id, org_id)
|
682
|
SELECT NEWID(), x.outlet_type, x.outlet_type, 'Active', GETDATE(), x.created_by, GETDATE(), x.updated_by, x.tenant_id, x.tenant_id
|
683
|
FROM
|
684
|
(
|
685
|
select outlet_type, created_by, updated_by, tenant_id from
|
686
|
|
687
|
group by outlet_type, created_by, updated_by, tenant_id
|
688
|
) x
|
689
|
WHERE NOT EXISTS(SELECT 1 FROM mst_outlet_type WHERE mst_outlet_type.outlet_type_code=x.outlet_type and mst_outlet_type.tenant_id=x.tenant_id)
|
690
|
|
691
|
and x.tenant_id = @TenantId
|
692
|
|
693
|
|
694
|
SELECT c.customer_id AS 'customer_id',c.customer_code AS 'customer_code'Into
|
695
|
inner join customer c on c.customer_code=di.customer_code
|
696
|
|
697
|
|
698
|
|
699
|
|
700
|
IF(SELECT auto_populate_customer_code from org where org_id = @OrganizationID) = 'No'
|
701
|
BEGIN
|
702
|
|
703
|
|
704
|
|
705
|
|
706
|
update cst set
|
707
|
cst.customer_name=IsNull(a.customer_name, ''),
|
708
|
cst.customer_Short_name=IsNull(a.customer_short_name, ''),
|
709
|
cst.state_id=IsNull(s.state_id, ''),
|
710
|
cst.chain_id=IsNull(c.chain_id, ''),
|
711
|
cst.channel_id=IsNull(ch.channel_id, ''),
|
712
|
cst.cluster_id=IsNull(cl.cluster_id, ''),
|
713
|
cst.status=IsNull(a.status, ''),
|
714
|
cst.updated_date=IsNull(a.updated_date, ''),
|
715
|
cst.updated_by=IsNull(a.updated_by, ''),
|
716
|
cst.country_id=IsNull(cy.country_id, ''),
|
717
|
cst.area=IsNull(ma.area_description, ''),
|
718
|
cst.address=IsNull(a.address1, ''),
|
719
|
cst.contact_no=IsNull(a.tel_no, ''),
|
720
|
cst.pic=IsNull(a.pic, ''),
|
721
|
cst.latitude=IsNull(a.latitude, ''),
|
722
|
cst.longitude=IsNull(a.longitude, ''),
|
723
|
cst.branch_id=IsNull(b.branch_id, ''),
|
724
|
cst.store_number=IsNull(a.store_number, ''),
|
725
|
cst.special_instruction=IsNull(a.special_instruction, ''),
|
726
|
cst.person_in_charge=IsNull(m.member_cd, ''),
|
727
|
cst.customer_on_hold=IsNull(a.customer_on_hold, '') ,
|
728
|
cst.terms_id=IsNull(ct.terms_id, '') ,
|
729
|
cst.price_id=IsNull(pc.price_id, ''),
|
730
|
cst.invoice_discount_id=IsNull(mid.invoice_discount_id, ''),
|
731
|
cst.area_id=IsNull(ma.area_id, '') ,
|
732
|
cst.salesperson_id=IsNull(m.member_id, ''),
|
733
|
cst.distributor_code=IsNull(a.distributor_code, ''),
|
734
|
cst.distributor_name=IsNull(a.distributor_name, ''),
|
735
|
cst.outlet_type_id=IsNull(ot.outlet_type_id, ''),
|
736
|
cst.buying_group_id_one=IsNull(buying_group_one.buying_group_id_one, @defaultBuyingGroup1Id),
|
737
|
cst.buying_group_id_two=IsNull(buying_group_two.buying_group_id_two, ''),
|
738
|
cst.buying_group_id_three=IsNull(buying_group_three.buying_group_id_three, ''),
|
739
|
cst.mt_gt = IsNull(a.mtgt, ''),
|
740
|
cst.customer_type = IsNull(a.customer_type, '')
|
741
|
FROM customer cst
|
742
|
inner join
|
743
|
inner join state s on a.state_code=s.state_code and s.org_id = a.tenant_id
|
744
|
inner join chain c on a.chain_code=c.chain_code and c.org_id= a.org_id
|
745
|
inner join channel ch on a.channel_code=ch.channel_code and ch.tenant_id = a.tenant_id
|
746
|
inner join cluster cl on a.cluster_code=cl.cluster_code and cl.channel_id = ch.channel_id and cl.tenant_id = a.tenant_id
|
747
|
inner join country cy on a.country_code=cy.country_code and cy.org_id= a.tenant_id
|
748
|
inner join mst_branch b on a.branch_code=b.branch_code and b.org_id= a.org_id
|
749
|
inner join mst_area ma on a.area_code=ma.area_code and ma.org_id= a.org_id and b.branch_id = ma.branch_id
|
750
|
inner join mst_terms_code ct on a.terms_code=ct.terms_code and ct.tenant_id= a.tenant_id
|
751
|
inner join mst_price_code pc on a.price_code=pc.price_code and pc.tenant_id= a.tenant_id and pc.org_id = a.org_id
|
752
|
inner join mst_invoice_discount mid on a.invoice_discount_code= mid.invoice_discount_code and mid.tenant_id= a.tenant_id
|
753
|
inner join member m on a.person_in_charge =m.member_cd and m.org_id=a.org_id
|
754
|
left join mst_outlet_type ot on ot.outlet_type_code = a.outlet_type and ot.tenant_id = a.tenant_id
|
755
|
left join mst_buying_group_one buying_group_one on buying_group_one.buying_group_one_code = a.buying_group_one_code and buying_group_one.tenant_id = a.tenant_id
|
756
|
left join mst_buying_group_two buying_group_two on buying_group_two.buying_group_two_code = a.buying_group_two_code and buying_group_two.tenant_id = a.tenant_id
|
757
|
left join mst_buying_group_three buying_group_three on buying_group_three.buying_group_three_code = a.buying_group_three_code and buying_group_three.tenant_id = a.tenant_id
|
758
|
|
759
|
|
760
|
WHERE EXISTS(SELECT 1 FROM customer st WHERE a.customer_code = st.customer_code)
|
761
|
AND isNull(a.error_message, '') = ''
|
762
|
AND a.process_id = @ProcessId
|
763
|
AND a.org_id = @OrganizationID
|
764
|
|
765
|
|
766
|
|
767
|
|
768
|
|
769
|
|
770
|
|
771
|
|
772
|
update ww set
|
773
|
ww.address1=IsNull(a.address1, ''),
|
774
|
ww.address2=IsNull(a.address2, ''),
|
775
|
ww.address3=IsNull(a.address3, ''),
|
776
|
ww.address4=IsNull(a.address4, ''),
|
777
|
ww.address5=IsNull(a.address5, ''),
|
778
|
ww.state_id=IsNull(s.state_id, ''),
|
779
|
ww.postcode=IsNull(a.postcode, ''),
|
780
|
ww.country_id=IsNull(cy.country_id, ''),
|
781
|
ww.status=IsNull(a.status, ''),
|
782
|
ww.tel_no=IsNull(a.tel_no, ''),
|
783
|
ww.additional_tel_no=IsNull(a.additional_tel_no, ''),
|
784
|
ww.updated_Date=IsNull(a.updated_date, ''),
|
785
|
ww.updated_by=IsNull(a.updated_by, ''),
|
786
|
ww.latitude=IsNull(a.latitude, ''),
|
787
|
ww.longitude=IsNull(a.longitude, ''),
|
788
|
ww.radius=IsNull(a.radius, ''),
|
789
|
ww.region_id= IsNull(r.region_id, '')
|
790
|
FROM mst_customer_contact_address ww
|
791
|
inner join customer wer on ww.customer_id=wer.customer_id
|
792
|
inner join
|
793
|
inner join state s on a.state_code=s.state_code and s.org_id = a.org_id
|
794
|
inner join country cy on a.country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
795
|
inner join mst_region r on r.region_code = a.region_code and r.org_id = a.org_id
|
796
|
WHERE EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE wer.customer_id = st.customer_id and st.address_type='Contact')
|
797
|
AND isNull(a.error_message, '') = '' and ww.address_type = 'Contact'
|
798
|
|
799
|
|
800
|
|
801
|
update ww set
|
802
|
ww.address1=Case When IsNull(a.billing_address1, '') = '' Then a.address1 ELSE a.billing_address1 END,
|
803
|
ww.address2=Case When IsNull(a.billing_address1, '') = '' Then a.address2 ELSE a.billing_address2 END,
|
804
|
ww.address3=Case When IsNull(a.billing_address1, '') = '' Then a.address3 ELSE a.billing_address3 END,
|
805
|
ww.address4=Case When IsNull(a.billing_address1, '') = '' Then a.address4 ELSE a.billing_address4 END,
|
806
|
ww.address5=Case When IsNull(a.billing_address1, '') = '' Then a.address5 ELSE a.billing_address5 END,
|
807
|
ww.state_id=IsNull(s.state_id, ''),
|
808
|
ww.postcode=IsNull(a.billing_postcode, ''),
|
809
|
ww.country_id=IsNull(cy.country_id, ''),
|
810
|
ww.status=IsNull(a.status, ''),
|
811
|
ww.tel_no=IsNull(a.tel_no, ''),
|
812
|
ww.additional_tel_no=IsNull(a.additional_tel_no, ''),
|
813
|
ww.updated_Date=IsNull(a.updated_date, ''),
|
814
|
ww.updated_by=IsNull(a.updated_by, ''),
|
815
|
ww.latitude=IsNull(a.billing_latitude, ''),
|
816
|
ww.longitude=IsNull(a.billing_longitude, ''),
|
817
|
ww.radius=IsNull(a.billing_radius, ''),
|
818
|
ww.region_id= IsNull(r.region_id, '')
|
819
|
FROM mst_customer_contact_address ww
|
820
|
inner join customer wer on ww.customer_id=wer.customer_id
|
821
|
inner join
|
822
|
inner join state s on a.billing_state_code=s.state_code and s.org_id = a.org_id
|
823
|
inner join country cy on a.billing_country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
824
|
inner join mst_region r on r.region_code = a.billing_region_code and r.org_id = a.org_id
|
825
|
WHERE EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE wer.customer_id = st.customer_id and st.address_type='Billing')
|
826
|
AND isNull(a.error_message, '') = '' and ww.address_type = 'Billing'
|
827
|
|
828
|
|
829
|
|
830
|
|
831
|
update ww set
|
832
|
ww.address1=Case When IsNull(a.shipping_address1, '') = '' Then a.address1 ELSE a.shipping_address1 END,
|
833
|
ww.address2=Case When IsNull(a.shipping_address1, '') = '' Then a.address2 ELSE a.shipping_address2 END,
|
834
|
ww.address3=Case When IsNull(a.shipping_address1, '') = '' Then a.address3 ELSE a.shipping_address3 END,
|
835
|
ww.address4=Case When IsNull(a.shipping_address1, '') = '' Then a.address4 ELSE a.shipping_address4 END,
|
836
|
ww.address5=Case When IsNull(a.shipping_address1, '') = '' Then a.address5 ELSE a.shipping_address5 END,
|
837
|
ww.state_id=IsNull(s.state_id, ''),
|
838
|
ww.postcode=IsNull(a.shipping_postcode, ''),
|
839
|
ww.country_id=IsNull(cy.country_id, ''),
|
840
|
ww.status=IsNull(a.status, ''),
|
841
|
ww.tel_no=IsNull(a.tel_no, ''),
|
842
|
ww.additional_tel_no=IsNull(a.additional_tel_no, ''),
|
843
|
ww.updated_Date=IsNull(a.updated_date, ''),
|
844
|
ww.updated_by=IsNull(a.updated_by, ''),
|
845
|
ww.latitude=IsNull(a.shipping_latitude, ''),
|
846
|
ww.longitude=IsNull(a.shipping_longitude, ''),
|
847
|
ww.radius=IsNull(a.shipping_radius, ''),
|
848
|
ww.region_id= IsNull(r.region_id, '')
|
849
|
FROM mst_customer_contact_address ww
|
850
|
inner join customer wer on ww.customer_id=wer.customer_id
|
851
|
inner join
|
852
|
inner join state s on a.shipping_state_code=s.state_code and s.org_id = a.org_id
|
853
|
inner join country cy on a.shipping_country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
854
|
inner join mst_region r on r.region_code = a.shipping_region_code and r.org_id = a.org_id
|
855
|
WHERE EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE wer.customer_id = st.customer_id and st.address_type='Shipping')
|
856
|
AND isNull(a.error_message, '') = '' and ww.address_type = 'Shipping'
|
857
|
|
858
|
|
859
|
|
860
|
|
861
|
|
862
|
|
863
|
|
864
|
|
865
|
update vv set
|
866
|
vv.branch_id=IsNull(b.branch_id, ''),
|
867
|
vv.status=IsNull(a.status, ''),
|
868
|
vv.updated_Date=IsNull(a.updated_date, ''),
|
869
|
vv.updated_by=IsNull(a.updated_by, '')
|
870
|
from mst_customer_branch vv
|
871
|
inner join customer xc on vv.customer_id=xc.customer_id
|
872
|
inner join
|
873
|
inner join mst_branch b on a.branch_code=b.branch_code and b.org_id = a.org_id
|
874
|
WHERE EXISTS(SELECT 1 FROM mst_customer_branch st WHERE xc.customer_id = st.customer_id)
|
875
|
AND isNull(a.error_message, '') = ''
|
876
|
|
877
|
|
878
|
|
879
|
|
880
|
|
881
|
|
882
|
|
883
|
|
884
|
|
885
|
|
886
|
|
887
|
update tur set
|
888
|
tur.user_id=IsNull(m.member_id, ''),
|
889
|
tur.updated_date=IsNull(a.updated_date, ''),
|
890
|
tur.updated_by=IsNull(a.updated_by, '')
|
891
|
from customer_to_user_relation tur
|
892
|
inner join customer dd on tur.customer_id=dd.customer_id
|
893
|
inner join
|
894
|
inner join member m on m.member_cd=a.person_in_charge and m.org_id = a.org_id
|
895
|
WHERE EXISTS(SELECT 1 FROM customer_to_user_relation st WHERE dd.customer_id = st.customer_id)
|
896
|
AND isNull(a.error_message, '') = ''
|
897
|
|
898
|
|
899
|
|
900
|
|
901
|
|
902
|
INSERT into customer
|
903
|
( customer_id, org_id,customer_code,customer_name,customer_short_name,customer_business_type,state_id,chain_id,channel_id,cluster_id,visit_frequency,status,created_date,created_by,updated_date,updated_by,country_id,area,distributor,geolocation,photo,address,contact_no,pic,latitude,longitude,file_name,file_directory,branch_id,current_balance,date_last_sales,date_last_pay,master_account,store_number,line_discount_code,special_instruction,outstanding_order_value,number_outstanding_order,person_in_charge,user_field,customer_on_hold,high_inv_days,credit_limit,terms_id,price_id,customer_class_id,invoice_discount_id,area_id,buying_group_id_one,buying_group_id_two,buying_group_id_three,salesperson_id,tenant_id,ams,ams_id, distributor_code, distributor_name, outlet_type_id, mt_gt, customer_type)
|
904
|
SELECT IsNull(a.customer_id, '') AS customer_id,
|
905
|
IsNull(a.org_id, '') AS OrgId,
|
906
|
IsNull(a.customer_code, '') AS CustomerCode,
|
907
|
IsNull(a.customer_name, '') AS CustomerName,
|
908
|
IsNull(a.customer_short_name, '') AS CustomerShortName,
|
909
|
IsNull('', '')AS CustomerBusinessType,
|
910
|
IsNull(s.state_id, '')As StateId,
|
911
|
IsNull(c.chain_id, '') AS ChainId,
|
912
|
IsNull(ch.channel_id, '') AS ChannelId,
|
913
|
IsNull(cl.cluster_id, '')AS ClusterId,0,
|
914
|
IsNull(a.status, '')As Status,
|
915
|
IsNull(a.created_date, '')AS CreatedDate,
|
916
|
IsNull(a.created_by, '')AS CreatedBy,
|
917
|
IsNull(a.updated_date, '')AS UpdatedDate,
|
918
|
IsNull(a.updated_by, '')AS UpdatedBy,
|
919
|
IsNull(cy.country_id, '')AS COuntryId,
|
920
|
IsNull(ma.area_description, '')AS AreaDescription,
|
921
|
IsNull('', '')AS Distributor,
|
922
|
IsNull('', '')AS Geolocation,
|
923
|
IsNull('', '')AS Photo,
|
924
|
IsNull(a.address1, '')AS Address1,
|
925
|
IsNull(a.tel_no, '')AS ContactNo,
|
926
|
IsNull(a.pic, '')AS Pic
|
927
|
,IsNull(a.latitude, '')AS Latitude,
|
928
|
IsNull(a.longitude, '')AS Longitude,
|
929
|
IsNull('', '')AS FileName,
|
930
|
IsNull('', '')AS FileDirectory,
|
931
|
IsNull(b.branch_id, '')AS BranchId,
|
932
|
0 AS CurrentBalance,
|
933
|
IsNull('', '') AS DateLastSales,
|
934
|
IsNull('', '') AS DateLastPay,
|
935
|
IsNull('', '')AS MasterAccount,
|
936
|
IsNull(a.store_number, '')AS StoreNumber,
|
937
|
IsNull('', '')AS LineDiscountCode,
|
938
|
IsNull(a.special_instruction, '')AS SpecialInstruction,
|
939
|
0 AS OutstandingOrderValue,
|
940
|
0 AS NumberOutStandingOrder,
|
941
|
IsNull(m.member_cd, '') AS PersonInCharge,
|
942
|
IsNull('', '') AS UserField,
|
943
|
IsNull(a.customer_on_hold, '') AS CustomerOnHold,
|
944
|
IsNull('', '') AS HighInvDays,
|
945
|
0 AS CreditLimit,
|
946
|
IsNull(ct.terms_id, '') AS TermId,
|
947
|
IsNull(pc.price_id, '') AS PriceId,
|
948
|
IsNull('', '') AS CustomerClassId,
|
949
|
IsNull(mid.invoice_discount_id, '') AS InvoiceDiscountId,
|
950
|
IsNull(ma.area_id, '') AS AreaId,
|
951
|
IsNull(buying_group_one.buying_group_id_one, @defaultBuyingGroup1Id) AS BuyingGroupIdOne,
|
952
|
IsNull(buying_group_two.buying_group_id_two, '') AS BuyingGroupIdTwo,
|
953
|
IsNull(buying_group_three.buying_group_id_three, '') AS BuyingGroupIdThree,
|
954
|
IsNull(m.member_id, '')AS SalespersonId,
|
955
|
IsNull(a.tenant_id, '') AS TenantId,
|
956
|
0 AS Ams,
|
957
|
IsNull('', '') As AmsId,
|
958
|
IsNull(a.distributor_code, '') AS DistributorCode,
|
959
|
IsNull(a.distributor_name, '') AS DistributorName,
|
960
|
IsNull(ot.outlet_type_id, '') AS OutletTypeId,
|
961
|
IsNull(a.mtgt, '') AS MtGt,
|
962
|
IsNull(a.customer_type, '') AS CustomerType
|
963
|
FROM
|
964
|
inner join org o on o.org_id = a.org_id
|
965
|
inner join state s on a.state_code=s.state_code and s.org_id = a.tenant_id
|
966
|
inner join chain c on a.chain_code=c.chain_code and c.org_id= a.org_id
|
967
|
inner join channel ch on a.channel_code=ch.channel_code and ch.org_id= a.org_id and ch.tenant_id = a.tenant_id
|
968
|
inner join cluster cl on a.cluster_code=cl.cluster_code and cl.channel_id = ch.channel_id and cl.tenant_id = a.tenant_id
|
969
|
inner join country cy on a.country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
970
|
inner join mst_branch b on a.branch_code=b.branch_code and b.org_id= a.org_id
|
971
|
inner join mst_area ma on a.area_code=ma.area_code and ma.org_id= a.org_id and b.branch_id = ma.branch_id
|
972
|
inner join mst_terms_code ct on a.terms_code=ct.terms_code and ct.tenant_id= a.tenant_id
|
973
|
inner join mst_price_code pc on a.price_code=pc.price_code and pc.tenant_id= a.tenant_id and pc.org_id = a.org_id
|
974
|
inner join mst_invoice_discount mid on a.invoice_discount_code= mid.invoice_discount_code and mid.tenant_id= a.tenant_id
|
975
|
inner join member m on a.person_in_charge =m.member_cd and m.org_id=a.org_id
|
976
|
left join mst_outlet_type ot on ot.outlet_type_code = a.outlet_type and ot.tenant_id = a.tenant_id
|
977
|
left join mst_buying_group_one buying_group_one on buying_group_one.buying_group_one_code = a.buying_group_one_code and buying_group_one.tenant_id = a.tenant_id
|
978
|
left join mst_buying_group_two buying_group_two on buying_group_two.buying_group_two_code = a.buying_group_two_code and buying_group_two.tenant_id = a.tenant_id
|
979
|
left join mst_buying_group_three buying_group_three on buying_group_three.buying_group_three_code = a.buying_group_three_code and buying_group_three.tenant_id = a.tenant_id
|
980
|
|
981
|
|
982
|
WHERE NOT EXISTS(SELECT 1 FROM customer st WHERE a.customer_code = st.customer_code)
|
983
|
AND isNull(a.error_message, '') = ''
|
984
|
AND a.process_id = @ProcessId
|
985
|
AND a.org_id = @OrganizationID
|
986
|
|
987
|
INSERT into mst_customer_contact_address ( customer_address_id,customer_id,address1,address2,address3,address4,address5,state_id,postcode,country_id,address_type,status,tel_no,additional_tel_no,created_date,created_by,updated_date,updated_by,org_id,tenant_id,latitude,longitude,radius,contact,sold_to_addr3_loc, region_id)
|
988
|
SELECT IsNull(a.customer_address_id, '') AS CustomerAddressId,
|
989
|
IsNull(a.customer_id, '') AS CustomerId,
|
990
|
IsNull(a.address1, '') AS Address1,
|
991
|
IsNull(a.address2, '') AS Address2,
|
992
|
IsNull(a.address3, '') AS Address3,
|
993
|
IsNull(a.address4, '') AS Address4,
|
994
|
IsNull(a.address5, '') AS Address5,
|
995
|
IsNull(s.state_id, '') AS StateId,
|
996
|
IsNull(a.postcode, '') AS PostCode,
|
997
|
IsNull(cy.country_id, '') AS CountryId,
|
998
|
'Contact' AS AddressType,
|
999
|
IsNull(a.status, '') AS Status,
|
1000
|
IsNull(a.tel_no, '') AS TelNo,
|
1001
|
IsNull(a.additional_tel_no, '') AS AdditionalTelNo,
|
1002
|
IsNull(a.created_date, '') AS CreatedDate,
|
1003
|
IsNull(a.created_by, '') AS CreatedBy,
|
1004
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1005
|
IsNull(a.updated_by, '') AS UpdatedBt,
|
1006
|
IsNull(a.org_id, '') AS OrgId,
|
1007
|
IsNull(a.tenant_id, '') AS TenantId,
|
1008
|
IsNull(a.latitude, '') AS Latitude,
|
1009
|
IsNull(a.longitude, '') AS Longitude,
|
1010
|
IsNull(a.radius, '') AS Radius,
|
1011
|
IsNull('', '') AS Contact,
|
1012
|
IsNull('', '') AS SoldToAddr3Loc,
|
1013
|
IsNull(r.region_id, '') AS region_id
|
1014
|
FROM
|
1015
|
inner join state s on a.state_code=s.state_code and s.org_id = a.org_id
|
1016
|
inner join country cy on a.country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1017
|
inner join mst_region r on r.region_code = a.region_code and r.org_id = a.org_id
|
1018
|
inner join customer werr on a.customer_code = werr.customer_code
|
1019
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE werr.customer_id = st.customer_id and st.address_type='Contact')
|
1020
|
AND isNull(a.error_message, '') = ''
|
1021
|
|
1022
|
|
1023
|
|
1024
|
INSERT into mst_customer_contact_address ( customer_address_id,customer_id,address1,address2,address3,address4,address5,state_id,postcode,country_id,address_type,status,tel_no,additional_tel_no,created_date,created_by,updated_date,updated_by,org_id,tenant_id,latitude,longitude,radius,contact,sold_to_addr3_loc, region_id)
|
1025
|
SELECT newid() AS CustomerAddressId,
|
1026
|
IsNull(wert.customer_id, '') AS CustomerId,
|
1027
|
Case When IsNull(a.billing_address1, '') = '' Then a.address1 ELSE a.billing_address1 END AS Address1,
|
1028
|
Case When IsNull(a.billing_address1, '') = '' Then a.address2 ELSE a.billing_address2 END AS Address2,
|
1029
|
Case When IsNull(a.billing_address1, '') = '' Then a.address3 ELSE a.billing_address3 END AS Address3,
|
1030
|
Case When IsNull(a.billing_address1, '') = '' Then a.address4 ELSE a.billing_address4 END AS Address4,
|
1031
|
Case When IsNull(a.billing_address1, '') = '' Then a.address5 ELSE a.billing_address5 END AS Address5,
|
1032
|
IsNull(s.state_id, '') AS StateId,
|
1033
|
IsNull(a.billing_postcode, '') AS PostCode,
|
1034
|
IsNull(cy.country_id, '') AS CountryId,
|
1035
|
'Billing' AS AddressType,
|
1036
|
IsNull(a.status, '') AS Status,
|
1037
|
IsNull(a.tel_no, '') AS TelNo,
|
1038
|
IsNull(a.additional_tel_no, '') AS AdditionalTelNo,
|
1039
|
IsNull(a.created_date, '') AS CreatedDate,
|
1040
|
IsNull(a.created_by, '') AS CreatedBy,
|
1041
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1042
|
IsNull(a.updated_by, '') AS UpdatedBt,
|
1043
|
IsNull(a.org_id, '') AS OrgId,
|
1044
|
IsNull(a.tenant_id, '') AS TenantId,
|
1045
|
IsNull(a.billing_latitude, '') AS Latitude,
|
1046
|
IsNull(a.billing_longitude, '') AS Longitude,
|
1047
|
IsNull(a.billing_radius, '') AS Radius,
|
1048
|
IsNull('', '') AS Contact,
|
1049
|
IsNull('', '') AS SoldToAddr3Loc,
|
1050
|
IsNull(r.region_id, '') AS region_id
|
1051
|
FROM
|
1052
|
inner join state s on a.billing_state_code=s.state_code and s.org_id = a.org_id
|
1053
|
inner join country cy on a.billing_country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1054
|
inner join mst_region r on r.region_code = a.billing_region_code and r.org_id = a.org_id
|
1055
|
inner join customer wert on a.customer_code=wert.customer_code
|
1056
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE wert.customer_id = st.customer_id and st.address_type='Billing')
|
1057
|
AND isNull(a.error_message, '') = ''
|
1058
|
|
1059
|
|
1060
|
|
1061
|
INSERT into mst_customer_contact_address ( customer_address_id,customer_id,address1,address2,address3,address4,address5,state_id,postcode,country_id,address_type,status,tel_no,additional_tel_no,created_date,created_by,updated_date,updated_by,org_id,tenant_id,latitude,longitude,radius,contact,sold_to_addr3_loc, region_id)
|
1062
|
SELECT newid() AS CustomerAddressId,
|
1063
|
IsNull(werw.customer_id, '') AS CustomerId,
|
1064
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address1 ELSE a.shipping_address1 END AS Address1,
|
1065
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address2 ELSE a.shipping_address2 END AS Address2,
|
1066
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address3 ELSE a.shipping_address3 END AS Address3,
|
1067
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address4 ELSE a.shipping_address4 END AS Address4,
|
1068
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address5 ELSE a.shipping_address5 END AS Address5,
|
1069
|
IsNull(s.state_id, '') AS StateId,
|
1070
|
IsNull(a.postcode, '') AS PostCode,
|
1071
|
IsNull(cy.country_id, '') AS CountryId,
|
1072
|
'Shipping' AS AddressType,
|
1073
|
IsNull(a.status, '') AS Status,
|
1074
|
IsNull(a.tel_no, '') AS TelNo,
|
1075
|
IsNull(a.additional_tel_no, '') AS AdditionalTelNo,
|
1076
|
IsNull(a.created_date, '') AS CreatedDate,
|
1077
|
IsNull(a.created_by, '') AS CreatedBy,
|
1078
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1079
|
IsNull(a.updated_by, '') AS UpdatedBt,
|
1080
|
IsNull(a.org_id, '') AS OrgId,
|
1081
|
IsNull(a.tenant_id, '') AS TenantId,
|
1082
|
IsNull(a.billing_latitude, '') AS Latitude,
|
1083
|
IsNull(a.billing_longitude, '') AS Longitude,
|
1084
|
IsNull(a.billing_radius, '') AS Radius,
|
1085
|
IsNull('', '') AS Contact,
|
1086
|
IsNull('', '') AS SoldToAddr3Loc,
|
1087
|
IsNull(r.region_id, '') AS region_id
|
1088
|
FROM
|
1089
|
inner join state s on a.billing_state_code=s.state_code and s.org_id = a.org_id
|
1090
|
inner join country cy on a.billing_country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1091
|
inner join mst_region r on r.region_code = a.shipping_region_code and r.org_id = a.org_id
|
1092
|
inner join customer werw on a.customer_code=werw.customer_code
|
1093
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE werw.customer_id = st.customer_id and st.address_type='Shipping')
|
1094
|
AND isNull(a.error_message, '') = ''
|
1095
|
|
1096
|
|
1097
|
|
1098
|
|
1099
|
INSERT into mst_customer_branch(customer_branch_id,customer_id,branch_id,status,created_date,created_by,updated_date,updated_by,org_id,tenant_id)
|
1100
|
SELECT IsNull(a.customer_branch_id, '') AS CustomerBranchId,
|
1101
|
IsNull(a.customer_id, '') AS CustomerId,
|
1102
|
IsNull(b.branch_id, '') AS BranchId,
|
1103
|
IsNull(a.status, '') AS Status,
|
1104
|
IsNull(a.created_date, '') AS CreatedDate,
|
1105
|
IsNull(a.created_by, '') AS CreatedBy,
|
1106
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1107
|
IsNull(a.updated_by, '') AS UpdatedBy,
|
1108
|
IsNull(a.org_id, '') AS OrgId,
|
1109
|
IsNull(a.tenant_id, '') AS TenantId
|
1110
|
FROM
|
1111
|
inner join mst_branch b on a.branch_code=b.branch_code and b.org_id = a.org_id
|
1112
|
inner join customer xc on a.customer_code=xc.customer_code
|
1113
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_branch st WHERE xc.customer_id = st.customer_id)
|
1114
|
AND isNull(a.error_message, '') = ''
|
1115
|
|
1116
|
|
1117
|
|
1118
|
INSERT into customer_to_user_relation(customer_to_user_relation_id,customer_id,user_id,created_date,created_by,updated_date,updated_by,org_id,tenant_id,start_date,end_date)
|
1119
|
SELECT IsNull(a.customer_to_user_relation_id, '') AS CustomerToUserRelationId,
|
1120
|
IsNull(a.customer_id, '') AS CustomerId,
|
1121
|
IsNull(m.member_id, '') AS user_id,
|
1122
|
IsNull(a.created_date, '') AS CreatedDate,
|
1123
|
IsNull(a.created_by, '') AS CreatedBy,
|
1124
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1125
|
IsNull(a.updated_by, '') AS UpdatedBy,
|
1126
|
IsNull(a.org_id, '') AS OrgId,
|
1127
|
IsNull(a.tenant_id, '') AS TenantId,
|
1128
|
'2023-01-01 00:00:00.000' as start_date,
|
1129
|
'2099-12-31 00:00:00.000' as end_date
|
1130
|
FROM
|
1131
|
inner join member m on m.member_cd=a.person_in_charge and m.org_id = a.org_id
|
1132
|
inner join customer aw on a.customer_code=aw.customer_code
|
1133
|
WHERE NOT EXISTS(SELECT 1 FROM customer_to_user_relation st WHERE aw.customer_id = st.customer_id)
|
1134
|
AND isNull(a.error_message, '') = ''
|
1135
|
|
1136
|
|
1137
|
|
1138
|
|
1139
|
END
|
1140
|
|
1141
|
|
1142
|
IF(SELECT auto_populate_customer_code from org where org_id = @OrganizationID) = 'Yes'
|
1143
|
BEGIN
|
1144
|
|
1145
|
|
1146
|
|
1147
|
|
1148
|
update cst set
|
1149
|
cst.customer_name=IsNull(a.customer_name, ''),
|
1150
|
cst.customer_Short_name=IsNull(a.customer_short_name, ''),
|
1151
|
cst.state_id=IsNull(s.state_id, ''),
|
1152
|
cst.chain_id=IsNull(c.chain_id, ''),
|
1153
|
cst.channel_id=IsNull(ch.channel_id, ''),
|
1154
|
cst.cluster_id=IsNull(cl.cluster_id, ''),
|
1155
|
cst.status=IsNull(a.status, ''),
|
1156
|
cst.updated_date=IsNull(a.updated_date, ''),
|
1157
|
cst.updated_by=IsNull(a.updated_by, ''),
|
1158
|
cst.country_id=IsNull(cy.country_id, ''),
|
1159
|
cst.area=IsNull(ma.area_description, ''),
|
1160
|
cst.address=IsNull(a.address1, ''),
|
1161
|
cst.contact_no=IsNull(a.tel_no, ''),
|
1162
|
cst.pic=IsNull(a.pic, ''),
|
1163
|
cst.latitude=IsNull(a.latitude, ''),
|
1164
|
cst.longitude=IsNull(a.longitude, ''),
|
1165
|
cst.branch_id=IsNull(b.branch_id, ''),
|
1166
|
cst.store_number=IsNull(a.store_number, ''),
|
1167
|
cst.special_instruction=IsNull(a.special_instruction, ''),
|
1168
|
cst.person_in_charge=IsNull(m.member_cd, ''),
|
1169
|
cst.customer_on_hold=IsNull(a.customer_on_hold, '') ,
|
1170
|
cst.terms_id=IsNull(ct.terms_id, '') ,
|
1171
|
cst.price_id=IsNull(pc.price_id, ''),
|
1172
|
cst.invoice_discount_id=IsNull(mid.invoice_discount_id, ''),
|
1173
|
cst.area_id=IsNull(ma.area_id, '') ,
|
1174
|
cst.salesperson_id=IsNull(m.member_id, ''),
|
1175
|
cst.distributor_code=IsNull(a.distributor_code, ''),
|
1176
|
cst.distributor_name=IsNull(a.distributor_name, ''),
|
1177
|
cst.outlet_type_id=IsNull(ot.outlet_type_id, ''),
|
1178
|
cst.buying_group_id_one=IsNull(buying_group_one.buying_group_id_one, @defaultBuyingGroup1Id),
|
1179
|
cst.buying_group_id_two=IsNull(buying_group_two.buying_group_id_two, ''),
|
1180
|
cst.buying_group_id_three=IsNull(buying_group_three.buying_group_id_three, ''),
|
1181
|
cst.mt_gt = IsNull(a.mtgt, ''),
|
1182
|
cst.customer_type = IsNull(a.customer_type, ''),
|
1183
|
cst.customer_class_id = IsNull(mcc.customer_class_id, '')
|
1184
|
FROM customer cst
|
1185
|
inner join
|
1186
|
inner join state s on a.state_code=s.state_code and s.org_id = a.org_id
|
1187
|
inner join chain c on a.chain_code=c.chain_code and c.tenant_id = a.tenant_id
|
1188
|
inner join channel ch on a.channel_code=ch.channel_code and ch.tenant_id = a.tenant_id
|
1189
|
inner join cluster cl on a.cluster_code=cl.cluster_code and cl.channel_id = ch.channel_id and cl.tenant_id = a.tenant_id
|
1190
|
inner join country cy on a.country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1191
|
inner join mst_branch b on a.branch_code=b.branch_code and b.org_id= a.org_id
|
1192
|
inner join mst_area ma on a.area_code=ma.area_code and ma.org_id= a.org_id and b.branch_id = ma.branch_id
|
1193
|
inner join mst_terms_code ct on a.terms_code=ct.terms_code and ct.tenant_id= a.tenant_id
|
1194
|
inner join mst_price_code pc on a.price_code=pc.price_code and pc.tenant_id= a.tenant_id and pc.org_id = a.org_id
|
1195
|
inner join mst_invoice_discount mid on a.invoice_discount_code= mid.invoice_discount_code and mid.tenant_id= a.tenant_id
|
1196
|
inner join member m on a.person_in_charge =m.member_cd and m.org_id=a.org_id
|
1197
|
left join mst_outlet_type ot on ot.outlet_type_code = a.outlet_type and ot.tenant_id = a.tenant_id
|
1198
|
left join mst_buying_group_one buying_group_one on buying_group_one.buying_group_one_code = a.buying_group_one_code and buying_group_one.tenant_id = a.tenant_id
|
1199
|
left join mst_buying_group_two buying_group_two on buying_group_two.buying_group_two_code = a.buying_group_two_code and buying_group_two.tenant_id = a.tenant_id
|
1200
|
left join mst_buying_group_three buying_group_three on buying_group_three.buying_group_three_code = a.buying_group_three_code and buying_group_three.tenant_id = a.tenant_id
|
1201
|
left join mst_customer_class mcc ON mcc.customer_class_code = a.customer_class_code AND mcc.tenant_id = a.tenant_id
|
1202
|
|
1203
|
|
1204
|
WHERE EXISTS(SELECT 1 FROM customer st WHERE a.customer_code = st.distributor_customer_code)
|
1205
|
AND isNull(a.error_message, '') = ''
|
1206
|
|
1207
|
|
1208
|
|
1209
|
|
1210
|
|
1211
|
|
1212
|
|
1213
|
|
1214
|
|
1215
|
|
1216
|
update ww set
|
1217
|
ww.address1=IsNull(a.address1, ''),
|
1218
|
ww.address2=IsNull(a.address2, ''),
|
1219
|
ww.address3=IsNull(a.address3, ''),
|
1220
|
ww.address4=IsNull(a.address4, ''),
|
1221
|
ww.address5=IsNull(a.address5, ''),
|
1222
|
ww.state_id=IsNull(s.state_id, ''),
|
1223
|
ww.postcode=IsNull(a.postcode, ''),
|
1224
|
ww.country_id=IsNull(cy.country_id, ''),
|
1225
|
ww.status=IsNull(a.status, ''),
|
1226
|
ww.tel_no=IsNull(a.tel_no, ''),
|
1227
|
ww.additional_tel_no=IsNull(a.additional_tel_no, ''),
|
1228
|
ww.updated_Date=IsNull(a.updated_date, ''),
|
1229
|
ww.updated_by=IsNull(a.updated_by, ''),
|
1230
|
ww.latitude=IsNull(a.latitude, ''),
|
1231
|
ww.longitude=IsNull(a.longitude, ''),
|
1232
|
ww.radius=IsNull(a.radius, ''),
|
1233
|
ww.region_id= IsNull(r.region_id, '')
|
1234
|
FROM mst_customer_contact_address ww
|
1235
|
inner join customer wer on ww.customer_id=wer.customer_id
|
1236
|
inner join
|
1237
|
inner join state s on a.state_code=s.state_code and s.org_id = a.org_id
|
1238
|
inner join country cy on a.country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1239
|
inner join mst_region r on r.region_code = a.region_code and r.org_id = a.org_id
|
1240
|
WHERE EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE wer.customer_id = st.customer_id and st.address_type='Contact')
|
1241
|
AND isNull(a.error_message, '') = '' and ww.address_type = 'Contact'
|
1242
|
|
1243
|
|
1244
|
|
1245
|
update ww set
|
1246
|
ww.address1=Case When IsNull(a.billing_address1, '') = '' Then a.address1 ELSE a.billing_address1 END,
|
1247
|
ww.address2=Case When IsNull(a.billing_address1, '') = '' Then a.address2 ELSE a.billing_address2 END,
|
1248
|
ww.address3=Case When IsNull(a.billing_address1, '') = '' Then a.address3 ELSE a.billing_address3 END,
|
1249
|
ww.address4=Case When IsNull(a.billing_address1, '') = '' Then a.address4 ELSE a.billing_address4 END,
|
1250
|
ww.address5=Case When IsNull(a.billing_address1, '') = '' Then a.address5 ELSE a.billing_address5 END,
|
1251
|
ww.state_id=IsNull(s.state_id, ''),
|
1252
|
ww.postcode=IsNull(a.billing_postcode, ''),
|
1253
|
ww.country_id=IsNull(cy.country_id, ''),
|
1254
|
ww.status=IsNull(a.status, ''),
|
1255
|
ww.tel_no=IsNull(a.tel_no, ''),
|
1256
|
ww.additional_tel_no=IsNull(a.additional_tel_no, ''),
|
1257
|
ww.updated_Date=IsNull(a.updated_date, ''),
|
1258
|
ww.updated_by=IsNull(a.updated_by, ''),
|
1259
|
ww.latitude=IsNull(a.billing_latitude, ''),
|
1260
|
ww.longitude=IsNull(a.billing_longitude, ''),
|
1261
|
ww.radius=IsNull(a.billing_radius, ''),
|
1262
|
ww.region_id= IsNull(r.region_id, '')
|
1263
|
FROM mst_customer_contact_address ww
|
1264
|
inner join customer wer on ww.customer_id=wer.customer_id
|
1265
|
inner join
|
1266
|
inner join state s on a.billing_state_code=s.state_code and s.org_id = a.org_id
|
1267
|
inner join country cy on a.billing_country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1268
|
inner join mst_region r on r.region_code = a.billing_region_code and r.org_id = a.org_id
|
1269
|
WHERE EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE wer.customer_id = st.customer_id and st.address_type='Billing')
|
1270
|
AND isNull(a.error_message, '') = '' and ww.address_type = 'Billing'
|
1271
|
|
1272
|
|
1273
|
|
1274
|
update ww set
|
1275
|
ww.address1=Case When IsNull(a.shipping_address1, '') = '' Then a.address1 ELSE a.shipping_address1 END,
|
1276
|
ww.address2=Case When IsNull(a.shipping_address1, '') = '' Then a.address2 ELSE a.shipping_address2 END,
|
1277
|
ww.address3=Case When IsNull(a.shipping_address1, '') = '' Then a.address3 ELSE a.shipping_address3 END,
|
1278
|
ww.address4=Case When IsNull(a.shipping_address1, '') = '' Then a.address4 ELSE a.shipping_address4 END,
|
1279
|
ww.address5=Case When IsNull(a.shipping_address1, '') = '' Then a.address5 ELSE a.shipping_address5 END,
|
1280
|
ww.state_id=IsNull(s.state_id, ''),
|
1281
|
ww.postcode=IsNull(a.shipping_postcode, ''),
|
1282
|
ww.country_id=IsNull(cy.country_id, ''),
|
1283
|
ww.status=IsNull(a.status, ''),
|
1284
|
ww.tel_no=IsNull(a.tel_no, ''),
|
1285
|
ww.additional_tel_no=IsNull(a.additional_tel_no, ''),
|
1286
|
ww.updated_Date=IsNull(a.updated_date, ''),
|
1287
|
ww.updated_by=IsNull(a.updated_by, ''),
|
1288
|
ww.latitude=IsNull(a.shipping_latitude, ''),
|
1289
|
ww.longitude=IsNull(a.shipping_longitude, ''),
|
1290
|
ww.radius=IsNull(a.shipping_radius, ''),
|
1291
|
ww.region_id= IsNull(r.region_id, '')
|
1292
|
FROM mst_customer_contact_address ww
|
1293
|
inner join customer wer on ww.customer_id=wer.customer_id
|
1294
|
inner join
|
1295
|
inner join state s on a.shipping_state_code=s.state_code and s.org_id = a.org_id
|
1296
|
inner join country cy on a.shipping_country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1297
|
inner join mst_region r on r.region_code = a.shipping_region_code and r.org_id = a.org_id
|
1298
|
WHERE EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE wer.customer_id = st.customer_id and st.address_type='Shipping')
|
1299
|
AND isNull(a.error_message, '') = '' and ww.address_type = 'Shipping'
|
1300
|
|
1301
|
|
1302
|
|
1303
|
|
1304
|
|
1305
|
|
1306
|
|
1307
|
update vv set
|
1308
|
vv.branch_id=IsNull(b.branch_id, ''),
|
1309
|
vv.status=IsNull(a.status, ''),
|
1310
|
vv.updated_Date=IsNull(a.updated_date, ''),
|
1311
|
vv.updated_by=IsNull(a.updated_by, '')
|
1312
|
from mst_customer_branch vv
|
1313
|
inner join customer xc on vv.customer_id=xc.customer_id
|
1314
|
inner join
|
1315
|
inner join mst_branch b on a.branch_code=b.branch_code and b.org_id = a.org_id
|
1316
|
WHERE EXISTS(SELECT 1 FROM mst_customer_branch st WHERE xc.customer_id = st.customer_id)
|
1317
|
AND isNull(a.error_message, '') = ''
|
1318
|
|
1319
|
|
1320
|
|
1321
|
|
1322
|
|
1323
|
|
1324
|
|
1325
|
|
1326
|
|
1327
|
|
1328
|
|
1329
|
update tur set
|
1330
|
tur.user_id=IsNull(m.member_id, ''),
|
1331
|
tur.updated_date=IsNull(a.updated_date, ''),
|
1332
|
tur.updated_by=IsNull(a.updated_by, '')
|
1333
|
from customer_to_user_relation tur
|
1334
|
inner join customer dd on tur.customer_id=dd.customer_id
|
1335
|
inner join
|
1336
|
inner join member m on m.member_cd=a.person_in_charge and m.org_id = a.org_id
|
1337
|
WHERE EXISTS(SELECT 1 FROM customer_to_user_relation st WHERE dd.customer_id = st.customer_id)
|
1338
|
AND isNull(a.error_message, '') = ''
|
1339
|
|
1340
|
|
1341
|
|
1342
|
|
1343
|
|
1344
|
INSERT into customer
|
1345
|
( customer_id, org_id,customer_code, distributor_customer_code, customer_name,customer_short_name,customer_business_type,state_id,chain_id,channel_id,cluster_id,visit_frequency,status,created_date,created_by,updated_date,updated_by,country_id,area,distributor,geolocation,photo,address,contact_no,pic,latitude,longitude,file_name,file_directory,branch_id,current_balance,date_last_sales,date_last_pay,master_account,store_number,line_discount_code,special_instruction,outstanding_order_value,number_outstanding_order,person_in_charge,user_field,customer_on_hold,high_inv_days,credit_limit,terms_id,price_id,invoice_discount_id,area_id,buying_group_id_one,buying_group_id_two,buying_group_id_three,salesperson_id,tenant_id,ams,ams_id, distributor_code, distributor_name, outlet_type_id, mt_gt, customer_type, customer_class_id)
|
1346
|
SELECT NEWID() AS customer_id,
|
1347
|
IsNull(a.org_id, '') AS OrgId,
|
1348
|
|
1349
|
IsNull(Replace(dbo.fn_get_configured_code(@OrganizationID, @TenantId, 'Customer', 'Code', IsNull(a.mtgt,'MT/GT'), DEFAULT), '[RUN]',Format(dbo.fn_get_configured_code(@OrganizationID, @TenantId, 'Customer', 'Running Number', IsNull(a.mtgt,'MT/GT'), DEFAULT)+ ROW_NUMBER() OVER (PARTITION BY a.mtgt ORDER BY a.customer_code), '000000')), '') [CustomerCode],
|
1350
|
IsNull(a.customer_code, '') AS CustomerCode,
|
1351
|
IsNull(a.customer_name, '') AS CustomerName,
|
1352
|
IsNull(a.customer_short_name, '') AS CustomerShortName,
|
1353
|
IsNull('', '')AS CustomerBusinessType,
|
1354
|
IsNull(s.state_id, '')As StateId,
|
1355
|
IsNull(c.chain_id, '') AS ChainId,
|
1356
|
IsNull(ch.channel_id, '') AS ChannelId,
|
1357
|
IsNull(cl.cluster_id, '')AS ClusterId,0,
|
1358
|
IsNull(a.status, '')As Status,
|
1359
|
IsNull(a.created_date, '')AS CreatedDate,
|
1360
|
IsNull(a.created_by, '')AS CreatedBy,
|
1361
|
IsNull(a.updated_date, '')AS UpdatedDate,
|
1362
|
IsNull(a.updated_by, '')AS UpdatedBy,
|
1363
|
IsNull(cy.country_id, '')AS CountryId,
|
1364
|
IsNull(ma.area_description, '')AS AreaDescription,
|
1365
|
IsNull('', '')AS Distributor,
|
1366
|
IsNull('', '')AS Geolocation,
|
1367
|
IsNull('', '')AS Photo,
|
1368
|
IsNull(a.address1, '')AS Address1,
|
1369
|
IsNull(a.tel_no, '')AS ContactNo,
|
1370
|
IsNull(a.pic, '')AS Pic
|
1371
|
,IsNull(a.latitude, '')AS Latitude,
|
1372
|
IsNull(a.longitude, '')AS Longitude,
|
1373
|
IsNull('', '')AS FileName,
|
1374
|
IsNull('', '')AS FileDirectory,
|
1375
|
IsNull(b.branch_id, '')AS BranchId,
|
1376
|
0 AS CurrentBalance,
|
1377
|
IsNull('', '') AS DateLastSales,
|
1378
|
IsNull('', '') AS DateLastPay,
|
1379
|
IsNull('', '')AS MasterAccount,
|
1380
|
IsNull(a.store_number, '')AS StoreNumber,
|
1381
|
IsNull('', '')AS LineDiscountCode,
|
1382
|
IsNull(a.special_instruction, '')AS SpecialInstruction,
|
1383
|
0 AS OutstandingOrderValue,
|
1384
|
0 AS NumberOutStandingOrder,
|
1385
|
IsNull(m.member_cd, '') AS PersonInCharge,
|
1386
|
IsNull('', '') AS UserField,
|
1387
|
IsNull(a.customer_on_hold, '') AS CustomerOnHold,
|
1388
|
IsNull('', '') AS HighInvDays,
|
1389
|
0 AS CreditLimit,
|
1390
|
IsNull(ct.terms_id, '') AS TermId,
|
1391
|
IsNull(pc.price_id, '') AS PriceId,
|
1392
|
IsNull(mid.invoice_discount_id, '') AS InvoiceDiscountId,
|
1393
|
IsNull(ma.area_id, '') AS AreaId,
|
1394
|
IsNull(buying_group_one.buying_group_id_one, @defaultBuyingGroup1Id) AS BuyingGroupIdOne,
|
1395
|
IsNull(buying_group_two.buying_group_id_two, '') AS BuyingGroupIdTwo,
|
1396
|
IsNull(buying_group_three.buying_group_id_three, '') AS BuyingGroupIdThree,
|
1397
|
IsNull(m.member_id, '')AS SalespersonId,
|
1398
|
IsNull(a.tenant_id, '') AS TenantId,
|
1399
|
0 AS Ams,
|
1400
|
IsNull('', '') As AmsId,
|
1401
|
IsNull(a.distributor_code, '') AS DistributorCode,
|
1402
|
IsNull(a.distributor_name, '') AS DistributorName,
|
1403
|
IsNull(ot.outlet_type_id, '') AS OutletTypeId,
|
1404
|
IsNull(a.mtgt, '') AS MtGt,
|
1405
|
IsNull(a.customer_type, '') AS CustomerType,
|
1406
|
IsNull(mcc.customer_class_id, '') AS CustomerClass
|
1407
|
FROM
|
1408
|
inner join org o on o.org_id = a.org_id
|
1409
|
inner join state s on a.state_code=s.state_code and s.org_id = a.org_id
|
1410
|
inner join chain c on a.chain_code=c.chain_code and c.tenant_id= a.tenant_id
|
1411
|
inner join channel ch on a.channel_code=ch.channel_code and ch.tenant_id = a.tenant_id
|
1412
|
inner join cluster cl on a.cluster_code=cl.cluster_code and cl.channel_id = ch.channel_id and cl.tenant_id = a.tenant_id
|
1413
|
inner join country cy on a.country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1414
|
inner join mst_branch b on a.branch_code=b.branch_code and b.org_id= a.org_id
|
1415
|
inner join mst_area ma on a.area_code=ma.area_code and ma.org_id= a.org_id and b.branch_id = ma.branch_id
|
1416
|
inner join mst_terms_code ct on a.terms_code=ct.terms_code and ct.tenant_id= a.tenant_id
|
1417
|
inner join mst_price_code pc on a.price_code=pc.price_code and pc.tenant_id= a.tenant_id and pc.org_id = a.org_id
|
1418
|
inner join mst_invoice_discount mid on a.invoice_discount_code= mid.invoice_discount_code and mid.tenant_id= a.tenant_id
|
1419
|
inner join member m on a.person_in_charge =m.member_cd and m.org_id=a.org_id
|
1420
|
left join mst_outlet_type ot on ot.outlet_type_code = a.outlet_type and ot.tenant_id = a.tenant_id
|
1421
|
left join mst_buying_group_one buying_group_one on buying_group_one.buying_group_one_code = a.buying_group_one_code and buying_group_one.tenant_id = a.tenant_id
|
1422
|
left join mst_buying_group_two buying_group_two on buying_group_two.buying_group_two_code = a.buying_group_two_code and buying_group_two.tenant_id = a.tenant_id
|
1423
|
left join mst_buying_group_three buying_group_three on buying_group_three.buying_group_three_code = a.buying_group_three_code and buying_group_three.tenant_id = a.tenant_id
|
1424
|
left join mst_customer_class mcc ON mcc.customer_class_code = a.customer_class_code AND mcc.tenant_id = a.tenant_id
|
1425
|
|
1426
|
|
1427
|
WHERE NOT EXISTS(SELECT 1 FROM customer st WHERE a.customer_code = st.distributor_customer_code and st.org_id = a.org_id)
|
1428
|
AND isNull(a.error_message, '') = ''
|
1429
|
|
1430
|
|
1431
|
|
1432
|
INSERT into mst_customer_contact_address ( customer_address_id,customer_id,address1,address2,address3,address4,address5,state_id,postcode,country_id,address_type,status,tel_no,additional_tel_no,created_date,created_by,updated_date,updated_by,org_id,tenant_id,latitude,longitude,radius,contact,sold_to_addr3_loc, region_id)
|
1433
|
SELECT NewId() AS CustomerAddressId,
|
1434
|
IsNull(werr.customer_id, '') AS CustomerId,
|
1435
|
IsNull(a.address1, '') AS Address1,
|
1436
|
IsNull(a.address2, '') AS Address2,
|
1437
|
IsNull(a.address3, '') AS Address3,
|
1438
|
IsNull(a.address4, '') AS Address4,
|
1439
|
IsNull(a.address5, '') AS Address5,
|
1440
|
IsNull(s.state_id, '') AS StateId,
|
1441
|
IsNull(a.postcode, '') AS PostCode,
|
1442
|
IsNull(cy.country_id, '') AS CountryId,
|
1443
|
'Contact' AS AddressType,
|
1444
|
IsNull(a.status, '') AS Status,
|
1445
|
IsNull(a.tel_no, '') AS TelNo,
|
1446
|
IsNull(a.additional_tel_no, '') AS AdditionalTelNo,
|
1447
|
IsNull(a.created_date, '') AS CreatedDate,
|
1448
|
IsNull(a.created_by, '') AS CreatedBy,
|
1449
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1450
|
IsNull(a.updated_by, '') AS UpdatedBt,
|
1451
|
IsNull(a.org_id, '') AS OrgId,
|
1452
|
IsNull(a.tenant_id, '') AS TenantId,
|
1453
|
IsNull(a.latitude, '') AS Latitude,
|
1454
|
IsNull(a.longitude, '') AS Longitude,
|
1455
|
IsNull(a.radius, '') AS Radius,
|
1456
|
IsNull('', '') AS Contact,
|
1457
|
IsNull('', '') AS SoldToAddr3Loc,
|
1458
|
IsNull(r.region_id, '') AS region_id
|
1459
|
FROM
|
1460
|
inner join state s on a.state_code=s.state_code and s.org_id = a.org_id
|
1461
|
inner join country cy on a.country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1462
|
inner join mst_region r on r.region_code = a.region_code and r.org_id = a.org_id
|
1463
|
inner join customer werr on a.customer_code = werr.distributor_customer_code
|
1464
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE werr.customer_id = st.customer_id and st.address_type='Contact')
|
1465
|
AND isNull(a.error_message, '') = ''
|
1466
|
|
1467
|
|
1468
|
|
1469
|
INSERT into mst_customer_contact_address ( customer_address_id,customer_id,address1,address2,address3,address4,address5,state_id,postcode,country_id,address_type,status,tel_no,additional_tel_no,created_date,created_by,updated_date,updated_by,org_id,tenant_id,latitude,longitude,radius,contact,sold_to_addr3_loc, region_id)
|
1470
|
SELECT Newid() AS CustomerAddressId,
|
1471
|
IsNull(werr.customer_id, '') AS CustomerId,
|
1472
|
Case When IsNull(a.billing_address1, '') = '' Then a.address1 ELSE a.billing_address1 END AS Address1,
|
1473
|
Case When IsNull(a.billing_address1, '') = '' Then a.address2 ELSE a.billing_address2 END AS Address2,
|
1474
|
Case When IsNull(a.billing_address1, '') = '' Then a.address3 ELSE a.billing_address3 END AS Address3,
|
1475
|
Case When IsNull(a.billing_address1, '') = '' Then a.address4 ELSE a.billing_address4 END AS Address4,
|
1476
|
Case When IsNull(a.billing_address1, '') = '' Then a.address5 ELSE a.billing_address5 END AS Address5,
|
1477
|
IsNull(s.state_id, '') AS StateId,
|
1478
|
IsNull(a.billing_postcode, '') AS PostCode,
|
1479
|
IsNull(cy.country_id, '') AS CountryId,
|
1480
|
'Billing' AS AddressType,
|
1481
|
IsNull(a.status, '') AS Status,
|
1482
|
IsNull(a.tel_no, '') AS TelNo,
|
1483
|
IsNull(a.additional_tel_no, '') AS AdditionalTelNo,
|
1484
|
IsNull(a.created_date, '') AS CreatedDate,
|
1485
|
IsNull(a.created_by, '') AS CreatedBy,
|
1486
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1487
|
IsNull(a.updated_by, '') AS UpdatedBt,
|
1488
|
IsNull(a.org_id, '') AS OrgId,
|
1489
|
IsNull(a.tenant_id, '') AS TenantId,
|
1490
|
IsNull(a.billing_latitude, '') AS Latitude,
|
1491
|
IsNull(a.billing_longitude, '') AS Longitude,
|
1492
|
IsNull(a.billing_radius, '') AS Radius,
|
1493
|
IsNull('', '') AS Contact,
|
1494
|
IsNull('', '') AS SoldToAddr3Loc,
|
1495
|
IsNull(r.region_id, '') AS region_id
|
1496
|
FROM
|
1497
|
inner join state s on a.billing_state_code=s.state_code and s.org_id = a.org_id
|
1498
|
inner join country cy on a.billing_country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1499
|
inner join mst_region r on r.region_code = a.billing_region_code and r.org_id = a.org_id
|
1500
|
inner join customer werr on a.customer_code = werr.distributor_customer_code
|
1501
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE werr.customer_id = st.customer_id and st.address_type='Billing')
|
1502
|
AND isNull(a.error_message, '') = ''
|
1503
|
|
1504
|
|
1505
|
|
1506
|
INSERT into mst_customer_contact_address ( customer_address_id,customer_id,address1,address2,address3,address4,address5,state_id,postcode,country_id,address_type,status,tel_no,additional_tel_no,created_date,created_by,updated_date,updated_by,org_id,tenant_id,latitude,longitude,radius,contact,sold_to_addr3_loc, region_id)
|
1507
|
SELECT newid() AS CustomerAddressId,
|
1508
|
IsNull(wert.customer_id, '') AS CustomerId,
|
1509
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address1 ELSE a.shipping_address1 END AS Address1,
|
1510
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address2 ELSE a.shipping_address2 END AS Address2,
|
1511
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address3 ELSE a.shipping_address3 END AS Address3,
|
1512
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address4 ELSE a.shipping_address4 END AS Address4,
|
1513
|
Case When IsNull(a.shipping_address1, '') = '' Then a.address5 ELSE a.shipping_address5 END AS Address5,
|
1514
|
IsNull(s.state_id, '') AS StateId,
|
1515
|
IsNull(a.shipping_postcode, '') AS PostCode,
|
1516
|
IsNull(cy.country_id, '') AS CountryId,
|
1517
|
'Shipping' AS AddressType,
|
1518
|
IsNull(a.status, '') AS Status,
|
1519
|
IsNull(a.tel_no, '') AS TelNo,
|
1520
|
IsNull(a.additional_tel_no, '') AS AdditionalTelNo,
|
1521
|
IsNull(a.created_date, '') AS CreatedDate,
|
1522
|
IsNull(a.created_by, '') AS CreatedBy,
|
1523
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1524
|
IsNull(a.updated_by, '') AS UpdatedBt,
|
1525
|
IsNull(a.org_id, '') AS OrgId,
|
1526
|
IsNull(a.tenant_id, '') AS TenantId,
|
1527
|
IsNull(a.shipping_latitude, '') AS Latitude,
|
1528
|
IsNull(a.shipping_longitude, '') AS Longitude,
|
1529
|
IsNull(a.shipping_radius, '') AS Radius,
|
1530
|
IsNull('', '') AS Contact,
|
1531
|
IsNull('', '') AS SoldToAddr3Loc,
|
1532
|
IsNull(r.region_id, '') AS region_id
|
1533
|
FROM
|
1534
|
inner join state s on a.shipping_state_code=s.state_code and s.org_id = a.org_id
|
1535
|
inner join country cy on a.shipping_country_code=cy.country_code and cy.tenant_id= a.tenant_id
|
1536
|
inner join mst_region r on r.region_code = a.shipping_region_code and r.org_id = a.org_id
|
1537
|
inner join customer wert on a.customer_code=wert.distributor_customer_code
|
1538
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_contact_address st WHERE wert.customer_id = st.customer_id and st.address_type='Shipping')
|
1539
|
AND isNull(a.error_message, '') = ''
|
1540
|
|
1541
|
|
1542
|
|
1543
|
INSERT into mst_customer_branch(customer_branch_id,customer_id,branch_id,status,created_date,created_by,updated_date,updated_by,org_id,tenant_id)
|
1544
|
SELECT IsNull(a.customer_branch_id, '') AS CustomerBranchId,
|
1545
|
IsNull(xc.customer_id, '') AS CustomerId,
|
1546
|
IsNull(b.branch_id, '') AS BranchId,
|
1547
|
IsNull(a.status, '') AS Status,
|
1548
|
IsNull(a.created_date, '') AS CreatedDate,
|
1549
|
IsNull(a.created_by, '') AS CreatedBy,
|
1550
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1551
|
IsNull(a.updated_by, '') AS UpdatedBy,
|
1552
|
IsNull(a.org_id, '') AS OrgId,
|
1553
|
IsNull(a.tenant_id, '') AS TenantId
|
1554
|
FROM
|
1555
|
inner join mst_branch b on a.branch_code=b.branch_code and b.org_id = a.org_id
|
1556
|
inner join customer xc on a.customer_code=xc.distributor_customer_code
|
1557
|
WHERE NOT EXISTS(SELECT 1 FROM mst_customer_branch st WHERE xc.customer_id = st.customer_id)
|
1558
|
AND isNull(a.error_message, '') = ''
|
1559
|
|
1560
|
|
1561
|
|
1562
|
INSERT into customer_to_user_relation(customer_to_user_relation_id,customer_id,user_id,created_date,created_by,updated_date,updated_by,org_id,tenant_id,start_date,end_date)
|
1563
|
SELECT IsNull(a.customer_to_user_relation_id, '') AS CustomerToUserRelationId,
|
1564
|
IsNull(aw.customer_id, '') AS CustomerId,
|
1565
|
IsNull(m.member_id, '') AS user_id,
|
1566
|
IsNull(a.created_date, '') AS CreatedDate,
|
1567
|
IsNull(a.created_by, '') AS CreatedBy,
|
1568
|
IsNull(a.updated_date, '') AS UpdatedDate,
|
1569
|
IsNull(a.updated_by, '') AS UpdatedBy,
|
1570
|
IsNull(a.org_id, '') AS OrgId,
|
1571
|
IsNull(a.tenant_id, '') AS TenantId,
|
1572
|
'2023-01-01 00:00:00.000' as start_date,
|
1573
|
'2099-12-31 00:00:00.000' as end_date
|
1574
|
FROM
|
1575
|
inner join member m on m.member_cd=a.person_in_charge and m.org_id = a.org_id
|
1576
|
inner join customer aw on a.customer_code=aw.distributor_customer_code
|
1577
|
WHERE NOT EXISTS(SELECT 1 FROM customer_to_user_relation st WHERE aw.customer_id = st.customer_id)
|
1578
|
AND isNull(a.error_message, '') = ''
|
1579
|
|
1580
|
|
1581
|
|
1582
|
|
1583
|
|
1584
|
Declare @MtCount int, @GtCount int;
|
1585
|
|
1586
|
SET @MtCount = (Select Count(1) from
|
1587
|
SET @GtCount = (Select Count(1) from
|
1588
|
|
1589
|
|
1590
|
SET @MtCount = (Select IsNull(@MtCount,0));
|
1591
|
SET @GtCount = (Select IsNull(@GtCount,0));
|
1592
|
|
1593
|
exec sp_update_configured_running_number @OrganizationID, @TenantId, 'MT', @MtCount;
|
1594
|
exec sp_update_configured_running_number @OrganizationID, @TenantId, 'GT', @GtCount;
|
1595
|
|
1596
|
|
1597
|
|
1598
|
|
1599
|
|
1600
|
|
1601
|
|
1602
|
|
1603
|
|
1604
|
|
1605
|
|
1606
|
|
1607
|
|
1608
|
END
|
1609
|
END
|
1610
|
END
|