//qrY9dgujkUtRZAX6SgnJEAwjhpvihM+aNW3nVB+meHeCw1mul22nR+QaDleKzS6H
package nc.bs.ic.m45.insert;

import nc.bs.ic.general.insert.IInsertBP;
import nc.bs.ic.general.insert.InsertBPTemplate;
import nc.bs.ic.general.insert.rule.before.CheckCliabilityValue;
import nc.bs.ic.general.rule.after.AtpAfterUpdate;
import nc.bs.ic.general.rule.before.CtplcustomeridCheck;
import nc.bs.ic.m45.base.BPPlugInPoint;
import nc.bs.ic.m45.base.rule.FillCostOrgRule;
import nc.bs.ic.m45.base.rule.PurchaseBillCheckRule;
import nc.bs.ic.m45.base.rule.PurchaseInAssetWarehouseCheck;
import nc.bs.ic.m45.base.rule.PurchaseInVOScaleCheckRule;
import nc.bs.ic.m45.base.rule.PurchaseinRetMarginProcess;
import nc.bs.ic.m45.base.rule.PurchaseinRetMarginProcessFor5805;
import nc.bs.ic.m45.base.rule.ReturnSnIsExistInEquipcardCheck;
import nc.bs.ic.m45.base.rule.VmiCheckRule;
import nc.bs.ic.m45.insert.rule.FillInVqtunitRule;
import nc.bs.ic.m45.insert.rule.InsReWritePIM;
import nc.bs.ic.m45.insert.rule.InsertCheckCanInRule;
import nc.bs.ic.m45.insert.rule.InsertCheckMaterialUnit;
import nc.bs.ic.m45.insert.rule.InsertRewriteITRule;
import nc.bs.ic.m45.insert.rule.InsertRewritePORule;
import nc.bs.ic.m45.wgpt.rule.WuYeWgptCheckInfoRule;
import nc.bs.ic.pub.base.ICAroundProcesser;
import nc.bs.ic.pub.base.IInsertRuleProvider;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.vo.ic.general.define.MetaNameConst;
import nc.vo.ic.m45.entity.PurchaseInBodyVO;
import nc.vo.ic.m45.entity.PurchaseInVO;
import nc.vo.ic.pub.util.StringUtil;
import nccloud.bs.ic.mobile.inbound.operation.rewrite.MobAfterPurchaseInSave;

/**
 * 采购入库单后台新增BP
 *   
 * @author songhy
 */
public class InsertBP implements IInsertBP<PurchaseInVO>,
    IInsertRuleProvider<PurchaseInVO> {

  @Override
  public void addAfterRule(PurchaseInVO[] vos,
      AroundProcesser<PurchaseInVO> processor) {

    /**
     * 放在现存量更新规则（OnhandAfterUpdate）之后，因为现存量更新时会调用预留接口，预留接口会回写采购订单，
     * 如果入库数量=订单数量，订单会自动入库关闭，入库关闭会解除预留，导致后续操作错误
     */
    ((ICAroundProcesser<PurchaseInVO>) processor).addAfterRuleAt(
        new InsertRewritePORule(), AtpAfterUpdate.class);
    processor.addAfterRule(new InsReWritePIM());
    processor.addAfterRule(new InsertRewriteITRule());
    processor.addAfterRule(new MobAfterPurchaseInSave());
    // processor.addAfterRule(new InsertRewritePORule());
  }

  @Override
  public void addBeforeRule(PurchaseInVO[] vos,
      AroundProcesser<PurchaseInVO> processor) {
    // 检查是否可以生成入库单
    processor.addBeforeRule(new InsertCheckCanInRule());
    processor.addBeforeRule(new FillCostOrgRule());
    processor.addBeforeRule(new PurchaseBillCheckRule());
    processor.addBeforeRule(new PurchaseInAssetWarehouseCheck());
    // 当数量（主数量，数量，报价数量）与采购订单一致时，使采购入库单表体价格和单价与采购订单一致
    //下面的规则处理会使入库单的改价不生效 hujieh
//    processor.addBeforeRule(new AdjustPriceAndMnyWhenSameNumRule());
    processor.addBeforeRule(new CtplcustomeridCheck<PurchaseInVO>());
    processor.addBeforeRule(new VmiCheckRule());
    processor.addBeforeRule(new ReturnSnIsExistInEquipcardCheck());
    // 红蓝字入库单倒挤，跟订单倒挤
    processor.addBeforeRule(new PurchaseinRetMarginProcess());
    processor.addBeforeRule(new PurchaseinRetMarginProcessFor5805());
    processor.addBeforeRule(new PurchaseInVOScaleCheckRule());
    processor.addBeforeRule(new InsertCheckMaterialUnit());
 // 利润中心校验规则
    processor.addBeforeRule(new CheckCliabilityValue<PurchaseInVO>(MetaNameConst.CLIABILITYOID,MetaNameConst.CIOLIABILITYOID));
    // 来源于进口模块--流向单，填充报价单位、报价换算率
    processor.addBeforeRule(new FillInVqtunitRule());
   //add by 周蓝萍 保存时验证入库信息是否与网购平台一致 2020-10-21 begin
  	processor.addBeforeRule(new WuYeWgptCheckInfoRule());
  //add by 周蓝萍 保存时验证入库信息是否与网购平台一致 2020-10-21 end
  }

  @Override
  public PurchaseInVO[] insert(PurchaseInVO[] bills) {
    // 应该在“物理自由辅助属性检查”规则执行前补充供应商
    this.fillCvendorid(bills);

    InsertBPTemplate<PurchaseInVO> insertBP =
        new InsertBPTemplate<PurchaseInVO>(BPPlugInPoint.InsertBP, this);
    return insertBP.insert(bills);
  }

  /**
   * 补表体的供应商，供应商已经挪到表头，表体的供应商要跟表头供应商一样
   * 
   * @param vos
   */
  private void fillCvendorid(PurchaseInVO[] vos) {
    String cvendor = null;
    for (PurchaseInVO vo : vos) {

      // 取表头供应商
      cvendor = vo.getHead().getCvendorid();
      if (StringUtil.isSEmptyOrNull(cvendor)) {
        continue;
      }

      // 为表体供应商赋值
      for (PurchaseInBodyVO body : vo.getBodys()) {
        body.setCvendorid(cvendor);
      }
    }
  }

}

//qrY9dgujkUtRZAX6SgnJEAwjhpvihM+aNW3nVB+meHeCw1mul22nR+QaDleKzS6H