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