销售退货单
This commit is contained in:
@@ -0,0 +1,297 @@
|
||||
using MyCode.Project.Domain.Message.Request.KingDee;
|
||||
using MyCode.Project.Domain.Message.Request.KingDee.SaveModel;
|
||||
using MyCode.Project.Domain.Message.Response.KingDee.K3Result;
|
||||
using MyCode.Project.Domain.Message.Response.KingDee.K3Result.Model;
|
||||
using MyCode.Project.Domain.Model;
|
||||
using MyCode.Project.Domain.Repositories;
|
||||
using MyCode.Project.Infrastructure.Common;
|
||||
using MyCode.Project.Infrastructure.Exceptions;
|
||||
using MyCode.Project.OutSideService;
|
||||
using MyCode.Project.Services.IServices;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MyCode.Project.Services.Implementation
|
||||
{
|
||||
/// <summary>
|
||||
/// 销售退货单服务实现
|
||||
/// 功能:处理销售退货单推送到金蝶云星空的业务逻辑
|
||||
/// </summary>
|
||||
public class SalesReturnService : ServiceBase, ISalesReturnService
|
||||
{
|
||||
private const string BILL_TYPE_CODE = "XSTHD01_SYS";
|
||||
private const string OWNER_TYPE = "BD_OwnerOrg";
|
||||
private const string EXCHANGE_TYPE = "HLTX01_SYS";
|
||||
private const string DEFAULT_ORG = "100";
|
||||
private const string DEFAULT_CURRENCY = "PRE001";
|
||||
private const string DEFAULT_UNIT = "Pcs";
|
||||
private const string ROW_TYPE_SERVICE = "Service";
|
||||
private const string RETURN_TYPE_CODE = "THLX01_SYS";
|
||||
private const string TRANSFER_BIZ_TYPE = "OverOrgSal";
|
||||
private const string FORM_ID = "SAL_RETURNSTOCK";
|
||||
private const int TRADE_TYPE_RETURN_ORDER = 8;
|
||||
|
||||
private IPushKingDeeOrderRepository _pushKingDeeOrderRepository;
|
||||
private IPushKingDeeOrderItemRepository _pushKingDeeOrderItemRepository;
|
||||
private IYTKJTShopParameterRepository _yTKJTShopParameterRepository;
|
||||
private IKingDeeService _kingDeeService;
|
||||
|
||||
public SalesReturnService(
|
||||
IPushKingDeeOrderRepository pushKingDeeOrderRepository,
|
||||
IPushKingDeeOrderItemRepository pushKingDeeOrderItemRepository,
|
||||
IYTKJTShopParameterRepository yTKJTShopParameterRepository,
|
||||
IKingDeeService kingDeeService)
|
||||
{
|
||||
_pushKingDeeOrderRepository = pushKingDeeOrderRepository;
|
||||
_pushKingDeeOrderItemRepository = pushKingDeeOrderItemRepository;
|
||||
_yTKJTShopParameterRepository = yTKJTShopParameterRepository;
|
||||
_kingDeeService = kingDeeService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 推送销售退货单到金蝶云星空
|
||||
/// </summary>
|
||||
/// <param name="id">PushKingDeeOrder表的主键ID(Guid格式的字符串)</param>
|
||||
/// <returns>返回金蝶API的响应结果(JSON格式)</returns>
|
||||
public string PushSalesReturnToKingDee(string id)
|
||||
{
|
||||
string result2 = "";
|
||||
var orderHead = _pushKingDeeOrderRepository.Queryable().Where(t => id == t.Id.ToString()).First();
|
||||
if (orderHead.Status != 0)
|
||||
{
|
||||
throw new BaseException("单据已经下推.");
|
||||
}
|
||||
|
||||
if (orderHead.TradeType != TRADE_TYPE_RETURN_ORDER)
|
||||
{
|
||||
throw new BaseException($"订单类型 {orderHead.TradeType} 不是销售退货单(TradeType应为8),无法推送");
|
||||
}
|
||||
|
||||
var param = _yTKJTShopParameterRepository
|
||||
.Queryable()
|
||||
.Where(t => t.FSHOPCODE == orderHead.ShopCode)
|
||||
.Where(t => t.FDOCUMENTSTATUS == "C")
|
||||
.First();
|
||||
if (param is null)
|
||||
{
|
||||
throw new BaseException($"门店编号:{orderHead.ShopCode},门店名称:{orderHead.ShopName},没有找到对应的门店参数 ");
|
||||
}
|
||||
|
||||
if (param.FSYNCHRONIZEKINGDEE == "1")
|
||||
{
|
||||
var response = PushKingdeeSalesReturn(orderHead, param);
|
||||
result2 = JsonHelper.ToJson(response);
|
||||
if (response.IsSuccess)
|
||||
{
|
||||
orderHead.Status = 2;
|
||||
_pushKingDeeOrderRepository.Update(orderHead);
|
||||
}
|
||||
}
|
||||
|
||||
return result2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 推送到金蝶云星空
|
||||
/// </summary>
|
||||
/// <param name="orderHead">源单信息</param>
|
||||
/// <param name="param">门店配置</param>
|
||||
/// <returns></returns>
|
||||
private K3CloudResponseStatus PushKingdeeSalesReturn(PushKingDeeOrder orderHead, YTKJTShopParameter param)
|
||||
{
|
||||
var itemList = _pushKingDeeOrderItemRepository
|
||||
.Queryable()
|
||||
.Where(t => orderHead.Id == t.PushKingDeeOrderId)
|
||||
.ToList();
|
||||
|
||||
if (itemList == null || itemList.Count == 0)
|
||||
{
|
||||
throw new BaseException($"销售退货单 {orderHead.Sheet} 没有找到明细数据,无法推送");
|
||||
}
|
||||
|
||||
string orgId = param.FSALEORGID?.ToString() ?? DEFAULT_ORG;
|
||||
|
||||
string warehouseCode = orderHead.WarehouseCode ?? param.FWAREHOUSECODE ?? "";
|
||||
|
||||
if (string.IsNullOrEmpty(warehouseCode))
|
||||
{
|
||||
throw new BaseException($"销售退货单 {orderHead.Sheet} 仓库编码为空,无法推送");
|
||||
}
|
||||
|
||||
var entryList = itemList.Select(n => BuildEntryItem(n, param, orgId, warehouseCode, orderHead.ConsignTime)).ToList();
|
||||
|
||||
if (entryList.Count == 0)
|
||||
{
|
||||
throw new BaseException($"销售退货单 {orderHead.Sheet} 明细数据转换失败,无法推送");
|
||||
}
|
||||
|
||||
var model = BuildMainModel(orderHead, param, orgId, entryList);
|
||||
|
||||
BillSave billSave = new BillSave()
|
||||
{
|
||||
Model = model,
|
||||
IsAutoSubmitAndAudit = true,
|
||||
};
|
||||
|
||||
var responseStatus = _kingDeeService.Save(FORM_ID, billSave);
|
||||
return responseStatus;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构建明细行数据
|
||||
/// </summary>
|
||||
private FSalesReturnEntryItem BuildEntryItem(PushKingDeeOrderItem item, YTKJTShopParameter param, string orgId, string warehouseCode, DateTime? consignTime)
|
||||
{
|
||||
string rowType = ROW_TYPE_SERVICE;
|
||||
|
||||
decimal realQty = Math.Abs(item.SellCount);
|
||||
|
||||
decimal taxRate = param.FTAXRATE;
|
||||
decimal taxPrice = item.SellCount != 0 ? Math.Round(Math.Abs(item.DivideSellTotal) / item.SellCount, 10) : 0;
|
||||
|
||||
decimal price = taxRate > 0 && taxPrice > 0 ? Math.Round(taxPrice / (1 + taxRate / 100), 10) : taxPrice;
|
||||
|
||||
string deliveryDate = consignTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
string inStockDate = consignTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
return new FSalesReturnEntryItem()
|
||||
{
|
||||
FRowType = rowType,
|
||||
FMaterialId = new FMaterialId()
|
||||
{
|
||||
FNumber = item.Barcode ?? ""
|
||||
},
|
||||
FUnitID = new FUnitID()
|
||||
{
|
||||
FNumber = item.Unit ?? DEFAULT_UNIT
|
||||
},
|
||||
FInventoryQty = 0,
|
||||
FRealQty = realQty,
|
||||
FPrice = price,
|
||||
FTaxPrice = taxPrice,
|
||||
FIsFree = false,
|
||||
FEntryTaxRate = param.FTAXRATE,
|
||||
FReturnType = new FReturnType()
|
||||
{
|
||||
FNumber = RETURN_TYPE_CODE
|
||||
},
|
||||
FOwnerTypeId = OWNER_TYPE,
|
||||
FOwnerId = new FOwnerId()
|
||||
{
|
||||
FNumber = orgId
|
||||
},
|
||||
FDeliveryDate = deliveryDate,
|
||||
FDiscountRate = 0,
|
||||
FPriceDiscount = 0,
|
||||
FAuxUnitQty = 0,
|
||||
FExtAuxUnitQty = 0,
|
||||
FSrcBillTypeID = "",
|
||||
FSrcBillNo = "",
|
||||
FISCONSUMESUM = "0",
|
||||
FSalUnitID = new FSalUnitID()
|
||||
{
|
||||
FNumber = item.Unit ?? DEFAULT_UNIT
|
||||
},
|
||||
FSalUnitQty = realQty,
|
||||
FSalBaseQty = realQty,
|
||||
FPriceBaseQty = realQty,
|
||||
FIsOverLegalOrg = false,
|
||||
FSOEntryId = 0,
|
||||
FARNOTJOINQTY = realQty,
|
||||
FIsReturnCheck = false,
|
||||
FSettleBySon = false,
|
||||
FBOMEntryId = 0,
|
||||
FMaterialID_Sal = new FMaterialID_Sal()
|
||||
{
|
||||
FNUMBER = item.Barcode ?? ""
|
||||
},
|
||||
FMrbEntryId = 0,
|
||||
FINSTOCKDATE = inStockDate,
|
||||
FVmiBusinessStatus = false
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构建主表数据
|
||||
/// </summary>
|
||||
private SalesReturnModel BuildMainModel(PushKingDeeOrder orderHead, YTKJTShopParameter param, string orgId, List<FSalesReturnEntryItem> entryList)
|
||||
{
|
||||
string dateStr = orderHead.ConsignTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
string customerId = param.FSALESCUSTOMERS > 0 ? param.FSALESCUSTOMERS.ToString() : "";
|
||||
string salesManId = param.FSALER.HasValue && param.FSALER.Value > 0 ? param.FSALER.Value.ToString() : "";
|
||||
|
||||
return new SalesReturnModel()
|
||||
{
|
||||
FID = 0,
|
||||
FBillTypeID = new FBillTypeID()
|
||||
{
|
||||
FNUMBER = BILL_TYPE_CODE
|
||||
},
|
||||
FDate = dateStr,
|
||||
FSaleOrgId = new FSaleOrgId()
|
||||
{
|
||||
FNumber = orgId
|
||||
},
|
||||
FRetcustId = new FRetcustId()
|
||||
{
|
||||
FNumber = customerId
|
||||
},
|
||||
FSaledeptid = new FSaledeptid()
|
||||
{
|
||||
FNumber = "02"
|
||||
},
|
||||
FTransferBizType = new FTransferBizType()
|
||||
{
|
||||
FNumber = TRANSFER_BIZ_TYPE
|
||||
},
|
||||
FSalesManId = new FSalesManId()
|
||||
{
|
||||
FNumber = salesManId
|
||||
},
|
||||
FStockOrgId = new FStockOrgId()
|
||||
{
|
||||
FNumber = orgId
|
||||
},
|
||||
FReceiveCustId = new FReceiveCustId()
|
||||
{
|
||||
FNumber = customerId
|
||||
},
|
||||
FSettleCustId = new FSettleCustId()
|
||||
{
|
||||
FNumber = customerId
|
||||
},
|
||||
FPayCustId = new FPayCustId()
|
||||
{
|
||||
FNumber = customerId
|
||||
},
|
||||
FOwnerTypeIdHead = OWNER_TYPE,
|
||||
FCDateOffsetValue = 0,
|
||||
FIsTotalServiceOrCost = false,
|
||||
FAllRefundNoGoods = false,
|
||||
SubHeadEntity = new FSubHeadEntityReturn()
|
||||
{
|
||||
FSettleCurrId = new FSettleCurrId()
|
||||
{
|
||||
FNumber = DEFAULT_CURRENCY
|
||||
},
|
||||
FSettleOrgId = new FSettleOrgId()
|
||||
{
|
||||
FNumber = orgId
|
||||
},
|
||||
FLocalCurrId = new FLocalCurrId()
|
||||
{
|
||||
FNumber = DEFAULT_CURRENCY
|
||||
},
|
||||
FExchangeTypeId = new FExchangeTypeId()
|
||||
{
|
||||
FNumber = EXCHANGE_TYPE
|
||||
},
|
||||
FExchangeRate = 1m
|
||||
},
|
||||
FEntity = entryList
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user