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.CreateSalesOrderLineForm; 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.sales.server.logic.SalesOrderLineLogic; 024import com.echothree.model.control.sales.server.logic.SalesOrderLogic; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.data.order.server.entity.Order; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import com.echothree.util.server.validation.Validator; 037import java.util.Arrays; 038import java.util.Collections; 039import java.util.List; 040 041public class CreateSalesOrderLineCommand 042 extends BaseSimpleCommand<CreateSalesOrderLineForm> { 043 044 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 049 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 050 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 051 new SecurityRoleDefinition(SecurityRoleGroups.SalesOrderLine.name(), SecurityRoles.Create.name()) 052 ))) 053 ))); 054 055 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 056 new FieldDefinition("OrderName", FieldType.ENTITY_NAME, false, null, null), 057 new FieldDefinition("OrderLineSequence", FieldType.UNSIGNED_INTEGER, false, null, null), 058 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("Quantity", FieldType.UNSIGNED_LONG, true, null, null), 062 new FieldDefinition("UnitAmount", FieldType.UNSIGNED_PRICE_UNIT, false, null, null), 063 new FieldDefinition("Taxable", FieldType.BOOLEAN, false, null, null), 064 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L), 065 new FieldDefinition("CancellationPolicyName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("ReturnPolicyName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("SourceName", FieldType.ENTITY_NAME, false, null, null) 068 )); 069 } 070 071 /** Creates a new instance of CreateSalesOrderLineCommand */ 072 public CreateSalesOrderLineCommand(UserVisitPK userVisitPK, CreateSalesOrderLineForm form) { 073 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 074 } 075 076 @Override 077 protected void setupValidator(Validator validator) { 078 String orderName = form.getOrderName(); 079 Order order = orderName == null ? null : SalesOrderLogic.getInstance().getOrderByName(this, orderName); 080 081 if(order != null) { 082 validator.setCurrency(OrderLogic.getInstance().getOrderCurrency(order)); 083 } 084 } 085 086 @Override 087 protected BaseResult execute() { 088 var result = SalesResultFactory.getCreateSalesOrderLineResult(); 089 var orderName = form.getOrderName(); 090 var itemName = form.getItemName(); 091 var inventoryConditionName = form.getInventoryConditionName(); 092 var cancellationPolicyName = form.getCancellationPolicyName(); 093 var returnPolicyName = form.getReturnPolicyName(); 094 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 095 var sourceName = form.getSourceName(); 096 var orderLineSequence = form.getOrderLineSequence(); 097 var quantity = form.getQuantity(); 098 var unitAmount = form.getUnitAmount(); 099 var description = form.getDescription(); 100 var taxable = form.getTaxable(); 101 102 var orderLine = SalesOrderLineLogic.getInstance().createOrderLine(session, this, getUserVisit(), orderName, 103 itemName, inventoryConditionName, cancellationPolicyName, returnPolicyName, unitOfMeasureTypeName, 104 sourceName, orderLineSequence, quantity, unitAmount, description, taxable, getParty()); 105 106 if(!hasExecutionErrors()) { 107 var orderLineDetail = orderLine.getLastDetail(); 108 109 result.setOrderName(orderLineDetail.getOrder().getLastDetail().getOrderName()); 110 result.setOrderLineSequence(orderLineDetail.getOrderLineSequence().toString()); 111 result.setEntityRef(orderLine.getPrimaryKey().getEntityRef()); 112 } 113 114 return result; 115 } 116 117}