void Dir(PaymentsForSupplier entity)
    {
        txtID.Text = entity.SupplierPaymentID.ToString();

        ddlSupplier.SelectedValue = entity.SupplierID.ToString();
        txtPayment.Text           = entity.Payment.ToString();
        txtPaymentDateM.Text      = entity.PaymentDateM.ToString("MM/dd/yyyy", culture);
        txtPaymentDateH.Text      = entity.PaymentDateH;
        oldPayment = entity.Payment;
    }
    bool Insert()
    {
        objPaymentsForSupplier = new PaymentsForSupplier();
        objPaymentsForSupplier.BeginTransaction();
        objPaymentsForSupplier.SupplierID   = int.Parse(ddlSupplier.SelectedValue);
        objPaymentsForSupplier.Payment      = Convert.ToDouble(txtPayment.Text.Trim());
        objPaymentsForSupplier.PaymentDateM = Convert.ToDateTime(txtPaymentDateM.Text.Trim(), culture);
        objPaymentsForSupplier.PaymentDateH = txtPaymentDateH.Text.Trim();
        objPaymentsForSupplier.Insert();

        objSupplier          = objSupplier.Single(p => p.SupplierID == int.Parse(ddlSupplier.SelectedValue));
        objSupplier.Balance -= Convert.ToDouble(txtPayment.Text.Trim());
        objSupplier.Update(p => p.SupplierID == objSupplier.SupplierID);
        objSupplier.EndTransaction();
        return(true);
    }
    bool Delete(int SupplierPaymentID)
    {
        try
        {
            objPaymentsForSupplier.BeginTransaction();
            objPaymentsForSupplier = objPaymentsForSupplier.Single(z => z.SupplierPaymentID == SupplierPaymentID);


            objSupplier          = objSupplier.Single(p => p.SupplierID == objPaymentsForSupplier.SupplierID);
            objSupplier.Balance += objPaymentsForSupplier.Payment;
            objSupplier.Update(p => p.SupplierID == objSupplier.SupplierID);

            objPaymentsForSupplier.Delete(p => p.SupplierPaymentID == objPaymentsForSupplier.SupplierPaymentID);
            objPaymentsForSupplier.EndTransaction();
        }
        catch (Exception ex)
        {
            return(false);
        }
        return(true);
    }
    bool Update()
    {
        objPurchasesMaster = new PurchasesMaster();


        objPurchasesMaster = objPurchasesMaster.Single(p => p.PurchaseID == Convert.ToInt32(txtID.Text.Trim()));

        //objPurchasesMaster.SupplierID = int.Parse(ddlSupplier.SelectedValue);
        objPurchasesMaster.EmployeeID    = (ddlEmployee.SelectedIndex > 0) ? (Nullable <int>) int.Parse(ddlEmployee.SelectedValue) : null;
        objPurchasesMaster.Notes         = txtNotes.Text.Trim();
        objPurchasesMaster.PurchaseDateM = Convert.ToDateTime(txtPurchaseDateM.Text.Trim(), culture);
        objPurchasesMaster.PurchaseDateH = txtPurchaseDateH.Text.Trim();
        objPurchasesMaster.ISCash        = cboxIsCash.Checked;

        double oldrest        = objPurchasesMaster.Rest;
        bool   oldPaymentType = objPurchasesMaster.ISCash;
        int?   paymentid      = objPurchasesMaster.SupplierPaymentID;

        objPurchasesMaster.TotalPurchase = Convert.ToDouble(lblTotalPurchasePrice.Text);
        objPurchasesMaster.Rest          = Convert.ToDouble(lblRest.Text);

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

        objPurchasesMaster.Payments = Payments;



        objPurchasesMaster.BeginTransaction();
        PaymentsForSupplier paymentforsupplier = new PaymentsForSupplier();

        if (paymentid == null)
        {
            if (Payments > 0)
            {
                paymentforsupplier.SupplierID        = Convert.ToInt32(ddlSupplier.SelectedValue);
                paymentforsupplier.PaymentDateM      = Convert.ToDateTime(txtPurchaseDateM.Text.Trim(), culture);
                paymentforsupplier.PaymentDateH      = txtPurchaseDateH.Text.Trim();
                paymentforsupplier.Payment           = Payments;
                objPurchasesMaster.SupplierPaymentID = Convert.ToInt32(paymentforsupplier.Insert());
            }
        }
        else if (paymentid != null)
        {
            paymentforsupplier = objPurchasesMaster.PaymentsForSupplier;
            paymentforsupplier.PaymentDateM = Convert.ToDateTime(txtPurchaseDateM.Text.Trim(), culture);
            paymentforsupplier.PaymentDateH = txtPurchaseDateH.Text.Trim();
            paymentforsupplier.Payment      = Payments;
            paymentforsupplier.Update(p => p.SupplierPaymentID == paymentforsupplier.SupplierPaymentID);
        }

        objPurchasesMaster.Update(p => p.PurchaseID == objPurchasesMaster.PurchaseID);

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


        List <PurchasesDetail> Purchaseslist = CurrentgvProducts(gvProducts);

        Purchaseslist.ForEach(p =>
        {
            p.Quantity    = SalesManager.UnitConvert(p.UnitID, p.PQTY);
            p.UnitPrice   = SalesManager.UnitPriceConvert(p.UnitID, p.PUnitPrice);
            p.BasicUnitID = SalesManager.getBasicUnitID;
        });

        //double TotalPurchasePrice = Purchaseslist.Sum(p => p.TotalPrice );
        List <PurchasesDetail> newPurchaseslist    = Purchaseslist.Where(p => p.PurchasesDetailID == 0).ToList();
        List <PurchasesDetail> updatePurchaseslist = Purchaseslist.Where(p => p.PurchasesDetailID > 0).ToList();



        if (newPurchaseslist.Count > 0)
        {
            newPurchaseslist.ForEach(p => p.PurchaseID = objPurchasesMaster.PurchaseID);
            newPurchaseslist.InsertAll();

            // update qty product in store(table product)
            newPurchaseslist.ForEach(p => SalesManager.UpdateproductQTY(QTYOperation.Increment, p.ProductID, p.Quantity));
        }
        if (updatePurchaseslist.Count > 0)
        {
            updatePurchaseslist.ForEach(p =>
            {
                p.Update(z => z.PurchasesDetailID == p.PurchasesDetailID);
                SalesManager.UpdateproductQTY(QTYOperation.Increment, p.ProductID, (p.Quantity - p.oldQty));
            });
        }



        //========= composite calcualtion balance --- compare between old value and new  value
        if (oldPaymentType != true)// not cash
        {
            // any way( - balance) form old supplier if user change supplier
            objSupplier          = objSupplier.Single(p => p.SupplierID == objPurchasesMaster.SupplierID);
            objSupplier.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));
                objSupplier.Balance += objPurchasesMaster.Rest;
            }
            objSupplier.Update(p => p.SupplierID == objSupplier.SupplierID);
        }
        else if (oldPaymentType == true)    //  cash
        {
            if (cboxIsCash.Checked != true) // if user change to (not cash) will add new price to supplier balance
            {
                objSupplier          = objSupplier.Single(p => p.SupplierID == int.Parse(ddlSupplier.SelectedValue));
                objSupplier.Balance += objPurchasesMaster.Rest;
                objSupplier.Update(p => p.SupplierID == objSupplier.SupplierID);
            }
        }

        objPurchasesMaster.EndTransaction();

        return(true);
    }
    bool Insert()
    {
        objPurchasesMaster = new PurchasesMaster();

        objPurchasesMaster.SupplierID    = int.Parse(ddlSupplier.SelectedValue);
        objPurchasesMaster.EmployeeID    = (ddlEmployee.SelectedIndex > 0) ? (Nullable <int>) int.Parse(ddlEmployee.SelectedValue) : null;
        objPurchasesMaster.Notes         = txtNotes.Text.Trim();
        objPurchasesMaster.PurchaseDateM = Convert.ToDateTime(txtPurchaseDateM.Text.Trim(), culture);
        objPurchasesMaster.PurchaseDateH = txtPurchaseDateH.Text.Trim();

        objPurchasesMaster.TotalPurchase = Convert.ToDouble(lblTotalPurchasePrice.Text);
        objPurchasesMaster.Rest          = Convert.ToDouble(lblRest.Text);

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

        objPurchasesMaster.Payments = Payments;


        objPurchasesMaster.ISCash = cboxIsCash.Checked;



        List <PurchasesDetail> Purchaseslist = new List <PurchasesDetail>();

        Purchaseslist = CurrentgvProducts(gvProducts);
        Purchaseslist.ForEach(p => {
            p.Quantity    = SalesManager.UnitConvert(p.UnitID, p.PQTY);
            p.UnitPrice   = SalesManager.UnitPriceConvert(p.UnitID, p.PUnitPrice);
            p.BasicUnitID = SalesManager.getBasicUnitID;
        });



        //double TotalPurchasePrice = Purchaseslist.Sum(p => p.TotalPrice);
        objPurchasesMaster.PurchasesDetails.AddRange(Purchaseslist);


        objPurchasesMaster.BeginTransaction();

        if (Payments > 0)
        {
            PaymentsForSupplier paymentforsupplier = new PaymentsForSupplier();
            paymentforsupplier.SupplierID        = Convert.ToInt32(ddlSupplier.SelectedValue);
            paymentforsupplier.PaymentDateM      = Convert.ToDateTime(txtPurchaseDateM.Text.Trim(), culture);
            paymentforsupplier.PaymentDateH      = txtPurchaseDateH.Text.Trim();
            paymentforsupplier.Payment           = Payments;
            objPurchasesMaster.SupplierPaymentID = Convert.ToInt32(paymentforsupplier.Insert());
        }

        objPurchasesMaster.Insert();

        // update qty product in store(table product)
        Purchaseslist.ForEach(p => SalesManager.UpdateproductQTY(QTYOperation.Increment, p.ProductID, p.Quantity));

        if (cboxIsCash.Checked != true)
        {
            objSupplier          = objSupplier.Single(p => p.SupplierID == int.Parse(ddlSupplier.SelectedValue));
            objSupplier.Balance += objPurchasesMaster.Rest;
            objSupplier.Update(p => p.SupplierID == objSupplier.SupplierID);
        }


        objPurchasesMaster.EndTransaction();



        return(true);
    }