Project

General

Profile

sp_DataLoad_di_Customer_import_20240609.sql

Muhammad Arif, 09/06/2024 01:10 PM

 
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
--Version 1 by Wei Joe - Master Data Auto Creation
8
--2024/02/26-RL: version 2: Changed di_customer_import to #tempCus for performance improvement
9
--2024/03/20-RL: version 3: Changed master data to insert/update based on tenant_id
10
*/
11

    
12
SET NOCOUNT ON;
13

    
14
        Select * into #tempCus from di_Customer_import where process_id = @ProcessId and org_id = @OrganizationID
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 #tempCus where IsNull(error_message,'') = '') > 0)
20
        BEGIN
21
                                        
22
                        ---------------------------------------------Update Insert Country------------------------------------
23
                        UPDATE country set country.country_code = x.country_code
24
                        FROM 
25
                        (
26
                        select country_code, tenant_id from 
27
                        #tempCus --where process_id = @ProcessId
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
                        --and di_Customer_import.process_id = @ProcessId 
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
                        #tempCus where IsNull(country_code,'') <> ''
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
                        --and x.process_id = @ProcessId 
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
                        #tempCus --where process_id = @ProcessId
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
                        --and di_Customer_import.process_id = @ProcessId 
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
                        #tempCus  where IsNull(billing_country_code,'') <> ''
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
                        --and x.process_id = @ProcessId 
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
                        #tempCus --where process_id = @ProcessId
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
                        --and di_Customer_import.process_id = @ProcessId 
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
                        #tempCus  where IsNull(shipping_country_code,'') <> ''
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
                        --and x.process_id = @ProcessId 
91
                        and x.tenant_id = @TenantId
92
                        
93

    
94
                        --------------------------------------------Update Insert Region---------------------------------------
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 #tempCus di
99
                        --where process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
112
                        --where di.process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
126
                        --where process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
139
                        --where di.process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
151
                        --where process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
164
                        --where di.process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
170
                        and x.org_id = @OrganizationID
171

    
172
                        
173
                        --------------------------------------------Update Insert State---------------------------------------
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 #tempCus di
178
                        --where process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
191
                        --where di.process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
205
                        --where process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
218
                        --where di.process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
231
                        --where process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
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 #tempCus di
244
                        --where di.process_id = @ProcessId
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
                        --and di.process_id = @ProcessId 
251
                        and x.org_id = @OrganizationID
252
                                        
253
                                        
254
                        ---------------------------------------------Update Insert Chain--------------------------------------
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 #tempCus di
259
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
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) --and chain.org_id=@OrganizationID)
275
                        --and x.process_id = @ProcessId 
276
                        and x.tenant_id =  @TenantId 
277
                        --arif 20240607 start
278
                        and x.org_id=@OrganizationID
279
                        --arif 20240607 end
280

    
281
                        
282
                        -------------------------------------------------Update Insert Channel (Channel maintain by org & tenant for Suntory) ------------------------------------
283
                        UPDATE channel SET channel_code = x.channel_code
284
                        FROM
285
                        (
286
                        select di.channel_code, di.org_id, di.tenant_id from #tempCus di
287
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
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
                        --and di.process_id = @ProcessId 
304
                        and x.org_id = @OrganizationID
305

    
306
                        
307
                        -----------------------------------------------------Update Insert Cluster------------------------------------
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 #tempCus di
312
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
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
                        --and di.process_id = @ProcessId 
331
                        and x.org_id = @OrganizationID
332
                        
333
                        
334
                        -------------------------------------------------Update Insert Mst Branch (Branch maintain at org level for Suntory) -----------------------------------------------
335
                        UPDATE mst_branch set branch_code = x.branch_code
336
                        FROM 
337
                        (
338
                        select di.branch_code, di.org_id from #tempCus di
339
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
351
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
356
                        and x.org_id = @OrganizationID
357

    
358
                        
359
                        ---------------------------------------------------Update Insert Mst Area-------------------------------------------------
360
                        UPDATE mst_area SET mst_area.area_code = x.area_code
361
                        FROM 
362
                        (
363
                        select di.area_code, di.org_id from #tempCus di
364
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
376
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
382
                        and x.org_id = @OrganizationID
383

    
384
                        
385
                        ---------------------------------------------Update Insert Mst Terms Code-------------------------------------------
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 #tempCus di
390
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
402
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
407
                        and x.tenant_id = @TenantId
408

    
409
                        
410
                        -----------------------------------------------Update Insert Mst Price Code (Maintain at org and tenant level) -------------------------------------------
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 #tempCus di
415
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
427
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
432
                        and x.org_id = @OrganizationID
433

    
434
                        
435
                        -----------------------------------------------Update Insert Mst Invoice Discount---------------------------------
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 #tempCus di
440
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
452
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
457
                        and x.tenant_id = @TenantId
458

    
459
                        
460
                        -----------------------------------------------Update Insert Buying Group One---------------------------------
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 #tempCus di
465
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
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
                        --and x.process_id = @ProcessId 
482
                        and x.tenant_id =  @TenantId
483

    
484

    
485
                        -----------------------------------------------Update Insert Buying Group Two---------------------------------
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 #tempCus di
490
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
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
                        --and x.process_id = @ProcessId 
507
                        and x.tenant_id =  @TenantId
508

    
509

    
510
                        -----------------------------------------------Update Insert Buying Group Three---------------------------------
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 #tempCus di
515
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
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
                        --and x.process_id = @ProcessId 
532
                        and x.tenant_id =  @TenantId
533

    
534
                        
535

    
536

    
537
                        ------------------------------------------------Update Insert member-----------------------------------------------
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 #tempCus di
543
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
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 #tempCus di
555
                        --where di.process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
560
                        and x.org_id = @OrganizationID
561

    
562

    
563

    
564
                        ------------------------------------------------Update Insert member branch-----------------------------------------------
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 #tempCus di
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 --di.process_id = @ProcessId and 
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
                        ----------------------------------------------Update Insert org_hierarchy ------------------------------------------------------
580
                        Declare @salesmanLevelParent varchar(50) = '', @groupCheck int = 0, @hierarchyId varchar(50) = '',  @salesmanLevel varchar(50) =''
581

    
582

    
583
                        --Select Top 1 @TenantId = tenant_id from #tempCus where process_id = @ProcessId
584
                        
585
                        -- Identify Level above salesman level
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
                        -- Check if have existing Salesman group
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
                        -- If 0 / >1 existing salesman group, create new. Else reuse existing.
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
                        -- == Insert into group members ==
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 #tempCus di
624
                        Left Join member m on m.member_cd = di.person_in_charge and m.org_id = di.org_id
625
                        where  --di.process_id = @ProcessId 
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
                        -- == Update Hierarchy Mapping ==
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
                        ------------------------------------------------Update Insert customer class-----------------------------------------------
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
                        #tempCus -- where process_id = @ProcessId
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
                        --and di_Customer_import.process_id = @ProcessId 
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
                        #tempCus --where process_id = @ProcessId
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
                        --and x.process_id = @ProcessId 
666
                        and x.org_id = @OrganizationID
667

    
668

    
669
                        ---------------------------------------------Update Insert Mst Outlet Type------------------------------------
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
                        #tempCus --where process_id = @ProcessId
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
                        --and di_Customer_import.process_id = @ProcessId 
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
                        #tempCus where IsNull(outlet_type,'') <> ''
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
                        --and x.process_id = @ProcessId 
691
                        and x.tenant_id =  @TenantId
692
                        
693
                        -------------------------------------------------------------------------------------------
694
                        SELECT c.customer_id AS 'customer_id',c.customer_code AS 'customer_code'Into #tempImportDelete from #tempCus di
695
                        inner join customer c on  c.customer_code=di.customer_code
696
                                --where di.org_id = @OrganizationID and di.process_id = @ProcessId
697

    
698
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
699

    
700
                        IF(SELECT auto_populate_customer_code from org where org_id = @OrganizationID) = 'No'
701
                        BEGIN
702
                                --Delete customer where exists (
703
                                --        SELECT 1 FROM #tempImportDelete temp where customer.customer_code = temp.customer_code and org_id = @OrganizationID)
704

    
705

    
706
                                update cst  set
707
                                cst.customer_name=IsNull(a.customer_name, ''),-- AS CustomerName,
708
                                cst.customer_Short_name=IsNull(a.customer_short_name, ''),-- AS CustomerShortName,
709
                                cst.state_id=IsNull(s.state_id, ''),--As StateId,
710
                                cst.chain_id=IsNull(c.chain_id, ''),-- AS ChainId,
711
                                cst.channel_id=IsNull(ch.channel_id, ''),-- AS ChannelId,
712
                                cst.cluster_id=IsNull(cl.cluster_id, ''),--AS ClusterId,0,
713
                                cst.status=IsNull(a.status, ''),--As Status,
714
                                cst.updated_date=IsNull(a.updated_date, ''),--AS UpdatedDate,
715
                                cst.updated_by=IsNull(a.updated_by, ''),--AS UpdatedBy,
716
                                cst.country_id=IsNull(cy.country_id, ''),--AS COuntryId,
717
                                cst.area=IsNull(ma.area_description, ''),--AS AreaDescription,
718
                                cst.address=IsNull(a.address1, ''),--AS Address1,
719
                                cst.contact_no=IsNull(a.tel_no, ''),--AS ContactNo,
720
                                cst.pic=IsNull(a.pic, ''),--AS Pic,
721
                                cst.latitude=IsNull(a.latitude, ''),--AS Latitude,
722
                                cst.longitude=IsNull(a.longitude, ''),--AS Longitude,
723
                                cst.branch_id=IsNull(b.branch_id, ''),--AS BranchId,
724
                                cst.store_number=IsNull(a.store_number, ''),--AS StoreNumber,
725
                                cst.special_instruction=IsNull(a.special_instruction, ''),--AS SpecialInstruction,
726
                                cst.person_in_charge=IsNull(m.member_cd, ''),-- AS PersonInCharge,
727
                                cst.customer_on_hold=IsNull(a.customer_on_hold, '') ,--AS CustomerOnHold,
728
                                cst.terms_id=IsNull(ct.terms_id, '') ,--AS TermId,
729
                                cst.price_id=IsNull(pc.price_id, ''),-- AS PriceId,
730
                                cst.invoice_discount_id=IsNull(mid.invoice_discount_id, ''),-- AS InvoiceDiscountId,
731
                                cst.area_id=IsNull(ma.area_id, '') ,--AS AreaId,
732
                                cst.salesperson_id=IsNull(m.member_id, ''),--AS SalespersonId
733
                                cst.distributor_code=IsNull(a.distributor_code, ''),--AS DistributorCode
734
                                cst.distributor_name=IsNull(a.distributor_name, ''),--AS DistributorName
735
                                cst.outlet_type_id=IsNull(ot.outlet_type_id, ''),--AS OutletTypeId
736
                                cst.buying_group_id_one=IsNull(buying_group_one.buying_group_id_one, @defaultBuyingGroup1Id),--AS Buying Group One
737
                                cst.buying_group_id_two=IsNull(buying_group_two.buying_group_id_two, ''),--AS Buying Group Two
738
                                cst.buying_group_id_three=IsNull(buying_group_three.buying_group_id_three, ''),--AS Buying Group Three
739
                                cst.mt_gt = IsNull(a.mtgt, ''), -- Mt/Gt
740
                                cst.customer_type = IsNull(a.customer_type, '') -- Mt/Gt
741
                                FROM customer cst
742
                                inner join #tempCus a  on cst.customer_Code=a.customer_code
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
                                --inner join customer_to_user_relation cu on m.member_id= cu.user_id and cu.org_id=a.org_id
759
                                --inner join mst_customer_class cc on a.id 
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
                                --DELETE mst_customer_contact_address where exists (
769
                                --        SELECT 1 FROM #tempImportDelete temp where mst_customer_contact_address.customer_id = temp.customer_id and org_id = @OrganizationID)
770

    
771

    
772
                                update ww set 
773
                                ww.address1=IsNull(a.address1, ''),-- AS Address1,
774
                                ww.address2=IsNull(a.address2, ''),-- AS Address2,
775
                                ww.address3=IsNull(a.address3, ''),-- AS Address3,
776
                                ww.address4=IsNull(a.address4, ''),-- AS Address4,
777
                                ww.address5=IsNull(a.address5, ''),-- AS Address5,
778
                                ww.state_id=IsNull(s.state_id, ''),-- AS StateId,
779
                                ww.postcode=IsNull(a.postcode, ''),-- AS PostCode,
780
                                ww.country_id=IsNull(cy.country_id, ''),-- AS CountryId,
781
                                ww.status=IsNull(a.status, ''),-- AS Status,
782
                                ww.tel_no=IsNull(a.tel_no, ''),-- AS TelNo,
783
                                ww.additional_tel_no=IsNull(a.additional_tel_no, ''),-- AS AdditionalTelNo,
784
                                ww.updated_Date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
785
                                ww.updated_by=IsNull(a.updated_by, ''),-- AS UpdatedBt,
786
                                ww.latitude=IsNull(a.latitude, ''),-- AS Latitude,
787
                                ww.longitude=IsNull(a.longitude, ''),-- AS Longitude,
788
                                ww.radius=IsNull(a.radius, ''),-- AS Radius,
789
                                ww.region_id= IsNull(r.region_id, '')-- AS region_id
790
                                FROM mst_customer_contact_address ww
791
                                inner join customer wer on ww.customer_id=wer.customer_id
792
                                inner join #tempCus a on a.customer_code = wer.customer_code
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
                                --AND a.process_id = @ProcessId 
799
                                --AND a.org_id = @OrganizationID
800
                                
801
                                update ww set 
802
                                ww.address1=Case When IsNull(a.billing_address1, '') = '' Then a.address1 ELSE a.billing_address1 END,-- AS Address1,
803
                                ww.address2=Case When IsNull(a.billing_address1, '') = '' Then a.address2 ELSE a.billing_address2 END,-- AS Address2,
804
                                ww.address3=Case When IsNull(a.billing_address1, '') = '' Then a.address3 ELSE a.billing_address3 END,-- AS Address3,
805
                                ww.address4=Case When IsNull(a.billing_address1, '') = '' Then a.address4 ELSE a.billing_address4 END,-- AS Address4,
806
                                ww.address5=Case When IsNull(a.billing_address1, '') = '' Then a.address5 ELSE a.billing_address5 END,-- AS Address5,
807
                                ww.state_id=IsNull(s.state_id, ''),-- AS StateId,
808
                                ww.postcode=IsNull(a.billing_postcode, ''),-- AS PostCode,
809
                                ww.country_id=IsNull(cy.country_id, ''),-- AS CountryId,
810
                                ww.status=IsNull(a.status, ''),-- AS Status,
811
                                ww.tel_no=IsNull(a.tel_no, ''),-- AS TelNo,
812
                                ww.additional_tel_no=IsNull(a.additional_tel_no, ''),-- AS AdditionalTelNo,
813
                                ww.updated_Date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
814
                                ww.updated_by=IsNull(a.updated_by, ''),-- AS UpdatedBt,
815
                                ww.latitude=IsNull(a.billing_latitude, ''),-- AS Latitude,
816
                                ww.longitude=IsNull(a.billing_longitude, ''),-- AS Longitude,
817
                                ww.radius=IsNull(a.billing_radius, ''),-- AS Radius,
818
                                ww.region_id= IsNull(r.region_id, '')-- AS region_id
819
                                FROM mst_customer_contact_address ww
820
                                inner join customer wer on ww.customer_id=wer.customer_id
821
                                inner join #tempCus a on a.customer_code = wer.customer_code
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
                                --AND a.process_id = @ProcessId 
828
                                --AND a.org_id = @OrganizationID
829
                                
830

    
831
                                update ww set 
832
                                ww.address1=Case When IsNull(a.shipping_address1, '') = '' Then a.address1 ELSE a.shipping_address1 END,-- AS Address1,
833
                                ww.address2=Case When IsNull(a.shipping_address1, '') = '' Then a.address2 ELSE a.shipping_address2 END,-- AS Address2,
834
                                ww.address3=Case When IsNull(a.shipping_address1, '') = '' Then a.address3 ELSE a.shipping_address3 END,-- AS Address3,
835
                                ww.address4=Case When IsNull(a.shipping_address1, '') = '' Then a.address4 ELSE a.shipping_address4 END,-- AS Address4,
836
                                ww.address5=Case When IsNull(a.shipping_address1, '') = '' Then a.address5 ELSE a.shipping_address5 END,-- AS Address5,
837
                                ww.state_id=IsNull(s.state_id, ''),-- AS StateId,
838
                                ww.postcode=IsNull(a.shipping_postcode, ''),-- AS PostCode,
839
                                ww.country_id=IsNull(cy.country_id, ''),-- AS CountryId,
840
                                ww.status=IsNull(a.status, ''),-- AS Status,
841
                                ww.tel_no=IsNull(a.tel_no, ''),-- AS TelNo,
842
                                ww.additional_tel_no=IsNull(a.additional_tel_no, ''),-- AS AdditionalTelNo,
843
                                ww.updated_Date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
844
                                ww.updated_by=IsNull(a.updated_by, ''),-- AS UpdatedBt,
845
                                ww.latitude=IsNull(a.shipping_latitude, ''),-- AS Latitude,
846
                                ww.longitude=IsNull(a.shipping_longitude, ''),-- AS Longitude,
847
                                ww.radius=IsNull(a.shipping_radius, ''),-- AS Radius,
848
                                ww.region_id= IsNull(r.region_id, '')-- AS region_id
849
                                FROM mst_customer_contact_address ww
850
                                inner join customer wer on ww.customer_id=wer.customer_id
851
                                inner join #tempCus a on a.customer_code = wer.customer_code
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
                                --AND a.process_id = @ProcessId 
858
                                --AND a.org_id = @OrganizationID
859
                                
860
                                
861
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
862
                                --DELETE mst_customer_branch where exists (
863
                                --        SELECT 1 FROM #tempImportDelete temp where mst_customer_branch.customer_id = temp.customer_id and org_id = @OrganizationID)
864

    
865
                                update vv set 
866
                                vv.branch_id=IsNull(b.branch_id, ''), --AS BranchId,
867
                                vv.status=IsNull(a.status, ''),-- AS Status,
868
                                vv.updated_Date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
869
                                vv.updated_by=IsNull(a.updated_by, '')-- AS UpdatedBy
870
                                from mst_customer_branch vv
871
                                inner join customer xc  on vv.customer_id=xc.customer_id
872
                                inner join  #tempCus a   on a.customer_code=xc.customer_code
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
                                --AND a.process_id = @ProcessId 
877
                                --AND a.org_id = @OrganizationID
878

    
879
                                
880
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
881

    
882
                                --DELETE customer_to_user_relation where exists (
883
                                --        SELECT 1 FROM #tempImportDelete temp where customer_to_user_relation.customer_id = temp.customer_id and org_id = @OrganizationID)
884
                                
885
                                --DROP table #tempImportDelete
886

    
887
                                update tur set
888
                                tur.user_id=IsNull(m.member_id, ''),-- AS user_id,
889
                                tur.updated_date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
890
                                tur.updated_by=IsNull(a.updated_by, '')-- AS UpdatedBy,
891
                                from customer_to_user_relation tur 
892
                                inner join customer dd on tur.customer_id=dd.customer_id
893
                                inner join #tempCus a  on a.customer_code=dd.customer_code
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
                                --AND a.process_id = @ProcessId 
898
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --inner join customer_to_user_relation cu on m.member_id= cu.user_id and cu.org_id=a.org_id
981
                                --inner join mst_customer_class cc on a.id 
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1022
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1059
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1096
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1116
                                --AND a.org_id = @OrganizationID
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 #tempCus a
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
                                --AND a.process_id = @ProcessId 
1136
                                --AND a.org_id = @OrganizationID
1137

    
1138
                                
1139
                        END
1140

    
1141
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1142
                        IF(SELECT auto_populate_customer_code from org where org_id = @OrganizationID) = 'Yes'
1143
                        BEGIN
1144
                                --Delete customer where exists (
1145
                                --        SELECT 1 FROM #tempImportDelete temp where customer.customer_code = temp.customer_code and org_id = @OrganizationID)
1146

    
1147

    
1148
                                update cst  set
1149
                                cst.customer_name=IsNull(a.customer_name, ''),-- AS CustomerName,
1150
                                cst.customer_Short_name=IsNull(a.customer_short_name, ''),-- AS CustomerShortName,
1151
                                cst.state_id=IsNull(s.state_id, ''),--As StateId,
1152
                                cst.chain_id=IsNull(c.chain_id, ''),-- AS ChainId,
1153
                                cst.channel_id=IsNull(ch.channel_id, ''),-- AS ChannelId,
1154
                                cst.cluster_id=IsNull(cl.cluster_id, ''),--AS ClusterId,0,
1155
                                cst.status=IsNull(a.status, ''),--As Status,
1156
                                cst.updated_date=IsNull(a.updated_date, ''),--AS UpdatedDate,
1157
                                cst.updated_by=IsNull(a.updated_by, ''),--AS UpdatedBy,
1158
                                cst.country_id=IsNull(cy.country_id, ''),--AS COuntryId,
1159
                                cst.area=IsNull(ma.area_description, ''),--AS AreaDescription,
1160
                                cst.address=IsNull(a.address1, ''),--AS Address1,
1161
                                cst.contact_no=IsNull(a.tel_no, ''),--AS ContactNo,
1162
                                cst.pic=IsNull(a.pic, ''),--AS Pic,
1163
                                cst.latitude=IsNull(a.latitude, ''),--AS Latitude,
1164
                                cst.longitude=IsNull(a.longitude, ''),--AS Longitude,
1165
                                cst.branch_id=IsNull(b.branch_id, ''),--AS BranchId,
1166
                                cst.store_number=IsNull(a.store_number, ''),--AS StoreNumber,
1167
                                cst.special_instruction=IsNull(a.special_instruction, ''),--AS SpecialInstruction,
1168
                                cst.person_in_charge=IsNull(m.member_cd, ''),-- AS PersonInCharge,
1169
                                cst.customer_on_hold=IsNull(a.customer_on_hold, '') ,--AS CustomerOnHold,
1170
                                cst.terms_id=IsNull(ct.terms_id, '') ,--AS TermId,
1171
                                cst.price_id=IsNull(pc.price_id, ''),-- AS PriceId,
1172
                                cst.invoice_discount_id=IsNull(mid.invoice_discount_id, ''),-- AS InvoiceDiscountId,
1173
                                cst.area_id=IsNull(ma.area_id, '') ,--AS AreaId,
1174
                                cst.salesperson_id=IsNull(m.member_id, ''),--AS SalespersonId
1175
                                cst.distributor_code=IsNull(a.distributor_code, ''),--AS DistributorCode
1176
                                cst.distributor_name=IsNull(a.distributor_name, ''),--AS DistributorName
1177
                                cst.outlet_type_id=IsNull(ot.outlet_type_id, ''),--AS OutletTypeId
1178
                                cst.buying_group_id_one=IsNull(buying_group_one.buying_group_id_one, @defaultBuyingGroup1Id),--AS Buying Group One
1179
                                cst.buying_group_id_two=IsNull(buying_group_two.buying_group_id_two, ''),--AS Buying Group Two
1180
                                cst.buying_group_id_three=IsNull(buying_group_three.buying_group_id_three, ''),--AS Buying Group Three
1181
                                cst.mt_gt = IsNull(a.mtgt, ''), -- Mt/Gt
1182
                                cst.customer_type = IsNull(a.customer_type, ''), -- Customer Type
1183
                                cst.customer_class_id = IsNull(mcc.customer_class_id, '') -- Customer Class
1184
                                FROM customer cst
1185
                                inner join #tempCus a  on cst.distributor_customer_code=a.customer_code
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
                                --inner join customer_to_user_relation cu on m.member_id= cu.user_id and cu.org_id=a.org_id
1203
                                --inner join mst_customer_class cc on a.id 
1204
                                WHERE  EXISTS(SELECT 1 FROM customer st WHERE a.customer_code = st.distributor_customer_code)
1205
                                AND isNull(a.error_message, '') = ''
1206
                                --AND a.process_id = @ProcessId 
1207
                                --AND a.org_id = @OrganizationID
1208
                                
1209
                                
1210
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1211

    
1212
                                --DELETE mst_customer_contact_address where exists (
1213
                                --        SELECT 1 FROM #tempImportDelete temp where mst_customer_contact_address.customer_id = temp.customer_id and org_id = @OrganizationID)
1214

    
1215

    
1216
                                update ww set 
1217
                                ww.address1=IsNull(a.address1, ''),-- AS Address1,
1218
                                ww.address2=IsNull(a.address2, ''),-- AS Address2,
1219
                                ww.address3=IsNull(a.address3, ''),-- AS Address3,
1220
                                ww.address4=IsNull(a.address4, ''),-- AS Address4,
1221
                                ww.address5=IsNull(a.address5, ''),-- AS Address5,
1222
                                ww.state_id=IsNull(s.state_id, ''),-- AS StateId,
1223
                                ww.postcode=IsNull(a.postcode, ''),-- AS PostCode,
1224
                                ww.country_id=IsNull(cy.country_id, ''),-- AS CountryId,
1225
                                ww.status=IsNull(a.status, ''),-- AS Status,
1226
                                ww.tel_no=IsNull(a.tel_no, ''),-- AS TelNo,
1227
                                ww.additional_tel_no=IsNull(a.additional_tel_no, ''),-- AS AdditionalTelNo,
1228
                                ww.updated_Date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
1229
                                ww.updated_by=IsNull(a.updated_by, ''),-- AS UpdatedBt,
1230
                                ww.latitude=IsNull(a.latitude, ''),-- AS Latitude,
1231
                                ww.longitude=IsNull(a.longitude, ''),-- AS Longitude,
1232
                                ww.radius=IsNull(a.radius, ''),-- AS Radius,
1233
                                ww.region_id= IsNull(r.region_id, '')-- AS region_id
1234
                                FROM mst_customer_contact_address ww
1235
                                inner join customer wer on ww.customer_id=wer.customer_id
1236
                                inner join #tempCus a on a.customer_code = wer.distributor_customer_code
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
                                --AND a.process_id = @ProcessId 
1243
                                --AND a.org_id = @OrganizationID
1244
                                
1245
                                update ww set 
1246
                                ww.address1=Case When IsNull(a.billing_address1, '') = '' Then a.address1 ELSE a.billing_address1 END,-- AS Address1,
1247
                                ww.address2=Case When IsNull(a.billing_address1, '') = '' Then a.address2 ELSE a.billing_address2 END,-- AS Address2,
1248
                                ww.address3=Case When IsNull(a.billing_address1, '') = '' Then a.address3 ELSE a.billing_address3 END,-- AS Address3,
1249
                                ww.address4=Case When IsNull(a.billing_address1, '') = '' Then a.address4 ELSE a.billing_address4 END,-- AS Address4,
1250
                                ww.address5=Case When IsNull(a.billing_address1, '') = '' Then a.address5 ELSE a.billing_address5 END,-- AS Address5,
1251
                                ww.state_id=IsNull(s.state_id, ''),-- AS StateId,
1252
                                ww.postcode=IsNull(a.billing_postcode, ''),-- AS PostCode,
1253
                                ww.country_id=IsNull(cy.country_id, ''),-- AS CountryId,
1254
                                ww.status=IsNull(a.status, ''),-- AS Status,
1255
                                ww.tel_no=IsNull(a.tel_no, ''),-- AS TelNo,
1256
                                ww.additional_tel_no=IsNull(a.additional_tel_no, ''),-- AS AdditionalTelNo,
1257
                                ww.updated_Date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
1258
                                ww.updated_by=IsNull(a.updated_by, ''),-- AS UpdatedBt,
1259
                                ww.latitude=IsNull(a.billing_latitude, ''),-- AS Latitude,
1260
                                ww.longitude=IsNull(a.billing_longitude, ''),-- AS Longitude,
1261
                                ww.radius=IsNull(a.billing_radius, ''),-- AS Radius,
1262
                                ww.region_id= IsNull(r.region_id, '')-- AS region_id
1263
                                FROM mst_customer_contact_address ww
1264
                                inner join customer wer on ww.customer_id=wer.customer_id
1265
                                inner join #tempCus a on a.customer_code = wer.distributor_customer_code
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
                                --AND a.process_id = @ProcessId 
1272
                                --AND a.org_id = @OrganizationID
1273
                                
1274
                                update ww set 
1275
                                ww.address1=Case When IsNull(a.shipping_address1, '') = '' Then a.address1 ELSE a.shipping_address1 END,-- AS Address1,
1276
                                ww.address2=Case When IsNull(a.shipping_address1, '') = '' Then a.address2 ELSE a.shipping_address2 END,-- AS Address2,
1277
                                ww.address3=Case When IsNull(a.shipping_address1, '') = '' Then a.address3 ELSE a.shipping_address3 END,-- AS Address3,
1278
                                ww.address4=Case When IsNull(a.shipping_address1, '') = '' Then a.address4 ELSE a.shipping_address4 END,-- AS Address4,
1279
                                ww.address5=Case When IsNull(a.shipping_address1, '') = '' Then a.address5 ELSE a.shipping_address5 END,-- AS Address5,
1280
                                ww.state_id=IsNull(s.state_id, ''),-- AS StateId,
1281
                                ww.postcode=IsNull(a.shipping_postcode, ''),-- AS PostCode,
1282
                                ww.country_id=IsNull(cy.country_id, ''),-- AS CountryId,
1283
                                ww.status=IsNull(a.status, ''),-- AS Status,
1284
                                ww.tel_no=IsNull(a.tel_no, ''),-- AS TelNo,
1285
                                ww.additional_tel_no=IsNull(a.additional_tel_no, ''),-- AS AdditionalTelNo,
1286
                                ww.updated_Date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
1287
                                ww.updated_by=IsNull(a.updated_by, ''),-- AS UpdatedBt,
1288
                                ww.latitude=IsNull(a.shipping_latitude, ''),-- AS Latitude,
1289
                                ww.longitude=IsNull(a.shipping_longitude, ''),-- AS Longitude,
1290
                                ww.radius=IsNull(a.shipping_radius, ''),-- AS Radius,
1291
                                ww.region_id= IsNull(r.region_id, '')-- AS region_id
1292
                                FROM mst_customer_contact_address ww
1293
                                inner join customer wer on ww.customer_id=wer.customer_id
1294
                                inner join #tempCus a on a.customer_code = wer.distributor_customer_code
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
                                --AND a.process_id = @ProcessId 
1301
                                --AND a.org_id = @OrganizationID
1302

    
1303
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1304
                                --DELETE mst_customer_branch where exists (
1305
                                --        SELECT 1 FROM #tempImportDelete temp where mst_customer_branch.customer_id = temp.customer_id and org_id = @OrganizationID)
1306

    
1307
                                update vv set 
1308
                                vv.branch_id=IsNull(b.branch_id, ''), --AS BranchId,
1309
                                vv.status=IsNull(a.status, ''),-- AS Status,
1310
                                vv.updated_Date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
1311
                                vv.updated_by=IsNull(a.updated_by, '')-- AS UpdatedBy
1312
                                from mst_customer_branch vv
1313
                                inner join customer xc  on vv.customer_id=xc.customer_id
1314
                                inner join  #tempCus a   on a.customer_code=xc.distributor_customer_code
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
                                --AND a.process_id = @ProcessId 
1319
                                --AND a.org_id = @OrganizationID
1320
                                
1321

    
1322
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1323

    
1324
                                --DELETE customer_to_user_relation where exists (
1325
                                --        SELECT 1 FROM #tempImportDelete temp where customer_to_user_relation.customer_id = temp.customer_id and org_id = @OrganizationID)
1326
                                
1327
                                --DROP table #tempImportDelete
1328

    
1329
                                update tur set
1330
                                tur.user_id=IsNull(m.member_id, ''),-- AS user_id,
1331
                                tur.updated_date=IsNull(a.updated_date, ''),-- AS UpdatedDate,
1332
                                tur.updated_by=IsNull(a.updated_by, '')-- AS UpdatedBy,
1333
                                from customer_to_user_relation tur 
1334
                                inner join customer dd on tur.customer_id=dd.customer_id
1335
                                inner join #tempCus a  on a.customer_code=dd.distributor_customer_code
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
                                --AND a.process_id = @ProcessId 
1340
                                --AND a.org_id = @OrganizationID
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
                                --IsNull(CONCAT(o.prefix, FORMAT(o.running_no + ROW_NUMBER() OVER (ORDER BY a.customer_code), '000000')), '') AS CustomerCode,
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 #tempCus a 
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
                                --inner join customer_to_user_relation cu on m.member_id= cu.user_id and cu.org_id=a.org_id
1426
                                --inner join mst_customer_class cc on a.id 
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
                                --AND a.process_id = @ProcessId 
1430
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1467
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1504
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1541
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1560
                                --AND a.org_id = @OrganizationID
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 #tempCus a 
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
                                --AND a.process_id = @ProcessId 
1580
                                --AND a.org_id = @OrganizationID
1581
                                
1582
                                --UPDATE org set running_no = (SELECT COUNT(1) FROM customer WHERE org_id = @OrganizationID) WHERE org_id = @OrganizationID
1583

    
1584
                                Declare @MtCount int, @GtCount int;
1585

    
1586
                                SET @MtCount = (Select Count(1) from #tempCus where org_id = @OrganizationID and process_id = @ProcessId and mtgt = 'MT' Group By mtgt) ;
1587
                                SET @GtCount = (Select Count(1) from #tempCus where org_id = @OrganizationID and process_id = @ProcessId and mtgt = 'GT' Group By mtgt) ;
1588
                                                                                                                           
1589
                                -- Set increment to 0 if NULL
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
                                --Update org set running_no = Case When x.mtgt = 'GT' Then running_no + x.count Else running_no End
1597
                                --from (Select Count(1) [count], mtgt from #tempCus where org_id = @OrganizationID and process_id = @ProcessId and mtgt = 'GT' Group By mtgt) x
1598
                                --Where org_id = @TenantId
1599

    
1600
                                --Update org set mt_running_no = Case When x.mtgt = 'MT' Then mt_running_no + x.count Else mt_running_no End
1601
                                --from (Select Count(1) [count], mtgt from #tempCus where org_id = @OrganizationID and process_id = @ProcessId and mtgt = 'MT' Group By mtgt) x
1602
                                --Where org_id = @TenantId
1603

    
1604

    
1605

    
1606

    
1607
                                
1608
                        END
1609
        END
1610
END