using System; using System.Collections.Generic; using System.Linq; using Win_in.Sfs.Scp.v1.Domain.Asns; using Win_in.Sfs.Scp.WebApi.Asns; namespace Win_in.Sfs.Scp.WebApi.Console { public class AsnFactory { public ASN_X12_856_3060 CreateAsnX128563060(TB_ASN scpAsn,List barcodes) { var asnCode = scpAsn.AsnBillNum; var senderId = scpAsn.VendId; var receiverId = "IACNA_ID"; var shipTime = scpAsn.ShipTime ?? DateTime.Now; var envType = ISA.EnvType.P; var authorization = ""; var security = ""; //初始化ASN单 var asn = CreateAsn(asnCode,senderId,receiverId,shipTime,envType,authorization,security); var functionalGroupCode = "123456789"; //初始化功能组 var functionalGroup = CreateFunctionalGroup(senderId,receiverId,shipTime,functionalGroupCode); var shipNoticeCode = "0001"; var datetimeType = "011"; //初始化发货单 var shipNotice = CreateShipNotice(shipNoticeCode,functionalGroupCode,shipTime,datetimeType); var gValue = 0; var gUom = "KG"; var nValue = 0; var nUom = "KG"; var routeSequenceCode = "B"; var identificationCode = "PSTV"; var mode = "LT"; var equipmentCode = "TL"; var equipmentNumber = "123456";//TODO 车牌号 //初始化发货明细 var shipment = CreateShipment(shipNotice,gValue,gUom,nValue,nUom,routeSequenceCode,identificationCode,mode, equipmentCode,equipmentNumber,functionalGroupCode,functionalGroupCode,senderId,receiverId); //计算与添加托盘和尾箱 var palletCodes = barcodes.Select(p => p.Extend2).Distinct().ToList(); foreach (var palletCode in palletCodes) { var palletBarcodes = barcodes.Where(p => p.Extend2==palletCode).ToList(); var items = new List(); var group = palletBarcodes .GroupBy(p => new { p.PartCode, p.Qty, p.Batch, p.PoUnit, p.PoBillNum,p.PackQty}) .Select(p => new { p.Key.PartCode, p.Key.Qty, p.Key.Batch, p.Key.PoUnit, p.Key.PoBillNum, p.Key.PackQty, Labels = p.Select(p=>p.BarCode).ToList() }) .ToList(); foreach (var b in group) { var accumQty = 0;//TODO 如何计算 var loadQty = b.Labels.Count; var unitQty = b.PackQty; var item = CreateItem(b.PartCode, loadQty * unitQty, b.PoUnit, accumQty, b.PoBillNum, loadQty, unitQty,b.Labels); items.Add(item); } //如果托标签为空,当作尾箱处理 if (string.IsNullOrEmpty(palletCode)) { foreach (var item in items) { shipment.AddOrphanItem(shipNotice,item); } } //添加托盘 else { var tare = CreateTare(palletCode); shipment.AddTare(shipNotice,tare); foreach (var item in items) { tare.AddItem(shipNotice, item); } } } //TODO 如何获取包装代码,或者规则是什么? var packagingCode = "PLT90"; //装载量需要在添加托盘和尾箱后再计算 var loadingQty = shipment.Tares.Count+shipment.OrphanItems.Count; shipment.SetTD1(packagingCode, loadingQty); shipNotice .AddShipment(shipment) //添加发货明细 .SetCTT() //设置发货单汇总 .SetSE(shipNoticeCode); //设置发货明细结尾 //添加发货单 functionalGroup .AddShipNotice(shipNotice) .SetGE(functionalGroupCode); //添加功能组 asn .AddFunctionGroup(functionalGroup) .SetIEA(asnCode); return asn; } private ASN_X12_856_3060 CreateAsn(string asnCode, string senderId, string receiverId, DateTime datetime, ISA.EnvType envType = ISA.EnvType.P, string authorization = "", string security = "") { var asn = new ASN_X12_856_3060(); asn.SetISA(asnCode, senderId, receiverId, datetime, envType, authorization, security); return asn; } private FunctionalGroup CreateFunctionalGroup(string senderId, string receiverId, DateTime datetime, string functionalGroupCode) { var functionGroup = new FunctionalGroup(); functionGroup.SetGS(senderId, receiverId, datetime, functionalGroupCode); return functionGroup; } private ShipNotice CreateShipNotice(string shipNoticeCode, string noticeNumber, DateTime datetime, string datetimeType="011", string purpose = "00") { var shipNotice = new ShipNotice(); shipNotice .SetST(shipNoticeCode) .SetBSN(noticeNumber, datetime, purpose) .SetDTM(datetime, datetimeType); shipNotice.AddSegment(4); return shipNotice; } private Shipment CreateShipment(ShipNotice shipNotice, decimal gValue, string gUom, decimal nValue, string nUom, string routeSequenceCode, string identificationCode, string mode, string equipmentCode, string equipmentNumber, string bmRefValue, string pkRefValue, string senderId, string receiverId) { var shipment = new Shipment(); shipment .SetHL() .SetMEA_G(gValue, gUom) .SetMEA_N(nValue, nUom) .SetTD5(routeSequenceCode, identificationCode, mode) .SetTD3(equipmentCode, equipmentNumber) .SetREF_BM(bmRefValue) .SetREF_PK(pkRefValue) .SetN1_SF(senderId) .SetN1_ST(receiverId) ; shipNotice.AddSegment(10); return shipment; } private Tare CreateTare(string tareLabelCode) { var tare = new Tare(); tare.SetHL() .SetREF_LS(tareLabelCode); return tare; } private Item CreateItem(string itemCode, decimal qty, string uom, int accumQty, string poNumber, int loadQty, decimal unitQty, Dictionary itemLabelCodes) { var item = new Item(); item.SetHL() .SetLIN(itemCode) .SetSN1(qty, uom, accumQty) .SetPRF(poNumber) .SetCLD(loadQty, unitQty) .SetREF_LS_LTs(itemLabelCodes); return item; } } }