Esempio n. 1
0
        public GenericResult Create(ProductReceiptViewModel productReceiptViewModel)
        {
            try
            {
                var receipt        = _mapper.Map <ProductReceiptViewModel, ProductReceipt>(productReceiptViewModel);
                var receiptDetails = _mapper.Map <List <ProductReceiptDetailViewModel>, List <ProductReceiptDetail> >(productReceiptViewModel.ProductReceiptDetails);
                foreach (var receiptDetail in receiptDetails)
                {
                    var product = _productRepository.FindById(receiptDetail.ProductId);
                    if (product.Price == 0 || product.Price < receiptDetail.OriginalPrice)
                    {
                        product.Price = receiptDetail.OriginalPrice + (receiptDetail.OriginalPrice * 10 / 100);
                        _productRepository.Update(product);
                    }
                }

                receipt.Total = receiptDetails.Sum(x => x.OriginalPrice * x.Quantity);
                _productReceiptRepository.Add(receipt);
                return(new GenericResult(true, "Add Successful", "Successful"));
            }
            catch (Exception)
            {
                return(new GenericResult(false, "Add Failed", "Error"));
            }
        }
Esempio n. 2
0
 public async Task <IActionResult> SaveEntity(ProductReceiptViewModel productReceiptViewModel)
 {
     if (!ModelState.IsValid)
     {
         var allErrors = ModelState.Values.SelectMany(v => v.Errors);
         return(new BadRequestObjectResult(allErrors));
     }
     if (productReceiptViewModel.Id == 0)
     {
         var notificationId = Guid.NewGuid().ToString();
         var announcement   = new AnnouncementViewModel
         {
             Title       = User.GetSpecificClaim("FullName"),
             DateCreated = DateTime.Now,
             Content     = $"The Product Receipt of {productReceiptViewModel.Supplier.FullName} has been created",
             Id          = notificationId,
             UserId      = User.GetUserId(),
             Image       = User.GetSpecificClaim("Avatar"),
             Status      = Status.Active
         };
         var announcementUsers = new List <AnnouncementUserViewModel>()
         {
             new AnnouncementUserViewModel()
             {
                 AnnouncementId = notificationId, HasRead = false, UserId = User.GetUserId()
             }
         };
         _productReceiptService.Create(announcement, announcementUsers, productReceiptViewModel);
         await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
     }
     else
     {
         var notificationId = Guid.NewGuid().ToString();
         var announcement   = new AnnouncementViewModel
         {
             Title       = User.GetSpecificClaim("FullName"),
             DateCreated = DateTime.Now,
             Content     = $"The Product Receipt of {productReceiptViewModel.Supplier.FullName} has been updated",
             Id          = notificationId,
             UserId      = User.GetUserId(),
             Image       = User.GetSpecificClaim("Avatar"),
             Status      = Status.Active
         };
         var announcementUsers = new List <AnnouncementUserViewModel>()
         {
             new AnnouncementUserViewModel()
             {
                 AnnouncementId = notificationId, HasRead = false, UserId = User.GetUserId()
             }
         };
         _productReceiptService.Update(announcement, announcementUsers, productReceiptViewModel);
         await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
     }
     _productReceiptService.Save();
     return(new OkObjectResult(productReceiptViewModel));
 }
Esempio n. 3
0
        public GenericResult Update(ProductReceiptViewModel productReceiptViewModel)
        {
            try
            {
                // Mapping to order domain
                var receipt = _mapper.Map <ProductReceiptViewModel, ProductReceipt>(productReceiptViewModel);
                // Lấy Toàn Bộ Chi Tiết Hóa Đơn Ra Sau Khi Mapping Vào
                var recriptDetails = receipt.ProductReceiptDetails;
                // Them 1 chi tiet hoa don
                var addDetails = recriptDetails.Where(x => x.Id == 0).ToList();
                // Update Chi Tiet hoa don
                var updateDetails = recriptDetails.Where(x => x.Id != 0).ToList();
                // Existed Details
                var existedDetails = _productReceiptDetailRepository.FindAll(x => x.ProductReceiptId == productReceiptViewModel.Id).ToList();
                //Clear db
                receipt.ProductReceiptDetails.Clear();
                _productReceiptDetailRepository.RemoveMultiple(existedDetails.Except(updateDetails).ToList());
                foreach (var receiptDetail in updateDetails)
                {
                    var product = _productRepository.FindById(receiptDetail.ProductId);
                    if (product.Price == 0 || product.Price < receiptDetail.OriginalPrice)
                    {
                        product.Price = receiptDetail.OriginalPrice + (receiptDetail.OriginalPrice * 10 / 100);
                        _productRepository.Update(product);
                    }
                    _productReceiptDetailRepository.Update(receiptDetail);
                }

                foreach (var receiptDetail in addDetails)
                {
                    var product = _productRepository.FindById(receiptDetail.ProductId);
                    if (product.Price == 0 || product.Price < receiptDetail.OriginalPrice)
                    {
                        product.Price = receiptDetail.OriginalPrice + (receiptDetail.OriginalPrice * 10 / 100);
                        _productRepository.Update(product);
                    }
                    _productReceiptDetailRepository.Add(receiptDetail);
                }

                receipt.Total = updateDetails.Sum(x => x.Quantity * x.OriginalPrice) + addDetails.Sum(x => x.Quantity * x.OriginalPrice);
                _productReceiptRepository.Update(receipt);
                return(new GenericResult(true, "Update Successful", "Successful"));
            }
            catch (Exception)
            {
                return(new GenericResult(false, "Update Failed", "Error"));
            }
        }