public ActionResult Edit([Bind(Include = "id,full_name,date_of_birth,mobile,address,facebook,email,desc,flag")] Models.Customer customer_tmp, FormCollection collection) { try { if (ModelState.IsValid) { var customer = db.Customers.Find(customer_tmp.id); if (customer_tmp.dateOfBirth != null) { customer.dateOfBirth = customer_tmp.dateOfBirth.ToString(); } customer.fullName = customer_tmp.fullName; customer.mobile = customer_tmp.mobile; customer.address = customer_tmp.address; customer.facebook = customer_tmp.facebook; customer.email = customer_tmp.email; //Images var images = TM.IO.UploadImages(Request.Files, DirUpload.imagesCustomer, 3); var tmp = images.UploadFileString(); if (tmp != null) { customer.images = tmp; } customer.desc = customer_tmp.desc; customer.flag = customer_tmp.flag; db.Entry(customer).State = EntityState.Modified; var customerPersonInfo = db.SubItems.Where(d => d.idKey == customer_tmp.id).ToList(); foreach (var item in customerPersonInfo) { db.SubItems.Remove(item); } var PersonInfo = db.SubItems.Where(d => d.appKey == AppKey.personInfo && d.flag > 0).OrderBy(d => d.value).ToList(); foreach (var item in PersonInfo) { var sub_item = new Models.SubItem(); var sub_value = collection[item.id.ToString() + "[]"].Split(','); sub_item.id = Guid.NewGuid(); sub_item.appKey = AppKey.customer; sub_item.idKey = customer.id; sub_item.mainKey = item.id.ToString(); sub_item.value = item.value; if (sub_value.Length > 0) { sub_item.subValue = sub_value[0]; } if (sub_value.Length > 1) { sub_item.extras = sub_value[1]; } sub_item.createdBy = Common.Auth.getUser().id.ToString(); sub_item.createdAt = DateTime.Now; sub_item.flag = 1; db.SubItems.Add(sub_item); } db.SaveChanges(); this.success(TM.Common.Language.msgUpdateSucsess); return(RedirectToAction("Edit")); } else { this.danger(TM.Common.Language.msgError); } } catch (Exception ex) { this.danger(TM.Common.Language.msgUpdateError); } ViewBag.PersonInfo = getPersonInfo(); return(View(customer_tmp)); }
public ActionResult Create([Bind(Include = "full_name,date_of_birth,mobile,address,facebook,email,card_id,desc,flag")] Models.Customer customer, FormCollection collection) { try { if (ModelState.IsValid) { customer.id = Guid.NewGuid(); if (customer.dateOfBirth != null) { customer.dateOfBirth = customer.dateOfBirth.ToString(); } //Images var images = TM.IO.UploadImages(Request.Files, DirUpload.imagesCustomer, 3); customer.images = images.UploadFileString(); customer.createdBy = Common.Auth.getUser().id.ToString(); customer.createdAt = DateTime.Now; db.Customers.Add(customer); var PersonInfo = db.SubItems.Where(d => d.appKey == AppKey.personInfo && d.flag > 0).OrderBy(d => d.value).ToList(); foreach (var item in PersonInfo) { var sub_item = new Models.SubItem(); var sub_value = collection[item.id.ToString() + "[]"].Split(','); sub_item.id = Guid.NewGuid(); sub_item.appKey = AppKey.customer; sub_item.idKey = customer.id; sub_item.mainKey = item.id.ToString(); sub_item.value = item.value; if (sub_value.Length > 0) { sub_item.subValue = sub_value[0]; } if (sub_value.Length > 1) { sub_item.extras = sub_value[1]; } sub_item.createdBy = Common.Auth.getUser().id.ToString(); sub_item.createdAt = DateTime.Now; sub_item.flag = 1; db.SubItems.Add(sub_item); } //Card var cardType = db.Items.Find(Guid.Parse(collection["code_key"])); var card = new Models.Item(); card.id = Guid.NewGuid(); card.codeKey = collection["code_key"]; card.quantity = long.Parse(collection["quantity"]); card.title = collection["totalPay"]; card.desc = collection["desc_card"]; card.price = decimal.Parse(collection["price"]); card.priceOld = decimal.Parse(collection["price_old"]); card.flag = 1; card = AddCardExtend(card, cardType, customer.id.ToString(), collection["startDate"], collection["quantity"]); db.Items.Add(card); //card.id = Guid.NewGuid(); //card.app_key = AppKey.cardPerson; //card.id_key = customer.id + "," + card.id_key; //card.code_key = customer.card_id; //card.title = collection["startDate"] + "," + collection["endDate"]; //card.desc = collection["totalPay"]; //card.price_old = decimal.Parse(collection["totalNotPay"]); //card.price = decimal.Parse(collection["totalPaid"]); //card.flag = customer.flag; //card.created_by = VinaphoneCommon.Auth.id().ToString(); //card.created_at = DateTime.Now; //db.items.Add(card); db.SaveChanges(); this.success(TM.Common.Language.msgCreateSucsess); return(RedirectToAction("Create")); } else { this.danger(TM.Common.Language.msgError); } } catch (Exception ex) { this.danger(TM.Common.Language.msgCreateError); } ViewBag.PersonInfo = getPersonInfo(); return(View(customer)); }