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.search.server.command;
018
019import com.echothree.control.user.search.common.form.SearchSalesOrderBatchesForm;
020import com.echothree.control.user.search.common.result.SearchResultFactory;
021import com.echothree.model.control.accounting.server.logic.CurrencyLogic;
022import com.echothree.model.control.batch.common.BatchConstants;
023import com.echothree.model.control.batch.server.logic.BatchLogic;
024import com.echothree.model.control.payment.server.logic.PaymentMethodLogic;
025import com.echothree.model.control.sales.server.search.SalesOrderBatchSearchEvaluator;
026import com.echothree.model.control.search.common.SearchKinds;
027import com.echothree.model.control.search.server.logic.SearchLogic;
028import com.echothree.model.control.sales.common.workflow.SalesOrderBatchStatusConstants;
029import com.echothree.model.control.workflow.server.control.WorkflowControl;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.persistence.Session;
037import com.google.common.base.Splitter;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040
041@Dependent
042public class SearchSalesOrderBatchesCommand
043        extends BaseSimpleCommand<SearchSalesOrderBatchesForm> {
044    
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046
047    static {
048        FORM_FIELD_DEFINITIONS = List.of(
049                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("BatchName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
052                new FieldDefinition("PaymentMethodName", FieldType.ENTITY_NAME, false, null, null),
053                new FieldDefinition("SalesOrderBatchStatusChoice", FieldType.ENTITY_NAME, false, null, null),
054                new FieldDefinition("BatchAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
055                new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null),
056                new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null),
057                new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null),
058                new FieldDefinition("Fields", FieldType.STRING, false, null, null)
059                );
060    }
061
062    /** Creates a new instance of SearchSalesOrderBatchesCommand */
063    public SearchSalesOrderBatchesCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, false);
065    }
066    
067    @Override
068    protected BaseResult execute() {
069        var searchLogic = SearchLogic.getInstance();
070        var result = SearchResultFactory.getSearchSalesOrderBatchesResult();
071        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.SALES_ORDER_BATCH.name());
072
073        if(!hasExecutionErrors()) {
074            var searchTypeName = form.getSearchTypeName();
075            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
076
077            if(!hasExecutionErrors()) {
078                var currencyIsoName = form.getCurrencyIsoName();
079                var currency = currencyIsoName == null ? null : CurrencyLogic.getInstance().getCurrencyByName(this, currencyIsoName);
080
081                if(!hasExecutionErrors()) {
082                    var paymentMethodName = form.getPaymentMethodName();
083                    var paymentMethod = paymentMethodName == null ? null : PaymentMethodLogic.getInstance().getPaymentMethodByName(this, paymentMethodName);
084
085                    if(!hasExecutionErrors()) {
086                        var workflowControl = Session.getModelController(WorkflowControl.class);
087                        var salesOrderBatchStatusChoice = form.getSalesOrderBatchStatusChoice();
088                        var salesOrderBatchStatusWorkflowStep = salesOrderBatchStatusChoice == null ? null :
089                            workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(SalesOrderBatchStatusConstants.Workflow_SALES_ORDER_BATCH_STATUS), salesOrderBatchStatusChoice);
090
091                        if(salesOrderBatchStatusChoice == null || salesOrderBatchStatusChoice != null) {
092                            var batchLogic = BatchLogic.getInstance();
093                            var batchType = batchLogic.getBatchTypeByName(this, BatchConstants.BatchType_SALES_ORDER);
094
095                            if(!hasExecutionErrors()) {
096                                var batchAliasTypeName = form.getBatchAliasTypeName();
097                                var batchAliasType = batchAliasTypeName == null ? null : batchLogic.getBatchAliasTypeByName(this, batchType, batchAliasTypeName);
098
099                                if(!hasExecutionErrors()) {
100                                    var salesOrderBatchSearchEvaluator = new SalesOrderBatchSearchEvaluator(getUserVisit(), searchType, searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind), searchLogic.getDefaultSearchSortDirection(null));
101                                    var createdSince = form.getCreatedSince();
102                                    var modifiedSince = form.getModifiedSince();
103                                    var fields = form.getFields();
104
105                                    salesOrderBatchSearchEvaluator.setCurrency(currency);
106                                    salesOrderBatchSearchEvaluator.setPaymentMethod(paymentMethod);
107                                    salesOrderBatchSearchEvaluator.setBatchStatusWorkflowStep(salesOrderBatchStatusWorkflowStep);
108                                    salesOrderBatchSearchEvaluator.setBatchAliasType(batchAliasType);
109                                    salesOrderBatchSearchEvaluator.setAlias(form.getAlias());
110                                    salesOrderBatchSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
111                                    salesOrderBatchSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
112                                    salesOrderBatchSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
113
114                                    result.setCount(salesOrderBatchSearchEvaluator.execute(this));
115                                }
116                            }
117                        } else {
118                            addExecutionError(ExecutionErrors.UnknownSalesOrderBatchStatusChoice.name(), salesOrderBatchStatusChoice);
119                        }                        
120                    }
121                }
122            }
123        }
124        
125        return result;
126    }
127}