$tousers=$this->db->query('SELECT `sent`-(`sent`%86400) as `day`,`to`,count(*) as `count`,sum(`size`) as `size` FROM '.VIEW_MESSAGES.' WHERE `sent` >= '.$start.' AND `sent` < '.$end.' GROUP BY FROM_UNIXTIME(`day`, "%Y.%m.%d."), `to`;');
$fromusers=$this->db->query('SELECT `sent`-(`sent`%86400) as `day`,`from`,count(*) as `count`,sum(`size`) as `size` FROM '.VIEW_MESSAGES.' WHERE `sent` >= '.$start.' AND `sent` < '.$end.' GROUP BY FROM_UNIXTIME(`day`, "%Y.%m.%d."), `from`;');
$addusers=$this->db->query("INSERT INTO ".TABLE_STAT_COUNTER." (`date`,`email`,`domain`,`sent`,`recd`,`sentsize`,`recdsize`) VALUES($date,'$username','".$parts[1]."',$sent,$recd,$sizesent,$sizerecd);");
if(ENABLE_SYSLOG==1){syslog(LOG_INFO,sprintf("processed %s to %s: %d records deleted, %d records added",date("d M Y",$return['starttimestamp']),date("d M Y",$return['endtimestamp']),$return['deletedstats'],$return['addedstats']));}
$query="SELECT `email` AS `item`,MIN(`date`) as `oldest`,MAX(`date`) as `newest`,sum(`sent`) as `sent`,sum(`recd`) as `recd`,SUM(`sentsize`) as `sentsize`,AVG(`sentsize`) as `sentavg`,SUM(`recdsize`) as `recdsize`,AVG(`recdsize`) as `recdavg` FROM ".TABLE_STAT_COUNTER;
$where="WHERE `email` IN ('".implode("','",$account_for_emails)."') OR `domain` IN ('".implode("','",$account_for_domains)."')";
$group="GROUP BY `email`";
}elseif($item=='domain'){
$account_for_domains=$this->__getDomains();
$query="SELECT `domain` AS `item`,MIN(`date`) as `oldest`,MAX(`date`) as `newest`,sum(`sent`) as `sent`,sum(`recd`) as `recd`,SUM(`sentsize`) as `sentsize`,AVG(`sentsize`) as `sentavg`,SUM(`recdsize`) as `recdsize`,AVG(`recdsize`) as `recdavg` FROM ".TABLE_STAT_COUNTER;
$where="WHERE `domain` IN ('".implode("','",$account_for_domains)."')";
$query="SELECT `email` AS `item`,MIN(`date`) as `oldest`,MAX(`date`) as `newest`,sum(`sent`) as `sent`,sum(`recd`) as `recd`,sum(`sentsize`) as `sentsize`,sum(`recdsize`) as `recdsize` FROM ".TABLE_STAT_COUNTER;
if($item=='email'){
$where="WHERE `email` IN ('".implode("','",$account_for_emails)."') OR `domain` IN ('".implode("','",$account_for_domains)."')";
$group="GROUP BY `email`";
}elseif($item=='domain'){
$where="WHERE `domain` IN ('".implode("','",$account_for_domains)."')";