declare t_num number; t_type number; t_name varchar2(20); t_tv varchar2(20); abon_inet number; --абонентская плата за интернет abon_paket number; -- абонентская плата за интернет при работе по пакету abon_tv_optik number; -- абонентская плата за ТВ по оптике abon_tv_gpon number; -- абонплата за ТВ по G-Pon-у port_rezerv number; -- сумма за резервирование порта add_abon number; -- если больше 0 то абонент подключился в этом месяце add_abon_sum number; -- сумма за подключение del_abon number; -- если больше 0 то абонент отключился в этом месяце del_abon_type varchar2(20); -- тип технологии отключенного абонента в этом месяце add_ktv number; -- если больше 0 то был наряд на подключение КТВ add_ktv_sum number; -- сумма за подключение КТВ del_ktv number; -- если больше 0 то был наряд на сняте КТВ ktv_all number; -- полная сумма всех начислений по КТВ inet_all number; -- полная сумма всех начислений по Интернету x number; z number; zz number; god number; -- Начальные установки - год mes1 number; -- Начальные установки - месяц mes2 number; -- Начальные установки - месяц dat1 date; -- Начальные установки - 1 день месяца dat2 date; -- Начальные установки - 1 день месяца + 1 type_unlim number; -- тип последней безлимитки cursor one is select p.id from shura.population p order by p.id; --получаем список л\с begin -- Начальные установки mes1:=1; mes2:=7; god:=2018; dat1:=to_date('01-'||to_char(mes1)||'-'||to_char(god),'dd-mm-yyyy'); dat2:=ADD_MONTHS(dat1,1); update shura.pev_big_rep set type_1=0, type_2=0,type_3=0, type_4=0, type_5=0; delete shura.pev_big_rep where gr_id>10; commit; for c in one loop --пробегаем по всем л\счетам физиков --dbms_output.put_line(to_char(c.id)); -- получаем полную сумму начислений за Интернет inet_all:=0; select sum(aa) into inet_all from ( select nvl(sum(b.balance),0) aa from shura.balance b, shura.tarifficator tt where b.population_id=c.id and b.month>=mes1 and b.month=dat1 and e.extra_date=dat1 and r.recomp_date=dat1 and s.execute_date=dat1 and s.execute_date=add_months(dat1,-8) and a.extra_date0 and a.one_service_id in (select iot.one_service_id from shura.inet_unlim_type iot) and a.arch in ('A','B') order by a.extra_date desc ) where rownum=1; if type_unlim=1 then type_unlim:=0; select ss into type_unlim from ( select a.extra_date dd , a.one_service_id ss from shura.extra a where a.extra_date>=add_months(dat1,-8) and a.extra_date0 and a.one_service_id in (select iot.one_service_id from shura.inet_unlim_type iot) and a.arch in ('A','B') order by a.extra_date desc ) where rownum=1; end if; if del_abon_type='Оптика' then update shura.pev_big_rep set type_1=type_1+1 where npp=8; --обновляем счетчик кол-ва отключенных клиентов if type_unlim in (453,501,533) then -- если оптика по пакету update shura.pev_big_rep set type_1=type_1+1 where npp=17; --обновляем счетчик кол-ва отключенных клиентов end if; end if; if del_abon_type='Смешанный тип' then update shura.pev_big_rep set type_2=type_2+1 where npp=8; --обновляем счетчик кол-ва отключенных клиентов end if; if del_abon_type='Радиоканал' then update shura.pev_big_rep set type_3=type_3+1 where npp=8; --обновляем счетчик кол-ва отключенных клиентов end if; if del_abon_type='Gpon' then update shura.pev_big_rep set type_4=type_4+1 where npp=8; --обновляем счетчик кол-ва отключенных клиентов if type_unlim in (519,520) then update shura.pev_big_rep set type_4=type_4+1 where npp=17; --обновляем счетчик кол-ва отключенных клиентов end if; end if; if del_abon_type not in ('Оптика','Gpon','Смешанный тип','Радиоканал') then update shura.pev_big_rep set type_5=type_5+1 where npp=8; --обновляем счетчик кол-ва отключенных клиентов end if; commit; end if; -- расчитаем попадаем ли этот абонент в подключенные в этом месяце и посчитаем стоимость подключения select count (1) into add_abon from shura.inet_set_order s where s.execute_date>=dat1 and s.execute_date0 then -- если нарядов на установку больше 0, то Считаем сумму подключения --dbms_output.put_line(to_char(c.id)); select nvl(sum(ss),0) into add_abon_sum from ( select nvl(sum(s.summa),0) ss from shura.inet_set_order s where s.execute_date>=dat1 and s.execute_date=dat1 and e.extra_date=dat2 or pp.del_date is null ) and pp.port_id=p.id and p.router_id=r.id --and r.del_date is null and r.router_type_id=rt.id and r.router_type_id not in (28,5,6) -- убираем криптошлюз и безлимитные порты and pp.population_id=c.id and rownum=1; if t_num=0 and (add_abon=1 or inet_all<>0)then update shura.pev_big_rep set type_5=type_5+inet_all where npp=10; --обновляем сумму ИТОГО для странных абонентов update shura.pev_big_rep set type_5=type_5+1 where npp=9; --обновляем кол-во странных абонентов update shura.pev_big_rep set type_5=type_5+add_abon where npp=7; -- увеличиваем кол-во притока за счет странных абонентов update shura.pev_big_rep set type_5=type_5+add_abon_sum where npp=2; -- увеличиваем сумму за подключение для странных абонентов insert into shura.pev_big_rep (GR_ID,type_5) values(c.id,inet_all); end if; if t_num>0 then -- расчитываем абонентскую плату клиента, чтобы потом подставить в столбец соответсвующий типу подключения С УЧЕТОМ ПЕРЕРАСЧЕТОВ select sum(ss) into abon_inet from ( select nvl(sum(e.summa),0) ss from shura.extra e where e.extra_date>=dat1 and e.extra_date=dat1 and e.recomp_date=dat1 and e.extra_date=dat1 and e.recomp_date=add_months(dat1,-8) and a.extra_date0 and a.one_service_id in (select iot.one_service_id from shura.inet_unlim_type iot) and a.arch in ('A','B') order by a.extra_date desc ) where rownum=1; if type_unlim=1 then type_unlim:=0; select ss into type_unlim from ( select a.extra_date dd , a.one_service_id ss from shura.extra a where a.extra_date>=add_months(dat1,-8) and a.extra_date0 and a.one_service_id in (select iot.one_service_id from shura.inet_unlim_type iot) and a.arch in ('A','B') order by a.extra_date desc ) where rownum=1; end if; --Считаем Резервирование порта select nvl(sum(e.summa),0) into port_rezerv from shura.extra e where e.extra_date>=dat1 and e.extra_date=dat2 or pp.del_date is null ) -- ,было and (pp.del_date>=last_day(dat1) and pp.port_id=p.id and p.router_id=r.id --and r.del_date is null and r.router_type_id=rt.id and r.router_type_id not in (28,5,6) -- убираем криптошлюз и безлимитные порты and pp.population_id=c.id and rownum=1; if t_name not in ('Оптика','Gpon','Смешанный тип','Радиоканал') then update shura.pev_big_rep set type_5=type_5+inet_all where npp=10; --обновляем сумму ИТОГО для странных абонентов update shura.pev_big_rep set type_5=type_5+1 where npp=9; --обновляем кол-во странных абонентов insert into shura.pev_big_rep (GR_ID,type_5) values(c.id,inet_all); end if; -- Если тип подключения ОПТИКА if t_name='Оптика' then update shura.pev_big_rep set type_1=type_1+1 where npp=9; --обновляем счетчик кол-ва клиентов по оптике update shura.pev_big_rep set type_1=type_1+abon_inet where npp=3; -- увеличиваем абонплату update shura.pev_big_rep set type_1=type_1+port_rezerv where npp=5; -- увеличиваем резервирование порта update shura.pev_big_rep set type_1=type_1+add_abon where npp=7; -- увеличиваем кол-во притока update shura.pev_big_rep set type_1=type_1+add_abon_sum where npp=2; -- увеличиваем сумму за подключение update shura.pev_big_rep set type_1=type_1+inet_all where npp=10; -- увеличиваем итоговую сумму -- select count(1) into t_num from shura.extra e where e.extra_date>=dat1 and e.extra_date0 and e.population_id=c.id; -- Если клиент работает по пакету if type_unlim in (453,501,533) then update shura.pev_big_rep set type_1=type_1+1 where npp=18; -- обновляем счетчик кол-ва клиентов по ПАКЕТУ update shura.pev_big_rep set type_1=type_1+abon_paket where npp=12; -- увеличиваем абонплату если у клиента пакет update shura.pev_big_rep set type_1=type_1+port_rezerv where npp=14; -- увеличиваем резервирование порта по пакету но такого быть не должно обычно update shura.pev_big_rep set type_1=type_1+add_abon where npp=16; -- увеличиваем кол-во притока update shura.pev_big_rep set type_1=type_1+add_abon_sum where npp=11; -- увеличиваем сумму за подключение update shura.pev_big_rep set type_1=type_1+inet_all where npp=19; -- увеличиваем итоговую сумму end if; end if; ---------------------------------------------------------- -- Если тип подключения G-PON if t_name='Gpon' then update shura.pev_big_rep set type_4=type_4+1 where npp=9;--обновляем счетчик кол-ва клиентов по G-PON update shura.pev_big_rep set type_4=type_4+abon_inet where npp=3; -- увеличиваем абонплату update shura.pev_big_rep set type_4=type_4+port_rezerv where npp=5; -- увеличиваем резервирование порта update shura.pev_big_rep set type_4=type_4+add_abon where npp=7; -- увеличиваем кол-во притока update shura.pev_big_rep set type_4=type_4+add_abon_sum where npp=2; -- увеличиваем сумму за подключение update shura.pev_big_rep set type_4=type_4+inet_all where npp=10; -- увеличиваем итоговую сумму -- select count(1) into t_num from shura.extra e where e.extra_date>=dat1 and e.extra_date0 and e.population_id=c.id; -- Если клиент работает по пакету if type_unlim in (519,520) then update shura.pev_big_rep set type_4=type_4+1 where npp=18;-- обновляем счетчик кол-ва клиентов по ПАКЕТУ update shura.pev_big_rep set type_4=type_4+abon_paket where npp=12; -- увеличиваем абонплату если у клиента пакет update shura.pev_big_rep set type_4=type_4+port_rezerv where npp=14; -- увеличиваем резервирование порта по пакету но такого быть не должно обычно update shura.pev_big_rep set type_4=type_4+add_abon where npp=16; -- увеличиваем кол-во притока update shura.pev_big_rep set type_4=type_4+add_abon_sum where npp=11; -- увеличиваем сумму за подключение update shura.pev_big_rep set type_4=type_4+inet_all where npp=19; -- увеличиваем итоговую сумму end if; end if; ---------------------------------------------------------- -- Если тип подключения Смешенный if t_name='Смешанный тип' then update shura.pev_big_rep set type_2=type_2+1 where npp=9;-- обновляем счетчик кол-ва клиентов по Смешанному типу update shura.pev_big_rep set type_2=type_2+abon_inet where npp=3; -- увеличиваем абонплату update shura.pev_big_rep set type_2=type_2+port_rezerv where npp=5; -- увеличиваем резервирование порта update shura.pev_big_rep set type_2=type_2+add_abon where npp=7; -- увеличиваем кол-во притока update shura.pev_big_rep set type_2=type_2+add_abon_sum where npp=2; -- увеличиваем сумму за подключение update shura.pev_big_rep set type_2=type_2+inet_all where npp=10; -- увеличиваем итоговую сумму end if; ---------------------------------------------------------- -- Если тип подключения Радиоканал if t_name='Радиоканал' then update shura.pev_big_rep set type_3=type_3+1 where npp=9;-- обновляем счетчик кол-ва клиентов по Радиоканалу update shura.pev_big_rep set type_3=type_3+abon_inet where npp=3; -- увеличиваем абонплату update shura.pev_big_rep set type_3=type_3+port_rezerv where npp=5; -- увеличиваем резервирование порта update shura.pev_big_rep set type_3=type_3+add_abon where npp=7; -- увеличиваем кол-во притока update shura.pev_big_rep set type_3=type_3+add_abon_sum where npp=2; -- увеличиваем сумму за подключение update shura.pev_big_rep set type_3=type_3+inet_all where npp=10; -- увеличиваем итоговую сумму end if; ---------------------------------------------------------- end if; commit; ---------------------------------------------------------- -------------------------БЛОК ТВ-------------------------- ---------------------------------------------------------- --- Проверяем, а не было ли в месяце наряда на установку КТВ, если был, то получаем стоимость подключения select count(1) into add_ktv from shura.set_order a where a.execute_date>=dat1 and a.execute_date0 then select sum(ss) into add_ktv_sum from ( select nvl(sum(a.summa),0) ss from shura.set_order a where a.execute_date>=dat1 and a.execute_date=dat1 and e.extra_date=dat1 and a.execute_date0 then -- если был наряд на сняте update shura.pev_big_rep set type_1=type_1+1 where npp=26;-- Увеличиваем счетчик клиентов end if; select count(1) into del_ktv from shura.drop_order a where a.execute_date>=dat1 and a.execute_date0 then -- если был наряд на сняте update shura.pev_big_rep set type_4=type_4+1 where npp=26;-- Увеличиваем счетчик клиентов end if; -- Считаем количество услуг КТВ на л\с абонента select count(1) into t_num from shura.month_extra me where me.tarifficator_id in (78,81,87,88,162,181) and --(78,98,139,140,141,147,153,154,162,176,181) and --78,81,87,88,162,181) and me.set_date<=last_day(dat1) and (me.del_date>=last_day(dat1) or me.del_date is null ) and me.population_id=c.id and rownum=1; -- если услуги есть, то у него КТВ подключено if t_num>0 then -- получаем общую сумму всех начислений ktv_all:=0; select sum(aa) into ktv_all from ( select nvl(sum(b.balance),0) aa from shura.balance b, shura.tarifficator tt where b.population_id=c.id and b.month>=mes1 and b.month=dat1 and e.extra_date=dat1 and r.recomp_date=last_day(dat1) or me.del_date is null ) and me.population_id=c.id and rownum=1; -- Если КТВ по оптике if t_tv='Оптика КТВ' then --считаем абонплату с учетом перерасчетов select sum(ss) into abon_tv_optik from ( select nvl(sum(b.balance),0) ss from shura.balance b where b.month>=mes1 and b.month=dat1 and r.recomp_date=mes1 and b.month=dat1 and r.recomp_date