bool Delete(int SalesID)
    {
        //return     objSalesMaster.Delete(p => p.SalesID == SalesID);

        objSalesMaster = objSalesMaster.Single(p => p.SalesID == SalesID);
        // totalpurchase-payments
        Double             rest      = objSalesMaster.Rest;//محصله الفاتوره النهائيه الا هى عباره عن اجمالى الفاتوره ناقص مدفوع العميل
        List <SalesDetail> itemslist = objSalesMaster.SalesDetails.ToList();
        var payments = objSalesMaster.CustomersPayment;

        objCustomer = objSalesMaster.Customer;


        try
        {
            objSalesMaster.BeginTransaction();

            //update Customer balance
            objCustomer.Balance -= rest;
            objCustomer.Update(p => p.CustomerID == objCustomer.CustomerID);
            //update products (store)
            itemslist.ForEach(p =>
            {
                //update qty
                SalesManager.UpdateproductQTY(QTYOperation.Increment, p.ProductID, p.Quantity);
            }
                              );



            // delete purchaseitems(PurchasesDetails)
            itemslist.DeleteAll();



            //delete masetr(PurchasesMaster)

            objSalesMaster.Delete(p => p.SalesID == SalesID);
            //delete payments
            if (payments != null)
            {
                payments.Delete(p => p.CustomerPaymentID == payments.CustomerPaymentID);
            }
            objSalesMaster.EndTransaction();
        }
        catch (Exception ex)
        {
            ExtenssionClass.Rollback();
            Page.Show(ex.Message);
            return(false);
        }
        return(true);
    }
    bool Update()
    {
        objSalesMaster = new SalesMaster();
        objSalesMaster = objSalesMaster.Single(p => p.SalesID == Convert.ToInt32(txtID.Text.Trim()));

        objSalesMaster.CustomerName = txtCustomerName.Text;

        objSalesMaster.EmployeeID = (ddlEmployee.SelectedIndex > 0) ? (Nullable <int>) int.Parse(ddlEmployee.SelectedValue) : null;

        //objSalesMaster.CustomerID  = int.Parse(ddlCustomer .SelectedValue);//?
        objSalesMaster.Notes = txtNotes.Text.Trim();

        objSalesMaster.SalesDateM = Convert.ToDateTime(txtSalesDateM.Text.Trim(), culture);

        objSalesMaster.SalesDateH = txtSalesDateH.Text.Trim();

        objSalesMaster.IsCash = cboxIsCash.Checked;

        double oldrest = objSalesMaster.Rest;

        bool oldPaymentType = objSalesMaster.IsCash;
        int? paymentid      = objSalesMaster.CustomerPaymentID;

        objSalesMaster.TotalSales = Convert.ToDouble(lblTotalSalesPrice.Text);
        objSalesMaster.Rest       = Convert.ToDouble(lblRest.Text);

        double Payments = Convert.ToDouble(!string.IsNullOrEmpty(txtPayment.Text) ? txtPayment.Text : "0.0");

        objSalesMaster.Payments = Payments;



        objSalesMaster.BeginTransaction();


        CustomersPayment CustomersPayment = new CustomersPayment();

        if (paymentid == null)
        {
            if (Payments > 0)
            {
                CustomersPayment.CustomerID        = objSalesMaster.CustomerID;;
                CustomersPayment.PaymentDateM      = Convert.ToDateTime(txtSalesDateM.Text.Trim(), culture);
                CustomersPayment.PaymentDateH      = txtSalesDateH.Text.Trim();
                CustomersPayment.Payment           = Payments;
                CustomersPayment.CustomerPaymentID = Convert.ToInt32(CustomersPayment.Insert());
            }
        }
        else if (paymentid != null)
        {
            CustomersPayment = objSalesMaster.CustomersPayment;
            CustomersPayment.PaymentDateM = Convert.ToDateTime(txtSalesDateM.Text.Trim(), culture);
            CustomersPayment.PaymentDateH = txtSalesDateH.Text.Trim();
            CustomersPayment.Payment      = Payments;
            CustomersPayment.Update(p => p.CustomerPaymentID == CustomersPayment.CustomerPaymentID);
        }

        objSalesMaster.Update(p => p.SalesID == objSalesMaster.SalesID);

        //var q=objPurchasesDetail.Filter(p => p.PurchaseID == objPurchasesMaster.PurchaseID).Select(p => (p.TotalPrice ));
        //double oldTotalPurchasePrice =(q!=null )?q.Sum():0.0;


        List <SalesDetail> SalesList = CurrentgvProducts(gvProducts);

        SalesList.ForEach(p =>
        {
            p.Quantity    = SalesManager.UnitConvert(p.UnitID, p.SQTY);
            p.UnitPrice   = SalesManager.UnitPriceConvert(p.UnitID, p.SUnitPrice);
            p.BasicUnitID = SalesManager.getBasicUnitID;
        });

        //double TotalPurchasePrice = Purchaseslist.Sum(p => p.TotalPrice );
        List <SalesDetail> newSalesList    = SalesList.Where(p => p.SalesDetailID == 0).ToList();
        List <SalesDetail> updateSalesList = SalesList.Where(p => p.SalesDetailID > 0).ToList();



        if (newSalesList.Count > 0)
        {
            newSalesList.ForEach(p => p.SalesID = objSalesMaster.SalesID);
            newSalesList.InsertAll();

            // update qty product in store(table product)
            newSalesList.ForEach(p => SalesManager.UpdateproductQTY(QTYOperation.Decrement, p.ProductID, p.Quantity));
        }
        if (updateSalesList.Count > 0)
        {
            updateSalesList.ForEach(p =>
            {
                p.Update(z => z.SalesDetailID == p.SalesDetailID);
                SalesManager.UpdateproductQTY(QTYOperation.Decrement, p.ProductID, (p.Quantity - p.oldQty));
            });
        }



        //========= composite calcualtion balance --- compare between old value and new  value
        if (oldPaymentType != true)// not cash
        {
            if (ddlCustomer.SelectedIndex > 0)
            {
                // any way( - balance) form old supplier if user change supplier
                objCustomer          = objCustomer.Single(p => p.CustomerID == objSalesMaster.CustomerID);
                objCustomer.Balance -= oldrest;
                // objSupplier.Update(p => p.SupplierID == objSupplier.SupplierID);

                if (cboxIsCash.Checked != true)// if (not cash) add new price to balance
                {
                    //objSupplier = objSupplier.Single(p => p.SupplierID == int.Parse(ddlSupplier.SelectedValue));
                    objCustomer.Balance += objSalesMaster.Rest;
                }
                objCustomer.Update(p => p.CustomerID == objCustomer.CustomerID);
            }
        }
        else if (oldPaymentType == true)    //  cash
        {
            if (cboxIsCash.Checked != true) // if user change to (not cash) will add new price to supplier balance
            {
                if (ddlCustomer.SelectedIndex > 0)
                {
                    objCustomer          = objCustomer.Single(p => p.CustomerID == objSalesMaster.CustomerID);
                    objCustomer.Balance += objSalesMaster.Rest;
                    objCustomer.Update(p => p.CustomerID == objCustomer.CustomerID);
                }
            }
        }

        objSalesMaster.EndTransaction();

        return(true);
    }