Esempio n. 1
0
        protected void Deposit_btn_Click(object sender, EventArgs e)
        {
            fund  = Convert.ToInt32(fundTxt.Text);
            AccId = Convert.ToInt32(DropDownList2.SelectedItem.Value);
            SupId = Convert.ToInt32(ViewState["SupId"]);
            if (fundTxt.Text != "" && fund != 0)
            {
                using (dbobj)
                {
                    Supplier sp = new Supplier();

                    sp          = (from s in dbobj.Suppliers where s.SupplierId == SupId select s).FirstOrDefault();
                    sp.Balance += fund;
                    dbobj.SaveChanges();

                    SupplierAccount spa = new SupplierAccount();
                    spa.SupplierId = sp.SupplierId;
                    spa.Particular = "Fund Deposit";
                    spa.Credit     = 0;
                    spa.Amount     = fund;
                    spa.Debit      = fund;
                    spa.Date       = System.DateTime.Now;
                    spa.AccountId  = AccId;
                    spa.UserId     = Convert.ToInt32(Session["UserId"]);

                    dbobj.SupplierAccounts.Add(spa);
                    dbobj.SaveChanges();

                    //fund = sp.Balance;

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


                    if (GridView3.Rows.Count >= 1)
                    {
                        for (int i = 0; i < GridView3.Rows.Count; i++)
                        {
                            PurcInv = Convert.ToInt32(GridView3.Rows[i].Cells[0].Text);
                            PurchaseInvoice pi = new PurchaseInvoice();

                            pi = (from p in dbobj.PurchaseInvoices where p.PurchaseInvoiceId == PurcInv select p).FirstOrDefault();


                            if (fund > 0)
                            {
                                if (fund >= pi.Balance)
                                {
                                    /*  SupplierAccount spa1 = new SupplierAccount();
                                     * spa1.SupplierId = sp.SupplierId;
                                     * spa1.Particular = "Payment for Purchase#" + PurcInv;
                                     * spa1.Credit = pi.Balance;
                                     * spa1.Debit = 0;
                                     * spa1.Date = System.DateTime.Now;
                                     * spa1.AccountId = AccId;
                                     * spa1.UserId = Convert.ToInt32(Session["UserId"]);
                                     *
                                     * dbobj.SupplierAccounts.Add(spa);
                                     * dbobj.SaveChanges();
                                     */
                                    int accb   = Convert.ToInt32(ac.CurrentBalance);
                                    int newbal = accb - fund;
                                    ac.CurrentBalance = newbal.ToString();
                                    dbobj.SaveChanges();

                                    AccountDetail acd = new AccountDetail();
                                    acd.Particular = "Purchase Inv# " + PurcInv;
                                    acd.Amount     = fund;
                                    acd.Mode       = "Outward";
                                    acd.DateTime   = System.DateTime.Now;
                                    acd.AccountId  = AccId;
                                    acd.UserId     = Convert.ToInt32(Session["UserId"]);
                                    acd.Type       = "Cash";
                                    acd.PurchInvId = PurcInv;

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

                                    //sp.Balance -= pi.Balance;
                                    //dbobj.SaveChanges();

                                    CashTran ct = new CashTran();
                                    ct.Particular      = "Purchase Inv# " + PurcInv;
                                    ct.Amount          = pi.Balance;
                                    ct.AccountDetailId = acd.AccountsDetailId;

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

                                    fund = fund - pi.Balance;

                                    pi.Paid    = pi.Amount;
                                    pi.Balance = 0;
                                    pi.Status  = "Cleared";


                                    dbobj.SaveChanges();
                                }

                                else if (fund < pi.Balance)
                                {
                                    pi.Paid    = pi.Paid + fund;
                                    pi.Balance = pi.Balance - fund;

                                    dbobj.SaveChanges();

                                    /* SupplierAccount spa1 = new SupplierAccount();
                                     * spa1.SupplierId = sp.SupplierId;
                                     * spa1.Particular = "Payment for Purchase#" + PurcInv;
                                     * spa1.Credit = fund;
                                     * spa1.Debit = 0;
                                     * spa1.Date = System.DateTime.Now;
                                     * spa1.AccountId = AccId;
                                     * spa1.UserId = Convert.ToInt32(Session["UserId"]);
                                     *
                                     * dbobj.SupplierAccounts.Add(spa);
                                     * dbobj.SaveChanges();
                                     */
                                    int accb   = Convert.ToInt32(ac.CurrentBalance);
                                    int newbal = accb - fund;
                                    ac.CurrentBalance = newbal.ToString();
                                    dbobj.SaveChanges();

                                    AccountDetail acd = new AccountDetail();
                                    acd.Particular = "Purchase Inv# " + PurcInv;
                                    acd.Amount     = fund;
                                    acd.Mode       = "Outward";
                                    acd.DateTime   = System.DateTime.Now;
                                    acd.AccountId  = AccId;
                                    acd.UserId     = Convert.ToInt32(Session["UserId"]);
                                    acd.Type       = "Cash";
                                    acd.PurchInvId = PurcInv;

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

                                    //sp.Balance -= fund;
                                    //dbobj.SaveChanges();

                                    CashTran ct = new CashTran();
                                    ct.Particular      = "Purchase Inv# " + PurcInv;
                                    ct.Amount          = fund;
                                    ct.AccountDetailId = acd.AccountsDetailId;

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

                                    fund = 0;
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            Response.Redirect(Request.RawUrl);
        }
Esempio n. 2
0
        protected void Button2_Click(object sender, EventArgs e)
        {
            qntyTxt.Focus();

            using (AdvInvSystemEntities dbobj = new AdvInvSystemEntities())
            {
                if (GridView1.Rows.Count != 0)
                {
                    if (Convert.ToInt32(paidTxt.Text) <= Convert.ToInt32(amountTxt.Text))
                    {
                        if (spcellTxt.Text != "")
                        {
                            Supplier sp = new Supplier();

                            sp = (from sup in dbobj.Suppliers
                                  where sup.CellNumber == spcellTxt.Text
                                  select sup).FirstOrDefault();


                            if (sp == null)
                            {
                                Supplier spn = new Supplier();

                                spn.Name       = spnmTxt.Text;
                                spn.CellNumber = spcellTxt.Text;
                                spn.Company    = spcomTxt.Text;
                                spn.Address    = spaddTxt.Text;
                                bal            = Convert.ToInt32(paidTxt.Text) - Convert.ToInt32(amountTxt.Text);
                                spn.Balance    = bal;

                                dbobj.Suppliers.Add(spn);
                                dbobj.SaveChanges();

                                supId = spn.SupplierId;
                            }

                            else
                            {
                                bal        = Convert.ToInt32(paidTxt.Text) - Convert.ToInt32(amountTxt.Text);
                                sp.Balance = sp.Balance + bal;
                                dbobj.SaveChanges();
                                supId = sp.SupplierId;
                            }
                        }
                        else
                        {
                            supId = 1;
                        }

                        PurchaseInvoice pi = new PurchaseInvoice();
                        pi.BrandId     = Convert.ToInt32(brdDropDown.SelectedItem.Value);
                        pi.Date        = System.DateTime.Now;
                        pi.ReferenceId = refTxt.Text;
                        pi.Amount      = Convert.ToInt32(amountTxt.Text);
                        pi.Paid        = Convert.ToInt32(paidTxt.Text);
                        pi.Balance     = Convert.ToInt32(amountTxt.Text) - Convert.ToInt32(paidTxt.Text);
                        if (pi.Balance != 0)
                        {
                            pi.Status = "Pending";
                        }
                        else
                        {
                            pi.Status = "Cleared";
                        }
                        pi.UserId        = Convert.ToInt32(Session["UserId"]);
                        pi.AccountCredit = Convert.ToInt32(AccDropDownlist.SelectedItem.Value);
                        pi.StockLocId    = Convert.ToInt32(brnchDownList.SelectedItem.Value);
                        locId            = pi.StockLocId;
                        pi.SupplierId    = supId;
                        if (PMDropdownlist.SelectedIndex != -1)
                        {
                            pi.PaymentMode = PMDropdownlist.SelectedItem.Value;
                        }
                        dbobj.PurchaseInvoices.Add(pi);
                        dbobj.SaveChanges();
                        ViewState["purInvId"] = pi.PurchaseInvoiceId;

                        SupplierAccount spa = new SupplierAccount();
                        spa.SupplierId = supId;
                        spa.Particular = "Purchash inv#" + pi.PurchaseInvoiceId;
                        spa.Amount     = Convert.ToInt32(amountTxt.Text);
                        spa.Debit      = pi.Paid;
                        spa.Credit     = Convert.ToInt32(amountTxt.Text) - pi.Paid;
                        spa.Date       = System.DateTime.Now;
                        spa.AccountId  = pi.AccountCredit;
                        spa.UserId     = 1;

                        dbobj.SupplierAccounts.Add(spa);
                        dbobj.SaveChanges();


                        ChequeList cql = new ChequeList();
                        if (PMDropdownlist.SelectedItem.Value == "Cheque")
                        {
                            cql.Mode         = "Outward";
                            cql.ChequeNumber = chqTxt.Text;
                            cql.Bank         = bnkTxt.Text;
                            cql.MadeBy       = mbTxt.Text;
                            cql.Amount       = Convert.ToInt32(ammTxt.Text);
                            cql.Date         = System.DateTime.Now;
                            cql.Status       = "Pending";
                            cql.AccountId    = 1;

                            dbobj.ChequeLists.Add(cql);
                            dbobj.SaveChanges();
                        }

                        ViewState["chequeId"] = cql.ChequeId;

                        PurchaseDetail purD = new PurchaseDetail();

                        for (int i = 0; i < GridView1.Rows.Count; i++)
                        {
                            purD.ProductId       = Convert.ToInt32(GridView1.Rows[i].Cells[2].Text);
                            purD.Quantity        = Convert.ToInt32(GridView1.Rows[i].Cells[4].Text);
                            purD.Price           = Convert.ToInt32(GridView1.Rows[i].Cells[5].Text);
                            purD.PurchaseInvoice = Convert.ToInt32(ViewState["purInvId"]);

                            dbobj.PurchaseDetails.Add(purD);
                            dbobj.SaveChanges();

                            Product pd = new Product();
                            pd = (from prod in dbobj.Products
                                  where prod.ProductId == purD.ProductId
                                  select prod).SingleOrDefault();

                            if (pd != null)
                            {
                                pd.PurchasePrice = Convert.ToInt32(GridView1.Rows[i].Cells[6].Text);
                                pd.SalePrice     = Convert.ToInt32(GridView1.Rows[i].Cells[7].Text);
                                dbobj.SaveChanges();
                            }

                            Stock st = new Stock();

                            st = (from sup in dbobj.Stocks
                                  where sup.ProductId == purD.ProductId && sup.LocationId == locId
                                  select sup).FirstOrDefault();
                            if (st != null)
                            {
                                //st.ProductId = purD.ProductId;
                                st.Quantity += purD.Quantity;
                                //st.LocationId = locId;

                                //dbobj.sp_Add_Stock(st.ProductId, st.LocationId, st.Quantity);
                                dbobj.SaveChanges();
                            }
                            else
                            {
                                Stock st1 = new Stock();
                                st1.ProductId  = purD.ProductId;
                                st1.Quantity   = purD.Quantity;
                                st1.LocationId = locId;

                                dbobj.Stocks.Add(st1);
                                dbobj.SaveChanges();
                            }
                        }
                        if (paidTxt.Text != "" || paidTxt.Text != "0")
                        {
                            int tempAccId = Convert.ToInt32(AccDropDownlist.SelectedItem.Value);

                            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) - Convert.ToInt32(paidTxt.Text)).ToString();
                                dbobj.SaveChanges();
                            }

                            AccountDetail acd = new AccountDetail();

                            acd.Particular = "Payment inv# " + ViewState["purInvId"];
                            acd.Amount     = Convert.ToInt32(paidTxt.Text);
                            acd.Mode       = "Outward";
                            acd.DateTime   = System.DateTime.Now;
                            acd.AccountId  = Convert.ToInt32(AccDropDownlist.SelectedItem.Value);
                            acd.UserId     = Convert.ToInt32(Session["UserId"]);
                            acd.Type       = PMDropdownlist.SelectedItem.Text;
                            acd.PurchInvId = Convert.ToInt32(ViewState["purInvId"]);

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

                            //pi.TransactionId = acd.AccountsDetailId;
                            ViewState["transId"] = acd.AccountsDetailId;



                            if (PMDropdownlist.SelectedItem.Text == "Cash")
                            {
                                CashTran ct = new CashTran();
                                ct.Particular      = "Purchase inv# " + ViewState["purInvId"];
                                ct.Amount          = Convert.ToInt32(paidTxt.Text);
                                ct.AccountDetailId = acd.AccountsDetailId;

                                dbobj.CashTrans.Add(ct);
                                dbobj.SaveChanges();
                            }
                            else if (PMDropdownlist.SelectedItem.Text == "Cheque")
                            {
                                BnkTan bkt = new BnkTan();
                                bkt.Particular      = "Purchase inv# " + ViewState["purInvId"];
                                bkt.Amount          = Convert.ToInt32(paidTxt.Text);
                                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();
                            }
                        }
                        Div2.Visible = true;
                        Response.Redirect(Request.RawUrl);
                    } // end of row count condition for checkout

                    else
                    {
                        Div1.Visible = true;
                    }
                }
                else
                {
                    Div1.Visible = true;
                }
            } // end of dbobj
        }