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