SeniorCitizenSavingViewModel.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // SeniorCitizenSavingViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 05/12/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class SeniorCitizenSavingViewModel {
  10. var delegate: SeniorCitizenSavingViewProtocol?
  11. var scssModel = [SeniorCitizenSavingModel]()
  12. //POST SCSS Interest API
  13. func getInterestRate(depositDate:String) {
  14. if let delegate = delegate {
  15. delegate.startLoader()
  16. let url = ApiUrl.BASE_URL + ApiUrl.GET_SCSS_INTEREST_RATE
  17. let parameters: [String: String] = [
  18. "depositDate": depositDate
  19. ]
  20. print(parameters)
  21. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  22. switch response.result {
  23. case .success(let value):
  24. delegate.stopLoader()
  25. print(value)
  26. let data = value as! NSDictionary
  27. print(data)
  28. DispatchQueue.main.async {
  29. let interestRate = data["interestRate"] as! Double
  30. delegate.setInterestRate(rate: String(interestRate))
  31. }
  32. case .failure(let error):
  33. delegate.stopLoader()
  34. print(error)
  35. //delegate.showError(error: error)
  36. }
  37. }
  38. }
  39. }
  40. //POST SCSS API
  41. func getSCSSOutput(depositAmount:String, years:String, interestFrequency:String, depositDate:String) {
  42. if let delegate = delegate {
  43. delegate.startLoader()
  44. let url = ApiUrl.BASE_URL + ApiUrl.GET_SCSS_OUTPUT
  45. var interest = String()
  46. switch interestFrequency {
  47. case "Monthly":
  48. interest = "12"
  49. case "Bi-Monthly":
  50. interest = "6"
  51. case "Quarterly":
  52. interest = "4"
  53. case "Triannually":
  54. interest = "3"
  55. case "Half Yearly":
  56. interest = "2"
  57. case "Annually":
  58. interest = "1"
  59. default:
  60. interest = ""
  61. }
  62. let parameters: [String: String] = [
  63. "deposit": depositAmount,
  64. "years": years,
  65. "interestFrequency": interest,
  66. "depositDate": depositDate
  67. ]
  68. print(parameters)
  69. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  70. switch response.result {
  71. case .success(let value):
  72. delegate.stopLoader()
  73. print(value)
  74. let data = value as! NSDictionary
  75. print(data)
  76. DispatchQueue.main.async {
  77. let maturityDisplayDate = data["maturityDisplayDate"] as? String
  78. let interestRate = data["interestRate"] as! Double
  79. let interestReceived = data["interestReceived"] as! Double
  80. let totalInterestReceived = data["totalInterestReceived"] as! Double
  81. let maturityDate = data["maturityDate"] as! String
  82. let seniorCitizenSavingSchemeLookupsList = data["seniorCitizenSavingSchemeLookupsList"] as! [NSDictionary]
  83. let scssData = SeniorCitizenSavingModel(maturityDate: maturityDate, maturityDisplayDate: maturityDisplayDate ?? "", interestReceived: interestReceived, totalInterestReceived: totalInterestReceived, interestRate: interestRate)
  84. for list in seniorCitizenSavingSchemeLookupsList {
  85. let serialNo = list.value(forKey: "serialNo") as! NSNumber
  86. let referenceDate = list.value(forKey: "referenceDate") as! String
  87. let referenceMonth = list.value(forKey: "referenceMonth") as! String
  88. let financialYear = list.value(forKey: "financialYear") as! String
  89. let amountDeposited = list.value(forKey: "amountDeposited") as! Double
  90. let interestReceived = list.value(forKey: "interestReceived") as! Double
  91. let totalInterestReceived = list.value(forKey: "totalInterestReceived") as! Double
  92. let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
  93. let list = SCSSLookupList(serialNo: Int(truncating: serialNo), amountDeposited: amountDeposited, totalInterestReceived: totalInterestReceived, interestReceived: interestReceived, daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear)
  94. scssData.seniorCitizenSavingSchemeLookupsList.append(list)
  95. }
  96. self.scssModel.append(scssData)
  97. delegate.navigate(scssData)
  98. }
  99. case .failure(let error):
  100. delegate.stopLoader()
  101. print(error)
  102. //delegate.showError(error: error)
  103. }
  104. }
  105. }
  106. }
  107. }
  108. protocol SeniorCitizenSavingViewProtocol {
  109. func startLoader()
  110. func stopLoader()
  111. func showError(error:String)
  112. func setInterestRate(rate:String)
  113. func navigate(_ data:SeniorCitizenSavingModel)
  114. }