protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                try
                {
                    CommonList.LoadRentType(ddlRentType);
                    CommonList.LoadStore(ddlStore);
                    hdfFeeRate.Value = (GetFeeRate(Convert.ToInt32(Session["store_id"])) / 10000).ToString();
                    string id = Request.QueryString["ID"];
                    string sId = Request.QueryString["sID"];
                    string copy = Request.QueryString["copy"];
                    int STORE_ID = Convert.ToInt32(Session["store_id"]);

                    if (!string.IsNullOrEmpty(id) && string.IsNullOrEmpty(copy)) // EDIT
                    {
                        using (var db = new RentBikeEntities())
                        {
                            int contractId = Helper.parseInt(id);
                            var contract = db.Contracts.FirstOrDefault(c => c.CONTRACT_STATUS == true && c.ID == contractId);

                            IsNewContract = false;
                            ContractID = id;
                            List<CONTRACT_FULL_VW> lst;
                            int contractid = Convert.ToInt32(id);

                            Store stor = new Store();
                            stor = db.Stores.FirstOrDefault(s => s.ID == STORE_ID && s.ACTIVE == true);

                            var st = from s in db.CONTRACT_FULL_VW
                                     where s.ID == contractid && s.ACTIVE == true
                                     select s;

                            lst = st.ToList<CONTRACT_FULL_VW>();
                            ddlStore.SelectedValue = STORE_ID.ToString();

                            bool bDifferentSTORE_ID = false;
                            if (Helper.parseInt(sId) != STORE_ID)
                            {
                                if (!IS_ADMIN)
                                    bDifferentSTORE_ID = true;
                                STORE_ID = Helper.parseInt(sId);
                            }

                            ddlStore.SelectedValue = STORE_ID.ToString();
                            if (!IS_ADMIN)
                            {
                                ddlStore.Enabled = false;
                            }

                            if (!lst[0].CONTRACT_STATUS || (bDifferentSTORE_ID && !string.IsNullOrEmpty(Request.QueryString["sID"])))
                            {
                                pnlTable.Enabled = false;
                                rptPayFeeSchedule.Visible = false;
                            }

                            CONTRACT_FULL_VW cntrct = lst[0];
                            txtLicenseNumber.Text = cntrct.LICENSE_NO;
                            txtCustomerName.Text = cntrct.CUSTOMER_NAME;
                            txtBirthDay.Text = string.Format("{0:dd/MM/yyyy}", cntrct.BIRTH_DAY);
                            txtRangeDate.Text = string.Format("{0:dd/MM/yyyy}", cntrct.LICENSE_RANGE_DATE);
                            txtPlaceDate.Text = cntrct.LICENSE_RANGE_PLACE;
                            txtPhone.Text = cntrct.PHONE;
                            txtPermanentResidence.Text = cntrct.PERMANENT_RESIDENCE;
                            txtCurrentResidence.Text = cntrct.CURRENT_RESIDENCE;
                            txtContractNo.Text = cntrct.CONTRACT_NO;
                            var rentType = db.RentTypes.Where(c => c.NAME == cntrct.RENT_TYPE_NAME).FirstOrDefault();
                            ddlRentType.SelectedValue = rentType.ID.ToString();
                            RentTypeID = cntrct.RENT_TYPE_ID;
                            txtAmount.Text = string.Format("{0:0,0}", cntrct.CONTRACT_AMOUNT);
                            txtFeePerDay.Text = string.Format("{0:0,0}", cntrct.FEE_PER_DAY);
                            txtRentDate.Text = string.Format("{0:dd/MM/yyyy}", cntrct.RENT_DATE);
                            txtEndDate.Text = string.Format("{0:dd/MM/yyyy}", cntrct.END_DATE);
                            txtNote.Text = cntrct.NOTE;

                            txtReferencePerson.Text = cntrct.REFERENCE_NAME;
                            txtItemName.Text = cntrct.ITEM_TYPE;
                            txtItemLicenseNo.Text = cntrct.ITEM_LICENSE_NO;
                            txtSerial1.Text = cntrct.SERIAL_1;
                            txtSerial2.Text = cntrct.SERIAL_2;
                            txtImplementer.Text = cntrct.IMPLEMENTER;
                            txtBackDocument.Text = cntrct.BACK_TO_DOCUMENTS;
                            txtItemDetail.Text = cntrct.DETAIL;
                            txtReferencePhone.Text = cntrct.REFERENCE_PHONE;
                            txtSchool.Text = cntrct.SCHOOL_NAME;
                            txtClass.Text = cntrct.CLASS_NAME;

                            Customer customer = db.Customers.FirstOrDefault(c => c.ID == cntrct.CUSTOMER_ID);
                            if (customer != null)
                            {
                                if (customer.IS_LOW_RECOVERABILITY)
                                {
                                    btnLowRecoverability.Text = "Bỏ khả năng thu hồi thấp";
                                    btnLowRecoverability.CommandArgument = "RevertLowRecoverability";
                                }
                            }

                            BuildPhotoLibrary(cntrct);

                            ddlStore.Enabled = txtContractNo.Enabled = txtRentDate.Enabled = txtEndDate.Enabled = false;

                            LoadPayFeeSchedule();
                        }
                    }
                    else // NEW
                    {
                        //Remove [rent other] option if user is just staff
                        if (PERMISSION == ROLE.STAFF)
                            ddlRentType.Items.Remove(ddlRentType.Items[ddlRentType.Items.Count - 1]);

                        IsNewContract = true;
                        btnFinishContract.Visible = false;
                        txtContractNo.Visible = false;
                        using (var db = new RentBikeEntities())
                        {
                            if (!IS_ADMIN)
                            {
                                ddlStore.SelectedValue = STORE_ID.ToString();
                                ddlStore.Enabled = false;
                            }
                            RentTypeID = Convert.ToInt32(ddlRentType.SelectedValue);
                            txtRentDate.Text = string.Format("{0:dd/MM/yyyy}", DateTime.Now);
                            txtEndDate.Text = string.Format("{0:dd/MM/yyyy}", DateTime.Now.AddDays(29));

                            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(copy))
                            {
                                int contractid = Convert.ToInt32(id);
                                CONTRACT_FULL_VW cntrct = db.CONTRACT_FULL_VW.Where(s => s.ID == contractid && s.ACTIVE == true).FirstOrDefault();
                                if (cntrct != null)
                                {
                                    if (IS_ADMIN)
                                        STORE_ID = cntrct.STORE_ID;
                                    else
                                        ddlStore.Enabled = false;

                                    ddlStore.SelectedValue = STORE_ID.ToString();

                                    txtLicenseNumber.Text = cntrct.LICENSE_NO;
                                    txtCustomerName.Text = cntrct.CUSTOMER_NAME;
                                    txtBirthDay.Text = string.Format("{0:dd/MM/yyyy}", cntrct.BIRTH_DAY);
                                    txtRangeDate.Text = string.Format("{0:dd/MM/yyyy}", cntrct.LICENSE_RANGE_DATE);
                                    txtPlaceDate.Text = cntrct.LICENSE_RANGE_PLACE;
                                    txtPhone.Text = cntrct.PHONE;
                                    txtPermanentResidence.Text = cntrct.PERMANENT_RESIDENCE;
                                    txtCurrentResidence.Text = cntrct.CURRENT_RESIDENCE;
                                    txtContractNo.Text = cntrct.CONTRACT_NO;
                                    var rentType = db.RentTypes.Where(c => c.NAME == cntrct.RENT_TYPE_NAME).FirstOrDefault();
                                    ddlRentType.SelectedValue = rentType.ID.ToString();
                                    RentTypeID = cntrct.RENT_TYPE_ID;
                                    txtAmount.Text = string.Format("{0:0,0}", cntrct.CONTRACT_AMOUNT);
                                    txtFeePerDay.Text = string.Format("{0:0,0}", cntrct.FEE_PER_DAY);
                                    txtNote.Text = cntrct.NOTE;

                                    txtReferencePerson.Text = cntrct.REFERENCE_NAME;
                                    txtItemName.Text = cntrct.ITEM_TYPE;
                                    txtItemLicenseNo.Text = cntrct.ITEM_LICENSE_NO;
                                    txtSerial1.Text = cntrct.SERIAL_1;
                                    txtSerial2.Text = cntrct.SERIAL_2;
                                    txtImplementer.Text = cntrct.IMPLEMENTER;
                                    txtBackDocument.Text = cntrct.BACK_TO_DOCUMENTS;
                                    txtItemDetail.Text = cntrct.DETAIL;
                                    txtReferencePhone.Text = cntrct.REFERENCE_PHONE;
                                    txtSchool.Text = cntrct.SCHOOL_NAME;
                                    txtClass.Text = cntrct.CLASS_NAME;

                                    BuildPhotoLibrary(cntrct);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.Message;
                    lblMessage.CssClass = "text-center text-danger";
                }
            }

            //Disable UI for Admin account
            if (IS_ADMIN) pnlTable.Enabled = false;
        }
        private void CreateNewStore()
        {
            using (TransactionScope scope = new TransactionScope())
            {

                Store st = new Store();
                st.NAME = txtName.Text.Trim();
                st.ADDRESS = txtAddress.Text.Trim();
                st.CITY_ID = Convert.ToInt32(ddlCity.SelectedValue);
                st.PHONE = txtPhone.Text.Trim();
                st.FAX = string.Empty;
                st.START_CAPITAL = Convert.ToDecimal(txtStartCapital.Text);
                //st.CURRENT_CAPITAL = Convert.ToDecimal(txtCurrentCapital.Text);
                //st.APPLY_DATE = Convert.ToDateTime(txtApplyDate.Text);
                //st.REVENUE_BEFORE_APPLY = Convert.ToDecimal(txtTotalRevenueBefore.Text);
                //st.TOTAL_COST_BEFORE = Convert.ToDecimal(txtTotalCostBefore.Text);
                //st.TOTAL_INVESMENT_BEFORE = Convert.ToDecimal(txtTotalInvesmentBefore.Text);
                st.REGISTER_DATE = Convert.ToDateTime(txtRegisterDate.Text);
                st.ACTIVE = rdbActive.Checked;
                st.NOTE = txtNote.Text.Trim();
                st.SEARCH_TEXT = string.Format("{0} {1} {2}", st.NAME, st.ADDRESS, st.PHONE);

                using (var rb = new RentBikeEntities())
                {
                    rb.Stores.Add(st);
                    rb.SaveChanges();
                }

                using (var rb1 = new RentBikeEntities())
                {
                    var item = rb1.InOutTypes.FirstOrDefault(s =>s.NAME == "Nhập vốn");

                    InOut io = new InOut();
                    io.IN_AMOUNT = Convert.ToDecimal(txtStartCapital.Text.Replace(",", string.Empty));
                    io.OUT_AMOUNT = 0;
                    io.CONTRACT_ID = -1;
                    io.PERIOD_ID = -1;
                    io.RENT_TYPE_ID = -1;
                    io.PERIOD_DATE = DateTime.Now;
                    io.MORE_INFO = "Vốn đầu tư ban đầu khi đăng ký cửa hàng";
                    io.STORE_ID = st.ID;
                    io.INOUT_TYPE_ID = item.ID;
                    io.INOUT_DATE = DateTime.Now;
                    io.SEARCH_TEXT = string.Format("{0} {1} ngày {2}", io.MORE_INFO, io.IN_AMOUNT, io.INOUT_DATE);

                    rb1.InOuts.Add(io);
                    rb1.SaveChanges();

                    StoreFee sf = new StoreFee();
                    sf.STORE_ID = st.ID;
                    sf.FEE_PERCENT = 0;
                    rb1.StoreFees.Add(sf);
                    rb1.SaveChanges();
                }

                WriteLog(Constants.ACTION_CREATE_STORE, false);

                scope.Complete();
            }
        }
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                // LOAD PAYPERIOD
                int periodId = Convert.ToInt32(Request.QueryString["ID"]);
                PayPeriod pp = new PayPeriod();
                using (var db = new RentBikeEntities())
                {
                    var item = db.PayPeriods.Where(s => s.ID == periodId).FirstOrDefault();
                    pp = item;
                    PeriodDate = item.PAY_DATE;
                }

                LoadGrid(pp);
                LoadPaidAmountAndTheLeft(pp);
                // DISPLAY SREEN
                hplContract.NavigateUrl = string.Format("FormContractUpdate.aspx?ID={0}", pp.CONTRACT_ID);
                hplContract.Text = "Xem chi tiết hợp đồng";

                Store st = new Store();
                using (var db = new RentBikeEntities())
                {
                    var contract = db.Contracts.Where(c => c.ID == pp.CONTRACT_ID).FirstOrDefault();
                    if (contract != null)
                    {
                        STORE_ID = contract.STORE_ID;
                        var item = db.Stores.Where(s => s.ID == contract.STORE_ID).FirstOrDefault();
                        if (item != null)
                        {
                            st = item;
                            txtStore.Text = st.NAME;
                            txtStore.Enabled = false;
                        }

                        var customer = db.Customers.FirstOrDefault(c =>c.ID == contract.CUSTOMER_ID);
                        if (customer != null) CustomerName = customer.NAME;
                    }
                }

                using (var db = new RentBikeEntities())
                {
                    var item = db.CONTRACT_FULL_VW.FirstOrDefault(itm => itm.ID == pp.CONTRACT_ID && itm.ACTIVE == true);
                    int inoutType = 0;
                    switch (item.RENT_TYPE_NAME)
                    {
                        case "Cho thuê xe":
                            inoutType = 14;
                            break;
                        case "Cho thuê thiết bị văn phòng":
                            inoutType = 15;
                            break;
                        case "Cho thuê mặt hàng khác":
                            inoutType = 16;
                            break;
                    }

                    var inouttypelist = db.InOutTypes.Where(s => s.IS_CONTRACT == true && s.ACTIVE == true).ToList();

                    ddInOutType.DataSource = inouttypelist;
                    ddInOutType.DataTextField = "NAME";
                    ddInOutType.DataValueField = "ID";
                    ddInOutType.DataBind();
                    ddInOutType.SelectedValue = inoutType.ToString();

                    //txtIncome.Text = pp.ACTUAL_PAY.ToString();

                    //
                }
            }
            //Disable UI for Admin account
            if (IS_ADMIN) pnlTable.Enabled = false;
        }