EMILoanViewModel.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // EMILoanViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 01/12/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class EMILoanViewModel {
  10. var delegate: EMILoanViewProtocol?
  11. var emiOriginalModel = [EMILoanOriginalModel]()
  12. var emiOriginalOutputList = [EMIOriginalLookupList]()
  13. var emiOutstandingModel = [EMILoanOutstandingModel]()
  14. var emiOutstandingOutputList = [EMIOutstandingLookupList]()
  15. //POST EMI Original API
  16. func getEMIOriginalLoan(loanFlag:String, originalInterestRate:String, originalLoanAmount:String, loanStartDate:String, loanTenure:String) {
  17. if let delegate = delegate {
  18. delegate.startLoader()
  19. let url = ApiUrl.BASE_URL + ApiUrl.GET_EMI_LOAN
  20. let parameters: [String: String] = [
  21. "loanFlag": loanFlag,
  22. "originalInterestRate": originalInterestRate,
  23. "originalLoanAmount": originalLoanAmount,
  24. "loanStartDate": loanStartDate,
  25. "loanTenure": loanTenure
  26. ]
  27. print(parameters)
  28. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  29. switch response.result {
  30. case .success(let value):
  31. delegate.stopLoader()
  32. print(value)
  33. let data = value as! NSDictionary
  34. print(data)
  35. DispatchQueue.main.async {
  36. let loanCategory = data["loanCategory"] as? Int
  37. let loanCategoryLabel = data["loanCategoryLabel"] as? String
  38. let loanDiscription = data["loanDiscription"] as? String
  39. let loanProvider = data["loanProvider"] as? String
  40. let loanType = data["loanType"] as! Int
  41. let loanTypeLabel = data["loanTypeLabel"] as? String
  42. let loanAmount = data["loanAmount"] as! Double
  43. let interestRate = data["interestRate"] as! Double
  44. let loanTenure = data["loanTenure"] as! Int
  45. let loanStartDate = data["loanStartDate"] as? String
  46. let numberOfEMI = data["numberOfEMI"] as! Int
  47. let emiAmount = data["emiAmount"] as! Double
  48. let loanEndDate = data["loanEndDate"] as! String
  49. let interestPaymentFrequency = data["interestPaymentFrequency"] as! Int
  50. let totalInterestPaid = data["totalInterestPaid"] as! Double
  51. let loan_original_flag = data["loan_original_flag"] as? String
  52. let displayDate = data["displayDate"] as? String
  53. let emiOriginalData = EMILoanOriginalModel(loanCategoryLabel: loanCategoryLabel ?? "", loanDiscription: loanDiscription ?? "", loanProvider: loanProvider ?? "", loanTypeLabel: loanTypeLabel ?? "", loanStartDate: loanStartDate ?? "", loanEndDate: loanEndDate, loan_original_flag: loan_original_flag ?? "", displayDate: displayDate ?? "", loanCategory: loanCategory ?? 0, loanType: loanType, loanTenure: loanTenure, numberOfEMI: numberOfEMI, interestPaymentFrequency: interestPaymentFrequency, loanAmount: loanAmount, interestRate: interestRate, emiAmount: emiAmount, totalInterestPaid: totalInterestPaid)
  54. self.emiOriginalModel.append(emiOriginalData)
  55. delegate.navigateOriginal(emiOriginalData)
  56. }
  57. case .failure(let error):
  58. delegate.stopLoader()
  59. print(error)
  60. //delegate.showError(error: error)
  61. }
  62. }
  63. }
  64. }
  65. //POST EMI Original Output List
  66. func getEMIOriginalOutputList(numberOfEMI:String, loanStartingDate:String, originalLoanAmount:String, loanEMIAmt:String, originalInterestRate:String, loanFlag:String) {
  67. if let delegate = delegate {
  68. delegate.startLoader()
  69. let url = ApiUrl.BASE_URL + ApiUrl.GET_EMI_LOAN_OUTPUT_LIST
  70. let parameters: [String: String] = [
  71. "numberOfEMI": numberOfEMI,
  72. "loanStartingDate": loanStartingDate,
  73. "originalLoanAmount": originalLoanAmount,
  74. "loanEMIAmt": loanEMIAmt,
  75. "originalInterestRate": originalInterestRate,
  76. "loanFlag": loanFlag
  77. ]
  78. print(parameters)
  79. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  80. switch response.result {
  81. case .success(let value):
  82. delegate.stopLoader()
  83. print(value)
  84. let data = value as! [NSDictionary]
  85. print(data)
  86. DispatchQueue.main.async {
  87. for item in data {
  88. let serialNumber = item["serialNumber"] as! Int
  89. let refDate = item["refDate"] as! String
  90. let financialYear = item["financialYear"] as! String
  91. let referenceMonth = item["referenceMonth"] as! String
  92. let installmentNumber = item["installmentNumber"] as! Int
  93. let begningBal = item["begningBal"] as! Double
  94. let interestPayment = item["interestPayment"] as! Double
  95. let principalPayment = item["principalPayment"] as! Double
  96. let endingBalance = item["endingBalance"] as! Double
  97. let emiAmount = item["emiAmount"] as! Double
  98. let totalPrincipalPaid = item["totalPrincipalPaid"] as! Double
  99. let totalInterestPaid = item["totalInterestPaid"] as! Double
  100. let loanEndDays = item["loanEndDays"] as! Int
  101. let displayDate = item["displayDate"] as! String
  102. 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)
  103. self.emiOriginalOutputList.append(emiOriginalOutputList)
  104. }
  105. }
  106. //delegate.navigate(bondModel: bondModel, bondOuputListModel: self.bondDebentureOutputListModel)
  107. case .failure(let error):
  108. delegate.stopLoader()
  109. print(error)
  110. //delegate.showError(error: error)
  111. }
  112. }
  113. }
  114. }
  115. //POST EMI Outstanding API
  116. func getEMIOutstandingLoan(loanFlag:String, originalInterestRate:String, originalLoanAmount:String, loanDayout:String, loanEMIAmt:String) {
  117. if let delegate = delegate {
  118. delegate.startLoader()
  119. let url = ApiUrl.BASE_URL + ApiUrl.GET_EMI_LOAN
  120. let parameters: [String: String] = [
  121. "loanFlag": loanFlag,
  122. "originalInterestRate": originalInterestRate,
  123. "originalLoanAmount": originalLoanAmount,
  124. "loanDayout": loanDayout,
  125. "loanEMIAmt": loanEMIAmt
  126. ]
  127. print(parameters)
  128. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  129. switch response.result {
  130. case .success(let value):
  131. delegate.stopLoader()
  132. print(value)
  133. let data = value as! NSDictionary
  134. print(data)
  135. DispatchQueue.main.async {
  136. let loanCategory = data["loanCategory"] as? Int
  137. let loanCategoryLabel = data["loanCategoryLabel"] as? String
  138. let loanDiscription = data["loanDiscription"] as? String
  139. let loanProvider = data["loanProvider"] as? String
  140. let loanType = data["loanType"] as? Int
  141. let loanTypeLabel = data["loanTypeLabel"] as? String
  142. let loanAmount = data["loanAmount"] as! Double
  143. let interestRate = data["interestRate"] as! Double
  144. let loanTenure = data["loanTenure"] as! Int
  145. let loanStartDate = data["loanStartDate"] as? String
  146. let numberOfEMI = data["numberOfEMI"] as! Int
  147. let emiAmount = data["emiAmount"] as! Double
  148. let loanEndDate = data["loanEndDate"] as! String
  149. let interestPaymentFrequency = data["interestPaymentFrequency"] as! Int
  150. let totalInterestPaid = data["totalInterestPaid"] as! Double
  151. let loan_original_flag = data["loan_original_flag"] as? String
  152. let displayDate = data["displayDate"] as? String
  153. let emiOutstandingData = EMILoanOutstandingModel(loanCategoryLabel: loanCategoryLabel ?? "", loanDiscription: loanDiscription ?? "", loanProvider: loanProvider ?? "", loanTypeLabel: loanTypeLabel ?? "", loanStartDate: loanStartDate ?? "", loanEndDate: loanEndDate, loan_original_flag: loan_original_flag ?? "", displayDate: displayDate ?? "", loanCategory: loanCategory ?? 0, loanType: loanType ?? 0, loanTenure: loanTenure, numberOfEMI: numberOfEMI, interestPaymentFrequency: interestPaymentFrequency, loanAmount: loanAmount, interestRate: interestRate, emiAmount: emiAmount, totalInterestPaid: totalInterestPaid)
  154. self.emiOutstandingModel.append(emiOutstandingData)
  155. //delegate.navigate(cpcdData)
  156. }
  157. case .failure(let error):
  158. delegate.stopLoader()
  159. print(error)
  160. //delegate.showError(error: error)
  161. }
  162. }
  163. }
  164. }
  165. //POST EMI Outstanding Output List
  166. func getEMIOutstandingOutputList(numberOfEMI:String, loanStartingDate:String, originalLoanAmount:String, loanEMIAmt:String, originalInterestRate:String, loanFlag:String) {
  167. if let delegate = delegate {
  168. delegate.startLoader()
  169. let url = ApiUrl.BASE_URL + ApiUrl.GET_EMI_LOAN_OUTPUT_LIST
  170. let parameters: [String: String] = [
  171. "numberOfEMI": numberOfEMI,
  172. "loanStartingDate": loanStartingDate,
  173. "originalLoanAmount": originalLoanAmount,
  174. "loanEMIAmt": loanEMIAmt,
  175. "originalInterestRate": originalInterestRate,
  176. "loanFlag": loanFlag
  177. ]
  178. print(parameters)
  179. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  180. switch response.result {
  181. case .success(let value):
  182. delegate.stopLoader()
  183. print(value)
  184. let data = value as! [NSDictionary]
  185. print(data)
  186. DispatchQueue.main.async {
  187. for item in data {
  188. let serialNumber = item["serialNumber"] as! Int
  189. let refDate = item["refDate"] as! String
  190. let financialYear = item["financialYear"] as! String
  191. let referenceMonth = item["referenceMonth"] as! String
  192. let installmentNumber = item["installmentNumber"] as! Int
  193. let begningBal = item["begningBal"] as! Double
  194. let interestPayment = item["interestPayment"] as! Double
  195. let principalPayment = item["principalPayment"] as! Double
  196. let endingBalance = item["endingBalance"] as! Double
  197. let emiAmount = item["emiAmount"] as! Double
  198. let totalPrincipalPaid = item["totalPrincipalPaid"] as! Double
  199. let totalInterestPaid = item["totalInterestPaid"] as! Double
  200. let loanEndDays = item["loanEndDays"] as! Int
  201. let displayDate = item["displayDate"] as! String
  202. 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)
  203. self.emiOutstandingOutputList.append(emiOutstandingOutputList)
  204. }
  205. }
  206. //delegate.navigate(bondModel: bondModel, bondOuputListModel: self.bondDebentureOutputListModel)
  207. case .failure(let error):
  208. delegate.stopLoader()
  209. print(error)
  210. //delegate.showError(error: error)
  211. }
  212. }
  213. }
  214. }
  215. }
  216. protocol EMILoanViewProtocol {
  217. func startLoader()
  218. func stopLoader()
  219. func showError(error:String)
  220. func navigateOriginal(_ data:EMILoanOriginalModel)
  221. func navigateOutstanding(_ data:EMILoanOutstandingModel)
  222. }