Merge pull request #3771 from monishdeb/CRM-15041
[civicrm-core.git] / CRM / Financial / BAO / ExportFormat / CSV.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36
37/*
38 * @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation
39 */
40
41class CRM_Financial_BAO_ExportFormat_CSV extends CRM_Financial_BAO_ExportFormat {
42
43 // For this phase, we always output these records too so that there isn't data referenced in the journal entries that isn't defined anywhere.
44 // Possibly in the future this could be selected by the user.
45 public static $complementaryTables = array(
46 'ACCNT',
47 'CUST',
48 );
49
50 /**
51 * class constructor
52 */
53 function __construct() {
54 parent::__construct();
55 }
56
e0ef6999
EM
57 /**
58 * @param $exportParams
59 */
6a488035
TO
60 function export($exportParams) {
61 $export = parent::export($exportParams);
62
63 // Save the file in the public directory
64 $fileName = self::putFile($export);
65
66 foreach ( self::$complementaryTables as $rct ) {
67 $func = "export{$rct}";
68 $this->$func();
69 }
70
71 // now do general journal entries
72 $this->exportTRANS();
73
74 $this->output($fileName);
75 }
76
e0ef6999
EM
77 /**
78 * @param $batchId
79 *
80 * @return Object
81 */
6a488035
TO
82 function generateExportQuery($batchId) {
83 $sql = "SELECT
84 ft.id as financial_trxn_id,
85 ft.trxn_date,
86 fa_to.accounting_code AS to_account_code,
87 fa_to.name AS to_account_name,
88 fa_to.account_type_code AS to_account_type_code,
89 ft.total_amount AS debit_total_amount,
90 ft.trxn_id AS trxn_id,
91 cov.label AS payment_instrument,
92 ft.check_number,
93 c.source AS source,
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
124 $params = array(1 => array($batchId, 'String'));
125 $dao = CRM_Core_DAO::executeQuery( $sql, $params );
126
127 return $dao;
128 }
129
e0ef6999
EM
130 /**
131 * @param $export
132 *
133 * @return string
134 */
6a488035
TO
135 function putFile($export) {
136 $config = CRM_Core_Config::singleton();
137 $fileName = $config->uploadDir.'Financial_Transactions_'.$this->_batchIds.'_'.date('YmdHis').'.'.$this->getFileExtension();
138 $this->_downloadFile[] = $config->customFileUploadDir.CRM_Utils_File::cleanFileName(basename($fileName));
139 $out = fopen($fileName, 'w');
140 fputcsv($out, $export['headers']);
141 unset($export['headers']);
142 if (!empty($export)) {
143 foreach ($export as $fields) {
144 fputcsv($out, $fields);
145 }
146 fclose($out);
147 }
148 return $fileName;
149 }
150
151 /**
152 * Format table headers
153 *
154 * @param array $values
155 * @return array
156 */
157 function formatHeaders($values) {
158 $arrayKeys = array_keys($values);
159 $headers = '';
160 if (!empty($arrayKeys)) {
161 foreach ($values[$arrayKeys[0]] as $title => $value) {
162 $headers[] = $title;
163 }
164 }
165 return $headers;
166 }
167
168 /**
169 * Generate CSV array for export
170 *
171 * @param array $export
172 *
173 */
174 function makeCSV($export) {
175 foreach ($export as $batchId => $dao) {
176 $financialItems = array();
177 $this->_batchIds = $batchId;
178 while ($dao->fetch()) {
03e04002 179 $creditAccountName = $creditAccountType =
6a488035
TO
180 $creditAccount = NULL;
181 if ($dao->credit_account) {
182 $creditAccountName = $dao->credit_account_name;
183 $creditAccountType = $dao->credit_account_type_code;
184 $creditAccount = $dao->credit_account;
185 }
186 else {
187 $creditAccountName = $dao->from_credit_account_name;
188 $creditAccountType = $dao->from_credit_account_type_code;
03e04002 189 $creditAccount = $dao->from_credit_account;
6a488035 190 }
03e04002 191
6a488035
TO
192 $financialItems[] = array(
193 'Transaction Date' => $dao->trxn_date,
194 'Debit Account' => $dao->to_account_code,
195 'Debit Account Name' => $dao->to_account_name,
196 'Debit Account Type' => $dao->to_account_type_code,
197 'Debit Account Amount (Unsplit)' => $dao->debit_total_amount,
198 'Transaction ID (Unsplit)' => $dao->trxn_id,
199 'Payment Instrument' => $dao->payment_instrument,
200 'Check Number' => $dao->check_number,
201 'Source' => $dao->source,
202 'Currency' => $dao->currency,
203 'Transaction Status' => $dao->status,
204 'Amount' => $dao->amount,
205 'Credit Account' => $creditAccount,
206 'Credit Account Name' => $creditAccountName,
207 'Credit Account Type' => $creditAccountType,
208 'Item Description' => $dao->item_description,
209 );
210 }
211 $financialItems['headers'] = self::formatHeaders($financialItems);
212 self::export($financialItems);
213 }
214 parent::initiateDownload();
215 }
216
e0ef6999
EM
217 /**
218 * @return string
219 */
6a488035
TO
220 function getFileExtension() {
221 return 'csv';
222 }
223
224 function exportACCNT() {
225 }
226
227 function exportCUST() {
228 }
229
230 function exportTRANS() {
231 }
232624b1 232}