public ActionResult Create(StorageWarehousingRequest request) { //get customerid //var customer = _customerDa.GetByQrCode(request.CustomerCode); //if (customer == null) //{ // return Json(new JsonMessage() { Erros = true, Message = "Customer is not exits" }, JsonRequestBehavior.AllowGet); //} //if (_da.CheckExistOrder(request.Code, customer.ID)) //{ // return Json(new JsonMessage() { Erros = true, Message = "Mã đơn hàng đã tồn tại" }, JsonRequestBehavior.AllowGet); //} var model = new StorageWarehousing { Code = request.Code, Status = (int)StatusWarehouse.New, // CustomerId = customer.ID, DateCreated = DateTime.Now.TotalSeconds(), IsDelete = false, DateRecive = request.ReceiveDate, DN_RequestWare = new List <DN_RequestWare>(), UrlConfirm = request.UrlConfirm, AgencyId = AgencyId }; foreach (var item in request.RequestWares) { //get priceunit and costprice var category = _categoryDa.GetItemById(item.CateId); if (category == null || category.CostPrice == null) { return(Json(new JsonMessage(true, "Sản phẩm chưa có giá nhập"))); } //get policyagency var policyDa = new PolicyAgencyDA(); var requestWareDA = new RequestWareDA(); // lay so luong da giao var quantityFinish = requestWareDA.GetQuantityFinish(AgencyId, item.CateId, request.ReceiveDate.DecimalToDate().Month); //so luong tinh hoa hong var quantityCaculate = quantityFinish + item.Quantity; decimal sale = 0; // tim chinh sach dai ly var policies = policyDa.GetAll(item.CateId).OrderBy(m => m.Quantity).ToList(); var policyActive = policies.Where(m => quantityCaculate <= m.Quantity) .OrderBy(m => m.Quantity).FirstOrDefault(); if (policyActive != null) { sale = item.Quantity * policyActive.Profit; } else if (policies.Count > 0) { sale = item.Quantity * policies[policies.Count - 1].Profit; } var requestWare = new DN_RequestWare() { GID = Guid.NewGuid(), CateID = item.CateId, Quantity = item.Quantity, QuantityActive = item.Quantity, Price = category.Price, CostPrice = category.CostPrice, Sale = sale, //TotalPrice = item.TotalPrice, Today = request.ReceiveDate.DecimalToDate().Date.TotalSeconds(), IsDelete = false }; if (item.Details != null) { foreach (var detail in item.Details) { requestWare.DN_RequestWareDetail.Add(new DN_RequestWareDetail() { RequestWareId = Guid.NewGuid(), ProductId = detail.ProductId, Quantity = detail.Quantity, }); } } model.DN_RequestWare.Add(requestWare); } _da.Add(model); _da.Save(); //add log _storageWareHouseLogDa.AddLog(new StorageWarehousingLog() { StorageWarehousingId = model.ID, // NewValue = JsonConvert.SerializeObject(model, new BinaryConverter()) }); return(Json(model.ID, JsonRequestBehavior.AllowGet)); }