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.sales.server.command;
018
019import com.echothree.control.user.sales.common.form.CreateSalesOrderPaymentPreferenceForm;
020import com.echothree.control.user.sales.common.result.SalesResultFactory;
021import com.echothree.model.control.order.server.logic.OrderLogic;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.payment.server.logic.PartyPaymentMethodLogic;
024import com.echothree.model.control.payment.server.logic.PaymentMethodLogic;
025import com.echothree.model.control.sales.server.logic.SalesOrderLogic;
026import com.echothree.model.control.sales.server.logic.SalesOrderPaymentPreferenceLogic;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.order.server.entity.OrderPaymentPreference;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
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.server.control.BaseSimpleCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.validation.Validator;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041
042@Dependent
043public class CreateSalesOrderPaymentPreferenceCommand
044        extends BaseSimpleCommand<CreateSalesOrderPaymentPreferenceForm> {
045
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                    new SecurityRoleDefinition(SecurityRoleGroups.SalesOrderPaymentPreference.name(), SecurityRoles.Create.name())
054                    ))
055                ));
056
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("OrderPaymentPreferenceSequence", FieldType.UNSIGNED_INTEGER, false, null, null),
060                new FieldDefinition("PaymentMethodName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("PartyPaymentMethodName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("WasPresent", FieldType.BOOLEAN, false, null, null),
063                new FieldDefinition("MaximumAmount", FieldType.PRICE_UNIT, false, null, null),
064                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
065                );
066    }
067
068    /** Creates a new instance of CreateSalesOrderPaymentPreferenceCommand */
069    public CreateSalesOrderPaymentPreferenceCommand() {
070        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
071    }
072
073    @Override
074    protected void setupValidator(Validator validator) {
075        var orderName = form.getOrderName();
076        var order = orderName == null ? null : SalesOrderLogic.getInstance().getOrderByName(this, orderName);
077        
078        if(order != null) {
079            validator.setCurrency(OrderLogic.getInstance().getOrderCurrency(order));
080        }
081    }
082    
083    @Override
084    protected BaseResult execute() {
085        var result = SalesResultFactory.getCreateSalesOrderPaymentPreferenceResult();
086        var orderName = form.getOrderName();
087        var order = SalesOrderLogic.getInstance().getOrderByName(this, orderName);
088        var paymentMethodName = form.getPaymentMethodName();
089        var paymentMethod = paymentMethodName == null ? null : PaymentMethodLogic.getInstance().getPaymentMethodByName(this, paymentMethodName);
090        var partyPaymentMethodName = form.getPartyPaymentMethodName();
091        var partyPaymentMethod = partyPaymentMethodName == null ? null : PartyPaymentMethodLogic.getInstance().getPartyPaymentMethodByName(this, partyPaymentMethodName);
092        OrderPaymentPreference orderPaymentPreference = null;
093        
094        if(!hasExecutionErrors()) {
095            var strOrderPaymentPreferenceSequence = form.getOrderPaymentPreferenceSequence();
096            var orderPaymentPreferenceSequence = strOrderPaymentPreferenceSequence == null ? null : Integer.valueOf(strOrderPaymentPreferenceSequence);
097            var strWasPresent = form.getWasPresent();
098            var wasPresent = strWasPresent == null ? null : Boolean.valueOf(strWasPresent);
099            var strMaximumAmount = form.getMaximumAmount();
100            var maximumAmount = strMaximumAmount == null ? null : Long.valueOf(strMaximumAmount);
101            var sortOrder = Integer.valueOf(form.getSortOrder());
102
103            orderPaymentPreference = SalesOrderPaymentPreferenceLogic.getInstance().createSalesOrderPaymentPreference(session,
104                    this, order, orderPaymentPreferenceSequence, paymentMethod, partyPaymentMethod, wasPresent, maximumAmount,
105                    sortOrder, getPartyPK());
106        }
107        
108        if(orderPaymentPreference != null) {
109            var orderPaymentPreferenceDetail = orderPaymentPreference.getLastDetail();
110            
111            result.setOrderName(orderPaymentPreferenceDetail.getOrder().getLastDetail().getOrderName());
112            result.setOrderPaymentPreferenceSequence(orderPaymentPreferenceDetail.getOrderPaymentPreferenceSequence().toString());
113            result.setEntityRef(orderPaymentPreference.getPrimaryKey().getEntityRef());
114        }
115
116        return result;
117    }
118
119}