private void UpdateDraftLineItems(OutgoingInvoiceDraft draft, IEnumerable <DraftLineItem> lineItems) { var draftLineItemIds = draft.LineItems.Select(li => li.Id); var lineItemsToAdd = lineItems.Where(l => !draftLineItemIds.Contains(l.Id)); var lineItemsToUpdate = lineItems.Where(l => draftLineItemIds.Contains(l.Id)); var lineItemsToRemove = draft.LineItems.Where(li => !lineItems.Any(l => l.Id == li.Id)); if (lineItemsToRemove.Any()) { lineItemsToRemove .ToList() .ForEach(l => draft.LineItems.Remove(l)); } if (lineItemsToUpdate.Any()) { lineItemsToUpdate .ToList() .ForEach(l => { var lineItem = draft.LineItems.Single(li => li.Id == l.Id); if (lineItem.Code != l.Code) { lineItem.Code = l.Code; } if (lineItem.Description != l.Description) { lineItem.Description = l.Description; } if (lineItem.Quantity != l.Quantity) { lineItem.Quantity = l.Quantity; } if (lineItem.TotalPrice != l.TotalPrice) { lineItem.TotalPrice = l.TotalPrice; } if (lineItem.UnitPrice != l.UnitPrice) { lineItem.UnitPrice = l.UnitPrice; } if (lineItem.Vat != l.Vat) { lineItem.Vat = l.Vat; } if (lineItem.VatDescription != l.VatDescription) { lineItem.VatDescription = l.VatDescription; } }); } if (lineItemsToAdd.Any()) { draft.LineItems.AddRange(lineItemsToAdd); } }
public async Task CreateDraft(PartyInfo customer, DateTime?date, string currency, decimal taxableAmount, decimal taxes, decimal totalPrice, string description, string paymentTerms, string purchaseOrderNumber, bool pricesAreVatIncluded, IEnumerable <DraftLineItem> lineItems, IEnumerable <PriceByVat> pricesByVat, IEnumerable <NonTaxableItem> nonTaxableItems, string providenceFundDescription, decimal?providenceFundRate, decimal?providenceFundAmount, string withholdingTaxDescription, decimal?withholdingTaxRate, decimal?withholdingTaxTaxableAmountRate, decimal?withholdingTaxAmount, decimal totalToPay) { var draft = new OutgoingInvoiceDraft { Id = Guid.NewGuid(), Currency = currency, Customer = customer, Date = date, Description = description, LineItems = lineItems.ToList(), NonTaxableItems = nonTaxableItems.ToList(), PricesAreVatIncluded = pricesAreVatIncluded, PricesByVat = pricesByVat.ToList(), PurchaseOrderNumber = purchaseOrderNumber, PaymentTerms = paymentTerms, TaxableAmount = taxableAmount, Taxes = taxes, TotalPrice = totalPrice, TotalToPay = totalToPay }; if (!string.IsNullOrWhiteSpace(providenceFundDescription) && providenceFundRate.HasValue && providenceFundAmount.HasValue) { draft.ProvidenceFund = new ProvidenceFund { Amount = providenceFundAmount, Description = providenceFundDescription, Rate = providenceFundRate }; } if (!string.IsNullOrWhiteSpace(withholdingTaxDescription) && withholdingTaxRate.HasValue && withholdingTaxTaxableAmountRate.HasValue && withholdingTaxAmount.HasValue) { draft.WithholdingTax = new WithholdingTax { Amount = withholdingTaxAmount.Value, Description = withholdingTaxDescription, Rate = withholdingTaxRate.Value, TaxableAmountRate = withholdingTaxTaxableAmountRate.Value }; } _context.Add(draft); await _context.SaveChangesAsync(); }
private void UpdateDraftNonTaxableItems(OutgoingInvoiceDraft draft, IEnumerable <NonTaxableItem> nonTaxableItems) { var nonTaxableItemIds = draft.NonTaxableItems.Select(t => t.Id); var nonTaxableItemsToAdd = nonTaxableItems.Where(nt => !nonTaxableItemIds.Contains(nt.Id)); var nonTaxableItemsToUpdate = nonTaxableItems.Where(nt => nonTaxableItemIds.Contains(nt.Id)); var nonTaxableItemsToRemove = draft.NonTaxableItems.Where(nt => !nonTaxableItems.Any(t => t.Id == nt.Id)); if (nonTaxableItemsToRemove.Any()) { nonTaxableItemsToRemove .ToList() .ForEach(nt => draft.NonTaxableItems.Remove(nt)); } if (nonTaxableItemsToUpdate.Any()) { nonTaxableItemsToUpdate .ToList() .ForEach(nt => { var nonTaxableItem = draft.NonTaxableItems.Single(t => t.Id == nt.Id); if (nonTaxableItem.Amount != nt.Amount) { nonTaxableItem.Amount = nt.Amount; } if (nonTaxableItem.Description != nt.Description) { nonTaxableItem.Description = nt.Description; } }); } if (nonTaxableItemsToAdd.Any()) { draft.NonTaxableItems.AddRange(nonTaxableItemsToAdd); } }
public async Task CreateDraft(PartyInfo customer, DateTime?date, string currency, decimal taxableAmount, decimal taxes, decimal totalPrice, string description, string paymentTerms, string purchaseOrderNumber, bool pricesAreVatIncluded, IEnumerable <DraftLineItem> lineItems, IEnumerable <PriceByVat> pricesByVat, IEnumerable <NonTaxableItem> nonTaxableItems) { var draft = new OutgoingInvoiceDraft { Id = Guid.NewGuid(), Currency = currency, Customer = customer, Date = date, Description = description, LineItems = lineItems.ToList(), NonTaxableItems = nonTaxableItems.ToList(), PricesAreVatIncluded = pricesAreVatIncluded, PricesByVat = pricesByVat.ToList(), PurchaseOrderNumber = purchaseOrderNumber, PaymentTerms = paymentTerms, TaxableAmount = taxableAmount, Taxes = taxes, TotalPrice = totalPrice }; _context.Add(draft); await _context.SaveChangesAsync(); }
private void UpdateDraftPricesByVat(OutgoingInvoiceDraft draft, IEnumerable <PriceByVat> pricesByVat) { draft.PricesByVat.Clear(); draft.PricesByVat.AddRange(pricesByVat); }