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