using DevComponents.DotNetBar; using DevComponents.DotNetBar.SuperGrid; using POS.Language.Language; using POSV.Component; using POSV.Entity; using POSV.Helper; using POSV.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace POSV.Report { public partial class ShiftDetail : BusinessForm { HandOverTicket handOverTicket = new HandOverTicket(); public ShiftDetail() { InitializeComponent(); this.controlBox1.Text = LangProxy.ToLang("交班记录查询"); this.controlBox1.ShowApplicationVersion = false; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; DefaultGridStyle.setDefaultGridStyle(this.ticketListTable); DefaultGridStyle.setDefaultGridStyle(this.detailListTable); DefaultGridStyle.setDefaultGridStyle(this.detailPartListTable); this.startPicker.Value = DateTime.Parse(Global.Instance.BusinessPlan.StartTimeSimple); this.endPicker.Value = DateTime.Parse(Global.Instance.BusinessPlan.EndTimeSimple); //第一步获取订单列表 List detail = getHandOverList(this.startPicker.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.endPicker.Value.ToString("yyyy-MM-dd HH:mm:ss")); ticketListTable.PrimaryGrid.DataSource = detail; } public List getHandOverList(string startTime, string endTime) { List list = null; try { using (var db = Global.Instance.OpenDataBase) { StringBuilder sqlBuld = new StringBuilder(); sqlBuld.Append(" select * "); sqlBuld.Append(" from pos_store_handover "); sqlBuld.Append(" where datetimeShift >= '{0}' and datetimeShift <= '{1}'"); sqlBuld.Append(" order by datetimeShift desc"); string sql = string.Format(sqlBuld.ToString(), startTime, endTime); list = db.Query(sql).ToList(); } } catch (Exception ex) { LOGGER.Error(ex); } if (list == null) { list = new List(); } return list; } public HandOverTicket getHandOverByTicketId(string ticketId) { try { using (var db = Global.Instance.OpenDataBase) { StringBuilder sqlBuld = new StringBuilder(); sqlBuld.Append(" select * "); sqlBuld.Append(" from pos_store_handover "); sqlBuld.Append(" where id = '{0}' "); string sql = string.Format(sqlBuld.ToString(), ticketId); handOverTicket = db.FirstOrDefault(sql); } } catch (Exception ex) { LOGGER.Error(ex); } return handOverTicket; } public List getHandOverDetailByTicketId(string ticketId) { List list = null; try { using (var db = Global.Instance.OpenDataBase) { StringBuilder sqlBuld = new StringBuilder(); sqlBuld.Append(" select * ,CASE busMode WHEN 7 THEN '微信点餐' ELSE 'POS' END as paySource "); sqlBuld.Append(" from pos_store_handover_detail "); sqlBuld.Append(" where ticketId = '{0}' "); string sql = string.Format(sqlBuld.ToString(), ticketId); list = db.Query(sql).ToList(); } } catch (Exception ex) { LOGGER.Error(ex); } if (list == null) { list = new List(); } return list; } public List getHandOverPartDetailByTicketId(string ticketId) { List list = null; try { using (var db = Global.Instance.OpenDataBase) { StringBuilder sqlBuld = new StringBuilder(); sqlBuld.Append(" select * "); sqlBuld.Append(" from pos_store_handover_detail_part "); sqlBuld.Append(" where ticketId = '{0}' "); string sql = string.Format(sqlBuld.ToString(), ticketId); list = db.Query(sql).ToList(); } } catch (Exception ex) { LOGGER.Error(ex); } if (list == null) { list = new List(); } return list; } /// /// 班次销售分析 /// /// public List loadHandOverProduct(string workerNo, string shiftNo, string storeId) { List handOverProductList = null; List list = null; try { using (var db = Global.Instance.OpenDataBase) { StringBuilder sqlBuld = new StringBuilder(); sqlBuld.Append(" SELECT a.typeId AS typeId, "); sqlBuld.Append(" ifnull(a.typeName,'') AS typeName, ifnull(a.productName,'') AS productName,a.specName as specName, "); sqlBuld.Append(" a.productNo AS productNo,round(SUM(a.quantity - a.rquantity),2) AS count,"); sqlBuld.Append(" round(sum(case when a.isSuit = 3 then a.quantity else 0 end), 2) as suitQuantity,"); sqlBuld.Append(" round(sum(case when a.isSuit != 3 then a.totalReceivableAmount else 0 end),2) as amount "); sqlBuld.Append(" FROM pos_order_item a "); sqlBuld.Append(" left join pos_order b on a.orderId = b.id "); sqlBuld.Append(" WHERE b.workerNo = '{0}' "); sqlBuld.Append(" and b.shiftNo = '{1}' "); sqlBuld.Append(" and b.storeId = '{2}' "); sqlBuld.Append(" and b.orderStatus in (0, 2, 4) "); sqlBuld.Append(" GROUP BY a.productId , a.specId"); sqlBuld.Append(" ORDER BY a.typeId,a.productNo "); string sql = string.Format(sqlBuld.ToString(), workerNo, shiftNo, storeId); handOverProductList = db.Query(sql).ToList(); string seriesId = ""; string seriesName = ""; Dictionary seriesMap = new Dictionary(); Dictionary typeMap = new Dictionary(); decimal count = 0.00M; decimal suitQuantity = 0.00M; decimal amount = 0.00M; decimal sumCount = 0.00M; decimal sumSuitQuantity = 0.00M; decimal sumAmount = 0.00M; int i = 0; List> result = new List>(); int listLength = handOverProductList.Count; foreach (HandOverProduct product in handOverProductList) { string typeId = product.TypeId; if (!seriesId.Equals(typeId)) { // 分类改变的时候把分类加入列表 if (i != 0) { seriesMap.Add("type", 1); seriesMap.Add("name", seriesName); seriesMap.Add("count", count); seriesMap.Add("suitQuantity", suitQuantity); seriesMap.Add("amount", amount); result.Add(seriesMap); //分类改变时,清除缓存 typeMap = new Dictionary(); } seriesMap = new Dictionary(); count = 0.00M; suitQuantity = 0.00M; amount = 0.00M; seriesId = typeId; seriesName = product.TypeName; } count += product.Count; suitQuantity += product.SuitQuantity; amount += product.Amount; sumCount += product.Count; sumSuitQuantity += product.SuitQuantity; sumAmount += product.Amount; //开始先加入分类名称 if (!typeMap.ContainsKey("name")) { typeMap.Add("type", 3); typeMap.Add("name", product.TypeName); typeMap.Add("count", ""); typeMap.Add("suitQuantity", ""); typeMap.Add("amount", ""); result.Add(typeMap); } // 菜品加入列表 Dictionary dishMap = new Dictionary(); dishMap.Add("type", 0); if (product.SpecName != null && !"".Equals(product.SpecName)) { dishMap.Add("name", product.ProductName + "(" + product.SpecName + ")"); } else { dishMap.Add("name", product.ProductName); } dishMap.Add("count", product.Count); dishMap.Add("suitQuantity", product.SuitQuantity); dishMap.Add("amount", product.Amount); result.Add(dishMap); i = i + 1; if (i == listLength) { seriesMap.Add("type", 1); seriesMap.Add("name", seriesName); seriesMap.Add("count", count); seriesMap.Add("suitQuantity", suitQuantity); seriesMap.Add("amount", amount); result.Add(seriesMap); //分类改变时,清除缓存 typeMap = new Dictionary(); } } Dictionary sumMap = new Dictionary(); sumMap.Add("name", LangProxy.ToLang("销售")); sumMap.Add("type", 2); sumMap.Add("count", sumCount); sumMap.Add("suitQuantity", sumSuitQuantity); sumMap.Add("amount", sumAmount); result.Add(sumMap); list = new List(); foreach (Dictionary map in result) { HandOverProduct entity = new HandOverProduct(); entity.Type = StringUtils.GetInt(map["type"]); entity.Name = StringUtils.GetString(map["name"]); entity.Count = StringUtils.GetDecimal(map["count"]); entity.SuitQuantity = StringUtils.GetDecimal(map["suitQuantity"]); entity.Amount = StringUtils.GetDecimal(map["amount"]); list.Add(entity); } } } catch (Exception ex) { LOGGER.Error(ex); } return list; } public List loadHandOverInfo(string workerNo, string shiftNo, string storeId) { List list = null; try { using (var db = Global.Instance.OpenDataBase) { StringBuilder sqlBuld = new StringBuilder(); sqlBuld.Append(" select a.promotionType,count(a.id) as count,round(sum(a.discountAmount),2) as amount from pos_order_item_promotion a "); sqlBuld.Append(" left join pos_order b on a.tradeNo = b.tradeNo "); sqlBuld.Append(" where a.promotionType<> '50' "); sqlBuld.Append(" and b.workerNo = '{0}' "); sqlBuld.Append(" and b.shiftNo = '{1}' "); sqlBuld.Append(" and b.storeId = '{2}' "); sqlBuld.Append(" and b.orderStatus in (0, 2, 4) "); sqlBuld.Append(" GROUP BY a.promotionType "); string sql = string.Format(sqlBuld.ToString(), workerNo, shiftNo, storeId); list = db.Query(sql).ToList(); } } catch (Exception ex) { LOGGER.Error(ex); } return list; } public List loadHandOverSaleMode(string workerNo, string shiftNo, string storeId) { List list = null; try { using (var db = Global.Instance.OpenDataBase) { StringBuilder sqlBuld = new StringBuilder(); sqlBuld.Append(" select orderType ,count(id) as count,round(sum(receivableAmount),2) as amount from pos_order "); sqlBuld.Append(" where workerNo = '{0}' "); sqlBuld.Append(" and shiftNo = '{1}' "); sqlBuld.Append(" and storeId = '{2}' "); sqlBuld.Append(" and orderStatus in (0, 2, 4) "); sqlBuld.Append(" GROUP BY orderType "); string sql = string.Format(sqlBuld.ToString(), workerNo, shiftNo, storeId); list = db.Query(sql).ToList(); } } catch (Exception ex) { LOGGER.Error(ex); } return list; } //汇总销售分类 public List loadHandOverProductType(string workerNo, string shiftNo, string storeId) { List list = null; try { using (var db = Global.Instance.OpenDataBase) { StringBuilder sqlBuld = new StringBuilder(); sqlBuld.Append(" select a.typeId AS typeId,a.typeName AS typeName,round(SUM(a.quantity - a.rquantity),2) AS count, "); sqlBuld.Append(" round(SUM(a.totalReceivableAmount),2) AS amount from pos_order_item a "); sqlBuld.Append(" left join pos_order b on a.tradeNo = b.tradeNo "); sqlBuld.Append(" where b.workerNo = '{0}' "); sqlBuld.Append(" and b.shiftNo = '{1}' "); sqlBuld.Append(" and b.storeId = '{2}' "); sqlBuld.Append(" and a.isSuit in (1, 2) "); sqlBuld.Append(" and b.orderStatus in (0, 2, 4) "); sqlBuld.Append(" GROUP BY a.typeId "); sqlBuld.Append(" ORDER BY a.typeId"); string sql = string.Format(sqlBuld.ToString(), workerNo, shiftNo, storeId); list = db.Query(sql).ToList(); } } catch (Exception ex) { LOGGER.Error(ex); } return list; } private void btn_time_Click(object sender, EventArgs e) { var obj = sender as ButtonX; var typeStr = obj.Tag as string; var type = (ReportQuickDate)Enum.Parse(typeof(ReportQuickDate), typeStr); var res = DateTimeUtils.CalculateBusinessPlanDate(type, this.startPicker.Value, this.endPicker.Value); this.startPicker.Value = res.Item1; this.endPicker.Value = res.Item2; List detail = getHandOverList(this.startPicker.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.endPicker.Value.ToString("yyyy-MM-dd HH:mm:ss")); ticketListTable.PrimaryGrid.DataSource = detail; } private void btn_query_Click(object sender, EventArgs e) { List detail = getHandOverList(this.startPicker.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.endPicker.Value.ToString("yyyy-MM-dd HH:mm:ss")); ticketListTable.PrimaryGrid.DataSource = detail; } private void btn_exit_Click(object sender, EventArgs e) { OnCloseTouchClick(sender, e); } private void OnControlBoxCloseClick(object sender, EventArgs e) { OnCloseTouchClick(sender, e); } private void OnCloseTouchClick(object sender, EventArgs e) { //先关闭父窗体 if (this.Owner != null) { this.Owner.Close(); } //再关闭当前窗体 this.Close(); } private void OnTicketRowActivated(object sender, DevComponents.DotNetBar.SuperGrid.GridRowActivatedEventArgs e) { GridPanel grid = e.GridPanel; if (e.NewActiveRow != null) { detailListTable.PrimaryGrid.Rows.Clear(); detailPartListTable.PrimaryGrid.Rows.Clear(); //先清除其他表数据 GridRow row = e.NewActiveRow as GridRow; grid.SetSelected(e.NewActiveRow, false); grid.SetActiveRow(e.NewActiveRow); var ticketId = row.Cells["Id"].Value.ToString(); List detail = getHandOverDetailByTicketId(ticketId); detailListTable.PrimaryGrid.DataSource = detail; List part = getHandOverPartDetailByTicketId(ticketId); detailPartListTable.PrimaryGrid.DataSource = part; handOverTicket = getHandOverByTicketId(ticketId); handOverTicket.Detail = detail; handOverTicket.Part = part; } else { detailListTable.PrimaryGrid.Rows.Clear(); detailPartListTable.PrimaryGrid.Rows.Clear(); } } private void btn_print_Click(object sender, EventArgs e) { //交班打印 this.ShowToastNotify(this, LangProxy.ToLang("开始打印")); //构建收银小票模版参数 handOverTicket.PrintType = ""; if (handOverTicket.Detail != null && handOverTicket.Detail.Count > 0) { HandOverDetail handOverDetail = handOverTicket.Detail.Find(x => "01".Equals(x.PayModeNo)); if (handOverDetail != null) { handOverTicket.handMoney = handOverDetail.HandsMoney; decimal difMoney = handOverDetail.HandsMoney - (handOverDetail.SumMoney + handOverTicket.Inmoney - handOverTicket.Outmoney); handOverTicket.diffMoney = difMoney; } } bool ShiftPrint = Global.Instance.GlobalConfigBoolValue(ConfigConstant.CONFIG_CASHIER_SHIFPRINT, false); if (ShiftPrint) { //汇总菜品类别销售 List ProductList = loadHandOverProduct(handOverTicket.WorkNo, handOverTicket.ShiftNo, handOverTicket.StoreId); handOverTicket.Product = ProductList; } //汇总优惠明细 handOverTicket.Info = loadHandOverInfo(handOverTicket.WorkNo, handOverTicket.ShiftNo, handOverTicket.StoreId); //汇总班次营业模式数据 handOverTicket.Mode = loadHandOverSaleMode(handOverTicket.WorkNo, handOverTicket.ShiftNo, handOverTicket.StoreId); //汇总分类明细 handOverTicket.ProductType = loadHandOverProductType(handOverTicket.WorkNo, handOverTicket.ShiftNo, handOverTicket.StoreId); var vars = ShiftHelper.BuilderTicketVariable(handOverTicket, ShiftPrint, true); Tuple result = ShiftHelper.PrinterTicket("交班汇总", vars, true, false); this.ShowToastNotify(this, string.Format("{0}", result.Item2)); } private void OnControlBoxMinClick(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } } }