123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // POTimeDepositViewModel.swift
- // Product Calculator
- //
- // Created by Suraj Kumar Mandal on 04/12/21.
- //
- import Foundation
- import Alamofire
- class POTimeDepositViewModel {
-
- var delegate: POTimeDepositViewProtocol?
-
- var poTimeModel = [POTimeDepositModel]()
-
- //PO time deposit API call
- func getPoTimeOutput(deposit:String, yearsDays:String, compundingFreq:String, rdDepositFreq:String, depositDate:String, interestRate:String) {
- if let delegate = delegate {
- delegate.startLoader()
- let url = ApiUrl.BASE_URL + ApiUrl.GET_PO_TIME_DEPOSIT
-
- var compound = String()
- switch compundingFreq {
- case "Monthly":
- compound = "12"
- case "Bi-Monthly":
- compound = "6"
- case "Quarterly":
- compound = "4"
- case "Triannually":
- compound = "3"
- case "Half Yearly":
- compound = "2"
- case "Annually":
- compound = "1"
- default:
- compound = ""
- }
-
- var interestPayout = String()
- switch rdDepositFreq {
- case "Monthly":
- interestPayout = "12"
- case "Bi-Monthly":
- interestPayout = "6"
- case "Quarterly":
- interestPayout = "4"
- case "Triannually":
- interestPayout = "3"
- case "Half Yearly":
- interestPayout = "2"
- case "Annually":
- interestPayout = "1"
- default:
- interestPayout = ""
- }
-
- let parameters: [String: String] = [
- "deposit": deposit,
- "yearsDays": yearsDays,
- "compundingFreq": compound,
- "rdDepositFreq": interestPayout,
- "depositDate": depositDate,
- "interestRate": interestRate
- ]
- print(parameters)
-
- AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
-
- switch response.result {
- case .success(let value):
- delegate.stopLoader()
- print(value)
- let data = value as! NSDictionary
- print(data)
- DispatchQueue.main.async {
- let depositAmount = data["depositAmount"] as! Double
- let amountDepositFreq = data["amountDepositFreq"] as! Int
- let annualInterest = data["annualInterest"] as! Double
- let term = data["term"] as! Int
- let compundFreq = data["compundFreq"] as! Double
- let depositDate = data["depositDate"] as? String
- let totalAmountDeposited = data["totalAmountDeposited"] as! Double
- let maturityAmount = data["maturityAmount"] as! Double
- let interestReceived = data["interestReceived"] as! Double
- let totalInterestReceived = data["totalInterestReceived"] as! Double
- let maturityDate = data["maturityDate"] as! String
- let interestRate = data["interestRate"] as! Double
-
- let poTimeDepositLookupList = data["poTimeDepositLookupList"] as! [NSDictionary]
-
- let poTimeData = POTimeDepositModel(depositDate: depositDate ?? "", maturityDate: maturityDate, term: term, amountDepositFreq:amountDepositFreq, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, maturityAmount: maturityAmount, interestReceived: interestReceived, totalInterestReceived: totalInterestReceived, totalAmountDeposited:totalAmountDeposited, interestRate:interestRate)
-
- for list in poTimeDepositLookupList {
- let serialNo = list.value(forKey: "serialNo") as! NSNumber
- let referenceDate = list.value(forKey: "referenceDate") as! String
- let referenceMonth = list.value(forKey: "referenceMonth") as! String
- let financialYear = list.value(forKey: "financialYear") as! String
- let openingBal = list.value(forKey: "openingBal") as! Double
- let amountDeposited = list.value(forKey: "amountDeposited") as! Double
- let interestPaid = list.value(forKey: "interestPaid") as! Double
- let interestAccrued = list.value(forKey: "interestAccrued") as! Double
- let totalInterestReceived = list.value(forKey: "totalInterestReceived") as! Double
- let closingBalance = list.value(forKey: "closingBalance") as! Double
- let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
-
- let list = POTimeDepositLookupList(serialNo: Int(truncating: serialNo), openingBal: openingBal, amountDeposited: amountDeposited, interestAccrued: interestAccrued, totalInterestReceived: totalInterestReceived, interestPaid: interestPaid, daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear, closingBalance: closingBalance)
-
- poTimeData.poTimeDepositLookupList.append(list)
- }
- self.poTimeModel.append(poTimeData)
- delegate.navigate(poTimeData)
- }
-
- case .failure(let error):
- delegate.stopLoader()
- print(error)
- //delegate.showError(error: error)
- }
- }
- }
- }
-
- }
- protocol POTimeDepositViewProtocol {
- func startLoader()
- func stopLoader()
- func showError(error:String)
- func navigate(_ data:POTimeDepositModel)
- }
|