EMILoanSummaryViewModel.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // EMILoanSummaryViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 07/12/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class EMILoanSummaryViewModel {
  10. var delegate: EMILoanSummaryViewProtocol?
  11. var emiOriginalOutputList = [EMIOriginalLookupList]()
  12. var emiOutstandingOutputList = [EMIOutstandingLookupList]()
  13. //POST EMI Original Output List
  14. func getEMIOriginalOutputList(numberOfEMI:String, loanStartingDate:String, originalLoanAmount:String, loanEMIAmt:String, originalInterestRate:String, loanFlag:String) {
  15. if let delegate = delegate {
  16. delegate.startLoader()
  17. let url = ApiUrl.BASE_URL + ApiUrl.GET_BOND_DEBENTURE_OUTPUT_LIST
  18. let parameters: [String: String] = [
  19. "numberOfEMI": numberOfEMI,
  20. "loanStartingDate": loanStartingDate,
  21. "originalLoanAmount": originalLoanAmount,
  22. "loanEMIAmt": loanEMIAmt,
  23. "originalInterestRate": originalInterestRate,
  24. "loanFlag": loanFlag
  25. ]
  26. print(parameters)
  27. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  28. switch response.result {
  29. case .success(let value):
  30. delegate.stopLoader()
  31. print(value)
  32. let data = value as! [NSDictionary]
  33. print(data)
  34. DispatchQueue.main.async {
  35. for item in data {
  36. let serialNumber = item["serialNumber"] as! Int
  37. let refDate = item["refDate"] as! String
  38. let financialYear = item["financialYear"] as! String
  39. let referenceMonth = item["referenceMonth"] as! String
  40. let installmentNumber = item["installmentNumber"] as! Int
  41. let begningBal = item["begningBal"] as! Double
  42. let interestPayment = item["interestPayment"] as! Double
  43. let principalPayment = item["principalPayment"] as! Double
  44. let endingBalance = item["endingBalance"] as! Double
  45. let emiAmount = item["emiAmount"] as! Double
  46. let totalPrincipalPaid = item["totalPrincipalPaid"] as! Double
  47. let totalInterestPaid = item["totalInterestPaid"] as! Double
  48. let loanEndDays = item["loanEndDays"] as! Int
  49. let displayDate = item["displayDate"] as! String
  50. let emiOriginalOutputList = EMIOriginalLookupList(serialNumber: serialNumber, installmentNumber: installmentNumber, loanEndDays: loanEndDays, refDate: refDate, financialYear: financialYear, referenceMonth: referenceMonth, displayDate: displayDate, begningBal: begningBal, interestPayment: interestPayment, principalPayment: principalPayment, endingBalance: endingBalance, emiAmount: emiAmount, totalPrincipalPaid: totalPrincipalPaid, totalInterestPaid: totalInterestPaid)
  51. self.emiOriginalOutputList.append(emiOriginalOutputList)
  52. }
  53. }
  54. delegate.loadOriginal(self.emiOriginalOutputList)
  55. case .failure(let error):
  56. delegate.stopLoader()
  57. print(error)
  58. //delegate.showError(error: error)
  59. }
  60. }
  61. }
  62. }
  63. //POST EMI Outstanding Output List
  64. func getEMIOutstandingOutputList(numberOfEMI:String, loanStartingDate:String, originalLoanAmount:String, loanEMIAmt:String, originalInterestRate:String, loanFlag:String) {
  65. if let delegate = delegate {
  66. delegate.startLoader()
  67. let url = ApiUrl.BASE_URL + ApiUrl.GET_BOND_DEBENTURE_OUTPUT_LIST
  68. let parameters: [String: String] = [
  69. "numberOfEMI": numberOfEMI,
  70. "loanStartingDate": loanStartingDate,
  71. "originalLoanAmount": originalLoanAmount,
  72. "loanEMIAmt": loanEMIAmt,
  73. "originalInterestRate": originalInterestRate,
  74. "loanFlag": loanFlag
  75. ]
  76. print(parameters)
  77. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  78. switch response.result {
  79. case .success(let value):
  80. delegate.stopLoader()
  81. print(value)
  82. let data = value as! [NSDictionary]
  83. print(data)
  84. DispatchQueue.main.async {
  85. for item in data {
  86. let serialNumber = item["serialNumber"] as! Int
  87. let refDate = item["refDate"] as! String
  88. let financialYear = item["financialYear"] as! String
  89. let referenceMonth = item["referenceMonth"] as! String
  90. let installmentNumber = item["installmentNumber"] as! Int
  91. let begningBal = item["begningBal"] as! Double
  92. let interestPayment = item["interestPayment"] as! Double
  93. let principalPayment = item["principalPayment"] as! Double
  94. let endingBalance = item["endingBalance"] as! Double
  95. let emiAmount = item["emiAmount"] as! Double
  96. let totalPrincipalPaid = item["totalPrincipalPaid"] as! Double
  97. let totalInterestPaid = item["totalInterestPaid"] as! Double
  98. let loanEndDays = item["loanEndDays"] as! Int
  99. let displayDate = item["displayDate"] as! String
  100. let emiOutstandingOutputList = EMIOutstandingLookupList(serialNumber: serialNumber, installmentNumber: installmentNumber, loanEndDays: loanEndDays, refDate: refDate, financialYear: financialYear, referenceMonth: referenceMonth, displayDate: displayDate, begningBal: begningBal, interestPayment: interestPayment, principalPayment: principalPayment, endingBalance: endingBalance, emiAmount: emiAmount, totalPrincipalPaid: totalPrincipalPaid, totalInterestPaid: totalInterestPaid)
  101. self.emiOutstandingOutputList.append(emiOutstandingOutputList)
  102. }
  103. }
  104. delegate.loadOutstanding(self.emiOutstandingOutputList)
  105. case .failure(let error):
  106. delegate.stopLoader()
  107. print(error)
  108. //delegate.showError(error: error)
  109. }
  110. }
  111. }
  112. }
  113. }
  114. protocol EMILoanSummaryViewProtocol {
  115. func startLoader()
  116. func stopLoader()
  117. func showError(error:String)
  118. func loadOriginal(_ data:[EMIOriginalLookupList])
  119. func loadOutstanding(_ data:[EMIOutstandingLookupList])
  120. }