private void LoadInvoiceProductValuesByInvoiceId(InvoiceViewModel model, int?id) { if (!id.HasValue) { return; } var invoice = _invoiceService.GetInvoiceMasterById(id.Value); if (invoice != null) { model.InvoiceCurrency = invoice.InvoiceCurrency; model.InvoiceAddress = invoice.InvoiceAddress; model.AccountId = invoice.AccountId; model.InvoiceDate = invoice.InvoiceDate; model.TenantName = invoice.TenantName; } var invoiceDetails = _invoiceService.GetAllInvoiceDetailByInvoiceId(id.Value); foreach (var x in invoiceDetails) { var pd = model.AllInvoiceProducts.FirstOrDefault(m => m.ProductId == x.ProductId); var taxPercentage = _productServices.GetProductMasterById(x.ProductId)?.GlobalTax?.PercentageOfAmount ?? 0; if (pd == null) { pd = new OrderProcessDetailsViewModel() { QtyProcessed = x.Quantity, ProductName = x.Description, //TODO: adding percentage here by adding navigation properties in invoices detail table TaxPercent = taxPercentage, ProductId = x.ProductId, Price = x.Price, }; pd.WarrantyAmount = x.WarrantyAmount; } else { pd.QtyProcessed += x.Quantity; } model.AllInvoiceProducts.Add(pd); } model.TaxAmount = model.AllInvoiceProducts.Select(I => I.TaxAmount).DefaultIfEmpty(0).Sum(); var amount = model.AllInvoiceProducts.Select(u => u.NetAmount).DefaultIfEmpty(0).Sum(); model.NetAmount = amount - model.TaxAmount; model.WarrantyAmount += model.AllInvoiceProducts.Select(u => u.WarrantyAmount).DefaultIfEmpty(0).Sum(); model.InvoiceTotal = Math.Round(model.NetAmount + model.TaxAmount + model.WarrantyAmount, 2); }
public InvoiceViewModel LoadInvoiceProductValuesByOrderProcessId(int orderProcessId, int?inventoryTransctionType = null) { InvoiceViewModel model = new InvoiceViewModel(); var orderProcess = _currentDbContext.OrderProcess.FirstOrDefault(u => u.OrderProcessID == orderProcessId && u.IsDeleted != true); if (orderProcess != null) { model.OrderNumber = orderProcess.Order.OrderNumber; model.OrderProcessId = orderProcess.OrderProcessID; model.InvoiceCurrency = orderProcess.Order.Account.GlobalCurrency.CurrencyName; model.InvoiceAddress = orderProcess.Order.Account.FullAddressWithNameHtml; model.AccountId = orderProcess.Order.AccountID ?? 0; model.InvoiceDate = orderProcess.InvoiceDate ?? DateTime.UtcNow; //model.TenantName = CurrentTenant.TenantName; } var orderProcesses = _currentDbContext.OrderProcessDetail .Where(o => o.OrderProcessId == orderProcessId && o.IsDeleted != true) .Include(o => o.ProductMaster) .ToList(); foreach (var x in orderProcesses) { var pd = model.AllInvoiceProducts.FirstOrDefault(m => m.ProductId == x.ProductId); if (pd == null) { pd = new OrderProcessDetailsViewModel() { QtyProcessed = x.QtyProcessed, ProductName = x.ProductMaster.Name, ProductCode = x.ProductMaster.SKUCode, ProductId = x.ProductId, OrderProcessId = x.OrderProcessId, //Remove methods from here Price = x?.OrderDetail?.Price ?? 0, OrderProcessDetailId = x.OrderProcessDetailID, TaxAmountsInvoice = ((x?.OrderDetail?.Price ?? 0 * x.QtyProcessed) / 100) * (x.OrderDetail.TaxName?.PercentageOfAmount ?? 0) }; if (x?.OrderDetail?.Warranty != null) { pd.WarrantyAmount = x.OrderDetail.Warranty.IsPercent ? Math.Round(x.OrderDetail.Warranty.PercentageOfPrice * ((pd.Price * x.QtyProcessed) / 100), 2) : Math.Round(x.OrderDetail.Warranty.FixedPrice * x.QtyProcessed, 2); } if (x?.OrderDetail?.TaxName != null) { pd.TaxPercent = x.OrderDetail.TaxName.PercentageOfAmount; } model.AllInvoiceProducts.Add(pd); } else { var index = model.AllInvoiceProducts.IndexOf(pd); model.AllInvoiceProducts[index].QtyProcessed += x.QtyProcessed; } } model.TaxAmount = model.AllInvoiceProducts.Select(I => I.TaxAmount).DefaultIfEmpty(0).Sum(); var amount = model.AllInvoiceProducts.Select(u => u.NetAmount).DefaultIfEmpty(0).Sum(); model.NetAmount = amount - model.TaxAmount; model.WarrantyAmount += model.AllInvoiceProducts.Select(u => u.WarrantyAmount).DefaultIfEmpty(0).Sum(); model.InvoiceTotal = Math.Round(model.NetAmount + model.TaxAmount + model.WarrantyAmount, 2); return(model); }