4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * @link http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation
40 class CRM_Financial_BAO_ExportFormat_CSV
extends CRM_Financial_BAO_ExportFormat
{
42 // 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.
43 // Possibly in the future this could be selected by the user.
44 public static $complementaryTables = array(
52 public function __construct() {
53 parent
::__construct();
57 * @param array $exportParams
59 public function export($exportParams) {
60 $export = parent
::export($exportParams);
62 // Save the file in the public directory
63 $fileName = self
::putFile($export);
65 foreach (self
::$complementaryTables as $rct) {
66 $func = "export{$rct}";
70 // now do general journal entries
73 $this->output($fileName);
81 public function generateExportQuery($batchId) {
83 ft.id as financial_trxn_id,
85 fa_to.accounting_code AS to_account_code,
86 fa_to.name AS to_account_name,
87 fa_to.account_type_code AS to_account_type_code,
88 ft.total_amount AS debit_total_amount,
89 ft.trxn_id AS trxn_id,
90 cov.label AS payment_instrument,
93 ft.currency AS currency,
94 cov_status.label AS status,
96 WHEN efti.entity_id IS NOT NULL
100 fa_from.account_type_code AS credit_account_type_code,
101 fa_from.accounting_code AS credit_account,
102 fa_from.name AS credit_account_name,
103 fac.account_type_code AS from_credit_account_type_code,
104 fac.accounting_code AS from_credit_account,
105 fac.name AS from_credit_account_name,
106 fi.description AS item_description
107 FROM civicrm_entity_batch eb
108 LEFT JOIN civicrm_financial_trxn ft ON (eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn')
109 LEFT JOIN civicrm_financial_account fa_to ON fa_to.id = ft.to_financial_account_id
110 LEFT JOIN civicrm_financial_account fa_from ON fa_from.id = ft.from_financial_account_id
111 LEFT JOIN civicrm_option_group cog ON cog.name = 'payment_instrument'
112 LEFT JOIN civicrm_option_value cov ON (cov.value = ft.payment_instrument_id AND cov.option_group_id = cog.id)
113 LEFT JOIN civicrm_entity_financial_trxn eftc ON (eftc.financial_trxn_id = ft.id AND eftc.entity_table = 'civicrm_contribution')
114 LEFT JOIN civicrm_contribution c ON c.id = eftc.entity_id
115 LEFT JOIN civicrm_option_group cog_status ON cog_status.name = 'contribution_status'
116 LEFT JOIN civicrm_option_value cov_status ON (cov_status.value = ft.status_id AND cov_status.option_group_id = cog_status.id)
117 LEFT JOIN civicrm_entity_financial_trxn efti ON (efti.financial_trxn_id = ft.id AND efti.entity_table = 'civicrm_financial_item')
118 LEFT JOIN civicrm_financial_item fi ON fi.id = efti.entity_id
119 LEFT JOIN civicrm_financial_account fac ON fac.id = fi.financial_account_id
120 LEFT JOIN civicrm_financial_account fa ON fa.id = fi.financial_account_id
121 WHERE eb.batch_id = ( %1 )";
123 $params = array(1 => array($batchId, 'String'));
124 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
134 public function putFile($export) {
135 $config = CRM_Core_Config
::singleton();
136 $fileName = $config->uploadDir
. 'Financial_Transactions_' . $this->_batchIds
. '_' . date('YmdHis') . '.' . $this->getFileExtension();
137 $this->_downloadFile
[] = $config->customFileUploadDir
. CRM_Utils_File
::cleanFileName(basename($fileName));
138 $out = fopen($fileName, 'w');
139 fputcsv($out, $export['headers']);
140 unset($export['headers']);
141 if (!empty($export)) {
142 foreach ($export as $fields) {
143 fputcsv($out, $fields);
151 * Format table headers
153 * @param array $values
156 public function formatHeaders($values) {
157 $arrayKeys = array_keys($values);
159 if (!empty($arrayKeys)) {
160 foreach ($values[$arrayKeys[0]] as $title => $value) {
168 * Generate CSV array for export
170 * @param array $export
173 public function makeCSV($export) {
174 foreach ($export as $batchId => $dao) {
175 $financialItems = array();
176 $this->_batchIds
= $batchId;
177 while ($dao->fetch()) {
178 $creditAccountName = $creditAccountType =
179 $creditAccount = NULL;
180 if ($dao->credit_account
) {
181 $creditAccountName = $dao->credit_account_name
;
182 $creditAccountType = $dao->credit_account_type_code
;
183 $creditAccount = $dao->credit_account
;
186 $creditAccountName = $dao->from_credit_account_name
;
187 $creditAccountType = $dao->from_credit_account_type_code
;
188 $creditAccount = $dao->from_credit_account
;
191 $financialItems[] = array(
192 'Internal ID' => $dao->financial_trxn_id
,
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 'Debit amount (Split)' => $dao->amount
,
200 'Payment Instrument' => $dao->payment_instrument
,
201 'Check Number' => $dao->check_number
,
202 'Source' => $dao->source
,
203 'Currency' => $dao->currency
,
204 'Transaction Status' => $dao->status
,
205 'Amount' => $dao->amount
,
206 'Credit Account' => $creditAccount,
207 'Credit Account Name' => $creditAccountName,
208 'Credit Account Type' => $creditAccountType,
209 'Item Description' => $dao->item_description
,
212 $financialItems['headers'] = self
::formatHeaders($financialItems);
213 self
::export($financialItems);
215 parent
::initiateDownload();
221 public function getFileExtension() {
225 public function exportACCNT() {
228 public function exportCUST() {
231 public function exportTRANS() {