package nc.impl.pu.m25.action;

import nc.bs.ic.base.InvoiceAfterUnApproveEvent;
import nc.bs.pu.m25.plugin.InvoicePluginPoint;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.impl.pu.m25.action.rule.unapprove.CancelSendAPDriveChkRule;
import nc.impl.pu.m25.action.rule.unapprove.SettledCheckRule;
import nc.impl.pu.m25.action.rule.unapprove.UnApproveAfterEventRuel;
import nc.impl.pu.m25.action.rule.unapprove.UnApproveBeforeEventRuel;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.impl.pubapp.pattern.data.bill.BillUpdate;
import nc.impl.pubapp.pattern.data.bill.tool.BillTransferTool;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pu.m25.rule.approve.UnApproveStatusChkRule;
import nc.vo.pu.pub.enumeration.PuBusiLogActionCode;
import nc.vo.pu.pub.enumeration.PuBusiLogPathCode;
import nc.vo.pu.pub.rule.ApproverPermissionRule;
import nc.vo.pu.pub.rule.busilog.WriteOperateLogRule;
import nc.vo.pu.pub.util.AggVOUtil;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.scmpub.res.billtype.POBillType;

public class InvoiceUnApproveAction {
	public InvoiceVO[] unapprove(InvoiceVO[] invoiceVOs, AbstractCompiler2 script) {
		BillTransferTool<InvoiceVO> tool = new BillTransferTool(invoiceVOs);
		InvoiceVO[] vos = (InvoiceVO[]) tool.getClientFullInfoBill();
		InvoiceVO[] orgVos = (InvoiceVO[]) tool.getOriginBills();
		CompareAroundProcesser<InvoiceVO> prcr = new CompareAroundProcesser(InvoicePluginPoint.UNAPPROVE);
		this.addRule(prcr,orgVos);
		prcr.before(vos, orgVos);
		InvoiceVO[] arr$;
		if (null != script) {
			try {
				script.procUnApproveFlow(script.getPfParameterVO());
				InvoiceVO[] unappovedVos = (InvoiceVO[]) ((InvoiceVO[]) script.getPfParameterVO().m_preValueVos);
				arr$ = unappovedVos;
				int len$ = unappovedVos.length;

				for (int i$ = 0; i$ < len$; ++i$) {
					InvoiceVO vo = arr$[i$];
					vo.getParentVO().setStatus(1);
				}

				vos = (InvoiceVO[]) (new BillUpdate()).update(unappovedVos, orgVos);
			} catch (Exception var12) {
				ExceptionUtils.wrappException(var12);
			}
		}

		prcr.after(vos, orgVos);
		String[] ids = AggVOUtil.getPrimaryKeys(vos);
		arr$ = (InvoiceVO[]) (new BillQuery(InvoiceVO.class)).query(ids);
		return (InvoiceVO[]) tool.getBillForToClient(arr$);
	}

	private void addRule(CompareAroundProcesser<InvoiceVO> prcr,InvoiceVO[] orgVos) {
		prcr.addBeforeFinalRule(new UnApproveStatusChkRule());
		prcr.addBeforeFinalRule(new SettledCheckRule());
		prcr.addBeforeRule(new ApproverPermissionRule(POBillType.Invoice.getCode()));
		prcr.addBeforeFinalRule(new CancelSendAPDriveChkRule());
		prcr.addBeforeFinalRule(new UnApproveBeforeEventRuel());
		prcr.addAfterFinalRule(new UnApproveAfterEventRuel());
		prcr.addAfterFinalRule(new WriteOperateLogRule(PuBusiLogPathCode.invoiceApprovePath.getCode(),
				PuBusiLogActionCode.unapprove.getCode()));
		prcr.addAfterFinalRule(new InvoiceAfterUnApproveEvent(orgVos));
	}
}