using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar.Controls; using POSV.ShoppingCart; using POSV.Utils; using POSV.Component; using POSV.Entity; using POS.Language.Language; namespace POSV.Business { [ToolboxItem(false)] public partial class DiscountPanelEx : AbstractFlyoutPanelEx { /// /// 操作员默认的最大折扣率 /// private decimal MaxDiscountRate = Global.Instance.Worker.MaxDiscountRate; /// /// 新的折扣率 /// private decimal NewDiscountRate = 0; /// /// 当前选择的记录,单品折扣依赖 /// private OrderItem _orderItem = null; /// /// 整单的记录,,整单折扣依赖 /// private OrderObject _orderObject = null; /// /// 按键操作 /// private ModuleKeyCode _keyCode = ModuleKeyCode.None; /// /// 权限控制码 /// private string _permissionCode = string.Empty; /// /// 授权人权限信息 /// private Tuple , Worker> _authz = null; /// /// 授权界面或者录入界面切换 /// private AuthOperator _auth = AuthOperator.None; private DiscountPanelEx() { InitializeComponent(); this.tabControl.TabsVisible = false; this.Height = this.Height - 30; } public DiscountPanelEx(ModuleKeyCode keyCode,OrderItem orderItem , OrderObject orderObject , string permissionCode , AuthOperator auth = AuthOperator.输入操作 , Tuple , Worker> authz = null) : this() { this._orderItem = orderItem; this._orderObject = orderObject; this._keyCode = keyCode; //操作功能的权限码 this._permissionCode = permissionCode; //授权人综合信息 this._authz = authz; //如果授权人存在,说明是外部传入,当前操作按照授权人的最高折扣率 if (this._authz != null) { this.MaxDiscountRate = this._authz.Item1; } //外部传入的操作 this._auth = auth; //定义授权界面的操作事件 this.authPanel.ButtonClick += OnAuthzButtonClick; //默认校验权限控制码 this.authPanel.ExpectValidate(this._permissionCode); } /// /// 授权事件 /// /// /// private void OnAuthzButtonClick(object sender , AuthEventArgs e) { switch (e.Action) { case AuthAction.Close: { this.OnCancelButtonClick(new FlyoutEventArgs(FlyoutAction.Cancel , e.KeyCode , null)); } break; case AuthAction.Cancel: { this.tabControl.SelectedTab = null; this.tabControl.SelectedTab = this.tabInput; } break; case AuthAction.Accept: { if (e.Data is Tuple , Worker>) { this._authz = e.Data as Tuple , Worker>; var logInfo = string.Format(LangProxy.ToLang("操作由[{0},{1}]授权") , this._authz.Item4.No , this._authz.Item4.Name); LogUtils.WriteLog(LogAction.授权操作 , logInfo , this._authz.Item4.No , this._orderObject.TradeNo); //修改为授权人的最大折扣率 this.MaxDiscountRate = this._authz.Item1; this.tabControl.SelectedTab = null; this.tabControl.SelectedTab = this.tabInput; this.ShowMessage(this.lblInfo , logInfo); } } break; } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.SetStyle(ControlStyles.Selectable , true); switch (this._keyCode) { case ModuleKeyCode.整单折扣: { //移除 整单折扣和单品折扣 this.tableLayoutPanel.Controls.Remove(this.touchButtonX13); this.tableLayoutPanel.Controls.Remove(this.touchButtonX5); //在[3,2]位置添加整单折扣 this.tableLayoutPanel.Controls.Add(this.touchButtonX13 , 3 , 2); this.tableLayoutPanel.SetRowSpan(this.touchButtonX13 , 2); this.touchButtonX13.Text = LangProxy.ToLang("确定"); } break; case ModuleKeyCode.单品折扣: { this.tableLayoutPanel.Controls.Remove(this.touchButtonX13); this.tableLayoutPanel.SetRowSpan(this.touchButtonX5 , 2); this.touchButtonX5.Text = LangProxy.ToLang("确定"); } break; } this.txtDiscount.Multiline = false; this.txtDiscount.Focus(); this.txtDiscount.SelectAll(); this.ActiveControl = this.txtDiscount; this.tabControl.SelectedTab = null; switch (this._auth) { case AuthOperator.输入操作: { this.tabControl.SelectedTab = this.tabInput; } break; case AuthOperator.授权操作: { this.tabControl.SelectedTab = this.tabAuthc; } break; } } public void ShowMessage(string message , bool error) { this.Invoke(new Action(() => { this.ShowMessage(this.lblInfo , message,error); })); } private void OnTouchClick(object sender , Component.TouchEventArgs e) { switch (e.Value) { case "clear": { this.txtDiscount.Text = string.Empty; this.txtDiscount.Focus(); } break; case "close": { this.OnCancelButtonClick(new FlyoutEventArgs(FlyoutAction.Cancel, e.Value, null)); } break; case "item": { if (this._orderItem == null) { this.ShowMessage(this.lblInfo, LangProxy.ToLang("单品折扣当前不可用"), true); } else { //最低售价销售或者权限不足时提示,收银员确认是否继续 bool isGo = true; if (string.IsNullOrEmpty(this.txtDiscount.Text.Trim())) { this.ShowMessage(this.lblInfo , LangProxy.ToLang("请输入折扣值....") , true); isGo = false; } //当前售价的最高优惠 decimal maxDiscountAmount = this._orderItem.Quantity * this._orderItem.Price * (1 - this.MaxDiscountRate); //优惠金额 var discountAmount = this._orderItem.Quantity * this._orderItem.Price * (1 - this.NewDiscountRate); if (isGo && discountAmount > maxDiscountAmount) { isGo = false; string info = string.Format(LangProxy.ToLang("权限不足,单价不低于{0}元") , OrderUtils.ToRound(this._orderItem.Price * this.MaxDiscountRate)); if (this._authz == null) { this.ShowMessage(this.lblInfo , info , true); this.authPanel.SetTitle(LangProxy.ToLang("单品折扣")); //期望的折扣率 decimal expectDiscountRate = this.NewDiscountRate; this.authPanel.ExpectValidate(this._permissionCode , expectDiscountRate); //等待授权操作 this.tabControl.SelectedTab = null; this.tabControl.SelectedTab = this.tabAuthc; } else { info = string.Format(LangProxy.ToLang("授权人权限不足,单价不低于{0}元") , OrderUtils.ToRound(this._orderItem.Price * this.MaxDiscountRate)); this.ShowMessage(this.lblInfo , info , true); this.txtDiscount.SelectAll(); this.txtDiscount.Focus(); } } //是否低于最低售价的验证 //最低售价下的金额 decimal minAmount = this._orderItem.Quantity * this._orderItem.MinPrice; if((this._orderItem.Amount - discountAmount) < minAmount) { //是否允许低于最低售价销售 bool allowMinPriceSale = Global.Instance.GlobalConfigBoolValue(ConfigConstant.CASHIER_ALLOW_MIN_PRICE_SALE , false); //允许低于最低售价 if (isGo && allowMinPriceSale) { //低于最低售价销售时提示 bool allowMinPriceSaleNofity = Global.Instance.GlobalConfigBoolValue(ConfigConstant.CASHIER_MIN_PRICE_SALE_NOTIFY , false); //低于最低售价销售时提示 if (isGo && allowMinPriceSaleNofity) { var dialog = new DialogForm(LangProxy.ToLang("销售提示信息") , LangProxy.ToLang("您确定要低于最低售价销售?" ), MessageBoxIcon.Warning , MessageBoxButtons.OKCancel); dialog.StartPosition = FormStartPosition.CenterParent; dialog.Size = new Size(320, 150); if (DialogResult.Cancel == dialog.ShowDialog(this)) { isGo = false; } } } else { this.ShowMessage(this.lblInfo , LangProxy.ToLang("不允许低于最低售价销售" ), true); isGo = false; } } if (isGo) { //单品议价 var promotion = new PromotionItem(); //标识 promotion.Id = IdWorkerUtils.Instance.NextId(); //租户ID promotion.TenantId = this._orderItem.TenantId; //订单ID promotion.OrderId = this._orderItem.OrderId; //订单编号 promotion.TradeNo = this._orderItem.TradeNo; //单品编号 promotion.ItemId = this._orderItem.Id; //类型 promotion.PromotionType = PromotionType.折扣; //折扣率 promotion.DiscountRate = this.NewDiscountRate; //是否启用 promotion.Enabled = false; this.OnAcceptButtonClick(new FlyoutEventArgs(FlyoutAction.Accept , e.Value , promotion)); } } } break; case "order": { if (this._orderObject == null) { this.ShowMessage(this.lblInfo , LangProxy.ToLang("整单折扣当前不可用") , true); } else { //*******整单折扣不控制最低售价 //最低售价销售时提示,收银员确认是否继续 bool isGo = true; if (string.IsNullOrEmpty(this.txtDiscount.Text.Trim())) { this.ShowMessage(this.lblInfo , LangProxy.ToLang("请输入折扣值....") , true); isGo = false; } //当前售价的最高优惠 decimal maxDiscountAmount = this._orderObject.Amount * (1 - this.MaxDiscountRate); //折扣后的优惠金额 var discountAmount = this._orderObject.Amount * (1 - this.NewDiscountRate); if (isGo && discountAmount > maxDiscountAmount) { isGo = false; string info = string.Format(LangProxy.ToLang("权限不足,总价不低于{0}元") , OrderUtils.ToRound(this._orderObject.Amount * this.MaxDiscountRate)); if (this._authz == null) { this.ShowMessage(this.lblInfo , info , true); this.authPanel.SetTitle(LangProxy.ToLang("整单折扣")); //期望的折扣率 decimal expectDiscountRate = this.NewDiscountRate; this.authPanel.ExpectValidate(this._permissionCode , expectDiscountRate); //等待授权操作 this.tabControl.SelectedTab = null; this.tabControl.SelectedTab = this.tabAuthc; } else { info = string.Format(LangProxy.ToLang("授权人权限不足,总价不低于{0}元") , OrderUtils.ToRound(this._orderObject.Amount * this.MaxDiscountRate)); this.ShowMessage(this.lblInfo , info , true); this.txtDiscount.SelectAll(); this.txtDiscount.Focus(); } } if (isGo) { //整单议价 var promotion = new PromotionOrder(); //标识 promotion.Id = IdWorkerUtils.Instance.NextId(); //租户ID promotion.TenantId = this._orderObject.TenantId; //订单ID promotion.OrderId = this._orderObject.Id; //订单编号 promotion.TradeNo = this._orderObject.TradeNo; //单品编号 promotion.ItemId = string.Empty; //类型 promotion.PromotionType = PromotionType.整单折扣; //折扣率 promotion.DiscountRate = this.NewDiscountRate; //优惠前金额 promotion.Amount = this._orderObject.Amount; //优惠金额 promotion.DiscountAmount = OrderUtils.ToRound(this._orderObject.Amount * (1 - this.NewDiscountRate)); //整单议价后的金额 promotion.ReceivableAmount = this._orderObject.Amount - promotion.DiscountAmount; //是否启用 promotion.Enabled = false; this.OnAcceptButtonClick(new FlyoutEventArgs(FlyoutAction.Accept , e.Value , promotion)); } } } break; default: InputSimulatorUtils.SendKey(KeyCodes.Map[e.Value]); break; } } private void OnDiscountEnterClick(object sender , Component.EnterEventArg e) { this.OnTouchClick(this.touchButtonX4 , new TouchEventArgs(this.touchButtonX4.KeyCode)); } private void OnValueChanged(object sender , EnterEventArg e) { if (!string.IsNullOrEmpty(e.Value) && this.txtDiscount.DecimalValue >= this.MaxDiscountRate * 10) { decimal v = (Convert.ToDecimal(e.Value) / Convert.ToDecimal(100.0)); decimal.TryParse(v.ToString("0.##") , out v); this.NewDiscountRate = v; } else { this.NewDiscountRate = 0; } } private void OnTabChanged(object sender , DevComponents.DotNetBar.SuperTabStripSelectedTabChangedEventArgs e) { var tab = this.tabControl.SelectedTab; var opt = AuthOperator.None; Enum.TryParse(tab.Tag.ToString() , out opt); switch (opt) { case AuthOperator.授权操作: { this.ActiveControl = this.authPanel; //设置焦点 this.authPanel.SetFocus(); } break; case AuthOperator.输入操作: { this.ActiveControl = this.txtDiscount; this.txtDiscount.Multiline = false; this.txtDiscount.Focus(); this.txtDiscount.SelectAll(); } break; } } private void OnTabChanging(object sender , DevComponents.DotNetBar.SuperTabStripSelectedTabChangingEventArgs e) { //是否开启授权 e.Cancel = !Global.Instance.GlobalConfigBoolValue(ConfigConstant.CASHIER_AUTO_LOCK_DISABLE , false); } } }