1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
248.
249.
250.
251.
252.
253.
254.
255.
256.
257.
258.
259.
260.
261.
262.
263.
264.
265.
266.
267.
268.
269.
270.
271.
272.
273.
274.
275.
276.
277.
278.
279.
280.
281.
282.
283.
284.
285.
286.
287.
288.
289.
290.
291.
292.
293.
294.
295.
296.
297.
298.
299.
300.
301.
302.
303.
304.
305.
306.
307.
308.
309.
310.
311.
312.
313.
314.
315.
316.
317.
318.
319.
320.
321.
322.
323.
324.
325.
326.
327.
328.
329.
330.
331.
332.
333.
334.
335.
336.
337.
338.
339.
340.
341.
342.
343.
344.
345.
346.
347.
348.
349.
350.
351.
352.
353.
354.
355.
356.
357.
358.
359.
360.
361.
362.
363.
364.
365.
366.
367.
368.
369.
370.
371.
372.
373.
374.
375.
376.
377.
378.
379.
380.
381.
382.
383.
384.
385.
386.
387.
388.
389.
390.
391.
392.
393.
394.
395.
396.
397.
398.
399.
400.
401.
402.
403.
404.
405.
406.
407.
408.
409.
410.
411.
412.
413.
414.
415.
416.
417.
418.
419.
420.
421.
422.
423.
424.
425.
426.
427.
428.
429.
430.
431.
432.
433.
434.
435.
436.
437.
438.
439.
440.
441.
442.
443.
444.
445.
446.
447.
448.
449.
450.
451.
452.
453.
454.
455.
456.
457.
458.
459.
460.
461.
462.
463.
464.
465.
466.
467.
468.
469.
470.
471.
472.
473.
474.
475.
476.
477.
478.
479.
480.
481.
482.
483.
484.
485.
486.
487.
488.
489.
490.
491.
492.
493.
494.
495.
496.
497.
498.
499.
500.
501.
502.
503.
504.
505.
506.
507.
508.
509.
510.
511.
512.
513.
514.
515.
516.
517.
518.
519.
520.
521.
522.
523.
524.
525.
526.
527.
528.
529.
530.
531.
532.
533.
534.
535.
536.
537.
538.
539.
540.
541.
542.
543.
544.
545.
546.
547.
548.
549.
550.
551.
552.
553.
554.
555.
556.
557.
558.
559.
560.
561.
562.
563.
564.
565.
566.
567.
568.
569.
570.
571.
572.
573.
574.
575.
576.
577.
578.
579.
580.
581.
582.
583.
584.
585.
586.
587.
588.
589.
590.
591.
592.
593.
594.
595.
596.
597.
598.
599.
600.
601.
602.
603.
604.
605.
606.
607.
608.
609.
610.
611.
612.
613.
614.
615.
616.
617.
618.
619.
620.
621.
622.
623.
624.
625.
626.
627.
628.
629.
630.
631.
632.
633.
634.
635.
636.
637.
638.
639.
640.
641.
642.
643.
644.
645.
646.
647.
648.
649.
650.
651.
SELECT
deals.Period
,Contracts.ContractID
,deals.Dog
,deals.ComisType
,deals.ComisTypeText
,SUM(deals.Qty) SumQty
--deals.*
--,Contracts.ContractID
FROM (
--Form class: nil Query name:
--Base: workdb
select distinct/*1 as RecurrentRight,
(case
when substring(dt.NumberExt,5,1) = '1' then 'Карт2'
when substring(dt.NumberExt,5,1) = '3' then 'ПереносТр'
when substring(dt.NumberExt,5,1) = '5' then 'НевСумм'
when substring(dt.NumberExt,5,1) = '7' then 'КАкцепт'
when substring(dt.NumberExt,5,1) = '8' then 'Транзит'
when substring(dt.NumberExt,5,1) = '9' then 'M_Pay'
when substring(dt.NumberExt,5,1) = 'A' then 'НезРасч'
when substring(dt.NumberExt,5,1) = 'B' then 'КартРезерв'
when substring(dt.NumberExt,5,1) = 'C' then 'Внеб'
when substring(dt.NumberExt,5,1) = 'D' then 'СпецСчета'
when substring(dt.NumberExt,5,1) = 'E' then 'Приостанов'
when substring(dt.NumberExt,5,1) = 'F' then 'КАкцепт2'
when substring(dt.NumberExt,5,1) = 'Y' then 'КартИнПФЛ'
else ' '
end
) as Kartoteka,
(case
when substring(dt.NumberExt,9,1) = '1' then 'Карт2'
when substring(dt.NumberExt,9,1) = '3' then 'ПереносТр'
when substring(dt.NumberExt,9,1) = '5' then 'НевСумм'
when substring(dt.NumberExt,9,1) = '7' then 'КАкцепт'
when substring(dt.NumberExt,9,1) = '8' then 'Транзит'
when substring(dt.NumberExt,9,1) = '9' then 'M_Pay'
when substring(dt.NumberExt,9,1) = 'A' then 'НезРасч'
when substring(dt.NumberExt,9,1) = 'B' then 'КартРезерв'
when substring(dt.NumberExt,9,1) = 'C' then 'Внеб'
when substring(dt.NumberExt,9,1) = 'D' then 'СпецСчета'
when substring(dt.NumberExt,9,1) = 'E' then 'Приостанов'
when substring(dt.NumberExt,9,1) = 'F' then 'КАкцепт2'
when substring(dt.NumberExt,9,1) = 'Y' then 'КартИнПФЛ'
when substring(dt.NumberExt,9,1) = 'V' then 'КартИнПФЛ'
else ' '
end
) as KartDel,
' ' as ByTrip,
dt.Type,
dt.BalanceQty,*/
FORMAT(dt.Date,'yyyy.MM') Period,
dt.DealProtocolID as DealProtocolID,
dt.Date as OperDate,
dt.ResourceID as ResDebID,
dt.ResourcePsvID as ResCreID,
dt.DocNumber as Number,
dt.DealTransactID as DealTransactID,
/*dt.DealID,
dt.Direction,
case
when it.InterfaceObjectID = 66 then case
when isnull(piout.BankID,0) = isnull(piin.BankID,0) then ' '
else substring('РасходПриход', convert(integer,1+dt.Direction*6),6)
end
when it.InterfaceObjectID = 30 then substring('РасходПриход', convert(integer,1+dt.Direction*6),6)
else ' '
end as Direct,
piin.ResIntID0 as AccInID,
piin.AccClient as AccIn,
iin.BIC as BicIn,
piout.ResIntID0 as AccOutID,
piout.AccClient as AccOut,
iout.BIC as BicOut,
dt.ParentID,*/
dt.InstrumentID,
dt.Confirmed as Confirmed,
cast(dt.Qty as float) as Qty,
cast(dt.FixQty as float) as QtyDeb,
convert(char(25),r1.AccNumber) as Brief1,
convert(char(25),r2.AccNumber) as Brief2,
/*'' as Brief3,*/
dt.NumberExt,
dt.OpCode,
dt.Priority,
dt.FixCourse as Course,
dt.TermDate,
dt.DocDate,
dt.ValueDate,
dt.Nominal,
coalesce(o.Brief,oc.Brief) as ObjClsBrief,
b.Brief as Batch,
/*(case
when dt.DealID > 0 then 'из сделки'
else ' '
end
) as Deal,
(case
when exists (select 1
from tDocRelation dr WITH (NOLOCK index=XPKtDocRelation)
where dr.DealTransactID = dt.DealTransactID
and dr.RelType = 3
and dr.Type = 0
)
then 'реестр'
else ' '
end
) as Package,
case when (dt.NumberLow &8) /8 = 1 then 'РЦ' else ' ' end as RCField,*/
dt.FundID as FundDebID,
tR.InstOwnerID,
r1.Name as NameDeb,
r2.Name as NameCre,
dt.Comment as Comment,
--REPLACE(dt.Comment, '№ ', '') Comment2,
--left(dt.Comment, 10) as Comment2,
CASE
-- когда есть символ '№ '
WHEN dt.Comment like 'Комиссия за обслуживание по договору №%' THEN LEFT(SUBSTRING (dt.Comment, 38, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 38+2, 100)) - 1 + 2)
WHEN dt.Comment like 'Требование по уплате комиссии за обслуживание по договору №%' THEN LEFT(SUBSTRING (dt.Comment, 59, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 59+2, 100)) - 1 + 2)
WHEN dt.Comment like 'Комиссия за несоблюдение условий по договору №%' THEN LEFT(SUBSTRING (dt.Comment, 46, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 46+2, 100)) - 1 + 2)
WHEN dt.Comment like 'Требование по уплате комиссии за несоблюдение условий по договору №%' THEN LEFT(SUBSTRING (dt.Comment, 67, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 67+2, 100)) - 1 + 2)
WHEN dt.Comment like 'Комиссия по договору эквайринга №%' THEN LEFT(SUBSTRING (dt.Comment, 33+2, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 33+2, 100)) - 1 + 0)
WHEN dt.Comment like 'Комиссия за обслуживание по договору%' THEN LEFT(SUBSTRING (dt.Comment, 38, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 38, 100)) - 1)
WHEN dt.Comment like 'Требование по уплате комиссии за обслуживание по договору%' THEN LEFT(SUBSTRING (dt.Comment, 59, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 59, 100)) - 1)
WHEN dt.Comment like 'Комиссия за несоблюдение условий по договору%' THEN LEFT(SUBSTRING (dt.Comment, 46, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 46, 100)) - 1)
WHEN dt.Comment like 'Требование по уплате комиссии за несоблюдение условий по договору%' THEN LEFT(SUBSTRING (dt.Comment, 67, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 67, 100)) - 1)
WHEN dt.Comment like 'Комиссия по договору эквайринга%' THEN LEFT(SUBSTRING (dt.Comment, 33, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 33, 100)) - 1)
ELSE 'Прочие проводки'
END as Dog,
CASE
WHEN dt.Comment like 'Комиссия за обслуживание по договору%' THEN 1 --фиксированный абонентский платеж
WHEN dt.Comment like 'Требование по уплате комиссии за обслуживание по договору%' THEN 1 --фиксированный абонентский платеж
WHEN dt.Comment like 'Комиссия за несоблюдение условий по договору%' THEN 2 --платеж за несоблюдение условий
WHEN dt.Comment like 'Требование по уплате комиссии за несоблюдение условий по договору%' THEN 2 --платеж за несоблюдение условий
WHEN dt.Comment like 'Комиссия по договору эквайринга%' THEN 3 --платеж за комиссию с оборота
ELSE 0
END as ComisType,
CASE
WHEN dt.Comment like 'Комиссия за обслуживание по договору%' THEN 'За обслуживание' --фиксированный абонентский платеж
WHEN dt.Comment like 'Требование по уплате комиссии за обслуживание по договору%' THEN 'За обслуживание' --фиксированный абонентский платеж
WHEN dt.Comment like 'Комиссия за несоблюдение условий по договору%' THEN 'За несоблюдение условий' --платеж за несоблюдение условий
WHEN dt.Comment like 'Требование по уплате комиссии за несоблюдение условий по договору%' THEN 'За несоблюдение условий' --платеж за несоблюдение условий
WHEN dt.Comment like 'Комиссия по договору эквайринга%' THEN 'За оборот' --платеж за комиссию с оборота
ELSE 'Прочие проводки'
END as ComisTypeText
/*dt.InitiatorID as InitiatorID,
dt.FundPsvID as FundCreID,
it.Brief as InstrumentBrief,
substring((r1.AccNumber + '00000000'),6,3) as DebFundShortName,
substring((r2.AccNumber + '00000000'),6,3) as CreFundShortName,
case
when exists(select 1
from tFoldingDocument fd WITH (NOLOCK index=XPKtFoldingDocument)
where fd.DealTransactID = dt.DealTransactID) then 'Свод'
when exists(select 1
from tFoldingDocumentRelation fdr WITH (NOLOCK index=XPKtFoldingDocumentRelation)
where fdr.DocumentSourceID = dt.DealTransactID) then 'ВклСв'
else ' '
end as FoldingDoc,
0 as RelationType,
case
when (isnull(de.Flags, 0) & 1) = 1 then 'ИЗМ'
else ''
end as Properties ,
isnull(de.flags,0) as UserFlag,
isnull(de.OnlineControlFlags,0) as OnlineControlFlags,
isnull(sd.EDDocSignStatusID,0) as StatusID,
case
when sd.EDDocSignStatusID = 10 then 'С'
when sd.EDDocSignStatusID = 20 then 'П'
when sd.EDDocSignStatusID = 30 then 'Г'
when sd.EDDocSignStatusID = 40 then 'В'
when sd.EDDocSignStatusID = 50 then 'ОП'
when sd.EDDocSignStatusID = 60 then 'ОВ'
when sd.EDDocSignStatusID = 70 then 'Б'
else ''
end as StatusType,
case when isnull(tdfpfst.OPKC,'')='' then '' else 'СБП' end as DPF,
case when Well_grnded.DocValue = 1 then 'Да' else '' end as Well_grounded,
isnull(pdpu.Flag,0) as PUFlag,
isnull(pdSDO.Flag,0) as SDOFlag,
_tTransition.TargetStateID as StateID,
dt.NumberLow as NumberLow*/
from
tDealTransact dt WITH (NOLOCK index=XIE3tDealTransact)
inner join tRKO_FinOper_Sync it WITH (NOLOCK index=XPKtRKO_FinOper_Sync)
on it.FinOperID = dt.InstrumentID
inner join tRKO_ExpProperty_Sync b WITH (NOLOCK index=XPKtRKO_ExpProperty_Sync)
on b.ExpPropertyID = dt.BatchID
and b.ExpPropertyID between 5000024 and 10000004671
inner join tRKO_UserRight_Sync us WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
on us.UserID = 1
and us.RightType = 2
and us.ObjectID = b.ExpPropertyID
inner join tRKO_UserRight_Sync ufo WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
on ufo.UserID = 1
and ufo.RightType = 4
and ufo.ObjectID = dt.InstrumentID
inner join tRKO_Account_Sync r1 WITH (NOLOCK index=XPKtRKO_Account_Sync)
on r1.AccountID = dt.ResourceID
and r1.BalanceID = 2140
--and r1.AccNumber LIKE '40__________________'
inner join tRKO_Account_Sync r2 WITH (NOLOCK index=XPKtRKO_Account_Sync)
on r2.AccountID = dt.ResourcePsvID
and r2.AccNumber LIKE '70601_______________'
--and (dt.InstitutionID = 2000 or r1.DepartmentID = 2000 or r2.DepartmentID = 2000)
inner join workdb.dbo.tObject _tObject WITH (NOLOCK index=XAK2tObject)
on _tObject.InstrumentID = dt.InstrumentID
and _tObject.ID = dt.DealtransactID
inner join workdb.dbo.tInstrument _tInstrument WITH (NOLOCK index=XPKtInstrument)
on _tInstrument.InstrumentID = _tObject.InstrumentID
inner join workdb.dbo.tProtocol _tProtocol WITH (NOLOCK index=XPKtProtocol)
on _tProtocol.ProtocolID = _tObject.CurrProtocolID
inner join workdb.dbo.tTransition _tTransition WITH (NOLOCK index=XPKtTransition)
on _tTransition.TransitionID = _tProtocol.TransitionID
left join workdb.dbo.tNode _tNode WITH (NOLOCK index=XPKtNode)
on _tNode.NodeID = _tTransition.TargetStateID
inner join tRKO_UserRight_Sync ub WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
on ub.UserID = 1
and ub.RightType = 14
and ub.ObjectID = _tTransition.TargetStateID
left join tRKO_ExpProperty_Sync o WITH (NOLOCK index=XPKtRKO_ExpProperty_Sync)
on o.ExpPropertyID = dt.PaymentType
and o.ExpPropertyType = 25
left join tObjClassifier oc WITH (NOLOCK index=XPKtDepClassifier)
on oc.ObjClassifierID = dt.PaymentType
left join tPrimaryDocumentAttr de WITH (NOLOCK index=XPKtPrimaryDocumentAttr)
on de.DealTransactID = dt.DealTransactID
left join tSignDoc sd WITH (NOLOCK index=XPKtSignDoc)
on sd.SignDocID = dt.DealTransactID
left join tDocumentPlatFast tdfpfst WITH (NOLOCK index=XPKtDocumentPlatFast)
on tdfpfst.DealTransactID = dt.DealTransactID
left join tDocRelation Well_grnded WITH (NOLOCK index=XPKtDocRelation)
on Well_grnded.DealTransactID = dt.DealTransactID
and Well_grnded.RelType = 51 --DOC_REL_REVAL_BASIS
/*left join tPayInstruct piin WITH (NOLOCK index=XIE2tPayInstruct)
on piin.DealTransactID = dt.DealTransactID
and piin.Type = 2
and piin.Direct = dt.Direction
and piin.Belong = dt.Direction*/
/*left join tRKO_Participant_Sync iin WITH (NOLOCK index=XPKtRKO_Participant_Sync)
on iin.ParticipantID = piin.BankID*/
/*left join tPayInstruct piout WITH (NOLOCK index=XIE2tPayInstruct)
on piout.DealTransactID = dt.DealTransactID
and piout.Type = 2
and piout.Direct = dt.Direction
and piout.Belong = 1 - dt.Direction*/
/*left join tRKO_Participant_Sync iout WITH (NOLOCK index=XPKtRKO_Participant_Sync)
on iout.ParticipantID = piout.BankID*/
left join tPrimaryDocPublicUtility pdpu WITH (NOLOCK index=XPKtPrimaryDocPublicUtility)
on pdpu.DealTransactID = dt.DealTransactID
left join tPrmDStateDefenseOrder pdSDO WITH (NOLOCK index=XPKtPrmDStateDefenseOrder)
on pdSDO.PrimaryDocumentID = dt.DealTransactID
left join tResource tR on tR.Brief = convert(char(25),r1.AccNumber)
where 1=1
and it.DsModuleID = 21
and dt.TransactType = 5
--and dt.Date = '20200430'
-- если необходим отчет за интервал между датами
and dt.Date >= DATEADD(d, 1, (EOMONTH (GETDATE(),-2))) --'20200501'
and dt.Date < DATEADD(d, 1, (EOMONTH (GETDATE(),-1))) --'20200601' -- +1 день
and (exists (select 1
from tRKO_UserRight_Sync ubr1 WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
where ubr1.UserID = 1
and ubr1.RightType = 19 --USER_RES_RESOURCE_PROFILE
and ubr1.ObjectID = r1.AccProfileID)
or
exists (select 1
from tRKO_UserRight_Sync ubr2 WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
where ubr2.UserID = 1
and ubr2.RightType = 19 --USER_RES_RESOURCE_PROFILE
and ubr2.ObjectID = r2.AccProfileID))
--and r1.AccNumber LIKE '40__________________'
and r2.AccNumber LIKE '70601_______________'
--and (dt.InstitutionID = 2000 or r1.DepartmentID = 2000 or r2.DepartmentID = 2000)
and dt.BatchID = 10000003353
--and tR.InstOwnerID in (10001544513, 10000426129)
UNION ALL
--Form class: nil Query name:
--Base: workdb
select distinct/*1 as RecurrentRight,
(case
when substring(dt.NumberExt,5,1) = '1' then 'Карт2'
when substring(dt.NumberExt,5,1) = '3' then 'ПереносТр'
when substring(dt.NumberExt,5,1) = '5' then 'НевСумм'
when substring(dt.NumberExt,5,1) = '7' then 'КАкцепт'
when substring(dt.NumberExt,5,1) = '8' then 'Транзит'
when substring(dt.NumberExt,5,1) = '9' then 'M_Pay'
when substring(dt.NumberExt,5,1) = 'A' then 'НезРасч'
when substring(dt.NumberExt,5,1) = 'B' then 'КартРезерв'
when substring(dt.NumberExt,5,1) = 'C' then 'Внеб'
when substring(dt.NumberExt,5,1) = 'D' then 'СпецСчета'
when substring(dt.NumberExt,5,1) = 'E' then 'Приостанов'
when substring(dt.NumberExt,5,1) = 'F' then 'КАкцепт2'
when substring(dt.NumberExt,5,1) = 'Y' then 'КартИнПФЛ'
else ' '
end
) as Kartoteka,
(case
when substring(dt.NumberExt,9,1) = '1' then 'Карт2'
when substring(dt.NumberExt,9,1) = '3' then 'ПереносТр'
when substring(dt.NumberExt,9,1) = '5' then 'НевСумм'
when substring(dt.NumberExt,9,1) = '7' then 'КАкцепт'
when substring(dt.NumberExt,9,1) = '8' then 'Транзит'
when substring(dt.NumberExt,9,1) = '9' then 'M_Pay'
when substring(dt.NumberExt,9,1) = 'A' then 'НезРасч'
when substring(dt.NumberExt,9,1) = 'B' then 'КартРезерв'
when substring(dt.NumberExt,9,1) = 'C' then 'Внеб'
when substring(dt.NumberExt,9,1) = 'D' then 'СпецСчета'
when substring(dt.NumberExt,9,1) = 'E' then 'Приостанов'
when substring(dt.NumberExt,9,1) = 'F' then 'КАкцепт2'
when substring(dt.NumberExt,9,1) = 'Y' then 'КартИнПФЛ'
when substring(dt.NumberExt,9,1) = 'V' then 'КартИнПФЛ'
else ' '
end
) as KartDel,
' ' as ByTrip,
dt.Type,
dt.BalanceQty,*/
FORMAT(dt.Date,'yyyy.MM') Period,
dt.DealProtocolID as DealProtocolID,
dt.Date as OperDate,
dt.ResourceID as ResDebID,
dt.ResourcePsvID as ResCreID,
dt.DocNumber as Number,
dt.DealTransactID as DealTransactID,
/*dt.DealID,
dt.Direction,
case
when it.InterfaceObjectID = 66 then case
when isnull(piout.BankID,0) = isnull(piin.BankID,0) then ' '
else substring('РасходПриход', convert(integer,1+dt.Direction*6),6)
end
when it.InterfaceObjectID = 30 then substring('РасходПриход', convert(integer,1+dt.Direction*6),6)
else ' '
end as Direct,
piin.ResIntID0 as AccInID,
piin.AccClient as AccIn,
iin.BIC as BicIn,
piout.ResIntID0 as AccOutID,
piout.AccClient as AccOut,
iout.BIC as BicOut,
dt.ParentID,*/
dt.InstrumentID,
dt.Confirmed as Confirmed,
cast(dt.Qty as float)*(-1) as Qty,
cast(dt.FixQty as float)*(-1) as QtyDeb,
convert(char(25),r1.AccNumber) as Brief1,
convert(char(25),r2.AccNumber) as Brief2,
/*'' as Brief3,*/
dt.NumberExt,
dt.OpCode,
dt.Priority,
dt.FixCourse as Course,
dt.TermDate,
dt.DocDate,
dt.ValueDate,
dt.Nominal,
coalesce(o.Brief,oc.Brief) as ObjClsBrief,
b.Brief as Batch,
/*(case
when dt.DealID > 0 then 'из сделки'
else ' '
end
) as Deal,
(case
when exists (select 1
from tDocRelation dr WITH (NOLOCK index=XPKtDocRelation)
where dr.DealTransactID = dt.DealTransactID
and dr.RelType = 3
and dr.Type = 0
)
then 'реестр'
else ' '
end
) as Package,
case when (dt.NumberLow &8) /8 = 1 then 'РЦ' else ' ' end as RCField,*/
dt.FundID as FundDebID,
tR.InstOwnerID,
r1.Name as NameDeb,
r2.Name as NameCre,
dt.Comment as Comment,
--REPLACE(dt.Comment, '№ ', '') Comment2,
--left(dt.Comment, 10) as Comment2,
CASE
-- когда есть символ '№ '
WHEN dt.Comment like 'Комиссия за обслуживание по договору №%' THEN LEFT(SUBSTRING (dt.Comment, 38, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 38+2, 100)) - 1 + 2)
WHEN dt.Comment like 'Требование по уплате комиссии за обслуживание по договору №%' THEN LEFT(SUBSTRING (dt.Comment, 59, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 59+2, 100)) - 1 + 2)
WHEN dt.Comment like 'Комиссия за несоблюдение условий по договору №%' THEN LEFT(SUBSTRING (dt.Comment, 46, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 46+2, 100)) - 1 + 2)
WHEN dt.Comment like 'Требование по уплате комиссии за несоблюдение условий по договору №%' THEN LEFT(SUBSTRING (dt.Comment, 67, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 67+2, 100)) - 1 + 2)
WHEN dt.Comment like 'Комиссия по договору эквайринга №%' THEN LEFT(SUBSTRING (dt.Comment, 33+2, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 33+2, 100)) - 1 + 0)
WHEN dt.Comment like 'Комиссия за обслуживание по договору%' THEN LEFT(SUBSTRING (dt.Comment, 38, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 38, 100)) - 1)
WHEN dt.Comment like 'Требование по уплате комиссии за обслуживание по договору%' THEN LEFT(SUBSTRING (dt.Comment, 59, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 59, 100)) - 1)
WHEN dt.Comment like 'Комиссия за несоблюдение условий по договору%' THEN LEFT(SUBSTRING (dt.Comment, 46, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 46, 100)) - 1)
WHEN dt.Comment like 'Требование по уплате комиссии за несоблюдение условий по договору%' THEN LEFT(SUBSTRING (dt.Comment, 67, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 67, 100)) - 1)
WHEN dt.Comment like 'Комиссия по договору эквайринга%' THEN LEFT(SUBSTRING (dt.Comment, 33, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 33, 100)) - 1)
WHEN dt.Comment like 'Возврат комиссии по договору эквайринга%' THEN LEFT(SUBSTRING (dt.Comment, 41, 100), CHARINDEX(' ', SUBSTRING (dt.Comment, 41, 100)) - 1)
ELSE 'Прочие проводки'
END as Dog,
CASE
WHEN dt.Comment like 'Комиссия за обслуживание по договору%' THEN 1 --фиксированный абонентский платеж
WHEN dt.Comment like 'Требование по уплате комиссии за обслуживание по договору%' THEN 1 --фиксированный абонентский платеж
WHEN dt.Comment like 'Комиссия за несоблюдение условий по договору%' THEN 2 --платеж за несоблюдение условий
WHEN dt.Comment like 'Требование по уплате комиссии за несоблюдение условий по договору%' THEN 2 --платеж за несоблюдение условий
WHEN dt.Comment like 'Комиссия по договору эквайринга%' THEN 3 --платеж за комиссию с оборота
WHEN dt.Comment like 'Возврат комиссии по договору эквайринга%' THEN 3 --платеж за комиссию с оборота
ELSE 0
END as ComisType,
CASE
WHEN dt.Comment like 'Комиссия за обслуживание по договору%' THEN 'За обслуживание' --фиксированный абонентский платеж
WHEN dt.Comment like 'Требование по уплате комиссии за обслуживание по договору%' THEN 'За обслуживание' --фиксированный абонентский платеж
WHEN dt.Comment like 'Комиссия за несоблюдение условий по договору%' THEN 'За несоблюдение условий' --платеж за несоблюдение условий
WHEN dt.Comment like 'Требование по уплате комиссии за несоблюдение условий по договору%' THEN 'За несоблюдение условий' --платеж за несоблюдение условий
WHEN dt.Comment like 'Комиссия по договору эквайринга%' THEN 'За оборот' --платеж за комиссию с оборота
WHEN dt.Comment like 'Возврат комиссии по договору эквайринга%' THEN 'За оборот' --платеж за комиссию с оборота
ELSE 'Прочие проводки'
END as ComisTypeText
/*dt.InitiatorID as InitiatorID,
dt.FundPsvID as FundCreID,
it.Brief as InstrumentBrief,
substring((r1.AccNumber + '00000000'),6,3) as DebFundShortName,
substring((r2.AccNumber + '00000000'),6,3) as CreFundShortName,
case
when exists(select 1
from tFoldingDocument fd WITH (NOLOCK index=XPKtFoldingDocument)
where fd.DealTransactID = dt.DealTransactID) then 'Свод'
when exists(select 1
from tFoldingDocumentRelation fdr WITH (NOLOCK index=XPKtFoldingDocumentRelation)
where fdr.DocumentSourceID = dt.DealTransactID) then 'ВклСв'
else ' '
end as FoldingDoc,
0 as RelationType,
case
when (isnull(de.Flags, 0) & 1) = 1 then 'ИЗМ'
else ''
end as Properties ,
isnull(de.flags,0) as UserFlag,
isnull(de.OnlineControlFlags,0) as OnlineControlFlags,
isnull(sd.EDDocSignStatusID,0) as StatusID,
case
when sd.EDDocSignStatusID = 10 then 'С'
when sd.EDDocSignStatusID = 20 then 'П'
when sd.EDDocSignStatusID = 30 then 'Г'
when sd.EDDocSignStatusID = 40 then 'В'
when sd.EDDocSignStatusID = 50 then 'ОП'
when sd.EDDocSignStatusID = 60 then 'ОВ'
when sd.EDDocSignStatusID = 70 then 'Б'
else ''
end as StatusType,
case when isnull(tdfpfst.OPKC,'')='' then '' else 'СБП' end as DPF,
case when Well_grnded.DocValue = 1 then 'Да' else '' end as Well_grounded,
isnull(pdpu.Flag,0) as PUFlag,
isnull(pdSDO.Flag,0) as SDOFlag,
_tTransition.TargetStateID as StateID,
dt.NumberLow as NumberLow*/
from
tDealTransact dt WITH (NOLOCK index=XIE3tDealTransact)
inner join tRKO_FinOper_Sync it WITH (NOLOCK index=XPKtRKO_FinOper_Sync)
on it.FinOperID = dt.InstrumentID
inner join tRKO_ExpProperty_Sync b WITH (NOLOCK index=XPKtRKO_ExpProperty_Sync)
on b.ExpPropertyID = dt.BatchID
and b.ExpPropertyID between 5000024 and 10000004671
inner join tRKO_UserRight_Sync us WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
on us.UserID = 1
and us.RightType = 2
and us.ObjectID = b.ExpPropertyID
inner join tRKO_UserRight_Sync ufo WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
on ufo.UserID = 1
and ufo.RightType = 4
and ufo.ObjectID = dt.InstrumentID
inner join tRKO_Account_Sync r1 WITH (NOLOCK index=XPKtRKO_Account_Sync)
on r1.AccountID = dt.ResourceID
and r1.BalanceID = 2140
and r1.AccNumber LIKE '70601_______________'
inner join tRKO_Account_Sync r2 WITH (NOLOCK index=XPKtRKO_Account_Sync)
on r2.AccountID = dt.ResourcePsvID
--and r2.AccNumber LIKE '70601_______________'
--and (dt.InstitutionID = 2000 or r1.DepartmentID = 2000 or r2.DepartmentID = 2000)
inner join workdb.dbo.tObject _tObject WITH (NOLOCK index=XAK2tObject)
on _tObject.InstrumentID = dt.InstrumentID
and _tObject.ID = dt.DealtransactID
inner join workdb.dbo.tInstrument _tInstrument WITH (NOLOCK index=XPKtInstrument)
on _tInstrument.InstrumentID = _tObject.InstrumentID
inner join workdb.dbo.tProtocol _tProtocol WITH (NOLOCK index=XPKtProtocol)
on _tProtocol.ProtocolID = _tObject.CurrProtocolID
inner join workdb.dbo.tTransition _tTransition WITH (NOLOCK index=XPKtTransition)
on _tTransition.TransitionID = _tProtocol.TransitionID
left join workdb.dbo.tNode _tNode WITH (NOLOCK index=XPKtNode)
on _tNode.NodeID = _tTransition.TargetStateID
inner join tRKO_UserRight_Sync ub WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
on ub.UserID = 1
and ub.RightType = 14
and ub.ObjectID = _tTransition.TargetStateID
left join tRKO_ExpProperty_Sync o WITH (NOLOCK index=XPKtRKO_ExpProperty_Sync)
on o.ExpPropertyID = dt.PaymentType
and o.ExpPropertyType = 25
left join tObjClassifier oc WITH (NOLOCK index=XPKtDepClassifier)
on oc.ObjClassifierID = dt.PaymentType
left join tPrimaryDocumentAttr de WITH (NOLOCK index=XPKtPrimaryDocumentAttr)
on de.DealTransactID = dt.DealTransactID
left join tSignDoc sd WITH (NOLOCK index=XPKtSignDoc)
on sd.SignDocID = dt.DealTransactID
left join tDocumentPlatFast tdfpfst WITH (NOLOCK index=XPKtDocumentPlatFast)
on tdfpfst.DealTransactID = dt.DealTransactID
left join tDocRelation Well_grnded WITH (NOLOCK index=XPKtDocRelation)
on Well_grnded.DealTransactID = dt.DealTransactID
and Well_grnded.RelType = 51 --DOC_REL_REVAL_BASIS
/*left join tPayInstruct piin WITH (NOLOCK index=XIE2tPayInstruct)
on piin.DealTransactID = dt.DealTransactID
and piin.Type = 2
and piin.Direct = dt.Direction
and piin.Belong = dt.Direction*/
/*left join tRKO_Participant_Sync iin WITH (NOLOCK index=XPKtRKO_Participant_Sync)
on iin.ParticipantID = piin.BankID*/
/*left join tPayInstruct piout WITH (NOLOCK index=XIE2tPayInstruct)
on piout.DealTransactID = dt.DealTransactID
and piout.Type = 2
and piout.Direct = dt.Direction
and piout.Belong = 1 - dt.Direction*/
/*left join tRKO_Participant_Sync iout WITH (NOLOCK index=XPKtRKO_Participant_Sync)
on iout.ParticipantID = piout.BankID*/
left join tPrimaryDocPublicUtility pdpu WITH (NOLOCK index=XPKtPrimaryDocPublicUtility)
on pdpu.DealTransactID = dt.DealTransactID
left join tPrmDStateDefenseOrder pdSDO WITH (NOLOCK index=XPKtPrmDStateDefenseOrder)
on pdSDO.PrimaryDocumentID = dt.DealTransactID
left join tResource tR on tR.Brief = convert(char(25),r1.AccNumber)
where 1=1
and it.DsModuleID = 21
and dt.TransactType = 5
--and dt.Date = '20200430'
-- если необходим отчет за интервал между датами
and dt.Date >= DATEADD(d, 1, (EOMONTH (GETDATE(),-2))) --'20200501'
and dt.Date < DATEADD(d, 1, (EOMONTH (GETDATE(),-1))) --'20200601' -- +1 день
and (exists (select 1
from tRKO_UserRight_Sync ubr1 WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
where ubr1.UserID = 1
and ubr1.RightType = 19 --USER_RES_RESOURCE_PROFILE
and ubr1.ObjectID = r1.AccProfileID)
or
exists (select 1
from tRKO_UserRight_Sync ubr2 WITH (NOLOCK index=XPKtRKO_UserRight_Sync)
where ubr2.UserID = 1
and ubr2.RightType = 19 --USER_RES_RESOURCE_PROFILE
and ubr2.ObjectID = r2.AccProfileID))
and r1.AccNumber LIKE '70601_______________'
--and r2.AccNumber LIKE '70601_______________'
--and (dt.InstitutionID = 2000 or r1.DepartmentID = 2000 or r2.DepartmentID = 2000)
and dt.BatchID = 10000003353
--and tR.InstOwnerID in (10001544513, 10000426129)
) deals
left join (
Select distinct
tInstitution.InstitutionID
,AgrP.ContractID
,AgrP.Number
from tContractCrdDev CCD
left join tcontract AgrP
on AgrP.contractID = CCD.ParentCCDID
left join tInstitution
on tInstitution.InstitutionId = AgrP.InstitutionID
left join tcontract Agr1
on Agr1.contractID = CCD.ContractCrdDevID
left Join tInstrument
on tInstrument.InstrumentID = Agr1.InstrumentID
left Join tCrdAccountLink CAL
on CAL.ObjectID = CCD.ContractCrdDevID
left join tCRD_RuleAcc_Sync ta
on CAL.RuleID = ta.RuleID
and CAL.Flags = 2 --Только действующие!
and ta.ObjectID in (10000002061,
10000002062,
10000002063,
10000002370,
10000002372)
left join tResource Acc
on Acc.ResourceID = CAL.AccountID
) Contracts
on Contracts.Number = deals.Dog --and Contracts.InstitutionID = deals.InstOwnerID
GROUP BY
deals.Period
,Contracts.ContractID
,deals.Dog
,deals.ComisType
,deals.ComisTypeText
option (FORCE ORDER, LOOP JOIN, KEEPFIXED PLAN, FAST 1)