diff --git a/so/src/private/nccloud/api/impl/so/m30/APISaleOrderMaitainImpl.java b/so/src/private/nccloud/api/impl/so/m30/APISaleOrderMaitainImpl.java index e76f956..63a2099 100644 --- a/so/src/private/nccloud/api/impl/so/m30/APISaleOrderMaitainImpl.java +++ b/so/src/private/nccloud/api/impl/so/m30/APISaleOrderMaitainImpl.java @@ -43,6 +43,13 @@ import nccloud.openapi.scmpub.pub.TransferCodeToPKTool; import nccloud.openapi.scmpub.pub.TransferMapToVOTool; import nccloud.putitf.riart.billtype.IBilltypeService; import org.apache.commons.lang3.ArrayUtils; +import nc.itf.uap.pf.IPFBusiAction; +import nc.vo.ml.NCLangRes4VoTransl; +import nc.vo.pub.lang.UFDate; +import nc.vo.so.pub.SOConstant; +import nc.vo.so.pub.util.SOCurrencyUtil; +import nc.vo.so.pub.keyvalue.IKeyValue; +import nc.vo.so.pub.keyvalue.VOKeyValue; import java.math.BigDecimal; import java.util.*; @@ -55,832 +62,1001 @@ import java.util.*; */ public class APISaleOrderMaitainImpl implements IAPISaleOrderMaitain { - private static String BODYTABLE = "so_saleorder_b"; - private static String HEADTABLE = "so_saleorder"; - - @Override - public SaleOrderVO[] save(List> paramList) throws BusinessException { - try { - List aggVOList = TransferMapToVOTool.transferMapToAggVO(paramList, SaleOrderVO.class); - - SaleOrderVO[] vos = (SaleOrderVO[]) aggVOList.toArray(new SaleOrderVO[aggVOList.size()]); - BillVOsCheckRule checker = new BillVOsCheckRule(new SaleOrderValidator()); - checker.check(vos); - - aggVOList = TransferCodeToPKTool.transferAggVO(aggVOList); - - - SaleOrderSaveUtil saveUtil = new SaleOrderSaveUtil(); - return saveUtil.processBill(vos); - } catch (Exception e) { - ExceptionUtils.marsh(e); - return null; - } - } - - @Override - public SaleOrderVO[] update(List> paramList) throws BusinessException { - List aggVOList = TransferMapToVOTool.transferMapToAggVO(paramList, SaleOrderVO.class); - - aggVOList = TransferCodeToPKTool.transferAggVO(aggVOList); - - SaleOrderVO[] vos = (SaleOrderVO[]) aggVOList.toArray(new SaleOrderVO[aggVOList.size()]); - - Map> ids = getIds(vos); - if (ids.keySet() == null || ids.values() == null || ids.values().size() == 0) { - ExceptionUtils.wrappBusinessException("请传入订单主键和订单行主键"); - } - String[] hids = (String[]) ids.keySet().toArray(new String[ids.keySet().size()]); - - ISaleOrderMaintain service = (ISaleOrderMaintain) NCLocator.getInstance().lookup(ISaleOrderMaintain.class); - SaleOrderVO[] originVos = service.querySaleorder(hids); - if (null == originVos || originVos.length == 0) { - ExceptionUtils.wrappBusinessException("根据传入数据未匹配到相关数据"); - } - - for (Map objectMap : paramList) { - Map headdata = (Map) objectMap.get(HEADTABLE); - List bodyArr = (List) objectMap.get(BODYTABLE); - for (Object body : bodyArr) { - Map bodydata = (Map) body; - if (!(bodydata.getOrDefault("vbdef11", "") + "").isEmpty()) { - String vbdef11 = bodydata.get("vbdef11") + ""; - String dr = bodydata.getOrDefault("dr", "0") + ""; - String csaleorderid = bodydata.get("csaleorderid") + ""; - String countSql = "SELECT count(1) FROM so_saleorder_b" + - " WHERE nvl(dr,0) = 0 and csaleorderid = '[csaleorderid]' and vbdef11 = '[vbdef11]' "; - countSql = countSql.replace("[csaleorderid]", csaleorderid); - countSql = countSql.replace("[vbdef11]", vbdef11); - Integer num = (Integer) new BaseDAO().executeQuery(countSql, new ColumnProcessor()); - if ("0".equals(dr) && num <= 0) { - bodydata.put("status", "add"); - } - } - } - } - - // SetUpdateData setData = new SetUpdateData(); - // setData.setData(vos, originVos); - // SaleOrderVO[] combinBillVOs = (SaleOrderVO[]) AggVOUtil.combinBillVO(vos, originVos); - // 创建一个新的数组,并进行深拷贝 - SaleOrderVO[] combinBillVOs = new SaleOrderVO[originVos.length]; - for (int i = 0; i < originVos.length; i++) { - combinBillVOs[i] = (SaleOrderVO) originVos[i].clone(); - } - // 将 vos 中的元素按主键存储在 Map 中 - Map vosMap = new HashMap<>(); - for (SaleOrderVO vo : vos) { - vosMap.put(vo.getParentVO().getCsaleorderid(), vo); - } - - // 遍历 combinBillVOs 并更新字段 - for (SaleOrderVO vo : combinBillVOs) { - SaleOrderHVO hvo = vo.getParentVO(); - String primaryKey = hvo.getCsaleorderid(); - SaleOrderVO bipVo = vosMap.get(primaryKey); - // 设置单据状态 - hvo.setStatus(VOStatus.UPDATED); - - Map bvoMap = new HashMap<>(); - for (SaleOrderBVO saleOrderBVO : bipVo.getChildrenVO()) { - bvoMap.put(saleOrderBVO.getCsaleorderbid(), saleOrderBVO); - } - - // 比较combinBillVOs中的BVO和vos中的BVO - for (SaleOrderBVO bvo : vo.getChildrenVO()) { - bvo.setStatus(VOStatus.UPDATED); - // 设置删除的物料行的状态,在bip传的数据中找不到 - String csaleorderbid = bvo.getCsaleorderbid(); - if (bvoMap.get(csaleorderbid) == null) { - bvo.setStatus(VOStatus.DELETED); - NCCForUAPLogger.debug("findDeletedBids:" + VOStatus.DELETED + ",csaleorderbid = " + csaleorderbid); - } - } - // 更新 combinBillVO 中的字段为 vos 中的值 - updateFields(vo, bipVo); - // 设置新增的物料行 - if (paramList != null && !paramList.isEmpty()) { - addBvo(combinBillVOs, paramList); - } - } - // 联动计算 - INumPriceMnyCalculator cal = new SaleOrderNPriceMnyCal(combinBillVOs); - cal.calculate(); - for (SaleOrderVO vo : combinBillVOs) { - SaleOrderHVO hvo = vo.getParentVO(); - SaleOrderHVO originHvo = null; - for (SaleOrderVO originVo : originVos) { - if (hvo.getCsaleorderid().equals(originVo.getParentVO().getCsaleorderid())) { - originHvo = originVo.getParentVO(); - } - } - String new_ctrantypeid = hvo.getCtrantypeid(); - boolean isChangedTranType = false;// 订单类型不修改 - if (originHvo != null && new_ctrantypeid != null && !new_ctrantypeid.equals(originHvo.getCtrantypeid())) { - isChangedTranType = true; - } - SaleOrderBVO[] bvos = vo.getChildrenVO(); - // 判断是否存在新增的子表 - boolean hasNewStatus = Arrays.stream(bvos) - .anyMatch(bvo -> bvo.getStatus() == VOStatus.NEW); - // 新增子表或改订单类型的情况下不校验是否存在下游 - if (!hasNewStatus && !isChangedTranType) { - String countSql = "SELECT count(1) FROM so_saleinvoice_b a" + - " LEFT JOIN so_saleinvoice b ON a.csaleinvoiceid = b.csaleinvoiceid" + - " WHERE b.fopposeflag = 0 AND nvl(b.dr, 0) = 0 and csrcid = '[csrcid]' "; - countSql = countSql.replace("[csrcid]", hvo.getCsaleorderid()); - Integer num = (Integer) new BaseDAO().executeQuery(countSql, new ColumnProcessor()); - if (num > 0) { - // fopposeflag 对冲标记 0=正常 1=已被对冲 2=对冲生成 - ExceptionUtils.wrappBusinessException("下游存在未红冲完成的销售发票"); - return null; - } - } - } - - fillcustomervidbyoid(combinBillVOs); - - // 保存 - ISaleOrderScriptMaintain maintainsrv = - NCLocator.getInstance().lookup(ISaleOrderScriptMaintain.class); - SaleOrderVO[] retvos = - maintainsrv.saleOrderUpdate(combinBillVOs, null, originVos); - return retvos; - } - - - private Map> getIds(SaleOrderVO[] vos) { - Map> ids = new HashMap>(); - for (SaleOrderVO vo : vos) { - String hid = vo.getParentVO().getCsaleorderid(); - Set bids = new HashSet(); - for (SaleOrderBVO bvo : vo.getChildrenVO()) { - bids.add(bvo.getCsaleorderbid()); - } - ids.put(hid, bids); - } - return ids; - } - - private void setOtherId(SaleOrderVO[] vos) { - for (SaleOrderVO vo : vos) { - // 部门、业务员、开票客户编码转id - SaleOrderHVO orderHVO = vo.getParentVO(); - String csaleorderid = orderHVO.getCsaleorderid(); - String cdeptvid = orderHVO.getCdeptvid(); - String cemployeeid = orderHVO.getCemployeeid(); - // 开票客户 - String cinvoicecustid = orderHVO.getCinvoicecustid(); - // 客户 - String ccustomerid = orderHVO.getCcustomerid(); - // 订单类型 - // String ctrantypeid = orderHVO.getAttributeValue("ctrantypeid") + ""; - String vtrantypecode = orderHVO.getVtrantypecode(); - NCCForUAPLogger.debug("setOtherId-ccustomerid:" + ccustomerid + ",vtrantypecode:" + vtrantypecode); - try { - String sql = ""; - // 部门 - if (cdeptvid != null && !cdeptvid.isEmpty()) { - sql = " select pk_vid,pk_dept from org_dept where code = '[code]' "; - sql = sql.replace("[code]", cdeptvid); - Map deptMap = (Map) new BaseDAO().executeQuery(sql, new MapProcessor()); - NCCForUAPLogger.debug("APISaleOrderMaitainImpl-setOtherId-deptObj:" + deptMap); - if (deptMap != null && !deptMap.isEmpty()) { - if (!"".equals(deptMap.getOrDefault("pk_dept", "") + "")) { - orderHVO.setCdeptid(deptMap.getOrDefault("pk_dept", "") + ""); - } - if (!"".equals(deptMap.getOrDefault("pk_vid", "") + "")) { - orderHVO.setCdeptvid(deptMap.getOrDefault("pk_vid", "") + ""); - } - } - } - // 业务员 - if (cemployeeid != null && !cemployeeid.isEmpty()) { - sql = " select pk_psndoc from bd_psndoc where code = '[code]' "; - sql = sql.replace("[code]", cemployeeid); - Object staffObj = new BaseDAO().executeQuery(sql, new ColumnProcessor("pk_psndoc")); - NCCForUAPLogger.debug("APISaleOrderMaitainImpl-setOtherId-staffObj:" + staffObj); - if (staffObj != null) { - String id = BFPubTools.getString_TrimAsNull(staffObj); - if (!id.isEmpty()) { - orderHVO.setCemployeeid(id); - } - } - } - // 开票客户 - if (cinvoicecustid != null && !cinvoicecustid.isEmpty()) { - sql = " select pk_customer from bd_customer where nvl(dr,0) = 0 and code = '[code]' "; - sql = sql.replace("[code]", cinvoicecustid); - Object invCustObj = new BaseDAO().executeQuery(sql, new ColumnProcessor("pk_customer")); - NCCForUAPLogger.debug("setOtherId-invCustObj:" + invCustObj); - if (invCustObj != null) { - String id = BFPubTools.getString_TrimAsNull(invCustObj); - if (!id.isEmpty()) { - orderHVO.setCinvoicecustid(id); - } - } - } - // 客户 - if (ccustomerid != null && !ccustomerid.isEmpty()) { - sql = " select pk_customer from bd_customer where nvl(dr,0) = 0 and code = '[code]' "; - sql = sql.replace("[code]", ccustomerid); - Object custObj = new BaseDAO().executeQuery(sql, new ColumnProcessor("pk_customer")); - NCCForUAPLogger.debug("setOtherId-custObj:" + custObj); - if (custObj != null) { - String id = BFPubTools.getString_TrimAsNull(custObj); - if (!id.isEmpty()) { - orderHVO.setCcustomerid(id); - } - } - } - // 交易类型 - if (vtrantypecode != null && !vtrantypecode.isEmpty()) { - IBilltypeService billtypeService = NCLocator.getInstance().lookup(IBilltypeService.class); - BilltypeVO billtype = billtypeService.getBilltype(vtrantypecode); - if (billtype != null && billtype.getPk_billtypeid() != null) { - NCCForUAPLogger.debug("setOtherId-pk_billtypeid:" + billtype.getPk_billtypeid()); - orderHVO.setCtrantypeid(billtype.getPk_billtypeid()); - } - } - } catch (Exception e) { - NCCForUAPLogger.debug("APISaleOrderMaitainImpl-setOtherId-exp:" + e.getMessage()); - throw new RuntimeException(e); - } - } - } - - private void updateFields(SaleOrderVO target, SaleOrderVO source) { - // 更新 SaleOrderHVO 字段 - SaleOrderHVO targetHVO = target.getParentVO(); - SaleOrderHVO sourceHVO = source.getParentVO(); - // 表头修改修改的字段 - String cdeptvid = sourceHVO.getCdeptvid(); - String cdeptid = sourceHVO.getCdeptid(); - if (cdeptvid != null && !cdeptvid.isEmpty()) { - targetHVO.setCdeptvid(cdeptvid); - targetHVO.setCdeptid(cdeptid); - } - - String cemployeeid = sourceHVO.getCemployeeid(); - if (cemployeeid != null && !cemployeeid.isEmpty()) { - targetHVO.setCemployeeid(cemployeeid); - } - String cinvoicecustid = sourceHVO.getCinvoicecustid(); - if (cinvoicecustid != null && !cinvoicecustid.isEmpty()) { - targetHVO.setCinvoicecustid(cinvoicecustid); - } - String ccustomerid = sourceHVO.getCcustomerid(); - if (ccustomerid != null && !ccustomerid.isEmpty()) { - targetHVO.setCcustomerid(ccustomerid); - } - // 订单类型id - String ctrantypeid = sourceHVO.getCtrantypeid(); - if (ctrantypeid != null && !ctrantypeid.isEmpty()) { - targetHVO.setCtrantypeid(ctrantypeid); - } - // 订单类型编码 - String vtrantypecode = sourceHVO.getVtrantypecode(); - if (vtrantypecode != null && !vtrantypecode.isEmpty()) { - targetHVO.setVtrantypecode(vtrantypecode); - } - - // 更新 SaleOrderBVO 字段 - SaleOrderBVO[] targetBVOs = target.getChildrenVO(); - SaleOrderBVO[] sourceBVOs = source.getChildrenVO(); - - // BVO 的主键是 csaleorderbid - Map sourceBVOsMap = new HashMap<>(); - for (SaleOrderBVO bvo : sourceBVOs) { - sourceBVOsMap.put(bvo.getCsaleorderbid(), bvo); - } - - for (SaleOrderBVO targetBVO : targetBVOs) { - String bvoId = targetBVO.getCsaleorderbid(); - SaleOrderBVO sourceBVO = sourceBVOsMap.get(bvoId); - if (sourceBVO != null) { - int status = targetBVO.getStatus(); - if (VOStatus.UPDATED == status) { - // 数量 - UFDouble nnum = sourceBVO.getNnum(); - targetBVO.setNnum(nnum); - targetBVO.setNastnum(nnum); - // targetBVO.setNqtunitnum(nnum); - // 价税合计 - targetBVO.setNorigtaxmny(sourceBVO.getNorigtaxmny()); - targetBVO.setCmaterialvid(sourceBVO.getCmaterialvid()); - // 是否赠品 - targetBVO.setBlargessflag(sourceBVO.getBlargessflag()); - // 折本汇率 - UFDouble nexchangerate = getUFDouble_NullAsOne(sourceBVO.getNexchangerate()); - targetBVO.setNexchangerate(nexchangerate); - // 物料编码 - String cmaterialvid = sourceBVO.getCmaterialvid(); - // 物料关联字段赋值 - if (cmaterialvid != null && !cmaterialvid.isEmpty()) { - setMaterl(targetBVO); - } - // 自定义项赋值 - String vbdef1 = sourceBVO.getVbdef1();// 合同编号 - Map defList = getDefList("zdy-001"); - targetBVO.setVbdef1(defList.getOrDefault(vbdef1, "")); - String vbdef5 = sourceBVO.getVbdef5();// 合同销售订单号 - defList.clear(); - defList = getDefList("BIP-ddh"); - targetBVO.setVbdef5(defList.getOrDefault(vbdef5, "")); - targetBVO.setVbdef10(sourceBVO.getVbdef10());// 项目名称 - targetBVO.setVbdef13(sourceBVO.getVbdef13());// 国网行项目号 - NCCForUAPLogger.debug(String.format( - "cmaterialvid = [%S], vbdef1 = [%S], vbdef5 = [%S], vbdef10 = [%S], vbdef13 = [%S]", - cmaterialvid, targetBVO.getVbdef1(), targetBVO.getVbdef5(), targetBVO.getVbdef10(), targetBVO.getVbdef13())); - } - - } - } - } - - /** - * 获取自定义项信息 - */ - private Map getDefList(String code) { - Map map = new HashMap(); - String strWhere = " pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code='" + code + "' and dr=0 ) and dr = 0"; - try { - DefdocVO[] defdocVOs = (DefdocVO[]) new HYSuperDMO().queryByWhereClause(DefdocVO.class, strWhere); - if (defdocVOs != null && defdocVOs.length > 0) { - for (DefdocVO defdocVO : defdocVOs) { - map.put(defdocVO.getCode().trim(), defdocVO.getPk_defdoc()); - } - } - } catch (DAOException e) { - e.printStackTrace(); - } - return map; - - } - - @Override - public void delete(String[] hids) throws BusinessException { - BillQuery queryTool = new BillQuery(SaleOrderVO.class); - SaleOrderVO[] saleOrderVOs = (SaleOrderVO[]) queryTool.query(hids); - if (saleOrderVOs.length == 0) { - ExceptionUtils.wrappBusinessException("根据传入数据未匹配到相关数据"); - } - - PfServiceScmUtil.processBatch("DELETE", SOBillType.Order.getCode(), saleOrderVOs, null, null); - } - - - private void fillcustomervidbyoid(SaleOrderVO[] combinBillVOs) { - Set customeridSet = new HashSet(); - Map customeridMap = new HashMap(); - for (SaleOrderVO vo : combinBillVOs) { - SaleOrderHVO hvo = vo.getParentVO(); - - if (!StringUtil.isSEmptyOrNull(hvo.getCcustomerid()) && StringUtil.isSEmptyOrNull(hvo.getCcustomervid())) { - customeridSet.add(hvo.getCcustomerid()); - } - - if (!StringUtil.isSEmptyOrNull(hvo.getCinvoicecustid()) && StringUtil.isSEmptyOrNull(hvo.getCinvoicecustvid())) { - customeridSet.add(hvo.getCinvoicecustid()); - } - - if (!StringUtil.isSEmptyOrNull(hvo.getChreceivecustid()) && - StringUtil.isSEmptyOrNull(hvo.getChreceivecustvid())) { - customeridSet.add(hvo.getChreceivecustid()); - } - - SaleOrderBVO[] bvos = vo.getChildrenVO(); - for (SaleOrderBVO bvo : bvos) { - - if (!StringUtil.isSEmptyOrNull(bvo.getCreceivecustid()) && - StringUtil.isSEmptyOrNull(bvo.getCreceivecustvid())) { - customeridSet.add(bvo.getCreceivecustid()); - } - - - if (!StringUtil.isSEmptyOrNull(bvo.getCvendorid()) && StringUtil.isSEmptyOrNull(bvo.getCvendorvid())) { - customeridSet.add(bvo.getCvendorid()); - } - } - } - - if (!customeridSet.isEmpty()) { - customeridMap = CustomerPubService.queryCustomerVidsByOids((String[]) CollectionUtils.setToArray(customeridSet)); - } - - for (SaleOrderVO vo : combinBillVOs) { - SaleOrderHVO hvo = vo.getParentVO(); - - if (!StringUtil.isSEmptyOrNull(hvo.getCcustomerid()) && StringUtil.isSEmptyOrNull(hvo.getCcustomervid())) { - if (null == customeridMap || customeridMap.size() == 0) { - hvo.setCcustomervid(hvo.getCcustomerid()); - } else { - hvo.setCcustomervid((String) customeridMap.get(hvo.getCcustomerid())); - } - } - - if (!StringUtil.isSEmptyOrNull(hvo.getCinvoicecustid()) && StringUtil.isSEmptyOrNull(hvo.getCinvoicecustvid())) { - if (null == customeridMap || customeridMap.size() == 0) { - hvo.setCinvoicecustvid(hvo.getCinvoicecustid()); - } else { - hvo.setCinvoicecustvid((String) customeridMap.get(hvo.getCinvoicecustid())); - } - } - - if (!StringUtil.isSEmptyOrNull(hvo.getChreceivecustid()) && - StringUtil.isSEmptyOrNull(hvo.getChreceivecustvid())) { - if (null == customeridMap || customeridMap.size() == 0) { - hvo.setChreceivecustvid(hvo.getChreceivecustid()); - } else { - hvo.setChreceivecustvid((String) customeridMap.get(hvo.getChreceivecustid())); - } - } - - SaleOrderBVO[] bvos = vo.getChildrenVO(); - for (SaleOrderBVO bvo : bvos) { - - if (!StringUtil.isSEmptyOrNull(bvo.getCreceivecustid()) && - StringUtil.isSEmptyOrNull(bvo.getCreceivecustvid())) { - if (null == customeridMap || customeridMap.size() == 0) { - bvo.setCreceivecustvid(bvo.getCreceivecustid()); - } else { - bvo.setCreceivecustvid((String) customeridMap.get(bvo.getCreceivecustid())); - } - } - - - if (!StringUtil.isSEmptyOrNull(bvo.getCvendorid()) && StringUtil.isSEmptyOrNull(bvo.getCvendorvid())) { - if (null == customeridMap || customeridMap.size() == 0) { - bvo.setCvendorvid(bvo.getCvendorid()); - } else { - bvo.setCvendorvid((String) customeridMap.get(bvo.getCvendorid())); - } - } - } - } - } - - @Override - public SaleOrderVO[] sendApprove(String[] hids) throws BusinessException { - SaleOrderVO[] vos = querySaleOrder(hids); - try { - for (SaleOrderVO aggvo : vos) { - SaleOrderHVO parentVO = aggvo.getParentVO(); - if (!BillStatus.FREE.equalsValue(parentVO.getFstatusflag())) { - ExceptionUtils.wrappBusinessException("当前销售订单:" + parentVO.getVbillcode() + "不是自由状态,不能提交"); - } - } - return (SaleOrderVO[]) PfServiceScmUtil.processBatch("SAVE", SOBillType.Order.getCode(), vos, null, null); - } catch (Exception e) { - ExceptionUtils.marsh(e); - return null; - } - } - - @Override - public SaleOrderVO[] unSendApprove(String[] hids) throws BusinessException { - SaleOrderVO[] vos = querySaleOrder(hids); - try { - for (SaleOrderVO aggvo : vos) { - SaleOrderHVO parentVO = aggvo.getParentVO(); - if (!BillStatus.AUDITING.equalsValue(parentVO.getFstatusflag())) { - ExceptionUtils.wrappBusinessException("当前销售订单:" + parentVO.getVbillcode() + "不是提交状态,不能收回"); - } - } - return (SaleOrderVO[]) PfServiceScmUtil.processBatch("UNSAVE", SOBillType.Order.getCode(), vos, null, null); - } catch (Exception e) { - ExceptionUtils.marsh(e); - return null; - } - } - - @Override - public SaleOrderVO[] approve(String[] hids) throws BusinessException { - SaleOrderVO[] vos = querySaleOrder(hids); - try { - for (SaleOrderVO aggvo : vos) { - SaleOrderHVO parentVO = aggvo.getParentVO(); - if (!BillStatus.FREE.equalsValue(parentVO.getFstatusflag()) && - !BillStatus.AUDIT.equalsValue(parentVO.getFstatusflag()) && - !BillStatus.AUDITING.equalsValue(parentVO.getFstatusflag())) { - ExceptionUtils.wrappBusinessException("当前销售订单:" + parentVO.getVbillcode() + "状态不符合审批条件,不能审批"); - } - } - return (SaleOrderVO[]) PfServiceScmUtil.processBatch("APPROVE", SOBillType.Order.getCode(), vos, null, null); - } catch (Exception e) { - ExceptionUtils.marsh(e); - return null; - } - } - - @Override - public SaleOrderVO[] unApprove(String[] hids) throws BusinessException { - SaleOrderVO[] vos = querySaleOrder(hids); - try { - for (SaleOrderVO aggvo : vos) { - SaleOrderHVO parentVO = aggvo.getParentVO(); - if (!BillStatus.AUDIT.equalsValue(parentVO.getFstatusflag()) && - !BillStatus.AUDITING.equalsValue(parentVO.getFstatusflag())) { - ExceptionUtils.wrappBusinessException("当前销售订单:" + parentVO.getVbillcode() + "状态不符合弃审条件,不能弃审"); - } - } - return (SaleOrderVO[]) PfServiceScmUtil.processBatch("UNAPPROVE", SOBillType.Order.getCode(), vos, null, null); - } catch (Exception e) { - ExceptionUtils.marsh(e); - return null; - } - } - - - private SaleOrderVO[] querySaleOrder(String[] hids) throws BusinessException { - ISaleOrderQueryAPI service = (ISaleOrderQueryAPI) NCLocator.getInstance().lookup(ISaleOrderQueryAPI.class); - SaleOrderVO[] vos = service.queryVOByIDs(hids); - if (ArrayUtils.isEmpty(vos)) { - ExceptionUtils.wrappBusinessException("没有符合条件的数据"); - } else { - return vos; - } - return null; - } - - /** - * 设置物料的相关字段 - */ - private void setMaterl(SaleOrderBVO bVO) { - String cmaterialvid = bVO.getCmaterialvid(); - UFDouble nnum = bVO.getNnum(); - UFDouble ntaxrate = bVO.getNtaxrate(); - try { - String sql = " select a.pk_material,a.pk_source, a.pk_measdoc cunitid,nvl(b.pk_measdoc,a.pk_measdoc) castunitid,nvl(b.measrate,'1/1') measrate " + - "from bd_material a left join bd_materialconvert b on a.pk_material=b.pk_material " + - "where a.code='" + cmaterialvid + "' "; - Map map = (Map) new BaseDAO().executeQuery(sql, new MapProcessor()); - if (map == null) { - throw new BusinessException(cmaterialvid + "物料未查到"); - } - NCCForUAPLogger.debug(String.format( - "setMaterl: cqtunitid = [%S], cunitid = [%S], castunitid = [%S]", - map.get("cqtunitid"), map.get("cunitid"), map.get("castunitid"))); - bVO.setAttributeValue("castunitid", map.get("castunitid")); - bVO.setAttributeValue("vchangerate", map.get("vchangerate")); - - // 重新计算金额 - bVO.setAttributeValue("cqtunitid", map.get("cunitid")); - bVO.setAttributeValue("cunitid", map.get("cunitid")); - - bVO.setAttributeValue("cmaterialid", map.get("pk_material")); - bVO.setAttributeValue("cmaterialvid", map.get("pk_material")); - - UFDouble nqtunitnum = BFPubTools.getUFDouble_NullAsZero(nnum).div(BFPubTools.getUFDouble_NullAsZero(map.get("measrate").toString().split("/")[0])); - bVO.setAttributeValue("nqtunitnum", nqtunitnum); - - sql = "select sl.pk_taxcode from bd_taxrate sl inner join bd_taxcode sm on sl.pk_taxcode=sm.pk_taxcode where sl.taxrate=" + ntaxrate + " and sm.pk_group<>'~' " + - ""; - - String o_pk_project = (String) new BaseDAO().executeQuery(sql, new ColumnProcessor()); - // 税码 - bVO.setAttributeValue("ctaxcodeid", o_pk_project); - } catch (BusinessException e) { - NCCForUAPLogger.debug("APISaleOrderMaitainImpl-setMaterl:" + e.getMessage()); - throw new RuntimeException(e); - } - } - - /** - * 修订销售订单的时候新增销售订单物料行的数据 - */ - private void addBvo(SaleOrderVO[] oldVOs, List> paramList) { - try { - SaleOrderVO[] copyVOs = new SaleOrderVO[oldVOs.length]; - for (int i = 0; i < oldVOs.length; i++) { - copyVOs[i] = (SaleOrderVO) oldVOs[i].clone(); - } - // 将 vos 中的元素按主键存储在 Map 中 - Map vosMap = new HashMap<>(); - for (SaleOrderVO vo : copyVOs) { - vosMap.put(vo.getParentVO().getCsaleorderid(), vo); - } - for (Map objectMap : paramList) { - Map headdata = (Map) objectMap.get("so_saleorder"); - String primaryKey = headdata.getOrDefault("csaleorderid", "") + ""; - SaleOrderVO vo = vosMap.get(primaryKey); - SaleOrderHVO hvo = vo.getParentVO(); - String csaleorderid = hvo.getCsaleorderid(); - String ybpk = hvo.getCorigcurrencyid(); - SaleOrderBVO[] bvos = vo.getChildrenVO(); - List bvoList = new ArrayList<>(Arrays.asList(bvos)); - SaleOrderBVO oneBvo = vo.getChildrenVO()[0]; - - List bodyArr = (List) objectMap.get("so_saleorder_b"); - for (Object body : bodyArr) { - Map bodydata = (Map) body; - if ("add".equals(bodydata.get("status"))) { - String pkOrgV = hvo.getPk_org_v(); - String pkOrg = hvo.getPk_org(); - String pk_group = AppContext.getInstance().getPkGroup(); - // 新数据的实体 - // SaleOrderBVO newBvo = (SaleOrderBVO) bvos[0].clone(); - SaleOrderBVO newBvo = new SaleOrderBVO(); - newBvo.setStatus(VOStatus.NEW); - newBvo.setAttributeValue("pk_group", pk_group); - String cprojectid = bodydata.get("cprojectid") + ""; - if (oneBvo != null && oneBvo.getCprojectid() != null && !"".equals(oneBvo.getCprojectid())) { - cprojectid = oneBvo.getCprojectid(); - } else { - String sql = " select pk_project from bd_project where project_code='" + cprojectid + "' and nvl(dr,0)=0 "; - cprojectid = (String) new BaseDAO().executeQuery(sql, new ColumnProcessor()); - } - newBvo.setAttributeValue("cprojectid", cprojectid); - // newBvo.setAttributeValue("ctrafficorgvid", pkOrg); - // newBvo.setAttributeValue("csendstockorgvid", pkOrg); - // newBvo.setAttributeValue("csendstordocid", pkOrg); - // newBvo.setAttributeValue("csendstockorgid", pkOrg); - // newBvo.setAttributeValue("carorgid", pkOrg); - // newBvo.setAttributeValue("carorgvid", pkOrg); - // newBvo.setAttributeValue("csettleorgid", pkOrg); - // newBvo.setAttributeValue("csettleorgvid", pkOrg); - - newBvo.setAttributeValue("ctaxcountryid", "CN"); - newBvo.setAttributeValue("crececountryid", "CN"); - newBvo.setAttributeValue("csendcountryid", "CN"); - - newBvo.setAttributeValue("fbuysellflag", 1); - UFDouble nexchangerateBip = getUFDouble_NullAsOne(bodydata.get("nexchangerate")); - newBvo.setNexchangerate(nexchangerateBip); - // 设置物料的关联字段的值 - newBvo.setCmaterialvid(bodydata.get("cmaterialvid") + ""); - UFDouble ntaxrate = BFPubTools.getUFDouble_NullAsZero(bodydata.get("ntaxrate")); - UFDouble nnum = BFPubTools.getUFDouble_NullAsZero(bodydata.get("nnum")); - newBvo.setAttributeValue("ntaxrate", ntaxrate); - newBvo.setAttributeValue("nnum", nnum); - newBvo.setVbdef11(bodydata.getOrDefault("vbdef11", "") + ""); - - UFDouble norigtaxprice = BFPubTools.getUFDouble_NullAsZero(bodydata.get("norigtaxprice")); - newBvo.setAttributeValue("norigtaxprice", norigtaxprice); - UFDouble norigprice = BFPubTools.getUFDouble_NullAsZero(bodydata.get("norigprice")); - newBvo.setAttributeValue("norigprice", norigprice); - setMaterl(newBvo); - newBvo.setCsaleorderbid(null); - - newBvo.setCcurrencyid(ybpk); - String zbbz = newBvo.getCcurrencyid(); - - newBvo.setFtaxtypeflag(1); - - // 折本汇率 - UFDouble nexchangerate = newBvo.getNexchangerate(); - // 含税单价 - UFDouble nqtorigtaxprice = BFPubTools.getUFDouble_NullAsZero(bodydata.get("nqtorigtaxprice")); - newBvo.setAttributeValue("nqtorigtaxprice", nqtorigtaxprice); - // 无税单价 - UFDouble nqtorigprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))); - // 价税合计 - UFDouble norigtaxmny = nqtorigtaxprice.multiply(newBvo.getNqtunitnum()).setScale(2, 4); - newBvo.setNorigtaxmny(norigtaxmny); - // 无税金额 - UFDouble norigmny = nqtorigprice.multiply(newBvo.getNqtunitnum()); - - newBvo.setNorigmny(nc.itf.fi.pub.Currency.getFormaUfValue(ybpk, norigmny)); - // 税额 - newBvo.setNqtorigprice(nqtorigprice.setScale(4, 4)); - - // 无税本币金额单价 - UFDouble taxspric = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))); - - - nqtorigprice = nqtorigprice.setScale(4, 4); - // nqtorigtaxnetprc--含税净价 - newBvo.setNqtorigtaxnetprc(nqtorigtaxprice); - //,nqtorignetprice --无税净价 - newBvo.setNqtorignetprice(nqtorigprice); - String Vqtunitrate = newBvo.getVqtunitrate(); - UFDouble dVqtunitrate = UFDouble.ONE_DBL; - if (Vqtunitrate != null) { - dVqtunitrate = BFPubTools.getUFDouble_NullAsZero(Vqtunitrate.split("/")[0]); - } - //,norigtaxprice --主含税单价 - - UFDouble wsje = taxspric.multiply(nexchangerate).multiply(newBvo.getNqtunitnum()); - if (ybpk.equals(zbbz) && BFPubTools.getString_TrimAsNull(newBvo.getCqtunitid()).equals(BFPubTools.getString_TrimAsNull(newBvo.getCastunitid()))) { - wsje = taxspric.multiply(nexchangerate).multiply(newBvo.getNqtunitnum()); - } - - - wsje = nc.itf.fi.pub.Currency.getFormaUfValue(zbbz, wsje); - // 本币无税金额 - newBvo.setNorigtaxprice(nqtorigtaxprice.div(dVqtunitrate).setScale(4, 4)); - //,norigprice --主无税单价 - newBvo.setNorigprice(nqtorigprice.div(dVqtunitrate).setScale(4, 4)); - //,norigtaxnetprice --主含税净价 - newBvo.setNorigtaxnetprice(newBvo.getNorigtaxprice()); - //,norignetprice --主无税净价 - newBvo.setNorignetprice(newBvo.getNorigprice()); - // ncaltaxmny --计税金额 - - // ,nqttaxprice --本币含税单价 - newBvo.setNqttaxprice(nqtorigtaxprice.multiply(nexchangerate)); - //,nqtprice --本币无税单价 - UFDouble bbwsd = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))).multiply(nexchangerate); - newBvo.setNqtprice(bbwsd.setScale(4, 4)); - //, nqttaxnetprice --本币含税净价 - newBvo.setNqttaxnetprice(nqtorigtaxprice.multiply(nexchangerate)); - //,nqtnetprice --本币无税净价 - UFDouble Nqtnetprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))).multiply(nexchangerate); - newBvo.setNqtnetprice(Nqtnetprice.setScale(4, 4)); - //,ntaxprice --主本币含税单价 , nprice --主本币无税单价 - newBvo.setNtaxprice(nqtorigtaxprice.div(dVqtunitrate).multiply(nexchangerate).setScale(4, 4)); - UFDouble Nprice = nqtorigtaxprice.div(dVqtunitrate).div(UFDouble.ONE_DBL.add(ntaxrate.div(100))).multiply(nexchangerate); - newBvo.setNprice(Nprice.setScale(4, 4)); - //,ntaxnetprice --主本币含税净价 - newBvo.setNtaxnetprice(nqtorigtaxprice.div(dVqtunitrate).multiply(nexchangerate).setScale(4, 4)); - //,nnetprice --主本币无税净价 - UFDouble nnetprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))).div(dVqtunitrate); - nnetprice = nnetprice.multiply(nexchangerate).setScale(4, 4); - newBvo.setNnetprice(nnetprice); - // nmny --本币无税金额 - // ntaxmny-- 本币价税合计 - newBvo.setNmny(Currency.getFormaUfValue(zbbz, norigmny.multiply(nexchangerate))); - newBvo.setNtaxmny(nqtorigtaxprice.multiply(nexchangerate).multiply(newBvo.getNqtunitnum()).setScale(2, 4)); - newBvo.setNcaltaxmny(wsje); - UFDouble ntax = norigtaxmny.multiply(nexchangerate).sub(wsje); - newBvo.setNtax(ntax.setScale(2, 4)); - newBvo.setCrowno(bodydata.get("crowno") + ""); - // 是否赠品 - String blargessflag = bodydata.getOrDefault("blargessflag", "N") + ""; - newBvo.setBlargessflag(UFBoolean.valueOf(blargessflag)); - bvoList.add(newBvo); - } - } - - SaleOrderBVO[] array = bvoList.toArray(new SaleOrderBVO[0]); - vo.setChildrenVO(array); - } - - // 填充默认值 - new SaleOrderSaveFillValue().setDefValue(copyVOs); - Map> listMap = new HashMap<>(); - for (SaleOrderVO copyVO : copyVOs) { - List newBvoList = new ArrayList<>(); - for (SaleOrderBVO saleOrderBVO : copyVO.getChildrenVO()) { - if (VOStatus.NEW == saleOrderBVO.getStatus()) { - SaleOrderBVO newBvo = (SaleOrderBVO) saleOrderBVO.clone(); - newBvoList.add(newBvo); - } - } - listMap.put(copyVO.getParentVO().getCsaleorderid(), newBvoList); - } - if (!listMap.isEmpty()) { - for (SaleOrderVO oldVO : oldVOs) { - String csaleorderid = oldVO.getParentVO().getCsaleorderid(); - SaleOrderBVO oneBvo = oldVO.getChildrenVO()[0]; - // 折本汇率 - UFDouble nexchangerate = oneBvo.getNexchangerate(); - nexchangerate = getUFDouble_NullAsOne(nexchangerate); - List newBvoList = listMap.get(csaleorderid); - if (newBvoList != null && !newBvoList.isEmpty()) { - // 上边填充默认值的时候取了系统的默认汇率,需要用BIP传的 - for (SaleOrderBVO saleOrderBVO : newBvoList) { - saleOrderBVO.setNexchangerate(nexchangerate); - } - SaleOrderBVO[] bvos = oldVO.getChildrenVO(); - List bvoList = new ArrayList<>(Arrays.asList(bvos)); - // 添加新增行 - bvoList.addAll(newBvoList); - SaleOrderBVO[] array = bvoList.toArray(new SaleOrderBVO[0]); - oldVO.setChildrenVO(array); - } - - } - } - /*for (SaleOrderVO combinBillVO : copyVOs) { - for (SaleOrderBVO saleOrderBVO : combinBillVO.getChildrenVO()) { - // 将实体对象转换为JSON字符串 - String jsonString = JSON.toJSONString(saleOrderBVO); - NCCForUAPLogger.debug("jsonString:" + jsonString); - } - }*/ - - } catch (Exception e) { - NCCForUAPLogger.debug("APISaleOrderMaitainImpl-addBvo:" + e.getMessage()); - throw new RuntimeException(e); - } - } - - private UFDouble getUFDouble_NullAsOne(Object value) { - if ((value == null) || (value.toString().trim().equals("")) || (value.toString().trim().equals("~"))) - return UFDouble.ONE_DBL; - if ((value instanceof UFDouble)) - return (UFDouble) value; - if ((value instanceof BigDecimal)) { - return new UFDouble((BigDecimal) value); - } - return new UFDouble(value.toString().trim()); - } + private static String BODYTABLE = "so_saleorder_b"; + private static String HEADTABLE = "so_saleorder"; + + public SaleOrderVO[] save(SaleOrderVO[] vos) throws BusinessException { + + SaleOrderVO[] fillvos = vos; + // 检查非空项 + for (SaleOrderVO vo : vos) { + SaleOrderHVO hvo = vo.getParentVO(); + UFDate dbilldate = hvo.getDbilldate(); + SaleOrderBVO[] bvos = vo.getChildrenVO(); + String sql = " select bd_currtype.pk_currtype from bd_currtype where (code='" + hvo.getCorigcurrencyid() + + "' or pk_currtype='" + hvo.getCorigcurrencyid() + "') and dr=0 "; + Object o = new BaseDAO().executeQuery(sql, new ColumnProcessor()); + if (o != null) { + hvo.setCorigcurrencyid(BFPubTools.getString_TrimAsNull(o)); + } else { + throw new BusinessException("表头币种不能为空或币种不存在"); + } + sql = " select bd_currtype.pk_currtype from bd_currtype where (code='" + bvos[0].getCcurrencyid() + + "' or pk_currtype='" + bvos[0].getCcurrencyid() + "') and dr=0 "; + Object o1 = new BaseDAO().executeQuery(sql, new ColumnProcessor()); + if (o1 == null) { + throw new BusinessException("表体币种不能为空或币种不存在"); + } + String csettleorgid = bvos[0].getCsettleorgid(); + String ccurrencyorgid = o1.toString(); + UFDouble exchangerate = SOCurrencyUtil.getInCurrencyRateByOrg(csettleorgid, + BFPubTools.getString_TrimAsNull(o), ccurrencyorgid, dbilldate); + for (SaleOrderBVO bvo : bvos) { + bvo.setCcurrencyid(BFPubTools.getString_TrimAsNull(o1)); + if (!BFPubTools.getString_TrimAsNull(o).equals(ccurrencyorgid)) { + bvo.setNexchangerate(exchangerate); + } else { + bvo.setNexchangerate(UFDouble.ONE_DBL); + } + + } + } + + BillVOsCheckRule checker = new BillVOsCheckRule(new SaleOrderValidator()); + checker.check(vos); + // 填充默认值 + new SaleOrderSaveFillValue().setDefValue(vos); + // 有值不覆盖 + for (SaleOrderVO ordervo : vos) { + calculatorPrice(ordervo); + } + + SaleOrderVO[] combinBillVOs = (SaleOrderVO[]) AggVOUtil.combinBillVO(fillvos, vos); + // 保存 + SaleOrderVO[] retvos = (SaleOrderVO[]) PfServiceScmUtil.processBatch(SOConstant.WRITE, + SOBillType.Order.getCode(), combinBillVOs, null, null); + + SaleOrderVO[] billvos = ((ISaleOrderQueryAPI) NCLocator.getInstance().lookup(ISaleOrderQueryAPI.class)) + .queryVOByIDs(new String[] { retvos[0].getParentVO().getPrimaryKey() }); + if (billvos != null) { + ((IPFBusiAction) NCLocator.getInstance().lookup(IPFBusiAction.class)).processAction("APPROVE", + billvos[0].getParentVO().getVtrantypecode(), null, billvos[0], null, null); + } + return retvos; + } + + public void calculatorPrice(SaleOrderVO ordervo) throws BusinessException { + + IKeyValue keyValue = new VOKeyValue(ordervo); + + String ctrantypeid = keyValue.getHeadStringValue(SaleOrderHVO.CTRANTYPEID); + if (StringUtil.isEmptyTrimSpace(ctrantypeid)) { + ExceptionUtils.wrappBusinessException( + NCLangRes4VoTransl.getNCLangRes().getStrByID("4006013_0", "04006013-0024")/* @res "请先选择交易类型!" */); + } + // 1.缓存交易类型VO + + SaleOrderBVO[] vbos = ordervo.getChildrenVO(); + UFDouble sumnum = UFDouble.ZERO_DBL; + UFDouble sumnny = UFDouble.ZERO_DBL; + String ybpk = ordervo.getParentVO().getCorigcurrencyid(); + + for (int i = 0; i < vbos.length; i++) { + SaleOrderBVO childrenVO = vbos[i]; + String zbbz = childrenVO.getCcurrencyid(); + + childrenVO.setFtaxtypeflag(1); + // 得到税率 + UFDouble ntaxrate = BFPubTools.getUFDouble_NullAsZero(childrenVO.getNtaxrate()); + + // 折本汇率 + UFDouble nexchangerate = childrenVO.getNexchangerate(); + // 含税单价 + UFDouble nqtorigtaxprice = childrenVO.getNqtorigtaxprice(); + // 无税单价 + UFDouble nqtorigprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))); + // 价税合计 + // UFDouble norigtaxmny=nqtorigtaxprice.multiply(childrenVO.getNqtunitnum()); + UFDouble norigtaxmny = nqtorigtaxprice.multiply(childrenVO.getNqtunitnum()).setScale(2, 4); + childrenVO.setNorigtaxmny(norigtaxmny); + // 无税金额 + UFDouble norigmny = nqtorigprice.multiply(childrenVO.getNqtunitnum()); + + childrenVO.setNorigmny(Currency.getFormaUfValue(ybpk, norigmny)); + // 税额 + childrenVO.setNqtorigprice(nqtorigprice.setScale(4, 4)); + + // 无税本币金额单价 + UFDouble taxspric = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))); + + sumnum = sumnum.add(childrenVO.getNastnum()); + sumnny = sumnny.add(childrenVO.getNorigtaxmny()); + + nqtorigprice = nqtorigprice.setScale(4, 4); + // nqtorigtaxnetprc--含税净价 + childrenVO.setNqtorigtaxnetprc(nqtorigtaxprice); + // ,nqtorignetprice --无税净价 + childrenVO.setNqtorignetprice(nqtorigprice); + String Vqtunitrate = childrenVO.getVqtunitrate(); + UFDouble dVqtunitrate = UFDouble.ONE_DBL; + if (Vqtunitrate != null) { + dVqtunitrate = BFPubTools.getUFDouble_NullAsZero(Vqtunitrate.split("/")[0]); + } + // ,norigtaxprice --主含税单价 + + UFDouble wsje = taxspric.multiply(nexchangerate).multiply(childrenVO.getNqtunitnum()); + if (ybpk.equals(zbbz) && BFPubTools.getString_TrimAsNull(childrenVO.getCqtunitid()) + .equals(BFPubTools.getString_TrimAsNull(childrenVO.getCastunitid()))) { + wsje = taxspric.multiply(nexchangerate).multiply(childrenVO.getNqtunitnum()); + } + + wsje = Currency.getFormaUfValue(zbbz, wsje); + // 本币无税金额 + childrenVO.setNorigtaxprice(nqtorigtaxprice.div(dVqtunitrate).setScale(4, 4)); + // ,norigprice --主无税单价 + childrenVO.setNorigprice(nqtorigprice.div(dVqtunitrate).setScale(4, 4)); + // ,norigtaxnetprice --主含税净价 + childrenVO.setNorigtaxnetprice(childrenVO.getNorigtaxprice()); + // ,norignetprice --主无税净价 + childrenVO.setNorignetprice(childrenVO.getNorigprice()); + // ncaltaxmny --计税金额 + + // ,nqttaxprice --本币含税单价 + childrenVO.setNqttaxprice(nqtorigtaxprice.multiply(nexchangerate)); + // ,nqtprice --本币无税单价 + UFDouble bbwsd = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))).multiply(nexchangerate); + childrenVO.setNqtprice(bbwsd.setScale(4, 4)); + // , nqttaxnetprice --本币含税净价 + childrenVO.setNqttaxnetprice(nqtorigtaxprice.multiply(nexchangerate)); + // ,nqtnetprice --本币无税净价 + UFDouble Nqtnetprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))).multiply(nexchangerate); + childrenVO.setNqtnetprice(Nqtnetprice.setScale(4, 4)); + // ,ntaxprice --主本币含税单价 , nprice --主本币无税单价 + childrenVO.setNtaxprice(nqtorigtaxprice.div(dVqtunitrate).multiply(nexchangerate).setScale(4, 4)); + UFDouble Nprice = nqtorigtaxprice.div(dVqtunitrate).div(UFDouble.ONE_DBL.add(ntaxrate.div(100))) + .multiply(nexchangerate); + childrenVO.setNprice(Nprice.setScale(4, 4)); + // ,ntaxnetprice --主本币含税净价 + // ,nnetprice --主本币无税净价 + childrenVO.setNtaxnetprice(nqtorigtaxprice.div(dVqtunitrate).multiply(nexchangerate).setScale(4, 4)); + UFDouble Nnetprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))).div(dVqtunitrate); + childrenVO.setNnetprice(Nnetprice.multiply(nexchangerate).setScale(4, 4)); +// ,nmny --本币无税金额 +// ,ntaxmny --本币价税合计 + childrenVO.setNmny(Currency.getFormaUfValue(zbbz, norigmny.multiply(nexchangerate))); + childrenVO.setNtaxmny( + nqtorigtaxprice.multiply(nexchangerate).multiply(childrenVO.getNqtunitnum()).setScale(2, 4)); +// childrenVO.setNtaxmny(nqtorigtaxprice.multiply(nexchangerate).multiply(childrenVO.getNqtunitnum())); + childrenVO.setNcaltaxmny(wsje); + UFDouble ntax = norigtaxmny.multiply(nexchangerate).sub(wsje); + childrenVO.setNtax(ntax.setScale(2, 4)); + } + ordervo.getParentVO().setNtotalnum(sumnum); +// ordervo.getParentVO().setNtotalorigmny(sumnny); + ordervo.getParentVO().setNtotalorigmny(sumnny.setScale(2, 4)); + } + + @Override + public SaleOrderVO[] save(List> paramList) throws BusinessException { + try { + List aggVOList = TransferMapToVOTool.transferMapToAggVO(paramList, SaleOrderVO.class); + + SaleOrderVO[] vos = (SaleOrderVO[]) aggVOList.toArray(new SaleOrderVO[aggVOList.size()]); + BillVOsCheckRule checker = new BillVOsCheckRule(new SaleOrderValidator()); + checker.check(vos); + + aggVOList = TransferCodeToPKTool.transferAggVO(aggVOList); + + SaleOrderSaveUtil saveUtil = new SaleOrderSaveUtil(); + return saveUtil.processBill(vos); + } catch (Exception e) { + ExceptionUtils.marsh(e); + return null; + } + } + + @Override + public SaleOrderVO[] update(List> paramList) throws BusinessException { + List aggVOList = TransferMapToVOTool.transferMapToAggVO(paramList, SaleOrderVO.class); + + aggVOList = TransferCodeToPKTool.transferAggVO(aggVOList); + + SaleOrderVO[] vos = (SaleOrderVO[]) aggVOList.toArray(new SaleOrderVO[aggVOList.size()]); + + Map> ids = getIds(vos); + if (ids.keySet() == null || ids.values() == null || ids.values().size() == 0) { + ExceptionUtils.wrappBusinessException("请传入订单主键和订单行主键"); + } + String[] hids = (String[]) ids.keySet().toArray(new String[ids.keySet().size()]); + + ISaleOrderMaintain service = (ISaleOrderMaintain) NCLocator.getInstance().lookup(ISaleOrderMaintain.class); + SaleOrderVO[] originVos = service.querySaleorder(hids); + if (null == originVos || originVos.length == 0) { + ExceptionUtils.wrappBusinessException("根据传入数据未匹配到相关数据"); + } + + for (Map objectMap : paramList) { + Map headdata = (Map) objectMap.get(HEADTABLE); + List bodyArr = (List) objectMap.get(BODYTABLE); + for (Object body : bodyArr) { + Map bodydata = (Map) body; + if (!(bodydata.getOrDefault("vbdef11", "") + "").isEmpty()) { + String vbdef11 = bodydata.get("vbdef11") + ""; + String dr = bodydata.getOrDefault("dr", "0") + ""; + String csaleorderid = bodydata.get("csaleorderid") + ""; + String countSql = "SELECT count(1) FROM so_saleorder_b" + + " WHERE nvl(dr,0) = 0 and csaleorderid = '[csaleorderid]' and vbdef11 = '[vbdef11]' "; + countSql = countSql.replace("[csaleorderid]", csaleorderid); + countSql = countSql.replace("[vbdef11]", vbdef11); + Integer num = (Integer) new BaseDAO().executeQuery(countSql, new ColumnProcessor()); + if ("0".equals(dr) && num <= 0) { + bodydata.put("status", "add"); + } + } + } + } + + // SetUpdateData setData = new SetUpdateData(); + // setData.setData(vos, originVos); + // SaleOrderVO[] combinBillVOs = (SaleOrderVO[]) AggVOUtil.combinBillVO(vos, + // originVos); + // 创建一个新的数组,并进行深拷贝 + SaleOrderVO[] combinBillVOs = new SaleOrderVO[originVos.length]; + for (int i = 0; i < originVos.length; i++) { + combinBillVOs[i] = (SaleOrderVO) originVos[i].clone(); + } + // 将 vos 中的元素按主键存储在 Map 中 + Map vosMap = new HashMap<>(); + for (SaleOrderVO vo : vos) { + vosMap.put(vo.getParentVO().getCsaleorderid(), vo); + } + + // 遍历 combinBillVOs 并更新字段 + for (SaleOrderVO vo : combinBillVOs) { + SaleOrderHVO hvo = vo.getParentVO(); + String primaryKey = hvo.getCsaleorderid(); + SaleOrderVO bipVo = vosMap.get(primaryKey); + // 设置单据状态 + hvo.setStatus(VOStatus.UPDATED); + + Map bvoMap = new HashMap<>(); + for (SaleOrderBVO saleOrderBVO : bipVo.getChildrenVO()) { + bvoMap.put(saleOrderBVO.getCsaleorderbid(), saleOrderBVO); + } + + // 比较combinBillVOs中的BVO和vos中的BVO + for (SaleOrderBVO bvo : vo.getChildrenVO()) { + bvo.setStatus(VOStatus.UPDATED); + // 设置删除的物料行的状态,在bip传的数据中找不到 + String csaleorderbid = bvo.getCsaleorderbid(); + if (bvoMap.get(csaleorderbid) == null) { + bvo.setStatus(VOStatus.DELETED); + NCCForUAPLogger.debug("findDeletedBids:" + VOStatus.DELETED + ",csaleorderbid = " + csaleorderbid); + } + } + // 更新 combinBillVO 中的字段为 vos 中的值 + updateFields(vo, bipVo); + // 设置新增的物料行 + if (paramList != null && !paramList.isEmpty()) { + addBvo(combinBillVOs, paramList); + } + } + // 联动计算 + INumPriceMnyCalculator cal = new SaleOrderNPriceMnyCal(combinBillVOs); + cal.calculate(); + for (SaleOrderVO vo : combinBillVOs) { + SaleOrderHVO hvo = vo.getParentVO(); + SaleOrderHVO originHvo = null; + for (SaleOrderVO originVo : originVos) { + if (hvo.getCsaleorderid().equals(originVo.getParentVO().getCsaleorderid())) { + originHvo = originVo.getParentVO(); + } + } + String new_ctrantypeid = hvo.getCtrantypeid(); + boolean isChangedTranType = false;// 订单类型不修改 + if (originHvo != null && new_ctrantypeid != null && !new_ctrantypeid.equals(originHvo.getCtrantypeid())) { + isChangedTranType = true; + } + SaleOrderBVO[] bvos = vo.getChildrenVO(); + // 判断是否存在新增的子表 + boolean hasNewStatus = Arrays.stream(bvos).anyMatch(bvo -> bvo.getStatus() == VOStatus.NEW); + // 新增子表或改订单类型的情况下不校验是否存在下游 + if (!hasNewStatus && !isChangedTranType) { + String countSql = "SELECT count(1) FROM so_saleinvoice_b a" + + " LEFT JOIN so_saleinvoice b ON a.csaleinvoiceid = b.csaleinvoiceid" + + " WHERE b.fopposeflag = 0 AND nvl(b.dr, 0) = 0 and csrcid = '[csrcid]' "; + countSql = countSql.replace("[csrcid]", hvo.getCsaleorderid()); + Integer num = (Integer) new BaseDAO().executeQuery(countSql, new ColumnProcessor()); + if (num > 0) { + // fopposeflag 对冲标记 0=正常 1=已被对冲 2=对冲生成 + ExceptionUtils.wrappBusinessException("下游存在未红冲完成的销售发票"); + return null; + } + } + } + + fillcustomervidbyoid(combinBillVOs); + + // 保存 + ISaleOrderScriptMaintain maintainsrv = NCLocator.getInstance().lookup(ISaleOrderScriptMaintain.class); + SaleOrderVO[] retvos = maintainsrv.saleOrderUpdate(combinBillVOs, null, originVos); + return retvos; + } + + private Map> getIds(SaleOrderVO[] vos) { + Map> ids = new HashMap>(); + for (SaleOrderVO vo : vos) { + String hid = vo.getParentVO().getCsaleorderid(); + Set bids = new HashSet(); + for (SaleOrderBVO bvo : vo.getChildrenVO()) { + bids.add(bvo.getCsaleorderbid()); + } + ids.put(hid, bids); + } + return ids; + } + + private void setOtherId(SaleOrderVO[] vos) { + for (SaleOrderVO vo : vos) { + // 部门、业务员、开票客户编码转id + SaleOrderHVO orderHVO = vo.getParentVO(); + String csaleorderid = orderHVO.getCsaleorderid(); + String cdeptvid = orderHVO.getCdeptvid(); + String cemployeeid = orderHVO.getCemployeeid(); + // 开票客户 + String cinvoicecustid = orderHVO.getCinvoicecustid(); + // 客户 + String ccustomerid = orderHVO.getCcustomerid(); + // 订单类型 + // String ctrantypeid = orderHVO.getAttributeValue("ctrantypeid") + ""; + String vtrantypecode = orderHVO.getVtrantypecode(); + NCCForUAPLogger.debug("setOtherId-ccustomerid:" + ccustomerid + ",vtrantypecode:" + vtrantypecode); + try { + String sql = ""; + // 部门 + if (cdeptvid != null && !cdeptvid.isEmpty()) { + sql = " select pk_vid,pk_dept from org_dept where code = '[code]' "; + sql = sql.replace("[code]", cdeptvid); + Map deptMap = (Map) new BaseDAO().executeQuery(sql, new MapProcessor()); + NCCForUAPLogger.debug("APISaleOrderMaitainImpl-setOtherId-deptObj:" + deptMap); + if (deptMap != null && !deptMap.isEmpty()) { + if (!"".equals(deptMap.getOrDefault("pk_dept", "") + "")) { + orderHVO.setCdeptid(deptMap.getOrDefault("pk_dept", "") + ""); + } + if (!"".equals(deptMap.getOrDefault("pk_vid", "") + "")) { + orderHVO.setCdeptvid(deptMap.getOrDefault("pk_vid", "") + ""); + } + } + } + // 业务员 + if (cemployeeid != null && !cemployeeid.isEmpty()) { + sql = " select pk_psndoc from bd_psndoc where code = '[code]' "; + sql = sql.replace("[code]", cemployeeid); + Object staffObj = new BaseDAO().executeQuery(sql, new ColumnProcessor("pk_psndoc")); + NCCForUAPLogger.debug("APISaleOrderMaitainImpl-setOtherId-staffObj:" + staffObj); + if (staffObj != null) { + String id = BFPubTools.getString_TrimAsNull(staffObj); + if (!id.isEmpty()) { + orderHVO.setCemployeeid(id); + } + } + } + // 开票客户 + if (cinvoicecustid != null && !cinvoicecustid.isEmpty()) { + sql = " select pk_customer from bd_customer where nvl(dr,0) = 0 and code = '[code]' "; + sql = sql.replace("[code]", cinvoicecustid); + Object invCustObj = new BaseDAO().executeQuery(sql, new ColumnProcessor("pk_customer")); + NCCForUAPLogger.debug("setOtherId-invCustObj:" + invCustObj); + if (invCustObj != null) { + String id = BFPubTools.getString_TrimAsNull(invCustObj); + if (!id.isEmpty()) { + orderHVO.setCinvoicecustid(id); + } + } + } + // 客户 + if (ccustomerid != null && !ccustomerid.isEmpty()) { + sql = " select pk_customer from bd_customer where nvl(dr,0) = 0 and code = '[code]' "; + sql = sql.replace("[code]", ccustomerid); + Object custObj = new BaseDAO().executeQuery(sql, new ColumnProcessor("pk_customer")); + NCCForUAPLogger.debug("setOtherId-custObj:" + custObj); + if (custObj != null) { + String id = BFPubTools.getString_TrimAsNull(custObj); + if (!id.isEmpty()) { + orderHVO.setCcustomerid(id); + } + } + } + // 交易类型 + if (vtrantypecode != null && !vtrantypecode.isEmpty()) { + IBilltypeService billtypeService = NCLocator.getInstance().lookup(IBilltypeService.class); + BilltypeVO billtype = billtypeService.getBilltype(vtrantypecode); + if (billtype != null && billtype.getPk_billtypeid() != null) { + NCCForUAPLogger.debug("setOtherId-pk_billtypeid:" + billtype.getPk_billtypeid()); + orderHVO.setCtrantypeid(billtype.getPk_billtypeid()); + } + } + } catch (Exception e) { + NCCForUAPLogger.debug("APISaleOrderMaitainImpl-setOtherId-exp:" + e.getMessage()); + throw new RuntimeException(e); + } + } + } + + private void updateFields(SaleOrderVO target, SaleOrderVO source) { + // 更新 SaleOrderHVO 字段 + SaleOrderHVO targetHVO = target.getParentVO(); + SaleOrderHVO sourceHVO = source.getParentVO(); + // 表头修改修改的字段 + String cdeptvid = sourceHVO.getCdeptvid(); + String cdeptid = sourceHVO.getCdeptid(); + if (cdeptvid != null && !cdeptvid.isEmpty()) { + targetHVO.setCdeptvid(cdeptvid); + targetHVO.setCdeptid(cdeptid); + } + + String cemployeeid = sourceHVO.getCemployeeid(); + if (cemployeeid != null && !cemployeeid.isEmpty()) { + targetHVO.setCemployeeid(cemployeeid); + } + String cinvoicecustid = sourceHVO.getCinvoicecustid(); + if (cinvoicecustid != null && !cinvoicecustid.isEmpty()) { + targetHVO.setCinvoicecustid(cinvoicecustid); + } + String ccustomerid = sourceHVO.getCcustomerid(); + if (ccustomerid != null && !ccustomerid.isEmpty()) { + targetHVO.setCcustomerid(ccustomerid); + } + // 订单类型id + String ctrantypeid = sourceHVO.getCtrantypeid(); + if (ctrantypeid != null && !ctrantypeid.isEmpty()) { + targetHVO.setCtrantypeid(ctrantypeid); + } + // 订单类型编码 + String vtrantypecode = sourceHVO.getVtrantypecode(); + if (vtrantypecode != null && !vtrantypecode.isEmpty()) { + targetHVO.setVtrantypecode(vtrantypecode); + } + + // 更新 SaleOrderBVO 字段 + SaleOrderBVO[] targetBVOs = target.getChildrenVO(); + SaleOrderBVO[] sourceBVOs = source.getChildrenVO(); + + // BVO 的主键是 csaleorderbid + Map sourceBVOsMap = new HashMap<>(); + for (SaleOrderBVO bvo : sourceBVOs) { + sourceBVOsMap.put(bvo.getCsaleorderbid(), bvo); + } + + for (SaleOrderBVO targetBVO : targetBVOs) { + String bvoId = targetBVO.getCsaleorderbid(); + SaleOrderBVO sourceBVO = sourceBVOsMap.get(bvoId); + if (sourceBVO != null) { + int status = targetBVO.getStatus(); + if (VOStatus.UPDATED == status) { + // 数量 + UFDouble nnum = sourceBVO.getNnum(); + targetBVO.setNnum(nnum); + targetBVO.setNastnum(nnum); + // targetBVO.setNqtunitnum(nnum); + // 价税合计 + targetBVO.setNorigtaxmny(sourceBVO.getNorigtaxmny()); + targetBVO.setCmaterialvid(sourceBVO.getCmaterialvid()); + // 是否赠品 + targetBVO.setBlargessflag(sourceBVO.getBlargessflag()); + // 折本汇率 + UFDouble nexchangerate = getUFDouble_NullAsOne(sourceBVO.getNexchangerate()); + targetBVO.setNexchangerate(nexchangerate); + // 物料编码 + String cmaterialvid = sourceBVO.getCmaterialvid(); + // 物料关联字段赋值 + if (cmaterialvid != null && !cmaterialvid.isEmpty()) { + setMaterl(targetBVO); + } + // 自定义项赋值 + String vbdef1 = sourceBVO.getVbdef1();// 合同编号 + Map defList = getDefList("zdy-001"); + targetBVO.setVbdef1(defList.getOrDefault(vbdef1, "")); + String vbdef5 = sourceBVO.getVbdef5();// 合同销售订单号 + defList.clear(); + defList = getDefList("BIP-ddh"); + targetBVO.setVbdef5(defList.getOrDefault(vbdef5, "")); + targetBVO.setVbdef10(sourceBVO.getVbdef10());// 项目名称 + targetBVO.setVbdef13(sourceBVO.getVbdef13());// 国网行项目号 + NCCForUAPLogger.debug(String.format( + "cmaterialvid = [%S], vbdef1 = [%S], vbdef5 = [%S], vbdef10 = [%S], vbdef13 = [%S]", + cmaterialvid, targetBVO.getVbdef1(), targetBVO.getVbdef5(), targetBVO.getVbdef10(), + targetBVO.getVbdef13())); + } + + } + } + } + + /** + * 获取自定义项信息 + */ + private Map getDefList(String code) { + Map map = new HashMap(); + String strWhere = " pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code='" + code + + "' and dr=0 ) and dr = 0"; + try { + DefdocVO[] defdocVOs = (DefdocVO[]) new HYSuperDMO().queryByWhereClause(DefdocVO.class, strWhere); + if (defdocVOs != null && defdocVOs.length > 0) { + for (DefdocVO defdocVO : defdocVOs) { + map.put(defdocVO.getCode().trim(), defdocVO.getPk_defdoc()); + } + } + } catch (DAOException e) { + e.printStackTrace(); + } + return map; + + } + + @Override + public void delete(String[] hids) throws BusinessException { + BillQuery queryTool = new BillQuery(SaleOrderVO.class); + SaleOrderVO[] saleOrderVOs = (SaleOrderVO[]) queryTool.query(hids); + if (saleOrderVOs.length == 0) { + ExceptionUtils.wrappBusinessException("根据传入数据未匹配到相关数据"); + } + + PfServiceScmUtil.processBatch("DELETE", SOBillType.Order.getCode(), saleOrderVOs, null, null); + } + + private void fillcustomervidbyoid(SaleOrderVO[] combinBillVOs) { + Set customeridSet = new HashSet(); + Map customeridMap = new HashMap(); + for (SaleOrderVO vo : combinBillVOs) { + SaleOrderHVO hvo = vo.getParentVO(); + + if (!StringUtil.isSEmptyOrNull(hvo.getCcustomerid()) && StringUtil.isSEmptyOrNull(hvo.getCcustomervid())) { + customeridSet.add(hvo.getCcustomerid()); + } + + if (!StringUtil.isSEmptyOrNull(hvo.getCinvoicecustid()) + && StringUtil.isSEmptyOrNull(hvo.getCinvoicecustvid())) { + customeridSet.add(hvo.getCinvoicecustid()); + } + + if (!StringUtil.isSEmptyOrNull(hvo.getChreceivecustid()) + && StringUtil.isSEmptyOrNull(hvo.getChreceivecustvid())) { + customeridSet.add(hvo.getChreceivecustid()); + } + + SaleOrderBVO[] bvos = vo.getChildrenVO(); + for (SaleOrderBVO bvo : bvos) { + + if (!StringUtil.isSEmptyOrNull(bvo.getCreceivecustid()) + && StringUtil.isSEmptyOrNull(bvo.getCreceivecustvid())) { + customeridSet.add(bvo.getCreceivecustid()); + } + + if (!StringUtil.isSEmptyOrNull(bvo.getCvendorid()) && StringUtil.isSEmptyOrNull(bvo.getCvendorvid())) { + customeridSet.add(bvo.getCvendorid()); + } + } + } + + if (!customeridSet.isEmpty()) { + customeridMap = CustomerPubService + .queryCustomerVidsByOids((String[]) CollectionUtils.setToArray(customeridSet)); + } + + for (SaleOrderVO vo : combinBillVOs) { + SaleOrderHVO hvo = vo.getParentVO(); + + if (!StringUtil.isSEmptyOrNull(hvo.getCcustomerid()) && StringUtil.isSEmptyOrNull(hvo.getCcustomervid())) { + if (null == customeridMap || customeridMap.size() == 0) { + hvo.setCcustomervid(hvo.getCcustomerid()); + } else { + hvo.setCcustomervid((String) customeridMap.get(hvo.getCcustomerid())); + } + } + + if (!StringUtil.isSEmptyOrNull(hvo.getCinvoicecustid()) + && StringUtil.isSEmptyOrNull(hvo.getCinvoicecustvid())) { + if (null == customeridMap || customeridMap.size() == 0) { + hvo.setCinvoicecustvid(hvo.getCinvoicecustid()); + } else { + hvo.setCinvoicecustvid((String) customeridMap.get(hvo.getCinvoicecustid())); + } + } + + if (!StringUtil.isSEmptyOrNull(hvo.getChreceivecustid()) + && StringUtil.isSEmptyOrNull(hvo.getChreceivecustvid())) { + if (null == customeridMap || customeridMap.size() == 0) { + hvo.setChreceivecustvid(hvo.getChreceivecustid()); + } else { + hvo.setChreceivecustvid((String) customeridMap.get(hvo.getChreceivecustid())); + } + } + + SaleOrderBVO[] bvos = vo.getChildrenVO(); + for (SaleOrderBVO bvo : bvos) { + + if (!StringUtil.isSEmptyOrNull(bvo.getCreceivecustid()) + && StringUtil.isSEmptyOrNull(bvo.getCreceivecustvid())) { + if (null == customeridMap || customeridMap.size() == 0) { + bvo.setCreceivecustvid(bvo.getCreceivecustid()); + } else { + bvo.setCreceivecustvid((String) customeridMap.get(bvo.getCreceivecustid())); + } + } + + if (!StringUtil.isSEmptyOrNull(bvo.getCvendorid()) && StringUtil.isSEmptyOrNull(bvo.getCvendorvid())) { + if (null == customeridMap || customeridMap.size() == 0) { + bvo.setCvendorvid(bvo.getCvendorid()); + } else { + bvo.setCvendorvid((String) customeridMap.get(bvo.getCvendorid())); + } + } + } + } + } + + @Override + public SaleOrderVO[] sendApprove(String[] hids) throws BusinessException { + SaleOrderVO[] vos = querySaleOrder(hids); + try { + for (SaleOrderVO aggvo : vos) { + SaleOrderHVO parentVO = aggvo.getParentVO(); + if (!BillStatus.FREE.equalsValue(parentVO.getFstatusflag())) { + ExceptionUtils.wrappBusinessException("当前销售订单:" + parentVO.getVbillcode() + "不是自由状态,不能提交"); + } + } + return (SaleOrderVO[]) PfServiceScmUtil.processBatch("SAVE", SOBillType.Order.getCode(), vos, null, null); + } catch (Exception e) { + ExceptionUtils.marsh(e); + return null; + } + } + + @Override + public SaleOrderVO[] unSendApprove(String[] hids) throws BusinessException { + SaleOrderVO[] vos = querySaleOrder(hids); + try { + for (SaleOrderVO aggvo : vos) { + SaleOrderHVO parentVO = aggvo.getParentVO(); + if (!BillStatus.AUDITING.equalsValue(parentVO.getFstatusflag())) { + ExceptionUtils.wrappBusinessException("当前销售订单:" + parentVO.getVbillcode() + "不是提交状态,不能收回"); + } + } + return (SaleOrderVO[]) PfServiceScmUtil.processBatch("UNSAVE", SOBillType.Order.getCode(), vos, null, null); + } catch (Exception e) { + ExceptionUtils.marsh(e); + return null; + } + } + + @Override + public SaleOrderVO[] approve(String[] hids) throws BusinessException { + SaleOrderVO[] vos = querySaleOrder(hids); + try { + for (SaleOrderVO aggvo : vos) { + SaleOrderHVO parentVO = aggvo.getParentVO(); + if (!BillStatus.FREE.equalsValue(parentVO.getFstatusflag()) + && !BillStatus.AUDIT.equalsValue(parentVO.getFstatusflag()) + && !BillStatus.AUDITING.equalsValue(parentVO.getFstatusflag())) { + ExceptionUtils.wrappBusinessException("当前销售订单:" + parentVO.getVbillcode() + "状态不符合审批条件,不能审批"); + } + } + return (SaleOrderVO[]) PfServiceScmUtil.processBatch("APPROVE", SOBillType.Order.getCode(), vos, null, + null); + } catch (Exception e) { + ExceptionUtils.marsh(e); + return null; + } + } + + @Override + public SaleOrderVO[] unApprove(String[] hids) throws BusinessException { + SaleOrderVO[] vos = querySaleOrder(hids); + try { + for (SaleOrderVO aggvo : vos) { + SaleOrderHVO parentVO = aggvo.getParentVO(); + if (!BillStatus.AUDIT.equalsValue(parentVO.getFstatusflag()) + && !BillStatus.AUDITING.equalsValue(parentVO.getFstatusflag())) { + ExceptionUtils.wrappBusinessException("当前销售订单:" + parentVO.getVbillcode() + "状态不符合弃审条件,不能弃审"); + } + } + return (SaleOrderVO[]) PfServiceScmUtil.processBatch("UNAPPROVE", SOBillType.Order.getCode(), vos, null, + null); + } catch (Exception e) { + ExceptionUtils.marsh(e); + return null; + } + } + + private SaleOrderVO[] querySaleOrder(String[] hids) throws BusinessException { + ISaleOrderQueryAPI service = (ISaleOrderQueryAPI) NCLocator.getInstance().lookup(ISaleOrderQueryAPI.class); + SaleOrderVO[] vos = service.queryVOByIDs(hids); + if (ArrayUtils.isEmpty(vos)) { + ExceptionUtils.wrappBusinessException("没有符合条件的数据"); + } else { + return vos; + } + return null; + } + + /** + * 设置物料的相关字段 + */ + private void setMaterl(SaleOrderBVO bVO) { + String cmaterialvid = bVO.getCmaterialvid(); + UFDouble nnum = bVO.getNnum(); + UFDouble ntaxrate = bVO.getNtaxrate(); + try { + String sql = " select a.pk_material,a.pk_source, a.pk_measdoc cunitid,nvl(b.pk_measdoc,a.pk_measdoc) castunitid,nvl(b.measrate,'1/1') measrate " + + "from bd_material a left join bd_materialconvert b on a.pk_material=b.pk_material " + + "where a.code='" + cmaterialvid + "' "; + Map map = (Map) new BaseDAO().executeQuery(sql, new MapProcessor()); + if (map == null) { + throw new BusinessException(cmaterialvid + "物料未查到"); + } + NCCForUAPLogger.debug(String.format("setMaterl: cqtunitid = [%S], cunitid = [%S], castunitid = [%S]", + map.get("cqtunitid"), map.get("cunitid"), map.get("castunitid"))); + bVO.setAttributeValue("castunitid", map.get("castunitid")); + bVO.setAttributeValue("vchangerate", map.get("vchangerate")); + + // 重新计算金额 + bVO.setAttributeValue("cqtunitid", map.get("cunitid")); + bVO.setAttributeValue("cunitid", map.get("cunitid")); + + bVO.setAttributeValue("cmaterialid", map.get("pk_material")); + bVO.setAttributeValue("cmaterialvid", map.get("pk_material")); + + UFDouble nqtunitnum = BFPubTools.getUFDouble_NullAsZero(nnum) + .div(BFPubTools.getUFDouble_NullAsZero(map.get("measrate").toString().split("/")[0])); + bVO.setAttributeValue("nqtunitnum", nqtunitnum); + + sql = "select sl.pk_taxcode from bd_taxrate sl inner join bd_taxcode sm on sl.pk_taxcode=sm.pk_taxcode where sl.taxrate=" + + ntaxrate + " and sm.pk_group<>'~' " + ""; + + String o_pk_project = (String) new BaseDAO().executeQuery(sql, new ColumnProcessor()); + // 税码 + bVO.setAttributeValue("ctaxcodeid", o_pk_project); + } catch (BusinessException e) { + NCCForUAPLogger.debug("APISaleOrderMaitainImpl-setMaterl:" + e.getMessage()); + throw new RuntimeException(e); + } + } + + /** + * 修订销售订单的时候新增销售订单物料行的数据 + */ + private void addBvo(SaleOrderVO[] oldVOs, List> paramList) { + try { + SaleOrderVO[] copyVOs = new SaleOrderVO[oldVOs.length]; + for (int i = 0; i < oldVOs.length; i++) { + copyVOs[i] = (SaleOrderVO) oldVOs[i].clone(); + } + // 将 vos 中的元素按主键存储在 Map 中 + Map vosMap = new HashMap<>(); + for (SaleOrderVO vo : copyVOs) { + vosMap.put(vo.getParentVO().getCsaleorderid(), vo); + } + for (Map objectMap : paramList) { + Map headdata = (Map) objectMap.get("so_saleorder"); + String primaryKey = headdata.getOrDefault("csaleorderid", "") + ""; + SaleOrderVO vo = vosMap.get(primaryKey); + SaleOrderHVO hvo = vo.getParentVO(); + String csaleorderid = hvo.getCsaleorderid(); + String ybpk = hvo.getCorigcurrencyid(); + SaleOrderBVO[] bvos = vo.getChildrenVO(); + List bvoList = new ArrayList<>(Arrays.asList(bvos)); + SaleOrderBVO oneBvo = vo.getChildrenVO()[0]; + + List bodyArr = (List) objectMap.get("so_saleorder_b"); + for (Object body : bodyArr) { + Map bodydata = (Map) body; + if ("add".equals(bodydata.get("status"))) { + String pkOrgV = hvo.getPk_org_v(); + String pkOrg = hvo.getPk_org(); + String pk_group = AppContext.getInstance().getPkGroup(); + // 新数据的实体 + // SaleOrderBVO newBvo = (SaleOrderBVO) bvos[0].clone(); + SaleOrderBVO newBvo = new SaleOrderBVO(); + newBvo.setStatus(VOStatus.NEW); + newBvo.setAttributeValue("pk_group", pk_group); + String cprojectid = bodydata.get("cprojectid") + ""; + if (oneBvo != null && oneBvo.getCprojectid() != null && !"".equals(oneBvo.getCprojectid())) { + cprojectid = oneBvo.getCprojectid(); + } else { + String sql = " select pk_project from bd_project where project_code='" + cprojectid + + "' and nvl(dr,0)=0 "; + cprojectid = (String) new BaseDAO().executeQuery(sql, new ColumnProcessor()); + } + newBvo.setAttributeValue("cprojectid", cprojectid); + // newBvo.setAttributeValue("ctrafficorgvid", pkOrg); + // newBvo.setAttributeValue("csendstockorgvid", pkOrg); + // newBvo.setAttributeValue("csendstordocid", pkOrg); + // newBvo.setAttributeValue("csendstockorgid", pkOrg); + // newBvo.setAttributeValue("carorgid", pkOrg); + // newBvo.setAttributeValue("carorgvid", pkOrg); + // newBvo.setAttributeValue("csettleorgid", pkOrg); + // newBvo.setAttributeValue("csettleorgvid", pkOrg); + + newBvo.setAttributeValue("ctaxcountryid", "CN"); + newBvo.setAttributeValue("crececountryid", "CN"); + newBvo.setAttributeValue("csendcountryid", "CN"); + + newBvo.setAttributeValue("fbuysellflag", 1); + UFDouble nexchangerateBip = getUFDouble_NullAsOne(bodydata.get("nexchangerate")); + newBvo.setNexchangerate(nexchangerateBip); + // 设置物料的关联字段的值 + newBvo.setCmaterialvid(bodydata.get("cmaterialvid") + ""); + UFDouble ntaxrate = BFPubTools.getUFDouble_NullAsZero(bodydata.get("ntaxrate")); + UFDouble nnum = BFPubTools.getUFDouble_NullAsZero(bodydata.get("nnum")); + newBvo.setAttributeValue("ntaxrate", ntaxrate); + newBvo.setAttributeValue("nnum", nnum); + newBvo.setVbdef11(bodydata.getOrDefault("vbdef11", "") + ""); + + UFDouble norigtaxprice = BFPubTools.getUFDouble_NullAsZero(bodydata.get("norigtaxprice")); + newBvo.setAttributeValue("norigtaxprice", norigtaxprice); + UFDouble norigprice = BFPubTools.getUFDouble_NullAsZero(bodydata.get("norigprice")); + newBvo.setAttributeValue("norigprice", norigprice); + setMaterl(newBvo); + newBvo.setCsaleorderbid(null); + + newBvo.setCcurrencyid(ybpk); + String zbbz = newBvo.getCcurrencyid(); + + newBvo.setFtaxtypeflag(1); + + // 折本汇率 + UFDouble nexchangerate = newBvo.getNexchangerate(); + // 含税单价 + UFDouble nqtorigtaxprice = BFPubTools.getUFDouble_NullAsZero(bodydata.get("nqtorigtaxprice")); + newBvo.setAttributeValue("nqtorigtaxprice", nqtorigtaxprice); + // 无税单价 + UFDouble nqtorigprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))); + // 价税合计 + UFDouble norigtaxmny = nqtorigtaxprice.multiply(newBvo.getNqtunitnum()).setScale(2, 4); + newBvo.setNorigtaxmny(norigtaxmny); + // 无税金额 + UFDouble norigmny = nqtorigprice.multiply(newBvo.getNqtunitnum()); + + newBvo.setNorigmny(nc.itf.fi.pub.Currency.getFormaUfValue(ybpk, norigmny)); + // 税额 + newBvo.setNqtorigprice(nqtorigprice.setScale(4, 4)); + + // 无税本币金额单价 + UFDouble taxspric = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))); + + nqtorigprice = nqtorigprice.setScale(4, 4); + // nqtorigtaxnetprc--含税净价 + newBvo.setNqtorigtaxnetprc(nqtorigtaxprice); + // ,nqtorignetprice --无税净价 + newBvo.setNqtorignetprice(nqtorigprice); + String Vqtunitrate = newBvo.getVqtunitrate(); + UFDouble dVqtunitrate = UFDouble.ONE_DBL; + if (Vqtunitrate != null) { + dVqtunitrate = BFPubTools.getUFDouble_NullAsZero(Vqtunitrate.split("/")[0]); + } + // ,norigtaxprice --主含税单价 + + UFDouble wsje = taxspric.multiply(nexchangerate).multiply(newBvo.getNqtunitnum()); + if (ybpk.equals(zbbz) && BFPubTools.getString_TrimAsNull(newBvo.getCqtunitid()) + .equals(BFPubTools.getString_TrimAsNull(newBvo.getCastunitid()))) { + wsje = taxspric.multiply(nexchangerate).multiply(newBvo.getNqtunitnum()); + } + + wsje = nc.itf.fi.pub.Currency.getFormaUfValue(zbbz, wsje); + // 本币无税金额 + newBvo.setNorigtaxprice(nqtorigtaxprice.div(dVqtunitrate).setScale(4, 4)); + // ,norigprice --主无税单价 + newBvo.setNorigprice(nqtorigprice.div(dVqtunitrate).setScale(4, 4)); + // ,norigtaxnetprice --主含税净价 + newBvo.setNorigtaxnetprice(newBvo.getNorigtaxprice()); + // ,norignetprice --主无税净价 + newBvo.setNorignetprice(newBvo.getNorigprice()); + // ncaltaxmny --计税金额 + + // ,nqttaxprice --本币含税单价 + newBvo.setNqttaxprice(nqtorigtaxprice.multiply(nexchangerate)); + // ,nqtprice --本币无税单价 + UFDouble bbwsd = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))) + .multiply(nexchangerate); + newBvo.setNqtprice(bbwsd.setScale(4, 4)); + // , nqttaxnetprice --本币含税净价 + newBvo.setNqttaxnetprice(nqtorigtaxprice.multiply(nexchangerate)); + // ,nqtnetprice --本币无税净价 + UFDouble Nqtnetprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))) + .multiply(nexchangerate); + newBvo.setNqtnetprice(Nqtnetprice.setScale(4, 4)); + // ,ntaxprice --主本币含税单价 , nprice --主本币无税单价 + newBvo.setNtaxprice(nqtorigtaxprice.div(dVqtunitrate).multiply(nexchangerate).setScale(4, 4)); + UFDouble Nprice = nqtorigtaxprice.div(dVqtunitrate).div(UFDouble.ONE_DBL.add(ntaxrate.div(100))) + .multiply(nexchangerate); + newBvo.setNprice(Nprice.setScale(4, 4)); + // ,ntaxnetprice --主本币含税净价 + newBvo.setNtaxnetprice( + nqtorigtaxprice.div(dVqtunitrate).multiply(nexchangerate).setScale(4, 4)); + // ,nnetprice --主本币无税净价 + UFDouble nnetprice = nqtorigtaxprice.div(UFDouble.ONE_DBL.add(ntaxrate.div(100))) + .div(dVqtunitrate); + nnetprice = nnetprice.multiply(nexchangerate).setScale(4, 4); + newBvo.setNnetprice(nnetprice); + // nmny --本币无税金额 + // ntaxmny-- 本币价税合计 + newBvo.setNmny(Currency.getFormaUfValue(zbbz, norigmny.multiply(nexchangerate))); + newBvo.setNtaxmny(nqtorigtaxprice.multiply(nexchangerate).multiply(newBvo.getNqtunitnum()) + .setScale(2, 4)); + newBvo.setNcaltaxmny(wsje); + UFDouble ntax = norigtaxmny.multiply(nexchangerate).sub(wsje); + newBvo.setNtax(ntax.setScale(2, 4)); + newBvo.setCrowno(bodydata.get("crowno") + ""); + // 是否赠品 + String blargessflag = bodydata.getOrDefault("blargessflag", "N") + ""; + newBvo.setBlargessflag(UFBoolean.valueOf(blargessflag)); + bvoList.add(newBvo); + } + } + + SaleOrderBVO[] array = bvoList.toArray(new SaleOrderBVO[0]); + vo.setChildrenVO(array); + } + + // 填充默认值 + new SaleOrderSaveFillValue().setDefValue(copyVOs); + Map> listMap = new HashMap<>(); + for (SaleOrderVO copyVO : copyVOs) { + List newBvoList = new ArrayList<>(); + for (SaleOrderBVO saleOrderBVO : copyVO.getChildrenVO()) { + if (VOStatus.NEW == saleOrderBVO.getStatus()) { + SaleOrderBVO newBvo = (SaleOrderBVO) saleOrderBVO.clone(); + newBvoList.add(newBvo); + } + } + listMap.put(copyVO.getParentVO().getCsaleorderid(), newBvoList); + } + if (!listMap.isEmpty()) { + for (SaleOrderVO oldVO : oldVOs) { + String csaleorderid = oldVO.getParentVO().getCsaleorderid(); + SaleOrderBVO oneBvo = oldVO.getChildrenVO()[0]; + // 折本汇率 + UFDouble nexchangerate = oneBvo.getNexchangerate(); + nexchangerate = getUFDouble_NullAsOne(nexchangerate); + List newBvoList = listMap.get(csaleorderid); + if (newBvoList != null && !newBvoList.isEmpty()) { + // 上边填充默认值的时候取了系统的默认汇率,需要用BIP传的 + for (SaleOrderBVO saleOrderBVO : newBvoList) { + saleOrderBVO.setNexchangerate(nexchangerate); + } + SaleOrderBVO[] bvos = oldVO.getChildrenVO(); + List bvoList = new ArrayList<>(Arrays.asList(bvos)); + // 添加新增行 + bvoList.addAll(newBvoList); + SaleOrderBVO[] array = bvoList.toArray(new SaleOrderBVO[0]); + oldVO.setChildrenVO(array); + } + + } + } + /* + * for (SaleOrderVO combinBillVO : copyVOs) { for (SaleOrderBVO saleOrderBVO : + * combinBillVO.getChildrenVO()) { // 将实体对象转换为JSON字符串 String jsonString = + * JSON.toJSONString(saleOrderBVO); NCCForUAPLogger.debug("jsonString:" + + * jsonString); } } + */ + + } catch (Exception e) { + NCCForUAPLogger.debug("APISaleOrderMaitainImpl-addBvo:" + e.getMessage()); + throw new RuntimeException(e); + } + } + + private UFDouble getUFDouble_NullAsOne(Object value) { + if ((value == null) || (value.toString().trim().equals("")) || (value.toString().trim().equals("~"))) + return UFDouble.ONE_DBL; + if ((value instanceof UFDouble)) + return (UFDouble) value; + if ((value instanceof BigDecimal)) { + return new UFDouble((BigDecimal) value); + } + return new UFDouble(value.toString().trim()); + } } - - -/* Location: D:\home_taikaijt\bipgj\home\yonyou\YonBIP2312\modules\so\META-INF\lib\so_openapi.jar!/nccloud/api/impl/so/m30/APISaleOrderMaitainImpl.class - * Java compiler version: 17 (61.0) - * JD-Core Version: 1.0.7 - */ \ No newline at end of file diff --git a/so/src/public/nccloud/api/so/m30/IAPISaleOrderMaitain.java b/so/src/public/nccloud/api/so/m30/IAPISaleOrderMaitain.java new file mode 100644 index 0000000..5bd671f --- /dev/null +++ b/so/src/public/nccloud/api/so/m30/IAPISaleOrderMaitain.java @@ -0,0 +1,24 @@ +package nccloud.api.so.m30; + +import java.util.List; +import java.util.Map; +import nc.vo.pub.BusinessException; +import nc.vo.so.m30.entity.SaleOrderVO; + +public interface IAPISaleOrderMaitain { + SaleOrderVO[] save(List> paramList) throws BusinessException; + + SaleOrderVO[] update(List> paramList) throws BusinessException; + + void delete(String[] paramArrayOfString) throws BusinessException; + + SaleOrderVO[] sendApprove(String[] paramArrayOfString) throws BusinessException; + + SaleOrderVO[] unSendApprove(String[] paramArrayOfString) throws BusinessException; + + SaleOrderVO[] approve(String[] paramArrayOfString) throws BusinessException; + + SaleOrderVO[] unApprove(String[] paramArrayOfString) throws BusinessException; + + public SaleOrderVO[] save(SaleOrderVO[] vos) throws BusinessException; +}