public async Task <IActionResult> Post([FromQuery] Int32 hid, Int32 docid) { // The post here is: // 1. Post a normal document with the content from this template doc // 2. Update the template doc with REFDOCID // Basic check if (hid <= 0 || docid <= 0) { return(BadRequest("No data inputted!")); } // Update the database SqlConnection conn = null; SqlCommand cmd = null; SqlDataReader reader = null; SqlTransaction tran = null; String queryString = String.Empty; String strErrMsg = String.Empty; FinanceTmpDocDPViewModel vmTmpDoc = new FinanceTmpDocDPViewModel(); HomeDefViewModel vmHome = new HomeDefViewModel(); FinanceDocumentUIViewModel vmFIDOC = new FinanceDocumentUIViewModel(); HttpStatusCode errorCode = HttpStatusCode.OK; String usrName = String.Empty; if (Startup.UnitTestMode) { usrName = UnitTestUtility.UnitTestUser; } else { var usrObj = HIHAPIUtility.GetUserClaim(this); usrName = usrObj.Value; } if (String.IsNullOrEmpty(usrName)) { return(BadRequest("User cannot recognize")); } try { using (conn = new SqlConnection(Startup.DBConnectionString)) { await conn.OpenAsync(); // Check: HID, it requires more info than just check, so it implemented it if (hid != 0) { String strHIDCheck = HIHDBUtility.getHomeDefQueryString() + " WHERE [ID]= @hid AND [USER] = @user"; cmd = new SqlCommand(strHIDCheck, conn); cmd.Parameters.AddWithValue("@hid", hid); cmd.Parameters.AddWithValue("@user", usrName); reader = await cmd.ExecuteReaderAsync(); if (!reader.HasRows) { errorCode = HttpStatusCode.BadRequest; throw new Exception("Not home found!"); } else { while (reader.Read()) { HIHDBUtility.HomeDef_DB2VM(reader, vmHome); // It shall be only one entry if found! break; } } reader.Dispose(); reader = null; cmd.Dispose(); cmd = null; } if (vmHome == null || String.IsNullOrEmpty(vmHome.BaseCurrency) || vmHome.ID != hid) { errorCode = HttpStatusCode.BadRequest; throw new Exception("Home Definition is invalid"); } // Check: DocID String checkString = HIHDBUtility.getFinanceDocADPListQueryString() + " WHERE [DOCID] = " + docid.ToString() + " AND [HID] = " + hid.ToString(); cmd = new SqlCommand(checkString, conn); reader = cmd.ExecuteReader(); if (!reader.HasRows) { errorCode = HttpStatusCode.BadRequest; throw new Exception("Invalid Doc ID inputted: " + docid.ToString()); } else { while (reader.Read()) { HIHDBUtility.FinTmpDocADP_DB2VM(reader, vmTmpDoc); // It shall be only one entry if found! break; } } reader.Dispose(); reader = null; cmd.Dispose(); cmd = null; // Check: Tmp doc has posted or not? if (vmTmpDoc == null || (vmTmpDoc.RefDocID.HasValue && vmTmpDoc.RefDocID.Value > 0)) { errorCode = HttpStatusCode.BadRequest; throw new Exception("Tmp Doc not existed yet or has been posted"); } if (!vmTmpDoc.ControlCenterID.HasValue && !vmTmpDoc.OrderID.HasValue) { errorCode = HttpStatusCode.BadRequest; throw new Exception("Tmp doc lack of control center or order"); } if (vmTmpDoc.TranAmount == 0) { errorCode = HttpStatusCode.BadRequest; throw new Exception("Tmp doc lack of amount"); } // Now go ahead for the creating tran = conn.BeginTransaction(); cmd = null; Int32 nNewDocID = 0; vmFIDOC.Desp = vmTmpDoc.Desp; vmFIDOC.DocType = FinanceDocTypeViewModel.DocType_Normal; vmFIDOC.HID = hid; //vmFIDOC.TranAmount = vmTmpDoc.TranAmount; vmFIDOC.TranCurr = vmHome.BaseCurrency; vmFIDOC.TranDate = vmTmpDoc.TranDate; vmFIDOC.CreatedAt = DateTime.Now; FinanceDocumentItemUIViewModel vmItem = new FinanceDocumentItemUIViewModel { AccountID = vmTmpDoc.AccountID }; if (vmTmpDoc.ControlCenterID.HasValue) { vmItem.ControlCenterID = vmTmpDoc.ControlCenterID.Value; } if (vmTmpDoc.OrderID.HasValue) { vmItem.OrderID = vmTmpDoc.OrderID.Value; } vmItem.Desp = vmTmpDoc.Desp; vmItem.ItemID = 1; vmItem.TranAmount = vmTmpDoc.TranAmount; vmItem.TranType = vmTmpDoc.TranType; vmFIDOC.Items.Add(vmItem); // Now go ahead for the creating queryString = HIHDBUtility.GetFinDocHeaderInsertString(); // Header cmd = new SqlCommand(queryString, conn) { Transaction = tran }; HIHDBUtility.BindFinDocHeaderInsertParameter(cmd, vmFIDOC, usrName); SqlParameter idparam = cmd.Parameters.AddWithValue("@Identity", SqlDbType.Int); idparam.Direction = ParameterDirection.Output; Int32 nRst = await cmd.ExecuteNonQueryAsync(); nNewDocID = (Int32)idparam.Value; vmFIDOC.ID = nNewDocID; cmd.Dispose(); cmd = null; // Then, creating the items foreach (FinanceDocumentItemUIViewModel ivm in vmFIDOC.Items) { queryString = HIHDBUtility.GetFinDocItemInsertString(); cmd = new SqlCommand(queryString, conn) { Transaction = tran }; HIHDBUtility.BindFinDocItemInsertParameter(cmd, ivm, nNewDocID); await cmd.ExecuteNonQueryAsync(); cmd.Dispose(); cmd = null; } // Then, update the template doc queryString = @"UPDATE [dbo].[t_fin_tmpdoc_dp] SET [REFDOCID] = @REFDOCID ,[UPDATEDBY] = @UPDATEDBY ,[UPDATEDAT] = @UPDATEDAT WHERE [HID] = @HID AND [DOCID] = @DOCID"; cmd = new SqlCommand(queryString, conn) { Transaction = tran }; cmd.Parameters.AddWithValue("@REFDOCID", nNewDocID); cmd.Parameters.AddWithValue("@UPDATEDBY", usrName); cmd.Parameters.AddWithValue("@UPDATEDAT", DateTime.Now); cmd.Parameters.AddWithValue("@HID", hid); cmd.Parameters.AddWithValue("@DOCID", docid); await cmd.ExecuteNonQueryAsync(); tran.Commit(); // Update the buffer of the relevant Account! var cacheAccountKey = String.Format(CacheKeys.FinAccount, hid, vmTmpDoc.AccountID); this._cache.Remove(cacheAccountKey); } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif if (tran != null) { tran.Rollback(); } strErrMsg = exp.Message; if (errorCode == HttpStatusCode.OK) { errorCode = HttpStatusCode.InternalServerError; } } finally { if (tran != null) { tran.Dispose(); tran = null; } if (reader != null) { reader.Dispose(); reader = null; } if (cmd != null) { cmd.Dispose(); cmd = null; } if (conn != null) { conn.Dispose(); conn = null; } } if (errorCode != HttpStatusCode.OK) { switch (errorCode) { case HttpStatusCode.Unauthorized: return(Unauthorized()); case HttpStatusCode.NotFound: return(NotFound()); case HttpStatusCode.BadRequest: return(BadRequest(strErrMsg)); default: return(StatusCode(500, strErrMsg)); } } var setting = new Newtonsoft.Json.JsonSerializerSettings { DateFormatString = HIHAPIConstants.DateFormatPattern, ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; return(new JsonResult(vmFIDOC, setting)); }
public async Task <IActionResult> Get([FromQuery] Int32 hid, Boolean skipPosted = true, DateTime?dtbgn = null, DateTime?dtend = null) { if (hid <= 0) { return(BadRequest("No HID inputted")); } String usrName = String.Empty; if (Startup.UnitTestMode) { usrName = UnitTestUtility.UnitTestUser; } else { var usrObj = HIHAPIUtility.GetUserClaim(this); usrName = usrObj.Value; } if (String.IsNullOrEmpty(usrName)) { return(BadRequest("User cannot recognize")); } List <FinanceTmpDocDPViewModel> listVm = new List <FinanceTmpDocDPViewModel>(); SqlConnection conn = null; SqlCommand cmd = null; SqlDataReader reader = null; String queryString = ""; String strErrMsg = ""; HttpStatusCode errorCode = HttpStatusCode.OK; try { queryString = HIHDBUtility.getFinanceDocADPListQueryString() + " WHERE [HID] = @hid "; if (skipPosted) { queryString += " AND [REFDOCID] IS NULL "; } if (dtbgn.HasValue) { queryString += " AND [TRANDATE] >= @dtbgn "; } if (dtend.HasValue) { queryString += " AND [TRANDATE] <= @dtend "; } queryString += " ORDER BY [TRANDATE] DESC"; using (conn = new SqlConnection(Startup.DBConnectionString)) { await conn.OpenAsync(); // Check Home assignment with current user try { HIHAPIUtility.CheckHIDAssignment(conn, hid, usrName); } catch (Exception) { errorCode = HttpStatusCode.BadRequest; throw; } cmd = new SqlCommand(queryString, conn); cmd.Parameters.AddWithValue("@hid", hid); if (dtbgn.HasValue) { cmd.Parameters.AddWithValue("@dtbgn", dtbgn.Value); } if (dtbgn.HasValue) { cmd.Parameters.AddWithValue("@dtend", dtend.Value); } reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { FinanceTmpDocDPViewModel dpvm = new FinanceTmpDocDPViewModel(); HIHDBUtility.FinTmpDocADP_DB2VM(reader, dpvm); listVm.Add(dpvm); } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); strErrMsg = exp.Message; if (errorCode == HttpStatusCode.OK) { errorCode = HttpStatusCode.InternalServerError; } } finally { if (reader != null) { reader.Dispose(); reader = null; } if (cmd != null) { cmd.Dispose(); cmd = null; } if (conn != null) { conn.Dispose(); conn = null; } } if (errorCode != HttpStatusCode.OK) { switch (errorCode) { case HttpStatusCode.Unauthorized: return(Unauthorized()); case HttpStatusCode.NotFound: return(NotFound()); case HttpStatusCode.BadRequest: return(BadRequest(strErrMsg)); default: return(StatusCode(500, strErrMsg)); } } var setting = new Newtonsoft.Json.JsonSerializerSettings { DateFormatString = HIHAPIConstants.DateFormatPattern, ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; return(new JsonResult(listVm, setting)); }
public async Task <IActionResult> Get([FromRoute] int id, [FromQuery] Boolean isADP = true, Int32 hid = 0) { if (hid <= 0) { return(BadRequest("Not HID inputted")); } String usrName = String.Empty; if (Startup.UnitTestMode) { usrName = UnitTestUtility.UnitTestUser; } else { var usrObj = HIHAPIUtility.GetUserClaim(this); usrName = usrObj.Value; } if (String.IsNullOrEmpty(usrName)) { return(BadRequest("User cannot recognize")); } FinanceADPDocumentUIViewModel vm = new FinanceADPDocumentUIViewModel(); SqlConnection conn = null; SqlCommand cmd = null; SqlDataReader reader = null; String queryString = ""; String strErrMsg = ""; HttpStatusCode errorCode = HttpStatusCode.OK; try { queryString = HIHDBUtility.getFinanceDocADPQueryString(id, hid, isADP); using (conn = new SqlConnection(Startup.DBConnectionString)) { await conn.OpenAsync(); // Check Home assignment with current user try { HIHAPIUtility.CheckHIDAssignment(conn, hid, usrName); } catch (Exception) { errorCode = HttpStatusCode.BadRequest; throw; } cmd = new SqlCommand(queryString, conn); reader = cmd.ExecuteReader(); if (!reader.HasRows) { errorCode = HttpStatusCode.NotFound; throw new Exception(); } // Header while (reader.Read()) { HIHDBUtility.FinDocHeader_DB2VM(reader, vm); } reader.NextResult(); // Items while (reader.Read()) { FinanceDocumentItemUIViewModel itemvm = new FinanceDocumentItemUIViewModel(); HIHDBUtility.FinDocItem_DB2VM(reader, itemvm); vm.Items.Add(itemvm); } reader.NextResult(); // Account while (reader.Read()) { FinanceAccountUIViewModel vmAccount = new FinanceAccountUIViewModel(); Int32 aidx = 0; aidx = HIHDBUtility.FinAccountHeader_DB2VM(reader, vmAccount, aidx); vmAccount.ExtraInfo_ADP = new FinanceAccountExtDPViewModel(); HIHDBUtility.FinAccountADP_DB2VM(reader, vmAccount.ExtraInfo_ADP, aidx); vm.AccountVM = vmAccount; } reader.NextResult(); // Tmp docs while (reader.Read()) { FinanceTmpDocDPViewModel dpvm = new FinanceTmpDocDPViewModel(); HIHDBUtility.FinTmpDocADP_DB2VM(reader, dpvm); vm.AccountVM.ExtraInfo_ADP.DPTmpDocs.Add(dpvm); } reader.NextResult(); // Tag if (reader.HasRows) { while (reader.Read()) { Int32 itemID = reader.GetInt32(0); String sterm = reader.GetString(1); foreach (var vitem in vm.Items) { if (vitem.ItemID == itemID) { vitem.TagTerms.Add(sterm); } } } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); strErrMsg = exp.Message; if (errorCode == HttpStatusCode.OK) { errorCode = HttpStatusCode.InternalServerError; } } finally { if (reader != null) { reader.Dispose(); reader = null; } if (cmd != null) { cmd.Dispose(); cmd = null; } if (conn != null) { conn.Dispose(); conn = null; } } if (errorCode != HttpStatusCode.OK) { switch (errorCode) { case HttpStatusCode.Unauthorized: return(Unauthorized()); case HttpStatusCode.NotFound: return(NotFound()); case HttpStatusCode.BadRequest: return(BadRequest(strErrMsg)); default: return(StatusCode(500, strErrMsg)); } } var setting = new Newtonsoft.Json.JsonSerializerSettings { DateFormatString = HIHAPIConstants.DateFormatPattern, ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; return(new JsonResult(vm, setting)); }