/// <summary> /// Sync actions with Permission object /// </summary> public void SyncPermissionWithAction() { Type permission=typeof(Permission); FieldInfo[] fields = permission.GetFields(); if (fields == null || fields.Length<=0) { return; } KuanMaiEntities db = new KuanMaiEntities(); try { foreach (FieldInfo field in fields) { var action = from a in db.Admin_Action where a.action_name == field.Name select a; if (action == null || action.ToList<Admin_Action>().Count == 0) { Admin_Action new_action = new Admin_Action(); new_action.action_name = field.Name; new_action.action_description = field.Name; new_action.enable = true; db.Admin_Action.Add(new_action); } } db.SaveChanges(); } catch (DbEntityValidationException ex) { } finally { db.Dispose(); } }
/// <summary> /// Calback from Mall Open API Authorization, it will verify if current login user has access to the system /// </summary> /// <param name="code">returns by Mall Open API Authorization</param> /// <returns></returns> public Access_Token AuthorizationCallBack(string code) { Access_Token request_token = null; BUser requester = new BUser(); //must get access token after mall authorization to identify user request_token = TokenManager.RequestAccessToken(code); if (request_token == null) { throw new KMJXCException("没有获取到Access token", ExceptionLevel.SYSTEM); } requester.Type = new BMallType() { ID = this.Mall_Type_ID }; requester.Mall_ID = request_token.Mall_User_ID; requester.Mall_Name = request_token.Mall_User_Name; requester.Parent_ID = 0; requester.Parent = null; KuanMaiEntities db = new KuanMaiEntities(); try { var db_user = from u in db.User where u.Mall_ID == requester.Mall_ID && u.Mall_Name == requester.Mall_Name && u.Mall_Type == this.Mall_Type_ID select new BUser { ID = u.User_ID, Name = u.Name, Mall_Name = u.Mall_Name, Mall_ID = u.Mall_ID, Password = u.Password, Parent_ID = (int)u.Parent_User_ID, }; List<BUser> users = db_user.ToList<BUser>(); //Create user in local db with mall owner id if (users.Count == 0) { this.InitializeMallManagers(request_token); if (this.ShopManager == null) { throw new KMJXCException("IShopManager 实例为null", ExceptionLevel.SYSTEM); } //check if current user's shop is ready in system Shop shop = this.ShopManager.GetShop(requester); if (shop == null) { BUser subUser = this.MallUserManager.GetSubUser(requester.Mall_ID, requester.Mall_Name); if (subUser == null) { throw new KMJXCException("用户:" + requester.Mall_Name + " 没有对应的" + ((KM.JXC.BL.Open.OBaseManager)this.ShopManager).MallType.Description + ",并且不属于任何店铺的子账户", ExceptionLevel.ERROR); } else { // if (subUser.Parent == null || string.IsNullOrEmpty(subUser.Parent.Mall_Name)) { throw new KMJXCException("用户:" + requester.Mall_Name + " 没有对应的" + ((KM.JXC.BL.Open.OBaseManager)this.ShopManager).MallType.Description + ",并且不属于任何店铺的子账户", ExceptionLevel.ERROR); } BUser mainUser = null; var u = from us in db.User where us.Mall_ID == subUser.Parent.Mall_ID && us.Mall_Type == requester.Type.ID && us.Mall_Name == subUser.Parent.Mall_Name select new BUser { ID = us.User_ID, Name = us.Name, Mall_Name = us.Mall_Name, Mall_ID = us.Mall_ID, Password = us.Password, Parent_ID = (int)us.Parent_User_ID, Type = new BMallType { ID = us.Mall_Type } }; if (u.ToList<BUser>().Count() == 1) { mainUser = u.ToList<BUser>()[0]; } if (mainUser == null) { throw new KMJXCException("主账户:" + subUser.Parent.Mall_Name + " 还没有初始化店铺信息,所有子账户无法登录系统", ExceptionLevel.ERROR); } requester.Parent_ID = mainUser.ID; requester.Parent = mainUser; requester.EmployeeInfo = subUser.EmployeeInfo; } } //create user in local db requester.Name = requester.Mall_Name; requester.Password = Guid.NewGuid().ToString(); User dbUser = new User(); dbUser.User_ID = requester.ID; dbUser.Mall_ID = requester.Mall_ID; dbUser.Mall_Name = requester.Mall_Name; dbUser.NickName = ""; dbUser.Name = requester.Name; dbUser.Mall_Type = requester.Type.ID; dbUser.Parent_Mall_ID = ""; dbUser.Parent_Mall_Name = ""; dbUser.Parent_User_ID = 0; dbUser.Password = ""; dbUser.Name = dbUser.Mall_Name; dbUser.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbUser.Modified = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbUser.Modified_By = 0; if (requester.Parent != null) { dbUser.Parent_Mall_ID = requester.Parent.Mall_ID; dbUser.Parent_Mall_Name = requester.Parent.Mall_Name; dbUser.Parent_User_ID = requester.Parent.ID; } db.User.Add(dbUser); db.SaveChanges(); //create access token for the new user request_token.User_ID = dbUser.User_ID; requester.ID = dbUser.User_ID; db.Access_Token.Add(request_token); //save employee if (requester.Parent_ID > 0 && requester.EmployeeInfo != null) { requester.EmployeeInfo.User_ID = requester.ID; Employee employee = new Employee(); employee.Name = requester.EmployeeInfo.Name; employee.IdentityCard = requester.EmployeeInfo.IdentityCard; employee.MatureDate = requester.EmployeeInfo.MatureDate; employee.Phone = requester.EmployeeInfo.Phone; employee.User_ID=requester.EmployeeInfo.User_ID; employee.HireDate = requester.EmployeeInfo.HireDate; employee.Gendar = requester.EmployeeInfo.Gendar; employee.Duty = requester.EmployeeInfo.Duty; employee.Email = requester.EmployeeInfo.Email; employee.Department = requester.EmployeeInfo.Department; employee.BirthDate = requester.EmployeeInfo.BirthDate; employee.Address = requester.EmployeeInfo.Address; db.Employee.Add(employee); } if (shop != null) { //create local shop information for the new main user shop.User_ID = requester.ID; shop.Parent_Shop_ID = 0; shop.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); shop.Synced = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Shop.Add(shop); db.SaveChanges(); //save shop user Shop_User shop_User = new Shop_User(); shop_User.User_ID = requester.ID; shop_User.Shop_ID = shop.Shop_ID; db.Shop_User.Add(shop_User); //update dbuser dbUser.Shop_ID = shop.Shop_ID; db.SaveChanges(); //create default stock house Store_House shouse = new Store_House(); shouse.Shop_ID = shop.Shop_ID; shouse.Title = "默认仓库"; shouse.User_ID = requester.ID; shouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Store_House.Add(shouse); db.SaveChanges(); } if (shop != null && requester.Parent_ID == 0) { //sync mall sub users to system //List<BUser> subUsers = this.MallUserManager.GetSubUsers(requester); //if (subUsers != null && subUsers.Count > 0 && shop.Shop_ID > 0) //{ // foreach (BUser user in subUsers) // { // User db1User = new User(); // db1User.Parent_Mall_ID = requester.Mall_ID; // db1User.Parent_Mall_Name = requester.Mall_Name; // db1User.Parent_User_ID = (int)requester.ID; // db1User.Mall_Name = user.Mall_Name; // db1User.Mall_ID = user.Mall_ID; // db1User.Mall_Type = user.Type.Mall_Type_ID; // db1User.Name = user.Name; // db1User.Password = ""; // db.User.Add(db1User); // db.SaveChanges(); // if (db1User.User_ID > 0) // { // //add shop user // Shop_User shop_User1 = new Shop_User(); // shop_User1.User_ID = requester.ID; // shop_User1.Shop_ID = shop.Shop_ID; // db.Shop_User.Add(shop_User1); // if (user.EmployeeInfo != null) // { // user.EmployeeInfo.User_ID = db1User.User_ID; // db.Employee.Add(user.EmployeeInfo); // //db.SaveChanges(); // } // } // } // db.SaveChanges(); //} } } else { //Verify if local db has non expried accesstoken requester = users[0]; Access_Token local_token = GetLocalToken(requester.ID, this.Mall_Type_ID); if (local_token != null) { long timeNow = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); //last access token is expried if (timeNow >= local_token.Expirse_In + local_token.Request_Time) { request_token = TokenManager.RequestAccessToken(code); request_token.User_ID = requester.ID; UpdateLocalAccessToken(request_token); } else { request_token = local_token; } } } } catch (DbEntityValidationException dbex) { throw new KMJXCException("登录失败,请联系管理员"); } catch (Exception ex) { throw new KMJXCException(ex.Message, ExceptionLevel.SYSTEM); } finally { if (db != null) { db.Dispose(); } } return request_token; }
/// <summary> /// /// </summary> /// <param name="sale_id"></param> public void CreateBackStock(int backsale_id,List<BOrder> orders,int backSaleStatus) { KuanMaiEntities db = new KuanMaiEntities(); try { if (orders == null || orders.Count == 0) { throw new KMJXCException("没有退货信息"); } Back_Sale dbbackSale = (from bsale in db.Back_Sale where bsale.Back_Sale_ID == backsale_id select bsale).FirstOrDefault<Back_Sale>(); if (dbbackSale == null) { throw new KMJXCException("退货单信息不存在"); } //dbbackSale.Status = backSaleStatus; var bdetails = from bsd in db.Back_Sale_Detail where bsd.Back_Sale_ID == backsale_id select bsd; string[] order_id = (from o in orders select o.Order_ID).ToArray<string>(); if (order_id != null) { bdetails = bdetails.Where(d => order_id.Contains(d.Order_ID)); } List<Sale_Detail> saleDetails=(from s in db.Sale_Detail where order_id.Contains(s.Mall_Order_ID) select s).ToList<Sale_Detail>(); List<Back_Sale_Detail> backSaleDetails = bdetails.ToList<Back_Sale_Detail>(); //Check if current sale trade has leave stock records //if the sale doesn't have leave stock, so no need to back stock Leave_Stock leave_Stock=(from ls in db.Leave_Stock where ls.Sale_ID==dbbackSale.Sale_ID select ls).FirstOrDefault<Leave_Stock>(); if (leave_Stock!=null) { List<Leave_Stock_Detail> leaveDetails=(from ld in db.Leave_Stock_Detail where ld.Leave_Stock_ID==leave_Stock.Leave_Stock_ID select ld).ToList<Leave_Stock_Detail>(); BBackStock backStock = new BBackStock(); backStock.BackSaleID = dbbackSale.Back_Sale_ID; backStock.BackSale = new BBackSale() { ID = dbbackSale.Back_Sale_ID }; if (backSaleStatus == 1 || backSaleStatus==2) { backStock.UpdateStock = true; } else { backStock.UpdateStock = false; } backStock.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); backStock.BackDateTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); backStock.Created_By = new BUser() { ID = this.CurrentUser.ID }; if (!string.IsNullOrEmpty(dbbackSale.Description)) { backStock.Description = dbbackSale.Description + "<br/> 生成了退库单"; } else { backStock.Description = "生成了退库单"; } backStock.Shop = new BShop() { ID = dbbackSale.Shop_ID }; //collect back stock details info from leave stock details backStock.Details = new List<BBackStockDetail>(); foreach (Back_Sale_Detail bsd in backSaleDetails) { BOrder order=(from o in orders where bsd.Order_ID == o.Order_ID select o).FirstOrDefault<BOrder>(); Sale_Detail saleDetail=(from s in saleDetails where s.Mall_Order_ID==bsd.Order_ID select s).FirstOrDefault<Sale_Detail>(); Leave_Stock_Detail leaveStockDetail = (from lsd in leaveDetails where lsd.Order_ID == bsd.Order_ID select lsd).FirstOrDefault<Leave_Stock_Detail>(); if (leaveStockDetail == null) { continue; } BBackStockDetail bsDetail = new BBackStockDetail(); bsDetail.Price = leaveStockDetail.Price; bsDetail.Quantity = leaveStockDetail.Quantity; if (order != null && order.Quantity > 0 && order.Quantity<=leaveStockDetail.Quantity) { bsDetail.Quantity = order.Quantity; } bsDetail.ProductID = leaveStockDetail.Product_ID; bsDetail.ParentProductID = leaveStockDetail.Parent_Product_ID; bsDetail.Batch = new BStockBatch() { ID = leaveStockDetail.Batch_ID }; bsDetail.StoreHouse = new BStoreHouse() { ID = leaveStockDetail.StoreHouse_ID }; backStock.Details.Add(bsDetail); bsd.Status = backSaleStatus; //5 means refound and successfully back to sock saleDetail.Status1 = (int)SaleDetailStatus.REFOUND_HANDLED; saleDetail.SyncResultMessage = "退货已经处理"; } if (backStock.Details.Count > 0) { this.CreateBackStock(backStock); } } db.SaveChanges(); } catch (KMJXCException kex) { throw kex; } catch(Exception ex) { throw ex; } finally { db.Dispose(); } }
/// <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(); } }
/// <summary> /// /// </summary> /// <param name="propertyId"></param> /// <param name="value"></param> public void CreatePropertyValues(int propertyId, List<string> value) { KuanMaiEntities db = new KuanMaiEntities(); try { Product_Spec ps=(from prop in db.Product_Spec where prop.Product_Spec_ID==propertyId select prop).FirstOrDefault<Product_Spec>(); if (ps == null) { throw new KMJXCException("属性不存在,不能添加属性值"); } List<Product_Spec_Value> psValues=(from psv in db.Product_Spec_Value where psv.Product_Spec_ID==propertyId select psv).ToList<Product_Spec_Value>(); if (value != null && value.Count > 0) { foreach (string v in value) { Product_Spec_Value propValue = (from propv in psValues where propv.Name == v select propv).FirstOrDefault<Product_Spec_Value>(); if (propValue == null) { propValue = new Product_Spec_Value(); propValue.Product_Spec_ID = propertyId; propValue.Name = v; propValue.Mall_PVID = ""; propValue.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Spec_Value.Add(propValue); } } db.SaveChanges(); } } catch { } finally { if (db != null) { db.Dispose(); } } }
/// <summary> /// /// </summary> /// <returns></returns> public bool UpdateEmployeeInfo(Employee employee) { bool result = false; if (this.CurrentUserPermission.UPDATE_EMPLOYEE == 0) { throw new KMJXCException("没有权限更新员工信息"); } KuanMaiEntities db = new KuanMaiEntities(); try { User user = (from u in db.User where u.User_ID == employee.User_ID select u).FirstOrDefault<User>(); if (user != null) { Employee existing = (from e in db.Employee where e.User_ID == employee.User_ID select e).FirstOrDefault<Employee>(); if (existing == null) { db.Employee.Add(employee); } else { this.UpdateProperties(existing, employee); } result = true; } } catch { } finally { if (db != null) { db.Dispose(); } } return result; }
/// <summary> /// Sync actions with Permission object /// </summary> public static void SyncPermissionWithAction() { Type permission = typeof(Permission); FieldInfo[] fields = permission.GetFields(); if (fields == null || fields.Length <= 0) { return; } KuanMaiEntities db = new KuanMaiEntities(); try { List<AdminActionAttribute> cates=new List<AdminActionAttribute>(); List<Admin_Action> allActions=(from action in db.Admin_Action select action).ToList<Admin_Action>(); List<Admin_Category> allCates=(from cate in db.Admin_Category select cate).ToList<Admin_Category>(); foreach (FieldInfo field in fields) { AdminActionAttribute attr = field.GetCustomAttribute<AdminActionAttribute>(); Admin_Action action = (from a in allActions where a.action_name == field.Name select a).FirstOrDefault<Admin_Action>(); if (action == null) { action = new Admin_Action(); action.action_name = field.Name; action.enable = true; db.Admin_Action.Add(action); } if (attr != null) { action.category_id = attr.ID; action.action_description = attr.ActionDescription; AdminActionAttribute existed = (from pcate in cates where pcate.ID == attr.ID select pcate).FirstOrDefault<AdminActionAttribute>(); if (existed == null) { cates.Add(attr); } } } db.SaveChanges(); foreach (Admin_Action action in allActions) { bool found = false; foreach (FieldInfo field in fields) { if (action.action_name == field.Name) { found = true; break; } } if (!found) { action.enable = false; //db.Admin_Action.Remove(action); } } db.SaveChanges(); //category foreach (AdminActionAttribute pcate in cates) { Admin_Category dbCate=(from c in allCates where c.ID==pcate.ID select c).FirstOrDefault<Admin_Category>(); if (dbCate == null) { dbCate = new Admin_Category(); dbCate.ID = pcate.ID; dbCate.Name = pcate.CategoryName; dbCate.System_category = pcate.IsSystem; dbCate.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Admin_Category.Add(dbCate); } } db.SaveChanges(); } catch (Exception ex) { } finally { db.Dispose(); } }
/// <summary> /// /// </summary> /// <param name="propertyId"></param> /// <param name="values"></param> /// <returns></returns> public bool AddNewPropValue(int propertyId, List<string> values) { bool result = false; KuanMaiEntities db = new KuanMaiEntities(); try { int[] child_shops = (from c in this.DBChildShops select c.Shop_ID).ToArray<int>(); Product_Spec ps = (from pc in db.Product_Spec where pc.Product_Spec_ID == propertyId select pc).FirstOrDefault<Product_Spec>(); if (ps == null) { throw new KMJXCException("属性丢失,不能添加属性值"); } if (this.Shop.Shop_ID != this.Main_Shop.Shop_ID) { if (ps.Shop_ID == this.Main_Shop.Shop_ID) { throw new KMJXCException("您不能修改主店铺产品库存属性"); } if (ps.Shop_ID == this.Shop.Shop_ID) { throw new KMJXCException("您不能其他主店铺产品库存属性"); } } else { if (ps.Shop_ID != this.Main_Shop.Shop_ID && !child_shops.Contains(ps.Shop_ID)) { throw new KMJXCException("您不能修改其他店铺的产品库存属性,只能修改主店铺或者子店铺产品库存属性"); } } StringBuilder error = new StringBuilder(); if (values != null && values.Count > 0) { foreach (string value in values) { Product_Spec_Value pv = (from psv in db.Product_Spec_Value where psv.Product_Spec_ID == propertyId && psv.Name == value select psv).FirstOrDefault<Product_Spec_Value>(); if (pv != null) { error.Append("属性值:"+value+" 已经存在<br/>"); continue; } pv = new Product_Spec_Value(); pv.Product_Spec_ID = propertyId; pv.Name = value; pv.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); pv.User_ID = this.CurrentUser.ID; db.Product_Spec_Value.Add(pv); } db.SaveChanges(); result = true; if (!string.IsNullOrEmpty(error.ToString())) { result = false; throw new KMJXCException(error.ToString()); } } base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = this.Shop.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_PRODUCT_PROPERTY }, Description = "" }); } catch (KMJXCException ex) { throw ex; } catch (Exception nex) { } finally { if (db != null) { db.Dispose(); } } return result; }
/// <summary> /// Update product information /// </summary> /// <param name="product"></param> /// <returns></returns> public bool UpdateProduct(ref BProduct bproduct) { BProduct product = bproduct; if (this.CurrentUserPermission.UPDATE_PRODUCT == 0) { throw new KMJXCException("没有权限更新产品"); } bool result = false; KuanMaiEntities db = new KuanMaiEntities(); try { Product dbProduct=(from pdt in db.Product where pdt.Product_ID==product.ID select pdt).FirstOrDefault<Product>(); if (dbProduct == null) { throw new KMJXCException("您要修改的产品信息不存在"); } if (this.Shop.Shop_ID != this.Main_Shop.Shop_ID) { if (dbProduct.Shop_ID == this.Main_Shop.Shop_ID) { throw new KMJXCException("您不能修改主店铺产品"); } if (dbProduct.Shop_ID != this.Shop.Shop_ID) { throw new KMJXCException("您不能其他主店铺产品"); } } else { int[] child_shops=(from c in this.DBChildShops select c.Shop_ID).ToArray<int>(); if (dbProduct.Shop_ID != this.Main_Shop.Shop_ID && !child_shops.Contains(dbProduct.Shop_ID)) { throw new KMJXCException("您无法修改其他店铺的产品,只能修改主店铺或者子店铺产品"); } } dbProduct.Name = product.Title; dbProduct.Description = product.Description; if (product.Category != null) { dbProduct.Product_Class_ID = product.Category.ID; } dbProduct.Update_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbProduct.Update_User_ID = this.CurrentUser.ID; //update images if (product.Images!=null && product.Images.Count > 0) { List<Image> existedImages=(from img in db.Image where img.ProductID==product.ID select img).ToList<Image>(); //Update new uploaded images foreach (Image newimg in product.Images) { Image tmp = (from eted in existedImages where eted.ID == newimg.ID select eted).FirstOrDefault<Image>(); if (tmp == null) { Image newone = (from ni in db.Image where ni.ID == newimg.ID select ni).FirstOrDefault<Image>(); newone.ProductID = product.ID; db.Image.Add(newone); } } //Remove deleted images string rootPath = product.FileRootPath; foreach (Image oldImg in existedImages) { Image tmp = (from eted in product.Images where eted.ID == oldImg.ID select eted).FirstOrDefault<Image>(); if (tmp == null) { db.Image.Remove(oldImg); if (rootPath != null && System.IO.File.Exists(rootPath + oldImg.Path)) { System.IO.File.Delete(rootPath + oldImg.Path); } } } } //update suppliers if (product.Suppliers != null) { foreach (Supplier s in product.Suppliers) { Product_Supplier ps = new Product_Supplier() { Product_ID = product.ID, Supplier_ID = s.Supplier_ID, Enabled=true,Created=DateTimeUtil.ConvertDateTimeToInt(DateTime.Now),Created_By=this.CurrentUser.ID }; db.Product_Supplier.Add(ps); } } //update children List<Product> children=(from p in db.Product where p.Parent_ID==dbProduct.Product_ID select p).ToList<Product>(); foreach (Product child in children) { child.Name = dbProduct.Name; child.Product_Class_ID = dbProduct.Product_Class_ID; child.Description = dbProduct.Description; } db.SaveChanges(); if (product.Children != null && product.Children.Count > 0) { foreach (BProduct child in product.Children) { if (child.ID == 0) { //create new child product with properties child.Parent = product; child.Children = null; this.CreateProduct(child); } else { //Update properties if (child.Properties != null && child.Properties.Count > 0) { List<Product_Specifications> properties = (from prop in db.Product_Specifications where prop.Product_ID == child.ID select prop).ToList<Product_Specifications>(); List<Product_Specifications> newProps = new List<Product_Specifications>(); if (properties.Count > 0) { //current just support edit existed property's value, doesn't support deleting property foreach (BProductProperty p in child.Properties) { Product_Specifications psprop = (from ep in properties where ep.Product_Spec_ID == p.PID select ep).FirstOrDefault<Product_Specifications>(); if (psprop == null) { //cretae new property for existed product psprop = new Product_Specifications() { Product_ID=child.ID, Product_Spec_ID=p.PID, Product_Spec_Value_ID=p.PVID, Created=DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), User_ID=this.CurrentUser.ID }; db.Product_Specifications.Add(psprop); } else { //update existed property's value if (psprop.Product_Spec_Value_ID != p.PVID) { psprop.Product_Spec_Value_ID = p.PVID; } } } db.SaveChanges(); } } } } } bproduct = this.GetProductFullInfo(product.ID); base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = bproduct.Shop.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.UPDATE_PRODUCT }, Description = "商品编号:" + bproduct.ID+ "\n商品名称:" + bproduct.Title }); result = true; } catch (KMJXCException kex) { throw kex; } catch { } finally { db.Dispose(); } return result; }
public static void SyncUserAction() { Type action = typeof(UserLogAction); FieldInfo[] fields = action.GetFields(); if (fields == null || fields.Length <= 0) { return; } KuanMaiEntities db = new KuanMaiEntities(); try { List<User_Action> allActions=(from ac in db.User_Action select ac).ToList<User_Action>(); foreach (FieldInfo field in fields) { UserActionAttribute attr = field.GetCustomAttribute<UserActionAttribute>(); int aValue = (int)field.GetValue(null); User_Action userAc=(from ac in allActions where ac.Action_ID==aValue && ac.Action_Name==field.Name select ac).FirstOrDefault<User_Action>(); if (userAc == null) { userAc = new User_Action(); userAc.Action_ID = aValue; userAc.Action_Name = field.Name; userAc.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); if (attr != null) { userAc.Action_Description = attr.Description; } db.User_Action.Add(userAc); } else { if (attr != null) { userAc.Action_Description = attr.Description; } } } db.SaveChanges(); } catch(Exception ex) { } finally { if (db != null) { db.Dispose(); } } }
/// <summary> /// Gets full local product information /// </summary> /// <param name="productId"></param> /// <returns></returns> public BProduct GetProductFullInfo(int productId,string mall_id=null) { if (productId == 0 && string.IsNullOrEmpty(mall_id)) { throw new KMJXCException("获取产品详细信息时候,必须输入产品编号或者已关联的商城宝贝编号"); } BProduct product = null; KuanMaiEntities db = new KuanMaiEntities(); try { if (productId == 0 && !string.IsNullOrEmpty(mall_id)) { productId=(from mp in db.Mall_Product where mp.Mall_ID==mall_id select mp.Outer_ID).FirstOrDefault<int>(); } if (productId == 0) { throw new KMJXCException("获取产品详细信息时候,必须输入产品编号或者已关联的商城宝贝编号"); } product = (from pudt in db.Product where pudt.Product_ID == productId select new BProduct { Shop = (from sp in db.Shop where sp.Shop_ID == pudt.Shop_ID select sp).FirstOrDefault<Shop>(), ID = pudt.Product_ID, Description = pudt.Description, Title = pudt.Name, Code = pudt.Code, CreateTime = pudt.Create_Time, Category = (from c in db.Product_Class where pudt.Product_Class_ID == c.Product_Class_ID select new BCategory { Name = c.Name, ID = c.Product_Class_ID, ParentID=(int)c.Parent_ID }).FirstOrDefault<BCategory>(), User = (from u in db.User where u.User_ID == pudt.User_ID select new BUser { ID = u.User_ID, Mall_Name = u.Mall_Name, Mall_ID = u.Mall_ID, }).FirstOrDefault<BUser>() }).FirstOrDefault<BProduct>(); if (product != null) { product.Images = (from img in db.Image where img.ProductID == product.ID select img).ToList<Image>(); product.Suppliers = (from sp in db.Supplier join ps in db.Product_Supplier on sp.Supplier_ID equals ps.Supplier_ID where ps.Product_ID == product.ID && ps.Enabled==true select sp ).ToList<Supplier>(); //product.Properties = properties; List<Product> children = (from p in db.Product where p.Parent_ID == product.ID select p).ToList<Product>(); int[] children_ids=(from c in children select c.Product_ID).ToArray<int>(); List<BProductProperty> properties = (from pp in db.Product_Specifications where children_ids.Contains(pp.Product_ID) select new BProductProperty { ProductID=pp.Product_ID, PID = pp.Product_Spec_ID, PName = (from prop in db.Product_Spec where prop.Product_Spec_ID == pp.Product_Spec_ID select prop.Name).FirstOrDefault<string>(), PVID = pp.Product_Spec_Value_ID, PValue = (from propv in db.Product_Spec_Value where propv.Product_Spec_Value_ID == pp.Product_Spec_Value_ID select propv.Name).FirstOrDefault<string>() }).OrderBy(p=>p.PID).ToList<BProductProperty>(); if (children != null && children.Count > 0) { if (product.Children == null) { product.Children = new List<BProduct>(); } foreach (Product pdt in children) { BProduct child = new BProduct() { ID=pdt.Product_ID,Title=product.Title}; child.Properties=(from prop in properties where prop.ProductID==child.ID select prop).ToList<BProductProperty>(); product.Children.Add(child); } } } } catch (KMJXCException kex) { throw kex; } catch (Exception ex) { throw ex; } finally { db.Dispose(); } return product; }
protected void Initialize() { if (this.Mall_Type_ID <= 0) { throw new Exception("Mall Type ID is invalid for open API"); } KuanMaiEntities db = new KuanMaiEntities(); try { var openKey = db.Open_Key.Where(p => p.Mall_Type_ID == this.Mall_Type_ID); if (openKey != null) { List<Open_Key> keys = openKey.ToList<Open_Key>(); if (keys.Count == 1) { this.Open_Key = keys[0]; } else { throw new Exception("More app key and secret pair for Mall Type ID:" + this.Mall_Type_ID); } } else { throw new Exception("Didn't find app key and secret for Mall Type ID:" + this.Mall_Type_ID); } var t = from tp in db.Mall_Type where tp.Mall_Type_ID == this.Mall_Type_ID select new BMallType { ID=tp.Mall_Type_ID,Name=tp.Name }; if (t.ToList<BMallType>().Count == 1) { this.MallType = t.ToList<BMallType>()[0]; } } catch (Exception ex) { throw ex; } finally { if (db != null) { db.Dispose(); } } }
/// <summary> /// Add one enter stock detail record /// </summary> /// <param name="detail"></param> /// <returns></returns> public bool EnterStockDetail(int stockId,BEnterStockDetail detail) { bool result = false; if (this.CurrentUserPermission.ADD_ENTER_STOCK == 0) { throw new KMJXCException("没有新增入库单的权限"); } if (detail.EnterStock == null && stockId<=0) { throw new KMJXCException("必须选择入库单"); } KuanMaiEntities db = new KuanMaiEntities(); if (stockId > 0) { Enter_Stock dbStock= (from st in db.Enter_Stock where st.Enter_Stock_ID == stockId select st).FirstOrDefault<Enter_Stock>(); detail.EnterStock = new BEnterStock() { ID = stockId, StoreHouse = new BStoreHouse() { ID = dbStock.StoreHouse_ID } }; } else { if (detail.EnterStock.ID <= 0) { throw new KMJXCException("必须选择入库单"); } } if (detail.Product == null) { throw new KMJXCException("必须指定商品"); } if (detail.Quantity == 0) { throw new KMJXCException("数量必须大于零"); } try { Enter_Stock_Detail dbDetail = new Enter_Stock_Detail(); dbDetail.Create_Date = detail.Created; dbDetail.Enter_Stock_ID = detail.EnterStock.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; db.Enter_Stock_Detail.Add(dbDetail); db.SaveChanges(); //update stock pile Stock_Pile stockPile = (from sp in db.Stock_Pile where sp.Product_ID == dbDetail.Product_ID && sp.StockHouse_ID==detail.EnterStock.StoreHouse.ID select sp).FirstOrDefault<Stock_Pile>(); if (stockPile != null) { stockPile.Quantity = stockPile.Quantity + dbDetail.Quantity; stockPile.Price = dbDetail.Price; if (stockPile.First_Enter_Time == 0) { stockPile.First_Enter_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); } } result = true; } catch { } finally { if (db != null) { db.Dispose(); } } return result; }
/// <summary> /// /// </summary> /// <param name="categoryId"></param> /// <param name="propName"></param> /// <param name="propValues"></param> /// <returns></returns> public BProperty CreateProperty(int categoryId,string propName,List<string> propValues,int shop_id=0) { BProperty bproperty = null; if (string.IsNullOrEmpty(propName)) { throw new KMJXCException("属性名称不能为空"); } KuanMaiEntities db = new KuanMaiEntities(); try { var existed = from props in db.Product_Spec where props.Name==propName select props; if (categoryId > 0) { //existed = existed.Where(a=>a.Product_Class_ID==categoryId); } if (this.Shop.Parent_Shop_ID > 0) { var pexisted = existed.Where(a=>a.Shop_ID==this.Main_Shop.Shop_ID); if (pexisted.FirstOrDefault<Product_Spec>() != null) { throw new KMJXCException("主店铺已经有此属性,不能重复创建,请使用现有主店铺的属性"); } } existed = existed.Where(a => a.Shop_ID == this.Shop.Shop_ID); if (existed.FirstOrDefault<Product_Spec>() != null) { throw new KMJXCException("此属性已经存在,不能重复创建"); } Product_Spec property = new Product_Spec(); property.Product_Class_ID = categoryId; property.Name = propName; property.User_ID = this.CurrentUser.ID; property.Shop_ID = this.Shop.Shop_ID; if (shop_id > 0) { property.Shop_ID = shop_id; } property.Mall_PID = ""; property.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Spec.Add(property); db.SaveChanges(); if (property.Product_Spec_ID <= 0) { throw new KMJXCException("属性创建失败"); } bproperty = new BProperty(); bproperty.ID = property.Product_Spec_ID; bproperty.Created_By = this.CurrentUser; bproperty.Created = (int)property.Created; bproperty.CategoryId = categoryId; bproperty.MID = ""; bproperty.Name = propName; if (propValues != null) { if (bproperty.Values == null) { bproperty.Values = new List<Product_Spec_Value>(); } foreach (string v in propValues) { Product_Spec_Value psv = new Product_Spec_Value(); psv.Mall_PVID = ""; psv.Name = v; psv.Product_Spec_ID = property.Product_Spec_ID; psv.Product_Spec_Value_ID = 0; psv.User_ID = this.CurrentUser.ID; psv.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Spec_Value.Add(psv); } db.SaveChanges(); bproperty.Values=(from pv in db.Product_Spec_Value where pv.Product_Spec_ID==property.Product_Spec_ID select pv).ToList<Product_Spec_Value>(); } } catch(KMJXCException ex) { throw ex; } finally { if (db != null) { db.Dispose(); } } return bproperty; }
/// <summary> /// Update supplier basic information /// </summary> /// <param name="supplier"></param> /// <returns></returns> public bool UpdateSupplier(Supplier supplier) { bool result = false; if (this.CurrentUserPermission.UPDATE_SUPPLIER == 0) { throw new KMJXCException("没有权限更新供应商"); } if (string.IsNullOrEmpty(supplier.Name)) { throw new KMJXCException("供应商名称不能为空"); } KuanMaiEntities db = null; try { db = new KuanMaiEntities(); int[] child_shops = (from c in this.DBChildShops select c.Shop_ID).ToArray<int>(); Supplier existed = (from supp in db.Supplier where supp.Supplier_ID == supplier.Supplier_ID select supp).FirstOrDefault<Supplier>(); if (existed == null) { throw new KMJXCException("要修改的供应商不存在"); } var obj = (from sp in db.Supplier where (child_shops.Contains(sp.Shop_ID) || sp.Shop_ID == this.Shop.Shop_ID || sp.Shop_ID == this.Main_Shop.Shop_ID) && supplier.Name.Contains(sp.Name) && sp.Supplier_ID!=existed.Supplier_ID select sp); if (obj.ToList<Supplier>().Count > 0) { throw new KMJXCException("供应商名称已经存在,请换个名字"); } if (this.Shop.Shop_ID != this.Main_Shop.Shop_ID) { if (existed.Shop_ID == this.Main_Shop.Shop_ID) { if (this.CurrentUser.Shop.ID != existed.Shop_ID) { throw new KMJXCException("您不能修改主店铺供应商"); } } else if (existed.Shop_ID!=this.Shop.Shop_ID) { throw new KMJXCException("您不能其他主店铺供应商"); } } else { if (existed.Shop_ID != this.Main_Shop.Shop_ID && !child_shops.Contains(existed.Shop_ID)) { throw new KMJXCException("您不能修改其他店铺的供应商,只能修改主店铺或者子店铺供应商"); } } supplier.Modified = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); supplier.Modified_By = this.CurrentUser.ID; this.UpdateProperties(existed, supplier); db.SaveChanges(); result = true; } catch (Exception ex) { throw ex; } finally { db.Dispose(); } return result; }
/// <summary> /// /// </summary> /// <param name="categoryId"></param> /// <returns></returns> public bool DisableCategory(int categoryId) { bool result = false; if (this.CurrentUserPermission.DISABLE_CATEGORY == 0) { throw new KMJXCException("没有权限禁用类目"); } KuanMaiEntities db = new KuanMaiEntities(); try { Product_Class cate=(from pc in db.Product_Class where pc.Product_Class_ID==categoryId select pc).FirstOrDefault<Product_Class>(); if (cate == null) { throw new KMJXCException("要操作的类目不存在"); } cate.Enabled = false; db.SaveChanges(); result = true; } catch (KMJXCException kex) { throw kex; } catch (Exception ex) { } finally { if (db != null) { db.Dispose(); } } return result; }
/// <summary> /// /// </summary> /// <param name="action"></param> public void CreateActionLog(BUserActionLog action) { if (action == null || action.Shop==null) { return; } KuanMaiEntities db = null; try { db = new KuanMaiEntities(); User_Action_Log log = new User_Action_Log(); log.Action = action.Action.Action_ID; log.Description = action.Description; if (action.User != null && action.User.ID > 0) { log.User_ID = action.User.ID; } else { log.User_ID = this.CurrentUser.ID; } log.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); if (string.IsNullOrEmpty(log.Description)) { log.Description = ""; } log.Shop_ID = action.Shop.ID; db.User_Action_Log.Add(log); db.SaveChanges(); } catch (DbEntityValidationException ex) { } catch (Exception ex) { } finally { if (db != null) { db.Dispose(); } } }
/// <summary> /// /// </summary> /// <param name="categoryId"></param> /// <returns></returns> public List<BProperty> GetProperties(int categoryId, int propId=0, string sortBy = null, string dir = null) { List<BProperty> properties = new List<BProperty>(); KuanMaiEntities db = new KuanMaiEntities(); try { int[] childs=(from c in this.DBChildShops select c.Shop_ID).ToArray<int>(); var props = from prop in db.Product_Spec //where (prop.Shop_ID == this.Shop.Shop_ID || prop.Shop_ID == this.Main_Shop.Shop_ID) select prop; if (this.Shop.Shop_ID == this.Main_Shop.Shop_ID) { props = props.Where(prop => (prop.Shop_ID == this.Shop.Shop_ID || childs.Contains(prop.Shop_ID))); } else { props = props.Where(prop => (prop.Shop_ID == this.Shop.Shop_ID || prop.Shop_ID==this.Main_Shop.Shop_ID)); } if (categoryId > 0) { props = props.Where(a => a.Product_Class_ID == categoryId); } if (propId > 0) { props=props.Where(a => a.Product_Spec_ID == propId); } var efProps = from p in props join shop in db.Shop on p.Shop_ID equals shop.Shop_ID into LShop from l_shop in LShop.DefaultIfEmpty() join user in db.User on p.User_ID equals user.User_ID into LUser from l_user in LUser.DefaultIfEmpty() join category in db.Product_Class on p.Product_Class_ID equals category.Product_Class_ID into LCategory from l_category in LCategory.DefaultIfEmpty() select new BProperty { ID = p.Product_Spec_ID, MID = p.Mall_PID, Name = p.Name, Shop = l_shop != null ? new BShop { ID = l_shop.Shop_ID, Title = l_shop.Name, Created = (int)l_shop.Created, Description = l_shop.Description } : new BShop { ID = 0, Title = "", Created = 0, Description = "" }, CategoryId = p.Product_Class_ID, Created = (int)p.Created, Created_By = l_user != null ? new BUser { ID = l_user.User_ID, Name = l_user.Name, Mall_Name = l_user.Mall_Name } : new BUser { ID = 0, Name = "", Mall_Name = "" }, //Values = (from ps in db.Product_Spec_Value where ps.Product_Spec_ID == p.Product_Spec_ID select ps).ToList<Product_Spec_Value>() Category = l_category }; properties = efProps.ToList<BProperty>(); if (properties.Count > 0) { foreach (BProperty p in properties) { p.Values = (from ps in db.Product_Spec_Value where ps.Product_Spec_ID == p.ID select ps).ToList<Product_Spec_Value>(); BShop shop = p.Shop; if (shop != null) { if (shop.ID==this.Main_Shop.Shop_ID) { p.FromMainShop = true; //p.Shop.Parent = (from sp in db.Shop // where sp.Shop_ID == shop.Parent_Shop_ID // select new BShop // { // ID = sp.Shop_ID, // Title = sp.Name, // Created = (int)sp.Created, // }).FirstOrDefault<BShop>(); } else if(childs!=null && childs.Contains(p.Shop.ID)){ p.FromChildShop = true; } } } } } catch(Exception ex) { } finally { if (db != null) { db.Dispose(); } } return properties; }
/// <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(); } }
/// <summary> /// /// </summary> /// <returns></returns> public Product_Spec CreateNewProperty(int categoryId, string propertyName, List<string> value) { Product_Spec ps=null; if (string.IsNullOrEmpty(propertyName)) { throw new KMJXCException("属性名称不能为空"); } KuanMaiEntities db = new KuanMaiEntities(); List<Product_Spec> properties = new List<Product_Spec>(); try { var propes=from props in db.Product_Spec where props.Name==propertyName && props.Shop_ID==this.Shop.Shop_ID select props; if (categoryId > 0) { propes.Where(c=>c.Product_Class_ID==categoryId); } properties = propes.ToList<Product_Spec>(); if (properties.Count > 0) { throw new KMJXCException("名为"+propertyName+"已经存在"); } ps = new Product_Spec(); ps.Product_Class_ID = categoryId; ps.Shop_ID = this.Shop.Shop_ID; ps.User_ID = this.CurrentUser.ID; ps.Name = propertyName; ps.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); ps.Mall_PID = ""; db.Product_Spec.Add(ps); db.SaveChanges(); int psId = ps.Product_Spec_ID; if (psId == 0) { throw new KMJXCException("产品属性创建失败"); } if (value != null) { foreach (string v in value) { Product_Spec_Value psv = new Product_Spec_Value(); psv.Product_Spec_ID = psId; psv.Name = v; psv.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); psv.User_ID = ps.User_ID; psv.Mall_PVID = ""; } } } catch { } finally { if (db != null) { db.Dispose(); } } return ps; }
/// <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(); } }
/// <summary> /// Create new user /// </summary> /// <param name="user"></param> /// <returns></returns> public BUser CreateNewUser(BUser user) { if (user == null) { throw new UserException("用户实体不能为空引用"); } if (string.IsNullOrEmpty(user.Name)) { throw new UserException("用户名不能为空"); } if (this.CurrentUserPermission.ADD_USER == 0) { throw new UserException("没有权限创建新用户"); } KuanMaiEntities dba = new KuanMaiEntities(); try { if (GetUser(user) != null) throw new UserException("用户名已经存在"); User dbUser = new User(); dbUser.User_ID = user.ID; dbUser.Mall_ID = user.Mall_ID; dbUser.Mall_Name = user.Mall_Name; dbUser.Name = user.Name; dbUser.Mall_Type = user.Type.ID; if (user.Parent != null) { dbUser.Parent_Mall_ID = user.Parent.Mall_ID; dbUser.Parent_Mall_Name = user.Parent.Mall_Name; dbUser.Parent_User_ID = user.Parent.ID; } dba.User.Add(dbUser); dba.SaveChanges(); return user; } catch (Exception ex) { throw ex; } finally { if (dba != null) { dba.Dispose(); } } }
/// <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; }