3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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_Financial_BAO_FinancialItem
extends CRM_Financial_DAO_FinancialItem
{
41 function __construct( ) {
42 parent
::__construct( );
46 * Takes a bunch of params that are needed to match certain criteria and
47 * retrieves the relevant objects. Typically the valid params are only
48 * contact_id. We'll tweak this function to be more full featured over a period
49 * of time. This is the inverse function of create. It also stores all the retrieved
50 * values in the default array
52 * @param array $params (reference ) an assoc array of name/value pairs
53 * @param array $defaults (reference ) an assoc array to hold the flattened values
55 * @return object CRM_Contribute_BAO_FinancialItem object
59 static function retrieve(&$params, &$defaults) {
60 $financialItem = new CRM_Financial_DAO_FinancialItem();
61 $financialItem->copyValues($params);
62 if ($financialItem->find(TRUE)) {
63 CRM_Core_DAO
::storeValues($financialItem, $defaults);
64 return $financialItem;
70 * function to add the financial items and financial trxn
72 * @param object $lineItem line item object
73 * @param object $contribution contribution object
79 static function add($lineItem, $contribution) {
80 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
81 $financialItemStatus = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_FinancialItem', 'status_id');
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 ||
$contribution->contribution_status_id
== array_search('In Progress', $contributionStatuses)) {
88 $itemStatus = array_search('Unpaid', $financialItemStatus);
90 elseif ($contribution->contribution_status_id
== array_search('Partially paid', $contributionStatuses)) {
91 $itemStatus = array_search('Partially paid', $financialItemStatus);
94 'transaction_date' => CRM_Utils_Date
::isoToMysql($contribution->receive_date
),
95 'contact_id' => $contribution->contact_id
,
96 'amount' => $lineItem->line_total
,
97 'currency' => $contribution->currency
,
98 'entity_table' => 'civicrm_line_item',
99 'entity_id' => $lineItem->id
,
100 'description' => ( $lineItem->qty
!= 1 ?
$lineItem->qty
. ' of ' : ''). ' ' . $lineItem->label
,
101 'status_id' => $itemStatus,
104 if ($lineItem->financial_type_id
) {
105 $searchParams = array(
106 'entity_table' => 'civicrm_financial_type',
107 'entity_id' => $lineItem->financial_type_id
,
108 'account_relationship' => 1,
112 CRM_Financial_BAO_FinancialTypeAccount
::retrieve( $searchParams, $result );
113 $params['financial_account_id'] = CRM_Utils_Array
::value( 'financial_account_id', $result );
116 $trxn = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution->id
, 'ASC', TRUE);
117 $trxnId['id'] = $trxn['financialTrxnId'];
118 return self
::create($params, NULL, $trxnId);
122 * function to create the financial Items and financial enity trxn
124 * @param array $params associated array to create financial items
125 * @param array $ids financial item ids
126 * @param array $trxnIds financial item ids
132 static function create(&$params, $ids = NULL, $trxnIds = NULL) {
133 $financialItem = new CRM_Financial_DAO_FinancialItem();
134 $financialItem->copyValues($params);
135 if (!empty($ids['id'])) {
136 $financialItem->id
= $ids['id'];
139 $financialItem->save();
140 if (!empty($trxnIds['id'])) {
141 $entity_financial_trxn_params = array(
142 'entity_table' => "civicrm_financial_item",
143 'entity_id' => $financialItem->id
,
144 'financial_trxn_id' => $trxnIds['id'],
145 'amount' => $params['amount'],
148 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
149 $entity_trxn->copyValues($entity_financial_trxn_params);
150 if (!empty($ids['entityFinancialTrxnId'])) {
151 $entity_trxn->id
= $ids['entityFinancialTrxnId'];
153 $entity_trxn->save();
155 return $financialItem;
159 * takes an associative array and creates a entity financial transaction object
161 * @param array $params (reference ) an assoc array of name/value pairs
163 * @return object CRM_Core_BAO_FinancialTrxn object
167 static function createEntityTrxn($params) {
168 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
169 $entity_trxn->copyValues($params);
170 $entity_trxn->save();
175 * retrive entity financial trxn details
177 * @param array $params (reference ) an assoc array of name/value pairs
179 * @param boolean $maxID to retrive max id
185 static function retrieveEntityFinancialTrxn($params, $maxId = FALSE) {
186 $financialItem = new CRM_Financial_DAO_EntityFinancialTrxn();
187 $financialItem->copyValues($params);
188 //retrieve last entry from civicrm_entity_financial_trxn
190 $financialItem->orderBy('id DESC');
191 $financialItem->limit(1);
193 $financialItem->find();
194 while ($financialItem->fetch()) {
195 $financialItems[$financialItem->id
] = array(
196 'id' => $financialItem->id
,
197 'entity_table' => $financialItem->entity_table
,
198 'entity_id' => $financialItem->entity_id
,
199 'financial_trxn_id' => $financialItem->financial_trxn_id
,
200 'amount' => $financialItem->amount
,
203 if (!empty($financialItems)) {
204 return $financialItems;
212 * check if contact is present in financial_item table
216 * @param array $contactIds an array contact id's
218 * @param array $error error to display
224 static function checkContactPresent($contactIds, &$error) {
225 if (empty($contactIds)) {
229 $allowPermDelete = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'allowPermDeleteFinancial');
231 if (!$allowPermDelete) {
232 $sql = 'SELECT DISTINCT(cc.id), cc.display_name FROM civicrm_contact cc
233 INNER JOIN civicrm_contribution con ON con.contact_id = cc.id
234 WHERE cc.id IN (' . implode (',', $contactIds) . ') AND con.is_test = 0';
235 $dao = CRM_Core_DAO
::executeQuery($sql);
237 while ($dao->fetch()) {
238 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid=$dao->id");
239 $not_deleted[$dao->id
] = "<a href='$url'>$dao->display_name</a>";
243 if (is_array($error)) {
244 $errorStatus = '<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>';
247 $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.');