Example #1
0
        public async Task Update(BillViewModel billVm)
        {
            var order = new BillViewModel().Map(billVm);
            var newDetails = order.BillDetails;
            var addedDetails = newDetails.Where(x => x.Id == 0).ToList();
            var updatedDetails = newDetails.Where(x => x.Id != 0).ToList();
            var existedDetails = (await _orderDetailRepository.FindAll(x => x.BillId == billVm.Id)).AsNoTracking();
            order.BillDetails.Clear();
            foreach (var detail in updatedDetails)
            {
                var product = await _productRepository.FindById(detail.ProductId);
                detail.Price = product.Price;
                await _orderDetailRepository.Update(detail);
            }

            foreach (var detail in addedDetails)
            {
                var product = await _productRepository.FindById(detail.ProductId);
                detail.Price = product.Price;
                await _orderDetailRepository.Add(detail);
            }
            await _orderDetailRepository.RemoveMultiple(existedDetails.Except(updatedDetails).ToList());
            order.DateCreated = DateTime.Now;
            order.DateModified = DateTime.Now;
            await _orderRepository.Update(order);
        }
Example #2
0
        public void Update(BillViewModel billViewModel)
        {
            //Mapping to order domain
            var order = Mapper.Map <BillViewModel, Bill>(billViewModel);
            //Get order Detail
            var newDetails = order.BillDetails;
            //new details added
            var addedDetails = newDetails.Where(x => x.Id == 0).ToList();
            //get updated details
            var updatedDetails = newDetails.Where(x => x.Id != 0).ToList();
            //Existed details
            var existedDetails = _orderDetailRepository.FindAll(x => x.BillId == billViewModel.Id);

            //Clear db
            order.BillDetails.Clear();
            //Update details
            foreach (BillDetail detail in updatedDetails)
            {
                Product product = _productRepository.FindById(detail.ProductId);
                detail.Price = product.Price;
                _orderDetailRepository.Update(detail);
            }
            //Add new details
            foreach (BillDetail detail in addedDetails)
            {
                Product product = _productRepository.FindById(detail.ProductId);
                detail.Price = product.Price;
                _orderDetailRepository.Add(detail);
            }
            _orderDetailRepository.RemoveMultiple(existedDetails.Except(updatedDetails).ToList());
            _orderRepository.Update(order);
        }
Example #3
0
        public void Update(BillViewModel billVm)
        {
            // mapping to bill
            var bill = _mapper.Map <BillViewModel, Bill>(billVm);

            // get bill detail
            var billDetail = billVm.BillDetails;

            // new detail add
            var addBillDetail = billDetail.Where(m => m.Id == 0).ToList();

            // get detail update
            var updateBillDetail = billDetail.Where(m => m.Id != 0).ToList();

            // existed detail
            var existedBillDetail = _billDetailRepository.FindAll(m => m.BillId == billVm.Id);

            // clear db
            billVm.BillDetails.Clear();

            // update bill detail
            foreach (var detail in updateBillDetail)
            {
                var productUpdate = _productRepository.FindById(detail.ProductId);
                detail.Price = productUpdate.Price;
                var detailUpdate = new BillDetail(detail.Id, detail.BillId, detail.ProductId, detail.Quantity,
                                                  detail.Price, detail.ColorId, detail.SizeId);
                _billDetailRepository.Update(detailUpdate);
            }

            // add bill detail
            foreach (var detail in addBillDetail)
            {
                var product = _productRepository.FindById(detail.ProductId);
                detail.Price = product.Price;
                var detailAdd = new BillDetail(bill.Id, detail.ProductId, detail.Quantity,
                                               detail.Price, detail.ColorId, detail.SizeId);
                _billDetailRepository.Add(detailAdd);
            }

            // xoá các dữ liệu bill detail
            var lstDelete = (from exist in existedBillDetail
                             where !updateBillDetail.Select(m => m.Id).Contains(exist.Id)
                             select exist).ToList();

            _billDetailRepository.RemoveMultiple(lstDelete);

            var billUp = new Bill(bill.Id, bill.CustomerName, bill.CustomerAddress, bill.CustomerMobile, bill.CustomerMessage, bill.BillStatus, bill.PaymentMethod, bill.Status, bill.CustomerId, bill.DateCreated);

            _billRepository.Update(billUp);
        }
Example #4
0
        public void Update(BillViewModel billVm)
        {
            //Mapping to order domain
            var order = _mapper.Map <Bill>(billVm);

            //Get order Detail
            var newDetails = order.BillDetails;

            //new details added
            var addedDetails = newDetails.Where(x => x.Id == 0).ToList();

            //get updated details
            var updatedDetails = newDetails.Where(x => x.Id != 0).ToList();

            //Existed details
            var existedDetails = _orderDetailRepository.FindAll(x => x.BillId == billVm.Id);

            //Clear db
            order.BillDetails.Clear();

            foreach (var detail in updatedDetails)
            {
                var product = _productRepository.FindById(detail.ProductId);
                detail.Price = product.Price;
                _orderDetailRepository.Update(detail);
            }

            foreach (var detail in addedDetails)
            {
                var product = _productRepository.FindById(detail.ProductId);
                detail.Price = product.Price;
                _orderDetailRepository.Add(detail);
            }

            var removeList = existedDetails.AsEnumerable().Except(updatedDetails).ToList();

            _orderDetailRepository.RemoveMultiple(removeList);

            _orderRepository.Update(order);
        }
Example #5
0
        public BillViewModel DeleteBill(int id)
        {
            var bill          = _billRepository.FindById(id);
            var billViewModel = Mapper.Map <Bill, BillViewModel>(bill);

            var billDetails = _billDetailRepository.FindAll(x => x.BillId == id);

            _billDetailRepository.RemoveMultiple(billDetails.ToList());
            _billRepository.Remove(bill);

            return(billViewModel);
        }
Example #6
0
 public List <BillDetailViewModel> GetBillDetails(int billId)
 {
     return(_orderDetailRepository
            .FindAll(x => x.BillId == billId, c => c.Bill, c => c.Color, c => c.Size, c => c.Product)
            .ProjectTo <BillDetailViewModel>().ToList());
 }