Skip to content

Commit

Permalink
write bill class. closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
moeindev committed Apr 25, 2021
1 parent 4d59ef0 commit 8cca635
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/main/kotlin/dev/persianTools/feature/bill/Bill.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package dev.persianTools.feature.bill

/**
* Bill class
*/
data class Bill(
var barcode: String? = null,
val currency: BillCurrency,
val billId: Int?,
val paymentId: Int?
): BillResult {

companion object {
/**
* Find bill ID and payment ID using barcode
* @sample
* val (billID,paymentID) = Bill.findByBarcode(barcode)
*/
fun findByBarcode(barcode: String): Pair<Int?,Int?> {
return Pair(
barcode.substring(0,13).toIntOrNull(),
barcode.substring(16).toIntOrNull()
)
}
}

override fun amount(): Int? {
if (paymentId == null) return null

val currencyAmount = if (currency == BillCurrency.RIAL) 1000 else 100

return (paymentId / 100000) * currencyAmount
}

override fun type(): BillType {
val billIdStr = billId.toString()

return billIdStr.substring(billIdStr.length - 2,
billIdStr.length -1).toIntOrNull()?.billType() ?: BillType.UNKNOWN
}

override fun barcode(): String? {
if (billId == null || paymentId == null) return null

val br = "${billId}000${paymentId}"

if (barcode == null) barcode = br

return br
}

override fun isValid(): Boolean = isValidBillId() && isValidBillPayment()

override fun isValidBillId(): Boolean {
var billIdStr = billId.toString()

if (billIdStr.length < 6) return false

val firstControlBit = billIdStr[billIdStr.length - 1]

billIdStr = billIdStr.substring(0,billIdStr.length - 1)

return calTheBit(billIdStr) == firstControlBit.toInt() && type() != BillType.UNKNOWN
}

override fun isValidBillPayment(): Boolean {
val billIdStr = billId.toString()
var paymentIdStr = paymentId.toString()

if (paymentIdStr.length < 6) return false

val firstControlBit = paymentIdStr[paymentIdStr.length - 2]
val secondControlBit = paymentIdStr[paymentIdStr.length - 1]

paymentIdStr = paymentIdStr.substring(0,paymentIdStr.length - 2)

return calTheBit(paymentIdStr) == firstControlBit.toInt() &&
calTheBit(billIdStr + paymentIdStr + firstControlBit) ==
secondControlBit.toInt()
}

private fun calTheBit(number: String): Int {
var sum = 0
var base = 2
number.forEachIndexed { index, _ ->
if (base == 8) base = 2

val subString = number.substring(
number.length - 1 - index,
number.length - index
).toInt()

sum += subString * base

base++
}
sum %= 11

sum = if (sum < 2) {
0
} else {
11 - sum
}
return sum
}
}

0 comments on commit 8cca635

Please sign in to comment.