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

1551 lines
68 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
using POSV.Bean;
using POSV.Bill;
using POSV.Card;
using POSV.Component;
using POSV.Entity;
using POSV.Member;
using POSV.MessageEvent;
using POSV.ShoppingCart;
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.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace POSV.Business
{
public partial class MCardDialogForm : BusinessForm
{
/// <summary>
/// 会员卡参数配置规则
/// </summary>
private MemberCardItem _currentCardItem = null;
/// <summary>
/// 当前读卡的方式
/// </summary>
private MemberCardReadType _currentReadCardType = MemberCardReadType.;
/// <summary>
/// 会员卡操作
/// </summary>
private MemberCardOperator _currentOperator = MemberCardOperator.;
/// <summary>
/// 当前会员对象,操作员输入条件后获取的对象
/// </summary>
private MemberInfoQueryResponse _currentMemberInfo = null;
/// <summary>
/// 会员卡支付前,请求的交易参考号
/// </summary>
private CardTradeCreateVoucherNoResponse _newVoucherNo = null;
/// <summary>
/// 当前会员选择的会员卡
/// </summary>
private MemberCard _currentCard = null;
/// <summary>
/// 是否是M1读卡
/// </summary>
private bool isM1Card = false;
/// <summary>
/// 订单对象
/// </summary>
private OrderObject _orderObject = null;
private decimal selectCardMony;
public MCardDialogForm(OrderObject orderObject)
{
InitializeComponent();
this.tabCardControl.TabsVisible = false;
this._orderObject = orderObject;
this.lblTips.Text = string.Format(this.lblTips.Tag.ToString(), this._orderObject.ReceivableAmount - this._orderObject.ReceivedAmount);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (DesignMode) return;
SetStyle(ControlStyles.Selectable, true);
//当前选中的会员卡支付
this.Tag = OrderUtils.GetPayMode("02");
//将当前应收金额赋值
this.txtCardNo.Focus();
this.txtCardNo.SelectAll();
this.labelMessage.Text = "请刷会员卡/扫码/输入手机号";
//消费金额
this.txtAmount.Multiline = false;
//会员卡号
this.txtCardNo.Multiline = false;
//会员密码
this.txtPasswd.Multiline = false;
this.BindUi(this._orderObject);
}
/// <summary>
/// 统一收单-订单创建(获取交易参考号)
/// </summary>
/// <param name="refresh"></param>
private void NewVoucherNo(bool refresh)
{
var data = BusinessUtils.Instance.CreateOrderCardTrade(this._orderObject);
if (data.Item1)
{
this._newVoucherNo = data.Item3;
}
else
{
this.labelMessage.Text = data.Item2;
this._newVoucherNo = null;
}
}
/// <summary>
/// 调整会员卡操作Tab项
/// </summary>
private void RefreshUi()
{
if (this._currentOperator == MemberCardOperator.)
{
this.labelMessage.Text = "请刷会员卡/扫码/输入手机号";
if (this._currentCardItem.ListReader.Contains(MemberCardReadType.IC) && this._currentCardItem.ListType.Contains(MemberCardType.Mifare))
{
//启用M1卡自动打开循环读卡模式
Task.Factory.StartNew(() =>
{
string showMsg = string.Empty;
while (true)
{
Thread.Sleep(1000);
var result = CardOperateUtils.Instance.ReadCardNoContinue();
if (result.Item1)
{
if (result.Item2.Length == 16)
{
if (!this.IsHandleCreated || this.IsDisposed) return;
this.Invoke(new Action(() =>
{
this.txtCardNo.Text = result.Item2;
isM1Card = true;
//读卡成功,模拟回车事件
InputSimulatorUtils.SendKey(KeyCodes.Map["return"]);
}));
return;
}
else
{
showMsg = "卡号非法";
}
}
else
{
result = CardOperateUtilsOther.Instance.ReadCardNoContinue();
if (result.Item1)
{
if (!this.IsHandleCreated || this.IsDisposed) return;
this.Invoke(new Action(() =>
{
this.txtCardNo.Text = result.Item2;
isM1Card = true;
//读卡成功,模拟回车事件
InputSimulatorUtils.SendKey(KeyCodes.Map["return"]);
}));
return;
}
else
{
showMsg = result.Item2;
}
}
if (!this.IsHandleCreated || this.IsDisposed) return;
if (!this.IsDisposed)
{
this.labelMessage.Text = showMsg;
}
}
});
}
}
else
{
//关闭读卡器
CardOperateUtils.Instance.TerminationCardRead();
}
switch (this._currentOperator)
{
case MemberCardOperator.:
{
this.ActiveControl = this.txtCardNo;
this.txtCardNo.Focus();
this.txtCardNo.SelectAll();
}
break;
case MemberCardOperator.:
{
var skipNoMoney = Global.Instance.GlobalConfigStringValue(ConfigConstant.CONFIG_CASHIER_CARDPAYSKIPNOMONEY, "0");
if (!BusinessUtils.Instance.HavMemberPromotionCoup(this._orderObject) && this._currentCard != null && this._currentCard.AvailableAmount <= 0 && "1".Equals(skipNoMoney))
{
this._currentOperator = MemberCardOperator.;
this._orderObject.Member = null;
this._currentCard = null;
BusinessUtils.Instance.RemoveMemberPromotion(this._orderObject);
this.BindUi(this._orderObject);
this.labelMessage.Text = "余额为0不允许消费请充值或选择其他付款方式!";
return;
}
var cardNo = string.IsNullOrEmpty(this._currentCard.FaceNo) ? this._currentCard.CardNo : this._currentCard.CardNo;
this.lblCardNo.Text = CardUtils.CardNoEncrypt(cardNo);
this.lblBalance.Text = this._currentCard.AvailableAmount.ToString();
this.lblPoint.Text = this._currentCard.TotalPoint.ToString();
this.lblName.Text = this._currentMemberInfo.Name;
this.lblMobile.Text = this._currentMemberInfo.Mobile;
if (this._currentCard.IsNoPwd == 1 && this._currentCardItem.PaymentType == 1 && this._currentMemberInfo.GetWay != MemberCardNoType.)
{
this.lblFree.Text = string.Format("免密额度:{0}元/笔", this._currentCard.NpAmount.ToString());
}
else
{
this.lblFree.Text = string.Format("凭密码消费");
}
//不抹零
this._orderObject.MalingAmount = 0;
//不找零
this._orderObject.ChangeAmount = 0;
//未收金额
var unpaidAmount = this._orderObject.ReceivableAmount - this._orderObject.ReceivedAmount;
//将当前应收金额赋值
this.txtAmount.Text = unpaidAmount >= 0 ? unpaidAmount.ToString() : "0";
this.selectCardMony = this._currentCard.AvailableAmount;
this.ActiveControl = this.txtAmount;
this.txtAmount.Focus();
this.txtAmount.SelectAll();
////如果会员余额小于未付金额,将应收金额赋值为会员余额
if (this._currentCard.AvailableAmount < unpaidAmount)
{
this.labelMessage.Text = "会员余额不足,请充值或选择其他付款方式";
}
else
{
this.labelMessage.Text = "请确认会员信息";
PayMoney();
}
}
break;
case MemberCardOperator.:
{
//消费前余额
decimal beforeAmount = this._currentCard.AvailableAmount;
this.lblBefAmount.Text = OrderUtils.ToRound(beforeAmount).ToString();
//本次消费金额
this.lblPayAmount.Text = this.txtAmount.DecimalValue.ToString();
//消费后的余额
decimal afterAmount = beforeAmount - this.txtAmount.DecimalValue;
this.lblAftAmount.Text = OrderUtils.ToRound(afterAmount).ToString();
this.ActiveControl = this.txtPasswd;
this.txtPasswd.Text = string.Empty;
this.txtPasswd.Focus();
this.txtPasswd.SelectAll();
this.labelMessage.Text = "请输入密码";
}
break;
}
//刷新支付汇总清单
//this.billSummary1.Refresh(this._orderObject);
////刷新支付列表
//this.billGrid1.RefreshGrid(this._orderObject.Pays);
}
public void BindUi(OrderObject orderObject)
{
if (orderObject == null) return;
this._orderObject = orderObject;
this._newVoucherNo = null;
//异步获取交易参考号
//Task.Factory.StartNew(() => {
// this.NewVoucherNo(false);
//});
this._currentMemberInfo = this._orderObject.Member;
if (this._currentMemberInfo != null)
{
this._currentCard = this._orderObject.Member.CurrentCard;
}
//会员规则
this._currentCardItem = JsonUtils.Deserialize<MemberCardItem>(Global.Instance.GlobalConfigStringValue(ConfigConstant.CASHIER_MEMBER_CARD));
//是否会员卡功能以管理端设置参数优先
var memberFunctionsPrior = Global.Instance.GlobalConfigBoolValue(ConfigConstant.CONFIG_CASHIER_MEMBER_FUNCTIONS_PRIOR, false);
//主界面刷卡
if (this._orderObject.Member != null && this._currentCardItem.Function.Contains(MemberCardFunctions.))
{
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = false;
this.tabCardPasswd.Visible = false;
this.tabCardSelected.Visible = false;
this.tabCardPay.Visible = true;
this.tabCardControl.SelectedTab = this.tabCardPay;
}
else
{
//主界面没有刷卡
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = true;
this.tabCardPasswd.Visible = false;
this.tabCardSelected.Visible = false;
this.tabCardPay.Visible = false;
this.tabCardControl.SelectedTab = this.tabCardRead;
this.txtCardNo.Text = string.Empty;
this.txtCardNo.Focus();
this.txtCardNo.SelectAll();
}
this.RefreshUi();
}
private bool VerifyInputValue()
{
if (this._orderObject == null)
{
this.labelMessage.Text = "订单数据错误啦,请拍个照...";
return false;
}
switch (this._currentOperator)
{
case MemberCardOperator.:
{
var inputValue = this.txtCardNo.Text.Trim();
if (string.IsNullOrEmpty(inputValue))
{
this.labelMessage.Text = "请刷会员卡/扫码/输入手机号";
return false;
}
//智能识别刷卡方式
var way = CardUtils.JudgeCardWay(Global.Instance.Worker.TenantId, inputValue);
if (way == MemberCardNoType.)
{
this.labelMessage.Text = "无法识别的卡号,请确认";
return false;
}
}
break;
case MemberCardOperator.:
{
if (this._newVoucherNo == null)
{
this.NewVoucherNo(false);
if (this._newVoucherNo == null)
{
this.labelMessage.Text = "获取交易参考号失败";
return false;
}
}
////输入是否满足结账条件:应收金额 = 实际收款金额 - 找零金额
//bool isVerify = (this._orderObject.ReceivableAmount == (this._orderObject.PaidAmount - this._orderObject.ChangeAmount));
//输入是否满足结账条件:消费明细中实收金额的合计 = 主单中的实收金额
var isVerify = this._orderObject.Pays.Sum(x => x.PaidAmount) >= this._orderObject.PaidAmount;
//满足结账条件
if (isVerify)
{
this.labelMessage.Text = "满足结账条件,点击完成结账";
this.txtAmount.SelectAll();
return false;
}
//当前选择的会员卡
var cardNo = this._currentCard.CardNo;
//实收金额,操作员录入消费卡金
decimal inputAmount = this.txtAmount.DecimalValue;
if (inputAmount < 0)
{
this.labelMessage.Text = "请输入金额";
this.txtAmount.SelectAll();
return false;
}
if (inputAmount > this._orderObject.ReceivableAmount)
{
this.labelMessage.Text = "不允许超出应收金额";
this.txtAmount.SelectAll();
return false;
}
//扣除已经在支付清单的余额
decimal freeze = this._orderObject.Pays.Where(x => x.CardNo == cardNo).Sum(x => x.Amount);
//当前卡可用余额
decimal balance = this._currentCard.AvailableAmount - freeze;
//操作员录入消费金额大于卡余额
if (inputAmount > balance)
{
this.labelMessage.Text = "卡余额不足,请重新输入";
this.txtAmount.SelectAll();
return false;
}
}
break;
case MemberCardOperator.:
{
//当前选择的会员卡
var cardNo = this._currentCard.CardNo;
decimal inputAmount = this.txtAmount.DecimalValue;
//扣除已经在支付清单的余额
decimal freeze = this._orderObject.Pays.Where(x => x.CardNo == cardNo).Sum(x => x.Amount);
//当前卡可用余额
decimal balance = this._currentCard.AvailableAmount - freeze;
//操作员录入消费金额大于卡余额
if (inputAmount > balance)
{
this.labelMessage.Text = "卡余额不足,请重新输入";
this.txtAmount.SelectAll();
return false;
}
this.labelMessage.Text = "请输入密码";
if (this._newVoucherNo == null)
{
this.labelMessage.Text = "获取交易参考号失败";
return false;
}
////输入是否满足结账条件:应收金额 = 实际收款金额 - 找零金额
//bool isVerify = (this._orderObject.ReceivableAmount == (this._orderObject.PaidAmount - this._orderObject.ChangeAmount));
//输入是否满足结账条件:消费明细中实收金额的合计 = 主单中的实收金额
var isVerify = this._orderObject.Pays.Sum(x => x.PaidAmount) >= this._orderObject.PaidAmount;
//满足结账条件
if (isVerify)
{
this.labelMessage.Text = "满足结账条件,点击完成结账";
this.txtAmount.SelectAll();
return false;
}
}
break;
}
return true;
}
/// <summary>
/// 操作是否验证通过
/// </summary>
private bool PaymentVerify = false;
private void OnKeyBoardTouchAfter(object sender, KeyboardExtEventArgs e)
{
switch (e.KeyCode)
{
case "clear":
{
//如果当前焦点控件是输入框
if (this.ActiveControl is TextBox)
{
var activeControl = this.ActiveControl as TextBox;
activeControl.Text = string.Empty;
RefreshUi();
}
}
break;
case "close":
{
OnCloseClick(sender, EventArgs.Empty);
}
break;
case "accept":
//验证通过
if (this.PaymentVerify)
{
switch (this._currentOperator)
{
case MemberCardOperator.:
{
var voucherNo = this.txtCardNo.Text.Trim();
//智能识别刷卡方式
var way = CardUtils.JudgeCardWay(Global.Instance.Worker.TenantId, voucherNo);
if (way == MemberCardNoType.)
{
this.labelMessage.Text = "无法识别的卡号,请确认";
return;
}
MemberInfoQueryRequest request = new MemberInfoQueryRequest();
request.Keyword = voucherNo;
switch (way)
{
case MemberCardNoType.:
{
request.Property = "scanCode";
}
break;
case MemberCardNoType.:
{
request.Property = "cardNo";
}
break;
case MemberCardNoType.:
{
request.Property = "mobile";
}
break;
case MemberCardNoType.:
{
request.Property = "surfaceNo";
}
break;
}
if (!isM1Card)
{
//是否允许手工输入卡号
var allowInput = Global.Instance.GlobalConfigStringValue(ConfigConstant.CASHIER_ALLOW_INPUT_CARDNO, "0");
if ("0".Equals(allowInput) && maxChangeTime > 200 && (request.Property.Equals("surfaceNo") || request.Property.Equals("cardNo")))
{
txtCardNo.Text = "";
this.labelMessage.Text = "不允许手工录入卡号";
return;
}
}
isM1Card = false;
request.ShopNo = Global.Instance.Authc.StoreNo;
request.WorkerNo = Global.Instance.Worker.No;
request.PosNo = Global.Instance.Authc.PosNo;
var resp = CardUtils.MemberInfoQuery(request);
//会员卡获取成功
if (resp.Item1)
{
this.OnAcceptButtonClick(new TransparentEventArgs(TransparentAction.Accept, e.KeyCode, ""));
//查询返回结果
this._currentMemberInfo = resp.Item3;
//读卡方式
this._currentMemberInfo.GetWay = way;
if (this._currentMemberInfo.CardList.Count == 0)
{
this.labelMessage.Text = "没有可用的会员卡...";
return;
}
//当前会员
if (this._orderObject.Member == null && this._currentCardItem.Function.Contains(MemberCardFunctions.))
{
this._orderObject.Member = this._currentMemberInfo;
//外部未刷卡,新会员,执行订单会员价
foreach (var item in this._orderObject.Items)
{
//套餐,自动找到套餐主菜进行优惠计算
if (item != null && item.RowState == OrderRowState.)
{
//套餐明细,找到套餐主菜进行优惠计算
var mainItem = this._orderObject.Items.Find(x => x.Group == item.Group && x.RowState == OrderRowState.);
if (mainItem != null)
{
if (item.Promotions != null && item.Promotions.Count > 0)
{
//有优惠,不计算单品优惠
//return;
}
else
{
var promResult = PromotionEngine.Instance.CalItemPromotion(this._orderObject, item);
if (promResult.Count > 0)
{
if (promResult.Exists(x => x.Type == PromotionType. || x.Type == PromotionType.))
{
//ApplyDoublePromMain(promResult, item);
}
else
{
var fineProm = promResult.OrderByDescending(x => x.DiscountAmount).ToList()[0];
var needUpdate = BusinessUtils.Instance.ApplyItemFinePromotion(this._orderObject, item, fineProm);
}
}
}
}
}
else
{
if (item != null && item.Promotions != null && item.Promotions.Count > 0)
{
//有优惠,不计算单品优惠
//return;
}
else
{
var promResult = PromotionEngine.Instance.CalItemPromotion(this._orderObject, item);
if (promResult.Count > 0)
{
if (promResult.Exists(x => x.Type == PromotionType. || x.Type == PromotionType.))
{
//ApplyDoublePromMain(promResult, item);
}
else
{
var fineProm = promResult.OrderByDescending(x => x.DiscountAmount).ToList()[0];
var needUpdate = BusinessUtils.Instance.ApplyItemFinePromotion(this._orderObject, item, fineProm);
}
}
//套餐道菜分摊
PromotionUtils.CalculateSuitShare(this._orderObject, item);
}
}
}
}
if (this._currentMemberInfo.CardList.Count > 1)
{
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = false;
this.tabCardPasswd.Visible = false;
this.tabCardSelected.Visible = true;
this.tabCardPay.Visible = false;
this.tabCardSelected.Text = "获取到" + this._currentMemberInfo.CardList.Count + "张卡,请选择";
this.tabCardControl.SelectedTab = this.tabCardSelected;
//多张会员卡,等待收银员选择
this._currentCard = null;
//进入多卡选择界面
this.BuilderSelectedCard(this._currentMemberInfo.CardList);
}
else
{
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = false;
this.tabCardPasswd.Visible = false;
this.tabCardSelected.Visible = false;
this.tabCardPay.Visible = true;
this.tabCardControl.SelectedTab = this.tabCardPay;
//只有一张会员卡,自动选择作为当前可用会员卡
this._currentCard = this._currentMemberInfo.CardList[0];
//当前选中的会员卡
if (this._orderObject.Member != null && this._orderObject.Member.Id.Equals(this._currentMemberInfo.Id))
{
this._orderObject.Member.CurrentCard = this._currentCard;
}
var skipNoMoney = Global.Instance.GlobalConfigStringValue(ConfigConstant.CONFIG_CASHIER_CARDPAYSKIPNOMONEY, "0");
if (!BusinessUtils.Instance.HavMemberPromotionCoup(this._orderObject) && this._currentCard != null && this._currentCard.AvailableAmount <= 0 && "1".Equals(skipNoMoney))
{
this._currentOperator = MemberCardOperator.;
this._orderObject.Member = null;
this._currentCard = null;
BusinessUtils.Instance.RemoveMemberPromotion(this._orderObject);
this.BindUi(this._orderObject);
this.txtCardNo.Text = voucherNo;
this.labelMessage.Text = "余额为0不允许消费请充值或选择其他付款方式!";
return;
}
this.RefreshUi();
}
}
else
{
this.labelMessage.Text = resp.Item2;
return;
}
}
break;
}
}
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 OnCloseClick(object sender, EventArgs e)
{
this._newVoucherNo = null;
//取消会员刷卡数据
this._orderObject.Member = null;
//关闭读卡器
CardOperateUtils.Instance.TerminationCardRead();
if (this.Owner != null)
{
this.Owner.Close();
}
this.Close();
}
private void OnKeyBoardTouchBefore(object sender, KeyboardExtEventArgs e)
{
this.PaymentVerify = VerifyInputValue();
}
private void AddMCardPayType(CardTradeCreateCardRequest request1, CardTradeCreateCardResponse response, bool useConfirmed, string passwd)
{
if (request1 == null || response == null || this._newVoucherNo == null) return;
//构建会员卡支付方式
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.PayNo = OrderUtils.Instance.GeneratePayNo();
item.Id = IdWorkerUtils.Instance.NextId();
item.Name = payMode.Name;
item.No = payMode.No;
item.PointFlag = payMode.PointFlag;
//实收金额
item.PaidAmount = this.txtAmount.DecimalValue;
//找零金额
item.ChangeAmount = Convert.ToDecimal(0.00);
//已收金额,真正意义上实际收款金额
item.Amount = this.txtAmount.DecimalValue;
item.CardNo = request1.CardNo;
//加入卡面号,手机号信息
if (this._currentCard != null)
{
item.CardFaceNo = this._currentCard.FaceNo;
}
if (this._orderObject.Member != null)
{
item.MemberMobileNo = this._orderObject.Member.Mobile;
}
//用户确认输入密码
item.UseConfirmed = useConfirmed;
//会员卡密码加密
item.Passwd = passwd;
//预支付编码
item.PrePayNo = response.PrePayment;
//交易参考号
item.TradeVoucherNo = this._newVoucherNo.TradeVoucherNo;
item.Memo = "会员卡支付";
item.Subscribe = string.Empty;
//会员卡支付状态,NOT_PAID未扣款
item.Status = (int)OrderPaymentStatus.;
item.StatusDesc = "预下单成功,未扣款";
if (item.Amount == 0)
{
return;
}
//将会员卡压入支付清单
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.txtAmount.Text = unpaidAmount < 0 ? "0" : unpaidAmount.ToString();
this.txtAmount.Focus();
this.txtAmount.SelectAll();
this.labelMessage.Text = "满足结账条件";
//通知结账
//默认支付成功
bool isPaySuccess = true;
//查找会员卡支付列表,如果有会员卡先扣会员卡钱
var cards = this._orderObject.Pays.FindAll(x => x.No.Equals("02"));
//包含会员卡支付,构建会员卡扣款列表
if (cards.Count > 0)
{
//交易参考号
string tradeVoucherNo = string.Empty;
//扣款卡列表
var payCards = new List<PayInfo>();
foreach (var card in cards)
{
PayInfo c = new PayInfo();
c.PrePayment = card.PrePayNo;
c.CardNo = card.CardNo;
c.Amount = Convert.ToInt32(card.Amount * 100);
c.IsNoPwd = card.UseConfirmed ? 0 : 1;
c.Passwd = card.Passwd;
payCards.Add(c);
tradeVoucherNo = card.TradeVoucherNo;
}
//记积分金额
var pointAmount = this._orderObject.Pays.FindAll(x => x.PointFlag == 1).Sum(x => x.Amount);
var request = new CardTradePayRequest();
request.TradeNo = this._orderObject.TradeNo;
request.MemberId = this._orderObject.Member == null ? string.Empty : this._orderObject.Member.Id;
request.Mobile = this._orderObject.Member == null ? string.Empty : this._orderObject.Member.Mobile;
request.TradeVoucherNo = tradeVoucherNo;
request.TotalAmount = Convert.ToInt32(this._orderObject.Amount * 100);
//实收金额
request.RealAmount = Convert.ToInt32(this._orderObject.PaidAmount * 100);
request.PointAmount = this._orderObject.Member == null ? 0 : Convert.ToInt32(pointAmount * 100);
request.PayCardAmount = payCards.Sum(x => x.Amount);
request.DiscountAmount = Convert.ToInt32(this._orderObject.DiscountAmount * 100);
request.PointFlag = 2;//以卡系统计算为准
request.PointValue = 0;
request.PayInfo = payCards;
var pays = new List<Pay>();
foreach (var p in this._orderObject.Pays)
{
var pay = new Pay();
pay.PayTypeNo = p.No;
pay.PayType = p.Name;
pay.Money = Convert.ToInt32(p.Amount * 100);
pay.PointRule = string.Empty;
pay.Point = 0;
pays.Add(pay);
}
request.Pay = pays;
int couponTotalAmount = 0;
var coupons = new List<Coupon>();
if (this._orderObject.Promotions != null)
{
foreach (PromotionOrder promotionOrder in this._orderObject.Promotions)
{
if (PromotionType. == promotionOrder.PromotionType)
{
Coupon coupon = new Coupon();
coupon.CouponId = promotionOrder.CouponId;
coupon.CouponType = "CASH";
coupon.Title = promotionOrder.PlanName;
coupon.Code = promotionOrder.PlanNo;
coupon.Discount = String.Format("{0}", promotionOrder.DiscountRate);
coupon.Cash = String.Format("{0}", promotionOrder.DiscountAmount);
coupon.Gift = promotionOrder.PlanName;
coupon.Memo = String.Format("电子代金券,优惠{0}", promotionOrder.DiscountAmount);
couponTotalAmount += Convert.ToInt32(promotionOrder.DiscountAmount * 100);
coupons.Add(coupon);
}
else if (PromotionType. == promotionOrder.PromotionType)
{
Coupon coupon = new Coupon();
coupon.CouponId = promotionOrder.CouponId;
coupon.CouponType = "DISCOUNT";
coupon.Title = promotionOrder.PlanName;
coupon.Code = promotionOrder.PlanNo;
coupon.Discount = String.Format("{0}", promotionOrder.DiscountRate);
coupon.Cash = String.Format("{0}", promotionOrder.DiscountAmount);
coupon.Gift = promotionOrder.PlanName;
coupon.Memo = String.Format("电子折扣券,优惠{0}", promotionOrder.DiscountAmount);
couponTotalAmount += Convert.ToInt32(promotionOrder.DiscountAmount * 100);
coupons.Add(coupon);
}
}
}
if (this._orderObject.Items != null)
{
foreach (OrderItem orderItem in this._orderObject.Items)
{
if (orderItem.Promotions != null)
{
foreach (PromotionItem promotionItem in orderItem.Promotions)
{
if (PromotionType. == promotionItem.PromotionType)
{
Coupon coupon = new Coupon();
coupon.CouponId = promotionItem.CouponId;
coupon.CouponType = "GIFT";
coupon.Title = promotionItem.PlanName;
coupon.Code = promotionItem.PlanNo;
coupon.Discount = String.Format("{0}", promotionItem.DiscountRate);
coupon.Cash = String.Format("{0}", promotionItem.DiscountAmount);
coupon.Gift = promotionItem.PlanName;
coupon.Memo = String.Format("电子兑换券,优惠{0}", promotionItem.DiscountAmount);
couponTotalAmount += Convert.ToInt32(promotionItem.DiscountAmount * 100);
coupons.Add(coupon);
}
}
}
}
}
request.Coupon = coupons;
request.CouponTotalAmount = couponTotalAmount;
request.ShopNo = Global.Instance.Authc.StoreNo;
request.WorkerNo = Global.Instance.Worker.No;
request.PosNo = Global.Instance.Authc.PosNo;
var payResult = CardUtils.CardTradePay(request);
if (payResult.Item1)
{
//会员卡扣款清单,用于打印
this._orderObject.CardPayResult = payResult.Item3;
var payRes = payResult.Item3;
//刷卡后积分计入主单
this._orderObject.PrePoint = OrderUtils.ToRound(payRes.PrePoint / 100.00M);
this._orderObject.AddPoint = OrderUtils.ToRound(payRes.GiftPoint / 100.00M);
this._orderObject.AftPoint = OrderUtils.ToRound(payRes.AftPoint / 100.00M);
//调整每张卡的扣款状态
foreach (var card in cards)
{
var payCard = payRes.CardList.Find(x => x.CardNo == card.CardNo);
if (payCard == null)
{
LOGGER.Error(string.Format("会员卡[{0}]支付成功后返回结果未包含该卡支付信息", card.CardNo));
}
else
{
card.CardBeforeBalance = OrderUtils.ToRound(payCard.PreAmount / 100.00M);
card.CardAftAmount = OrderUtils.ToRound(payCard.AftAmount / 100.00M);
}
card.Status = (int)OrderPaymentStatus.;
card.StatusDesc = payResult.Item2;
card.PayTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
this.labelMessage.Text = payResult.Item2;
}
//会员卡扣款成功
isPaySuccess = true;
}
else
{
//会员卡扣款失败
isPaySuccess = false;
this.labelMessage.Text = payResult.Item2;
}
}
else
{
//不包含会员卡,进行判断是否使用会员,会员充值积分
if (this._orderObject.IsMember == 1)
{
var result = BusinessUtils.Instance.AddCardPoint4Order(this._orderObject);
if (result.Item1)
{
isPaySuccess = true;
}
else
{
MsgEvent.Send(Constant.PAY_MESSAGE_EVENT_NOTIFY, new Tuple<bool, string>(true, result.Item2));
isPaySuccess = false;
}
}
}
//支付成功
if (isPaySuccess)
{
//整单状态
this._orderObject.OrderStatus = OrderStatus.;
//整单支付状态
this._orderObject.PaymentStatus = OrderPaymentStatus.;
//通知主界面结账成功
MsgEvent.Send(Constant.ORDER_PAYMENT_NOTIFY, this._orderObject);
//关闭界面
OnCloseClick(null, EventArgs.Empty);
}
}
/// <summary>
/// 构建多张会员卡选择
/// </summary>
private void BuilderSelectedCard(List<MemberCard> lists)
{
this.itemPanel.Items.Clear();
this.itemPanel.Font = Constant.NORMAL_FONT;
foreach (var card in lists)
{
MetroTileItem item = new MetroTileItem();
item.TileStyle.PaddingTop = 2;
item.TileStyle.PaddingLeft = item.TileStyle.PaddingRight = item.TileStyle.PaddingBottom = -1;
item.CheckBehavior = eMetroTileCheckBehavior.LeftMouseButtonClick;
item.OptionGroup = "SelectedCard";
item.TileSize = new Size(180, 45);
item.Checked = false;
item.TileColor = eMetroTileColor.Teal;
item.TileStyle.Font = Constant.NORMAL_FONT;
item.TileStyle.TextAlignment = eStyleTextAlignment.Near;
item.TileStyle.TextLineAlignment = eStyleTextAlignment.Near;
item.TitleTextFont = Constant.NORMAL_FONT;
item.TitleTextAlignment = ContentAlignment.BottomLeft;
//卡状态(1-正常;2-预售;3-挂失;4-冻结;5-销户;)
var cardStatus = (MemberCardStatus)Enum.ToObject(typeof(MemberCardStatus), card.Status);
item.Text = string.Format("{0} {1} {2}", card.CardNo, cardStatus.ToString(), this._currentMemberInfo.Name);
item.TitleText = string.Format("余额:{0} 积分:{1}", card.AvailableAmount, card.TotalPoint);
item.Tag = card;
item.MouseDown += (o, args) =>
{
MetroTileItem _item = o as MetroTileItem;
_item.Checked = true;
};
item.CheckedChanged += (o, args) =>
{
MetroTileItem _item = o as MetroTileItem;
if (_item.Checked)
{
MemberCard _card = _item.Tag as MemberCard;
//多张卡,操作员选择一张
this._currentCard = _card;
//当前选中的会员卡
if (this._orderObject.Member != null && this._orderObject.Member.Id.Equals(this._currentMemberInfo.Id))
{
this._orderObject.Member.CurrentCard = this._currentCard;
}
_item.TileColor = eMetroTileColor.Coffee;
//选择会员卡后转入会员消费
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = false;
this.tabCardPasswd.Visible = false;
this.tabCardSelected.Visible = false;
this.tabCardPay.Visible = true;
this.tabCardControl.SelectedTab = this.tabCardPay;
this.RefreshUi();
}
else
{
_item.TileColor = eMetroTileColor.Teal;
}
};
this.itemPanel.Items.Add(item);
}
this.itemPanel.Invalidate();
}
private void OnBackReadCardClick(object sender, EventArgs e)
{
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = true;
this.tabCardPasswd.Visible = false;
this.tabCardSelected.Visible = false;
this.tabCardPay.Visible = false;
this.tabCardControl.SelectedTab = this.tabCardRead;
RefreshUi();
}
private void OnChangedCardClick(object sender, EventArgs e)
{
//如果是多张卡,转入多卡选择界面
if (this._currentMemberInfo.CardList.Count > 1)
{
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = false;
this.tabCardPasswd.Visible = false;
this.tabCardSelected.Visible = true;
this.tabCardPay.Visible = false;
this.tabCardSelected.Text = "获取到" + this._currentMemberInfo.CardList.Count + "张卡,请选择";
this.tabCardControl.SelectedTab = this.tabCardSelected;
//多张会员卡,等待收银员选择
this._currentCard = null;
//进入多卡选择界面
this.BuilderSelectedCard(this._currentMemberInfo.CardList);
}
else
{
var skipNoMoney = Global.Instance.GlobalConfigStringValue(ConfigConstant.CONFIG_CASHIER_CARDPAYSKIPNOMONEY, "0");
if ("1".Equals(skipNoMoney) && BusinessUtils.Instance.HavMemberPromotion(this._orderObject))
{
this.labelMessage.Text = "已使用会员优惠不允许换卡";
return;
}
this.labelMessage.Text = "请刷会员卡/扫码/输入手机号";
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = true;
this.tabCardPasswd.Visible = false;
this.tabCardSelected.Visible = false;
this.tabCardPay.Visible = false;
this.tabCardControl.SelectedTab = this.tabCardRead;
this.txtCardNo.Focus();
this.txtCardNo.SelectAll();
}
}
private void OnMCardEnterClick(object sender, Component.EnterEventArg e)
{
this.SimulateKeyboard(new KeyboardExtEventArgs("accept"));
}
public void SimulateKeyboard(KeyboardExtEventArgs e)
{
switch (this._currentOperator)
{
case MemberCardOperator.:
{
this.OnKeyBoardTouchBefore(this.billKeyboard2, e);
if (this.PaymentVerify)
{
this.OnKeyBoardTouchAfter(this.billKeyboard2, e);
}
}
break;
case MemberCardOperator.:
{
this.OnKeyBoardTouchBefore(this.billKeyboard3, e);
if (this.PaymentVerify)
{
this.OnKeyBoardTouchAfter(this.billKeyboard3, e);
}
}
break;
}
}
private long maxChangeTime = 0;
private bool input = false;
private long upTime = 0;
/// <summary>
/// 监听输入框不允许输入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnMemberValueChage(object sender, Component.EnterEventArg e)
{
if (txtCardNo.Text.Length == 0)
{
maxChangeTime = 0;
input = false;
upTime = 0;
}
if (txtCardNo.Text.Length == 1)
{
upTime = DateTime.Now.ToUniversalTime().Ticks;
}
if (txtCardNo.Text.Length > 1)
{
input = true;
}
if (input)
{
var nowTime = DateTime.Now.ToUniversalTime().Ticks;
if ((nowTime - upTime) / 10000 > maxChangeTime)
{
maxChangeTime = (nowTime - upTime) / 10000;
}
LOGGER.Info("输入号码耗时======>" + maxChangeTime);
}
}
/// <summary>
/// 确认支付信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnOk(object sender, EventArgs e)
{
PayMoney();
}
/// <summary>
/// 支付
/// </summary>
private void PayMoney()
{
//实收金额,操作员录入消费卡金
decimal inputAmount = this.txtAmount.DecimalValue;
if (this.selectCardMony < inputAmount)
{
this.labelMessage.Text = "会员余额不足,请充值或选择其他付款方式";
return;
}
this.PaymentVerify = VerifyInputValue();
//当前卡号
var cardNo = this._currentCard.CardNo;
//zhangy 2020-03-14 Add 系统参数开启:输入手机号支付不用输入密码
var _notPassword = Global.Instance.GlobalConfigBoolValue(ConfigConstant.CASHIER_ALLOW_MEMBER_DONOTENTERPASSWORD, true);
//是否启用免密支付,前台收银参数中关闭免密支付
bool notAllowNoPwd = this._currentCardItem.PaymentType == 0;
//1)设置开启免密支付2)开启免密支付但支付金额大于免密支付额度3收银参数关闭免密支付
if (notAllowNoPwd || this._currentCard.IsNoPwd == 0 || (this._currentCard.IsNoPwd == 1 && this._currentCard.NpAmount < inputAmount) || (this._currentMemberInfo.GetWay == MemberCardNoType. && !_notPassword) )
{
this._currentOperator = MemberCardOperator.;
this.tabCardRead.Visible = false;
this.tabCardPasswd.Visible = true;
this.tabCardSelected.Visible = false;
this.tabCardPay.Visible = false;
this.tabCardControl.SelectedTab = this.tabCardPasswd;
this.RefreshUi();
}
else
{
var request = new CardTradeCreateCardRequest();
request.TradeVoucherNo = this._newVoucherNo.TradeVoucherNo;
request.TradeNo = this._orderObject.TradeNo;
request.CardNo = this._currentCard.CardNo;
request.CardTypeNo = this._currentCard.CardTypeNo;
request.CardLevelNo = this._currentCard.CardLevelNo;
request.Amount = Convert.ToInt32(this.txtAmount.DecimalValue * 100);
request.ShopNo = Global.Instance.Authc.StoreNo;
request.WorkerNo = Global.Instance.Worker.No;
request.PosNo = Global.Instance.Authc.PosNo;
//会员卡统一下单
var unifiedOrder = CardUtils.CardTradeCreateCard(request);
if (!unifiedOrder.Item1)
{
this.labelMessage.Text = unifiedOrder.Item2;
return;
}
//构建会员卡支付列表默认没有支付密码按免密支付处理免密支付采用扣款金额RSA加密
string passwd = RSAFromPkcs8.encryptData(request.Amount.ToString(), "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCh5zYnahq/k+1NpEF/fs3q31W+PPnb593gs6RP07bFK3Oq84VH45POVT/UkJJZG623sGLLs/TuIoAx6wQqIQRZMtdZEwQA2fcXu9zEHN/sqa8oBeEfFzGdBE21KX8n0KCmP6GfyPUOCzte6ADl/zAfz24Tf1Q/5iB/Xc++BEVglQIDAQAB", "UTF-8");
this.AddMCardPayType(request, unifiedOrder.Item3, false, passwd);
}
}
private void OnKeyBoardTouchAfter(object sender, BillKeyboardEventArgs e)
{
switch (e.KeyCode)
{
case "clear":
{
//如果当前焦点控件是输入框
if (this.ActiveControl is TextBox)
{
var activeControl = this.ActiveControl as TextBox;
activeControl.Text = string.Empty;
RefreshUi();
}
}
break;
case "close":
{
OnCloseClick(sender, EventArgs.Empty);
}
break;
case "accept":
//验证通过
if (this.PaymentVerify)
{
this.OnAcceptButtonClick(new TransparentEventArgs(TransparentAction.Accept, e.KeyCode, ""));
switch (this._currentOperator)
{
case MemberCardOperator.:
{
//当前选择的会员卡
var cardNo = this._currentCard.CardNo;
//实收金额,操作员录入消费卡金
var inputValue = this.txtPasswd.Text;
var validRequest = new CardValidRequest();
validRequest.CardNo = cardNo;
validRequest.Passwd = inputValue;
validRequest.ShopNo = Global.Instance.Authc.StoreNo;
validRequest.PosNo = Global.Instance.Authc.PosNo;
//校验卡密码
var data = CardUtils.CardValid(validRequest);
if (!data.Item1)
{
this.labelMessage.Text = data.Item2;
this.txtPasswd.SelectAll();
return;
}
var request = new CardTradeCreateCardRequest();
request.TradeVoucherNo = this._newVoucherNo.TradeVoucherNo;
request.TradeNo = this._orderObject.TradeNo;
request.CardNo = this._currentCard.CardNo;
request.CardTypeNo = this._currentCard.CardTypeNo;
request.CardLevelNo = this._currentCard.CardLevelNo;
request.Amount = Convert.ToInt32(this.txtAmount.DecimalValue * 100);
request.ShopNo = Global.Instance.Authc.StoreNo;
request.WorkerNo = Global.Instance.Worker.No;
request.PosNo = Global.Instance.Authc.PosNo;
//会员卡统一下单
var unifiedOrder = CardUtils.CardTradeCreateCard(request);
if (!unifiedOrder.Item1)
{
this.labelMessage.Text = unifiedOrder.Item2;
return;
}
//构建会员卡支付列表
string passwd = DesUtils.Encrypt(this.txtPasswd.Text);
this.AddMCardPayType(request, unifiedOrder.Item3, true, passwd);
}
break;
}
}
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 OnKeyBoardTouchBefore(object sender, BillKeyboardEventArgs e)
{
this.PaymentVerify = VerifyInputValue();
}
//会员卡结账
private void OnMPasswdEnterClick(object sender, EnterEventArg e)
{
this.OnKeyBoardTouchAfter(sender, new BillKeyboardEventArgs("accept"));
}
private void touchButtonX2_Click(object sender, EventArgs e)
{
TransparentForm trans = new TransparentForm(this, new CardRechargeForm());
trans.ShowDialog();
}
}
}