PONationalSavingViewModel.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // PONationalSavingViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 02/12/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class PONationalSavingViewModel {
  10. var delegate: PONationalSavingViewProtocol?
  11. var poNSCModel = [PONationalSavingModel]()
  12. //POST PO National Saving Certificates Interest API
  13. func getPoNSCInterest(depositDate:String) {
  14. if let delegate = delegate {
  15. delegate.startLoader()
  16. let url = ApiUrl.BASE_URL + ApiUrl.GET_PO_NATIONAL_SAVING_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 PO National Saving Certificates API
  41. func getPoNSCOutput(openingAmount:String, annualInterest:String, tenure:String, payoutFrequency:String, depositDate:String) {
  42. if let delegate = delegate {
  43. delegate.startLoader()
  44. let url = ApiUrl.BASE_URL + ApiUrl.GET_PO_NATIONAL_SAVING
  45. var interest = String()
  46. switch payoutFrequency {
  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. "openingAmount": openingAmount,
  64. "annualInterest": annualInterest,
  65. "tenure": tenure,
  66. "payoutFrequency": interest,
  67. "depositDate": depositDate
  68. ]
  69. print(parameters)
  70. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  71. switch response.result {
  72. case .success(let value):
  73. delegate.stopLoader()
  74. print(value)
  75. let data = value as! NSDictionary
  76. print(data)
  77. DispatchQueue.main.async {
  78. let depositAmount = data["depositAmount"] as! Double
  79. let annualInterest = data["annualInterest"] as! Double
  80. let term = data["term"] as! Int
  81. let compundFreq = data["compundFreq"] as! Double
  82. let depositDate = data["depositDate"] as? String
  83. let maturityAmount = data["maturityAmount"] as! Double
  84. let interestReceived = data["interestReceived"] as! Double
  85. let totalInterestReceived = data["totalInterestReceived"] as! Double
  86. let maturityDate = data["maturityDate"] as! String
  87. let poNScLookupList = data["poNScLookupList"] as! [NSDictionary]
  88. let poNScData = PONationalSavingModel(depositDate: depositDate ?? "", maturityDate: maturityDate, term: term, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, maturityAmount: maturityAmount, totalInterestReceived: totalInterestReceived, interestReceived: interestReceived)
  89. for list in poNScLookupList {
  90. let serialNo = list.value(forKey: "serialNo") as! NSNumber
  91. let referenceDate = list.value(forKey: "referenceDate") as! String
  92. let referenceMonth = list.value(forKey: "referenceMonth") as! String
  93. let financialYear = list.value(forKey: "financialYear") as! String
  94. let openingBal = list.value(forKey: "openingBal") as! Double
  95. let amountDeposited = list.value(forKey: "amountDeposited") as! Double
  96. let interestAccrued = list.value(forKey: "interestAccrued") as! Double
  97. let interestCredited = list.value(forKey: "interestCredited") as! Double
  98. let totalInterestReceived = list.value(forKey: "totalInterestReceived") as! Double
  99. let closingBalance = list.value(forKey: "closingBalance") as! Double
  100. let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
  101. let list = PONSCLookupList(serialNo: Int(truncating: serialNo), openingBal: openingBal, amountDeposited: amountDeposited, interestAccrued: interestAccrued, totalInterestReceived: totalInterestReceived, interestCredited: interestCredited, daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear, closingBalance: closingBalance)
  102. poNScData.poNScLookupList.append(list)
  103. }
  104. self.poNSCModel.append(poNScData)
  105. delegate.navigate(poNScData)
  106. }
  107. case .failure(let error):
  108. delegate.stopLoader()
  109. print(error)
  110. //delegate.showError(error: error)
  111. }
  112. }
  113. }
  114. }
  115. }
  116. protocol PONationalSavingViewProtocol {
  117. func startLoader()
  118. func stopLoader()
  119. func showError(error:String)
  120. func setInterestRate(rate:String)
  121. func navigate(_ data:PONationalSavingModel)
  122. }