bool deleteSalesDetail(int SalesDetailID)
    {
        // objSalesDetail.BeginTransaction();
        // objSalesDetail = objSalesDetail.Single(p => p.SalesDetailID == SalesDetailID);


        //// double Totalproductprice = objSalesDetail.Price * objSalesDetail.Quantity;

        // // update balance
        // objCustomer = objCustomer.Single(p => p.CustomerID == Convert.ToInt32(ddlCustomer.SelectedValue));
        //// objCustomer.Balance =(objCustomer.Balance)- (Totalproductprice);
        // objCustomer.Update(p => p.CustomerID == objCustomer.CustomerID);

        // //update qty
        // SalesManager.UpdateproductQTY(QTYOperation.Increment, objSalesDetail.ProductID, objSalesDetail.Quantity);
        // // delete row
        // bool status = objSalesDetail.Delete(p => p.SalesDetailID == SalesDetailID);
        // objSalesDetail.EndTransaction();
        // return status;

        objSalesDetail.BeginTransaction();
        objSalesDetail = objSalesDetail.Single(p => p.SalesDetailID == SalesDetailID);
        //var payment = objPurchasesDetail.PurchasesMaster.PaymentsForSupplier;
        //payment.Delete(p => p.SupplierPaymentID == payment.SupplierPaymentID);

        objSalesMaster             = objSalesDetail.SalesMaster;
        objSalesMaster.TotalSales -= objSalesDetail.TotalPrice;
        objSalesMaster.Rest       -= objSalesDetail.TotalPrice;

        objSalesMaster.Update(p => p.SalesID == objSalesMaster.SalesID);
        hfOldRest.Value = objSalesMaster.Rest.ToString();
        // update balance
        if (ddlCustomer.SelectedIndex > 0)
        {
            objCustomer         = objCustomer.Single(p => p.CustomerID == objSalesDetail.SalesMaster.CustomerID);
            objCustomer.Balance = (objCustomer.Balance) - (objSalesDetail.TotalPrice);
            objCustomer.Update(p => p.CustomerID == objCustomer.CustomerID);
        }

        //update qty
        SalesManager.UpdateproductQTY(QTYOperation.Increment, objSalesDetail.ProductID, objSalesDetail.Quantity);

        bool status = objSalesDetail.Delete(p => p.SalesDetailID == SalesDetailID);

        objSalesDetail.EndTransaction();
        return(status);
    }
    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);
    }