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