zhaoxinyu
11 months ago
7 changed files with 430 additions and 11 deletions
@ -0,0 +1,343 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using EFCore.BulkExtensions; |
||||
|
using SettleAccount.Domain.BQ; |
||||
|
using TaskJob.EventArgs; |
||||
|
using TaskJob.Interfaces; |
||||
|
using Volo.Abp; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Win.Abp.Snowflakes; |
||||
|
using Win.Sfs.BaseData.ImportExcelCommon; |
||||
|
using Win.Sfs.SettleAccount.Bases; |
||||
|
using Win.Sfs.SettleAccount.CommonManagers; |
||||
|
|
||||
|
namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs |
||||
|
{ |
||||
|
public class PDMakeService : BASE_SERVICE, ITransientDependency, IExportJob |
||||
|
{ |
||||
|
protected readonly SettleAccountDbContext _dbcontext; |
||||
|
public PDMakeService( |
||||
|
SettleAccountDbContext dbcontext, |
||||
|
IExcelImportAppService excelImportService, |
||||
|
ISnowflakeIdGenerator snowflakeIdGenerator, |
||||
|
ICommonManager commonManager) : base(excelImportService, snowflakeIdGenerator, commonManager) |
||||
|
{ |
||||
|
_dbcontext = dbcontext; |
||||
|
} |
||||
|
|
||||
|
public string ExportFile(Guid id, List<string> exportName, List<CustomCondition> property) |
||||
|
{ |
||||
|
|
||||
|
var billList = property.Where(p => p.Name == "invoiceBillNum").FirstOrDefault().Value.Split(',').ToList(); |
||||
|
var invList = _dbcontext.Set<INVOICE_GRP>().Where(p => billList.Contains(p.InvbillNum)).ToList();//所有提交发票信息
|
||||
|
int count = invList.Select(p => p.InvGroupNum).Distinct().Count(); |
||||
|
if (count > 1) |
||||
|
{ |
||||
|
throw new UserFriendlyException($"发票不属于一个发票分组"); |
||||
|
} |
||||
|
var lostlist = invList.Where(p => p.InvoiceState == InvoiceBillState.报废 || p.State != SettleBillState.财务已审核).ToList(); |
||||
|
if (lostlist.Count > 0) |
||||
|
{ |
||||
|
var invs = lostlist.Select(p => p.InvbillNum).ToList(); |
||||
|
throw new UserFriendlyException($"发票{string.Join(",", invs)}已经报废或不是财务审核状态!", "400"); |
||||
|
} |
||||
|
var inv = invList.FirstOrDefault(); |
||||
|
if (inv.BusinessType == EnumBusinessType.JisBBAC) |
||||
|
{ |
||||
|
var pdlist = _dbcontext.Set<BBAC_PD>().Where(p => billList.Contains(p.BillNum)).ToList();//包含发票号的代扣减单
|
||||
|
if (pdlist != null && pdlist.Count > 0) |
||||
|
{ |
||||
|
var invs = pdlist.Select(p => p.BillNum).ToList(); |
||||
|
throw new UserFriendlyException($"发票{string.Join(",", invs)}已经生成待扣减过不能再次提交", "400"); |
||||
|
} |
||||
|
var parentList = invList.Select(p => p.ParentInvbillNum).ToList();//所有发票存在父发票号
|
||||
|
List<string> oldinvs = new List<string>(); |
||||
|
oldinvs.AddRange(billList); |
||||
|
if (parentList != null && parentList.Count > 0) |
||||
|
{ |
||||
|
oldinvs.AddRange(parentList); |
||||
|
} |
||||
|
List<string> oldlist = new List<string>();//以前提交过的发票
|
||||
|
List<string> newlist = new List<string>();//没有提交过的发票
|
||||
|
var pdoldlist = pdlist.Where(p => oldinvs.Contains(p.BillNum)).ToList(); |
||||
|
if (pdoldlist != null && pdoldlist.Count > 0) |
||||
|
{ |
||||
|
oldlist = pdoldlist.Select(p => p.BillNum).ToList();//已经存在扣减记录,的发票号
|
||||
|
} |
||||
|
newlist = billList.Where(p => !oldlist.Contains(p)).ToList();//不存在扣减记录的发票号
|
||||
|
List<BBAC_CAN_SA_DETAIL> entityList = new List<BBAC_CAN_SA_DETAIL>();//结算明细
|
||||
|
var namelist = invList.Select(p => p.InvbillNum).ToList(); |
||||
|
if (oldlist.Count > 0) |
||||
|
{ |
||||
|
var involdList = invList.Where(p => oldinvs.Contains(p.ParentInvbillNum)).ToList(); |
||||
|
var involdname = involdList.Select(p => p.InvbillNum); |
||||
|
var adjlist = _dbcontext.Set<PUB_ADJ_DETAIL>().Where(p => involdname.Contains(p.InvBillNum)); |
||||
|
|
||||
|
if (adjlist != null && adjlist.Any()) |
||||
|
{ |
||||
|
foreach (var itm in adjlist) |
||||
|
{ |
||||
|
var detail = new BBAC_CAN_SA_DETAIL( |
||||
|
guid: GuidGenerator.Create(), |
||||
|
keyCode: itm.KeyCode, |
||||
|
version: itm.Version, |
||||
|
billNum: itm.InvGroupNum, |
||||
|
settleBillNum: itm.SettleBillNum, |
||||
|
lU: itm.LU, |
||||
|
pN: itm.PN, |
||||
|
site: itm.Site, |
||||
|
qty: itm.Qty, |
||||
|
price: 0, |
||||
|
category: itm.BusinessType, |
||||
|
isReturn: itm.Qty > 0 ? false : true, |
||||
|
settleDate: itm.SettleDate, |
||||
|
groupNum: itm.GroupNum, |
||||
|
invGroupNum: itm.InvGroupNum, |
||||
|
contactid: itm.Extend1,//生产号
|
||||
|
invbillnum: itm.InvBillNum, |
||||
|
partcode: itm.PartCode); |
||||
|
detail.ErpLoc = itm.ErpLoc; |
||||
|
detail.RealPartCode = itm.RealPartCode; |
||||
|
entityList.Add(detail); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if (newlist.Count > 0) |
||||
|
{ |
||||
|
var entitys = invList.Where(p => newlist.Contains(p.InvbillNum)).ToList(); |
||||
|
var entityDetail = _dbcontext.Set<BBAC_CAN_SA_DETAIL>().Where(p => newlist.Contains(p.InvbillNum)).ToList();//结算明细
|
||||
|
var adjlist = _dbcontext.Set<PUB_ADJ_DETAIL>().Where(p => newlist.Contains(p.InvBillNum)).ToList();//发票关联调整表明细
|
||||
|
if (adjlist != null && adjlist.Count > 0) |
||||
|
{ |
||||
|
foreach (var itm in adjlist) |
||||
|
{ |
||||
|
var detail = new BBAC_CAN_SA_DETAIL( |
||||
|
guid: GuidGenerator.Create(), |
||||
|
keyCode: itm.KeyCode, |
||||
|
version: itm.Version, |
||||
|
billNum: itm.InvGroupNum, |
||||
|
settleBillNum: itm.SettleBillNum, |
||||
|
lU: itm.LU, |
||||
|
pN: itm.PN, |
||||
|
site: itm.Site, |
||||
|
qty: itm.Qty, |
||||
|
price: 0, |
||||
|
category: itm.BusinessType, |
||||
|
isReturn: itm.Qty > 0 ? false : true, |
||||
|
settleDate: itm.SettleDate, |
||||
|
groupNum: itm.GroupNum, |
||||
|
invGroupNum: itm.InvGroupNum, |
||||
|
contactid: itm.Extend1,//生产号
|
||||
|
invbillnum: itm.InvBillNum, |
||||
|
partcode: itm.PartCode |
||||
|
); |
||||
|
detail.ErpLoc = itm.ErpLoc; |
||||
|
detail.RealPartCode = itm.RealPartCode; |
||||
|
entityDetail.Add(detail); |
||||
|
} |
||||
|
} |
||||
|
entityList.AddRange(entityDetail); |
||||
|
} |
||||
|
var bbacmainlist = new List<BBAC_PD>(); |
||||
|
foreach (var itm in invList) |
||||
|
{ |
||||
|
var bbac = new BBAC_PD( |
||||
|
guid: itm.Id, |
||||
|
version: int.Parse(DateTime.Now.ToString("yyyyMM")), |
||||
|
billNum: itm.InvbillNum, |
||||
|
settleBillNum: itm.InvGroupNum, |
||||
|
state: SettleBillState.客户已收票, |
||||
|
invGroupNum: itm.InvGroupNum, |
||||
|
site: string.Empty |
||||
|
); |
||||
|
bbacmainlist.Add(bbac); |
||||
|
} |
||||
|
var bbacDetail = new List<BBAC_PD_DETAIL>(); |
||||
|
foreach (var itm in entityList) |
||||
|
{ |
||||
|
var detail = new BBAC_PD_DETAIL( |
||||
|
guid: itm.Id, |
||||
|
keyCode: itm.KeyCode, |
||||
|
version: itm.Version, |
||||
|
billNum: itm.InvbillNum, |
||||
|
lU: itm.LU, |
||||
|
rELU: string.Empty, |
||||
|
pN: itm.PN, |
||||
|
rEPN: string.Empty, |
||||
|
site: itm.Site, |
||||
|
qty: itm.Qty, |
||||
|
price: 0, |
||||
|
invGroupNum: itm.InvGroupNum, |
||||
|
settleDate: itm.SettleDate, |
||||
|
groupNum: itm.GroupNum, |
||||
|
extend1: string.Empty, |
||||
|
extend2: string.Empty, |
||||
|
extend3: string.Empty, |
||||
|
extend4: itm.PartCode, |
||||
|
p_businessType: itm.BusinessType |
||||
|
);//厂内实际零件号
|
||||
|
detail.RELU = string.IsNullOrEmpty(itm.RealPartCode) ? itm.PartCode : itm.RealPartCode;//厂内替换零件号
|
||||
|
detail.Extend2 = itm.ErpLoc;//ERP库位
|
||||
|
bbacDetail.Add(detail); |
||||
|
} |
||||
|
|
||||
|
_dbcontext.BulkInsert(bbacmainlist); |
||||
|
_dbcontext.BulkInsert(bbacDetail); |
||||
|
foreach (var itm in invList) |
||||
|
{ |
||||
|
itm.State = SettleBillState.客户已收票; |
||||
|
}; |
||||
|
_dbcontext.BulkUpdate(invList); |
||||
|
|
||||
|
} |
||||
|
else if (inv.BusinessType == EnumBusinessType.JisHBPO || inv.BusinessType == EnumBusinessType.MaiDanJianHBPO) |
||||
|
{ |
||||
|
var pdlist = _dbcontext.Set<HBPO_PD>().Where(p => billList.Contains(p.BillNum)).ToList();//包含发票号的代扣减单
|
||||
|
if (pdlist != null && pdlist.Count > 0) |
||||
|
{ |
||||
|
var invs = pdlist.Select(p => p.BillNum).ToList(); |
||||
|
throw new UserFriendlyException($"发票{string.Join(",", invs)}已经生成待扣减过不能再次提交", "400"); |
||||
|
} |
||||
|
var parentList = invList.Select(p => p.ParentInvbillNum).ToList();//所有发票存在父发票号
|
||||
|
List<string> oldinvs = new List<string>(); |
||||
|
oldinvs.AddRange(billList); |
||||
|
if (parentList != null && parentList.Count > 0) |
||||
|
{ |
||||
|
oldinvs.AddRange(parentList); |
||||
|
} |
||||
|
List<string> oldlist = new List<string>();//以前提交过的发票
|
||||
|
List<string> newlist = new List<string>();//没有提交过的发票
|
||||
|
var pdoldlist = pdlist.Where(p => oldinvs.Contains(p.BillNum)).ToList(); |
||||
|
if (pdoldlist != null && pdoldlist.Count > 0) |
||||
|
{ |
||||
|
oldlist = pdoldlist.Select(p => p.BillNum).ToList();//已经存在扣减记录,的发票号
|
||||
|
} |
||||
|
newlist = billList.Where(p => !oldlist.Contains(p)).ToList();//不存在扣减记录的发票号
|
||||
|
List<HBPO_CAN_SA_DETAIL> entityList = new List<HBPO_CAN_SA_DETAIL>();//结算明细
|
||||
|
var namelist = invList.Select(p => p.InvbillNum).ToList(); |
||||
|
if (oldlist.Count > 0) |
||||
|
{ |
||||
|
var involdList = invList.Where(p => oldinvs.Contains(p.ParentInvbillNum)).ToList(); |
||||
|
var involdname = involdList.Select(p => p.InvbillNum); |
||||
|
var adjlist = _dbcontext.Set<PUB_ADJ_DETAIL>().Where(p => involdname.Contains(p.InvBillNum)); |
||||
|
if (adjlist != null && adjlist.Any()) |
||||
|
{ |
||||
|
foreach (var itm in adjlist) |
||||
|
{ |
||||
|
var detail = new HBPO_CAN_SA_DETAIL( |
||||
|
guid: GuidGenerator.Create(), |
||||
|
keyCode: itm.KeyCode, |
||||
|
version: itm.Version, |
||||
|
billNum: itm.InvGroupNum, |
||||
|
settleBillNum: itm.SettleBillNum, |
||||
|
lU: itm.LU, |
||||
|
pN: itm.PN, |
||||
|
site: itm.Site, |
||||
|
qty: itm.Qty, |
||||
|
price: 0, |
||||
|
businessType: itm.BusinessType, |
||||
|
settleDate: itm.SettleDate, |
||||
|
groupNum: itm.GroupNum, |
||||
|
invGroupNum: itm.InvGroupNum, |
||||
|
invbillnum: itm.InvBillNum, |
||||
|
partcode: itm.PartCode |
||||
|
); |
||||
|
detail.ErpLoc = itm.ErpLoc; |
||||
|
detail.RealPartCode = itm.RealPartCode; |
||||
|
entityList.Add(detail); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if (newlist.Count > 0) |
||||
|
{ |
||||
|
var entitys = invList.Where(p => newlist.Contains(p.InvbillNum)).ToList(); |
||||
|
var entityDetail = _dbcontext.Set<HBPO_CAN_SA_DETAIL>().Where(p => newlist.Contains(p.InvbillNum)).ToList();//结算明细
|
||||
|
var adjlist = _dbcontext.Set<PUB_ADJ_DETAIL>().Where(p => newlist.Contains(p.InvBillNum)).ToList();//发票关联调整表明细
|
||||
|
if (adjlist != null && adjlist.Count > 0) |
||||
|
{ |
||||
|
foreach (var itm in adjlist) |
||||
|
{ |
||||
|
var entity = new HBPO_CAN_SA_DETAIL( |
||||
|
guid: GuidGenerator.Create(), |
||||
|
keyCode: itm.KeyCode, |
||||
|
version: itm.Version, |
||||
|
billNum: itm.InvGroupNum, |
||||
|
settleBillNum: itm.SettleBillNum, |
||||
|
lU: itm.LU, |
||||
|
pN: itm.PN, |
||||
|
site: itm.Site, |
||||
|
qty: itm.Qty, |
||||
|
price: 0, |
||||
|
businessType: itm.BusinessType, |
||||
|
settleDate: itm.SettleDate, |
||||
|
groupNum: itm.GroupNum, |
||||
|
invGroupNum: itm.InvGroupNum, |
||||
|
invbillnum: itm.InvBillNum, |
||||
|
partcode: itm.PartCode); |
||||
|
entity.ErpLoc = itm.ErpLoc;//补字段库位
|
||||
|
entity.PartCode = itm.RealPartCode;//真实零件号
|
||||
|
entityDetail.Add(entity); |
||||
|
} |
||||
|
} |
||||
|
entityList.AddRange(entityDetail); |
||||
|
} |
||||
|
var bbacmainlist = new List<HBPO_PD>(); |
||||
|
foreach (var itm in invList) |
||||
|
{ |
||||
|
var bbac = new HBPO_PD( |
||||
|
guid: itm.Id, |
||||
|
version: int.Parse(DateTime.Now.ToString("yyyyMM")), |
||||
|
billNum: itm.InvbillNum, |
||||
|
settleBillNum: itm.InvGroupNum, |
||||
|
state: SettleBillState.客户已收票, |
||||
|
invGroupNum: itm.InvGroupNum, |
||||
|
site: string.Empty |
||||
|
); |
||||
|
bbacmainlist.Add(bbac); |
||||
|
} |
||||
|
var bbacDetail = new List<HBPO_PD_DETAIL>(); |
||||
|
foreach (var itm in entityList) |
||||
|
{ |
||||
|
var entity = new HBPO_PD_DETAIL( |
||||
|
guid: itm.Id, |
||||
|
keyCode: itm.KeyCode, |
||||
|
version: itm.Version, |
||||
|
billNum: itm.InvbillNum, |
||||
|
lU: itm.LU, |
||||
|
rELU: string.Empty, |
||||
|
pN: itm.PN, |
||||
|
rEPN: string.Empty, |
||||
|
site: itm.Site, |
||||
|
qty: itm.Qty, |
||||
|
price: 0, |
||||
|
invGroupNum: itm.InvGroupNum, |
||||
|
settleDate: itm.SettleDate, |
||||
|
groupNum: itm.GroupNum, |
||||
|
extend1: string.Empty, |
||||
|
extend2: string.Empty, |
||||
|
extend3: string.Empty, |
||||
|
extend4: itm.PartCode, |
||||
|
p_businessType: itm.BusinessType |
||||
|
); |
||||
|
entity.RELU = string.IsNullOrEmpty(itm.RealPartCode) ? itm.PartCode : itm.RealPartCode;//厂内替换零件号
|
||||
|
entity.Extend2 = itm.ErpLoc;//ERP库位
|
||||
|
bbacDetail.Add(entity |
||||
|
); |
||||
|
} |
||||
|
_dbcontext.BulkInsert(bbacmainlist); |
||||
|
_dbcontext.BulkInsert(bbacDetail); |
||||
|
foreach (var itm in invList) |
||||
|
{ |
||||
|
itm.State = SettleBillState.客户已收票; |
||||
|
}; |
||||
|
_dbcontext.BulkUpdate(invList); |
||||
|
|
||||
|
} |
||||
|
return id.ToString(); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue