PublicProvidentViewModel.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // PublicProvidentViewModel.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 06/12/21.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class PublicProvidentViewModel {
  10. var delegate: PublicProvidentViewProtocol?
  11. var ppfModel = [PublicProvidentModel]()
  12. //POST PPF Interest API
  13. func getInterestRate(depositDate:String) {
  14. if let delegate = delegate {
  15. delegate.startLoader()
  16. let url = ApiUrl.BASE_URL + ApiUrl.GET_PPF_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 PPF without extension API
  41. func getPPFOutput(depositAmount:String, tenureType:String, tenure:String, rdDepositFreq:String, compundingFreq:String, depositDate:String) {
  42. if let delegate = delegate {
  43. delegate.startLoader()
  44. let url = ApiUrl.BASE_URL + ApiUrl.GET_PPF_WITHOUT_EXTENSION
  45. var interest = String()
  46. switch compundingFreq {
  47. case "Monthly":
  48. interest = "12"
  49. case "Bi-Monthly":
  50. interest = "6"
  51. case "Quarterly":
  52. interest = "4"
  53. case "Triannually":
  54. interest = "3"
  55. case "Half Yearly":
  56. interest = "2"
  57. case "Annually":
  58. interest = "1"
  59. default:
  60. interest = ""
  61. }
  62. var depositFrequency = String()
  63. switch rdDepositFreq {
  64. case "Monthly":
  65. depositFrequency = "12"
  66. case "Bi-Monthly":
  67. depositFrequency = "6"
  68. case "Quarterly":
  69. depositFrequency = "4"
  70. case "Triannually":
  71. depositFrequency = "3"
  72. case "Half Yearly":
  73. depositFrequency = "2"
  74. case "Annually":
  75. depositFrequency = "1"
  76. default:
  77. depositFrequency = ""
  78. }
  79. let parameters: [String: String] = [
  80. "deposit": depositAmount,
  81. "tenureType": tenureType,
  82. "tenure": tenure,
  83. "rdDepositFreq": depositFrequency,
  84. "compundingFreq": interest,
  85. "depositDate": depositDate,
  86. ]
  87. print(parameters)
  88. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  89. switch response.result {
  90. case .success(let value):
  91. delegate.stopLoader()
  92. print(value)
  93. let data = value as! NSDictionary
  94. print(data)
  95. DispatchQueue.main.async {
  96. let depositAmount = data["depositAmount"] as! Double
  97. let amountDepositFreq = data["amountDepositFreq"] as! Int
  98. let annualInterest = data["annualInterest"] as! Double
  99. let term = data["term"] as! Int
  100. let compundFreq = data["compundFreq"] as! Double
  101. let depositDate = data["depositDate"] as? String
  102. let totalAmountDeposited = data["totalAmountDeposited"] as! Double
  103. let maturityAmount = data["maturityAmount"] as! Double
  104. let totalInterestReceived = data["totalInterestReceived"] as! Double
  105. let maturityDate = data["maturityDate"] as! String
  106. let annualAmountDeposited = data["annualAmountDeposited"] as! Double
  107. let maturityDisplayDate = data["maturityDisplayDate"] as? String
  108. let depositDisplayDate = data["depositDisplayDate"] as? String
  109. let currentBalance = data["currentBalance"] as! Double
  110. let ppfFixedAmountLookupList = data["ppfFixedAmountLookupList"] as! [NSDictionary]
  111. let ppfData = PublicProvidentModel(depositDate: depositDate ?? "", maturityDate: maturityDate, maturityDisplayDate: maturityDisplayDate ?? "", depositDisplayDate: depositDisplayDate ?? "", amountDepositFreq: amountDepositFreq, term: term, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, totalAmountDeposited: totalAmountDeposited, maturityAmount: maturityAmount, totalInterestReceived: totalInterestReceived, annualAmountDeposited: annualAmountDeposited, currentBalance: currentBalance)
  112. for list in ppfFixedAmountLookupList {
  113. let serialNo = list.value(forKey: "serialNo") as! NSNumber
  114. let referenceDate = list.value(forKey: "referenceDate") as! String
  115. let referenceMonth = list.value(forKey: "referenceMonth") as! String
  116. let financialYear = list.value(forKey: "financialYear") as! String
  117. let openingBal = list.value(forKey: "openingBal") as! Double
  118. let amountDeposited = list.value(forKey: "amountDeposited") as! Double
  119. let interestEarned = list.value(forKey: "interestEarned") as! Double
  120. let interestCredited = list.value(forKey: "interestCredited") as! Double
  121. let interestRate = list.value(forKey: "interestRate") as! Double
  122. let totalInterestAccrued = list.value(forKey: "totalInterestAccrued") as! Double
  123. let closingBalance = list.value(forKey: "closingBalance") as! Double
  124. let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
  125. let currentBalance = list.value(forKey: "currentBalance") as! Double
  126. let list = PPFLookupList(serialNo: Int(truncating: serialNo), daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear, openingBal: openingBal, amountDeposited: amountDeposited, interestEarned: interestEarned, interestCredited: interestCredited, totalInterestAccrued: totalInterestAccrued, closingBalance: closingBalance, interestRate: interestRate, currentBalance:currentBalance)
  127. ppfData.ppfFixedAmountLookupList.append(list)
  128. }
  129. self.ppfModel.append(ppfData)
  130. delegate.navigate(ppfData)
  131. }
  132. case .failure(let error):
  133. delegate.stopLoader()
  134. print(error)
  135. //delegate.showError(error: error)
  136. }
  137. }
  138. }
  139. }
  140. //POST PPF with extension API
  141. func getPPFExtOutput(depositAmount:String, tenureType:String, tenure:String, rdDepositFreq:String, compundingFreq:String, depositDate:String, totalAmountDiposited:String) {
  142. if let delegate = delegate {
  143. delegate.startLoader()
  144. let url = ApiUrl.BASE_URL + ApiUrl.GET_PPF_WITH_EXTENSION
  145. var interest = String()
  146. switch compundingFreq {
  147. case "Monthly":
  148. interest = "12"
  149. case "Bi-Monthly":
  150. interest = "6"
  151. case "Quarterly":
  152. interest = "4"
  153. case "Triannually":
  154. interest = "3"
  155. case "Half Yearly":
  156. interest = "2"
  157. case "Annually":
  158. interest = "1"
  159. default:
  160. interest = ""
  161. }
  162. var depositFrequency = String()
  163. switch rdDepositFreq {
  164. case "Monthly":
  165. depositFrequency = "12"
  166. case "Bi-Monthly":
  167. depositFrequency = "6"
  168. case "Quarterly":
  169. depositFrequency = "4"
  170. case "Triannually":
  171. depositFrequency = "3"
  172. case "Half Yearly":
  173. depositFrequency = "2"
  174. case "Annually":
  175. depositFrequency = "1"
  176. default:
  177. depositFrequency = ""
  178. }
  179. let parameters: [String: String] = [
  180. "deposit": depositAmount,
  181. "tenureType": tenureType,
  182. "tenure": tenure,
  183. "rdDepositFreq": depositFrequency,
  184. "compundingFreq": interest,
  185. "depositDate": depositDate,
  186. //"maturityAmount": maturityAmount,
  187. "totalAmountDiposited": totalAmountDiposited
  188. ]
  189. print(parameters)
  190. AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
  191. switch response.result {
  192. case .success(let value):
  193. delegate.stopLoader()
  194. print(value)
  195. let data = value as! NSDictionary
  196. print(data)
  197. DispatchQueue.main.async {
  198. let depositAmount = data["depositAmount"] as! Double
  199. let amountDepositFreq = data["amountDepositFreq"] as! Int
  200. let annualInterest = data["annualInterest"] as! Double
  201. let term = data["term"] as! Int
  202. let compundFreq = data["compundFreq"] as! Double
  203. let depositDate = data["depositDate"] as? String
  204. let totalAmountDeposited = data["totalAmountDeposited"] as! Double
  205. let maturityAmount = data["maturityAmount"] as! Double
  206. let totalInterestReceived = data["totalInterestReceived"] as! Double
  207. let maturityDate = data["depositMaturityDate"] as! String
  208. let annualAmountDeposited = data["annualAmountDeposited"] as! Double
  209. let maturityDisplayDate = data["maturityDisplayDate"] as! String
  210. let depositDisplayDate = data["depositDisplayDate"] as! String
  211. let currentBalance = data["currentBalance"] as! Double
  212. let ppfFixedAmountLookupList = data["ppfFixedAmountLookupList"] as! [NSDictionary]
  213. let ppfData = PublicProvidentModel(depositDate: depositDate ?? "", maturityDate: maturityDate, maturityDisplayDate: maturityDisplayDate, depositDisplayDate: depositDisplayDate, amountDepositFreq: amountDepositFreq, term: term, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, totalAmountDeposited: totalAmountDeposited, maturityAmount: maturityAmount, totalInterestReceived: totalInterestReceived, annualAmountDeposited: annualAmountDeposited, currentBalance: currentBalance)
  214. for list in ppfFixedAmountLookupList {
  215. let serialNo = list.value(forKey: "serialNo") as! NSNumber
  216. let referenceDate = list.value(forKey: "referenceDate") as! String
  217. let referenceMonth = list.value(forKey: "referenceMonth") as! String
  218. let financialYear = list.value(forKey: "financialYear") as! String
  219. let openingBal = list.value(forKey: "openingBal") as! Double
  220. let amountDeposited = list.value(forKey: "amountDeposited") as! Double
  221. let interestEarned = list.value(forKey: "interestEarned") as! Double
  222. let interestCredited = list.value(forKey: "interestCredited") as! Double
  223. let interestRate = list.value(forKey: "interestRate") as! Double
  224. let totalInterestAccrued = list.value(forKey: "totalInterestAccrued") as! Double
  225. let closingBalance = list.value(forKey: "closingBalance") as! Double
  226. let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
  227. let currentBalance = list.value(forKey: "currentBalance") as! Double
  228. let list = PPFLookupList(serialNo: Int(truncating: serialNo), daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear, openingBal: openingBal, amountDeposited: amountDeposited, interestEarned: interestEarned, interestCredited: interestCredited, totalInterestAccrued: totalInterestAccrued, closingBalance: closingBalance, interestRate: interestRate, currentBalance:currentBalance)
  229. ppfData.ppfFixedAmountLookupList.append(list)
  230. }
  231. self.ppfModel.append(ppfData)
  232. delegate.navigate(ppfData)
  233. }
  234. case .failure(let error):
  235. delegate.stopLoader()
  236. print(error)
  237. //delegate.showError(error: error)
  238. }
  239. }
  240. }
  241. }
  242. }
  243. protocol PublicProvidentViewProtocol {
  244. func startLoader()
  245. func stopLoader()
  246. func showError(error:String)
  247. func setInterestRate(rate:String)
  248. func navigate(_ data:PublicProvidentModel)
  249. }