// // PORecurringDepositViewModel.swift // Product Calculator // // Created by Suraj Kumar Mandal on 03/12/21. // import Foundation import Alamofire class PORecurringDepositViewModel { var delegate: PORecurringDepositViewProtocol? var poRecurringModel = [PORecurringDepositModel]() //POST PO recurring deposit Interest API func getInterestRate(depositDate:String) { if let delegate = delegate { delegate.startLoader() let url = ApiUrl.BASE_URL + ApiUrl.GET_PO_RECURRING_DEPOSIT_INTEREST let parameters: [String: String] = [ "depositDate": depositDate ] 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 interestRate = data["interestRate"] as! Double delegate.setInterestRate(rate: String(interestRate)) } case .failure(let error): delegate.stopLoader() print(error) //delegate.showError(error: error) } } } } //POST PO recurring deposit API func getPoRecurringOutput(depositAmount:String, yearsDays:String, rdDepositFreq:String, compundingFreq:String, depositDate:String) { if let delegate = delegate { delegate.startLoader() let url = ApiUrl.BASE_URL + ApiUrl.GET_PO_RECURRING_DEPOSIT var interest = String() switch compundingFreq { case "Monthly": interest = "12" case "Bi-Monthly": interest = "6" case "Quarterly": interest = "4" case "Triannually": interest = "3" case "Half Yearly": interest = "2" case "Annually": interest = "1" default: interest = "" } var depositFrequency = String() switch rdDepositFreq { case "Monthly": depositFrequency = "12" case "Bi-Monthly": depositFrequency = "6" case "Quarterly": depositFrequency = "4" case "Triannually": depositFrequency = "3" case "Half Yearly": depositFrequency = "2" case "Annually": depositFrequency = "1" default: depositFrequency = "" } let parameters: [String: String] = [ "deposit": depositAmount, "yearsDays": yearsDays, "rdDepositFreq": depositFrequency, "compundingFreq": interest, "depositDate": depositDate ] 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 totalInterestReceived = data["totalInterestReceived"] as! Double let maturityDate = data["maturityDate"] as! String let maturityDisplayDate = data["maturityDisplayDate"] as? String let interestRate = data["interestRate"] as! Double let poRecurringLookupList = data["poRecurringLookupList"] as! [NSDictionary] let poRecurringData = PORecurringDepositModel(depositDate: depositDate ?? "", maturityDate: maturityDate, amountDepositFreq: amountDepositFreq, term: term, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, totalAmountDeposited: totalAmountDeposited, maturityAmount: maturityAmount, totalInterestReceived: totalInterestReceived, interestRate: interestRate) for list in poRecurringLookupList { 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 interestAccrued = list.value(forKey: "interestAccrued") as! Double let interestCredited = list.value(forKey: "interestCredited") as! Double let totalInterestAccrued = list.value(forKey: "totalInterestAccrued") as! Double let closingBalance = list.value(forKey: "closingBalance") as! Double let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber let list = PORecurringLookupList(serialNo: Int(truncating: serialNo), daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear, openingBal: openingBal, amountDeposited: amountDeposited, interestAccrued: interestAccrued, interestCredited: interestCredited, totalInterestAccrued: totalInterestAccrued, closingBalance: closingBalance) poRecurringData.poRecurringLookupList.append(list) } self.poRecurringModel.append(poRecurringData) delegate.navigate(poRecurringData) } case .failure(let error): delegate.stopLoader() print(error) //delegate.showError(error: error) } } } } } protocol PORecurringDepositViewProtocol { func startLoader() func stopLoader() func showError(error:String) func setInterestRate(rate:String) func navigate(_ data:PORecurringDepositModel) }