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