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 POSV.MessageEvent; using POSV.Utils; using POSV.Component; using POSV.ShoppingCart; using POSV.Entity; namespace POSV.Bill { [ToolboxItem(true)] public partial class CashControl : AbstractBill { /// /// 现金结算 编码 /// private const string PayModeNo = "01"; public CashControl() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); SetStyle(ControlStyles.Selectable , true); this.txtCash.Multiline = false; this.txtCash.Focus(); this.txtCash.Select(); } protected override void OnPaintBackground(PaintEventArgs e) { e.Graphics.Clear(Focused ? SystemColors.Control : SystemColors.Control); } protected override void OnGotFocus(EventArgs e) { Invalidate(); base.OnGotFocus(e); } protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); Invalidate(); } public override void SetFocus() { this.txtCash.Focus(); this.txtCash.SelectAll(); } /// /// 订单对象 /// private OrderObject _orderObject = null; public override void BindUi(OrderObject orderObject) { if (orderObject == null) return; this._orderObject = orderObject; //计算抹零金额, 除了微信、支付宝、银联钱包、会员卡外,其他支付方式存在抹零 var malingAmount = OrderUtils.MalingAmount(this._orderObject.ReceivableAmount , null); //抹零数据赋值给订单 this._orderObject.MalingAmount = malingAmount; //未收金额 var unpaidAmount = this._orderObject.PaidAmount - this._orderObject.ReceivedAmount; //将当前应收金额赋值 this.txtCash.Text = unpaidAmount >= 0 ? unpaidAmount.ToString() : "0"; this.txtCash.SelectAll(); //刷新支付汇总清单 this.billSummary1.Refresh(orderObject); //刷新支付列表 this.billGrid1.RefreshGrid(this._orderObject.Pays); } public override bool Focused { get { if (this.ActiveControl == null) { this.ActiveControl = this.txtCash; } return this.ActiveControl.Focused; } } private decimal CalculateChangeAmount(decimal inputMoney) { //待收金额 = 应收金额 - 已收金额 decimal receivableAmount = this._orderObject.PaidAmount - this._orderObject.Pays.FindAll(x => !x.No.Equals(PayModeNo)).Sum(x => x.PaidAmount); //重新计算找零金额 return inputMoney < receivableAmount ? 0 : (inputMoney - receivableAmount); } public override void SimulateKeyboard(BillKeyboardEventArgs e) { base.SimulateKeyboard(e); this.OnKeyboardBefore(this.billKeyboard1,e); if (this.PaymentVerify) { this.OnKeyboardAfter(this.billKeyboard1,e); } } /// /// 操作是否验证通过 /// private bool PaymentVerify = false; private void OnKeyboardBefore(object sender , BillKeyboardEventArgs e) { this.PaymentVerify = false; if (this._orderObject == null) { MsgEvent.Send(Constant.PAY_MESSAGE_EVENT_NOTIFY , new Tuple(true,"订单数据错误啦,请拍个照...")); return; } //操作员输入的实收金额 decimal inputMoney = this.txtCash.DecimalValue; if(inputMoney < 0) { MsgEvent.Send(Constant.PAY_MESSAGE_EVENT_NOTIFY , new Tuple(false , "请输入金额...")); this.txtCash.Focus(); this.txtCash.SelectAll(); return; } //找零金额 //decimal changeAmount = CalculateChangeAmount(inputMoney); ////前台收银找零不允许超过100元 //bool notAllowMoreOneHundred = Global.Instance.GlobalConfigBoolValue(ConfigConstant.CASHIER_NOT_ALLOW_MORE_ONE_HUNDRED); //if (notAllowMoreOneHundred && changeAmount > 100) //{ // MsgEvent.Send(Constant.PAY_MESSAGE_EVENT_NOTIFY , new Tuple(true , "找零不允许超过100元...")); // return; //} ////找零 //this._orderObject.ChangeAmount = changeAmount; this.PaymentVerify = true; } private void AddCashPayType() { //实收金额,操作员录入的金额 decimal inputMoney = this.txtCash.DecimalValue; if (inputMoney <= 0) { MsgEvent.Send(Constant.PAY_MESSAGE_EVENT_NOTIFY , new Tuple(false , "请输入金额...")); this.txtCash.Focus(); this.txtCash.SelectAll(); return; } //是否有需要删除的现金支付项 PayItem oldItem = null; //重复现金支付方式合并处理 if (this._orderObject.Pays.Exists(x=>x.No == PayModeNo)) { oldItem = this._orderObject.Pays.Find(x=>x.No == PayModeNo); inputMoney += oldItem.PaidAmount; } //重新计算找零金额 decimal changeAmount = this.CalculateChangeAmount(inputMoney); //前台收银找零不允许超过100元 bool notAllowMoreOneHundred = Global.Instance.GlobalConfigBoolValue(ConfigConstant.CASHIER_NOT_ALLOW_MORE_ONE_HUNDRED); if (notAllowMoreOneHundred && changeAmount > 100) { MsgEvent.Send(Constant.PAY_MESSAGE_EVENT_NOTIFY , new Tuple(true , "00找零不允许超过100元...")); this.txtCash.Focus(); this.txtCash.SelectAll(); return; } //找零 this._orderObject.ChangeAmount = changeAmount; var payMode = this.Tag as PayMode; //构建现金支付方式 PayItem item = OrderUtils.ToPayItem(payMode); //租户ID item.TenantId = Global.Instance.Authc.TenantId; //订单号 item.OrderId = this._orderObject.Id; item.TradeNo = this._orderObject.TradeNo; //实收金额,输入的金额 item.PaidAmount = inputMoney; //找零金额 item.ChangeAmount = changeAmount; //已收金额,真正意义上实际收款金额 item.Amount = this._orderObject.PaidAmount; item.CardNo = ""; item.Memo = "现金结账"; item.Status = (int)OrderPaymentStatus.已付款; item.StatusDesc = "现金"; item.PayTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //删除已经存在的现金支付方式 if (oldItem != null) { this._orderObject.Pays.Remove(oldItem); } //将新的现金支付压入支付清单 this._orderObject.Pays.Add(item); //刷新界面数据 this.billSummary1.Refresh(this._orderObject); this.billGrid1.RefreshGrid(this._orderObject.Pays); //未收金额 var unpaidAmount = this._orderObject.PaidAmount - this._orderObject.ReceivedAmount; this.txtCash.Text = unpaidAmount < 0 ? "0" : unpaidAmount.ToString(); this.txtCash.Focus(); this.txtCash.SelectAll(); } private void OnKeyboardAfter(object sender , BillKeyboardEventArgs e) { switch (e.KeyCode) { case "clear": //如果当前焦点控件是输入框 if (this.ActiveControl is TextBox) { var activeControl = this.ActiveControl as TextBox; activeControl.Text = string.Empty; } break; case "accept": //验证通过 if (this.PaymentVerify) { AddCashPayType(); } break; case "100": { //如果当前焦点控件是输入框 if (this.ActiveControl is TextBox) { var activeControl = this.ActiveControl as TextBox; activeControl.Text = string.Empty; } InputSimulatorUtils.SendKey(KeyCodes.Map["1"]); InputSimulatorUtils.SendKey(KeyCodes.Map["0"]); InputSimulatorUtils.SendKey(KeyCodes.Map["0"]); } break; case "50": { //如果当前焦点控件是输入框 if (this.ActiveControl is TextBox) { var activeControl = this.ActiveControl as TextBox; activeControl.Text = string.Empty; } InputSimulatorUtils.SendKey(KeyCodes.Map["5"]); InputSimulatorUtils.SendKey(KeyCodes.Map["0"]); } break; case "20": { //如果当前焦点控件是输入框 if (this.ActiveControl is TextBox) { var activeControl = this.ActiveControl as TextBox; activeControl.Text = string.Empty; } InputSimulatorUtils.SendKey(KeyCodes.Map["2"]); InputSimulatorUtils.SendKey(KeyCodes.Map["0"]); } break; case "10": { //如果当前焦点控件是输入框 if (this.ActiveControl is TextBox) { var activeControl = this.ActiveControl as TextBox; activeControl.Text = string.Empty; } InputSimulatorUtils.SendKey(KeyCodes.Map["1"]); InputSimulatorUtils.SendKey(KeyCodes.Map["0"]); } break; default: InputSimulatorUtils.SendKey(KeyCodes.Map[e.KeyCode]); break; } } private void OnPaymentRowDeleteClick(object sender , PaymentRowDeleteClickEventArgs e) { if (this._orderObject.Pays.Exists(x => x.Id == e.ObjectId)) { var _oldItem = this._orderObject.Pays.Find(x => x.Id == e.ObjectId); //删除已经存在的支付方式 this._orderObject.Pays.Remove(_oldItem); //未收金额 var unpaidAmount = this._orderObject.PaidAmount - this._orderObject.ReceivedAmount; this.txtCash.Text = unpaidAmount < 0 ? "0" : unpaidAmount.ToString(); this.txtCash.Focus(); this.txtCash.SelectAll(); if(this._orderObject.Pays.Count == 0 || !this._orderObject.Pays.Exists(x=>x.No == PayModeNo)) { this._orderObject.ChangeAmount = 0; } //刷新界面数据 this.billSummary1.Refresh(this._orderObject); this.billGrid1.RefreshGrid(this._orderObject.Pays); } } private void OnCashEnterClick(object sender , EnterEventArg e) { this.OnKeyboardBefore(this.billKeyboard1 , new BillKeyboardEventArgs("accept")); if (this.PaymentVerify) { this.OnKeyboardAfter(this.billKeyboard1 , new BillKeyboardEventArgs("accept")); } } public override void Clear() { base.Clear(); this.txtCash.Text = string.Empty; this._orderObject = null; } } }