Ejemplo n.º 1
0
        public bool ChangeStage(int id, int stageId, int modifiedUser)
        {
            var dbDeal  = db.DEALs.Find(id);
            var dbStage = db.STAGEs.Find(stageId);

            if (dbDeal != null && dbStage != null)
            {
                var histories = dbDeal.STAGE_HISTORY.OrderByDescending(sh => sh.ModifiedAt);
                if (histories.Count() > 0)
                {
                    var current = histories.FirstOrDefault();
                    if (current.STAGE.ID == stageId)
                    {
                        return(false);
                    }
                }
                dbDeal.ExpectedRevenue = (long)((dbDeal.Amount * dbStage.Probability) / 100);

                var newStageHistory = new STAGE_HISTORY();
                newStageHistory.ModifiedBy = modifiedUser;
                newStageHistory.ModifiedAt = DateTime.Now;
                newStageHistory.STAGE_ID   = stageId;
                newStageHistory.DEAL_ID    = dbDeal.ID;
                if (stageId == (int)EnumStage.LOST)
                {
                    dbDeal.isLost      = true;
                    dbDeal.ClosingDate = newStageHistory.ModifiedAt;
                }
                else
                {
                    dbDeal.isLost = false;
                }
                if (stageId == (int)EnumStage.WON)
                {
                    dbDeal.ClosingDate = newStageHistory.ModifiedAt;
                }

                db.STAGE_HISTORY.Add(newStageHistory);
                db.SaveChanges();

                var owner      = db.USERs.Find(dbDeal.DealOwner);
                var modifyUser = db.USERs.Find(modifiedUser);
                var creator    = db.USERs.Find(dbDeal.CreatedBy);

                var notifyModel = new NotificationApiModel();
                notifyModel.title          = "Stage updated";
                notifyModel.content        = $"Deal {dbDeal.Name}'s stage has been updated by {modifyUser?.Username}.";
                notifyModel.createdAt      = DateTime.Now;
                notifyModel.module         = "deals";
                notifyModel.moduleObjectId = dbDeal.ID;
                NotificationManager.SendNotification(notifyModel, new List <USER> {
                    owner, creator
                });
                NotificationManager.ReloadDashboardSale();

                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public bool Update(int dealId, DealCreateApiModel apiModel, int modifiedUser)
        {
            var dbDeal = db.DEALs.Find(dealId);

            if (dbDeal != null)
            {
                if (apiModel.account != 0)
                {
                    dbDeal.ACCOUNT_ID = apiModel.account;
                }
                dbDeal.Amount = apiModel.amount;
                if (apiModel.closingDate != null)
                {
                    dbDeal.ClosingDate = DbDateHelper.ToNullIfTooEarlyForDb(apiModel.closingDate.Value);
                }
                if (apiModel.expectedClosingDate != null)
                {
                    dbDeal.ExpectedClosingDate = DbDateHelper.ToNullIfTooEarlyForDb(apiModel.expectedClosingDate.Value);
                }
                if (apiModel.contact != 0)
                {
                    dbDeal.Contact_ID = apiModel.contact;
                }
                if (apiModel.owner != 0)
                {
                    dbDeal.DealOwner = apiModel.owner;
                }
                dbDeal.Description     = apiModel.description;
                dbDeal.ExpectedRevenue = apiModel.expectedRevenue;
                if (apiModel.stage != 0)
                {
                    var dbStage = db.STAGEs.Find(apiModel.stage);
                    dbDeal.ExpectedRevenue = (long)((apiModel.amount * dbStage.Probability) / 100);
                    var newStageHistory = new STAGE_HISTORY();
                    newStageHistory.ModifiedBy = modifiedUser;
                    newStageHistory.ModifiedAt = DateTime.Now;
                    newStageHistory.STAGE_ID   = apiModel.stage;
                    if (apiModel.stage == (int)EnumStage.LOST)
                    {
                        dbDeal.isLost      = true;
                        dbDeal.ClosingDate = newStageHistory.ModifiedAt;
                    }
                    else
                    {
                        dbDeal.isLost = false;
                    }
                    if (apiModel.stage == (int)EnumStage.WON)
                    {
                        dbDeal.ClosingDate = newStageHistory.ModifiedAt;
                    }
                    dbDeal.STAGE_HISTORY.Add(newStageHistory);
                    if (!String.IsNullOrEmpty(apiModel.lostReason))
                    {
                        //find lost reason
                        var lostReason = db.LOST_REASON.Where(c => c.Reason.ToLower().Contains(apiModel.lostReason.ToLower())).FirstOrDefault();
                        if (lostReason == null)
                        {
                            var newLostReason = new LOST_REASON();
                            newLostReason.Reason = apiModel.lostReason;
                            db.LOST_REASON.Add(newLostReason);
                            dbDeal.LOST_REASON = newLostReason;
                            //newLostReason.DEALs.Add(dbDeal);
                        }
                        else
                        {
                            dbDeal.LOST_REASON_ID = lostReason.ID;
                            //lostReason.DEALs.Add(dbDeal);
                        }
                        //dbDeal.LOST_REASON_ID = apiModel.lostReason;
                    }
                }
                dbDeal.Name = apiModel.name;
                if (apiModel.priority != 0)
                {
                    dbDeal.PRIORITY_ID = apiModel.priority;
                }

                if (apiModel.campaign != 0)
                {
                    dbDeal.CAMPAIGN_ID = apiModel.campaign;
                }
                if (apiModel.contact != 0)
                {
                    dbDeal.Contact_ID = apiModel.contact;
                }

                dbDeal.ModifiedAt = DateTime.Now;
                dbDeal.ModifiedBy = modifiedUser;
                db.SaveChanges();

                var owner      = db.USERs.Find(dbDeal.DealOwner);
                var modifyUser = db.USERs.Find(modifiedUser);
                var creator    = db.USERs.Find(dbDeal.CreatedBy);

                var notifyModel = new NotificationApiModel();
                notifyModel.title          = "Deal updated";
                notifyModel.content        = $"Deal {dbDeal.Name} has been updated by {modifyUser?.Username}.";
                notifyModel.createdAt      = DateTime.Now;
                notifyModel.module         = "deals";
                notifyModel.moduleObjectId = dbDeal.ID;
                NotificationManager.SendNotification(notifyModel, new List <USER> {
                    owner, creator
                });
                NotificationManager.ReloadDashboardSale();

                return(true);
            }
            else
            {
                return(false);
            }
        }