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