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