public async Task <IActionResult> Post([FromBody] FinancePlanViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Perform the checks
            if (vm.HID <= 0)
            {
                return(BadRequest("No HID inputted!"));
            }
            if (vm == null ||
                vm.StartDate > vm.TargetDate ||
                (vm.PlanType == FinancePlanTypeEnum.Account && (!vm.AccountID.HasValue || vm.AccountID.Value <= 0)) ||
                (vm.PlanType == FinancePlanTypeEnum.AccountCategory && (!vm.AccountCategoryID.HasValue || vm.AccountCategoryID.Value <= 0)) ||
                (vm.PlanType == FinancePlanTypeEnum.ControlCenter && (!vm.ControlCenterID.HasValue || vm.ControlCenterID.Value <= 0)) ||
                (vm.PlanType == FinancePlanTypeEnum.TranType && (!vm.TranTypeID.HasValue || vm.TranTypeID.Value <= 0))
                )
            {
                return(BadRequest("Invalid data to create"));
            }

            String usrName = "";

            try
            {
                if (Startup.UnitTestMode)
                {
                    usrName = UnitTestUtility.UnitTestUser;
                }
                else
                {
                    var usrObj = HIHAPIUtility.GetUserClaim(this);
                    usrName = usrObj.Value;
                }
            }
            catch
            {
                return(BadRequest("Not valid HTTP HEAD: User and Scope Failed!"));
            }

            // Update the database
            SqlConnection  conn        = null;
            SqlCommand     cmd         = null;
            SqlDataReader  reader      = null;
            SqlTransaction tran        = null;
            String         queryString = "";
            Int32          nNewPlanID  = -1;
            String         strErrMsg   = "";
            HttpStatusCode errorCode   = HttpStatusCode.OK;

            try
            {
                using (conn = new SqlConnection(Startup.DBConnectionString))
                {
                    await conn.OpenAsync();

                    // Check HID assignment
                    try
                    {
                        HIHAPIUtility.CheckHIDAssignment(conn, vm.HID, usrName);
                    }
                    catch (Exception)
                    {
                        errorCode = HttpStatusCode.BadRequest;
                        throw;
                    }

                    if (vm.PlanType == FinancePlanTypeEnum.Account)
                    {
                        // Check the account
                        queryString = @"SELECT [ID], [Status]
                            FROM [dbo].[t_fin_account] WHERE [ID] = " + vm.AccountID.Value.ToString() + " AND [HID] = " + vm.HID.ToString();

                        cmd    = new SqlCommand(queryString, conn);
                        reader = cmd.ExecuteReader();
                        if (!reader.HasRows)
                        {
                            errorCode = HttpStatusCode.BadRequest;
                            throw new Exception("Account doesnot exist: " + vm.AccountID.Value.ToString());
                        }
                        else
                        {
                            // Check the status
                            await reader.ReadAsync();

                            if (!reader.IsDBNull(1))
                            {
                                FinanceAccountStatus nAccountStatus = (FinanceAccountStatus)reader.GetByte(1);
                                if (nAccountStatus == FinanceAccountStatus.Frozen ||
                                    nAccountStatus == FinanceAccountStatus.Closed)
                                {
                                    errorCode = HttpStatusCode.BadRequest;
                                    throw new Exception("Account status is invalid: " + vm.AccountID.Value.ToString());
                                }
                            }
                        }
                        reader.Dispose();
                        reader = null;
                        cmd.Dispose();
                        cmd = null;

                        // Now create the DB entry
                        // Begin the transaction
                        tran = conn.BeginTransaction();

                        // Now go ahead for the creating
                        queryString = HIHDBUtility.GetFinPlanInsertString();

                        cmd = new SqlCommand(queryString, conn)
                        {
                            Transaction = tran
                        };

                        vm.CreatedBy = usrName;
                        vm.CreatedAt = DateTime.Now;
                        HIHDBUtility.BindFinPlanInsertParameter(cmd, vm);
                        SqlParameter idparam = cmd.Parameters.AddWithValue("@Identity", SqlDbType.Int);
                        idparam.Direction = ParameterDirection.Output;

                        Int32 nRst = await cmd.ExecuteNonQueryAsync();

                        nNewPlanID = (Int32)idparam.Value;

                        // Now commit it!
                        tran.Commit();

                        // Update the buffer
                        try
                        {
                            var cacheKey = String.Format(CacheKeys.FinPlanList, vm.HID);
                            this._cache.Remove(cacheKey);
                        }
                        catch (Exception)
                        {
                            // Do nothing here.
                        }
                    }
                    else if (vm.PlanType == FinancePlanTypeEnum.AccountCategory)
                    {
                    }
                    else if (vm.PlanType == FinancePlanTypeEnum.ControlCenter)
                    {
                    }
                    else if (vm.PlanType == FinancePlanTypeEnum.TranType)
                    {
                    }


                    // Update the buffer
                    // Account List
                    try
                    {
                        var cacheKey = String.Format(CacheKeys.FinPlanList, vm.HID, null);
                        this._cache.Remove(cacheKey);
                    }
                    catch (Exception)
                    {
                        // Do nothing here.
                    }
                }
            }
            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));
                }
            }

            vm.ID = nNewPlanID;
            var setting = new Newtonsoft.Json.JsonSerializerSettings
            {
                DateFormatString = HIHAPIConstants.DateFormatPattern,
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };

            return(new JsonResult(vm, setting));
        }