private CounterTO[] getAllTrx(Date trxDate) { BonusDAO bonusDAO = new BonusDAO(); CounterTO[] listCounterTO = bonusDAO.getAllTrx(trxDate); for (CounterTO cTO : listCounterTO) { BizCenterTO[] bcTOList = bonusDAO.getAllBizCtrInfo(cTO.getMbrTO().getId()); cTO.getMbrTO().setBizCtrTOList(bcTOList); } return listCounterTO; }
private CounterTO getRegCounterTO(String mbrId) { BonusDAO bonusDAO = new BonusDAO(); CounterTO counterTO = bonusDAO.getRegCounterTO(mbrId); int countryId = Integer.parseInt(counterTO.getBranchTO().getCountry().getId()); int salesItemId = ConstantValue.MBR_TYPE_PLATINUM; SalesItemPricingService sipService = new SalesItemPricingService(); SalesItemPricingTO sipTO = sipService.loadCounterInfo(salesItemId, countryId); counterTO.setPlatinumBv(sipTO.getBv()); return counterTO; }
public void calculateBonus(BonusTO bonusTO) { CounterTO[] allTrx = getAllTrx(bonusTO.getTrxDate()); if (allTrx != null && allTrx.length > 0) { MbrTO newMbr = new MbrTO(); Map<String, BonusTO> mainMap = new HashMap<String, BonusTO>(); Map<String, ArrayList<BonusReferralTO>> referMap = new HashMap<String, ArrayList<BonusReferralTO>>(); Map<String, BonusDevelopmentTO[]> devMap = new HashMap<String, BonusDevelopmentTO[]>(); Map<String, ArrayList<BonusDevInfoTO>> devInfoMap = new HashMap<String, ArrayList<BonusDevInfoTO>>(); Map<String, ArrayList<BonusMatchingTO>> matchMap = new HashMap<String, ArrayList<BonusMatchingTO>>(); for (int i = 0; i < allTrx.length; i++) { newMbr = allTrx[i].getMbrTO(); SponsorCenterTO scTO = allTrx[i].getMbrTO().getSponsorCenterTO(); double totalBv = allTrx[i].getTotalBv(); ArrayList<BonusReferralTO> referralList; BonusReferralTO brTO = new BonusReferralTO(); if (referMap.get(scTO.getUpMemberId()) != null) { referralList = referMap.get(scTO.getUpMemberId()); BonusReferralTO referTO = referralList.get(0); brTO.setMbrTO(referTO.getMbrTO()); brTO.setPercentage(referTO.getPercentage()); brTO.setTotalRW(totalBv); brTO.setTotalAmt(brTO.getTotalRW() * (brTO.getPercentage() / 100)); brTO.setFromMbrTO(newMbr); referralList.add(brTO); } else { referralList = new ArrayList<BonusReferralTO>(); double percentage = ConstantValue.DOUBLE_ZERO; MbrTO sponsorTO = new MbrTO(); sponsorTO.setId(scTO.getUpMemberId()); sponsorTO.setJoinType(scTO.getUpJoinType()); if (sponsorTO.getJoinType() == ConstantValue.MBR_TYPE_DIAMOND || sponsorTO.getJoinType() == ConstantValue.MBR_TYPE_PLATINUM) { percentage = ConstantBonusValue.REFERRAL_PLATINUM; } else if (sponsorTO.getJoinType() == ConstantValue.MBR_TYPE_GOLD) { percentage = ConstantBonusValue.REFERRAL_GOLD; } else if (sponsorTO.getJoinType() == ConstantValue.MBR_TYPE_SILVER) { percentage = ConstantBonusValue.REFERRAL_SILVER; } brTO.setMbrTO(sponsorTO); brTO.setPercentage(percentage * 100); brTO.setTotalRW(totalBv); brTO.setTotalAmt(brTO.getTotalRW() * percentage); brTO.setFromMbrTO(newMbr); referralList.add(brTO); } referMap.put(scTO.getUpMemberId(), referralList); double unitOneBv = ConstantValue.DOUBLE_ZERO; double otherUnitBv = ConstantValue.DOUBLE_ZERO; if (newMbr.getJoinType() == ConstantValue.MBR_TYPE_DIAMOND) { if (allTrx[i].getTrxType() == ConstantValue.TRXN_UPGRADE) { CounterTO regCounterTO = getRegCounterTO(newMbr.getId()); unitOneBv = regCounterTO.getPlatinumBv() - regCounterTO.getTotalBv(); double roundUnitBv = (totalBv - unitOneBv) / ConstantBonusValue.DIAMOND_UPGRADE_UNIT; otherUnitBv = Math.round(roundUnitBv * 100.0) / 100.0; } else { int unitBv = ((int) totalBv) / ConstantValue.TOTAL_BIZ_CENTER; double remainder = totalBv - (unitBv * ConstantValue.TOTAL_BIZ_CENTER); double roundUnitBv = unitBv + remainder; unitOneBv = Math.round(roundUnitBv * 100.0) / 100.0; otherUnitBv = unitBv; } } else { unitOneBv = totalBv; } for (BizCenterTO bcTO : allTrx[i].getMbrTO().getBizCtrTOList()) { if (bcTO.getStatus().equalsIgnoreCase(ConstantValue.STATUS_ACTIVE)) { String bizTraceKey = bcTO.getTraceKey(); String bizExcludeKey = bcTO.getExcludeKey(); String[] allBizUpline = bizTraceKey.split(">"); String[] allExclude = (bizExcludeKey != null && bizExcludeKey.length() > 0 ? bizExcludeKey.split(">") : null); Map<String, String> bizMap = new HashMap<String, String>(); for (String bizUpline : allBizUpline) { String[] splitBiz = bizUpline.split("-"); String bizId = ConstantValue.EMPTY_STRING; String loc = ConstantValue.EMPTY_STRING; for (String value : splitBiz) { if (value.equalsIgnoreCase(ConstantValue.LOCATION_LEFT) || value.equalsIgnoreCase(ConstantValue.LOCATION_RIGHT)) { loc = value; } else { bizId = value; } } bizMap.put(bizId, loc); } if (allExclude != null) { for (String bizExclude : allExclude) { if (bizMap.get(bizExclude) != null) { bizMap.remove(bizExclude); } } } for (Map.Entry<String, String> bizEntry : bizMap.entrySet()) { double newLeft = ConstantValue.DOUBLE_ZERO; double newRight = ConstantValue.DOUBLE_ZERO; if (bizEntry.getValue().equalsIgnoreCase(ConstantValue.LOCATION_LEFT)) { if (bcTO.getUnit() == ConstantValue.UNIT_1) { newLeft = unitOneBv; } else { newLeft = otherUnitBv; } } else { if (bcTO.getUnit() == ConstantValue.UNIT_1) { newRight = unitOneBv; } else { newRight = otherUnitBv; } } ArrayList<BonusDevInfoTO> devInfoList; BonusDevInfoTO bviTO = new BonusDevInfoTO(); bviTO.setFromMbrTO(newMbr); bviTO.setLeft(newLeft); bviTO.setRight(newRight); if (devInfoMap.get(bizEntry.getKey()) != null) { devInfoList = devInfoMap.get(bizEntry.getKey()); BonusDevInfoTO devInfoTO = devInfoList.get(0); bviTO.setMbrTO(devInfoTO.getMbrTO()); } else { devInfoList = new ArrayList<BonusDevInfoTO>(); MbrTO bizCtrBingoTO = getBizCtrBingoTO(bizEntry.getKey()); bviTO.setMbrTO(bizCtrBingoTO); } devInfoList.add(bviTO); devInfoMap.put(bizEntry.getKey(), devInfoList); } } } } for (Map.Entry<String, ArrayList<BonusReferralTO>> referEntry : referMap.entrySet()) { ArrayList<BonusReferralTO> referList = referEntry.getValue(); BonusReferralTO[] listReferTO = (BonusReferralTO[]) referList.toArray(new BonusReferralTO[0]); MbrTO bonusMbr = null; double totalReferral = ConstantValue.DOUBLE_ZERO; for (BonusReferralTO referTO : listReferTO) { if (bonusMbr == null) { bonusMbr = referTO.getMbrTO(); } totalReferral += referTO.getTotalAmt(); } BonusTO newBonusTO = new BonusTO(); newBonusTO.setTrxDate(bonusTO.getTrxDate()); newBonusTO.setMbrTO(bonusMbr); newBonusTO.setTotalReferral(totalReferral); newBonusTO.setListBonusReferralTO(listReferTO); mainMap.put(newBonusTO.getMbrTO().getId(), newBonusTO); } for (Map.Entry<String, ArrayList<BonusDevInfoTO>> devInfoEntry : devInfoMap.entrySet()) { ArrayList<BonusDevInfoTO> devInfoList = devInfoEntry.getValue(); BonusDevInfoTO[] listDevInfoTO = (BonusDevInfoTO[]) devInfoList.toArray(new BonusDevInfoTO[0]); for (BonusDevInfoTO devInfoTO : listDevInfoTO) { ArrayList<BonusDevelopmentTO> devArrList = new ArrayList<BonusDevelopmentTO>(); BonusDevelopmentTO bdTO = new BonusDevelopmentTO(); bdTO.setListDevInfoTO(listDevInfoTO); if (devMap.get(devInfoTO.getMbrTO().getId()) != null) { BonusDevelopmentTO[] listDevTO = devMap.get(devInfoTO.getMbrTO().getId()); for (BonusDevelopmentTO devTO : listDevTO) { bdTO.setMbrTO(devTO.getMbrTO()); bdTO.setUnit(devTO.getUnit()); bdTO.setBfLeft(devTO.getBfLeft()); bdTO.setBfRight(devTO.getBfRight()); if (devTO.getUnit() == devInfoTO.getMbrTO().getBonusUnit()) { double newLeft = devTO.getNewLeft() + devInfoTO.getLeft(); double newRight = devTO.getNewRight() + devInfoTO.getRight(); bdTO.setNewLeft(newLeft); bdTO.setNewRight(newRight); } else { bdTO.setNewLeft(devTO.getNewLeft()); bdTO.setNewRight(devTO.getNewRight()); } devArrList.add(bdTO); } } else { bdTO.setMbrTO(devInfoTO.getMbrTO()); bdTO.setUnit(devInfoTO.getMbrTO().getBonusUnit()); bdTO.setBfLeft(devInfoTO.getMbrTO().getBonusTotalLeft()); bdTO.setBfRight(devInfoTO.getMbrTO().getBonusTotalRight()); bdTO.setNewLeft(devInfoTO.getLeft()); bdTO.setNewRight(devInfoTO.getRight()); devArrList.add(bdTO); } devMap.put( devInfoTO.getMbrTO().getId(), (BonusDevelopmentTO[]) devArrList.toArray(new BonusDevelopmentTO[0])); } } for (Map.Entry<String, BonusDevelopmentTO[]> devEntry : devMap.entrySet()) { BonusDevelopmentTO[] devList = devEntry.getValue(); MbrTO bonusMbr = null; double totalBingo = ConstantValue.DOUBLE_ZERO; for (BonusDevelopmentTO devTO : devList) { double totalLeft = ConstantValue.DOUBLE_ZERO; double totalRight = ConstantValue.DOUBLE_ZERO; double cfLeft = ConstantValue.DOUBLE_ZERO; double cfRight = ConstantValue.DOUBLE_ZERO; double dailyLimit = ConstantValue.DOUBLE_ZERO; double bingo = ConstantValue.DOUBLE_ZERO; if (bonusMbr == null) { bonusMbr = devTO.getMbrTO(); } totalLeft = devTO.getBfLeft() + devTO.getNewLeft(); totalRight = devTO.getBfRight() + devTO.getNewRight(); if (bonusMbr.getJoinType() == ConstantValue.MBR_TYPE_DIAMOND || bonusMbr.getJoinType() == ConstantValue.MBR_TYPE_PLATINUM) { dailyLimit = ConstantBonusValue.DEV_DAILY_LIMIT_PLATINUM; } else if (bonusMbr.getJoinType() == ConstantValue.MBR_TYPE_GOLD) { dailyLimit = ConstantBonusValue.DEV_DAILY_LIMIT_GOLD; } else if (bonusMbr.getJoinType() == ConstantValue.MBR_TYPE_SILVER) { dailyLimit = ConstantBonusValue.DEV_DAILY_LIMIT_SILVER; } if (totalLeft > ConstantValue.DOUBLE_ZERO && totalRight > ConstantValue.DOUBLE_ZERO) { if (totalLeft == totalRight) { bingo = (totalLeft * ConstantBonusValue.DEVELOPMENT_PERCENTAGE); if (bingo > dailyLimit) { bingo = dailyLimit; } } else if (totalLeft > totalRight) { bingo = ((totalLeft - totalRight) * ConstantBonusValue.DEVELOPMENT_PERCENTAGE); if (bingo > dailyLimit) { bingo = dailyLimit; } else { cfLeft = totalLeft - totalRight; } } else { bingo = ((totalRight - totalLeft) * ConstantBonusValue.DEVELOPMENT_PERCENTAGE); if (bingo > dailyLimit) { bingo = dailyLimit; } else { cfRight = totalRight - totalLeft; } } } else { cfLeft = totalLeft; cfRight = totalRight; } devTO.setMbrTO(bonusMbr); devTO.setTotalLeft(totalLeft); devTO.setTotalRight(totalRight); devTO.setCfLeft(cfLeft); devTO.setCfRight(cfRight); devTO.setBingoAmt(bingo); totalBingo += bingo; } if (totalBingo > 0) { MbrTO uplineMatchTO = getMatchingUpline(bonusMbr.getId()); String spTraceKey = uplineMatchTO.getSponsorCenterTO().getTraceKey(); String[] allSponsor = spTraceKey.split(">"); Arrays.sort(allSponsor, Collections.reverseOrder()); int payingLevel = ConstantValue.INTEGER_ONE; for (String sponsor : allSponsor) { MbrTO matchUplineTO = getSimpleMbrTO(sponsor); boolean payable = false; if (matchUplineTO.getJoinType() == ConstantValue.MBR_TYPE_DIAMOND || matchUplineTO.getJoinType() == ConstantValue.MBR_TYPE_PLATINUM) { if (payingLevel <= ConstantBonusValue.MATCH_PAY_LEVEL_PLATINUM) { payable = true; } } else if (matchUplineTO.getJoinType() == ConstantValue.MBR_TYPE_GOLD) { if (payingLevel <= ConstantBonusValue.MATCH_PAY_LEVEL_GOLD) { payable = true; } } else if (matchUplineTO.getJoinType() == ConstantValue.MBR_TYPE_SILVER) { if (payingLevel <= ConstantBonusValue.MATCH_PAY_LEVEL_GOLD) { payable = true; } } if (payable) { ArrayList<BonusMatchingTO> matchingList; BonusMatchingTO bmTO = new BonusMatchingTO(); if (matchMap.get(matchUplineTO.getId()) != null) { matchingList = matchMap.get(matchUplineTO.getId()); BonusMatchingTO matchTO = matchingList.get(0); bmTO.setJoinType(matchTO.getJoinType()); bmTO.setPayLevel(payingLevel); bmTO.setDevBonus(totalBingo); bmTO.setPercentage(ConstantBonusValue.MATCHING_PERCENTAGE * 100); bmTO.setTotalAmt(totalBingo * ConstantBonusValue.MATCHING_PERCENTAGE); bmTO.setMbrTO(matchTO.getMbrTO()); bmTO.setFromMbrTO(bonusMbr); matchingList.add(bmTO); } else { matchingList = new ArrayList<BonusMatchingTO>(); bmTO.setJoinType(newMbr.getJoinType()); bmTO.setPayLevel(payingLevel); bmTO.setDevBonus(totalBingo); bmTO.setPercentage(ConstantBonusValue.MATCHING_PERCENTAGE * 100); bmTO.setTotalAmt(totalBingo * ConstantBonusValue.MATCHING_PERCENTAGE); bmTO.setMbrTO(matchUplineTO); bmTO.setFromMbrTO(bonusMbr); matchingList.add(bmTO); } matchMap.put(matchUplineTO.getId(), matchingList); payingLevel++; } } } BonusTO newBonusTO = new BonusTO(); if (mainMap.get(bonusMbr.getId()) != null) { newBonusTO = mainMap.get(bonusMbr.getId()); newBonusTO.setTotalDevelopment(totalBingo); newBonusTO.setListBonusDevelopmentTO(devList); } else { newBonusTO.setTrxDate(bonusTO.getTrxDate()); newBonusTO.setMbrTO(bonusMbr); newBonusTO.setTotalDevelopment(totalBingo); newBonusTO.setListBonusDevelopmentTO(devList); } mainMap.put(bonusMbr.getId(), newBonusTO); } for (Map.Entry<String, ArrayList<BonusMatchingTO>> matchEntry : matchMap.entrySet()) { ArrayList<BonusMatchingTO> matchList = matchEntry.getValue(); BonusMatchingTO[] listMatchTO = (BonusMatchingTO[]) matchList.toArray(new BonusMatchingTO[0]); MbrTO bonusMbr = null; double totalMatching = ConstantValue.DOUBLE_ZERO; for (BonusMatchingTO matchTO : listMatchTO) { if (bonusMbr == null) { bonusMbr = matchTO.getMbrTO(); } totalMatching += matchTO.getTotalAmt(); } BonusTO newBonusTO = new BonusTO(); if (mainMap.get(bonusMbr.getId()) != null) { newBonusTO = mainMap.get(bonusMbr.getId()); newBonusTO.setTotalMatching(totalMatching); newBonusTO.setListBonusMatchingTO(listMatchTO); } else { newBonusTO.setTrxDate(bonusTO.getTrxDate()); newBonusTO.setMbrTO(bonusMbr); newBonusTO.setTotalMatching(totalMatching); newBonusTO.setListBonusMatchingTO(listMatchTO); } mainMap.put(bonusMbr.getId(), newBonusTO); } for (Map.Entry<String, BonusTO> mainEntry : mainMap.entrySet()) { BonusTO bTO = mainEntry.getValue(); double totalBonus = ConstantValue.DOUBLE_ZERO; totalBonus = bTO.getTotalReferral() + bTO.getTotalDevelopment() + bTO.getTotalMatching(); BonusDAO bonusDAO = new BonusDAO(); if (totalBonus > 0) { BonusDetainTO detainTO = getDetainBonusTO(bTO.getMbrTO().getId(), bTO.getTrxDate()); double newDetain = totalBonus * ConstantBonusValue.DETAIN_PERCENTAGE; double totalDetain = ConstantValue.DOUBLE_ZERO; if (detainTO != null && detainTO.getId() > 0) { if (detainTO.getTotalDetain() < ConstantBonusValue.DETAIN_MAX) { totalDetain = newDetain + detainTO.getTotalDetain(); if (totalDetain > ConstantBonusValue.DETAIN_MAX) { detainTO.setTotalDetain(ConstantBonusValue.DETAIN_MAX); bTO.setTotalDetain(ConstantBonusValue.DETAIN_MAX - detainTO.getTotalDetain()); } else { detainTO.setTotalDetain(totalDetain); bTO.setTotalDetain(newDetain); } } } else { totalDetain = newDetain; detainTO.setMbrTO(bTO.getMbrTO()); detainTO.setBonusDate(Utility.dateToBonusMonth(bTO.getTrxDate())); if (totalDetain > ConstantBonusValue.DETAIN_MAX) { bTO.setTotalDetain(ConstantBonusValue.DETAIN_MAX); detainTO.setTotalDetain(ConstantBonusValue.DETAIN_MAX); } else { bTO.setTotalDetain(totalDetain); detainTO.setTotalDetain(totalDetain); } } bTO.setBonusDetainTO(detainTO); bTO.setTotalBonus(totalBonus - bTO.getTotalDetain()); bonusDAO.insertBonus(bTO); } else { bonusDAO.updateBonus(bTO); } } } }