Exemple #1
0
        /// <summary>
        /// Add multiple stock detail records
        /// </summary>
        /// <param name="stock"></param>
        /// <returns></returns>
        public bool CreateEnterStockDetails(Enter_Stock dbstock,List<BEnterStockDetail> details,bool updateStock=false)
        {
            bool result = false;
            if (this.CurrentUserPermission.ADD_ENTER_STOCK == 0)
            {
                throw new KMJXCException("没有新增入库单产品信息的权限");
            }

            if (dbstock.Enter_Stock_ID <= 0)
            {
                throw new KMJXCException("");
            }

            if (details == null)
            {
                throw new KMJXCException("输入错误",ExceptionLevel.SYSTEM);
            }

            KuanMaiEntities db = new KuanMaiEntities();
            List<Stock_Pile> stockPiles = (from sp in db.Stock_Pile where sp.Shop_ID == dbstock.Shop_ID select sp).ToList<Stock_Pile>();

            int totalQuantity = 0;

            List<Stock_Batch> batches=(from sb in db.Stock_Batch where sb.ShopID==dbstock.Shop_ID select sb ).ToList<Stock_Batch>();

            foreach (BEnterStockDetail detail in details)
            {
                Product tmp = (from p in db.Product where p.Product_ID == detail.Product.ID select p).FirstOrDefault<Product>();
                if (tmp == null)
                {
                    continue;
                }

                Enter_Stock_Detail dbDetail = new Enter_Stock_Detail();
                dbDetail.Create_Date = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                dbDetail.Enter_Stock_ID = dbstock.Enter_Stock_ID;
                dbDetail.Have_Invoice = detail.Invoiced;
                dbDetail.Invoice_Amount = detail.InvoiceAmount;
                dbDetail.Invoice_Num = detail.InvoiceNumber;
                dbDetail.Price = detail.Price;
                dbDetail.Product_ID = detail.Product.ID;
                dbDetail.Quantity = (int)detail.Quantity;

                totalQuantity += dbDetail.Quantity;
                int parentProductId = dbDetail.Product_ID;
                if (tmp.Parent_ID > 0)
                {
                    parentProductId = tmp.Parent_ID;
                }
                dbDetail.Parent_Product_ID = parentProductId;

                List<Stock_Batch> pBatches = (from b in batches where b.ProductID == parentProductId select b).ToList<Stock_Batch>();
                Stock_Batch batch = (from b in pBatches where b.Price==dbDetail.Price select b).FirstOrDefault<Stock_Batch>();
                if (batch == null)
                {
                    if (pBatches.Count == 1 && pBatches[0].Price == 0)
                    {
                        batch = pBatches[0];
                        batch.Price = dbDetail.Price;
                        batch.Name = "P" + batch.Price.ToString("0.00");
                    }
                    else
                    {
                        batch = new Stock_Batch() { ProductID = parentProductId, ParentProductID = 0, Price = dbDetail.Price, ShopID = dbstock.Shop_ID, Desc = "" };
                        batch.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        batch.Created_By = this.CurrentUser.ID;
                        batch.Name = "P" + batch.Price.ToString("0.00");
                        db.Stock_Batch.Add(batch);
                        db.SaveChanges();
                    }
                }

                dbDetail.Batch_ID = batch.ID;
                db.Enter_Stock_Detail.Add(dbDetail);

                if (updateStock)
                {
                    //update stock pile
                    Stock_Pile zeroPile = (from sp in stockPiles where sp.Product_ID == dbDetail.Product_ID && sp.StockHouse_ID == dbstock.StoreHouse_ID && sp.Quantity==0 select sp).FirstOrDefault<Stock_Pile>();
                    if (zeroPile != null && zeroPile.Batch_ID!=batch.ID)
                    {
                        db.Stock_Pile.Remove(zeroPile);
                        db.SaveChanges();
                    }

                    Stock_Pile stockPile = (from sp in stockPiles where sp.Product_ID == dbDetail.Product_ID && sp.StockHouse_ID == dbstock.StoreHouse_ID && sp.Batch_ID==dbDetail.Batch_ID select sp).FirstOrDefault<Stock_Pile>();
                    if (stockPile == null)
                    {
                        stockPile = new Stock_Pile();
                        stockPile.Product_ID = dbDetail.Product_ID;
                        stockPile.Shop_ID = this.Shop.Shop_ID;
                        stockPile.StockHouse_ID = dbstock.StoreHouse_ID;
                        stockPile.Quantity = dbDetail.Quantity;
                        stockPile.Price = dbDetail.Price;
                        stockPile.Batch_ID = dbDetail.Batch_ID;
                        stockPile.First_Enter_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        db.Stock_Pile.Add(stockPile);
                    }
                    else
                    {
                        stockPile.Quantity = stockPile.Quantity + dbDetail.Quantity;
                        stockPile.Price = dbDetail.Price;
                    }
                    Product product = null;

                    if (tmp.Parent_ID > 0)
                    {
                        product = (from p in db.Product
                                   join p1 in db.Product on p.Product_ID equals p1.Parent_ID
                                   where p1.Product_ID == dbDetail.Product_ID
                                   select p).FirstOrDefault<Product>();
                    }
                    else if (tmp.Parent_ID == 0)
                    {
                        product = tmp;
                    }

                    if (product != null)
                    {
                        product.Quantity += dbDetail.Quantity;
                    }
                }
            }

            try
            {
                db.SaveChanges();
                result = true;
            }
            catch(Exception ex)
            {
                throw new KMJXCException(ex.Message, ExceptionLevel.SYSTEM);
            }
            finally
            {
                db.Dispose();
            }

            return result;
        }
Exemple #2
0
        /// <summary>
        /// Add new enter stock record
        /// </summary>
        /// <param name="stock">Instance of Enter_Stock object</param>
        /// <returns></returns>
        public bool CreateEnterStock(BEnterStock stock)
        {
            bool result = false;
            if (stock == null)
            {
                return result;
            }

            if (stock.BuyID <= 0) {
                throw new KMJXCException("入库单未包含验货单信息");
            }

            if (stock.Shop==null)
            {
                stock.Shop = new BShop() { ID = this.Shop_Id, Title=this.Shop.Name };
            }

            if (stock.StoreHouse ==null)
            {
                throw new KMJXCException("入库单未包含仓库信息");
            }

            if (stock.Created_By == null)
            {
                stock.Created_By = this.CurrentUser;
            }

            if (this.CurrentUserPermission.ADD_ENTER_STOCK == 0)
            {
                throw new KMJXCException("没有新增入库单的权限");
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                //update buy
                Buy dbBuy = (from buy in db.Buy where buy.Buy_ID == stock.BuyID select buy).FirstOrDefault<Buy>();
                if (dbBuy == null)
                {
                    throw new KMJXCException("编号为:"+stock.BuyID+" 的验货单没有找到");
                }

                if (dbBuy.Shop_ID != this.Shop.Shop_ID)
                {
                    throw new KMJXCException("编号为:" + stock.BuyID + " 为别的店铺的验货单,您不能操作,请不要再次尝试");
                }

                if (dbBuy.Status == 1)
                {
                    throw new KMJXCException("编号为:" + stock.BuyID + " 的验货单已经入库,不能再次入库");
                }
                Enter_Stock dbStock = new Enter_Stock();

                dbStock.Buy_ID = stock.BuyID;
                dbStock.Enter_Date = stock.Created;
                dbStock.Enter_Stock_ID = 0;
                dbStock.Shop_ID = this.Shop.Shop_ID;
                dbStock.StoreHouse_ID = stock.StoreHouse.ID;
                dbStock.User_ID = stock.Created_By.ID;
                dbStock.Status = 0;
                db.Enter_Stock.Add(dbStock);
                db.SaveChanges();
                if (dbStock.Enter_Stock_ID <= 0)
                {
                    throw new KMJXCException("入库单创建失败");
                }
                result = true;

                if (stock.Details == null || stock.Details.Count == 0)
                {
                    stock.Details = (from d in db.Buy_Detail
                                     where d.Buy_ID == stock.BuyID
                                     select new BEnterStockDetail
                                     {
                                         Price = d.Price,
                                         Product = new BProduct() { ID=d.Product_ID },
                                         Quantity=d.Quantity
                                     }).ToList<BEnterStockDetail>();
                }

                result = result & this.CreateEnterStockDetails(dbStock, stock.Details, stock.UpdateStock);

                if (result)
                {
                    if (stock.UpdateStock)
                    {
                        dbStock.Status = 1;
                    }

                    if (dbBuy != null)
                    {
                        base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = dbBuy.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_ENTER_STOCK }, Description = "" });
                        dbBuy.Status = 1;
                        db.SaveChanges();
                    }
                }
            }

            return result;
        }