
			select distinct
				 EvnDrug.EvnDrug_id as "EvnDrug_id"
				,coalesce(EvnDrug.Drug_id, DD.Drug_id) as "Drug_id"
				,coalesce(Drug.Drug_Name, dcm.DrugComplexMnn_RusName, '') as "Drug_Name"
				,DD.DrugPrepFas_id as "DrugPrepFas_id"
				,DD.Drug_Fas as "Drug_Fas"
				,EPTD.EvnPrescrTreatDrug_id as "EvnPrescrTreatDrug_id"
				,EPTD.DrugComplexMnn_id as "DrugComplexMnn_id"
				-- для списания в полке берем общее число приемов в курсе
				,case when EVPL.Evn_id is not null
					then ECT.EvnCourseTreat_PrescrCount
					else EPT.EvnPrescrTreat_PrescrCount
				end as "EvnPrescrTreat_PrescrCount"
				,case when EVPL.Evn_id is not null
					then coalesce(ECTD.EvnCourseTreatDrug_FactCount,0)
					else coalesce(EPTD.EvnPrescrTreatDrug_FactCount,0)
				end as "EvnPrescrTreatDrug_FactCount"
				,EPTD.EvnPrescrTreatDrug_DoseDay as "EvnPrescrTreatDrug_DoseDay"
				,EPT.EvnPrescrTreat_Descr as "EvnPrescrTreat_Descr" -- Комментарий
				,ECTD.EvnCourseTreatDrug_id as "EvnCourseTreatDrug_id"
				,ECTD.EvnCourseTreatDrug_FactDose as "EvnCourseTreatDrug_FactDose"
				,ECTD.EvnCourseTreatDrug_FactCount as "EvnCourseTreatDrug_FactCount"
				,coalesce(EvnDrug.EvnCourse_id, EPT.EvnCourse_id) as "EvnCourse_id"
				,coalesce(EvnDrug.EvnDrug_rid, EPT.EvnPrescrTreat_rid) as "EvnDrug_rid"
				,coalesce(EvnDrug.EvnDrug_pid, EPT.EvnPrescrTreat_pid) as "EvnDrug_pid"
				,coalesce(EvnDrug.EvnPrescr_id, EPT.EvnPrescrTreat_id) as "EvnPrescr_id"
				,coalesce(EvnDrug.Person_id, EPT.Person_id) as "Person_id"
				,coalesce(EvnDrug.PersonEvn_id, EPT.PersonEvn_id) as "PersonEvn_id"
				,coalesce(EvnDrug.Server_id, EPT.Server_id) as "Server_id"
				,to_char (coalesce(EvnDrug.EvnDrug_setDT, EPT.EvnPrescrTreat_setDT), 'dd.mm.yyyy') as "EvnDrug_setDate"
				,coalesce(EvnDrug.EvnDrug_setTime, EPT.EvnPrescrTreat_setTime) as "EvnDrug_setTime"
				,coalesce(cast(EvnDrug.EvnDrug_Kolvo as numeric(18,0)), (EPTD.EvnPrescrTreatDrug_Kolvo * COALESCE(EPT.EvnPrescrTreat_PrescrCount, 1))) as "EvnDrug_Kolvo"
				,coalesce(EvnDrug.EvnDrug_KolvoEd, (EPTD.EvnPrescrTreatDrug_KolvoEd * COALESCE(EPT.EvnPrescrTreat_PrescrCount, 1))) as "EvnDrug_KolvoEd"
				,case when EVPL.Evn_id is not null then 1 else 2 end as "ParentEvn_IsStac"
				,to_char (v_Evn.Evn_setDT, 'dd.mm.yyyy') as "Evn_setDate"
				,LS.Lpu_id as "Lpu_id"
				,LS.LpuSection_id as "LpuSection_id"
				,COALESCE(LS.LpuSection_Name, '') as "LpuSection_Name"
				,MP.MedPersonal_id as "MedPersonal_id"
				,COALESCE(MP.Person_Fio, '') as "MedPersonal_FIO"
				,DD.DocumentUcStr_id as "DocumentUcStr_oid"
				,DD.DocumentUcStr_Ost as "DocumentUcStr_Ost"
				,DD.Storage_tid as "Storage_id"
				,DD.Mol_id as "Mol_id"
				,DD.Mol_Name as "Mol_Name"
				,DD.DrugFinance_Name as "DrugFinance_Name"
				,DD.WhsDocumentCostItemType_Name as "WhsDocumentCostItemType_Name"
				,COALESCE(DD.DocumentUcStr_Name, '') as "DocumentUcStr_Name",
				coalesce(Drug.Drug_Ean,EAN.DrugEAN_Code) as "DrugEAN_Code"
				
			from
				v_EvnPrescrTreat EPT
				inner join v_EvnPrescrTreatDrug EPTD on EPT.EvnPrescrTreat_id = EPTD.EvnPrescrTreat_id
				left join lateral (select * from v_EvnDrug where EvnPrescrTreatDrug_id = EPTD.EvnPrescrTreatDrug_id limit 1) EvnDrug ON TRUE
				left join v_EvnCourseTreat ECT on ECT.EvnCourseTreat_id = EPT.EvnCourse_id
				left join v_EvnCourseTreatDrug ECTD on ECTD.EvnCourseTreatDrug_id = EPTD.EvnCourseTreatDrug_id
				left join rls.v_Drug Drug on Drug.Drug_id = EPTD.Drug_id
				left join rls.DrugEAN EAN on EAN.Drug_id = Drug.Drug_id
				left join rls.v_DrugComplexMnn dcm on dcm.DrugComplexMnn_id = coalesce(EPTD.DrugComplexMnn_id,Drug.DrugComplexMnn_id)
				inner join v_Evn on v_Evn.Evn_id = EPT.EvnPrescrTreat_pid
				inner join v_Lpu on v_Evn.Lpu_id = v_Lpu.Lpu_id 
				left join EvnPS EPS on EPS.Evn_id = v_Evn.Evn_id
				left join EvnSection ES on ES.Evn_id = v_Evn.Evn_id
				left join EvnVizit EVPL on EVPL.Evn_id = v_Evn.Evn_id
				inner join v_LpuSection_all LS on LS.LpuSection_id = COALESCE(ECT.LpuSection_id, ES.LpuSection_id, EVPL.LpuSection_id, EPS.LpuSection_pid)
				inner join v_MedPersonal MP on MP.MedPersonal_id = COALESCE(ECT.MedPersonal_id, ES.MedPersonal_id, EVPL.MedPersonal_id, EPS.MedPersonal_pid)
				LEFT JOIN LATERAL (
					select
						D.DrugPrepFas_id as DrugPrepFas_id
						,D.Drug_Fas as Drug_Fas
						,DU.Storage_tid as Storage_tid
						,STOR.Storage_id as Storage_id
						,DUS.Drug_id as Drug_id
						,DUS.DocumentUcStr_id as DocumentUcStr_id
						,coalesce(ost.DocumentUcStr_Ost,0) as DocumentUcStr_Ost
						,DF.DrugFinance_Name as DrugFinance_Name
						,WDCIT.WhsDocumentCostItemType_Name as WhsDocumentCostItemType_Name
						,DUS.DocumentUcStr_Ser as DocumentUcStr_Ser
						,M.Mol_id as Mol_id
						,case
							when M.MedPersonal_id is not null then MP.Person_Fio
							else RTRIM(LTRIM((COALESCE(M.Person_SurName, '') || ' ' || COALESCE(M.Person_FirName, '') || ' ' || COALESCE(M.Person_SecName, ''))))
						end as Mol_Name
						,cast(cast(round(coalesce(ost.DocumentUcStr_Ost, 0), 4) as numeric(16, 4)) as varchar(20)) || ', фин. '|| RTRIM(RTRIM(COALESCE(DF.DrugFinance_Name, 'отсут.'))) || ', серия ' || RTRIM(COALESCE(DUS.DocumentUcStr_Ser, ''))
						as DocumentUcStr_Name
						,DU.DocumentUc_id as DocumentUc_id
						,DUS.DocumentUcStr_Price as DocumentUcStr_Price
						,DUS.DocumentUcStr_Sum as DocumentUcStr_Sum
						,DUS.GoodsUnit_bid as GoodsUnit_bid

					from
						v_DrugShipmentLink DSL
						inner join v_DrugOstatRegistry DOR on DOR.DrugShipment_id = DSL.DrugShipment_id
						inner join rls.v_Drug D on D.Drug_id = DOR.Drug_id
						inner join v_DocumentUcStr DUS on DUS.DocumentUcStr_id = DSL.DocumentUcStr_id
						inner join v_DocumentUc DU on DU.DocumentUc_id = DUS.DocumentUc_id
						left join v_Storage STOR on STOR.Storage_id = DOR.Storage_id
						left join v_Mol M on M.Mol_id = DU.Mol_tid and M.Storage_id = DU.Storage_tid
						LEFT JOIN LATERAL (
							select Person_Fio as Person_Fio
							from v_MedPersonal
							where MedPersonal_id = M.MedPersonal_id
							limit 1
						) MP ON TRUE
						left join v_DrugFinance DF on DF.DrugFinance_id = DU.DrugFinance_id
						left join v_WhsDocumentCostItemType WDCIT on WDCIT.WhsDocumentCostItemType_id = DU.WhsDocumentCostItemType_id
						LEFT JOIN LATERAL (
							select coalesce(sum(rED.EvnDrug_Kolvo),0) as EvnDrug_Kolvo
							from v_EvnDrug rED
							inner join v_DocumentUcStr rDUS on rDUS.DocumentUcStr_id = rED.DocumentUcStr_id
							inner join v_DocumentUc rDU on rDU.DocumentUc_id = rDUS.DocumentUc_id
							inner join v_DrugShipmentLink oDSL on oDSL.DocumentUcStr_id = rED.DocumentUcStr_oid
							where oDSL.DrugShipment_id = DSL.DrugShipment_id and coalesce(rDU.DrugDocumentStatus_id,1) = 1 --новый
							limit 1
						) rl ON TRUE
						LEFT JOIN LATERAL (
							select
								(DOR.DrugOstatRegistry_Kolvo-coalesce(rl.EvnDrug_Kolvo,0)) as DocumentUcStr_Ost
                            limit 1
						) ost ON TRUE
					where  1=1
						and (D.Drug_id = coalesce(EvnDrug.Drug_id, EPTD.Drug_id)
						OR D.DrugComplexMnn_id = EPTD.DrugComplexMnn_id) --не всегда работает
						and DOR.Org_id = v_Lpu.Org_id
						and STOR.Storage_id in (
			                select
			                    SSL.Storage_id as Storage_id
			                from
			                    v_StorageStructLevel SSL
			                    left join v_MedService MS on MS.MedService_id = SSL.MedService_id
			                where
			                    SSL.LpuSection_id = LS.LpuSection_id or MS.LpuSection_id = LS.LpuSection_id
			            )
                    limit 1
				) DD ON TRUE
				
			where
				EPT.EvnPrescrTreat_id = '500801005273616'
				
			order by EPTD.EvnPrescrTreatDrug_id
		
