public ActionResult AddPurchaseInvoice(Models.ViewModels.Purchases.AddPurchaseInvoice model)
        {
            if (string.IsNullOrEmpty(model.VendorInvoiceNo))
            {
                return(RedirectToAction("PurchaseOrders"));
            }

            var existingPO = _purchasingService.GetPurchaseOrderById(model.Id);
            var vendor     = _purchasingService.GetVendorById(existingPO.VendorId);

            var purchInvoice = new PurchaseInvoiceHeader()
            {
                Date            = model.Date,
                VendorInvoiceNo = model.VendorInvoiceNo,
                Vendor          = vendor,
                VendorId        = vendor.Id,
                CreatedBy       = User.Identity.Name,
                CreatedOn       = DateTime.Now,
                ModifiedBy      = User.Identity.Name,
                ModifiedOn      = DateTime.Now
            };

            foreach (var line in model.PurchaseInvoiceLines)
            {
                var item        = _inventoryService.GetItemById(line.ItemId);
                var measurement = _inventoryService.GetMeasurementById(line.UnitOfMeasurementId);
                purchInvoice.PurchaseInvoiceLines.Add(new PurchaseInvoiceLine()
                {
                    ItemId           = item.Id,
                    MeasurementId    = measurement.Id,
                    Quantity         = line.Quantity,
                    ReceivedQuantity = line.ReceivedQuantity,
                    Cost             = item.Cost.Value,
                    Discount         = 0,
                    Amount           = item.Cost.Value * line.ReceivedQuantity,
                    CreatedBy        = User.Identity.Name,
                    CreatedOn        = DateTime.Now,
                    ModifiedBy       = User.Identity.Name,
                    ModifiedOn       = DateTime.Now
                });
            }
            _purchasingService.AddPurchaseInvoice(purchInvoice, existingPO.Id);
            return(RedirectToAction("PurchaseOrders"));
        }