private void SetTotalPrice()
 {
     dblInTotal.Value = (double)PurchaseInvoiceItemVms.Select(item => item.TotalPrice).Sum();
 }
        private void SavePurchaseInvoice()
        {
            ErrorProvider.Clear();
            var      isFormValid = true;
            Supplier newSupplier = null;

            if (txtSupplierName.Text.IsNullOrEmptyOrWhiteSpace())
            {
                isFormValid = false;
                ErrorProvider.SetError(txtSupplierName, Resources.ThisFieldIsRequired);
            }
            else if (!SuppliersNames.Contains(txtSupplierName.Text.FullTrim()))
            {
                isFormValid = ShowConfirmationDialog(Resources.SupplierNotExists) == DialogResult.Yes;
                newSupplier = new Supplier {
                    Name = txtSupplierName.Text.FullTrim()
                };
            }

            if (!PurchaseInvoiceItemVms.Any())
            {
                isFormValid = false;
                ShowErrorMsg(Resources.InvoiceWithoutItems);
            }

            if (!isFormValid)
            {
                return;
            }
            if (!PurchaseInvoiceItemVms.Any())
            {
                ShowErrorMsg(Resources.NoItemsAdded);
                return;
            }

            if (newSupplier != null)
            {
                SupplierManager.AddSupplier(newSupplier);
            }

            var purchaseInvoice = new PurchaseInvoice
            {
                SupplierId = SupplierManager.GetSupplierIdByName(txtSupplierName.Text.FullTrim()),
                Date       = dtInvoiceDate.Value,
                Total      = (decimal)dblInTotal.Value,
                Paid       = (decimal)dblInPaid.Value,
                Discount   = (decimal)dblInDiscount.Value
            };

            PurchaseInvoiceManager.AddPurchaseInvoice(purchaseInvoice);
            PurchaseInvoiceItemManager.AddPurchaseInvoiceItems(PurchaseInvoiceItemVms.Select(item =>
                                                                                             new PurchaseInvoiceItem
            {
                InvoiceId  = purchaseInvoice.Id,
                MaterialId = item.MaterialId,
                Quantity   = item.Quantity,
                UnitPrice  = item.UnitPrice,
                Notes      = item.Notes
            }).ToList());
            if (purchaseInvoice.Paid > 0)
            {
                PurchaseInvoicePaymentManager.AddPurchaseInvoicePayment(new PurchaseInvoicePayment
                {
                    InvoiceId = purchaseInvoice.Id,
                    Date      = dtInvoiceDate.Value,
                    Paid      = (decimal)dblInPaid.Value
                });
            }
            MaterialManager.UpdateQuantitiesAfterCreatingPurchaseInvoice(PurchaseInvoiceItemVms);
            ShowInfoMsg(Resources.InvoiceCreatedSuccessfully);
            Close();
        }