예제 #1
0
    private void PopulateDailyExpBySearch(string FieldName, string Value)
    {
        PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster DailyExp = new PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster();

        if (FieldName == "ByDate")
        {
            dtDailyExp = DailyExp.GetDailyExpMasterByExpDate(Value);
        }
        if (FieldName == "ToAndFromDate")
        {
            string[] ToandFrom = Value.Split('#');
            string   toDate    = "";
            string   fromDate  = "";
            for (int i = 0; i < ToandFrom.Length; i++)
            {
                toDate   = ToandFrom[i].ToString();
                fromDate = ToandFrom[i + 1].ToString();
                break;
            }

            dtDailyExp = DailyExp.GetDailyExpMasterByToandFromDate(toDate.ToString(), fromDate.ToString());
        }
        if (FieldName == "ByMonthYear")
        {
            dtDailyExp = DailyExp.GetDailyExpMasterByMonthYear(Value);
        }

        lblCashInHand.Text   = "0";
        lblTotalExpense.Text = "0";

        if (dtDailyExp.Rows.Count > 0)
        {
            for (int i = 0; i < dtDailyExp.Rows.Count; i++)
            {
                try
                {
                    if (dtDailyExp.Rows[i]["ID"].ToString() == dtDailyExp.Rows[i + 1]["ID"].ToString())
                    {
                        //lblCashInHand.Text = Convert.ToString(Convert.ToInt32(lblCashInHand.Text) + Convert.ToInt32(dtDailyExp.Rows[i]["StartCash"].ToString()));
                        //lblTotalExpense.Text = Convert.ToString(Convert.ToInt32(lblTotalExpense.Text) + Convert.ToInt32(dtDailyExp.Rows[i]["TotalAmount"].ToString()));
                    }
                    else
                    {
                        lblCashInHand.Text   = Convert.ToString(Convert.ToInt32(lblCashInHand.Text) + Convert.ToInt32(dtDailyExp.Rows[i]["StartCash"].ToString()));
                        lblTotalExpense.Text = Convert.ToString(Convert.ToInt32(lblTotalExpense.Text) + Convert.ToInt32(dtDailyExp.Rows[i]["TotalAmount"].ToString()));
                    }
                }
                catch
                {
                    lblCashInHand.Text   = Convert.ToString(Convert.ToInt32(lblCashInHand.Text) + Convert.ToInt32(dtDailyExp.Rows[i]["StartCash"].ToString()));
                    lblTotalExpense.Text = Convert.ToString(Convert.ToInt32(lblTotalExpense.Text) + Convert.ToInt32(dtDailyExp.Rows[i]["TotalAmount"].ToString()));
                }
            }
        }
        dgDailyExps.DataSource = dtDailyExp;
        dgDailyExps.DataBind();
        CntExpNumber = 1;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //string ModeCheck = "";
        if (Request.QueryString["DailyExpMasterID"] != null && Request.QueryString["DailyExpMasterID"].ToString() != "" && Request.QueryString["DailyExpDetailID"] != null && Request.QueryString["DailyExpDetailID"].ToString() != "" && !Page.IsPostBack)
        {
            // This is for Updating and ITEM in Expense
            long ExpenseDetailID = Convert.ToInt32(Request.QueryString["DailyExpDetailID"].ToString());
            PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster Expense = new PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster();
            txtItemDescID.Text  = Request.QueryString["DailyExpDetailID"].ToString();
            lblTotalAmount.Text = "0";
            dtExpenseEdit       = Expense.GetDailyExpDetailByID(ExpenseDetailID);
            for (int a = 0; a < dtExpenseEdit.Rows.Count; a++)
            {
                if (dtExpenseEdit.Rows[a]["ID"].ToString() == Request.QueryString["DailyExpDetailID"].ToString())
                {
                    txtItemDesc.Text           = dtExpenseEdit.Rows[a]["ItemDesc"].ToString();
                    txtItemDescID.Text         = dtExpenseEdit.Rows[a]["ID"].ToString();
                    txtQuantity.Text           = dtExpenseEdit.Rows[a]["Quantity"].ToString();
                    txtUnitPrice.Text          = dtExpenseEdit.Rows[a]["UnitPrice"].ToString();
                    txtItemAmount.Text         = dtExpenseEdit.Rows[a]["ItemAmount"].ToString();
                    txtItemAmountPrevious.Text = dtExpenseEdit.Rows[a]["ItemAmount"].ToString();

                    PopulateExpenses(Convert.ToInt32(Request.QueryString["DailyExpMasterID"].ToString()));
                }

                lblTotalAmount.Text     = Convert.ToString(Convert.ToInt32(lblTotalAmount.Text) + Convert.ToInt32(dtExpenseEdit.Rows[a]["ItemAmount"].ToString()));
                lblTotalExpOfToday.Text = lblTotalAmount.Text;
            }
        }

        if (!Page.IsPostBack)
        {
            PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster ExpM    = new PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster();
            PCSN.InvoiceSystem.BusinessLogicLayer.Closing        closing = new PCSN.InvoiceSystem.BusinessLogicLayer.Closing();
            DataTable dtClosing = new DataTable();
            DataTable dtExpM    = new DataTable();
            // It is to refresh the total expense page.
            if (Request.QueryString["DailyExpMasterID"] != null && Request.QueryString["DailyExpMasterID"].ToString() != "")
            {
                txtDailyExpMasterID.Text = Request.QueryString["DailyExpMasterID"].ToString();
            }
            else
            {
                dtExpM = ExpM.GetDailyExpMasterByExpDate(DateTime.Now.ToShortDateString());
                if (dtExpM.Rows.Count > 0)
                {
                    txtDailyExpMasterID.Text = dtExpM.Rows[0]["ID"].ToString();
                    lblExpenseNumber.Text    = txtDailyExpMasterID.Text;
                }
            }
            if (txtDailyExpMasterID.Text == "")
            {
                // This will run everytime u access the Expense page, to check if it is a new Day or the closing has been made for this day?.

                // Checking the Closing of Today?
                dtClosing = closing.GetAllDailyClosingByDate(DateTime.Now.ToShortDateString());
                if (dtClosing.Rows.Count > 0)
                {
                }
                else
                {
                    // So the Closing has not been performed.. Means NO NEW EXPENSE

                    dtExpM = ExpM.GetDailyExpMasterByExpDate(DateTime.Now.ToShortDateString());
                    if (dtExpM.Rows.Count > 0)
                    {
                        txtDailyExpMasterID.Text = dtExpM.Rows[0]["ID"].ToString();
                        lblExpenseNumber.Text    = txtDailyExpMasterID.Text;
                    }

                    // Performing Closing ( IT WILL CHECK IF THE CLOSING FOR YESTERDAY IS PERFORMED OR NOT )
                    //=============================
                    long DEM_ID = 0;
                    // First we will get the EXPENSE ID of Yesterday
                    dtExpM = ExpM.GetDailyExpMasterByExpDate(DateTime.Now.AddDays(-1).ToShortDateString());
                    if (dtExpM.Rows.Count > 0)
                    {
                        DEM_ID = Convert.ToInt32(dtExpM.Rows[0]["ID"].ToString());
                    }
                    // Now we are Checking the Closing of Yesterday
                    dtClosing = closing.GetAllDailyClosingByDate(DateTime.Now.AddDays(-1).ToShortDateString());
                    if (dtClosing.Rows.Count > 0 || DEM_ID == 0)
                    {
                        // Closing is already DONE or It was a Holiday or WeekEnd
                    }
                    else
                    {
                        // CLOSING Daily Closing of Yesterday
                        closing.InsertDailyClosing(DateTime.Now.AddDays(-1).ToShortDateString(), DEM_ID);
                    }
                    //========================================
                }
            }
            if (txtDailyExpMasterID.Text == "")
            {
                // This is when a Fresh Expense is About to Generate
                // DAILY CLOSING Is DONE for Previous day
                lblRemainingCash.Text = "0";
                lblStartCash.Text     = "0";
                PCSN.InvoiceSystem.BusinessLogicLayer.OurCompany ourCompany = new PCSN.InvoiceSystem.BusinessLogicLayer.OurCompany();
                dtOurCompany = ourCompany.GetOurCompanyByID(Convert.ToInt32("1"));

                if (dtOurCompany.Rows.Count <= 0)
                {
                    txtOurCompany.Text = "PC.Solutions.NET";
                }
                else
                {
                    txtOurCompanyID.Text = dtOurCompany.Rows[0]["ID"].ToString();
                    txtOurCompany.Text   = dtOurCompany.Rows[0]["CompanyName"].ToString() + Environment.NewLine;
                    txtOurCompany.Text   = txtOurCompany.Text + dtOurCompany.Rows[0]["CompanyAddress"].ToString() + Environment.NewLine;
                }
                lblStartCash.Text = "0";
                PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster Expense    = new PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster();
                PCSN.InvoiceSystem.BusinessLogicLayer.CashInHand     CashinHand = new PCSN.InvoiceSystem.BusinessLogicLayer.CashInHand();
                DataTable dtCashinHand = new DataTable();
                // Getting Cash in Hand Detail
                dtCashinHand = CashinHand.GetAllCashInHandByMAXID();
                if (dtCashinHand.Rows.Count > 0)
                {
                    txtCashInHandID.Text = dtCashinHand.Rows[0]["ID"].ToString();
                    dtCashinHand         = CashinHand.GetAllCashInHandByDateIN(DateTime.Now.ToShortDateString());
                    // Getting the CASH IN HAND details of Today
                    if (dtCashinHand.Rows.Count > 0)
                    {
                        if (dtCashinHand.Rows.Count > 1)
                        {
                            for (int i = 0; i > dtCashinHand.Rows.Count; i++)
                            {
                                txtCashInHandID.Text  = dtCashinHand.Rows[i]["ID"].ToString();
                                lblStartCash.Text     = Convert.ToString(Convert.ToInt32(lblStartCash.Text) + Convert.ToInt32(dtCashinHand.Rows[i]["CashIN"].ToString()));
                                lblRemainingCash.Text = Convert.ToString(Convert.ToInt32(lblRemainingCash.Text) + Convert.ToInt32(dtCashinHand.Rows[i]["TotalAmount"].ToString()));
                            }
                        }
                        else
                        {
                            txtCashInHandID.Text  = dtCashinHand.Rows[0]["ID"].ToString();
                            lblStartCash.Text     = dtCashinHand.Rows[0]["CashAvailable"].ToString();
                            lblRemainingCash.Text = dtCashinHand.Rows[0]["CashAvailable"].ToString();
                        }
                    }
                    else
                    {
                        dtCashinHand = CashinHand.GetAllCashInHandByMAXID();
                        if (dtCashinHand.Rows.Count > 0)
                        {
                            long CashinhandID = CashinHand.InsertCashInHand(Convert.ToInt32(dtCashinHand.Rows[0]["ChequeIssueID"].ToString()), Convert.ToInt32(dtCashinHand.Rows[0]["CashAvailable"].ToString()), Convert.ToInt32("0"), DateTime.Now.ToShortDateString(), Convert.ToInt32(dtCashinHand.Rows[0]["CashAvailable"].ToString()), "Automatic Entry to Data Base of Starting Cash in Hand for Today");

                            dtCashinHand = CashinHand.GetAllCashInHandByMAXID();
                            if (dtCashinHand.Rows.Count > 0)
                            {
                                txtCashInHandID.Text  = dtCashinHand.Rows[0]["ID"].ToString();
                                lblStartCash.Text     = dtCashinHand.Rows[0]["CashAvailable"].ToString();
                                lblRemainingCash.Text = dtCashinHand.Rows[0]["CashAvailable"].ToString();
                            }
                        }
                    }
                }
                else
                {
                    Response.Redirect("CashRegister.aspx?errmsg=You do not have enough Funds available to use as Cash. Please issue a Cheque for Personal Use and then Use it in order to use Daily Expense System.");
                }

                lblHeaderDate.Text      = Convert.ToString(DateTime.Now.ToShortDateString());
                lblTotalAmount.Text     = "0";
                lblTotalExpOfToday.Text = lblTotalAmount.Text;


                dtExpense = Expense.GetAllDailyExpMasterMAX();
                if (dtExpense.Rows.Count == 0 || dtExpense.Rows[0]["MAXID"].ToString() == "")
                {
                    lblExpenseNumber.Text = "1";
                }
                else
                {
                    lblExpenseNumber.Text = Convert.ToInt32(Convert.ToInt32(dtExpense.Rows[0]["MAXID"].ToString()) + 1).ToString();
                }
            }
            else
            {
                // This is when the Expense ID is getting from somewhere
                // AND the DAILY CLOSING has not been performed
                // Like from Query Strings

                PCSN.InvoiceSystem.BusinessLogicLayer.OurCompany ourCompany = new PCSN.InvoiceSystem.BusinessLogicLayer.OurCompany();
                dtOurCompany = ourCompany.GetOurCompanyByID(Convert.ToInt32("1"));

                if (dtOurCompany.Rows.Count <= 0)
                {
                    txtOurCompany.Text = "PC.Solutions.NET";
                }
                else
                {
                    txtOurCompanyID.Text = dtOurCompany.Rows[0]["ID"].ToString();
                    txtOurCompany.Text   = dtOurCompany.Rows[0]["CompanyName"].ToString() + Environment.NewLine;
                    txtOurCompany.Text   = txtOurCompany.Text + dtOurCompany.Rows[0]["CompanyAddress"].ToString() + Environment.NewLine;
                }

                PCSN.InvoiceSystem.BusinessLogicLayer.CashInHand CashinHand = new PCSN.InvoiceSystem.BusinessLogicLayer.CashInHand();
                DataTable dtCashinHand = new DataTable();
                dtCashinHand = CashinHand.GetAllCashInHandByMAXID();
                // Setting up the CASH IN HAND VALUES
                if (dtCashinHand.Rows.Count > 0)
                {
                    txtCashInHandID.Text = dtCashinHand.Rows[0]["ID"].ToString();
                }
                else
                {
                    // If Cash in Hand Not Found then you Need to Add some Cash in Hand in order to run Daily Expense
                    Response.Redirect("CashRegister.aspx");
                }

                lblHeaderDate.Text  = Convert.ToString(DateTime.Now.ToShortDateString());
                lblTotalAmount.Text = "0";
                PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster Expense = new PCSN.InvoiceSystem.BusinessLogicLayer.DailyExpMaster();

                dtExpense = Expense.GetDailyExpMasterByID(Convert.ToInt32(txtDailyExpMasterID.Text));
                if (dtExpense.Rows.Count > 0)
                {
                    lblStartCash.Text       = dtExpense.Rows[0]["StartCash"].ToString();
                    lblRemainingCash.Text   = dtExpense.Rows[0]["EndCash"].ToString();
                    lblExpenseNumber.Text   = dtExpense.Rows[0]["ID"].ToString();
                    lblTotalAmount.Text     = dtExpense.Rows[0]["TotalAmount"].ToString();
                    txtCashInHandID.Text    = dtExpense.Rows[0]["CashInHandID"].ToString();
                    lblHeaderDate.Text      = dtExpense.Rows[0]["ExpDate"].ToString();
                    lblTotalExpOfToday.Text = lblTotalAmount.Text;
                    lblRemainingCash.Text   = Convert.ToString(Convert.ToInt32(lblStartCash.Text) - Convert.ToInt32(lblTotalExpOfToday.Text));
                }
                if (txtDailyExpMasterID.Text != "")
                {
                    PopulateExpenses(Convert.ToInt32(txtDailyExpMasterID.Text));
                }
            }
        }
    }