//public long CreateCOA(string headFor, long ParentCOAID) //{ // long COAID = ParentCOAID; // //var query=from x in // return COAID; //} public long CreateCOA(string headFor, long coaid) { try { LoginUser loginUser = new LoginUser(); long COAID = 0; long? CompanyID = loginUser.CompanyID; var query = (from x in db.AccCOAMappings from y in db.AccCOAs .Where(p => (p.COAID == x.AccCOAID)) .DefaultIfEmpty() select new { x.AccCOAID, x.Prefix, x.Suffix, x.CreateChild, y.COACode, y.COAName, y.HasChild, y.BalanceType, y.AccTypeID, y.CompanyID, y.CompanyBranchID, x.AccCOAConfigID }).Where(c => c.AccCOAConfigID == coaid && c.CompanyID == CompanyID).SingleOrDefault(); if (query != null) { if (query.CreateChild == true) { int? MaxCode = Convert.ToInt32((db.AccCOAs.Where(r => r.ParentCOAID == query.AccCOAID).Select(r => r.COACode).ToList()).Max()); AccCOA accCOA = new AccCOA(); accCOA.AccTypeID = query.AccTypeID; accCOA.BalanceType = query.BalanceType; accCOA.CompanyID = query.CompanyID; accCOA.CompanyBranchID = query.CompanyBranchID; accCOA.COAName = query.Prefix + headFor + query.Suffix + ":" + query.COAName; accCOA.COACode = MaxCode > 0 ? (MaxCode + 1).ToString() : query.COACode.ToString() + "001"; accCOA.ParentCOAID = (long)query.AccCOAID; db.AccCOAs.Add(accCOA); db.SaveChanges(); COAID = accCOA.COAID; } else { COAID = Convert.ToInt64(query.AccCOAID); } } return COAID; } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } }
// PUT api/AccCOA/5 public HttpResponseMessage PutAccCOA(long id, AccCOA acccoa) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != acccoa.COAID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } acccoa.UpdateBy = loginUser.UserID; db.Entry(acccoa).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
// POST api/AccCOA public HttpResponseMessage PostAccCOA(AccCOA acccoa) { if (ModelState.IsValid) { acccoa.InsertBy = loginUser.UserID; db.AccCOAs.Add(acccoa); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, acccoa); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = acccoa.COAID })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }