Example #1
0
        public ServiceResult SaveProject(ObjProject obj)
        {
            try
            {
                var project = Map(obj);
                project.Residue = project.Cost;
                _projectRepository.Insert(project);
                _projectRepository.SaveChanges();

                //Добавить проверку на статус
                if (obj.RootType == RootTypes.Client)
                {
                    var client = _clientRepository.Get(obj.RootId);
                    client.Credit += obj.Cost * CurrencyConverter.ConvertValute(obj.Currency, CurrencyType.Rub);
                    _clientRepository.Update(client);
                    _clientRepository.SaveChanges();
                }

                return(ServiceResult.SuccessResult(project.Id));
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex.ToString());
                return(ServiceResult.ErrorResult("Ошибка сохранения проекта"));
            }
        }
Example #2
0
        public ServiceResult Add(ObjFinance objFinance)
        {
            try
            {
                var finance = Map(objFinance);
                _financeRepository.Insert(finance);
                _financeRepository.SaveChanges();
                _logger.LogDebug("Save Finance");

                if (finance.FinanceType == FinanceTypes.Expence)
                {
                    //проверяем на принадлежность к пользователю
                    if (finance.UserId != null)
                    {
                        var participant = _participantRepository.Get(finance.UserId.Value, finance.ProjectId, RootTypes.Project);
                        if (participant == null)
                        {
                            return(ServiceResult.ErrorResult("Добавьте участника"));
                        }

                        participant.Residue -= finance.Cost * CurrencyConverter.ConvertValute(finance.Currency, participant.Currency);

                        _participantRepository.Update(participant);
                        _participantRepository.SaveChanges();
                        _logger.LogDebug("Save participant");
                    }
                }
                //для поступления только один вариант
                if (finance.FinanceType == FinanceTypes.Receipt)
                {
                    var project = _projectRepository.Get(finance.ProjectId);
                    if (project == null)
                    {
                        return(ServiceResult.ErrorResult("Проект не найден"));
                    }
                    int ClientId = project.RootId;
                    var client   = _clientRepository.Get(ClientId);
                    if (client == null)
                    {
                        return(ServiceResult.ErrorResult("Клиент не найден"));
                    }

                    project.Residue -= finance.Cost * CurrencyConverter.ConvertValute(finance.Currency, project.Currency);
                    _projectRepository.Update(project);
                    _projectRepository.SaveChanges();
                    _logger.LogDebug("Save project");

                    client.Credit -= finance.Cost * CurrencyConverter.ConvertValute(finance.Currency, CurrencyType.Rub);
                    _clientRepository.Update(client);
                    _clientRepository.SaveChanges();
                    _logger.LogDebug("Save Client");
                }
                return(ServiceResult.SuccessResult());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(ServiceResult.ErrorResult("Ошибка сохранения"));
            }
        }
Example #3
0
        public ServiceResult Delete(int id)
        {
            try
            {
                var finance = _financeRepository.Get(id);
                if (finance.FinanceType == FinanceTypes.Expence)
                {
                    if (finance.UserId != null)
                    {
                        var paricipant = _participantRepository.Get(finance.UserId.Value, finance.ProjectId, RootTypes.Project);
                        if (paricipant == null)
                        {
                            return(ServiceResult.ErrorResult("Не найден сотрудник по расходу"));
                        }
                        paricipant.Residue += finance.Cost * CurrencyConverter.ConvertValute(finance.Currency, paricipant.Currency);

                        _participantRepository.Update(paricipant);
                        _participantRepository.SaveChanges();
                        _logger.LogInformation("participant Save");
                    }
                }
                if (finance.FinanceType == FinanceTypes.Receipt)
                {
                    var project = _projectRepository.Get(finance.ProjectId);
                    if (project == null)
                    {
                        return(ServiceResult.ErrorResult("Проект не найден"));
                    }
                    var client = _clientRepository.Get(project.RootId);
                    if (client == null)
                    {
                        return(ServiceResult.ErrorResult("Клиент не найден"));
                    }

                    project.Residue += finance.Cost * CurrencyConverter.ConvertValute(finance.Currency, project.Currency);
                    _projectRepository.Update(project);
                    _projectRepository.SaveChanges();
                    _logger.LogInformation("project Save");

                    client.Credit += finance.Cost * CurrencyConverter.ConvertValute(finance.Currency, CurrencyType.Rub);
                    _clientRepository.Update(client);
                    _clientRepository.SaveChanges();
                }
                _financeRepository.Delete(id);
                _financeRepository.SaveChanges();

                return(ServiceResult.SuccessResult());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(ServiceResult.ErrorResult("Ошибка удаления"));
            }
        }
Example #4
0
        public ServiceResult Delete(int id)
        {
            try
            {
                var project = _projectRepository.Get(id);

                if (project.RootType == RootTypes.Client)
                {
                    var client = _clientRepository.Get(project.RootId);
                    client.Credit -= project.Residue * CurrencyConverter.ConvertValute(project.Currency, CurrencyType.Rub);
                }
                _projectRepository.Delete(id);
                _projectRepository.SaveChanges();

                return(ServiceResult.SuccessResult());
            }
            catch (Exception ex)
            {
                _Logger.LogError("Delete project error ProjectId = {0} : {1}", id, ex);
                return(ServiceResult.ErrorResult("Ошибка при удалении проекта"));
            }
        }
Example #5
0
 public ServiceResult EditProject(ObjProject obj)
 {
     try
     {
         var project = _projectRepository.Get(obj.Id);
         UpdateMap(project, obj);
         _projectRepository.Update(project);
         _projectRepository.SaveChanges();
         if (obj.RootType == RootTypes.Client)
         {
             var client = _clientRepository.Get(obj.RootId);
             client.Credit += (obj.Cost * CurrencyConverter.ConvertValute(obj.Currency, CurrencyType.Rub)) -
                              (project.Cost * CurrencyConverter.ConvertValute(project.Currency, CurrencyType.Rub));
             _clientRepository.Update(client);
             _clientRepository.SaveChanges();
         }
         return(ServiceResult.SuccessResult(obj.Id));
     }
     catch (Exception ex)
     {
         _Logger.LogError("Edit Project error ProjectId = {0} : {1}", obj.Id, ex);
         return(ServiceResult.ErrorResult("Ошибка при сохранении проекта"));
     }
 }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="rootType"></param>
        /// <returns></returns>
        public ServiceResult <string> Delete(int Id, RootTypes rootType)
        {
            string ControllerName = "Home";

            switch (rootType)
            {
            case RootTypes.User:
                try
                {
                    //Удаляем пользователя из учасников проекта
                    var participants = _participantRepository.All().ToList();
                    for (int i = 0; i < participants.Count; i++)
                    {
                        if (participants[i].UserId == Id)
                        {
                            _participantRepository.Delete(participants[i]);
                        }
                    }
                    _participantRepository.SaveChanges();

                    _userRepository.Delete(Id);
                    _userRepository.SaveChanges();
                    ControllerName = "Users";
                }
                catch (Exception ex)
                {
                    return(ServiceResult <string> .ErrorResult("Ошибка при удалении сотрудника"));
                }
                break;

            case RootTypes.PotentialCLient:
                try
                {
                    _potentialClientRepository.Delete(Id);
                    _potentialClientRepository.SaveChanges();
                    ControllerName = "PotentialClients";
                }
                catch
                {
                    return(ServiceResult <string> .ErrorResult("Ошибка при удалении "));
                }
                break;

            case RootTypes.Client:
                try
                {
                    var projects = _projectRepository.All().ToList();
                    for (int i = 0; i < projects.Count; i++)
                    {
                        if (projects[i].RootType == RootTypes.Client && projects[i].RootId == Id)
                        {
                            _projectRepository.Delete(projects[i]);
                        }
                    }
                    _projectRepository.SaveChanges();
                    _clientRepository.Delete(Id);
                    _clientRepository.SaveChanges();
                    ControllerName = "Clients";
                }
                catch (Exception ex)
                {
                    return(ServiceResult <string> .ErrorResult("Ошибка при удалении клиента"));
                }
                break;

            case RootTypes.Project:
                try
                {
                    var project = _projectRepository.Get(Id);

                    if (project.RootType == RootTypes.Client)
                    {
                        var client = _clientRepository.Get(project.RootId);
                        client.Credit -= project.Residue * CurrencyConverter.ConvertValute(project.Currency, CurrencyType.Rub);
                    }
                    _projectRepository.Delete(Id);
                    _projectRepository.SaveChanges();
                    ControllerName = "Projects";
                }
                catch (Exception ex)
                {
                    return(ServiceResult <string> .ErrorResult("Ошбика при удалении проекта"));
                }
                break;
            }

            //Зачищаем модули


            return(ServiceResult <string> .SuccessResult(ControllerName));
        }