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