KisanVikasPatraViewModel.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // KisanVikasPatraViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 03/12/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class KisanVikasPatraViewModel {
  10. var delegate: KisanVikasPatraViewProtocol?
  11. var kvpModel = [KisanVikasPatraModel]()
  12. //POST PO National Saving Certificates Interest API
  13. func getKVPInterest(depositDate:String) {
  14. if let delegate = delegate {
  15. delegate.startLoader()
  16. let url = ApiUrl.BASE_URL + ApiUrl.GET_KVP_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 Kisan vikas patra API
  41. func getKVPOutput(depositAmount:String, depositDate:String) {
  42. if let delegate = delegate {
  43. delegate.startLoader()
  44. let url = ApiUrl.BASE_URL + ApiUrl.GET_KISAN_VIKAS_PATRA
  45. let parameters: [String: String] = [
  46. "deposit": depositAmount,
  47. "depositDate": depositDate
  48. ]
  49. print(parameters)
  50. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  51. switch response.result {
  52. case .success(let value):
  53. delegate.stopLoader()
  54. print(value)
  55. let data = value as! NSDictionary
  56. print(data)
  57. DispatchQueue.main.async {
  58. let depositAmount = data["depositAmount"] as! Double
  59. let annualInterest = data["annualInterest"] as! Double
  60. let term = data["term"] as! Int
  61. let compundFreq = data["compundFreq"] as! Double
  62. let depositDate = data["depositDate"] as? String
  63. let maturityAmount = data["maturityAmount"] as! Double
  64. let interestReceived = data["interestReceived"] as! Double
  65. let totalInterestReceived = data["totalInterestReceived"] as! Double
  66. let interestCredited = data["interestCredited"] as! Double
  67. let maturityDate = data["maturityDate"] as? String
  68. let kisanVikasPatraLookupList = data["kisanVikasPatraLookupList"] as! [NSDictionary]
  69. let kvpData = KisanVikasPatraModel(depositDate: depositDate ?? "", maturityDate: maturityDate ?? "", term: term, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, maturityAmount: maturityAmount, totalInterestReceived: totalInterestReceived, interestReceived: interestReceived, interestCredited: interestCredited)
  70. for list in kisanVikasPatraLookupList {
  71. let serialNo = list.value(forKey: "serialNo") as! NSNumber
  72. let referenceDate = list.value(forKey: "referenceDate") as! String
  73. let referenceMonth = list.value(forKey: "referenceMonth") as! String
  74. let financialYear = list.value(forKey: "financialYear") as! String
  75. let yearOfInvestment = list.value(forKey: "yearOfInvestment") as! Double
  76. let interestRate = list.value(forKey: "interestRate") as! Double
  77. let openingBal = list.value(forKey: "openingBal") as! Double
  78. let amountDeposited = list.value(forKey: "amountDeposited") as! Double
  79. let interestAccrued = list.value(forKey: "interestAccrued") as! Double
  80. let interestReceived = list.value(forKey: "interestReceived") as! Double
  81. let totalInterestReceived = list.value(forKey: "totalInterestReceived") as! Double
  82. let closingBalance = list.value(forKey: "closingBalance") as! Double
  83. let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
  84. let list = KVPLookupList(serialNo: Int(truncating: serialNo), openingBal: openingBal, amountDeposited: amountDeposited, interestAccrued: interestAccrued, totalInterestReceived: totalInterestReceived, interestReceived: interestReceived, daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear, closingBalance: closingBalance, yearOfInvestment: yearOfInvestment, interestRate: interestRate)
  85. kvpData.kisanVikasPatraLookupList.append(list)
  86. }
  87. self.kvpModel.append(kvpData)
  88. delegate.navigate(kvpData)
  89. }
  90. case .failure(let error):
  91. delegate.stopLoader()
  92. print(error)
  93. //delegate.showError(error: error)
  94. }
  95. }
  96. }
  97. }
  98. }
  99. protocol KisanVikasPatraViewProtocol {
  100. func startLoader()
  101. func stopLoader()
  102. func showError(error:String)
  103. func setInterestRate(rate:String)
  104. func navigate(_ data:KisanVikasPatraModel)
  105. }