4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Financial_BAO_FinancialItem
extends CRM_Financial_DAO_FinancialItem
{
42 function __construct( ) {
43 parent
::__construct( );
47 * Takes a bunch of params that are needed to match certain criteria and
48 * retrieves the relevant objects. Typically the valid params are only
49 * contact_id. We'll tweak this function to be more full featured over a period
50 * of time. This is the inverse function of create. It also stores all the retrieved
51 * values in the default array
53 * @param array $params (reference ) an assoc array of name/value pairs
54 * @param array $defaults (reference ) an assoc array to hold the flattened values
56 * @return object CRM_Contribute_BAO_FinancialItem object
60 static function retrieve(&$params, &$defaults) {
61 $financialItem = new CRM_Financial_DAO_FinancialItem();
62 $financialItem->copyValues($params);
63 if ($financialItem->find(TRUE)) {
64 CRM_Core_DAO
::storeValues($financialItem, $defaults);
65 return $financialItem;
71 * function to add the financial items and financial trxn
73 * @param object $lineItem line item object
74 * @param object $contribution contribution object
80 static function add($lineItem, $contribution) {
81 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
82 $financialItemStatus = CRM_Core_PseudoConstant
::accountOptionValues('financial_item_status');
83 if ($contribution->contribution_status_id
== array_search('Completed', $contributionStatuses)) {
84 $itemStatus = array_search('Paid', $financialItemStatus);
86 elseif ($contribution->contribution_status_id
== array_search('Pending', $contributionStatuses)) {
87 $itemStatus = array_search('Unpaid', $financialItemStatus);
90 'transaction_date' => CRM_Utils_Date
::isoToMysql($contribution->receive_date
),
91 'contact_id' => $contribution->contact_id
,
92 'amount' => $lineItem->line_total
,
93 'currency' => $contribution->currency
,
94 'entity_table' => 'civicrm_line_item',
95 'entity_id' => $lineItem->id
,
96 'description' => ( $lineItem->qty
!= 1 ?
$lineItem->qty
. ' of ' : ''). ' ' . $lineItem->label
,
97 'status_id' => $itemStatus,
100 if ($lineItem->financial_type_id
) {
101 $searchParams = array(
102 'entity_table' => 'civicrm_financial_type',
103 'entity_id' => $lineItem->financial_type_id
,
104 'account_relationship' => 1,
108 CRM_Financial_BAO_FinancialTypeAccount
::retrieve( $searchParams, $result );
109 $params['financial_account_id'] = CRM_Utils_Array
::value( 'financial_account_id', $result );
112 $trxn = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution->id
, 'ASC', TRUE);
113 $trxnId['id'] = $trxn['financialTrxnId'];
115 self
::create($params, NULL, $trxnId);
119 * function to create the financial Items and financial enity trxn
121 * @param array $params associated array to create financial items
122 * @param array $ids financial item ids
123 * @param array $trxnIds financial item ids
129 static function create(&$params, $ids = NULL, $trxnIds = NULL) {
130 $financialItem = new CRM_Financial_DAO_FinancialItem();
131 $financialItem->copyValues($params);
132 if (CRM_Utils_Array
::value('id', $ids)) {
133 $financialItem->id
= $ids['id'];
136 $financialItem->save();
137 if (CRM_Utils_Array
::value('id', $trxnIds)) {
138 $entity_financial_trxn_params = array(
139 'entity_table' => "civicrm_financial_item",
140 'entity_id' => $financialItem->id
,
141 'financial_trxn_id' => $trxnIds['id'],
142 'amount' => $params['amount'],
145 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
146 $entity_trxn->copyValues($entity_financial_trxn_params);
147 if (CRM_Utils_Array
::value('entityFinancialTrxnId', $ids)) {
148 $entity_trxn->id
= $ids['entityFinancialTrxnId'];
150 $entity_trxn->save();
152 return $financialItem;
156 * takes an associative array and creates a entity financial transaction object
158 * @param array $params (reference ) an assoc array of name/value pairs
160 * @return object CRM_Core_BAO_FinancialTrxn object
164 static function createEntityTrxn($params) {
165 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
166 $entity_trxn->copyValues($params);
167 $entity_trxn->save();
172 * retrive entity financial trxn details
174 * @param array $params (reference ) an assoc array of name/value pairs
176 * @param boolean $maxID to retrive max id
182 static function retrieveEntityFinancialTrxn($params, $maxId = FALSE) {
183 $financialItem = new CRM_Financial_DAO_EntityFinancialTrxn();
184 $financialItem->copyValues($params);
185 //retrieve last entry from civicrm_entity_financial_trxn
187 $financialItem->orderBy('id DESC');
188 $financialItem->limit(1);
190 $financialItem->find();
191 while ($financialItem->fetch()) {
192 $financialItems[$financialItem->id
] = array(
193 'id' => $financialItem->id
,
194 'entity_table' => $financialItem->entity_table
,
195 'entity_id' => $financialItem->entity_id
,
196 'financial_trxn_id' => $financialItem->financial_trxn_id
,
197 'amount' => $financialItem->amount
,
200 if (!empty($financialItems)) {
201 return $financialItems;
209 * check if contact is present in financial_item table
213 * @param array $contactIds an array contact id's
215 * @param array $error error to display
221 static function checkContactPresent($contactIds, &$error) {
222 if (empty($contactIds)) {
226 $allowPermDelete = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'allowPermDeleteFinancial');
228 if (!$allowPermDelete) {
229 $sql = 'SELECT DISTINCT(cc.id), cc.display_name FROM civicrm_contact cc
230 INNER JOIN civicrm_contribution con ON con.contact_id = cc.id
231 WHERE cc.id IN (' . implode (',', $contactIds) . ') AND con.is_test = 0';
232 $dao = CRM_Core_DAO
::executeQuery($sql);
234 while ($dao->fetch()) {
235 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid=$dao->id");
236 $not_deleted[$dao->id
] = "<a href='$url'>$dao->display_name</a>";
240 if (is_array($error)) {
241 $errorStatus = '<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>';
244 $error['_qf_default'] = $errorStatus . ts('This contact(s) can not be permanently deleted because the contact record is linked to one or more live financial transactions. Deleting this contact would result in the loss of financial data.');