Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
c866eb5f TO |
34 | /** |
35 | * @link http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation | |
6a488035 | 36 | */ |
6a488035 TO |
37 | class CRM_Financial_BAO_ExportFormat_CSV extends CRM_Financial_BAO_ExportFormat { |
38 | ||
cded2ebf SB |
39 | /** |
40 | * For this phase, we always output these records too so that there isn't data | |
41 | * referenced in the journal entries that isn't defined anywhere. | |
42 | * | |
43 | * Possibly in the future this could be selected by the user. | |
44 | */ | |
6a488035 TO |
45 | public static $complementaryTables = array( |
46 | 'ACCNT', | |
47 | 'CUST', | |
48 | ); | |
49 | ||
50 | /** | |
fe482240 | 51 | * Class constructor. |
6a488035 | 52 | */ |
00be9182 | 53 | public function __construct() { |
6a488035 TO |
54 | parent::__construct(); |
55 | } | |
56 | ||
e0ef6999 | 57 | /** |
100fef9d | 58 | * @param array $exportParams |
e0ef6999 | 59 | */ |
00be9182 | 60 | public function export($exportParams) { |
6a488035 TO |
61 | $export = parent::export($exportParams); |
62 | ||
cded2ebf | 63 | // Save the file in the public directory. |
6a488035 TO |
64 | $fileName = self::putFile($export); |
65 | ||
481a74f4 | 66 | foreach (self::$complementaryTables as $rct) { |
6a488035 TO |
67 | $func = "export{$rct}"; |
68 | $this->$func(); | |
69 | } | |
70 | ||
6a488035 TO |
71 | $this->output($fileName); |
72 | } | |
73 | ||
e0ef6999 | 74 | /** |
100fef9d | 75 | * @param int $batchId |
e0ef6999 EM |
76 | * |
77 | * @return Object | |
78 | */ | |
00be9182 | 79 | public function generateExportQuery($batchId) { |
6a488035 TO |
80 | $sql = "SELECT |
81 | ft.id as financial_trxn_id, | |
82 | ft.trxn_date, | |
83 | fa_to.accounting_code AS to_account_code, | |
84 | fa_to.name AS to_account_name, | |
85 | fa_to.account_type_code AS to_account_type_code, | |
86 | ft.total_amount AS debit_total_amount, | |
87 | ft.trxn_id AS trxn_id, | |
88 | cov.label AS payment_instrument, | |
89 | ft.check_number, | |
90 | c.source AS source, | |
68212cd2 GC |
91 | c.id AS contribution_id, |
92 | c.contact_id AS contact_id, | |
93 | eb.batch_id AS batch_id, | |
6a488035 TO |
94 | ft.currency AS currency, |
95 | cov_status.label AS status, | |
03e04002 | 96 | CASE |
81e472f4 PN |
97 | WHEN efti.entity_id IS NOT NULL |
98 | THEN efti.amount | |
99 | ELSE eftc.amount | |
100 | END AS amount, | |
6a488035 TO |
101 | fa_from.account_type_code AS credit_account_type_code, |
102 | fa_from.accounting_code AS credit_account, | |
103 | fa_from.name AS credit_account_name, | |
104 | fac.account_type_code AS from_credit_account_type_code, | |
105 | fac.accounting_code AS from_credit_account, | |
106 | fac.name AS from_credit_account_name, | |
107 | fi.description AS item_description | |
108 | FROM civicrm_entity_batch eb | |
109 | LEFT JOIN civicrm_financial_trxn ft ON (eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn') | |
110 | LEFT JOIN civicrm_financial_account fa_to ON fa_to.id = ft.to_financial_account_id | |
111 | LEFT JOIN civicrm_financial_account fa_from ON fa_from.id = ft.from_financial_account_id | |
112 | LEFT JOIN civicrm_option_group cog ON cog.name = 'payment_instrument' | |
113 | LEFT JOIN civicrm_option_value cov ON (cov.value = ft.payment_instrument_id AND cov.option_group_id = cog.id) | |
114 | LEFT JOIN civicrm_entity_financial_trxn eftc ON (eftc.financial_trxn_id = ft.id AND eftc.entity_table = 'civicrm_contribution') | |
115 | LEFT JOIN civicrm_contribution c ON c.id = eftc.entity_id | |
116 | LEFT JOIN civicrm_option_group cog_status ON cog_status.name = 'contribution_status' | |
117 | LEFT JOIN civicrm_option_value cov_status ON (cov_status.value = ft.status_id AND cov_status.option_group_id = cog_status.id) | |
118 | LEFT JOIN civicrm_entity_financial_trxn efti ON (efti.financial_trxn_id = ft.id AND efti.entity_table = 'civicrm_financial_item') | |
119 | LEFT JOIN civicrm_financial_item fi ON fi.id = efti.entity_id | |
120 | LEFT JOIN civicrm_financial_account fac ON fac.id = fi.financial_account_id | |
121 | LEFT JOIN civicrm_financial_account fa ON fa.id = fi.financial_account_id | |
122 | WHERE eb.batch_id = ( %1 )"; | |
123 | ||
75c8a3f7 GC |
124 | CRM_Utils_Hook::batchQuery($sql); |
125 | ||
6a488035 | 126 | $params = array(1 => array($batchId, 'String')); |
481a74f4 | 127 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
6a488035 TO |
128 | |
129 | return $dao; | |
130 | } | |
131 | ||
e0ef6999 EM |
132 | /** |
133 | * @param $export | |
134 | * | |
135 | * @return string | |
136 | */ | |
00be9182 | 137 | public function putFile($export) { |
6a488035 | 138 | $config = CRM_Core_Config::singleton(); |
92fcb95f TO |
139 | $fileName = $config->uploadDir . 'Financial_Transactions_' . $this->_batchIds . '_' . date('YmdHis') . '.' . $this->getFileExtension(); |
140 | $this->_downloadFile[] = $config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($fileName)); | |
6a488035 | 141 | $out = fopen($fileName, 'w'); |
0efdabe7 MW |
142 | if (!empty($export['headers'])) { |
143 | fputcsv($out, $export['headers']); | |
144 | } | |
6a488035 TO |
145 | unset($export['headers']); |
146 | if (!empty($export)) { | |
147 | foreach ($export as $fields) { | |
148 | fputcsv($out, $fields); | |
149 | } | |
150 | fclose($out); | |
151 | } | |
152 | return $fileName; | |
153 | } | |
154 | ||
155 | /** | |
fe482240 | 156 | * Format table headers. |
6a488035 TO |
157 | * |
158 | * @param array $values | |
159 | * @return array | |
160 | */ | |
00be9182 | 161 | public function formatHeaders($values) { |
6a488035 TO |
162 | $arrayKeys = array_keys($values); |
163 | $headers = ''; | |
164 | if (!empty($arrayKeys)) { | |
165 | foreach ($values[$arrayKeys[0]] as $title => $value) { | |
166 | $headers[] = $title; | |
167 | } | |
168 | } | |
169 | return $headers; | |
170 | } | |
171 | ||
172 | /** | |
fe482240 | 173 | * Generate CSV array for export. |
6a488035 TO |
174 | * |
175 | * @param array $export | |
6a488035 | 176 | */ |
9b05de29 | 177 | public function makeExport($export) { |
35129997 | 178 | // getting data from admin page |
aaffa79f | 179 | $prefixValue = Civi::settings()->get('contribution_invoice_settings'); |
35129997 | 180 | |
6a488035 TO |
181 | foreach ($export as $batchId => $dao) { |
182 | $financialItems = array(); | |
183 | $this->_batchIds = $batchId; | |
75c8a3f7 GC |
184 | |
185 | $batchItems = array(); | |
186 | $queryResults = array(); | |
187 | ||
6a488035 | 188 | while ($dao->fetch()) { |
408b79bf | 189 | $creditAccountName = $creditAccountType = $creditAccount = NULL; |
6a488035 TO |
190 | if ($dao->credit_account) { |
191 | $creditAccountName = $dao->credit_account_name; | |
192 | $creditAccountType = $dao->credit_account_type_code; | |
193 | $creditAccount = $dao->credit_account; | |
194 | } | |
195 | else { | |
196 | $creditAccountName = $dao->from_credit_account_name; | |
197 | $creditAccountType = $dao->from_credit_account_type_code; | |
03e04002 | 198 | $creditAccount = $dao->from_credit_account; |
6a488035 | 199 | } |
03e04002 | 200 | |
35129997 GC |
201 | $invoiceNo = CRM_Utils_Array::value('invoice_prefix', $prefixValue) . "" . $dao->contribution_id; |
202 | ||
6a488035 | 203 | $financialItems[] = array( |
68212cd2 | 204 | 'Batch ID' => $dao->batch_id, |
35129997 | 205 | 'Invoice No' => $invoiceNo, |
68212cd2 | 206 | 'Contact ID' => $dao->contact_id, |
35129997 | 207 | 'Financial Trxn ID/Internal ID' => $dao->financial_trxn_id, |
6a488035 TO |
208 | 'Transaction Date' => $dao->trxn_date, |
209 | 'Debit Account' => $dao->to_account_code, | |
210 | 'Debit Account Name' => $dao->to_account_name, | |
211 | 'Debit Account Type' => $dao->to_account_type_code, | |
212 | 'Debit Account Amount (Unsplit)' => $dao->debit_total_amount, | |
213 | 'Transaction ID (Unsplit)' => $dao->trxn_id, | |
c40e1ff4 | 214 | 'Debit amount (Split)' => $dao->amount, |
6a488035 TO |
215 | 'Payment Instrument' => $dao->payment_instrument, |
216 | 'Check Number' => $dao->check_number, | |
217 | 'Source' => $dao->source, | |
218 | 'Currency' => $dao->currency, | |
219 | 'Transaction Status' => $dao->status, | |
220 | 'Amount' => $dao->amount, | |
221 | 'Credit Account' => $creditAccount, | |
222 | 'Credit Account Name' => $creditAccountName, | |
223 | 'Credit Account Type' => $creditAccountType, | |
224 | 'Item Description' => $dao->item_description, | |
225 | ); | |
75c8a3f7 GC |
226 | |
227 | end($financialItems); | |
228 | $batchItems[] = &$financialItems[key($financialItems)]; | |
229 | $queryResults[] = get_object_vars($dao); | |
6a488035 | 230 | } |
75c8a3f7 GC |
231 | |
232 | CRM_Utils_Hook::batchItems($queryResults, $batchItems); | |
233 | ||
6a488035 TO |
234 | $financialItems['headers'] = self::formatHeaders($financialItems); |
235 | self::export($financialItems); | |
236 | } | |
237 | parent::initiateDownload(); | |
238 | } | |
239 | ||
e0ef6999 EM |
240 | /** |
241 | * @return string | |
242 | */ | |
00be9182 | 243 | public function getFileExtension() { |
6a488035 TO |
244 | return 'csv'; |
245 | } | |
246 | ||
b296a7d5 SB |
247 | public function exportACCNT() { |
248 | } | |
249 | ||
250 | public function exportCUST() { | |
251 | } | |
252 | ||
253 | public function exportTRANS() { | |
254 | } | |
255 | ||
232624b1 | 256 | } |