Проект

Общее

Профиль

Переносы и переплаты » Унивирсальный алгоритм обработки возрватов и переносов 12052022.sql

 
1
-- ??????? ?????
2
use kv2022
3

    
4
-- ??????? - ?????? ??????? ????, ??????? ??? ???????? ??????
5
-- ??????? - ??????, ??????? ????????? ? ???? ?? ??????? ???????????, ????????? ?? ?????????. ???????? ?????? ??????? ???? ? ???????? ?????? ????? ????????????? ???? ? ????? ?????????? ???????????
6
-- ??????? ????????? - ????? ????? ???? ?? ????????? ????????? ?? ????????? ??. 
7

    
8
declare 
9
@kod_adres bigint
10
, @corrected_kod_adres bigint
11
, @previous_data_gash smalldatetime
12
, @vid smallint
13
, @no_kv bigint
14
, @corrected_no_kv bigint
15
, @final_transaction bit
16
, @month tinyint
17

    
18
, @external_id uniqueidentifier
19
, @external_id_reserv uniqueidentifier
20
, @cMonth tinyint
21
, @cYear smallint
22
, @return varchar(100)
23
, @Error varchar(150)
24

    
25
Select @cMonth=month, @cYear=year From spr..uf_GetCurrentPeriod()
26

    
27
Declare tCursor Cursor for
28

    
29
Select  [kod_adres]
30
      ,[corrected_kod_adres]
31
      ,[no_kv]
32
      ,[corrected_no_kv]
33
      ,[month]
34
      ,[vid]
35
      ,[final_transaction]
36
      ,[previous_data_gash]
37
From sbrf_no_kv_refunds 
38
Where is_taken = 0 
39
--And previous_data_gash  is not null
40
--And Year(previous_data_gash)>=2022
41
--AND month=5
42
--And corrected_kod_adres<>25025005005
43
--
44
And no_kv not in (
45
290000105 -- ???????? ???, ??? ???? ????? ??? ???? ????, ?? ???? ??? ???? - ????????, ??? ??????? ??? ??????? ? post_t
46
,1170051906-- ? ????? ???? ??? ???????, ???? ??? ????
47
,1170048172 ) -- ? ????? ???? ??? ???????, ???? ??? ????
48

    
49
Open tCursor
50

    
51
Fetch Next From tCursor Into
52
@kod_adres
53
,@corrected_kod_adres
54
,@no_kv
55
,@corrected_no_kv
56
,@month
57
,@vid
58
,@final_transaction
59
,@previous_data_gash
60

    
61

    
62
While @@FETCH_STATUS = 0 
63
Begin
64
	Select @external_id = Null
65

    
66
	-- ???? ??? ?? 2022 ???, ?? ??????? ?? ?? ?????? ?????, ?.?. ????? ?? ??????????? ???? ????. ????? ??????? ????? ???.
67
	If Year(@previous_data_gash)<=2021 Goto Create_Body
68

    
69
	-- ?????? ????? ????????, ??? ?? ???? ??? ??????????????? ? ???, ??????? ???? ??? ???? ???????. 
70
	-- Step1 Begin>> 
71

    
72
	-- ????  *??????? ?????????*
73
	If (@previous_data_gash is Null)
74
	Begin
75
		-- ? ?????? ?????? ??? ??????? ??????????? ??????? ?? ??????????, ?.?. ?? ?? ???????? ???
76
		-- ????????????? ??????????? ??? ???????? ??????????
77

    
78
		-- ????? ??????? ??? ????
79
		-- 1 ?? ??????? ??????? ??????? ????? ??? ? payment_details ?? ???? ????? ?? ????, ??????? ???? ? post_t
80

    
81
		Select @external_id = NewId()
82

    
83
		Insert [172.16.1.150].fz54_details.dbo.link_storage
84
		Select @no_kv, @external_id, @kod_adres,null
85

    
86
		Insert [172.16.1.150].fz54_details.dbo.payment_details
87
		Select 
88
		@external_id external_id/*NEWID()*/
89
		,ctdtl.kod_adres
90
		,null
91
		,null
92
		,null
93
		,null
94
		,tPhone.phone phone
95
		,@cYear --ctdtl.year
96
		,ctdtl.mes
97
		,null date_operation/*???? ????????*/
98
		,prvds.id_provider id_company /*???????????-??????????? 1048*/
99
		,srvs.id_service as id_item
100
		,Round(Summa*-0.01,2) price-- ????????? ??????/??????
101
		,1 quantity-- ??????????
102
		,Round(Summa*-0.01,2) summa-- ?????
103
		,1 payment_method -- 1214 ??????? ??????? ??????? - ?????? ??????
104
		,4 payment_object -- ???-?? ?? ?????? ????? ??? 1212 
105
		-- ??? ??????????? ?????? ??? ??? ??????? ??????
106
		,IsNull(_agnt.vat,1) vat -- ?????? ??? -- ??? ????? ?????????-- Prokopenko 10/06 ??? ????,?????????? ??????????? ????? ???????? ???20 ? ???? ????????? ????
107
		, 5 as payments -- ??? ????? 5 - ???? ????? ?????? (????????? ??????????????)
108
		,Case When agnts.id_agent is not null Then 'another' Else null End agent_type -- ??? ??????
109
		,isnull(agnts.id_agent,1) supplier_info  -- ??? ??????????, ???? ???????? ??? ??????. ???? null, ?? ????????? ???
110
		,1 -- ???????
111
		,Null
112
		From 
113
		(Select *, @cMonth mes From post_t Where no_kv = @no_kv and kod_adres = @kod_adres
114
		Union all
115
		Select * From post_a Where no_kv = @no_kv and kod_adres = @kod_adres) ctdtl
116
		Left Join [172.16.1.150].fz54.dbo.vServices srvs On srvs.id_service=ctdtl.kod_uslugi
117
		Left Join [172.16.1.150].fz54.dbo.providers_to_kvar prvds on prvds.kod_postav=1--??? ?????? ???
118
		Left Join [172.16.1.150].fz54.dbo.agents_to_kvar agnts on agnts.kod_postav=ctdtl.kod_postav
119
		Left Join (
120
		Select * From 
121
		(
122
			Select Row_Number() over (PARTITION BY kod_adres Order by stel) as rank -- #ChangeContact08032022 
123
				,kod_adres
124
					,'+7'+Replace(Replace(Replace(stel,'(',''),')',''),'-','') phone
125
						From spr.dbo.contact 
126
							where stel like '(9%'
127
		) Tstel
128
		Where rank=1 
129
		) tPhone On ctdtl.kod_adres=tPhone.kod_adres
130
		-- ????? ??? ????????? ?????? ???
131
		Left join [172.16.1.150].fz54.dbo.agents _agnt On _agnt.id_agent=agnts.id_agent
132
		
133
		-- ?? ??????????? ?????????? ????? ??????? ???? ??????? ?? ??? #Stp2
134

    
135

    
136
	End
137

    
138
	-- ???????? ? ???????? ?????
139
	If (@previous_data_gash is Not Null) and (Year(@previous_data_gash)>=2022)
140
	Begin
141
		If exists (Select * From [172.16.1.150].fz54_details.dbo.link_storage Where no_kv = @no_kv And payer_id = @kod_adres)
142
			Begin
143
				Select 'cntr', @no_kv
144

    
145
				Select @external_id = NewId()
146
			-- ???? ??, ?? ?????????? ??? ? ????????, ??? ???? 
147
			-- Step 1.1 Begin>>
148
			
149
			Select @external_id_reserv = external_id
150
			From [172.16.1.150].fz54_details.dbo.link_storage
151
			Where no_kv = @no_kv 
152
			And refund_number is null
153
			And payer_id = @kod_adres
154

    
155
			--refunds
156

    
157
			-- ?.?. ?????? ?????????????? ?????????? ??????, ??? ??????????? ??????? ????? ?????? ? ? 
158
			Insert [172.16.1.150].fz54_details.dbo.refunds
159
			Select  
160
			   @external_id
161
			  ,[payer_id]
162
			  ,[timestamp]
163
			  ,[operation_type]
164
			  ,[operation_number]
165
			  ,[rollback_number]
166
			  ,[phone]
167
			  ,[year]
168
			  ,[month]
169
			  ,[date_operation]
170
			  ,[id_company]
171
			  ,[id_item]
172
			  ,[price]
173
			  ,[quantity]
174
			  ,[summa]
175
			  ,[payment_method] -- ??? ?????????, ??? ????
176
			  ,[payment_object]
177
			  ,[vat]
178
			  ,5 -- ??? ????????????? "???? ????? ??????" id = 5, name = 4
179
			  ,[agent_type]
180
			  ,[supplier_info]
181
			  ,1 is_refund
182
			  ,0 is_taken
183
			  ,6 ErrorCode
184
			  ,1 PayOnPrpay
185
			  ,1 KvOnPrpay
186
			From [172.16.1.150].fz54_details.dbo.payment_details
187
			Where external_id = @external_id_reserv
188
			
189
			--payment_details
190
			Insert [172.16.1.150].fz54_details.dbo.payment_details
191
			Select  
192
			   @external_id
193
			  ,[payer_id]
194
			  ,[timestamp]
195
			  ,[operation_type]
196
			  ,[operation_number]
197
			  ,[rollback_number]
198
			  ,[phone]
199
			  ,[year]
200
			  ,[month]
201
			  ,[date_operation]
202
			  ,[id_company]
203
			  ,[id_item]
204
			  ,[price]
205
			  ,[quantity]
206
			  ,[summa]
207
			  ,[payment_method] -- ??? ?????????, ??? ????
208
			  ,[payment_object]
209
			  ,[vat]
210
			  ,5 -- ??? ????????????? "???? ????? ??????" id = 5, name = 4
211
			  ,[agent_type]
212
			  ,[supplier_info]
213
			  ,1 is_refund
214
			  ,Null is_taken
215
			From [172.16.1.150].fz54_details.dbo.payment_details
216
			Where external_id = @external_id_reserv
217

    
218
			-- ????????? ?????? ?? ??????? ? ???????? ?????????
219
			Update [172.16.1.150].fz54_details.dbo.link_storage
220
			Set   refund_number = @external_id
221
			Where external_id = @external_id_reserv
222

    
223
			-- ??????????? ????????? ??????? ????? ????????? ????, ????? ????? ????? ???? ?? ??????? ??? ???????
224
			Insert [172.16.1.150].fz54_details.dbo.link_storage 
225
			Select @no_kv, @external_id, @kod_adres, null
226

    
227
			-- Step 1.1 End<<
228
			End
229
		Else
230
			Begin
231
			-- ???? ???, ?? ?????? ??? ? ????? ????, ??? ???????? ??? ???????? ?????? ????
232
			-- Step 1.2 Begin>>
233
			  If exists (Select * From [172.16.1.150].fz54_details.dbo.prepayment_details Where no_kv = @no_kv And payer_id = @kod_adres)
234
				  Begin
235
					Select 'ulus', @no_kv
236
					--prepayment_details
237
					-- ?? ??????? ????? ??? ? ????? external_id
238
					exec [172.16.1.150].fz54_details.dbo.usp_construct_prepayment @no_kv,5,1,0,'',@kod_adres
239

    
240
					-- ??????? ???????? ????? ???? ? ??????? ?????????. ???-?? ?? ???????? ?????? ??? ??? ? ????? ???????
241
					Insert [172.16.1.150].fz54_details.dbo.refunds
242
					Select *
243
					,6 ErrorCode
244
					,1 PayOnPrpay
245
					,1 KvOnPrpay
246
					From [172.16.1.150].fz54_details.dbo.payment_details
247
					Where external_id = (Select external_id From [172.16.1.150].fz54_details.dbo.link_storage Where no_kv=@no_kv And refund_number is null And payer_id = @kod_adres)
248

    
249
					-- ??????? ????????????? ????? ????
250
					Select @external_id = external_id From [172.16.1.150].fz54_details.dbo.link_storage Where no_kv=@no_kv And refund_number is null And payer_id = @kod_adres
251

    
252

    
253
				  End
254
			-- Step 1.2 End<<
255
			  Else
256
				-- ???? ? ????? ???, ?? ????????????? ??? ?? ??? ??????, ??????? ???? ? ??, ? ??????? ??????? "?????? ??????" ? ??????? ???????
257
				-- ????? ???????? ?????????? ????????? ?????? ??? ????? ????
258

    
259
				-- Step 1.3 Begin>>
260
				Begin
261
					Select '????? ?????!', @no_kv
262
						Close tCursor
263
							Deallocate tCursor
264
					Return
265
				End
266

    
267
			-- Step 1.3 End<<				
268
			End
269
	End
270

    
271

    
272
	-- Step1 End<< 
273

    
274

    
275
	-- ????? ????, ??? ??????? ???????????, ????? ??????? ??? ???????
276
	-- ??? ????? ???? ??? ????????, ???? ?????? ????? ????? ?? ??????, ?? ????? ???????????? ????????? usp_calculate_prepayment_cheking
277
	-- ???? ??????? usp_calculate_prepayment_cheking_current
278
	-- ?? ???? ??? ????????? ?????? ?? ??, ??? ? ??????? ????????? usp_calculate_prepayment, ?????? ???????? ? ????????? post_a(t)
279
	--#Stp2
280
	
281
	Create_Body:
282
	If @month=@cMonth
283
	Begin
284
		-- ??? 1. ????????? ?????? ????
285
		exec [172.16.1.150].fz54_details.dbo.usp_calculate_prepayment_cheking_current @month, @cYear, @corrected_no_kv,@return Output, @corrected_kod_adres
286
		Select '??????:'+@return
287

    
288
		-- ??? 2. ????????? ???
289
			If (Select PATINDEX('%?????%', @return))>0
290
			Begin
291
				exec [172.16.1.150].fz54_details.dbo.usp_construct_prepayment @corrected_no_kv, 5, 0, 0, @return Output, @corrected_kod_adres
292
					Select '????:'+@return
293
			End
294

    
295

    
296
		Select @Error=''
297

    
298
		exec usp_fz54_CheckOperation @no_kv, @corrected_no_kv, @kod_adres, @corrected_kod_adres, @previous_data_gash, @Error Output
299

    
300
		If @Error in ('001', '002', '003')
301
		Begin
302
			Update sbrf_no_kv_refunds Set is_taken = 1 Where no_kv=@no_kv And corrected_no_kv = @corrected_no_kv And is_taken = 0
303

    
304
			If @external_id is not Null
305
			Begin
306
				Update [172.16.1.150].fz54_details.dbo.payment_details
307
					Set is_taken = 0
308
						Where external_id = @external_id
309
			End
310

    
311
			-- ????????? ???????? is_taken ? ???????? 0
312
			Select @external_id = external_id
313
				From [172.16.1.150].fz54_details.dbo.link_storage
314
					Where no_kv = @corrected_no_kv And payer_id = @corrected_kod_adres and refund_number is null
315

    
316
			Update [172.16.1.150].fz54_details.dbo.payment_details
317
					Set is_taken = 0
318
						Where external_id = @external_id
319
        End
320
		Else
321
		Begin
322
			Select @Error
323
			-- rollback
324
			return
325
		End
326

    
327
	End
328
	Else
329
	Begin
330
		-- ??? 1. ????????? ?????? ????
331
		exec [172.16.1.150].fz54_details.dbo.usp_calculate_prepayment_cheking @month, @cYear, @corrected_no_kv, @return Output,@corrected_kod_adres
332
		Select '??????:'+@return
333
		-- ??? 2. ????????? ???
334
		If (Select PATINDEX('%?????%', @return))>0
335
		Begin
336
			exec [172.16.1.150].fz54_details.dbo.usp_construct_prepayment @corrected_no_kv, 5, 0, 0, @return Output, @corrected_kod_adres
337
				Select '????:'+@return
338
		End
339

    
340
		Select @Error=''
341

    
342
		exec usp_fz54_CheckOperation @no_kv, @corrected_no_kv, @kod_adres, @corrected_kod_adres, @previous_data_gash, @Error Output
343

    
344
		If @Error in ('001', '002', '003')
345
		Begin
346
			Update sbrf_no_kv_refunds Set is_taken = 1 Where no_kv=@no_kv And corrected_no_kv = @corrected_no_kv And is_taken = 0
347
			-- 
348
			If @external_id is not Null
349
			Begin
350
				Update [172.16.1.150].fz54_details.dbo.payment_details
351
					Set is_taken = 0
352
						Where external_id = @external_id
353
			End
354

    
355
			-- ????????? ???????? is_taken ? ???????? 0
356
			Select @external_id = external_id
357
				From [172.16.1.150].fz54_details.dbo.link_storage
358
					Where no_kv = @corrected_no_kv And payer_id = @corrected_kod_adres
359

    
360
			Update [172.16.1.150].fz54_details.dbo.payment_details
361
					Set is_taken = 0
362
						Where external_id = @external_id
363
        End
364
		Else
365
		Begin
366
			Select @Error
367
			-- rollback
368
			return
369
		End
370
	End
371

    
372

    
373

    
374
Fetch Next From tCursor Into
375
	@kod_adres
376
	,@corrected_kod_adres
377
	,@no_kv
378
	,@corrected_no_kv
379
	,@month
380
	,@vid
381
	,@final_transaction
382
	,@previous_data_gash
383
End
384

    
385

    
386
Close tCursor
387
Deallocate tCursor
388

    
389

    
390
--Select * From [172.16.1.150].fz54_details.dbo.link_storage Where no_kv in (1170008793, 1170043491)
391

    
392

    
393

    
394

    
395

    
396

    
397
-- ????? ?????? ??????? ?????????. ? ???? ?????? ? ??? ????? ?????? ????? ?????? ??????
398

    
(4-4/4)