public static void UpdateInvoice(InvoiceModel invoiceModel) { var storageDbEntities = new StorageDBEntities(); Invoice invoice = storageDbEntities.Invoices.Where(inv => inv.ID == invoiceModel.ID && inv.UserID == UserHelper.UserID).FirstOrDefault(); if (invoice != null) { // updates invoice metadata invoice.SupplierID = invoiceModel.Supplier.ID; invoice.RecipientID = invoiceModel.Recipient.ID; invoice.PriceType = invoiceModel.PriceType; foreach (var productInInvoice in invoice.ProductsInInvoices.ToList()) { ProductsInInvoiceModel productsInInvoiceModel = invoiceModel.Products.Where(p => p.ProductID == productInInvoice.ProductID).FirstOrDefault(); // product was updated by user if (productsInInvoiceModel != null) { productInInvoice.Price = productsInInvoiceModel.Price; productInInvoice.Quantity = productsInInvoiceModel.Quantity; invoiceModel.Products.Remove(productsInInvoiceModel); } // product was deleted by user else { storageDbEntities.ProductsInInvoices.DeleteObject(productInInvoice); } } // products were created by user foreach (var productsInInvoiceModel in invoiceModel.Products.Where(p => p.ProductID > 0).ToList()) { ProductsInInvoice productsInInvoice = new ProductsInInvoice { Price = productsInInvoiceModel.Price, Quantity = productsInInvoiceModel.Quantity, ProductID = productsInInvoiceModel.ProductID }; invoice.ProductsInInvoices.Add(productsInInvoice); } storageDbEntities.SaveChanges(); } }
public static InvoiceModel GetInvoice(int id) { var storageDbEntities = new StorageDBEntities(); Invoice invoice = storageDbEntities.Invoices.Where(inv => inv.ID == id && inv.UserID == UserHelper.UserID).FirstOrDefault(); if (invoice != null) { InvoiceModel invoiceModel = new InvoiceModel { Supplier = new ClientModel { ID = invoice.Supplier.ID, Name = invoice.Supplier.Name, Address = invoice.Supplier.Address, Telephone = invoice.Supplier.Telephone }, Recipient = new ClientModel { ID = invoice.Recipient.ID, Name = invoice.Recipient.Name, Address = invoice.Recipient.Address, Telephone = invoice.Recipient.Telephone }, Date = invoice.Date, Type = invoice.Type, Number = invoice.Number, PriceType = invoice.PriceType, ID = invoice.ID }; foreach (ProductsInInvoice productsInInvoice in invoice.ProductsInInvoices.ToList()) { ProductsInInvoiceModel productsInInvoiceModel = new ProductsInInvoiceModel { ID = productsInInvoice.ID, Price = productsInInvoice.Price, Quantity = productsInInvoice.Quantity, Total = productsInInvoice.Price * Convert.ToDecimal(productsInInvoice.Quantity), Product = new ProductModel { ID = productsInInvoice.Product.ID, Name = productsInInvoice.Product.Name, Code = productsInInvoice.Product.Code, Unit = productsInInvoice.Product.Unit, } }; invoiceModel.Products.Add(productsInInvoiceModel); invoiceModel.MasterTotal += productsInInvoiceModel.Total; } invoiceModel.Products = invoiceModel.Products.OrderBy(p => Convert.ToInt32(p.Product.Code)).ToList(); for (int position = 0; position < invoiceModel.Products.Count; position++) { invoiceModel.Products[position].Position = position + 1; } return(invoiceModel); } return(null); }