using
System;
using
System.Collections.Generic;
using
System.Data;
using
System.Linq;
class
Program
{
static
void
Main()
{
var
osList = GetOS();
var
allpoList = GetALLPO();
string
strKey =
string
.Empty;
var
tempOSList =
new
List<OSList>();
foreach
(
var
allpoItem
in
allpoList)
{
int
allpoQty = allpoItem.Qty.GetValueOrDefault();
strKey = allpoItem.Code + allpoItem.Customer;
tempOSList = osList.Where(x => x.Code + x.Customer == strKey).Where(x => !x.IsAllocation)
.ToList();
if
(tempOSList.Count == 0 || allpoQty <= 0)
continue
;
osList.RemoveAll(x => x.Code + x.Customer == strKey && !x.IsAllocation);
var
add =
new
List<OSList>();
foreach
(
var
tempOSItem
in
tempOSList)
{
int
tempOSQty = tempOSItem.Qty.GetValueOrDefault() - tempOSItem.Gap.GetValueOrDefault();
int
currentQty = Math.Min(allpoQty, tempOSQty);
if
(tempOSQty - currentQty == 0)
{
tempOSItem.IsAllocation =
true
;
}
if
(tempOSItem.Remarks ==
"allocated"
)
{
var
newadd = tempOSItem.Copy();
newadd.Gap = currentQty;
newadd.Date = allpoItem.Date;
newadd.Remarks =
"allocated"
;
add.Add(newadd);
}
else
{
tempOSItem.Gap = currentQty;
tempOSItem.Date = allpoItem.Date;
tempOSItem.Remarks =
"allocated"
;
tempOSItem.AllocationSum += currentQty;
}
allpoQty -= currentQty;
if
(allpoQty <= 0)
{
break
;
}
}
tempOSList.AddRange(add);
osList.AddRange(tempOSList);
}
Console.ReadKey();
}
public
static
List<OSList> GetOS()
{
var
os =
new
List<OSList>
{
new
OSList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=30000},
new
OSList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=12000},
new
OSList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=45000},
new
OSList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=30000},
new
OSList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=33000},
new
OSList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=30000},
new
OSList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=27000}
};
return
os;
}
public
static
List<ALLPOList> GetALLPO()
{
var
allpo =
new
List<ALLPOList>
{
new
ALLPOList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=45000,Date=
"2024/12/14"
},
new
ALLPOList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=27000,Date=
"2024/12/10"
},
new
ALLPOList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=30000,Date=
"2024/12/10"
},
new
ALLPOList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=45000,Date=
"2024/12/14"
},
new
ALLPOList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=30000,Date=
"2024/12/25"
},
new
ALLPOList{ To=
"248776"
,End=
"300348"
,Code=
"Q13FC1350000200"
,Customer=
"1010217255039"
,Qty=30000,Date=
"2024/12/21"
}
};
return
allpo;
}
public
class
OSList
{
public
string
To {
get
;
set
; }
public
string
End {
get
;
set
; }
public
string
Code {
get
;
set
; }
public
string
Customer {
get
;
set
; }
public
int
? Qty {
get
;
set
; }
public
int
? Gap {
get
;
set
; }
public
string
Date {
get
;
set
; }
public
bool
IsAllocation {
get
;
set
; }
public
int
? AllocationSum {
get
;
set
; }
public
string
Remarks {
get
;
set
; }
public
OSList Copy()
{
return
new
OSList
{
To =
this
.To,
End =
this
.End,
Code =
this
.Code,
Customer =
this
.Customer,
Qty =
this
.Qty ?? 0
};
}
}
public
class
ALLPOList
{
public
string
To {
get
;
set
; }
public
string
End {
get
;
set
; }
public
string
Code {
get
;
set
; }
public
string
Customer {
get
;
set
; }
public
int
? Qty {
get
;
set
; }
public
string
Date {
get
;
set
; }
}
}