PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/SIMSProject/Views/Inventory/ProductTracking/ProductTrackingUserControl.ascx.cs

#
C# | 316 lines | 281 code | 35 blank | 0 comment | 55 complexity | 394840ee40d965c0dbfa6e7d34c5843a MD5 | raw file
  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.Web.UI.WebControls.WebParts;
  5. using Microsoft.SharePoint;
  6. using SIMSProject.ControlTemplates.SIMSProject;
  7. using Microsoft.SharePoint.WebControls;
  8. using System.Data;
  9. using SIMSProject.Common;
  10. namespace SIMSProject.Views.Inventory.ProductTracking
  11. {
  12. public partial class ProductTrackingUserControl : UserControl
  13. {
  14. SPItem item = SPContext.Current.Item;
  15. SPWeb web = SPContext.Current.Web;
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. if (SPContext.Current.FormContext.FormMode != SPControlMode.New)
  19. {
  20. this.ffTrackingType.ControlMode = SPControlMode.Display;
  21. }
  22. if (!IsPostBack)
  23. {
  24. this.hdfSessionName.Value = Guid.NewGuid().ToString().Replace("-", "");
  25. this.btReject.Attributes.Add("onclick", "if($('#tdDirectorComment textarea').val()==''){alert('Please enter comment!');return false;};return confirm('Are you sure to reject this request ?')");
  26. }
  27. ShowHideControl();
  28. }
  29. protected void ShowHideControl()
  30. {
  31. if (item["Status"] == null || item["Status"].ToString() == "")
  32. {
  33. this.trApproveDate.Visible = false;
  34. this.trDirector.Visible = false;
  35. this.trDirectorComment.Visible = false;
  36. if (SPContext.Current.FormContext.FormMode != SPControlMode.Display)
  37. {
  38. this.btSave.Visible = true;
  39. }
  40. }
  41. else
  42. {
  43. this.trApproveDate.Visible = true;
  44. this.trDirector.Visible = true;
  45. this.trDirectorComment.Visible = true;
  46. this.Title.ControlMode = SPControlMode.Display;
  47. this.ffDescription.ControlMode = SPControlMode.Display;
  48. if (item["Status"].ToString() == "Director Review" && web.IsCurrentUserMemberOfGroup(web.Groups[Constant.Groups.Director].ID) && SPContext.Current.FormContext.FormMode != SPControlMode.Display)
  49. {
  50. this.btApprove.Visible = true;
  51. this.btReject.Visible = true;
  52. this.ffDirectorComment.ControlMode = SPControlMode.Edit;
  53. }
  54. else
  55. {
  56. this.ffDirectorComment.ControlMode = SPControlMode.Display;
  57. }
  58. }
  59. }
  60. protected override void CreateChildControls()
  61. {
  62. base.CreateChildControls();
  63. ProductAdding productAddingControl = (ProductAdding)Page.LoadControl("~/_CONTROLTEMPLATES/SIMSProject/ProductAdding.ascx");
  64. productAddingControl.ParentListName = "Product Tracking";
  65. productAddingControl.SetSessionName (this.hdfSessionName.Value);
  66. productAddingControl.parentPage = this;
  67. this.phProductAddingModule.Controls.Add(productAddingControl);
  68. }
  69. public string GetTrackingType()
  70. {
  71. return this.ffTrackingType.Value.ToString();
  72. }
  73. protected override void OnInit(EventArgs e)
  74. {
  75. base.OnInit(e);
  76. if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit || SPContext.Current.FormContext.FormMode == SPControlMode.New)
  77. {
  78. SPContext.Current.FormContext.OnSaveHandler += this.CustomSaveHandler;
  79. }
  80. }
  81. private void CustomSaveHandler(object o, EventArgs e)
  82. {
  83. UpdateProduct();
  84. SPContext.Current.Item["Year"] = DateTime.Now.Year;
  85. SPContext.Current.Item["Month"] = DateTime.Now.Month;
  86. SPContext.Current.Web.AllowUnsafeUpdates = true;
  87. SaveButton.SaveItem(SPContext.Current, false, string.Empty);
  88. }
  89. private void UpdateProduct()
  90. {
  91. DataTable dt = Session[this.hdfSessionName.Value] as DataTable;
  92. int id, quantity;
  93. SPSite siteColl = SPContext.Current.Site;
  94. SPWeb site = SPContext.Current.Web;
  95. SPSecurity.RunWithElevatedPrivileges(delegate()
  96. {
  97. using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
  98. {
  99. using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
  100. {
  101. SPList listProduct = ElevatedSite.Lists["Product"];
  102. SPListItem item;
  103. if (dt != null)
  104. {
  105. foreach (DataRow dr in dt.Rows)
  106. {
  107. id = (int)dr["id"];
  108. quantity = (int)dr["Quantity"];
  109. item = listProduct.GetItemById(id);
  110. int currentQuantity = Convert.ToInt32(item["Quantity"]);
  111. if (this.ffTrackingType.Value.ToString() == "Import")
  112. {
  113. item["Quantity"] = currentQuantity + quantity;
  114. }
  115. else if (this.ffTrackingType.Value.ToString() == "Export" || this.ffTrackingType.Value.ToString() == "Remove")
  116. {
  117. item["Quantity"] = currentQuantity - quantity;
  118. }
  119. ElevatedSite.AllowUnsafeUpdates = true;
  120. item.Update();
  121. ElevatedSite.AllowUnsafeUpdates = false;
  122. }
  123. }
  124. }
  125. }
  126. });
  127. }
  128. private void UpdateProductDetail()
  129. {
  130. int id = SPContext.Current.Item.ID;
  131. int totalQuantity = 0;
  132. DataTable productTable = (DataTable)Session[this.hdfSessionName.Value];
  133. SPList childList = SPContext.Current.Web.Lists["Product Tracking Detail"];
  134. for (int i = 0; i < productTable.Rows.Count; i++)
  135. {
  136. SPQuery query = new SPQuery();
  137. query.Query = "<Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>" + id.ToString() + "</Value></Eq><Eq><FieldRef Name='Product_x0020_Id' /><Value Type='Text'>" + Convert.ToString(productTable.Rows[i]["id"]) + "</Value></Eq></And></Where>";
  138. if (childList.GetItems(query).Count == 0)
  139. {
  140. SPListItem item = childList.Items.Add();
  141. item["Title"] = id;
  142. item["Product Name"] = productTable.Rows[i]["ProductName"];
  143. item["Product Code"] = productTable.Rows[i]["ProductCode"];
  144. item["Product Id"] = productTable.Rows[i]["id"];
  145. item["Quantity"] = productTable.Rows[i]["Quantity"];
  146. item.Update();
  147. }
  148. else
  149. {
  150. SPListItem item = childList.GetItems(query)[0];
  151. item["Quantity"] = productTable.Rows[i]["Quantity"];
  152. item.Update();
  153. }
  154. int temp = 0;
  155. Int32.TryParse(Convert.ToString(productTable.Rows[i]["Quantity"]), out temp);
  156. totalQuantity += temp;
  157. }
  158. SPContext.Current.Item["Total Quantity"] = totalQuantity;
  159. SaveButton.SaveItem(SPContext.Current, false, string.Empty);
  160. }
  161. private bool CheckProduct()
  162. {
  163. DataTable dt = Session[this.hdfSessionName.Value] as DataTable;
  164. if (dt != null && dt.Rows.Count != 0)
  165. {
  166. string message = string.Empty;
  167. foreach (DataRow dr in dt.Rows)
  168. {
  169. int id = 0;
  170. Int32.TryParse(Convert.ToString(dr["id"]), out id);
  171. int quantity = 0;
  172. Int32.TryParse(Convert.ToString(dr["Quantity"]), out quantity);
  173. SPListItem product = SPContext.Current.Web.Lists["Product"].GetItemById(id);
  174. int productQuantity = 0;
  175. Int32.TryParse(Convert.ToString(product["Quantity"]), out productQuantity);
  176. if (quantity > productQuantity)
  177. {
  178. message += "The quanlity of " + product["Title"] + " is not enough\\r\\n";
  179. }
  180. }
  181. if (message != string.Empty)
  182. {
  183. message = "<script type='text/javascript'>window.alert('" + message + "');</script>";
  184. this.Page.Response.Write(message);
  185. return false;
  186. }
  187. else return true;
  188. }
  189. else
  190. {
  191. this.Page.Response.Write("<script type='text/javascript'>window.alert('Please insert some Product to this tracking');</script>");
  192. return false;
  193. }
  194. }
  195. protected void btSave_Click(object o, EventArgs e)
  196. {
  197. if (Page.IsValid)
  198. {
  199. if (this.ffTrackingType.Value.ToString() == "Export" || this.ffTrackingType.Value.ToString() == "Remove")
  200. {
  201. if (!CheckProduct()) return;
  202. }
  203. else
  204. {
  205. DataTable dt = Session[this.hdfSessionName.Value] as DataTable;
  206. if (dt == null || dt.Rows.Count == 0)
  207. {
  208. this.Page.Response.Write("<script type='text/javascript'>window.alert('Please insert some Material to this tracking');</script>");
  209. return;
  210. }
  211. }
  212. if (SPContext.Current.FormContext.FormMode == SPControlMode.New)
  213. {
  214. SPContext.Current.Web.AllowUnsafeUpdates = true;
  215. SaveButton.SaveItem(SPContext.Current, false, string.Empty);
  216. if (this.ffTrackingType.Value.ToString() != "Remove")
  217. {
  218. UpdateProduct();
  219. }
  220. else
  221. {
  222. item["Status"] = "Director Review";
  223. Common.Common.SendEmail(web, Common.Common.GetEmailAddressesFromName("Director"), string.Empty, "[SIMS][Remove Invalid Material Request]&nbsp;" + item["Title"], this.BuildMailContent());
  224. }
  225. UpdateProductDetail();
  226. SPContext.Current.Item["Month"] = DateTime.Now.Month;
  227. SPContext.Current.Item["Year"] = DateTime.Now.Year;
  228. }
  229. SPContext.Current.Web.AllowUnsafeUpdates = true;
  230. SaveButton.SaveItem(SPContext.Current, false, string.Empty);
  231. this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
  232. }
  233. }
  234. protected void btApprove_Click(object o, EventArgs e)
  235. {
  236. if (CheckProduct())
  237. {
  238. SPContext.Current.Item["Status"] = "Approved";
  239. SPContext.Current.Item["Director"] = SPContext.Current.Web.CurrentUser;
  240. SPContext.Current.Item["Approved Date"] = DateTime.Now;
  241. SaveButton.SaveItem(SPContext.Current, false, string.Empty);
  242. Common.Common.SendEmail(web, Common.Common.GetEmailAddressesFromName(getUser("Created By").LoginName), string.Empty, "[SIMS][Remove Invalid Product Request][Approved]" + item["Title"], this.BuildMailContent());
  243. this.UpdateProduct();
  244. this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
  245. }
  246. }
  247. protected void btReject_Click(object o, EventArgs e)
  248. {
  249. SPContext.Current.Item["Status"] = "Rejected";
  250. SPContext.Current.Item["Director"] = SPContext.Current.Web.CurrentUser;
  251. SPContext.Current.Item["Approved Date"] = DateTime.Now;
  252. SaveButton.SaveItem(SPContext.Current, false, string.Empty);
  253. Common.Common.SendEmail(web, Common.Common.GetEmailAddressesFromName(getUser("Created By").LoginName), string.Empty, "[SIMS][Remove Invalid Product Request][Rejected]" + item["Title"], this.BuildMailContent());
  254. this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
  255. }
  256. protected SPUser getUser(string field)
  257. {
  258. string strUser = SPContext.Current.Item[field].ToString();
  259. string[] information = strUser.Split(';');
  260. SPUser user = SPContext.Current.Web.SiteUsers.GetByID(Convert.ToInt32(information[0]));
  261. return user;
  262. }
  263. private string BuildMailContent()
  264. {
  265. string content = string.Empty;
  266. string path = Request.Url.Authority + "/Lists/Product Tracking/DispForm.aspx?ID=" + SPContext.Current.Item["ID"].ToString();
  267. content += "<b>Title:</b>" + item["Title"] + "<br/>";
  268. content += "<b>Date:</b>" + item["Created"] + "<br/>";
  269. content += "<b>Requestor:</b>" + getUser("Created By").Name + "<br/>";
  270. content += "<b>Description:</b>" + item["Description"] + "<br/><hr/>";
  271. if (item["Status"].ToString() != "Director Review")
  272. {
  273. content += "<b>Director:</b>" + getUser("Director").Name + "<br/>";
  274. content += "<b>Date:</b>" + item["Approved Date"] + "<br/>";
  275. content += "<b>Comment:</b>" + item["Director Comment"] + "<br/>";
  276. }
  277. content += "<hr/>" + path;
  278. return content;
  279. }
  280. }
  281. }