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