
				select
					dorl.DrugOstatRegistryLink_id as "DrugOstatRegistryLink_id",
					dorl.DrugOstatRegistryLink_Count as "DrugOstatRegistryLink_Count",
					reserved_dor.DrugOstatRegistry_id as "Reserved_DrugOstatRegistry_id",
					reserved_dor.DrugOstatRegistry_Cost as "Reserved_DrugOstatRegistry_Cost",
					available_dor.DrugOstatRegistry_id as "Available_DrugOstatRegistry_id",
					available_dor.DrugOstatRegistry_Cost as "Available_DrugOstatRegistry_Cost"
				from
					v_DocumentUcStr dus 

					left join v_DrugOstatRegistryLink dorl  on

						dorl.DrugOstatRegistryLink_TableName = 'DocumentUcStr' and
						dorl.DrugOstatRegistryLink_TableID = dus.DocumentUcStr_id
					left join v_DrugOstatRegistry reserved_dor  on reserved_dor.DrugOstatRegistry_id = dorl.DrugOstatRegistry_id

					LEFT JOIN LATERAL (

						select 
							i_dor.DrugOstatRegistry_id,
							i_dor.DrugOstatRegistry_Kolvo,
							i_dor.DrugOstatRegistry_Cost
						from
							v_DrugOstatRegistry i_dor 

						where
							i_dor.SubAccountType_id = 1 and
							COALESCE(i_dor.Contragent_id, 0) = COALESCE(reserved_dor.Contragent_id, 0) and

							COALESCE(i_dor.Org_id, 0) = COALESCE(reserved_dor.Org_id, 0) and

							COALESCE(i_dor.DrugFinance_id, 0) = COALESCE(reserved_dor.DrugFinance_id, 0) and

							COALESCE(i_dor.DrugShipment_id, 0) = COALESCE(reserved_dor.DrugShipment_id, 0) and

							COALESCE(i_dor.Drug_id, 0) = COALESCE(reserved_dor.Drug_id, 0) and

							COALESCE(i_dor.WhsDocumentCostItemType_id, 0) = COALESCE(reserved_dor.WhsDocumentCostItemType_id, 0) and

							COALESCE(i_dor.PrepSeries_id, 0) = COALESCE(reserved_dor.PrepSeries_id, 0) and

							COALESCE(i_dor.Okei_id, 0) = COALESCE(reserved_dor.Okei_id, 0) and

							COALESCE(i_dor.Storage_id, 0) = COALESCE(reserved_dor.Storage_id, 0) and

							COALESCE(i_dor.DrugOstatRegistry_Cost, 0) = COALESCE(reserved_dor.DrugOstatRegistry_Cost, 0) and

							COALESCE(i_dor.Drug_did, 0) = COALESCE(reserved_dor.Drug_did, 0) and

							COALESCE(i_dor.GoodsUnit_id, '57'::bigint) = COALESCE(reserved_dor.GoodsUnit_id, '57'::bigint)
                         limit 1

					) available_dor ON true
				where
					dorl.DrugOstatRegistryLink_id is not null and
					(
						(
							'450101000032701' is not null and
							dus.DocumentUc_id = '450101000032701'
						) or (
							NULL is not null and
							dus.DocumentUcStr_id = NULL
						)
					);
			
