<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * Trim ADSL Traffic Records, truncating any zeros
 *
 * @package    ADSL
 * @category   Tasks
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Task_Adsl_Traffictrim extends Task_Adsl_Trafficget {
	protected function _execute(array $params) {
		$t = ORM::factory('Service_Plugin_Adsl_Traffic')
			->select('service,supplier_id')
			->group_by('service,supplier_id');

		foreach ($t->find_all() as $group) {

			$zerorow = FALSE;
			foreach (ORM::factory('Service_Plugin_Adsl_Traffic')->where('service','=',$group->service)->where('supplier_id','=',$group->supplier_id)->order_by('date')->order_by('time')->find_all() as $spato)
				if ($spato->total() === 0) {
					if ($zerorow) {
						// Cant use delete(), as our primary key is not used in this table.
						$db = DB::query(Database::DELETE,DB::delete($spato->table_name())
							->where('service','=',$group->service)
							->where('supplier_id','=',$group->supplier_id)
							->where('date','=',$spato->date)
							->where('time','=',$spato->time));

						$db->execute();
					}

					$zerorow = TRUE;

				} else
					$zerorow = FALSE;

			// An attempt to free some memory.
			$spato = NULL;
		}
	}
}
?>