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) { 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"); }