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