Пример #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="trade_id"></param>
        /// <param name="order_id"></param>
        /// <param name="mall_product_id"></param>
        /// <param name="mall_sku_id"></param>
        /// <param name="parent_product_id"></param>
        /// <param name="product_id"></param>
        /// <param name="mapproduct"></param>
        public void CreateLeaveStockForMallTrade(string trade_id, string order_id, string mall_product_id, string mall_sku_id, int parent_product_id, int product_id, bool mapproduct = false)
        {
            if (string.IsNullOrEmpty(mall_product_id))
            {
                throw new KMJXCException("丢失商城宝贝编号");
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                Sale trade = (from t in db.Sale where t.Mall_Trade_ID == trade_id select t).FirstOrDefault<Sale>();
                if (trade == null)
                {
                    throw new KMJXCException("交易号为:" + trade_id + " 的交易不存在");
                }

                Sale_Detail order = (from sd in db.Sale_Detail where sd.Mall_Order_ID == order_id && sd.Mall_Trade_ID == trade_id select sd).FirstOrDefault<Sale_Detail>();
                if (order == null)
                {
                    throw new KMJXCException("交易号为:" + trade_id + " 的交易不存在");
                }

                Product dbproduct=(from p in db.Product where p.Product_ID==parent_product_id select p).FirstOrDefault<Product>();
                if (dbproduct == null)
                {
                    throw new KMJXCException("进销存产品编号为:" + parent_product_id + " 的产品不存在");
                }

                order.Parent_Product_ID = parent_product_id;
                order.Product_ID = parent_product_id;

                if (!string.IsNullOrEmpty(mall_sku_id))
                {
                    if (product_id <= 0)
                    {
                        throw new KMJXCException("交易产品有SKU属性,更新库存时必须选择进销存产品对应的销售属性");
                    }

                    Product childproduct = (from p in db.Product where p.Product_ID == product_id select p).FirstOrDefault<Product>();
                    if (childproduct == null)
                    {
                        throw new KMJXCException("进销存产品编号为:" + parent_product_id + ", 库存编号为:"+product_id+" 的产品不存在");
                    }

                    order.Product_ID = product_id;
                }

                Leave_Stock leaveStock = (from ls in db.Leave_Stock where ls.Sale_ID == trade_id select ls).FirstOrDefault<Leave_Stock>();
                if (leaveStock == null)
                {
                    leaveStock = new Leave_Stock();
                    leaveStock.Sale_ID = trade_id;
                    leaveStock.Leave_Date = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    leaveStock.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    leaveStock.Shop_ID = trade.Shop_ID;
                    leaveStock.Status = 0;
                    leaveStock.User_ID = this.CurrentUser.ID;
                    db.Leave_Stock.Add(leaveStock);
                }

                Leave_Stock_Detail lsDetail = (from lsd in db.Leave_Stock_Detail where lsd.Order_ID == order_id select lsd).FirstOrDefault<Leave_Stock_Detail>();
                if (lsDetail != null)
                {
                    throw new KMJXCException("交易号为:" + trade_id + ",订单号为:" + order_id + " 的出库记录已经存在,不能重复出库");
                }
                int[] csp_ids = (from child in this.DBChildShops select child.Shop_ID).ToArray<int>();
                Store_House house = (from store in db.Store_House where store.Default == true && (store.Shop_ID == trade.Shop_ID || store.Shop_ID == this.Main_Shop.Shop_ID || csp_ids.Contains(store.Shop_ID)) select store).FirstOrDefault<Store_House>();
                int stockPileProductId = parent_product_id;
                if (product_id > 0)
                {
                    stockPileProductId = product_id;
                }

                Stock_Pile stockPile = null;
                lsDetail = new Leave_Stock_Detail();
                if (house != null)
                {
                    stockPile = (from sp in db.Stock_Pile where sp.Product_ID == stockPileProductId && sp.StockHouse_ID == house.StoreHouse_ID && sp.Quantity >= order.Quantity select sp).OrderBy(s=>s.Batch_ID).FirstOrDefault<Stock_Pile>();
                }

                if (stockPile == null)
                {
                    //get store house when it has the specific product
                    var tmpstockPile = from sp in db.Stock_Pile where sp.Product_ID == stockPileProductId && sp.Quantity >= order.Quantity select sp;
                    if (tmpstockPile.Count() > 0)
                    {
                        stockPile = tmpstockPile.OrderBy(s => s.Batch_ID).ToList<Stock_Pile>()[0];
                        Store_House tmpHouse = (from h in db.Store_House where h.StoreHouse_ID == stockPile.StockHouse_ID select h).FirstOrDefault<Store_House>();
                        if (tmpHouse != null)
                        {
                            house = tmpHouse;
                        }
                    }
                    else
                    {
                        //cannot leave stock, no stock pile
                        order.Status1 = (int)SaleDetailStatus.NO_ENOUGH_STOCK;
                        order.SyncResultMessage = "没有足够的库存,不能出库";
                    }
                }

                //no stock cannot leave stock
                if (stockPile != null)
                {
                    db.SaveChanges();
                    order.Status1 = (int)SaleDetailStatus.LEAVED_STOCK;
                    order.SyncResultMessage = "出库仓库:" + house.Title;
                    lsDetail.Leave_Stock_ID = leaveStock.Leave_Stock_ID;
                    lsDetail.Quantity = order.Quantity;
                    lsDetail.Price = order.Price;
                    lsDetail.StoreHouse_ID = house.StoreHouse_ID;
                    lsDetail.Order_ID = order_id;
                    lsDetail.Amount = (double)order.Amount;
                    lsDetail.Parent_Product_ID = parent_product_id;
                    lsDetail.Product_ID = product_id;
                    if (string.IsNullOrEmpty(mall_sku_id))
                    {
                        lsDetail.Product_ID = parent_product_id;
                    }

                    lsDetail.StoreHouse_ID = stockPile.StockHouse_ID;
                    lsDetail.Batch_ID = stockPile.Batch_ID;
                    //Update stock
                    stockPile.Quantity = stockPile.Quantity - order.Quantity;

                    //Update stock field in Product table
                    Product product = (from pdt in db.Product where pdt.Product_ID == lsDetail.Parent_Product_ID select pdt).FirstOrDefault<Product>();
                    if (product != null)
                    {
                        product.Quantity = product.Quantity - order.Quantity;
                    }
                    lsDetail.Order_ID = order_id;

                    db.Leave_Stock_Detail.Add(lsDetail);
                    base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = leaveStock.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_LEAVE_STOCK }, Description = "同步订单时未能成功自动出库更新库存,手动出库并更新库存" });
                    db.SaveChanges();
                }

                if (mapproduct)
                {
                    Mall_Product mallProduct=(from mp in db.Mall_Product where mp.Mall_ID==mall_product_id select mp).FirstOrDefault<Mall_Product>();
                    if (mallProduct != null)
                    {

                        ShopManager shopManager = new ShopManager(this.CurrentUser, this.Shop, this.CurrentUserPermission);
                        if (shopManager.MapMallProduct(mall_product_id, parent_product_id))
                        {
                            mallProduct.Outer_ID = order.Parent_Product_ID;
                            if (!string.IsNullOrEmpty(mall_sku_id) && product_id>0)
                            {
                                Mall_Product_Sku sku = (from sk in db.Mall_Product_Sku where sk.Mall_ID == mallProduct.Mall_ID && sk.SKU_ID == mall_sku_id select sk).FirstOrDefault<Mall_Product_Sku>();

                                if (shopManager.MapSku(mall_sku_id, product_id))
                                {
                                    sku.Outer_ID = order.Product_ID;
                                }
                            }
                        }
                    }

                    db.SaveChanges();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// The leave stocks won't be created while orders products are not mapped with local products
        /// </summary>
        /// <param name="trades"></param>
        private void CreateLeaveStocks(List<BSale> trades,Shop shop)
        {
            if (trades == null)
            {
                return;
            }
            if (shop == null)
            {
                return;
            }
            KuanMaiEntities db = new KuanMaiEntities();

            int[] csp_ids = (from child in this.DBChildShops select child.Shop_ID).ToArray<int>();
            if (csp_ids == null)
            {
                csp_ids = new int[1];
            }

            List<Product> allproducts = (from pdt in db.Product where (pdt.Shop_ID == shop.Shop_ID || pdt.Shop_ID == this.Main_Shop.Shop_ID || csp_ids.Contains(pdt.Shop_ID)) select pdt).ToList<Product>();
            string[] sale_ids=(from sale in trades select sale.Sale_ID).ToArray<string>();
            List<Leave_Stock> cacheLeaveStocks=(from ls in db.Leave_Stock where sale_ids.Contains(ls.Sale_ID) select ls).ToList<Leave_Stock>();
            Store_House house = (from store in db.Store_House where store.Default == true && (store.Shop_ID == shop.Shop_ID || store.Shop_ID == this.Main_Shop.Shop_ID || csp_ids.Contains(store.Shop_ID)) select store).FirstOrDefault<Store_House>();
            List<Store_House> houses = (from store in db.Store_House select store).ToList<Store_House>();
            List<Stock_Pile> stockPiles = (from sp in db.Stock_Pile where sp.Shop_ID == shop.Shop_ID || sp.Shop_ID == this.Main_Shop.Shop_ID || csp_ids.Contains(sp.Shop_ID) select sp).ToList<Stock_Pile>();
            List<Sale_Detail> tradeDetails=(from tradeDetail in db.Sale_Detail where sale_ids.Contains(tradeDetail.Mall_Trade_ID) select tradeDetail).ToList<Sale_Detail>();
            List<Stock_Batch> batches=(from b in db.Stock_Batch where b.ShopID==shop.Shop_ID select b).ToList<Stock_Batch>();
            try
            {
                foreach (BSale trade in trades)
                {
                    bool isNew = false;
                    Leave_Stock dbStock = null;

                    dbStock=(from cstock in cacheLeaveStocks where cstock.Sale_ID==trade.Sale_ID select cstock).FirstOrDefault<Leave_Stock>();
                    if (dbStock == null)
                    {
                        isNew = true;
                        dbStock = new Leave_Stock();
                        dbStock.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        dbStock.Leave_Date = trade.Synced;
                        dbStock.Sale_ID = trade.Sale_ID;
                        dbStock.Shop_ID = shop.Shop_ID;
                        dbStock.User_ID = this.CurrentUser.ID;
                    }

                    if (trade.Orders != null)
                    {
                        List<Leave_Stock_Detail> dbLDetails = new List<Leave_Stock_Detail>();
                        #region handle sale orders
                        foreach (BOrder order in trade.Orders)
                        {
                            Leave_Stock_Detail dbDetail = new Leave_Stock_Detail();
                            int stockPileProductId = 0;

                            Sale_Detail order_detail = (from orderDetail in tradeDetails where orderDetail.Mall_Trade_ID == trade.Sale_ID && orderDetail.Mall_Order_ID == order.Order_ID select orderDetail).FirstOrDefault<Sale_Detail>();
                            if (order_detail == null)
                            {
                                continue;
                            }

                            order_detail.SyncResultMessage = "";
                            if (order.Refound)
                            {
                                order_detail.Status1 = (int)SaleDetailStatus.REFOUND_BEFORE_SEND;
                                order_detail.SyncResultMessage = "已经退货,不需要出库";
                                db.SaveChanges();
                                continue;
                            }

                            Product parentPdt = (from pdt in allproducts where pdt.Product_ID == order.Parent_Product_ID select pdt).FirstOrDefault<Product>();
                            Product childPdt = (from pdt in allproducts where pdt.Product_ID == order.Product_ID select pdt).FirstOrDefault<Product>();

                            order.Parent_Product_ID = 0;
                            if (parentPdt != null)
                            {
                                order.Parent_Product_ID = parentPdt.Product_ID;
                                if (childPdt != null)
                                {
                                    order.Product_ID = childPdt.Product_ID;
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(order.Mall_SkuID))
                                    {
                                        order_detail.Status1 = (int)SaleDetailStatus.NOT_CONNECTED;
                                        order_detail.SyncResultMessage = "SKU未关联,不能更新库存";
                                        db.SaveChanges();
                                        continue;
                                    }
                                }
                            }
                            else
                            {
                                if (childPdt != null)
                                {
                                    order.Parent_Product_ID = childPdt.Parent_ID;
                                    order.Product_ID = childPdt.Product_ID;
                                }
                            }

                            //no need to create leave stock while the mall product is not mapped with local product
                            if (order.Product_ID == 0 && order.Parent_Product_ID == 0)
                            {
                                order_detail.Status1 = (int)SaleDetailStatus.NOT_CONNECTED;
                                order_detail.SyncResultMessage = "宝贝未关联,不能更新库存";
                                db.SaveChanges();
                                continue;
                            }

                            stockPileProductId = order.Product_ID;
                            if (stockPileProductId == 0 && string.IsNullOrEmpty(order.Mall_SkuID) && order.Parent_Product_ID>0)
                            {
                                stockPileProductId = order.Parent_Product_ID;
                            }

                            dbDetail.Leave_Stock_ID = dbStock.Leave_Stock_ID;
                            dbDetail.Price = order.Price;
                            dbDetail.Quantity = order.Quantity;
                            dbDetail.Amount = order.Amount;
                            Stock_Pile stockPile = null;
                            if (house != null)
                            {
                                //order_detail.SyncResultMessage = "默认仓库:" + house.Title;
                                //create leave stock from default store house
                                stockPile = (from sp in stockPiles where sp.Product_ID == stockPileProductId && sp.StockHouse_ID == house.StoreHouse_ID && sp.Quantity >= order.Quantity select sp).OrderBy(s=>s.Batch_ID).FirstOrDefault<Stock_Pile>();
                            }

                            if (stockPile == null)
                            {
                                if (!string.IsNullOrEmpty(order_detail.SyncResultMessage))
                                {
                                    //order_detail.SyncResultMessage = "默认仓库:" + house.Title + "没有库存或者库存数量不够<br/>";
                                }
                                //get store house when it has the specific product
                                var tmpstockPile = from sp in stockPiles where sp.Product_ID == stockPileProductId && sp.Quantity >= order.Quantity select sp;
                                if (tmpstockPile.Count() > 0)
                                {
                                    stockPile = tmpstockPile.OrderBy(s=>s.Batch_ID).ToList<Stock_Pile>()[0];
                                    Store_House tmpHouse = (from h in houses where h.StoreHouse_ID == stockPile.StockHouse_ID select h).FirstOrDefault<Store_House>();
                                    if (tmpHouse != null)
                                    {
                                        house = tmpHouse;
                                    }
                                }
                                else
                                {
                                    //cannot leave stock, no stock pile
                                    order_detail.Status1 = (int)SaleDetailStatus.NO_ENOUGH_STOCK;
                                    order_detail.SyncResultMessage = "没有足够的库存,不能出库";
                                }
                            }

                            //no stock cannot leave stock
                            if (stockPile != null)
                            {
                                order_detail.Status1 = (int)SaleDetailStatus.LEAVED_STOCK;
                                order_detail.SyncResultMessage = "出库仓库:" + house.Title;
                                dbDetail.Parent_Product_ID = order.Parent_Product_ID;
                                dbDetail.Product_ID = order.Product_ID;
                                dbDetail.StoreHouse_ID = stockPile.StockHouse_ID;
                                dbDetail.Batch_ID = stockPile.Batch_ID;
                                //Update stock
                                stockPile.Quantity = stockPile.Quantity - order.Quantity;

                                //Update stock field in Product table
                                Product product=(from pdt in allproducts where pdt.Product_ID==dbDetail.Parent_Product_ID select pdt).FirstOrDefault<Product>();
                                if (product != null)
                                {
                                    product.Quantity = product.Quantity - order.Quantity;
                                }
                                dbDetail.Order_ID = order.Order_ID;
                                dbLDetails.Add(dbDetail);
                                //db.Leave_Stock_Detail.Add(dbDetail);
                            }
                        }
                        #endregion

                        if (isNew && dbLDetails.Count>0)
                        {
                            db.Leave_Stock.Add(dbStock);
                            db.SaveChanges();
                            foreach (Leave_Stock_Detail d in dbLDetails)
                            {
                                if (d.Leave_Stock_ID == 0)
                                {
                                    d.Leave_Stock_ID = dbStock.Leave_Stock_ID;
                                }

                                db.Leave_Stock_Detail.Add(d);
                            }
                        }
                    }
                }

                base.CreateActionLog(new BUserActionLog() { Shop=new BShop{ ID=shop.Shop_ID}, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_LEAVE_STOCK }, Description = "同步商城订单到进销存,成功调用商城API,未出库的订单已成功出库并更新了产品库存" });
                db.SaveChanges();
            }
            catch (KMJXCException kex)
            {
                throw kex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Dispose();
            }
        }
Пример #3
0
        /// <summary>
        /// Single order leave stock
        /// </summary>
        /// <param name="lstock"></param>
        /// <returns></returns>
        public bool CreateLeaveStock(BLeaveStock leaveStock)
        {
            bool result = false;

            if (this.CurrentUserPermission.ADD_LEAVE_STOCK == 0)
            {
                throw new KMJXCException("没有权限出库");
            }

            if (leaveStock.Sale == null || string.IsNullOrEmpty(leaveStock.Sale.Sale_ID))
            {
                throw new KMJXCException("必须选择订单出库");
            }

            if (leaveStock.Shop == null)
            {
                leaveStock.Shop = new BShop() { ID=this.Shop.Shop_ID};
                //throw new KMJXCException("必须选择店铺");
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                int[] csp_ids=(from child in this.DBChildShops select child.Shop_ID).ToArray<int>();
                if (csp_ids == null)
                {
                    csp_ids = new int[1];
                }

                List<Product> products=(from pdt in db.Product where pdt.Shop_ID==this.Shop.Shop_ID || pdt.Shop_ID==this.Main_Shop.Shop_ID || csp_ids.Contains(pdt.Shop_ID) select pdt).ToList<Product>();

                Leave_Stock dbStock = new Leave_Stock();
                dbStock.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                dbStock.Leave_Date = leaveStock.LeaveDate;
                dbStock.Leave_Stock_ID = 0;
                dbStock.Sale_ID = leaveStock.Sale.Sale_ID;
                dbStock.Shop_ID = leaveStock.Shop.ID;
                dbStock.User_ID = this.CurrentUser.ID;
                db.Leave_Stock.Add(dbStock);
                db.SaveChanges();

                if (dbStock.Leave_Stock_ID <= 0)
                {
                    throw new KMJXCException("出库单创建失败");
                }

                if (leaveStock.Details != null)
                {
                    foreach (BLeaveStockDetail detail in leaveStock.Details)
                    {
                        Leave_Stock_Detail dbDetail = new Leave_Stock_Detail();
                        dbDetail.Leave_Stock_ID = dbStock.Leave_Stock_ID;
                        dbDetail.Price = detail.Price;
                        dbDetail.Quantity = detail.Quantity;
                        dbDetail.StoreHouse_ID = detail.StoreHouse.ID;
                        dbDetail.Product_ID = detail.ProductID;
                        if (detail.Parent_ProductID == 0)
                        {
                            dbDetail.Product_ID = (from p in products where p.Product_ID == detail.ProductID select p.Parent_ID).FirstOrDefault<int>();
                        }

                        db.Leave_Stock_Detail.Add(dbDetail);
                    }
                }

                db.SaveChanges();
                result = true;
            }

            return result;
        }