From 83e7eae679a1acb3dbaf83570455bc98d0772bdf Mon Sep 17 00:00:00 2001 From: zhaoxinyu <89237069@qq.com> Date: Thu, 11 Jul 2024 15:33:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BQ/Syncs/GenerateJisInvoiceService.cs | 1202 +++-------------- .../Entities/BQ/INVOICE_GRP.cs | 132 ++ 2 files changed, 290 insertions(+), 1044 deletions(-) diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/GenerateJisInvoiceService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/GenerateJisInvoiceService.cs index 7c028fc3..76ab8092 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/GenerateJisInvoiceService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/GenerateJisInvoiceService.cs @@ -68,7 +68,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs where d.SettleDate >= p.BeginTime && d.SettleDate <= p.EndTime && p.IsCancel == false && p.ClientCode == main.Site select new TEMP_CAN_SA_DETAIL { - Id = d.Id, + Id = d.Id, SettleBillNum = d.SettleBillNum, Site = d.Site, Version = d.Version, @@ -153,14 +153,14 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs entitys = entitys.OrderBy(p => p.LU).ToList(); - - var invs = FirstInvoiceExtend1(entitys, new List(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); - // var invs = SecInvoice(entitys, new List(), dtos, main.Version, main.InvGroupNum, string.Empty, main.BusinessType, query.ToList()); + + var invs = SecInvoice(entitys, new List(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); + // var invs = SecInvoice(entitys, new List(), dtos, main.Version, main.InvGroupNum, string.Empty, main.BusinessType, query.ToList()); if (invs.Count > 0) { main.State = SettleBillState.已开票; @@ -184,721 +184,100 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs var q = from d in dto1s join p in priceList on d.LU equals p.LU where d.SettleDate >= p.BeginTime && d.SettleDate <= p.EndTime && p.IsCancel == false && p.ClientCode == main.Site - 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 (billNum.Substring(0, 1) == "C")//一次开票 - { - var notlist = notQuery.Select(p => new TEMP_NOT_SA_DETAIL - { - KeyCode = p.KeyCode, - Version = p.Version, - SettleBillNum = p.SettleBillNum, - LU = p.LU, - PN = p.PN, - Site = p.Site, - Qty = p.Qty, - Price = p.Price, - BusinessType = p.BusinessType, - IsReturn = "", - InvGroupNum = p.InvGroupNum, - SettleDate = p.SettleDate, - GroupNum = p.GroupNum, - ContractDocID = string.Empty, - PartCode = p.PartCode - - }).ToList();//不能结算 - var invs = FirstInvoice(entitys, new List(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); - if (invs.Count > 0) - { - main.State = SettleBillState.已开票; - } - } - else//二次开票 - { - var query = from b in _dbcontext.Set() - join a in (from d in _dbcontext.Set() - where d.BillNum == main.BillNum - select d.GroupNum).Distinct() - on b.GroupNum equals a - group b by new { b.GroupNum, b.LU } into g - select new TEMP_NOT_SA_DETAIL - { - GroupNum = g.Key.GroupNum, - LU = g.Key.LU, - Qty = g.Sum(p => p.Qty), - - }; - //var invs = FirstInvoiceExtend1(entitys, new List(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); - var invs = SecInvoice(entitys, new List(), dtos, main.Version, main.InvGroupNum, string.Empty, main.BusinessType, query.ToList()); - if (invs.Count > 0) - { - main.State = SettleBillState.已开票; - } - } - } - _dbcontext.BulkUpdate(new List() { main }); - } - } - return id.ToString(); - } - public List SecInvoice(List p_list, List p_ajdlist, List dtos, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType, List p_notlist = null) - where TDetail : SA_CAN_BASE - { - 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 - && group1.Price == group.Price - && group1.Qty == group.Qty - && group1.Amt == group.Amt - ) - { - tempList.Add(group1); - partCount++;//符合条件加入到零件中 - continue; - } - sum += group1.Amt; - if (sum > 10000000) - { - break; - } - 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, itm.Qty, itm.Price, itm.Amt } - equals - new { itm1.LU, itm1.BeginDate, itm1.EndDate, itm1.ContractDocID, itm1.PartCode, itm1.Qty, itm1.Price, itm1.Amt } - into temp - from tm in temp.DefaultIfEmpty() - where tm == null - select new TMEP_INV - { - LU = itm.LU, - Amt = itm.Amt, - Qty = itm.Qty, - Price = itm.Price, - BeginDate = itm.BeginDate, - ContractDocID = itm.ContractDocID, - EndDate = itm.EndDate, - PartCode = itm.PartCode, - - }; - groups1 = query.ToList(); - } - } - if (invoiceMap.Count > 0) - { - var groupList = new List(); - var notDetialList = new List(); - var detailList = new List(); - var invlist = new List(); - var salist = new List(); - var adjlist = new List(); - int count = invoiceMap.Keys.Count;//计算第几张发票计数器 - int i = 1; - 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, itm.ContractDocID } equals new { itm1.LU, itm1.PartCode, itm1.BeginDate, itm1.EndDate, itm1.ContractDocID } - 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 _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: detail.ContractDocID, - extend2: string.Empty, - beginDate: detail.BeginDate, - endDate: detail.EndDate, - partcode: detail.PartCode - )); - } - if (_entityDetailList.Count > 0) - { - detailList.AddRange(_entityDetailList); - } - #region 发票不能结算的明细 - var innotls = new List(); - var sq = query.GroupBy(p => new { p.LU, p.GroupNum }) - .Select(p => new { version = p_version, LU = p.Key.LU, Qty = p.Sum(itm => itm.Qty), InvBillNum = group.Key, GroupNum = p.Key.GroupNum }); - foreach (var sitm in sq) - { - innotls.Add(new INVOICE_NOT_SETTLE( - guid: GuidGenerator.Create(), - version: p_version, - invGroupNum: p_InvGroupNum, - settleGroupNum: sitm.GroupNum, - lU: sitm.LU, - lU1: sitm.LU, - extend1: "可结算", - extend2: string.Empty, - qty: sitm.Qty, - p_invbillnum: group.Key - )); - } - if (innotls.Count > 0) - { - notDetialList.AddRange(innotls); - } - if (p_notlist != null && p_notlist.Count > 0) - { - var innotls1 = new List(); - - var grouplist = query.Select(p => p.GroupNum).ToList(); - - - var notlist = p_notlist.Where(p => grouplist.Contains(p.GroupNum)); - - foreach (var nitm in notlist) - { - innotls1.Add(new INVOICE_NOT_SETTLE( - guid: GuidGenerator.Create(), - version: p_version, - invGroupNum: p_InvGroupNum, - settleGroupNum: nitm.GroupNum, - lU: nitm.LU, - lU1: nitm.LU, - extend1: "不可结算", - extend2: string.Empty, - qty: nitm.Qty, - p_invbillnum: group.Key - )); - } - if (innotls.Count > 0) - { - notDetialList.AddRange(innotls1); - } - } - #endregion - decimal amt = ls.Sum(k => k.Amt);//金额 - decimal txtAmt = Math.Round(amt * 0.13m, 2);//税后金额 - decimal readAmt = amt + Math.Round(amt * 0.13m, 2);//税额 - - //var contractList = ls.Select(p => p.ContractDocID).Distinct(); - var _groupList = query.GroupBy(p => new { p.GroupNum }) - .Select(p => new { GroupNum = p.Key.GroupNum, Amt = p.Sum(itm => Math.Round(itm.Qty * itm.Price, 2)) }).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 lst = dtos.Where(p => !string.IsNullOrEmpty(p.Site)).ToList(); - string site = lst.FirstOrDefault().Site; - string clientCode = string.Empty; - switch (site) - { - case "1040": - clientCode = "C001"; - break; - case "1046": - clientCode = "C171"; - break; - default: - clientCode = "C004"; - break; - - } - 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.13m, - parent: p_parentInvBillNum, - preTaxDiff: 0, - taxDiff: 0, - clientCode: string.Empty, - realAmt: readAmt - ); - invbill.ClientCode = clientCode; - invbill.CreationTime = DateTime.MinValue; - invbill.LastModificationTime = DateTime.Now; - invbill.Site = dtos.FirstOrDefault().Site; - invlist.Add(invbill); - } - _dbcontext.BulkInsert(invlist); - _dbcontext.BulkInsert(groupList); - _dbcontext.BulkInsert(detailList); - _dbcontext.BulkInsert(notDetialList); - - if (salist.Count > 0) - { - _dbcontext.BulkUpdate(salist); - } - if (adjlist.Count > 0) - { - _dbcontext.BulkUpdate(adjlist); - } - //await _repository.DbContext.BulkInsertAsync(notDetialList); - _invls = invlist.Select(p => p.InvbillNum).ToList(); - } - return _invls; - } - public List FirstInvoice(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 _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>(); - var first = p_list.FirstOrDefault(); - 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)//是否存在分组 - { - if (first.BusinessType == EnumBusinessType.JisBBAC || first.BusinessType == EnumBusinessType.ZhiGongJianHBPO) - { - 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); - } - } - else - { - int count = 1; - foreach (var _itm1 in copyDic) - { - if (itm.Key == _itm1.Key)//相同结算分组项不计算,已初始化 - { - invoiceGroupNumList.Add(itm.Key); - continue; - } - - if (count > 14) - { - break; - } - //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);//所有条件都满足添加发票和结算分组对应关系 - //count++; - - } - 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 detailList = new List(); - var invlist = new List(); - var salist = new List(); - var adjlist = new List(); - List lsby = new List(); - var grouplist = p_list.Select(p => p.GroupNum).Distinct().ToList();//可结分组 - lsby.AddRange(grouplist); - var nolist = p_notlist.Where(p => !grouplist.Contains(p.GroupNum)).Select(p => p.GroupNum).ToList();//不可结分组 - int count = invoiceMap.Keys.Count;//计算第几张发票计数器 - int i = 1; - 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; - adjlist.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, p.LU })//明细 - .Select(itm => new - { - PartCode = itm.Key.PartCode, - InvGroupNum = itm.Key.InvGroupNum, - LU = itm.Key.LU, - ContactDocID = itm.Key.ContractDocID, - Price = itm.Key.Price, - Amt = Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2),//税前 - Tax = Math.Round(Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2) * 0.13m, 2),//税 - TaxAmt = Math.Round(Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2) * 0.13m, 2) + 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 = detailDtos.Sum(k => k.TaxAmt);//税后金额 - decimal realAmt = detailDtos.Sum(k => k.Tax);//税额 - - 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: detail.ContactDocID, - extend2: string.Empty, - beginDate: detail.BeginDate, - endDate: detail.EndDate, - partcode: detail.PartCode - )); - } - if (_entityDetailList.Count > 0) - { - detailList.AddRange(_entityDetailList); - } - var innotls = new List(); - var sq = dtos.Where(p => itm.Value.Contains(p.GroupNum)).GroupBy(p => new { p.LU, p.GroupNum }) - .Select(p => new { version = p_version, LU = p.Key.LU, Qty = p.Sum(itm => itm.Qty), InvBillNum = itm.Key, GroupNum = p.Key.GroupNum }); - - foreach (var sitm in sq) - { - innotls.Add(new INVOICE_NOT_SETTLE( - guid: GuidGenerator.Create(), - version: p_version, - invGroupNum: p_InvGroupNum, - settleGroupNum: sitm.GroupNum, - lU: sitm.LU, - lU1: sitm.LU, - extend1: "可结算", - extend2: string.Empty, - qty: sitm.Qty, - p_invbillnum: itm.Key - )); - } - if (innotls.Count > 0) - { - notDetialList.AddRange(innotls); - } - if (p_notlist != null && p_notlist.Count > 0) - { - var innotls1 = new List(); - var groupnum = itm.Value; - if (count == i)//最后一张发票追加 - { - groupnum.AddRange(nolist);//追加完全不可结 - } - var notls = p_notlist.Where(p => groupnum.Contains(p.GroupNum)).GroupBy(p => new { p.GroupNum, p.LU }) - .Select(p => new { GroupNum = p.Key.GroupNum, LU = p.Key.LU, Qty = p.Sum(itm => itm.Qty), InvBillNum = itm.Key }); - foreach (var nitm in notls) - { - innotls1.Add(new INVOICE_NOT_SETTLE( - guid: GuidGenerator.Create(), - version: p_version, - invGroupNum: p_InvGroupNum, - settleGroupNum: nitm.GroupNum, - lU: nitm.LU, - lU1: nitm.LU, - extend1: "不可结算", - extend2: string.Empty, - qty: nitm.Qty, - p_invbillnum: itm.Key - )); - } - if (innotls.Count > 0) - { - notDetialList.AddRange(innotls1); - } - } - 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.13m, - parent: !string.IsNullOrEmpty(p_parentInvBillNum) ? p_parentInvBillNum : string.Empty, - preTaxDiff: 0, - taxDiff: 0, - clientCode: string.Empty, - realAmt: realAmt - - ); - string site = dtos.Where(p => !string.IsNullOrEmpty(p.Site)).FirstOrDefault().Site; - string clientCode = string.Empty; - switch (site) - { - case "1040": - clientCode = "C001"; - break; - case "1046": - clientCode = "C171"; - break; - default: - clientCode = "C004"; - break; - } - invbill.ClientCode = clientCode; - invbill.Site = dtos.FirstOrDefault().Site; - invbill.CreationTime = DateTime.MinValue; - invbill.LastModificationTime = DateTime.Now; - invlist.Add(invbill); - i++; - } - //using (var transaction = _repository.DbContext.Database.BeginTransaction()) - //{ - // try - // { - // 执行批量数据操作 - if (salist.Count > 0) - { - _dbcontext.BulkUpdate(salist); - } - _dbcontext.BulkInsert(invlist); - _dbcontext.BulkInsert(groupList); - _dbcontext.BulkInsert(detailList); - if (adjlist.Count > 0) - { - _dbcontext.BulkInsert(adjlist); - } - if (notDetialList.Count > 0) - { - _dbcontext.BulkInsert(notDetialList); - } - // 提交事务 - // transaction.Commit(); - // } - // catch (Exception) - // { - // // 回滚事务 - // transaction.Rollback(); - // throw; - // } - //} - _invls = invlist.Select(p => p.InvbillNum).ToList(); - + 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 (billNum.Substring(0, 1) == "C")//一次开票 + { + var notlist = notQuery.Select(p => new TEMP_NOT_SA_DETAIL + { + KeyCode = p.KeyCode, + Version = p.Version, + SettleBillNum = p.SettleBillNum, + LU = p.LU, + PN = p.PN, + Site = p.Site, + Qty = p.Qty, + Price = p.Price, + BusinessType = p.BusinessType, + IsReturn = "", + InvGroupNum = p.InvGroupNum, + SettleDate = p.SettleDate, + GroupNum = p.GroupNum, + ContractDocID = string.Empty, + PartCode = p.PartCode + }).ToList();//不能结算 + var invs = FirstInvoice(entitys, new List(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); + if (invs.Count > 0) + { + main.State = SettleBillState.已开票; + } + } + else//二次开票 + { + var query = from b in _dbcontext.Set() + join a in (from d in _dbcontext.Set() + where d.BillNum == main.BillNum + select d.GroupNum).Distinct() + on b.GroupNum equals a + group b by new { b.GroupNum, b.LU } into g + select new TEMP_NOT_SA_DETAIL + { + GroupNum = g.Key.GroupNum, + LU = g.Key.LU, + Qty = g.Sum(p => p.Qty), + }; + var notlist = new List(); + var invs = SecInvoice(entitys, new List(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); + //var invs = SecInvoice(entitys, new List(), dtos, main.Version, main.InvGroupNum, string.Empty, main.BusinessType, query.ToList()); + if (invs.Count > 0) + { + main.State = SettleBillState.已开票; + } + } + } + _dbcontext.BulkUpdate(new List() { main }); + } } - return _invls; + return id.ToString(); } - public List 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 + + public List FirstInvoice(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 _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();//变换数组副本 - var group= p_list.GroupBy(p => new { p.GroupNum, p.LU }).Select(p => new { GroupNum = p.Key.GroupNum, LU = p.Key.LU, Count = p.Count() }); - - - - - - - foreach (var itm in _query.ToList()) { dic.Add(itm.GroupNum, itm.Amt); copyDic.Add(itm.GroupNum, itm.Amt); } - Dictionary> invoiceMap = new Dictionary>(); var first = p_list.FirstOrDefault(); foreach (var itm in dic) @@ -907,75 +286,76 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs List invoiceGroupNumList = new List();//每个发票对应的结算分组号 List List = new List(); decimal sum = itm.Value;//初始分组合计金额 - - List luList = group.Where(p => p.GroupNum == itm.Key).Select(p => p.LU).ToList(); //初始LU种类 + //List luList = dtos.Where(p => p.GroupNum == itm.Key).Select(p => p.LU).Distinct().ToList(); //初始LU种类 if (copyDic.ContainsKey(itm.Key) == true)//是否存在分组 { - //if (first.BusinessType == EnumBusinessType.JisBBAC || first.BusinessType == EnumBusinessType.ZhiGongJianHBPO) - //{ - foreach (var _itm1 in copyDic) + if (first.BusinessType == EnumBusinessType.JisBBAC || first.BusinessType == EnumBusinessType.ZhiGongJianHBPO) { - if (itm.Key == _itm1.Key)//相同结算分组项不计算,已初始化 - { - invoiceGroupNumList.Add(itm.Key); - continue; - } - var grouplist = group.Where(p => p.GroupNum == _itm1.Key).Select(p => p.LU).ToList();//每项LU种类 - luList.AddRange(grouplist); - luList = luList.Distinct().ToList(); - if (luList.Count > 15)//累加零件不超过20种 + foreach (var _itm1 in copyDic) { - break; + 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);//所有条件都满足添加发票和结算分组对应关系 } - sum += _itm1.Value; - if (sum > 10000000) + invoiceMap.Add(invoiceBillNum, invoiceGroupNumList);//记录发票对应关系 + foreach (var rem in invoiceGroupNumList)//移除 { - break; + copyDic.Remove(rem); } - invoiceGroupNumList.Add(_itm1.Key);//所有条件都满足添加发票和结算分组对应关系 } - invoiceMap.Add(invoiceBillNum, invoiceGroupNumList);//记录发票对应关系 - foreach (var rem in invoiceGroupNumList)//移除 + else { - copyDic.Remove(rem); + int count = 1; + foreach (var _itm1 in copyDic) + { + if (itm.Key == _itm1.Key)//相同结算分组项不计算,已初始化 + { + invoiceGroupNumList.Add(itm.Key); + continue; + } + + if (count > 14) + { + break; + } + //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);//所有条件都满足添加发票和结算分组对应关系 + //count++; + + } + invoiceMap.Add(invoiceBillNum, invoiceGroupNumList);//记录发票对应关系 + foreach (var rem in invoiceGroupNumList)//移除 + { + copyDic.Remove(rem); + } } - //} - //else - //{ - // int count = 1; - // foreach (var _itm1 in copyDic) - // { - // if (itm.Key == _itm1.Key)//相同结算分组项不计算,已初始化 - // { - // invoiceGroupNumList.Add(itm.Key); - // continue; - // } - // if (count > 14) - // { - // break; - // } - // //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);//所有条件都满足添加发票和结算分组对应关系 - // //count++; - // } - // invoiceMap.Add(invoiceBillNum, invoiceGroupNumList);//记录发票对应关系 - // foreach (var rem in invoiceGroupNumList)//移除 - // { - // copyDic.Remove(rem); - // } - //} } } if (invoiceMap.Keys.Count > 0) @@ -1202,20 +582,21 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs //} _invls = invlist.Select(p => p.InvbillNum).ToList(); + + + } return _invls; } - - - - public List FirstInvoiceExtend1(List p_list, List p_adjlist, List dtos, List p_notlist, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType) + public List SecInvoice(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, new() { - + + List _invls = new List(); var groupDtos = dtos.GroupBy(p => new { p.InvGroupNum, p.PartCode, p.Price, p.BeginDate, p.EndDate, p.ContractDocID, p.LU })//明细 .Select(itm => new GroupPartCode @@ -1258,10 +639,10 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs foreach (var big in biglist)//单条大于1000W的开票 { - var list = p_list.Where(p => p.InvGroupNum == big.InvGroupNum && p.PartCode == big.PartCode && p.Price == big.Price && p.LU == big.LU ); + var list = p_list.Where(p => p.InvGroupNum == big.InvGroupNum && p.PartCode == big.PartCode && p.Price == big.Price && p.LU == big.LU); var gener = new InvoiceGeneratorSame(); gener.GenerateInvoices(p_list); - var invList = gener.Invoices; + var invList = gener.Invoices; foreach (var inv in invList) { var partlist = inv.Parts; @@ -1291,7 +672,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs decimal txtAmt = detailDtos.Sum(k => k.TaxAmt);//税后金额 decimal realAmt = detailDtos.Sum(k => k.Tax);//税额 - var invoicegrup=partlist.Select(p => p.GroupNum); + var invoicegrup = partlist.Select(p => p.GroupNum); var mapList = new List(); foreach (var groupnum in invoicegrup) { @@ -1394,7 +775,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs var query = from part in p_list join part1 in partlist on new { part.InvGroupNum, part.PartCode, part.LU, part.Price } equals new { part1.InvGroupNum, part1.PartCode, part1.LU, part1.Price } - select part; + select part; var entityList = query.ToList(); @@ -1406,7 +787,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs decimal txtAmt = partlist.Sum(k => k.TaxAmt);//税后金额 decimal realAmt = partlist.Sum(k => k.Tax);//税额 - var invoicegroupls =entityList.Select(p => p.GroupNum); + var invoicegroupls = entityList.Select(p => p.GroupNum); var mapList = new List(); foreach (var groupnum in invoicegroupls) { @@ -1511,281 +892,14 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs { _dbcontext.BulkInsert(notDetialList); } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // if (invList.Count > 0) - // { - // var groupList = new List(); - // var notDetialList = new List(); - // var detailList = new List(); - // var invlist = new List(); - // var salist = new List(); - // var adjlist = new List(); - // foreach (var inv in invList) - // { - // var partlist = inv.Parts; - // if (partlist.Count > 0) - // { - // var query = from itm in p_list join itm1 in dtos on itm.Id equals itm1.Id - // select itm1; - // var Dtos = query.ToList(); - // var detailDtos = Dtos.GroupBy(p => new { p.InvGroupNum, p.PartCode, p.Price, p.BeginDate, p.EndDate, p.ContractDocID, p.LU })//明细 - // .Select(itm => new - // { - // PartCode = itm.Key.PartCode, - // InvGroupNum = itm.Key.InvGroupNum, - // LU = itm.Key.LU, - // ContactDocID = itm.Key.ContractDocID, - // Price = itm.Key.Price, - // Amt = Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2),//税前 - // Tax = Math.Round(Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2) * 0.13m, 2),//税 - // TaxAmt = Math.Round(Math.Round(itm.Sum(k => k.Qty) * itm.Key.Price, 2) * 0.13m, 2) + 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 = detailDtos.Sum(k => k.TaxAmt);//税后金额 - // decimal realAmt = detailDtos.Sum(k => k.Tax);//税额 - - - //#region 发票明细 - // List _entityDetailList = new List(); - // foreach (var detail in detailDtos) - // { - // _entityDetailList.Add( - // new INVOICE_WAIT_DETAIL( - // guid: GuidGenerator.Create(), - // version: p_version, - // invbillNum: inv.InvBillNum, - // invGroupNum: p_InvGroupNum, - // lU: detail.LU, - // qty: detail.Qty, - // bussiessType: businessType, - // amt: detail.Amt, - // pRICE: detail.Price, - // extend1: detail.ContactDocID, - // extend2: string.Empty, - // beginDate: detail.BeginDate, - // endDate: detail.EndDate, - // partcode: detail.PartCode - // )); - // } - // if (_entityDetailList.Count > 0) - // { - // detailList.AddRange(_entityDetailList); - // } - // #endregion - // #region 发票 - // var invbill = new INVOICE_GRP - // (guid: GuidGenerator.Create(), - // realnvBillNum: string.Empty, - // invbillNum: inv.InvBillNum, - // amt: amt, - // taxAmt: txtAmt, - // fileName: string.Empty, - // businessType: businessType, - // invGroupNum: p_InvGroupNum, - // state: SettleBillState.已开票, - // invoiceBillState: InvoiceBillState.正常, - // tax: 0.13m, - // parent: !string.IsNullOrEmpty(p_parentInvBillNum) ? p_parentInvBillNum : string.Empty, - // preTaxDiff: 0, - // taxDiff: 0, - // clientCode: string.Empty, - // realAmt: realAmt - - // ); - // string site = dtos.Where(p => !string.IsNullOrEmpty(p.Site)).FirstOrDefault().Site; - // string clientCode = string.Empty; - // switch (site) - // { - // case "1040": - // clientCode = "C001"; - // break; - // case "1046": - // clientCode = "C171"; - // break; - // default: - // clientCode = "C004"; - // break; - // } - // invbill.ClientCode = clientCode; - // invbill.Site = dtos.FirstOrDefault().Site; - // invbill.CreationTime = DateTime.MinValue; - // invbill.LastModificationTime = DateTime.Now; - // invlist.Add(invbill); - // #endregion - - - // } - // } - // } - return new List(); - } - - - - - - - } - public class InvoiceBySame where T : SA_CAN_BASE, new() - { - public string InvBillNum { set; get; } - public InvoiceBySame() { } - private List parts = new List(); - public IReadOnlyList Parts => parts.AsReadOnly(); - public decimal TotalAmount => parts.Sum(p => p.Price); - // public int PartCount => parts.GroupBy(p => new { p.LU, p.PartCode }).Count(); - public bool CanAddPart(T part) - { - return TotalAmount + part.Price <= 10000000 ; - } - - public void AddPart(T part) - { - if (CanAddPart(part)) - { - part.InvbillNum = InvBillNum; - parts.Add(part); - - } - - } - } - - public class InvoiceGeneratorSame where T : SA_CAN_BASE, new() - { - private List> invoices = new List>(); - - public IReadOnlyList> Invoices => invoices.AsReadOnly(); - - public void GenerateInvoices(List parts) - { - InvoiceBySame currentInvoice = new InvoiceBySame(); - invoices.Add(currentInvoice); - currentInvoice.InvBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); - foreach (var part in parts) - { - if (!currentInvoice.CanAddPart(part)) - { - currentInvoice = new InvoiceBySame(); - currentInvoice.InvBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); - invoices.Add(currentInvoice); - } - currentInvoice.AddPart(part); - } - } - public InvoiceGeneratorSame() - { - - } - public InvoiceGeneratorSame(List> invoices) - { - this.invoices = invoices; - } - } - - public class InvoiceByDiff - { - public string InvBillNum { set; get; } - public InvoiceByDiff() { } - private List parts = new List(); - public IReadOnlyList Parts => parts.AsReadOnly(); - public decimal TotalAmount => parts.Sum(p => p.Amt); - public int PartCount => parts.GroupBy(p => new { p.LU, p.PartCode }).Count(); - public bool CanAddPart(GroupPartCode part) - { - return TotalAmount + part.Amt < 10000000 && PartCount<15; - } - public void AddPart(GroupPartCode part) - { - if (CanAddPart(part)) - { - - parts.Add(part); - - } - } - } - - public class InvoiceGeneratorDiff - { - private List invoices = new List(); - - public IReadOnlyList Invoices => invoices.AsReadOnly(); - - public void GenerateInvoices(List parts) - { - InvoiceByDiff currentInvoice = new InvoiceByDiff(); - invoices.Add(currentInvoice); - currentInvoice.InvBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); - foreach (var part in parts) - { - if (!currentInvoice.CanAddPart(part)) - { - currentInvoice = new InvoiceByDiff(); - currentInvoice.InvBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); - invoices.Add(currentInvoice); - } - currentInvoice.AddPart(part); - } - } - public InvoiceGeneratorDiff() - { - - } - public InvoiceGeneratorDiff(List invoices) - { - this.invoices = invoices; + _invls = invlist.Select(p => p.InvbillNum).ToList(); + return _invls; } } - public class GroupPartCode - { - public string PartCode { set; get; } - public string InvGroupNum { set; get; } - public string LU { set; get; } - public string ContactDocID { set; get; } - public decimal Price { set; get; } - public decimal Amt { set; get; } - public decimal Tax { set; get; } - public decimal TaxAmt { set; get; } - public decimal Qty { set; get; } - public DateTime? BeginDate { set; get; } - public DateTime? EndDate { set; get; } - - } + + + + diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/INVOICE_GRP.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/INVOICE_GRP.cs index 6e788228..f9e8bfec 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/INVOICE_GRP.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/INVOICE_GRP.cs @@ -1,7 +1,11 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Linq; +using SettleAccount.Bases; using Volo.Abp.Domain.Entities.Auditing; using Win.Sfs.SettleAccount; +using Win.Sfs.SettleAccount.Bases; namespace SettleAccount.Domain.BQ { @@ -92,4 +96,132 @@ namespace SettleAccount.Domain.BQ { } } + public class InvoiceBySame where T : SA_CAN_BASE, new() + { + public string InvBillNum { set; get; } + public InvoiceBySame() { } + private List parts = new List(); + public IReadOnlyList Parts => parts.AsReadOnly(); + public decimal TotalAmount => parts.Sum(p => p.Price); + // public int PartCount => parts.GroupBy(p => new { p.LU, p.PartCode }).Count(); + public bool CanAddPart(T part) + { + return TotalAmount + part.Price <= 10000000; + } + + public void AddPart(T part) + { + if (CanAddPart(part)) + { + part.InvbillNum = InvBillNum; + parts.Add(part); + + } + + } + } + + public class InvoiceGeneratorSame where T : SA_CAN_BASE, new() + { + private List> invoices = new List>(); + + public IReadOnlyList> Invoices => invoices.AsReadOnly(); + + public void GenerateInvoices(List parts) + { + InvoiceBySame currentInvoice = new InvoiceBySame(); + invoices.Add(currentInvoice); + currentInvoice.InvBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); + foreach (var part in parts) + { + if (!currentInvoice.CanAddPart(part)) + { + currentInvoice = new InvoiceBySame(); + currentInvoice.InvBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); + invoices.Add(currentInvoice); + } + currentInvoice.AddPart(part); + } + } + public InvoiceGeneratorSame() + { + + } + public InvoiceGeneratorSame(List> invoices) + { + this.invoices = invoices; + } + } + + public class InvoiceByDiff + { + public string InvBillNum { set; get; } + public InvoiceByDiff() { } + private List parts = new List(); + public IReadOnlyList Parts => parts.AsReadOnly(); + public decimal TotalAmount => parts.Sum(p => p.Amt); + public int PartCount => parts.GroupBy(p => new { p.LU, p.PartCode }).Count(); + public bool CanAddPart(GroupPartCode part) + { + return TotalAmount + part.Amt < 10000000 && PartCount < 15; + } + public void AddPart(GroupPartCode part) + { + if (CanAddPart(part)) + { + + parts.Add(part); + + } + } + } + + public class InvoiceGeneratorDiff + { + private List invoices = new List(); + + public IReadOnlyList Invoices => invoices.AsReadOnly(); + + public void GenerateInvoices(List parts) + { + InvoiceByDiff currentInvoice = new InvoiceByDiff(); + invoices.Add(currentInvoice); + currentInvoice.InvBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); + foreach (var part in parts) + { + if (!currentInvoice.CanAddPart(part)) + { + currentInvoice = new InvoiceByDiff(); + currentInvoice.InvBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); + invoices.Add(currentInvoice); + } + currentInvoice.AddPart(part); + } + } + public InvoiceGeneratorDiff() + { + + } + public InvoiceGeneratorDiff(List invoices) + { + this.invoices = invoices; + } + } + public class GroupPartCode + { + public string PartCode { set; get; } + public string InvGroupNum { set; get; } + public string LU { set; get; } + public string ContactDocID { set; get; } + public decimal Price { set; get; } + public decimal Amt { set; get; } + public decimal Tax { set; get; } + public decimal TaxAmt { set; get; } + public decimal Qty { set; get; } + public DateTime? BeginDate { set; get; } + public DateTime? EndDate { set; get; } + + } + + }