You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

230 lines
9.3 KiB

using CB = BWP.B3CowButcherManage.BO;
using BWP.B3CowButcherManage.NamedValueTemplate;
using BWP.B3Frameworks;
using BWP.B3Frameworks.BL;
using BWP.B3Frameworks.BO.NamedValueTemplate;
using BWP.B3SheepButcherManage.BO;
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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSingSoft.WebPluginFramework;
using BWP.B3Frameworks.Utils;
namespace BWP.B3SheepButcherManage.BL
{
[BusinessInterface(typeof(WeightBillBL))]
[LogicName("羊过磅单")]
public interface IWeightBillBL : IDepartmentWorkFlowBillBL<WeightBill>
{
void UpdateDetailOtherNumber(IEnumerable<long> weightDetailIDs);
}
public class WeightBillBL : DepartmentWorkFlowBillBL<WeightBill>, IWeightBillBL
{
protected override void beforeSave(WeightBill dmo)
{
foreach (var detail in dmo.WeighRecords)
detail.RemainNumber = detail.Number;
if (dmo.WeighRecords.Count > 0)
{
dmo.BuyWeigh1 = dmo.WeighRecords.Sum(x => (x.Weight ?? 0).Value);
dmo.BuyNum = dmo.WeighRecords.Sum(x => (x.Number));
}
SetAbnormal_Money(dmo);
foreach (var detail in dmo.WeighAbnormalRecording)
detail.Abnormal_Money = detail.Number * detail.Price;
dmo.AbnormalMoney = dmo.WeighAbnormalRecording.Sum(x => (x.Abnormal_Money ?? 0).Value);
base.beforeSave(dmo);
}
public void SetAbnormal_Money(WeightBill dmo)
{
var riIDs = dmo.WeighAbnormalRecording.Where(x => x.RewardItem_ID.HasValue).Select(x => x.RewardItem_ID).Distinct();
if (riIDs.Count() <= 0)
return;
var dom = new DQueryDom(new JoinAlias(typeof(CB.SanctionSetting)));
dom.Columns.Add(DQSelectColumn.Field("RewardItem_ID"));
dom.Columns.Add(DQSelectColumn.Field("Condition"));
dom.Columns.Add(DQSelectColumn.Field("BeforeScript"));
dom.Where.Conditions.Add(DQCondition.EQ("FitBill", .));
dom.Where.Conditions.Add(DQCondition.EQ("BillState", .));
dom.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("RewardItem_ID"), riIDs.Select(x => DQExpression.Value(x)).ToArray()));
var sanctionList = dom.EExecuteList<long, string, string>(Session);
foreach (var detail in dmo.WeighAbnormalRecording)
{
var items = sanctionList.Where(x => x.Item1 == detail.RewardItem_ID);
if (items.Count() > 0)
{
var result = items.First();
var runner = new Runner();
runner.Global["奖罚依据"] = "";
runner.Execute(result.Item3);
var bType = TypeMarshal.AsString(runner.Global["奖罚依据"]);
if (bType == "羊过磅单")
{
runner.Global["单价"] = 0m;
runner.Execute(result.Item2);
detail.Price = TypeMarshal.AsNumber(runner.Global["单价"]);
}
}
}
}
protected override void doCheck(WeightBill dmo)
{
var statPay = new StatPay();
var bl = BIFactory.Create<IStatPayBL>(Session);
bl.InitNewDmo(statPay);
statPay.Supplier_ID = dmo.Supplier_ID;
statPay.WeightBill_ID = dmo.ID;
statPay.WeighTime = dmo.WeighTime;
statPay.RealWeight = dmo.BuyWeigh1;
statPay.RealNumber = dmo.BuyNum;
statPay.Department_ID = dmo.Department_ID;
statPay.Employee_ID = dmo.Employee_ID;
statPay.AccountingUnit_ID = dmo.AccountingUnit_ID;
statPay.ValuationArea_ID = dmo.ValuationArea_ID;
if (dmo.MoneyCountMethod != null)
{
statPay.MoneyCountMethod = dmo.MoneyCountMethod.Value;
}
foreach (var abnormal in dmo.WeighAbnormalRecording)
{
var exception = new StatPay_Exception();
exception.RewardItem_ID = abnormal.RewardItem_ID;
exception.Number = abnormal.Number;
exception.Price = abnormal.Price;
exception.Money = abnormal.Abnormal_Money;
statPay.ExceptionDetails.Add(exception);
}
bl.Insert(statPay);
AppendMessage("生成结算单No." + statPay.ID);
base.doCheck(dmo);
}
protected override void doUnCheck(WeightBill dmo)
{
if (dmo.WeighRecords.Any(x => x.PlanNumber > 0))
throw new ApplicationException("过磅单已经排宰");
var stayPayBL = BIFactory.Create<IStatPayBL>(Session);
var queryDom = new DmoQuery(typeof(StatPay));
queryDom.Where.Conditions.Add(DQCondition.EQ("WeightBill_ID", dmo.ID));
foreach (StatPay pay in Session.ExecuteList(queryDom))
{
if (pay.BillState != .)
{
throw new Exception("结算单:" + pay.ID + " 单据状态不是未审核");
}
stayPayBL.Delete(pay);
AppendMessage("删除结算单No." + pay.ID);
}
base.doUnCheck(dmo);
}
/// <summary>
/// 更新屠宰单明细的已计划数量,剩余数量
/// </summary>
public void UpdateDetailOtherNumber(IEnumerable<long> weightDetailIDs)
{
if (weightDetailIDs.Count() == 0)
return;
var exp = weightDetailIDs.Distinct().Select(x => DQExpression.Value(x)).ToArray();
var main = new JoinAlias(typeof(Summary));
var detail = new JoinAlias(typeof(WeightBill_Record));
var query = new DQueryDom(detail);
Summary.Register(query, exp);
query.From.AddJoin(JoinType.Left, new DQDmoSource(main), DQCondition.EQ(main, "ID", detail, "ID"));
query.Columns.Add(DQSelectColumn.Field("ID", detail));
query.Columns.Add(DQSelectColumn.Sum(main, "Number"));
query.Columns.Add(DQSelectColumn.Max(detail, "Number"));
query.GroupBy.Expressions.Add(DQExpression.Field(detail, "ID"));
var list = new List<Tuple<long, int, int>>();
using (var reader = Session.ExecuteReader(query))
{
while (reader.Read())
{
var item1 = (long)reader[0];
var item2 = Convert.ToInt32(reader[1] ?? 0);
var item3 = Convert.ToInt32(reader[2] ?? 0);
var last = item3 - item2;
if (last < 0)
throw new Exception(string.Format("过磅单明细ID{0} 头数{1} 已计划{2} 超出{3}", item1, item3, item2, -last));
list.Add(new Tuple<long, int, int>(item1, item2, last));
}
}
foreach (var item in list)
UpdateReocrdPlanNumber(item);
AfterUpdateRecord(weightDetailIDs);
}
private void UpdateReocrdPlanNumber(Tuple<long, int, int> item)
{
var update = new DQUpdateDom(typeof(WeightBill_Record));
update.Columns.Add(new DQUpdateColumn("PlanNumber", item.Item2));
update.Columns.Add(new DQUpdateColumn("RemainNumber", item.Item3));
update.Where.Conditions.Add(DQCondition.EQ("ID", item.Item1));
Session.ExecuteNonQuery(update);
}
private void AfterUpdateRecord(IEnumerable<long> detailIDs)
{
var sql = string.Format("with detail(id) as( select WeightBill_ID from B3SheepButcherManage_WeightBill_Record where id in ({0}) group by WeightBill_ID) update main set main.[RowVersion] = main.[RowVersion]+1 from B3SheepButcherManage_WeightBill main join detail on main.id=detail.id", string.Join(",", detailIDs));
Session.ExecuteSqlNonQuery(sql);
}
class Summary
{
public long ID { get; set; }
public int Number { get; set; }
public static void Register(DQueryDom root, IDQExpression[] ids)
{
var q1 = ButcherQuery(ids);
q1.UnionNext.Select = HurryButcherQuery(ids);
q1.UnionNext.Type = UnionType.All;
root.RegisterQueryTable(typeof(Summary), new string[] { "ID", "Number" }, q1);
}
static DQueryDom ButcherQuery(IDQExpression[] ids)
{
var main = new JoinAlias(typeof(Butcher));
var detail = new JoinAlias(typeof(Butcher_Detail));
var query = new DQueryDom(detail);
query.From.AddJoin(JoinType.Left, new DQDmoSource(main), DQCondition.EQ(detail, "Butcher_ID", main, "ID"));
query.Where.Conditions.Add(DQCondition.InList(DQExpression.Field(detail, "Weigh_Detail_ID"), ids));
query.Where.Conditions.Add(DQCondition.InEQ(main, "BillState", .));
query.Columns.Add(DQSelectColumn.Field("Weigh_Detail_ID", detail));
query.Columns.Add(DQSelectColumn.Count());
query.GroupBy.Expressions.Add(DQExpression.Field(detail, "Weigh_Detail_ID"));
return query;
}
static DQueryDom HurryButcherQuery(IDQExpression[] ids)
{
var detail = new JoinAlias(typeof(HurryButcher_Detail));
var main = new JoinAlias(typeof(HurryButcher));
var query = new DQueryDom(detail);
query.From.AddJoin(JoinType.Left, new DQDmoSource(main), DQCondition.EQ(detail, "HurryButcher_ID", main, "ID"));
query.Where.Conditions.Add(DQCondition.InList(DQExpression.Field(detail, "Weigh_Detail_ID"), ids));
query.Where.Conditions.Add(DQCondition.InEQ(main, "BillState", .));
query.Columns.Add(DQSelectColumn.Field("Weigh_Detail_ID", detail));
query.Columns.Add(DQSelectColumn.Sum("Number"));
query.GroupBy.Expressions.Add(DQExpression.Field(detail, "Weigh_Detail_ID"));
return query;
}
}
}
}