}
         if ($currency_convert_to_base == TRUE) {
             $comment .= ' [' . $base_currency_obj->getISOCode() . ']';
         }
         $final_rows[$row['transaction_date']][$row[$final_group_key]] = array('source' => $source, 'comment' => $comment, 'transaction_date' => $row['transaction_date']);
     }
     if ($row['type'] == 'total') {
         $final_rows[$row['transaction_date']][$row[$final_group_key]]['records'][$row['type']][] = array('type' => $row['type'], 'account' => $row['account'], 'amount' => $row['amount'], 'total_debits' => $row['total_debits'], 'total_credits' => $row['total_credits'], 'total_diff' => $row['total_diff']);
     } else {
         $final_rows[$row['transaction_date']][$row[$final_group_key]]['records'][$row['type']][] = array('type' => $row['type'], 'account' => $row['account'], 'amount' => $row['amount']);
     }
 }
 unset($rows);
 //var_dump($final_rows);
 if ($action == 'export') {
     $gle = new GeneralLedgerExport();
     //$gle->setFileFormat('CSV');
     $gle->setFileFormat($filter_data['export_type']);
 }
 //Flatten final rows
 foreach ($final_rows as $final_row_a) {
     foreach ($final_row_a as $final_row_b) {
         if ($action == 'export') {
             $je = new GeneralLedgerExport_JournalEntry();
             $je->setDate($final_row_b['transaction_date']);
             if ($final_row_b['source'] == '--') {
                 $final_row_b['source'] = 'TimeTrex';
             }
             if ($final_row_b['comment'] == '--') {
                 $final_row_b['comment'] = 'Payroll';
             }
 function _outputExportGeneralLedger($format)
 {
     Debug::Text('Generating GL export for Format: ' . $format, __FILE__, __LINE__, __METHOD__, 10);
     //Calculate sub-total so we know where the journal entries start/stop.
     $enable_grouping = FALSE;
     if (is_array($this->formatGroupConfig()) and count($this->formatGroupConfig()) > 0) {
         Debug::Arr($this->formatGroupConfig(), 'Group Config: ', __FILE__, __LINE__, __METHOD__, 10);
         $enable_grouping = TRUE;
     }
     $file_name = 'no_data.txt';
     $data = NULL;
     if (is_array($this->form_data)) {
         //Need to group the exported data so the number of journal entries can be reduced.
         $this->form_data = Group::GroupBy($this->form_data, $this->formatGroupConfig());
         $gle = new GeneralLedgerExport();
         $gle->setFileFormat($format);
         $prev_group_key = NULL;
         $i = 0;
         foreach ($this->form_data as $row) {
             $group_key = 0;
             if ($enable_grouping == TRUE) {
                 $comment = array();
                 foreach ($this->formatGroupConfig() as $group_column => $group_agg) {
                     if (is_int($group_agg) and isset($row[$group_column]) and $group_column != 'account') {
                         if (is_array($row[$group_column]) and isset($row[$group_column]['display'])) {
                             $comment[] = $row[$group_column]['display'];
                             $group_key .= crc32($row[$group_column]['display']);
                         } elseif ($row[$group_column] != '') {
                             $comment[] = $row[$group_column];
                             $group_key .= $row[$group_column];
                         }
                     } else {
                         $group_key .= 0;
                     }
                 }
                 unset($group_column, $group_agg);
             }
             //Debug::Arr($row, 'GL Export Row: Group Key: '. $group_key , __FILE__, __LINE__, __METHOD__,10);
             if ($prev_group_key === NULL or $prev_group_key != $group_key) {
                 if ($i > 0) {
                     Debug::Text('Ending previous JE: Group Key: ' . $group_key, __FILE__, __LINE__, __METHOD__, 10);
                     $gle->setJournalEntry($je);
                     //Add previous JE before starting a new one.
                 }
                 Debug::Text('Starting new JE: Group Key: ' . $group_key, __FILE__, __LINE__, __METHOD__, 10);
                 $je = new GeneralLedgerExport_JournalEntry();
                 if (isset($row['pay_stub_transaction_date'])) {
                     $je->setDate($row['pay_stub_transaction_date']);
                 } elseif (isset($row['transaction-date_stamp'])) {
                     $je->setDate(TTDate::parseDateTime($row['transaction-date_stamp']));
                 } else {
                     $je->setDate(time());
                 }
                 $je->setSource(APPLICATION_NAME);
                 if (isset($comment) and is_array($comment) and count($comment) > 0) {
                     $je->setComment(implode(' ', $comment));
                 } else {
                     $je->setComment(TTi18n::getText('Payroll'));
                 }
             }
             if (isset($row['debit_amount']) and $row['debit_amount'] > 0) {
                 Debug::Text('Adding Debit Record for: ' . $row['debit_amount'], __FILE__, __LINE__, __METHOD__, 10);
                 $record = new GeneralLedgerExport_Record();
                 $record->setAccount($row['account']);
                 $record->setType('debit');
                 $record->setAmount($row['debit_amount']);
                 $je->setRecord($record);
             }
             if (isset($row['credit_amount']) and $row['credit_amount'] > 0) {
                 Debug::Text('Adding Credit Record for: ' . $row['credit_amount'], __FILE__, __LINE__, __METHOD__, 10);
                 $record = new GeneralLedgerExport_Record();
                 $record->setAccount($row['account']);
                 $record->setType('credit');
                 $record->setAmount($row['credit_amount']);
                 $je->setRecord($record);
             }
             unset($record);
             $prev_group_key = $group_key;
             $i++;
         }
         $gle->setJournalEntry($je);
         //Handle last JE here
         if ($gle->compile() == TRUE) {
             $data = $gle->getCompiledData();
             Debug::Text('Exporting as: ' . $format, __FILE__, __LINE__, __METHOD__, 10);
             if ($format == 'simply') {
                 $file_name = 'general_ledger_' . str_replace(array('/', ',', ' '), '_', TTDate::getDate('DATE', time())) . '.txt';
             } elseif ($format == 'quickbooks') {
                 $file_name = 'general_ledger_' . str_replace(array('/', ',', ' '), '_', TTDate::getDate('DATE', time())) . '.iif';
             } else {
                 $file_name = 'general_ledger_' . str_replace(array('/', ',', ' '), '_', TTDate::getDate('DATE', time())) . '.csv';
             }
         }
     }
     return array('file_name' => $file_name, 'mime_type' => 'application/text', 'data' => $data);
 }