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.edit.PartyCreditLimitEdit; 020import com.echothree.control.user.term.common.edit.TermEditFactory; 021import com.echothree.control.user.term.common.form.EditPartyCreditLimitForm; 022import com.echothree.control.user.term.common.result.TermResultFactory; 023import com.echothree.control.user.term.common.spec.PartyCreditLimitSpec; 024import com.echothree.model.control.accounting.server.control.AccountingControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.party.server.logic.PartyChainLogic; 028import com.echothree.model.control.term.server.control.TermControl; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.command.EditMode; 035import com.echothree.util.common.form.BaseForm; 036import com.echothree.util.server.control.BaseEditCommand; 037import com.echothree.util.server.persistence.Session; 038import com.echothree.util.server.string.AmountUtils; 039import com.echothree.util.server.validation.Validator; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042 043@Dependent 044public class EditPartyCreditLimitCommand 045 extends BaseEditCommand<PartyCreditLimitSpec, PartyCreditLimitEdit> { 046 047 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 048 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 049 050 static { 051 SPEC_FIELD_DEFINITIONS = List.of( 052 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null), 053 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null) 054 ); 055 056 EDIT_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("CreditLimit", FieldType.UNSIGNED_PRICE_LINE, false, null, null), 058 new FieldDefinition("PotentialCreditLimit", FieldType.UNSIGNED_PRICE_LINE, false, null, null) 059 ); 060 } 061 062 /** Creates a new instance of EditPartyCreditLimitCommand */ 063 public EditPartyCreditLimitCommand() { 064 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 065 } 066 067 @Override 068 protected void setupValidatorForEdit(Validator validator, BaseForm specForm) { 069 var accountingControl = Session.getModelController(AccountingControl.class); 070 var currencyIsoName = spec.getCurrencyIsoName(); 071 validator.setCurrency(accountingControl.getCurrencyByIsoName(currencyIsoName)); 072 } 073 074 @Override 075 protected BaseResult execute() { 076 var partyControl = Session.getModelController(PartyControl.class); 077 var result = TermResultFactory.getEditPartyCreditLimitResult(); 078 var partyName = spec.getPartyName(); 079 var party = partyControl.getPartyByName(partyName); 080 081 if(party != null) { 082 var accountingControl = Session.getModelController(AccountingControl.class); 083 var currencyIsoName = spec.getCurrencyIsoName(); 084 var currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 085 086 if(currency != null) { 087 var termControl = Session.getModelController(TermControl.class); 088 089 if(editMode.equals(EditMode.LOCK)) { 090 var partyCreditLimit = termControl.getPartyCreditLimit(party, currency); 091 092 if(partyCreditLimit != null) { 093 result.setPartyCreditLimit(termControl.getPartyCreditLimitTransfer(getUserVisit(), partyCreditLimit)); 094 095 if(lockEntity(party)) { 096 var edit = TermEditFactory.getPartyCreditLimitEdit(); 097 098 result.setEdit(edit); 099 edit.setCreditLimit(AmountUtils.getInstance().formatPriceLine(currency, partyCreditLimit.getCreditLimit())); 100 edit.setPotentialCreditLimit(AmountUtils.getInstance().formatPriceLine(currency, partyCreditLimit.getPotentialCreditLimit())); 101 } else { 102 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 103 } 104 105 result.setEntityLock(getEntityLockTransfer(party)); 106 } else { 107 addExecutionError(ExecutionErrors.UnknownPartyCreditLimit.name()); 108 } 109 } else if(editMode.equals(EditMode.UPDATE)) { 110 var partyCreditLimitValue = termControl.getPartyCreditLimitValueForUpdate(party, currency); 111 112 if(partyCreditLimitValue != null) { 113 if(lockEntityForUpdate(party)) { 114 var strCreditLimit = edit.getCreditLimit(); 115 var creditLimit = strCreditLimit == null? null: Long.valueOf(strCreditLimit); 116 var strPotentialCreditLimit = edit.getPotentialCreditLimit(); 117 var potentialCreditLimit = strPotentialCreditLimit == null? null: Long.valueOf(strPotentialCreditLimit); 118 119 try { 120 partyCreditLimitValue.setCreditLimit(creditLimit); 121 partyCreditLimitValue.setPotentialCreditLimit(potentialCreditLimit); 122 123 if(partyCreditLimitValue.hasBeenModified()) { 124 var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); 125 var updatedBy = getPartyPK(); 126 127 termControl.updatePartyCreditLimitFromValue(partyCreditLimitValue, updatedBy); 128 129 if(partyTypeName.equals(PartyTypes.CUSTOMER.name())) { 130 // ExecutionErrorAccumulator is passed in as null so that an Exception will be thrown if there is an error. 131 PartyChainLogic.getInstance().createPartyCreditLimitChangedChainInstance(null, party, updatedBy); 132 } 133 } 134 } finally { 135 unlockEntity(party); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.EntityLockStale.name()); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownPartyCreditLimit.name()); 142 } 143 } 144 } else { 145 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 146 } 147 } else { 148 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 149 } 150 151 return result; 152 } 153 154}