You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.5 KiB

6 years ago
  1. //
  2. // AutoRefundsViewController.swift
  3. // GME Remit
  4. //
  5. // Created by Mac on 11/20/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class AutoRefundsViewController: UIViewController {
  10. struct StringConstants {
  11. let headerTitleText = "how_much_would_you_like_to_refund".localized()
  12. let availableBalanceTitleText = "available_balance_text".localized()
  13. let refundChargeTitleTxty = "per_refund_bank_charge".localized()
  14. let navigationTitle = "auto_refund_title_text".localized()
  15. let yourRefundText = "you_refund_text".localized()
  16. let refundButtonText = "refund_text".localized()
  17. }
  18. @IBOutlet weak var backgroundView: UIView!
  19. @IBOutlet weak var countryBackgroundView: UIView!
  20. @IBOutlet weak var availableBalanceTitleLabel: UILabel!
  21. @IBOutlet weak var refundChargeTitleLabel: UILabel!
  22. @IBOutlet weak var yourRefundLabel: UILabel!
  23. @IBOutlet weak var refundButton: UIButton!
  24. @IBOutlet weak var availableBalanceLabel: UILabel!
  25. @IBOutlet weak var refundChargeLabel: UILabel!
  26. @IBOutlet weak var amountTextfield: UITextField!
  27. var autorefund: AutoRefund? {
  28. didSet {
  29. let availableBalance = (autorefund?.currentBalance ?? "0") + "(KRW)"
  30. let refundCharge = (autorefund?.chargeAmount ?? "10,000") + "(KRW)"
  31. self.availableBalanceLabel.text = availableBalance
  32. self.refundChargeLabel.text = refundCharge
  33. }
  34. }
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. setup()
  38. // Do any additional setup after loading the view.
  39. }
  40. private func setup() {
  41. setupNormalNavigation()
  42. self.backgroundView.layer.borderColor = UIColor.init(hex: "E0E0E0").cgColor
  43. self.backgroundView.layer.borderWidth = 0.5
  44. self.backgroundView.layer.cornerRadius = 6
  45. self.countryBackgroundView.layer.cornerRadius = 6
  46. configureText()
  47. }
  48. private func configureText() {
  49. self.availableBalanceTitleLabel.text = StringConstants().availableBalanceTitleText + " : "
  50. self.refundChargeLabel.text = StringConstants().refundChargeTitleTxty + " : "
  51. self.yourRefundLabel.text = StringConstants().yourRefundText
  52. self.refundButton.setTitle(StringConstants().refundButtonText, for: UIControlState.normal)
  53. }
  54. override func viewWillAppear(_ animated: Bool) {
  55. super.viewWillAppear(animated)
  56. self.navigationItem.title = StringConstants().navigationTitle
  57. fetchInfo()
  58. }
  59. private func fetchInfo() {
  60. self.showProgressHud()
  61. let username = Utility.getMyUserName() ?? ""
  62. self.fetchAutoRefundInfo(username: username, success: { (autorefund) in
  63. self.autorefund = autorefund
  64. self.hideProgressHud()
  65. }) { (error) in
  66. self.hideProgressHud()
  67. self.show(error: error.localizedDescription)
  68. }
  69. }
  70. @IBAction func refund(_ sender: Any) {
  71. let amount = self.amountTextfield.text!
  72. let minAmnt = self.autorefund?.minAmount ?? "0"
  73. if amount.isEmpty {self.show(error: "add refund amount"); return}
  74. if !isValidRefundable(amount: amount) {
  75. self.show(error: "Amount cannot be minimum than \(Utility.getCommaSeperatedString(numberString: minAmnt) ?? "")")
  76. return
  77. }
  78. let chargeAmt = self.autorefund?.chargeAmount ?? ""
  79. let userName = Utility.getMyUserName()
  80. let id = Utility.getMyId()
  81. self.showProgressHud()
  82. self.refund(amount: amount, userName: userName, chargeAmount: chargeAmt, userId: id, success: { (successMessage) in
  83. self.hideProgressHud()
  84. self.alertWithOk(message: successMessage?.message, title: "Success", okTitle: "Ok", style: UIAlertControllerStyle.alert, OkStyle: UIAlertActionStyle.default, okAction: {
  85. self.navigationController?.popViewController(animated: true)
  86. self.updateBalance(value: successMessage)
  87. })
  88. }) { (error) in
  89. self.hideProgressHud()
  90. self.show(error: error.localizedDescription)
  91. }
  92. }
  93. private func updateBalance(value: SuccessMessage?) {
  94. let balance = value?.extra ?? ""
  95. let yearlyLimits = value?.yearlyLimit ?? ""
  96. UserDefaults.standard.set(yearlyLimits, forKey: UserKeys.yearlyLimit)
  97. UserDefaults.standard.set(balance, forKey: UserKeys.availableBalance)
  98. let userInfo = [SideMenuNavigationNotifications.availableBalance : balance]
  99. NotificationCenter.default.post(name: self.getAvailableBalanceNotificationName(), object: nil, userInfo: userInfo)
  100. }
  101. func getAvailableBalanceNotificationName() -> Notification.Name {
  102. return Notification.Name.init(rawValue: SideMenuNavigationNotifications.availableBalance)
  103. }
  104. private func isValidRefundable(amount: String) -> Bool {
  105. let _minRefundAmount = self.autorefund?.minAmount ?? "0"
  106. let minRefundAmount = _minRefundAmount.replacingOccurrences(of: ",", with: "")
  107. print(minRefundAmount)
  108. guard let amt = Double(amount), let minRefundAmt = Double(minRefundAmount) else {return false}
  109. let result = amt >= minRefundAmt
  110. return result
  111. }
  112. func show(error: String) {
  113. self.alert(message: error)
  114. }
  115. }
  116. extension AutoRefundsViewController: FetchAutoRefundInfo {
  117. }
  118. extension AutoRefundsViewController: RefundService {
  119. }