INFRA-132 - Fix spacing of @return tag in comments
[civicrm-core.git] / CRM / Financial / BAO / FinancialItem.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 class CRM_Financial_BAO_FinancialItem extends CRM_Financial_DAO_FinancialItem {
37
38 /**
39 * Class constructor
40 */
41 public function __construct() {
42 parent::__construct();
43 }
44
45 /**
46 * Fetch object based on array of properties
47 *
48 * @param array $params
49 * (reference ) an assoc array of name/value pairs.
50 * @param array $defaults
51 * (reference ) an assoc array to hold the flattened values.
52 *
53 * @return CRM_Contribute_BAO_FinancialItem object
54 * @static
55 */
56 public static function retrieve(&$params, &$defaults) {
57 $financialItem = new CRM_Financial_DAO_FinancialItem();
58 $financialItem->copyValues($params);
59 if ($financialItem->find(TRUE)) {
60 CRM_Core_DAO::storeValues($financialItem, $defaults);
61 return $financialItem;
62 }
63 return NULL;
64 }
65
66 /**
67 * Add the financial items and financial trxn
68 *
69 * @param object $lineItem
70 * Line item object.
71 * @param object $contribution
72 * Contribution object.
73 * @param bool $taxTrxnID
74 *
75 * @static
76 * @return void
77 */
78 public static function add($lineItem, $contribution, $taxTrxnID = FALSE) {
79 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
80 $financialItemStatus = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialItem', 'status_id');
81 $itemStatus = NULL;
82 if ($contribution->contribution_status_id == array_search('Completed', $contributionStatuses)
83 || $contribution->contribution_status_id == array_search('Pending refund', $contributionStatuses)) {
84 $itemStatus = array_search('Paid', $financialItemStatus);
85 }
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);
89 }
90 elseif ($contribution->contribution_status_id == array_search('Partially paid', $contributionStatuses)) {
91 $itemStatus = array_search('Partially paid', $financialItemStatus);
92 }
93 $params = array(
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,
102 );
103
104 if ($taxTrxnID) {
105 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
106 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
107 $params['amount'] = $lineItem->tax_amount;
108 $params['description'] = $taxTerm;
109 $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
110 }
111 else {
112 $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
113 }
114 if ($lineItem->financial_type_id) {
115 $searchParams = array(
116 'entity_table' => 'civicrm_financial_type',
117 'entity_id' => $lineItem->financial_type_id,
118 'account_relationship' => $accountRel,
119 );
120
121 $result = array();
122 CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result);
123 $params['financial_account_id'] = CRM_Utils_Array::value('financial_account_id', $result);
124 }
125 $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE);
126 $trxnId['id'] = $trxn['financialTrxnId'];
127
128 return self::create($params, NULL, $trxnId);
129 }
130
131 /**
132 * Create the financial Items and financial enity trxn
133 *
134 * @param array $params
135 * Associated array to create financial items.
136 * @param array $ids
137 * Financial item ids.
138 * @param array $trxnIds
139 * Financial item ids.
140 *
141 * @static
142 * @return object
143 */
144 public static function create(&$params, $ids = NULL, $trxnIds = NULL) {
145 $financialItem = new CRM_Financial_DAO_FinancialItem();
146
147 if (!empty($ids['id'])) {
148 CRM_Utils_Hook::pre('edit', 'FinancialItem', $ids['id'], $params);
149 }
150 else {
151 CRM_Utils_Hook::pre('create', 'FinancialItem', NULL, $params);
152 }
153
154 $financialItem->copyValues($params);
155 if (!empty($ids['id'])) {
156 $financialItem->id = $ids['id'];
157 }
158
159 $financialItem->save();
160 if (!empty($trxnIds['id'])) {
161 $entity_financial_trxn_params = array(
162 'entity_table' => "civicrm_financial_item",
163 'entity_id' => $financialItem->id,
164 'financial_trxn_id' => $trxnIds['id'],
165 'amount' => $params['amount'],
166 );
167
168 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
169 $entity_trxn->copyValues($entity_financial_trxn_params);
170 if (!empty($ids['entityFinancialTrxnId'])) {
171 $entity_trxn->id = $ids['entityFinancialTrxnId'];
172 }
173 $entity_trxn->save();
174 }
175 if (!empty($ids['id'])) {
176 CRM_Utils_Hook::post('edit', 'FinancialItem', $financialItem->id, $financialItem);
177 }
178 else {
179 CRM_Utils_Hook::post('create', 'FinancialItem', $financialItem->id, $financialItem);
180 }
181 return $financialItem;
182 }
183
184 /**
185 * Takes an associative array and creates a entity financial transaction object
186 *
187 * @param array $params
188 * (reference ) an assoc array of name/value pairs.
189 *
190 * @return CRM_Core_BAO_FinancialTrxn object
191 * @static
192 */
193 public static function createEntityTrxn($params) {
194 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
195 $entity_trxn->copyValues($params);
196 $entity_trxn->save();
197 return $entity_trxn;
198 }
199
200 /**
201 * Retrive entity financial trxn details
202 *
203 * @param array $params
204 * (reference ) an assoc array of name/value pairs.
205 * @param bool $maxId
206 * To retrive max id.
207 *
208 * @return array
209 * @static
210 */
211 public static function retrieveEntityFinancialTrxn($params, $maxId = FALSE) {
212 $financialItem = new CRM_Financial_DAO_EntityFinancialTrxn();
213 $financialItem->copyValues($params);
214 //retrieve last entry from civicrm_entity_financial_trxn
215 if ($maxId) {
216 $financialItem->orderBy('id DESC');
217 $financialItem->limit(1);
218 }
219 $financialItem->find();
220 while ($financialItem->fetch()) {
221 $financialItems[$financialItem->id] = array(
222 'id' => $financialItem->id,
223 'entity_table' => $financialItem->entity_table,
224 'entity_id' => $financialItem->entity_id,
225 'financial_trxn_id' => $financialItem->financial_trxn_id,
226 'amount' => $financialItem->amount,
227 );
228 }
229 if (!empty($financialItems)) {
230 return $financialItems;
231 }
232 else {
233 return NULL;
234 }
235 }
236
237 /**
238 * Check if contact is present in financial_item table
239 *
240 * CRM-12929
241 *
242 * @param array $contactIds
243 * An array contact id's.
244 *
245 * @param array $error
246 * Error to display.
247 *
248 * @return array
249 * @static
250 */
251 public static function checkContactPresent($contactIds, &$error) {
252 if (empty($contactIds)) {
253 return FALSE;
254 }
255
256 $allowPermDelete = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'allowPermDeleteFinancial');
257
258 if (!$allowPermDelete) {
259 $sql = 'SELECT DISTINCT(cc.id), cc.display_name FROM civicrm_contact cc
260 INNER JOIN civicrm_contribution con ON con.contact_id = cc.id
261 WHERE cc.id IN (' . implode(',', $contactIds) . ') AND con.is_test = 0';
262 $dao = CRM_Core_DAO::executeQuery($sql);
263 if ($dao->N) {
264 while ($dao->fetch()) {
265 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$dao->id");
266 $not_deleted[$dao->id] = "<a href='$url'>$dao->display_name</a>";
267 }
268
269 $errorStatus = '';
270 if (is_array($error)) {
271 $errorStatus = '<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>';
272 }
273
274 $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.');
275 return $error;
276 }
277 }
278 return FALSE;
279 }
280 }