<commit_before>//
//  TripCSVGenerator.swift
//  SmartReceipts
//
//  Created by <NAME> on 03/12/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation

fileprivate let TABLE_SPACING = "\n \n"

class TripCSVGenerator: ReportCSVGenerator {
    override func generateTo(path: String) -> Bool {
        do { try generateContent().write(to: path.asFileURL) }
        catch {
            Logger.error("CSV write error: \(error.localizedDescription)")
           return false
        }
        return true
    }
    
    @objc private func generateContent() -> Data {
        let content = NSMutableString()
        
        if !WBPreferences.omitDefaultPdfTable() {
            appendReceiptsTable(content)
            content.append(TABLE_SPACING)
        }
        
        appendDistancesTable(content)
        
        if WBPreferences.includeCategoricalSummation() {
            appendCategoricalSummationTable(content)
        }
        
        if WBPreferences.separatePaymantsByCategory() {
            appendSeparatedByCategoryTable(content)
        }
        
        return (content as String).byteOrderMarked
    }
    
    private func appendReceiptsTable(_ content: NSMutableString) {
        let receiptTable = ReportCSVTable(content: content, columns: receiptColumns())!
        receiptTable.includeHeaders = WBPreferences.includeCSVHeaders()
        
        var recs = receipts()
        if WBPreferences.printDailyDistanceValues() {
            let dReceipts = DistancesToReceiptsConverter.convertDistances(distances()) as! [WBReceipt]
            recs.append(contentsOf: dReceipts)
            recs.sort(by: { $1.date.compare($0.date) == .orderedAscending })
        }
        receiptTable.append(withRows: recs)
    }
    
    private func appendCategoricalSummationTable(_ content: NSMutableString) {
        content.append(TABLE_SPACING)
        let receiptTable = ReportCSVTable(content: content, columns: categoryColumns())!
        receiptTable.includeHeaders = WBPreferences.includeCSVHeaders()<commit_msg>Fix an issue which leads to blank CSV reports<commit_after>//
//  TripCSVGenerator.swift
//  SmartReceipts
//
//  Created by <NAME> on 03/12/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation

fileprivate let TABLE_SPACING = "\n \n"

class TripCSVGenerator: ReportCSVGenerator {
    override func generateTo(path: String) -> Bool {
        do { try generateContent().write(to: path.asFileURL) }
        catch {
            Logger.error("CSV write error: \(error.localizedDescription)")
           return false
        }
        return true
    }
    
    @objc private func generateContent() -> Data {
        let content = NSMutableString()
        
        appendReceiptsTable(content)
        content.append(TABLE_SPACING)
        
        appendDistancesTable(content)
        
        if WBPreferences.includeCategoricalSummation() {
            appendCategoricalSummationTable(content)
        }
        
        if WBPreferences.separatePaymantsByCategory() {
            appendSeparatedByCategoryTable(content)
        }
        
        return (content as String).byteOrderMarked
    }
    
    private func appendReceiptsTable(_ content: NSMutableString) {
        let receiptTable = ReportCSVTable(content: content, columns: receiptColumns())!
        receiptTable.includeHeaders = WBPreferences.includeCSVHeaders()
        
        var recs = receipts()
        if WBPreferences.printDailyDistanceValues() {
            let dReceipts = DistancesToReceiptsConverter.convertDistances(distances()) as! [WBReceipt]
            recs.append(contentsOf: dReceipts)
            recs.sort(by: { $1.date.compare($0.date) == .orderedAscending })
        }
        receiptTable.append(withRows: recs)
    }
    
    private func appendCategoricalSummationTable(_ content: NSMutableString) {
        content.append(TABLE_SPACING)
        let receiptTable = ReportCSVTable(content: content, columns: categoryColumns())!
        receiptTable.includeHeaders = WBPreferences.includeCSVHeaders()
{'fraction_non_alphanumeric': 0.08898721730580138, 'fraction_numerical': 0.00688298918387414, 'mean_word_length': 2.5506108202443283, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2887706', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1135, 'n_words': 225}