176 lines
6.0 KiB
C#
176 lines
6.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using WMS.Web.Core;
|
||
using WMS.Web.Core.Internal.Results;
|
||
using WMS.Web.Domain.Values;
|
||
|
||
namespace WMS.Web.Domain.Entitys
|
||
{
|
||
/// <summary>
|
||
/// erp出库任务单
|
||
/// </summary>
|
||
[Serializable]
|
||
[Table("t_erp_outstock_task")]
|
||
public class OutStockTask : EntityBase
|
||
{
|
||
public OutStockTask() { }
|
||
/// <summary>
|
||
/// 主键 订单编号
|
||
/// </summary>
|
||
[Column("Id")]
|
||
public override int Id { get; set; }
|
||
/// <summary>
|
||
/// 单据编号
|
||
/// </summary>
|
||
[Column("BillNo")]
|
||
public string BillNo { get; set; }
|
||
/// <summary>
|
||
/// 来源单号
|
||
///</summary>
|
||
[Column("SourceBillNo")]
|
||
public string SourceBillNo { get; set; }
|
||
/// <summary>
|
||
/// 发货组织
|
||
///</summary>
|
||
[Column("DeliveryOrgId")]
|
||
public int DeliveryOrgId { get; set; }
|
||
/// <summary>
|
||
/// 收货客户
|
||
///</summary>
|
||
[Column("ReceiptCustomerId")]
|
||
public int ReceiptCustomerId { get; set; }
|
||
/// <summary>
|
||
/// 单据状态
|
||
/// </summary>
|
||
[Column("Status")]
|
||
public OutStockStatus Status { get; set; } = OutStockStatus.Wait;
|
||
/// <summary>
|
||
/// 单据类型
|
||
/// </summary>
|
||
[Column("Type")]
|
||
public OutStockType Type { get; set; } = OutStockType.Sal;
|
||
/// <summary>
|
||
/// 操作人(出库人)
|
||
/// </summary>
|
||
[Column("OperatorId")]
|
||
public int? OperatorId { get; set; }
|
||
/// <summary>
|
||
/// 操作时间(出库时间)
|
||
/// </summary>
|
||
[Column("OperateTime")]
|
||
public DateTime? OperateTime { get; set; }
|
||
/// <summary>
|
||
/// 创建时间(erp那边的创建时间)
|
||
///</summary>
|
||
[Column("CreateTime")]
|
||
public DateTime? CreateTime { get; set; }
|
||
/// <summary>
|
||
/// 明细
|
||
/// </summary>
|
||
public List<OutStockTaskDetails> Details = new List<OutStockTaskDetails>();
|
||
public void Create(OutStockType type, string sourceBillNo, int deliveryOrgId, int receiptCustomerId, DateTime createTime)
|
||
{
|
||
this.Type = type;
|
||
this.SourceBillNo = sourceBillNo;
|
||
this.DeliveryOrgId = deliveryOrgId;
|
||
this.ReceiptCustomerId = receiptCustomerId;
|
||
this.CreateTime = createTime;
|
||
}
|
||
/// <summary>
|
||
/// 出库 反写 任务单
|
||
/// </summary>
|
||
/// <param name="materialId"></param>
|
||
/// <param name="qty"></param>
|
||
/// <returns></returns>
|
||
public Result OutStock(int materialId, decimal qty)
|
||
{
|
||
var detail = this.Details.FirstOrDefault(f => f.MaterialId == materialId);
|
||
if (detail == null) return Result.ReFailure(ResultCodes.OrderNoData);
|
||
if ((detail.AccruedQty - detail.RealityQty) < qty)
|
||
return Result.ReFailure(ResultCodes.OutStockQtyError);
|
||
|
||
if (detail.RealityQty <= 0)
|
||
detail.OutStockBeginTime = DateTime.Now;
|
||
|
||
detail.OutStockEndTime = DateTime.Now;
|
||
|
||
detail.RealityQty = detail.RealityQty + qty;
|
||
|
||
if (this.Details.Where(w => w.RealityQty >= w.AccruedQty).Count() == this.Details.Count())
|
||
this.Status = OutStockStatus.Already;
|
||
else
|
||
this.Status = OutStockStatus.Part;
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
/// <summary>
|
||
/// 生成单据号
|
||
/// </summary>
|
||
public void GenerateNo()
|
||
{
|
||
//用户手动输入了 就不自动生成了
|
||
if (!string.IsNullOrEmpty(this.BillNo)) return;
|
||
|
||
if (this.Id.ToString().Length >= 8)
|
||
{
|
||
this.BillNo = "CKRW" + this.Id.ToString();
|
||
return;
|
||
}
|
||
|
||
string idStr = this.Id.ToString();
|
||
while (true)
|
||
{
|
||
idStr = "0" + idStr;
|
||
if (idStr.Length >= 8) break;
|
||
}
|
||
//this.Number = CNSpellTranslator.GetFirstSpell(this.Name) + idStr;
|
||
this.BillNo = "CKZL" + idStr;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 作废
|
||
/// </summary>
|
||
public void Repeal(int creatorId)
|
||
{
|
||
this.OperatorId = creatorId;
|
||
this.OperateTime = DateTime.Now;
|
||
this.Status = OutStockStatus.Repeal;
|
||
}
|
||
/// <summary>
|
||
/// 合并
|
||
/// </summary>
|
||
/// <param name="list"></param>
|
||
/// <param name="creatorId"></param>
|
||
/// <returns></returns>
|
||
public Result Merge(List<OutStockTask> list, int creatorId)
|
||
{
|
||
// 符合合并数据逻辑:出库状态为”待拣货”+出库类型为:销售出库+发货组织一致+收货客户一致+发货仓库一致
|
||
if (list.Where(w => w.Status != OutStockStatus.Wait).Any()) return Result.ReFailure(ResultCodes.MergeStatusError);
|
||
if (list.Where(w => w.Type != OutStockType.Sal).Any()) return Result.ReFailure(ResultCodes.MergeStatusError);
|
||
if (list.GroupBy(g => g.DeliveryOrgId).Count() > 1) return Result.ReFailure(ResultCodes.MergeStatusError);
|
||
if (list.GroupBy(g => g.ReceiptCustomerId).Count() > 1) return Result.ReFailure(ResultCodes.MergeStatusError);
|
||
|
||
var details = list.SelectMany(s => s.Details).ToList();
|
||
|
||
if (details.GroupBy(g => g.StockCode).Count() > 1) return Result.ReFailure(ResultCodes.MergeStatusError);
|
||
|
||
//清空数据绑定
|
||
foreach (var d in details)
|
||
{
|
||
d.Id = 0;
|
||
d.Fid = 0;
|
||
}
|
||
this.OperatorId = creatorId;
|
||
this.OperateTime = DateTime.Now;
|
||
this.Status = OutStockStatus.Wait;
|
||
this.Type = OutStockType.Sal;
|
||
this.Details = details;
|
||
|
||
return Result.ReSuccess();
|
||
}
|
||
}
|
||
}
|