private void ExecuteSave() { try { DateTime _dt = DateTime.Now; _newSupply.RegistrationDate = _dt; _supplyServ.AddSupply(_newSupply); int _supplyID = _supplyServ.GetLastSupplyID(); foreach (var item in _supplyCategories) { SupplyCategory _supplyCategory = new SupplyCategory { CategoryID = item.CategoryID, Cost = item.Cost, CostTotal = item.CostTotal, SupplyID = _supplyID, 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 = _newSupply.ClientID, Date = _newSupply.Date, RegistrationDate = _dt, Statement = "فاتورة مشتريات رقم " + _supplyID, Credit = _newSupply.Cost, Debit = _newSupply.CashPaid + _newSupply.DiscountPaid }; _clientAccountServ.AddAccount(_account); if (_newSupply.CashPaid > 0) { Safe _safe = new Safe { Date = _newSupply.Date, RegistrationDate = _dt, Statement = "فاتورة مشتريات رقم " + _supplyID + " من العميل : " + _selectedClient.Name, Amount = -_newSupply.CashPaid, Source = 3 }; _safeServ.AddSafe(_safe); } NewSupply = new Supply(); NewSupplyCategory = new SupplyCategoryVM(); SupplyCategories = new ObservableCollection <SupplyCategoryVM>(); NewSupply.Date = DateTime.Now; OldCosts = new ObservableCollection <SupplyCategory>(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private async void ExecuteSaveAsync() { if (NewSupply.Future == true && NewSupplyFuture.Place == null) { await _currentWindow.ShowMessageAsync("فشل الإضافة", "يجب كتابة مكان الدفع", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "موافق", DialogMessageFontSize = 25, DialogTitleFontSize = 30 }); return; } Mouse.OverrideCursor = Cursors.Wait; DateTime _dt = DateTime.Now; _newSupply.RegistrationDate = _dt; _supplyServ.AddSupply(_newSupply); int _supplyID = _supplyServ.GetLastSupplyID(); 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 = _supplyID, 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 (_newSupply.Future == true) { _newSupplyFuture.Change = _newSupply.Change; _newSupplyFuture.SupplyID = _supplyID; if (_newSupplyFuture.Cheque == false) { _newSupplyFuture.ChequeNumber = null; } _supplyFutureServ.AddSupplyFuture(_newSupplyFuture); _placesSuggestions.Add(_newSupplyFuture.Place); } ClientAccount _account = new ClientAccount { ClientID = _newSupply.ClientID, Date = _newSupply.Date, RegistrationDate = _dt, Statement = "فاتورة مشتريات رقم " + _supplyID, Credit = _newSupply.CostAfterTax, Debit = _newSupply.CashPaid }; _clientAccountServ.AddAccount(_account); if (_newSupply.DiscountPaid != 0 || _newSupply.TotalDiscount != 0) { _account = new ClientAccount { ClientID = _newSupply.ClientID, Date = _newSupply.Date, RegistrationDate = _dt, Statement = "خصومات فاتورة مشتريات رقم " + _supplyID, Credit = _newSupply.DiscountPaid, Debit = _newSupply.TotalDiscount }; _clientAccountServ.AddAccount(_account); } if (_newSupply.CashPaid > 0) { Safe _safe = new Safe { Date = _newSupply.Date, RegistrationDate = _dt, Statement = "فاتورة مشتريات رقم " + _supplyID + " من العميل : " + _selectedClient.Name, Amount = -_newSupply.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"] = _supplyID; ds.Sale[i]["Date"] = _newSupply.Date; ds.Sale[i]["Client"] = _selectedClient.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(_newSupply.CostAfterTax), 2); ds.Sale[i]["OldDebt"] = Math.Abs(Math.Round(Convert.ToDecimal(_newSupply.OldDebt), 2)); ds.Sale[i]["Paid"] = Math.Abs(Math.Round(Convert.ToDecimal(_newSupply.CashPaid + _newSupply.DiscountPaid), 2)); ds.Sale[i]["NewDebt"] = Math.Abs(Math.Round(Convert.ToDecimal(_newSupply.NewDebt), 2)); if (_newSupply.NewDebt > 0) { ds.Sale[i]["PrintingMan"] = "له"; } else if (_newSupply.NewDebt < 0) { ds.Sale[i]["PrintingMan"] = "عليه"; } if (_newSupply.OldDebt > 0) { ds.Sale[i]["Type"] = "له"; } else if (_newSupply.OldDebt < 0) { ds.Sale[i]["Type"] = "عليه"; } ds.Sale[i]["BillTotal"] = Math.Abs(Math.Round(Convert.ToDecimal(_newSupply.OldDebt), 2) + Math.Round(Convert.ToDecimal(_newSupply.CostAfterTax), 2)); if (Math.Round(Convert.ToDecimal(_newSupply.OldDebt), 2) + Math.Round(Convert.ToDecimal(_newSupply.CostAfterTax), 2) > 0) { ds.Sale[i]["Type2"] = "له"; } else if (Math.Round(Convert.ToDecimal(_newSupply.OldDebt), 2) + Math.Round(Convert.ToDecimal(_newSupply.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(); NewSupply = new Supply(); NewSupplyCategory = new SupplyCategoryVM(); NewSupplyFuture = new SupplyFuture(); SupplyCategories = new ObservableCollection <SupplyCategoryVM>(); NewSupply.Date = DateTime.Now; NewSupplyFuture.Date = DateTime.Now; OldCosts = new ObservableCollection <SupplyCategory>(); _currentWindow.ShowDialog(); }