EPFViewModel.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // EPFViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 08/12/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class EPFViewModel {
  10. var delegate: EPFViewProtocol?
  11. var epfModel = [EPFModel]()
  12. //POST EPF Interest Rate API
  13. func getInterestRate() {
  14. if let delegate = delegate {
  15. delegate.startLoader()
  16. let url = ApiUrl.BASE_URL + ApiUrl.GET_EPF_INTEREST_RATE
  17. AF.request(url, method: .get, encoding: JSONEncoding.default).responseJSON { response in
  18. switch response.result {
  19. case .success(let value):
  20. delegate.stopLoader()
  21. print(value)
  22. let data = value as! NSDictionary
  23. print(data)
  24. DispatchQueue.main.async {
  25. let interestRate = data["interestRate"] as! Double
  26. delegate.setInterestRate(rate: String(interestRate))
  27. }
  28. case .failure(let error):
  29. delegate.stopLoader()
  30. print(error)
  31. //delegate.showError(error: error)
  32. }
  33. }
  34. }
  35. }
  36. //POST EPF Growth Rate API
  37. func getGrowthRate() {
  38. if let delegate = delegate {
  39. delegate.startLoader()
  40. let url = ApiUrl.BASE_URL + ApiUrl.GET_EPF_GROWTH_RATE
  41. AF.request(url, method: .get, encoding: JSONEncoding.default).responseJSON { response in
  42. switch response.result {
  43. case .success(let value):
  44. delegate.stopLoader()
  45. print(value)
  46. let data = value as! NSDictionary
  47. print(data)
  48. DispatchQueue.main.async {
  49. let interestRate = data["interestRate"] as! Double
  50. delegate.setGrowthRate(rate: String(interestRate))
  51. }
  52. case .failure(let error):
  53. delegate.stopLoader()
  54. print(error)
  55. //delegate.showError(error: error)
  56. }
  57. }
  58. }
  59. }
  60. //POST EPF API
  61. func getEPFOutput(clientDOB:String, currEPFBal:String, currEPSBal:String, monthlyBasicDA:String, expectedIncreaseSal:String, contributionUptoAge:String, epfWithdrawAlage:String, increaseMonth:String) {
  62. if let delegate = delegate {
  63. delegate.startLoader()
  64. let url = ApiUrl.BASE_URL + ApiUrl.GET_EPF_DETAILS
  65. let parameters: [String: String] = [
  66. "clientDOB": clientDOB,
  67. "currEPFBal": currEPFBal,
  68. "currEPSBal": currEPSBal,
  69. "monthlyBasicDA": monthlyBasicDA,
  70. "expectedIncreaseSal": expectedIncreaseSal,
  71. "contributionUptoAge": contributionUptoAge,
  72. "epfWithdrawAlage": epfWithdrawAlage,
  73. "increaseMonth": increaseMonth,
  74. ]
  75. print(parameters)
  76. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  77. switch response.result {
  78. case .success(let value):
  79. delegate.stopLoader()
  80. print(value)
  81. let data = value as! NSDictionary
  82. print(data)
  83. DispatchQueue.main.async {
  84. let clientDOB = data["clientDOB"] as? String
  85. let currEPF = data["currEPF"] as! Double
  86. let currEPS = data["currEPS"] as! Double
  87. let monBasicDA = data["monBasicDA"] as! Double
  88. let expSalInc = data["expSalInc"] as! Double
  89. let expEPFO = data["expEPFO"] as! Double
  90. let conUptoAge = data["conUptoAge"] as! Int
  91. let epfWiDAge = data["epfWiDAge"] as! Int
  92. let planDate = data["planDate"] as? String
  93. let salIncreaseMonth = data["salIncreaseMonth"] as! Int
  94. let epfBalWithdraw = data["epfBalWithdraw"] as! Double
  95. let epsBalClientAge = data["epsBalClientAge"] as! Double
  96. let totalInterestEarned = data["totalInterestEarned"] as! Double
  97. let employeeCont = data["employeeCont"] as! Double
  98. let employerCont = data["employerCont"] as! Double
  99. let currentInterestRate = data["currentInterestRate"] as! Double
  100. let lookupList = data["lookupList"] as! [NSDictionary]
  101. let epfData = EPFModel(conUptoAge: conUptoAge, epfWiDAge: epfWiDAge, salIncreaseMonth: salIncreaseMonth, currEPF: currEPF, currEPS: currEPS, monBasicDA: monBasicDA, expSalInc: expSalInc, expEPFO: expEPFO, epfBalWithdraw: epfBalWithdraw, epsBalClientAge: epsBalClientAge, totalInterestEarned: totalInterestEarned, employeeCont: employeeCont, employerCont: employerCont, currentInterestRate: currentInterestRate)
  102. for list in lookupList {
  103. let serialNumber = list.value(forKey: "serialNumber") as! NSNumber
  104. let refDate = list.value(forKey: "refDate") as! String
  105. let financialYear = list.value(forKey: "financialYear") as! String
  106. let referenceMonth = list.value(forKey: "referenceMonth") as! String
  107. let clientAge = list.value(forKey: "clientAge") as! Int
  108. let monBasicDA = list.value(forKey: "monBasicDA") as! Double
  109. let openingBalEPF = list.value(forKey: "openingBalEPF") as! Double
  110. let openingBalEPS = list.value(forKey: "openingBalEPS") as! Double
  111. let employeeContEPF = list.value(forKey: "employeeContEPF") as! Double
  112. let employerContEPF = list.value(forKey: "employerContEPF") as! Double
  113. let employerContEPS = list.value(forKey: "employerContEPS") as! Double
  114. let interestRateEPF = list.value(forKey: "interestRateEPF") as! Double
  115. let interestEarnedEPF = list.value(forKey: "interestEarnedEPF") as! Double
  116. let totalInterestEarnedEPF = list.value(forKey: "totalInterestEarnedEPF") as! Double
  117. let closingBalEPF = list.value(forKey: "closingBalEPF") as! Double
  118. let closingBalEPS = list.value(forKey: "closingBalEPS") as! Double
  119. let displayDate = list.value(forKey: "displayDate") as! String
  120. let list = EPFLookupList(serialNumber: Int(truncating: serialNumber), clientAge: clientAge, refDate: refDate, financialYear: financialYear, referenceMonth: referenceMonth, displayDate: displayDate, monBasicDA: monBasicDA, openingBalEPF: openingBalEPF, openingBalEPS: openingBalEPS, employeeContEPF: employeeContEPF, employerContEPF: employerContEPF, employerContEPS: employerContEPS, interestRateEPF: interestRateEPF, interestEarnedEPF: interestEarnedEPF, totalInterestEarnedEPF: totalInterestEarnedEPF, closingBalEPF: closingBalEPF, closingBalEPS: closingBalEPS)
  121. epfData.lookupList.append(list)
  122. }
  123. self.epfModel.append(epfData)
  124. delegate.navigate(epfData)
  125. }
  126. case .failure(let error):
  127. delegate.stopLoader()
  128. print(error)
  129. //delegate.showError(error: error)
  130. }
  131. }
  132. }
  133. }
  134. }
  135. protocol EPFViewProtocol {
  136. func startLoader()
  137. func stopLoader()
  138. func showError(error:String)
  139. func setInterestRate(rate:String)
  140. func setGrowthRate(rate:String)
  141. func navigate(_ data:EPFModel)
  142. }