return FALSE;
}
+ /**
+ * Validate Financial Type has Deferred Revenue account relationship
+ * with Financial Account
+ *
+ * @param array $params
+ *
+ * @param int $contributionID
+ *
+ * @param obj $form
+ *
+ * @return string
+ *
+ */
+ public static function checkFinancialTypeHasDeferred($params, $contributionID = NULL, $form = NULL) {
+ if (!CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
+ return FALSE;
+ }
+ $recognitionDate = CRM_Utils_Array::value('revenue_recognition_date', $params);
+ if (!(!CRM_Utils_System::isNull($recognitionDate)
+ || ($contributionID && $params['prevContribution']->revenue_recognition_date))
+ ) {
+ return FALSE;
+ }
+
+ $message = ts('Revenue recognition date can only be specified if the financial type selected has a deferred revenue account configured. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts');
+ $lineItems = CRM_Utils_Array::value('line_item', $params);
+ $financialTypeID = CRM_Utils_Array::value('financial_type_id', $params);
+ if (!$financialTypeID) {
+ $financialTypeID = $params['prevContribution']->financial_type_id;
+ }
+ if (($contributionID || !empty($params['price_set_id'])) && empty($lineItems)) {
+ if (!$contributionID) {
+ CRM_Price_BAO_PriceSet::processAmount($form->_priceSet['fields'],
+ $params, $items);
+ }
+ else {
+ $items = CRM_Price_BAO_LineItem::getLineItems($contributionID, 'contribution', TRUE, TRUE, TRUE);
+ }
+ if (!empty($items)) {
+ $lineItems[] = $items;
+ }
+ }
+ $deferredFinancialType = self::getDeferredFinancialType();
+ if (!empty($lineItems)) {
+ foreach ($lineItems as $lineItem) {
+ foreach ($lineItem as $items) {
+ if (!array_key_exists($items['financial_type_id'], $deferredFinancialType)) {
+ return $message;
+ }
+ }
+ }
+ }
+ elseif (!array_key_exists($financialTypeID, $deferredFinancialType)) {
+ return $message;
+ }
+ return FALSE;
+ }
+
}
$this->assertFalse($message, "The financial account can be deleted. Failed asserting this was true.");
}
+ /**
+ * Test for validating financial type has deferred revenue account relationship.
+ */
+ public function testcheckFinancialTypeHasDeferred() {
+ Civi::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
+ $params = array();
+ $valid = CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($params);
+ $this->assertFalse($valid, "This should have been false");
+ $cid = $this->individualCreate();
+ $params = array(
+ 'contact_id' => $cid,
+ 'receive_date' => '2010-01-20',
+ 'total_amount' => 100,
+ 'financial_type_id' => 3,
+ 'revenue_recognition_date' => date('Ymd', strtotime("+1 month")),
+ 'line_items' => array(
+ array(
+ 'line_item' => array(
+ array(
+ 'entity_table' => 'civicrm_contribution',
+ 'price_field_id' => 8,
+ 'price_field_value_id' => 16,
+ 'label' => 'test 1',
+ 'qty' => 1,
+ 'unit_price' => 100,
+ 'line_total' => 100,
+ ),
+ array(
+ 'entity_table' => 'civicrm_contribution',
+ 'price_field_id' => 8,
+ 'price_field_value_id' => 17,
+ 'label' => 'Test 2',
+ 'qty' => 1,
+ 'unit_price' => 200,
+ 'line_total' => 200,
+ 'financial_type_id' => 1,
+ ),
+ ),
+ 'params' => array(),
+ ),
+ ),
+ );
+ $contribution = CRM_Contribute_BAO_Contribution::create($params);
+ $valid = CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($params, $contribution->id);
+ $message = "Revenue recognition date can only be specified if the financial type selected has a deferred revenue account configured. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts";
+ $this->assertEquals($valid, $message, "The messages do not match");
+ }
+
}