откуда берутся ограничения ( справочник?)
			// проверка по всем заявкам
			$cAll = $db->sql_query("
				SELECT
					coalesce(p.nc, 0) - coalesce(m.nc, 0) nc,
					coalesce(p.okpdtr, m.okpdtr) okpdtr
				FROM (
					select
						sum(abs(coalesce(d.need_count, 0))) nc,
						d.okpdtr || coalesce('-' || d.okpdtrf11, '') okpdtr
					from mg_request r
					left join mg_request_data d on r.id = d.request_id
					where
						(r.type = 'prim' or (r.type = 'second' and r.increase = true))
						and r.status = 'include_in_quota'
						and r.region = '$cRegion'
						and r.year = '$cYear'
						and r.person_id = '$cPersonId'
						and d.okpdtr || coalesce('-' || d.okpdtrf11, '') in ($cOkpdtrStr)
						and r.id <> $cReqId
					group by d.okpdtr || coalesce('-' || d.okpdtrf11, '')
				) p FULL JOIN (
					select
						sum(abs(coalesce(d.need_count,0))) nc,
						d.okpdtr || coalesce('-' || d.okpdtrf11, '') okpdtr
					from mg_request r
					left join mg_request_data d on r.id = d.request_id
					where
						r.type = 'second' and r.increase = false
						and r.status not in ('not_sent', 'recall')
						and r.region = '$cRegion'
						and r.year = '$cYear'
						and r.person_id = '$cPersonId'
						and d.okpdtr || coalesce('-' || d.okpdtrf11, '') in ($cOkpdtrStr)
						and r.id <> $cReqId
					group by d.okpdtr || coalesce('-' || d.okpdtrf11, '')
				) m ON p.okpdtr = m.okpdtr
			");

			while (($cF = $db->sql_fetchrow($cAll)) !== false) {
				$cAllArr[$cF['okpdtr']] = intval($cF['nc']);
			}

			// проверка по конкретной заявке
			$cOne = $db->sql_query("
				SELECT
					coalesce(p.nc, 0) - coalesce(m.nc, 0) nc,
					coalesce(p.okpdtr, m.okpdtr) okpdtr
				FROM (
					select
						sum(abs(coalesce(d.need_count, 0))) nc,
						d.okpdtr || coalesce('-' || d.okpdtrf11, '') okpdtr
					from mg_request r
					left join mg_request_data d on r.id = d.request_id
					where
						(r.type = 'prim' or (r.type = 'second' and r.increase = true))
						and r.status = 'include_in_quota'
						and r.region = '$cRegion'
						and r.year = '$cYear'
						and r.person_id = '$cPersonId'
						and d.okpdtr || coalesce('-' || d.okpdtrf11, '') in ($cOkpdtrStr)
						and r.id = $cParentId
					group by d.okpdtr || coalesce('-' || d.okpdtrf11, '')
				) p FULL JOIN (
					select
						sum(abs(coalesce(d.need_count,0))) nc,
						d.okpdtr || coalesce('-' || d.okpdtrf11, '') okpdtr
					from mg_request r
					left join mg_request_data d on r.id = d.request_id
					where
						r.type = 'second' and r.increase = false
						and r.status not in ('not_sent', 'recall')
						and r.region = '$cRegion'
						and r.year = '$cYear'
						and r.person_id = '$cPersonId'
						and d.okpdtr || coalesce('-' || d.okpdtrf11, '') in ($cOkpdtrStr)
						and r.parent_id = $cParentId
						and r.id <> $cReqId
					group by d.okpdtr || coalesce('-' || d.okpdtrf11, '')
				) m ON p.okpdtr = m.okpdtr
			");

			while (($cF = $db->sql_fetchrow($cOne)) !== false) {
				$cOneArr[$cF['okpdtr']] = intval($cF['nc']);
			}

			foreach ($cOkpdtrArr as $cOkpdtr => $cNC) {
				if ($cNC > $cAllArr[$cOkpdtr]) {
					$fer = 1;
					$fermess .= "<LI>Превышена суммарная квота по всем заявкам по коду профессии $cOkpdtr";
				}
				if ($cNC > $cOneArr[$cOkpdtr]) {
					$fer = 1;
					$fermess .= "<LI>Превышена суммарная квота по данной заявке по коду профессии $cOkpdtr";
					if ($cOneArr[$cOkpdtr] > 0) $fermess .= ", заявку на уменьшение можно подать не более, чем на $cOneArr[$cOkpdtr] работников";
				}
			}