DROP PROCEDURE `pav_PrepareAgrmsTableForPredPlat`;
CREATE PROCEDURE `pav_PrepareAgrmsTableForPredPlat`(IN `gr_id` INT) COMMENT 'Пересчет' NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN
DECLARE done INT DEFAULT 0;
DECLARE agrm_idd INT;
DECLARE uider INT;
DECLARE balancer double;
DECLARE num CHAR(100);
DECLARE nam VARCHAR(100);
DECLARE mob CHAR(100);
DECLARE adr VARCHAR(200);
DECLARE agrms CURSOR FOR
select accounts_addr.address,agreements.number,agreements.agrm_id as ag,agreements.uid as uuid,agreements.balance,accounts.name,accounts.mobile from
vgroups inner join agreements on vgroups.uid=agreements.uid inner join accounts on agreements.uid=accounts.uid left join accounts_addr on accounts_addr.uid=accounts.uid
where vgroups.blocked<>10 and vgroups.uid in (select uid from usergroups_staff where usergroups_staff.group_id=gr_id) and agreements.archive=0 and vgroups.archive=0 and LENGTH(accounts.mobile)>=10 and accounts_addr.type=1
group by agreements.agrm_id;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
delete from lanbpredplat where grp=gr_id;
OPEN agrms;
REPEAT
FETCH agrms INTO adr,num,agrm_idd,uider,balancer,nam,mob;
if NOT done then
insert into lanbpredplat (am_usl,am_inet,am_tv,agrm_id,grp,uid,balance,number,username,phone,address) values (0,0,0,agrm_idd,gr_id,uider,balancer,num,nam,mob,adr);
END IF;
UNTIL done END REPEAT;
CLOSE agrms;
end