н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs
@@ -5,12 +5,18 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.SquareCabin;
using WIDESEA_IBasicService;
using WIDESEA_ISquareCabinServices;
using WIDESEA_IWMsInfoServices;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
@@ -18,12 +24,26 @@
namespace WIDESEA_SquareCabinServices
{
    public class DeliveryOrderServices : ServiceBase<Dt_DeliveryOrder, IRepository<Dt_DeliveryOrder>>, IDeliveryOrderServices
    public partial class DeliveryOrderServices : ServiceBase<Dt_DeliveryOrder, IRepository<Dt_DeliveryOrder>>, IDeliveryOrderServices
    {
        static string SearchDate = "";
        public DeliveryOrderServices(IRepository<Dt_DeliveryOrder> BaseDal) : base(BaseDal)
        private readonly IBasicService _basicService;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IInventory_BatchServices _inventory_BatchServices;
        private readonly IInventoryInfoService _inventoryInfoService;
        private readonly IDeliveryOrderDetailServices _deliveryOrderDetailServices;
        private readonly ISupplyTaskService _supplyTaskService;
        private readonly ICabinOrderServices _cabinOrderServices;
        public IRepository<Dt_DeliveryOrder> Repository => BaseDal;
        public DeliveryOrderServices(IRepository<Dt_DeliveryOrder> BaseDal, IBasicService basicService, IUnitOfWorkManage unitOfWorkManage, IInventory_BatchServices inventory_BatchServices, IInventoryInfoService inventoryInfoService, IDeliveryOrderDetailServices deliveryOrderDetailServices, ISupplyTaskService supplyTaskService, ICabinOrderServices cabinOrderServices) : base(BaseDal)
        {
            _basicService = basicService;
            _unitOfWorkManage = unitOfWorkManage;
            _deliveryOrderDetailServices = deliveryOrderDetailServices;
            _supplyTaskService = supplyTaskService;
            _inventory_BatchServices = inventory_BatchServices;
            _inventoryInfoService = inventoryInfoService;
            _cabinOrderServices = cabinOrderServices;
        }
        /// <summary>
@@ -282,7 +302,7 @@
                                    };
                                    entityOrder.Details.Add(detail);
                                    inv.OutboundQuantity += (float)use;
                                    inv.OutboundQuantity += use;
                                    BaseDal.Db.Updateable(inv).ExecuteCommand();
                                    remainingPartial -= use;
@@ -313,7 +333,7 @@
                                        };
                                        entityOrder.Details.Add(detail);
                                        inv.OutboundQuantity += (float)use;
                                        inv.OutboundQuantity += use;
                                        BaseDal.Db.Updateable(inv).ExecuteCommand();
                                        remainingPartial -= use;
@@ -356,7 +376,7 @@
                                    };
                                    entityOrder.Details.Add(detail);
                                    inv.OutboundQuantity += (float)useQty;
                                    inv.OutboundQuantity += useQty;
                                    BaseDal.Db.Updateable(inv).ExecuteCommand();
                                    remainingFullBoxes -= useBoxes;
@@ -391,7 +411,7 @@
                                        };
                                        entityOrder.Details.Add(detail);
                                        inv.OutboundQuantity += (float)useQty;
                                        inv.OutboundQuantity += useQty;
                                        BaseDal.Db.Updateable(inv).ExecuteCommand();
                                        remainingFullBoxes -= useBoxes;
@@ -511,7 +531,7 @@
                            // æ›´æ–°æ˜Žç»†çŠ¶æ€
                            BaseDal.Db.Updateable<Dt_DeliveryOrderDetail>()
                              .SetColumns(d => new Dt_DeliveryOrderDetail { Status = 1,OotDetailStatus="已完成" })
                              .SetColumns(d => new Dt_DeliveryOrderDetail { Status = 1, OotDetailStatus = "已完成" })
                              .Where(d => d.DeliveryOrderId == order.Id && d.Status == 0)
                              .ExecuteCommand();
@@ -1097,6 +1117,273 @@
            }
        }
        public WebResponseContent GetPdDeliveryOrders(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
                string warehouseCode = saveModel.MainData["warehouseId"].ToString();
                string orderNo = saveModel.MainData["orderNo"].ToString();
                List<Dt_DeliveryOrder> dt_ReceiveOrders = new List<Dt_DeliveryOrder>();
                if (string.IsNullOrEmpty(orderNo))
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Warehouse_no == warehouseCode && x.Out_type == "3").Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
                }
                else
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.Out_no.Contains(orderNo) || x.Client_no.Contains(orderNo)) && (x.OutStatus == "新建" || x.OutStatus == "开始" && x.Out_type == "3") && x.Warehouse_no == warehouseCode).OrderByDescending(x => x.CreateDate).Includes(x => x.Details).ToPageList(pageNo, 5);
                }
                content.OK(data: dt_ReceiveOrders);
            }
            catch (Exception)
            {
                throw;
            }
            return content;
        }
        public WebResponseContent GetDeliveryOrders(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
                string warehouseCode = saveModel.MainData["warehouseId"].ToString();
                string orderNo = saveModel.MainData["orderNo"].ToString();
                List<Dt_DeliveryOrder> dt_ReceiveOrders = new List<Dt_DeliveryOrder>();
                if (string.IsNullOrEmpty(orderNo))
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Warehouse_no == warehouseCode && x.Out_type != "3").Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
                }
                else
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.Out_no.Contains(orderNo) || x.Client_no.Contains(orderNo)) && (x.OutStatus == "新建" || x.OutStatus == "开始" && x.Out_type != "3") && x.Warehouse_no == warehouseCode).OrderByDescending(x => x.CreateDate).Includes(x => x.Details).ToPageList(pageNo, 5);
                }
                content.OK(data: dt_ReceiveOrders);
            }
            catch (Exception)
            {
                throw;
            }
            return content;
        }
        public WebResponseContent GetDeliveryOrderDetail(int pageNo, string orderNo, bool isPick)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_DeliveryOrder cabinOrder = new Dt_DeliveryOrder();
            if (isPick)
                cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Includes(x => x.Details).First(x => x.Out_no == orderNo && x.Out_type == "3");
            else
                cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Includes(x => x.Details).First(x => x.Out_no == orderNo && x.Out_type != "3");
            List<Dt_DeliveryOrderDetail>? cabinOrderDetails = cabinOrder.Details?.Where(x => x.Reservoirarea == pageNo.ToString()).ToList();
            content.OK(data: cabinOrderDetails);
            return content;
        }
        public WebResponseContent MatPicking(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var LocationCode = saveModel.MainData["LocationCode"].ToString();
                var orderNo = saveModel.MainData["orderNo"].ToString();
                var batchNo = saveModel.MainData["batchNo"].ToString();
                var Inqty = saveModel.MainData["Inqty"].ObjToInt();
                var warehouseCode = saveModel.MainData["warehouseCode"].ToString();
                Dt_DeliveryOrder cabinOrder = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == orderNo && x.Warehouse_no == warehouseCode && x.Out_type == "3").Includes(x => x.Details).First();
                if (cabinOrder == null) return WebResponseContent.Instance.Error($"盘点单已完成");
                Dt_DeliveryOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Batch_num == batchNo && x.Reservoirarea == warehouseCode).First();
                if (cabinOrderDetail == null) return WebResponseContent.Instance.Error($"盘点单明细已完成");
                Dt_MaterielInfo materielInfo = _basicService.MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == cabinOrderDetail.Goods_no);
                if (materielInfo == null) return WebResponseContent.Instance.Error($"请维护物料编号【{cabinOrderDetail.Goods_no}】的物料信息");
                cabinOrderDetail.Order_Outqty += Inqty;
                if (cabinOrderDetail.Order_Outqty > cabinOrderDetail.Order_qty)
                    return WebResponseContent.Instance.Error($"实盘数量不可超出账面数量");
                #region å¤„理出库单,货位,库存,库存批次信息
                _unitOfWorkManage.BeginTran();
                #region å‡ºåº“单
                cabinOrder.OutStatus = "开始";
                cabinOrderDetail.OotDetailStatus = "开始";
                if (cabinOrderDetail.Order_Outqty == cabinOrderDetail.Order_qty)
                {
                    cabinOrderDetail.OotDetailStatus = "已完成";
                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrderDetail, OperateTypeEnum.自动完成);
                }
                else
                {
                    _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
                }
                var cabinOrder1 = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == cabinOrder.Out_no && x.Out_type == "3").Includes(x => x.Details).First();
                if (cabinOrder1.Details == null || cabinOrder1.Details.Count < 1) cabinOrder.OutStatus = "已完成";
                if (cabinOrder.OutStatus == "已完成")
                    Repository.DeleteAndMoveIntoHty(cabinOrder, OperateTypeEnum.自动完成);
                else
                    Repository.UpdateData(cabinOrder);
                #endregion
                #region åº“å­˜
                Dt_InventoryInfo inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.LocationCode == LocationCode);
                if (inventoryInfo == null) return WebResponseContent.Instance.Error($"未找到货位【{LocationCode}】的库存信息");
                inventoryInfo.SupplyQuantity += Inqty;
                _inventoryInfoService.UpdateData(inventoryInfo);
                #endregion
                #region ä»»åŠ¡è®°å½•
                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                {
                    WarehouseCode = cabinOrderDetail.Reservoirarea,
                    BatchNo = inventoryInfo.BatchNo,
                    MaterielName = inventoryInfo.MaterielName,
                    MaterielCode = inventoryInfo.MaterielCode,
                    MaterielSpec = inventoryInfo.MaterielSpec,
                    TaskType = TaskTypeEnum.OutInventory.ObjToInt(),
                    TaskStatus = SupplyStatusEnum.CheckFinish.ObjToInt(),
                    CreateDate = DateTime.Now,
                    Creater = App.User.UserName,
                    LocationCode = LocationCode,
                    OrderNo = cabinOrder.Out_no,
                    StockQuantity = inventoryInfo.StockQuantity,
                    SupplyQuantity = Inqty,
                    Remark = "盘点"
                };
                _supplyTaskService.AddData(supplyTask);
                #endregion
                #region åº“存批次
                Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode == inventoryInfo.MaterielCode);
                if (inventory_Batch != null)
                {
                    inventory_Batch.SupplyQuantity += Inqty;
                    _inventory_BatchServices.UpdateData(inventory_Batch);
                }
                #endregion
                _unitOfWorkManage.CommitTran();
                #endregion
                content.OK(cabinOrderDetail.Order_Outqty.ToString());
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        public WebResponseContent FeedbackOut(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var LocationCode = saveModel.MainData["LocationCode"].ToString();
                var orderNo = saveModel.MainData["orderNo"].ToString();
                var batchNo = saveModel.MainData["batchNo"].ToString();
                var Inqty = saveModel.MainData["Inqty"].ObjToInt();
                var warehouseCode = saveModel.MainData["warehouseCode"].ToString();
                Dt_DeliveryOrder cabinOrder = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == orderNo && x.Warehouse_no == warehouseCode && x.Out_type != "3").Includes(x => x.Details).First();
                if (cabinOrder == null) return WebResponseContent.Instance.Error($"出库单已完成");
                Dt_DeliveryOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Batch_num == batchNo && x.Reservoirarea == warehouseCode).First();
                if (cabinOrderDetail == null) return WebResponseContent.Instance.Error($"出库单明细已完成");
                Dt_MaterielInfo materielInfo = _basicService.MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == cabinOrderDetail.Goods_no);
                if (materielInfo == null) return WebResponseContent.Instance.Error($"请维护物料编号【{cabinOrderDetail.Goods_no}】的物料信息");
                cabinOrderDetail.Order_Outqty += Inqty;
                if (cabinOrderDetail.Order_Outqty > cabinOrderDetail.Order_qty)
                    return WebResponseContent.Instance.Error($"出库数量不可超出单据数量");
                #region å¤„理出库单,货位,库存,库存批次信息
                _unitOfWorkManage.BeginTran();
                #region å‡ºåº“单
                cabinOrder.OutStatus = "开始";
                cabinOrderDetail.OotDetailStatus = "开始";
                if (cabinOrderDetail.Order_Outqty == cabinOrderDetail.Order_qty)
                {
                    cabinOrderDetail.OotDetailStatus = "已完成";
                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrderDetail, OperateTypeEnum.自动完成);
                }
                else
                {
                    _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
                }
                var cabinOrder1 = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == cabinOrder.Out_no && x.Out_type != "3").Includes(x => x.Details).First();
                if (cabinOrder1.Details == null || cabinOrder1.Details.Count < 1) cabinOrder.OutStatus = "已完成";
                Repository.UpdateData(cabinOrder);
                #endregion
                #region åº“å­˜
                Dt_InventoryInfo inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.LocationCode == LocationCode);
                if (inventoryInfo == null) return WebResponseContent.Instance.Error($"未找到货位【{LocationCode}】的库存信息");
                inventoryInfo.StockQuantity -= Inqty;
                if (inventoryInfo.StockQuantity <= 0)
                    _inventoryInfoService.DeleteData(inventoryInfo);
                else
                    _inventoryInfoService.UpdateData(inventoryInfo);
                #endregion
                #region è´§ä½
                var location = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == LocationCode);
                if (location == null) return WebResponseContent.Instance.Error($"请维护货位编号【{LocationCode}】的货位信息");
                if (location.EnableStatus == EnableStatusEnum.Disable.ObjToInt())
                    return WebResponseContent.Instance.Error($"货位编号【{LocationCode}】已禁用,请恢复正常再使用");
                Dt_InventoryInfo inventoryInfo1 = _inventoryInfoService.Repository.QueryFirst(x => x.LocationCode == LocationCode);
                if (inventoryInfo1 == null)
                {
                    location.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                    _basicService.LocationInfoService.UpdateData(location);
                }
                #endregion
                #region ä»»åŠ¡è®°å½•
                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                {
                    WarehouseCode = cabinOrderDetail.Reservoirarea,
                    BatchNo = inventoryInfo.BatchNo,
                    MaterielName = inventoryInfo.MaterielName,
                    MaterielCode = inventoryInfo.MaterielCode,
                    MaterielSpec = inventoryInfo.MaterielSpec,
                    TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
                    TaskType = TaskTypeEnum.OutPick.ObjToInt(),
                    CreateDate = DateTime.Now,
                    Creater = App.User.UserName,
                    LocationCode = location.LocationCode,
                    OrderNo = cabinOrder.Out_no,
                    StockQuantity = Inqty,
                    SupplyQuantity = 0,
                    Remark = "出库"
                };
                _supplyTaskService.AddData(supplyTask);
                #endregion
                #region åº“存批次
                Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode == inventoryInfo.MaterielCode);
                if (inventory_Batch != null)
                {
                    inventory_Batch.StockQuantity -= Inqty;
                    if (inventory_Batch.StockQuantity <= 0)
                        _inventory_BatchServices.DeleteData(inventory_Batch);
                    else
                        _inventory_BatchServices.UpdateData(inventory_Batch);
                }
                #endregion
                _unitOfWorkManage.CommitTran();
                #endregion
                content.OK(cabinOrderDetail.Order_Outqty.ToString());
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
    }
}