/// <summary> /// Create new supplier /// </summary> /// <param name="supplier"></param> /// <returns></returns> public bool CreateSupplier(Supplier supplier) { bool result = false; if (this.CurrentUserPermission.ADD_SUPPLIER == 0) { throw new KMJXCException("没有权限添加新供应商"); } if (string.IsNullOrEmpty(supplier.Name)) { throw new KMJXCException("供应商名称不能为空"); } if (supplier.User_ID == 0 && this.CurrentUser!=null) { supplier.User_ID = this.CurrentUser.ID; } using (KuanMaiEntities db = new KuanMaiEntities()) { var obj = (from sp in db.Supplier where (sp.Shop_ID == this.Shop.Shop_ID || sp.Shop_ID == this.Main_Shop.Shop_ID) && supplier.Name.Contains(sp.Name) select sp); if (obj.ToList<Supplier>().Count > 0) { throw new KMJXCException("供应商名称已经存在"); } db.Supplier.Add(supplier); db.SaveChanges(); result = true; } return result; }
public ApiMessage Create() { ApiMessage message = new ApiMessage() { Status="ok"}; HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; HttpRequestBase request = context.Request; string user_id = User.Identity.Name; UserManager userMgr = new UserManager(int.Parse(user_id), null); BUser user = userMgr.CurrentUser; SupplierManager supplierManager = new SupplierManager(userMgr.CurrentUser, userMgr.Shop, userMgr.CurrentUserPermission); int province_id = 0; int city_id = 0; int district_id = 0; string address = request["address"]; string name = request["name"]; string fax = request["fax"]; string phone = request["phone"]; string postcode = request["postcode"]; string contact = request["contact"]; string remark=request["remark"]; int.TryParse(request["province_id"],out province_id); int.TryParse(request["city_id"],out city_id); int.TryParse(request["district_id"],out district_id); try { Supplier supplier = new Supplier(); supplier.Name = name; supplier.Phone = phone; supplier.PostalCode = postcode; supplier.Province_ID = province_id; if (province_id > 0 && province_id == city_id) { supplier.City_ID = district_id; } else { supplier.City_ID = city_id; } supplier.Address = address; supplier.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); supplier.Enabled = true; supplier.Fax = fax; supplier.Shop_ID = supplierManager.Shop.Shop_ID; supplier.Contact_Person = contact; supplier.User_ID = supplierManager.CurrentUser.ID; supplier.Remark = remark; if (supplierManager.CreateSupplier(supplier)) { message.Status = "ok"; message.Message = "供应商创建成功"; } } catch (KM.JXC.Common.KMException.KMJXCException kex) { message.Status = "failed"; message.Message = kex.Message; } catch (Exception ex) { message.Status = "failed"; message.Message = "未知错误"; } return message; }
/// <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; }