INFRA-132 - Fix spacing of @return tag in comments
[civicrm-core.git] / CRM / Financial / BAO / FinancialItem.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36class CRM_Financial_BAO_FinancialItem extends CRM_Financial_DAO_FinancialItem {
37
38 /**
100fef9d 39 * Class constructor
6a488035 40 */
045f52a3 41 public function __construct() {
481a74f4 42 parent::__construct();
6a488035
TO
43 }
44
45 /**
c490a46a 46 * Fetch object based on array of properties
6a488035 47 *
ed5dd492
TO
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.
6a488035 52 *
c490a46a 53 * @return CRM_Contribute_BAO_FinancialItem object
6a488035
TO
54 * @static
55 */
00be9182 56 public static function retrieve(&$params, &$defaults) {
6a488035
TO
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 /**
100fef9d 67 * Add the financial items and financial trxn
6a488035 68 *
ed5dd492
TO
69 * @param object $lineItem
70 * Line item object.
71 * @param object $contribution
72 * Contribution object.
73 * @param bool $taxTrxnID
03e04002 74 *
03e04002 75 * @static
6a488035
TO
76 * @return void
77 */
00be9182 78 public static function add($lineItem, $contribution, $taxTrxnID = FALSE) {
6a488035 79 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
7611ae71 80 $financialItemStatus = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialItem', 'status_id');
b81ee58c 81 $itemStatus = NULL;
0bdf3091
PJ
82 if ($contribution->contribution_status_id == array_search('Completed', $contributionStatuses)
83 || $contribution->contribution_status_id == array_search('Pending refund', $contributionStatuses)) {
6a488035 84 $itemStatus = array_search('Paid', $financialItemStatus);
03e04002 85 }
f6bae84f 86 elseif ($contribution->contribution_status_id == array_search('Pending', $contributionStatuses)
b81ee58c 87 || $contribution->contribution_status_id == array_search('In Progress', $contributionStatuses)) {
6a488035 88 $itemStatus = array_search('Unpaid', $financialItemStatus);
03e04002 89 }
f8325309
PJ
90 elseif ($contribution->contribution_status_id == array_search('Partially paid', $contributionStatuses)) {
91 $itemStatus = array_search('Partially paid', $financialItemStatus);
92 }
6a488035
TO
93 $params = array(
94 'transaction_date' => CRM_Utils_Date::isoToMysql($contribution->receive_date),
03e04002 95 'contact_id' => $contribution->contact_id,
6a488035
TO
96 'amount' => $lineItem->line_total,
97 'currency' => $contribution->currency,
98 'entity_table' => 'civicrm_line_item',
99 'entity_id' => $lineItem->id,
481a74f4 100 'description' => ($lineItem->qty != 1 ? $lineItem->qty . ' of ' : ''). ' ' . $lineItem->label,
6a488035
TO
101 'status_id' => $itemStatus,
102 );
03e04002 103
0b7bd9dd 104 if ($taxTrxnID) {
5a18a545 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);
0b7bd9dd 107 $params['amount'] = $lineItem->tax_amount;
5a18a545 108 $params['description'] = $taxTerm;
0b7bd9dd 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 }
6a488035 114 if ($lineItem->financial_type_id) {
03e04002 115 $searchParams = array(
6a488035
TO
116 'entity_table' => 'civicrm_financial_type',
117 'entity_id' => $lineItem->financial_type_id,
0b7bd9dd 118 'account_relationship' => $accountRel,
6a488035
TO
119 );
120
121 $result = array();
481a74f4
TO
122 CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result);
123 $params['financial_account_id'] = CRM_Utils_Array::value('financial_account_id', $result);
6a488035 124 }
c40e1ff4 125 $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE);
126 $trxnId['id'] = $trxn['financialTrxnId'];
6a488035 127
0aaf8fe9 128 return self::create($params, NULL, $trxnId);
03e04002 129 }
6a488035
TO
130
131 /**
100fef9d 132 * Create the financial Items and financial enity trxn
6a488035 133 *
ed5dd492
TO
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.
03e04002 140 *
03e04002 141 * @static
6a488035
TO
142 * @return object
143 */
00be9182 144 public static function create(&$params, $ids = NULL, $trxnIds = NULL) {
6a488035 145 $financialItem = new CRM_Financial_DAO_FinancialItem();
045f52a3 146
a9ed1dc0
PN
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 }
045f52a3 153
6a488035 154 $financialItem->copyValues($params);
a7488080 155 if (!empty($ids['id'])) {
03e04002 156 $financialItem->id = $ids['id'];
6a488035
TO
157 }
158
159 $financialItem->save();
a7488080 160 if (!empty($trxnIds['id'])) {
6a488035
TO
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 );
03e04002 167
6a488035
TO
168 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
169 $entity_trxn->copyValues($entity_financial_trxn_params);
a7488080 170 if (!empty($ids['entityFinancialTrxnId'])) {
6a488035
TO
171 $entity_trxn->id = $ids['entityFinancialTrxnId'];
172 }
173 $entity_trxn->save();
174 }
a9ed1dc0
PN
175 if (!empty($ids['id'])) {
176 CRM_Utils_Hook::post('edit', 'FinancialItem', $financialItem->id, $financialItem);
177 }
045f52a3 178 else {
a9ed1dc0
PN
179 CRM_Utils_Hook::post('create', 'FinancialItem', $financialItem->id, $financialItem);
180 }
6a488035 181 return $financialItem;
03e04002 182 }
6a488035
TO
183
184 /**
100fef9d 185 * Takes an associative array and creates a entity financial transaction object
6a488035 186 *
ed5dd492
TO
187 * @param array $params
188 * (reference ) an assoc array of name/value pairs.
6a488035 189 *
c490a46a 190 * @return CRM_Core_BAO_FinancialTrxn object
6a488035
TO
191 * @static
192 */
00be9182 193 public static function createEntityTrxn($params) {
6a488035
TO
194 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
195 $entity_trxn->copyValues($params);
196 $entity_trxn->save();
197 return $entity_trxn;
198 }
199
200 /**
100fef9d 201 * Retrive entity financial trxn details
6a488035 202 *
ed5dd492
TO
203 * @param array $params
204 * (reference ) an assoc array of name/value pairs.
205 * @param bool $maxId
206 * To retrive max id.
6a488035
TO
207 *
208 * @return array
6a488035
TO
209 * @static
210 */
00be9182 211 public static function retrieveEntityFinancialTrxn($params, $maxId = FALSE) {
6a488035
TO
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 );
03e04002 228 }
6a488035
TO
229 if (!empty($financialItems)) {
230 return $financialItems;
231 }
232 else {
045f52a3 233 return NULL;
6a488035
TO
234 }
235 }
2efcf0c2 236
f182074e 237 /**
100fef9d 238 * Check if contact is present in financial_item table
f182074e
PN
239 *
240 * CRM-12929
241 *
ed5dd492
TO
242 * @param array $contactIds
243 * An array contact id's.
f182074e 244 *
ed5dd492
TO
245 * @param array $error
246 * Error to display.
f182074e
PN
247 *
248 * @return array
f182074e
PN
249 * @static
250 */
00be9182 251 public static function checkContactPresent($contactIds, &$error) {
f182074e
PN
252 if (empty($contactIds)) {
253 return FALSE;
254 }
2efcf0c2 255
f182074e
PN
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
260INNER JOIN civicrm_contribution con ON con.contact_id = cc.id
045f52a3 261WHERE cc.id IN (' . implode(',', $contactIds) . ') AND con.is_test = 0';
f182074e
PN
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 }
2efcf0c2 268
f182074e
PN
269 $errorStatus = '';
270 if (is_array($error)) {
271 $errorStatus = '<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>';
272 }
2efcf0c2 273
86bfa4f6 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.');
2efcf0c2 275 return $error;
f182074e
PN
276 }
277 }
278 return FALSE;
279 }
6a488035 280}