diff --git a/androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt b/androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt index 9c5b3b2..c7e730e 100644 --- a/androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt +++ b/androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt @@ -51,6 +51,8 @@ class AndroidUtils private constructor() { fun getDebounceUtils(): DebounceUtils = DebounceUtils + fun getPaymentUtils(): PaymentUtils = PaymentUtils + /** * Quick Access methods @@ -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 @@ -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 diff --git a/androidutils/src/main/java/com/xihad/androidutils/utils/PaymentUtils.kt b/androidutils/src/main/java/com/xihad/androidutils/utils/PaymentUtils.kt new file mode 100644 index 0000000..ff1c446 --- /dev/null +++ b/androidutils/src/main/java/com/xihad/androidutils/utils/PaymentUtils.kt @@ -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 { + + val cashList: MutableList = ArrayList() + val decimalFormat = DecimalFormat("#.00") + val currencyArray: MutableList = 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 = 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 +} \ No newline at end of file diff --git a/androidutils/src/main/java/com/xihad/androidutils/utils/Utils.kt b/androidutils/src/main/java/com/xihad/androidutils/utils/Utils.kt index 81ba3c8..eb7441b 100644 --- a/androidutils/src/main/java/com/xihad/androidutils/utils/Utils.kt +++ b/androidutils/src/main/java/com/xihad/androidutils/utils/Utils.kt @@ -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()