//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nccloud.api.impl.so.m30.fill;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.pubapp.AppBsContext;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.pub.PubAppTool;
import nc.vo.scmpub.fill.pricemny.INumPriceMnyCalculator;
import nc.vo.so.m30.entity.SaleOrderVO;
import nc.vo.so.m30.rule.DirectStoreRule;
import nc.vo.so.m30.rule.PayTermRule;
import nc.vo.so.pub.SOConstant;
import nc.vo.so.pub.enumeration.BillStatus;
import nc.vo.so.pub.keyvalue.IKeyValue;
import nc.vo.so.pub.keyvalue.VOKeyValue;
import nc.vo.so.pub.rule.*;
import nc.vo.so.pub.util.ArrayUtil;

import java.util.TimeZone;

public class SaleOrderSaveFillValue {
    public SaleOrderSaveFillValue() {
    }

    public void setDefValue(SaleOrderVO[] vos) throws BusinessException {
        SaleOrgFillRule<SaleOrderVO> orgfill = new SaleOrgFillRule(vos);
        orgfill.setOrgEmplyDept();
        BusitypeFillRule<SaleOrderVO> busitypefill = new BusitypeFillRule(vos);
        busitypefill.setBusitype();
        MaterialFullRule<SaleOrderVO> materialfull = new MaterialFullRule(vos);
        materialfull.setMaterialOid();

        for (SaleOrderVO salebillvo : vos) {
            IKeyValue keyValue = new VOKeyValue(salebillvo);
            this.setDefultInfo(keyValue);
        }

        INumPriceMnyCalculator cal = new SaleOrderNPriceMnyCal(vos);
        cal.calculate();
    }

    public void setDefValue1(SaleOrderVO[] vos) throws BusinessException {
        SaleOrgFillRule<SaleOrderVO> orgfill = new SaleOrgFillRule(vos);
        orgfill.setOrgEmplyDept();
        BusitypeFillRule<SaleOrderVO> busitypefill = new BusitypeFillRule(vos);
        busitypefill.setBusitype();
        MaterialFullRule<SaleOrderVO> materialfull = new MaterialFullRule(vos);
        materialfull.setMaterialOid();

        for (SaleOrderVO salebillvo : vos) {
            IKeyValue keyValue = new VOKeyValue(salebillvo);
            this.setDefultInfo(keyValue);
        }
    }

    private void setDefultInfo(IKeyValue keyValue) {
        this.setForceDefValue(keyValue);
        BodyValueRowRule bodycouuitl = new BodyValueRowRule(keyValue);
        SaleOrgRelationRule orgrelrule = new SaleOrgRelationRule(keyValue);
        int[] sendstockrows = bodycouuitl.getValueNullRows("csendstockorgvid");
        orgrelrule.setSendStockOrg(sendstockrows);
        int[] trafficrows = bodycouuitl.getValueNullRows("ctrafficorgvid");
        orgrelrule.setTrafficOrg(trafficrows);
        int[] finacerows = bodycouuitl.getValueNullRows("csettleorgvid");
        orgrelrule.setFinanceOrg(finacerows);
        SOProfitCenterValueRule profitRule = new SOProfitCenterValueRule(keyValue);
        profitRule.setProfitCenterValue("csprofitcentervid", "csprofitcenterid", sendstockrows);
        SOCurrencyRule currule = new SOCurrencyRule(keyValue);
        currule.setCurrency(finacerows);
        SOCustRelaDefValueRule custrefrule = new SOCustRelaDefValueRule(keyValue);
        custrefrule.setCustRelaDefValue();
        SOExchangeRateRule exrule = new SOExchangeRateRule(keyValue);
        exrule.calcBodyExchangeRates(finacerows);
        PayTermRule payTermRule = new PayTermRule(keyValue);
        payTermRule.setPayTermInfo();
        int[] custisnullrows = bodycouuitl.getValueNullRows("creceivecustid");
        custrefrule.setRelaReceiveCust(custisnullrows);
        int[] eiveaddrnullrows = bodycouuitl.getValueNullRows("creceiveaddrid");
        ReceiveCustDefAddrRule defaddrule = new ReceiveCustDefAddrRule(keyValue);
        defaddrule.setCustDefaultAddress(eiveaddrnullrows);
        int[] needchangerows = ArrayUtil.combinArrays(new int[][]{sendstockrows, finacerows});
        SOCountryInfoRule countryrule = new SOCountryInfoRule(keyValue);
        countryrule.setCountryInfo(needchangerows);
        SOBuysellTriaRule buyflgrule = new SOBuysellTriaRule(keyValue);
        buyflgrule.setBuysellAndTriaFlag(needchangerows);
        int[] rows = bodycouuitl.getMarNotNullRows();
        String ctrantypeid = keyValue.getHeadStringValue("ctrantypeid");
        if (!PubAppTool.isNull(ctrantypeid)) {
            DirectStoreRule dirstorerule = new DirectStoreRule(keyValue);
            dirstorerule.setDirectStore(rows);
        }

        SOGlobalExchangeRate globalraterule = new SOGlobalExchangeRate(keyValue);
        globalraterule.calcGlobalExchangeRate(rows);
        SOGroupExchangeRate groupraterule = new SOGroupExchangeRate(keyValue);
        groupraterule.calcGroupExchangeRate(rows);
    }

    private void setForceDefValue(IKeyValue keyValue) {
        keyValue.setHeadValue("fstatusflag", BillStatus.FREE.getIntegerValue());
        UFDouble discountrate = keyValue.getHeadUFDoubleValue("ndiscountrate");
        if (null == discountrate) {
            discountrate = SOConstant.ONEHUNDRED;
            keyValue.setHeadValue("ndiscountrate", discountrate);
        }

        String invoicecust = keyValue.getHeadStringValue("cinvoicecustid");
        if (PubAppTool.isNull(invoicecust)) {
            SOCustRelaDefValueRule custrelarule = new SOCustRelaDefValueRule(keyValue);
            custrelarule.setCustRelaInvoiceCust();
        }

        UFDate busdate = AppBsContext.getInstance().getBusiDate();
        if (keyValue.getHeadValue("dbilldate") == null) {
            keyValue.setHeadValue("dbilldate", busdate);
        }

        String pk_group = AppBsContext.getInstance().getPkGroup();
        keyValue.setHeadValue("pk_group", pk_group);
        String pk_org = keyValue.getHeadStringValue("pk_org");
        String timeZone = InvocationInfoProxy.getInstance().getTimeZone();
        TimeZone zone = TimeZone.getTimeZone(timeZone);
        UFDate enddate = busdate.asEnd(zone);
        if (!InvocationInfoProxy.getInstance().getSensitiveTimeZone()) {
            enddate = busdate.asEnd();
        }

        int bodycount = keyValue.getBodyCount();

        for (int i = 0; i < bodycount; ++i) {
            keyValue.setBodyValue(i, "dbilldate", busdate);
            keyValue.setBodyValue(i, "pk_group", pk_group);
            keyValue.setBodyValue(i, "pk_org", pk_org);
            UFDate senddate = keyValue.getBodyUFDateValue(i, "dsenddate");
            if (null == senddate || senddate.ufBefore(busdate)) {
                keyValue.setBodyValue(i, "dsenddate", enddate);
            }

            UFDate receivedate = keyValue.getBodyUFDateValue(i, "dreceivedate");
            if (null == receivedate || receivedate.ufBefore(busdate)) {
                keyValue.setBodyValue(i, "dreceivedate", enddate);
            }

            UFDouble disrate = keyValue.getBodyUFDoubleValue(i, "ndiscountrate");
            if (null == disrate) {
                keyValue.setBodyValue(i, "ndiscountrate", discountrate);
            }

            UFDouble itemdisrate = keyValue.getBodyUFDoubleValue(i, "nitemdiscountrate");
            if (null == itemdisrate) {
                keyValue.setBodyValue(i, "nitemdiscountrate", SOConstant.ONEHUNDRED);
            }

            keyValue.setBodyValue(i, "frowstatus", BillStatus.FREE.getIntegerValue());
        }

    }
}
