public Data_Access_Layer.City GetOrCreateCity(Common.DTO.City model)
        {
            Data_Access_Layer.City city = null;

            if (model.Id != 0)
            {
                city = _sqliteConnection
                       .Find <Data_Access_Layer.City>(model.Id);

                return(city);
            }

            city = _sqliteConnection
                   .Table <Data_Access_Layer.City>()
                   .FirstOrDefault(ci => ci.Name == model.Name);

            if (city == null)
            {
                city = new Data_Access_Layer.City
                {
                    Name = model.Name
                };

                _sqliteConnection.Insert(city);
            }

            return(city);
        }
        private async Task <Data_Access_Layer.City> GetOrAddCity(Common.DTO.City model)
        {
            Data_Access_Layer.City city = null;

            if (model.Id == 0)
            {
                city = await CityRepository.Find(model.Name);

                if (city == null)
                {
                    city = new Data_Access_Layer.City()
                    {
                        Name = model.Name
                    };

                    await CityRepository.Add(city);

                    Save();
                }
            }
            else
            {
                city = await CityRepository.Get(model.Id);
            }

            return(city);
        }
        public override Common.DTO.City Map(Data_Access_Layer.City entity)
        {
            var dto = base.Map(entity);

            Map(entity, dto);
            return(dto);
        }
Beispiel #4
0
 public virtual Common.DTO.City Map(Data_Access_Layer.City entity)
 {
     return(new Common.DTO.City
     {
         Id = entity.Id,
         Name = entity.Name,
         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;
            }
        }