| @ -0,0 +1,293 @@ | |||||
| using BWP.B3Frameworks; | |||||
| using BWP.B3Frameworks.BO; | |||||
| using BWP.B3Frameworks.BO.NamedValueTemplate; | |||||
| using BWP.B3Sale.BO; | |||||
| using BWP.Web.Utils; | |||||
| using Forks.EnterpriseServices.DomainObjects2; | |||||
| using Forks.EnterpriseServices.DomainObjects2.DQuery; | |||||
| using Forks.EnterpriseServices.SqlDoms; | |||||
| using Forks.Utils; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Web.UI; | |||||
| using TSingSoft.WebControls2; | |||||
| using TSingSoft.WebPluginFramework; | |||||
| namespace BWP.Web.Pages.B3QingDaoWanFu.Reports.SaleSummaryReport_ | |||||
| { | |||||
| class SaleSummaryReport : ClickQueryReportPage | |||||
| { | |||||
| protected override string AccessRoleName | |||||
| { | |||||
| get { return "B3QingDaoWanFu.报表.销售统计表"; } | |||||
| } | |||||
| protected override string Caption | |||||
| { | |||||
| get { return "销售统计表"; } | |||||
| } | |||||
| protected override void InitDFGrid(DFGrid mDfGrid) | |||||
| { | |||||
| base.InitDFGrid(mDfGrid); | |||||
| mDfGrid.AllowRowGroup = true; | |||||
| mDfGrid.AllowColGroup = true; | |||||
| mDfGrid.AllowSorting = true; | |||||
| mDfGrid.HeaderPagerLock = true; | |||||
| } | |||||
| protected override void CreateQueryPanel(VLayoutPanel vPanel) | |||||
| { | |||||
| var list = new List<Tuple<string, string>>(); | |||||
| list.Add(new Tuple<string, string>("First", "一级部门")); | |||||
| list.Add(new Tuple<string, string>("Second", "二级部门")); | |||||
| var dic = list.Select((item) => item.Item1).EToHashSet(); | |||||
| var tablePanel = vPanel.Add(new TableLayoutPanel(1, 2), new VLayoutOption(System.Web.UI.WebControls.HorizontalAlign.Justify)); | |||||
| tablePanel.Add(0, 1, 0, 1, CreateFirstDept()); | |||||
| tablePanel.Add(0, 1, 1, 2, CreateSecondDept()); | |||||
| } | |||||
| List<Tuple<long, string>> FirstDepts | |||||
| { | |||||
| get | |||||
| { | |||||
| if (ViewState["FirstDepts"] == null) | |||||
| ViewState["FirstDepts"] = GetFirstDepts(); | |||||
| return (List<Tuple<long, string>>)ViewState["FirstDepts"]; | |||||
| } | |||||
| set { ViewState["FirstDepts"] = value; } | |||||
| } | |||||
| List<Tuple<long, string>> GetFirstDepts() | |||||
| { | |||||
| var dept = SaleDepartmentInfo; | |||||
| var query = new DQueryDom(new JoinAlias(typeof(Department))); | |||||
| query.Columns.Add(DQSelectColumn.Field("ID")); | |||||
| query.Columns.Add(DQSelectColumn.Field("Name")); | |||||
| query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Domain_ID", DomainContext.Current.ID), DQCondition.EQ("Stopped", false), DQCondition.EQ("Depth", dept.Item2 + 1))); | |||||
| query.Where.Conditions.Add(DQCondition.EQ(string.Format("TreeDeep{0}ID", dept.Item2), dept.Item1)); | |||||
| query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID")); | |||||
| return query.EExecuteList<long, string>(); | |||||
| } | |||||
| Tuple<long, int> SaleDepartmentInfo | |||||
| { | |||||
| get | |||||
| { | |||||
| if (ViewState["SaleDepartmentInfo"] == null) | |||||
| { | |||||
| var dept = WebBLUtil.GetSingleDmo<Department>("Name", "销售部", "ID", "Depth"); | |||||
| if (dept == null) | |||||
| throw new Exception("没有名称为销售部的部门"); | |||||
| ViewState["SaleDepartmentInfo"] = new Tuple<long, int>(dept.ID, dept.Depth); | |||||
| } | |||||
| return (Tuple<long, int>)ViewState["SaleDepartmentInfo"]; | |||||
| } | |||||
| } | |||||
| List<Tuple<long, string>> SecondDepts | |||||
| { | |||||
| get | |||||
| { | |||||
| if (ViewState["SecondDepts"] == null) | |||||
| ViewState["SecondDepts"] = GetSecondDepts(); | |||||
| return (List<Tuple<long, string>>)ViewState["SecondDepts"]; | |||||
| } | |||||
| set { ViewState["SecondDepts"] = value; } | |||||
| } | |||||
| List<Tuple<long, string>> GetSecondDepts() | |||||
| { | |||||
| var depth = SaleDepartmentInfo.Item2; | |||||
| var selectedItem = firstFilterTree.GetSelecteItem(); | |||||
| if (!selectedItem.Selected || string.IsNullOrEmpty(selectedItem.Value)) | |||||
| return new List<Tuple<long, string>>(); | |||||
| var query = new DQueryDom(new JoinAlias(typeof(Department))); | |||||
| query.Columns.Add(DQSelectColumn.Field("ID")); | |||||
| query.Columns.Add(DQSelectColumn.Field("Name")); | |||||
| query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Domain_ID", DomainContext.Current.ID), DQCondition.EQ("Stopped", false), DQCondition.EQ("Depth", depth + 2))); | |||||
| query.Where.Conditions.Add(DQCondition.EQ(string.Format("TreeDeep{0}ID", depth + 1), long.Parse(firstFilterTree.GetSelecteItem().Value))); | |||||
| query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID")); | |||||
| return query.EExecuteList<long, string>(); | |||||
| } | |||||
| static FilterTree firstFilterTree; | |||||
| private Control CreateFirstDept() | |||||
| { | |||||
| firstFilterTree = new FilterTree() { AllLinkOnFirst = true }; | |||||
| firstFilterTree.HorizontalRepeatColumns = 10; | |||||
| this.Load += delegate | |||||
| { | |||||
| if (!IsPostBack) | |||||
| { | |||||
| firstFilterTree.DataSource.Childs.Clear(); | |||||
| foreach (var item in FirstDepts) | |||||
| { | |||||
| var node = new FilterTreeNode(item.Item2, item.Item1.ToString()); | |||||
| firstFilterTree.DataSource.Childs.Add(node); | |||||
| } | |||||
| } | |||||
| }; | |||||
| mTreeContainer.Add("First", firstFilterTree); | |||||
| firstFilterTree.Select += (sender, e) => | |||||
| { | |||||
| SecondDepts = GetSecondDepts(); | |||||
| BuildSecond(); | |||||
| }; | |||||
| firstFilterTree.FilterAction = (query, node) => | |||||
| { | |||||
| if (!string.IsNullOrEmpty(node.Value)) | |||||
| { | |||||
| query.Where.Conditions.Add(DQCondition.EQ(JoinAlias.Create("dept"), "TreeDeep1ID", long.Parse(node.Value))); | |||||
| } | |||||
| }; | |||||
| return firstFilterTree; | |||||
| } | |||||
| void BuildSecond() | |||||
| { | |||||
| secondFilterTree.DataSource.Childs.Clear(); | |||||
| foreach (var item in SecondDepts) | |||||
| { | |||||
| var node = new FilterTreeNode(item.Item2, item.Item1.ToString()); | |||||
| secondFilterTree.DataSource.Childs.Add(node); | |||||
| } | |||||
| } | |||||
| static FilterTree secondFilterTree; | |||||
| private Control CreateSecondDept() | |||||
| { | |||||
| secondFilterTree = new FilterTree() { AllLinkOnFirst = true }; | |||||
| secondFilterTree.HorizontalRepeatColumns = 10; | |||||
| this.Load += delegate | |||||
| { | |||||
| if (!IsPostBack) | |||||
| BuildSecond(); | |||||
| }; | |||||
| mTreeContainer.Add("Second", secondFilterTree); | |||||
| secondFilterTree.FilterAction = (query, node) => | |||||
| { | |||||
| if (!string.IsNullOrEmpty(node.Value)) | |||||
| { | |||||
| query.Where.Conditions.Add(DQCondition.EQ(JoinAlias.Create("dept"), "TreeDeep2ID", long.Parse(node.Value))); | |||||
| } | |||||
| }; | |||||
| return secondFilterTree; | |||||
| } | |||||
| readonly string[] goodsBrand = new string[] { "条类", "分割品", "副产品", "冻品" }; | |||||
| protected override DQueryDom GetQueryDom() | |||||
| { | |||||
| var main = new JoinAlias(typeof(TempClass)); | |||||
| var dept = new JoinAlias("dept", typeof(Department)); | |||||
| var _3Dept = new JoinAlias("_3Dept", typeof(Department)); | |||||
| var customer = new JoinAlias(typeof(Customer)); | |||||
| var goods = new JoinAlias(typeof(SaleGoods)); | |||||
| var query = new DQueryDom(main); | |||||
| TempClass.Register(query); | |||||
| query.From.AddJoin(JoinType.Left, new DQDmoSource(dept), DQCondition.EQ(main, "Department_ID", dept, "ID")); | |||||
| query.From.AddJoin(JoinType.Left, new DQDmoSource(_3Dept), DQCondition.EQ(dept, "TreeDeep3ID", _3Dept, "ID")); | |||||
| query.From.AddJoin(JoinType.Left, new DQDmoSource(customer), DQCondition.EQ(main, "Customer_ID", customer, "ID")); | |||||
| query.From.AddJoin(JoinType.Left, new DQDmoSource(goods), DQCondition.EQ(main, "Goods_ID", goods, "ID")); | |||||
| query.Columns.Add(DQSelectColumn.Field("Name", _3Dept, "销售部门")); | |||||
| query.GroupBy.Expressions.Add(DQExpression.Field(_3Dept, "Name")); | |||||
| query.Columns.Add(DQSelectColumn.Field("CustomerCatalog_Name", customer)); | |||||
| query.GroupBy.Expressions.Add(DQExpression.Field(customer, "CustomerCatalog_Name")); | |||||
| query.GroupBy.Expressions.Add(DQExpression.Field(customer, "CustomerCatalog_ID")); | |||||
| query.OrderBy.Expressions.Add(DQOrderByExpression.Create(customer, "CustomerCatalog_ID")); | |||||
| foreach (var item in goodsBrand) | |||||
| { | |||||
| if (item == "条类") | |||||
| { | |||||
| query.Columns.Add(DQSelectColumn.Create(DQExpression.Divide(DQExpression.Sum(DQExpression.LogicCase(DQCondition.And(DQCondition.EQ(goods, "Brand", item), DQCondition.EQ("Type", 2)), DQExpression.Field("SecondNumber"), DQExpression.NULL)), DQExpression.Value(7)), item + "|上周日均头数")); | |||||
| query.Columns.Add(DQSelectColumn.Create(DQExpression.Sum(DQExpression.LogicCase(DQCondition.And(DQCondition.EQ(goods, "Brand", item), DQCondition.EQ("Type", 1)), DQExpression.Field("SecondNumber"), DQExpression.NULL)), item + "|头数")); | |||||
| } | |||||
| query.Columns.Add(DQSelectColumn.Create(DQExpression.Divide(DQExpression.Sum(DQExpression.LogicCase(DQCondition.And(DQCondition.EQ(goods, "Brand", item), DQCondition.EQ("Type", 2)), DQExpression.Field("MainNumber"), DQExpression.NULL)), DQExpression.Value(7)), item + "|上周日均重量")); | |||||
| query.Columns.Add(DQSelectColumn.Create(DQExpression.Sum(DQExpression.LogicCase(DQCondition.And(DQCondition.EQ(goods, "Brand", item), DQCondition.EQ("Type", 1)), DQExpression.Field("MainNumber"), DQExpression.NULL)), item + "|重量")); | |||||
| } | |||||
| query.Columns.Add(DQSelectColumn.Create(DQExpression.Divide(DQExpression.Sum(DQExpression.LogicCase(DQCondition.EQ("Type", 2), DQExpression.Field("MainNumber"), DQExpression.NULL)), DQExpression.Value(7)), "上周日均重量")); | |||||
| query.Columns.Add(DQSelectColumn.Create(DQExpression.Sum(DQExpression.LogicCase(DQCondition.EQ("Type", 1), DQExpression.Field("MainNumber"), DQExpression.NULL)), "当日重量合计")); | |||||
| var idx = 0; | |||||
| foreach (var col in query.Columns) | |||||
| { | |||||
| ColumnNames.Add(col.Name); | |||||
| if (idx > 1) | |||||
| { | |||||
| SumColumnNames.Add(col.Name); | |||||
| GroupSumColumnNamnes.Add(col.Name); | |||||
| } | |||||
| idx++; | |||||
| } | |||||
| return query; | |||||
| } | |||||
| class TempClass | |||||
| { | |||||
| public long? Department_ID { get; set; } | |||||
| public long? Customer_ID { get; set; } | |||||
| public long? Goods_ID { get; set; } | |||||
| public int Type { get; set; } | |||||
| public Money<decimal>? MainNumber { get; set; } | |||||
| public Money<decimal>? SecondNumber { get; set; } | |||||
| public static void Register(DQueryDom root) | |||||
| { | |||||
| var q1 = GetSaleOutStore(true); | |||||
| q1.UnionNext.Select = GetSaleOutStore(false); | |||||
| root.RegisterQueryTable(typeof(TempClass), new string[] { "Department_ID", "Customer_ID", "Goods_ID", "Type", "MainNumber", "SecondNumber" }, q1); | |||||
| } | |||||
| static DQueryDom GetSaleOutStore(bool today) | |||||
| { | |||||
| var main = new JoinAlias(today + "M", typeof(SaleOutStore)); | |||||
| var detail = new JoinAlias(today + "D", typeof(SaleOutStore_Detail)); | |||||
| //var dept = new JoinAlias("dept", typeof(Department)); | |||||
| var query = new DQueryDom(main); | |||||
| query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "SaleOutStore_ID")); | |||||
| // query.From.AddJoin(JoinType.Left, new DQDmoSource(dept), DQCondition.EQ(main, "Department_ID", dept, "ID")); | |||||
| query.Columns.Add(DQSelectColumn.Field("Department_ID", main)); | |||||
| query.Columns.Add(DQSelectColumn.Field("Customer_ID", main)); | |||||
| query.Columns.Add(DQSelectColumn.Field("SaleGoods_ID", detail)); | |||||
| query.Columns.Add(DQSelectColumn.Create(DQExpression.Value(today ? 1 : 2), "Type")); | |||||
| query.Columns.Add(DQSelectColumn.Field("UnitNum", detail)); | |||||
| query.Columns.Add(DQSelectColumn.Field("SecondNumber", detail)); | |||||
| var timeRange = GetDateRange(today); | |||||
| query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Domain_ID", DomainContext.Current.ID), DQCondition.GreaterThanOrEqual("BillState", 单据状态.已审核), DQCondition.Between("LoadTime", timeRange.Item1, timeRange.Item2))); | |||||
| return query; | |||||
| } | |||||
| static Tuple<DateTime, DateTime> GetDateRange(bool today) | |||||
| { | |||||
| if (today) | |||||
| return new Tuple<DateTime, DateTime>(DateTime.Today, DateTime.Today + new TimeSpan(23, 59, 59)); | |||||
| var d = (int)DateTime.Today.DayOfWeek; | |||||
| if (DateTime.Today.DayOfWeek == DayOfWeek.Sunday) | |||||
| d = 7; | |||||
| var date = DateTime.Today.AddDays(-d); | |||||
| return new Tuple<DateTime, DateTime>(date.AddDays(-6), date); | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||