/** asc po convert to so */
  public void doConvertsSO(NpcSalesDetailInfo soin) throws Exception {
    ControlPartStatusBean cpsb = null;
    NpcSalesDetailInfo so = ascPOConvertTOSoDao.getSODetialsBySoNo(soin.getSaleNo());

    Double purchaseAmt = soin.getTotalPrice();

    // If SSC order parts from NPC, System doesn’t check ‘purchase limit ‘.(add by huayi)
    boolean isASC = true;

    OrganizationInfo org = ascPOConvertTOSoDao.getOrgInfo(soin.getScOrgCode());

    if (SysConstants.DEFAULT_SSC_LEVEL.equals(org.getAscLevel())) {
      isASC = false;
    }
    cpsb =
        npcSaleService.checkHoldLogic(
            isASC, purchaseAmt, so.getCustomerUnitCode(), so.getPartCode(), so.getPartNum());

    // original code
    /*cpsb=npcSaleService.checkHoldLogic(true,purchaseAmt, so.getCustomerUnitCode(), so.getPartCode(), so.getPartNum());*/

    if (cpsb.isHold()) { // 如果是Control Part、ControlParts、Not Enough Inventory的话 Hold住So
      so.setReasonCode(cpsb.getPendingReason());
      if (so.getReasonCode() != null
          && !so.getReasonCode()
              .contains(
                  SysConstants
                      .HOLD_REASON_NOT_ENOUGH)) { // 'N'为Not Enough Inventory ,Not Enough Inventory
                                                  // hole住了不用发送
        // 发送RPOC
        rpocProcessService.saveSOPendingRPOC(so, this.timezone);
      }
      so.setPartStatus(SysConstants.SALES_DETAIL_STATUS_PENDDING); // Hold住SO
    } else {
      this.reSetNpcPurchaseLimit(so.getCustomerUnitCode(), purchaseAmt); // 更新NpcPurchaseLimit
      so.setPartStatus(SysConstants.SALES_DETAIL_STATUS_WAITTING_PARTS);
    }
    generatePartRequest(so); // Hold住So也生成PartRequest算价钱算税但Hold不更新NpcPurchaseLimit
    so.setConvertFlag(SysConstants.SALE_CONVERT_FLGA_SUCCESS);

    ascPOConvertTOSoDao.updateSODetails(so);
  }
  /** 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;
  }