Skip to content
Snippets Groups Projects
Select Git revision
21 results Searching

Query.php

Blame
  • Forked from sdoc / BillRun
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Query.php 5.86 KiB
    <?php
    
    /**
     * @package         Billing
     * @copyright       Copyright (C) 2012-2015 S.D.O.C. LTD. All rights reserved.
     * @license         GNU Affero General Public License Version 3; see LICENSE.txt
     */
    
    /**
     * This is a parser to be used by the auto renew action.
     *
     * @author Tom Feigin
     * @todo This class is very similar to balances query, 
     * a generic query class should be created for both to implement.
     */
    class Billrun_ActionManagers_Subscribersautorenew_Query extends Billrun_ActionManagers_APIAction{
    	
    	/**
    	 * Field to hold the data to be written in the DB.
    	 * @var type Array
    	 */
    	protected $query = array();
    	
    	const DEFAULT_ERROR = "Success querying auto renew";
    	
    	/**
    	 */
    	public function __construct() {
    		parent::__construct(array('error' => self::DEFAULT_ERROR));
    		$this->collection = Billrun_Factory::db()->subscribers_auto_renew_servicesCollection();
    	}
    	
    	/**
    	 * Get a plan record according to the subscribers auto renew record.
    	 * @param Mongodloid_Entity $record
    	 * @return plan record.
    	 */
    	protected function getPlanRecord($record) {
    		$planCollection = Billrun_Factory::db()->plansCollection();
    		$planQuery = Billrun_Util::getDateBoundQuery();
    		$planQuery["name"] = $record['charging_plan_name'];
    		$planQuery["type"] = 'charging';
    		return $planCollection->query($planQuery)->cursor()->current();
    	}
    	
    	/**
    	 * Populate the plan values.
    	 * @param Mongodloid_Entity $record - Record to populate with plan values.
    	 */
    	protected function populatePlanValues(&$record) {
    		if(!isset($record['charging_plan_name'])) {
    			$errorCode = Billrun_Factory::config()->getConfigValue("autorenew_error_base") + 4;
    			$this->reportError($errorCode, Zend_Log::NOTICE);
    			return false;
    		}
    		
    		$planRecord = $this->getPlanRecord($record);
    		if($planRecord->isEmpty()) {
    			$errorCode = Billrun_Factory::config()->getConfigValue("autorenew_error_base") + 5;
    			$this->reportError($errorCode, Zend_Log::NOTICE);
    			return false;
    		}
    		
    		$record['includes'] = $this->getIncludesArray($planRecord);
    		
    		return true;
    	}
    	
    	/**
    	 * Get the 'includes' array to return for the plan record.