public async Task <OrphanageDataModel.FinancialData.Bail> Add(OrphanageDataModel.FinancialData.Bail bail)
        {
            try
            {
                bail.UserId = Program.CurrentUser.Id;
                var retBail = (OrphanageDataModel.FinancialData.Bail) await _apiClient.Bails_PostAsync(bail);
            }
            catch (ApiClientException apiEx)
            {
                var retObject = await _exceptionHandler.HandleApiPostFunctionsAndShowErrors(getBail, apiEx);

                if (retObject == null)
                {
                    retObject = await _exceptionHandler.HandleApiPostFunctions(getBail, apiEx);

                    if (retObject != null)
                    {
                        _mainViewModel.ShowBailEditView(retObject.Id);
                    }
                    return(null);
                }
                else
                {
                    return(retObject);
                }
            }

            return(null);
        }
Esempio n. 2
0
        public async Task <IEnumerable <OrphanageDataModel.FinancialData.Bail> > GetBails(int Uid, int pageSize, int pageNum)
        {
            IList <OrphanageDataModel.FinancialData.Bail> bailsList = new List <OrphanageDataModel.FinancialData.Bail>();

            using (var _orphanageDBC = new OrphanageDbCNoBinary())
            {
                int totalSkiped = pageSize * pageNum;
                int bailsCount  = await _orphanageDBC.Bails.AsNoTracking().CountAsync();

                if (bailsCount < totalSkiped)
                {
                    totalSkiped = bailsCount - pageSize;
                }
                if (totalSkiped < 0)
                {
                    totalSkiped = 0;
                }
                var bails = await _orphanageDBC.Bails.AsNoTracking()
                            .OrderBy(c => c.Id).Skip(() => totalSkiped).Take(() => pageSize)
                            .Include(b => b.Account)
                            .Include(b => b.Guarantor)
                            .Where(b => b.UserId == Uid)
                            .ToListAsync();

                foreach (var bail in bails)
                {
                    OrphanageDataModel.FinancialData.Bail bailsToFill = bail;
                    _selfLoopBlocking.BlockBailSelfLoop(ref bailsToFill);
                    bailsList.Add(bailsToFill);
                }
            }
            return(bailsList);
        }
        public async Task <OrphanageDataModel.FinancialData.Bail> getBail(int Bid)
        {
            var returnedBail = await _apiClient.Bails_GetAsync(Bid);

            _CurrentBail = returnedBail;
            return(returnedBail);
        }
Esempio n. 4
0
        public async Task <bool> SaveBail(OrphanageDataModel.FinancialData.Bail bailToSave)
        {
            _logger.Information($"Trying to save Bail");
            if (bailToSave == null)
            {
                _logger.Error($"the parameter object bailToSave is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (bailToSave.AccountID <= 0)
            {
                _logger.Error($"the AccountId of the parameter object bailToSave equals {bailToSave.AccountID}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (bailToSave.GuarantorID <= 0)
            {
                _logger.Error($"the GuarantorID of the parameter object bailToSave equals {bailToSave.GuarantorID}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (OrphanageDbCNoBinary orphanageDc = new OrphanageDbCNoBinary())
            {
                int ret = 0;
                orphanageDc.Configuration.LazyLoadingEnabled       = true;
                orphanageDc.Configuration.ProxyCreationEnabled     = true;
                orphanageDc.Configuration.AutoDetectChangesEnabled = true;

                var orginalBail = await orphanageDc.Bails.
                                  Include(m => m.Account).
                                  Include(c => c.Guarantor).
                                  FirstOrDefaultAsync(m => m.Id == bailToSave.Id);

                if (orginalBail == null)
                {
                    _logger.Error($"the original bail object with id {bailToSave.Id} object is not founded, ObjectNotFoundException will be thrown");
                    throw new Exceptions.ObjectNotFoundException();
                }

                orginalBail.AccountID     = bailToSave.AccountID;
                orginalBail.Amount        = bailToSave.Amount;
                orginalBail.EndDate       = bailToSave.EndDate;
                orginalBail.GuarantorID   = bailToSave.GuarantorID;
                orginalBail.IsExpired     = bailToSave.IsExpired;
                orginalBail.IsFamilyBail  = bailToSave.IsFamilyBail;
                orginalBail.IsMonthlyBail = bailToSave.IsMonthlyBail;
                orginalBail.StartDate     = bailToSave.StartDate;
                orginalBail.Note          = bailToSave.Note;
                ret += await orphanageDc.SaveChangesAsync();

                if (ret > 0)
                {
                    _logger.Information($"the object bail with id {orginalBail.Id} has been saved successfully, {ret} changes has been made ");
                    return(true);
                }
                else
                {
                    _logger.Information($"the object bail with id {orginalBail.Id} has been saved successfully, nothing has changed");
                    return(false);
                }
            }
        }
Esempio n. 5
0
        public async Task <OrphanageDataModel.FinancialData.Bail> AddBail(OrphanageDataModel.FinancialData.Bail bailToAdd)
        {
            _logger.Information($"Trying to add new Bail");
            if (bailToAdd == null)
            {
                _logger.Error($"the parameter object bailToAdd is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (bailToAdd.AccountID <= 0)
            {
                _logger.Error($"the AccountId of the parameter object bailToAdd equals {bailToAdd.AccountID}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (bailToAdd.GuarantorID <= 0)
            {
                _logger.Error($"the GuarantorID of the parameter object bailToAdd equals {bailToAdd.GuarantorID}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (var orphanageDBC = new OrphanageDbCNoBinary())
            {
                using (var Dbt = orphanageDBC.Database.BeginTransaction())
                {
                    if (bailToAdd.Account != null)
                    {
                        bailToAdd.Account = null;
                    }

                    if (bailToAdd.ActingUser != null)
                    {
                        bailToAdd = null;
                    }

                    if (bailToAdd.Guarantor != null)
                    {
                        bailToAdd.Guarantor = null;
                    }

                    orphanageDBC.Bails.Add(bailToAdd);
                    var ret = await orphanageDBC.SaveChangesAsync();

                    if (ret >= 1)
                    {
                        Dbt.Commit();
                        _logger.Information($"new bail object with id{bailToAdd.Id} has been added");
                        _selfLoopBlocking.BlockBailSelfLoop(ref bailToAdd);
                        _logger.Information($"the bail object with id{bailToAdd.Id} will be returned");
                        return(bailToAdd);
                    }
                    else
                    {
                        Dbt.Rollback();
                        _logger.Warning($"something went wrong, nothing was added, null will be returned");
                        return(null);
                    }
                }
            }
        }
        public async Task <bool> Save(OrphanageDataModel.FinancialData.Bail bail)
        {
            try
            {
                await _apiClient.Bails_PutAsync(bail);

                return(true);
            }
            catch (ApiClientException apiEx)
            {
                return(_exceptionHandler.HandleApiSaveException(apiEx));
            }
        }
Esempio n. 7
0
        public async Task <HttpResponseMessage> Post(object bail)
        {
            var bailEntity = JsonConvert.DeserializeObject <OrphanageDataModel.FinancialData.Bail>(bail.ToString());

            OrphanageDataModel.FinancialData.Bail ret = null;

            ret = await _bailDbService.AddBail(bailEntity);

            if (ret != null)
            {
                return(Request.CreateResponse(System.Net.HttpStatusCode.Created, ret));
            }
            else
            {
                return(_httpMessageConfiguerer.NothingChanged());
            }
        }
Esempio n. 8
0
        public async Task <IEnumerable <OrphanageDataModel.FinancialData.Bail> > GetBails(int Uid)
        {
            IList <OrphanageDataModel.FinancialData.Bail> bailsList = new List <OrphanageDataModel.FinancialData.Bail>();

            using (var _orphanageDBC = new OrphanageDbCNoBinary())
            {
                var bails = await _orphanageDBC.Bails.AsNoTracking()
                            .Include(b => b.Account)
                            .Include(b => b.Guarantor)
                            .Where(b => b.UserId == Uid)
                            .ToListAsync();

                foreach (var bail in bails)
                {
                    OrphanageDataModel.FinancialData.Bail bailsToFill = bail;
                    _selfLoopBlocking.BlockBailSelfLoop(ref bailsToFill);
                    bailsList.Add(bailsToFill);
                }
            }
            return(bailsList);
        }
Esempio n. 9
0
        private async void BailEditView_Load(object sender, EventArgs e)
        {
            if (_bailId != -1)
            {
                _bail = await _bailEditViewModel.getBail(_bailId);

                if (_bail != null)
                {
                    bailBindingSource.DataSource = _bail;
                    _CurrentGuarantorId          = _bail.GuarantorID;
                    _CurrentAccountId            = _bail.AccountID;
                    txtGuarantorName.Text        = _bail.Guarantor.Name.FullName();
                    lblCurrency.Text             = _bail.Account.CurrencyShortcut;
                    chkIsExpired.Checked         = _bail.IsExpired;
                    if (_bail.Guarantor.IsSupportingOnlyFamilies)
                    {
                        chkIsFamilyBail.Enabled = false;
                        chkIsFamilyBail.Checked = true;
                    }
                    if (_CurrentGuarantorId <= 0)
                    {
                        DisableEnableControls(false);
                    }
                    else
                    {
                        DisableEnableControls(true);
                    }
                }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
            else
            {
                bailBindingSource.DataSource = new OrphanageDataModel.FinancialData.Bail();
            }
        }
Esempio n. 10
0
        public void BlockBailSelfLoop(ref OrphanageDataModel.FinancialData.Bail bail)
        {
            if (bail == null)
            {
                return;
            }

            if (bail.Account != null)
            {
                bail.Account.Bails = null;
            }
            if (bail.Families != null)
            {
                foreach (var fam in bail.Families)
                {
                    fam.Bail    = null;
                    fam.Orphans = null;
                }
            }
            if (bail.Orphans != null)
            {
                foreach (var orp in bail.Orphans)
                {
                    orp.Bail = null;
                }
            }
            if (bail.Guarantor != null)
            {
                var gua = bail.Guarantor;
                if (gua.Bails != null)
                {
                    gua.Bails = null;
                }
                BlockGuarantorSelfLoop(ref gua);
                bail.Guarantor = gua;
            }
        }
Esempio n. 11
0
 private async void btnSave_Click(object sender, EventArgs e)
 {
     ((OrphanageDataModel.FinancialData.Bail)bailBindingSource.DataSource).IsExpired = chkIsExpired.Checked;
     _bailEntityValidator.controlCollection = Controls;
     _bailEntityValidator.DataEntity        = bailBindingSource.DataSource;
     if (_bailEntityValidator.IsValid())
     {
         _bail             = (OrphanageDataModel.FinancialData.Bail)bailBindingSource.DataSource;
         _bail.GuarantorID = _CurrentGuarantorId;
         _bail.AccountID   = _CurrentAccountId;
         if (chkNoTime.Checked)
         {
             _bail.EndDate   = null;
             _bail.StartDate = null;
         }
         if (_addNewBail)
         {
             if (await _bailEditViewModel.Add(_bail) != null)
             {
                 this.DialogResult = DialogResult.OK;
                 this.Close();
             }
         }
         else
         {
             if (await _bailEditViewModel.Save(_bail))
             {
                 this.DialogResult = DialogResult.OK;
                 this.Close();
             }
         }
     }
     else
     {
         ValidateAndShowError();
     }
 }
Esempio n. 12
0
        private async Task <IEnumerable <OrphanageDataModel.FinancialData.Bail> > prepareBailsList(IEnumerable <OrphanageDataModel.FinancialData.Bail> bailsList)
        {
            IList <OrphanageDataModel.FinancialData.Bail> returnedbailsList = new List <OrphanageDataModel.FinancialData.Bail>();

            if (bailsList != null && bailsList.Count() > 0)
            {
                foreach (var bail in bailsList)
                {
                    OrphanageDataModel.FinancialData.Bail bailsToFill = bail;
                    _selfLoopBlocking.BlockBailSelfLoop(ref bailsToFill);
                    bail.OrphansCount = await GetOrphansCount(bail.Id);

                    bail.FamiliesCount = await GetFamiliesCount(bail.Id);

                    returnedbailsList.Add(bailsToFill);
                }
            }
            else
            {
                _logger.Warning($"the returned bails are null, empty list will be returned");
            }
            _logger.Information($"{returnedbailsList.Count} records of bails will be returned");
            return(returnedbailsList);
        }
Esempio n. 13
0
        public async Task <bool> BailFamilies(int BailId, IList <int> familiesIds)
        {
            bool ret = true;

            _logger.Information($"trying to set Bail with id({BailId}) to the families with the given Id list");
            _logger.Information($"trying to get families with the given Id list");
            if (familiesIds == null || familiesIds.Count == 0)
            {
                _logger.Information($"the given Id list is null or empty, false will be returned");
                return(false);
            }
            using (var _orphanageDBC = new OrphanageDbCNoBinary())
            {
                _orphanageDBC.Configuration.AutoDetectChangesEnabled = true;
                _orphanageDBC.Configuration.LazyLoadingEnabled       = true;
                _orphanageDBC.Configuration.ProxyCreationEnabled     = true;

                var families = await _orphanageDBC.Families
                               .Where(o => familiesIds.Contains(o.Id))
                               .ToListAsync();

                if (families == null || families.Count == 0)
                {
                    _logger.Warning($"there is no families founded in the given ids list, false will be returned");
                    return(false);
                }
                OrphanageDataModel.FinancialData.Bail bail = null;
                if (BailId > 0)
                {
                    bail = await _orphanageDBC.Bails.AsNoTracking().FirstOrDefaultAsync(b => b.Id == BailId);

                    if (bail == null)
                    {
                        _logger.Error($"bail with id ({BailId}) has not been found, ObjectNotFoundException will be thrown");
                        throw new ObjectNotFoundException();
                    }
                }
                foreach (var fam in families)
                {
                    if (BailId > 0)
                    {
                        _logger.Information($"trying to set value ({BailId}) to bailId property for the family with the id ({fam.Id})");
                        fam.IsBailed = true;
                        fam.BailId   = BailId;
                    }
                    else
                    {
                        _logger.Information($"trying to set bailId property to NULL for the family with the id ({fam.Id})");
                        fam.IsBailed = false;
                        fam.BailId   = null;
                    }
                    if (await _orphanageDBC.SaveChangesAsync() > 0)
                    {
                        _logger.Information($"bailId property has been set successfully to the value ({BailId}) for the family with the id ({fam.Id})");
                    }
                    else
                    {
                        _logger.Warning($"bailId property has not been set to the value ({BailId}) for the family with the id ({fam.Id})");
                        ret = false;
                    }
                }
            }
            return(ret);
        }
Esempio n. 14
0
        public async Task <string> GetOrphanSummary(int Oid)
        {
            var orp = _SourceOrphans.FirstOrDefault(o => o.Id == Oid);

            if (orp == null)
            {
                return(string.Empty);
            }
            var brothersTask = _apiClient.Orphans_GetBrothersAsync(Oid);
            Task <OrphanageDataModel.FinancialData.Bail> bailTask       = null;
            Task <OrphanageDataModel.FinancialData.Bail> FamilyBailTask = null;

            if (orp.IsBailed)
            {
                bailTask = _apiClient.Bails_GetAsync(orp.BailId.Value);
            }
            if (orp.Family.IsBailed)
            {
                FamilyBailTask = _apiClient.Bails_GetAsync(orp.Family.BailId.Value);
            }
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine(Properties.Resources.FatherName + ": " + orp.Family.Father.Name.FullName());
            stringBuilder.AppendLine(Properties.Resources.FatherDeathDate + ": " + _dataFormatterService.GetFormattedDate(orp.Family.Father.DateOfDeath));
            stringBuilder.AppendLine(Properties.Resources.MotherFirstName + ": " + orp.Family.Mother.Name.FullName());
            stringBuilder.AppendLine(Properties.Resources.MotherIsDead + ": " + _translateService.BooleanToString(orp.Family.Mother.IsDead));
            if (orp.Family.Mother.IsDead && orp.Family.Mother.DateOfDeath.HasValue)
            {
                stringBuilder.AppendLine(Properties.Resources.MotherDeathDate + ": " + _dataFormatterService.GetFormattedDate(orp.Family.Mother.DateOfDeath.Value));
            }
            stringBuilder.AppendLine(Properties.Resources.MotherIsMarried + ": " + _translateService.BooleanToString(orp.Family.Mother.IsMarried));
            if (orp.Family.Mother.IsMarried && orp.Family.Mother.HusbandName != null && orp.Family.Mother.HusbandName.Length > 0)
            {
                stringBuilder.AppendLine(Properties.Resources.MotherHusbandName + ": " + orp.Family.Mother.HusbandName);
            }
            if (orp.Age.HasValue)
            {
                stringBuilder.AppendLine(Properties.Resources.Age + ": " + _translateService.DateToString(orp.Birthday));
            }
            var brothers = await brothersTask;

            if (brothers != null)
            {
                int boys  = brothers.Count(o => _translateService.IsBoy(o.Gender));
                int girls = brothers.Count(o => !_translateService.IsBoy(o.Gender));
                stringBuilder.AppendLine(Properties.Resources.BrothersCountString + ": " + boys + " " + Properties.Resources.MalesString + ", " + girls + " " + Properties.Resources.FemalesString);
            }
            else
            {
                stringBuilder.AppendLine(Properties.Resources.BrothersCountString + ": 0 " + Properties.Resources.MalesString + ", 0 " + Properties.Resources.FemalesString);
            }
            if (bailTask != null || FamilyBailTask != null)
            {
                stringBuilder.AppendLine(Properties.Resources.IsBailed + ": " + Properties.Resources.BooleanTrue);
                OrphanageDataModel.FinancialData.Bail orpBail = null;
                if (bailTask != null)
                {
                    orpBail = await bailTask;
                }
                if (FamilyBailTask != null)
                {
                    orpBail = await FamilyBailTask;
                }
                if (orpBail != null)
                {
                    stringBuilder.AppendLine(Properties.Resources.GuarantorName + ": " + orpBail?.Guarantor?.Name.FullName());
                    stringBuilder.AppendLine(Properties.Resources.BailIsFamily + ": " + _translateService.BooleanToString(orpBail.IsFamilyBail));
                    stringBuilder.AppendLine(Properties.Resources.BailAmount + ": " + orpBail.Amount.ToString() + " " + orpBail?.Account.CurrencyShortcut);
                    stringBuilder.AppendLine(Properties.Resources.BailIsMonthly + ": " + _translateService.BooleanToString(orpBail.IsMonthlyBail));
                    stringBuilder.AppendLine(Properties.Resources.BailIsEnded + ": " + _translateService.BooleanToString(orpBail.IsExpired));
                }
            }
            else
            {
                stringBuilder.AppendLine(Properties.Resources.IsBailed + ": " + Properties.Resources.BooleanFalse);
            }

            if (orp.EducationId.HasValue && orp.Education != null)
            {
                if (orp.Education.Stage.Contains(Properties.Resources.EducationNonStudyKeyword))
                {
                    stringBuilder.AppendLine(Properties.Resources.IsStudying + ": " + Properties.Resources.BooleanFalse);
                    if (orp.Education.Reasons != null && orp.Education.Reasons.Length > 0)
                    {
                        stringBuilder.AppendLine(Properties.Resources.EducationNonStudyingReasons + ": " + orp.Education.Reasons);
                    }
                }
                else
                {
                    stringBuilder.AppendLine(Properties.Resources.IsStudying + ": " + Properties.Resources.BooleanTrue);
                    if (orp.Education.Stage != null && orp.Education.Stage.Length > 0)
                    {
                        stringBuilder.AppendLine(Properties.Resources.EducationStage + ": " + orp.Education.Stage);
                    }
                    if (orp.Education.DegreesRate.HasValue)
                    {
                        stringBuilder.AppendLine(Properties.Resources.EducationAvaregeGrade + ": " + orp.Education.DegreesRate.Value + "%");
                    }
                    if (orp.Education.School != null && orp.Education.School.Length > 0)
                    {
                        stringBuilder.AppendLine(Properties.Resources.EducationSchoolName + ": " + orp.Education.School);
                    }
                }
            }
            else
            {
                stringBuilder.AppendLine(Properties.Resources.IsStudying + ": " + Properties.Resources.BooleanFalse);
            }
            if (orp.HealthId.HasValue && orp.HealthStatus != null)
            {
                stringBuilder.AppendLine(Properties.Resources.IsSick + ": " + Properties.Resources.BooleanTrue);
                if (orp.HealthStatus.SicknessName != null && orp.HealthStatus.SicknessName.Length > 0)
                {
                    stringBuilder.AppendLine(Properties.Resources.HealthSicknessName + ": " + orp.HealthStatus.SicknessName);
                }
                if (orp.HealthStatus.Medicine != null && orp.HealthStatus.Medicine.Length > 0)
                {
                    stringBuilder.AppendLine(Properties.Resources.HealthMedicen + ": " + orp.HealthStatus.Medicine);
                }
                if (orp.HealthStatus.Cost.HasValue)
                {
                    stringBuilder.AppendLine(Properties.Resources.Cost + ": " + orp.HealthStatus.Cost.Value);
                }
            }
            else
            {
                stringBuilder.AppendLine(Properties.Resources.IsSick + ": " + Properties.Resources.BooleanFalse);
            }
            return(stringBuilder.ToString());
        }