public bool Add(DateTime datetime, out string strResult)
        {
            strResult = string.Empty;
            bool result        = false;
            var  dailyBalancel = DailyBalanceRepository.GetQueryable().Where(i => i.SettleDate <= datetime);

            if (dailyBalancel.Any())
            {
                #region 日结表移入历史表
                try
                {
                    foreach (var item in dailyBalancel.ToArray())
                    {
                        DailyBalanceHistory history = new DailyBalanceHistory();
                        history.ID             = item.ID;
                        history.SettleDate     = item.SettleDate;
                        history.WarehouseCode  = item.WarehouseCode;
                        history.ProductCode    = item.ProductCode;
                        history.UnitCode       = item.UnitCode;
                        history.Beginning      = item.Beginning;
                        history.EntryAmount    = item.EntryAmount;
                        history.DeliveryAmount = item.DeliveryAmount;
                        history.ProfitAmount   = item.ProfitAmount;
                        history.LossAmount     = item.LossAmount;
                        history.Ending         = item.Ending;
                        DailyBalanceHistoryRepository.Add(history);
                    }
                    result = true;
                }
                catch (Exception e)
                {
                    strResult = e.InnerException.ToString();
                    result    = false;
                }
                #endregion

                #region  除数据
                try
                {
                    foreach (var item in dailyBalancel.ToList())
                    {
                        DailyBalanceRepository.Delete(item);
                        DailyBalanceRepository.SaveChanges();
                        result = true;
                    }
                }
                catch (Exception e)
                {
                    strResult = e.InnerException.ToString();
                    result    = false;
                }
                #endregion

                DailyBalanceHistoryRepository.SaveChanges();
            }
            else
            {
                strResult = "数据不存在!";
            }
            return(result);
        }
Beispiel #2
0
        public Boolean DoDailyBalance(string warehouseCode, string settleDate, ref string errorInfo)
        {
            try
            {
                using (var scope = new TransactionScope())
                {
                    var inQuery           = InBillDetailRepository.GetQueryable().AsEnumerable();
                    var outQuery          = OutBillDetailRepository.GetQueryable().AsEnumerable();
                    var profitLossQuery   = ProfitLossBillDetailRepository.GetQueryable().AsEnumerable();
                    var dailyBalanceQuery = DailyBalanceRepository.GetQueryable().AsEnumerable();

                    DateTime dt1 = Convert.ToDateTime(settleDate);

                    if (DateTime.Now < dt1)
                    {
                        errorInfo = "选择日结日期大于当前日期,不可以进行日结!";
                        return(false);
                    }
                    var dailyBalance = dailyBalanceQuery.Where(d => d.SettleDate < dt1)
                                       .OrderByDescending(d => d.SettleDate)
                                       .FirstOrDefault();
                    string t = dailyBalance != null?dailyBalance.SettleDate.ToString("yyyy-MM-dd") : "";

                    var oldDailyBalance = dailyBalanceQuery.Where(d => (d.WarehouseCode == warehouseCode ||
                                                                        string.IsNullOrEmpty(warehouseCode)) &&
                                                                  d.SettleDate.ToString("yyyy-MM-dd") == settleDate)
                                          .ToArray();
                    DailyBalanceRepository.Delete(oldDailyBalance);
                    DailyBalanceRepository.SaveChanges();

                    var query = inQuery.Where(a => (a.InBillMaster.WarehouseCode == warehouseCode ||
                                                    string.IsNullOrEmpty(warehouseCode)) &&
                                              a.InBillMaster.BillDate.ToString("yyyy-MM-dd") == settleDate
                                              ).Select(a => new
                    {
                        BillDate       = a.InBillMaster.BillDate.ToString("yyyy-MM-dd"),
                        WarehouseCode  = a.InBillMaster.Warehouse.WarehouseCode,
                        ProductCode    = a.ProductCode,
                        UnitCode       = a.Product.UnitCode,
                        Beginning      = decimal.Zero,
                        EntryAmount    = a.RealQuantity,
                        DeliveryAmount = decimal.Zero,
                        ProfitAmount   = decimal.Zero,
                        LossAmount     = decimal.Zero,
                        Ending         = decimal.Zero
                    }).Union(outQuery.Where(a => (a.OutBillMaster.WarehouseCode == warehouseCode ||
                                                  string.IsNullOrEmpty(warehouseCode)) &&
                                            a.OutBillMaster.BillDate.ToString("yyyy-MM-dd") == settleDate
                                            ).Select(a => new
                    {
                        BillDate       = a.OutBillMaster.BillDate.ToString("yyyy-MM-dd"),
                        WarehouseCode  = a.OutBillMaster.Warehouse.WarehouseCode,
                        ProductCode    = a.ProductCode,
                        UnitCode       = a.Product.UnitCode,
                        Beginning      = decimal.Zero,
                        EntryAmount    = decimal.Zero,
                        DeliveryAmount = a.RealQuantity,
                        ProfitAmount   = decimal.Zero,
                        LossAmount     = decimal.Zero,
                        Ending         = decimal.Zero
                    })).Union(profitLossQuery.Where(a => (a.ProfitLossBillMaster.WarehouseCode == warehouseCode ||
                                                          string.IsNullOrEmpty(warehouseCode)) &&
                                                    a.ProfitLossBillMaster.BillDate.ToString("yyyy-MM-dd") == settleDate
                                                    ).Select(a => new
                    {
                        BillDate       = a.ProfitLossBillMaster.BillDate.ToString("yyyy-MM-dd"),
                        WarehouseCode  = a.ProfitLossBillMaster.Warehouse.WarehouseCode,
                        ProductCode    = a.ProductCode,
                        UnitCode       = a.Product.UnitCode,
                        Beginning      = decimal.Zero,
                        EntryAmount    = decimal.Zero,
                        DeliveryAmount = decimal.Zero,
                        ProfitAmount   = a.Quantity > 0 ? Math.Abs(a.Quantity) : decimal.Zero,
                        LossAmount     = a.Quantity < 0 ? Math.Abs(a.Quantity) : decimal.Zero,
                        Ending         = decimal.Zero
                    })).Union(dailyBalanceQuery.Where(d => (d.WarehouseCode == warehouseCode ||
                                                            string.IsNullOrEmpty(warehouseCode)) &&
                                                      d.SettleDate.ToString("yyyy-MM-dd") == t &&
                                                      d.Ending > decimal.Zero
                                                      ).Select(a => new
                    {
                        BillDate       = settleDate,
                        WarehouseCode  = a.WarehouseCode,
                        ProductCode    = a.ProductCode,
                        UnitCode       = a.Product.UnitCode,
                        Beginning      = a.Ending,
                        EntryAmount    = decimal.Zero,
                        DeliveryAmount = decimal.Zero,
                        ProfitAmount   = decimal.Zero,
                        LossAmount     = decimal.Zero,
                        Ending         = decimal.Zero
                    }
                                                               ));

                    var newDailyBalance = query.GroupBy(a => new { a.BillDate, a.WarehouseCode, a.ProductCode, a.UnitCode })
                                          .Select(a => new DailyBalance
                    {
                        SettleDate     = Convert.ToDateTime(a.Key.BillDate),
                        WarehouseCode  = a.Key.WarehouseCode,
                        ProductCode    = a.Key.ProductCode,
                        UnitCode       = a.Key.UnitCode,
                        Beginning      = a.Sum(d => d.Beginning),
                        EntryAmount    = a.Sum(d => d.EntryAmount),
                        DeliveryAmount = a.Sum(d => d.DeliveryAmount),
                        ProfitAmount   = a.Sum(d => d.ProfitAmount),
                        LossAmount     = a.Sum(d => d.LossAmount),
                        Ending         = a.Sum(d => d.Beginning) + a.Sum(d => d.EntryAmount) - a.Sum(d => d.DeliveryAmount) + a.Sum(d => d.ProfitAmount) - a.Sum(d => d.LossAmount),
                    }).ToArray();

                    newDailyBalance.AsParallel().ForAll(b => b.ID = Guid.NewGuid());
                    foreach (var item in newDailyBalance)
                    {
                        item.ID = Guid.NewGuid();
                        DailyBalanceRepository.Add(item);
                    }

                    DailyBalanceRepository.SaveChanges();
                    scope.Complete();
                }
                return(true);
            }
            catch (Exception e)
            {
                errorInfo = "日结时出现错误,详情:" + e.Message;
                return(false);
            }
        }