/** set error flag */
 public void doSetErrorFlag(String poNo) {
   NpcSalesDetailInfo sofordb = ascPOConvertTOSoDao.getSODetialsByPo(poNo);
   // 如果是之前跑过的话就不用再设Erro状态了,因为在这之前已经设置过了ReWrongError了
   // 忘记之前为什么没加也能置成X了
   if (null != sofordb
       && !SysConstants.NPC_INTERFACE_STATUS_REWRONG_ERROR.equals(sofordb.getConvertFlag())) {
     sofordb.setConvertFlag(SysConstants.SALE_CONVERT_FLGA_ERROR);
     sofordb.setUpdateBy(-1L);
     sofordb.setUpdateDate(CommonUtil.getCurrentDateWithTimeZone(this.timezone));
     ascPOConvertTOSoDao.updateSODetails(sofordb);
   }
 }
  /**
   * @param so
   * @throws Exception
   */
  private void generatePartRequest(NpcSalesDetailInfo so) throws Exception {

    NpcPartInfo parts = npcPartInfoService.findByCode(so.getPartCode());
    // 插入TD_NPC_PARTS_REQUEST表
    NpcPartsRequest npcPartsRequest = new NpcPartsRequest();
    npcPartsRequest.setFormID(so.getSaleNo());
    npcPartsRequest.setSkuType(parts.getSkuType());
    npcPartsRequest.setReqNum(so.getPartNum());
    if (SysConstants.SALES_DETAIL_STATUS_PENDDING.equals(so.getPartStatus())) {
      npcPartsRequest.setReqStat(SysConstants.REQ_STAT_PENDING); // Pending
    } else {
      npcPartsRequest.setReqStat(SysConstants.REQ_STAT_WAITTING_FOR_ALLOCATE); // 零件申请状态
    }

    npcPartsRequest.setPartsCode(so.getPartCode());
    npcPartsRequest.setRequestPartCode(so.getPartCode());

    // npcPartsRequest.setPerCost(new Double(parts.getNpcBasePrice()));//取零件的BasePrice
    // wubin at 20120626. 取Supplier Price
    npcPartsRequest.setPerCost(new Double(parts.getSupplierPrice()));
    npcPartsRequest.setReqType(getReqType(so.getAscPoType().trim()));
    npcPartsRequest.setReqDate(CommonUtil.getCurrentDateWithTimeZone(this.timezone));
    npcPartsRequest.setRequestBy(-1L);
    npcPartsRequest.setCreateBy(-1L);
    npcPartsRequest.setCreateDate(CommonUtil.getCurrentDateWithTimeZone(this.timezone));
    npcPartsRequest.setAscCnsgCode(so.getCustomerCnsgCode());
    npcPartsRequest.setAscUnitCode(so.getCustomerUnitCode());
    npcPartsRequest.setAllocateStatus(
        SysConstants.REQ_THREAD_STATUS_INIT); // 设置PartsRequest线程状态初始状态
    npcPartsRequest.setOrganizationCode(SysConstants.NPC_ORGANIZATION_CODE);
    npcPartsRequest.setAscPoNo(so.getAscPoNo()); // asc po No
    npcPartsRequest.setRemark(so.getRemark());
    // 更新
    ascPOConvertTOSoDao.addNpcPartsRequest(npcPartsRequest);
    so.setRequestId(npcPartsRequest.getReqID()); // 设置 RequestId
  }
  /** create so */
  public NpcSalesDetailInfo createSO(AscPo po, OrganizationInfo orgInfo) throws Exception {
    NpcSalesDetailInfo so = ascPOConvertTOSoDao.getSODetialsByPo(po.getPoNo());
    if (so == null) { // 第一次发的Po
      // 设置大单信息
      NpcSalesInfo npcSalesInfo = new NpcSalesInfo();
      npcSalesInfo.setSaleNo(npcFormNumberBuilderService.getNpcSONumber(getCurrentCountryDate()));
      ascPOConvertTOSoDao.addSOMain(npcSalesInfo);
      // 设置小单信息

      so = new NpcSalesDetailInfo();
      so.setPartStatus(SysConstants.SALES_DETAIL_STATUS_INITIAL); // 临时状态
      so.setSaleNo(npcSalesInfo.getSaleNo());
      so.setPartCode(po.getPartCode());
      so.setAppPartCode(po.getPartCode());
      so.setPartNum(po.getOrderNum());
      // so.setUnitPrice(po.getPerCost());
      so.setRemark(po.getRemark());
      so.setCreateBy(-1L);
      so.setCreateDate(CommonUtil.getCurrentDateWithTimeZone(this.timezone));
      so.setAscPoNo(po.getPoNo());
      // so.setCustomerOrgCode(po.getOrgCode()+"");//此字段不与数据关联
      so.setScOrgCode(orgInfo.getId());

      String unitCode = orgInfo.getUnitCode();
      if (unitCode == null || "".equals(unitCode)) {
        logger.error(
            "Convert So :Organization unitCode is null or empty country["
                + getCountryCode()
                + "]poNo["
                + po.getPoNo()
                + "]orgCode["
                + orgInfo.getId()
                + "]");
        System.err.println(
            "Convert So :Organization unitCode is null or empty country["
                + getCountryCode()
                + "]poNo["
                + po.getPoNo()
                + "]orgCode["
                + orgInfo.getId()
                + "]");
        return null;
        //			throw new ApplicationRuntimeException("Convert So :Organization unitCode is null or
        // empty poNo["+po.getPoNo()+"]orgCode["+orgInfo.getId()+"]");
      }
      so.setCustomerUnitCode(unitCode); // 只存unitCode不存OrgCdoe So pojo类里的orgCode 不与数据库关连
      so.setCustomerCnsgCode(orgInfo.getShipTo());
      so.setAscPoType(po.getPoType());
      so.setFormCreateDate(CommonUtil.getCurrentDateWithTimeZone(this.timezone));
      so.setShipType(orgInfo.getShipType()); // Asc的ShipType
      so.setIsBackToBack(SysConstants.IS_BACK_TO_BACK_N);
      so.setConsignFlag(SysConstants.CONSIGN_FLAG_Y);
      so.setVersion(0);
      so.setIsPre(SysConstants.SALES_NOTPRE); // 销售  (销售/预销售)
      so.setDiscount(SysConstants.DISCOUNT_ZERO); // 不打折
      so.setDiscountAmt(0D);

      // Stock transfer : If it is ‘SSC TO’, then set ‘Source’ value for ’AT’,else value for
      // ’AP’.(add by huayi)
      if (orgInfo != null) {
        if (SysConstants.DEFAULT_SSC_LEVEL.equals(orgInfo.getAscLevel())) {
          so.setSource(NPCSaleSource.AT.name());
        } else {
          so.setSource(NPCSaleSource.AP.name());
        }
      }

      // for testing.
      if (StringUtils.isBlank(so.getSource())) {
        // so.setSource( NPCSaleSource.M.name() );
        so.setRemark("converso:so.Source is null.");
      }

      // 计算税
      Double sellingPrice =
          markupPrivceQueryService.getCurrentSellingPrice(
              so.getPartCode(),
              so.getCustomerUnitCode() + "",
              SysConstants.CUSTOMER_GRP_ASC.toString()); // 计算价格
      if (sellingPrice == null) {
        logger.error(
            "CalculationsSellingPrice error sellingPrice is null:partCode["
                + so.getPartCode()
                + "]CustomerUnitCode"
                + so.getCustomerUnitCode()
                + "]poNo["
                + po.getPoNo()
                + "]");
        throw new CalculationsSellingPriceRuntimeException(
            "CalculationsSellingPrice error sellingPrice is null:partCode["
                + so.getPartCode()
                + "]CustomerUnitCode"
                + so.getCustomerUnitCode()
                + "]poNo["
                + po.getPoNo()
                + "]");
      }
      if (sellingPrice.doubleValue() == 0) {
        logger.error(
            "CalculationsSellingPrice error sellingPrice is zero:partCode["
                + so.getPartCode()
                + "]CustomerUnitCode"
                + so.getCustomerUnitCode()
                + "]poNo["
                + po.getPoNo()
                + "]");
        throw new CalculationsSellingPriceRuntimeException(
            "CalculationsSellingPrice error sellingPrice is zero:partCode["
                + so.getPartCode()
                + "]CustomerUnitCode"
                + so.getCustomerUnitCode()
                + "]poNo["
                + po.getPoNo()
                + "]");
      }
      Double purchaseAmt =
          MathUtils.multiply(sellingPrice, so.getPartNum().doubleValue()); // TODO用庆哥的方法算
      so.setUnitPrice(sellingPrice);
      so.setTotalPrice(purchaseAmt);
      ArrayList<NpcPartTaxBean> partTaxList = null;
      partTaxList =
          npcPartTaxService.getPartTaxListBean(
              purchaseAmt,
              so.getPartCode(),
              SysConstants.NPC_ORGANIZATION_CODE,
              SysConstants.CUSTOMER_GRP_ASC);
      so.setConvertFlag(SysConstants.SALE_CONVERT_FLGA_IN); // 销售状态为正在处理
      ascPOConvertTOSoDao.addSO(so); // 先保存So之后才能取到SO的Id

      for (NpcPartTaxBean npcPartTaxBean : partTaxList) {
        NpcPartSalesTaxDetail salesTax = new NpcPartSalesTaxDetail();
        salesTax.setCreateBy(-1L);
        salesTax.setCreatedDate(CommonUtil.getCurrentDateWithTimeZone(this.timezone));
        salesTax.setDeletFlag(0);
        salesTax.setSoId(so.getSoId());
        salesTax.setSaleNo(so.getSaleNo());
        salesTax.setTaxAmt(
            RoundingUtil.totalTaxAmtRounding(npcPartTaxBean.getTaxAmt(), super.getCountryCode()));
        salesTax.setTaxCode(npcPartTaxBean.getTaxCode());
        salesTax.setTaxRate(npcPartTaxBean.getTaxRate());
        salesTax.setConvertedTaxRate(npcPartTaxBean.getActualTaxRate());
        ascPOConvertTOSoDao.saveSalesTaxDetail(salesTax); // 插入税表
      }

    } else if (SysConstants.SALE_CONVERT_FLGA_ERROR.equals(so.getConvertFlag())) { // 上次处理Po时发生错误
      // so.setCustomerOrgCode(po.getOrgCode()+"");//此字段不与数据关联所以要重新set一下
      // 加上Update时间
      so.setUpdateDate(CommonUtil.getCurrentDateWithTimeZone(this.timezone));
      so.setUpdateBy(-1L);
      so.setConvertFlag(SysConstants.NPC_INTERFACE_STATUS_REWRONG_ERROR); // 如果是上次处理失败过的这里设置成Rewrond
      ascPOConvertTOSoDao.updateSODetails(so);
      return so;
    } else if (SysConstants.SALE_CONVERT_FLGA_IN.equals(so.getConvertFlag())
        || SysConstants.SALE_CONVERT_FLGA_SUCCESS.equals(so.getConvertFlag())) { // 重复的PO不做处理
      logger.info("Po coverted :" + so.getAscPoNo());
      return null;
    } else if (SysConstants.NPC_INTERFACE_STATUS_REWRONG_ERROR.equals(
        so.getConvertFlag())) { // 如果是Rewrong还出错的话就不做Convert
      logger.info("rewrong error po :" + so.getAscPoNo());
      return null;
    }

    return so;
  }