001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.control.user.term.server.command;
018
019import com.echothree.control.user.term.common.form.CreatePartyCreditLimitForm;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.term.server.control.TermControl;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.server.control.BaseSimpleCommand;
030import com.echothree.util.server.persistence.Session;
031import java.util.Arrays;
032import java.util.Collections;
033import java.util.List;
034import javax.enterprise.context.RequestScoped;
035
036@RequestScoped
037public class CreatePartyCreditLimitCommand
038        extends BaseSimpleCommand<CreatePartyCreditLimitForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("CreditLimit:CurrencyIsoName,CurrencyIsoName", FieldType.UNSIGNED_PRICE_LINE, false, null, null),
047                new FieldDefinition("PotentialCreditLimit:CurrencyIsoName,CurrencyIsoName", FieldType.UNSIGNED_PRICE_LINE, false, null, null)
048                ));
049    }
050    
051    /** Creates a new instance of CreatePartyCreditLimitCommand */
052    public CreatePartyCreditLimitCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, false);
054    }
055    
056    @Override
057    protected BaseResult execute() {
058        var partyControl = Session.getModelController(PartyControl.class);
059        var partyName = form.getPartyName();
060        var party = partyControl.getPartyByName(partyName);
061        
062        if(party != null) {
063            var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName();
064            
065            if(partyTypeName.equals(PartyTypes.CUSTOMER.name()) || partyTypeName.equals(PartyTypes.VENDOR.name())) {
066                var accountingControl = Session.getModelController(AccountingControl.class);
067                var currencyIsoName = form.getCurrencyIsoName();
068                var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
069                
070                if(currency != null) {
071                    var termControl = Session.getModelController(TermControl.class);
072                    var partyCreditLimit = termControl.getPartyCreditLimit(party, currency);
073                    
074                    if(partyCreditLimit == null) {
075                        var strCreditLimit = form.getCreditLimit();
076                        var creditLimit = strCreditLimit == null? null: Long.valueOf(strCreditLimit);
077                        var strPotentialCreditLimit = form.getPotentialCreditLimit();
078                        var potentialCreditLimit = strPotentialCreditLimit == null? null: Long.valueOf(strPotentialCreditLimit);
079                        
080                        if(creditLimit != null || potentialCreditLimit != null) {
081                            termControl.createPartyCreditLimit(party, currency, creditLimit, potentialCreditLimit, getPartyPK());
082                        }
083                    } else {
084                        addExecutionError(ExecutionErrors.DuplicatePartyCreditLimit.name());
085                    }
086                } else {
087                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
088                }
089            } else {
090                addExecutionError(ExecutionErrors.InvalidPartyType.name(), partyTypeName);
091            }
092        } else {
093            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
094        }
095        
096        return null;
097    }
098    
099}