public async Task AddGoodsReceivedNote(AddGoodsReceivedNote model)
        {
            await RunTaskInTransaction(async() =>
            {
                var note = new Data_Access_Layer.GoodsReceivedNote
                {
                    DocumentId  = model.DocumentId,
                    IssueDate   = model.IssueDate,
                    ReceiveDate = model.ReceiveDate,
                    InvoiceId   = model.InvoiceId
                };

                await GoodsReceivedNoteRepository.Add(note);

                var invoiceEntries = 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);

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

                        await ProductDetailsRepository.Add(productDetail);

                        continue;
                    }

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

                    productDetails.Count += entry.Count;

                    ProductDetailsRepository.Update(productDetails);
                }

                return(string.Empty);
            });
        }
        public override Common.DTO.Product Map(Data_Access_Layer.Product entity)
        {
            var dto = base.Map(entity);

            Map(entity, dto);
            return(dto);
        }
Beispiel #3
0
 public virtual Common.DTO.Product Map(Data_Access_Layer.Product entity)
 {
     return(new Common.DTO.Product
     {
         Id = entity.Id,
         Name = entity.Name,
         Image = entity.Image,
         ProductAttributes = entity.ProductAttributes
                             .Select(pa => new Common.DTO.ProductAttribute
         {
             Attribute = Map(pa.Attribute),
             Value = pa.Value
         }).ToList(),
         ProductDetails = entity.ProductDetails
                          .Select(pd => new Common.DTO.ProductDetail
         {
             Location = Map(pd.Location),
             Count = pd.Count
         })
                          .ToList(),
         CreatedAt = entity.CreatedAt,
         LastModifiedAt = entity.LastModifiedAt
     });
 }
        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;
            }
        }