Exemple #1
0
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (_upDownAmount.Value == null || _upDownPrice.Value == null)
            {
                return;
            }

            var item = new InvoiceItem
            {
                Name     = _tbName.Text,
                Price    = _upDownPrice.Value.Value,
                Amount   = _upDownAmount.Value.Value,
                Currency = _currency,
                Unit     = _cbUnit.SelectedItem as UnitOfMeasure
            };

            item.SetTotal();

            var result = Saver.Checker(item);

            if (result)
            {
                _items.Add(item);
                Init();
            }
        }
Exemple #2
0
        public Invoice Update(Invoice model)
        {
            using (var context = new Context())
            {
                var existingParent = context.Invoices
                                     .Where(p => p.Id == model.Id)
                                     .Include(p => p.Items).Include(p => p.PaymentData).Include(p => p.DocumentData)
                                     .Include(p => p.Consumer).Include(p => p.Vendor).Include(p => p.Customer)
                                     .Include(p => p.PaymentData.PaymentMethod)
                                     .SingleOrDefault();

                if (existingParent != null)
                {
                    // Delete children
                    foreach (var existingChild in existingParent.Items.ToList())
                    {
                        context.InvoicesItems.Remove(existingChild);
                    }

                    // Update and Insert children
                    for (var index = 0; index < model.Items.Count; index++)
                    {
                        var childModel = model.Items[index];
                        var newChild   = new InvoiceItem()
                        {
                            Id       = childModel.Id,
                            Name     = childModel.Name,
                            Price    = childModel.Price,
                            Currency = context.Currencies.FirstOrDefault(c => c.Id == childModel.Currency.Id),
                            Amount   = childModel.Amount,
                            Unit     = context.UnitsOfMeasure.FirstOrDefault(u => u.Id == childModel.Unit.Id),
                        };
                        newChild.SetTotal();
                        existingParent.Items.Add(newChild);
                    }
                }
                else
                {
                    existingParent = model;
                }

                if (_cbCustomer.SelectedItem != null)
                {
                    existingParent.Customer = context.Customers.FirstOrDefault(c => c.Id == ((Customer)_cbCustomer.SelectedItem).Id);
                }

                if (_cbConsumer.SelectedItem != null)
                {
                    existingParent.Consumer = context.Consumers.FirstOrDefault(c => c.Id == ((Consumer)_cbConsumer.SelectedItem).Id);
                }
                else
                {
                    existingParent.Consumer = null;
                }

                if (_cbVendor.SelectedItem != null)
                {
                    existingParent.Vendor = context.Vendors.FirstOrDefault(c => c.Id == ((Vendor)_cbVendor.SelectedItem).Id);
                }

                if (existingParent.PaymentData == null)
                {
                    existingParent.PaymentData = new PaymentData();
                }

                if (_cbPaymentMethod.SelectedItem != null)
                {
                    existingParent.PaymentData.PaymentMethod =
                        context.PaymentMethods.FirstOrDefault(c =>
                                                              c.Id == ((PaymentMethod)_cbPaymentMethod.SelectedItem).Id);
                }

                existingParent.PaymentData.PaymentDate = DateTime.ParseExact(_lblPaymentDate.Content.ToString(), Properties.strings.dateFormat, System.Globalization.CultureInfo.InvariantCulture);
                if (existingParent.DocumentData == null)
                {
                    existingParent.DocumentData = new DocumentData();
                }

                existingParent.DocumentData.Date   = DateTime.ParseExact(_lblDate.Content.ToString(), Properties.strings.dateFormat, System.Globalization.CultureInfo.InvariantCulture);
                existingParent.DocumentData.Number = _lblNumber.Content.ToString();
                existingParent.DocumentData.Place  = _tbPlace.Text;

                if (!_isNew)
                {
                    context.Entry(existingParent.PaymentData).State  = EntityState.Modified;
                    context.Entry(existingParent.DocumentData).State = EntityState.Modified;
                    context.Entry(existingParent).State = EntityState.Modified;
                }

                if (Saver.Save(existingParent, context))
                {
                    return(existingParent);
                }
                else
                {
                    return(null);
                }
            }
        }