001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.model.control.sales.server.transfer;
018
019import com.echothree.model.control.accounting.server.control.AccountingControl;
020import com.echothree.model.control.batch.server.control.BatchControl;
021import com.echothree.model.control.batch.server.transfer.GenericBatchTransferCache;
022import com.echothree.model.control.core.server.control.EntityInstanceControl;
023import com.echothree.model.control.order.server.control.OrderBatchControl;
024import com.echothree.model.control.payment.server.control.PaymentMethodControl;
025import com.echothree.model.control.sales.common.transfer.SalesOrderBatchTransfer;
026import com.echothree.model.control.sales.server.control.SalesOrderBatchControl;
027import com.echothree.model.data.batch.server.entity.Batch;
028import com.echothree.model.data.user.server.entity.UserVisit;
029import com.echothree.util.server.persistence.Session;
030import com.echothree.util.server.string.AmountUtils;
031import javax.enterprise.context.RequestScoped;
032
033@RequestScoped
034public class SalesOrderBatchTransferCache
035        extends GenericBatchTransferCache<SalesOrderBatchTransfer> {
036    
037    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
038    BatchControl batchControl = Session.getModelController(BatchControl.class);
039    EntityInstanceControl entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
040    PaymentMethodControl paymentMethodControl = Session.getModelController(PaymentMethodControl.class);
041    OrderBatchControl orderBatchControl = Session.getModelController(OrderBatchControl.class);
042    SalesOrderBatchControl salesOrderBatchControl = Session.getModelController(SalesOrderBatchControl.class);
043
044    /** Creates a new instance of SalesOrderBatchTransferCache */
045    protected SalesOrderBatchTransferCache() {
046        super();
047
048        setIncludeEntityInstance(true);
049    }
050    
051    @Override
052    public SalesOrderBatchTransfer getTransfer(UserVisit userVisit, Batch batch) {
053        var salesOrderBatchTransfer = get(batch);
054        
055        if(salesOrderBatchTransfer == null) {
056            var batchDetail = batch.getLastDetail();
057            var orderBatch = orderBatchControl.getOrderBatch(batch);
058            var salesOrderBatch = salesOrderBatchControl.getSalesOrderBatch(batch);
059            var batchTypeTransfer = batchControl.getBatchTypeTransfer(userVisit, batchDetail.getBatchType());
060            var batchName = batchDetail.getBatchName();
061            var currency = orderBatch.getCurrency();
062            var currencyTransfer = accountingControl.getCurrencyTransfer(userVisit, currency);
063            var count = orderBatch.getCount();
064            var paymentMethodTransfer = paymentMethodControl.getPaymentMethodTransfer(userVisit, salesOrderBatch.getPaymentMethod());
065            var amount = AmountUtils.getInstance().formatPriceLine(currency, orderBatch.getAmount());
066            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(batch.getPrimaryKey());
067            
068            salesOrderBatchTransfer = new SalesOrderBatchTransfer(batchTypeTransfer, batchName, currencyTransfer, paymentMethodTransfer, count, amount,
069                    getBatchStatus(userVisit, batch, entityInstance));
070            put(userVisit, batch, salesOrderBatchTransfer, entityInstance);
071            
072            handleOptions(userVisit, batch, salesOrderBatchTransfer);
073        }
074        
075        return salesOrderBatchTransfer;
076    }
077    
078}