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