<commit_before><?php

/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2019 BillRun Technologies Ltd. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

require_once APPLICATION_PATH . '/application/controllers/Action/Api.php';

/**
 * Balance action class of version 3
 *
 * @package  Action
 * @since    0.5
 */
class V3_discountAction extends ApiAction {

	public function execute() {
		$request = $this->getRequest();
		$accountJson = json_decode($request->get("data"), TRUE);
		$aid = $request->get("aid");
		$action = $request->get("action");
		
		$actualAid = empty($aid) ? (empty($accountJson['aid']) ? -1 : $accountJson['aid']) : $aid;
		Billrun_Factory::log()->log("Execute discount api call to " . $actualAid, Zend_Log::INFO);
		
		$options = [
			'fake_cycle' => true,
			'generate_pdf' => false,
			'output' => 'discounts',
			'aid' => $actualAid,
			'data' => $accountJson,
		];
		
		switch ($action) {
			case 'remove':
				$ret = $this->removeDiscountsFromAccount($accountJson);
				return $ret == false 
					? $this->setError($ret, $request->getRequest())
					: $this->setSuccess($ret, $request->getRequest());
			case 'query':
				$options['eligible_only'] = false;
				break;
			case 'query_eligible':
			default:
				$options['eligible_only'] = true;
		}
		
		$this->forward('generateExpected', $options);
		return false;
	}
	
	/**
	 * remove discount lines by stamps received
	 * 
	 * @param type $params
	 */
	protected function removeDiscountsFromAccount($data) {
		if (!is_array($data)) {
			return FALSE;
		}
		foreach ($data as $stamp) {
			if (!(gettype($stamp) == 'string' && strlen($stamp) == 32)) {
				return FALSE;
			}
		}

		Billrun_Discount::remove($data);
		return $data;
	}

}<commit_msg>BRCD-1870 - V3 discount API issue<commit_after><?php

/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2019 BillRun Technologies Ltd. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

require_once APPLICATION_PATH . '/application/controllers/Action/Api.php';

/**
 * Balance action class of version 3
 *
 * @package  Action
 * @since    0.5
 */
class V3_discountAction extends ApiAction {
	
	use Billrun_Traits_Api_UserPermissions;

	public function execute() {
		$request = $this->getRequest();
		$accountJson = json_decode($request->get("data"), TRUE);
		$aid = $request->get("aid");
		$action = $request->get("action");
		if (!empty($action) && $action == 'remove') {
			$this->permissionLevel = Billrun_Traits_Api_IUserPermissions::PERMISSION_WRITE;
		}
		$this->allowed();
		$actualAid = empty($aid) ? (empty($accountJson['aid']) ? -1 : $accountJson['aid']) : $aid;
		Billrun_Factory::log()->log("Execute discount api call to " . $actualAid, Zend_Log::INFO);
		
		$options = [
			'fake_cycle' => true,
			'generate_pdf' => false,
			'output' => 'discounts',
			'aid' => $actualAid,
			'data' => $accountJson,
		];
		
		switch ($action) {
			case 'remove':
				$ret = $this->removeDiscountsFromAccount($accountJson);
				return $ret == false 
					? $this->setError($ret, $request->getRequest())
					: $this->setSuccess($ret, $request->getRequest());
			case 'query':
				$options['eligible_only'] = false;
				break;
			case 'query_eligible':
			default:
				$options['eligible_only'] = true;
		}
		
		$this->forward('generateExpected', $options);
		return false;
	}
	
	/**
	 * remove discount lines by stamps received
	 * 
	 * @param type $params
	 */
	protected function removeDiscountsFromAccount($data) {
		if (!is_array($data)) {
			return FALSE;
		}
		foreach ($data as $stamp) {
			if (!(gettype($stamp) == 'string' && strlen($stamp) == 32)) {
				return FALSE;
			}
		}

		Billrun_Discount::remove($data);
		return $data;
	}
	
	protected function getPermissionLevel() {
		return Billrun_Traits_Api_IUserPermissions::PERMISSION_READ;
	}

}
{'fraction_non_alphanumeric': 0.16565040650406504, 'fraction_numerical': 0.009400406504065041, 'mean_word_length': 3.6869047619047617, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2485756', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1265, 'n_words': 284}