delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;
delete from ccmanager where pid=402 limit 10000;


/* update ademo set budget = 48000000, qty = 120000000 where pid=402 and code_categ = 1001;
update ademo set budget = budget*2, qty = qty*2 where pid=402 and code_categ = 1001 and code_center in ('1001','1002');
update ademo set budget = budget*4, qty = qty*4 where pid=402 and code_categ = 1001 and code_center in ('3001','3002');
update ademo set budget = 24000000, qty = 32000000 where pid=402 and code_categ = 1002;
update ademo set budget = 36000000, qty = 30000000 where pid=402 and code_categ = 1003;
update ademo set budget = 12000000, qty = 5000000 where pid=402 and code_categ = 2004; */

/* UPDATE CALENDAR */
/* Return a random number >= 350 and <=450: */

UPDATE calendar SET rand_number = FLOOR(RAND()*(360-260+1)+360);

/* NORTH FARMS */
/* pid=402 */
/* BUDGET */
insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid)
select distinct a.code_center,a.code_categ,concat(date_format(c.calday,'%Y-%m'),'-01'),a.qty/12,a.budget/12,1,a.pid
from ademo a, calendar c
where a.pid=402
and c.calday >= '2019-01-01' and c.calday <= '2019-12-31';


/* MILK */
/* MORNING */
insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid,comms_html)
select a.code_center,a.code_categ,c.calday,(a.qty/c.rand_number)/2,(a.budget/c.rand_number)/2,2,a.pid,'Morning'
from ademo a, calendar c
where c.calday >= '2019-01-01' and c.calday <= '2019-12-31'
and a.code_categ in (select code_categ from categs where pid = 402 and code_categ_group = '1000')
and a.pid=402;

/* EVENING */
insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid,comms_html)
select a.code_center,a.code_categ,c.calday,(a.qty/c.rand_number)/2,(a.budget/c.rand_number)/2,2,a.pid,'Evening'
from ademo a, calendar c
where c.calday >= '2019-01-01' and c.calday <= '2019-12-31'
and a.code_categ in (select code_categ from categs where pid = 402 and code_categ_group = '1000')
and a.pid=402;

/* NIGHT */
insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid,comms_html)
select a.code_center,a.code_categ,c.calday,(a.qty/c.rand_number)/5,(a.budget/c.rand_number)/5,2,a.pid,'Night'
from ademo a, calendar c
where c.calday >= '2019-01-01' and c.calday <= '2019-12-31'
and date_format(c.calday,'%m') in ('04','05','06')
and a.code_categ in (select code_categ from categs where pid = 402 and code_categ_group = '1000')
and a.pid=402;


/* WASTE ALL */
insert into ccmanager (code_center,code_categ,rab_date,rab_qty,rab_number,mid,pid)
select a.code_center,a.code_categ,c.calday,a.qty/c.rand_number,a.budget/c.rand_number,2,a.pid
from ademo a, calendar c
where c.calday >= '2019-01-01' and c.calday <= '2019-12-31'
and a.code_categ in (select code_categ from categs where pid = 402 and code_categ_group != '1000')
and a.pid=402;

/* ADD MORE PRODUCTION ON CENTER 1001 */
update ccmanager set rab_qty=rab_qty*1.5, rab_number = rab_number*1.5 where pid = 402  and mid=2 and code_center in ('1001','1002','1003','1005','4001');