/// <summary> /// 对账 /// </summary> /// <param name="lstModify">需要修改状态的往来账列表</param> /// <param name="lstAdd">需要增加的往来账列表</param> /// <param name="wastBookinfo">需要添加的资金流信息</param> public void Checking(IList <ReckoningInfo> lstModify, IList <ReckoningInfo> lstAdd, WasteBookInfo wastBookinfo) { using (var scop = new TransactionScope()) { try { //1.更新被对账往来为已对账 if (lstModify.Count > 0) { _reckoningDao.UpdateCheckState(lstModify, 1); } //2.新增往来账(包括调账) foreach (ReckoningInfo reckoningInfo in lstAdd) { reckoningInfo.AuditingState = (int)AuditingState.Yes; string errorMessage; _reckoningDao.Insert(reckoningInfo, out errorMessage); } //3.资金流 增加一条数据 if (wastBookinfo.Income != 0) { _wasteBookManager.Insert(wastBookinfo); } scop.Complete(); } catch (Exception ex) { throw new Exception("对账失败!", ex); } } }
/// <summary>异步完成订单生成往来账 /// </summary> /// <param name="orderInfo">订单</param> /// <param name="orderDetailList">订单明细</param> /// <param name="errorMessage">异常信息</param> /// <returns></returns> public bool AddByCompleteOrder(GoodsOrderInfo orderInfo, IList <GoodsOrderDetailInfo> orderDetailList, out string errorMessage) { GoodsOrderDeliverInfo goodsOrderDeliverInfo; var reckoningList = NewCreateReckoningInfoList(orderInfo, orderDetailList, out goodsOrderDeliverInfo, out errorMessage).ToList(); if (reckoningList.Count == 0) { errorMessage = "此订单无需生成往来帐!"; return(true); } using (var tran = new System.Transactions.TransactionScope()) { //增加往来帐 foreach (var reckoning in reckoningList) { if (FilialeManager.IsEntityShopFiliale(reckoning.FilialeId)) { if (!AddReckoningToEntityShop(reckoning, out errorMessage)) { errorMessage = "插入门店往来帐失败," + errorMessage + "\r\n数据:" + new Framework.Core.Serialize.JsonSerializer().Serialize(reckoning); return(false); } } else { var success = _reckoningDao.Insert(reckoning, out errorMessage); if (!success) { errorMessage = "插入往来帐失败," + errorMessage + "\r\n数据:" + new Framework.Core.Serialize.JsonSerializer().Serialize(reckoning); return(false); } } } if (goodsOrderDeliverInfo != null) { _goodsOrderDeliver.DeleteOrderDeliver(goodsOrderDeliverInfo.OrderId); var result = _goodsOrderDeliver.InsertOrderDeliver(goodsOrderDeliverInfo); if (!result) { errorMessage = "插入订单快递运费信息失败," + errorMessage + "\r\n数据:" + new Framework.Core.Serialize.JsonSerializer().Serialize(goodsOrderDeliverInfo); return(false); } } tran.Complete(); return(true); } }
/// <summary> /// 确认收获异常,原采购完成,生成新采购 /// </summary> /// <param name="applyId"></param> /// <param name="storageRecord"></param> /// <param name="storageRecordDetail"></param> /// <param name="applyStock"></param> /// <param name="stockDetailInfos"></param> /// <param name="list"></param> /// <param name="msg"> </param> /// <returns></returns> public int ShopConfirmPurchaseOrder(Guid applyId, StorageRecordInfo storageRecord, IList <StorageRecordDetailInfo> storageRecordDetail, ApplyStockInfo applyStock, IList <ApplyStockDetailInfo> stockDetailInfos, IList <ReckoningInfo> list, out string msg) { int state = 0; //获取商品特定时间下最近的结算价存档 var applyInfo = _applyStockDao.FindById(applyId); if (applyInfo == null) { msg = "门店要货申请记录未找到!"; return(-1); } IDictionary <Guid, decimal> dicAvgSettlePrice; using (var scope = new TransactionScope()) { var isError = storageRecord != null && storageRecordDetail != null && storageRecordDetail.Count != 0; var stockState = isError ? (int)ApplyStockState.CheckPending : (int)ApplyStockState.Finished; var flag = UpdateApplyStockState(applyInfo, stockState, false, out dicAvgSettlePrice, out msg); if (flag) { if (isError) { try { _storageManager.NewAddStorageRecordAndDetailList(storageRecord, storageRecordDetail, out msg); } catch (Exception ex) { state = -1; SAL.LogCenter.LogService.LogError(string.Format("ERP服务门店确认收获添加库存记录失败,applyID={0},storageRecord={1},storageRecordDetail={2},applyStock={3},stockDetailInfos={4},reckoningList={5}", applyId, new Framework.Core.Serialize.JsonSerializer().Serialize(storageRecord), new Framework.Core.Serialize.JsonSerializer().Serialize(storageRecordDetail), new Framework.Core.Serialize.JsonSerializer().Serialize(applyStock), new Framework.Core.Serialize.JsonSerializer().Serialize(stockDetailInfos), new Framework.Core.Serialize.JsonSerializer().Serialize(list)), "门店管理", ex); } } if (state == 0 && applyStock != null && stockDetailInfos != null && stockDetailInfos.Count != 0) { state = AddNewApplyStock(applyStock, stockDetailInfos, dicAvgSettlePrice, out msg); } if (state >= 0) { bool result = true; if (list != null && list.Count > 0) { foreach (var reckoningInfo in list) { string error; var sucess = _reckoning.Insert(reckoningInfo, out error); if (!sucess) { result = false; msg = "插入往来帐失败!"; break; } } } if (result) { scope.Complete(); } else { state = -1; SAL.LogCenter.LogService.LogError(string.Format("ERP服务门店确认收获更改状态失败,applyID={0},storageRecord={1},storageRecordDetail={2},applyStock={3},stockDetailInfos={4},reckoningList={5}", applyId, new Framework.Core.Serialize.JsonSerializer().Serialize(storageRecord), new Framework.Core.Serialize.JsonSerializer().Serialize(storageRecordDetail), new Framework.Core.Serialize.JsonSerializer().Serialize(applyStock), new Framework.Core.Serialize.JsonSerializer().Serialize(stockDetailInfos), new Framework.Core.Serialize.JsonSerializer().Serialize(list)), "门店管理"); } } } else { state = -1; SAL.LogCenter.LogService.LogError(string.Format("ERP服务门店确认收获更改状态失败,applyID={0},storageRecord={1},storageRecordDetail={2},applyStock={3},stockDetailInfos={4},reckoningList={5}", applyId, new Framework.Core.Serialize.JsonSerializer().Serialize(storageRecord), new Framework.Core.Serialize.JsonSerializer().Serialize(storageRecordDetail), new Framework.Core.Serialize.JsonSerializer().Serialize(applyStock), new Framework.Core.Serialize.JsonSerializer().Serialize(stockDetailInfos), new Framework.Core.Serialize.JsonSerializer().Serialize(list)), "门店管理"); } } return(state); }
/// <summary> /// 按指定计算日期、销售公司、物流配送公司、仓库、储位,生成销售公司的采购单和采购入库单 /// </summary> /// <param name="calculateDate"></param> /// <param name="saleFilialeThirdCompanyId">销售公司往来单位Id</param> /// <param name="hostingFilialeId">物流公司ID</param> /// <param name="warehouseId"></param> /// <param name="storageType"></param> internal static void GenerateSaleFilialeStockInAndPurchasing(DateTime calculateDate, Guid saleFilialeThirdCompanyId, Guid hostingFilialeId, Guid warehouseId, int storageType) { var hostingFilialeThirdCompany = _companyCussent.GetCompanyByRelevanceFilialeId(hostingFilialeId); //根据物流公司获取往来单位 if (hostingFilialeThirdCompany == null) { return; } var saleFilialeId = _companyCussent.GetRelevanceFilialeIdByCompanyId(saleFilialeThirdCompanyId); //根据往来单位获取公司 if (saleFilialeId == Guid.Empty) { return; } if (saleFilialeId == hostingFilialeId) { return; } bool isGenerated = SaleFilialeStorageInDal.IsGenerated(calculateDate, saleFilialeId, hostingFilialeThirdCompany.CompanyId, warehouseId, storageType); if (!isGenerated) { var simpleStockRecordDetails = SaleFilialeStorageInDal.GetSaleFilialeStockInDetailList(calculateDate, saleFilialeThirdCompanyId, hostingFilialeId, warehouseId, storageType); if (!simpleStockRecordDetails.Any()) { return; } // 不重新获取采购价,直接取对应的物流配送公司给销售公司的的销售出库单里计算即可得到 var purchasePriceDict = (from item in simpleStockRecordDetails.GroupBy(g => g.GoodsId) select new KeyValuePair <Guid, decimal>(item.Key, item.Sum(s => s.Amount) / item.Sum(s => s.Quantity))) .ToDictionary(kv => kv.Key, kv => kv.Value); //销售公司采购单 var purchasingRecord = new PurchasingInfo { PurchasingID = Guid.NewGuid(), PurchasingNo = _codeManager.GetCode(CodeType.PH), CompanyID = hostingFilialeThirdCompany.CompanyId, CompanyName = hostingFilialeThirdCompany.CompanyName, FilialeID = saleFilialeId, WarehouseID = warehouseId, PurchasingState = (int)PurchasingState.AllComplete, PurchasingType = (int)PurchasingType.AutoInternal, StartTime = calculateDate, EndTime = DateTime.Now, Description = "[内部采购]", PmId = Guid.Empty, PmName = string.Empty, Director = "系统", PersonResponsible = Guid.Empty, ArrivalTime = calculateDate.Date, PurchasingFilialeId = saleFilialeId }; var purchasingRecordDetailList = simpleStockRecordDetails.Select(m => new PurchasingDetailInfo { PurchasingGoodsID = Guid.NewGuid(), PurchasingID = purchasingRecord.PurchasingID, GoodsID = m.RealGoodsId, GoodsName = m.GoodsName, GoodsCode = m.GoodsCode, Specification = m.Specification, CompanyID = hostingFilialeThirdCompany.CompanyId, Price = purchasePriceDict.ContainsKey(m.GoodsId) ? purchasePriceDict[m.GoodsId] : 0, PlanQuantity = m.Quantity, PurchasingGoodsType = (int)PurchasingGoodsType.NoGift, RealityQuantity = m.Quantity, State = 1, Description = "[内部采购]", Units = string.Empty }).ToList(); //销售公司采购进货入库单 var stockRecord = new StorageRecordInfo { StockId = Guid.NewGuid(), TradeCode = _codeManager.GetCode(CodeType.RK), FilialeId = saleFilialeId, WarehouseId = warehouseId, ThirdCompanyID = hostingFilialeThirdCompany.CompanyId, DateCreated = calculateDate.Date, Transactor = "系统操作", Description = "[内部采购]", LogisticsCode = string.Empty, StockType = (int)StorageRecordType.BuyStockIn, StockState = (int)StorageRecordState.Finished, LinkTradeType = (int)StorageRecordLinkTradeType.Purchasing, LinkTradeCode = purchasingRecord.PurchasingNo, LinkTradeID = purchasingRecord.PurchasingID, RelevanceFilialeId = Guid.Empty, RelevanceWarehouseId = Guid.Empty, StockValidation = false, StorageType = storageType, SubtotalQuantity = 0, AccountReceivable = 0, TradeBothPartiesType = (int)TradeBothPartiesType.HostingToSale }; var stockRecordDetailList = simpleStockRecordDetails.Select(m => new StorageRecordDetailInfo { StockId = stockRecord.StockId, GoodsId = m.GoodsId, Specification = m.Specification, Quantity = m.Quantity, UnitPrice = purchasePriceDict.ContainsKey(m.GoodsId) ? purchasePriceDict[m.GoodsId] : 0, GoodsName = m.GoodsName, GoodsCode = m.GoodsCode, RealGoodsId = m.RealGoodsId, Description = "[内部采购]", NonceWarehouseGoodsStock = 0, Units = string.Empty, ShelfType = 1 }).ToList(); List <ReckoningInfo> reckoningInfos = new List <ReckoningInfo>(); var accountReceivable = Math.Round(Math.Abs(stockRecordDetailList.Sum(act => act.Quantity * act.UnitPrice)), 2); //可得采购应付款 var saleFilialeReckoning = new ReckoningInfo(Guid.NewGuid(), stockRecord.FilialeId, stockRecord.ThirdCompanyID, _codeManager.GetCode(CodeType.PY), stockRecord.Description, -accountReceivable, (int)ReckoningType.Defray, (int)ReckoningStateType.Currently, (int)CheckType.NotCheck, (int)AuditingState.Yes, stockRecord.TradeCode, stockRecord.WarehouseId) { LinkTradeType = (int)ReckoningLinkTradeType.StockIn, IsOut = stockRecord.IsOut }; //可镜销售出库应收款 var hostingFilialeReckoning = new ReckoningInfo(Guid.NewGuid(), hostingFilialeId, saleFilialeThirdCompanyId, _codeManager.GetCode(CodeType.PY), stockRecord.Description, accountReceivable, (int)ReckoningType.Income, (int)ReckoningStateType.Currently, (int)CheckType.NotCheck, (int)AuditingState.Yes, stockRecord.TradeCode, stockRecord.WarehouseId) { LinkTradeType = (int)ReckoningLinkTradeType.GoodsOrder, IsOut = stockRecord.IsOut }; reckoningInfos.Add(saleFilialeReckoning); reckoningInfos.Add(hostingFilialeReckoning); using (var tran = new TransactionScope(TransactionScopeOption.Required)) { try { var result = _purchasingManager.Insert(purchasingRecord, purchasingRecordDetailList); if (!result) { LogError(string.Format("生成采购单失败!计算日期:{0},销售公司:{1},物流配送公司:{2},仓库:{3},储位:{4}", calculateDate, saleFilialeId, hostingFilialeId, warehouseId, storageType)); return; } result = _storageManager.AddStorageRecord(stockRecord, stockRecordDetailList); if (!result) { LogError(string.Format("生成采购入库单失败!计算日期:{0},销售公司:{1},物流配送公司:{2},仓库:{3},储位:{4}", calculateDate, saleFilialeId, hostingFilialeId, warehouseId, storageType)); return; } foreach (var reckoningInfo in reckoningInfos) { string errorMessage; result = _reckoning.Insert(reckoningInfo, out errorMessage); if (!result) { LogError(string.Format("生成往来账失败!计算日期:{0},销售公司:{1},物流配送公司:{2},仓库:{3},储位:{4},{5}", calculateDate, saleFilialeId, hostingFilialeId, warehouseId, storageType, errorMessage)); return; } } // 计算即时结算价 var items = _realTimeGrossSettlementManager.CreateByPurchaseStockIn(stockRecord.StockId, calculateDate).ToList(); items.ForEach(m => _realTimeGrossSettlementManager.Calculate(m)); tran.Complete(); LogInfo(string.Format("生成成功!计算日期:{0},销售公司:{1},物流配送公司:{2},仓库:{3},储位:{4}", calculateDate, saleFilialeId, hostingFilialeId, warehouseId, storageType)); } catch (Exception ex) { LogError(string.Format("生成采购单和采购入库单失败!计算日期:{0},销售公司:{1},物流配送公司:{2},仓库:{3},储位:{4}", calculateDate, saleFilialeId, hostingFilialeId, warehouseId, storageType), ex); return; } } } }