async public Task <HandleMessage> DoUnAccound(DateTime xExeDate) { if (_BeginDate >= xExeDate) { return(HandleMessage.CreateFail(string.Format("回帳失敗, 執行日期必須大於 {0}", _BeginDate.ToString("yyyy/MM/dd")))); } var xCashFlow = LookupCashFlow().Where(w => w.TradeDate >= xExeDate).ToList(); var xCommodityMast = LookupCommodityMast().Where(w => w.TradeDate >= xExeDate).ToList(); if (xCashFlow.IsEmpty() && xCommodityMast.IsEmpty()) { return(HandleMessage.CreateFail("回帳失敗, 無現金流資料")); } foreach (var item in xCashFlow) { _Entity.Entry(item).State = EntityState.Deleted; } foreach (var item in xCommodityMast) { _Entity.Entry(item).State = EntityState.Deleted; } bool success = await _Entity.SaveChangesAsync() > 0; return(HandleMessage.Create(success, "回帳成功", "回帳失敗")); }
async public Task <HandleMessage> DoAccound(DateTime xExeDate) { if (_BeginDate >= xExeDate) { return(HandleMessage.CreateFail(string.Format("結帳失敗, 執行日期必須大於 {0}", _BeginDate.ToString("yyyy/MM/dd")))); } DateTime?xStart = null; var xMast = LookupCashFlow(); if (xMast.IsNotEmpty()) { xStart = xMast.Max(m => m.TradeDate).AddDays(1); } else { return(HandleMessage.CreateFail("結帳失敗, 無現金流資料")); } if (xStart.HasValue.Not()) { return(HandleMessage.CreateFail("結帳失敗, 無起始日期")); } if (xExeDate < xStart) { return(HandleMessage.CreateFail(string.Format("結帳失敗, 執行日期必須大於{0}", xStart.Value.ToString("yyyy/MM/dd")))); } foreach (var d in xStart.Value.ToDates(xExeDate)) { var xShipper = _Entity.Shipper.AsNoTracking().Where(w => w.TradeDate == d).ToList(); ///出貨交易 var xTrading = _Entity.Trading.AsNoTracking() ///賣出交易 .Where(w => xShipper.Where(x => x.TransNo != string.Empty).Select(s => s.TransNo).Contains(w.ShipperNo)).ToList(); var xReceipt = _Entity.Receipt.AsNoTracking().Where(w => w.TradeDate == d).ToList(); ///付款交易 var xPurchase = _Entity.Purchase.AsNoTracking().Where(w => w.TradeDate == d).ToList(); ///進貨交易 var xOrder = _Entity.Order.AsNoTracking() ///下單交易 .Where(w => xPurchase.Where(x => x.TransNo != string.Empty).Select(s => s.TransNo).Contains(w.PurchaseNo)).ToList(); var xYDAY = GetCashFlow(d.AddDays(-1)); var item = new CashFlow(); item.TradeDate = d; item.Income = xShipper.Sum(s => s.TradeAmount); item.Expenses = xShipper.Sum(s => s.Fee) + xReceipt.Sum(s => s.TradeAmount) + xPurchase.Sum(s => s.Fee); item.Balance = (xYDAY.IsNotNull() ? xYDAY.Balance : decimal.Zero) + item.Income - item.Expenses; _Entity.Entry(item).State = EntityState.Added; foreach (var c in _Entity.Commodity.AsNoTracking().Select(s => s.ID)) { var xYDAY_CM = GetCommodityMast(d.AddDays(-1), c); var item_CM = new CommodityMast(); item_CM.TradeDate = d; item_CM.CommodityID = c; item_CM.Quantity = (xYDAY_CM.IsNotNull() ? xYDAY_CM.Quantity : decimal.Zero) - xTrading.Where(w => w.CommodityID == c).Sum(s => s.TradeQuantity) + xOrder.Where(w => w.CommodityID == c).Sum(s => s.TradeQuantity); _Entity.Entry(item).State = EntityState.Added; } } bool success = await _Entity.SaveChangesAsync() > 0; return(HandleMessage.Create(success, "結帳成功", "結帳失敗")); }