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 java.util.List; 031import javax.enterprise.context.Dependent; 032import javax.inject.Inject; 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 @Inject 050 AccountingControl accountingControl; 051 052 @Inject 053 PartyControl partyControl; 054 055 @Inject 056 TermControl termControl; 057 058 059 /** Creates a new instance of CreatePartyCreditLimitCommand */ 060 public CreatePartyCreditLimitCommand() { 061 super(null, FORM_FIELD_DEFINITIONS, false); 062 } 063 064 @Override 065 protected BaseResult execute() { 066 var partyName = form.getPartyName(); 067 var party = partyControl.getPartyByName(partyName); 068 069 if(party != null) { 070 var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); 071 072 if(partyTypeName.equals(PartyTypes.CUSTOMER.name()) || partyTypeName.equals(PartyTypes.VENDOR.name())) { 073 var currencyIsoName = form.getCurrencyIsoName(); 074 var currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 075 076 if(currency != null) { 077 var partyCreditLimit = termControl.getPartyCreditLimit(party, currency); 078 079 if(partyCreditLimit == null) { 080 var strCreditLimit = form.getCreditLimit(); 081 var creditLimit = strCreditLimit == null? null: Long.valueOf(strCreditLimit); 082 var strPotentialCreditLimit = form.getPotentialCreditLimit(); 083 var potentialCreditLimit = strPotentialCreditLimit == null? null: Long.valueOf(strPotentialCreditLimit); 084 085 if(creditLimit != null || potentialCreditLimit != null) { 086 termControl.createPartyCreditLimit(party, currency, creditLimit, potentialCreditLimit, getPartyPK()); 087 } 088 } else { 089 addExecutionError(ExecutionErrors.DuplicatePartyCreditLimit.name()); 090 } 091 } else { 092 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 093 } 094 } else { 095 addExecutionError(ExecutionErrors.InvalidPartyType.name(), partyTypeName); 096 } 097 } else { 098 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 099 } 100 101 return null; 102 } 103 104}