POTimeDepositViewModel.swift 6.2 KB

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