public override Common.DTO.Invoice Map(Data_Access_Layer.Invoice entity)
        {
            var dto = base.Map(entity);

            Map(entity, dto);
            return(dto);
        }
Esempio n. 2
0
 public virtual Common.DTO.Invoice Map(Data_Access_Layer.Invoice entity)
 {
     return(new Common.DTO.Invoice
     {
         Id = entity.Id,
         InvoiceType = entity.InvoiceType,
         CompletionDate = entity.CompletionDate,
         Counterparty = Map(entity.Counterparty),
         City = Map(entity.City),
         IssueDate = entity.IssueDate,
         PaymentMethod = entity.PaymentMethod,
         DocumentId = entity.DocumentId,
         Total = entity.Total,
         VAT = entity.VAT,
         Products = entity.Products
                    .Select(Map)
                    .ToList(),
         CreatedAt = entity.CreatedAt,
         LastModifiedAt = entity.LastModifiedAt
     });
 }
        public async Task CreateInvoices(IEnumerable <AddInvoice> models)
        {
            await RunTaskInTransaction(async() =>
            {
                foreach (var model in models)
                {
                    Data_Access_Layer.Invoice invoice = null;
                    Data_Access_Layer.City city       = null;

                    if (model.City != null)
                    {
                        city = GetOrCreateCity(model.City);
                    }

                    var entries  = new List <Data_Access_Layer.Entry>();
                    var totalVAT = 0m;
                    var total    = 0m;

                    if (model.IssueDate == DateTime.MinValue)
                    {
                        model.IssueDate = DateTime.Now;
                    }

                    if (model.CompletionDate == DateTime.MinValue)
                    {
                        model.CompletionDate = DateTime.Now;
                    }

                    invoice = new Data_Access_Layer.Invoice
                    {
                        CounterpartyId = model.Counterparty.Id,
                        CityId         = model.City.Id,
                        CompletionDate = model.CompletionDate,
                        IssueDate      = model.IssueDate,
                        DocumentId     = model.DocumentId,
                        Total          = total,
                        VAT            = totalVAT,
                        PaymentMethod  = model.PaymentMethod,
                        CanEdit        = model.CanEdit
                    };

                    if (city != null)
                    {
                        invoice.CityId = city.Id;
                    }

                    await InvoiceRepository.Add(invoice);

                    foreach (var product in model.Products)
                    {
                        var entry = new Data_Access_Layer.Entry
                        {
                            Name      = product.Name,
                            Price     = product.Price,
                            Count     = product.Count,
                            VAT       = product.VAT,
                            InvoiceId = invoice.Id
                        };

                        var amount = entry.Count *entry.Price;
                        totalVAT  += amount *entry.VAT;
                        total     += amount;
                        entries.Add(entry);
                    }

                    await EntryRepository.AddRange(entries);

                    invoice.Total = total;
                    invoice.VAT   = totalVAT;

                    InvoiceRepository.Update(invoice);
                }

                return(string.Empty);
            });
        }
        public async Task CreateInvoices(IEnumerable <AddInvoice> models)
        {
            var transaction = _dbContext.Database.BeginTransaction();

            try
            {
                foreach (var model in models)
                {
                    Data_Access_Layer.Invoice invoice = null;
                    Data_Access_Layer.City    city    = null;

                    if (model.City != null)
                    {
                        city = await GetOrAddCity(model.City);
                    }

                    var entries  = new List <Data_Access_Layer.Entry>();
                    var totalVAT = 0m;
                    var total    = 0m;

                    if (model.IssueDate == DateTime.MinValue)
                    {
                        model.IssueDate = DateTime.Now;
                    }

                    if (model.CompletionDate == DateTime.MinValue)
                    {
                        model.CompletionDate = DateTime.Now;
                    }

                    invoice = new Data_Access_Layer.Invoice
                    {
                        CounterpartyId = model.Counterparty.Id,
                        CityId         = model.City.Id,
                        CompletionDate = model.CompletionDate,
                        IssueDate      = model.IssueDate,
                        DocumentId     = model.DocumentId,
                        Total          = total,
                        VAT            = totalVAT,
                        PaymentMethod  = model.PaymentMethod,
                        CanEdit        = model.CanEdit
                    };

                    if (city != null)
                    {
                        invoice.CityId = city.Id;
                    }

                    await InvoiceRepository.Add(invoice);

                    Save();

                    foreach (var product in model.Products)
                    {
                        var entry = new Data_Access_Layer.Entry
                        {
                            Name      = product.Name,
                            Price     = product.Price,
                            Count     = product.Count,
                            VAT       = product.VAT,
                            InvoiceId = invoice.Id
                        };

                        var amount = entry.Count * entry.Price;
                        totalVAT += amount * entry.VAT;
                        total    += amount;
                        entries.Add(entry);
                    }

                    await EntryRepository.AddRange(entries);

                    Save();

                    invoice.Total = total;
                    invoice.VAT   = totalVAT;

                    InvoiceRepository.Update(invoice);
                    Save();
                }

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