Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 | ||
36 | class 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'); | |
81 | $financialItemStatus = CRM_Core_PseudoConstant::accountOptionValues('financial_item_status'); | |
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 | } |
6a488035 TO |
88 | $params = array( |
89 | 'transaction_date' => CRM_Utils_Date::isoToMysql($contribution->receive_date), | |
03e04002 | 90 | 'contact_id' => $contribution->contact_id, |
6a488035 TO |
91 | 'amount' => $lineItem->line_total, |
92 | 'currency' => $contribution->currency, | |
93 | 'entity_table' => 'civicrm_line_item', | |
94 | 'entity_id' => $lineItem->id, | |
95 | 'description' => ( $lineItem->qty != 1 ? $lineItem->qty . ' of ' : ''). ' ' . $lineItem->label, | |
96 | 'status_id' => $itemStatus, | |
97 | ); | |
03e04002 | 98 | |
6a488035 | 99 | if ($lineItem->financial_type_id) { |
03e04002 | 100 | $searchParams = array( |
6a488035 TO |
101 | 'entity_table' => 'civicrm_financial_type', |
102 | 'entity_id' => $lineItem->financial_type_id, | |
103 | 'account_relationship' => 1, | |
104 | ); | |
105 | ||
106 | $result = array(); | |
107 | CRM_Financial_BAO_FinancialTypeAccount::retrieve( $searchParams, $result ); | |
108 | $params['financial_account_id'] = CRM_Utils_Array::value( 'financial_account_id', $result ); | |
109 | } | |
110 | ||
111 | $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE); | |
112 | $trxnId['id'] = $trxn['financialTrxnId']; | |
113 | ||
114 | self::create($params, NULL, $trxnId); | |
03e04002 | 115 | } |
6a488035 TO |
116 | |
117 | /** | |
118 | * function to create the financial Items and financial enity trxn | |
119 | * | |
120 | * @param array $params associated array to create financial items | |
121 | * @param array $ids financial item ids | |
122 | * @param array $trxnIds financial item ids | |
03e04002 | 123 | * |
6a488035 | 124 | * @access public |
03e04002 | 125 | * @static |
6a488035 TO |
126 | * @return object |
127 | */ | |
128 | static function create(&$params, $ids = NULL, $trxnIds = NULL) { | |
129 | $financialItem = new CRM_Financial_DAO_FinancialItem(); | |
130 | $financialItem->copyValues($params); | |
131 | if (CRM_Utils_Array::value('id', $ids)) { | |
03e04002 | 132 | $financialItem->id = $ids['id']; |
6a488035 TO |
133 | } |
134 | ||
135 | $financialItem->save(); | |
136 | if (CRM_Utils_Array::value('id', $trxnIds)) { | |
137 | $entity_financial_trxn_params = array( | |
138 | 'entity_table' => "civicrm_financial_item", | |
139 | 'entity_id' => $financialItem->id, | |
140 | 'financial_trxn_id' => $trxnIds['id'], | |
141 | 'amount' => $params['amount'], | |
142 | ); | |
03e04002 | 143 | |
6a488035 TO |
144 | $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn(); |
145 | $entity_trxn->copyValues($entity_financial_trxn_params); | |
146 | if (CRM_Utils_Array::value('entityFinancialTrxnId', $ids)) { | |
147 | $entity_trxn->id = $ids['entityFinancialTrxnId']; | |
148 | } | |
149 | $entity_trxn->save(); | |
150 | } | |
151 | return $financialItem; | |
03e04002 | 152 | } |
6a488035 TO |
153 | |
154 | /** | |
155 | * takes an associative array and creates a entity financial transaction object | |
156 | * | |
157 | * @param array $params (reference ) an assoc array of name/value pairs | |
158 | * | |
159 | * @return object CRM_Core_BAO_FinancialTrxn object | |
160 | * @access public | |
161 | * @static | |
162 | */ | |
163 | static function createEntityTrxn($params) { | |
164 | $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn(); | |
165 | $entity_trxn->copyValues($params); | |
166 | $entity_trxn->save(); | |
167 | return $entity_trxn; | |
168 | } | |
169 | ||
170 | /** | |
171 | * retrive entity financial trxn details | |
172 | * | |
173 | * @param array $params (reference ) an assoc array of name/value pairs | |
174 | * | |
03e04002 | 175 | * @param boolean $maxID to retrive max id |
6a488035 TO |
176 | * |
177 | * @return array | |
178 | * @access public | |
179 | * @static | |
180 | */ | |
181 | static function retrieveEntityFinancialTrxn($params, $maxId = FALSE) { | |
182 | $financialItem = new CRM_Financial_DAO_EntityFinancialTrxn(); | |
183 | $financialItem->copyValues($params); | |
184 | //retrieve last entry from civicrm_entity_financial_trxn | |
185 | if ($maxId) { | |
186 | $financialItem->orderBy('id DESC'); | |
187 | $financialItem->limit(1); | |
188 | } | |
189 | $financialItem->find(); | |
190 | while ($financialItem->fetch()) { | |
191 | $financialItems[$financialItem->id] = array( | |
192 | 'id' => $financialItem->id, | |
193 | 'entity_table' => $financialItem->entity_table, | |
194 | 'entity_id' => $financialItem->entity_id, | |
195 | 'financial_trxn_id' => $financialItem->financial_trxn_id, | |
196 | 'amount' => $financialItem->amount, | |
197 | ); | |
03e04002 | 198 | } |
6a488035 TO |
199 | if (!empty($financialItems)) { |
200 | return $financialItems; | |
201 | } | |
202 | else { | |
203 | return null; | |
204 | } | |
205 | } | |
206 | } |