<commit_before><?php
/**
 * Adyen Payment Module
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * @category	Adyen
 * @package	Adyen_Payment
 * @copyright	Copyright (c) 2011 Adyen (http://www.adyen.com)
 * @license	http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
/**
 * @category   Payment Gateway
 * @package    Adyen_Payment
 * @author     Adyen
 * @property   Adyen B.V
 * @copyright  Copyright (c) 2014 Adyen BV (http://www.adyen.com)
 */

// php adyen.php -action loadBillingAgreements

require_once 'abstract.php';

class Adyen_Payments_Shell extends Mage_Shell_Abstract
{

    /**
   	 * Run script
   	 *
   	 * @return void
   	 */
   	public function run() {
   		$action = $this->getArg('action');
   		if (empty($action)) {
   			echo $this->usageHelp();
   		} else {
   			$actionMethodName = $action.'Action';
   			if (method_exists($this, $actionMethodName)) {
   				$this->$actionMethodName();
   			} else {
   				echo "Action $action not found!\n";
   				echo $this->usageHelp();
   				exit(1);
   			}
   		}
   	}

    /**
   	 * Retrieve Usage Help Message
   	 *
   	 * @return string
   	 */
   	public function usageHelp() {
   		$help = 'Available actions: ' . "\n";
   		$methods = get_class_methods($this);
   		foreach ($methods as $method) {
   			if (substr($method, -6) == 'Action') {
   				$help .= '    -action ' . substr($method, 0, -6);
   				$helpMethod = $method.'Help';
   				if (method_exists($this, $helpMethod)) {
   					$help .= $this->$helpMethod();
   				}
   				$help .= "\n";
   			}
   		}
   		return $help;
   	}


	/**
	 * Method to load all billing agreements into Magento.*
	 * @todo move to seperate model so it's easier to call internally
	 * @throws Exception
	 * @throws Mage_Core_Exception
	 */
   	public function loadBillingAgreementsAction()
	{
		$api = Mage::getSingleton('adyen/api');

		foreach (Mage::app()->getStores(true) as $store) {
            /** @var Mage_Core_Model_Store $store */
            echo sprintf("Load for store %s\n", $store->getCode());

			$customerCollection = Mage::getResourceModel('customer/customer_collection');
			$customerCollection->addFieldToFilter('store_id', $store->getId());

			$select = $customerCollection->getSelect();
			$select->reset(Varien_Db_Select::COLUMNS);
			$select->columns('e.entity_id');
			$customerCollection->joinAttribute(
				'adyen_customer_ref',
				'customer/adyen_customer_ref',
				'entity_id', null, 'left'
			);

			$customerReferences = $customerCollection->getConnection()->fetchPairs($select);
			foreach ($customerReferences as $customerId => $adyenCustomerRef) {
				$customerReference = $adyenCustomerRef ?: $customerId;

				$recurringContracts = $api->listRecurringContracts($customerReference, $store);
                echo sprintf("Found %s recurring contracts for customer %s (ref. %s)\n", count($recurringContracts), $customerId, $customerReference);

				$billingAgreementCollection = Mage::getResourceModel('adyen/billing_agreement_collection')
					->addCustomerFilter($customerId)
					->addStoreFilter($store);

				//Update the billing agreements
				foreach ($recurringContracts as $recurringContract) {
					/** @var Adyen_Payment_Model_Billing_Agreement $billingAgreement */
					$billingAgreement = $billingAgreementCollection
						->getItemByColumnValue('reference_id', $recurringContract['recurringDetailReference']);

					if (! $billingAgreement) {
						$billingAgreement = Mage::getModel('adyen/billing_agreement');
						$billingAgreement->setCustomerId($customerId);
						$billingAgreement->setStoreId($store->getId());
						$billingAgreement->setStatus($billingAgreement::STATUS_ACTIVE);
					} else {
						$billingAgreement->setStatus($billingAgreement::STATUS_ACTIVE);
						$billingAgreementCollection->removeItemByKey($billingAgreement->getId());
					}

					try {
						$billingAgreement->parseRecurringContractData($recurringContract);
						$billingAgreement->save();
					} catch (Adyen_Payment_Exception $e) {
						echo sprintf("Error while adding recurring contract data to billing agreement: %s\n", $e->getMessage());
						var_dump($recurringContract);
					} catch (Exception $e) {
						throw $e;
					}
				}

				foreach ($billingAgreementCollection as $billingAgreement) {<commit_msg>[FFM-492] B: Only retrieve Adyen billing agreements when loading agreements<commit_after><?php
/**
 * Adyen Payment Module
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * @category	Adyen
 * @package	Adyen_Payment
 * @copyright	Copyright (c) 2011 Adyen (http://www.adyen.com)
 * @license	http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
/**
 * @category   Payment Gateway
 * @package    Adyen_Payment
 * @author     Adyen
 * @property   <NAME>
 * @copyright  Copyright (c) 2014 <NAME> (http://www.adyen.com)
 */

// php adyen.php -action loadBillingAgreements

require_once 'abstract.php';

class Adyen_Payments_Shell extends Mage_Shell_Abstract
{

    /**
   	 * Run script
   	 *
   	 * @return void
   	 */
   	public function run() {
   		$action = $this->getArg('action');
   		if (empty($action)) {
   			echo $this->usageHelp();
   		} else {
   			$actionMethodName = $action.'Action';
   			if (method_exists($this, $actionMethodName)) {
   				$this->$actionMethodName();
   			} else {
   				echo "Action $action not found!\n";
   				echo $this->usageHelp();
   				exit(1);
   			}
   		}
   	}

    /**
   	 * Retrieve Usage Help Message
   	 *
   	 * @return string
   	 */
   	public function usageHelp() {
   		$help = 'Available actions: ' . "\n";
   		$methods = get_class_methods($this);
   		foreach ($methods as $method) {
   			if (substr($method, -6) == 'Action') {
   				$help .= '    -action ' . substr($method, 0, -6);
   				$helpMethod = $method.'Help';
   				if (method_exists($this, $helpMethod)) {
   					$help .= $this->$helpMethod();
   				}
   				$help .= "\n";
   			}
   		}
   		return $help;
   	}


	/**
	 * Method to load all billing agreements into Magento.*
	 * @todo move to seperate model so it's easier to call internally
	 * @throws Exception
	 * @throws Mage_Core_Exception
	 */
   	public function loadBillingAgreementsAction()
	{
		$api = Mage::getSingleton('adyen/api');

		foreach (Mage::app()->getStores(true) as $store) {
            /** @var Mage_Core_Model_Store $store */
            echo sprintf("Load for store %s\n", $store->getCode());

			$customerCollection = Mage::getResourceModel('customer/customer_collection');
			$customerCollection->addFieldToFilter('store_id', $store->getId());

			$select = $customerCollection->getSelect();
			$select->reset(Varien_Db_Select::COLUMNS);
			$select->columns('e.entity_id');
			$customerCollection->joinAttribute(
				'adyen_customer_ref',
				'customer/adyen_customer_ref',
				'entity_id', null, 'left'
			);

			$customerReferences = $customerCollection->getConnection()->fetchPairs($select);
			foreach ($customerReferences as $customerId => $adyenCustomerRef) {
				$customerReference = $adyenCustomerRef ?: $customerId;

				$recurringContracts = $api->listRecurringContracts($customerReference, $store);
                echo sprintf("Found %s recurring contracts for customer %s (ref. %s)\n", count($recurringContracts), $customerId, $customerReference);

				$billingAgreementCollection = Mage::getResourceModel('adyen/billing_agreement_collection')
					->addCustomerFilter($customerId)
					->addStoreFilter($store)
                    ->addFieldToFilter('method_code', ['like' => 'adyen_%']);

				//Update the billing agreements
				foreach ($recurringContracts as $recurringContract) {
					/** @var Adyen_Payment_Model_Billing_Agreement $billingAgreement */
					$billingAgreement = $billingAgreementCollection
						->getItemByColumnValue('reference_id', $recurringContract['recurringDetailReference']);

					if (! $billingAgreement) {
						$billingAgreement = Mage::getModel('adyen/billing_agreement');
						$billingAgreement->setCustomerId($customerId);
						$billingAgreement->setStoreId($store->getId());
						$billingAgreement->setStatus($billingAgreement::STATUS_ACTIVE);
					} else {
						$billingAgreement->setStatus($billingAgreement::STATUS_ACTIVE);
						$billingAgreementCollection->removeItemByKey($billingAgreement->getId());
					}

					try {
						$billingAgreement->parseRecurringContractData($recurringContract);
						$billingAgreement->save();
					} catch (Adyen_Payment_Exception $e) {
						echo sprintf("Error while adding recurring contract data to billing agreement: %s\n", $e->getMessage());
						var_dump($recurringContract);
					} catch (Exception $e) {
						throw $e;
					}
				}

				foreach ($billingAgreementCollection as $billingAgreement) {
{'fraction_non_alphanumeric': 0.13534698996655517, 'fraction_numerical': 0.004494147157190635, 'mean_word_length': 3.403589507593189, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1949020', 'n_tokens_mistral': 3497, 'n_tokens_neox': 2996, 'n_words': 656}