@ -1215,13 +1215,9 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs
public List < string > FirstInvoiceExtend1 < 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 , new ( )
{
//var gener = new InvoiceGeneratorSame();
//var list =p_list.OrderBy(p => p.LU).ThenBy(p=>p.PartCode).ToList();
//gener.GenerateInvoices(p_list);
//var invList = gener.Invoices;
var detailDtos = dtos . GroupBy ( p = > new { p . InvGroupNum , p . PartCode , p . Price , p . BeginDate , p . EndDate , p . ContractDocID , p . LU } ) //明细
var groupDtos = dtos . GroupBy ( p = > new { p . InvGroupNum , p . PartCode , p . Price , p . BeginDate , p . EndDate , p . ContractDocID , p . LU } ) //明细
. Select ( itm = > new GroupPartCode
{
PartCode = itm . Key . PartCode ,
@ -1237,10 +1233,9 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs
EndDate = itm . Key . EndDate
} ) . ToList ( ) ;
List < GroupPartCode > biglist = new List < GroupPartCode > ( ) ;
List < GroupPartCode > smalllist = new List < GroupPartCode > ( ) ;
foreach ( var itm in detailDtos )
foreach ( var itm in groupDtos ) //分类1000W以上或1000W以下
{
if ( itm . Amt > 1 0 0 0 0 0 0 0 )
{
@ -1251,18 +1246,281 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs
smalllist . Add ( itm ) ;
}
}
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 itm in biglist )
foreach ( var big in biglist ) //单条大于1000W的开票
{
var list = p_list . Where ( p = > p . InvGroupNum = = itm . InvGroupNum & & p . PartCode = = itm . PartCode & & p . Price = = itm . Price & & p . LU = = itm . LU ) ;
var gener = new InvoiceGeneratorSame ( ) ;
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 < TDetail > ( ) ;
gener . GenerateInvoices ( p_list ) ;
var invList = gener . Invoices ;
var invList = gener . Invoices ;
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.13 m , 2 ) , //税
TaxAmt = Math . Round ( Math . Round ( itm . Sum ( k = > k . Qty ) * itm . Key . Price , 2 ) * 0.13 m , 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 invoicegrup = partlist . Select ( p = > p . GroupNum ) ;
var mapList = new List < INVOICE_MAP_GROUP > ( ) ;
foreach ( var groupnum in invoicegrup )
{
mapList . Add ( new INVOICE_MAP_GROUP (
guid : GuidGenerator . Create ( ) ,
version : p_version ,
invbillNum : inv . InvBillNum ,
invGroupNum : p_InvGroupNum ,
settleGroupNum : groupnum ,
amt : 0 ,
extend1 : string . Empty ,
extend2 : string . Empty
)
) ;
}
if ( mapList . Count > 0 )
{
groupList . AddRange ( mapList ) ;
}
#region 发票明细
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 : 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.13 m ,
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
}
}
}
smalllist = smalllist . OrderBy ( p = > p . Amt ) . ToList ( ) ;
var gener1 = new InvoiceGeneratorDiff ( ) ;
gener1 . GenerateInvoices ( smalllist ) ;
var invList1 = gener1 . Invoices ;
foreach ( var inv in invList1 ) //小于1000W的开票
{
var partlist = inv . Parts ;
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 ;
var entityList = query . ToList ( ) ;
salist . AddRange ( entityList ) ;
if ( partlist . Count > 0 )
{
decimal amt = partlist . Sum ( k = > k . Amt ) ; //金额
decimal txtAmt = partlist . Sum ( k = > k . TaxAmt ) ; //税后金额
decimal realAmt = partlist . Sum ( k = > k . Tax ) ; //税额
var invoicegroupls = entityList . Select ( p = > p . GroupNum ) ;
var mapList = new List < INVOICE_MAP_GROUP > ( ) ;
foreach ( var groupnum in invoicegroupls )
{
mapList . Add ( new INVOICE_MAP_GROUP (
guid : GuidGenerator . Create ( ) ,
version : p_version ,
invbillNum : inv . InvBillNum ,
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 partlist )
{
_ 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 . Value ,
endDate : detail . EndDate . Value ,
partcode : detail . PartCode
) ) ;
}
if ( _ entityDetailList . Count > 0 )
{
detailList . AddRange ( _ entityDetailList ) ;
}
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.13 m ,
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 ) ;
}
}
if ( salist . Count > 0 )
{
_d bcontext . BulkUpdate ( salist ) ;
}
_d bcontext . BulkInsert ( invlist ) ;
_d bcontext . BulkInsert ( groupList ) ;
_d bcontext . BulkInsert ( detailList ) ;
if ( adjlist . Count > 0 )
{
_d bcontext . BulkInsert ( adjlist ) ;
}
if ( notDetialList . Count > 0 )
{
_d bcontext . BulkInsert ( notDetialList ) ;
}
@ -1402,47 +1660,47 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs
}
public class InvoiceBySame
public class InvoiceBySame < T > where T : SA_CAN_BASE , new ( )
{
public string InvBillNum { set ; get ; }
public InvoiceBySame ( ) { }
private List < SA_CAN_BASE > parts = new List < SA_CAN_BASE > ( ) ;
public IReadOnlyList < SA_CAN_BASE > Parts = > parts . AsReadOnly ( ) ;
private List < T > parts = new List < T > ( ) ;
public IReadOnlyList < T > 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 ( SA_CAN_BASE part )
// public int PartCount => parts.GroupBy(p => new { p.LU, p.PartCode }).Count();
public bool CanAddPart ( T part )
{
return TotalAmount + part . Price < = 1 0 0 0 0 0 0 0 & & PartCount < 1 5 ; ;
return TotalAmount + part . Price < = 1 0 0 0 0 0 0 0 ;
}
public void AddPart ( SA_CAN_BASE part )
public void AddPart ( T part )
{
if ( CanAddPart ( part ) )
{
part . InvbillNum = InvBillNum ;
parts . Add ( part ) ;
//throw new InvalidOperationException("Cannot add part to invoice. Check constraints.");
}
}
}
public class InvoiceGeneratorSame
public class InvoiceGeneratorSame < T > where T : SA_CAN_BASE , new ( )
{
private List < InvoiceBySame > invoices = new List < InvoiceBySame > ( ) ;
private List < InvoiceBySame < T > > invoices = new List < InvoiceBySame < T > > ( ) ;
public IReadOnlyList < InvoiceBySame > Invoices = > invoices . AsReadOnly ( ) ;
public IReadOnlyList < InvoiceBySame < T > > Invoices = > invoices . AsReadOnly ( ) ;
public void GenerateInvoices < TDetail > ( List < TDetail > parts ) where TDetail : SA_CAN_BASE , new ( )
public void GenerateInvoices ( List < T > parts )
{
InvoiceBySame currentInvoice = new InvoiceBySame ( ) ;
InvoiceBySame < T > currentInvoice = new InvoiceBySame < T > ( ) ;
invoices . Add ( currentInvoice ) ;
currentInvoice . InvBillNum = OrderNumberGenerator . GenerateOrderNumber ( "INV" ) ;
foreach ( var part in parts )
{
if ( ! currentInvoice . CanAddPart ( part ) )
{
currentInvoice = new InvoiceBySame ( ) ;
currentInvoice = new InvoiceBySame < T > ( ) ;
currentInvoice . InvBillNum = OrderNumberGenerator . GenerateOrderNumber ( "INV" ) ;
invoices . Add ( currentInvoice ) ;
}
@ -1453,7 +1711,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs
{
}
public InvoiceGeneratorSame ( List < InvoiceBySame > invoices )
public InvoiceGeneratorSame ( List < InvoiceBySame < T > > invoices )
{
this . invoices = invoices ;
}
@ -1466,18 +1724,18 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs
private List < GroupPartCode > parts = new List < GroupPartCode > ( ) ;
public IReadOnlyList < GroupPartCode > Parts = > parts . AsReadOnly ( ) ;
public decimal TotalAmount = > parts . Sum ( p = > p . Amt ) ;
// public int PartCount => parts.GroupBy(p => new { p.LU, p.PartCode }).Count();
public int PartCount = > parts . GroupBy ( p = > new { p . LU , p . PartCode } ) . Count ( ) ;
public bool CanAddPart ( GroupPartCode part )
{
return TotalAmount + part . Amt < = 1 0 0 0 0 0 0 0 ;
return TotalAmount + part . Amt < 1 0 0 0 0 0 0 0 & & PartCount < 1 5 ;
}
public void AddPart ( GroupPartCode part )
{
if ( CanAddPart ( part ) )
{
//part.InvbillNum = InvBillNum;
parts . Add ( part ) ;
//throw new InvalidOperationException("Cannot add part to invoice. Check constraints.");
}
}
}