Example #1
0
 public BBaseManager(BUser user, Shop shop, Permission permission)
 {
     this.CurrentUser = user;
     GetUserById(user.ID);
     this.Shop = shop;
     this.GetShops();
     permissionManager = new PermissionManager(this.Shop.Shop_ID);
     this.CurrentUserPermission = permission;
     this.GetUserPermission();
 }
Example #2
0
 public ShopCategoryManager(BUser user, Shop shop, Permission permission)
     : base(user, shop, permission)
 {
     mallShopManager = new TaoBaoShopManager(this.AccessToken, this.Shop.Mall_Type_ID);
 }
Example #3
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();
            }
        }
Example #4
0
 public SalesManager(BUser user, Shop shop, Permission permission)
     : base(user, shop, permission)
 {
     stockManager = new StockManager(user, shop, permission);
     tradeManager = new TaobaoTradeManager(this.AccessToken, this.Shop.Mall_Type_ID);
 }
Example #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private bool SyncMallTrades(int startTime, int endTime, string status,int syncType,Shop shop)
        {
            bool result = false;
            try
            {
                KuanMaiEntities db = new KuanMaiEntities();
                IOTradeManager tradeManager = null;
                Access_Token token = this.AccessToken;
                Shop desshop = this.Shop;
                if (shop != null)
                {
                    desshop = shop;
                    token = (from t in db.Access_Token
                             from u in db.User
                             where t.User_ID == u.User_ID && u.User_ID == shop.User_ID
                             select t).FirstOrDefault<Access_Token>();

                    if (token == null)
                    {
                        throw new KMJXCException("店铺 "+shop.Name+" 的授权已经过期,请使用此店铺掌柜账号登录一次系统");
                    }

                    if (IsTokenExpired(token))
                    {
                        throw new KMJXCException("店铺 " + shop.Name + " 的授权已经过期,请使用此店铺掌柜账号登录一次系统");
                    }
                }

                tradeManager = new TaobaoTradeManager(token, desshop.Mall_Type_ID);
                List<BSale> allSales = new List<BSale>();

                Sale_SyncTime syncTime = null;

                syncTime = (from sync in db.Sale_SyncTime where sync.ShopID == desshop.Shop_ID && sync.SyncType == syncType orderby sync.LastSyncTime descending select sync).FirstOrDefault<Sale_SyncTime>();
                DateTime tNow = DateTime.Now;
                long timeNow = DateTimeUtil.ConvertDateTimeToInt(tNow);
                long lastSyncModifiedTime = 0;
                lastSyncModifiedTime = DateTimeUtil.ConvertDateTimeToInt(tNow.AddDays(-1));
                if (syncTime != null)
                {
                    lastSyncModifiedTime = syncTime.LastTradeModifiedEndTime;
                }

                syncTime = new Sale_SyncTime();
                syncTime.ShopID = desshop.Shop_ID;
                syncTime.SyncUser = this.CurrentUser.ID;
                syncTime.LastSyncTime = timeNow;

                syncTime.SyncType = syncType;
                db.Sale_SyncTime.Add(syncTime);

                //0 is normal sync trade by created time, 1 is increment sync trade by modified time
                if (syncType == 0)
                {
                    syncTime.LastTradeStartEndTime = endTime + 1;
                    syncTime.LastTradeModifiedEndTime = 0;
                    DateTime? sDate = null;
                    DateTime? eDate = null;

                    if (startTime > 0)
                    {
                        sDate = DateTimeUtil.ConvertToDateTime(startTime);
                    }

                    if (endTime > 0)
                    {
                        eDate = DateTimeUtil.ConvertToDateTime(endTime);
                    }

                    allSales = tradeManager.SyncMallTrades(sDate, eDate, status);
                }
                else if (syncType == 1)
                {
                    syncTime.LastTradeStartEndTime = 0;
                    syncTime.LastTradeModifiedEndTime = timeNow + 1;
                    allSales = tradeManager.IncrementSyncMallTrades(lastSyncModifiedTime, timeNow, status);
                }

                List<BSale> sales = new List<BSale>();

                foreach (BSale s in allSales)
                {
                    List<BSale> existed = (from ss in sales where ss.Sale_ID == s.Sale_ID select ss).ToList<BSale>();
                    if (existed == null || existed.Count == 0)
                    {
                        sales.Add(s);
                    }
                }

                base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = shop.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.SYNC_SHOP_TRADE }, Description = "同步商城订单到进销存,成功调用商城API," + sales.Count+" 条新的交易同步回进销存" });
                this.HandleMallTrades(sales, desshop);
                db.SaveChanges();
            }
            catch (KMJXCTaobaoException mex)
            {
                throw mex;
            }
            catch (KMJXCMallException mex)
            {
                throw mex;
            }
            catch (KMJXCException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }
Example #6
0
        /// <summary>
        /// Handle sales synchronized from mall including creating leave stocks and handle refounded trades which already synchronized before.
        /// </summary>
        /// <param name="allSales"></param>
        private void HandleMallTrades(List<BSale> allSales,Shop shop)
        {
            if (allSales == null)
            {
                return;
            }

            List<BSale> newSales = new List<BSale>();
            List<BSale> backSales = new List<BSale>();
            List<Product> allProducts = null;
            KuanMaiEntities db = new KuanMaiEntities();
            try
            {
                int[] child_shops=(from c in this.DBChildShops select c.Shop_ID).ToArray<int>();
                allProducts = (from p in db.Product
                               where p.Shop_ID == shop.Shop_ID || child_shops.Contains(p.Shop_ID) || p.Shop_ID == this.Main_Shop.Shop_ID
                               select p).ToList<Product>();
                var tmpExp = from expsp in db.Express_Shop
                             join exp in db.Express on expsp.Express_ID equals exp.Express_ID into lExp
                             from l_exp in lExp
                             where expsp.Shop_ID == shop.Shop_ID && expsp.IsDefault == 1
                             select l_exp;
                Express defaultExp = tmpExp.FirstOrDefault<Express>();
                List<Express_Fee> expFees = new List<Express_Fee>();
                if (defaultExp != null)
                {
                    expFees = (from fee in db.Express_Fee
                               where fee.Express_ID == defaultExp.Express_ID && fee.Shop_ID == shop.Shop_ID
                               select fee).ToList<Express_Fee>();
                }
                long timeNow = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                var customers = from customer in db.Customer
                                where customer.Mall_Type_ID == shop.Mall_Type_ID
                                select customer;
                List<Customer_Shop> shop_customers = (from shop_cus in db.Customer_Shop where shop_cus.Shop_ID == shop.Shop_ID select shop_cus).ToList<Customer_Shop>();
                string[] sale_ids=(from sale in allSales select sale.Sale_ID).ToArray<string>();
                List<Sale> dbSales = (from sale in db.Sale where sale_ids.Contains(sale.Mall_Trade_ID) select sale).ToList<Sale>();
                List<Common_District> areas = BBaseManager.Areas;
                foreach (BSale sale in allSales)
                {
                    Sale dbSale = new Sale();
                    dbSale.Amount = sale.Amount;
                    dbSale.Buyer_ID = 0;
                    dbSale.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    dbSale.Express_Cop = "";
                    dbSale.Mall_Trade_ID = sale.Sale_ID;
                    dbSale.Modified = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    dbSale.Post_Fee = sale.Post_Fee;
                    dbSale.Province_ID = 0;
                    dbSale.City_ID = 0;
                    dbSale.Sync_User = this.CurrentUser.ID;
                    dbSale.HasRefound = sale.HasRefound;
                    if (defaultExp != null)
                    {
                        dbSale.Express_ID = defaultExp.Express_ID;
                        dbSale.Express_Cop = defaultExp.Name;
                    }
                    List<Express_Fee> tmpFees = new List<Express_Fee>();
                    if (sale.Buyer != null)
                    {
                        if (sale.Buyer.Province != null)
                        {
                            sale.Buyer.Province = (from p in areas where p.name == sale.Buyer.Province.name select p).FirstOrDefault<Common_District>();
                            if (sale.Buyer.Province != null)
                            {
                                dbSale.Province_ID = sale.Buyer.Province.id;
                                tmpFees=(from fee in expFees where fee.Province_ID ==  dbSale.Province_ID select fee).ToList<Express_Fee>();
                            }
                        }
                        if (sale.Buyer.City != null)
                        {
                            sale.Buyer.City = (from p in areas where p.name == sale.Buyer.City.name select p).FirstOrDefault<Common_District>();
                            if (sale.Buyer.City != null)
                            {
                                dbSale.City_ID = sale.Buyer.City.id;
                                tmpFees = (from fee in expFees where fee.City_ID == dbSale.City_ID select fee).ToList<Express_Fee>();
                            }
                        }
                        if (!string.IsNullOrEmpty(sale.Buyer.Mall_ID))
                        {
                            Customer cus = (from custo in customers where custo.Mall_ID == sale.Buyer.Mall_ID select custo).FirstOrDefault<Customer>();
                            if (cus == null)
                            {
                                cus = new Customer();
                                cus.Address = sale.Buyer.Address;
                                cus.Name = sale.Buyer.Name;
                                cus.City_ID = dbSale.City_ID;
                                cus.Province_ID = dbSale.Province_ID;
                                cus.Mall_ID = sale.Buyer.Mall_ID;
                                cus.Mall_Type_ID = this.Shop.Mall_Type_ID;
                                cus.Phone = sale.Buyer.Phone;
                                db.Customer.Add(cus);
                                db.SaveChanges();
                                Customer_Shop cs = new Customer_Shop() { Shop_ID = shop.Shop_ID, Customer_ID = cus.Customer_ID };
                                db.Customer_Shop.Add(cs);
                            }
                            else
                            {
                                //update customer info
                                cus.Address = sale.Buyer.Address;
                                cus.Name = sale.Buyer.Name;
                                cus.City_ID = dbSale.City_ID;
                                cus.Province_ID = dbSale.Province_ID;
                                cus.Mall_ID = sale.Buyer.Mall_ID;
                                cus.Mall_Type_ID = this.Shop.Mall_Type_ID;
                                cus.Phone = sale.Buyer.Phone;
                                Customer_Shop cuss = (from cusshop in shop_customers where cusshop.Customer_ID == cus.Customer_ID && cusshop.Shop_ID == shop.Shop_ID select cusshop).FirstOrDefault<Customer_Shop>();
                                if (cuss == null)
                                {
                                    Customer_Shop cs = new Customer_Shop() { Shop_ID = shop.Shop_ID, Customer_ID = cus.Customer_ID };
                                    db.Customer_Shop.Add(cs);
                                }
                            }
                            dbSale.Buyer_ID = cus.Customer_ID;
                        }
                    }
                    if (tmpFees != null && tmpFees.Count > 0)
                    {
                        dbSale.Post_Fee1 = tmpFees[0].Fee;
                    }
                    dbSale.Sale_Time = sale.SaleDateTime;
                    dbSale.Shop_ID = shop.Shop_ID;
                    dbSale.Status = sale.Status;
                    dbSale.StockStatus = 0;
                    dbSale.Synced = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    Sale existed = null;
                    if (dbSales != null && dbSales.Count>0)
                    {
                        existed = (from s in dbSales where s.Mall_Trade_ID == dbSale.Mall_Trade_ID select s).FirstOrDefault<Sale>();
                    }
                    if (sale.HasRefound)
                    {
                        backSales.Add(sale);
                    }
                    if (existed == null)
                    {
                        newSales.Add(sale);
                        db.Sale.Add(dbSale);
                        if (sale.Orders != null)
                        {
                            foreach (BOrder order in sale.Orders)
                            {
                                Sale_Detail sd = new Sale_Detail();
                                sd.Amount = order.Amount;
                                sd.Discount = order.Discount;
                                sd.Mall_Order_ID = order.Order_ID;
                                sd.Mall_Trade_ID = sale.Sale_ID;
                                sd.Refound = order.Refound;
                                sd.Parent_Product_ID = 0;
                                sd.Product_ID = 0;
                                sd.Title = order.Title;
                                if (string.IsNullOrEmpty(sd.Title))
                                {
                                    sd.Title = "";
                                }
                                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>();
                                if (parentPdt != null)
                                {
                                    sd.Parent_Product_ID = parentPdt.Product_ID;
                                }
                                else
                                {
                                    if (childPdt != null)
                                    {
                                        sd.Parent_Product_ID = childPdt.Parent_ID;
                                    }
                                }
                                if (childPdt != null)
                                {
                                    sd.Product_ID = childPdt.Product_ID;
                                }
                                sd.Price = order.Price;
                                sd.Quantity = order.Quantity;
                                sd.Status = order.Status;
                                if (string.IsNullOrEmpty(sd.Status))
                                {
                                    sd.Status = "0";
                                }
                                sd.ImageUrl = order.ImageUrl;
                                sd.Status1 = order.Status1;
                                sd.StockStatus = 0;
                                sd.Mall_PID = order.Mall_PID;
                                sd.Mall_SkuID = order.Mall_SkuID;
                                sd.Supplier_ID = 0;
                                sd.Refound = order.Refound;
                                db.Sale_Detail.Add(sd);
                            }
                        }
                    }
                    else
                    {
                        List<Sale_Detail> details = (from detail in db.Sale_Detail
                                                     where detail.Mall_Trade_ID == dbSale.Mall_Trade_ID
                                                     select detail).ToList<Sale_Detail>();
                        existed.Modified = dbSale.Modified;
                        existed.Status = dbSale.Status;
                        foreach (BOrder order in sale.Orders)
                        {
                            Sale_Detail sd = (from ed in details where ed.Mall_Order_ID == order.Order_ID select ed).FirstOrDefault<Sale_Detail>();

                            if (sd != null)
                            {
                                continue;
                            }
                            sd = new Sale_Detail();
                            sd.Amount = order.Amount;
                            sd.Discount = order.Discount;
                            sd.Mall_Order_ID = order.Order_ID;
                            sd.Mall_Trade_ID = sale.Sale_ID;

                            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>();

                            if (parentPdt != null)
                            {
                                sd.Parent_Product_ID = parentPdt.Product_ID;
                            }
                            else
                            {
                                if (childPdt != null)
                                {
                                    sd.Parent_Product_ID = childPdt.Parent_ID;
                                }
                            }

                            if (childPdt != null)
                            {
                                sd.Product_ID = childPdt.Product_ID;
                            }

                            sd.Price = order.Price;
                            sd.Quantity = order.Quantity;
                            sd.Status = order.Status;
                            if (string.IsNullOrEmpty(sd.Status))
                            {
                                sd.Status = "0";
                            }
                            sd.ImageUrl = order.ImageUrl;
                            //sd.Status1 = order.Status1;
                            sd.StockStatus = 0;
                            sd.Mall_PID = order.Mall_PID;
                            sd.Supplier_ID = 0;
                            sd.Refound = order.Refound;
                            db.Sale_Detail.Add(sd);
                        }
                    }
                }

                db.SaveChanges();
                this.CreateLeaveStocks(newSales, shop);
                this.HandleBackTrades(backSales,shop);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                db.Dispose();
            }
        }
Example #7
0
        /// <summary>
        /// The back sales won't be created while the sales don't has corresponding leave stocks
        /// Example:Customers refounded before expressed
        /// </summary>
        /// <param name="trades"></param>
        private void HandleBackTrades(List<BSale> trades, Shop shop)
        {
            if (trades == null || trades.Count == 0 || shop==null)
            {
                return;
            }

            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                string[] sale_ids=(from trade in trades select trade.Sale_ID).ToArray<string>();
                List<Leave_Stock> leave_Stocks=(from ls in db.Leave_Stock where sale_ids.Contains(ls.Sale_ID) select ls).ToList<Leave_Stock>();
                int[] ls_ids=(from ls in leave_Stocks select ls.Leave_Stock_ID).ToArray<int>();
                List<Leave_Stock_Detail> leave_stock_Details=(from lsd in db.Leave_Stock_Detail where ls_ids.Contains(lsd.Leave_Stock_ID) select lsd).ToList<Leave_Stock_Detail>();
                List<Back_Sale> back_Sales=(from backSale in db.Back_Sale where sale_ids.Contains(backSale.Sale_ID) select backSale).ToList<Back_Sale>();
                int[] bs_ids=(from bs in back_Sales select bs.Back_Sale_ID).ToArray<int>();
                List<Back_Sale_Detail> back_Sale_Details=(from bsd in db.Back_Sale_Detail where bs_ids.Contains(bsd.Back_Sale_ID) select bsd).ToList<Back_Sale_Detail>();
                List<Sale_Detail> saleDetails=(from sd in db.Sale_Detail where sale_ids.Contains(sd.Mall_Trade_ID) select sd).ToList<Sale_Detail>();
                foreach (BSale trade in trades)
                {
                    Leave_Stock ls=(from leaveStock in leave_Stocks where leaveStock.Sale_ID==trade.Sale_ID select leaveStock).FirstOrDefault<Leave_Stock>();
                    //no leave stock no need to create back sale
                    if (ls == null)
                    {
                        continue;
                    }

                    Back_Sale bSale=(from b_Sale in back_Sales where b_Sale.Sale_ID==trade.Sale_ID select b_Sale).FirstOrDefault<Back_Sale>();
                    bool isNewBackSale = false;
                    double totalRefound = 0;
                    if (bSale == null)
                    {
                        isNewBackSale = true;
                        bSale = new Back_Sale();
                        bSale.Back_Date = trade.Synced;
                        bSale.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        bSale.Description = "";
                        bSale.Sale_ID = trade.Sale_ID;
                        bSale.Shop_ID = shop.Shop_ID;
                        bSale.Status = 0;
                        bSale.User_ID = this.CurrentUser.ID;
                    }

                    if (isNewBackSale)
                    {
                        db.Back_Sale.Add(bSale);
                        db.SaveChanges();
                    }
                    if (trade.Orders != null)
                    {
                        foreach (BOrder order in trade.Orders)
                        {
                            Sale_Detail sale_detail=(from odetail in saleDetails where odetail.Mall_Order_ID==order.Order_ID select odetail).FirstOrDefault<Sale_Detail>();
                            if (sale_detail == null)
                            {
                                continue;
                            }

                            //1- refound succeed
                            //0- is normal
                            if (!order.Refound)
                            {
                                continue;
                            }

                            Back_Sale_Detail dbSaleDetail = (from bsd in back_Sale_Details where bsd.Order_ID==order.Order_ID select bsd).FirstOrDefault<Back_Sale_Detail>();
                            Leave_Stock_Detail dbLeaveStockDetail=(from dblsd in leave_stock_Details where dblsd.Order_ID==order.Order_ID select dblsd).FirstOrDefault<Leave_Stock_Detail>();
                            //no need to create back sale detail while no leave stock detail
                            if (dbSaleDetail == null && dbLeaveStockDetail!=null)
                            {
                                dbSaleDetail = new Back_Sale_Detail();
                                dbSaleDetail.Order_ID = order.Order_ID;
                                dbSaleDetail.Back_Sale_ID = bSale.Back_Sale_ID;
                                dbSaleDetail.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                                dbSaleDetail.Description = "同步商城订单时处理有退货的订单(有出库单详细信息)";
                                dbSaleDetail.Parent_Product_ID = dbLeaveStockDetail.Parent_Product_ID;
                                dbSaleDetail.Price = order.Price;
                                dbSaleDetail.Product_ID = dbLeaveStockDetail.Product_ID;
                                dbSaleDetail.Quantity = order.Quantity;
                                dbSaleDetail.Status = 0;
                                dbSaleDetail.Refound = order.Amount;
                                totalRefound += order.Amount;
                                db.Back_Sale_Detail.Add(dbSaleDetail);
                                sale_detail.Status1 = (int)SaleDetailStatus.REFOUNDED_WAIT_HANDLE;
                            }
                        }
                        bSale.Amount = totalRefound;
                    }
                }

                base.CreateActionLog(new BUserActionLog() { Shop=new BShop{ ID=shop.Shop_ID}, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_BACK_SALE }, Description = "同步商城订单到进销存,成功调用商城API,有退货交易,进销存退货单成功创建" });
                db.SaveChanges();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                db.Dispose();
            }
        }
Example #8
0
 public PermissionManagement(BUser user, Shop shop, Permission permission)
     : base(user, shop, permission)
 {
     PermissionManager = new PermissionManager(shop.Shop_ID);
 }
Example #9
0
 public SupplierManager(BUser user, Shop shop, Permission permission)
     : base(user, shop, permission)
 {
 }
Example #10
0
 public BuyManager(BUser user, Shop shop, Permission permission)
     : base(user, shop, permission)
 {
 }
Example #11
0
 public ProductManager(BUser user, Shop shop, Permission permission)
     : base(user, shop, permission)
 {
     stockManager = new StockManager(user, shop, permission);
 }
Example #12
0
        /// <summary>
        /// Add child shop
        /// </summary>
        /// <param name="parent_shop"></param>
        /// <param name="shop"></param>
        /// <returns></returns>
        public bool AddChildShop(Shop parent_shop,Shop shop)
        {
            bool result = false;

            if (shop == null)
            {
                throw new KMJXCException("您要添加的子店铺信息不存在,请先使用子店铺的主账户登录进销存,然后在执行添加子店铺操作");
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                Shop_Child_Request scr = (from sr in db.Shop_Child_Request where sr.Shop_ID == parent_shop.Shop_ID && sr.Child_Shop_ID == shop.Shop_ID && sr.Status == 0 select sr).FirstOrDefault<Shop_Child_Request>();
                if (scr == null)
                {
                    scr = new Shop_Child_Request();
                    scr.Shop_ID = (int)parent_shop.Shop_ID;
                    scr.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    scr.Child_Shop_ID = (int)shop.Shop_ID;
                    scr.Created_By = (int)this.CurrentUser.ID;
                    scr.Status = 0;
                    scr.Modified_By = 0;
                    scr.Modified = 0;
                    db.Shop_Child_Request.Add(scr);
                    db.SaveChanges();
                    result = true;
                }
                else
                {
                    throw new KMJXCException("已经发送过添加子店铺请求,请不要重复发送");
                }
            }

            return result;
        }
Example #13
0
 public ShopManager(BUser user, Shop shop, Permission permission)
     : base(user, shop, permission)
 {
     this.Mall_Type = shop.Mall_Type_ID;
     this.UserManager = new UserManager(user, permission); ;
 }
Example #14
0
 public ShopManager(BUser user, Shop shop, Permission permission,UserManager userMgr)
     : base(user, shop, permission)
 {
     this.Mall_Type=shop.Mall_Type_ID;
     this.UserManager=userMgr;
 }
Example #15
0
        /// <summary>
        /// Sync onsale products to local database
        /// </summary>
        /// <returns></returns>
        public List<BMallProduct> SyncMallOnSaleProducts(int shop_id=0,bool create_product=false,bool mapProduct=false)
        {
            List<BMallProduct> newProducts = new List<BMallProduct>();
            List<BMallProduct> products = new List<BMallProduct>();
            IOProductManager productManager = new TaobaoProductManager(this.AccessToken,this.Shop.Mall_Type_ID);
            List<BMallProduct> newUnMappedProducts = new List<BMallProduct>();
            long total = 0;
            long page = 1;
            long pageSize = 40;
            List<BMallProduct> tmp = new List<BMallProduct>();
            Shop shop = this.Shop;
            if (shop_id > 0)
            {
                shop = new Shop() { Shop_ID=shop_id };
            }
            tmp = productManager.GetOnSaleProducts(this.CurrentUser, shop, page, pageSize, out total);

            if (tmp != null)
            {
                products = products.Concat(tmp).ToList<BMallProduct>();
            }

            long totalPage = 1;

            if (total > pageSize)
            {
                if (total % pageSize == 0)
                {
                    totalPage = total / pageSize;
                }
                else
                {
                    totalPage = total / pageSize + 1 ;
                }
            }

            if (totalPage > 1)
            {
                page++;
                while (page <= totalPage)
                {
                    tmp = productManager.GetOnSaleProducts(this.CurrentUser, this.Shop, page, pageSize, out total);
                    products = products.Concat(tmp).ToList<BMallProduct>();
                    page++;
                }
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                List<Product> locProducts=(from locPdt in db.Product where locPdt.Shop_ID==this.Shop.Shop_ID select locPdt).ToList<Product>();

                List<Mall_Product> dbMallProducts=(from pdt in db.Mall_Product
                                                   where pdt.Shop_ID==this.Shop.Shop_ID
                                                   select pdt).ToList<Mall_Product>();

                List<Mall_Product_Sku> dbSkus = (from sku in db.Mall_Product_Sku
                                                 where sku.Shop_ID == this.Shop.Shop_ID
                                                 select sku).ToList<Mall_Product_Sku>();

                foreach (BMallProduct product in products)
                {
                    Mall_Product dbProduct=(from dbPdt in dbMallProducts where dbPdt.Mall_ID==product.ID select dbPdt).FirstOrDefault<Mall_Product>();
                    bool isNew = false;
                    if (dbProduct == null)
                    {
                        isNew = true;
                        dbProduct = new Mall_Product();
                        dbProduct.CreatedProduct = false;
                        dbProduct.FirstSync = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    }

                    Product mappedLocProduct=(from p in locProducts where p.Product_ID==product.OuterID select p).FirstOrDefault<Product>();
                    if (mappedLocProduct == null)
                    {
                        product.OuterID = 0;
                    }

                    dbProduct.Created = product.Created;
                    dbProduct.Description = product.Description;
                    dbProduct.Mall_ID = product.ID;
                    dbProduct.Modified = product.Modified;
                    dbProduct.Outer_ID = product.OuterID;
                    dbProduct.PicUrl = product.PicUrl;
                    dbProduct.Price = product.Price;
                    dbProduct.Quantity = (int)product.Quantity;
                    dbProduct.Shop_ID = this.Shop.Shop_ID;

                    if (product.Shop != null)
                    {
                        dbProduct.Shop_ID = product.Shop.ID;
                    }
                    else {
                        product.Shop = new BShop { ID=this.Shop.Shop_ID };
                    }

                    dbProduct.Synced = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    dbProduct.Title = product.Title;

                    if (isNew)
                    {
                        db.Mall_Product.Add(dbProduct);
                        newProducts.Add(product);
                        if (mappedLocProduct == null)
                        {
                            newUnMappedProducts.Add(product);
                        }
                    }

                    if (product.Skus!=null)
                    {
                        foreach (BMallSku mSku in product.Skus)
                        {
                            bool skuNew = false;
                            Mall_Product_Sku dbSku=(from dbs in dbSkus where mSku.SkuID== dbs.SKU_ID select dbs).FirstOrDefault<Mall_Product_Sku>();
                            if (dbSku == null)
                            {
                                skuNew = true;
                                dbSku = new Mall_Product_Sku();
                            }

                            Product childLocProduct=(from p in locProducts where p.Product_ID==mSku.OuterID select p).FirstOrDefault<Product>();
                            if (childLocProduct == null)
                            {
                                mSku.OuterID = 0;
                            }

                            dbSku.Outer_ID = mSku.OuterID;
                            dbSku.Mall_ID = mSku.MallProduct_ID;
                            dbSku.Price = mSku.Price;
                            dbSku.Properties = mSku.Properities;
                            dbSku.Properties_name = mSku.PropertiesName;
                            dbSku.Quantity = (int)mSku.Quantity;
                            dbSku.Shop_ID = product.Shop.ID;
                            dbSku.SKU_ID = mSku.SkuID;
                            if (skuNew)
                            {
                                db.Mall_Product_Sku.Add(dbSku);
                            }
                        }
                    }
                }

                SyncWithMall sync = new SyncWithMall();
                sync.Shop_ID = this.Shop.Shop_ID;
                sync.User_ID = this.CurrentUser.ID;
                sync.SyncType = 0;//宝贝同步
                sync.SyncTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                db.SyncWithMall.Add(sync);
                db.SaveChanges();

                if (create_product)
                {
                    this.CreateProductsByMallProducts(newUnMappedProducts,mapProduct);
                }

                base.CreateActionLog(new BUserActionLog() {Shop=new BShop{ ID=shop.Shop_ID}, Action = new BUserAction() { Action_ID = UserLogAction.SYNC_SHOP_ONSALE_PRODUCT }, Description = "同步商城在售宝贝,共有 " + newProducts.Count+ " 个新宝贝同步到进销存" });
            }

            return newProducts;
        }
Example #16
0
 public ReportFactory(BUser user, Shop shop, Permission permission)
     : base(user, shop, permission)
 {
 }