using Kingdee.CDP.WebApi.SDK;
using MyCode.Project.Domain.Message.Act.PurchaseOrder;
using MyCode.Project.Domain.Message.Common;
using MyCode.Project.Domain.Message.Request.InvoiceOrder;
using MyCode.Project.Domain.Message.Request.KingDee;
using MyCode.Project.Domain.Message.Request.PurchaseOrder;
using MyCode.Project.Domain.Message.Response.InvoiceOrder;
using MyCode.Project.Domain.Message.Response.PurchaseOrder;
using MyCode.Project.Domain.Message.Response.User;
using MyCode.Project.Domain.Model;
using MyCode.Project.Domain.Repositories;
using MyCode.Project.Infrastructure.Common;
using MyCode.Project.Infrastructure.Exceptions;
using MyCode.Project.Infrastructure.Extensions;
using MyCode.Project.OutSideService;
using MyCode.Project.Repositories;
using MyCode.Project.Repositories.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
namespace MyCode.Project.Services.Implementation
{
///
/// 发货通知单 相关服务
///
public class InvoiceOrderService : ServiceBase , IInvoiceOrderService
{
private IWorkProcessService _workProcessService;
private IKingDeeService _kingDeeService;
private IPurchaseOrderRepository _purchaseOrderRepository;
private IPurchaseOrderItemRepository _purchaseOrderItemRepository;
private IInvoiceOrderItemRepository _invoiceOrderItemRepository;
private IInvoiceOrderRepository _invoiceOrderRepository;
private ITiaoMaRepository _tiaoMaRepository;
public InvoiceOrderService(IPurchaseOrderRepository purchaseOrderRepository
, IPurchaseOrderItemRepository purchaseOrderItemRepository
, IInvoiceOrderItemRepository invoiceOrderItemRepository
, IInvoiceOrderRepository invoiceOrderRepository
, ITiaoMaRepository tiaoMaRepository
, IWorkProcessService workProcessService
, IKingDeeService kingDeeService)
{
_purchaseOrderItemRepository = purchaseOrderItemRepository;
_purchaseOrderRepository = purchaseOrderRepository;
_invoiceOrderItemRepository = invoiceOrderItemRepository;
_invoiceOrderRepository = invoiceOrderRepository;
_tiaoMaRepository = tiaoMaRepository;
_workProcessService = workProcessService;
_kingDeeService = kingDeeService;
}
#region GetPageList(发货通知单分页列表)
///
/// 发货通知单分页列表
///
///
///
///
public PageResult GetPageList(PagedSearch pagedSearch, LoginInfo loginInfo)
{
return _invoiceOrderRepository.GetPageList(pagedSearch, loginInfo.SupplierId, loginInfo.FPurchaseOrgId);
}
#endregion
#region DeleteOrder(删除某个发货订单)
///
/// 删除某个发货订单
///
///
///
///
///
[TransactionCallHandler]
public void DeleteOrder(IdAct act, LoginInfo loginInfo)
{
var orderItems = _invoiceOrderItemRepository.Queryable().Where(t => t.Id == act.Id).ToList();
Guid pid = orderItems.Select(t => t.InvoiceOrderId.Value).FirstOrDefault();
var orderHead = _invoiceOrderRepository.Queryable().Where(t => t.SupplierId == loginInfo.SupplierId && t.Id == pid).First();
var ids = orderItems.Select(t => t.Id).ToList();
if (orderHead == null)
{
throw new BaseException("订单数据不正确");
}
orderItems = _invoiceOrderItemRepository.Queryable().Where(t => t.InvoiceOrderId == orderHead.Id).ToList();
var OrderItemIds = orderItems.Select(t => t.PurchaseOrderItemId).ToList();
BillDelete billDelete = new BillDelete();
billDelete.Numbers = orderHead.FBillNo;
try
{
var result = _kingDeeService.Delete("PUR_ReceiveBill", billDelete);
//LogHelper.Info(result);
var purchaseItem = _purchaseOrderItemRepository.Queryable().Where(t => OrderItemIds.Contains(t.Id)).ToList();
purchaseItem.ForEach(t =>
{
var temp = orderItems.FirstOrDefault(h => h.PurchaseOrderItemId == t.Id);
if (temp != null)
{
t.FRemainReceiveQty = t.FRemainReceiveQty + temp.Qty.SafeValue();
t.FReceiveQty = t.FReceiveQty - temp.Qty.SafeValue();
}
});
_purchaseOrderItemRepository.Update(purchaseItem);
_invoiceOrderItemRepository.Delete(t => t.InvoiceOrderId == orderHead.Id);
_invoiceOrderRepository.Delete(t => t.Id == orderHead.Id);
_tiaoMaRepository.Delete(t => ids.Contains(t.InvoceOrderItemId.Value));
}
catch (Exception ex)
{
throw new BaseException($"删除失败,原因是{ex.Message}");
}
}
#endregion
#region
///
/// 根据收料通知单号查列表
///
///
public string QueryList(string FbillNo, string FSupplierId)
{
BillQuery queryParam = new BillQuery();
queryParam = new BillQuery()
{
FormId = "PUR_ReceiveBill",
FieldKeys = $@"FID,FBillNo,FMaterialId.FNumber,FSupplierLot,FLot.fname,FSUPDELQTY,FDetailEntity_FEntryId",
TopRowCount = 100000,
Limit = 20000,
StartRow = 0,
};
FilterList filterString = new FilterList(false);
//FilterItem FDocumentStatusItem = new FilterItem("FBillNo", "67", "C", "0");
//FilterItem FCancelStatusItem = new FilterItem("FCancelStatus", "105", "A", "0");
FilterItem FCancelStatusItem1 = new FilterItem { FieldName = "FBillNo", Compare = "67", Value = FbillNo, Logic = "0" };
FilterItem FCancelStatusItem2 = new FilterItem { FieldName = "FSupplierId.FNumber", Compare = "67", Value = FSupplierId, Logic = "0" };
//FilterItem FCancelStatusItem3 = new FilterItem { FieldName = "FSupplierId.FNumber", Compare = "67", Value = FSupplierId.ToString(), Logic = "0" };
//FilterItem FCancelStatusItem4 = new FilterItem { FieldName = "FBaseDeliveryMaxQty", Compare = "21", Value = "0", Logic = "0" };
filterString.AddFilterItem(FCancelStatusItem1).AddFilterItem(FCancelStatusItem2);
////.AddFilterItem(FCancelStatusItem4)
//.AddFilterItem(FCancelStatusItem2).AddFilterItem(FCancelStatusItem3);
queryParam.FilterString = filterString.GetFilterString();
var result = _kingDeeService.QueryList(queryParam);
return result;
}
#endregion
#region SetMSSSupplierLot(查询金蝶的美塞斯批号)
///
/// 写入金蝶的美塞斯批号
///
///
///
public void SetMSSSupplierLot(string sheet)
{
var sheetOrder = _invoiceOrderRepository.Queryable().Where(t => t.Sheet == sheet).First();
if (sheetOrder == null)
return;
var result = QueryList(sheetOrder.FBillNo, sheetOrder.SupplierId);
LogHelper.Info(sheet + "的收料订单数据");
LogHelper.Info(result);
/**
* *
* [{"FID":100160,"FBillNo":"CGSL000148","FMaterialId.FNumber":"89833-001","FSupplierLot":"AA5","FLot.fname":"A1","FSUPDELQTY":100.0000000000,"FDetailEntity.FEntryId":100299}
* ,{"FID":100160,"FBillNo":"CGSL000148","FMaterialId.FNumber":"89834-002","FSupplierLot":"aa6","FLot.fname":"A2","FSUPDELQTY":100.0000000000,"FDetailEntity.FEntryId":100300}]
*
* */
List data = JsonHelper.ToObject>(result);
//List data = System.Text.Json.JsonSerializer.Deserialize>(result);
if (data.Count > 0)
{
int fid = 0;
Guid ida = Guid.Empty;
List fids = data.Select(k => int.Parse((string)k["FID"])).ToList().Distinct().ToList();
var oldOrderItemList = _invoiceOrderItemRepository.Queryable().Where(g => g.InvoiceOrderId == sheetOrder.Id).ToList();
List entityIdList = new List();
foreach (var item in data)
{
int fido = int.Parse((string)item["FID"]);
string FSupplierLot = (string)item["FSupplierLot"];
if (string.IsNullOrWhiteSpace(FSupplierLot))
FSupplierLot = FSupplierLot.Trim();
else
FSupplierLot = "";
var oldItems = oldOrderItemList.Where(k => k.MaterialCode == (string)item["FMaterialId.FNumber"] && k.Qty == (decimal)item["FSUPDELQTY"]
).ToList();
var oldItem = oldItems.FirstOrDefault();
if (oldItems.Count > 1)
{
oldItem= oldItems.FirstOrDefault(h=>h.FSupplierLot== FSupplierLot);
}
if (oldItem == null)
{
//throw new BaseException((string)item["FMaterialId.FNumber"] + (decimal)item["FSUPDELQTY"] + item["FSupplierLot"] + "找不到对应的明细");
continue;
}
else
{
oldItem.Fid = item["FID"];
oldItem.MSSSupplierLot = item["FLot.fname"];
oldItem.EntityId = item["FDetailEntity.FEntryId"];
}
}
_invoiceOrderItemRepository.Update(oldOrderItemList);
//物料编码,数量,收料通知单号,供应商批号,key
//AddTiaoMa addTiao = new AddTiaoMa();
//List addTiaoMas = new List();
//addTiaoMas = oldOrderItemList.Select(t => new AddTiaoMa
//{
// FBarCode = $@"{t.MaterialCode}*{t.Qty.Value.ToString("F2")}*{sheetOrder.FBillNo}*{t.FSupplierLot}",
// FBarCodeRule = "03",
// FBillCode = sheetOrder.FBillNo,
// FSupplierLot = t.FSupplierLot == null ? "" : t.FSupplierLot,
// FMaterialId = t.MaterialCode,
// FLot=t.MSSSupplierLot,
// FQty = t.Qty.Value.ToString()
//}).ToList();
//_workProcessService.Add(this.MerchantId, "AddTiaoMa", "生成条码档案", JsonHelper.ToJson(addTiaoMas), 5);
}
}
#endregion
#region SetBaoShuTiaoMa(根据每包数量得到全部包裹打印条码)
///
/// 根据每包数量得到全部包裹打印条码
///
///
///
///
[TransactionCallHandler]
public TiaoMaResp SetBaoShuTiaoMa(TiaoMaAct act)
{
if (act.CuseQty <= 0)
{
throw new BaseException("每包数量必须大于0");
}
//_tiaoMaRepository.Delete(t => t.InvoceOrderItemId == act.Id);
var orderItem = _invoiceOrderItemRepository.Queryable().Where(t => t.Id == act.Id).First();
if (orderItem == null)
{
throw new BaseException("不正确的ID值");
}
var order = _invoiceOrderRepository.Queryable().Where(t => t.Id == orderItem.InvoiceOrderId).First();
if (order == null)
{
throw new BaseException("不正确的ID数据");
}
List addList = new List();
if (orderItem.CuseQty == act.CuseQty)
{
var barcodeList = _tiaoMaRepository.Queryable().Where(t => t.InvoceOrderItemId == orderItem.Id).ToList();
addList =AutoMapperHelper.AutoMappToList( barcodeList);
addList.ForEach(t =>
{
t.SupplierId = order.SupplierId;
t.SupplierName = order.SupplierName;
});
}
var allBarcodeList = _tiaoMaRepository.Queryable().Where(t => t.FBillNo == order.FBillNo).Select(t => t.FBarCode).ToList().Distinct().ToList();
if (addList.Count==0)
{
_tiaoMaRepository.Delete(t=>t.InvoceOrderItemId== orderItem.Id);
orderItem.CuseQty = act.CuseQty;
_invoiceOrderItemRepository.Update(orderItem);
decimal zongQty = orderItem.Qty.SafeValue();
int yu = (int)Math.Ceiling(zongQty % act.CuseQty);
int baoShu = 0;
baoShu = (int)(zongQty / act.CuseQty);
// 条码 :物料编码,数量,收料通知单号,供应商批号,key
for (int i = 0; i < baoShu; i++)
{
string FBarCode = $@"{orderItem.MaterialCode}*{act.CuseQty.ToString("F2")}*{orderItem.FBillNo}*{orderItem.FSupplierLot}";
TiaoMaList tiaoMa = new TiaoMaList();
tiaoMa = AutoMapperHelper.AutoMappToSingle(orderItem);
tiaoMa.Id = Guid.NewGuid();
tiaoMa.FBarCode = FBarCode;
tiaoMa.Qty = act.CuseQty;
tiaoMa.SortNum = i + 1;
tiaoMa.PurchaseBillNo = orderItem.FBillNo;
tiaoMa.FBillNo = order.FBillNo;
tiaoMa.SupplierId = order.SupplierId;
tiaoMa.SupplierName = order.SupplierName;
tiaoMa.InvoceOrderItemId = orderItem.Id;
addList.Add(tiaoMa);
}
if (yu > 0)
{
string FBarCode = $@"{orderItem.MaterialCode}*{decimal.Parse(yu.ToString()).ToString("F2")}*{orderItem.FBillNo}*{orderItem.FSupplierLot}";
TiaoMaList tiaoMa = new TiaoMaList();
tiaoMa = AutoMapperHelper.AutoMappToSingle(orderItem);
tiaoMa.Id = Guid.NewGuid();
tiaoMa.FBarCode = FBarCode;
tiaoMa.Qty = yu;
tiaoMa.SortNum = baoShu + 1;
tiaoMa.PurchaseBillNo = orderItem.FBillNo;
tiaoMa.FBillNo = order.FBillNo;
tiaoMa.SupplierId = order.SupplierId;
tiaoMa.SupplierName = order.SupplierName;
tiaoMa.InvoceOrderItemId = orderItem.Id;
addList.Add(tiaoMa);
}
if (addList.Count > 0)
{
var addtemp = AutoMapperHelper.AutoMappToList(addList);
_tiaoMaRepository.Add(addtemp);
}
List addTiaoMas = new List();
var addList2 = addList.Where(t => !allBarcodeList.Contains(t.FBarCode)).ToList();
addTiaoMas = addList2.Select(t => new AddTiaoMa
{
FBarCode = t.FBarCode,
FBarCodeRule = "01",
FBillCode = t.FBillNo,
FSupplierLot = t.FSupplierLot == null ? "" : t.FSupplierLot,
FMaterialId = t.MaterialCode,
FLot = t.MSSSupplierLot,
FQty = t.Qty.Value.ToString()
}).ToList();
_workProcessService.Add(this.MerchantId, "AddTiaoMa", "生成条码档案", JsonHelper.ToJson(addTiaoMas), 5);
}
TiaoMaResp result = new TiaoMaResp();
result.Id = orderItem.Id;
result.BarCodeList = addList;
return result;
}
#endregion
}
}