private int SaveLead(Models.LeadModels.NewLead Model)
        {
            int siteCoID = siteusercompanyid;

            var lead = new ProjectInfo()
            {
                ProjectID = Model.LeadID ?? 0,
                SiteCoID  = siteCoID,
                //ProjectTypeID = Model.TypeID,
                ProjectName     = Model.LeadName,
                ProjectNumber   = Model.JobNumber ?? "",
                ProjectStatusID = Model.StatusID,
                ContactID       = Model.ClientID,
                ProjectAddress1 = Model.Address1,
                ProjectAddress2 = Model.Address2,
                ProjectCity     = Model.City,
                ProjectState    = Model.State,
                ProjectCountry  = Model.Country,
                ProjectZip      = Model.Zip,
                ProjectPhone    = Model.Phone,
                ProjectEmail    = Model.Email,
                DateUploaded    = DateTime.Now,
                BuilderID       = Model.BuilderID,
                Site            = Model.Site,
                Lot             = Model.Lot,
                SalesID         = Model.SalesPersonID,
                ProjectTypeID   = 6
            };

            ProjectLeadInfo pli = new ProjectLeadInfo()
            {
                SiteCoID          = siteCoID,
                ProjectID         = Model.LeadID,
                StatusID          = Model.StatusID,
                LeadTypeID        = Model.TypeID,
                LeadBudget        = Model.Budget,
                RatingID          = Model.PriorityID,
                LeadProbabilityID = Model.ProbabilityID,
                LeadCloseDate     = Model.CloseDate,
                StageID           = Model.LeadPhaseID,
                LeadSystemTypeID  = Model.SystemID,
                LeadDateModified  = DateTime.Now
            };

            using (TransactionScope tran = new TransactionScope())
            {
                var projectId = repo.SaveProjectInfo(lead);
                var pliId     = repo.AddProjectLeadInfo(pli, projectId);
                repo.SaveProjectCommunications(projectId, Model.ProjectCommunicationIDs.Where(p => p > 0));

                tran.Complete();

                return(projectId);
            }
        }
        public async Task <ActionResult> Create(Models.LeadModels.NewLead Model)
        {
            var errorList = new List <string>();

            if (ModelState.IsValid)
            {
                int projectId = SaveLead(Model);
                if (projectId > 0)
                {
                    var ProjectInfo = db.GetProjectInfoByProjectID(projectId).FirstOrDefault();
                    var dbx         = DropboxConnection();
                    if (dbx != null)
                    {
                        var full = await dbx.Users.GetCurrentAccountAsync();

                        var list = await dbx.Files.ListFolderAsync(string.Empty);

                        bool flag = true;
                        // get folders and sub folders
                        foreach (var item in list.Entries.Where(i => i.IsFolder))
                        {
                            if (item.Name == ProjectInfo.Project)
                            {
                                flag = false;
                            }
                        }
                        if (flag == true)
                        {
                            await dbx.Files.CreateFolderAsync("/" + ProjectInfo.Project, true);
                        }
                    }
                    return(Json(new { status = "success", leadId = projectId }));
                }
                else
                {
                    errorList.Add("Lead couldn't be saved. Please retry.");
                }
            }

            errorList.AddRange((from item in ModelState.Values
                                from error in item.Errors
                                select error.ErrorMessage).ToList()
                               );

            return(Json(new { status = "error", errors = errorList }));
        }