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.

213 lines
7.7 KiB

  1. using Swift.DAL.Common;
  2. using Swift.DAL.SwiftDAL;
  3. using Swift.DAL.VoucherReport;
  4. using Swift.web.Library;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Web;
  11. namespace Swift.web.BillVoucher.VoucherEntryWithTax
  12. {
  13. public partial class VoucherEntry : System.Web.UI.Page
  14. {
  15. private const string ViewFunctionId = "20302400";
  16. private const string DateFunctionId = "20302410";
  17. private readonly RemittanceLibrary _sdd = new RemittanceLibrary();
  18. private readonly VoucherReportDAO _vrd = new VoucherReportDAO();
  19. protected void Page_Load(object sender, EventArgs e)
  20. {
  21. Authenticate();
  22. string methodName = Request.Form["MethodName"];
  23. if (methodName == "SaveTemp")
  24. SaveTempData();
  25. if (methodName == "PopulateTempData")
  26. ShowTempVoucher();
  27. if (methodName == "DeleteTemp")
  28. DeleteTemp();
  29. if (methodName == "SaveMainData")
  30. SaveMainData();
  31. if (!IsPostBack)
  32. {
  33. Misc.MakeAmountTextBox(ref amt);
  34. transactionDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
  35. PopulateDDL();
  36. }
  37. }
  38. private void Authenticate()
  39. {
  40. _sdd.CheckAuthentication(ViewFunctionId);
  41. }
  42. private void PopulateDDL()
  43. {
  44. _sdd.SetDDL(ref voucherType, "EXEC Proc_dropdown_remit @FLAG='voucherDDL'", "value", "functionName", "", "");
  45. _sdd.SetDDL(ref Department, "EXEC Proc_dropdown_remit @FLAG='Department'", "RowId", "DepartmentName", "", "Select Department");
  46. _sdd.SetDDL(ref Branch, "EXEC Proc_dropdown_remit @FLAG='Branch'", "agentId", "agentName", "", "Select Branch");
  47. }
  48. protected void SaveTempData()
  49. {
  50. DbResult _dbRes = new DbResult();
  51. string amount = Request.Form["amt"];
  52. string acInfo = Request.Form["acInfo"];
  53. string dropDownDrCr = Request.Form["dropDownDrCr"];
  54. string Department = Request.Form["Department"];
  55. string Branch = Request.Form["Branch"];
  56. string EmpName = Request.Form["EmpName"];
  57. string Field1 = Request.Form["Field1"];
  58. string percent = Request.Form["Percent"];
  59. if (GetStatic.ParseDouble(amount) <= 0)
  60. {
  61. _dbRes.SetError("1", "Please enter valid Amount!", null);
  62. GetStatic.JsonResponse(_dbRes, this);
  63. return;
  64. }
  65. _dbRes = _vrd.InsertTempVoucherEntryDetailsNew(GetStatic.GetSessionId(), GetStatic.GetUser(), acInfo, dropDownDrCr, amount, Department, Branch, EmpName, Field1, "", percent);
  66. GetStatic.JsonResponse(_dbRes, this);
  67. }
  68. protected bool AllowChangeDate()
  69. {
  70. return _sdd.HasRight(DateFunctionId);
  71. }
  72. protected void DeleteTemp()
  73. {
  74. string rowId = Request.Form["RowId"];
  75. var res = _vrd.DeleteRecordVoucherEntryDetails(rowId);
  76. GetStatic.JsonResponse(res, this);
  77. }
  78. protected void Unsave()
  79. {
  80. ShowTempVoucher();
  81. }
  82. protected void SaveMainData()
  83. {
  84. DbResult _dbRes = new DbResult();
  85. string fileName = "";
  86. string filePath = "";
  87. var VoucherImage = Request.Files["vImage"];
  88. var date = Request.Form["transactionDate"];
  89. var narration = Request.Form["narrationField"];
  90. var vType = Request.Form["voucherType"];
  91. var chequeNumber = Request.Form["chequeNo"];
  92. if (null != VoucherImage)
  93. {
  94. // Get the file extension
  95. string fileExtension = System.IO.Path.GetExtension(VoucherImage.FileName);
  96. if (!IsImage(VoucherImage))
  97. {
  98. _dbRes.SetError("1", "File types other than image are not acceptable.", null);
  99. GetStatic.JsonResponse(_dbRes, this);
  100. return;
  101. }
  102. else
  103. {
  104. // Get the file size
  105. int fileSize = VoucherImage.ContentLength;
  106. // If file size is greater than 2 MB
  107. if (fileSize > Convert.ToInt32(GetStatic.GetUploadFileSize()))
  108. {
  109. _dbRes.SetError("1", "File size cannot be greater than 2 MB", null);
  110. GetStatic.JsonResponse(_dbRes, this);
  111. return;
  112. }
  113. else
  114. {
  115. // Upload the file
  116. fileName = "UploadedVoucher-" + GetTimestamp(DateTime.Now) + fileExtension;
  117. string path = GetStatic.ReadWebConfig("filePath") + "VoucherDoc\\";
  118. if (!Directory.Exists(path))
  119. Directory.CreateDirectory(path);
  120. filePath = path + fileName;
  121. VoucherImage.SaveAs(filePath);
  122. }
  123. }
  124. }
  125. _dbRes = _vrd.SaveTempTransaction(GetStatic.GetSessionId(), date, narration, vType, chequeNumber, GetStatic.GetUser(), fileName);
  126. if (!string.IsNullOrEmpty(filePath) && _dbRes.ErrorCode != "0")
  127. {
  128. File.Delete(filePath);
  129. }
  130. GetStatic.JsonResponse(_dbRes, this);
  131. }
  132. private void ShowTempVoucher()
  133. {
  134. IList<VoucherTempData> _voucherData = new List<VoucherTempData>();
  135. _voucherData = _vrd.GetTempVoucherEntryDataDetailsList(GetStatic.GetSessionId());
  136. GetStatic.JsonResponse(_voucherData, this);
  137. }
  138. public static bool IsImage(HttpPostedFile fileUpload)
  139. {
  140. if (Path.GetExtension(fileUpload.FileName).ToLower() != ".jpg"
  141. && Path.GetExtension(fileUpload.FileName).ToLower() != ".png"
  142. && Path.GetExtension(fileUpload.FileName).ToLower() != ".gif"
  143. && Path.GetExtension(fileUpload.FileName).ToLower() != ".jpeg")
  144. {
  145. return false;
  146. }
  147. if (fileUpload.ContentType.ToLower() != "image/jpg" &&
  148. fileUpload.ContentType.ToLower() != "image/jpeg" &&
  149. fileUpload.ContentType.ToLower() != "image/pjpeg" &&
  150. fileUpload.ContentType.ToLower() != "image/gif" &&
  151. fileUpload.ContentType.ToLower() != "image/x-png" &&
  152. fileUpload.ContentType.ToLower() != "image/png")
  153. {
  154. return false;
  155. }
  156. try
  157. {
  158. byte[] buffer = new byte[512];
  159. fileUpload.InputStream.Read(buffer, 0, 512);
  160. string content = Encoding.UTF8.GetString(buffer);
  161. if (Regex.IsMatch(content, @"<script|<html|<head|<title|<body|<pre|<table|<a\s+href|<img|<plaintext|<cross\-domain\-policy|<?php",
  162. RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline))
  163. {
  164. return false;
  165. }
  166. }
  167. catch (Exception)
  168. {
  169. return false;
  170. }
  171. try
  172. {
  173. using (var bitmap = new System.Drawing.Bitmap(fileUpload.InputStream))
  174. {
  175. }
  176. }
  177. catch (Exception)
  178. {
  179. return false;
  180. }
  181. return true;
  182. }
  183. public static string GetTimestamp(DateTime value)
  184. {
  185. return value.ToString("yyyyMMddHHmmssffff");
  186. }
  187. }
  188. }