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