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