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.

193 lines
6.1 KiB

5 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 headerTitleLabel: UILabel!
  24. @IBOutlet weak var refundButton: UIButton!
  25. @IBOutlet weak var availableBalanceLabel: UILabel!
  26. @IBOutlet weak var refundChargeLabel: UILabel!
  27. @IBOutlet weak var amountTextfield: GMENumberTextField!
  28. var autorefund: AutoRefund? {
  29. didSet {
  30. let availableBalance = (autorefund?.currentBalance ?? "0") + "(KRW)"
  31. let refundCharge = (autorefund?.chargeAmount ?? "10,000") + "(KRW)"
  32. self.availableBalanceLabel.text = availableBalance
  33. self.refundChargeLabel.text = refundCharge
  34. }
  35. }
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. setup()
  39. // Do any additional setup after loading the view.
  40. }
  41. private func setup() {
  42. setupNormalNavigation()
  43. self.backgroundView.layer.borderColor = UIColor.init(hex: "E0E0E0").cgColor
  44. self.backgroundView.layer.borderWidth = 0.5
  45. self.backgroundView.layer.cornerRadius = 6
  46. self.countryBackgroundView.layer.cornerRadius = 6
  47. configureText()
  48. amountTextfield.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
  49. headerTitleLabel.textColor = .themeRed
  50. refundButton.backgroundColor = .themeRed
  51. amountTextfield.textColor = .themeRed
  52. refundButton.layer.cornerRadius = 5
  53. }
  54. @objc private func editingChanged(_ sender: UITextField) {
  55. amountTextfield.text = Utility.getCommaSeperatedString(numberString: amountTextfield.text!)
  56. }
  57. private func configureText() {
  58. self.availableBalanceTitleLabel.text = StringConstants().availableBalanceTitleText + " : "
  59. self.refundChargeTitleLabel.text = StringConstants().refundChargeTitleTxty + " : "
  60. self.yourRefundLabel.text = StringConstants().yourRefundText
  61. self.refundButton.setTitle(StringConstants().refundButtonText, for: UIControl.State.normal)
  62. self.headerTitleLabel.text = StringConstants().headerTitleText
  63. }
  64. override func viewWillAppear(_ animated: Bool) {
  65. super.viewWillAppear(animated)
  66. self.navigationItem.title = StringConstants().navigationTitle
  67. fetchInfo()
  68. }
  69. override func viewDidAppear(_ animated: Bool) {
  70. super.viewDidAppear(animated)
  71. amountTextfield.becomeFirstResponder()
  72. }
  73. override func viewWillDisappear(_ animated: Bool) {
  74. super.viewWillAppear(animated)
  75. view.endEditing(true)
  76. }
  77. private func fetchInfo() {
  78. self.showProgressHud()
  79. let username = Utility.getMyUserName()
  80. self.fetchAutoRefundInfo(
  81. username: username,
  82. success: { (autorefund) in
  83. self.autorefund = autorefund
  84. self.hideProgressHud()
  85. },
  86. failure: { (error) in
  87. self.hideProgressHud()
  88. self.alertWithOk(
  89. type: .error,
  90. message: error.localizedDescription,
  91. okTitle: "ok_text".localized()) {
  92. self.navigationController?.popViewController(animated: true)
  93. }
  94. self.show(error: error.localizedDescription)
  95. }
  96. )
  97. }
  98. @IBAction func refund(_ sender: Any) {
  99. guard let amount = self.amountTextfield.text?.stringRemovingComma() else { return }
  100. let minAmnt = self.autorefund?.minAmount ?? "0"
  101. if amount.isEmpty {
  102. self.show(error: "add refund amount")
  103. return
  104. }
  105. if !isValidRefundable(amount: amount) {
  106. let min = Utility.getCommaSeperatedString(numberString: minAmnt) ?? ""
  107. self.show(error: "Amount cannot be minimum than \(min)")
  108. return
  109. }
  110. let chargeAmt = self.autorefund?.chargeAmount ?? ""
  111. let userName = Utility.getMyUserName()
  112. let id = Utility.getMyId()
  113. self.showProgressHud()
  114. self.refund(
  115. amount: amount,
  116. userName: userName,
  117. chargeAmount: chargeAmt,
  118. userId: id,
  119. success: { (successMessage) in
  120. self.hideProgressHud()
  121. self.alertWithOk(
  122. type: .normal,
  123. message: successMessage?.message,
  124. title: "Success",
  125. okTitle: "Ok",
  126. okAction: {
  127. self.navigationController?.popViewController(animated: true)
  128. self.updateBalance(value: successMessage)
  129. })
  130. },
  131. failure: { (error) in
  132. self.hideProgressHud()
  133. self.show(error: error.localizedDescription)
  134. }
  135. )
  136. }
  137. private func updateBalance(value: SuccessMessage?) {
  138. let balance = value?.extra ?? ""
  139. GMEDB.shared.user.set(balance, .availableBalance)
  140. let userInfo = [SideMenuNavigationNotifications.availableBalance : balance]
  141. NotificationCenter.default.post(
  142. name: self.getAvailableBalanceNotificationName(),
  143. object: nil,
  144. userInfo: userInfo
  145. )
  146. }
  147. func getAvailableBalanceNotificationName() -> Notification.Name {
  148. return Notification.Name.init(rawValue: SideMenuNavigationNotifications.availableBalance)
  149. }
  150. private func isValidRefundable(amount: String) -> Bool {
  151. let minRefundAmount = (self.autorefund?.minAmount ?? "0").replacingOccurrences(of: ",", with: "")
  152. guard let amt = Double(amount), let minRefundAmt = Double(minRefundAmount) else {return false}
  153. let result = amt >= minRefundAmt
  154. return result
  155. }
  156. func show(error: String) {
  157. self.alert(type: .error, message: error)
  158. }
  159. }
  160. extension AutoRefundsViewController: FetchAutoRefundInfo {}
  161. extension AutoRefundsViewController: RefundService {}