diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application.Contracts/Entities/BQ/Dtos/PUB_ADJ_DETAIL_DTO.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application.Contracts/Entities/BQ/Dtos/PUB_ADJ_DETAIL_DTO.cs index 0fea4691..434afff1 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application.Contracts/Entities/BQ/Dtos/PUB_ADJ_DETAIL_DTO.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application.Contracts/Entities/BQ/Dtos/PUB_ADJ_DETAIL_DTO.cs @@ -83,8 +83,6 @@ public class PUB_ADJ_DETAIL_IMP_DTO [ImporterHeader(Name = "厂内零件号")] public string PartCode { get; set; } - - [ImporterHeader(Name = "标识号")] public string PN { get; set; } [ImporterHeader(Name = "数量")] diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Bases/ADJ_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Bases/ADJ_SERVICE.cs index bce2a72e..2ba35ed6 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Bases/ADJ_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Bases/ADJ_SERVICE.cs @@ -21,6 +21,7 @@ using Win.Sfs.SettleAccount.Entities.BQ.Dtos; using Win.Sfs.SettleAccount.Entities.BQ.Managers; using Win.Sfs.SettleAccount.ExcelImporter; using Win.Sfs.SettleAccount.ExportReports; +using Win.Sfs.SettleAccount.MaterialRelationships; using Win.Sfs.Shared.RepositoryBase; namespace Win.Sfs.SettleAccount.Bases; @@ -34,18 +35,21 @@ public class ADJ_SERVICE : BASE_SERVICE protected readonly INormalEfCoreRepository _repository; protected readonly BaseDomainService _baseDomainService; protected readonly INormalEfCoreRepository _invRepository; - + protected readonly INormalEfCoreRepository _relRepository; + public ADJ_SERVICE( INormalEfCoreRepository repository, BaseDomainService baseDomainService, IExcelImportAppService excelImportService, ISnowflakeIdGenerator snowflakeIdGenerator, ICommonManager commonManager, - INormalEfCoreRepository invRepository + INormalEfCoreRepository invRepository, + INormalEfCoreRepository relRepository ) : base(excelImportService, snowflakeIdGenerator, commonManager) { + _relRepository = relRepository; _invRepository = invRepository; _repository = repository; _baseDomainService = baseDomainService; @@ -64,6 +68,7 @@ public class ADJ_SERVICE : BASE_SERVICE var dtos = ObjectMapper.Map, List>(entitys); return new PagedResultDto(totalCount, dtos); } + /// /// 导入文件 /// @@ -81,10 +86,11 @@ public class ADJ_SERVICE : BASE_SERVICE { throw new BusinessException("8989", "导入数据记录为0条"); } - var invs=result.Select(p => p.OldInvBillNum).ToList();//所有发票号 - var deletels = _repository.Where(p =>invs.Contains(p.OldInvBillNum) ).ToList();//所有关联发票号的调整表 - var invlist= _invRepository.Where(p => invs.Contains(p.InvbillNum)).ToList();//所有发票 - var dlists= deletels.Where(p=>string.IsNullOrEmpty(p.InvBillNum)).ToList();//所有新发票号不为空的调整记录 + var relation = _relRepository.ToList(); + var invs = result.Select(p => p.OldInvBillNum).ToList();//所有发票号 + var deletels = _repository.Where(p => invs.Contains(p.OldInvBillNum)).ToList();//所有关联发票号的调整表 + var invlist = _invRepository.Where(p => invs.Contains(p.InvbillNum)).ToList();//所有发票 + var dlists = deletels.Where(p => string.IsNullOrEmpty(p.InvBillNum)).ToList();//所有新发票号不为空的调整记录 foreach (var itm in dlists) { errorList.Add(new ErrorExportDto() { ItemCode = $"{itm.LU}", Message = $"客户零件号{itm.LU}标识号{itm.PN}下线日期{itm.SettleDate}记录已经被重新开票,新发票号为:{itm.InvBillNum}" }); @@ -112,13 +118,14 @@ public class ADJ_SERVICE : BASE_SERVICE } var query = from itm in entityList join itm1 in invlist - on itm.InvBillNum equals itm1.InvbillNum + on itm.InvBillNum equals itm1.InvbillNum + join itm2 in relation on itm.LU equals itm2.SettleMaterialCode select new PUB_ADJ_DETAIL( id: GuidGenerator.Create(), keyCode: itm.PN + itm.LU, version: itm.Version, settleBillNum: itm.SettleBillNum, - lU: itm.LU, + lU: itm2.ErpMaterialCode, pN: itm.PN, site: itm.Site, qty: itm.Qty, @@ -130,13 +137,107 @@ public class ADJ_SERVICE : BASE_SERVICE groupNum: itm.GroupNum, oldinv: itm.InvBillNum, inv: string.Empty, - partcode:itm.PartCode + partcode: itm.PartCode + + + - ); await _repository.DbContext.BulkDeleteAsync(deletels);//删除发票下所有调整明细 await _repository.DbContext.BulkInsertAsync(query.ToList()); return ApplicationConsts.SuccessStr; } + + + + + + + + + + + + + + /// + /// 导入文件 + /// + /// + /// + /// + [HttpPost] + [DisableRequestSizeLimit] + public async Task CheckImport(List p_list) + { + List errorList = new List(); + //ExportImporter _exportImporter = new ExportImporter(); + //var result = await _exportImporter.UploadExcelImport(files, _excelImportService); + //if (result.Count == 0) + //{ + // throw new BusinessException("8989", "导入数据记录为0条"); + //} + // var invs= p_list.Select(p => p.InvBillNum).ToList();//所有发票号 + //var deletels = _repository.Where(p =>invs.Contains(p.OldInvBillNum) ).ToList();//所有关联发票号的调整表 + // var invlist= _invRepository.Where(p => invs.Contains(p.InvbillNum)).ToList();//所有旧发票 + // var dlists= deletels.Where(p=>string.IsNullOrEmpty(p.InvBillNum)).ToList();//所有新发票号不为空的调整记录 + //foreach (var itm in dlists) + //{ + // errorList.Add(new ERR_EXP_DTO() { ItemCode = $"{itm.LU}", Message = $"客户零件号{itm.LU}标识号{itm.PN}下线日期{itm.SettleDate}记录已经被重新开票,新发票号为:{itm.InvBillNum}" }); + //} + //var first = deletels.FirstOrDefault(); + //if (!string.IsNullOrEmpty(first.InvBillNum)) + //{ + // throw new BusinessException("8989", $"调整记录已经生成新发票号:{first.InvBillNum}"); + //} + var entityList = ObjectMapper.Map, List>(p_list); + var codelist = entityList.Select(p => p.LU).ToList(); + var errors = await _baseDomainService.CheckBase(codelist, new BASE_CONF() { IsRelationShip = true }); + foreach (var itm in errors) + { + errorList.Add( + new ERR_EXP_DTO() { ItemCode = $"{itm}", Message = $"[客户零件关系表]不存在客户零件号{itm}" }); + } + var errorEntitylist = await _baseDomainService.CheckPriceList(entityList); + if (errorEntitylist.Count > 0) + { + foreach (var itm in errorEntitylist) + { + errorList.Add(new ERR_EXP_DTO() { ItemCode = $"{itm.LU}", Message = $"客户零件号{itm.LU}标识号{itm.PN}下线日期{itm.SettleDate}不存在价格单记录" }); + } + } + if (errorList.Count > 0) + { + return await ExportErrorReportAsync(errorList); + } + //var query = from itm in entityList + // join itm1 in invlist + // on itm.InvBillNum equals itm1.InvbillNum + // select new PUB_ADJ_DETAIL( + // id: GuidGenerator.Create(), + // keyCode: itm.PN + itm.LU, + // version: itm.Version, + // settleBillNum: itm.SettleBillNum, + // lU: itm.LU, + // pN: itm.PN, + // site: itm.Site, + // qty: itm.Qty, + // extend1: string.Empty, + // price: 0, + // invGroupNum: itm1.InvGroupNum, + // settleDate: itm.SettleDate, + // businessType: itm1.BusinessType, + // groupNum: itm.GroupNum, + // oldinv: itm.InvBillNum, + // inv: string.Empty, + // partcode:itm.PartCode + + + // ); + //await _repository.DbContext.BulkDeleteAsync(deletels);//删除发票下所有调整明细 + //await _repository.DbContext.BulkInsertAsync(query.ToList()); + return ApplicationConsts.SuccessStr; + } + } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Bases/BA_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Bases/BA_SERVICE.cs index 28de6d76..038f321a 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Bases/BA_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Bases/BA_SERVICE.cs @@ -322,6 +322,71 @@ namespace Win.Sfs.SettleAccount.Bases throw new BusinessException("8989", "生成失败,请检查调整表和旧发票内容"); } + + /// + /// hbpo、jit、备件等 + /// + /// 可结算明细列表 + /// 版本号 + /// 发票分组 + /// 原发票号 + protected async Task> ReissueSecInvoiceExtend(List p_list, List p_adjlist, List p_tmplist, int p_version, string p_parentInvBillNum) where TDetail : SA_CAN_BASE + { + var ls = await _invMng.ReissueSecInvoiceExtend(p_list, p_adjlist, p_tmplist, p_parentInvBillNum, p_version); + if (ls.Count>0) + { + return ls; + } + + throw new BusinessException("8989", "生成失败,请检查调发票整表和旧发票内容"); + + + } + + /// + /// 第一次开票 + /// + /// + /// + /// + /// + /// 要作废的发票号 + /// + /// + protected async Task> ReissueFirstInvoiceExtend(List p_list, List p_adjlist, List dtos, int p_version, string p_parentInvBillNum) where TDetail : SA_CAN_BASE + { + var ls = await _invMng.ReissueFirstInvoiceExtend(p_list, p_adjlist, dtos, p_parentInvBillNum, p_version); + if (ls.Count>0) + { + return ls; + } + throw new BusinessException("8989", "生成失败,请检查调整表和旧发票内容"); + + } + + + + + + + + + + + + + + + + + + + + + + + + /// /// 校验规则 diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_BA_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_BA_SERVICE.cs index d08525e8..2f8d7b73 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_BA_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_BA_SERVICE.cs @@ -1,4 +1,5 @@ +using DocumentFormat.OpenXml.Spreadsheet; using EFCore.BulkExtensions; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -12,6 +13,7 @@ using System.Text; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Application.Services; +using Volo.Abp.ObjectMapping; using Win.Abp.Snowflakes; using Win.Sfs.BaseData.ImportExcelCommon; using Win.Sfs.SettleAccount.Bases; @@ -65,18 +67,152 @@ namespace Win.Sfs.SettleAccount.Entities.BQ /// public override async Task ReissueInvoice(string p_invbillnum) { - var mappingList= await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 - if (mappingList != null && mappingList.Count() > 0) + //var mappingList= await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ + var version = int.Parse(DateTime.Now.ToString("yyyymm")); + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + var inv = await GetInvoiceGroupByInvBillNum(p_invbillnum); + + var settle = await _bbacMng.GetMainAsync(inv.InvGroupNum); + + if (settle == null) { - var version=int.Parse(DateTime.Now.ToString("yyyymm")); - var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 - var inv= await GetInvoiceGroupByInvBillNum(p_invbillnum); + throw new UserFriendlyException($"选择发票:{p_invbillnum}对应的可结算单不在请检查!"); + + } + + version = settle.Version; + if (inv != null) + { + if (inv.InvoiceState == InvoiceBillState.报废) + { + throw new UserFriendlyException($"选择发票:{p_invbillnum}状态为报废状态不能重开!"); + } + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new UserFriendlyException($"选择发票:{p_invbillnum}状态在财务审核后可以报废重开,之前不能重开可以退回!"); + } + + var bbaclist = await _bbacMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 + var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 + if (adjlist != null && adjlist.Count() > 0) + { + foreach (var itm in adjlist) + { + bbaclist.Add(new BBAC_CAN_SA_DETAIL( + guid: itm.Id, + 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: itm.Price, + category: itm.BusinessType, + isReturn: itm.Qty > 0 ? false : true, + settleDate: itm.SettleDate, + groupNum: itm.GroupNum, + invGroupNum: itm.InvGroupNum, + contactid: itm.Extend1,//生产号 + invbillnum: string.Empty, + partcode: itm.PartCode + )); + } + } + var entitys = bbaclist;//合并库存调整单和就发票可结算明细数据 + var gNumList = entitys.Select(p => p.GroupNum).Distinct().ToList(); + //var groupNumList = entitys.Select(p => new { p.GroupNum).Distinct().ToList(); + //var notList = _notRepository.Where(p => gNumList.Contains(p.GroupNum)).ToList();//不能结算 + var priceList = _priceRepository.ToList();//价格单 + var errorList = await CheckInvoiceGenerationRules(entitys, priceList, inv.BusinessType); + if (errorList.Count() > 0) + { + return await ExportErrorReportAsync(errorList); + } + var dto1s = ObjectMapper.Map, List>(entitys); + var q = from d in dto1s + join p in priceList on d.PartCode equals p.LU + where d.SettleDate >= p.BeginTime && d.SettleDate <= p.EndTime//客户零件号 + select new TEMP_CAN_SA_DETAIL + { + Id = d.Id, + SettleBillNum = d.SettleBillNum, + Site = d.Site, + Version = d.Version, + Price = p.Price, + BillNum = d.BillNum, + SettleDate = d.SettleDate, + InvGroupNum = d.InvGroupNum, + LU = d.LU, + MaterialDesc = d.MaterialDesc, + PN = d.PN, + Qty = d.Qty, + GroupNum = d.GroupNum, + Amt = Math.Round(d.Qty * p.Price, 2), + ContractDocID = d.ContractDocID, + BeginDate = p.BeginTime, + EndDate = p.EndTime, + PartCode = d.PartCode + + }; + var dtos = q.ToList(); + if (dtos != null && dtos.Count > 0) + { + if (p_invbillnum.Substring(0, 3) == "INV")//一次开票重开 + { + await ReissueFirstInvoice(bbaclist, adjlist, dtos, version, p_invbillnum); + } + else//二次开票 + { + await ReissueSecInvoice(bbaclist, adjlist, dtos, version, p_invbillnum); + } + } + } + else + { + throw new UserFriendlyException($"不存发票号为:{p_invbillnum}发票"); + } + //} + //else + //{ + // throw new UserFriendlyException( $"不存发票号:{p_invbillnum}对应的结算分组号"); + //} + return ApplicationConsts.SuccessStr; + } + + [HttpPost] + /// + /// 发票重开 + /// + /// + /// + public virtual async Task ReissueInvoiceExtend(List p_list) + { + + + var adjlist = ObjectMapper.Map, List>(p_list); + if (adjlist == null && adjlist.Count == 0) + { + throw new UserFriendlyException($"调整表无数据!"); + } + var first = p_list.FirstOrDefault(); + var invbillnum = first.InvBillNum; + //var mappingList= await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ + var version=int.Parse(DateTime.Now.ToString("yyyymm")); + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + var inv= await GetInvoiceGroupByInvBillNum(invbillnum); var settle =await _bbacMng.GetMainAsync(inv.InvGroupNum); if(settle==null) { - throw new BusinessException("8989", $"选择发票:{p_invbillnum}对应的可结算单不在请检查!"); + throw new UserFriendlyException( $"选择发票:{invbillnum}对应的可结算单不在请检查!"); + } version=settle.Version; @@ -84,10 +220,15 @@ namespace Win.Sfs.SettleAccount.Entities.BQ { if (inv.InvoiceState == InvoiceBillState.报废) { - throw new BusinessException("8989", $"选择发票:{p_invbillnum}状态为报废状态不能重开!"); + throw new UserFriendlyException( $"选择发票:{invbillnum}状态为报废状态不能重开!"); + } + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new UserFriendlyException( $"选择发票:{invbillnum}状态在财务审核后可以报废重开,之前不能重开可以退回!"); } - var bbaclist = await _bbacMng.GetContainsAsync(inv.InvbillNum, gList);//结算分组对应结算零件 - var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 + + var bbaclist = await _bbacMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 + // var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 if (adjlist != null && adjlist.Count() > 0) { foreach (var itm in adjlist) @@ -153,26 +294,166 @@ namespace Win.Sfs.SettleAccount.Entities.BQ var dtos = q.ToList(); if (dtos != null && dtos.Count > 0) { - if (p_invbillnum.Substring(0, 3) == "INV")//一次开票重开 + if (invbillnum.Substring(0, 3) == "INV")//一次开票重开 { - await ReissueFirstInvoice(bbaclist,adjlist,dtos,version, p_invbillnum); + await ReissueFirstInvoice(bbaclist,adjlist,dtos,version, invbillnum); } else//二次开票 { - await ReissueSecInvoice(bbaclist,adjlist,dtos,version, p_invbillnum); + await ReissueSecInvoice(bbaclist,adjlist,dtos,version, invbillnum); } } } else { - throw new BusinessException("8989", $"不存发票号为:{p_invbillnum}发票"); + throw new UserFriendlyException( $"不存发票号为:{invbillnum}发票"); } - } - else + //} + //else + //{ + // throw new UserFriendlyException( $"不存发票号:{p_invbillnum}对应的结算分组号"); + //} + return ApplicationConsts.SuccessStr; + } + + [HttpPost] + /// + /// 发票重开列表 + /// + /// + /// + public virtual async Task> ReissueInvoiceList(List p_list) + { + List ls = new List(); + + var adjlist=ObjectMapper.Map, List>(p_list); + if (adjlist == null && adjlist.Count==0) { - throw new BusinessException("8989", $"不存发票号:{p_invbillnum}对应的结算分组号"); + throw new UserFriendlyException($"调整表无数据!"); } - return ApplicationConsts.SuccessStr; + var first = p_list.FirstOrDefault(); + var invbillnum = first.InvBillNum; + //var mappingList = await GetMapGroupAsync(invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ + var version = int.Parse(DateTime.Now.ToString("yyyymm")); + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + var inv = await GetInvoiceGroupByInvBillNum(invbillnum); + + var settle = await _bbacMng.GetMainAsync(inv.InvGroupNum); + + if (settle == null) + { + throw new UserFriendlyException($"选择发票:{invbillnum}对应的可结算单不在请检查!"); + } + version = settle.Version; + if (inv != null) + { + if (inv.InvoiceState == InvoiceBillState.报废) + { + throw new UserFriendlyException($"选择发票:{invbillnum}状态为报废状态不能重开!"); + } + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new UserFriendlyException($"选择发票:{invbillnum}状态在财务审核后可以报废重开,之前请退回重开!"); + } + + var bbaclist = await _bbacMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 + //var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 + if (adjlist != null && adjlist.Count() > 0) + { + foreach (var itm in adjlist) + { + bbaclist.Add(new BBAC_CAN_SA_DETAIL( + guid: itm.Id, + 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: itm.Price, + category: itm.BusinessType, + isReturn: itm.Qty > 0 ? false : true, + settleDate: itm.SettleDate, + groupNum: itm.GroupNum, + invGroupNum: itm.InvGroupNum, + contactid: itm.Extend1,//生产号 + invbillnum: string.Empty, + partcode: itm.PartCode + )); + } + } + var entitys = bbaclist;//合并库存调整单和就发票可结算明细数据 + var gNumList = entitys.Select(p => p.GroupNum).Distinct().ToList(); + + var priceList = _priceRepository.ToList();//价格单 + //var errorList = await CheckInvoiceGenerationRules(entitys, priceList, inv.BusinessType); + //if (errorList.Count() > 0) + //{ + // return await ExportErrorReportAsync(errorList); + //} + var dto1s = ObjectMapper.Map, List>(entitys); + var q = from d in dto1s + join p in priceList on d.PartCode equals p.LU + where d.SettleDate >= p.BeginTime && d.SettleDate <= p.EndTime//客户零件号 + select new TEMP_CAN_SA_DETAIL + { + Id = d.Id, + SettleBillNum = d.SettleBillNum, + Site = d.Site, + Version = d.Version, + Price = p.Price, + BillNum = d.BillNum, + SettleDate = d.SettleDate, + InvGroupNum = d.InvGroupNum, + LU = d.LU, + MaterialDesc = d.MaterialDesc, + PN = d.PN, + Qty = d.Qty, + GroupNum = d.GroupNum, + Amt = Math.Round(d.Qty * p.Price, 2), + ContractDocID = d.ContractDocID, + BeginDate = p.BeginTime, + EndDate = p.EndTime, + PartCode = d.PartCode + + }; + var dtos = q.ToList(); + if (dtos != null && dtos.Count > 0) + { + if (invbillnum.Substring(0, 3) == "INV")//一次开票重开 + { + var entities=await ReissueFirstInvoiceExtend(bbaclist, adjlist, dtos, version, invbillnum); + + ls= ObjectMapper.Map, List>(entities); + + + } + else//二次开票 + { + var entities = await ReissueSecInvoiceExtend(bbaclist, adjlist, dtos, version, invbillnum); + ls=ObjectMapper.Map, List>(entities); + + } + } + } + else + { + throw new UserFriendlyException($"不存发票号为:{invbillnum}发票"); + } + //} + //else + //{ + // throw new UserFriendlyException($"不存发票号:{p_invbillnum}对应的结算分组号"); + //} + return ls; + } + + + } } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_CAN_SA_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_CAN_SA_SERVICE.cs index d37068a0..98e2ef67 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_CAN_SA_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_CAN_SA_SERVICE.cs @@ -159,7 +159,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ } else { - throw new BusinessException("8989", string.Format("{0}不存在该可结算单号", input.BillNum)); + throw new UserFriendlyException(string.Format("{0}不存在该可结算单号", input.BillNum)); } return ApplicationConsts.SuccessStr; } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/HBPO_BA_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/HBPO_BA_SERVICE.cs index 95b97db3..0472def5 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/HBPO_BA_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/HBPO_BA_SERVICE.cs @@ -56,25 +56,29 @@ namespace Win.Sfs.SettleAccount.Entities.BQ /// public override async Task ReissueInvoice(string p_invbillnum) { - var mappingList = await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 - if (mappingList != null && mappingList.Count() > 0) - { + //var mappingList = await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ var version = int.Parse(DateTime.Now.ToString("yyyymm")); - var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 var inv = await GetInvoiceGroupByInvBillNum(p_invbillnum); var settle = await _hbpoMng.GetMainAsync(inv.InvGroupNum); if (settle == null) { - throw new BusinessException("8989", $"选择发票:{p_invbillnum}对应的可结算单不在请检查!"); + throw new UserFriendlyException( $"选择发票:{p_invbillnum}对应的可结算单不存在请检查!"); } version = settle.Version; if (inv != null) { if (inv.InvoiceState == InvoiceBillState.报废) { - throw new BusinessException("8989", $"选择发票:{p_invbillnum}状态为报废状态不能重开!"); + throw new UserFriendlyException( $"选择发票:{p_invbillnum}状态为报废状态不能重开!"); + } + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new UserFriendlyException( $"选择发票:{p_invbillnum}状态在财务审核后可以报废重开,之前请退回重开!"); } - var hbpolist = await _hbpoMng.GetContainsAsync(inv.InvbillNum, gList);//结算分组对应结算零件 + var hbpolist = await _hbpoMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 if (adjlist != null && adjlist.Count > 0) { @@ -150,15 +154,288 @@ namespace Win.Sfs.SettleAccount.Entities.BQ } else { - throw new BusinessException("8989", $"不存发票号为:{p_invbillnum}发票"); + throw new UserFriendlyException( $"不存发票号为:{p_invbillnum}发票"); + } + //} + //else + //{ + // throw new UserFriendlyException( $"不存发票号:{p_invbillnum}对应的结算分组号"); + //} + return ApplicationConsts.SuccessStr; + } + + [HttpPost] + /// + /// 发票重开 + /// + /// + /// + public virtual async Task ReissueInvoiceExtend(List p_list) + { + + var adjlist = ObjectMapper.Map, List>(p_list); + if (adjlist == null && adjlist.Count == 0) + { + throw new UserFriendlyException($"调整表无数据!"); + } + var first = p_list.FirstOrDefault(); + var invbillnum = first.InvBillNum; + + + //var mappingList = await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ + var version = int.Parse(DateTime.Now.ToString("yyyymm")); + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + var inv = await GetInvoiceGroupByInvBillNum(invbillnum); + var settle = await _hbpoMng.GetMainAsync(inv.InvGroupNum); + if (settle == null) + { + throw new UserFriendlyException($"选择发票:{invbillnum}对应的可结算单不存在请检查!"); + } + version = settle.Version; + if (inv != null) + { + if (inv.InvoiceState == InvoiceBillState.报废) + { + throw new UserFriendlyException($"选择发票:{invbillnum}状态为报废状态不能重开!"); + } + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new UserFriendlyException($"选择发票:{invbillnum}状态在财务审核后可以报废重开,之前请退回重开!"); + } + var hbpolist = await _hbpoMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 + // var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 + if (adjlist != null && adjlist.Count > 0) + { + foreach (var itm in adjlist) + { + hbpolist.Add(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: itm.Price, + businessType: itm.BusinessType, + settleDate: itm.SettleDate, + groupNum: itm.GroupNum, + invGroupNum: itm.InvGroupNum, + invbillnum: string.Empty, + partcode: itm.PartCode + )); + } + } + var entitys = hbpolist;//合并库存调整单和就发票可结算明细数据 + var gNumList = entitys.Select(p => p.GroupNum).Distinct().ToList(); + //var groupNumList = entitys.Select(p => new { p.GroupNum).Distinct().ToList(); + //var notList = _notRepository.Where(p => gNumList.Contains(p.GroupNum)).ToList();//不能结算 + var priceList = _priceRepository.ToList();//价格单 + var errorList = await CheckInvoiceGenerationRules(entitys, priceList, inv.BusinessType); + if (errorList.Count() > 0) + { + return await ExportErrorReportAsync(errorList); + } + // var dto1s = ObjectMapper.Map, List>(entitys); + var q = from d in entitys + join p in priceList on d.PartCode equals p.LU + where d.SettleDate >= p.BeginTime && d.SettleDate <= p.EndTime + select new TEMP_CAN_SA_DETAIL + { + SettleBillNum = d.SettleBillNum, + Site = d.Site, + Version = d.Version, + Price = p.Price, + BillNum = d.BillNum, + SettleDate = d.SettleDate, + InvGroupNum = d.InvGroupNum, + LU = d.LU, + // MaterialDesc = d.MaterialDesc, + PN = d.PN, + Qty = d.Qty, + GroupNum = d.GroupNum, + Amt = Math.Round(d.Qty * p.Price, 2), + ContractDocID = "n", + BeginDate = p.BeginTime, + EndDate = p.EndTime, + PartCode = d.PartCode + }; + var dtos = q.ToList(); + if (dtos != null && dtos.Count > 0) + { + if (invbillnum.Substring(0, 3) == "INV")//一次开票重开 + { + await ReissueFirstInvoice(hbpolist, adjlist, dtos, version, invbillnum); + + } + else//二次开票 + { + await ReissueSecInvoice(hbpolist, adjlist, dtos, version, invbillnum); + } } } else { - throw new BusinessException("8989", $"不存发票号:{p_invbillnum}对应的结算分组号"); + throw new UserFriendlyException($"不存发票号为:{invbillnum}发票"); } + //} + //else + //{ + // throw new UserFriendlyException( $"不存发票号:{p_invbillnum}对应的结算分组号"); + //} return ApplicationConsts.SuccessStr; } + + + + + + [HttpPost] + /// 发票重开列表 + /// + /// + /// + public virtual async Task> ReissueInvoiceList( List p_list) + { + List ls = new List(); + + var adjlist = ObjectMapper.Map, List>(p_list); + if (adjlist == null && adjlist.Count == 0) + { + throw new UserFriendlyException($"调整表无数据!"); + } + var first = p_list.FirstOrDefault(); + var invbillnum = first.InvBillNum; + + //var mappingList = await GetMapGroupAsync(invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ + var version = int.Parse(DateTime.Now.ToString("yyyymm")); + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + var inv = await GetInvoiceGroupByInvBillNum(invbillnum); + + var settle = await _hbpoMng.GetMainAsync(inv.InvGroupNum); + + if (settle == null) + { + throw new UserFriendlyException($"选择发票:{invbillnum}对应的可结算单不在请检查!"); + } + version = settle.Version; + if (inv != null) + { + if (inv.InvoiceState == InvoiceBillState.报废) + { + throw new UserFriendlyException($"选择发票:{invbillnum}状态为报废状态不能重开!"); + } + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new UserFriendlyException($"选择发票:{invbillnum}状态在财务审核后可以报废重开,之前请退回重开!"); + } + + var bbaclist = await _hbpoMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 + //var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 + if (adjlist != null && adjlist.Count() > 0) + { + foreach (var itm in adjlist) + { + bbaclist.Add(new HBPO_CAN_SA_DETAIL( + guid: itm.Id, + 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: itm.Price, + businessType: itm.BusinessType, + + settleDate: itm.SettleDate, + groupNum: itm.GroupNum, + invGroupNum: itm.InvGroupNum, + + invbillnum: string.Empty, + partcode: itm.PartCode + )); + } + } + var entitys = bbaclist;//合并库存调整单和就发票可结算明细数据 + var gNumList = entitys.Select(p => p.GroupNum).Distinct().ToList(); + + var priceList = _priceRepository.ToList();//价格单 + //var errorList = await CheckInvoiceGenerationRules(entitys, priceList, inv.BusinessType); + //if (errorList.Count() > 0) + //{ + // return await ExportErrorReportAsync(errorList); + //} + var dto1s = ObjectMapper.Map, List>(entitys); + var q = from d in dto1s + join p in priceList on d.PartCode equals p.LU + where d.SettleDate >= p.BeginTime && d.SettleDate <= p.EndTime//客户零件号 + select new TEMP_CAN_SA_DETAIL + { + Id = d.Id, + SettleBillNum = d.SettleBillNum, + Site = d.Site, + Version = d.Version, + Price = p.Price, + BillNum = d.BillNum, + SettleDate = d.SettleDate, + InvGroupNum = d.InvGroupNum, + LU = d.LU, + MaterialDesc = d.MaterialDesc, + PN = d.PN, + Qty = d.Qty, + GroupNum = d.GroupNum, + Amt = Math.Round(d.Qty * p.Price, 2), + ContractDocID = "N", + BeginDate = p.BeginTime, + EndDate = p.EndTime, + PartCode = d.PartCode + + }; + var dtos = q.ToList(); + if (dtos != null && dtos.Count > 0) + { + if (invbillnum.Substring(0, 3) == "INV")//一次开票重开 + { + var entities = await ReissueFirstInvoiceExtend(bbaclist, adjlist, dtos, version, invbillnum); + + ls = ObjectMapper.Map, List>(entities); + + + } + else//二次开票 + { + var entities = await ReissueSecInvoiceExtend(bbaclist, adjlist, dtos, version, invbillnum); + ls = ObjectMapper.Map, List>(entities); + + } + } + } + else + { + throw new UserFriendlyException($"不存发票号为:{invbillnum}发票"); + } + //} + //else + //{ + // throw new UserFriendlyException($"不存发票号:{invbillnum}对应的结算分组号"); + //} + return ls; + + } + + + + + + } } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/PUB_BA_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/PUB_BA_SERVICE.cs index 62c492dc..9212088a 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/PUB_BA_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/PUB_BA_SERVICE.cs @@ -15,6 +15,7 @@ using Win.Sfs.BaseData.ImportExcelCommon; using Win.Sfs.SettleAccount.Bases; using Win.Sfs.SettleAccount.CommonManagers; using Win.Sfs.SettleAccount.Constant; +using Win.Sfs.SettleAccount.Entities.BQ.Dtos; using Win.Sfs.SettleAccount.Entities.BQ.Managers; using Win.Sfs.SettleAccount.Entities.BQ.Temp; using Win.Sfs.SettleAccount.Entities.Prices; @@ -43,6 +44,138 @@ namespace Win.Sfs.SettleAccount.Entities.BQ } + /// + /// 提交发票 + /// + /// + /// + /// + /// + [HttpPost] + public virtual async Task ReissueInvoiceExtend(List p_list) + { + var first = p_list.FirstOrDefault(); + var invbillnum = first.InvBillNum; + + List ls = new List(); + var adjlist = ObjectMapper.Map, List>(p_list); + if (adjlist == null && adjlist.Count == 0) + { + throw new UserFriendlyException($"调整表无数据!"); + } + //var mappingList = await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ + var version = int.Parse(DateTime.Now.ToString("yyyymm")); + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + var inv = await GetInvoiceGroupByInvBillNum(invbillnum); + + var settle = await _pubMng.GetMainAsync(inv.InvGroupNum); + + if (settle == null) + { + throw new BusinessException("8989", $"选择发票:{invbillnum}对应的可结算单不在请检查!"); + } + version = settle.Version; + if (inv != null) + { + if (inv.InvoiceState == InvoiceBillState.报废) + { + throw new BusinessException("8989", $"选择发票:{invbillnum}状态为报废状态不能重开!"); + } + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new BusinessException("8989", $"选择发票:{invbillnum}状态在财务审核后可以报废重开,之前不能重开可以退回!"); + } + var publist = await _pubMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 + //var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 + if (adjlist != null && adjlist.Count() > 0) + { + foreach (var itm in adjlist) + { + publist.Add(new PUB_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: itm.Price, + businessType: itm.BusinessType, + settleDate: itm.SettleDate, + groupNum: itm.GroupNum, + invGroupNum: itm.InvGroupNum, + invbillnum: string.Empty, + partcode: itm.PartCode + + + )); + } + } + var entitys = publist;//合并库存调整单和就发票可结算明细数据 + var gNumList = entitys.Select(p => p.GroupNum).Distinct().ToList(); + //var groupNumList = entitys.Select(p => new { p.GroupNum).Distinct().ToList(); + //var notList = _notRepository.Where(p => gNumList.Contains(p.GroupNum)).ToList();//不能结算 + + var priceList = _priceRepository.ToList();//价格单 + var errorList = await CheckInvoiceGenerationRules(entitys, priceList, inv.BusinessType); + if (errorList.Count > 0) + { + return await ExportErrorReportAsync(errorList); + } + // var dto1s = ObjectMapper.Map, List>(entitys); + var q = from d in entitys + join p in priceList on d.PartCode equals p.LU + where d.SettleDate >= p.BeginTime && d.SettleDate <= p.EndTime + select new TEMP_CAN_SA_DETAIL + { + SettleBillNum = d.SettleBillNum, + Site = d.Site, + Version = d.Version, + Price = p.Price, + BillNum = d.BillNum, + SettleDate = d.SettleDate, + InvGroupNum = d.InvGroupNum, + LU = d.LU, + // MaterialDesc = d.MaterialDesc, + PN = d.PN, + Qty = d.Qty, + GroupNum = d.GroupNum, + Amt = Math.Round(d.Qty * p.Price, 2), + ContractDocID = "n", + BeginDate = p.BeginTime, + EndDate = p.EndTime, + PartCode = d.PartCode + + }; + var dtos = q.ToList(); + if (dtos != null && dtos.Count > 0) + { + if (invbillnum.Substring(0, 3) == "INV")//一次开票重开 + { + await ReissueFirstInvoice(publist, adjlist, dtos, version, invbillnum); + } + else//二次开票 + { + await ReissueSecInvoice(publist, adjlist, dtos, version, invbillnum); + } + } + } + else + { + throw new BusinessException("8989", $"不存发票号为:{invbillnum}发票"); + } + //} + //else + //{ + // throw new BusinessException("8989", $"不存发票号:{p_invbillnum}对应的结算分组号"); + //} + return ApplicationConsts.SuccessStr; + } + @@ -55,11 +188,11 @@ namespace Win.Sfs.SettleAccount.Entities.BQ public override async Task ReissueInvoice(string p_invbillnum) { - var mappingList = await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 - if (mappingList != null && mappingList.Count() > 0) - { + //var mappingList = await GetMapGroupAsync(p_invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ var version = int.Parse(DateTime.Now.ToString("yyyymm")); - var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 var inv = await GetInvoiceGroupByInvBillNum(p_invbillnum); var settle = await _pubMng.GetMainAsync(inv.InvGroupNum); @@ -75,7 +208,11 @@ namespace Win.Sfs.SettleAccount.Entities.BQ { throw new BusinessException("8989", $"选择发票:{p_invbillnum}状态为报废状态不能重开!"); } - var publist = await _pubMng.GetContainsAsync(inv.InvbillNum, gList);//结算分组对应结算零件 + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new BusinessException("8989", $"选择发票:{p_invbillnum}状态在财务审核后可以报废重开,之前不能重开可以退回!"); + } + var publist = await _pubMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 if (adjlist != null && adjlist.Count() > 0) { @@ -156,12 +293,156 @@ namespace Win.Sfs.SettleAccount.Entities.BQ { throw new BusinessException("8989", $"不存发票号为:{p_invbillnum}发票"); } - } - else + //} + //else + //{ + // throw new BusinessException("8989", $"不存发票号:{p_invbillnum}对应的结算分组号"); + //} + return ApplicationConsts.SuccessStr; + } + [HttpPost] + /// 发票重开列表 + /// + /// + /// + public virtual async Task> ReissueInvoiceList(List p_list) + { + + var first=p_list.FirstOrDefault(); + var invbillnum=first.InvBillNum; + + List ls = new List(); + var adjlist = ObjectMapper.Map, List>(p_list); + if (adjlist == null && adjlist.Count == 0) { - throw new BusinessException("8989", $"不存发票号:{p_invbillnum}对应的结算分组号"); + throw new UserFriendlyException($"调整表无数据!"); } - return ApplicationConsts.SuccessStr; + + //var mappingList = await GetMapGroupAsync(invbillnum);//发票对应结算分组 + //if (mappingList != null && mappingList.Count() > 0) + //{ + var version = int.Parse(DateTime.Now.ToString("yyyymm")); + //var gList = mappingList.Select(p => p.SettleGroupNum).ToList();//获取发票所有结算分组 + var inv = await GetInvoiceGroupByInvBillNum(invbillnum); + + var settle = await _pubMng.GetMainAsync(inv.InvGroupNum); + + if (settle == null) + { + throw new UserFriendlyException($"选择发票:{invbillnum}对应的可结算单不在请检查!"); + } + version = settle.Version; + if (inv != null) + { + if (inv.InvoiceState == InvoiceBillState.报废) + { + throw new UserFriendlyException($"选择发票:{invbillnum}状态为报废状态不能重开!"); + } + if (inv.State == SettleBillState.商务已审核 || inv.State == SettleBillState.已开票) + { + throw new UserFriendlyException($"选择发票:{invbillnum}状态在财务审核后可以报废重开,之前请退回重开!"); + } + + var bbaclist = await _pubMng.GetContainsAsync(inv.InvbillNum);//结算分组对应结算零件 + //var adjlist = await _adjRepository.Where(p => p.OldInvBillNum == inv.InvbillNum).ToListAsync();//调整表明细 + if (adjlist != null && adjlist.Count() > 0) + { + foreach (var itm in adjlist) + { + bbaclist.Add(new PUB_CAN_SA_DETAIL( + guid: itm.Id, + 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: itm.Price, + businessType: itm.BusinessType, + + settleDate: itm.SettleDate, + groupNum: itm.GroupNum, + invGroupNum: itm.InvGroupNum, + + invbillnum: string.Empty, + partcode: itm.PartCode + )); + } + } + var entitys = bbaclist;//合并库存调整单和就发票可结算明细数据 + var gNumList = entitys.Select(p => p.GroupNum).Distinct().ToList(); + + var priceList = _priceRepository.ToList();//价格单 + //var errorList = await CheckInvoiceGenerationRules(entitys, priceList, inv.BusinessType); + //if (errorList.Count() > 0) + //{ + // return await ExportErrorReportAsync(errorList); + //} + var dto1s = ObjectMapper.Map, List>(entitys); + var q = from d in dto1s + join p in priceList on d.PartCode equals p.LU + where d.SettleDate >= p.BeginTime && d.SettleDate <= p.EndTime//客户零件号 + select new TEMP_CAN_SA_DETAIL + { + Id = d.Id, + SettleBillNum = d.SettleBillNum, + Site = d.Site, + Version = d.Version, + Price = p.Price, + BillNum = d.BillNum, + SettleDate = d.SettleDate, + InvGroupNum = d.InvGroupNum, + LU = d.LU, + MaterialDesc = d.MaterialDesc, + PN = d.PN, + Qty = d.Qty, + GroupNum = d.GroupNum, + Amt = Math.Round(d.Qty * p.Price, 2), + ContractDocID = "N", + BeginDate = p.BeginTime, + EndDate = p.EndTime, + PartCode = d.PartCode + + }; + var dtos = q.ToList(); + if (dtos != null && dtos.Count > 0) + { + if (invbillnum.Substring(0, 3) == "INV")//一次开票重开 + { + var entities = await ReissueFirstInvoiceExtend(bbaclist, adjlist, dtos, version, invbillnum); + + ls = ObjectMapper.Map, List>(entities); + + + } + else//二次开票 + { + var entities = await ReissueSecInvoiceExtend(bbaclist, adjlist, dtos, version, invbillnum); + ls = ObjectMapper.Map, List>(entities); + + } + } + } + else + { + throw new UserFriendlyException($"不存发票号为:{invbillnum}发票"); + } + //} + //else + //{ + // throw new UserFriendlyException($"不存发票号:{invbillnum}对应的结算分组号"); + //} + return ls; + } + + + + + + + } } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/BBAC_CAN_SA_MNG.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/BBAC_CAN_SA_MNG.cs index 2647e5d8..8087e393 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/BBAC_CAN_SA_MNG.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/BBAC_CAN_SA_MNG.cs @@ -253,7 +253,10 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Managers return await _detailRepository.Where(p =>p.InvbillNum== p_invbillnum && p_list.Contains(p.GroupNum)).ToListAsync(); } - + public virtual async Task> GetContainsAsync(string p_invbillnum) + { + return await _detailRepository.Where(p => p.InvbillNum == p_invbillnum).ToListAsync(); + } /// /// 通过发票号获取结算数据 /// diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/HBPO_CAN_SA_MNG.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/HBPO_CAN_SA_MNG.cs index 44735d1a..a907f7fa 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/HBPO_CAN_SA_MNG.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/HBPO_CAN_SA_MNG.cs @@ -263,6 +263,17 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Managers return await _detailRepository.Where(p => p.InvbillNum == p_invbillnum && p_list.Contains(p.GroupNum)).ToListAsync(); } + /// + /// 获取发票对应结算分组所有零件 + /// + /// + /// + public virtual async Task> GetContainsAsync(string p_invbillnum) + { + return await _detailRepository.Where(p => p.InvbillNum == p_invbillnum).ToListAsync(); + } + + /// /// 通过发票号获取结算数据 diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/INV_MNG.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/INV_MNG.cs index 9c57a9de..f1f17945 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/INV_MNG.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/INV_MNG.cs @@ -28,7 +28,7 @@ using Win.Sfs.Shared.RepositoryBase; namespace Win.Sfs.SettleAccount.Entities.BQ.Managers { - public class INV_MNG : DomainService + public partial class INV_MNG : DomainService { //private readonly INormalEfCoreRepository _canRepository; private readonly INormalEfCoreRepository _repository; @@ -1385,6 +1385,11 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Managers ); invlist.Add(invbill); } + + + + + await _repository.DbContext.BulkInsertAsync(invlist); await _repository.DbContext.BulkInsertAsync(groupList); await _repository.DbContext.BulkInsertAsync(detailList); @@ -1900,10 +1905,10 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Managers switch (inv.BusinessType) { case EnumBusinessType.MaiDanJianHBPO: - invlist = await MakeInvoice(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, true); + invlist = await MakeInvoice(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, true); break; case EnumBusinessType.MaiDanJianBBAC: - invlist = await MakeInvoice(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, true); + invlist = await MakeInvoice(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, true); break; case EnumBusinessType.ZhiGongJianBBAC: invlist = await JITInvoice(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, true);//重开可以变多张发票 @@ -1926,23 +1931,6 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Managers } - - - - - //if (inv.BusinessType == EnumBusinessType.MaiDanJianHBPO) - //{ - // invlist = await MakeInvoice(p_list,p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, true); - //} - //if (inv.BusinessType == EnumBusinessType.BeiJian) - //{ - // invlist = await BJInvoice(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, true); - //} - //else - //{ - // invlist = await FirstInvoice(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType);//重开可以变多张发票 - //} - if (invlist.Count == 0) { return false; @@ -2239,10 +2227,10 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Managers switch (inv.BusinessType) { case EnumBusinessType.MaiDanJianHBPO: - invlist = await MakeInvoice(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, false); + invlist = await MakeInvoice(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, false); break; case EnumBusinessType.MaiDanJianBBAC: - invlist = await MakeInvoice(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, false); + invlist = await MakeInvoice(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, false); break; case EnumBusinessType.ZhiGongJianBBAC: invlist = await JITInvoice(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType,false);//重开可以变多张发票 diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/INV_MNG_EXT.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/INV_MNG_EXT.cs new file mode 100644 index 00000000..e9af0582 --- /dev/null +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/INV_MNG_EXT.cs @@ -0,0 +1,1284 @@ +using EFCore.BulkExtensions; +using EmptyFiles; +using Hangfire; +using Microsoft.AspNetCore.SignalR; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.EntityFrameworkCore; +using NPOI.SS.Formula.Functions; +using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; +using SettleAccount.Bases; +using SettleAccount.Domain.BQ; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Security.Policy; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Auditing; +using Volo.Abp.Data; +using Volo.Abp.Domain.Entities; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.Domain.Services; +using Win.Sfs.SettleAccount.Bases; +using Win.Sfs.SettleAccount.Entities.BQ.Temp; +using Win.Sfs.SettleAccount.Entities.Prices; +using Win.Sfs.Shared.RepositoryBase; + +namespace Win.Sfs.SettleAccount.Entities.BQ.Managers +{ + public partial class INV_MNG : DomainService + { + + + /// + /// hbpo、jit、备件等 + /// + /// 可结算明细列表 + /// 版本号 + /// 发票分组 + /// 原发票号 + public async Task> SecInvoiceExtend(List p_list, List p_ajdlist, List dtos, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType) + where TDetail : SA_CAN_BASE + + { + var detailList = new List(); + List _invls = new List(); + var groups1 = dtos.GroupBy(p => new {p.PartCode, p.LU, p.Price, p.BeginDate, p.EndDate, p.ContractDocID }).Select(p => new TMEP_INV + { + PartCode=p.Key.PartCode, + LU = p.Key.LU, + Amt = p.Sum(itm => itm.Amt), + Qty = p.Sum(itm => itm.Qty), + ContractDocID = p.Key.ContractDocID, + Price = p.Key.Price, + BeginDate = p.Key.BeginDate, + EndDate = p.Key.EndDate + }).ToList();//汇总记录不出现重复值 + var groups = dtos.GroupBy(p => new {p.PartCode, p.LU, p.Price, p.BeginDate, p.EndDate, p.ContractDocID }).Select(p => new TMEP_INV + { + PartCode = p.Key.PartCode, + LU = p.Key.LU, + Amt = p.Sum(itm => itm.Amt), + Qty = p.Sum(itm => itm.Qty), + Price = p.Key.Price, + ContractDocID = p.Key.ContractDocID, + BeginDate = p.Key.BeginDate, + EndDate = p.Key.EndDate + }).ToList();//汇总记录不出现重复值 + Dictionary> invoiceMap = new Dictionary>();//发票和发票明细关系 + foreach (var group in groups) + { + int i = groups1.Count(p => p.LU == group.LU + && p.PartCode==group.PartCode + && p.BeginDate == group.BeginDate + && p.EndDate == group.EndDate + && p.ContractDocID == group.ContractDocID + ); + if (i > 0) + { + string invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("CINV"); + List tempList = new List(); + decimal sum = group.Amt;//初始合计金额 + int partCount = 0; + foreach (var group1 in groups1) + { + if (group.LU == group1.LU + && group.PartCode == group1.PartCode + && group.BeginDate == group1.BeginDate + && group.EndDate == group1.EndDate + && group1.ContractDocID == group.ContractDocID + ) + { + tempList.Add(group1); + partCount++;//符合条件加入到零件中 + continue; + } + partCount++; + if (partCount > 30) + { + continue; + } + if (sum > 10000000) + { + break; + } + sum += group1.Amt; + tempList.Add(group1); + } + invoiceMap.Add(invoiceBillNum, tempList); + var query = from itm in groups1 //更新分组 + join itm1 in tempList + on new { itm.LU, itm.BeginDate, itm.EndDate, itm.ContractDocID ,itm.PartCode} + equals new { itm1.LU, itm1.BeginDate, itm1.EndDate, itm1.ContractDocID,itm1.PartCode } into temp + from tm in temp + where tm == null + select new TMEP_INV + { + LU = itm.LU, + Amt = itm.Amt, + Qty = itm.Qty, + BeginDate = itm.BeginDate, + ContractDocID = itm.ContractDocID, + EndDate = itm.EndDate + }; + groups1 = query.ToList(); + } + } + if (invoiceMap.Count > 0) + { + + var groupList = new List(); + var notDetialList = new List(); + + var invlist = new List(); + var salist = new List(); + var adjlist = new List(); + foreach (var group in invoiceMap) + { + var key = group.Key;//发票票号 + var ls = group.Value;//发票明细 + + //反向查找结算数据 + var query = from itm in dtos + join itm1 in ls + on new { itm.LU,itm.PartCode, itm.BeginDate, itm.EndDate } equals new { itm1.LU,itm1.PartCode, itm1.BeginDate, itm1.EndDate } + join itm2 in p_list on new { itm.PartCode,itm.PN, itm.SettleDate } equals new { itm2.PartCode,itm2.PN, itm2.SettleDate } + select itm2; + foreach (var itm in query) + { + itm.InvbillNum = key; + salist.Add(itm); + } + if (!string.IsNullOrEmpty(p_parentInvBillNum)) + { + //反向查找调整数据 + var query1 = from itm in dtos + join itm1 in ls + on new { itm.LU,itm.PartCode ,itm.BeginDate, itm.EndDate } equals new { itm1.LU,itm1.PartCode, itm1.BeginDate, itm1.EndDate } + join itm2 in p_ajdlist on new { itm.PartCode, itm.PN, itm.SettleDate } equals new { itm2.PartCode, itm2.PN, itm2.SettleDate } + select itm2; + + foreach (var itm in query1) + { + itm.InvBillNum = key; + adjlist.Add(itm); + } + + } + + List < INVOICE_WAIT_DETAIL > _entityDetailList = new List(); + foreach (var detail in ls) + { + _entityDetailList.Add( + new INVOICE_WAIT_DETAIL( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + lU: detail.LU, + qty: detail.Qty, + bussiessType: businessType, + amt: detail.Amt, + pRICE: detail.Price, + extend1: string.Empty, + extend2: string.Empty, + beginDate: detail.BeginDate, + endDate: detail.EndDate, + partcode:detail.PartCode + )); + } + if (_entityDetailList.Count > 0) + { + detailList.AddRange(_entityDetailList); + } + decimal amt = detailList.Sum(k => k.Amt); + decimal txtAmt = Math.Round(detailList.Sum(k => k.Amt), 2); + + var contractList = ls.Select(p => p.ContractDocID).Distinct(); + var _groupList = dtos.Where(p => contractList.Contains(p.ContractDocID)).GroupBy(p => new { p.GroupNum }) + .Select(p => new { GroupNum = p.Key.GroupNum, Amt = p.Sum(itm => itm.Amt) }).Distinct(); + //var _groupList = dtos.GroupBy(p => new { p.GroupNum }) + // .Select(p => new { GroupNum = p.Key.GroupNum, Amt = p.Sum(itm => itm.Amt) }).Distinct(); + List group1 = new List(); + + foreach (var en in _groupList) + { + group1.Add( + new INVOICE_MAP_GROUP( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + settleGroupNum: en.GroupNum, + amt: en.Amt, + extend1: string.Empty, + extend2: string.Empty)); + } + if (group1.Count > 0) + { + groupList.AddRange(group1); + } + + var invbill = new INVOICE_GRP + (guid: GuidGenerator.Create(), + realnvBillNum: string.Empty, + invbillNum: key, + amt: amt, + taxAmt: txtAmt, + fileName: string.Empty, + businessType: businessType, + invGroupNum: p_InvGroupNum, + state: SettleBillState.已开票, + invoiceBillState: InvoiceBillState.正常, + tax: 0, + parent: p_parentInvBillNum, + preTaxDiff: 0, + taxDiff: 0, + clientCode: string.Empty, + realAmt: 0 + ); + invlist.Add(invbill); + } + + + //await _repository.DbContext.BulkInsertAsync(invlist); + //await _repository.DbContext.BulkInsertAsync(groupList); + //await _repository.DbContext.BulkInsertAsync(detailList); + //if(salist.Count>0) + //{ + // await _repository.DbContext.BulkUpdateAsync(salist); + //} + //if (adjlist.Count > 0) + //{ + // await _repository.DbContext.BulkUpdateAsync(adjlist); + //} + + //await _repository.DbContext.BulkInsertAsync(notDetialList); + //_invls = invlist.Select(p => p.InvbillNum).ToList(); + } + return detailList; + + } + + public async Task> FirstInvoiceExtend(List p_list, List p_adjlist, List dtos, List p_notlist, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType) + where TDetail : SA_CAN_BASE + { + List _invls = new List(); + var detailList = new List(); + var _query = dtos.GroupBy(p => new { p.GroupNum }).Select(p => new { GroupNum = p.Key.GroupNum, Amt = p.Sum(itm => itm.Amt) }); + Dictionary dic = new Dictionary();//原本 + Dictionary copyDic = new Dictionary();//变换数组副本 + foreach (var itm in _query.ToList()) + { + dic.Add(itm.GroupNum, itm.Amt); + copyDic.Add(itm.GroupNum, itm.Amt); + } + Dictionary> invoiceMap = new Dictionary>(); + foreach (var itm in dic) + { + string invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); + List invoiceGroupNumList = new List();//每个发票对应的结算分组号 + List List = new List(); + decimal sum = itm.Value;//初始分组合计金额 + // List luList = dtos.Where(p => p.GroupNum == itm.Key).Select(p => p.LU).Distinct().ToList(); //初始LU种类 + if (copyDic.ContainsKey(itm.Key) == true)//是否存在分组 + { + foreach (var _itm1 in copyDic) + { + if (itm.Key == _itm1.Key)//相同结算分组项不计算,已初始化 + { + invoiceGroupNumList.Add(itm.Key); + continue; + } + //var grouplist = dtos.Where(p => p.GroupNum == _itm1.Key).Select(p => p.LU).Distinct().ToList();//每项LU种类 + //luList.AddRange(grouplist); + //luList = luList.Distinct().ToList(); + //if (luList.Count > 20)//累加零件不超过20种 + //{ + // continue; + //} + sum += _itm1.Value; + if (sum > 10000000) + { + break; + } + invoiceGroupNumList.Add(_itm1.Key);//所有条件都满足添加发票和结算分组对应关系 + } + invoiceMap.Add(invoiceBillNum, invoiceGroupNumList);//记录发票对应关系 + foreach (var rem in invoiceGroupNumList)//移除 + { + copyDic.Remove(rem); + } + } + } + if (invoiceMap.Keys.Count > 0) + { + + var groupList = new List(); + var notDetialList = new List(); + + var invlist = new List(); + var salist = new List(); + var adjlist = new List(); + + foreach (var itm in invoiceMap)//分组影响和 + { + var key = itm.Key;//发票票号 + var ls = itm.Value;//结算分组号列表 + //反向查找结算数据 + var invdetails = p_list.Where(p => ls.Contains(p.GroupNum)).ToList(); + foreach (var detail in invdetails) + { + detail.InvbillNum = key; + salist.Add(detail); + } + if (!string.IsNullOrEmpty(p_parentInvBillNum)) + { + //反向查找调整数据 + var adjdetails = p_adjlist.Where(p => ls.Contains(p.GroupNum)).ToList(); + foreach (var detail in adjdetails) + { + detail.InvBillNum = key; + adjdetails.Add(detail); + } + } + + var detailDtos = dtos.Where(p => ls.Contains(p.GroupNum)).GroupBy(p => new { p.InvGroupNum, p.PartCode, p.Price, p.BeginDate, p.EndDate, p.ContractDocID }) + .Select(itm => new + { + PartCode = itm.Key.PartCode, + InvGroupNum = itm.Key.InvGroupNum, + LU = itm.Key.PartCode, + ContactDocID = itm.Key.ContractDocID, + Price = itm.Key.Price, + Amt = Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2), + Qty = itm.Sum(k => k.Qty), + BeginDate = itm.Key.BeginDate, + EndDate = itm.Key.EndDate + }) + .ToList(); + decimal amt = detailDtos.Sum(k => k.Amt); + decimal txtAmt = Math.Round(detailDtos.Sum(k => k.Amt), 2); + var mapList = new List(); + foreach (var groupnum in ls) + { + mapList.Add(new INVOICE_MAP_GROUP( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + settleGroupNum: groupnum, + amt: 0, + extend1: string.Empty, + extend2: string.Empty + ) + ); + } + if (mapList.Count > 0) + { + groupList.AddRange(mapList); + } + List _entityDetailList = new List(); + foreach (var detail in detailDtos) + { + _entityDetailList.Add( + new INVOICE_WAIT_DETAIL( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + lU: detail.LU, + qty: detail.Qty, + bussiessType: businessType, + amt: detail.Amt, + pRICE: detail.Price, + extend1: string.Empty, + extend2: string.Empty, + beginDate: detail.BeginDate, + endDate: detail.EndDate, + partcode: detail.PartCode + )); + } + if (_entityDetailList.Count > 0) + { + detailList.AddRange(_entityDetailList); + } + var innotls = new List(); + if (p_notlist != null && p_notlist.Count > 0) + { + var notls = p_notlist.GroupBy(p => new { p.GroupNum, p.LU }).Select(p => new { GroupNum = p.Key.GroupNum, LU = p.Key.LU, Qty = p.Sum(itm => itm.Qty) }); + foreach (var nitm in notls) + { + innotls.Add(new INVOICE_NOT_SETTLE( + guid: GuidGenerator.Create(), + version: p_version, + invGroupNum: p_InvGroupNum, + settleGroupNum: nitm.GroupNum, + lU: string.Empty, + lU1: nitm.LU, + extend1: string.Empty, + extend2: string.Empty, + qty: nitm.Qty + )); + } + if (innotls.Count > 0) + { + notDetialList.AddRange(innotls); + } + } + var invbill = new INVOICE_GRP + (guid: GuidGenerator.Create(), + realnvBillNum: string.Empty, + invbillNum: key, + amt: amt, + taxAmt: txtAmt, + fileName: string.Empty, + businessType: businessType, + invGroupNum: p_InvGroupNum, + state: SettleBillState.已开票, + invoiceBillState: InvoiceBillState.正常, + tax: 0, + parent: string.Empty, + preTaxDiff: 0, + taxDiff: 0, + clientCode: string.Empty, + realAmt: 0 + ); + invlist.Add(invbill); + } + //await _repository.DbContext.BulkInsertAsync(invlist); + //await _repository.DbContext.BulkInsertAsync(groupList); + //await _repository.DbContext.BulkInsertAsync(detailList); + + //if (salist.Count > 0) + //{ + // await _repository.DbContext.BulkUpdateAsync(salist); + //} + //if (adjlist.Count > 0) + //{ + // await _repository.DbContext.BulkUpdateAsync(adjlist); + //} + //if (notDetialList.Count > 0) + //{ + // await _repository.DbContext.BulkInsertAsync(notDetialList); + //} + //_invls = invlist.Select(p => p.InvbillNum).ToList(); + } + + return detailList; + } + /// + /// hbpojis + /// + /// 可结算明细列表 + /// 版本号 + /// 发票分组 + /// 原发票号 + public async Task> MakeInvoiceExtend(List details,List p_adjlist, List dtos, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType, bool p_first) where TDetail : SA_CAN_BASE + { + List _invls = new List(); + var detailList = new List(); + var groups1 = dtos.GroupBy(p => new { p.PartCode,p.LU, p.Price, p.BeginDate, p.EndDate, p.ContractDocID }).Select(p => new TMEP_INV + { + PartCode=p.Key.PartCode, + LU = p.Key.LU, + Amt = p.Sum(itm => itm.Amt), + Qty = p.Sum(itm => itm.Qty), + ContractDocID = p.Key.ContractDocID, + Price = p.Key.Price, + BeginDate = p.Key.BeginDate, + EndDate = p.Key.EndDate + }).ToList();//汇总记录不出现重复值 + var groups = dtos.GroupBy(p => new {p.PartCode, p.LU, p.Price, p.BeginDate, p.EndDate, p.ContractDocID }).Select(p => new TMEP_INV + { + PartCode=p.Key.PartCode, + LU = p.Key.LU, + Amt = p.Sum(itm => itm.Amt), + Qty = p.Sum(itm => itm.Qty), + Price = p.Key.Price, + ContractDocID = p.Key.ContractDocID, + BeginDate = p.Key.BeginDate, + EndDate = p.Key.EndDate + }).ToList();//汇总记录不出现重复值 + Dictionary> invoiceMap = new Dictionary>();//发票和发票明细关系 + foreach (var group in groups) + { + int i = groups1.Count(p => p.LU == group.LU + && p.PartCode==group.PartCode + && p.BeginDate == group.BeginDate + && p.EndDate == group.EndDate + && p.ContractDocID == group.ContractDocID + ); + if (i > 0) + { + string invoiceBillNum = string.Empty; + if (p_first == true) + { + invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); + } + else + { + invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("CINV"); + } + List tempList = new List(); + decimal sum = group.Amt;//初始合计金额 + int partCount = 0; + foreach (var group1 in groups1) + { + if (group.LU == group1.LU + && group.BeginDate == group1.BeginDate + && group.EndDate == group1.EndDate + && group1.ContractDocID == group.ContractDocID + && group1.PartCode== group1.PartCode + ) + { + tempList.Add(group1); + partCount++;//符合条件加入到零件中 + continue; + } + partCount++; + if (partCount > 30) + { + continue; + } + if (sum > 10000000) + { + break; + } + sum += group1.Amt; + tempList.Add(group1); + } + invoiceMap.Add(invoiceBillNum, tempList);//每张发票的明细记录 + var query = from itm in groups1 + join itm1 in tempList + on new { itm.LU, itm.BeginDate, itm.EndDate, itm.ContractDocID ,itm.PartCode} + equals new { itm1.LU, itm1.BeginDate, itm1.EndDate, itm1.ContractDocID,itm1.PartCode } into temp + from tm in temp + where tm == null + select new TMEP_INV + { + PartCode= itm.PartCode, + LU = itm.LU, + Amt = itm.Amt, + Qty = itm.Qty, + BeginDate = itm.BeginDate, + ContractDocID = itm.ContractDocID, + EndDate = itm.EndDate + }; + groups1 = query.ToList(); + } + } + if (invoiceMap.Count > 0) + { + var groupList = new List(); + var notDetialList = new List(); + + var invlist = new List(); + var salist = new List(); + var adjlist = new List(); + foreach (var group in invoiceMap) + { + var key = group.Key;//发票票号 + var ls = group.Value;//发票明细 + + //更新结算记录更新 + var query = from itm in dtos + join itm1 in ls + on new { LU=itm.PartCode, itm.BeginDate, itm.EndDate } equals new { itm1.LU, itm1.BeginDate, itm1.EndDate } + join itm2 in details on new { itm.LU,itm.PartCode,itm.PN, itm.SettleDate } equals new { itm2.LU,itm2.PartCode,itm2.PN,itm2.SettleDate } + select itm2; + foreach (var itm in query) + { + itm.InvbillNum = key; + salist.Add(itm); + } + if (!string.IsNullOrEmpty(p_parentInvBillNum))//发票作废开票 + { + var query1 = from itm in dtos + join itm1 in ls + on new {LU= itm.PartCode, itm.BeginDate, itm.EndDate } equals new { itm1.LU, itm1.BeginDate, itm1.EndDate } + join itm2 in p_adjlist on new { itm.LU, itm.SettleDate } equals new { itm2.LU, itm2.SettleDate } + select itm2; + + foreach (var itm in query1) + { + itm.InvBillNum = key; + adjlist.Add(itm); + } + } + + List _entityDetailList = new List(); + foreach (var detail in ls) + { + _entityDetailList.Add( + new INVOICE_WAIT_DETAIL( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + lU: detail.LU, + qty: detail.Qty, + bussiessType: businessType, + amt: detail.Amt, + pRICE: detail.Price, + extend1: string.Empty, + extend2: string.Empty, + beginDate: detail.BeginDate, + endDate: detail.EndDate, + partcode:detail.PartCode + + )); + } + if (_entityDetailList.Count > 0) + { + detailList.AddRange(_entityDetailList); + } + decimal amt = detailList.Sum(k => k.Amt); + decimal txtAmt = Math.Round(detailList.Sum(k => k.Amt), 2); + + + + var invbill = new INVOICE_GRP + (guid: GuidGenerator.Create(), + realnvBillNum: string.Empty, + invbillNum: key, + amt: amt, + taxAmt: txtAmt, + fileName: string.Empty, + businessType: businessType, + invGroupNum: p_InvGroupNum, + state: SettleBillState.已开票, + invoiceBillState: InvoiceBillState.正常, + tax: 0, + parent: p_parentInvBillNum, + preTaxDiff:0, + taxDiff:0, + clientCode:string.Empty, + realAmt:0 + + ); + invlist.Add(invbill); + } + //await _repository.DbContext.BulkInsertAsync(invlist); + ////await _repository.DbContext.BulkInsertAsync(groupList); + //await _repository.DbContext.BulkInsertAsync(detailList); + //if (salist.Count > 0) + //{ + // await _repository.DbContext.BulkUpdateAsync(salist); + //} + //if (adjlist.Count > 0) + //{ + // await _repository.DbContext.BulkUpdateAsync(adjlist); + //} + //await _repository.DbContext.BulkInsertAsync(notDetialList); + //_invls = invlist.Select(p => p.InvbillNum).ToList(); + } + return detailList; + } + /// + /// 第一次开票 + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> JITInvoiceExtend(List p_list,List p_adjlist, List dtos, List p_notlist, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType,bool p_first) + where TDetail:SA_CAN_BASE + { + List _invls = new List(); + var detailList = new List(); + var _query = dtos.GroupBy(p => new { p.GroupNum }).Select(p => new { GroupNum = p.Key.GroupNum, Amt = p.Sum(itm => itm.Amt) }); + Dictionary dic = new Dictionary();//原本 + Dictionary copyDic = new Dictionary();//变换数组副本 + foreach (var itm in _query.ToList()) + { + dic.Add(itm.GroupNum, itm.Amt); + copyDic.Add(itm.GroupNum, itm.Amt); + } + Dictionary> invoiceMap = new Dictionary>(); + foreach (var itm in dic) + { + string invoiceBillNum = string.Empty; + if (p_first == true) + { + invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); + } + else + { + invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("CINV"); + } + + List invoiceGroupNumList = new List();//每个发票对应的结算分组号 + List List = new List(); + decimal sum = itm.Value;//初始分组合计金额 + // List luList = dtos.Where(p => p.GroupNum == itm.Key).Select(p => p.LU).Distinct().ToList(); //初始LU种类 + if (copyDic.ContainsKey(itm.Key) == true)//是否存在分组 + { + foreach (var _itm1 in copyDic) + { + if (itm.Key == _itm1.Key)//相同结算分组项不计算,已初始化 + { + invoiceGroupNumList.Add(itm.Key); + continue; + } + //var grouplist = dtos.Where(p => p.GroupNum == _itm1.Key).Select(p => p.LU).Distinct().ToList();//每项LU种类 + //luList.AddRange(grouplist); + //luList = luList.Distinct().ToList(); + //if (luList.Count > 20)//累加零件不超过20种 + //{ + // continue; + //} + sum += _itm1.Value; + if (sum > 10000000) + { + break; + } + invoiceGroupNumList.Add(_itm1.Key);//所有条件都满足添加发票和结算分组对应关系 + } + invoiceMap.Add(invoiceBillNum, invoiceGroupNumList);//记录发票对应关系 + foreach (var rem in invoiceGroupNumList)//移除 + { + copyDic.Remove(rem); + } + } + } + if (invoiceMap.Keys.Count > 0) + { + + var groupList = new List(); + var notDetialList = new List(); + + var invlist = new List(); + var salist = new List(); + var adjlist = new List(); + + foreach (var itm in invoiceMap)//分组影响和 + { + var key = itm.Key;//发票票号 + var ls = itm.Value;//结算分组号列表 + //反向查找结算数据 + var invdetails = p_list.Where(p => ls.Contains(p.GroupNum)).ToList(); + foreach (var detail in invdetails) + { + detail.InvbillNum = key; + salist.Add(detail); + } + if (!string.IsNullOrEmpty(p_parentInvBillNum)) + { + //反向查找调整数据 + var adjdetails = p_adjlist.Where(p => ls.Contains(p.GroupNum)).ToList(); + foreach (var detail in adjdetails) + { + detail.InvBillNum = key; + adjdetails.Add(detail); + } + } + + var detailDtos = dtos.Where(p => ls.Contains(p.GroupNum)).GroupBy(p => new { p.InvGroupNum, p.PartCode, p.Price, p.BeginDate, p.EndDate, p.ContractDocID }) + .Select(itm => new + { + PartCode=itm.Key.PartCode, + InvGroupNum = itm.Key.InvGroupNum, + LU = itm.Key.PartCode, + ContactDocID = itm.Key.ContractDocID, + Price = itm.Key.Price, + Amt = Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2), + Qty = itm.Sum(k => k.Qty), + BeginDate = itm.Key.BeginDate, + EndDate = itm.Key.EndDate + }) + .ToList(); + decimal amt = detailDtos.Sum(k => k.Amt); + decimal txtAmt = Math.Round(detailDtos.Sum(k => k.Amt), 2); + var mapList = new List(); + foreach (var groupnum in ls) + { + mapList.Add(new INVOICE_MAP_GROUP( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + settleGroupNum: groupnum, + amt: 0, + extend1: string.Empty, + extend2: string.Empty + ) + ); + } + if (mapList.Count > 0) + { + groupList.AddRange(mapList); + } + List _entityDetailList = new List(); + foreach (var detail in detailDtos) + { + _entityDetailList.Add( + new INVOICE_WAIT_DETAIL( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + lU: detail.LU, + qty: detail.Qty, + bussiessType: businessType, + amt: detail.Amt, + pRICE: detail.Price, + extend1: string.Empty, + extend2: string.Empty, + beginDate: detail.BeginDate, + endDate: detail.EndDate, + partcode:detail.PartCode + )); + } + if (_entityDetailList.Count > 0) + { + detailList.AddRange(_entityDetailList); + } + var innotls = new List(); + if (p_notlist != null && p_notlist.Count > 0) + { + var notls = p_notlist.GroupBy(p => new { p.GroupNum, p.LU }).Select(p => new { GroupNum = p.Key.GroupNum, LU = p.Key.LU, Qty = p.Sum(itm => itm.Qty) }); + foreach (var nitm in notls) + { + innotls.Add(new INVOICE_NOT_SETTLE( + guid: GuidGenerator.Create(), + version: p_version, + invGroupNum: p_InvGroupNum, + settleGroupNum: nitm.GroupNum, + lU: string.Empty, + lU1: nitm.LU, + extend1: string.Empty, + extend2: string.Empty, + qty: nitm.Qty + )); + } + if (innotls.Count > 0) + { + notDetialList.AddRange(innotls); + } + } + var invbill = new INVOICE_GRP + (guid: GuidGenerator.Create(), + realnvBillNum: string.Empty, + invbillNum: key, + amt: amt, + taxAmt: txtAmt, + fileName: string.Empty, + businessType: businessType, + invGroupNum: p_InvGroupNum, + state: SettleBillState.已开票, + invoiceBillState: InvoiceBillState.正常, + tax: 0, + parent: string.Empty, + preTaxDiff: 0, + taxDiff: 0, + clientCode: string.Empty, + realAmt: 0 + ); + invlist.Add(invbill); + } + //await _repository.DbContext.BulkInsertAsync(invlist); + //await _repository.DbContext.BulkInsertAsync(groupList); + //await _repository.DbContext.BulkInsertAsync(detailList); + + //if(salist.Count>0) + //{ + // await _repository.DbContext.BulkUpdateAsync(salist); + //} + //if (adjlist.Count > 0) + //{ + // await _repository.DbContext.BulkUpdateAsync(adjlist); + //} + //if (notDetialList.Count > 0) + //{ + // await _repository.DbContext.BulkInsertAsync(notDetialList); + //} + //_invls = invlist.Select(p => p.InvbillNum).ToList(); + } + + return detailList; + } + + /// + /// 重开一次开票 + /// + /// + /// + /// + /// + /// + public async Task> ReissueFirstInvoiceExtend(List p_list,List p_adjlist, List dtos, string p_OldInvBillNum, int p_version) + where TDetail:SA_CAN_BASE + { + List invlist = new List(); + if (!string.IsNullOrEmpty(p_OldInvBillNum)) + { + + var inv = await GetInvoiceAsync(p_OldInvBillNum); + if (inv == null) + { + + throw new UserFriendlyException( $"找不到选择要重开发票号{p_OldInvBillNum}不能重开"); + } + if (inv.InvoiceState == InvoiceBillState.报废) + { + throw new UserFriendlyException($"选择要重开发票号{p_OldInvBillNum}已经作废不能重开"); + } + inv.InvoiceState = InvoiceBillState.报废; + + + + switch (inv.BusinessType) + { + case EnumBusinessType.MaiDanJianHBPO: + invlist = await MakeInvoiceExtend(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, true); + break; + case EnumBusinessType.MaiDanJianBBAC: + invlist = await MakeInvoiceExtend(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, true); + break; + case EnumBusinessType.ZhiGongJianBBAC: + invlist = await JITInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, true);//重开可以变多张发票 + break; + case EnumBusinessType.ZhiGongJianHBPO: + invlist = await JITInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, true);//重开可以变多张发票 + break; + case EnumBusinessType.BeiJian: + invlist = await BJInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, true); + break; + case EnumBusinessType.JisBBAC: + invlist = await FirstInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType);//重开可以变多张发票 + break; + case EnumBusinessType.JisHBPO: + invlist = await FirstInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType);//重开可以变多张发票 + break; + case EnumBusinessType.YinDuJian: + invlist = await JITInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, true);//重开可以变多张发票 + break; + + } + + } + return invlist; + + + + } + + public async Task> BJInvoiceExtend(List p_list, List p_adjlist, List dtos, List p_notlist, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType,bool p_first=true) + where TDetail : SA_CAN_BASE + { + List _invls = new List(); + var detailList = new List(); + var _query = dtos.GroupBy(p => new { p.GroupNum }).Select(p => new { GroupNum = p.Key.GroupNum, Amt = p.Sum(itm => itm.Amt) }); + Dictionary dic = new Dictionary();//原本 + Dictionary copyDic = new Dictionary();//变换数组副本 + foreach (var itm in _query.ToList()) + { + dic.Add(itm.GroupNum, itm.Amt); + copyDic.Add(itm.GroupNum, itm.Amt); + } + Dictionary> invoiceMap = new Dictionary>(); + foreach (var itm in dic) + { + string invoiceBillNum = string.Empty; + if (p_first == true) + { + invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); + } + else + { + invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("CINV"); + } + + List invoiceGroupNumList = new List();//每个发票对应的结算分组号 + List List = new List(); + decimal sum = itm.Value;//初始分组合计金额 + // List luList = dtos.Where(p => p.GroupNum == itm.Key).Select(p => p.LU).Distinct().ToList(); //初始LU种类 + if (copyDic.ContainsKey(itm.Key) == true)//是否存在分组 + { + foreach (var _itm1 in copyDic) + { + if (itm.Key == _itm1.Key)//相同结算分组项不计算,已初始化 + { + invoiceGroupNumList.Add(itm.Key); + continue; + } + //var grouplist = dtos.Where(p => p.GroupNum == _itm1.Key).Select(p => p.LU).Distinct().ToList();//每项LU种类 + //luList.AddRange(grouplist); + //luList = luList.Distinct().ToList(); + //if (luList.Count > 20)//累加零件不超过20种 + //{ + // continue; + //} + sum += _itm1.Value; + if (sum > 10000000) + { + break; + } + invoiceGroupNumList.Add(_itm1.Key);//所有条件都满足添加发票和结算分组对应关系 + } + invoiceMap.Add(invoiceBillNum, invoiceGroupNumList);//记录发票对应关系 + foreach (var rem in invoiceGroupNumList)//移除 + { + copyDic.Remove(rem); + } + } + } + if (invoiceMap.Keys.Count > 0) + { + + var groupList = new List(); + var notDetialList = new List(); + + var invlist = new List(); + var salist = new List(); + var adjlist = new List(); + + foreach (var itm in invoiceMap)//分组影响和 + { + var key = itm.Key;//发票票号 + var ls = itm.Value;//结算分组号列表 + //反向查找结算数据 + var invdetails = p_list.Where(p => ls.Contains(p.GroupNum)).ToList(); + foreach (var detail in invdetails) + { + detail.InvbillNum = key; + salist.Add(detail); + } + if (!string.IsNullOrEmpty(p_parentInvBillNum)) + { + //反向查找调整数据 + var adjdetails = p_adjlist.Where(p => ls.Contains(p.GroupNum)).ToList(); + foreach (var detail in adjdetails) + { + detail.InvBillNum = key; + adjdetails.Add(detail); + } + } + + var detailDtos = dtos.Where(p => ls.Contains(p.GroupNum)).GroupBy(p => new { p.InvGroupNum, p.PartCode, p.Price, p.BeginDate, p.EndDate, p.ContractDocID }) + .Select(itm => new + { + PartCode = itm.Key.PartCode, + InvGroupNum = itm.Key.InvGroupNum, + LU = itm.Key.PartCode, + ContactDocID = itm.Key.ContractDocID, + Price = itm.Key.Price, + Amt = Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2), + Qty = itm.Sum(k => k.Qty), + BeginDate = itm.Key.BeginDate, + EndDate = itm.Key.EndDate + }) + .ToList(); + + var query = from inv1 in invdetails + join dto1 in dtos on + new { inv1.LU, inv1.PN } equals new { dto1.LU, dto1.PN } + select new + { + PartCode = inv1.PartCode, + InvGroupNum = inv1.InvGroupNum, + LU = inv1.LU, + GroupNum=inv1.GroupNum, + Price = inv1.Price, + Amt = dto1.Amt, + Qty = dto1.Qty, + BeginDate = dto1.BeginDate, + EndDate = dto1.EndDate, + DeliveryNumber = inv1.GetProperty("DeliveryNumber", ""),//交货号 + InvoiceNumber = inv1.GetProperty("InvoiceNumber", ""),//发票号 + VendorCode = inv1.GetProperty("VendorCode", ""),//供应商代码 + VendorName = inv1.GetProperty("VendorName", ""),//供应商名称 + PurchaseOrderNumber = inv1.GetProperty("PurchaseOrderNumber", ""),//采购订单号 + DeliveryIndexNumber = inv1.GetProperty("DeliveryIndexNumber", ""),//交付索引号 + PartName = inv1.GetProperty("PartName", ""),//零件名称 + }; + + decimal amt = detailDtos.Sum(k => k.Amt); + decimal txtAmt = Math.Round(detailDtos.Sum(k => k.Amt), 2); + var mapList = new List(); + foreach (var groupnum in ls) + { + mapList.Add(new INVOICE_MAP_GROUP( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + settleGroupNum: groupnum, + amt: 0, + extend1: string.Empty, + extend2: string.Empty + ) + ); + } + if (mapList.Count > 0) + { + groupList.AddRange(mapList); + } + List _entityDetailList = new List(); + foreach (var detail in query.ToList()) + { + + var entity = new INVOICE_WAIT_DETAIL( + guid: GuidGenerator.Create(), + version: p_version, + invbillNum: key, + invGroupNum: p_InvGroupNum, + lU: detail.LU, + qty: detail.Qty, + bussiessType: businessType, + amt: detail.Amt, + pRICE: detail.Price, + extend1: detail.GroupNum, + extend2: string.Empty, + beginDate: detail.BeginDate, + endDate: detail.EndDate, + partcode: detail.PartCode); + entity.SetProperty("DeliveryNumber", detail.DeliveryIndexNumber);//交货号 + entity.SetProperty("VendorCode", detail.VendorCode);//供应商代码 + entity.SetProperty("VendorName", detail.VendorName);//供应商名称 + entity.SetProperty("PurchaseOrderNumber", "");//采购订单号 + entity.SetProperty("DeliveryIndexNumber", "");//交付索引号 + entity.SetProperty("PartName", "");//零件名称 + + _entityDetailList.Add( entity + + ); + } + if (_entityDetailList.Count > 0) + { + detailList.AddRange(_entityDetailList); + } + //var innotls = new List(); + //if (p_notlist != null && p_notlist.Count > 0) + //{ + // var notls = p_notlist.GroupBy(p => new { p.GroupNum, p.LU }).Select(p => new { GroupNum = p.Key.GroupNum, LU = p.Key.LU, Qty = p.Sum(itm => itm.Qty) }); + // foreach (var nitm in notls) + // { + // innotls.Add(new INVOICE_NOT_SETTLE( + // guid: GuidGenerator.Create(), + // version: p_version, + // invGroupNum: p_InvGroupNum, + // settleGroupNum: nitm.GroupNum, + // lU: string.Empty, + // lU1: nitm.LU, + // extend1: string.Empty, + // extend2: string.Empty, + // qty: nitm.Qty + // )); + // } + // if (innotls.Count > 0) + // { + // notDetialList.AddRange(innotls); + // } + //} + var invbill = new INVOICE_GRP + (guid: GuidGenerator.Create(), + realnvBillNum: string.Empty, + invbillNum: key, + amt: amt, + taxAmt: txtAmt, + fileName: string.Empty, + businessType: businessType, + invGroupNum: p_InvGroupNum, + state: SettleBillState.已开票, + invoiceBillState: InvoiceBillState.正常, + tax: 0, + parent: string.Empty, + preTaxDiff: 0, + taxDiff: 0, + clientCode: string.Empty, + realAmt: 0 + ); + invlist.Add(invbill); + } + //await _repository.DbContext.BulkInsertAsync(invlist); + //await _repository.DbContext.BulkInsertAsync(groupList); + //await _repository.DbContext.BulkInsertAsync(detailList); + + //if (salist.Count > 0) + //{ + // await _repository.DbContext.BulkUpdateAsync(salist); + //} + //if (adjlist.Count > 0) + //{ + // await _repository.DbContext.BulkUpdateAsync(adjlist); + //} + //if (notDetialList.Count > 0) + //{ + // await _repository.DbContext.BulkInsertAsync(notDetialList); + //} + //_invls = invlist.Select(p => p.InvbillNum).ToList(); + } + + return detailList; + } + /// + /// 重开二次开票 + /// + /// + /// + /// + /// + /// + public async Task> ReissueSecInvoiceExtend(List p_list, List p_adjlist, List dtos, string p_OldInvBillNum, int p_version) + where TDetail : SA_CAN_BASE + { + List invlist = new List(); + if (!string.IsNullOrEmpty(p_OldInvBillNum)) + { + var inv = await GetInvoiceAsync(p_OldInvBillNum); + if (inv == null) + { + + throw new BusinessException("8989", $"找不到选择要重开发票号{p_OldInvBillNum}不能重开"); + } + if (inv.InvoiceState == InvoiceBillState.报废) + { + throw new BusinessException("8989", $"选择要重开发票号{p_OldInvBillNum}已经作废不能重开"); + } + inv.InvoiceState = InvoiceBillState.报废; + + + + switch (inv.BusinessType) + { + case EnumBusinessType.MaiDanJianHBPO: + invlist = await MakeInvoiceExtend(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, false); + break; + case EnumBusinessType.MaiDanJianBBAC: + invlist = await MakeInvoiceExtend(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, string.Empty, inv.BusinessType, false); + break; + case EnumBusinessType.ZhiGongJianBBAC: + invlist = await JITInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, false);//重开可以变多张发票 + break; + case EnumBusinessType.ZhiGongJianHBPO: + invlist = await JITInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, false);//重开可以变多张发票 + break; + case EnumBusinessType.BeiJian: + invlist = await BJInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, false); + break; + case EnumBusinessType.JisBBAC: + invlist = await SecInvoiceExtend(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType); + break; + case EnumBusinessType.JisHBPO: + invlist = await SecInvoiceExtend(p_list, p_adjlist, dtos, p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType); + break; + case EnumBusinessType.YinDuJian: + invlist = await JITInvoiceExtend(p_list, p_adjlist, dtos, new List(), p_version, inv.InvGroupNum, inv.InvbillNum, inv.BusinessType, false);//重开可以变多张发票 + break; + + } + + + } + return invlist; + + } + } +} diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/PUB_CAN_SA_MNG.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/PUB_CAN_SA_MNG.cs index 3ee1cd84..6ae5ec15 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/PUB_CAN_SA_MNG.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Managers/PUB_CAN_SA_MNG.cs @@ -255,6 +255,17 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Managers } + /// + /// 获取发票对应结算分组所有零件 + /// + /// + /// + public virtual async Task> GetContainsAsync(string p_invbillnum) + { + return await _detailRepository.Where(p => p.InvbillNum == p_invbillnum).ToListAsync(); + } + + /// /// 通过发票号获取结算数据 diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Temp/TempClass.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Temp/TempClass.cs index 5837ceb3..99369b3e 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Temp/TempClass.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Temp/TempClass.cs @@ -5,6 +5,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using SettleAccount.Bases; +using SettleAccount.Domain.BQ; +using Volo.Abp.Application.Dtos; namespace Win.Sfs.SettleAccount.Entities.BQ.Temp; public class TMEP_INV @@ -27,6 +29,27 @@ public class TMEP_INV /// public DateTime EndDate { get; set; } } +public class INVOICE_GRP_DETAIL +{ + /// + /// 发票明细 + /// + public List INVOICE_DETAIL { get; set; } + /// + /// 发票和结算分组对应关系 + /// + public List INVOICE_MAP_GROUP { get; set; } + + /// + /// 发票调整明细 + /// + public List ADJ_DETAIL { get; set; } + +} + + + + public class TEMP_CAN_SA_DETAIL {