3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Core_BAO_FinancialTrxn
extends CRM_Financial_DAO_FinancialTrxn
{
37 function __construct() {
38 parent
::__construct();
42 * takes an associative array and creates a financial transaction object
44 * @param array $params (reference ) an assoc array of name/value pairs
46 * @param string $trxnEntityTable entity_table
48 * @return object CRM_Core_BAO_FinancialTrxn object
52 static function create(&$params, $trxnEntityTable = null ) {
53 $trxn = new CRM_Financial_DAO_FinancialTrxn();
54 $trxn->copyValues($params);
56 if (!CRM_Utils_Rule
::currencyCode($trxn->currency
)) {
57 $config = CRM_Core_Config
::singleton();
58 $trxn->currency
= $config->defaultCurrency
;
63 // save to entity_financial_trxn table
64 $entityFinancialTrxnParams =
66 'entity_table' => "civicrm_contribution",
67 'financial_trxn_id' => $trxn->id
,
68 'amount' => $params['total_amount'],
69 'currency' => $trxn->currency
,
72 if (!empty($trxnEntityTable)) {
73 $entityFinancialTrxnParams['entity_table'] = $trxnEntityTable['entity_table'];
74 $entityFinancialTrxnParams['entity_id'] = $trxnEntityTable['entity_id'];
77 $entityFinancialTrxnParams['entity_id'] = $params['contribution_id'];
80 $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
81 $entityTrxn->copyValues($entityFinancialTrxnParams);
87 * Takes a bunch of params that are needed to match certain criteria and
88 * retrieves the relevant objects. Typically the valid params are only
89 * contact_id. We'll tweak this function to be more full featured over a period
90 * of time. This is the inverse function of create. It also stores all the retrieved
91 * values in the default array
93 * @param array $params (reference ) an assoc array of name/value pairs
94 * @param array $defaults (reference ) an assoc array to hold the flattened values
96 * @return object CRM_Contribute_BAO_ContributionType object
100 static function retrieve( &$params, &$defaults ) {
101 $financialItem = new CRM_Financial_DAO_FinancialTrxn( );
102 $financialItem->copyValues($params);
103 if ($financialItem->find(true)) {
104 CRM_Core_DAO
::storeValues( $financialItem, $defaults );
105 return $financialItem;
112 * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record.
113 * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object.
115 * @param string $entityTable name of the entity table usually 'civicrm_contact'
116 * @param int $entityID id of the entity usually the contactID.
117 * @param string $orderBy to get single trxn id for a entity table i.e last or first.
119 * @return array( ) reference $tag array of catagory id's the contact belongs to.
124 static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE) {
125 $ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL);
129 $condition = " AND ((ceft1.entity_table IS NOT NULL) OR (cft.payment_instrument_id IS NOT NULL AND ceft1.entity_table IS NULL)) ";
131 $query = "SELECT ceft.id, ceft.financial_trxn_id FROM `civicrm_financial_trxn` cft
132 LEFT JOIN civicrm_entity_financial_trxn ceft
133 ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution'
134 LEFT JOIN civicrm_entity_financial_trxn ceft1
135 ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item'
136 LEFT JOIN civicrm_financial_item cfi ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id
137 WHERE ceft.entity_id = %1 AND (cfi.entity_table <> 'civicrm_financial_trxn' or cfi.entity_table is NULL)
139 ORDER BY cft.id {$orderBy}
142 $params = array(1 => array($entity_id, 'Integer'));
143 $dao = CRM_Core_DAO
::executeQuery($query, $params);
145 $ids['entityFinancialTrxnId'] = $dao->id
;
146 $ids['financialTrxnId'] = $dao->financial_trxn_id
;
152 * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record.
153 * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object.
155 * @param string $entityTable name of the entity table usually 'civicrm_contact'
156 * @param int $entityID id of the entity usually the contactID.
158 * @return array( ) reference $tag array of catagory id's the contact belongs to.
163 static function getFinancialTrxnTotal($entity_id) {
165 SELECT (ft.amount+SUM(ceft.amount)) AS total FROM civicrm_entity_financial_trxn AS ft
166 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.financial_trxn_id = ceft.entity_id
167 WHERE ft.entity_table = 'civicrm_contribution' AND ft.entity_id = %1
170 $sqlParams = array(1 => array($entity_id, 'Integer'));
171 return CRM_Core_DAO
::singleValueQuery($query, $sqlParams);
175 * Given an financial_trxn_id check for previous entity_financial_trxn.
177 * @param int $financialTrxn_id id of the latest payment.
179 * @return array( ) $payment array of previous payments
184 static function getPayments($financial_trxn_id) {
186 SELECT ef1.financial_trxn_id, sum(ef1.amount) amount
187 FROM civicrm_entity_financial_trxn ef1
188 LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.financial_trxn_id = ef2.entity_id
189 WHERE ef2.financial_trxn_id =%1
190 AND ef2.entity_table = 'civicrm_financial_trxn'
191 AND ef1.entity_table = 'civicrm_financial_item'
192 GROUP BY ef1.financial_trxn_id
194 SELECT ef1.financial_trxn_id, ef1.amount
195 FROM civicrm_entity_financial_trxn ef1
196 LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.entity_id = ef2.entity_id
197 WHERE ef2.financial_trxn_id =%1
198 AND ef2.entity_table = 'civicrm_financial_trxn'
199 AND ef1.entity_table = 'civicrm_financial_trxn'";
201 $sqlParams = array(1 => array($financial_trxn_id, 'Integer'));
202 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
205 while ($dao->fetch()) {
206 $result[$i]['financial_trxn_id'] = $dao->financial_trxn_id
;
207 $result[$i]['amount'] = $dao->amount
;
211 if (empty($result)) {
212 $query = "SELECT sum( amount ) amount FROM civicrm_entity_financial_trxn WHERE financial_trxn_id =%1 AND entity_table = 'civicrm_financial_item'";
213 $sqlParams = array(1 => array($financial_trxn_id, 'Integer'));
214 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
217 $result[0]['financial_trxn_id'] = $financial_trxn_id;
218 $result[0]['amount'] = $dao->amount
;
225 * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record.
226 * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object.
228 * @param string $entityTable name of the entity table usually 'civicrm_contact'
229 * @param int $entityID id of the entity usually the contactID.
231 * @return array( ) reference $tag array of catagory id's the contact belongs to.
236 static function getFinancialTrxnLineTotal($entity_id, $entity_table = 'civicrm_contribution') {
237 $query = "SELECT lt.price_field_value_id AS id, ft.financial_trxn_id,ft.amount AS amount FROM civicrm_entity_financial_trxn AS ft
238 LEFT JOIN civicrm_financial_item AS fi ON fi.id = ft.entity_id AND fi.entity_table = 'civicrm_line_item' AND ft.entity_table = 'civicrm_financial_item'
239 LEFT JOIN civicrm_line_item AS lt ON lt.id = fi.entity_id AND lt.entity_table = %2
240 WHERE lt.entity_id = %1 ";
242 $sqlParams = array(1 => array($entity_id, 'Integer'), 2 => array($entity_table, 'String'));
243 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
244 while($dao->fetch()){
245 $result[$dao->financial_trxn_id
][$dao->id
] = $dao->amount
;
247 if (!empty($result)) {
256 * Delete financial transaction
258 * @return true on success, false otherwise
262 static function deleteFinancialTrxn($entity_id) {
263 $query = "DELETE ceft1, cfi, ceft, cft FROM `civicrm_financial_trxn` cft
264 LEFT JOIN civicrm_entity_financial_trxn ceft
265 ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution'
266 LEFT JOIN civicrm_entity_financial_trxn ceft1
267 ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item'
268 LEFT JOIN civicrm_financial_item cfi
269 ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id
270 WHERE ceft.entity_id = %1";
271 CRM_Core_DAO
::executeQuery($query, array(1 => array($entity_id, 'Integer')));
276 * create financial transaction for premium
281 static function createPremiumTrxn($params) {
282 if ((!CRM_Utils_Array
::value('financial_type_id', $params) ||
!CRM_Utils_Array
::value('contributionId', $params)) && !CRM_Utils_Array
::value('oldPremium', $params)) {
286 if (CRM_Utils_Array
::value('cost', $params)) {
287 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
288 $financialAccountType = CRM_Contribute_PseudoConstant
::financialAccountType($params['financial_type_id']);
289 $accountRelationship = CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND label IN ('Premiums Inventory Account is', 'Cost of Sales Account is')");
290 $toFinancialAccount = CRM_Utils_Array
::value('isDeleted', $params) ?
'Premiums Inventory Account is' : 'Cost of Sales Account is';
291 $fromFinancialAccount = CRM_Utils_Array
::value('isDeleted', $params) ?
'Cost of Sales Account is': 'Premiums Inventory Account is';
292 $accountRelationship = array_flip($accountRelationship);
293 $financialtrxn = array(
294 'to_financial_account_id' => $financialAccountType[$accountRelationship[$toFinancialAccount]],
295 'from_financial_account_id' => $financialAccountType[$accountRelationship[$fromFinancialAccount]],
296 'trxn_date' => date('YmdHis'),
297 'total_amount' => CRM_Utils_Array
::value('cost', $params) ?
$params['cost'] : 0,
298 'currency' => CRM_Utils_Array
::value('currency', $params),
299 'status_id' => array_search('Completed', $contributionStatuses)
301 $trxnEntityTable['entity_table'] = 'civicrm_contribution';
302 $trxnEntityTable['entity_id'] = $params['contributionId'];
303 CRM_Core_BAO_FinancialTrxn
::create($financialtrxn, $trxnEntityTable);
306 if (CRM_Utils_Array
::value('oldPremium', $params)) {
307 $premiumParams = array(
308 'id' => $params['oldPremium']['product_id']
310 $productDetails = array();
311 CRM_Contribute_BAO_ManagePremiums
::retrieve($premiumParams, $productDetails);
313 'cost' => CRM_Utils_Array
::value('cost', $productDetails),
314 'currency' => CRM_Utils_Array
::value('currency', $productDetails),
315 'financial_type_id' => CRM_Utils_Array
::value('financial_type_id', $productDetails),
316 'contributionId' => $params['oldPremium']['contribution_id'],
319 CRM_Core_BAO_FinancialTrxn
::createPremiumTrxn($params);
323 * create financial trxn and items when fee is charged
325 * @params params to create trxn entries
331 static function recordFees($params) {
332 $expenseTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
333 $domainId = CRM_Core_Config
::domainID();
335 if (CRM_Utils_Array
::value('prevContribution', $params)) {
336 $amount = $params['prevContribution']->fee_amount
;
338 $amount = $params['fee_amount'] - $amount;
342 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccountType($params['financial_type_id'], $expenseTypeId);
343 $params['trxnParams']['from_financial_account_id'] = $params['to_financial_account_id'];
344 $params['trxnParams']['to_financial_account_id'] = $financialAccount;
345 $params['trxnParams']['total_amount'] = $amount;
346 $params['trxnParams']['fee_amount'] =
347 $params['trxnParams']['net_amount'] = 0;
348 $params['trxnParams']['status_id'] = CRM_Core_OptionGroup
::getValue('contribution_status','Completed','name');
349 $params['trxnParams']['contribution_id'] = isset($params['contribution']->id
) ?
$params['contribution']->id
: $params['contribution_id'];
350 $trxn = self
::create($params['trxnParams']);
351 if (!CRM_Utils_Array
::value('entity_id', $params)) {
352 $financialTrxnID = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['trxnParams']['contribution_id'], 'DESC');
353 $params['entity_id'] = $financialTrxnID['financialTrxnId'];
357 'financial_account_id' => $financialAccount,
358 'contact_id' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain', $domainId, 'contact_id'),
359 'created_date' => date('YmdHis'),
360 'transaction_date' => date('YmdHis'),
362 'description' => 'Fee',
363 'status_id' => CRM_Core_OptionGroup
::getValue('financial_item_status','Paid','name'),
364 'entity_table' => 'civicrm_financial_trxn',
365 'entity_id' => $params['entity_id'],
366 'currency' => $params['trxnParams']['currency'],
368 $trxnIDS['id'] = $trxn->id
;
369 $financialItem = CRM_Financial_BAO_FinancialItem
::create($fItemParams, NULL, $trxnIDS);