private void AddBtn_OnClicktn_OnClick(object sender, RoutedEventArgs e) { if (!(InvoiceDg.SelectedItem is Product product) || !TxtQnt.Value.HasValue) { return; } product.Qnt += Convert.ToInt32(TxtQnt.Value); using (var db = new ObjectContext()) { db.Update(product); InvoiceDg.ItemsSource = db.Products.ToList(); var stock = new StockHistoric(); stock.Product_Id = product.Id; stock.Qnt = Convert.ToInt32(TxtQnt.Value); stock.Date = DateTime.Now; stock.Movement = MovementStock.Entrée; db.InsertWithIdentity(stock); } }
private void SaveBtn_OnClick(object sender, RoutedEventArgs e) { if (CustomersCb.SelectedIndex == -1) { MessageBox.Show("Il faut séléctionner un client", "Attention", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (invoiceItems.Count == 0) { MessageBox.Show("Il faut ajouter aux moins une ligne à la facture ", "Attention", MessageBoxButton.OK, MessageBoxImage.Information); return; } using (var db = new ObjectContext()) { var invoice = new Invoice(); invoice.CreationDate = DateTime.Now; invoice.InvoiceNumber = $"{DateTime.Now.Year}-{(db.Invoices.Count() + 1).ToString().PadLeft(4,'0')}"; invoice.Customer_Id = ((Customer)CustomersCb.SelectedItem).Id; invoice.InvoiceType = InvoiceType.Facture; invoice.IsValid = true; if (AmmoutpaidXTxt.Value.HasValue) { invoice.PaidAmmount = Convert.ToDecimal(AmmoutpaidXTxt.Value); } invoice.THT = (decimal)invoiceItems.Sum(x => x.THT); invoice.TTC = invoiceItems.Sum(x => x.TTC); invoice.Left = decimal.Subtract(invoice.TTC, invoice.PaidAmmount); if (invoice.Left > 0) { var customer = db.Customers.FirstOrDefault(x => x.Id == invoice.Customer_Id); if (customer != null) { customer.Debt += invoice.Left; if (customer.Debt <= 0) { invoice.Status = InvoiceStatus.Reglée; invoice.Left = 0; } else { invoice.Left = customer.Debt; invoice.Status = invoice.Left < invoice.PaidAmmount ? InvoiceStatus.Reglée : InvoiceStatus.Non_Reglée; } db.InsertWithInt64Identity(invoice); } db.Update(customer); } var newInvoice = db.Invoices.FirstOrDefault(x => x.CreationDate == invoice.CreationDate && x.Customer_Id == invoice.Customer_Id && x.InvoiceNumber == invoice.InvoiceNumber); foreach (var invoiceItem in invoiceItems) { if (newInvoice == null) { continue; } invoiceItem.Invoice_Id = newInvoice.Id; db.InsertWithInt64Identity(invoiceItem); var stock = db.Products.FirstOrDefault(x => x.Id == invoiceItem.Product_Id); if (stock != null && stock.Qnt >= invoiceItem.Qnt) { stock.Qnt -= invoiceItem.Qnt; db.Update(stock); } else if (stock != null) { stock.Qnt = 0; db.Update(stock); } var stock2 = new StockHistoric(); stock2.Product_Id = stock.Id; stock2.Qnt = invoiceItem.Qnt; stock2.Date = DateTime.Now; stock2.Movement = MovementStock.Sortie; db.InsertWithIdentity(stock2); } MessageBox.Show("Enregistrement terminé avec succcés !", "Enregistrement", MessageBoxButton.OK, MessageBoxImage.Information); } }