@ -18,15 +18,14 @@ import net.pokeranalytics.android.util.TextFormat
import net.pokeranalytics.android.util.extensions.findById
import java.text.DateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.abs
open class Transaction : RealmObject ( ) , RowRepresentable , RowUpdatable , Manageable , StaticRowRepresentableDataSource , TimeFilterable ,
Filterable , DatedBankrollGraphEntry {
companion object {
fun newInstance ( realm : Realm , bankroll : Bankroll , date : Date ? = null , type : TransactionType , amount : Double , comment : String ? = null ) : Transaction {
fun newInstance ( realm : Realm , bankroll : Bankroll , date : Date ? = null , type : TransactionType , amount : Double , comment : String ? = null , destination : Bankroll ? = null , transferRate : Double ? = null ) : Transaction {
val transaction = realm . copyToRealm ( Transaction ( ) )
transaction . date = date ?: Date ( )
@ -34,14 +33,14 @@ open class Transaction : RealmObject(), RowRepresentable, RowUpdatable, Manageab
transaction . type = type
transaction . bankroll = bankroll
transaction . comment = comment ?: " "
transaction . destination = destination
transaction . transferRate = transferRate
return transaction
}
if ( destination != null ) { // we make sure transfers are negative
transaction . amount = abs ( amount ) * - 1
}
val rowRepresentation : List < RowRepresentable > by lazy {
val rows = ArrayList < RowRepresentable > ( )
rows . addAll ( TransactionPropertiesRow . values ( ) )
rows
return transaction
}
fun fieldNameForQueryType ( queryCondition : Class < out QueryCondition > ) : String ? {
@ -88,6 +87,12 @@ open class Transaction : RealmObject(), RowRepresentable, RowUpdatable, Manageab
// A user comment
var comment : String = " "
// The destination Bankroll of a transfer
var destination : Bankroll ? = null
// The rate of the transfer when bankrolls are in different currencies
var transferRate : Double ? = null
// Timed interface
override var dayOfWeek : Int ? = null
override var month : Int ? = null
@ -97,6 +102,11 @@ open class Transaction : RealmObject(), RowRepresentable, RowUpdatable, Manageab
@Ignore
override val viewType : Int = RowViewType . ROW _TRANSACTION . ordinal
val displayAmount : Double
get ( ) { // for transfers we want to show a positive value (in the feed for instance)
return if ( this . destination == null ) { this . amount } else { abs ( this . amount ) }
}
override fun updateValue ( value : Any ? , row : RowRepresentable ) {
when ( row ) {
TransactionPropertiesRow . BANKROLL -> bankroll = value as Bankroll ?
@ -104,11 +114,13 @@ open class Transaction : RealmObject(), RowRepresentable, RowUpdatable, Manageab
TransactionPropertiesRow . AMOUNT -> amount = if ( value == null ) 0.0 else ( value as String ) . toDouble ( )
TransactionPropertiesRow . COMMENT -> comment = value as String ? ?: " "
TransactionPropertiesRow . DATE -> date = value as Date ? ?: Date ( )
TransactionPropertiesRow . DESTINATION -> destination = value as ? Bankroll
TransactionPropertiesRow . RATE -> transferRate = ( value as String ? ) ?. toDouble ( )
}
}
override fun adapterRows ( ) : List < RowRepresentable > ? {
return rowRepresentation
return rowRepresentation ( )
}
override fun isValidForSave ( ) : Boolean {
@ -144,6 +156,24 @@ open class Transaction : RealmObject(), RowRepresentable, RowUpdatable, Manageab
return SaveValidityStatus . VALID
}
fun rowRepresentation ( ) : List < RowRepresentable > {
val rows = ArrayList < RowRepresentable > ( )
when ( this . type ?. kind ) {
TransactionType . Value . TRANSFER . uniqueIdentifier -> {
if ( this . bankroll != null && this . bankroll ?. currency ?. code != this . destination ?. currency ?. code ) {
rows . addAll ( TransactionPropertiesRow . transferRateRows )
} else {
rows . addAll ( TransactionPropertiesRow . transferRows )
}
}
else -> {
rows . addAll ( TransactionPropertiesRow . baseRows )
}
}
return rows
}
// GraphIdentifiableEntry
@Ignore