Beispiel #1
0
        private void ExecuteSave()
        {
            try
            {
                Mouse.OverrideCursor      = Cursors.Wait;
                _selectedSale.Salesperson = SelectedSalesperson;
                _saleServ.UpdateSale(_selectedSale);
                _safeServ.DeleteSafe(_selectedSale.RegistrationDate);
                _clientAccountServ.DeleteAccount(_selectedSale.RegistrationDate);
                var saleCategories = _saleCategoryServ.GetSaleCategories(_selectedSale.ID);
                foreach (var item in saleCategories)
                {
                    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;
                    _categoryServ.UpdateCategory(cat);
                }
                _saleCategoryServ.DeleteSaleCategories(ID);
                foreach (var item in _saleCategories)
                {
                    SaleCategory _saleCategory = new SaleCategory
                    {
                        CategoryID = item.CategoryID,
                        Cost       = item.Cost,
                        CostTotal  = item.CostTotal,
                        Price      = item.Price,
                        PriceTotal = item.PriceTotal,
                        SaleID     = ID,
                        Qty        = item.Qty
                    };
                    _saleCategoryServ.AddSaleCategory(_saleCategory);
                    Category cat = _categoryServ.GetCategory(item.CategoryID);
                    cat.Qty = cat.Qty - item.Qty;
                    _categoryServ.UpdateCategory(cat);
                }

                ClientAccount _account = new ClientAccount
                {
                    ClientID         = _selectedSale.ClientID,
                    Date             = _selectedSale.Date,
                    RegistrationDate = _selectedSale.RegistrationDate,
                    Statement        = "فاتورة مبيعات رقم " + ID,
                    Credit           = _selectedSale.CashPaid + _selectedSale.DiscountPaid,
                    Debit            = _selectedSale.Price
                };
                _clientAccountServ.AddAccount(_account);

                if (_selectedSale.CashPaid > 0)
                {
                    Safe _safe = new Safe
                    {
                        Date             = _selectedSale.Date,
                        RegistrationDate = _selectedSale.RegistrationDate,
                        Statement        = "فاتورة مبيعات رقم " + ID + " للعميل: " + _selectedSale.Client.Name,
                        Amount           = _selectedSale.CashPaid,
                        Source           = 4
                    };
                    _safeServ.AddSafe(_safe);
                }
                Mouse.OverrideCursor = null;
                _currentWindow.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void ExecuteSave()
        {
            Mouse.OverrideCursor      = Cursors.Wait;
            _selectedSale.Salesperson = SelectedSalesperson;
            _saleServ.UpdateSale(_selectedSale);
            _safeServ.DeleteSafe(_selectedSale.RegistrationDate);
            _clientAccountServ.DeleteAccount(_selectedSale.RegistrationDate);
            var saleCategories = _saleCategoryServ.GetSaleCategories(_selectedSale.ID);

            foreach (var item in saleCategories)
            {
                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;
                _categoryServ.UpdateCategory(cat);
            }
            _saleCategoryServ.DeleteSaleCategories(ID);
            foreach (var item in _saleCategories)
            {
                SaleCategory _saleCategory = new SaleCategory
                {
                    CategoryID              = item.CategoryID,
                    Cost                    = item.Cost,
                    PriceAfterDiscount      = item.PriceAfterDiscount,
                    PriceTotalAfterDiscount = item.PriceTotalAfterDiscount,
                    CostTotal               = item.CostTotal,
                    Price                   = item.Price,
                    PriceTotal              = item.PriceTotal,
                    Discount                = item.Discount,
                    DiscountValue           = item.DiscountValue,
                    DiscountValueTotal      = item.DiscountValueTotal,
                    SaleID                  = ID,
                    Qty = item.Qty
                };
                _saleCategoryServ.AddSaleCategory(_saleCategory);
                Category cat = _categoryServ.GetCategory(item.CategoryID);
                cat.Qty = cat.Qty - item.Qty;
                _categoryServ.UpdateCategory(cat);
            }

            ClientAccount _account = new ClientAccount
            {
                ClientID         = _selectedSale.ClientID,
                Date             = _selectedSale.Date,
                RegistrationDate = _selectedSale.RegistrationDate,
                Statement        = "فاتورة مبيعات رقم " + ID,
                Credit           = _selectedSale.Paid,
                Debit            = _selectedSale.PriceAfterDiscount + _selectedSale.TransportCost
            };

            _clientAccountServ.AddAccount(_account);

            if (_selectedSale.Paid > 0)
            {
                Safe _safe = new Safe
                {
                    Date             = _selectedSale.Date,
                    RegistrationDate = _selectedSale.RegistrationDate,
                    Statement        = "فاتورة مبيعات رقم " + ID + " للعميل: " + _selectedSale.Client.Name,
                    Amount           = _selectedSale.Paid,
                    Source           = 4
                };
                _safeServ.AddSafe(_safe);
            }


            DS ds = new DS();

            ds.Sale.Rows.Clear();
            int i = 0;

            foreach (var item in _saleCategories)
            {
                ds.Sale.Rows.Add();
                ds.Sale[i]["ID"]            = ID;
                ds.Sale[i]["Date"]          = _selectedSale.Date;
                ds.Sale[i]["Client"]        = _selectedSale.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.PriceAfterDiscount), 2);
                ds.Sale[i]["TotalPrice"]    = Math.Round(Convert.ToDecimal(item.PriceTotalAfterDiscount), 2);
                ds.Sale[i]["BillPrice"]     = Math.Round(Convert.ToDecimal(_selectedSale.PriceAfterDiscount), 2);
                ds.Sale[i]["OldDebt"]       = Math.Abs(Convert.ToDecimal(_selectedSale.OldDebt));
                ds.Sale[i]["TransportCost"] = _selectedSale.TransportCost;
                ds.Sale[i]["BillTotal"]     = Math.Abs(Math.Round(Convert.ToDecimal(_selectedSale.PriceTotal), 2));
                ds.Sale[i]["Paid"]          = _selectedSale.Paid;
                ds.Sale[i]["NewDebt"]       = Math.Abs(Math.Round(Convert.ToDecimal(_selectedSale.NewDebt), 2));
                if (_selectedSale.NewDebt > 0)
                {
                    ds.Sale[i]["Type"] = "له";
                }
                else if (_selectedSale.NewDebt < 0)
                {
                    ds.Sale[i]["Type"] = "عليه";
                }
                ds.Sale[i]["Salesperson"] = _selectedSalesperson.Name;
                ds.Sale[i]["PrintingMan"] = _selectedSale.PrintingMan;

                if (_selectedSale.OldDebt > 0)
                {
                    ds.Sale[i]["Type2"] = "له";
                }
                else if (_selectedSale.OldDebt < 0)
                {
                    ds.Sale[i]["Type2"] = "عليه";
                }
                ds.Sale[i]["Address"] = _selectedSale.Client.Address;
                try
                {
                    ToWord toWord = new ToWord(Math.Abs(Convert.ToDecimal(_selectedSale.NewDebt)), currencies[0]);
                    ds.Sale[i]["NewDebtAR"] = toWord.ConvertToArabic();
                }
                catch
                {
                    ds.Sale[i]["NewDebtAR"] = string.Empty;
                }

                i++;
            }
            ReportWindow rpt     = new ReportWindow();
            SaleReport   saleRPT = new SaleReport();

            saleRPT.SetDataSource(ds.Tables["Sale"]);
            rpt.crv.ViewerCore.ReportSource = saleRPT;
            Mouse.OverrideCursor            = null;
            _currentWindow.Hide();
            rpt.ShowDialog();
            _currentWindow.Close();
        }