public async Task <HttpResult <bool> > AddGoodsReceivedNote(GoodsReceivedNote note, CancellationToken token = default(CancellationToken))
        {
            var result = new HttpResult <bool>();

            var model = new WebApiServer.Controllers.Note.ViewModel.AddGoodsReceivedNote
            {
                DocumentId = note.DocumentId,
                InvoiceId  = note.InvoiceId,
                IssueDate  = note.IssueDate,
                NoteEntry  = note
                             .NoteEntry
                             .Select(ne => new WebApiServer.Controllers.Note.ViewModel.NoteEntry
                {
                    Location = new Common.DTO.Location
                    {
                        Id   = ne.Location.Id,
                        Name = ne.Location.Name
                    },
                    Name = ne.Name
                })
                             .ToList(),
                ReceiveDate = note.ReceiveDate
            };

            await _unitOfWork.AddGoodsReceivedNote(model);

            return(result);
        }
        public async Task AddGoodsReceivedNote(Controllers.Note.ViewModel.AddGoodsReceivedNote model)
        {
            var transaction = _dbContext.Database.BeginTransaction();

            try
            {
                var note = new Data_Access_Layer.GoodsReceivedNote
                {
                    DocumentId  = model.DocumentId,
                    IssueDate   = model.IssueDate,
                    ReceiveDate = model.ReceiveDate,
                    InvoiceId   = model.InvoiceId
                };

                await GoodsReceivedNoteRepository.Add(note);

                Save();

                var invoiceEntries = await EntryRepository.GetForInvoice(model.InvoiceId);

                foreach (var noteEntry in model.NoteEntry)
                {
                    var productEntity = await ProductRepository.Find(noteEntry.Name);

                    Data_Access_Layer.ProductDetail productDetail = null;

                    var entry = invoiceEntries
                                .FirstOrDefault(ie => ie.Name == noteEntry.Name);

                    if (productEntity == null)
                    {
                        var product = new Data_Access_Layer.Product
                        {
                            Name  = entry.Name,
                            Price = entry.Price,
                            VAT   = entry.VAT
                        };

                        await ProductRepository.Add(product);

                        Save();

                        productDetail = new Data_Access_Layer.ProductDetail
                        {
                            LocationId = noteEntry.Location.Id,
                            ProductId  = product.Id,
                            Count      = entry.Count,
                        };

                        await ProductDetailsRepository.Add(productDetail);

                        Save();

                        continue;
                    }

                    var productDetails = ProductDetailsRepository
                                         .GetForProduct(productEntity.Id)
                                         .FirstOrDefault(pd => pd.LocationId == noteEntry.Location.Id);

                    productDetails.Count += entry.Count;

                    ProductDetailsRepository.Update(productDetails);

                    Save();
                }
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }
        }