Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
232624b1 | 5 | | CiviCRM version 4.4 | |
6a488035 TO |
6 | +--------------------------------------------------------------------+ |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
32 | * @copyright CiviCRM LLC (c) 2004-2013 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | ||
37 | /* | |
38 | * @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation | |
39 | */ | |
40 | ||
41 | class 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 | ||
57 | function export($exportParams) { | |
58 | $export = parent::export($exportParams); | |
59 | ||
60 | // Save the file in the public directory | |
61 | $fileName = self::putFile($export); | |
62 | ||
63 | foreach ( self::$complementaryTables as $rct ) { | |
64 | $func = "export{$rct}"; | |
65 | $this->$func(); | |
66 | } | |
67 | ||
68 | // now do general journal entries | |
69 | $this->exportTRANS(); | |
70 | ||
71 | $this->output($fileName); | |
72 | } | |
73 | ||
74 | function generateExportQuery($batchId) { | |
75 | $sql = "SELECT | |
76 | ft.id as financial_trxn_id, | |
77 | ft.trxn_date, | |
78 | fa_to.accounting_code AS to_account_code, | |
79 | fa_to.name AS to_account_name, | |
80 | fa_to.account_type_code AS to_account_type_code, | |
81 | ft.total_amount AS debit_total_amount, | |
82 | ft.trxn_id AS trxn_id, | |
83 | cov.label AS payment_instrument, | |
84 | ft.check_number, | |
85 | c.source AS source, | |
86 | ft.currency AS currency, | |
87 | cov_status.label AS status, | |
03e04002 | 88 | CASE |
81e472f4 PN |
89 | WHEN efti.entity_id IS NOT NULL |
90 | THEN efti.amount | |
91 | ELSE eftc.amount | |
92 | END AS amount, | |
6a488035 TO |
93 | fa_from.account_type_code AS credit_account_type_code, |
94 | fa_from.accounting_code AS credit_account, | |
95 | fa_from.name AS credit_account_name, | |
96 | fac.account_type_code AS from_credit_account_type_code, | |
97 | fac.accounting_code AS from_credit_account, | |
98 | fac.name AS from_credit_account_name, | |
99 | fi.description AS item_description | |
100 | FROM civicrm_entity_batch eb | |
101 | LEFT JOIN civicrm_financial_trxn ft ON (eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn') | |
102 | LEFT JOIN civicrm_financial_account fa_to ON fa_to.id = ft.to_financial_account_id | |
103 | LEFT JOIN civicrm_financial_account fa_from ON fa_from.id = ft.from_financial_account_id | |
104 | LEFT JOIN civicrm_option_group cog ON cog.name = 'payment_instrument' | |
105 | LEFT JOIN civicrm_option_value cov ON (cov.value = ft.payment_instrument_id AND cov.option_group_id = cog.id) | |
106 | LEFT JOIN civicrm_entity_financial_trxn eftc ON (eftc.financial_trxn_id = ft.id AND eftc.entity_table = 'civicrm_contribution') | |
107 | LEFT JOIN civicrm_contribution c ON c.id = eftc.entity_id | |
108 | LEFT JOIN civicrm_option_group cog_status ON cog_status.name = 'contribution_status' | |
109 | LEFT JOIN civicrm_option_value cov_status ON (cov_status.value = ft.status_id AND cov_status.option_group_id = cog_status.id) | |
110 | LEFT JOIN civicrm_entity_financial_trxn efti ON (efti.financial_trxn_id = ft.id AND efti.entity_table = 'civicrm_financial_item') | |
111 | LEFT JOIN civicrm_financial_item fi ON fi.id = efti.entity_id | |
112 | LEFT JOIN civicrm_financial_account fac ON fac.id = fi.financial_account_id | |
113 | LEFT JOIN civicrm_financial_account fa ON fa.id = fi.financial_account_id | |
114 | WHERE eb.batch_id = ( %1 )"; | |
115 | ||
116 | $params = array(1 => array($batchId, 'String')); | |
117 | $dao = CRM_Core_DAO::executeQuery( $sql, $params ); | |
118 | ||
119 | return $dao; | |
120 | } | |
121 | ||
122 | function putFile($export) { | |
123 | $config = CRM_Core_Config::singleton(); | |
124 | $fileName = $config->uploadDir.'Financial_Transactions_'.$this->_batchIds.'_'.date('YmdHis').'.'.$this->getFileExtension(); | |
125 | $this->_downloadFile[] = $config->customFileUploadDir.CRM_Utils_File::cleanFileName(basename($fileName)); | |
126 | $out = fopen($fileName, 'w'); | |
127 | fputcsv($out, $export['headers']); | |
128 | unset($export['headers']); | |
129 | if (!empty($export)) { | |
130 | foreach ($export as $fields) { | |
131 | fputcsv($out, $fields); | |
132 | } | |
133 | fclose($out); | |
134 | } | |
135 | return $fileName; | |
136 | } | |
137 | ||
138 | /** | |
139 | * Format table headers | |
140 | * | |
141 | * @param array $values | |
142 | * @return array | |
143 | */ | |
144 | function formatHeaders($values) { | |
145 | $arrayKeys = array_keys($values); | |
146 | $headers = ''; | |
147 | if (!empty($arrayKeys)) { | |
148 | foreach ($values[$arrayKeys[0]] as $title => $value) { | |
149 | $headers[] = $title; | |
150 | } | |
151 | } | |
152 | return $headers; | |
153 | } | |
154 | ||
155 | /** | |
156 | * Generate CSV array for export | |
157 | * | |
158 | * @param array $export | |
159 | * | |
160 | */ | |
161 | function makeCSV($export) { | |
162 | foreach ($export as $batchId => $dao) { | |
163 | $financialItems = array(); | |
164 | $this->_batchIds = $batchId; | |
165 | while ($dao->fetch()) { | |
03e04002 | 166 | $creditAccountName = $creditAccountType = |
6a488035 TO |
167 | $creditAccount = NULL; |
168 | if ($dao->credit_account) { | |
169 | $creditAccountName = $dao->credit_account_name; | |
170 | $creditAccountType = $dao->credit_account_type_code; | |
171 | $creditAccount = $dao->credit_account; | |
172 | } | |
173 | else { | |
174 | $creditAccountName = $dao->from_credit_account_name; | |
175 | $creditAccountType = $dao->from_credit_account_type_code; | |
03e04002 | 176 | $creditAccount = $dao->from_credit_account; |
6a488035 | 177 | } |
03e04002 | 178 | |
6a488035 TO |
179 | $financialItems[] = array( |
180 | 'Transaction Date' => $dao->trxn_date, | |
181 | 'Debit Account' => $dao->to_account_code, | |
182 | 'Debit Account Name' => $dao->to_account_name, | |
183 | 'Debit Account Type' => $dao->to_account_type_code, | |
184 | 'Debit Account Amount (Unsplit)' => $dao->debit_total_amount, | |
185 | 'Transaction ID (Unsplit)' => $dao->trxn_id, | |
186 | 'Payment Instrument' => $dao->payment_instrument, | |
187 | 'Check Number' => $dao->check_number, | |
188 | 'Source' => $dao->source, | |
189 | 'Currency' => $dao->currency, | |
190 | 'Transaction Status' => $dao->status, | |
191 | 'Amount' => $dao->amount, | |
192 | 'Credit Account' => $creditAccount, | |
193 | 'Credit Account Name' => $creditAccountName, | |
194 | 'Credit Account Type' => $creditAccountType, | |
195 | 'Item Description' => $dao->item_description, | |
196 | ); | |
197 | } | |
198 | $financialItems['headers'] = self::formatHeaders($financialItems); | |
199 | self::export($financialItems); | |
200 | } | |
201 | parent::initiateDownload(); | |
202 | } | |
203 | ||
204 | function getFileExtension() { | |
205 | return 'csv'; | |
206 | } | |
207 | ||
208 | function exportACCNT() { | |
209 | } | |
210 | ||
211 | function exportCUST() { | |
212 | } | |
213 | ||
214 | function exportTRANS() { | |
215 | } | |
232624b1 | 216 | } |