using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using BWP.B3Frameworks;
|
|
using BWP.B3Frameworks.BO.NamedValueTemplate;
|
|
using BWP.B3Frameworks.Utils;
|
|
using BWP.B3Sale.BL;
|
|
using BWP.B3Sale.BO;
|
|
using BWP.B3Sale.Utils;
|
|
using BWP.B3UnitedInfos.BO;
|
|
using Forks.EnterpriseServices.DomainObjects2;
|
|
using Forks.EnterpriseServices.DomainObjects2.DQuery;
|
|
using Forks.Utils;
|
|
using TSingSoft.WebPluginFramework;
|
|
|
|
namespace B3_QiLianMuGe.Utils
|
|
{
|
|
[TypeIOC(typeof(SaleOutStoreBL), typeof(SaleOutStoreBL.ICreateInOutStoreBill))]
|
|
public class CreateInOutStoreBillIOC : SaleOutStoreBL.ICreateInOutStoreBill
|
|
{
|
|
public void Invoke(IDmoSession session, SaleOutStore saleOutStore, InOutStoreBill target, ref bool flag)
|
|
{
|
|
SetInOutStoreBillDetailByWeightInfos(session, saleOutStore, target);
|
|
if (target.Details.Count > 0)
|
|
flag = true;
|
|
else
|
|
flag = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理扫条码按条码的生产批次对应的存货批次出库
|
|
/// </summary>
|
|
/// <param name="dmo"></param>
|
|
/// <param name="target"></param>
|
|
private void SetInOutStoreBillDetailByWeightInfos(IDmoSession Session,SaleOutStore dmo, InOutStoreBill target)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(WeightingInfor)));
|
|
query.Columns.Add(DQSelectColumn.Field("DetailID"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("Product_Batch"));
|
|
query.Columns.Add(DQSelectColumn.Sum("MainUnitNum"));
|
|
query.Columns.Add(DQSelectColumn.Sum("SecondNumber"));
|
|
query.Where.Conditions.Add(DQCondition.EQ("BillType", DmoTypeIDAttribute.GetID(typeof(SaleOutStore))));
|
|
query.Where.Conditions.Add(DQCondition.EQ("BillID", dmo.ID));
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("DetailID"));
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("Goods_ID"));
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("Product_Batch"));
|
|
var tuples = query.EExecuteList<long, long?, string, Money<decimal>?, Money<decimal>?>(Session);
|
|
if (tuples.Count() > 0)
|
|
{
|
|
var results = tuples.Where(x => string.IsNullOrEmpty(x.Item3) && x.Item4 > 0);
|
|
foreach (var result in results)
|
|
{
|
|
var oDetails = dmo.Details.Where(x => x.ID == result.Item1);
|
|
if (oDetails.Count() > 0)
|
|
{
|
|
var detail = oDetails.First();
|
|
detail.UnitNum = result.Item4;
|
|
detail.Number = result.Item4;
|
|
detail.SecondNumber = result.Item5;
|
|
detail.GoodsBatch_ID = null;
|
|
var dic = new Dictionary<long, List<TmpData>>();
|
|
var outDetails = AddDetailByPdAndBatchNo(Session, dmo, detail, dic);
|
|
foreach (var item in outDetails)
|
|
{
|
|
var targetDetail = new InOutStoreBill_Detail();
|
|
target.Details.Add(targetDetail);
|
|
var details = dmo.Details.Where(x => x.ID == detail.ID);
|
|
SaleOutStore_Detail outDetail = null;
|
|
if (details.Count() > 0)
|
|
{
|
|
outDetail = details.First();
|
|
}
|
|
if (dmo.BusinessProperty == B3Sale业务属性.正常出库)
|
|
targetDetail.Store_ID = dmo.Store_ID ?? 0;
|
|
else
|
|
{
|
|
if (outDetail != null)
|
|
targetDetail.Store_ID = outDetail.Store_ID ?? 0;
|
|
}
|
|
targetDetail.Goods_ID = item.SaleGoods_ID;
|
|
targetDetail.Number = item.Number.Value;
|
|
targetDetail.SecondNumber = item.SecondNumber;
|
|
targetDetail.GoodsBatch_ID = item.GoodsBatch_ID;
|
|
if (outDetail != null)
|
|
{
|
|
targetDetail.CargoSpace_ID = item.CargoSpace_ID;
|
|
targetDetail.Price = item.Price;
|
|
targetDetail.Money = item.Price * item.Number;
|
|
targetDetail.BrandItem_ID = item.BrandItem_ID;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var detail in tuples.Where(x => !string.IsNullOrEmpty(x.Item3)))
|
|
{
|
|
if (detail.Item4 == null || detail.Item4 == 0)
|
|
{
|
|
continue;
|
|
}
|
|
var targetDetail = new InOutStoreBill_Detail();
|
|
target.Details.Add(targetDetail);
|
|
var details = dmo.Details.Where(x => x.ID == detail.Item1);
|
|
SaleOutStore_Detail outDetail = null;
|
|
if (details.Count() > 0)
|
|
{
|
|
outDetail = details.First();
|
|
}
|
|
if (dmo.BusinessProperty == B3Sale业务属性.正常出库)
|
|
targetDetail.Store_ID = dmo.Store_ID ?? 0;
|
|
else
|
|
{
|
|
if (outDetail != null)
|
|
targetDetail.Store_ID = outDetail.Store_ID ?? 0;
|
|
}
|
|
targetDetail.Goods_ID = detail.Item2.Value;
|
|
targetDetail.Number = detail.Item4 ?? 0;
|
|
targetDetail.SecondNumber = detail.Item5 ?? 0;
|
|
var tuple = SaleUtil.GetGoodsBatch(DomainContext.Current.ID, detail.Item3, detail.Item2.Value);
|
|
if (tuple != null)
|
|
targetDetail.GoodsBatch_ID = tuple.Item1;
|
|
if (outDetail != null)
|
|
{
|
|
targetDetail.CargoSpace_ID = outDetail.CargoSpace_ID;
|
|
targetDetail.Price = outDetail.Price;
|
|
targetDetail.Money = outDetail.Price * targetDetail.Number;
|
|
targetDetail.BrandItem_ID = outDetail.BrandItem_ID;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static List<SaleOutStore_Detail> AddDetailByPdAndBatchNo(IDmoSession session, SaleOutStore saleOutStore, SaleOutStore_Detail outStoreDetail, Dictionary<long, List<TmpData>> dic)
|
|
{
|
|
if (!dic.ContainsKey(outStoreDetail.SaleGoods_ID))
|
|
{
|
|
dic.Add(outStoreDetail.SaleGoods_ID, GetStoreDetailInfor(session, saleOutStore.Store_ID, outStoreDetail.SaleGoods_ID));
|
|
}
|
|
var list = dic[outStoreDetail.SaleGoods_ID];
|
|
var theNum = outStoreDetail.Number ?? 0;//需要转的数量
|
|
var theSecNum = outStoreDetail.SecondNumber ?? 0;//需要转的辅数量
|
|
var num = theNum;
|
|
var details = new List<SaleOutStore_Detail>();
|
|
foreach (var unionView in list)
|
|
{
|
|
if (num <= 0)
|
|
break;
|
|
if (unionView.Number <= 0)
|
|
continue;
|
|
Money<decimal> thisNum;//当前明细分配数量
|
|
Money<decimal> thisSecNum;//当前明细分配辅数量
|
|
if (unionView.Number >= num)
|
|
{
|
|
thisNum = num;
|
|
}
|
|
else
|
|
{
|
|
thisNum = unionView.Number;
|
|
}
|
|
if (unionView.SecondNumber >= theSecNum)
|
|
{
|
|
thisSecNum = theSecNum;
|
|
}
|
|
else
|
|
{
|
|
thisSecNum = unionView.SecondNumber;
|
|
}
|
|
|
|
var outDetail = new SaleOutStore_Detail();
|
|
DmoUtil.CopyDmoFields(outStoreDetail, outDetail, "UnitNum", "Number", "SecondNumber", "Money", "RebateMoney");
|
|
outDetail.Price = outStoreDetail.Price;
|
|
outDetail.ProductionDate = unionView.ProductionDate;
|
|
outDetail.GoodsBatch_ID = unionView.GoodsBatch_ID;
|
|
outDetail.Number = thisNum;
|
|
if (outDetail.RightRatio != 0)
|
|
outDetail.UnitNum = outDetail.Number / outDetail.RightRatio * outDetail.LeftRatio;
|
|
if (outDetail.Goods_UnitConvertDirection == 主辅转换方向.由主至辅 || outDetail.Goods_UnitConvertDirection == 主辅转换方向.双向转换)
|
|
{
|
|
outDetail.SecondNumber = outDetail.Number / outDetail.RightRatio * outDetail.LeftRatio;
|
|
thisSecNum = outDetail.SecondNumber ?? 0;
|
|
}
|
|
else
|
|
{
|
|
outDetail.SecondNumber = thisSecNum;
|
|
}
|
|
outDetail.HiddenNumber = outDetail.Number;
|
|
outDetail.HiddenUnitNum = outDetail.UnitNum;
|
|
outDetail.HiddenSecondNumber = outDetail.SecondNumber;
|
|
|
|
details.Add(outDetail);
|
|
num -= thisNum;
|
|
theSecNum -= thisSecNum;
|
|
unionView.Number -= thisNum;
|
|
unionView.SecondNumber -= thisSecNum;
|
|
}
|
|
|
|
if (num != 0)
|
|
throw new Exception(string.Format("{0}可用库存还差{1},不允许出库,审核失败", outStoreDetail.Goods_Name, num));
|
|
var lastDetail = details.LastOrDefault();
|
|
if (lastDetail != null)
|
|
{
|
|
lastDetail.Money = (lastDetail.Money ?? 0m) + (outStoreDetail.Money ?? 0) - details.Sum(x => (x.Money ?? 0).Value);
|
|
lastDetail.SecondNumber = (lastDetail.SecondNumber ?? 0m) + (outStoreDetail.SecondNumber ?? 0) - details.Sum(x => (x.SecondNumber ?? 0).Value);
|
|
}
|
|
return details;
|
|
}
|
|
|
|
static List<TmpData> GetStoreDetailInfor(IDmoSession session, long? store_ID, long? saleGoods_ID)
|
|
{
|
|
var dom1 = new DQueryDom(new JoinAlias(typeof(StoreDetail)));
|
|
dom1.Columns.Add(DQSelectColumn.Field("GoodsBatch_ProductionDate"));
|
|
dom1.Columns.Add(DQSelectColumn.Field("GoodsBatch_ID"));
|
|
dom1.Columns.Add(DQSelectColumn.Field("Number"));
|
|
dom1.Columns.Add(DQSelectColumn.Field("SecondNumber"));
|
|
dom1.Where.Conditions.Add(DQCondition.EQ("Goods_ID", saleGoods_ID));
|
|
dom1.Where.Conditions.Add(DQCondition.EQ("Store_ID", store_ID));
|
|
dom1.Where.Conditions.Add(DQCondition.GreaterThan(DQExpression.Field("Number"), DQExpression.Value(0)));
|
|
dom1.OrderBy.Expressions.Add(DQOrderByExpression.Create("GoodsBatch_ProductionDate"));
|
|
dom1.OrderBy.Expressions.Add(DQOrderByExpression.Create("GoodsBatch_ID"));
|
|
|
|
var list = new List<TmpData>();
|
|
using (var reader = session.ExecuteReader(dom1))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
var tmp = new TmpData();
|
|
tmp.ProductionDate = (DateTime?)reader[0];
|
|
tmp.GoodsBatch_ID = (long?)reader[1];
|
|
tmp.Number = (Money<decimal>?)reader[2] ?? 0;
|
|
tmp.SecondNumber = (Money<decimal>?)reader[3] ?? 0;
|
|
list.Add(tmp);
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
class TmpData
|
|
{
|
|
public long? GoodsBatch_ID { get; set; }
|
|
public DateTime? ProductionDate { get; set; }
|
|
public Money<decimal> Number { get; set; }
|
|
public Money<decimal> SecondNumber { get; set; }
|
|
}
|
|
}
|
|
}
|