Exemple #1
0
        public object GetCheckBillMaster()
        {
            var checkBillMaster = CheckBillMasterRepository.GetQueryable()
                                  .Where(i => i.Status == "2" || i.Status == "3")
                                  .Select(i => new { BillNo = i.BillNo, BillType = "4" })
                                  .ToArray();

            return(checkBillMaster);
        }
        /// <summary>
        /// 盘点反审
        /// </summary>
        /// <param name="billNo">单据号</param>
        /// <returns></returns>
        public bool AntiTrial(string billNo)
        {
            bool result = false;
            var  outbm  = CheckBillMasterRepository.GetQueryable().FirstOrDefault(i => i.BillNo == billNo);

            if (outbm != null && outbm.Status == "2")
            {
                outbm.Status         = "1";
                outbm.VerifyDate     = null;
                outbm.UpdateTime     = DateTime.Now;
                outbm.VerifyPersonID = null;
                CheckBillMasterRepository.SaveChanges();
                result = true;
            }
            return(result);
        }
        public bool Delete(string BillNo)
        {
            var checkbm = CheckBillMasterRepository.GetQueryable().FirstOrDefault(i => i.BillNo == BillNo && i.Status == "1");

            if (checkbm != null)
            {
                //Del(OutBillDetailRepository, ibm.OutBillAllots);
                foreach (var item in checkbm.CheckBillDetails.ToArray())
                {
                    item.Storage.IsLock = "0";
                }
                Del(CheckBillDetailRepository, checkbm.CheckBillDetails);
                CheckBillMasterRepository.Delete(checkbm);
                CheckBillMasterRepository.SaveChanges();
            }
            return(true);
        }
        public bool Add(string billNo, string wareCode)
        {
            Guid empanyid = new Guid("d9f369dd-d793-41f5-a191-815503766e94");
            var  check    = new CheckBillMaster();

            check.BillNo          = "12070001CK";
            check.BillDate        = DateTime.Now;
            check.BillTypeCode    = "1";
            check.WarehouseCode   = "CK001";
            check.OperatePersonID = empanyid;
            check.VerifyDate      = DateTime.Now;
            check.Status          = "1";
            check.IsActive        = "1";
            check.UpdateTime      = DateTime.Now;

            CheckBillMasterRepository.Add(check);
            CheckBillMasterRepository.SaveChanges();
            return(true);
        }
        public object GetCheckBillMaster()
        {
            string str             = "";
            var    checkBillDetail = CheckBillDetailRepository.GetQueryable().Where(i => i.Status != "2").Select(b => b.BillNo).ToArray();

            for (int i = 0; i < checkBillDetail.Length; i++)
            {
                str += checkBillDetail[i];
            }
            var checkBillMaster = CheckBillMasterRepository.GetQueryable().ToArray().Where(i => str.Contains(i.BillNo) && i.Status != "5")
                                  .Distinct()
                                  .OrderByDescending(t => t.BillDate)
                                  .Select(i => new
            {
                BillNo = i.BillNo
            });

            return(checkBillMaster);
        }
        /// <summary>
        /// 盘点审核
        /// </summary>
        /// <param name="billNo">单据号</param>
        /// <param name="userName">登陆用户</param>
        /// <returns></returns>
        public bool Audit(string billNo, string userName)
        {
            bool result   = false;
            var  checkbm  = CheckBillMasterRepository.GetQueryable().FirstOrDefault(i => i.BillNo == billNo);
            var  employee = EmployeeRepository.GetQueryable().FirstOrDefault(i => i.UserName == userName);

            if (employee != null)
            {
                if (checkbm != null && checkbm.Status == "1")
                {
                    checkbm.Status         = "2";
                    checkbm.VerifyDate     = DateTime.Now;
                    checkbm.UpdateTime     = DateTime.Now;
                    checkbm.VerifyPersonID = employee.ID;
                    CheckBillMasterRepository.SaveChanges();
                    result = true;
                }
            }
            return(result);
        }
        public bool Delete(string billNo)
        {
            var checkbm = CheckBillMasterRepository.GetQueryable().FirstOrDefault(i => i.BillNo == billNo && i.Status == "1");

            if (checkbm != null)
            {
                foreach (var item in checkbm.CheckBillDetails.ToArray())
                {
                    item.Storage.IsLock = "0";
                }
                using (var scope = new TransactionScope())
                {
                    CheckBillMasterRepository.SaveChanges();
                    CheckBillDetailRepository.GetObjectSet().DeleteEntity(d => d.BillNo == billNo);
                    CheckBillMasterRepository.GetObjectSet().DeleteEntity(c => c.BillNo == billNo);
                    scope.Complete();
                }
            }
            return(true);
        }
        /// <summary>
        /// 生成盘点的主表ID
        /// </summary>
        /// <returns></returns>
        public object GetCheckBillNo()
        {
            IQueryable <CheckBillMaster> CheckMasterQuery = CheckBillMasterRepository.GetQueryable();
            string sysTime      = System.DateTime.Now.ToString("yyMMdd");
            var    inBillMaster = CheckMasterQuery.Where(i => i.BillNo.Contains(sysTime)).ToArray().OrderBy(i => i.BillNo).Select(i => new { i.BillNo }.BillNo);

            if (inBillMaster.Count() == 0)
            {
                return(System.DateTime.Now.ToString("yyMMdd") + "0001" + "CH");
            }
            else
            {
                string billNoStr = inBillMaster.Last(b => b.Contains(sysTime));
                int    i         = Convert.ToInt32(billNoStr.ToString().Substring(6, 4));
                i++;
                string newcode = i.ToString();
                for (int j = 0; j < 4 - i.ToString().Length; j++)
                {
                    newcode = "0" + newcode;
                }
                return(System.DateTime.Now.ToString("yyMMdd") + newcode + "CH");
            }
        }
        /// <summary>
        /// 盘点确认
        /// </summary>
        /// <param name="billNo">单据号</param>
        /// <returns></returns>
        public bool confirmCheck(string billNo, string userName, out string errorInfo)
        {
            bool result = false;

            errorInfo = string.Empty;
            var checkbm     = CheckBillMasterRepository.GetQueryable().FirstOrDefault(i => i.BillNo == billNo);
            var checkDetail = CheckBillDetailRepository.GetQueryable().Where(c => c.BillNo == checkbm.BillNo &&
                                                                             c.ProductCode == c.RealProductCode &&
                                                                             c.Quantity != c.RealQuantity &&
                                                                             c.Status == "2");

            using (var scope = new TransactionScope())
            {
                try
                {
                    if (checkDetail.Count() > 0)
                    {
                        string billno = GenProfitLossBillNo(userName).ToString();
                        //添加损益主表
                        var pbm      = new ProfitLossBillMaster();
                        var employee = EmployeeRepository.GetQueryable().FirstOrDefault(i => i.UserName == userName);
                        if (employee != null)
                        {
                            pbm.BillNo          = billno;
                            pbm.BillDate        = DateTime.Now;
                            pbm.BillTypeCode    = "5002";
                            pbm.WarehouseCode   = checkbm.WarehouseCode;
                            pbm.OperatePersonID = employee.ID;
                            pbm.Status          = "1";
                            pbm.IsActive        = "1";
                            pbm.UpdateTime      = DateTime.Now;

                            ProfitLossBillMasterRepository.Add(pbm);
                            ProfitLossBillMasterRepository.SaveChanges();
                        }

                        //添加损益细表
                        foreach (var item in checkDetail.ToArray())
                        {
                            decimal differQuantity = item.RealQuantity - item.Quantity;        //损益数量
                            if (Locker.LockNoEmptyStorage(item.Storage, item.Product) != null) //锁库存
                            {
                                var pbd = new ProfitLossBillDetail();
                                pbd.BillNo      = billno;
                                pbd.CellCode    = item.CellCode;
                                pbd.StorageCode = item.StorageCode;
                                pbd.ProductCode = item.ProductCode;
                                pbd.UnitCode    = item.UnitCode;
                                pbd.Price       = item.Product != null ? item.Product.CostPrice : 0;
                                pbd.Quantity    = differQuantity;

                                if (differQuantity > 0)
                                {
                                    item.Storage.InFrozenQuantity += differQuantity;
                                }
                                else
                                {
                                    item.Storage.OutFrozenQuantity += Math.Abs(differQuantity);
                                }
                                ProfitLossBillDetailRepository.Add(pbd);
                                item.Storage.LockTag = string.Empty;
                                ProfitLossBillDetailRepository.SaveChanges();
                            }
                            scope.Complete();
                        }
                    }

                    var checkBillDetail = CheckBillDetailRepository.GetQueryable().Where(c => c.BillNo == checkbm.BillNo);//解锁盘点锁定
                    foreach (var item in checkBillDetail.ToArray())
                    {
                        item.Storage.IsLock = "0";
                    }
                    if (checkbm != null && checkbm.Status == "4")
                    {
                        checkbm.Status     = "5";
                        checkbm.VerifyDate = DateTime.Now;
                        checkbm.UpdateTime = DateTime.Now;
                        CheckBillMasterRepository.SaveChanges();
                        result = true;
                    }
                }
                catch (Exception e)
                {
                    errorInfo = "确认盘点损益失败!原因:" + e.Message;
                }
                scope.Complete();
            }
            return(result);
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --异动生成
        /// </summary>
        /// <param name="beginDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool ChangedAdd(string beginDate, string endDate, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse>      wareQuery     = WarehouseRepository.GetQueryable();
            IQueryable <Storage>        storageQuery  = StorageRepository.GetQueryable();
            IQueryable <InBillAllot>    inAllotQuery  = InBillAllotRepository.GetQueryable();
            IQueryable <OutBillAllot>   outAllotQuery = OutBillAllotRepository.GetQueryable();
            IQueryable <MoveBillDetail> moveBillQuery = MoveBillDetailRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                if (beginDate == string.Empty || beginDate == null)
                {
                    beginDate = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
                }
                if (endDate == string.Empty || endDate == null)
                {
                    endDate = DateTime.Now.ToString("yyyy-MM-dd");
                }
                DateTime begin = Convert.ToDateTime(beginDate);
                DateTime end   = Convert.ToDateTime(endDate);
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        Console.WriteLine("start");
                        var sw = new Stopwatch();
                        sw.Start();

                        #region 循环所有仓库的订单,一个仓库一个盘点单据
                        var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);
                        foreach (var item in warehouses.ToArray())
                        {
                            var inCells      = inAllotQuery.Where(i => i.FinishTime >= begin && i.FinishTime <= end && i.Cell.WarehouseCode == item.WarehouseCode).OrderBy(i => i.CellCode).Select(i => i.CellCode);
                            var outCells     = outAllotQuery.Where(o => o.FinishTime >= begin && o.FinishTime <= end && o.Cell.WarehouseCode == item.WarehouseCode).OrderBy(o => o.CellCode).Select(o => o.CellCode);
                            var moveInCells  = moveBillQuery.Where(m => m.FinishTime >= begin && m.FinishTime <= end && m.InCell.WarehouseCode == item.WarehouseCode).OrderBy(m => m.InCell.CellCode).Select(m => m.InCell.CellCode);
                            var moveOutCells = moveBillQuery.Where(m => m.FinishTime >= begin && m.FinishTime <= end && m.OutCell.WarehouseCode == item.WarehouseCode).OrderBy(m => m.OutCell.CellCode).Select(m => m.OutCell.CellCode);
                            var storages     = storageQuery.Where(s => s.Quantity > 0 && s.IsLock == "0" && (inCells.Any(i => i == s.CellCode) || outCells.Any(o => o == s.CellCode) || moveInCells.Any(mi => mi == s.CellCode) || moveOutCells.Any(mo => mo == s.CellCode)))
                                               .OrderBy(s => s.ProductCode)
                                               .Select(s => s).ToArray();
                            if (storages.Any())
                            {
                                string billNo = GetCheckBillNo().ToString();
                                var    check  = new CheckBillMaster();
                                check.BillNo          = billNo;
                                check.BillDate        = DateTime.Now;
                                check.BillTypeCode    = billType;
                                check.WarehouseCode   = item.WarehouseCode;
                                check.OperatePersonID = employee.ID;
                                check.Status          = "1";
                                check.IsActive        = "1";
                                check.UpdateTime      = DateTime.Now;

                                CheckBillMasterRepository.Add(check);
                                storages.AsParallel().ForAll(stor =>
                                {
                                    var checkDetail             = new CheckBillDetail();
                                    checkDetail.CellCode        = stor.CellCode;
                                    checkDetail.StorageCode     = stor.StorageCode;
                                    checkDetail.ProductCode     = stor.ProductCode;
                                    checkDetail.UnitCode        = stor.Product.UnitCode;
                                    checkDetail.Quantity        = stor.Quantity;
                                    checkDetail.RealProductCode = stor.ProductCode;
                                    checkDetail.RealUnitCode    = stor.Product.UnitCode;
                                    checkDetail.RealQuantity    = stor.Quantity;
                                    checkDetail.Status          = "0";
                                    lock (check.CheckBillDetails)
                                    {
                                        check.CheckBillDetails.Add(checkDetail);
                                    }
                                    stor.IsLock = "1";
                                });
                                result = true;
                            }
                            else
                            {
                                info = "所选择查询的时间无数据!";
                            }
                        }

                        CheckBillMasterRepository.SaveChanges();
                        #endregion

                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);

                        sw.Restart();
                        CheckBillMasterRepository.SaveChanges();
                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);
                        scope.Complete();
                    }
                }
                catch (Exception e)
                {
                    result = false;
                    info   = e.Message;
                }
            }
            return(result);
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --产品生成
        /// </summary>
        /// <param name="products">产品数据</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool ProductAdd(string products, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse> wareQuery    = WarehouseRepository.GetQueryable();
            IQueryable <Storage>   storageQuery = StorageRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                if (products != null && products != string.Empty)
                {
                    products = products.Substring(0, products.Length - 1);
                    try
                    {
                        using (var scope = new TransactionScope())
                        {
                            Console.WriteLine("start");
                            var sw = new Stopwatch();
                            sw.Start();

                            #region products 这个有值,就把这个值里面所有的卷烟信息所在的仓库的货位的储存信息生成盘点单,一个仓库一个盘点单据
                            var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);
                            foreach (var item in warehouses.ToArray())
                            {
                                var storages = storageQuery.Where(s => s.Product != null && products.Contains(s.ProductCode) && s.Cell.WarehouseCode == item.WarehouseCode && s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode)
                                               .Select(s => s).ToArray();
                                if (storages.Any())
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    storages.AsParallel().ForAll(stor =>
                                    {
                                        var checkDetail             = new CheckBillDetail();
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.Product.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.Product.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity;
                                        checkDetail.Status          = "0";
                                        lock (check.CheckBillDetails)
                                        {
                                            check.CheckBillDetails.Add(checkDetail);
                                        }
                                        stor.IsLock = "1";
                                    });
                                    result = true;
                                }
                                else
                                {
                                    info = "所选择的产品无数据!";
                                }
                            }
                            #endregion

                            sw.Stop();
                            Console.WriteLine(sw.ElapsedMilliseconds);

                            sw.Restart();
                            CheckBillMasterRepository.SaveChanges();
                            sw.Stop();
                            Console.WriteLine(sw.ElapsedMilliseconds);
                            scope.Complete();
                        }
                    }
                    catch (Exception e)
                    {
                        result = false;
                        info   = e.Message;
                    }
                }
                else
                {
                    info = "产品信息为空!无法生成!";
                }
            }
            else
            {
                info = "当前登陆用户为空!请重新登陆!";
            }
            return(result);
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --货位生成
        /// </summary>
        /// <param name="ware">仓库</param>
        /// <param name="area">库区</param>
        /// <param name="shelf">货架</param>
        /// <param name="cell">货位</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool CellAdd(string ware, string area, string shelf, string cell, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse> wareQuery    = WarehouseRepository.GetQueryable();
            IQueryable <Storage>   storageQuery = StorageRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        Console.WriteLine("start");
                        var sw = new Stopwatch();
                        sw.Start();

                        #region ware 这个有值,就把这个值里面所有的仓库的货位的储存信息生成盘点单,一个仓库一个盘点单据

                        if (ware != null && ware != string.Empty)
                        {
                            ware = ware.Substring(0, ware.Length - 1);
                            var wares = wareQuery.Where(w => ware.Contains(w.WarehouseCode)).ToArray();

                            foreach (var item in wares)
                            {
                                var storages = storageQuery.Where(s => s.Cell.WarehouseCode == item.WarehouseCode &&
                                                                  s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode)
                                               .Select(s => s).ToArray();
                                if (storages.Any())
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    storages.AsParallel().ForAll(stor =>
                                    {
                                        var checkDetail             = new CheckBillDetail();
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.Product.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.Product.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity;
                                        checkDetail.Status          = "0";
                                        lock (check.CheckBillDetails)
                                        {
                                            check.CheckBillDetails.Add(checkDetail);
                                        }
                                        stor.IsLock = "1";
                                    });
                                    result = true;
                                }
                                else
                                {
                                    info = "所选择的货位无数据!";
                                }
                            }
                        }
                        #endregion

                        #region ware 这个无值,把全部仓库里面包含area,shelf,cell的数据生成盘点单,一个仓库一个盘点单据

                        if (area != null && area != string.Empty || shelf != null && shelf != string.Empty || cell != null && cell != string.Empty)
                        {
                            if (area != string.Empty)
                            {
                                area = area.Substring(0, area.Length - 1);
                            }
                            if (shelf != string.Empty)
                            {
                                shelf = shelf.Substring(0, shelf.Length - 1);
                            }
                            if (cell != string.Empty)
                            {
                                cell = cell.Substring(0, cell.Length - 1);
                            }

                            var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);

                            foreach (var item in warehouses.ToArray())
                            {
                                var storages = storageQuery.Where(s => s.Cell.WarehouseCode == item.WarehouseCode &&
                                                                  (area.Contains(s.Cell.Shelf.Area.AreaCode) ||
                                                                   shelf.Contains(s.Cell.Shelf.ShelfCode) ||
                                                                   cell.Contains(s.Cell.CellCode)) &&
                                                                  s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode)
                                               .Select(s => s).ToArray();
                                if (storages.Any())
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    storages.AsParallel().ForAll(stor =>
                                    {
                                        var checkDetail             = new CheckBillDetail();
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.Product.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.Product.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity;
                                        checkDetail.Status          = "0";
                                        lock (check.CheckBillDetails)
                                        {
                                            check.CheckBillDetails.Add(checkDetail);
                                        }
                                        stor.IsLock = "1";
                                    });
                                    result = true;
                                }
                            }
                        }
                        #endregion

                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);

                        sw.Restart();
                        CheckBillMasterRepository.SaveChanges();
                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);
                        scope.Complete();
                    }
                }
                catch (Exception e)
                {
                    result = false;
                    info   = e.Message;
                }
            }
            else
            {
                info = "当前登陆用户为空!请重新登陆!";
            }
            return(result);
        }
        public object GetDetails(int page, int rows, string BillNo, string beginDate, string endDate, string OperatePersonCode, string CheckPersonCode, string Status, string IsActive)
        {
            IQueryable <CheckBillMaster> CheckBillMasterQuery = CheckBillMasterRepository.GetQueryable();
            //var checkBillMasters = CheckBillMasterQuery.Where(i => i.BillNo.Contains(BillNo) && i.OperatePerson.EmployeeName.Contains(OperatePersonCode));
            var checkBillMasters = CheckBillMasterQuery.Where(i => i.BillNo.Contains(BillNo) &&
                                                              i.Status != "5")
                                   .OrderByDescending(t => t.BillDate)
                                   .OrderByDescending(t => t.BillNo)
                                   .Select(i => i);

            if (!BillNo.Equals(string.Empty) && BillNo != null)
            {
                checkBillMasters = checkBillMasters.Where(i => i.BillNo.Contains(BillNo));
            }
            if (!beginDate.Equals(string.Empty))
            {
                DateTime begin = Convert.ToDateTime(beginDate);
                checkBillMasters = checkBillMasters.Where(i => Convert.ToDateTime(i.BillDate) >= begin);
            }
            if (!endDate.Equals(string.Empty))
            {
                DateTime end = Convert.ToDateTime(endDate);
                checkBillMasters = checkBillMasters.Where(i => Convert.ToDateTime(i.BillDate) <= end);
            }
            if (!OperatePersonCode.Equals(string.Empty) && OperatePersonCode != null)
            {
                checkBillMasters = checkBillMasters.Where(i => i.OperatePerson.EmployeeCode.Contains(OperatePersonCode));
            }
            if (!Status.Equals(string.Empty))
            {
                checkBillMasters = checkBillMasters.Where(i => i.Status.Contains(Status));
            }
            if (!IsActive.Equals(string.Empty))
            {
                checkBillMasters = checkBillMasters.Where(i => i.IsActive.Contains(IsActive));
            }
            if (!CheckPersonCode.Equals(string.Empty))
            {
                checkBillMasters = checkBillMasters.Where(i => i.VerifyPerson.EmployeeCode == CheckPersonCode);
            }

            int total = checkBillMasters.Count();

            checkBillMasters = checkBillMasters.Skip((page - 1) * rows).Take(rows);

            var temp = checkBillMasters.ToArray().AsEnumerable().Select(i => new
            {
                i.BillNo,
                BillDate = i.BillDate.ToString("yyyy-MM-dd hh:mm:ss"),
                i.Warehouse.WarehouseCode,
                i.Warehouse.WarehouseName,
                OperatePersonCode = i.OperatePerson.EmployeeCode,
                OperatePersonName = i.OperatePerson.EmployeeName,
                VerifyPersonCode  = i.VerifyPersonID == null ? string.Empty : i.VerifyPerson.EmployeeCode,
                VerifyPersonName  = i.VerifyPersonID == null ? string.Empty : i.VerifyPerson.EmployeeName,
                BillTypeCode      = i.BillType.BillTypeCode,
                BillTypeName      = i.BillType.BillTypeName,
                VerifyDate        = i.VerifyDate == null ? string.Empty : ((DateTime)i.VerifyDate).ToString("yyyy-MM-dd hh:mm:ss"),
                Status            = WhatStatus(i.Status),
                IsActive          = i.IsActive == "1" ? "可用" : "不可用",
                Description       = i.Description,
                UpdateTime        = i.UpdateTime.ToString("yyyy-MM-dd hh:mm:ss")
            });

            return(new { total, rows = temp.ToArray() });
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --产品生成
        /// </summary>
        /// <param name="products">产品数据</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool ProductAdd(string products, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse> wareQuery    = WarehouseRepository.GetQueryable();
            IQueryable <Storage>   storageQuery = StorageRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                if (products != null && products != string.Empty)
                {
                    products = products.Substring(0, products.Length - 1);
                    try
                    {
                        using (var scope = new TransactionScope())
                        {
                            #region products 这个有值,就把这个值里面所有的卷烟信息所在的仓库的货位的储存信息生成盘点单,一个仓库一个盘点单据
                            var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);
                            foreach (var item in warehouses.ToArray())
                            {
                                var storages = storageQuery.Where(s => s.ProductCode != null && products.Contains(s.ProductCode) && s.Cell.Shelf.Area.Warehouse.WarehouseCode == item.WarehouseCode && s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode).AsEnumerable()
                                               .Select(s => new
                                {
                                    s.StorageCode,
                                    s.Cell.CellCode,
                                    s.Cell.CellName,
                                    s.Product.ProductCode,
                                    s.Product.ProductName,
                                    s.Product.Unit.UnitCode,
                                    s.Product.Unit.UnitName,
                                    Quantity = s.Quantity / s.Product.Unit.Count,
                                    s.Product.Unit.Count,
                                    IsActive    = s.IsActive == "1" ? "可用" : "不可用",
                                    StorageTime = s.StorageTime.ToString("yyyy-MM-dd"),
                                    UpdateTime  = s.UpdateTime.ToString("yyyy-MM-dd")
                                });
                                if (storages.Count() > 0)
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    CheckBillMasterRepository.SaveChanges();

                                    foreach (var stor in storages.ToArray())
                                    {
                                        var checkDetail = new CheckBillDetail();
                                        checkDetail.BillNo          = billNo;
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity * stor.Count;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity * stor.Count;
                                        checkDetail.Status          = "1";
                                        CheckBillDetailRepository.Add(checkDetail);
                                        CheckBillDetailRepository.SaveChanges();

                                        var storage = storageQuery.FirstOrDefault(s => s.StorageCode == stor.StorageCode);
                                        storage.IsLock = "1";
                                        StorageRepository.SaveChanges();
                                    }
                                    result = true;
                                }
                                else
                                {
                                    info = "所选择的产品无数据!";
                                }
                            }
                            #endregion
                            scope.Complete();
                        }
                    }
                    catch (Exception e)
                    {
                        info = e.Message;
                    }
                }
                else
                {
                    info = "产品信息为空!无法生成!";
                }
            }
            else
            {
                info = "当前登陆用户为空!请重新登陆!";
            }
            return(result);
        }
        /// <summary>
        /// 根据参数生成并保存盘点数据  --货位生成
        /// </summary>
        /// <param name="ware">仓库</param>
        /// <param name="area">库区</param>
        /// <param name="shelf">货架</param>
        /// <param name="cell">货位</param>
        /// <param name="UserName">登陆用户</param>
        /// <returns></returns>
        public bool CellAdd(string ware, string area, string shelf, string cell, string UserName, string billType, out string info)
        {
            bool result = false;

            info = string.Empty;
            IQueryable <Warehouse> wareQuery    = WarehouseRepository.GetQueryable();
            IQueryable <Storage>   storageQuery = StorageRepository.GetQueryable();
            var employee = EmployeeRepository.GetQueryable().FirstOrDefault(e => e.UserName == UserName);

            if (employee != null)
            {
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        #region ware 这个有值,就把这个值里面所有的仓库的货位的储存信息生成盘点单,一个仓库一个盘点单据

                        if (ware != null && ware != string.Empty)
                        {
                            ware = ware.Substring(0, ware.Length - 1);
                            var wares = wareQuery.Where(w => ware.Contains(w.WarehouseCode));

                            foreach (var item in wares.ToArray())
                            {
                                var storages = storageQuery.Where(s => s.Cell.Shelf.Area.Warehouse.WarehouseCode == item.WarehouseCode && s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode).AsEnumerable()
                                               .Select(s => new
                                {
                                    s.StorageCode,
                                    s.Cell.CellCode,
                                    s.Cell.CellName,
                                    s.IsLock,
                                    s.Product.ProductCode,
                                    s.Product.ProductName,
                                    s.Product.Unit.UnitCode,
                                    s.Product.Unit.UnitName,
                                    Quantity = s.Quantity / s.Product.Unit.Count,
                                    s.Product.Unit.Count,
                                    IsActive    = s.IsActive == "1" ? "可用" : "不可用",
                                    StorageTime = s.StorageTime.ToString("yyyy-MM-dd"),
                                    UpdateTime  = s.UpdateTime.ToString("yyyy-MM-dd")
                                });
                                if (storages.Count() > 0)
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    CheckBillMasterRepository.SaveChanges();

                                    foreach (var stor in storages.ToArray())
                                    {
                                        var checkDetail = new CheckBillDetail();
                                        checkDetail.BillNo          = billNo;
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity * stor.Count;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity * stor.Count;
                                        checkDetail.Status          = "1";

                                        CheckBillDetailRepository.Add(checkDetail);
                                        CheckBillDetailRepository.SaveChanges();

                                        var storage = storageQuery.FirstOrDefault(s => s.StorageCode == stor.StorageCode);
                                        storage.IsLock = "1";
                                        StorageRepository.SaveChanges();
                                        scope.Complete();
                                    }
                                    result = true;
                                }
                                else
                                {
                                    info = "所选择的货位无数据!";
                                }
                            }
                        }
                        #endregion

                        #region ware 这个无值,把全部仓库里面包含area,shelf,cell的数据生成盘点单,一个仓库一个盘点单据

                        if (area != null && area != string.Empty || shelf != null && shelf != string.Empty || cell != null && cell != string.Empty)
                        {
                            if (area != string.Empty)
                            {
                                area = area.Substring(0, area.Length - 1);
                            }
                            if (shelf != string.Empty)
                            {
                                shelf = shelf.Substring(0, shelf.Length - 1);
                            }
                            if (cell != string.Empty)
                            {
                                cell = cell.Substring(0, cell.Length - 1);
                            }

                            var warehouses = wareQuery.OrderBy(w => w.WarehouseCode);

                            foreach (var item in warehouses.ToArray())
                            {
                                var storages = storageQuery.ToList().Where(s => s.Cell.Shelf.Area.Warehouse.WarehouseCode == item.WarehouseCode && (area.Contains(s.Cell.Shelf.Area.AreaCode) || shelf.Contains(s.Cell.Shelf.ShelfCode) || cell.Contains(s.Cell.CellCode)) && s.Quantity > 0 && s.IsLock == "0")
                                               .OrderBy(s => s.StorageCode).AsEnumerable()
                                               .Select(s => new
                                {
                                    s.StorageCode,
                                    s.Cell.CellCode,
                                    s.Cell.CellName,
                                    s.Product.ProductCode,
                                    s.Product.ProductName,
                                    s.Product.Unit.UnitCode,
                                    s.Product.Unit.UnitName,
                                    Quantity = s.Quantity / s.Product.Unit.Count,
                                    s.Product.Unit.Count,
                                    IsActive    = s.IsActive == "1" ? "可用" : "不可用",
                                    StorageTime = s.StorageTime.ToString("yyyy-MM-dd"),
                                    UpdateTime  = s.UpdateTime.ToString("yyyy-MM-dd")
                                });
                                if (storages.Count() > 0)
                                {
                                    string billNo = GetCheckBillNo().ToString();
                                    var    check  = new CheckBillMaster();
                                    check.BillNo          = billNo;
                                    check.BillDate        = DateTime.Now;
                                    check.BillTypeCode    = billType;
                                    check.WarehouseCode   = item.WarehouseCode;
                                    check.OperatePersonID = employee.ID;
                                    check.Status          = "1";
                                    check.IsActive        = "1";
                                    check.UpdateTime      = DateTime.Now;

                                    CheckBillMasterRepository.Add(check);
                                    CheckBillMasterRepository.SaveChanges();

                                    foreach (var stor in storages.ToArray())
                                    {
                                        var checkDetail = new CheckBillDetail();
                                        checkDetail.BillNo          = billNo;
                                        checkDetail.CellCode        = stor.CellCode;
                                        checkDetail.StorageCode     = stor.StorageCode;
                                        checkDetail.ProductCode     = stor.ProductCode;
                                        checkDetail.UnitCode        = stor.UnitCode;
                                        checkDetail.Quantity        = stor.Quantity * stor.Count;
                                        checkDetail.RealProductCode = stor.ProductCode;
                                        checkDetail.RealUnitCode    = stor.UnitCode;
                                        checkDetail.RealQuantity    = stor.Quantity * stor.Count;
                                        checkDetail.Status          = "1";
                                        CheckBillDetailRepository.Add(checkDetail);
                                        CheckBillDetailRepository.SaveChanges();

                                        var storage = storageQuery.FirstOrDefault(s => s.StorageCode == stor.StorageCode);
                                        storage.IsLock = "1";
                                        StorageRepository.SaveChanges();
                                        scope.Complete();
                                    }
                                    result = true;
                                }
                            }
                        }
                        #endregion

                        scope.Complete();
                    }
                }
                catch (Exception e)
                {
                    info = e.Message;
                }
            }
            else
            {
                info = "当前登陆用户为空!请重新登陆!";
            }
            return(result);
        }
        public bool Add(DateTime datetime, out string strResult)
        {
            bool result = false;

            strResult = string.Empty;

            var checkBillMaster = CheckBillMasterRepository.GetQueryable().Where(i => i.BillDate <= datetime);
            var checkBillDetail = CheckBillDetailRepository.GetQueryable().Where(i => i.CheckBillMaster.BillDate <= datetime);

            if (checkBillMaster.Any())
            {
                #region 主表移入历史表
                try
                {
                    foreach (var item in checkBillMaster.ToArray())
                    {
                        CheckBillMasterHistory history = new CheckBillMasterHistory();

                        history.BillNo          = item.BillNo;
                        history.BillDate        = item.BillDate;
                        history.BillTypeCode    = item.BillTypeCode;
                        history.WarehouseCode   = item.WarehouseCode;
                        history.OperatePersonID = item.OperatePersonID;
                        history.Status          = item.Status;
                        history.VerifyPersonID  = item.VerifyPersonID;
                        history.VerifyDate      = item.VerifyDate;
                        history.Description     = item.Description;
                        history.IsActive        = item.IsActive;
                        history.UpdateTime      = item.UpdateTime;

                        CheckBillMasterHistoryRepository.Add(history);
                    }
                    result = true;
                }
                catch (Exception e)
                {
                    strResult = "主库单:" + e.InnerException.ToString();
                    result    = false;
                }
                #endregion

                if (checkBillDetail.Any())
                {
                    #region 细表移入历史表
                    try
                    {
                        foreach (var item in checkBillDetail.ToArray())
                        {
                            CheckBillDetailHistory history = new CheckBillDetailHistory();
                            history.BillNo          = item.BillNo;
                            history.CellCode        = item.CellCode;
                            history.StorageCode     = item.StorageCode;
                            history.ProductCode     = item.ProductCode;
                            history.UnitCode        = item.UnitCode;
                            history.Quantity        = item.Quantity;
                            history.RealProductCode = item.ProductCode;
                            history.RealUnitCode    = item.RealUnitCode;
                            history.RealQuantity    = item.Quantity;
                            history.Status          = item.Status;

                            CheckBillDetailHistoryRepository.Add(history);
                        }
                        result = true;
                    }
                    catch (Exception e)
                    {
                        strResult = "细库单:" + e.InnerException.ToString();;
                    }
                    #endregion
                }
                if (result == true)
                {
                    #region  除主细分配表
                    try
                    {
                        foreach (var item in checkBillMaster.ToList())
                        {
                            Del(CheckBillDetailRepository, item.CheckBillDetails);
                            CheckBillMasterRepository.Delete(item);
                            result = true;
                        }
                    }
                    catch (Exception e)
                    {
                        strResult = "删除情况:" + e.InnerException.ToString();
                    }
                    CheckBillMasterHistoryRepository.SaveChanges();
                    #endregion
                }
            }
            else
            {
                strResult = "数据不存在!";
            }
            return(result);
        }