You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.1 KiB

  1. using Swift.DAL.Remittance.BonusManagement;
  2. using Swift.DAL.SwiftDAL;
  3. using Swift.web.Component.Grid;
  4. using Swift.web.Component.Grid.gridHelper;
  5. using Swift.web.Library;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.IO;
  10. namespace Swift.web.Remit.BonusManagement.PrizeSetup
  11. {
  12. public partial class PrizeSetup : System.Web.UI.Page
  13. {
  14. readonly RemittanceLibrary sl = new RemittanceLibrary();
  15. readonly PrizeSetupDao psdao = new PrizeSetupDao();
  16. readonly StaticDataDdl sdl = new StaticDataDdl();
  17. private readonly SwiftGrid grid = new SwiftGrid();
  18. readonly RemittanceDao sd = new RemittanceDao();
  19. protected const string GridName = "grid_prizeSetup";
  20. private const string ViewFunctionId = "20821000";
  21. private const string AddEditFunctionId = "20821010";
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24. Authenticate();
  25. Misc.MakeNumericTextbox(ref points);
  26. if (!IsPostBack)
  27. {
  28. if (GetBonusSchemeId() > 0)
  29. Populate(null);
  30. if (GetId() > 0)
  31. {
  32. Populate(GetId().ToString());
  33. }
  34. }
  35. LoadGrid();
  36. }
  37. private void DeleteRow()
  38. {
  39. string id = GetId().ToString();
  40. if (string.IsNullOrEmpty(id))
  41. return;
  42. DbResult dbResult = psdao.DeletePrize(GetStatic.GetUser(), id);
  43. ManageMessage(dbResult);
  44. }
  45. private void ManageMessage(DbResult dbResult)
  46. {
  47. if (dbResult.ErrorCode == "0")
  48. {
  49. LoadGrid();
  50. }
  51. GetStatic.PrintMessage(Page, dbResult);
  52. }
  53. private void Authenticate()
  54. {
  55. sl.CheckAuthentication(ViewFunctionId);
  56. }
  57. private void LoadGrid()
  58. {
  59. grid.ColumnList = new List<GridColumn>
  60. {
  61. new GridColumn("sn", "SN", "4", "T"),
  62. new GridColumn("schemeName", "Scheme Name", "90", "T"),
  63. new GridColumn("points", "Points", "90", "T"),
  64. new GridColumn("giftItem", "Gift Items", "100", "T")
  65. };
  66. bool allowAddEdit = sl.HasRight(AddEditFunctionId);
  67. grid.GridType = 1;
  68. grid.GridName = GridName;
  69. grid.GridDataSource = SwiftGrid.GridDS.RemittanceDB;
  70. grid.GridWidth = 500;
  71. grid.GridMinWidth = 500;
  72. grid.InputPerRow = 2;
  73. grid.AllowEdit = allowAddEdit;
  74. grid.AllowDelete = allowAddEdit;
  75. grid.EditCallBackFunction = "OpenInEditMode";
  76. grid.RowIdField = "schemePrizeId";
  77. grid.AddPage = "PrizeSetup.aspx";
  78. grid.ThisPage = "PrizeSetup.aspx";
  79. string sql = "EXEC proc_bonusOperationSetup @flag = 's' ,@bonusSchemeId =" + sd.FilterString(GetBonusSchemeId().ToString());
  80. grid.SetComma();
  81. rpt_grid.InnerHtml = grid.CreateGrid(sql);
  82. }
  83. protected int GetBonusSchemeId()
  84. {
  85. return Convert.ToInt32(Request.QueryString["bonusSchemeId"]);
  86. }
  87. private int GetId()
  88. {
  89. return GetStatic.ParseInt(hddDetailId.Value);
  90. }
  91. protected void Populate(string id)
  92. {
  93. if (id == null)
  94. {
  95. schemeName.Text = Request.QueryString["schemeName"].ToString();
  96. sdl.SetStaticDdl(ref giftItem, "7900");
  97. }
  98. else
  99. {
  100. DataRow dr = psdao.SelectPrizeById(GetStatic.GetUser(), GetId().ToString());
  101. schemeName.Text = dr["schemeName"].ToString();
  102. points.Text = dr["points"].ToString();
  103. giftImageFile.ImageUrl = GetStatic.GetUrlRoot() + "/Handler/Docs.ashx?id=" + dr["schemePrizeId"].ToString() + "&mode=bonusPrize" + "&file=" + dr["giftImage"];
  104. sl.SetDDL(ref giftItem, "EXEC proc_dropDownLists @flag= 'gift-item'", "valueId", "detailTitle", GetStatic.GetRowData(dr, "giftItem"), "");
  105. }
  106. }
  107. protected void btnSave_Click(object sender, EventArgs e)
  108. {
  109. ManagePrize();
  110. }
  111. private void ManagePrize()
  112. {
  113. string type = "doc";
  114. string root = "";
  115. string info = "";
  116. if (giftImage.PostedFile.FileName != null)
  117. {
  118. string pFile = giftImage.PostedFile.FileName.Replace("\\", "/");
  119. int pos = pFile.LastIndexOf(".");
  120. if (pos < 0)
  121. type = "";
  122. else
  123. type = pFile.Substring(pos + 1, pFile.Length - pos - 1);
  124. root = GetStatic.GetAppRoot(); //ConfigurationSettings.AppSettings["root"];
  125. string extension = Path.GetExtension(giftImage.PostedFile.FileName);
  126. string fileName = giftItem.SelectedItem.Text + "_" + GetTimestamp(DateTime.Now) + extension;
  127. info = UploadFile(giftImage.PostedFile.FileName, "", root);
  128. if (info.Substring(0, 5) == "error")
  129. return;
  130. DbResult dbResult = psdao.Update(GetStatic.GetUser(), GetId().ToString(), GetBonusSchemeId().ToString(), points.Text, giftItem.Text, fileName);
  131. if (dbResult.ErrorCode == "1")
  132. {
  133. ManageMessage(dbResult);
  134. return;
  135. }
  136. string locationToMove = root + "\\OtherDocuments\\Rewards" + "\\" + dbResult.Id;
  137. string fileToCreate = locationToMove + "\\" + fileName;
  138. if (File.Exists(fileToCreate))
  139. File.Delete(fileToCreate);
  140. if (!Directory.Exists(locationToMove))
  141. Directory.CreateDirectory(locationToMove);
  142. File.Move(info, fileToCreate);
  143. string strMessage = "File Uploaded Successfully";
  144. dbResult.SetError("0", strMessage, "");
  145. ManageMessage(dbResult);
  146. }
  147. }
  148. public string UploadFile(String fileName, string id, string root)
  149. {
  150. if (fileName == "")
  151. {
  152. return "error:Invalid filename supplied";
  153. }
  154. if (giftImage.PostedFile.ContentLength == 0)
  155. {
  156. return "error:Invalid file content";
  157. }
  158. try
  159. {
  160. if (giftImage.PostedFile.ContentLength <= 2048000)
  161. {
  162. string tmpPath = root + "\\doc\\tmp\\";
  163. if (!Directory.Exists(tmpPath))
  164. Directory.CreateDirectory(tmpPath);
  165. string saved_file_name = root + "\\doc\\tmp\\" + id + "_" + fileName;
  166. giftImage.PostedFile.SaveAs(saved_file_name);
  167. return saved_file_name;
  168. }
  169. else
  170. {
  171. return "error:Unable to upload,file exceeds maximum limit";
  172. }
  173. }
  174. catch (UnauthorizedAccessException ex)
  175. {
  176. return "error:" + ex.Message + "Permission to upload file denied";
  177. }
  178. }
  179. protected void btnEdit_Click(object sender, EventArgs e)
  180. {
  181. Populate(GetId().ToString());
  182. }
  183. protected void btnDelete_Click(object sender, EventArgs e)
  184. {
  185. DeleteRow();
  186. }
  187. public static string GetTimestamp(DateTime value)
  188. {
  189. var timeValue = value.ToString("hhmmssffffff");
  190. return timeValue + DateTime.Now.Ticks;
  191. }
  192. }
  193. }