001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.term.server.transfer;
018
019import com.echothree.model.control.term.common.TermTypes;
020import com.echothree.model.control.term.common.transfer.TermTransfer;
021import com.echothree.model.control.term.common.transfer.TermTypeTransfer;
022import com.echothree.model.control.term.server.control.TermControl;
023import com.echothree.model.data.term.server.entity.DateDrivenTerm;
024import com.echothree.model.data.term.server.entity.StandardTerm;
025import com.echothree.model.data.term.server.entity.Term;
026import com.echothree.model.data.term.server.entity.TermDetail;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.util.server.string.PercentUtils;
029
030public class TermTransferCache
031        extends BaseTermTransferCache<Term, TermTransfer> {
032    
033    /** Creates a new instance of TermTransferCache */
034    public TermTransferCache(UserVisit userVisit, TermControl termControl) {
035        super(userVisit, termControl);
036        
037        setIncludeEntityInstance(true);
038    }
039    
040    public TermTransfer getTermTransfer(Term term) {
041        TermTransfer termTransfer = get(term);
042        
043        if(termTransfer == null) {
044            TermDetail termDetail = term.getLastDetail();
045            String termName = termDetail.getTermName();
046            TermTypeTransferCache termTypeTransferCache = termControl.getTermTransferCaches(userVisit).getTermTypeTransferCache();
047            TermTypeTransfer termType = termTypeTransferCache.getTermTypeTransfer(termDetail.getTermType());
048            Boolean isDefault = termDetail.getIsDefault();
049            Integer sortOrder = termDetail.getSortOrder();
050            String description = termControl.getBestTermDescription(term, getLanguage());
051            String netDueDays = null;
052            String discountPercentage = null;
053            String discountDays = null;
054            String netDueDayOfMonth = null;
055            String dueNextMonthDays = null;
056            String discountBeforeDayOfMonth = null;
057            
058            String termTypeName = termDetail.getTermType().getTermTypeName();
059            if(termTypeName.equals(TermTypes.STANDARD.name())) {
060                StandardTerm standardTerm = termControl.getStandardTerm(term);
061                
062                netDueDays = standardTerm.getNetDueDays().toString();
063                discountPercentage = PercentUtils.getInstance().formatFractionalPercent(standardTerm.getDiscountPercentage());
064                discountDays = standardTerm.getDiscountDays().toString();
065            } else if(termTypeName.equals(TermTypes.DATE_DRIVEN.name())) {
066                DateDrivenTerm dateDrivenTerm = termControl.getDateDrivenTerm(term);
067                
068                netDueDayOfMonth = dateDrivenTerm.getNetDueDayOfMonth().toString();
069                dueNextMonthDays = dateDrivenTerm.getDueNextMonthDays().toString();
070                discountPercentage = PercentUtils.getInstance().formatFractionalPercent(dateDrivenTerm.getDiscountPercentage());
071                discountBeforeDayOfMonth = dateDrivenTerm.getDiscountBeforeDayOfMonth().toString();
072            }
073            
074            termTransfer = new TermTransfer(termName, termType, isDefault, sortOrder, description,
075                    discountPercentage, netDueDays, discountDays, netDueDayOfMonth, dueNextMonthDays, discountBeforeDayOfMonth);
076            put(term, termTransfer);
077        }
078        return termTransfer;
079    }
080    
081}