protected void Button1_Click(object sender, EventArgs e)
        {
            if (invTxt.Text != "")
            {
                int tempSaleInvId = Convert.ToInt32(invTxt.Text);
                try
                {
                    using (AdvInvSystemEntities dbobj = new AdvInvSystemEntities())
                    {
                        SaleInvoice s = new SaleInvoice();
                        s = (from s1 in dbobj.SaleInvoices where s1.SaleInvoicId == tempSaleInvId select s1).FirstOrDefault();
                        var si = from si1 in dbobj.SaleInvoices where si1.SaleInvoicId == tempSaleInvId select si1;


                        GridView3.DataSource = si.ToList();
                        GridView3.DataBind();

                        Customer cu = new Customer();

                        cu = (from c in dbobj.Customers where c.CustomerId == s.CustomerId select c).FirstOrDefault();

                        if (cu != null)
                        {
                            cusDetLbl.Text = cu.CustomerName + " / " + cu.CellNumber;

                            cusDetLbl.Visible = true;
                            Button3.Visible   = true;
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int tempId = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);

            using (AdvInvSystemEntities dbobj = new AdvInvSystemEntities())
            {
                //saleInvoices si = new saleInvoices();
                SaleInvoice si = new SaleInvoice();
                si = (from sid in dbobj.SaleInvoices
                      where sid.SaleInvoicId == tempId
                      select sid).FirstOrDefault();

                var sil = dbobj.sp_saleInvoice_Dept(tempId);

                DetailsView1.DataSource = sil;
                DetailsView1.DataBind();

                loadGrid(tempId);
                GridView2.Visible    = true;
                DetailsView1.Visible = true;
            }
        }
Example #3
0
        public static void DepositFund(int invId, int fundAmount, string mode, int acc, string madeby)
        {
            int tempRemAm = 0;
            int tempCusId;

            try
            {
                using (AdvInvSystemEntities dbobj = new AdvInvSystemEntities())
                {
                    SaleInvoice si = new SaleInvoice();
                    si        = (from s in dbobj.SaleInvoices where s.SaleInvoicId == invId select s).FirstOrDefault();
                    tempCusId = si.CustomerId;

                    Customer cu = new Customer();
                    cu          = (from c in dbobj.Customers where c.CustomerId == tempCusId select c).FirstOrDefault();
                    cu.Balance += fundAmount;
                    dbobj.SaveChanges();

                    CustomersAccount ca = new CustomersAccount();
                    ca.CustomerId = tempCusId;
                    ca.Date       = System.DateTime.Now;
                    ca.Debit      = fundAmount;
                    ca.Credit     = 0;
                    dbobj.CustomersAccounts.Add(ca);
                    dbobj.SaveChanges();



                    if (si.Paid != si.Amount)
                    {
                        tempRemAm  = si.Payable - si.Paid;
                        fundAmount = fundAmount - tempRemAm;

                        if (fundAmount == si.Payable)
                        {
                            si.Paid   = si.Payable;
                            si.Status = "Cleared";
                        }

                        else
                        {
                            si.Paid   = fundAmount;
                            si.Status = "Pending";
                        }
                        si.PaymentMode = mode;
                        si.AccountId   = acc;
                        dbobj.SaveChanges();

                        AccountDetail ac = new AccountDetail();
                        ac.Particular = "Sale inv# " + si.SaleInvoicId;
                        ac.Amount     = si.Paid;
                        ac.Mode       = "Inward";
                        ac.DateTime   = System.DateTime.Now;
                        ac.AccountId  = acc;
                        ac.UserId     = Convert.ToInt32(HttpContext.Current.Session["UserId"]);
                        dbobj.AccountDetails.Add(ac);
                        dbobj.SaveChanges();


                        if (si.PaymentMode == "Cash")
                        {
                            CashTran ct = new CashTran();
                            ct.Particular      = "Sale inv# " + si.SaleInvoicId;
                            ct.Amount          = si.Paid;
                            ct.AccountDetailId = ac.AccountsDetailId;

                            dbobj.CashTrans.Add(ct);
                            dbobj.SaveChanges();
                        }
                        else if (si.PaymentMode == "Bank")
                        {
                            BnkTan bkt = new BnkTan();
                            bkt.Particular      = "Sale inv# " + si.SaleInvoicId;
                            bkt.Amount          = si.Paid;
                            bkt.AccountDetailId = ac.AccountsDetailId;


                            bkt.MadeBy  = madeby;
                            bkt.DueDate = System.DateTime.Now;

                            dbobj.BnkTans.Add(bkt);
                            dbobj.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count >= 1)
            {
                CurBalance = Convert.ToInt32(paidTxt.Text) + Convert.ToInt32(PreBal.Text);
                prevBal    = Convert.ToInt32(PreBal.Text);
                int temppayable = (Convert.ToInt32(ViewState["gt"]) - Convert.ToInt32(TotaldiscTxt.Text));
                if (Convert.ToInt32(paidTxt.Text) <= temppayable)
                {
                    Button2.Enabled = false;
                    try
                    {
                        using (AdvInvSystemEntities dbobj = new AdvInvSystemEntities())
                        {
                            if (cuscellTxt.Text != "")
                            {
                                string   cuscell = cuscellTxt.Text;
                                Customer cu      = new Customer();
                                cu = (from cust in dbobj.Customers
                                      where cust.CellNumber == cuscell
                                      select cust).FirstOrDefault();

                                if (cu == null)
                                {
                                    Customer cu1 = new Customer();
                                    cu1.CustomerName = cusTxt.Text;
                                    cu1.CellNumber   = cuscellTxt.Text;
                                    cu1.Address      = addTxt.Text;
                                    cu1.Type         = custTypeTxt.Text;
                                    cu1.BadDebt      = "Non-Defaulter";
                                    cu1.Balance      = CurBalance;
                                    cu1.Cnic         = cnicTxt.Text;

                                    dbobj.Customers.Add(cu1);
                                    dbobj.SaveChanges();

                                    ViewState["cusId"] = cu1.CustomerId;
                                }

                                else
                                {
                                    cu.Balance = CurBalance;
                                    dbobj.SaveChanges();
                                    cuId = cu.CustomerId;
                                    ViewState["cusId"] = cu.CustomerId;
                                }
                            }
                            else
                            {
                                Customer cu = new Customer();
                                cu = (from cust in dbobj.Customers
                                      where cust.CustomerId == 1
                                      select cust).FirstOrDefault();

                                cu.Balance         = CurBalance;
                                ViewState["cusId"] = 1;
                            }

                            SaleInvoice si = new SaleInvoice();
                            si.CustomerId        = Convert.ToInt32(ViewState["cusId"]);
                            si.Date              = System.DateTime.Now;
                            si.BranchId          = Convert.ToInt32(Session["BranchId"]);
                            si.Amount            = Convert.ToInt32(ViewState["gt"]);
                            si.Discount          = Convert.ToInt32(TotaldiscTxt.Text);
                            si.Payable           = Convert.ToInt32(ViewState["gt"]) - Convert.ToInt32(TotaldiscTxt.Text);
                            ViewState["payable"] = si.Payable;

                            int      tempcu2Id = Convert.ToInt32(ViewState["cusId"]);
                            Customer cu2       = new Customer();
                            cu2 = (from cust in dbobj.Customers
                                   where cust.CustomerId == tempcu2Id
                                   select cust).FirstOrDefault();

                            /*  if (Convert.ToInt32(paidTxt.Text) == 0 && prevBal <= 0)
                             * {
                             *    cu2.Balance = cu2.Balance - si.Payable;
                             *    si.Paid = 0;
                             *    si.ChangeReturn = si.Paid - si.Payable;
                             *    dbobj.SaveChanges();
                             * }
                             */
                            if (CurBalance != 0)
                            {
                                if (CurBalance >= si.Payable)
                                {
                                    si.Paid         = si.Payable;
                                    si.ChangeReturn = Convert.ToInt32(paidTxt.Text) - si.Payable;
                                    si.Status       = "Cleared";

                                    cu2.Balance = cu2.Balance - si.Payable;

                                    dbobj.SaveChanges();
                                }
                                else
                                {
                                    si.Paid         = Convert.ToInt32(paidTxt.Text);
                                    si.ChangeReturn = Convert.ToInt32(paidTxt.Text) - si.Payable;
                                    si.Status       = "Pending";

                                    cu2.Balance = CurBalance - si.Payable;
                                    dbobj.SaveChanges();
                                }
                            }
                            else
                            {
                                si.Paid         = 0;
                                si.ChangeReturn = 0;
                                si.Status       = "Pending";

                                cu2.Balance = cu2.Balance - si.Payable;
                            }

                            if (Convert.ToInt32(Session["BranchId"]) == 1)
                            {
                                accId = 1;
                            }

                            else
                            {
                                accId = 2;
                            }

                            TimeZoneInfo tzi;
                            DateTime     dtTz;
                            tzi  = TimeZoneInfo.FindSystemTimeZoneById("Pakistan Standard Time");
                            dtTz = TimeZoneInfo.ConvertTime(DateTime.Now, tzi);


                            si.Time = dtTz.ToString("h:mm:ss tt");

                            si.AccountId   = accId;
                            si.PaymentMode = "Cash";

                            si.UserId = Convert.ToInt32(Session["UserId"]);
                            dbobj.SaleInvoices.Add(si);
                            dbobj.SaveChanges();

                            ViewState["sInvId"] = si.SaleInvoicId;
                            Session["sInvId"]   = ViewState["sInvId"];


                            //   if (PMDropdownlist.SelectedItem.Value == "Cheque")
                            // {
                            //   ChequeList chql = new ChequeList();

                            // chql.Mode = "Inward";
                            // chql.ChequeNumber = chqTxt.Text;
                            // chql.Bank = bnkTxt.Text;
                            // chql.Amount = Convert.ToInt32(ammTxt.Text);
                            // chql.MadeBy = mbTxt.Text;
                            // chql.Date = System.DateTime.Now;
                            // chql.Status = "Pending";
                            // chql.AccountId = Convert.ToInt32(AccDropDownlist.SelectedItem.Value);

                            // dbobj.ChequeLists.Add(chql);
                            // dbobj.SaveChanges();
                            // }


                            CustomersAccount cusA = new CustomersAccount();
                            cusA.CustomerId = Convert.ToInt32(ViewState["cusId"]);
                            cusA.Date       = System.DateTime.Now;
                            cusA.Particular = "Sale Invoice# " + si.SaleInvoicId;

                            cusA.Amount = si.Payable;

                            if (Convert.ToInt32(paidTxt.Text) == 0 && prevBal <= 0)
                            {
                                cusA.Credit = cusA.Amount;
                                cusA.Debit  = 0;
                            }
                            if (Convert.ToInt32(paidTxt.Text) == 0 && prevBal > 0)
                            {
                                if (prevBal < cusA.Amount)
                                {
                                    cusA.Debit  = prevBal;
                                    cusA.Credit = cusA.Amount - prevBal;
                                }
                                if (prevBal >= cusA.Amount)
                                {
                                    cusA.Credit = 0;
                                    cusA.Debit  = cusA.Amount;
                                }
                            }

                            if (Convert.ToInt32(paidTxt.Text) < cusA.Amount && prevBal < 0)
                            {
                                cusA.Credit = cusA.Amount - Convert.ToInt32(paidTxt.Text);
                                cusA.Debit  = Convert.ToInt32(paidTxt.Text);
                            }
                            if (Convert.ToInt32(paidTxt.Text) < cusA.Amount && prevBal == 0)
                            {
                                cusA.Credit = cusA.Amount - Convert.ToInt32(paidTxt.Text);
                                cusA.Debit  = Convert.ToInt32(paidTxt.Text);
                            }
                            if (Convert.ToInt32(paidTxt.Text) < cusA.Amount && prevBal > 0)
                            {
                                if (prevBal + Convert.ToInt32(paidTxt.Text) < cusA.Amount)
                                {
                                    cusA.Debit  = prevBal + Convert.ToInt32(paidTxt.Text);
                                    cusA.Credit = cusA.Amount - (prevBal + Convert.ToInt32(paidTxt.Text));
                                }

                                if (prevBal + Convert.ToInt32(paidTxt.Text) >= cusA.Amount)
                                {
                                    cusA.Credit = 0;
                                    cusA.Debit  = cusA.Amount;
                                }
                            }

                            if (Convert.ToInt32(paidTxt.Text) == cusA.Amount)
                            {
                                cusA.Credit = 0;
                                cusA.Debit  = cusA.Amount;
                            }



                            dbobj.CustomersAccounts.Add(cusA);
                            dbobj.SaveChanges();

                            int tempCuId = Convert.ToInt32(ViewState["cusId"]);


                            SaleDetail sd = new SaleDetail();
                            for (int i = 0; i < GridView1.Rows.Count; i++)
                            {
                                sd.ProductId      = Convert.ToInt32(GridView1.Rows[i].Cells[2].Text);
                                sd.Quatity        = Convert.ToInt32(GridView1.Rows[i].Cells[4].Text);
                                sd.Rate           = Convert.ToInt32(GridView1.Rows[i].Cells[5].Text);
                                sd.DiscountedRate = Convert.ToInt32(GridView1.Rows[i].Cells[6].Text);
                                sd.SaleInvoiceId  = Convert.ToInt32(ViewState["sInvId"]);

                                dbobj.SaleDetails.Add(sd);
                                dbobj.SaveChanges();

                                Stock st           = new Stock();
                                int   tempbranchId = Convert.ToInt32(Session["BranchId"]);
                                st = (from sup in dbobj.Stocks
                                      where sup.ProductId == sd.ProductId && sup.LocationId == tempbranchId
                                      select sup).FirstOrDefault();
                                if (st != null)
                                {
                                    st.ProductId = sd.ProductId;
                                    st.Quantity -= sd.Quatity;
                                    dbobj.SaveChanges();
                                }
                            }

                            if (CurBalance != 0)
                            {
                                int tempAccId = accId;

                                Account ac = new Account();
                                ac = (from act in dbobj.Accounts
                                      where act.AccountId == tempAccId
                                      select act).FirstOrDefault();

                                if (ac != null)
                                {
                                    ac.CurrentBalance = (Convert.ToInt32(ac.CurrentBalance) + si.Paid).ToString();
                                    dbobj.SaveChanges();
                                }

                                AccountDetail acd = new AccountDetail();

                                acd.Particular = "Sale inv # " + ViewState["sInvId"];
                                acd.Amount     = si.Paid;
                                //acd.Amount = 0;
                                acd.Mode      = "Inward";
                                acd.DateTime  = System.DateTime.Now;
                                acd.AccountId = accId;
                                acd.UserId    = Convert.ToInt32(Session["UserId"]);
                                acd.Type      = "Cash";
                                acd.SaleInvId = si.SaleInvoicId;

                                dbobj.AccountDetails.Add(acd);
                                dbobj.SaveChanges();
                                int acdId = acd.AccountsDetailId;



                                if (acd.Type == "Cash")
                                {
                                    CashTran ct = new CashTran();
                                    ct.Particular = "Sale inv# " + ViewState["sInvId"];

                                    ct.Amount          = si.Paid;
                                    ct.AccountDetailId = acd.AccountsDetailId;

                                    dbobj.CashTrans.Add(ct);
                                    dbobj.SaveChanges();
                                }
                                else if (acd.Type == "Cheque")
                                {
                                    BnkTan bkt = new BnkTan();
                                    bkt.Particular      = "Sale inv# " + ViewState["sInvId"];
                                    bkt.Amount          = si.Paid;
                                    bkt.AccountDetailId = acd.AccountsDetailId;
                                    if (chqTxt.Text != "")
                                    {
                                        bkt.ChequeId = Convert.ToInt32(ViewState["chequeId"]);
                                    }
                                    bkt.MadeBy  = mbTxt.Text;
                                    bkt.DueDate = System.DateTime.Now;

                                    dbobj.BnkTans.Add(bkt);
                                    dbobj.SaveChanges();
                                }
                            }
                        }
                        Session["sInvId"] = ViewState["sInvId"];
                        string pageurl = "InvoicePrint.aspx";
                        Response.Write("<script> window.open('" + pageurl + "','_blank'); </script>");


                        //  Session["sInvId"] = Convert.ToInt32(ViewState["sInvId"]);

                        Response.Redirect("InvoicePrint.aspx");
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    Afail.Visible = true;
                }

                //int sid = Convert.ToInt32(Session["sInvId"]);
                // Response.Redirect(Request.RawUrl);
                // string pageurl1 = "InvoicePrint.aspx";
                //  Response.Write("<script> window.open('" + pageurl1 + "','_blank'); </script>");
            }
            else
            {
                Afail.Visible = true;
            }
        }
        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int     tempId = (Int32)ViewState["tempId"];
            int     id     = Convert.ToInt32(GridView2.Rows[e.RowIndex].Cells[0].Text);
            TextBox qn     = (TextBox)GridView2.Rows[e.RowIndex].FindControl("qntyTxt");
            TextBox rt     = (TextBox)GridView2.Rows[e.RowIndex].FindControl("rateTxt");
            TextBox dr     = (TextBox)GridView2.Rows[e.RowIndex].FindControl("drTxt");

            int prvBal  = Convert.ToInt32(dr.Text);
            int prvQnty = 0;
            int proId;
            int stLoc;

            //int qn = Convert.ToInt32(GridView2.Rows[e.RowIndex].Cells[3].Text);


            try
            {
                using (AdvInvSystemEntities dbobj = new AdvInvSystemEntities())
                {
                    var saleDetail = (from sd in dbobj.SaleDetails where sd.SaleInvoiceDetailId == id select sd).FirstOrDefault();
                    prvQnty = saleDetail.Quatity;
                    proId   = saleDetail.ProductId;

                    if (Convert.ToInt32(qn.Text) > 0)
                    {
                        saleDetail.Quatity        = Convert.ToInt32(qn.Text);
                        saleDetail.Rate           = Convert.ToInt32(rt.Text);
                        saleDetail.DiscountedRate = Convert.ToInt32(qn.Text) * Convert.ToInt32(rt.Text);
                        dbobj.SaveChanges();
                    }
                    else if (Convert.ToInt32(qn.Text) < 0)
                    {
                        dbobj.SaleDetails.Remove(saleDetail);
                        dbobj.SaveChanges();
                    }
                    if (saleDetail.DiscountedRate > prvBal)
                    {
                        diff = saleDetail.DiscountedRate - prvBal;
                    }
                    if (saleDetail.DiscountedRate < prvBal)
                    {
                        diff = saleDetail.DiscountedRate - prvBal;
                    }

                    CustomersAccount ca = new CustomersAccount();


                    SaleInvoice si = new SaleInvoice();
                    si = (from s in dbobj.SaleInvoices where s.SaleInvoicId == tempId select s).FirstOrDefault();

                    stLoc = si.BranchId;

                    si.Payable = si.Payable + diff;
                    si.Amount  = si.Amount + diff;

                    if (si.Status == "Cleared")
                    {
                        si.Paid = si.Payable;
                    }



                    dbobj.SaveChanges();

                    var stl = (from st in dbobj.Stocks where st.ProductId == proId && st.LocationId == stLoc select st).FirstOrDefault();


                    if (Convert.ToInt32(qn.Text) > 0)
                    {
                        if (prvQnty > Convert.ToInt32(qn.Text))
                        {
                            diff          = prvQnty - Convert.ToInt32(qn.Text);
                            stl.Quantity += diff;
                            dbobj.SaveChanges();
                        }
                        else if (Convert.ToInt32(qn.Text) > prvQnty)
                        {
                            diff          = Convert.ToInt32(qn.Text) - prvQnty;
                            stl.Quantity -= diff;
                            dbobj.SaveChanges();
                        }
                    }
                    else if (Convert.ToInt32(qn.Text) <= 0)
                    {
                        var st2 = (from st in dbobj.Stocks where st.ProductId == proId && st.LocationId == stLoc select st).FirstOrDefault();


                        st2.Quantity += prvQnty;
                        dbobj.SaveChanges();
                    }

                    /*  Customer cu = new Customer();
                     * cu = (from cust in dbobj.Customers
                     *      where cust.CustomerId == si.CustomerId
                     *      select cust).FirstOrDefault();
                     * if (cu.Balance != 0)
                     * {
                     *    cu.Balance += diff;
                     *    dbobj.SaveChanges();
                     * }
                     */



                    CustomersAccount cusA = new CustomersAccount();
                    cusA.CustomerId = si.CustomerId;
                    cusA.Date       = System.DateTime.Now;
                    cusA.Credit     = diff;
                    cusA.Debit      = diff;

                    cusA.Status = "Cleared";

                    dbobj.CustomersAccounts.Add(cusA);
                    dbobj.SaveChanges();

                    Account ac = new Account();
                    ac = (from act in dbobj.Accounts
                          where act.AccountId == si.AccountId
                          select act).FirstOrDefault();
                    ac.CurrentBalance = (Convert.ToInt32(ac.CurrentBalance) + diff).ToString();
                    dbobj.SaveChanges();

                    AccountDetail acd = new AccountDetail();

                    acd.Particular = "Sale Return inv# " + si.SaleInvoicId;
                    acd.Amount     = diff;

                    acd.Mode      = "Outward";
                    acd.DateTime  = System.DateTime.Now;
                    acd.AccountId = (Int32)si.AccountId;
                    acd.UserId    = Convert.ToInt32(Session["UserId"]);
                    acd.Type      = "Cash";
                    acd.SaleInvId = si.SaleInvoicId;

                    dbobj.AccountDetails.Add(acd);
                    dbobj.SaveChanges();

                    CashTran ct = new CashTran();
                    ct.Particular = "Sale Return inv# " + si.SaleInvoicId;

                    ct.Amount          = diff;
                    ct.AccountDetailId = acd.AccountsDetailId;

                    dbobj.CashTrans.Add(ct);
                    dbobj.SaveChanges();



                    GridView2.EditIndex = -1;

                    loadGrid(tempId);

                    DataBind();

                    Response.Redirect(Request.RawUrl);
                }
            }
            catch (Exception)
            {
            }
        }
        protected void Deposit_btn_Click(object sender, EventArgs e)
        {
            int acId = Convert.ToInt32(DropDownList2.SelectedItem.Value);

            fund = Convert.ToInt32(fundTxt.Text);
            if (fundTxt.Text != "" || Convert.ToInt32(fundTxt.Text) != 0)
            {
                int tempCustId = Convert.ToInt32(GridView1.Rows[0].Cells[0].Text);
                fund = Convert.ToInt32(fundTxt.Text);
                try
                {
                    using (AdvInvSystemEntities dbobj = new AdvInvSystemEntities())
                    {
                        Customer cu = new Customer();
                        cu = (from c in dbobj.Customers where c.CustomerId == tempCustId select c).FirstOrDefault();

                        cu.Balance = cu.Balance + fund;
                        dbobj.SaveChanges();



                        CustomersAccount ca = new CustomersAccount();
                        ca.CustomerId = tempCustId;
                        ca.Date       = System.DateTime.Now;
                        ca.Particular = "Fund Deposit";
                        ca.Amount     = fund;
                        ca.Debit      = fund;
                        ca.Credit     = 0;
                        ca.Status     = "Cleared";
                        dbobj.CustomersAccounts.Add(ca);
                        dbobj.SaveChanges();

                        //fund = (Int32)cu.Balance;

                        if (GridView2.Rows.Count >= 1)
                        {
                            for (int i = 0; i <= GridView2.Rows.Count; i++)
                            {
                                if (fund != 0)
                                {
                                    int         tempSInv = Convert.ToInt32(GridView2.Rows[i].Cells[0].Text);
                                    SaleInvoice si       = new SaleInvoice();

                                    si = (from s in dbobj.SaleInvoices where s.SaleInvoicId == tempSInv select s).FirstOrDefault();
                                    int tempBal = si.Payable - si.Paid;
                                    if (fund >= tempBal)
                                    {
                                        si.Paid = si.Payable;

                                        si.Status = "Cleared";
                                        dbobj.SaveChanges();

                                        //cu.Balance = cu.Balance - tempBal;
                                        //dbobj.SaveChanges();

                                        AccountDetail acd = new AccountDetail();
                                        acd.Particular = "Sale inv# " + si.SaleInvoicId;
                                        acd.Amount     = tempBal;
                                        acd.Mode       = "Inward";
                                        acd.DateTime   = System.DateTime.Now;
                                        acd.UserId     = Convert.ToInt32(Session["UserId"]);
                                        acd.SaleInvId  = si.SaleInvoicId;
                                        acd.Type       = "Cash";
                                        acd.AccountId  = Convert.ToInt32(DropDownList2.SelectedItem.Value);

                                        dbobj.AccountDetails.Add(acd);
                                        dbobj.SaveChanges();

                                        Account ac = new Account();
                                        ac = (from a in dbobj.Accounts where a.AccountId == acId select a).FirstOrDefault();

                                        ac.CurrentBalance = (Convert.ToInt32(ac.CurrentBalance) + tempBal).ToString();
                                        dbobj.SaveChanges();

                                        CashTran ct = new CashTran();
                                        ct.Particular      = "Sale inv# " + si.SaleInvoicId;
                                        ct.Amount          = tempBal;
                                        ct.AccountDetailId = acd.AccountsDetailId;

                                        dbobj.CashTrans.Add(ct);
                                        dbobj.SaveChanges();

                                        fund = fund - tempBal;
                                    }
                                    else if (fund > 0 && fund < tempBal)
                                    {
                                        int tempremBal = Convert.ToInt32(fund);


                                        si.Paid = si.Paid + Convert.ToInt32(fund);
                                        dbobj.SaveChanges();

                                        //cu.Balance = cu.Balance - Convert.ToInt32(fund);
                                        //dbobj.SaveChanges();

                                        AccountDetail acd = new AccountDetail();
                                        acd.Particular = "Sale inv# " + si.SaleInvoicId;
                                        acd.Amount     = tempremBal;
                                        acd.Mode       = "Inward";
                                        acd.DateTime   = System.DateTime.Now;
                                        acd.UserId     = Convert.ToInt32(Session["UserId"]);
                                        acd.SaleInvId  = si.SaleInvoicId;
                                        acd.Type       = "Cash";
                                        acd.AccountId  = Convert.ToInt32(DropDownList2.SelectedItem.Value);

                                        dbobj.AccountDetails.Add(acd);
                                        dbobj.SaveChanges();

                                        Account ac = new Account();
                                        ac = (from a in dbobj.Accounts where a.AccountId == acId select a).FirstOrDefault();

                                        ac.CurrentBalance = (Convert.ToInt32(ac.CurrentBalance) + tempremBal).ToString();
                                        dbobj.SaveChanges();


                                        CashTran ct = new CashTran();
                                        ct.Particular      = "Sale inv# " + si.SaleInvoicId;
                                        ct.Amount          = tempremBal;
                                        ct.AccountDetailId = acd.AccountsDetailId;

                                        dbobj.CashTrans.Add(ct);
                                        dbobj.SaveChanges();

                                        fund = 0;
                                        break;
                                    }
                                }
                            }
                        }
                        Response.Redirect(Request.RawUrl);
                    }
                }
                catch (Exception)
                {
                }
            }
        }