private string GetDeliveryStatus(HaiOrder order) { int?totalOrder = 0; int?totalDelivery = 0; var products = order.OrderProducts; foreach (var item in products) { totalOrder += item.Quantity; totalDelivery += item.QuantityFinish; } if (totalDelivery == 0) { return("incomplete"); } if (totalDelivery == totalOrder) { return("complete"); } if (totalOrder > totalDelivery) { return("less"); } if (totalOrder < totalDelivery) { return("more"); } return("incomplete"); }
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); }