using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
using DS.WMS.Common.Enum;
using DS.WMS.Core.BaseInfo.Entity;
using DS.WMS.Core.FeeModule.Dtos;
using DS.WMS.Core.FeeModule.Entity;
using DS.WMS.Core.FeeModule.Interface;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.WmsModule.Entity;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using SqlSugar;
namespace DS.WMS.Core.FeeModule.Method;
public class WmsStorageBillService : IWmsStorageBillService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IWmsFeeComonService _wmsFeeCommonService;
///
///
///
///
public WmsStorageBillService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
_wmsFeeCommonService = _serviceProvider.GetRequiredService();
}
public DataResult> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.Select()
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
}
public DataResult GetWmsStorageBillInfo(string id)
{
var gid = Guid.Parse(id);
var data = db.Queryable()
.Select()
.Where((a) => a.WMSDUIID == gid)
.First();
return DataResult.Success(data);
}
public DataResult GetWmsStorageBillDetailList(string id)
{
var data = db.Queryable()
// .Select()
.Select(a => new WmsStorageBillDetailListModel()
{
FEETYPENAME = a.FEETYPE == 1 ? "应收" : "应付",
},
true)
.Where(a => a.WMSDUIID == Guid.Parse(id)).ToList();
return DataResult.Successed("查询成功!", data);
}
public DataResult> GetWmsPayInfoList(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.LeftJoin((a, b) => a.WMSID == b.WMSID)
.LeftJoin((a, b, c) => b.WMSID == c.WMSID)
// .LeftJoin((a, b,c,d,e,f,g) => e.GoodsFeeTypeGID == g.GID)
.Select((a, b, c) => new WmsPayInfoListPageModel()
{
GID = a.GID,
WMSID = a.WMSID,
BSNO = b.BSNO,
BILLTYPE = b.BILLTYPE,
REMARK = b.REMARK,
CORPID = b.CORPID,
CUSTOMERNAME = b.CUSTOMERNAME,
MATERIALNO = b.MATERIALNO,
GoodsId = b.GoodsId,
GOODSCODE = b.GOODSCODE,
GOODSNAME = b.GOODSNAME,
GOODSHSCODE = b.GOODSHSCODE,
MBLNO = b.MBLNO,
CNTRNO = b.CNTRNO,
OLD_CNTRNO = b.OLD_CNTRNO,
KGS = c.KGS,
PKGS = c.PKGS,
NETWEIGHT = c.NETWEIGHT,
KINDPKGS = c.KINDPKGS,
STORAGEUNIT = c.STORAGEUNIT,
STORAGEUNITCOUNT = a.STORAGEUNITCOUNT,
FEERATEWMSID = a.FEERATEWMSID,
INDATE = b.INDATE,
WMSINDATE = b.WMSINDATE,
FEESTARTDATE = a.FEESTARTDATE,
FEERATESTARTDATE = b.FEERATESTARTDATE,
FEEENDDATE = (a.FEEENDDATE == null
? (b.FEEENDDATE == null ? DateTime.Now.AddYears(100) : b.FEEENDDATE)
: (a.FEEENDDATE > b.FEEENDDATE ? b.FEEENDDATE : a.FEEENDDATE)),
CUSTOMNO = b.CUSTOMNO,
ACCBOOKNO = b.ACCBOOKNO,
GOODSOWNER = b.GOODSOWNER,
ARCLIENTWMSOUT = a.ARCLIENTWMSOUT,
STOREHOUSE = c.STOREHOUSE,
AREACODE = c.AREACODE,
ClientId = b.ClientId
},
true)
.MergeTable()
.Where(whereList).ToQueryPage(request.PageCondition);
// .Where(x=>x.FEEENDDATE >= x.FEESTARTDATE)
return data;
}
public DataResult ImportWmsPayInfo(WmsPayInfoImportInput model)
{
if (model.Ids.Length == 0)
{
return DataResult.Failed("请勾选账单数据!");
}
var payInfoList = db.Queryable().Where(x => model.Ids.Contains(x.gid.ToString()))
.ToList();
if (payInfoList.Any(x => x.STORAGEUNITCOUNT == 0))
{
return DataResult.Failed("存在计费数量为0的信息!");
}
var wmslist = db.Queryable().Where(x => payInfoList.Select(x => x.WMSID).Contains(x.WMSID)).ToList();
var wmsChangelist = db.Queryable().Where(x => payInfoList.Select(x => x.WMSID).Contains(x.WMSID))
.ToList();
var wmsFeelist = db.Queryable()
.Where(x => payInfoList.Select(x => x.WMSID).Contains(x.PID) && x.FEENAME == "仓储费")
.ToList();
var clientId = wmslist.First().ClientId;
var clientName = db.Queryable().First(x => x.GID == Guid.Parse(clientId)).SHORTNAME;
if (wmslist.Any(x => x.ClientId != clientId))
{
return DataResult.Failed("只能选择同一客户!");
}
var initDate = new DateTime(1900, 1, 1);
var feecalendars = new List();
try
{
//开启事务
db.Ado.BeginTran();
var gid = Guid.NewGuid();
var now = DateTime.Now;
var bsno = NumUtil.GetBillNo(db, "WM09");
var feeDuiStorage = new OP_WMS_FEEDUI_STORAGE
{
WMSDUIID = gid,
ClientId = clientId,
BSNO = bsno,
CUSTOMERNAME = clientName,
ACCDATE = model.ACCDATE,
STARTDATE = model.FEESTARTDATE,
ENDDATE = model.FEEENDDATE,
QUANTITY = 0,
BILLSTATUS = "1000",
AMOUNT = 0,
BSSTATUS = false,
FEESTATUS = false
};
#region 处理明细
foreach (var item in payInfoList)
{
DateTime startDate = initDate;
DateTime endDate = initDate;
//遍历日期 搞清每天的费率 和计费数量
for (DateTime _currday = (DateTime)feeDuiStorage.STARTDATE;
_currday <= feeDuiStorage.ENDDATE;
_currday = _currday.AddDays(1))
{
//如果还没到达这票业务的费用开始日期,跳过
if (_currday < item.FEESTARTDATE) continue;
if (_currday > item.FEEENDDATE) continue;
var currChangeList = wmsChangelist.Where(x => x.WMSID == item.WMSID).ToList();
var changes = _wmsFeeCommonService.GetWmsChangeList(_currday, currChangeList).Data;
var feeList = wmsFeelist.Where(x => x.PID == item.WMSID).ToList();
var doDateList = new List();
foreach (var change in changes)
{
if (doDateList.Any(x => x == change.DODATE))
continue;
//当日计费数量
decimal currCount = (decimal)change.STORAGEUNITCOUNT3;
//如果是出库的/货转出库的,计费数量取变动前;
if (change.CHANGETYPE == WMSCHANGTYPE.出库)
{
if (_currday == (DateTime)change.DODATE)
{
var maxValue = wmsChangelist.Max(x => x.STORAGEUNITCOUNT2);
currCount = (decimal)maxValue;
doDateList.Add(change.DODATE);
}
}
if (startDate == initDate) startDate = _currday;
endDate = _currday;
var currFeeRate = 0.0m;
currFeeRate = _wmsFeeCommonService.GetWmsFeeRate(feeList, _currday, (DateTime)item.FEERATESTARTDATE).Data;
var feecalendar = new OP_WMS_FEECALENDAR()
{
FEECALENDARID = Guid.NewGuid(),
WMSID = item.WMSID,
WMSDUIID = gid,
CUSTOMERNAME = clientName,
FEEDATE = _currday,
QUANTITY = currCount,
PRICE = currFeeRate,
AMOUNT = currCount * currFeeRate,
FEETYPE = 1,
//应收
};
feecalendars.Add(feecalendar);
}
}
}
// //重新生成费用日历
db.Insertable(feecalendars).ExecuteCommand();
db.Insertable(feeDuiStorage).ExecuteCommand();
#endregion
db.Ado.CommitTran();
return DataResult.Successed("引入成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("引入失败!" + ex);
}
}
public DataResult DelWmsStorageBill(string id)
{
var gid = Guid.Parse(id);
var storageBill = db.Queryable().First(x => x.WMSDUIID == gid);
if (storageBill.BILLSTATUS =="1003")
{
return DataResult.Failed("单据已审核,不能修改!");
}
try
{
//开启事务
db.Ado.BeginTran();
db.Deleteable(storageBill).ExecuteCommand();
var detail = db.Queryable().Where(x => x.WMSDUIID == gid).ToList();
if (detail.Count() > 0)
{
db.Deleteable(detail).ExecuteCommand();
}
var feecalendars = db.Queryable().Where(x => x.WMSDUIID == gid).ToList();
if (feecalendars.Count() > 0)
{
db.Deleteable(feecalendars).ExecuteCommand();
}
var fees = db.Queryable().Where(x => x.BXGID == id).ToList();
if (fees.Count() > 0)
{
db.Deleteable(fees).ExecuteCommand();
}
db.Ado.CommitTran();
return DataResult.Successed("删除成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("删除失败!" + ex);
}
}
public DataResult GetWmsFeeCalendarList(string id)
{
var data = db.Queryable().Where(a => a.WMSDUIID == Guid.Parse(id))
.LeftJoin((a, b) => a.WMSID == b.WMSID)
.Select((a, b) => new WmsFeeCalendarListModel()
{
FEETYPENAME = a.FEETYPE == 1 ? "应收" : "应付",
},
true)
.MergeTable()
.OrderBy(x => x.FEEDATE)
.ToList();
return DataResult.Successed("查询成功!", data);
}
public DataResult MakeWmsFee(string id)
{
var gid = Guid.Parse(id);
var info = db.Queryable().First(x => x.WMSDUIID == gid);
if (info.IsNullOrEmpty())
{
return DataResult.Failed("月结账单不存在!");
}
if (info.BILLSTATUS =="1003")
{
return DataResult.Failed("单据已审核,不能修改!");
}
#region 删除之前信息
var detail = db.Queryable().Where(x => x.WMSDUIID == gid).ToList();
// var bsnoList = detail.Select(x => x.WMSDUIDETAILID.ToString()).ToList();
if (detail.Count() > 0)
{
db.Deleteable(detail).ExecuteCommand();
}
var wmsFeecalendars = db.Queryable().Where(x => x.WMSDUIID == gid).ToList();
if (wmsFeecalendars.Count() > 0)
{
db.Deleteable(wmsFeecalendars).ExecuteCommand();
}
var fees = db.Queryable().Where(x => x.BXGID == id).ToList();
if (fees.Count() > 0)
{
db.Deleteable(fees).ExecuteCommand();
}
#endregion
var initDate = new DateTime(1900, 1, 1);
var feecalendars = new List();
var wmslist = db.Queryable().Where(x => x.ClientId == info.ClientId).ToList();
var wmsChangelist = db.Queryable()
.Where(x => wmslist.Select(a => a.WMSID).Contains((Guid)x.WMSID))
.ToList();
var wmsFeelist = db.Queryable()
.Where(x => wmslist.Select(x => x.WMSID).Contains((Guid)x.PID) && x.FEENAME == "仓储费")
.ToList();
var wmsFeeList = new List();
var fee = db.Queryable().First(x => x.NAME == "仓储费");
try
{
//开启事务
db.Ado.BeginTran();
foreach (var item in wmslist)
{
DateTime startDate = initDate;
DateTime endDate = initDate;
decimal wmsAmount = 0; //单票库存总金额
decimal wmsCount = 0; //单票库存总计费数量
//遍历日期 搞清每天的费率 和计费数量
for (DateTime _currday = (DateTime)info.STARTDATE;
_currday <= info.ENDDATE;
_currday = _currday.AddDays(1))
{
//如果还没到达这票业务的费用开始日期,跳过
if (_currday < item.FEESTARTDATE) continue;
if (_currday > item.FEEENDDATE) continue;
var currChangeList = wmsChangelist.Where(x => x.WMSID == item.WMSID).ToList();
// Logger.Log(NLog.LogLevel.Info, "异常信息:" + item.WMSID);
var changes = _wmsFeeCommonService.GetWmsChangeList(_currday, currChangeList).Data;
var feeList = wmsFeelist.Where(x => x.PID == item.WMSID).ToList();
var doDateList = new List();
foreach (var change in changes)
{
if (doDateList.Any(x => x == change.DODATE))
continue;
//当日计费数量
decimal currCount = (decimal)change.STORAGEUNITCOUNT3;
//如果是出库的/货转出库的,计费数量取变动前;
if (change.CHANGETYPE == WMSCHANGTYPE.出库)
{
if (_currday == (DateTime)change.DODATE)
{
var maxValue = wmsChangelist.Max(x => x.STORAGEUNITCOUNT2);
currCount = (decimal)maxValue;
doDateList.Add(change.DODATE);
}
}
if (startDate == initDate) startDate = _currday;
endDate = _currday;
var currFeeRate = 0.0m;
currFeeRate = _wmsFeeCommonService.GetWmsFeeRate(feeList, _currday, (DateTime)item.FEERATESTARTDATE).Data;
var feecalendar = new OP_WMS_FEECALENDAR()
{
FEECALENDARID = Guid.NewGuid(),
WMSID = item.WMSID,
WMSDUIID = gid,
CUSTOMERNAME = info.CUSTOMERNAME,
FEEDATE = _currday,
QUANTITY = currCount,
PRICE = currFeeRate,
AMOUNT = currCount * currFeeRate,
FEETYPE = 1,
//应收
};
wmsAmount += currCount * currFeeRate;
wmsCount += currCount;
feecalendars.Add(feecalendar);
}
if (wmsAmount >= 0)
{
//生成单票费用:
var wmsfee = new OP_WMS_FEEDUI_STORAGE_DETAIL();
if (wmsFeeList.Any(x => x.MBLNO == item.MBLNO))
{
wmsfee = wmsFeeList.First(x => x.MBLNO == item.MBLNO);
wmsfee.QUANTITY += wmsCount;
wmsfee.AMOUNT += wmsAmount;
if (startDate < wmsfee.STARTDATE)
wmsfee.STARTDATE = startDate;
if (endDate > wmsfee.ENDDATE)
wmsfee.ENDDATE = endDate;
}
else
{
wmsfee.WMSDUIDETAILID = Guid.NewGuid();
wmsfee.WMSDUIID = info.WMSDUIID;
wmsfee.CUSTOMERNAME = info.CUSTOMERNAME;
wmsfee.ACCDATE = info.ACCDATE;
wmsfee.MBLNO = item.MBLNO;
wmsfee.QUANTITY = wmsCount;
wmsfee.AMOUNT = wmsAmount;
wmsfee.STARTDATE = startDate;
wmsfee.ENDDATE = endDate;
wmsfee.FEETYPE = 1;
wmsFeeList.Add(wmsfee);
}
}
}
}
if (wmsFeeList.Count > 0)
{
foreach (var item in wmsFeeList)
{
// info.QUANTITY += item.QUANTITY;
// info.AMOUNT += item.AMOUNT;
if (item.AMOUNT > 0)
{
//给列表增加一条费用
var newfee = new Fee()
{
GID = Guid.NewGuid().ToString(),
ClientId = info.ClientId,
FeeId = fee.GID,
FEENAME = fee.NAME,
BSNO = item.WMSDUIDETAILID.ToString().ToUpper(),
BXGID = info.WMSDUIID.ToString().ToUpper(),
CUSTOMERNAME = info.CUSTOMERNAME,
FEETYPE = 1,
QUANTITY = item.AMOUNT,
UNITPRICE = 1,
AMOUNT = item.AMOUNT,
SETTLEMENT = 0,
ORDERAMOUNT = 0,
ORDERINVOICE = 0,
ORDERINVSETTLEMENT = 0,
INVOICE = 0,
CURRENCY = "RMB",
EXCHANGERATE = 1,
ENTERDATE = DateTime.Now,
ENTEROPERATOR = user.GetUserGID(),
FEESTATUS = 1,
TAXRATE = 0,
ISADVANCEDPAY = false,
ISINVOICE = false,
ISCRMORDERFEE = false,
F_NO = item.MBLNO,
INPUTMODE = "月结",
};
newfee.setTax(ref newfee);
db.Insertable(newfee).ExecuteCommand();
}
}
info.QUANTITY = wmsFeeList.Sum(x => x.QUANTITY);
info.AMOUNT = wmsFeeList.Sum(x => x.AMOUNT);
db.Insertable(wmsFeeList).ExecuteCommand();
}
//重新生成费用日历
db.Insertable(feecalendars).ExecuteCommand();
db.Updateable(info).ExecuteCommand();
db.Ado.CommitTran();
return DataResult.Successed("计算仓储费成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("计算仓储费失败!" + ex);
}
}
///
///
///
///
///
public DataResult EditWmsStorageBill(WmsStorageBillInput model)
{
if (model.WMSDUIID.IsNullOrEmpty())
{
var isExist = db.Queryable().Where(x => x.BSNO == model.BSNO).First();
if (isExist!=null)
{
return DataResult.Failed("单据编码已存在!");
}
var data = model.Adapt();
db.Insertable(data).ExecuteCommand();
return DataResult.Successed("添加成功!");
}
else
{
var info =db.Queryable().Where(x => x.WMSDUIID == model.WMSDUIID).First();
if (info.BILLSTATUS =="1003")
{
return DataResult.Failed("单据已审核,不能修改!");
}
info = model.Adapt(info);
db.Updateable(info).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand();
return DataResult.Successed("更新成功!");
}
}
public DataResult AuditWmsStorageBill(string id)
{
var gid = Guid.Parse(id);
var exitInfo = db.Queryable().First(x => x.WMSDUIID == gid);
if (exitInfo.BILLSTATUS == "1003" )
{
return DataResult.Failed("已审核通过!");
}
var detail = db.Queryable().Where(x => x.WMSDUIID == gid);
if (detail.Count()==0)
{
return DataResult.Failed("账单明细不能为空!");
}
var fees = db.Queryable().Where(x => x.BXGID == id).ToList();
if (fees.Count() == 0)
{
return DataResult.Failed("费用明细不能为空,请计算仓储费用!");
}
exitInfo.BILLSTATUS = "1003";
db.Updateable(exitInfo).ExecuteCommand();
return DataResult.Successed("审核成功");
}
public DataResult CancelAuditWmsStorageBill(string id)
{
var gid = Guid.Parse(id);
var exitInfo = db.Queryable().First(x => x.WMSDUIID == gid);
if (exitInfo.BILLSTATUS == "1000" )
{
return DataResult.Failed("未审核!");
}
exitInfo.BILLSTATUS = "1000";
db.Updateable(exitInfo).ExecuteCommand();
return DataResult.Successed("取消审核成功");
}
}