diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php
index 31e04c38d3d0..d09ac055180b 100644
--- a/CRM/Core/BAO/FinancialTrxn.php
+++ b/CRM/Core/BAO/FinancialTrxn.php
@@ -685,10 +685,10 @@ public static function createDeferredTrxn($lineItems, $contributionDetails, $upd
         }
         foreach ($results['values'] as $result) {
           if ($result['account_relationship'] == $accountRel) {
-            $trxnParams['to_financial_account_id'] = $result['financial_account_id'];
+            $trxnParams['from_financial_account_id'] = $result['financial_account_id'];
           }
           else {
-            $trxnParams['from_financial_account_id'] = $result['financial_account_id'];
+            $trxnParams['to_financial_account_id'] = $result['financial_account_id'];
           }
         }
         foreach ($deferredRevenue['revenue'] as $revenue) {
diff --git a/CRM/Report/Form/Contribute/DeferredRevenue.php b/CRM/Report/Form/Contribute/DeferredRevenue.php
index 9447082bc8c5..72f5a5e32474 100644
--- a/CRM/Report/Form/Contribute/DeferredRevenue.php
+++ b/CRM/Report/Form/Contribute/DeferredRevenue.php
@@ -302,7 +302,7 @@ public function from() {
         ON entity_financial_trxn_item.entity_id = {$this->_aliases['civicrm_financial_item']}.id
         AND entity_financial_trxn_item.entity_table = 'civicrm_financial_item'
       INNER JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn_1']}
-        ON {$this->_aliases['civicrm_financial_trxn_1']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}.id
+        ON {$this->_aliases['civicrm_financial_trxn_1']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}.id
         AND {$this->_aliases['civicrm_financial_trxn_1']}.id =  entity_financial_trxn_item.financial_trxn_id 
       INNER JOIN civicrm_entity_financial_trxn financial_trxn_contribution
         ON financial_trxn_contribution.financial_trxn_id = {$this->_aliases['civicrm_financial_trxn_1']}.id
diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
index 005a14d8c84f..c2ca619c11e0 100644
--- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
+++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
@@ -1248,4 +1248,60 @@ public function testReplaceContributionTokens() {
       <p>Contribution Receive Date: May 11th, 2015</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
   }
 
+  /**
+   * Test for contribution with deferred revenue.
+   */
+  public function testContributionWithDeferredRevenue() {
+    $contactId = $this->individualCreate();
+    Civi::settings()->set('deferred_revenue_enabled', TRUE);
+    $params = array(
+      'contact_id' => $contactId,
+      'receive_date' => '20120511',
+      'total_amount' => 100.00,
+      'financial_type_id' => 'Event Fee',
+      'trxn_id' => 12345,
+      'invoice_id' => 67890,
+      'source' => 'SSF',
+      'contribution_status_id' => 'Completed',
+      'revenue_recognition_date' => date('Ymd', strtotime("+3 month")),
+    );
+    $contribution = $this->callAPISuccess('contribution', 'create', $params);
+
+    $this->callAPISuccessGetCount('EntityFinancialTrxn', array(
+      'entity_table' => "civicrm_contribution",
+      'entity_id' => $contribution['id'],
+    ), 2);
+
+    $checkAgainst = array(
+      'financial_trxn_id.to_financial_account_id.name' => 'Deferred Revenue - Event Fee',
+      'financial_trxn_id.from_financial_account_id.name' => 'Event Fee',
+      'financial_trxn_id' => '2',
+    );
+    $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', array(
+      'return' => array(
+        "financial_trxn_id.from_financial_account_id.name",
+        "financial_trxn_id.to_financial_account_id.name",
+        "financial_trxn_id",
+      ),
+      'entity_table' => "civicrm_contribution",
+      'entity_id' => $contribution['id'],
+      'financial_trxn_id.is_payment' => 0,
+    ), $checkAgainst);
+
+    $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', array(
+      'entity_table' => "civicrm_financial_item",
+      'financial_trxn_id' => $result['financial_trxn_id'],
+      'return' => array('entity_id'),
+    ));
+
+    $checkAgainst = array(
+      'financial_account_id.name' => 'Deferred Revenue - Event Fee',
+      'id' => $result['entity_id'],
+    );
+    $result = $this->callAPISuccessGetSingle('FinancialItem', array(
+      'id' => $result['entity_id'],
+      'return' => array("financial_account_id.name"),
+    ), $checkAgainst);
+  }
+
 }