using BWP.B3CowButcherManage.NamedValueTemplate;
|
|
using BWP.B3Frameworks;
|
|
using BWP.B3Frameworks.BL;
|
|
using BWP.B3Frameworks.BO.NamedValueTemplate;
|
|
using BWP.B3Frameworks.Utils;
|
|
using BWP.B3SheepButcherManage.BO;
|
|
using BWP.B3SheepButcherManage.Utils;
|
|
using Forks.EnterpriseServices;
|
|
using Forks.EnterpriseServices.BusinessInterfaces;
|
|
using Forks.EnterpriseServices.DomainObjects2;
|
|
using Forks.EnterpriseServices.DomainObjects2.DQuery;
|
|
using Forks.EnterpriseServices.SimpleScript;
|
|
using Forks.EnterpriseServices.SqlDoms;
|
|
using Forks.Utils;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using TSingSoft.WebPluginFramework;
|
|
using CW = BWP.B3CowButcherManage.BO;
|
|
|
|
namespace BWP.B3SheepButcherManage.BL
|
|
{
|
|
[LogicName("羊结算单")]
|
|
[BusinessInterface(typeof(StatPayBL))]
|
|
public interface IStatPayBL : IDepartmentWorkFlowBillBL<StatPay>
|
|
{
|
|
[Log(Disabled = true)]
|
|
void SetDetailPrice(StatPay dmo);
|
|
|
|
[Log(Disabled = true)]
|
|
void SetButcherDetails(List<long> detailList, long statPayID);
|
|
|
|
[Log(Disabled = true)]
|
|
void CancelButcherDetails(List<long> detailList, long statPayID);
|
|
|
|
[Log(Disabled = true)]
|
|
void AddButcherDetails(StatPay dmo);
|
|
|
|
[Log(Disabled = true)]
|
|
void CancelAllButcherDetails(long statPayID);
|
|
|
|
[Log(Disabled = true)]
|
|
void SetHurryButchers(List<long> detailList, long statPayID);
|
|
|
|
[Log(Disabled = true)]
|
|
void CancelHurryButchers(List<long> detailList, long statPayID);
|
|
|
|
[Log(Disabled = true)]
|
|
void CancelAllHurryButchers(long statPayID);
|
|
|
|
[Log(Disabled = true)]
|
|
void AddHurryButchers(StatPay dmo);
|
|
|
|
[Log(Disabled = true)]
|
|
void AddOtherReward(StatPay dmo);
|
|
|
|
void WeightCreateStayDom(StatPay statPay);
|
|
}
|
|
|
|
public class StatPayBL : DepartmentWorkFlowBillBL<StatPay>, IStatPayBL
|
|
{
|
|
protected override void doDelete(StatPay dmo)
|
|
{
|
|
dmo.WeightBill_ID = null;
|
|
base.doDelete(dmo);
|
|
}
|
|
|
|
protected override void doInitNewDmo(StatPay dmo)
|
|
{
|
|
base.doInitNewDmo(dmo);
|
|
FillValuationArea(dmo);
|
|
}
|
|
|
|
void FillValuationArea(StatPay dmo)
|
|
{
|
|
if (dmo.AccountingUnit_ID == null)
|
|
return;
|
|
var query = new DQueryDom(new JoinAlias(typeof(CW.ValuationArea)));
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("Name"));
|
|
query.Range = SelectRange.Top(2);
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("AccountingUnit_ID", dmo.AccountingUnit_ID), DQCondition.EQ("Stopped", false)));
|
|
var result = query.EExecuteList<long, string>();
|
|
if (result.Count == 1)
|
|
{
|
|
dmo.ValuationArea_ID = result.First().Item1;
|
|
dmo.ValuationArea_Name = result.First().Item2;
|
|
}
|
|
}
|
|
|
|
protected override void beforeDelete(StatPay dmo)
|
|
{
|
|
//删除的时候删掉屠宰记录明细上的结算单号
|
|
UpdateButcherDetailStatPayIdNull(dmo.Details.Select(x => x.StatPay_ID).ToList());
|
|
base.beforeDelete(dmo);
|
|
}
|
|
|
|
void UpdateButcherDetailStatPayIdNull(List<long> ids)
|
|
{
|
|
if (ids.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
var dom = new DQUpdateDom(typeof(Butcher_Detail));
|
|
dom.Where.Conditions.EFieldInList("ID", ids);
|
|
dom.Columns.Add(new DQUpdateColumn("StatPay_ID", DQExpression.NULL));
|
|
Session.ExecuteNonQuery(dom);
|
|
}
|
|
|
|
|
|
protected override void beforeSave(StatPay dmo)
|
|
{
|
|
if (dmo.ValuationArea_ID == null)
|
|
FillValuationArea(dmo);
|
|
RefreshPriceID(dmo);
|
|
|
|
foreach (var detail in dmo.HurryButcherDetails)
|
|
{
|
|
detail.Money = detail.Price * detail.PriceWeight;
|
|
}
|
|
|
|
foreach (var detail in dmo.Details)
|
|
{
|
|
detail.Money = detail.PriceWeight * detail.Price * ((detail.Discount ?? 100) / 100);
|
|
}
|
|
|
|
|
|
foreach (StatPay_OtherReward detail in dmo.OtherRewardDetails)
|
|
{
|
|
detail.Money = detail.Price * detail.Number;
|
|
}
|
|
|
|
if (dmo.MoneyCountMethod != 计价方式.毛重价)
|
|
{
|
|
foreach (StatPay_Exception detail in dmo.ExceptionDetails)
|
|
{
|
|
detail.Money = detail.Price * detail.Number;
|
|
}
|
|
}
|
|
|
|
dmo.BillMoney = dmo.Details.Sum(x => (x.Money ?? 0).Value) + dmo.HurryButcherDetails.Sum(x => (x.Money ?? 0).Value) + dmo.ExceptionDetails.Sum(x => (x.Money ?? 0).Value) + dmo.OtherRewardDetails.Sum(x => (x.Money ?? 0).Value);
|
|
|
|
dmo.ExceptionMoney = dmo.ExceptionDetails.Sum(x => (x.Money ?? 0).Value);
|
|
dmo.OtherRewardMoney = dmo.OtherRewardDetails.Sum(x => (x.Money ?? 0).Value);
|
|
|
|
SetDmoProperties(dmo);
|
|
|
|
if (dmo.MoneyCountMethod == 计价方式.毛重价)
|
|
{
|
|
SetPrice(dmo);
|
|
foreach (var item in dmo.WeightDetails)
|
|
{
|
|
item.Money = item.Price * item.Weight;
|
|
|
|
}
|
|
dmo.Money = dmo.WeightDetails.Sum(x => (x.Money ?? 0).Value) - dmo.ExceptionDetails.Sum(x => (x.Money ?? 0).Value) - dmo.OtherRewardDetails.Sum(x => (x.Money ?? 0).Value);
|
|
|
|
}
|
|
base.beforeSave(dmo);
|
|
}
|
|
|
|
public void WeightCreateStayDom(StatPay dmo)
|
|
{
|
|
if (dmo.ValuationArea_ID == null)
|
|
FillValuationArea(dmo);
|
|
RefreshPriceID(dmo);
|
|
if (dmo.MoneyCountMethod == 计价方式.毛重价)
|
|
{
|
|
SetPrice(dmo);
|
|
foreach (var item in dmo.WeightDetails)
|
|
{
|
|
item.Money = item.Price * item.Number;
|
|
|
|
}
|
|
dmo.Money = dmo.WeightDetails.Sum(x => (x.Money ?? 0).Value) -
|
|
dmo.ExceptionDetails.Sum(x => (x.Money ?? 0).Value) -
|
|
dmo.OtherRewardDetails.Sum(x => (x.Money ?? 0).Value);
|
|
}
|
|
}
|
|
|
|
private void SetPrice(StatPay dmo)
|
|
{
|
|
if (dmo.FixedPrice_ID == null || dmo.ValuationArea_ID == null)
|
|
return;
|
|
var bl = BIFactory.Create<IFixedPriceBL>(BCWrapClassType.Inner, this);
|
|
var weighPrice = bl.Load(dmo.FixedPrice_ID.Value);
|
|
if (weighPrice == null)
|
|
return;
|
|
foreach (var detail in dmo.WeightDetails)
|
|
{
|
|
var avgW = (detail.Number ?? 0) == 0 ? 0 : (detail.Weight / detail.Number);
|
|
var priceDetail = weighPrice.Details.FirstOrDefault(
|
|
x =>
|
|
{
|
|
var result = x.LiveVarieties_ID == detail.LiveVarieties_ID && x.ValuationArea_ID == dmo.ValuationArea_ID;
|
|
return result;
|
|
});
|
|
if (priceDetail == null)
|
|
continue;
|
|
detail.Price = priceDetail.Price;
|
|
}
|
|
|
|
}
|
|
|
|
private void RefreshPriceID(StatPay dmo)
|
|
{
|
|
var detail = new JoinAlias(typeof(FixedPrice_Detail));
|
|
var bill = new JoinAlias(typeof(FixedPrice));
|
|
var dom = new DQueryDom(bill);
|
|
dom.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(bill, "ID", detail, "FixedPrice_ID"));
|
|
dom.Where.Conditions.Add(DQCondition.EQ(detail, "ValuationArea_ID", dmo.ValuationArea_ID));
|
|
dom.Where.Conditions.Add(DQCondition.EQ(bill, "AccountingUnit_ID", dmo.AccountingUnit_ID));
|
|
dom.Where.Conditions.Add(DQCondition.EQ("BillState", 单据状态.已审核));
|
|
dom.Where.Conditions.Add(DQCondition.LessThanOrEqual("ExeTime", dmo.WeighTime));
|
|
dom.Columns.Add(DQSelectColumn.Field("ID"));
|
|
dom.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true));
|
|
var id = dom.EExecuteScalar<long?>(Session);
|
|
if (id != dmo.FixedPrice_ID)
|
|
{
|
|
if (dmo.Details.Count != 0)
|
|
{
|
|
throw new ApplicationException("定价单已变更!需要清除屠宰明细");
|
|
}
|
|
if (dmo.HurryButcherDetails.Count != 0)
|
|
{
|
|
throw new ApplicationException("定价单已变更!需要清除急宰明细");
|
|
}
|
|
}
|
|
dmo.FixedPrice_ID = id;
|
|
}
|
|
|
|
private void SetDmoProperties(StatPay dmo)
|
|
{
|
|
dmo.SumWeight = dmo.Details.Sum(x => (x.Weight ?? 0).Value);
|
|
dmo.PriceWeight = dmo.Details.Sum(x => (x.PriceWeight ?? 0).Value);
|
|
dmo.HurryPriceWeight = dmo.HurryButcherDetails.Sum(x => (x.PriceWeight ?? 0).Value);
|
|
dmo.StatPayTotalWeight = (dmo.PriceWeight ?? 0) + (dmo.HurryPriceWeight ?? 0);
|
|
|
|
if (dmo.RealWeight.HasValue && dmo.RealWeight.Value != 0)
|
|
{
|
|
dmo.CheckTareWeightPrice = (dmo.BillMoney ?? 0) / dmo.RealWeight;
|
|
if (dmo.CheckTareWeightPrice != null)
|
|
{
|
|
dmo.CheckTareWeightPrice = decimal.Round(dmo.CheckTareWeightPrice.Value.Value, 2);
|
|
}
|
|
//胴体率计算公式=胴体重/毛重
|
|
dmo.NewBodyRate = dmo.Details.Sum(x => (x.Weight ?? 0).Value) / dmo.RealWeight;
|
|
}
|
|
dmo.Money = dmo.BillMoney;
|
|
|
|
//5、实付单价默认=毛重单价,可以修改,并保存;
|
|
//6、实付金额=收购重量*实付单价
|
|
|
|
if (!dmo.UpdateActualPrice)
|
|
{
|
|
dmo.ActualMoney = dmo.Money;
|
|
}
|
|
else
|
|
{
|
|
dmo.ActualMoney = dmo.ActualPrice * dmo.RealWeight;
|
|
}
|
|
|
|
if (dmo.RealWeight != null && dmo.RealWeight != 0)
|
|
{
|
|
//毛重均价=应付金额/收购重量 调整为 毛重均价=实付金额/收购重量
|
|
dmo.RawPrice = Math.Round((dmo.ActualMoney ?? 0) / (dmo.RealWeight), 2);
|
|
}
|
|
|
|
if (!dmo.UpdateActualPrice)
|
|
{
|
|
dmo.ActualPrice = dmo.RawPrice;
|
|
}
|
|
}
|
|
|
|
|
|
protected override List<string> MinDmoProperties
|
|
{
|
|
get
|
|
{
|
|
var list = base.MinDmoProperties;
|
|
list.Add("WeightBill_ID");
|
|
list.Add("UpdateActualPrice");
|
|
return list;
|
|
}
|
|
}
|
|
|
|
public void SetButcherDetails(List<long> detailList, long statPayID)
|
|
{
|
|
SetDetails<Butcher_Detail>(detailList, statPayID);
|
|
}
|
|
|
|
public void CancelButcherDetails(List<long> detailList, long statPayID)
|
|
{
|
|
CancelDetails<Butcher_Detail>(detailList, statPayID);
|
|
}
|
|
|
|
//屠宰记录汇总 急宰汇总 重新计算收购头数,收购重量
|
|
public void ResetAcquisitionInfo(StatPay dmo)
|
|
{
|
|
var 屠宰记录头数 = dmo.Details.Sum(x => x.Number ?? 0);
|
|
var 急宰记录头数 = dmo.HurryButcherDetails.Sum(x => x.Number ?? 0); //StatPay_HurryButcher
|
|
//var 磅后记录头数 = dmo.HurryButcherDetails.Sum(x => x.Number ?? 0);
|
|
// if (屠宰记录头数 + 急宰记录头数 == dmo.RealNumber)
|
|
// {
|
|
// return;
|
|
// }
|
|
var 总头数 = 屠宰记录头数 + 急宰记录头数;
|
|
var 过磅单已结算 = Get过磅单已结算(dmo.ID, dmo.WeightBill_ID);
|
|
var 已结算过磅单头数 = 过磅单已结算.Item1;
|
|
var 已结算过磅单重量 = Math.Round((decimal)过磅单已结算.Item2, 2);
|
|
if (dmo.WeighBill_BuyNum == null || dmo.WeighBill_BuyWeigh1 == null)
|
|
DmoUtil.RefreshDependency(dmo, "WeightBill_ID");
|
|
if (总头数 + 已结算过磅单头数 > (dmo.WeighBill_BuyNum ?? 0))
|
|
{
|
|
throw new Exception("已结算头数:" + 已结算过磅单头数 + " 本次结算头数:" + 总头数 + "不能大于 过磅单:" + dmo.WeightBill_ID + " 总收购头数:" + dmo.WeighBill_BuyNum);
|
|
}
|
|
|
|
dmo.RealNumber = 总头数;//收购头数计算
|
|
|
|
var 榜前急宰重量 = Math.Round(dmo.HurryButcherDetails.Sum(x => (decimal?)x.Weight ?? 0), 2);
|
|
if (总头数 == (dmo.WeighBill_BuyNum ?? 0))
|
|
{
|
|
//如果刚好等于过磅单的头数,则重量直接减掉,防止小数点
|
|
dmo.RealWeight = Math.Round((decimal)(dmo.WeighBill_BuyWeigh1 ?? 0), 2);//收购重量计算
|
|
}
|
|
else
|
|
{
|
|
var 过磅单每头猪的重量 = dmo.WeighBill_BuyWeigh1 / dmo.WeighBill_BuyNum;
|
|
var 本次榜后结算重量 = Math.Round((总头数 * 过磅单每头猪的重量 ?? 0), 2);
|
|
dmo.RealWeight = 榜前急宰重量 + 本次榜后结算重量;
|
|
}
|
|
//更新到数据库
|
|
UpdateHeadNumAndWeight(dmo.ID, dmo.RealNumber, dmo.RealWeight);
|
|
}
|
|
|
|
private void UpdateHeadNumAndWeight(long dmoId, int? dmoRealNumber, Money<decimal>? dmoRealWeight)
|
|
{
|
|
var updateDom = new DQUpdateDom(typeof(StatPay));
|
|
updateDom.Where.Conditions.Add(DQCondition.EQ("ID", dmoId));
|
|
updateDom.Columns.Add(new DQUpdateColumn("RealNumber", dmoRealNumber));
|
|
updateDom.Columns.Add(new DQUpdateColumn("RealWeight", dmoRealWeight));
|
|
Session.ExecuteNonQuery(updateDom);
|
|
}
|
|
|
|
private Tuple<int, Money<decimal>> Get过磅单已结算(long dmoId, long? dmoWeighId)
|
|
{
|
|
var query = new DmoQuery(typeof(StatPay));
|
|
query.Where.Conditions.Add(DQCondition.InEQ("BillState", 单据状态.已作废));
|
|
query.Where.Conditions.Add(DQCondition.InEQ("ID", dmoId));
|
|
query.Where.Conditions.Add(DQCondition.EQ("WeightBill_ID", dmoWeighId ?? 0));
|
|
query.Where.Conditions.Add(DQCondition.EQ("Domain_ID", DomainContext.Current.ID));
|
|
|
|
var list = Session.ExecuteList(query).Cast<StatPay>().ToList();
|
|
var 屠宰记录头数 = list.Sum(x => x.Details.Sum(y => y.Number ?? 0));
|
|
var 榜后急宰记录头数 = list.Sum(x => x.HurryButcherDetails.Sum(z => z.Number ?? 0));
|
|
var 已结算头数 = 屠宰记录头数 + 榜后急宰记录头数;
|
|
|
|
// var 屠宰记录重量 = list.Sum(x => x.Details.Sum(y => (decimal?)y.Weight ?? 0));
|
|
// var 榜后急宰记录重量 = list.Sum(x => x.HurryButcherDetails.Where(y => y.AfterWeigh).Sum(z => (decimal?)z.Weight ?? 0));
|
|
var 榜前急宰记录重量 = list.Sum(x => x.HurryButcherDetails.Sum(z => (decimal?)z.Weight ?? 0));
|
|
|
|
var 结算单总收购重量 = list.Sum(x => (decimal?)x.RealWeight ?? 0);
|
|
var 已结算收购重量 = 结算单总收购重量 - 榜前急宰记录重量;
|
|
return new Tuple<int, Money<decimal>>(已结算头数, 已结算收购重量);
|
|
|
|
}
|
|
|
|
//屠宰记录汇总
|
|
public void AddButcherDetails(StatPay dmo)
|
|
{
|
|
var dom = new DQueryDom(new JoinAlias(typeof(Butcher_Detail)));//屠宰单明细
|
|
dom.Where.Conditions.Add(DQCondition.EQ("StatPay_ID", dmo.ID));//结算单
|
|
dom.Columns.Add(DQSelectColumn.Field("Butcher_ID"));
|
|
dom.Columns.Add(DQSelectColumn.Field("Livestock_ID"));
|
|
dom.Columns.Add(DQSelectColumn.Field("Livestock_Name"));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Field("FinalWeight"));
|
|
dom.Columns.Add(DQSelectColumn.Field("Remark"));
|
|
dom.Columns.Add(DQSelectColumn.Field("LiveVarieties_ID"));
|
|
dom.Columns.Add(DQSelectColumn.Field("LiveVarieties_Name"));
|
|
dom.OrderBy.Expressions.Add(DQOrderByExpression.Create("LiveVarieties_ID", false));
|
|
var list = new StatPay_ButcherCollection();
|
|
using (var reader = Session.ExecuteReader(dom))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
var detail = new StatPay_Butcher();
|
|
detail.Butcher_ID = (long?)reader[0];
|
|
detail.Livestock_ID = (long?)reader[1];
|
|
detail.Livestock_Name = (string)reader[2];
|
|
detail.Number = 1;
|
|
detail.Weight = (Money<decimal>?)(reader[3]);
|
|
detail.Remark = (string)reader[4];
|
|
detail.OldPriceWeight = detail.Weight;
|
|
detail.PriceWeight = detail.Weight;
|
|
detail.LiveVarieties_ID = (long?)reader[5];
|
|
detail.LiveVarieties_Name = (string)reader[6];
|
|
list.Add(detail);
|
|
}
|
|
}
|
|
|
|
SetPrice(dmo, list);
|
|
|
|
dmo.Details.Clear();
|
|
var groupByPrice = list.GroupBy(x => new { x.LiveVarieties_ID, x.Livestock_ID, x.Butcher_ID, x.Remark, x.Price });
|
|
|
|
foreach (var newDetail in groupByPrice)
|
|
{
|
|
var weight = newDetail.Sum(x => (x.Weight ?? 0).Value);
|
|
var first = newDetail.FirstOrDefault();
|
|
var d = new StatPay_Butcher
|
|
{
|
|
Butcher_ID = first.Butcher_ID,
|
|
Livestock_ID = first.Livestock_ID,
|
|
Livestock_Name = first.Livestock_Name,
|
|
LiveVarieties_ID = first.LiveVarieties_ID,
|
|
LiveVarieties_Name = first.LiveVarieties_Name,
|
|
Number = newDetail.Count(),
|
|
OldPriceWeight = weight,
|
|
Weight = weight,
|
|
Discount = first.Discount,
|
|
Remark = first.Remark,
|
|
Price = first.Price
|
|
};
|
|
d.PriceWeight = d.Weight;
|
|
|
|
d.Money = d.PriceWeight * d.Price * ((d.Discount ?? 100) / 100);
|
|
dmo.Details.Add(d);
|
|
}
|
|
|
|
ResetAcquisitionInfo(dmo);
|
|
}
|
|
|
|
public void CancelAllButcherDetails(long statPayID)
|
|
{
|
|
var wid = InnerBLUtil.GetDmoPropertyByID<long?>(Session, typeof(StatPay), "WeightBill_ID", statPayID);
|
|
if (wid.HasValue)
|
|
{
|
|
var sql = string.Format("update d set d.StatPay_ID = null from B3SheepButcherManage_Butcher_Detail d left outer join B3SheepButcherManage_StatPay m on d.StatPay_ID=m.ID where d.WeightBill_ID={0} and m.ID ={1} and d.StatPay_ID is not null", wid, statPayID);
|
|
Session.ExecuteSqlNonQuery(sql);
|
|
}
|
|
CancelAllDetails<Butcher_Detail>(statPayID);
|
|
}
|
|
|
|
public void SetHurryButchers(List<long> detailList, long statPayID)
|
|
{
|
|
SetDetails<HurryButcher_Detail>(detailList, statPayID);
|
|
}
|
|
|
|
public void CancelHurryButchers(List<long> detailList, long statPayID)
|
|
{
|
|
CancelDetails<HurryButcher_Detail>(detailList, statPayID);
|
|
}
|
|
|
|
public void CancelAllHurryButchers(long statPayID)
|
|
{
|
|
CancelAllDetails<HurryButcher_Detail>(statPayID);
|
|
}
|
|
|
|
public void AddHurryButchers(StatPay dmo)
|
|
{
|
|
var detailAlias = new JoinAlias(typeof(HurryButcher_Detail));
|
|
var billAlias = new JoinAlias(typeof(HurryButcher));
|
|
var dom = new DQueryDom(detailAlias);
|
|
dom.From.AddJoin(JoinType.Inner, new DQDmoSource(billAlias), DQCondition.EQ(billAlias, "ID", detailAlias, "HurryButcher_ID"));
|
|
|
|
dom.Where.Conditions.Add(DQCondition.EQ(detailAlias, "StatPay_ID", dmo.ID));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Field("HurryButcher_ID"));
|
|
dom.GroupBy.Expressions.Add(DQExpression.Field("HurryButcher_ID"));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Field("Livestock_ID"));
|
|
dom.GroupBy.Expressions.Add(DQExpression.Field("Livestock_ID"));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Field("Livestock_Name"));
|
|
dom.GroupBy.Expressions.Add(DQExpression.Field("Livestock_Name"));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Sum("Weight"));
|
|
dom.Columns.Add(DQSelectColumn.Sum("Number"));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Field("Remark"));
|
|
dom.GroupBy.Expressions.Add(DQExpression.Field("Remark"));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Sum("GrossWeight"));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Field("LiveVarieties_ID"));
|
|
dom.GroupBy.Expressions.Add(DQExpression.Field("LiveVarieties_ID"));
|
|
|
|
dom.Columns.Add(DQSelectColumn.Field("LiveVarieties_Name"));
|
|
dom.GroupBy.Expressions.Add(DQExpression.Field("LiveVarieties_Name"));
|
|
|
|
using (var reader = Session.ExecuteReader(dom))
|
|
{
|
|
dmo.HurryButcherDetails.Clear();
|
|
while (reader.Read())
|
|
{
|
|
var detail = new StatPay_HurryButcher();
|
|
detail.HurryButcher_ID = (long?)reader[0];
|
|
detail.Livestock_ID = (long?)reader[1];
|
|
detail.Livestock_Name = (string)reader[2];
|
|
detail.Weight = (Money<decimal>?)reader[3];
|
|
detail.Number = (int?)reader[4];
|
|
detail.Remark = (string)reader[5];
|
|
detail.PriceWeight = detail.Weight;
|
|
detail.GrossWeight = (Money<decimal>?)reader[6];
|
|
detail.LiveVarieties_ID = (long?)reader[7];
|
|
detail.LiveVarieties_Name = (string)reader[8];
|
|
dmo.HurryButcherDetails.Add(detail);
|
|
}
|
|
}
|
|
|
|
SetPrice(dmo, dmo.HurryButcherDetails);
|
|
foreach (var detail in dmo.HurryButcherDetails)
|
|
{
|
|
detail.Money = detail.Price * detail.Weight;
|
|
}
|
|
|
|
ResetAcquisitionInfo(dmo);
|
|
}
|
|
|
|
public void AddOtherReward(StatPay dmo)
|
|
{
|
|
dmo.OtherRewardDetails.Clear();
|
|
var sanction = new JoinAlias(typeof(CW.SanctionSetting));
|
|
var abnormalItem = new JoinAlias(typeof(CW.RewardItem));
|
|
|
|
var dom = new DQueryDom(sanction);
|
|
dom.From.AddJoin(JoinType.Left, new DQDmoSource(abnormalItem),
|
|
DQCondition.EQ(sanction, "RewardItem_ID", abnormalItem, "ID"));
|
|
dom.Columns.Add(DQSelectColumn.Field("Name", abnormalItem));
|
|
dom.Columns.Add(DQSelectColumn.Field("ID", sanction));
|
|
dom.Columns.Add(DQSelectColumn.Field("Condition", sanction));
|
|
dom.Columns.Add(DQSelectColumn.Field("BeforeScript", sanction));
|
|
dom.Where.Conditions.Add(DQCondition.EQ(sanction, "FitBill", 奖罚设置适用单据类型.结算单));
|
|
dom.Where.Conditions.Add(DQCondition.EQ("BillState", 单据状态.已审核));
|
|
var sanctionList = dom.EExecuteList<string, long, string, string>(Session);
|
|
foreach (var tuple in sanctionList)
|
|
{
|
|
if (string.IsNullOrEmpty(tuple.Item3))
|
|
continue;
|
|
if (string.IsNullOrEmpty(tuple.Item4))
|
|
continue;
|
|
var runner = new Runner();
|
|
runner.Global["奖罚依据"] = "";
|
|
runner.Execute(tuple.Item4);
|
|
var bType = TypeMarshal.AsString(runner.Global["奖罚依据"]);
|
|
switch (bType)
|
|
{
|
|
case "羊屠宰单":
|
|
AddRewardWithButcher(dmo, runner, tuple);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void AddDefaultOtherReward(StatPay dmo, Runner runner, Tuple<string, long, string, string> tuple)
|
|
{
|
|
runner.Global["单价"] = 0m;
|
|
runner.Global["头数"] = 0;
|
|
runner.Global["金额"] = 0m;
|
|
|
|
runner.Execute(tuple.Item3 ?? string.Empty);
|
|
var price = TypeMarshal.AsNumber(runner.Global["单价"]);
|
|
var number = TypeMarshal.AsNumber(runner.Global["头数"]);
|
|
var money = TypeMarshal.AsNumber(runner.Global["金额"]);
|
|
|
|
var reDetail = new StatPay_OtherReward();
|
|
reDetail.Price = price;
|
|
reDetail.Number = number;
|
|
reDetail.Money = money;
|
|
|
|
if ((reDetail.Money ?? 0) == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
reDetail.RewardItem_ID = tuple.Item2;
|
|
reDetail.RewardItem_Name = tuple.Item1;
|
|
DmoUtil.RefreshDependency(reDetail, "RewardItem_ID");
|
|
dmo.OtherRewardDetails.Add(reDetail);
|
|
}
|
|
|
|
private DQueryDom GetRewardButcherIdDom(long? dmoWeighId)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(Butcher_Detail)));
|
|
query.Where.Conditions.Add(DQCondition.EQ("WeightBill_ID", dmoWeighId));
|
|
query.Columns.Add(DQSelectColumn.Field("Butcher_ID"));
|
|
query.Distinct = true;
|
|
return query;
|
|
}
|
|
|
|
|
|
//根据屠宰单号分开
|
|
private void AddRewardWithButcherAbnormal(StatPay dmo, Runner runner, Tuple<string, long, string, string> tuple)
|
|
{
|
|
var dom1 = new DQueryDom(new JoinAlias(typeof(Butcher_Detail)));
|
|
dom1.Where.Conditions.Add(DQCondition.EQ("StatPay_ID", dmo.ID));
|
|
dom1.Columns.Add(DQSelectColumn.Field("PrePhase"));
|
|
dom1.Columns.Add(DQSelectColumn.Field("Butcher_ID"));
|
|
dom1.Distinct = true;
|
|
var result = dom1.EExecuteList<int?, long?>(Session);
|
|
if (result == null)
|
|
return;
|
|
var phaseList = result.GroupBy(x => x.Item2);
|
|
|
|
|
|
|
|
// var detail = new JoinAlias(typeof(ButcherAbnormalRecordDetailed));
|
|
// var bill = new JoinAlias(typeof(ButcherAbnormalRecord));
|
|
// var dom = new DQueryDom(detail);
|
|
// var allCondtions = DQCondition.Or(phaseList.Select(x =>
|
|
//DQCondition.And(
|
|
// DQCondition.EQ(bill, "Butcher_ID", x.Key),
|
|
// DQCondition.InList(
|
|
// DQExpression.Field(detail, "PrePhase"),
|
|
// x.Select(y => DQExpression.Value(y.Item1)).ToArray())
|
|
// )
|
|
|
|
//));
|
|
// dom.From.AddJoin(JoinType.Inner, new DQDmoSource(bill), DQCondition.EQ(detail, "ButcherAbnormalRecordDetailed_ID", bill, "ID"));
|
|
// dom.Where.Conditions.Add(DQCondition.And(DQCondition.EQ(bill, "AccountingUnit_ID", dmo.AccountingUnit_ID), DQCondition.EQ(bill, "BillState", 单据状态.已审核))); dom.Where.Conditions.Add(DQCondition.EQ(detail, "Sanction_ID", tuple.Item2));
|
|
// dom.Where.Conditions.Add(allCondtions);
|
|
// dom.Columns.Add(DQSelectColumn.Sum("Number"));
|
|
// dom.Columns.Add(DQSelectColumn.Sum("SubWeight"));
|
|
|
|
// int numberAbn = 0;
|
|
// decimal subWeightAbn = 0m;
|
|
// using (var reader = Session.ExecuteReader(dom))
|
|
// {
|
|
// if (reader.Read())
|
|
// {
|
|
// numberAbn = ((int?)reader[0]) ?? 0;
|
|
// subWeightAbn = ((decimal?)reader[1]) ?? 0;
|
|
// }
|
|
// }
|
|
// runner.Global["屠宰单结算重"] = 0;
|
|
// runner.Global["屠宰异常单头数"] = numberAbn;
|
|
// runner.Global["屠宰异常单扣重"] = subWeightAbn;
|
|
// runner.Global["单价"] = 0m;
|
|
// runner.Global["头数"] = 0;
|
|
// runner.Global["金额"] = 0m;
|
|
// runner.Global["结算奖罚重量"] = 0m;
|
|
|
|
// runner.Execute(tuple.Item3);
|
|
// var price = TypeMarshal.AsNumber(runner.Global["单价"]);
|
|
// var number = TypeMarshal.AsNumber(runner.Global["头数"]);
|
|
// var money = TypeMarshal.AsNumber(runner.Global["金额"]);
|
|
// var rewardWeight = TypeMarshal.AsNumber(runner.Global["结算奖罚重量"]);
|
|
|
|
// if (money == 0 && rewardWeight == 0)
|
|
// {
|
|
// return;
|
|
// }
|
|
|
|
// var reDetail = new StatPay_OtherReward();
|
|
// reDetail.Price = price;
|
|
// reDetail.Number = number;
|
|
// reDetail.Money = money;
|
|
// reDetail.Weight = rewardWeight;
|
|
// reDetail.Sanction_ID = tuple.Item2;
|
|
// reDetail.AbnormalItem_Name = tuple.Item1;
|
|
// DmoUtil.RefreshDependency(reDetail, "Sanction_ID");
|
|
// dmo.OtherRewardDetails.Add(reDetail);
|
|
}
|
|
|
|
private decimal? GetSubWeightByScanId(long weighid, long sanction_ID)
|
|
{
|
|
var bill = new JoinAlias(typeof(WeightBill));
|
|
var detail = new JoinAlias(typeof(WeightBill_AbnormalRecording));
|
|
//
|
|
var query = new DQueryDom(bill);
|
|
query.From.AddJoin(JoinType.Left, new DQDmoSource(detail), DQCondition.EQ(bill, "ID", detail, "WeightBill_ID"));
|
|
query.Where.Conditions.Add(DQCondition.EQ(bill, "ID", weighid));
|
|
query.Where.Conditions.Add(DQCondition.EQ(detail, "ID", sanction_ID));
|
|
query.Columns.Add(DQSelectColumn.Field("SubWeight", detail));
|
|
return query.EExecuteScalar<decimal?>(Session);
|
|
}
|
|
|
|
private void AddRewardWithButcher(StatPay dmo, Runner runner, Tuple<string, long, string, string> tuple)
|
|
{
|
|
var dom = new DQueryDom(new JoinAlias(typeof(Butcher_Detail)));//WeightBill_ID 过磅单号
|
|
dom.Where.Conditions.Add(DQCondition.EQ("StatPay_ID", dmo.ID));
|
|
dom.Columns.Add(DQSelectColumn.Field("Weight"));
|
|
dom.Columns.Add(DQSelectColumn.Field("WeightBill_ID"));
|
|
|
|
var list = new List<Tuple<Money<decimal>, long>>();
|
|
using (var reader = Session.ExecuteReader(dom))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
var w = (Money<decimal>?)reader[0];
|
|
if (w.HasValue)
|
|
list.Add(new Tuple<Money<decimal>, long>(w.Value, (long)reader[1]));
|
|
}
|
|
}
|
|
|
|
var details = new List<StatPay_OtherReward>();
|
|
foreach (var w in list)
|
|
{
|
|
runner.Global["屠宰单结算重"] = w.Item1;
|
|
runner.Global["屠宰异常单头数"] = 0;
|
|
runner.Global["单价"] = 0m;
|
|
runner.Global["头数"] = 0;
|
|
runner.Global["金额"] = 0m;
|
|
runner.Global["结算奖罚重量"] = 0m;
|
|
var var = new OnLoadVar
|
|
{
|
|
Loader = (() => GetSubWeightByScanId(w.Item2, tuple.Item2))
|
|
};
|
|
runner.Global["扣重"] = var;
|
|
runner.Execute(tuple.Item3);
|
|
var price = TypeMarshal.AsNumber(runner.Global["单价"]);
|
|
var number = TypeMarshal.AsNumber(runner.Global["头数"]);
|
|
var money = TypeMarshal.AsNumber(runner.Global["金额"]);
|
|
var rewardWeight = TypeMarshal.AsNumber(runner.Global["结算奖罚重量"]);
|
|
if (money == 0 && rewardWeight == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
|
|
var detail = new StatPay_OtherReward();
|
|
detail.Price = price;
|
|
detail.Number = number;
|
|
detail.Money = money;
|
|
detail.Weight = rewardWeight;
|
|
details.Add(detail);
|
|
}
|
|
var groups = details.GroupBy(x => x.Price);
|
|
foreach (var @group in groups)
|
|
{
|
|
var detail = @group.FirstOrDefault();
|
|
detail.RewardItem_ID = tuple.Item2;
|
|
detail.RewardItem_Name = tuple.Item1;
|
|
detail.Price = group.Key;
|
|
detail.Number = group.Sum(x => (x.Number ?? 0).Value);
|
|
detail.Money = group.Sum(x => (x.Money ?? 0).Value);
|
|
detail.Weight = group.Sum(x => (x.Weight ?? 0).Value);
|
|
dmo.OtherRewardDetails.Add(detail);
|
|
}
|
|
}
|
|
|
|
private void CancelAllDetails<T>(long statPayID)
|
|
{
|
|
var dom = new DQUpdateDom(typeof(T));
|
|
dom.Where.Conditions.Add(DQCondition.EQ("StatPay_ID", statPayID));
|
|
dom.Columns.Add(new DQUpdateColumn("StatPay_ID", DQExpression.NULL));
|
|
Session.ExecuteNonQuery(dom);
|
|
}
|
|
|
|
private void SetDetails<T>(IList ids, long statPayID)
|
|
{
|
|
if (ids.Count == 0)
|
|
throw new Exception("没有选择记录");
|
|
var dom = new DQUpdateDom(typeof(T));
|
|
dom.Where.Conditions.Add(DQCondition.IsNull(DQExpression.Field("StatPay_ID")));
|
|
|
|
dom.Where.Conditions.Add(DQCondition.IsNotNull(DQExpression.Field("Weight")));
|
|
|
|
dom.Where.Conditions.EFieldInList("ID", ids);
|
|
dom.Columns.Add(new DQUpdateColumn("StatPay_ID", statPayID));
|
|
Session.ExecuteNonQuery(dom);
|
|
}
|
|
|
|
private void CancelDetails<T>(IList ids, long statPayID)
|
|
{
|
|
if (ids.Count == 0)
|
|
throw new Exception("没有选择记录");
|
|
var dom = new DQUpdateDom(typeof(T));
|
|
dom.Where.Conditions.Add(DQCondition.EQ("StatPay_ID", statPayID));
|
|
dom.Where.Conditions.EFieldInList("ID", ids);
|
|
dom.Columns.Add(new DQUpdateColumn("StatPay_ID", DQExpression.NULL));
|
|
Session.ExecuteNonQuery(dom);
|
|
}
|
|
|
|
private void SetPrice(StatPay dmo, IEnumerable<IStatDetail> list)
|
|
{
|
|
foreach (var detail in list)
|
|
{
|
|
if (detail.Remark == null)
|
|
detail.Remark = "";
|
|
}
|
|
if (dmo.FixedPrice_ID == null || dmo.ValuationArea_ID == null)
|
|
return;
|
|
var bl = BIFactory.Create<IFixedPriceBL>(BCWrapClassType.Inner, this);
|
|
var weighPrice = bl.Load(dmo.FixedPrice_ID.Value);
|
|
if (weighPrice == null)
|
|
return;
|
|
bool isStatPay_Butcher = false;
|
|
bool isHurry = false;
|
|
if (list.Any())
|
|
{
|
|
var ft = list.First().GetType();
|
|
if (ft == typeof(StatPay_Butcher))
|
|
isStatPay_Butcher = true;
|
|
else if (ft == typeof(StatPay_HurryButcher))
|
|
isHurry = true;
|
|
}
|
|
foreach (var detail in list)
|
|
{
|
|
FixedPrice_Detail priceDetail = null;
|
|
if (isStatPay_Butcher)
|
|
priceDetail = weighPrice.Details.FirstOrDefault(
|
|
x => (x.LiveVarieties_ID ?? 0) == (detail.LiveVarieties_ID ?? 0) && x.Livestock_ID == detail.Livestock_ID && x.ValuationArea_ID == dmo.ValuationArea_ID);
|
|
else if (isHurry)
|
|
priceDetail =
|
|
weighPrice.Details.FirstOrDefault(
|
|
x => (x.LiveVarieties_ID ?? 0) == (detail.LiveVarieties_ID ?? 0) && x.Livestock_ID == detail.Livestock_ID && x.ValuationArea_ID == dmo.ValuationArea_ID);
|
|
else
|
|
priceDetail =
|
|
weighPrice.Details.FirstOrDefault(
|
|
x => x.Livestock_ID == detail.Livestock_ID && x.ValuationArea_ID == dmo.ValuationArea_ID);
|
|
if (priceDetail == null)
|
|
continue;
|
|
detail.Price = priceDetail.Price;
|
|
}
|
|
}
|
|
|
|
public void SetDetailPrice(StatPay dmo)
|
|
{
|
|
var list = BeforeSetPrice(dmo.ID, this);
|
|
DoSetDetailPrice(dmo, list);
|
|
}
|
|
|
|
private static StatPay_ButcherCollection BeforeSetPrice(long id, StatPayBL statPayBL)
|
|
{
|
|
var config = new B3SheepButcherManageConfig().SubtractWeight.Value;
|
|
if (config == 0)
|
|
config = 1m;
|
|
var dom = new DQueryDom(new JoinAlias(typeof(Butcher_Detail)));
|
|
dom.Where.Conditions.Add(DQCondition.EQ("StatPay_ID", id));
|
|
dom.Columns.Add(DQSelectColumn.Field("Butcher_ID"));
|
|
// 重量 - 扣重
|
|
dom.Columns.Add(DQSelectColumn.Create(DQExpression.Subtract(DQExpression.IfNull(DQExpression.Multiply(DQExpression.Field("Weight"), DQExpression.Value(config)), DQExpression.Value(0)), DQExpression.IfNull(DQExpression.Field("SubtractWeight"), DQExpression.Value(0))), "finalWeight"));
|
|
dom.Columns.Add(DQSelectColumn.Field("Remark"));
|
|
dom.Columns.Add(DQSelectColumn.Field("Livestock_ID"));
|
|
var list = new StatPay_ButcherCollection();
|
|
using (var reader = statPayBL.Session.ExecuteReader(dom))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
var detail = new StatPay_Butcher
|
|
{
|
|
Butcher_ID = (long?)reader[0],
|
|
Number = 1
|
|
};
|
|
var dec = Math.Floor(Convert.ToDecimal(reader[1]) * 10);
|
|
detail.Weight = dec / 10;
|
|
detail.Remark = (string)reader[2];
|
|
detail.OldPriceWeight = detail.Weight;
|
|
detail.PriceWeight = detail.Weight;
|
|
detail.Livestock_ID = (long?)reader[3];
|
|
list.Add(detail);
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private void DoSetDetailPrice(StatPay dmo, StatPay_ButcherCollection list)
|
|
{
|
|
if (dmo.WeightBill_ID == null || dmo.FixedPrice_ID == null || dmo.ValuationArea_ID == null)
|
|
return;
|
|
var bl = BIFactory.Create<IFixedPriceBL>(BCWrapClassType.Inner, this);
|
|
var weighPrice = bl.Load(dmo.FixedPrice_ID.Value);
|
|
if (weighPrice == null)
|
|
return;
|
|
foreach (var detail in list)
|
|
{
|
|
foreach (var priceDetail in weighPrice.Details.Where(x => x.ValuationArea_ID == dmo.ValuationArea_ID && x.Livestock_ID == detail.Livestock_ID))
|
|
{
|
|
detail.Price = priceDetail.Price;
|
|
detail.WeighPrice_DetailID = priceDetail.ID;
|
|
if (string.IsNullOrEmpty(detail.Remark))
|
|
{
|
|
detail.Remark = null;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
dmo.Details.Clear();
|
|
var groupByPrice = list.GroupBy(x => new { x.Livestock_ID, x.WeighPrice_DetailID, x.Butcher_ID, x.Remark, x.Discount, x.Price });
|
|
foreach (var newDetail in groupByPrice)
|
|
{
|
|
var weight = newDetail.Sum(x =>
|
|
{
|
|
if (x.Weight.HasValue)
|
|
return x.Weight.Value.Value;
|
|
else
|
|
return 0;
|
|
});
|
|
|
|
var d = new StatPay_Butcher
|
|
{
|
|
Butcher_ID = newDetail.First().Butcher_ID,
|
|
Livestock_ID = newDetail.First().Livestock_ID,
|
|
Livestock_Name = newDetail.First().Livestock_Name,
|
|
Number = newDetail.Count(),
|
|
Weight = weight,
|
|
Discount = newDetail.First().Discount,
|
|
Remark = newDetail.First().Remark,
|
|
OldPriceWeight = weight,
|
|
PriceWeight = weight,
|
|
Price = newDetail.First().Price
|
|
};
|
|
d.Money = d.PriceWeight * d.Price * ((d.Discount ?? 100) / 100);
|
|
dmo.Details.Add(d);
|
|
}
|
|
}
|
|
}
|
|
}
|