package nc.impl.pu.m25.action;

import nc.bs.ic.base.InvoiceAfterApproveEvent;
import nc.bs.pu.m25.plugin.InvoicePluginPoint;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.impl.pu.m25.action.rule.approve.ApproveAfterEventRuel;
import nc.impl.pu.m25.action.rule.approve.ApproveBeforeEventRuel;
import nc.impl.pu.m25.action.rule.approve.ApproveTogetherFeeProcRule;
import nc.impl.pu.m25.action.rule.approve.SupplierInvoiceFrozenChkRule;
import nc.impl.pu.m25.action.rule.approve.UpdateApproveInfoRule;
import nc.impl.pu.m25.action.rule.approve.VOFilterFor55E6Rule;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.impl.pubapp.pattern.data.bill.tool.BillTransferTool;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.itf.scmpub.reference.uap.pf.PfScriptClassAdaptor;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pu.m25.env.InvoiceUIToBSEnv;
import nc.vo.pu.m25.rule.InvoiceAutoSettleRule;
import nc.vo.pu.m25.rule.approve.ApproveStatusChkRule;
import nc.vo.pu.pub.enumeration.PuBusiLogActionCode;
import nc.vo.pu.pub.enumeration.PuBusiLogPathCode;
import nc.vo.pu.pub.rule.busilog.WriteOperateLogRule;
import nc.vo.pu.pub.rule.pf.ApprovedVOFilterRule;
import nc.vo.pu.pub.rule.pf.UpdatePflowVORule;
import nc.vo.pu.pub.util.AggVOUtil;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class InvoiceApproveAction {
	public InvoiceVO[] approve(InvoiceVO[] invoiceVOs, AbstractCompiler2 script, InvoiceUIToBSEnv[] envs) {
		BillTransferTool<InvoiceVO> tool = new BillTransferTool(invoiceVOs);
		InvoiceVO[] vos = (InvoiceVO[]) tool.getClientFullInfoBill();
		InvoiceVO[] orgVos = (InvoiceVO[]) tool.getOriginBills();
		CompareAroundProcesser<InvoiceVO> prcr = new CompareAroundProcesser(InvoicePluginPoint.APPROVE);
		this.addRule(prcr, envs, null != script ? script.getPfParameterVO() : null);
		prcr.before(vos, vos);
		if (null != script) {
			try {
				script.procFlowBacth(script.getPfParameterVO());
			} catch (Exception var11) {
				ExceptionUtils.wrappException(var11);
			}

			prcr.after((InvoiceVO[]) ((InvoiceVO[]) script.getPfParameterVO().m_preValueVos), orgVos);
			String[] ids = AggVOUtil.getPrimaryKeys(vos);
			InvoiceVO[] approvedVos = (InvoiceVO[]) (new BillQuery(InvoiceVO.class)).query(ids);
			PfScriptClassAdaptor pfadptor = new PfScriptClassAdaptor(script);
			pfadptor.setVo(null != approvedVos && approvedVos.length > 0 ? approvedVos[0] : null);
			pfadptor.setVos(approvedVos);
			return (InvoiceVO[]) tool.getBillForToClient(approvedVos);
		} else {
			return null;
		}
	}

	private void addRule(CompareAroundProcesser<InvoiceVO> prcr, InvoiceUIToBSEnv[] envs, PfParameterVO pfParameterVO) {
		prcr.addBeforeFinalRule(new ApproveStatusChkRule());
		prcr.addBeforeFinalRule(new SupplierInvoiceFrozenChkRule());
		prcr.addBeforeFinalRule(new ApproveBeforeEventRuel());
		prcr.addAfterFinalRule(new WriteOperateLogRule(PuBusiLogPathCode.invoiceApprovePath.getCode(),
				PuBusiLogActionCode.approve.getCode()));
		prcr.addAfterFinalRule(new UpdateApproveInfoRule());
		prcr.addAfterFinalRule(new ApprovedVOFilterRule());
		prcr.addAfterFinalRule(new ApproveAfterEventRuel());
		prcr.addAfterFinalRule(new VOFilterFor55E6Rule());
		prcr.addAfterRule(new ApproveTogetherFeeProcRule(envs));
		prcr.addAfterFinalRule(new InvoiceAutoSettleRule());
		prcr.addAfterFinalRule(new UpdatePflowVORule(pfParameterVO));
		prcr.addAfterFinalRule(new InvoiceAfterApproveEvent());	
	}
}