package nc.ui.pu.m25.action.processor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import nc.itf.pu.pub.IPURemoteCallCombinator;
import nc.ui.pu.m25.model.InvoiceBillManageModel;
import nc.ui.pu.m25.view.InvoiceUIState;
import nc.ui.pu.pub.util.BusitypeSetter;
import nc.ui.pubapp.uif2app.actions.intf.ICopyActionProcessor;
import nc.ui.pubapp.uif2app.funcnode.trantype.TrantypeFuncUtils;
import nc.vo.pu.m25.entity.InvoiceHeaderVO;
import nc.vo.pu.m25.entity.InvoiceItemVO;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pu.pub.enumeration.POEnumBillStatus;
import nc.vo.pu.pub.util.BillHelper;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.AppContext;
import nc.vo.scmpub.res.billtype.POBillType;
import nc.vo.uif2.LoginContext;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class CopyActionProcessor implements ICopyActionProcessor<InvoiceVO> {
	private InvoiceBillManageModel model = null;

	public InvoiceBillManageModel getModel() {
		return this.model;
	}

	public void processVOAfterCopy(InvoiceVO billVO, LoginContext context) {
		if (billVO != null) {
			this.batchProc(billVO, context);
			this.setHeadDefaultValues(billVO, context);
			this.setBodyDefaultValues(billVO);
		}
	}

	public void setModel(InvoiceBillManageModel model) {
		this.model = model;
	}

	private void batchProc(InvoiceVO vo, LoginContext context) {
		List<IPURemoteCallCombinator> rccRuleLst = new ArrayList();
		this.register_BizRule(rccRuleLst, vo, context);
		Iterator i$ = rccRuleLst.iterator();

		while (i$.hasNext()) {
			IPURemoteCallCombinator rccRule = (IPURemoteCallCombinator) i$.next();
			if (null != rccRule) {
				rccRule.process();
			}
		}

	}

	private void copyTranstype(InvoiceVO vo, String transtype, String pk_trantype) {
		vo.getParentVO().setPk_busitype((String) null);
		if (StringUtils.isBlank(transtype)) {
			vo.getParentVO().setVtrantypecode((String) null);
			vo.getParentVO().setCtrantypeid((String) null);
		} else {
			vo.getParentVO().setVtrantypecode(transtype);
			vo.getParentVO().setCtrantypeid(pk_trantype);
		}

	}

	private void register_BizRule(List<IPURemoteCallCombinator> rccRuleLst, InvoiceVO vo, LoginContext context) {
		String transtype = TrantypeFuncUtils.getTrantype(context);
		String pk_trantype = TrantypeFuncUtils.getTrantypePk(context);
		this.copyTranstype(vo, transtype, pk_trantype);
		(new BusitypeSetter(POBillType.Invoice, new BillHelper(vo), context)).copySet(rccRuleLst);
	}

	private void setBodyDefaultValues(InvoiceVO billVO) {
		InvoiceItemVO[] items = billVO.getChildrenVO();
		if (!ArrayUtils.isEmpty(items)) {
			InvoiceItemVO[] arr$ = items;
			int len$ = items.length;

			for (int i$ = 0; i$ < len$; ++i$) {
				InvoiceItemVO vo = arr$[i$];
				vo.setPk_invoice((String) null);
				vo.setPk_invoice_b((String) null);
				vo.setCfirstbid((String) null);
				vo.setCfirstid((String) null);
				vo.setCfirsttypecode((String) null);
				vo.setVfirstcode((String) null);
				vo.setVfirstrowno((String) null);
				vo.setVfirsttrantype((String) null);
				vo.setFirstbts((UFDateTime) null);
				vo.setFirstts((UFDateTime) null);
				vo.setCsourcebid((String) null);
				vo.setCsourceid((String) null);
				vo.setCsourcetypecode((String) null);
				vo.setSourcebts((UFDateTime) null);
				vo.setSourcets((UFDateTime) null);
				vo.setVsourcecode((String) null);
				vo.setVsourcerowno((String) null);
				vo.setVsourcetrantype((String) null);
				vo.setTs((UFDateTime) null);
				vo.setNaccumsettmny((UFDouble) null);
				vo.setNaccumsettnum((UFDouble) null);
				vo.setPk_order((String) null);
				vo.setPk_order_b((String) null);
				vo.setVordercode((String) null);
				vo.setVordertrantype((String) null);
				vo.setVbdef13(null);
				vo.setVbdef14(null);
				vo.setVbdef15(null);
				vo.setVbdef16(null);
				vo.setVbdef17(null);
				vo.setVbdef20(null);
				vo.setVbdef12(null);
				vo.setVbdef11(null);
			}

		}
	}

	private void setHeadDefaultValues(InvoiceVO billVO, LoginContext context) {
		InvoiceHeaderVO header = billVO.getParentVO();
		if (header != null) {
			header.setVbillcode((String) null);
			header.setPk_invoice((String) null);
			header.setTs((UFDateTime) null);
			header.setTaudittime((UFDate) null);
			header.setApprover((String) null);
			header.setModifier((String) null);
			header.setModifiedtime((UFDateTime) null);
			header.setIprintcount(0);
			header.setFbillstatus(POEnumBillStatus.FREE.toInteger());
			UFDate curDate = AppContext.getInstance().getBusiDate();
			header.setDarrivedate(curDate);
			header.setDbilldate(curDate);
			header.setCreator(context.getPk_loginUser());
			header.setBillmaker(context.getPk_loginUser());
			header.setBapflag(UFBoolean.FALSE);
			if (this.model.getInvoiceUIState() != InvoiceUIState.FEE_INVC) {
				header.setBfee(UFBoolean.FALSE);
				header.setPk_parentinvoice((String) null);
			}

			header.setBfrozen(UFBoolean.FALSE);
			header.setVfrozenreason((String) null);
			header.setTfrozentime((UFDate) null);
			header.setPk_frozenuser((String) null);
			header.setBvirtual(UFBoolean.FALSE);
			header.setBinitial(UFBoolean.FALSE);
			header.setDmakedate((UFDate) null);
			header.setVdef17(null);
			header.setVdef18(null);
		}
	}
}