Merge remote-tracking branch 'upstream/master' into pledge-batchentry
[civicrm-core.git] / CRM / Financial / BAO / FinancialItem.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 function __construct( ) {
42 parent::__construct( );
43 }
44
45 /**
46 * Fetch object based on array of properties
47 *
48 * @param array $params (reference ) an assoc array of name/value pairs
49 * @param array $defaults (reference ) an assoc array to hold the flattened values
50 *
51 * @return CRM_Contribute_BAO_FinancialItem object
52 * @access public
53 * @static
54 */
55 static function retrieve(&$params, &$defaults) {
56 $financialItem = new CRM_Financial_DAO_FinancialItem();
57 $financialItem->copyValues($params);
58 if ($financialItem->find(TRUE)) {
59 CRM_Core_DAO::storeValues($financialItem, $defaults);
60 return $financialItem;
61 }
62 return NULL;
63 }
64
65 /**
66 * Add the financial items and financial trxn
67 *
68 * @param object $lineItem line item object
69 * @param object $contribution contribution object
70 * @param boolean $taxTrxnID
71 *
72 * @access public
73 * @static
74 * @return void
75 */
76 static function add($lineItem, $contribution, $taxTrxnID = FALSE) {
77 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
78 $financialItemStatus = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialItem', 'status_id');
79 $itemStatus = NULL;
80 if ($contribution->contribution_status_id == array_search('Completed', $contributionStatuses)
81 || $contribution->contribution_status_id == array_search('Pending refund', $contributionStatuses)) {
82 $itemStatus = array_search('Paid', $financialItemStatus);
83 }
84 elseif ($contribution->contribution_status_id == array_search('Pending', $contributionStatuses)
85 || $contribution->contribution_status_id == array_search('In Progress', $contributionStatuses)) {
86 $itemStatus = array_search('Unpaid', $financialItemStatus);
87 }
88 elseif ($contribution->contribution_status_id == array_search('Partially paid', $contributionStatuses)) {
89 $itemStatus = array_search('Partially paid', $financialItemStatus);
90 }
91 $params = array(
92 'transaction_date' => CRM_Utils_Date::isoToMysql($contribution->receive_date),
93 'contact_id' => $contribution->contact_id,
94 'amount' => $lineItem->line_total,
95 'currency' => $contribution->currency,
96 'entity_table' => 'civicrm_line_item',
97 'entity_id' => $lineItem->id,
98 'description' => ( $lineItem->qty != 1 ? $lineItem->qty . ' of ' : ''). ' ' . $lineItem->label,
99 'status_id' => $itemStatus,
100 );
101
102 if ($taxTrxnID) {
103 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
104 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
105 $params['amount'] = $lineItem->tax_amount;
106 $params['description'] = $taxTerm;
107 $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
108 }
109 else {
110 $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
111 }
112 if ($lineItem->financial_type_id) {
113 $searchParams = array(
114 'entity_table' => 'civicrm_financial_type',
115 'entity_id' => $lineItem->financial_type_id,
116 'account_relationship' => $accountRel,
117 );
118
119 $result = array();
120 CRM_Financial_BAO_FinancialTypeAccount::retrieve( $searchParams, $result );
121 $params['financial_account_id'] = CRM_Utils_Array::value( 'financial_account_id', $result );
122 }
123 $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE);
124 $trxnId['id'] = $trxn['financialTrxnId'];
125
126 return self::create($params, NULL, $trxnId);
127 }
128
129 /**
130 * Create the financial Items and financial enity trxn
131 *
132 * @param array $params associated array to create financial items
133 * @param array $ids financial item ids
134 * @param array $trxnIds financial item ids
135 *
136 * @access public
137 * @static
138 * @return object
139 */
140 static function create(&$params, $ids = NULL, $trxnIds = NULL) {
141 $financialItem = new CRM_Financial_DAO_FinancialItem();
142 $financialItem->copyValues($params);
143 if (!empty($ids['id'])) {
144 $financialItem->id = $ids['id'];
145 }
146
147 $financialItem->save();
148 if (!empty($trxnIds['id'])) {
149 $entity_financial_trxn_params = array(
150 'entity_table' => "civicrm_financial_item",
151 'entity_id' => $financialItem->id,
152 'financial_trxn_id' => $trxnIds['id'],
153 'amount' => $params['amount'],
154 );
155
156 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
157 $entity_trxn->copyValues($entity_financial_trxn_params);
158 if (!empty($ids['entityFinancialTrxnId'])) {
159 $entity_trxn->id = $ids['entityFinancialTrxnId'];
160 }
161 $entity_trxn->save();
162 }
163 return $financialItem;
164 }
165
166 /**
167 * Takes an associative array and creates a entity financial transaction object
168 *
169 * @param array $params (reference ) an assoc array of name/value pairs
170 *
171 * @return CRM_Core_BAO_FinancialTrxn object
172 * @access public
173 * @static
174 */
175 static function createEntityTrxn($params) {
176 $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn();
177 $entity_trxn->copyValues($params);
178 $entity_trxn->save();
179 return $entity_trxn;
180 }
181
182 /**
183 * Retrive entity financial trxn details
184 *
185 * @param array $params (reference ) an assoc array of name/value pairs
186 * @param bool $maxId to retrive max id
187 *
188 * @return array
189 * @access public
190 * @static
191 */
192 static function retrieveEntityFinancialTrxn($params, $maxId = FALSE) {
193 $financialItem = new CRM_Financial_DAO_EntityFinancialTrxn();
194 $financialItem->copyValues($params);
195 //retrieve last entry from civicrm_entity_financial_trxn
196 if ($maxId) {
197 $financialItem->orderBy('id DESC');
198 $financialItem->limit(1);
199 }
200 $financialItem->find();
201 while ($financialItem->fetch()) {
202 $financialItems[$financialItem->id] = array(
203 'id' => $financialItem->id,
204 'entity_table' => $financialItem->entity_table,
205 'entity_id' => $financialItem->entity_id,
206 'financial_trxn_id' => $financialItem->financial_trxn_id,
207 'amount' => $financialItem->amount,
208 );
209 }
210 if (!empty($financialItems)) {
211 return $financialItems;
212 }
213 else {
214 return null;
215 }
216 }
217
218 /**
219 * Check if contact is present in financial_item table
220 *
221 * CRM-12929
222 *
223 * @param array $contactIds an array contact id's
224 *
225 * @param array $error error to display
226 *
227 * @return array
228 * @access public
229 * @static
230 */
231 static function checkContactPresent($contactIds, &$error) {
232 if (empty($contactIds)) {
233 return FALSE;
234 }
235
236 $allowPermDelete = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'allowPermDeleteFinancial');
237
238 if (!$allowPermDelete) {
239 $sql = 'SELECT DISTINCT(cc.id), cc.display_name FROM civicrm_contact cc
240 INNER JOIN civicrm_contribution con ON con.contact_id = cc.id
241 WHERE cc.id IN (' . implode (',', $contactIds) . ') AND con.is_test = 0';
242 $dao = CRM_Core_DAO::executeQuery($sql);
243 if ($dao->N) {
244 while ($dao->fetch()) {
245 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$dao->id");
246 $not_deleted[$dao->id] = "<a href='$url'>$dao->display_name</a>";
247 }
248
249 $errorStatus = '';
250 if (is_array($error)) {
251 $errorStatus = '<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>';
252 }
253
254 $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.');
255 return $error;
256 }
257 }
258 return FALSE;
259 }
260 }