public async Task <ApiResult <bool> > SplitBills(long id) { var order = await _context.OrderPlans .Include(x => x.OrderDetails) .Where(x => x.Id == id).FirstOrDefaultAsync(); var listOrderDetails = new List <OrderDetail>(); foreach (var item in order.OrderDetails) { var amount = item.Amount - item.EnterAmount; if (amount > 0) { var orderDetails = new OrderDetail() { Amount = amount, IdMaterials = item.IdMaterials, IdOrderPlan = item.IdOrderPlan, IdSupplier = item.IdSupplier, Price = item.Price, Unit = item.Unit }; listOrderDetails.Add(orderDetails); } } var stt = 1; Location: string code = order.Code + "-" + stt.ToString(); var checkCode = await _context.OrderPlans.AnyAsync(x => x.Code == code); if (checkCode) { stt++; goto Location; } var orderPlan = new OrderPlan() { Code = code, IdAuthority = order.IdAuthority, IdCreator = order.IdCreator, IdResponsible = order.IdResponsible, Censorship = false, CreatedDate = DateTime.Now, ExpectedDate = DateTime.Now, Name = order.Name + " (Tách)", Note = order.Note, Status = StatusOrderPlan.Unfinished, OrderDetails = listOrderDetails }; _context.OrderPlans.Add(orderPlan); await _context.SaveChangesAsync(); order.Status = StatusOrderPlan.Accomplished; var note = order.Note + " (Đã từng tách kế hoạch)"; order.Note = note; _context.OrderPlans.Update(order); await _context.SaveChangesAsync(); return(new ApiSuccessResult <bool>()); }
public async Task <ApiResult <bool> > CombinedBills(CombinedBills bundle) { if (bundle.Combined.Length < 2) { return(new ApiErrorResult <bool>("Gộp thất bại")); } List <OrderDetail> listOrderDetails = new List <OrderDetail>(); foreach (var item in bundle.Combined) { var orderPlan = await _context.OrderPlans.Include(x => x.OrderDetails) .Where(x => x.Id == item).FirstOrDefaultAsync(); foreach (var x in orderPlan.OrderDetails) { var orderDetail = new OrderDetail() { IdMaterials = x.IdMaterials, IdSupplier = x.IdSupplier, Amount = x.Amount - x.EnterAmount, Note = x.Note, Price = x.Price, Unit = x.Unit }; listOrderDetails.Add(orderDetail); } } // tạo code var code = await _context.ManageCodes.FirstOrDefaultAsync(x => x.Name == bundle.Code); var stt = 1; Location: var location = code.Location + stt; var str = code.Name + location; var checkCode = await _context.OrderPlans.AnyAsync(x => x.Code == str); if (checkCode) { stt++; goto Location; } var idcreate = await _userManager.FindByNameAsync(bundle.NameCreator); var order = new OrderPlan() { Code = str, Name = "(Gộp kế hoạch)", CreatedDate = DateTime.Now, ExpectedDate = DateTime.Now, Note = "(Gộp kế hoạch)", OrderDetails = listOrderDetails, IdCreator = idcreate.Id, IdResponsible = bundle.IdResponsible }; _context.OrderPlans.Add(order); await _context.SaveChangesAsync(); List <OrderPlan> orderPlanUpdate = new List <OrderPlan>(); foreach (var item in bundle.Combined) { var orderPlan = await _context.OrderPlans.Include(x => x.OrderDetails) .Where(x => x.Id == item).FirstOrDefaultAsync(); orderPlan.Status = StatusOrderPlan.Accomplished; orderPlanUpdate.Add(orderPlan); } _context.OrderPlans.UpdateRange(orderPlanUpdate); await _context.SaveChangesAsync(); return(new ApiSuccessResult <bool>()); }
public async Task <ApiResult <bool> > Create(CreateOrderPlan bundle) { var order = _context.OrderPlans.Include(x => x.OrderDetails); var orderDetails = new List <OrderDetail>(); if (bundle.Pack.Length > 0) { var i = 0; foreach (var item in bundle.Pack) { orderDetails.Add(new OrderDetail() { Amount = bundle.Amount[i], IdMaterials = bundle.IdMaterials[i], Unit = item }); i++; } } var oderPlan = new OrderPlan() { Name = bundle.Name, ExpectedDate = bundle.ExpectedDate, CreatedDate = DateTime.Now, Note = bundle.Note, IdResponsible = bundle.IdResponsible, OrderDetails = orderDetails }; var user = await _userManager.FindByNameAsync(bundle.NameCreator); oderPlan.IdCreator = user.Id; var code = await _context.ManageCodes.FirstOrDefaultAsync(x => x.Name == bundle.Code); var stt = 1; Location: var location = code.Location + stt; var str = code.Name + location; var checkCode = await _context.OrderPlans.AnyAsync(x => x.Code == str); if (checkCode) { stt++; goto Location; } code.Location = location; _context.ManageCodes.Update(code); await _context.SaveChangesAsync(); oderPlan.Code = str; _context.OrderPlans.Add(oderPlan); await _context.SaveChangesAsync(); return(new ApiSuccessResult <bool>()); }