Skip to content

Commit

Permalink
payment
Browse files Browse the repository at this point in the history
  • Loading branch information
ziadulislam committed Nov 30, 2022
1 parent 89075b3 commit 668ea94
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
17 changes: 17 additions & 0 deletions androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class AndroidUtils private constructor() {

fun getDebounceUtils(): DebounceUtils = DebounceUtils

fun getPaymentUtils(): PaymentUtils = PaymentUtils


/**
* Quick Access methods
Expand Down Expand Up @@ -80,6 +82,8 @@ class AndroidUtils private constructor() {
fun getJsonFromAsset(context: Context, fileName: String) =
Utils.getJsonFromAsset(context, fileName)

fun validateEmailAddress(email: String?) = Utils.validateEmailAddress(email)


/**
* ApplicationUtil
Expand All @@ -105,6 +109,19 @@ class AndroidUtils private constructor() {
fun decrypt(value: String) = EncryptionUtil.decrypt(value)


/**
* PaymentUtils
*/
fun getIncludingTax(total: Double, tax: Double) = PaymentUtils.getIncludingTax(total, tax)

fun getExcludingTax(total: Double, tax: Double) = PaymentUtils.getExcludingTax(total, tax)

fun twoDigitDouble(value: Double) = PaymentUtils.twoDigitDouble(value)

fun twoDigitString(value: Double) = PaymentUtils.twoDigitString(value)

fun stringToNumber(inputNumber: String) = PaymentUtils.stringToNumber(inputNumber)


/**
* show a Toast in simple way
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.xihad.androidutils.utils

import android.annotation.SuppressLint
import java.text.DecimalFormat
import java.util.*
import kotlin.math.ceil

object PaymentUtils {


fun getIncludingTax(total: Double, tax: Double): Double {
// VAT amount = Value inclusive of tax X tax rate ÷ (100 + tax rate)
return twoDigitDouble(total * tax / (100 + tax))
}

fun getExcludingTax(total: Double, tax: Double): Double {
return twoDigitDouble(total * tax / 100)
}




@SuppressLint("DefaultLocale")
fun twoDigitDouble(value: Double): Double {
var newVal = 0.00
try {
newVal = String.format(" %.2f", value).toDouble()
} catch (e: Exception) {
e.printStackTrace()
}
return newVal
}

@SuppressLint("DefaultLocale")
fun twoDigitString(value: Double): String {
var newVal = ""
newVal = try {
String.format(" %.2f", value)
} catch (e: Exception) {
e.printStackTrace()
value.toString()
}
return newVal
}

fun stringToNumber(inputNumber: String): Double {
var outputNumber = 0.00
try {
outputNumber = inputNumber.toDouble()
} catch (e: NumberFormatException) {
e.printStackTrace()
}
return outputNumber
}

fun getCashOption(totalPrice: Double): List<CashModel> {

val cashList: MutableList<CashModel> = ArrayList()
val decimalFormat = DecimalFormat("#.00")
val currencyArray: MutableList<Double> = ArrayList()
if (totalPrice > 0.00) {
currencyArray.add(twoDigitDouble(totalPrice))
var gtm5 = totalPrice % 5
gtm5 = decimalFormat.format(gtm5).toDouble()
if (gtm5 != 0.0) {
currencyArray.add(twoDigitDouble(totalPrice - gtm5 + 5))
}
var gtm10 = totalPrice % 10
gtm10 = decimalFormat.format(gtm10).toDouble()
if (gtm10 != 0.0) {
currencyArray.add(twoDigitDouble(totalPrice - gtm10 + 10))
}
var gtm20 = totalPrice % 20
if (gtm20 != 0.0) {
gtm20 = decimalFormat.format(gtm20).toDouble()
currencyArray.add(twoDigitDouble(totalPrice - gtm20 + 20))
}
var gtm50 = totalPrice % 50
if (gtm50 != 0.0) {
gtm50 = decimalFormat.format(gtm50).toDouble()
currencyArray.add(twoDigitDouble(totalPrice - gtm50 + 50))
}
if (currencyArray.isNotEmpty() && currencyArray[0] % 1 != 0.0) {
currencyArray.add(1, twoDigitDouble(ceil(currencyArray[0])))
}
val hs: Set<Double> = HashSet(currencyArray)
currencyArray.clear()
currencyArray.addAll(hs)
currencyArray.sort()
if (currencyArray.size == 6) currencyArray.removeAt(5)
currencyArray.add(-5.5)
for (i in currencyArray.indices) {
val cashModel = CashModel()
if (i == 0) {
if (currencyArray[i] == 5.501) {
cashModel.amount = 0.0
cashModel.labelAmount = "Custom"
} else {
cashModel.labelAmount = twoDigitString(currencyArray[i])
cashModel.amount = twoDigitDouble(currencyArray[i])
}
cashList.add(cashModel)
}
if (i == 1) {
if (currencyArray[i] == -5.5) {
cashModel.amount = 0.0
cashModel.labelAmount = "Custom"
} else {
cashModel.labelAmount = twoDigitString(currencyArray[i])
cashModel.amount = twoDigitDouble(currencyArray[i])
}
cashList.add(cashModel)
}
if (i == 2) {
if (currencyArray[i] == -5.5) {
cashModel.amount = 0.0
cashModel.labelAmount = "Custom"
} else {
cashModel.labelAmount = twoDigitString(currencyArray[i])
cashModel.amount = twoDigitDouble(currencyArray[i])
}
cashList.add(cashModel)
}
if (i == 3) {
if (currencyArray[i] == -5.5) {
cashModel.amount = 0.0
cashModel.labelAmount = "Custom"
} else {
cashModel.labelAmount = twoDigitString(currencyArray[i])
cashModel.amount = twoDigitDouble(currencyArray[i])
}
cashList.add(cashModel)
}
if (i == 4) {
if (currencyArray[i] == -5.5) {
cashModel.amount = 0.0
cashModel.labelAmount = "Custom"
} else {
cashModel.labelAmount = twoDigitString(currencyArray[i])
cashModel.amount = twoDigitDouble(currencyArray[i])
}
cashList.add(cashModel)
}
if (i == 5) {
if (currencyArray[i] == -5.5) {
cashModel.amount = 0.0
cashModel.labelAmount = "Custom"
} else {
cashModel.labelAmount = twoDigitString(currencyArray[i])
cashModel.amount = twoDigitDouble(currencyArray[i])
}
cashList.add(cashModel)
}
}
}
return cashList
}


}

class CashModel {
var labelAmount = ""
var amount = 0.0
var isSelected = false
}
10 changes: 10 additions & 0 deletions androidutils/src/main/java/com/xihad/androidutils/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ import java.io.IOException
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.*
import java.util.regex.Pattern

object Utils {


fun validateEmailAddress(email: String?): Boolean {
val address =
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE)
val matcher = email?.let { address.matcher(it) }
return matcher?.find() ?: false
}


fun postDelayed(milliSecond: Long, func: () -> Unit) {
Handler(Looper.getMainLooper()).postDelayed({
func()
Expand Down

0 comments on commit 668ea94

Please sign in to comment.