Esempio n. 1
0
 private void SetTotalPrice()
 {
     dblInTotal.Value = (double)InvoiceItemVms.Select(item => item.TotalPrice).Sum();
 }
Esempio n. 2
0
        private void SaveInvoice()
        {
            ErrorProvider.Clear();
            var    isFormValid = true;
            Client newClient   = null;

            if (txtClientName.Text.IsNullOrEmptyOrWhiteSpace())
            {
                isFormValid = false;
                ErrorProvider.SetError(txtClientName, Resources.ThisFieldIsRequired);
            }
            else if (!ClientsNames.Contains(txtClientName.Text.FullTrim()))
            {
                isFormValid = ShowConfirmationDialog(Resources.ClientNotExists) == DialogResult.Yes;
                newClient   = new Client {
                    Name = txtClientName.Text.FullTrim()
                };
            }
            if (!InvoiceItemVms.Any())
            {
                isFormValid = false;
                ShowErrorMsg(Resources.InvoiceWithoutItems);
            }
            if (!isFormValid)
            {
                return;
            }
            if (!InvoiceItemVms.Any())
            {
                ShowErrorMsg(Resources.NoItemsAdded);
                return;
            }

            if (newClient != null)
            {
                ClientManager.AddClient(newClient);
            }

            var invoice = new Invoice
            {
                ClientId = ClientManager.GetClientIdByName(txtClientName.Text.FullTrim()),
                Date     = dtInvoiceDate.Value,
                Total    = (decimal)dblInTotal.Value,
                Paid     = (decimal)dblInPaid.Value,
                Discount = (decimal)dblInDiscount.Value
            };

            InvoiceManager.AddInvoice(invoice);
            InvoiceItemManager.AddInvoiceItems(InvoiceItemVms.Select(item => new InvoiceItem
            {
                InvoiceId  = invoice.Id,
                MaterialId = item.MaterialId,
                Quantity   = item.Quantity,
                Price      = item.TotalPrice,
                Notes      = item.Notes
            }).ToList());
            if (invoice.Paid > 0)
            {
                InvoicePaymentManager.AddInvoicePayment(new InvoicePayment
                {
                    InvoiceId = invoice.Id,
                    Date      = dtInvoiceDate.Value,
                    Paid      = (decimal)dblInPaid.Value
                });
            }
            MaterialManager.UpdateQuantitiesAfterCreatingInvoice(InvoiceItemVms);
            ShowInfoMsg(Resources.InvoiceCreatedSuccessfully);
            Close();
        }