学 赵
1 year ago
2 changed files with 844 additions and 1 deletions
@ -0,0 +1,841 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using AutoMapper; |
|||
using DocumentFormat.OpenXml.Presentation; |
|||
using EFCore.BulkExtensions; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.CodeAnalysis.CSharp.Syntax; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using SettleAccount.Bases; |
|||
using SettleAccount.Domain.BQ; |
|||
using TaskJob.EventArgs; |
|||
using TaskJob.Interfaces; |
|||
using Volo.Abp.Application.Services; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Uow; |
|||
using Win.Sfs.SettleAccount.Bases; |
|||
using Win.Sfs.SettleAccount.Bases.DomainServices; |
|||
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.CodeSettings; |
|||
using Win.Sfs.SettleAccount.Entities.Prices; |
|||
using Win.Sfs.Shared.RepositoryBase; |
|||
|
|||
namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs |
|||
{ |
|||
|
|||
public class GenerateJisInvoiceService : ApplicationService, ITransientDependency, IExportJob |
|||
{ |
|||
protected readonly SettleAccountDbContext _dbcontext; |
|||
public GenerateJisInvoiceService(SettleAccountDbContext dbcontext) |
|||
{ |
|||
_dbcontext = dbcontext; |
|||
} |
|||
|
|||
//public string Make(string billNum)
|
|||
|
|||
//{
|
|||
// var main = _dbcontext.Set<T>().FirstOrDefault(p => p.InvGroupNum == billNum);
|
|||
// //var main = await _bbacMng.GetMainAsync(invbillNum);
|
|||
// if (main != null)
|
|||
// {
|
|||
// var entitys = _dbcontext.Set<BBAC_CAN_SA_DETAIL>().Where(p => p.InvGroupNum == billNum).ToList();//可结算
|
|||
// var priceList = _dbcontext.Set<PriceList>().Where(p => p.IsCancel == false).ToList();//价格表
|
|||
// var groupNumList = entitys.Select(p => p.GroupNum).Distinct().ToList();
|
|||
// var notQuery = _dbcontext.Set<BBAC_NOT_SA_DETAIL>().Where(p => p.SettleBillNum == main.SettleBillNum);
|
|||
// var dto1s = ObjectMapper.Map<List<BBAC_CAN_SA_DETAIL>, List<BBAC_CAN_SA_DETAIL_DTO>>(entitys);
|
|||
// 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 = d.ContractDocID,
|
|||
// 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();//不能结算
|
|||
// FirstInvoice(entitys, new List<PUB_ADJ_DETAIL>(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType);
|
|||
// }
|
|||
// else//二次开票
|
|||
// {
|
|||
// SecInvoice(entitys, new List<PUB_ADJ_DETAIL>(), dtos, main.Version, main.InvGroupNum, string.Empty, main.BusinessType);
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
//}
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
public string ExportFile(Guid id, List<string> exportName, List<CustomCondition> property) |
|||
{ |
|||
var billNum = property.Where(p => p.Name == "BillNum").FirstOrDefault().Value; |
|||
|
|||
var BussinessType = property.Where(p => p.Name == "BussinessType").FirstOrDefault().Value; |
|||
|
|||
|
|||
if (BussinessType == "BBACJIS") |
|||
{ |
|||
var main = _dbcontext.Set<BBAC_CAN_SA>().FirstOrDefault(p => p.InvGroupNum == billNum); |
|||
//var main = await _bbacMng.GetMainAsync(invbillNum);
|
|||
if (main != null) |
|||
{ |
|||
var entitys = _dbcontext.Set<BBAC_CAN_SA_DETAIL>().Where(p => p.InvGroupNum == billNum).ToList();//可结算
|
|||
var priceList = _dbcontext.Set<PriceList>().Where(p => p.IsCancel == false).ToList();//价格表
|
|||
var groupNumList = entitys.Select(p => p.GroupNum).Distinct().ToList(); |
|||
var notQuery = _dbcontext.Set<BBAC_NOT_SA_DETAIL>().Where(p => p.SettleBillNum == main.SettleBillNum); |
|||
var dto1s = ObjectMapper.Map<List<BBAC_CAN_SA_DETAIL>, List<BBAC_CAN_SA_DETAIL_DTO>>(entitys); |
|||
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 = d.ContractDocID, |
|||
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<PUB_ADJ_DETAIL>(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); |
|||
if (invs.Count > 0) |
|||
{ |
|||
main.State = SettleBillState.已开票; |
|||
} |
|||
|
|||
|
|||
} |
|||
else//二次开票
|
|||
{ |
|||
var invs = SecInvoice(entitys, new List<PUB_ADJ_DETAIL>(), dtos, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); |
|||
if (invs.Count > 0) |
|||
{ |
|||
main.State = SettleBillState.已开票; |
|||
} |
|||
} |
|||
} |
|||
_dbcontext.BulkUpdate(new List<BBAC_CAN_SA>() { main }); |
|||
|
|||
|
|||
} |
|||
} |
|||
if(BussinessType=="HBPOJIS") |
|||
{ |
|||
var main = _dbcontext.Set<HBPO_CAN_SA>().FirstOrDefault(p => p.InvGroupNum == billNum); |
|||
//var main = await _bbacMng.GetMainAsync(invbillNum);
|
|||
if (main != null) |
|||
{ |
|||
var entitys = _dbcontext.Set<HBPO_CAN_SA_DETAIL>().Where(p => p.InvGroupNum == billNum).ToList();//可结算
|
|||
var priceList = _dbcontext.Set<PriceList>().Where(p => p.IsCancel == false).ToList();//价格表
|
|||
var groupNumList = entitys.Select(p => p.GroupNum).Distinct().ToList(); |
|||
var notQuery = _dbcontext.Set<HBPO_NOT_SA_DETAIL>().Where(p => p.SettleBillNum == main.SettleBillNum); |
|||
var dto1s = ObjectMapper.Map<List<HBPO_CAN_SA_DETAIL>, List<HBPO_CAN_SA_DETAIL_DTO>>(entitys); |
|||
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<PUB_ADJ_DETAIL>(), dtos, notlist, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); |
|||
if (invs.Count > 0) |
|||
{ |
|||
main.State = SettleBillState.已开票; |
|||
} |
|||
} |
|||
else//二次开票
|
|||
{ |
|||
var invs = SecInvoice(entitys, new List<PUB_ADJ_DETAIL>(), dtos, main.Version, main.InvGroupNum, string.Empty, main.BusinessType); |
|||
if (invs.Count > 0) |
|||
{ |
|||
main.State = SettleBillState.已开票; |
|||
} |
|||
} |
|||
} |
|||
_dbcontext.BulkUpdate(new List<HBPO_CAN_SA>() { main }); |
|||
} |
|||
} |
|||
return id.ToString(); |
|||
} |
|||
|
|||
public List<string> SecInvoice<TDetail>(List<TDetail> p_list, List<PUB_ADJ_DETAIL> p_ajdlist, List<TEMP_CAN_SA_DETAIL> dtos, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType) |
|||
where TDetail : SA_CAN_BASE |
|||
{ |
|||
List<string> _invls = new List<string>(); |
|||
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<string, List<TMEP_INV>> invoiceMap = new Dictionary<string, List<TMEP_INV>>();//发票和发票明细关系
|
|||
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<TMEP_INV> tempList = new List<TMEP_INV>(); |
|||
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<INVOICE_MAP_GROUP>(); |
|||
var notDetialList = new List<INVOICE_NOT_SETTLE>(); |
|||
var detailList = new List<INVOICE_WAIT_DETAIL>(); |
|||
var invlist = new List<INVOICE_GRP>(); |
|||
var salist = new List<TDetail>(); |
|||
var adjlist = new List<PUB_ADJ_DETAIL>(); |
|||
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<INVOICE_WAIT_DETAIL>(); |
|||
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); |
|||
} |
|||
decimal amt = detailList.Sum(k => k.Amt);//金额
|
|||
decimal txtAmt = detailList.Sum(k => k.Amt) + Math.Round(detailList.Sum(k => k.Amt) * 0.13m, 2);//税后金额
|
|||
decimal readAmt = Math.Round(detailList.Sum(k => k.Amt) * 0.13m, 2);//税额
|
|||
|
|||
//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<INVOICE_MAP_GROUP> group1 = new List<INVOICE_MAP_GROUP>(); |
|||
|
|||
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.Site = dtos.FirstOrDefault().Site; |
|||
invlist.Add(invbill); |
|||
} |
|||
_dbcontext.BulkInsert(invlist); |
|||
_dbcontext.BulkInsert(groupList); |
|||
_dbcontext.BulkInsert(detailList); |
|||
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; |
|||
|
|||
} |
|||
[UnitOfWork(false)] |
|||
public List<string> FirstInvoice<TDetail>(List<TDetail> p_list, List<PUB_ADJ_DETAIL> p_adjlist, List<TEMP_CAN_SA_DETAIL> dtos, List<TEMP_NOT_SA_DETAIL> p_notlist, int p_version, string p_InvGroupNum, string p_parentInvBillNum, EnumBusinessType businessType) |
|||
where TDetail : SA_CAN_BASE |
|||
{ |
|||
List<string> _invls = new List<string>(); |
|||
var _query = dtos.GroupBy(p => new { p.GroupNum }).Select(p => new { GroupNum = p.Key.GroupNum, Amt = p.Sum(itm => itm.Amt) }); |
|||
Dictionary<string, decimal> dic = new Dictionary<string, decimal>();//原本
|
|||
Dictionary<string, decimal> copyDic = new Dictionary<string, decimal>();//变换数组副本
|
|||
foreach (var itm in _query.ToList()) |
|||
{ |
|||
dic.Add(itm.GroupNum, itm.Amt); |
|||
copyDic.Add(itm.GroupNum, itm.Amt); |
|||
|
|||
} |
|||
Dictionary<string, List<string>> invoiceMap = new Dictionary<string, List<string>>(); |
|||
var first = p_list.FirstOrDefault(); |
|||
foreach (var itm in dic) |
|||
{ |
|||
string invoiceBillNum = OrderNumberGenerator.GenerateOrderNumber("INV"); |
|||
List<string> invoiceGroupNumList = new List<string>();//每个发票对应的结算分组号
|
|||
List<string> List = new List<string>(); |
|||
decimal sum = itm.Value;//初始分组合计金额
|
|||
// List<string> 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<INVOICE_MAP_GROUP>(); |
|||
var notDetialList = new List<INVOICE_NOT_SETTLE>(); |
|||
var detailList = new List<INVOICE_WAIT_DETAIL>(); |
|||
var invlist = new List<INVOICE_GRP>(); |
|||
var salist = new List<TDetail>(); |
|||
var adjlist = new List<PUB_ADJ_DETAIL>(); |
|||
List<string> lsby = new List<string>(); |
|||
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), |
|||
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.Amt) + Math.Round(detailDtos.Sum(k => k.Amt) * 0.13m, 2);//税后金额
|
|||
decimal realAmt = Math.Round(detailDtos.Sum(k => k.Amt) * 0.13m, 2);//税额
|
|||
|
|||
var mapList = new List<INVOICE_MAP_GROUP>(); |
|||
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<INVOICE_WAIT_DETAIL> _entityDetailList = new List<INVOICE_WAIT_DETAIL>(); |
|||
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<INVOICE_NOT_SETTLE>(); |
|||
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<INVOICE_NOT_SETTLE>(); |
|||
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.Now; |
|||
invlist.Add(invbill); |
|||
i++; |
|||
} |
|||
//using (var transaction = _repository.DbContext.Database.BeginTransaction())
|
|||
//{
|
|||
// try
|
|||
// {
|
|||
// 执行批量数据操作
|
|||
if (salist.Count > 0) |
|||
{ |
|||
_dbcontext.BulkUpdateAsync(salist).ConfigureAwait(false); |
|||
} |
|||
_dbcontext.BulkInsertAsync(invlist).ConfigureAwait(false); |
|||
_dbcontext.BulkInsertAsync(groupList).ConfigureAwait(false); |
|||
_dbcontext.BulkInsertAsync(detailList).ConfigureAwait(false); |
|||
if (adjlist.Count > 0) |
|||
{ |
|||
_dbcontext.BulkInsertAsync(adjlist).ConfigureAwait(false); |
|||
} |
|||
if (notDetialList.Count > 0) |
|||
{ |
|||
_dbcontext.BulkInsertAsync(notDetialList).ConfigureAwait(false); |
|||
} |
|||
// 提交事务
|
|||
// transaction.Commit();
|
|||
// }
|
|||
// catch (Exception)
|
|||
// {
|
|||
// // 回滚事务
|
|||
// transaction.Rollback();
|
|||
// throw;
|
|||
// }
|
|||
//}
|
|||
_invls = invlist.Select(p => p.InvbillNum).ToList(); |
|||
|
|||
|
|||
|
|||
|
|||
} |
|||
return _invls; |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue