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