private Customer GetRequestCustomer(InvoiceCreate request) { var customer = _customerRepository.Get(request.PersonId, x => x.CustomerPrices); if (customer == null) { throw new NotFoundException(); } return(customer); }
private List <InvoiceItem> MapInvoiceItems(InvoiceCreate request) { var products = GetInvoiceProducts(request); return(request.Items.Join(products, x => x.ProductId, x => x.Id, (x, y) => new InvoiceItem { Price = x.Price, Quantity = x.Quantity, ProductId = x.ProductId, ProductName = y.Name }).ToList()); }
private Invoice MapInvoice(InvoiceCreate request, Customer customer) { var invoice = new Invoice { Person = customer, PersonId = customer.Id, CustomerName = request.CustomerName, Shipping = request.Shipping ?? customer.Shipping ?? Shipping.Self, PaymentMethod = request.PaymentMethod ?? customer.PaymentMethod ?? PaymentMethod.Cash, Number = _documentNumberGenerator.NextForInvoice(), DeliveryAddress = request.DeliveryAddress, Items = MapInvoiceItems(request) }; invoice.Total = invoice.CalculateTotal(); return(invoice); }
public InvoiceResult Create(InvoiceCreate request) { var customer = GetRequestCustomer(request); var invoice = MapInvoice(request, customer); customer.Accept(invoice); _invoiceRepository.Add(invoice); if (request.PaymentMethod == PaymentMethod.Cash) { CreateCancellingPayment(invoice, customer); } UpdateCustomerPrices(customer, invoice); return(new InvoiceResult(invoice.Number, invoice.Total, invoice.Balance)); }
private IEnumerable <Product> GetInvoiceProducts(InvoiceCreate request) { var productIds = request.Items.Select(x => x.ProductId).ToList(); return(_productRepository.Query().Where(x => productIds.Contains(x.Id)).ToList()); }