private void ExecuteSave() { try { _supplyServ.UpdateSupply(_selectedSupply); _safeServ.DeleteSafe(_selectedSupply.RegistrationDate); _clientAccountServ.DeleteAccount(_selectedSupply.RegistrationDate); var supplyCategories = _supplyCategoryServ.GetSupplyCategories(_selectedSupply.ID); foreach (var item in supplyCategories) { Category cat = _categoryServ.GetCategory(item.CategoryID); if (cat.Qty - item.Qty != 0) { cat.Cost = ((cat.Cost * cat.Qty) - item.CostTotal) / (cat.Qty - item.Qty); } if (cat.Cost < 0) { cat.Cost = 0; } cat.Qty = cat.Qty - item.Qty; _categoryServ.UpdateCategory(cat); } _supplyCategoryServ.DeleteSupplyCategories(ID); foreach (var item in _supplyCategories) { SupplyCategory _supplyCategory = new SupplyCategory { CategoryID = item.CategoryID, Cost = item.Cost, CostTotal = item.CostTotal, SupplyID = ID, Qty = item.Qty, Price = item.Price }; _supplyCategoryServ.AddSupplyCategory(_supplyCategory); Category cat = _categoryServ.GetCategory(item.CategoryID); if (cat.Qty + item.Qty != 0) { cat.Cost = (item.CostTotal + (cat.Cost * cat.Qty)) / (cat.Qty + item.Qty); } cat.Qty = cat.Qty + item.Qty; cat.Price = item.Price; _categoryServ.UpdateCategory(cat); } ClientAccount _account = new ClientAccount { ClientID = _selectedSupply.ClientID, Date = _selectedSupply.Date, RegistrationDate = _selectedSupply.RegistrationDate, Statement = "فاتورة مشتريات رقم " + ID, Credit = _selectedSupply.Cost, Debit = _selectedSupply.CashPaid + _selectedSupply.DiscountPaid }; _clientAccountServ.AddAccount(_account); if (_selectedSupply.CashPaid > 0) { Safe _safe = new Safe { Date = _selectedSupply.Date, RegistrationDate = _selectedSupply.RegistrationDate, Statement = "فاتورة مشتريات رقم " + ID + " من العميل : " + _selectedSupply.Client.Name, Amount = -_selectedSupply.CashPaid, Source = 3 }; _safeServ.AddSafe(_safe); } _currentWindow.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private async void ExecuteSaveAsync() { if (SelectedSupply.Future == true && SelectedSupplyFuture.Place == null) { await _currentWindow.ShowMessageAsync("فشل الإضافة", "يجب كتابة مكان الدفع", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "موافق", DialogMessageFontSize = 25, DialogTitleFontSize = 30 }); return; } Mouse.OverrideCursor = Cursors.Wait; _supplyServ.UpdateSupply(_selectedSupply); _safeServ.DeleteSafe(_selectedSupply.RegistrationDate); _clientAccountServ.DeleteAccount(_selectedSupply.RegistrationDate); _supplyFutureServ.DeleteSupplyFuture(ID); var supplyCategories = _supplyCategoryServ.GetSupplyCategories(_selectedSupply.ID); foreach (var item in supplyCategories) { Category cat = _categoryServ.GetCategory(item.CategoryID); if (cat.Qty - item.Qty != 0) { cat.Cost = ((cat.Cost * cat.Qty) - item.CostTotalAfterDiscount) / (cat.Qty - item.Qty); } cat.Qty = cat.Qty - item.Qty; _categoryServ.UpdateCategory(cat); } _supplyCategoryServ.DeleteSupplyCategories(ID); foreach (var item in _supplyCategories) { SupplyCategory _supplyCategory = new SupplyCategory { CategoryID = item.CategoryID, Cost = item.Cost, CostAfterDiscount = item.CostAfterDiscount, CostAfterTax = item.CostAfterTax, CostTotal = item.CostTotal, CostTotalAfterDiscount = item.CostTotalAfterDiscount, CostTotalAfterTax = item.CostTotalAfterTax, Discount = item.Discount, DiscountValue = item.DiscountValue, DiscountValueTotal = item.DiscountValueTotal, SupplyID = ID, Qty = item.Qty, Tax = item.Tax, TaxValue = item.TaxValue, TaxValueTotal = item.TaxValueTotal, Price = item.Price }; _supplyCategoryServ.AddSupplyCategory(_supplyCategory); Category cat = _categoryServ.GetCategory(item.CategoryID); if (cat.Qty + item.Qty != 0) { cat.Cost = (item.CostTotalAfterDiscount + (cat.Cost * cat.Qty)) / (cat.Qty + item.Qty); } cat.Qty = cat.Qty + item.Qty; cat.Price = item.Price; _categoryServ.UpdateCategory(cat); } if (_selectedSupply.Future == true) { _selectedSupplyFuture.Change = _selectedSupply.Change; _selectedSupplyFuture.SupplyID = ID; if (_selectedSupplyFuture.Cheque == false) { _selectedSupplyFuture.ChequeNumber = null; } _supplyFutureServ.AddSupplyFuture(_selectedSupplyFuture); } ClientAccount _account = new ClientAccount { ClientID = _selectedSupply.ClientID, Date = _selectedSupply.Date, RegistrationDate = _selectedSupply.RegistrationDate, Statement = "فاتورة مشتريات رقم " + ID, Credit = _selectedSupply.CostAfterTax, Debit = _selectedSupply.CashPaid }; _clientAccountServ.AddAccount(_account); if (_selectedSupply.DiscountPaid != 0 || _selectedSupply.TotalDiscount != 0) { _account = new ClientAccount { ClientID = _selectedSupply.ClientID, Date = _selectedSupply.Date, RegistrationDate = _selectedSupply.RegistrationDate, Statement = "خصومات فاتورة مشتريات رقم " + ID, Credit = _selectedSupply.DiscountPaid, Debit = _selectedSupply.TotalDiscount }; _clientAccountServ.AddAccount(_account); } if (_selectedSupply.CashPaid > 0) { Safe _safe = new Safe { Date = _selectedSupply.Date, RegistrationDate = _selectedSupply.RegistrationDate, Statement = "فاتورة مشتريات رقم " + ID + " من العميل : " + _selectedSupply.Client.Name, Amount = -_selectedSupply.CashPaid, Source = 3 }; _safeServ.AddSafe(_safe); } DS ds = new DS(); ds.Sale.Rows.Clear(); int i = 0; foreach (var item in _supplyCategories) { ds.Sale.Rows.Add(); ds.Sale[i]["ID"] = ID; ds.Sale[i]["Date"] = _selectedSupply.Date; ds.Sale[i]["Client"] = _selectedSupply.Client.Name; ds.Sale[i]["Serial"] = i + 1; ds.Sale[i]["Category"] = item.Category + " " + item.Company; ds.Sale[i]["Qty"] = item.Qty; ds.Sale[i]["Price"] = Math.Round(Convert.ToDecimal(item.CostAfterTax), 2); ds.Sale[i]["TotalPrice"] = Math.Round(Convert.ToDecimal(item.CostTotalAfterTax), 2); ds.Sale[i]["BillPrice"] = Math.Round(Convert.ToDecimal(_selectedSupply.CostAfterTax), 2); ds.Sale[i]["OldDebt"] = Math.Abs(Math.Round(Convert.ToDecimal(_selectedSupply.OldDebt), 2)); ds.Sale[i]["Paid"] = Math.Abs(Math.Round(Convert.ToDecimal(_selectedSupply.CashPaid + _selectedSupply.DiscountPaid), 2)); ds.Sale[i]["NewDebt"] = Math.Abs(Math.Round(Convert.ToDecimal(_selectedSupply.NewDebt), 2)); if (_selectedSupply.NewDebt > 0) { ds.Sale[i]["PrintingMan"] = "له"; } else if (_selectedSupply.NewDebt < 0) { ds.Sale[i]["PrintingMan"] = "عليه"; } if (_selectedSupply.OldDebt > 0) { ds.Sale[i]["Type"] = "له"; } else if (_selectedSupply.OldDebt < 0) { ds.Sale[i]["Type"] = "عليه"; } ds.Sale[i]["BillTotal"] = Math.Abs(Math.Round(Convert.ToDecimal(_selectedSupply.OldDebt), 2) + Math.Round(Convert.ToDecimal(_selectedSupply.CostAfterTax), 2)); if (Math.Round(Convert.ToDecimal(_selectedSupply.OldDebt), 2) + Math.Round(Convert.ToDecimal(_selectedSupply.CostAfterTax), 2) > 0) { ds.Sale[i]["Type2"] = "له"; } else if (Math.Round(Convert.ToDecimal(_selectedSupply.OldDebt), 2) + Math.Round(Convert.ToDecimal(_selectedSupply.CostAfterTax), 2) < 0) { ds.Sale[i]["Type2"] = "عليه"; } i++; } ReportWindow rpt = new ReportWindow(); SupplyReport supplyRPT = new SupplyReport(); supplyRPT.SetDataSource(ds.Tables["Sale"]); rpt.crv.ViewerCore.ReportSource = supplyRPT; Mouse.OverrideCursor = null; _currentWindow.Hide(); rpt.ShowDialog(); _currentWindow.Close(); }