public static string ConvertToJsonString(CardSale cardSale) { return new JavaScriptSerializer().Serialize(new { id = cardSale.id, memberId = cardSale.memberId, balance = cardSale.balance, cash = cardSale.cash, bankUnion = cardSale.bankUnion, payTime = cardSale.payTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), //explain = cardSale.explain, abandon = cardSale.abandon, server = cardSale.server }); }
public static string ConvertToJsonString(CardSale cardSale) { return(new JavaScriptSerializer().Serialize(new { id = cardSale.id, memberId = cardSale.memberId, balance = cardSale.balance, cash = cardSale.cash, bankUnion = cardSale.bankUnion, payTime = cardSale.payTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), //explain = cardSale.explain, abandon = cardSale.abandon, server = cardSale.server })); }
public Result PostSale([FromBody] SaleItem saleItem,int store) { Result result = new Result(); try { using (TransactionScope transcope = new TransactionScope()) { Department department = DepartmentBLL.GetById(store); if(department==null) { result.obj = "错误的门店编号"; }else { Card card = db.Cards.Find(saleItem.cardId); var charge = (from o in db.CardRecharges where o.CardId == card.CardId select o.Money).Sum(); var resume = (from o in db.CardSales where o.CardId == card.CardId select (decimal?)o.Money).Sum() ?? 0; if (card.Money != charge - resume) { card.State = CardState.异常; } if (card.State != CardState.正常) { result.obj = string.Format("该储值卡已失效,状态为{0}", card.State); } else if (saleItem.money <= 0) { //result.obj = "金额不能小于0"; //退款 //查当天消费单 List<decimal> saleList = (from o in db.CardSales where o.CardId == card.CardId && o.SaleTime > DateTime.Today orderby o.Money select o.Money).ToList(); //将已退部分消费设为0 for(int i=0;i<saleList.Count;i++) { if (saleList[i] < 0) { for(int j =i+1;j<saleList.Count;j++) { if(saleList[i]==0-saleList[j]) { saleList[i] = 0; saleList[j] = 0; break; } } } } bool match=false; for(int i=0;i<saleList.Count;i++) { if(saleList[i]==0-saleItem.money) { match = true; break; } } if(match==true) { CardSale sale = new CardSale(); sale.CardId = saleItem.cardId; sale.Money = saleItem.money; sale.BeforeMoney = card.Money; card.Money -= saleItem.money; sale.Remark = saleItem.remark; sale.Sales = UserInfo.CurUser.Id; sale.Store = store; sale.SaleTime = DateTime.Now; db.CardSales.Add(sale); db.SaveChanges(); OUDAL.AccessLog.AddLogAndSave(db, UserInfo.CurUser.Name, card.CardId, Card.LogClass, "退款", string.Format("销售单id:{0}金额:{1}", sale.Id, sale.Money)); db.SaveChanges(); transcope.Complete(); result.success = true; result.obj = string.Format("提交成功,{0}退款{1}元,余额{2}元", card.CardCode, 0-saleItem.money, card.Money); } else { result.obj = string.Format("找不到卡{0}在当天{1}的消费记录,不能退款",card.CardCode,0-saleItem.money); } } else if (card.Money < saleItem.money) { result.obj = "余额不足"; } else { CardSale sale = new CardSale(); sale.CardId = saleItem.cardId; sale.Money = saleItem.money; sale.BeforeMoney = card.Money; card.Money -= saleItem.money; sale.Remark = saleItem.remark; sale.Sales = UserInfo.CurUser.Id; sale.Store = store; sale.SaleTime = DateTime.Now; db.CardSales.Add(sale); db.SaveChanges(); OUDAL.AccessLog.AddLogAndSave(db, UserInfo.CurUser.Name, card.CardId, Card.LogClass, "消费", string.Format("销售单id:{0}金额:{1}", sale.Id, sale.Money)); db.SaveChanges(); transcope.Complete(); result.success = true; result.obj = string.Format("提交成功,{0}消费{1}元,余额{2}元",card.CardCode, saleItem.money,card.Money); } } } } catch (Exception exp) { LogHelper.LogHelper.Error("刷卡消费错误",exp); result.obj = exp.Message; } return result; }