public UpdateDeliveryResult UpdateDelivery(string productId, string orderId, int quantity, string user, string token) { var log = new MongoHistoryAPI() { APIUrl = "/api/manageorder/updatedelivery", CreateTime = DateTime.Now, Sucess = 1 }; var result = new UpdateDeliveryResult() { id = "1", msg = "success" }; try { if (!mongoHelper.checkLoginSession(user, token)) { throw new Exception("Wrong token and user login!"); } HaiStaff staff = db.HaiStaffs.Where(p => p.UserLogin == user).FirstOrDefault(); if (staff == null) { throw new Exception("Sai thong tin nguoi dat"); } var checkOrderProduct = db.OrderProducts.Where(p => p.OrderId == orderId && p.ProductId == productId).FirstOrDefault(); if (checkOrderProduct == null) { throw new Exception("Sai thong tin"); } checkOrderProduct.QuantityFinish = quantity; checkOrderProduct.ModifyDate = DateTime.Now; db.Entry(checkOrderProduct).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); // var order = checkOrderProduct.HaiOrder; string deliveryStt = GetDeliveryStatus(order); var status = db.DeliveryStatus.Find(deliveryStt); order.DStatus = deliveryStt; result.deliveryStatus = status.Name; result.deliveryStatusCode = status.Id; result.finish = quantity; db.Entry(order).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); // // update process: nhan vien khoi tao var history = new OrderProductHistory() { Id = Guid.NewGuid().ToString(), CreateDate = DateTime.Now, Notes = "Nhan vien cong ty cap nhat", OrderId = checkOrderProduct.OrderId, ProductId = checkOrderProduct.ProductId, Quantity = quantity, StaffId = staff.Id }; db.OrderProductHistories.Add(history); db.SaveChanges(); HaiUtil.SendNotifi("Đơn hàng " + order.Code, "Đã cập nhật số lượng giao " + HaiUtil.ConvertProductQuantityText(checkOrderProduct.ProductInfo.Quantity, quantity, checkOrderProduct.ProductInfo.Unit) + "\nCho sản phẩm " + checkOrderProduct.ProductInfo.PName, staff.UserLogin, db, mongoHelper); } catch (Exception e) { result.id = "0"; result.msg = e.Message; log.Sucess = 0; } log.ReturnInfo = new JavaScriptSerializer().Serialize(result); mongoHelper.createHistoryAPI(log); return(result); }
public ResultInfo StaffComplete() { var log = new MongoHistoryAPI() { APIUrl = "/api/order/staffcomplete", CreateTime = DateTime.Now, Sucess = 1 }; var result = new ResultInfo() { id = "1", msg = "success" }; try { var requestContent = Request.Content.ReadAsStringAsync().Result; var jsonserializer = new JavaScriptSerializer(); var paser = jsonserializer.Deserialize <OrderInfoRequest>(requestContent); log.Content = new JavaScriptSerializer().Serialize(paser); if (!mongoHelper.checkLoginSession(paser.user, paser.token)) { throw new Exception("Wrong token and user login!"); } DateTime dateSuggest = DateTime.ParseExact(paser.timeSuggest, "d/M/yyyy", null); CInfoCommon cinfo = db.CInfoCommons.Where(p => p.CCode == paser.code).FirstOrDefault(); HaiStaff staff = db.HaiStaffs.Where(p => p.UserLogin == paser.user).FirstOrDefault(); if (staff == null) { throw new Exception("Sai thong tin nguoi dat"); } string orderType = "order"; if (paser.inCheckIn == 1) { orderType = "checkinorder"; } else if (paser.inCheckIn == 0) { orderType = "order"; } if (String.IsNullOrEmpty(orderType)) { throw new Exception("Sai thong tin dat hang"); } // if (paser.product == null || paser.product.Count() == 0) { throw new Exception("Thieu thong tin san pham"); } if (cinfo == null) { throw new Exception("Sai thong tin khach hang"); } // create code int? number = GetOrderNumber(cinfo.BranchCode); string code = cinfo.BranchCode + (100000 + number); // tạo đơn hàng var order = new HaiOrder() { Id = Guid.NewGuid().ToString(), OrderType = orderType, ShipType = paser.shipType, PayType = paser.payType, Agency = cinfo.Id, CreateDate = DateTime.Now, OrderStatus = "process", ReceiveAddress = paser.address, Notes = paser.notes, ExpectDate = dateSuggest, BrachCode = cinfo.BranchCode, Code = code, OrderNumber = number, ReceivePhone1 = paser.phone, UserCreate = paser.user, DateCode = DateTime.Now.Date.ToString("ddMMyyyy"), DebtTimeLine = paser.debtTime, DStatus = "incomplete", C1Code = "", C1Id = "", C1Name = "" }; if (paser.c1 == "000") { order.SalePlace = "B"; } else { order.SalePlace = "CI"; var checkC1 = db.C1Info.Where(p => p.Code == paser.c1).FirstOrDefault(); if (checkC1 == null) { throw new Exception("Sai thông tin nơi lấy hàng"); } order.C1Code = checkC1.Code; order.C1Id = checkC1.Id; order.C1Name = checkC1.StoreName; } db.HaiOrders.Add(order); db.SaveChanges(); // danh sach san pham mua double?priceTotal = 0; foreach (var item in paser.product) { // kiem tra san pham var checkProduct = db.ProductInfoes.Find(item.code); if (checkProduct != null && item.quantity > 0) { double?perPrice = checkProduct.Price != null ? checkProduct.Price : 0; double?price = perPrice * item.quantity; var productOrder = new OrderProduct() { OrderId = order.Id, ModifyDate = DateTime.Now, PerPrice = checkProduct.Price, Quantity = item.quantity, ProductId = checkProduct.Id, PriceTotal = price, QuantityFinish = 0, HasBill = item.hasBill }; db.OrderProducts.Add(productOrder); db.SaveChanges(); priceTotal += price; } } if (priceTotal == 0) { db.HaiOrders.Remove(order); db.SaveChanges(); throw new Exception("Sai thong tin san pham (ma san pham) hoac so luong"); } else { order.PriceTotal = priceTotal; db.Entry(order).State = EntityState.Modified; db.SaveChanges(); } // update process: nhan vien khoi tao OrderStaff orderStaff = new OrderStaff() { Id = Guid.NewGuid().ToString(), CreateTime = DateTime.Now, OrderId = order.Id, Notes = "Khoi tao", ProcessId = "create", StaffId = staff.Id }; db.OrderStaffs.Add(orderStaff); db.SaveChanges(); // gui thong bao // nhan vien HaiUtil.SendNotifi("Đơn hàng " + order.Code, "Bạn vừa tạo đơn hàng cho " + cinfo.CName, staff.UserLogin, db, mongoHelper); // c2 // HaiUtil.SendNotifi("Đơn hàng " + order.Code, "Bạn có 1 đơn hàng được tạo bởi nhân viên Công ty H.A.I " + staff.FullName + "(" + staff.Code + ")", cinfo.UserLogin, db, mongoHelper); } catch (Exception e) { result.id = "0"; result.msg = e.Message; log.Sucess = 0; } log.ReturnInfo = new JavaScriptSerializer().Serialize(result); mongoHelper.createHistoryAPI(log); return(result); }
public ResultInfo UpdateOrderProduct() { var log = new MongoHistoryAPI() { APIUrl = "/api/c1order/updateorderproduct", CreateTime = DateTime.Now, Sucess = 1 }; var result = new C1OrderResult() { id = "1", msg = "success" }; try { var requestContent = Request.Content.ReadAsStringAsync().Result; var jsonserializer = new JavaScriptSerializer(); var paser = jsonserializer.Deserialize <UpdateOrderRequest>(requestContent); log.Content = new JavaScriptSerializer().Serialize(paser); if (!mongoHelper.checkLoginSession(paser.user, paser.token)) { throw new Exception("Wrong token and user login!"); } var c1Info = db.C1Info.Where(p => p.CInfoCommon.UserLogin == paser.user).FirstOrDefault(); if (c1Info == null) { throw new Exception("Sai thong tin"); } var orderProduct = db.OrderProducts.Where(p => p.ProductId == paser.productId && p.OrderId == paser.orderId).FirstOrDefault(); if (orderProduct == null) { throw new Exception("Sai thong tin"); } if (orderProduct.HaiOrder.OrderStatus != "process") { throw new Exception("Đơn hàng không thể cập nhật"); } // check quantity int?quantityRemain = orderProduct.Quantity - orderProduct.QuantityFinish; if (quantityRemain < paser.quantity) { throw new Exception("Số lượng giao vượt quá"); } orderProduct.QuantityFinish = orderProduct.QuantityFinish + paser.quantity; db.Entry(orderProduct).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); // save history var history = new OrderProductHistory() { Id = Guid.NewGuid().ToString(), CreateDate = DateTime.Now, Notes = "Đại lý " + c1Info.StoreName + "(" + c1Info.Code + ") đã cập nhật", OrderId = orderProduct.OrderId, ProductId = orderProduct.ProductId, Quantity = paser.quantity }; db.OrderProductHistories.Add(history); db.SaveChanges(); // gui thong bao var order = db.HaiOrders.Find(paser.orderId); if (order != null) { var staff = order.OrderStaffs.Where(p => p.ProcessId == "create").FirstOrDefault(); // nhan vien if (staff != null) { HaiUtil.SendNotifi("Đơn hàng " + order.Code, "Cửa hàng " + c1Info.StoreName + " vừa giao hàng", staff.HaiStaff.UserLogin, db, mongoHelper); } // c2 HaiUtil.SendNotifi("Đơn hàng " + order.Code, "Cửa hàng " + c1Info.StoreName + " vừa giao hàng", order.CInfoCommon.UserLogin, db, mongoHelper); } } catch (Exception e) { result.id = "0"; result.msg = e.Message; log.Sucess = 0; } log.ReturnInfo = new JavaScriptSerializer().Serialize(result); mongoHelper.createHistoryAPI(log); return(result); }