<commit_before>//
//  AddInvoiceViewController.swift
//  iTax
//
//  Created by <NAME> on 13.06.2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa
import RealmSwift

class AddInvoiceViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

  @IBOutlet weak var numberField: NSTextField!
  @IBOutlet weak var issueDateField: NSDatePicker!
  @IBOutlet weak var paymentDeadlineField: NSDatePicker!
  
  @IBOutlet weak var fullNameField: NSTextField!
  @IBOutlet weak var shortNameField: NSTextField!
  @IBOutlet weak var streetAddressField: NSTextField!
  @IBOutlet weak var zipSmallField: NSTextField!
  @IBOutlet weak var zipBigField: NSTextField!
  @IBOutlet weak var cityField: NSTextField!
  
  @IBOutlet weak var itemsTable: NSTableView!
  
  var invoicesViewController: InvoicesViewController?
  var invoiceType: InvoiceType = InvoiceType.CostInvoice {
    didSet {
      if oldValue != InvoiceType.IncomeInvoice && invoiceType == InvoiceType.IncomeInvoice {
        numberField.stringValue = generateInvoiceNumber()
        numberField.enabled = false
      }
    }
  }
  var items: [InvoiceItem] = []
  lazy var realm = try! Realm()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    itemsTable.setDelegate(self)
    itemsTable.setDataSource(self)
    
    resetDates()
  }
  
  // MARK: - private
  
  private func resetDates() {
    issueDateField.dateValue = NSDate()
    
    let calendar = NSCalendar.currentCalendar()
    let dayComponent = NSDateComponents()
    dayComponent.day = 5
    
    guard let paymentDate = calendar.dateByAddingComponents(dayComponent, toDate: NSDate(), options: []) else { return }
    
    paymentDeadlineField.dateValue = paymentDate
  }
  
  private func addEmptyItem() {
    items.append(InvoiceItem())
    itemsTable.reloadData()
  }
  
  private func closeWindow() {
    let application = NSApplication.sharedApplication()
    application.abortModal()
    self.view.window?.close()
    invoicesViewController?.reloadInvoices()
  }
  
  private func persistChangesTo(sender: NSTextField) {
    let cellView = sender.superview as! NSTableCellView
    
    guard let identifier = cellView.identifier else {
      return
    }
    
    let row = itemsTable.selectedRow
    
    switch identifier {
    case "NameCellID":
      items[row].name = sender.stringValue
    case "UnitCellID":
      items[row].unit = sender.stringValue
    case "QuantityCellID":
      if let quantity = Int(sender.stringValue) {
        items[row].quantity = quantity
      }
    case "TaxCellID":
      if let taxRate = Int(sender.stringValue.stringByReplacingOccurrencesOfString("%", withString: "")) {
        items[row].taxRate = taxRate
      }
    case "NetCellID":
      if let netPrice = Double(sender.stringValue) {
        items[row].netPrice = netPrice
      }
    case "GrossCellID":
      if let grossPrice = Double(sender.stringValue) {
        items[row].grossPrice = grossPrice
      }
    default: break
    }
    
    itemsTable.reloadData()
  }
  
  private func generateInvoiceNumber() -> String {
    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let year = calendar.component(.Year, fromDate: date)
    
    let (dateFrom, dateTo) = yearBoundariesFor(calendar, year: year)
    
    guard let from = dateFrom, to = dateTo else {
      return ""
    }
    
    let typePredicate = NSPredicate(format: "typeRaw = %@", InvoiceType.IncomeInvoice.rawValue)
    let datePredicate = NSPredicate(format: "issueDate >= %@ AND issueDate <= %@", from, to)
    let count = realm.objects(Invoice.self).filter(typePredicate).filter(datePredicate).count
    return "\(count+1)/\(year)"
  }
  
  private func yearBoundariesFor(calendar: NSCalendar, year: Int) -> (NSDate?, NSDate?) {
    let components = NSDateComponents()
    components.year = year
    components.month = 1
    components.day = 1
    let from = calendar.dateFromComponents(components)
    components.month = 12
    components.day = 31
    components.hour = 23
    components.minute = 59
    components.second = 59
    let to = calendar.dateFromComponents(components)
    
    return (from, to)
  }
  
  private func saveInvoice() -> Bool {
    let invoice = Invoice()
    invoice.number = numberField.stringValue
    invoice.issueDate = issueDateField.dateValue
    invoice.paymentDeadline = paymentDeadlineField.dateValue
    
    invoice.customerFullName = fullNameField.stringValue
    invoice.customerShortName = shortNameField.stringValue
    invoice.customerAddressCity = cityField.stringValue
    invoice.customerAddressStreet = streetAddressField.stringValue
    let postalCode = "\(zipSmallField.stringValue)-\(zipBigField.stringValue)"<commit_msg>Fix invoice type assignment<commit_after>//
//  AddInvoiceViewController.swift
//  iTax
//
//  Created by <NAME> on 13.06.2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa
import RealmSwift

class AddInvoiceViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

  @IBOutlet weak var numberField: NSTextField!
  @IBOutlet weak var issueDateField: NSDatePicker!
  @IBOutlet weak var paymentDeadlineField: NSDatePicker!
  
  @IBOutlet weak var fullNameField: NSTextField!
  @IBOutlet weak var shortNameField: NSTextField!
  @IBOutlet weak var streetAddressField: NSTextField!
  @IBOutlet weak var zipSmallField: NSTextField!
  @IBOutlet weak var zipBigField: NSTextField!
  @IBOutlet weak var cityField: NSTextField!
  
  @IBOutlet weak var itemsTable: NSTableView!
  
  var invoicesViewController: InvoicesViewController?
  var invoiceType: InvoiceType = InvoiceType.CostInvoice {
    didSet {
      if oldValue != InvoiceType.IncomeInvoice && invoiceType == InvoiceType.IncomeInvoice {
        numberField.stringValue = generateInvoiceNumber()
        numberField.enabled = false
      }
    }
  }
  var items: [InvoiceItem] = []
  lazy var realm = try! Realm()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    itemsTable.setDelegate(self)
    itemsTable.setDataSource(self)
    
    resetDates()
  }
  
  // MARK: - private
  
  private func resetDates() {
    issueDateField.dateValue = NSDate()
    
    let calendar = NSCalendar.currentCalendar()
    let dayComponent = NSDateComponents()
    dayComponent.day = 5
    
    guard let paymentDate = calendar.dateByAddingComponents(dayComponent, toDate: NSDate(), options: []) else { return }
    
    paymentDeadlineField.dateValue = paymentDate
  }
  
  private func addEmptyItem() {
    items.append(InvoiceItem())
    itemsTable.reloadData()
  }
  
  private func closeWindow() {
    let application = NSApplication.sharedApplication()
    application.abortModal()
    self.view.window?.close()
    invoicesViewController?.reloadInvoices()
  }
  
  private func persistChangesTo(sender: NSTextField) {
    let cellView = sender.superview as! NSTableCellView
    
    guard let identifier = cellView.identifier else {
      return
    }
    
    let row = itemsTable.selectedRow
    
    switch identifier {
    case "NameCellID":
      items[row].name = sender.stringValue
    case "UnitCellID":
      items[row].unit = sender.stringValue
    case "QuantityCellID":
      if let quantity = Int(sender.stringValue) {
        items[row].quantity = quantity
      }
    case "TaxCellID":
      if let taxRate = Int(sender.stringValue.stringByReplacingOccurrencesOfString("%", withString: "")) {
        items[row].taxRate = taxRate
      }
    case "NetCellID":
      if let netPrice = Double(sender.stringValue) {
        items[row].netPrice = netPrice
      }
    case "GrossCellID":
      if let grossPrice = Double(sender.stringValue) {
        items[row].grossPrice = grossPrice
      }
    default: break
    }
    
    itemsTable.reloadData()
  }
  
  private func generateInvoiceNumber() -> String {
    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let year = calendar.component(.Year, fromDate: date)
    
    let (dateFrom, dateTo) = yearBoundariesFor(calendar, year: year)
    
    guard let from = dateFrom, to = dateTo else {
      return ""
    }
    
    let typePredicate = NSPredicate(format: "typeRaw = %@", InvoiceType.IncomeInvoice.rawValue)
    let datePredicate = NSPredicate(format: "issueDate >= %@ AND issueDate <= %@", from, to)
    let count = realm.objects(Invoice.self).filter(typePredicate).filter(datePredicate).count
    return "\(count+1)/\(year)"
  }
  
  private func yearBoundariesFor(calendar: NSCalendar, year: Int) -> (NSDate?, NSDate?) {
    let components = NSDateComponents()
    components.year = year
    components.month = 1
    components.day = 1
    let from = calendar.dateFromComponents(components)
    components.month = 12
    components.day = 31
    components.hour = 23
    components.minute = 59
    components.second = 59
    let to = calendar.dateFromComponents(components)
    
    return (from, to)
  }
  
  private func saveInvoice() -> Bool {
    let invoice = Invoice()
    invoice.type = invoiceType
    invoice.number = numberField.stringValue
    invoice.issueDate = issueDateField.dateValue
    invoice.paymentDeadline = paymentDeadlineField.dateValue
    
    invoice.customerFullName = fullNameField.stringValue
    invoice.customerShortName = shortNameField.stringValue
    invoice.customerAddressCity = cityField.stringValue
    invoice.customerAddressStreet = streetAddressField.stringValue
    let postalCode = "\(zipSmallField.stringValue)-\(zipBigField.stringValue)"
{'fraction_non_alphanumeric': 0.09471805103433792, 'fraction_numerical': 0.005460464139451854, 'mean_word_length': 3.3173164097914776, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 15, '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': '3205159', 'n_tokens_mistral': 2943, 'n_tokens_neox': 2844, 'n_words': 638}