Ejemplo n.º 1
0
        // PUT api/Collaborator/5
        public HttpResponseMessage PutCollaborator(long id, Collaborator collaborator)
        {
            collaborator.Language = null;
            collaborator.Currency=null;
            collaborator.Country=null;
            collaborator.City = null;
            //collaborator.ReportTo = null;
            collaborator.Department = null;
            collaborator.Designation = null;
            collaborator.CustomerType = null;
            collaborator.SupplierType = null;
            collaborator.UpdateBy = loginUser.UserID;
            collaborator.UpdateDate = DateTime.Now.Date;
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != collaborator.CollaboratorID)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(collaborator).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
Ejemplo n.º 2
0
        // POST api/Collaborator
        public HttpResponseMessage PostCollaborator(Collaborator collaborator)
        {
            if (ModelState.IsValid)
            {
                if (WebSecurity.GetUserId(collaborator.EmailID)>0)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    //RegisterModel RM=new RegisterModel();
                    //RM.UserName=collaborator.Name;
                    //RM.Password="******";
                    ControlCOA controlCOA = new ControlCOA();

                    string CollaboratorCode = "";
                    //long COAID = 0;
                    if (collaborator.IsCustomer == true)
                    {
                        string prefix = db.CustomerTypes.Where(ct => ct.CustomerTypeID == collaborator.CustomerTypeID).Select(s => s.CustomerTypeShortName).SingleOrDefault();
                        CollaboratorCode = prefix+"-";
                        collaborator.CustomerCOAID = controlCOA.CreateCOA(collaborator.Title + " " + collaborator.Name, 5);
                    }
                    else if (collaborator.IsSupplier == true)
                    {
                        string prefix = db.SupplierTypes.Where(st => st.SupplierTypeID == collaborator.SupplierTypeID).Select(s => s.SupplierTypeShortName).SingleOrDefault();
                        CollaboratorCode = prefix+"-";
                        collaborator.SupplierCOAID = controlCOA.CreateCOA(collaborator.Title + " " + collaborator.Name, 10);
                    }
                    else if (collaborator.IsEmployee == true)
                    {
                        string prefix = db.Departments.Where(st => st.DepartmentID == collaborator.DepartmentID).Select(s => s.DepartmentCode).SingleOrDefault();
                        CollaboratorCode = prefix; //"EMP-" + DateTime.Now.ToString("yyyyMMdd");
                        collaborator.EmployeeCOAID = controlCOA.CreateCOA(collaborator.Title + " " + collaborator.Name, 11);
                    }

                    if (CollaboratorCode == "")
                    {
                        CollaboratorCode = "CP-";
                    }

                    int? MaxCode = Convert.ToInt32((db.Collaborators.Where(r => r.CollaboratorCode.StartsWith(CollaboratorCode)).Select(r => r.CollaboratorCode.Substring(CollaboratorCode.Length, 3)).ToList()).Max());
                    string CBCode = CollaboratorCode + ((MaxCode + 1).ToString()).PadLeft(3, '0');
                    collaborator.CollaboratorCode = CBCode;

                    WebSecurity.CreateUserAndAccount(collaborator.EmailID, "123456");
                    //AccountController acc = new AccountController();
                    //acc.Register(RM);
                    if (collaborator.IsEmployee == true)
                    {
                        collaborator.UserID = WebSecurity.GetUserId(collaborator.EmailID);
                    }
                    //WebSecurity.InitializeDatabaseConnection();
                    //WebSecurity.CreateUserAndAccount(, );
                    collaborator.SalesAssociateID = loginUser.UserID;
                    collaborator.InsertBy = loginUser.UserID;
                    collaborator.InsertDate = DateTime.Now.Date;
                    db.Collaborators.Add(collaborator);
                    db.SaveChanges();

                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, collaborator);
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = collaborator.CollaboratorID }));
                    return response;
                }

            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }