private void UpdateStore(string id) { using (var db = new RentBikeEntities()) { int storeid = Convert.ToInt32(id); var st = (from s in db.Stores where s.ID == storeid select s).FirstOrDefault(); 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.Trim()); //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.Trim()); st.ACTIVE = rdbActive.Checked; st.NOTE = txtNote.Text.Trim(); st.SEARCH_TEXT = string.Format("{0} {1} {2}", st.NAME, st.ADDRESS, st.PHONE); db.SaveChanges(); var storefee = db.StoreFees.FirstOrDefault(s =>s.STORE_ID == storeid); storefee.FEE_PERCENT = 0; db.SaveChanges(); } WriteLog(Constants.ACTION_UPDATE_STORE, 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 void btnSave_Click(object sender, EventArgs e) { // SAVE INOUT int periodId = Convert.ToInt32(Request.QueryString["ID"]); using (var db = new RentBikeEntities()) { if (!string.IsNullOrEmpty(txtIncome.Text)) { var pp = db.PayPeriods.FirstOrDefault(s => s.ID == periodId); pp.ACTUAL_PAY = Convert.ToDecimal(txtIncome.Text) + pp.ACTUAL_PAY; db.SaveChanges(); var contract = db.Contracts.FirstOrDefault(c => c.ID == pp.CONTRACT_ID && c.CONTRACT_STATUS == true); List <PayPeriod> payList = db.PayPeriods.Where(c => c.CONTRACT_ID == pp.CONTRACT_ID).ToList(); decimal totalActualPay = payList.Select(c => c.ACTUAL_PAY).DefaultIfEmpty(0).Sum(); decimal totalPlanPay = payList.Select(c => c.AMOUNT_PER_PERIOD).DefaultIfEmpty(0).Sum(); if (totalActualPay > totalPlanPay) { CommonList.CreatePayPeriod(db, contract.ID, payList.LastOrDefault(), false); } InOut io = new InOut(); io.IN_AMOUNT = Convert.ToDecimal(txtIncome.Text); io.OUT_AMOUNT = 0; io.CONTRACT_ID = pp.CONTRACT_ID; io.PERIOD_ID = pp.ID; io.RENT_TYPE_ID = contract.RENT_TYPE_ID; io.INOUT_TYPE_ID = Convert.ToInt32(ddInOutType.SelectedValue); io.PERIOD_DATE = pp.PAY_DATE; io.MORE_INFO = txtMoreInfo.Text.Trim(); io.STORE_ID = Convert.ToInt32(Session["store_id"]); io.SEARCH_TEXT = string.Format("{0} ", io.MORE_INFO); io.INOUT_DATE = DateTime.Now; io.CREATED_BY = Session["username"].ToString(); io.CREATED_DATE = DateTime.Now; io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io); db.SaveChanges(); var customer = db.Customers.FirstOrDefault(c => c.ID == contract.CUSTOMER_ID); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện thu phí kỳ hạn ngày {2} của hợp đồng {3} số tiền {4} vào lúc {5}", Convert.ToString(Session["username"]), STORE_NAME, pp.PAY_DATE.ToString("dd/MM/yyyy"), customer.NAME, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_INOUT, message, false); Response.Redirect("FormContractUpdate.aspx?ID=" + pp.CONTRACT_ID, false); } } }
protected void btnSave_Click(object sender, EventArgs e) { // SAVE INOUT int periodId = Convert.ToInt32(Request.QueryString["ID"]); using (var db = new RentBikeEntities()) { if (!string.IsNullOrEmpty(txtIncome.Text)) { var pp = db.PayPeriods.FirstOrDefault(s => s.ID == periodId); pp.ACTUAL_PAY = Convert.ToDecimal(txtIncome.Text) + pp.ACTUAL_PAY; db.SaveChanges(); var contract = db.Contracts.FirstOrDefault(c => c.ID == pp.CONTRACT_ID && c.CONTRACT_STATUS == true); List<PayPeriod> payList = db.PayPeriods.Where(c => c.CONTRACT_ID == pp.CONTRACT_ID).ToList(); decimal totalActualPay = payList.Select(c => c.ACTUAL_PAY).DefaultIfEmpty(0).Sum(); decimal totalPlanPay = payList.Select(c => c.AMOUNT_PER_PERIOD).DefaultIfEmpty(0).Sum(); if (totalActualPay > totalPlanPay) { CommonList.CreatePayPeriod(db, contract.ID, payList.LastOrDefault(), false); } InOut io = new InOut(); io.IN_AMOUNT = Convert.ToDecimal(txtIncome.Text); io.OUT_AMOUNT = 0; io.CONTRACT_ID = pp.CONTRACT_ID; io.PERIOD_ID = pp.ID; io.RENT_TYPE_ID = contract.RENT_TYPE_ID; io.INOUT_TYPE_ID = Convert.ToInt32(ddInOutType.SelectedValue); io.PERIOD_DATE = pp.PAY_DATE; io.MORE_INFO = txtMoreInfo.Text.Trim(); io.STORE_ID = Convert.ToInt32(Session["store_id"]); io.SEARCH_TEXT = string.Format("{0} ", io.MORE_INFO); io.INOUT_DATE = DateTime.Now; io.CREATED_BY = Session["username"].ToString(); io.CREATED_DATE = DateTime.Now; io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io); db.SaveChanges(); var customer = db.Customers.FirstOrDefault(c => c.ID == contract.CUSTOMER_ID); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện thu phí kỳ hạn ngày {2} của hợp đồng {3} số tiền {4} vào lúc {5}", Convert.ToString(Session["username"]), STORE_NAME, pp.PAY_DATE.ToString("dd/MM/yyyy"), customer.NAME, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_INOUT, message, false); Response.Redirect("FormContractUpdate.aspx?ID=" + pp.CONTRACT_ID, false); } } }
protected void rptAccount_ItemCommand(object sender, RepeaterCommandEventArgs e) { try { if (e.CommandName == "Delete" && !string.IsNullOrEmpty(e.CommandArgument.ToString())) { var id = Convert.ToInt32(e.CommandArgument.ToString()); using (var db = new RentBikeEntities()) { var account = db.Accounts.FirstOrDefault(c => c.ID == id); if (account != null) { db.Accounts.Remove(account); db.SaveChanges(); } LoadData(); ViewState["DeleteSuccess"] = "Xóa tài khoản thành công"; } } } catch (Exception ex) { ViewState["DeleteFail"] = ex.Message; } }
protected void btnSave_Click(object sender, EventArgs e) { string result = ValidateFields(); if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } using (var db = new RentBikeEntities()) { int inOutId = Helper.parseInt(Request.QueryString["id"]); if (inOutId != 0) { var io = db.InOuts.FirstOrDefault(c => c.ID == inOutId); // SAVE INOUT io.IN_AMOUNT = Convert.ToDecimal(txtIncome.Text); io.MORE_INFO = txtMoreInfo.Text.Trim(); io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; db.SaveChanges(); } int periodId = Helper.parseInt(Request.QueryString["pid"]); if (periodId != 0) { // SAVE PERIOD decimal totalInAmountOfPeriod = db.InOuts.Where(c => c.PERIOD_ID == periodId).Select(c => c.IN_AMOUNT).DefaultIfEmpty(0).Sum(); var pp = db.PayPeriods.FirstOrDefault(c => c.ID == periodId); pp.ACTUAL_PAY = totalInAmountOfPeriod; db.SaveChanges(); var contract = db.Contracts.FirstOrDefault(c => c.ID == pp.CONTRACT_ID); var customer = db.Customers.FirstOrDefault(c => c.ID == contract.CUSTOMER_ID); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa thu phí kỳ hạn ngày {2} của hợp đồng {3} số tiền {4} vào lúc {5}", Convert.ToString(Session["username"]), STORE_NAME, pp.PAY_DATE.ToString("dd/MM/yyyy"), customer.NAME, Helper.FormatedAsCurrency(Convert.ToDecimal(txtIncome.Text.Trim())), DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_UPDATE_INOUT, message, false); Response.Redirect(string.Format("FormInOutUpdate.aspx?ID={0}", periodId), false); } Response.Redirect("FormDailyIncomeOutcome.aspx", false); } }
protected void btnSave_Click(object sender, EventArgs e) { string result = ValidateFields(); if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } using (var db = new RentBikeEntities()) { int inOutId = Helper.parseInt(Request.QueryString["id"]); if (inOutId != 0) { var io = db.InOuts.FirstOrDefault(c =>c.ID == inOutId); // SAVE INOUT io.IN_AMOUNT = Convert.ToDecimal(txtIncome.Text); io.MORE_INFO = txtMoreInfo.Text.Trim(); io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; db.SaveChanges(); } int periodId = Helper.parseInt(Request.QueryString["pid"]); if (periodId != 0) { // SAVE PERIOD decimal totalInAmountOfPeriod = db.InOuts.Where(c =>c.PERIOD_ID == periodId).Select(c =>c.IN_AMOUNT).DefaultIfEmpty(0).Sum(); var pp = db.PayPeriods.FirstOrDefault(c =>c.ID == periodId); pp.ACTUAL_PAY = totalInAmountOfPeriod; db.SaveChanges(); var contract = db.Contracts.FirstOrDefault(c => c.ID == pp.CONTRACT_ID); var customer = db.Customers.FirstOrDefault(c =>c.ID == contract.CUSTOMER_ID); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa thu phí kỳ hạn ngày {2} của hợp đồng {3} số tiền {4} vào lúc {5}", Convert.ToString(Session["username"]), STORE_NAME, pp.PAY_DATE.ToString("dd/MM/yyyy"), customer.NAME, Helper.FormatedAsCurrency(Convert.ToDecimal(txtIncome.Text.Trim())), DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_UPDATE_INOUT, message, false); Response.Redirect(string.Format("FormInOutUpdate.aspx?ID={0}", periodId), false); } Response.Redirect("FormDailyIncomeOutcome.aspx", false); } }
private void WriteLog(string action, bool isCrashed) { Log lg = new Log(); lg.ACCOUNT = Session["username"].ToString(); lg.STORE = STORE_NAME; lg.LOG_ACTION = action; lg.LOG_DATE = DateTime.Now; lg.IS_CRASH = isCrashed; lg.LOG_MSG = string.Format("Tài khoản {0} cửa hàng {1} thực hiện {2} vào lúc {3}", lg.ACCOUNT, STORE_NAME, lg.LOG_ACTION, lg.LOG_DATE); lg.SEARCH_TEXT = lg.LOG_MSG; using (var db = new RentBikeEntities()) { db.Logs.Add(lg); db.SaveChanges(); } }
protected void btnLowRecoverability_Click(object sender, EventArgs e) { try { string id = Request.QueryString["ID"]; using (var db = new RentBikeEntities()) { int contractId = Helper.parseInt(id); var contract = db.Contracts.FirstOrDefault(c => c.CONTRACT_STATUS == true && c.ID == contractId); if (contract != null) { Customer customer = db.Customers.FirstOrDefault(c => c.ID == contract.CUSTOMER_ID); if (customer != null) { string message = string.Empty; if (btnLowRecoverability.CommandArgument == "LowRecoverability") { customer.IS_LOW_RECOVERABILITY = true; lblMessage.Text = string.Format(Constants.LOW_RECOVERABILITY, customer.NAME); btnLowRecoverability.Text = "Bỏ khả năng thu hồi thấp"; btnLowRecoverability.CommandArgument = "RevertLowRecoverability"; message = string.Format("Tài khoản {0} cửa hàng {1} đã đánh dấu khả năng thu hồi thấp cho khách hàng {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, customer.NAME, DateTime.Now); } else { customer.IS_LOW_RECOVERABILITY = false; lblMessage.Text = string.Format(Constants.REVERT_LOW_RECOVERABILITY, customer.NAME); btnLowRecoverability.Text = "Khả năng thu hồi thấp"; btnLowRecoverability.CommandArgument = "LowRecoverability"; message = string.Format("Tài khoản {0} cửa hàng {1} đã bỏ khả năng thu hồi thấp cho khách hàng {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, customer.NAME, DateTime.Now); } db.SaveChanges(); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_UPDATE_CONTRACT, message, false); } } } } catch (Exception ex) { lblMessage.Text = ex.Message; lblMessage.CssClass = "text-center text-danger"; Logger.Log(ex.Message); } }
protected void btnSave_Click(object sender, EventArgs e) { string result = ValidateFields(); if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } using (TransactionScope ts = new TransactionScope()) { using (var db = new RentBikeEntities()) { string message = string.Empty; InOut io = db.InOuts.FirstOrDefault(c =>c.ID == inOutId); if (io == null) { io = new InOut(); io.INOUT_TYPE_ID = Convert.ToInt32(ddlInOutFee.SelectedValue); io.MORE_INFO = txtMoreInfo.Text.Trim(); io.CONTRACT_ID = -1; io.PERIOD_ID = -1; io.RENT_TYPE_ID = -1; io.PERIOD_DATE = new DateTime(1, 1, 1); if (ddlStore.Enabled == false) { io.STORE_ID = STORE_ID; } else { io.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); } var item = db.InOutTypes.FirstOrDefault(s =>s.ID == io.INOUT_TYPE_ID); if (item.IS_INCOME) { io.IN_AMOUNT = Convert.ToDecimal(txtFeeAmount.Text.Trim()); io.OUT_AMOUNT = 0; message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện {2} ({3}) vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, ddlInOutFee.SelectedItem.Text, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); } else { io.IN_AMOUNT = 0; io.OUT_AMOUNT = Convert.ToDecimal(txtFeeAmount.Text.Trim()); message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện {2} ({3}) vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, ddlInOutFee.SelectedItem.Text, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); } io.INOUT_DATE = Convert.ToDateTime(txtFeeDate.Text); io.SEARCH_TEXT = string.Format("{0} {1} {2}", io.INOUT_DATE, io.MORE_INFO, item.NAME); io.CREATED_BY = Session["username"].ToString(); io.CREATED_DATE = DateTime.Now; io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_INOUT, message, false); } else { io.INOUT_TYPE_ID = Convert.ToInt32(ddlInOutFee.SelectedValue); io.INOUT_DATE = Convert.ToDateTime(txtFeeDate.Text); io.MORE_INFO = txtMoreInfo.Text.Trim(); if (ddlStore.Enabled == false) { io.STORE_ID = Convert.ToInt32(Session["store_id"]); } else { io.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); } var item = db.InOutTypes.FirstOrDefault(s =>s.ID == io.INOUT_TYPE_ID); if (item.IS_INCOME) { io.IN_AMOUNT = Convert.ToDecimal(txtFeeAmount.Text.Trim()); io.OUT_AMOUNT = 0; message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa {2} ({3}) vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, ddlInOutFee.SelectedItem.Text, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); } else { io.IN_AMOUNT = 0; io.OUT_AMOUNT = Convert.ToDecimal(txtFeeAmount.Text.Trim()); message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa {2} ({3}) vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, ddlInOutFee.SelectedItem.Text, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); } io.SEARCH_TEXT = string.Format("{0} {1} {2}", io.INOUT_DATE, io.MORE_INFO, item.NAME); io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_INOUT, message, false); } db.SaveChanges(); } ts.Complete(); } Response.Redirect("FormDailyIncomeOutcome.aspx", 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(); } }
private void WriteLog(string action, bool isCrashed) { Log lg = new Log(); lg.ACCOUNT = Session["username"].ToString(); lg.STORE = STORE_NAME; lg.LOG_ACTION = action; lg.LOG_DATE = DateTime.Now; lg.IS_CRASH = isCrashed; lg.LOG_MSG = string.Format("Tài khoản {0} cửa hàng {1} thực hiện {2} vào lúc {3}", lg.ACCOUNT, STORE_NAME, lg.LOG_ACTION, lg.LOG_DATE); lg.SEARCH_TEXT = lg.LOG_MSG; using (var db = new RentBikeEntities()) { db.Logs.Add(lg); db.SaveChanges(); } }
private void UpdateStore(string id) { using (var db = new RentBikeEntities()) { int storeid = Convert.ToInt32(id); var st = (from s in db.Stores where s.ID == storeid select s).FirstOrDefault(); 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.Trim()); //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.Trim()); st.ACTIVE = rdbActive.Checked; st.NOTE = txtNote.Text.Trim(); st.SEARCH_TEXT = string.Format("{0} {1} {2}", st.NAME, st.ADDRESS, st.PHONE); db.SaveChanges(); var storefee = db.StoreFees.FirstOrDefault(s =>s.STORE_ID == storeid); storefee.FEE_PERCENT = 0; db.SaveChanges(); } WriteLog(Constants.ACTION_UPDATE_STORE, false); }
protected void btnSave_Click(object sender, EventArgs e) { string result = ValidateFields(); if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } using (TransactionScope ts = new TransactionScope()) { using (var db = new RentBikeEntities()) { string message = string.Empty; InOut io = db.InOuts.FirstOrDefault(c => c.ID == inOutId); if (io == null) { io = new InOut(); io.INOUT_TYPE_ID = Convert.ToInt32(ddlInOutFee.SelectedValue); io.MORE_INFO = txtMoreInfo.Text.Trim(); io.CONTRACT_ID = -1; io.PERIOD_ID = -1; io.RENT_TYPE_ID = -1; io.PERIOD_DATE = new DateTime(1, 1, 1); if (ddlStore.Enabled == false) { io.STORE_ID = STORE_ID; } else { io.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); } var item = db.InOutTypes.FirstOrDefault(s => s.ID == io.INOUT_TYPE_ID); if (item.IS_INCOME) { io.IN_AMOUNT = Convert.ToDecimal(txtFeeAmount.Text.Trim()); io.OUT_AMOUNT = 0; message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện {2} ({3}) vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, ddlInOutFee.SelectedItem.Text, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); } else { io.IN_AMOUNT = 0; io.OUT_AMOUNT = Convert.ToDecimal(txtFeeAmount.Text.Trim()); message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện {2} ({3}) vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, ddlInOutFee.SelectedItem.Text, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); } io.INOUT_DATE = Convert.ToDateTime(txtFeeDate.Text); io.SEARCH_TEXT = string.Format("{0} {1} {2}", io.INOUT_DATE, io.MORE_INFO, item.NAME); io.CREATED_BY = Session["username"].ToString(); io.CREATED_DATE = DateTime.Now; io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_INOUT, message, false); } else { io.INOUT_TYPE_ID = Convert.ToInt32(ddlInOutFee.SelectedValue); io.INOUT_DATE = Convert.ToDateTime(txtFeeDate.Text); io.MORE_INFO = txtMoreInfo.Text.Trim(); if (ddlStore.Enabled == false) { io.STORE_ID = Convert.ToInt32(Session["store_id"]); } else { io.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); } var item = db.InOutTypes.FirstOrDefault(s => s.ID == io.INOUT_TYPE_ID); if (item.IS_INCOME) { io.IN_AMOUNT = Convert.ToDecimal(txtFeeAmount.Text.Trim()); io.OUT_AMOUNT = 0; message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa {2} ({3}) vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, ddlInOutFee.SelectedItem.Text, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); } else { io.IN_AMOUNT = 0; io.OUT_AMOUNT = Convert.ToDecimal(txtFeeAmount.Text.Trim()); message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa {2} ({3}) vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, ddlInOutFee.SelectedItem.Text, Helper.FormatedAsCurrency(io.IN_AMOUNT), DateTime.Now); } io.SEARCH_TEXT = string.Format("{0} {1} {2}", io.INOUT_DATE, io.MORE_INFO, item.NAME); io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_INOUT, message, false); } db.SaveChanges(); } ts.Complete(); } Response.Redirect("FormDailyIncomeOutcome.aspx", false); }
protected void btnSave_Click(object sender, EventArgs e) { using (TransactionScope trans = new TransactionScope()) { int contractId = Convert.ToInt32(Request.QueryString["ID"]); using (var db = new RentBikeEntities()) { var con = db.CONTRACT_FULL_VW.FirstOrDefault(c => c.ID == contractId && c.ACTIVE == true); string closedContractName = string.Empty; switch (con.RENT_TYPE_ID) { case 1: closedContractName = "Thanh lý thuê xe"; break; case 2: closedContractName = "Thanh lý thuê thiết bị"; break; default: closedContractName = "Thanh lý thuê khác"; break; } // INOUT --> IN amount InOut io1 = new InOut(); io1.IN_AMOUNT = con.CONTRACT_AMOUNT; io1.OUT_AMOUNT = 0; io1.CONTRACT_ID = con.ID; io1.PERIOD_ID = -1; io1.PERIOD_DATE = new DateTime(1, 1, 1); io1.RENT_TYPE_ID = con.RENT_TYPE_ID; var item = db.InOutTypes.FirstOrDefault(s =>s.NAME == "Thanh lý"); io1.INOUT_TYPE_ID = item.ID; io1.MORE_INFO = txtMoreInfo.Text.Trim(); io1.STORE_ID = con.STORE_ID; io1.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4}", con.CONTRACT_NO, con.CUSTOMER_NAME, con.STORE_NAME, closedContractName, txtMoreInfo.Text.Trim()); io1.INOUT_DATE = DateTime.Now; io1.CREATED_BY = Session["username"].ToString(); io1.CREATED_DATE = DateTime.Now; io1.UPDATED_BY = Session["username"].ToString(); io1.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io1); // IN --> Rent Fee if (!string.IsNullOrEmpty(txtRealIncome.Text)) { decimal realInAmount = Convert.ToDecimal(txtRealIncome.Text.Replace(",", string.Empty)); if (realInAmount > 0) { InOut io2 = new InOut(); string feeName = string.Empty; switch (con.RENT_TYPE_NAME) { case "Cho thuê xe": feeName = "Phí thuê xe"; break; case "Cho thuê thiết bị văn phòng": feeName = "Phí thuê thiết bị"; break; case "Cho thuê mặt hàng khác": feeName = "Phí khác"; break; } item = db.InOutTypes.First(s =>s.NAME == feeName); io2.INOUT_TYPE_ID = item.ID; io2.RENT_TYPE_ID = con.RENT_TYPE_ID; io2.CONTRACT_ID = con.ID; io2.IN_AMOUNT = realInAmount; io2.OUT_AMOUNT = 0; io2.MORE_INFO = txtMoreInfo.Text; io2.PERIOD_DATE = DateTime.Now; io2.STORE_ID = con.STORE_ID; io2.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4}", con.CONTRACT_NO, con.CUSTOMER_NAME, con.STORE_NAME, feeName, txtMoreInfo.Text.Trim()); io2.INOUT_DATE = DateTime.Now; io2.CREATED_BY = Session["username"].ToString(); io2.CREATED_DATE = DateTime.Now; io2.UPDATED_BY = Session["username"].ToString(); io2.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io2); } } //Out --> Return redundant fee if the client comes before deadline date. if (!string.IsNullOrEmpty(txtReduceAmount.Text)) { decimal reduceAmount = Convert.ToDecimal(txtReduceAmount.Text.Replace(",", string.Empty)); if (reduceAmount > 0) { InOut io3 = new InOut(); item = db.InOutTypes.FirstOrDefault(s =>s.NAME == "Trả lại phí thừa"); io3.INOUT_TYPE_ID = item.ID; io3.RENT_TYPE_ID = con.RENT_TYPE_ID; io3.CONTRACT_ID = con.ID; io3.IN_AMOUNT = 0; io3.OUT_AMOUNT = reduceAmount; io3.RENT_TYPE_ID = con.RENT_TYPE_ID; io3.MORE_INFO = txtMoreInfo.Text.Trim(); io3.PERIOD_DATE = DateTime.Now; io3.STORE_ID = con.STORE_ID; io3.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4}", con.CONTRACT_NO, con.CUSTOMER_NAME, con.STORE_NAME, "Trả lại phí thừa", txtMoreInfo.Text.Trim()); io3.INOUT_DATE = DateTime.Now; io3.CREATED_BY = Session["username"].ToString(); io3.CREATED_DATE = DateTime.Now; io3.UPDATED_BY = Session["username"].ToString(); io3.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io3); } } db.SaveChanges(); // Writelog string rentTypeNameClosed = string.Empty; switch (con.RENT_TYPE_ID) { case 1: rentTypeNameClosed = "thuê xe"; break; case 2: rentTypeNameClosed = "thuê thiết bị"; break; default: rentTypeNameClosed = "thuê khác"; break; } var customer = db.Customers.FirstOrDefault(c => c.ID == con.CUSTOMER_ID); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện thanh lý hợp đồng {2} của khách hàng {3} vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, rentTypeNameClosed, customer.NAME, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CLOSE_CONTRACT, message, false); } using (var db = new RentBikeEntities()) { // Update status contract var con = db.Contracts.FirstOrDefault(c =>c.ID == contractId); con.CONTRACT_STATUS = false; con.CLOSE_CONTRACT_DATE = DateTime.Now; // Insert History row ContractHistory ch = new ContractHistory(); ch.CONTRACT_ID = con.ID; ch.CONTRACT_NO = con.CONTRACT_NO; ch.CUSTOMER_ID = con.CUSTOMER_ID; ch.CONTRACT_AMOUNT = con.CONTRACT_AMOUNT; ch.DETAIL = con.DETAIL; ch.RENT_DATE = con.RENT_DATE; ch.END_DATE = con.END_DATE; ch.FEE_PER_DAY = con.FEE_PER_DAY; ch.ITEM_LICENSE_NO = con.ITEM_LICENSE_NO; ch.ITEM_TYPE = con.ITEM_TYPE; ch.NOTE = con.NOTE; ch.REFERENCE_NAME = con.REFERENCE_NAME; ch.RENT_TYPE_ID = con.RENT_TYPE_ID; ch.SERIAL_1 = con.SERIAL_1; ch.SERIAL_2 = con.SERIAL_2; ch.STORE_ID = con.STORE_ID; ch.SEARCH_TEXT = con.SEARCH_TEXT; ch.PAY_FEE_MESSAGE = string.Empty; ch.CLOSE_CONTRACT_DATE = DateTime.Now; ch.REFERENCE_PHONE = con.REFERENCE_PHONE; ch.SCHOOL_NAME = con.SCHOOL_NAME; ch.CLASS_NAME = con.CLASS_NAME; ch.IMPLEMENTER = con.IMPLEMENTER; ch.BACK_TO_DOCUMENTS = con.BACK_TO_DOCUMENTS; ch.PHOTO_1 = con.PHOTO_1; ch.THUMBNAIL_PHOTO_1 = con.THUMBNAIL_PHOTO_1; ch.PHOTO_2 = con.PHOTO_2; ch.THUMBNAIL_PHOTO_2 = con.THUMBNAIL_PHOTO_2; ch.PHOTO_3 = con.PHOTO_3; ch.THUMBNAIL_PHOTO_3 = con.THUMBNAIL_PHOTO_3; ch.PHOTO_4 = con.PHOTO_4; ch.THUMBNAIL_PHOTO_4 = con.THUMBNAIL_PHOTO_4; ch.PHOTO_5 = con.PHOTO_5; ch.THUMBNAIL_PHOTO_5 = con.THUMBNAIL_PHOTO_5; ch.CREATED_BY = Session["username"].ToString(); ch.CREATED_DATE = DateTime.Now; ch.UPDATED_BY = Session["username"].ToString(); ch.UPDATED_DATE = DateTime.Now; if (CommonList.IsBadContract(db, contractId)) { con.IS_BAD_CONTRACT = ch.IS_BAD_CONTRACT = true; } db.ContractHistories.Add(ch); db.SaveChanges(); trans.Complete(); } } Response.Redirect("FormContractManagement.aspx"); }
protected void btnSave_Click(object sender, EventArgs e) { string result = ValidateFields(); if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } string id = Request.QueryString["ID"]; if (string.IsNullOrEmpty(id)) { using (var db = new RentBikeEntities()) { int accid = Convert.ToInt32(id); var acc = (from s in db.Accounts where s.ACC == txtAccount.Text.Trim() select s).FirstOrDefault(); if (acc != null) { lblMessage.Text = "Tài khoản này đã tồn tại trên hệ thống."; return; } // New account Account item = new Account(); item.ACC = txtAccount.Text.Trim(); item.PASSWORD = Helper.EncryptPassword(txtNewPassword.Text.Trim()); txtOldPassword.Enabled = false; txtConfirmPassword.Enabled = false; item.PERMISSION_ID = Convert.ToInt32(ddlPermission.SelectedValue); item.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); item.NAME = txtName.Text.Trim(); item.CITY_ID = Convert.ToInt32(ddlCity.SelectedValue); item.PHONE = txtPhone.Text.Trim(); item.REGISTER_DATE = Convert.ToDateTime(txtRegisterDate.Text); item.ACTIVE = rdbActive.Checked; item.NOTE = txtNote.Text.Trim(); item.SEARCH_TEXT = string.Format("{0} {1}", item.ACC, item.NAME); db.Accounts.Add(item); db.SaveChanges(); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện tạo mới tài khoản {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, item.ACC, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_ACCOUNT, false); } } else { //// Edit using (var db = new RentBikeEntities()) { int accid = Convert.ToInt32(id); var item = (from s in db.Accounts where s.ID == accid select s).FirstOrDefault(); bool bRefresh = false; item.PASSWORD = Helper.EncryptPassword(txtNewPassword.Text.Trim()); if (USER_NAME == item.ACC) { bRefresh = true; } item.ACC = txtAccount.Text.Trim(); item.PERMISSION_ID = Convert.ToInt32(ddlPermission.SelectedValue); item.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); item.NAME = txtName.Text.Trim(); item.CITY_ID = Convert.ToInt32(ddlCity.SelectedValue); item.PHONE = txtPhone.Text.Trim(); item.REGISTER_DATE = Convert.ToDateTime(txtRegisterDate.Text); item.ACTIVE = rdbActive.Checked; item.NOTE = txtNote.Text.Trim(); db.SaveChanges(); if (bRefresh) { Session["username"] = item.ACC; Session["name"] = item.NAME; Session["permission"] = item.PERMISSION_ID; Session["city_id"] = item.CITY_ID; Session["store_id"] = item.STORE_ID; } string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa tài khoản {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, item.ACC, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_UPDATE_ACCOUNT, false); } } Response.Redirect("FormAccountManagement.aspx"); }
protected void btnSave_Click(object sender, EventArgs e) { string result = ValidateFields(); if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } string id = Request.QueryString["ID"]; if (string.IsNullOrEmpty(id)) { using (var db = new RentBikeEntities()) { int accid = Convert.ToInt32(id); var acc = (from s in db.Accounts where s.ACC == txtAccount.Text.Trim() select s).FirstOrDefault(); if (acc != null) { lblMessage.Text = "Tài khoản này đã tồn tại trên hệ thống."; return; } // New account Account item = new Account(); item.ACC = txtAccount.Text.Trim(); item.PASSWORD = Helper.EncryptPassword(txtNewPassword.Text.Trim()); txtOldPassword.Enabled = false; txtConfirmPassword.Enabled = false; item.PERMISSION_ID = Convert.ToInt32(ddlPermission.SelectedValue); item.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); item.NAME = txtName.Text.Trim(); item.CITY_ID = Convert.ToInt32(ddlCity.SelectedValue); item.PHONE = txtPhone.Text.Trim(); item.REGISTER_DATE = Convert.ToDateTime(txtRegisterDate.Text); item.ACTIVE = rdbActive.Checked; item.NOTE = txtNote.Text.Trim(); item.SEARCH_TEXT = string.Format("{0} {1}", item.ACC, item.NAME); db.Accounts.Add(item); db.SaveChanges(); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện tạo mới tài khoản {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, item.ACC, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_ACCOUNT, false); } } else { //// Edit using (var db = new RentBikeEntities()) { int accid = Convert.ToInt32(id); var item = (from s in db.Accounts where s.ID == accid select s).FirstOrDefault(); bool bRefresh = false; item.PASSWORD = Helper.EncryptPassword(txtNewPassword.Text.Trim()); if (USER_NAME == item.ACC) bRefresh = true; item.ACC = txtAccount.Text.Trim(); item.PERMISSION_ID = Convert.ToInt32(ddlPermission.SelectedValue); item.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); item.NAME = txtName.Text.Trim(); item.CITY_ID = Convert.ToInt32(ddlCity.SelectedValue); item.PHONE = txtPhone.Text.Trim(); item.REGISTER_DATE = Convert.ToDateTime(txtRegisterDate.Text); item.ACTIVE = rdbActive.Checked; item.NOTE = txtNote.Text.Trim(); db.SaveChanges(); if (bRefresh) { Session["username"] = item.ACC; Session["name"] = item.NAME; Session["permission"] = item.PERMISSION_ID; Session["city_id"] = item.CITY_ID; Session["store_id"] = item.STORE_ID; } string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa tài khoản {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, item.ACC, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_UPDATE_ACCOUNT, false); } } Response.Redirect("FormAccountManagement.aspx"); }
protected void btnSave_Click(object sender, EventArgs e) { try { using (TransactionScope ts = new TransactionScope()) { string id = Request.QueryString["ID"]; string copy = Request.QueryString["copy"]; string result = ValidateFields(); int cusid = 0; if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } if (string.IsNullOrEmpty(id) || (!string.IsNullOrEmpty(copy) && copy == "1")) // NEW { Contract contract = CommonList.GetContractByLicenseNo(txtLicenseNumber.Text.Trim()); if (contract != null) { // Initialize StringWriter instance. StringWriter stringWriter = new StringWriter(); // Put HtmlTextWriter in using block because it needs to call Dispose. using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter)) { writer.Write("Số CMTND/GPLX này hiện tại đã đăng ký hợp đồng "); writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Format("FormContractUpdate.aspx?ID={0}&sID={1}", contract.ID, contract.STORE_ID)); writer.RenderBeginTag(HtmlTextWriterTag.A); // Start of A writer.Write(contract.CONTRACT_NO); writer.RenderEndTag(); //End of A lblMessage.Text = stringWriter.ToString(); return; } } Customer cusItem = null; using (var db = new RentBikeEntities()) { bool IsNewCust = false; cusItem = db.Customers.FirstOrDefault(c => c.LICENSE_NO == txtLicenseNumber.Text.Trim() && c.NAME == txtCustomerName.Text.Trim()); if (cusItem == null) { IsNewCust = true; cusItem = new Customer(); } cusItem.NAME = txtCustomerName.Text.Trim(); cusItem.BIRTH_DAY = DateTime.ParseExact(txtBirthDay.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); cusItem.LICENSE_NO = txtLicenseNumber.Text.Trim(); cusItem.LICENSE_RANGE_DATE = DateTime.ParseExact(txtRangeDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); cusItem.LICENSE_RANGE_PLACE = txtPlaceDate.Text.Trim(); cusItem.PHONE = txtPhone.Text.Trim(); cusItem.PERMANENT_RESIDENCE = txtPermanentResidence.Text.Trim(); cusItem.CURRENT_RESIDENCE = txtCurrentResidence.Text.Trim(); if (IsNewCust) { db.Customers.Add(cusItem); } db.SaveChanges(); cusid = cusItem.ID; } // New Contract Contract item = new Contract(); item.RENT_TYPE_ID = Convert.ToInt32(ddlRentType.SelectedValue); item.FEE_PER_DAY = Math.Round(Convert.ToDecimal(txtFeePerDay.Text.Replace(",", string.Empty))); if (!string.IsNullOrEmpty(txtRentDate.Text)) { item.RENT_DATE = DateTime.ParseExact(txtRentDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); } else item.RENT_DATE = DateTime.Now; if (!string.IsNullOrEmpty(txtEndDate.Text)) { item.END_DATE = DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); } else item.END_DATE = item.RENT_DATE.AddDays(29); item.EXTEND_END_DATE = item.END_DATE; item.CLOSE_CONTRACT_DATE = new DateTime(1, 1, 1); item.PAY_FEE_MESSAGE = string.Empty; item.NOTE = txtNote.Text; item.REFERENCE_ID = -1; item.REFERENCE_NAME = txtReferencePerson.Text.Trim(); item.ITEM_TYPE = txtItemName.Text.Trim(); item.ITEM_LICENSE_NO = txtItemLicenseNo.Text.Trim(); item.SERIAL_1 = txtSerial1.Text.Trim(); item.SERIAL_2 = txtSerial2.Text.Trim(); item.REFERENCE_PHONE = txtReferencePhone.Text.Trim(); item.SCHOOL_NAME = txtSchool.Text.Trim(); item.CLASS_NAME = txtClass.Text.Trim(); item.IMPLEMENTER = txtImplementer.Text.Trim(); item.BACK_TO_DOCUMENTS = txtBackDocument.Text.Trim(); item.DETAIL = txtItemDetail.Text.Trim(); item.CUSTOMER_ID = cusid; item.CONTRACT_STATUS = true; item.CONTRACT_AMOUNT = Convert.ToDecimal(txtAmount.Text.Replace(",", string.Empty)); item.CREATED_BY = Session["username"].ToString(); item.CREATED_DATE = DateTime.Now; item.UPDATED_BY = Session["username"].ToString(); item.UPDATED_DATE = DateTime.Now; //item.CONTRACT_NO = txtContractNo.Text.Trim(); if (ddlStore.Enabled == true) item.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); else item.STORE_ID = Convert.ToInt32(Session["store_id"]); item.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8}", txtCustomerName.Text.Trim(), txtBirthDay.Text.Trim(), txtLicenseNumber.Text.Trim(), txtRangeDate.Text.Trim(), txtPermanentResidence.Text.Trim(), txtCurrentResidence.Text.Trim(), txtPhone.Text.Trim(), item.CONTRACT_NO, item.RENT_DATE.ToString("dd/MM/yyyy")); SavePhoto(item); using (var db = new RentBikeEntities()) { db.Contracts.Add(item); db.SaveChanges(); DateTime periodTime = DateTime.Today; if (!string.IsNullOrEmpty(txtRentDate.Text)) { periodTime = DateTime.ParseExact(txtRentDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); } PayPeriod newPeriod = new PayPeriod(); newPeriod.PAY_DATE = periodTime; CommonList.CreatePayPeriod(db, item.ID, newPeriod, true); } InOut io = new InOut(); io.CONTRACT_ID = item.ID; io.IN_AMOUNT = 0; io.OUT_AMOUNT = Convert.ToDecimal(txtAmount.Text); io.RENT_TYPE_ID = Convert.ToInt32(ddlRentType.SelectedValue); io.PERIOD_DATE = DateTime.Now; io.MORE_INFO = string.Format("Cho khách {0} thuê: {1} ngày {2} trị giá {3}", txtCustomerName.Text.Trim(), txtItemName.Text.Trim(), DateTime.Now.ToString("dd/MM/yyyy"), txtAmount.Text.Trim()); if (ddlStore.Enabled == true) io.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); else io.STORE_ID = Convert.ToInt32(Session["store_id"]); io.SEARCH_TEXT = string.Format("{0} ", io.MORE_INFO); io.INOUT_DATE = DateTime.Now; io.CREATED_BY = Session["username"].ToString(); io.CREATED_DATE = DateTime.Now; io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; io.INOUT_TYPE_ID = CommonList.GetInoutTypeFromRentType(Helper.parseInt(ddlRentType.SelectedValue)); using (var rbdb = new RentBikeEntities()) { rbdb.InOuts.Add(io); rbdb.SaveChanges(); CommonList.AutoExtendPeriod(rbdb, item.ID); } string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện làm hợp đồng cho khách hàng {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, cusItem.NAME, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_CONTRACT, message, false); ts.Complete(); } else // EDIT { result = ValidatePermission(); if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } int contractId = Helper.parseInt(id); bool bUpdateInOutAndPeriod = false; using (var db = new RentBikeEntities()) { var item = db.Contracts.FirstOrDefault(itm => itm.ID == contractId); int rentTypeId = Helper.parseInt(ddlRentType.SelectedValue); decimal contractAmount = Convert.ToDecimal(txtAmount.Text); decimal feePerDay = Convert.ToDecimal(txtFeePerDay.Text); if (contractAmount != item.CONTRACT_AMOUNT || rentTypeId != item.RENT_TYPE_ID || feePerDay != item.FEE_PER_DAY) bUpdateInOutAndPeriod = true; if (bUpdateInOutAndPeriod) { //Update for contract record only. int inOutTypeId = CommonList.GetInoutTypeFromRentType(item.RENT_TYPE_ID); var inOut = db.InOuts.FirstOrDefault(c => c.CONTRACT_ID == contractId && c.INOUT_TYPE_ID == inOutTypeId); if (inOut != null) { // SAVE INOUT inOut.OUT_AMOUNT = contractAmount; inOut.RENT_TYPE_ID = rentTypeId; inOut.INOUT_TYPE_ID = CommonList.GetInoutTypeFromRentType(rentTypeId); inOut.MORE_INFO = inOut.SEARCH_TEXT = string.Format("Cho khách {0} thuê: {1} ngày {2} trị giá {3}", txtCustomerName.Text.Trim(), txtItemName.Text.Trim(), DateTime.Now.ToString("dd/MM/yyyy"), txtAmount.Text.Trim()); inOut.UPDATED_BY = Session["username"].ToString(); inOut.UPDATED_DATE = DateTime.Now; } //Update for others in out record of the contract var listInOut = db.InOuts.Where(c => c.CONTRACT_ID == contractId && c.RENT_TYPE_ID == item.RENT_TYPE_ID).ToList(); foreach (var io in listInOut) { io.RENT_TYPE_ID = rentTypeId; io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; } //Update for 3 first PayPeriod records List<PayPeriod> listPayPeriod = db.PayPeriods.Where(c => c.CONTRACT_ID == contractId).ToList(); foreach (var pp in listPayPeriod.Take(3)) { pp.AMOUNT_PER_PERIOD = feePerDay * 10; } //Update for remain PayPeriod records int multipleFee = Convert.ToInt32(Decimal.Floor(contractAmount / 100000)); decimal increateFeeCar = (feePerDay * 10) + (multipleFee * 50 * 10); decimal increateFeeEquip = (feePerDay * 10) + (multipleFee * 100 * 10); decimal increateFeeOther = (feePerDay * 10); foreach (var pp in listPayPeriod.Skip(3)) { switch (rentTypeId) { case 1: if (((feePerDay / multipleFee) * 10) < 4000) pp.AMOUNT_PER_PERIOD = increateFeeCar; else pp.AMOUNT_PER_PERIOD = feePerDay * 10; break; case 2: if (((feePerDay / multipleFee) * 10) < 6000) pp.AMOUNT_PER_PERIOD = increateFeeEquip; else pp.AMOUNT_PER_PERIOD = feePerDay * 10; break; default: pp.AMOUNT_PER_PERIOD = increateFeeOther; break; } } } //Update contract infor item.NOTE = txtNote.Text; item.REFERENCE_ID = -1; item.REFERENCE_NAME = txtReferencePerson.Text.Trim(); item.ITEM_TYPE = txtItemName.Text.Trim(); item.ITEM_LICENSE_NO = txtItemLicenseNo.Text.Trim(); item.RENT_TYPE_ID = Helper.parseInt(ddlRentType.SelectedValue); item.CONTRACT_AMOUNT = Convert.ToDecimal(txtAmount.Text); item.FEE_PER_DAY = Convert.ToDecimal(txtFeePerDay.Text); item.SERIAL_1 = txtSerial1.Text.Trim(); item.SERIAL_2 = txtSerial2.Text.Trim(); item.REFERENCE_PHONE = txtReferencePhone.Text.Trim(); item.SCHOOL_NAME = txtSchool.Text.Trim(); item.CLASS_NAME = txtClass.Text.Trim(); item.IMPLEMENTER = txtImplementer.Text.Trim(); item.BACK_TO_DOCUMENTS = txtBackDocument.Text.Trim(); item.DETAIL = txtItemDetail.Text.Trim(); item.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8}", txtCustomerName.Text.Trim(), txtBirthDay.Text.Trim(), txtLicenseNumber.Text.Trim(), txtRangeDate.Text.Trim(), txtPermanentResidence.Text.Trim(), txtCurrentResidence.Text.Trim(), txtPhone.Text.Trim(), item.CONTRACT_NO, item.RENT_DATE.ToString("dd/MM/yyyy")); item.UPDATED_BY = Session["username"].ToString(); item.UPDATED_DATE = DateTime.Now; //Contract photo SavePhoto(item); //Update customer infor var cusItem = db.Customers.FirstOrDefault(c => c.ID == item.CUSTOMER_ID); if (cusItem != null) { cusItem.NAME = txtCustomerName.Text.Trim(); cusItem.BIRTH_DAY = DateTime.ParseExact(txtBirthDay.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); cusItem.LICENSE_NO = txtLicenseNumber.Text.Trim(); cusItem.LICENSE_RANGE_DATE = DateTime.ParseExact(txtRangeDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); cusItem.LICENSE_RANGE_PLACE = txtPlaceDate.Text.Trim(); cusItem.PHONE = txtPhone.Text.Trim(); cusItem.PERMANENT_RESIDENCE = txtPermanentResidence.Text.Trim(); cusItem.CURRENT_RESIDENCE = txtCurrentResidence.Text.Trim(); } db.SaveChanges(); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa hợp đồng của khách hàng {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, cusItem.NAME, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_UPDATE_CONTRACT, message, false); } ts.Complete(); } Response.Redirect("FormContractManagement.aspx", false); } } catch (Exception ex) { lblMessage.Text = ex.Message; lblMessage.CssClass = "text-center text-danger"; Logger.Log(ex.Message); } }
protected void btnLowRecoverability_Click(object sender, EventArgs e) { try { string id = Request.QueryString["ID"]; using (var db = new RentBikeEntities()) { int contractId = Helper.parseInt(id); var contract = db.Contracts.FirstOrDefault(c => c.CONTRACT_STATUS == true && c.ID == contractId); if (contract != null) { Customer customer = db.Customers.FirstOrDefault(c => c.ID == contract.CUSTOMER_ID); if (customer != null) { string message = string.Empty; if (btnLowRecoverability.CommandArgument == "LowRecoverability") { customer.IS_LOW_RECOVERABILITY = true; lblMessage.Text = string.Format(Constants.LOW_RECOVERABILITY, customer.NAME); btnLowRecoverability.Text = "Bỏ khả năng thu hồi thấp"; btnLowRecoverability.CommandArgument = "RevertLowRecoverability"; message = string.Format("Tài khoản {0} cửa hàng {1} đã đánh dấu khả năng thu hồi thấp cho khách hàng {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, customer.NAME, DateTime.Now); } else { customer.IS_LOW_RECOVERABILITY = false; lblMessage.Text = string.Format(Constants.REVERT_LOW_RECOVERABILITY, customer.NAME); btnLowRecoverability.Text = "Khả năng thu hồi thấp"; btnLowRecoverability.CommandArgument = "LowRecoverability"; message = string.Format("Tài khoản {0} cửa hàng {1} đã bỏ khả năng thu hồi thấp cho khách hàng {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, customer.NAME, DateTime.Now); } db.SaveChanges(); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_UPDATE_CONTRACT, message, false); } } } } catch(Exception ex) { lblMessage.Text = ex.Message; lblMessage.CssClass = "text-center text-danger"; Logger.Log(ex.Message); } }
protected void btnSave_Click(object sender, EventArgs e) { using (TransactionScope trans = new TransactionScope()) { int contractId = Convert.ToInt32(Request.QueryString["ID"]); using (var db = new RentBikeEntities()) { var con = db.CONTRACT_FULL_VW.FirstOrDefault(c => c.ID == contractId && c.ACTIVE == true); string closedContractName = string.Empty; switch (con.RENT_TYPE_ID) { case 1: closedContractName = "Thanh lý thuê xe"; break; case 2: closedContractName = "Thanh lý thuê thiết bị"; break; default: closedContractName = "Thanh lý thuê khác"; break; } // INOUT --> IN amount InOut io1 = new InOut(); io1.IN_AMOUNT = con.CONTRACT_AMOUNT; io1.OUT_AMOUNT = 0; io1.CONTRACT_ID = con.ID; io1.PERIOD_ID = -1; io1.PERIOD_DATE = new DateTime(1, 1, 1); io1.RENT_TYPE_ID = con.RENT_TYPE_ID; var item = db.InOutTypes.FirstOrDefault(s => s.NAME == "Thanh lý"); io1.INOUT_TYPE_ID = item.ID; io1.MORE_INFO = txtMoreInfo.Text.Trim(); io1.STORE_ID = con.STORE_ID; io1.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4}", con.CONTRACT_NO, con.CUSTOMER_NAME, con.STORE_NAME, closedContractName, txtMoreInfo.Text.Trim()); io1.INOUT_DATE = DateTime.Now; io1.CREATED_BY = Session["username"].ToString(); io1.CREATED_DATE = DateTime.Now; io1.UPDATED_BY = Session["username"].ToString(); io1.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io1); // IN --> Rent Fee if (!string.IsNullOrEmpty(txtRealIncome.Text)) { decimal realInAmount = Convert.ToDecimal(txtRealIncome.Text.Replace(",", string.Empty)); if (realInAmount > 0) { InOut io2 = new InOut(); string feeName = string.Empty; switch (con.RENT_TYPE_NAME) { case "Cho thuê xe": feeName = "Phí thuê xe"; break; case "Cho thuê thiết bị văn phòng": feeName = "Phí thuê thiết bị"; break; case "Cho thuê mặt hàng khác": feeName = "Phí khác"; break; } item = db.InOutTypes.First(s => s.NAME == feeName); io2.INOUT_TYPE_ID = item.ID; io2.RENT_TYPE_ID = con.RENT_TYPE_ID; io2.CONTRACT_ID = con.ID; io2.IN_AMOUNT = realInAmount; io2.OUT_AMOUNT = 0; io2.MORE_INFO = txtMoreInfo.Text; io2.PERIOD_DATE = DateTime.Now; io2.STORE_ID = con.STORE_ID; io2.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4}", con.CONTRACT_NO, con.CUSTOMER_NAME, con.STORE_NAME, feeName, txtMoreInfo.Text.Trim()); io2.INOUT_DATE = DateTime.Now; io2.CREATED_BY = Session["username"].ToString(); io2.CREATED_DATE = DateTime.Now; io2.UPDATED_BY = Session["username"].ToString(); io2.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io2); } } //Out --> Return redundant fee if the client comes before deadline date. if (!string.IsNullOrEmpty(txtReduceAmount.Text)) { decimal reduceAmount = Convert.ToDecimal(txtReduceAmount.Text.Replace(",", string.Empty)); if (reduceAmount > 0) { InOut io3 = new InOut(); item = db.InOutTypes.FirstOrDefault(s => s.NAME == "Trả lại phí thừa"); io3.INOUT_TYPE_ID = item.ID; io3.RENT_TYPE_ID = con.RENT_TYPE_ID; io3.CONTRACT_ID = con.ID; io3.IN_AMOUNT = 0; io3.OUT_AMOUNT = reduceAmount; io3.RENT_TYPE_ID = con.RENT_TYPE_ID; io3.MORE_INFO = txtMoreInfo.Text.Trim(); io3.PERIOD_DATE = DateTime.Now; io3.STORE_ID = con.STORE_ID; io3.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4}", con.CONTRACT_NO, con.CUSTOMER_NAME, con.STORE_NAME, "Trả lại phí thừa", txtMoreInfo.Text.Trim()); io3.INOUT_DATE = DateTime.Now; io3.CREATED_BY = Session["username"].ToString(); io3.CREATED_DATE = DateTime.Now; io3.UPDATED_BY = Session["username"].ToString(); io3.UPDATED_DATE = DateTime.Now; db.InOuts.Add(io3); } } db.SaveChanges(); // Writelog string rentTypeNameClosed = string.Empty; switch (con.RENT_TYPE_ID) { case 1: rentTypeNameClosed = "thuê xe"; break; case 2: rentTypeNameClosed = "thuê thiết bị"; break; default: rentTypeNameClosed = "thuê khác"; break; } var customer = db.Customers.FirstOrDefault(c => c.ID == con.CUSTOMER_ID); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện thanh lý hợp đồng {2} của khách hàng {3} vào lúc {4}", Convert.ToString(Session["username"]), STORE_NAME, rentTypeNameClosed, customer.NAME, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CLOSE_CONTRACT, message, false); } using (var db = new RentBikeEntities()) { // Update status contract var con = db.Contracts.FirstOrDefault(c => c.ID == contractId); con.CONTRACT_STATUS = false; con.CLOSE_CONTRACT_DATE = DateTime.Now; // Insert History row ContractHistory ch = new ContractHistory(); ch.CONTRACT_ID = con.ID; ch.CONTRACT_NO = con.CONTRACT_NO; ch.CUSTOMER_ID = con.CUSTOMER_ID; ch.CONTRACT_AMOUNT = con.CONTRACT_AMOUNT; ch.DETAIL = con.DETAIL; ch.RENT_DATE = con.RENT_DATE; ch.END_DATE = con.END_DATE; ch.FEE_PER_DAY = con.FEE_PER_DAY; ch.ITEM_LICENSE_NO = con.ITEM_LICENSE_NO; ch.ITEM_TYPE = con.ITEM_TYPE; ch.NOTE = con.NOTE; ch.REFERENCE_NAME = con.REFERENCE_NAME; ch.RENT_TYPE_ID = con.RENT_TYPE_ID; ch.SERIAL_1 = con.SERIAL_1; ch.SERIAL_2 = con.SERIAL_2; ch.STORE_ID = con.STORE_ID; ch.SEARCH_TEXT = con.SEARCH_TEXT; ch.PAY_FEE_MESSAGE = string.Empty; ch.CLOSE_CONTRACT_DATE = DateTime.Now; ch.REFERENCE_PHONE = con.REFERENCE_PHONE; ch.SCHOOL_NAME = con.SCHOOL_NAME; ch.CLASS_NAME = con.CLASS_NAME; ch.IMPLEMENTER = con.IMPLEMENTER; ch.BACK_TO_DOCUMENTS = con.BACK_TO_DOCUMENTS; ch.PHOTO_1 = con.PHOTO_1; ch.THUMBNAIL_PHOTO_1 = con.THUMBNAIL_PHOTO_1; ch.PHOTO_2 = con.PHOTO_2; ch.THUMBNAIL_PHOTO_2 = con.THUMBNAIL_PHOTO_2; ch.PHOTO_3 = con.PHOTO_3; ch.THUMBNAIL_PHOTO_3 = con.THUMBNAIL_PHOTO_3; ch.PHOTO_4 = con.PHOTO_4; ch.THUMBNAIL_PHOTO_4 = con.THUMBNAIL_PHOTO_4; ch.PHOTO_5 = con.PHOTO_5; ch.THUMBNAIL_PHOTO_5 = con.THUMBNAIL_PHOTO_5; ch.CREATED_BY = Session["username"].ToString(); ch.CREATED_DATE = DateTime.Now; ch.UPDATED_BY = Session["username"].ToString(); ch.UPDATED_DATE = DateTime.Now; if (CommonList.IsBadContract(db, contractId)) { con.IS_BAD_CONTRACT = ch.IS_BAD_CONTRACT = true; } db.ContractHistories.Add(ch); db.SaveChanges(); trans.Complete(); } } Response.Redirect("FormContractManagement.aspx"); }
protected void btnSave_Click(object sender, EventArgs e) { try { using (TransactionScope ts = new TransactionScope()) { string id = Request.QueryString["ID"]; string copy = Request.QueryString["copy"]; string result = ValidateFields(); int cusid = 0; if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } if (string.IsNullOrEmpty(id) || (!string.IsNullOrEmpty(copy) && copy == "1")) // NEW { Contract contract = CommonList.GetContractByLicenseNo(txtLicenseNumber.Text.Trim()); if (contract != null) { // Initialize StringWriter instance. StringWriter stringWriter = new StringWriter(); // Put HtmlTextWriter in using block because it needs to call Dispose. using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter)) { writer.Write("Số CMTND/GPLX này hiện tại đã đăng ký hợp đồng "); writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Format("FormContractUpdate.aspx?ID={0}&sID={1}", contract.ID, contract.STORE_ID)); writer.RenderBeginTag(HtmlTextWriterTag.A); // Start of A writer.Write(contract.CONTRACT_NO); writer.RenderEndTag(); //End of A lblMessage.Text = stringWriter.ToString(); return; } } Customer cusItem = null; using (var db = new RentBikeEntities()) { bool IsNewCust = false; cusItem = db.Customers.FirstOrDefault(c => c.LICENSE_NO == txtLicenseNumber.Text.Trim() && c.NAME == txtCustomerName.Text.Trim()); if (cusItem == null) { IsNewCust = true; cusItem = new Customer(); } cusItem.NAME = txtCustomerName.Text.Trim(); cusItem.BIRTH_DAY = DateTime.ParseExact(txtBirthDay.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); cusItem.LICENSE_NO = txtLicenseNumber.Text.Trim(); cusItem.LICENSE_RANGE_DATE = DateTime.ParseExact(txtRangeDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); cusItem.LICENSE_RANGE_PLACE = txtPlaceDate.Text.Trim(); cusItem.PHONE = txtPhone.Text.Trim(); cusItem.PERMANENT_RESIDENCE = txtPermanentResidence.Text.Trim(); cusItem.CURRENT_RESIDENCE = txtCurrentResidence.Text.Trim(); if (IsNewCust) { db.Customers.Add(cusItem); } db.SaveChanges(); cusid = cusItem.ID; } // New Contract Contract item = new Contract(); item.RENT_TYPE_ID = Convert.ToInt32(ddlRentType.SelectedValue); item.FEE_PER_DAY = Math.Round(Convert.ToDecimal(txtFeePerDay.Text.Replace(",", string.Empty))); if (!string.IsNullOrEmpty(txtRentDate.Text)) { item.RENT_DATE = DateTime.ParseExact(txtRentDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); } else { item.RENT_DATE = DateTime.Now; } if (!string.IsNullOrEmpty(txtEndDate.Text)) { item.END_DATE = DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); } else { item.END_DATE = item.RENT_DATE.AddDays(29); } item.EXTEND_END_DATE = item.END_DATE; item.CLOSE_CONTRACT_DATE = new DateTime(1, 1, 1); item.PAY_FEE_MESSAGE = string.Empty; item.NOTE = txtNote.Text; item.REFERENCE_ID = -1; item.REFERENCE_NAME = txtReferencePerson.Text.Trim(); item.ITEM_TYPE = txtItemName.Text.Trim(); item.ITEM_LICENSE_NO = txtItemLicenseNo.Text.Trim(); item.SERIAL_1 = txtSerial1.Text.Trim(); item.SERIAL_2 = txtSerial2.Text.Trim(); item.REFERENCE_PHONE = txtReferencePhone.Text.Trim(); item.SCHOOL_NAME = txtSchool.Text.Trim(); item.CLASS_NAME = txtClass.Text.Trim(); item.IMPLEMENTER = txtImplementer.Text.Trim(); item.BACK_TO_DOCUMENTS = txtBackDocument.Text.Trim(); item.DETAIL = txtItemDetail.Text.Trim(); item.CUSTOMER_ID = cusid; item.CONTRACT_STATUS = true; item.CONTRACT_AMOUNT = Convert.ToDecimal(txtAmount.Text.Replace(",", string.Empty)); item.CREATED_BY = Session["username"].ToString(); item.CREATED_DATE = DateTime.Now; item.UPDATED_BY = Session["username"].ToString(); item.UPDATED_DATE = DateTime.Now; //item.CONTRACT_NO = txtContractNo.Text.Trim(); if (ddlStore.Enabled == true) { item.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); } else { item.STORE_ID = Convert.ToInt32(Session["store_id"]); } item.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8}", txtCustomerName.Text.Trim(), txtBirthDay.Text.Trim(), txtLicenseNumber.Text.Trim(), txtRangeDate.Text.Trim(), txtPermanentResidence.Text.Trim(), txtCurrentResidence.Text.Trim(), txtPhone.Text.Trim(), item.CONTRACT_NO, item.RENT_DATE.ToString("dd/MM/yyyy")); SavePhoto(item); using (var db = new RentBikeEntities()) { db.Contracts.Add(item); db.SaveChanges(); DateTime periodTime = DateTime.Today; if (!string.IsNullOrEmpty(txtRentDate.Text)) { periodTime = DateTime.ParseExact(txtRentDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); } PayPeriod newPeriod = new PayPeriod(); newPeriod.PAY_DATE = periodTime; CommonList.CreatePayPeriod(db, item.ID, newPeriod, true); } InOut io = new InOut(); io.CONTRACT_ID = item.ID; io.IN_AMOUNT = 0; io.OUT_AMOUNT = Convert.ToDecimal(txtAmount.Text); io.RENT_TYPE_ID = Convert.ToInt32(ddlRentType.SelectedValue); io.PERIOD_DATE = DateTime.Now; io.MORE_INFO = string.Format("Cho khách {0} thuê: {1} ngày {2} trị giá {3}", txtCustomerName.Text.Trim(), txtItemName.Text.Trim(), DateTime.Now.ToString("dd/MM/yyyy"), txtAmount.Text.Trim()); if (ddlStore.Enabled == true) { io.STORE_ID = Convert.ToInt32(ddlStore.SelectedValue); } else { io.STORE_ID = Convert.ToInt32(Session["store_id"]); } io.SEARCH_TEXT = string.Format("{0} ", io.MORE_INFO); io.INOUT_DATE = DateTime.Now; io.CREATED_BY = Session["username"].ToString(); io.CREATED_DATE = DateTime.Now; io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; io.INOUT_TYPE_ID = CommonList.GetInoutTypeFromRentType(Helper.parseInt(ddlRentType.SelectedValue)); using (var rbdb = new RentBikeEntities()) { rbdb.InOuts.Add(io); rbdb.SaveChanges(); CommonList.AutoExtendPeriod(rbdb, item.ID); } string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện làm hợp đồng cho khách hàng {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, cusItem.NAME, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_CREATE_CONTRACT, message, false); ts.Complete(); } else // EDIT { result = ValidatePermission(); if (!string.IsNullOrEmpty(result)) { lblMessage.Text = result; return; } int contractId = Helper.parseInt(id); bool bUpdateInOutAndPeriod = false; using (var db = new RentBikeEntities()) { var item = db.Contracts.FirstOrDefault(itm => itm.ID == contractId); int rentTypeId = Helper.parseInt(ddlRentType.SelectedValue); decimal contractAmount = Convert.ToDecimal(txtAmount.Text); decimal feePerDay = Convert.ToDecimal(txtFeePerDay.Text); if (contractAmount != item.CONTRACT_AMOUNT || rentTypeId != item.RENT_TYPE_ID || feePerDay != item.FEE_PER_DAY) { bUpdateInOutAndPeriod = true; } if (bUpdateInOutAndPeriod) { //Update for contract record only. int inOutTypeId = CommonList.GetInoutTypeFromRentType(item.RENT_TYPE_ID); var inOut = db.InOuts.FirstOrDefault(c => c.CONTRACT_ID == contractId && c.INOUT_TYPE_ID == inOutTypeId); if (inOut != null) { // SAVE INOUT inOut.OUT_AMOUNT = contractAmount; inOut.RENT_TYPE_ID = rentTypeId; inOut.INOUT_TYPE_ID = CommonList.GetInoutTypeFromRentType(rentTypeId); inOut.MORE_INFO = inOut.SEARCH_TEXT = string.Format("Cho khách {0} thuê: {1} ngày {2} trị giá {3}", txtCustomerName.Text.Trim(), txtItemName.Text.Trim(), DateTime.Now.ToString("dd/MM/yyyy"), txtAmount.Text.Trim()); inOut.UPDATED_BY = Session["username"].ToString(); inOut.UPDATED_DATE = DateTime.Now; } //Update for others in out record of the contract var listInOut = db.InOuts.Where(c => c.CONTRACT_ID == contractId && c.RENT_TYPE_ID == item.RENT_TYPE_ID).ToList(); foreach (var io in listInOut) { io.RENT_TYPE_ID = rentTypeId; io.UPDATED_BY = Session["username"].ToString(); io.UPDATED_DATE = DateTime.Now; } //Update for 3 first PayPeriod records List <PayPeriod> listPayPeriod = db.PayPeriods.Where(c => c.CONTRACT_ID == contractId).ToList(); foreach (var pp in listPayPeriod.Take(3)) { pp.AMOUNT_PER_PERIOD = feePerDay * 10; } //Update for remain PayPeriod records int multipleFee = Convert.ToInt32(Decimal.Floor(contractAmount / 100000)); decimal increateFeeCar = (feePerDay * 10) + (multipleFee * 50 * 10); decimal increateFeeEquip = (feePerDay * 10) + (multipleFee * 100 * 10); decimal increateFeeOther = (feePerDay * 10); foreach (var pp in listPayPeriod.Skip(3)) { switch (rentTypeId) { case 1: if (((feePerDay / multipleFee) * 10) < 4000) { pp.AMOUNT_PER_PERIOD = increateFeeCar; } else { pp.AMOUNT_PER_PERIOD = feePerDay * 10; } break; case 2: if (((feePerDay / multipleFee) * 10) < 6000) { pp.AMOUNT_PER_PERIOD = increateFeeEquip; } else { pp.AMOUNT_PER_PERIOD = feePerDay * 10; } break; default: pp.AMOUNT_PER_PERIOD = increateFeeOther; break; } } } //Update contract infor item.NOTE = txtNote.Text; item.REFERENCE_ID = -1; item.REFERENCE_NAME = txtReferencePerson.Text.Trim(); item.ITEM_TYPE = txtItemName.Text.Trim(); item.ITEM_LICENSE_NO = txtItemLicenseNo.Text.Trim(); item.RENT_TYPE_ID = Helper.parseInt(ddlRentType.SelectedValue); item.CONTRACT_AMOUNT = Convert.ToDecimal(txtAmount.Text); item.FEE_PER_DAY = Convert.ToDecimal(txtFeePerDay.Text); item.SERIAL_1 = txtSerial1.Text.Trim(); item.SERIAL_2 = txtSerial2.Text.Trim(); item.REFERENCE_PHONE = txtReferencePhone.Text.Trim(); item.SCHOOL_NAME = txtSchool.Text.Trim(); item.CLASS_NAME = txtClass.Text.Trim(); item.IMPLEMENTER = txtImplementer.Text.Trim(); item.BACK_TO_DOCUMENTS = txtBackDocument.Text.Trim(); item.DETAIL = txtItemDetail.Text.Trim(); item.SEARCH_TEXT = string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8}", txtCustomerName.Text.Trim(), txtBirthDay.Text.Trim(), txtLicenseNumber.Text.Trim(), txtRangeDate.Text.Trim(), txtPermanentResidence.Text.Trim(), txtCurrentResidence.Text.Trim(), txtPhone.Text.Trim(), item.CONTRACT_NO, item.RENT_DATE.ToString("dd/MM/yyyy")); item.UPDATED_BY = Session["username"].ToString(); item.UPDATED_DATE = DateTime.Now; //Contract photo SavePhoto(item); //Update customer infor var cusItem = db.Customers.FirstOrDefault(c => c.ID == item.CUSTOMER_ID); if (cusItem != null) { cusItem.NAME = txtCustomerName.Text.Trim(); cusItem.BIRTH_DAY = DateTime.ParseExact(txtBirthDay.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); cusItem.LICENSE_NO = txtLicenseNumber.Text.Trim(); cusItem.LICENSE_RANGE_DATE = DateTime.ParseExact(txtRangeDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); cusItem.LICENSE_RANGE_PLACE = txtPlaceDate.Text.Trim(); cusItem.PHONE = txtPhone.Text.Trim(); cusItem.PERMANENT_RESIDENCE = txtPermanentResidence.Text.Trim(); cusItem.CURRENT_RESIDENCE = txtCurrentResidence.Text.Trim(); } db.SaveChanges(); string message = string.Format("Tài khoản {0} cửa hàng {1} thực hiện chỉnh sửa hợp đồng của khách hàng {2} vào lúc {3}", Convert.ToString(Session["username"]), STORE_NAME, cusItem.NAME, DateTime.Now); Helper.WriteLog(Convert.ToString(Session["username"]), STORE_NAME, Constants.ACTION_UPDATE_CONTRACT, message, false); } ts.Complete(); } Response.Redirect("FormContractManagement.aspx", false); } } catch (Exception ex) { lblMessage.Text = ex.Message; lblMessage.CssClass = "text-center text-danger"; Logger.Log(ex.Message); } }