211 lines
8.7 KiB
C#
211 lines
8.7 KiB
C#
using Gatedge.K3Cloud.Utils;
|
|
using Gatedge.K3Cloud.Utils.Exceptions;
|
|
using Gatedge.K3Cloud.Utils.Model.K3Request;
|
|
using Gatedge.K3Cloud.Utils.Model.K3Result.Model;
|
|
using Gatedge.ScanCode.Models.K3Request.SaveModel;
|
|
using Gatedge.ScanCode.Models.Vo;
|
|
using Gatedge.ScanCode.Services.IServices;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Gatedge.ScanCode.Services
|
|
{
|
|
/// <summary>
|
|
/// 采购订单服务
|
|
/// </summary>
|
|
public class PurchaseOrderService : IPurchaseOrderService
|
|
{
|
|
|
|
private readonly string _FormName = "采购订单";
|
|
private readonly string _FormId = "PUR_PurchaseOrder";
|
|
private readonly string _TargetFormId = "STK_InStock";
|
|
private readonly string _ConvertRuleId = "c423ca95-0184-4e10-9176-86f9a544b931";
|
|
|
|
/// <summary>
|
|
/// 金蝶云星空工具类
|
|
/// </summary>
|
|
private readonly K3CloudApiUtils _utils;
|
|
|
|
/// <summary>
|
|
/// 构造方法
|
|
/// </summary>
|
|
public PurchaseOrderService(K3CloudApiUtils utils)
|
|
{
|
|
_utils = utils;
|
|
}
|
|
|
|
class PushCustomParam
|
|
{
|
|
public decimal InStockQty { get; set; }
|
|
public string InStockId { get; set; }
|
|
public DateTime FStockDate { get; set; }
|
|
public DateTime FExpirationDate { get; set; }
|
|
public int FLot_Id { get; set; }
|
|
public string FLot_Text { get; set; }
|
|
}
|
|
/// <summary>
|
|
/// 下推采购入库单
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public K3CloudResponseStatus PushPurInStock(int barRecordId)
|
|
{
|
|
IBarRecordService barRecordService = new BarRecordService(_utils);
|
|
IStkInStockService stkInStockService = new StkInStockService(_utils);
|
|
|
|
var barRecordViewParam = new View()
|
|
{
|
|
Id = barRecordId.ToString(),
|
|
};
|
|
var barRecord = barRecordService.View(barRecordViewParam);
|
|
// 单据状态检查
|
|
if (barRecord.DocumentStatus == "B")
|
|
{
|
|
throw new Exception("该扫描结果已经被提交,不允许重复提交");
|
|
}
|
|
if (barRecord.DocumentStatus == "C")
|
|
{
|
|
throw new Exception("该扫描结果已经被提交,不允许重复提交");
|
|
}
|
|
// 提交单据,不允许重复提交,相当于上锁
|
|
var submitBarRecor = new Submit()
|
|
{
|
|
Ids = barRecordId.ToString(),
|
|
};
|
|
var submitBarRecorResult = barRecordService.Submit(submitBarRecor);
|
|
// 如果提交不成功
|
|
if (!submitBarRecorResult.IsSuccess)
|
|
{
|
|
throw new Exception("该扫描结果已经被提交,不允许重复提交");
|
|
}
|
|
// 如果提交成功,开始下推生产入库单
|
|
var barEntityList = barRecord.FBarEntity;
|
|
// 构造下推参数
|
|
Push billPush = new Push();
|
|
billPush.EntryIds = string.Join(",", barEntityList.Select(n => n.FSrcEntryId));
|
|
billPush.CustomParams = new Dictionary<string, object>();
|
|
billPush.CustomParams.Add("IsConvertByScanCode", true); // 是否由条码下推
|
|
billPush.CustomParams.Add("FBarRecordNo", barRecord.BillNo); // 是否由条码下推
|
|
|
|
billPush.RuleId = _ConvertRuleId; // 注塑/装配车间排产计划专用转换规则
|
|
billPush.TargetFormId = _TargetFormId; // 目标单据类型:采购入库单
|
|
billPush.IsEnableDefaultRule = false; // 不启用默认规则
|
|
billPush.IsDraftWhenSaveFail = false; // 保存失败,不自动暂存
|
|
var paramList = new Dictionary<string, PushCustomParam>();
|
|
foreach (var item in barEntityList)
|
|
{
|
|
var pushCustomParams = new PushCustomParam()
|
|
{
|
|
InStockQty = item.FBarQty ?? 0, // 下推数量
|
|
InStockId = item.FBarStockId_Id?.ToString() ?? string.Empty, // 入库仓库
|
|
FStockDate = item.FStockDate ?? DateTime.MinValue, // 入库日期
|
|
FExpirationDate = item.FExpirationDate ?? DateTime.MinValue, // 有效期至
|
|
FLot_Id = item.FLot_Id ?? 0, // 批号Id
|
|
FLot_Text = item.FLot_Text ?? string.Empty // 批号文本
|
|
};
|
|
paramList.Add(item.FSrcEntryId ?? "0", pushCustomParams);
|
|
}
|
|
billPush.CustomParams.Add("PushCustomParams", JsonConvert.SerializeObject(paramList));
|
|
|
|
|
|
// 执行下推
|
|
var pushResp = _utils.Push(_FormId, billPush);
|
|
// 如果失败,保存错误信息到扫描记录
|
|
if (pushResp?.IsSuccess != true)
|
|
{
|
|
var errorInfo = string.Join("\r\n", pushResp.Errors.Select(n => n.Message));
|
|
barRecordService.SaveErrorInfo(barRecordId, errorInfo);
|
|
// 撤销提交的栈板单据
|
|
CancelAssignBarRecord(barRecordId);
|
|
return pushResp;
|
|
}
|
|
|
|
//如果都成功,将入库单提交
|
|
if (pushResp?.SuccessEntitys.Count == 0)
|
|
{
|
|
throw new Exception("下推出现异常");
|
|
}
|
|
var inStockBillIds = pushResp?.SuccessEntitys?.Select(n => n.Id.ToString()).ToList();
|
|
Submit stkInStockSubmitParam = new Submit()
|
|
{
|
|
Ids = string.Join(',', inStockBillIds)
|
|
};
|
|
var submitResult = stkInStockService.Submit(stkInStockSubmitParam);
|
|
// 如果提交不成功,删除本次入库的单据,并返回结果
|
|
if (!submitResult.IsSuccess)
|
|
{
|
|
var errorInfo = string.Join("\r\n", submitResult.Errors.Select(n => n.Message));
|
|
barRecordService.SaveErrorInfo(barRecordId, errorInfo);
|
|
// 撤销提交的栈板单据
|
|
CancelAssignBarRecord(barRecordId);
|
|
// 撤销并删除所有下推数据
|
|
CancelAssignDeletePrdInStock(inStockBillIds);
|
|
return submitResult;
|
|
}
|
|
// 如果提交成功,审核入库单
|
|
Audit stkInStockAuditParam = new Audit()
|
|
{
|
|
Ids = string.Join(',', inStockBillIds)
|
|
};
|
|
var auditResult = stkInStockService.Audit(stkInStockAuditParam);
|
|
// 如果审核不成功,删除本次入库的单据,并返回结果
|
|
if (!auditResult.IsSuccess)
|
|
{
|
|
var errorInfo = string.Join("\r\n", auditResult.Errors.Select(n => n.Message));
|
|
barRecordService.SaveErrorInfo(barRecordId, errorInfo);
|
|
// 撤销提交的栈板单据
|
|
CancelAssignBarRecord(barRecordId);
|
|
// 撤销并删除所有下推数据
|
|
CancelAssignDeletePrdInStock(inStockBillIds);
|
|
return auditResult;
|
|
}
|
|
|
|
// 如果审核成功,更新栈板单据的结果信息
|
|
var result = barRecordService.SubmitSuccess(barRecordId, auditResult.SuccessEntitys.Select(n => new BarRecordSave.ResultEntry
|
|
{
|
|
FResultBillNo = n.Number,
|
|
FResultId = n.Id.ToString(),
|
|
FResultBillId = new Models.K3Request.BaseData.FormType()
|
|
{
|
|
FID = _TargetFormId
|
|
}
|
|
}));
|
|
return auditResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 撤销并删除所有下推数据
|
|
/// </summary>
|
|
/// <param name="deleteIds"></param>
|
|
private void CancelAssignDeletePrdInStock(List<string> deleteIds)
|
|
{
|
|
IStkInStockService stkInStockService = new StkInStockService(_utils);
|
|
CancelAssign cancelAssign = new CancelAssign()
|
|
{
|
|
Ids = string.Join(',', deleteIds.Select(n => n.ToString()))
|
|
};
|
|
stkInStockService.CancelAssign(cancelAssign);
|
|
Delete stkInStockDeleteParam = new Delete()
|
|
{
|
|
Ids = string.Join(',', deleteIds.Select(n => n.ToString()))
|
|
};
|
|
stkInStockService.Delete(stkInStockDeleteParam);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 撤销栈板单据提交
|
|
/// </summary>
|
|
/// <param name="barRecordId"></param>
|
|
private void CancelAssignBarRecord(int barRecordId)
|
|
{
|
|
IBarRecordService barRecordService = new BarRecordService(_utils);
|
|
CancelAssign cancelAssignBarRecord = new CancelAssign()
|
|
{
|
|
Ids = barRecordId.ToString()
|
|
};
|
|
// 撤销
|
|
var cancelAssignBarRecordResult = barRecordService.CancelAssign(cancelAssignBarRecord);
|
|
}
|
|
}
|
|
}
|