FixedDepositViewModel.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // FixedDepositViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 16/11/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class FixedDepositViewModel {
  10. var delegate: FixedDepositViewProtocol?
  11. var fdtdrDetails = [FDTDRDetailsModel]()
  12. var fdStdrDetails = [FDSTDRDetailsModel]()
  13. //Fixed deposit TDR API call
  14. func getBankFDTDRDetails(depositAmount:String, annualInterest:String, termYearsDays:String, tenure:String, payoutFrq:String, depositDate:String) {
  15. if let delegate = delegate {
  16. delegate.startLoader()
  17. let url = ApiUrl.BASE_URL + ApiUrl.GET_FDTDR
  18. var terms = String()
  19. switch termYearsDays {
  20. case "Year":
  21. terms = "Y"
  22. case "Days":
  23. terms = "D"
  24. default:
  25. terms = ""
  26. }
  27. var interestPayoutFrequency = String()
  28. switch payoutFrq {
  29. case "Monthly":
  30. interestPayoutFrequency = "12"
  31. case "Bi-Monthly":
  32. interestPayoutFrequency = "6"
  33. case "Quarterly":
  34. interestPayoutFrequency = "4"
  35. case "Triannually":
  36. interestPayoutFrequency = "3"
  37. case "Half Yearly":
  38. interestPayoutFrequency = "2"
  39. case "Annually":
  40. interestPayoutFrequency = "1"
  41. default:
  42. interestPayoutFrequency = ""
  43. }
  44. let parameters: [String: String] = [
  45. "depositAmount": depositAmount,
  46. "annualInterest": annualInterest,
  47. "termYearsDays": terms,
  48. "tenure": tenure,
  49. "payoutFrq": interestPayoutFrequency,
  50. "depositDate": depositDate
  51. ]
  52. print(parameters)
  53. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  54. switch response.result {
  55. case .success(let value):
  56. delegate.stopLoader()
  57. print(value)
  58. let data = value as! NSDictionary
  59. print(data)
  60. DispatchQueue.main.async {
  61. let interestReceived = data["interestReceived"] as? Double
  62. let totalInterestReceived = data["totalInterestReceived"] as? Double
  63. let maturityDate = data["maturityDate"] as? String
  64. let maturityDisplayDate = data["maturityDisplayDate"] as? String
  65. let bankFdTdrLookupList = data["bankFdTdrLookupList"] as! [NSDictionary]
  66. let fdtdrData = FDTDRDetailsModel(maturityDate: maturityDate ?? "", maturityDisplayDate: maturityDisplayDate ?? "", interestReceived: Int(interestReceived ?? 0), totalInterestReceived: Int(totalInterestReceived ?? 0))
  67. for list in bankFdTdrLookupList {
  68. let serialNo = list.value(forKey: "serialNo") as! NSNumber
  69. let referenceDate = list.value(forKey: "referenceDate") as! String
  70. let referenceMonth = list.value(forKey: "referenceMonth") as! String
  71. let financialYear = list.value(forKey: "financialYear") as! String
  72. let amountDeposited = list.value(forKey: "amountDeposited") as! Double
  73. let interestReceived = list.value(forKey: "interestReceived") as! Double
  74. let interestAccrued = list.value(forKey: "interestAccrued") as! Double
  75. let totalInterestAccrued = list.value(forKey: "totalInterestAccrued") as! Double
  76. let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
  77. let list = FDTDRLookupList(serialNo: Int(truncating: serialNo), amountDeposited: Int(amountDeposited), interestReceived: Int(interestReceived), interestAccrued: Int(interestAccrued), totalInterestAccrued: Int(totalInterestAccrued), daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear)
  78. fdtdrData.bankFdTdrLookupList.append(list)
  79. }
  80. self.fdtdrDetails.append(fdtdrData)
  81. delegate.navigateWithFDTDR(fdtdrData)
  82. }
  83. case .failure(let error):
  84. delegate.stopLoader()
  85. print(error)
  86. //delegate.showError(error: error)
  87. }
  88. }
  89. }
  90. }
  91. //Fixed deposit STDR API call
  92. func getBankFDSTDRCDCPOutputList(depositAmount:String, annualInterest:String, termYearsDays:String, tenure:String, compoundFrequency:String, depositDate:String) {
  93. if let delegate = delegate {
  94. delegate.startLoader()
  95. let url = ApiUrl.BASE_URL + ApiUrl.GET_FDSTDR
  96. var terms = String()
  97. switch termYearsDays {
  98. case "Year":
  99. terms = "Y"
  100. case "Days":
  101. terms = "D"
  102. default:
  103. terms = ""
  104. }
  105. var interestPayoutFrequency = String()
  106. switch compoundFrequency {
  107. case "Monthly":
  108. interestPayoutFrequency = "12"
  109. case "Bi-Monthly":
  110. interestPayoutFrequency = "6"
  111. case "Quarterly":
  112. interestPayoutFrequency = "4"
  113. case "Triannually":
  114. interestPayoutFrequency = "3"
  115. case "Half Yearly":
  116. interestPayoutFrequency = "2"
  117. case "Annually":
  118. interestPayoutFrequency = "1"
  119. default:
  120. interestPayoutFrequency = ""
  121. }
  122. let parameters: [String: String] = [
  123. "depositAmount": depositAmount,
  124. "annualInterest": annualInterest,
  125. "termYearsDays": terms,
  126. "tenure": tenure,
  127. "compoundFrequency": interestPayoutFrequency,
  128. "depositDate": depositDate
  129. ]
  130. print(parameters)
  131. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  132. switch response.result {
  133. case .success(let value):
  134. delegate.stopLoader()
  135. print(value)
  136. let data = value as! NSDictionary
  137. print(data)
  138. DispatchQueue.main.async {
  139. let depositAmount = data["depositAmount"] as! Double
  140. let annualInterest = data["annualInterest"] as! Double
  141. let term = data["term"] as! Int
  142. let compundFreq = data["compundFreq"] as! Double
  143. let depositDate = data["depositDate"] as! String
  144. let maturityAmount = data["maturityAmount"] as! Double
  145. let interestReceived = data["interestReceived"] as! Double
  146. let totalInterestReceived = data["totalInterestReceived"] as! Double
  147. let maturityDate = data["maturityDate"] as! String
  148. let bankFdStdrLookupList = data["bankFdTdrLookupList"] as! [NSDictionary]
  149. let fdStdrData = FDSTDRDetailsModel(depositDate: depositDate, maturityDate: maturityDate, term: term, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, maturityAmount: maturityAmount, interestReceived: interestReceived, totalInterestReceived: totalInterestReceived)
  150. for list in bankFdStdrLookupList {
  151. let serialNo = list.value(forKey: "serialNo") as! NSNumber
  152. let referenceDate = list.value(forKey: "referenceDate") as! String
  153. let referenceMonth = list.value(forKey: "referenceMonth") as! String
  154. let financialYear = list.value(forKey: "financialYear") as! String
  155. let openingBal = list.value(forKey: "openingBal") as! Double
  156. let amountDeposited = list.value(forKey: "amountDeposited") as! Double
  157. let interestCredited = list.value(forKey: "interestCredited") as! Double
  158. let interestAccrued = list.value(forKey: "interestAccrued") as! Double
  159. let totalInterestReceived = list.value(forKey: "totalInterestReceived") as! Double
  160. let closingBalance = list.value(forKey: "closingBalance") as! Double
  161. let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
  162. let list = FDSTDRLookupList(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)
  163. fdStdrData.bankFdStdrLookupList.append(list)
  164. }
  165. self.fdStdrDetails.append(fdStdrData)
  166. delegate.navigateWithFDSTDR(fdStdrData)
  167. }
  168. case .failure(let error):
  169. delegate.stopLoader()
  170. print(error)
  171. //delegate.showError(error: error)
  172. }
  173. }
  174. }
  175. }
  176. }
  177. protocol FixedDepositViewProtocol {
  178. func startLoader()
  179. func stopLoader()
  180. func showError(error:String)
  181. func navigateWithFDTDR(_ data:FDTDRDetailsModel)
  182. func navigateWithFDSTDR(_ data:FDSTDRDetailsModel)
  183. }