public ActionResult Create(DiscountModels model) { try { if (!model.BType) { if (model.Value < 0 || model.Value > 100) { ModelState.AddModelError("Value", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Value must between 0 and 100")); } } string msg = ""; bool result = _factory.InsertOrUpdateDiscount(model, ref msg); if (result) { return(RedirectToAction("Index")); } else { //return RedirectToAction("Create"); ModelState.AddModelError("Name", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(msg)); return(View(model)); } } catch (Exception ex) { _logger.Error("Discount_Create: " + ex); return(new HttpStatusCodeResult(400, ex.Message)); } }
public DiscountModels GetDetail(string id) { try { DiscountModels model = _factory.GetListDiscount(null, id)[0]; model.BType = model.Type == (byte)Commons.EValueType.Currency ? true : false; return(model); } catch (Exception ex) { _logger.Error("Discount_Detail: " + ex); return(null); } }
public ActionResult Import(DiscountModels model) { try { if (model.ListStores == null) { ModelState.AddModelError("ListStores", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Please choose store.")); return(View(model)); } if (model.ExcelUpload == null || model.ExcelUpload.ContentLength <= 0) { ModelState.AddModelError("ExcelUpload", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Excel filename cannot be null")); return(View(model)); } ImportModel importModel = new ImportModel(); string msg = ""; StatusResponse response = _factory.Import(model.ExcelUpload, model.ListStores, ref importModel, ref msg); if (!response.Status) { ModelState.AddModelError("", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(response.MsgError)); return(View(model)); } // Delete File Excel and File Zip Image CommonHelper.DeleteFileFromServer(CommonHelper.GetFilePath(model.ExcelUpload)); //if (!ModelState.IsValid) // return View(model); if (msg.Equals("")) { return(View("ImportDetail", importModel)); } else { _logger.Error("Employee_Import: " + msg); ModelState.AddModelError("ExcelUpload", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(msg)); return(View(model)); } } catch (Exception e) { _logger.Error("Discount_Import: " + e); //return new HttpStatusCodeResult(400, e.Message); ModelState.AddModelError("ExcelUpload", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Import file have error.")); return(View(model)); } }
public bool InsertOrUpdateDiscount(DiscountModels model, ref string msg) { try { DiscountApiModels paraBody = new DiscountApiModels(); paraBody.AppKey = Commons.AppKey; paraBody.AppSecret = Commons.AppSecret; paraBody.CreatedUser = Commons.CreateUser; paraBody.RegisterToken = new RegisterTokenModels(); paraBody.Id = model.ID; paraBody.StoreId = model.StoreID; paraBody.Name = model.Name; paraBody.Description = model.Description; paraBody.Value = model.Value; paraBody.Type = model.Type = (model.BType ? (byte)Commons.EValueType.Currency : (byte)Commons.EValueType.Percent); paraBody.IsAllowOpenValue = model.IsAllowOpenValue; paraBody.IsApplyTotalBill = model.IsApplyTotalBill; paraBody.IsActive = model.IsActive; //==================== var result = (ResponseApiModels)ApiResponse.Post <ResponseApiModels>(Commons.CreateOrEditDiscount, null, paraBody); if (result != null) { if (result.Success) { return(true); } else { _logger.Error(result.Message); msg = result.Message; return(false); } } else { _logger.Error(result); return(false); } } catch (Exception e) { _logger.Error("Discount_InsertOrUpdate: " + e); return(false); } }
public ActionResult Export(DiscountModels model) { try { if (model.ListStores == null) { ModelState.AddModelError("ListStores", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Please choose store.")); return(View(model)); } XLWorkbook wb = new XLWorkbook(); var ws = wb.Worksheets.Add("Sheet1"); StatusResponse response = _factory.Export(ref ws, model.ListStores); if (!response.Status) { ModelState.AddModelError("", response.MsgError); return(View(model)); } ViewBag.wb = wb; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.Charset = System.Text.UTF8Encoding.UTF8.WebName; Response.ContentEncoding = System.Text.UTF8Encoding.UTF8; Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", String.Format(@"attachment;filename={0}.xlsx", CommonHelper.GetExportFileName("Discount").Replace(" ", "_"))); using (var memoryStream = new System.IO.MemoryStream()) { wb.SaveAs(memoryStream); memoryStream.WriteTo(HttpContext.Response.OutputStream); memoryStream.Close(); } HttpContext.Response.End(); return(RedirectToAction("Export")); } catch (Exception e) { _logger.Error("Discount_Export: " + e); //return new HttpStatusCodeResult(400, e.Message); ModelState.AddModelError("ListStores", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Import file have error.")); return(View(model)); } }
public ActionResult Edit(DiscountModels model) { try { if (string.IsNullOrEmpty(model.StoreID)) { ModelState.AddModelError("StoreID", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Please choose Store.")); } if (string.IsNullOrEmpty(model.Name)) { ModelState.AddModelError("Name", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Discount Name is required")); } if (!model.BType) { if (model.Value < 0 || model.Value > 100) { ModelState.AddModelError("Value", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Value must between 0 and 100")); } } if (!ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Edit", model)); } //==================== string msg = ""; var result = _factory.InsertOrUpdateDiscount(model, ref msg); if (result) { return(RedirectToAction("Index")); } else { //return PartialView("_Edit", model); ModelState.AddModelError("Name", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(msg)); return(PartialView("_Edit", model)); } } catch (Exception ex) { _logger.Error("Discount_Edit: " + ex); return(new HttpStatusCodeResult(400, ex.Message)); } }
public ActionResult Delete(DiscountModels model) { try { string msg = ""; var result = _factory.DeleteDiscount(model.ID, ref msg); if (!result) { //ModelState.AddModelError("Name", "Have a error when you delete an Discount"); ModelState.AddModelError("Name", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(msg)); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Delete", model)); } return(new HttpStatusCodeResult(HttpStatusCode.OK)); } catch (Exception ex) { _logger.Error("Discount_Delete: " + ex); ModelState.AddModelError("Name", _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Have an error when you delete a Discount")); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Delete", model)); } }
public DiscountModels CouponCode(string DiscountCode, string OrderID, string ProductID, string OrderDetailID, ref bool success, ref string msg) { DiscountModels item = new DiscountModels(); try { YourCartRequest paraBody = new YourCartRequest(); paraBody.DiscountCode = DiscountCode; paraBody.OrderID = OrderID; paraBody.OrderDetailID = OrderDetailID; paraBody.ID = ProductID; NSLog.Logger.Info("CouponCode Request: ", paraBody); var result = (NSApiResponse)ApiResponse.Post <NSApiResponse>(Commons.ClientSiteYourCartCouponCode, null, paraBody); NSLog.Logger.Info("CouponCode Result: ", result); dynamic data = result.Data; success = result.Success; msg = result.Message; var lstDataRaw = data["Discount"]; var lstObject = JsonConvert.SerializeObject(lstDataRaw); item = JsonConvert.DeserializeObject <DiscountModels>(lstObject); if (item == null) { item = new DiscountModels(); } NSLog.Logger.Info("CouponCode", item); return(item); } catch (Exception e) { NSLog.Logger.Error("CouponCode_Fail", e); return(item); } }
public ActionResult Create() { DiscountModels model = new DiscountModels(); return(View(model)); }
public ActionResult Export() { DiscountModels model = new DiscountModels(); return(View(model)); }
public PartialViewResult Delete(string id) { DiscountModels model = GetDetail(id); return(PartialView("_Delete", model)); }
public PartialViewResult Edit(string id) { DiscountModels model = GetDetail(id); return(PartialView("_Edit", model)); }
// IMPORT public StatusResponse Import(HttpPostedFileBase excelFile, List <string> lstStore, ref ImportModel importModel, ref string msg) { StatusResponse Response = new StatusResponse(); DataTable dt = new DataTable(); FileInfo[] lstFileImg = new FileInfo[] { }; Response = ProcessDataImport(ref dt, excelFile, "SBInventoryDiscount.xlsx", out lstFileImg); if (!Response.Status) { msg = Response.MsgError; return(Response); } List <DiscountModels> listData = new List <DiscountModels>(); ImportItem itemErr = null; bool flagInsert = true; string msgError = ""; foreach (var item in lstStore) { foreach (DataRow row in dt.Rows) { try { flagInsert = true; msgError = ""; string rowText = ""; for (int i = 0; i < dt.Columns.Count; i++) { rowText += row[i].ToString().Trim(); } if (string.IsNullOrEmpty(rowText)) { continue; } DiscountModels model = new DiscountModels(); model.Index = row[0].ToString(); // 1 - Discount Name model.Name = row[1].ToString().Trim().Replace(" ", " "); // 2 - IsActive model.IsActive = GetBoolValue(dt.Columns[2].ColumnName.Replace("#", "."), row[0].ToString(), row[2].ToString()); // 3 - Value Type string discountType = row[3].ToString().Trim().Replace(" ", " "); if (discountType == _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Percent")) { model.Type = (byte)Commons.EValueType.Percent; } else if (discountType == _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Currency")) { model.Type = (byte)Commons.EValueType.Currency; } else // Null { model.Type = (byte)Commons.EValueType.Currency; } // 4 - Value //if (!double.TryParse(row[4].ToString(), out doubleVal)) // throw new Exception(string.Format("Data in row #{0} is not valid, {1} must be a number , cannot import this row", row[0].ToString(), dt.Columns[4].ColumnName.Replace("#", "."))); double doubleVal = 0; double.TryParse(row[4].ToString(), out doubleVal); model.Value = doubleVal; string sIsAllowOpenValue = row[5].ToString().Equals("") ? "No" : row[5].ToString(); string sIsApplyToTotalBill = row[6].ToString().Equals("") ? "No" : row[6].ToString(); // 5 - IsAllowOpenDiscount model.IsAllowOpenValue = GetBoolValue(dt.Columns[5].ColumnName.Replace("#", "."), row[0].ToString(), sIsAllowOpenValue); model.IsApplyTotalBill = GetBoolValue(dt.Columns[6].ColumnName.Replace("#", "."), row[0].ToString(), sIsApplyToTotalBill); // 6 - Remark model.Description = row[7].ToString().Trim().Equals("") ? "$" : row[7].ToString().Trim(); model.StoreID = item; if (string.IsNullOrEmpty(model.Name)) { flagInsert = false; msgError = _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Discount Name is required"); } if (model.Type == (byte)Commons.EValueType.Percent) { if (model.Value < 0 || model.Value > 100) { flagInsert = false; msgError += "<br/>" + _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Please enter a value greater than or equal to 0"); } } else { if (model.Value < 0) { flagInsert = false; msgError += "<br/>" + _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Please enter a value greater than or equal to 0."); } } if (flagInsert) { listData.Add(model); } else { itemErr = new ImportItem(); itemErr.Name = model.Name; itemErr.ListFailStoreName.Add(""); itemErr.ListErrorMsg.Add("Row:" + model.Index + msgError); importModel.ListImport.Add(itemErr); } } catch (Exception e) { importModel.ListImport.Add(new ImportItem { Name = "Discount", ListFailStoreName = lstStore, ListErrorMsg = new List <string> { e.Message } }); } } } Response.Status = true; //===================== //try //{ DiscountApiModels paraBody = new DiscountApiModels(); paraBody.AppKey = Commons.AppKey; paraBody.AppSecret = Commons.AppSecret; paraBody.CreatedUser = Commons.CreateUser; paraBody.RegisterToken = new RegisterTokenModels(); paraBody.ListDiscount = listData; //==================== var result = (ResponseApiModels)ApiResponse.Post <ResponseApiModels>(Commons.ImportDiscount, null, paraBody); if (result != null) { dynamic data = result.Data; var lstC = data["ListProperty"]; var lstContent = JsonConvert.SerializeObject(lstC); var listError = JsonConvert.DeserializeObject <List <ImportResult> >(lstContent); foreach (ImportResult itemError in listError) { itemErr = new ImportItem(); itemErr.Name = itemError.Property; itemErr.ListFailStoreName.Add(itemError.StoreName); itemErr.ListErrorMsg.Add("Row: " + itemError.Index + "<br/>" + _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(itemError.Error)); importModel.ListImport.Add(itemErr); } if (importModel.ListImport.Count == 0) { ImportItem item = new ImportItem(); item.Name = "Discount"; item.ListSuccessStoreName.Add("Import Discount Successful"); importModel.ListImport.Add(item); } } return(Response); }