Merge pull request #1117 from ravishnair/CRM-13002
[civicrm-core.git] / CRM / Financial / BAO / FinancialItem.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36
37 class CRM_Financial_BAO_FinancialItem extends CRM_Financial_DAO_FinancialItem {
38
39 /**
40 * class constructor
41 */
42 function __construct( ) {
43 parent::__construct( );
44 }
45
46 /**
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
52 *
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
55 *
56 * @return object CRM_Contribute_BAO_FinancialItem object
57 * @access public
58 * @static
59 */
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;
66 }
67 return NULL;
68 }
69
70 /**
71 * function to add the financial items and financial trxn
72 *
73 * @param object $lineItem line item object
74 * @param object $contribution contribution object
75 *
76 * @access public
77 * @static
78 * @return void
79 */
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);
85 }
86 elseif ($contribution->contribution_status_id == array_search('Pending', $contributionStatuses)) {
87 $itemStatus = array_search('Unpaid', $financialItemStatus);
88 }
89 $params = array(
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,
98 );
99
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,
105 );
106
107 $result = array();
108 CRM_Financial_BAO_FinancialTypeAccount::retrieve( $searchParams, $result );
109 $params['financial_account_id'] = CRM_Utils_Array::value( 'financial_account_id', $result );
110 }
111
112 $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE);
113 $trxnId['id'] = $trxn['financialTrxnId'];
114
115 self::create($params, NULL, $trxnId);
116 }
117
118 /**
119 * function to create the financial Items and financial enity trxn
120 *
121 * @param array $params associated array to create financial items
122 * @param array $ids financial item ids
123 * @param array $trxnIds financial item ids
124 *
125 * @access public
126 * @static
127 * @return object
128 */
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'];
134 }
135
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'],
143 );
144
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'];
149 }
150 $entity_trxn->save();
151 }
152 return $financialItem;
153 }
154
155 /**
156 * takes an associative array and creates a entity financial transaction object
157 *
158 * @param array $params (reference ) an assoc array of name/value pairs
159 *
160 * @return object CRM_Core_BAO_FinancialTrxn object
161 * @access public
162 * @static
163 */
164 static function createEntityTrxn($params) {
165 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
166 $entity_trxn->copyValues($params);
167 $entity_trxn->save();
168 return $entity_trxn;
169 }
170
171 /**
172 * retrive entity financial trxn details
173 *
174 * @param array $params (reference ) an assoc array of name/value pairs
175 *
176 * @param boolean $maxID to retrive max id
177 *
178 * @return array
179 * @access public
180 * @static
181 */
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
186 if ($maxId) {
187 $financialItem->orderBy('id DESC');
188 $financialItem->limit(1);
189 }
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,
198 );
199 }
200 if (!empty($financialItems)) {
201 return $financialItems;
202 }
203 else {
204 return null;
205 }
206 }
207
208 /**
209 * check if contact is present in financial_item table
210 *
211 * CRM-12929
212 *
213 * @param array $contactIds an array contact id's
214 *
215 * @param array $error error to display
216 *
217 * @return array
218 * @access public
219 * @static
220 */
221 static function checkContactPresent($contactIds, &$error) {
222 if (empty($contactIds)) {
223 return FALSE;
224 }
225
226 $allowPermDelete = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'allowPermDeleteFinancial');
227
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);
233 if ($dao->N) {
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>";
237 }
238
239 $errorStatus = '';
240 if (is_array($error)) {
241 $errorStatus = '<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>';
242 }
243
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.');
245 return $error;
246 }
247 }
248 return FALSE;
249 }
250 }