delete from ccmanager where pid = 202;

insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid)
select cc,ctk,bdate,qty,budget,1,202
from ctos.aac_budget
where bid=0;

insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid)
select cc,ctk,bdate,qty,budget,2,202
from ctos.aac_budget
where bid>0;

insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid)
select b.cc,b.ctk,p.pdate,1,sum(b.budget)*(p.performed/100),4,202
from ctos.aac_perform p, ctos.aac_budget b
where p.cc = b.cc
group by b.cc,b.ctk,p.pdate;


insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid)
select a.cc,a.ctk,m.frd,a.qty,a.totalcost,5,202
from ctos.aac_actual_costs a, ctos.aac_months m
where a.monid = m.monid;


insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid)
select cc,ctk,bdate,qty,forecast,7,202
from ctos.aac_forecast;

insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid)
select b.cc,b.ctk,p.pdate,1,sum(b.forecast)*(p.performed/100),9,202
from ctos.aac_perform p, ctos.aac_forecast b
where p.cc = b.cc
group by b.cc,b.ctk,p.pdate,p.performed;


delete from ccmanager where pid = 202 and code_center not in (select code_center from centers where pid = 202 and code_center_group in ('10','20'));