private PortfoViewModel getPortfo(int id)
        {
            var             item  = _portfoRepository.GetAsQueryable(x => x.Id == id, includeProperties: "Company.CompanyType,PortfoShareholderPairs").FirstOrDefault();
            PortfoViewModel model = _mapper.Map <Portfo, PortfoViewModel>(item);

            model.CurrentCapital = _companyShareRepository.GetAsQueryable(x => x.CompanyId == item.CompanyId)
                                   .OrderByDescending(x => x.Id).Select(x => x.CurrentCapital).FirstOrDefault();
            model.CompanyTypeTitle = item.Company.CompanyType.Title;

            //&& x.ShareholderId == 47 && x.CompanyId == 19
            //var shareholderList = _companyShareholderRepository.GetAsQueryable(x => x.IncludeInFormula).GroupBy(woak => new { woak.CompanyId, woak.ShareholderId }).Select(g => g.OrderByDescending(c => c.Id).FirstOrDefault()).ToList();

            var shareholderList = _companyShareholderRepository.GetAsQueryableGroupBy(woak => new { woak.CompanyId, woak.ShareholderId }, x => x.IncludeInFormula).Select(g => g.OrderByDescending(c => c.Id).FirstOrDefault()).ToList();

            var allPossibleShareholders = shareholderList.Join(_companyRepository.GetAll(), a => a.CompanyId, b => b.Id, (a, b) => new { companiesShare = a, companies = b }).Join(_shareholderRepository.GetAll(), a => a.companiesShare.ShareholderId, b => b.Id, (a, b) => new { companiesShare2 = a, shares = b }).ToList();


            List <CompanyShareholderViewModel> fakes = new List <CompanyShareholderViewModel>();

            foreach (var woak in allPossibleShareholders)
            {
                //x => x.CompanyId == item.CompanyId && x.FormStatusFilter == KavoshFrameWorkCore.FormStatus.Verified).OrderByDescending(x => x.Date)
                var companyShare = _companyShareRepository
                                   .GetAsQueryable(x => x.CompanyId == woak.companiesShare2.companiesShare.CompanyId && x.FormStatus == KavoshFrameWorkCore.FormStatus.Verified).OrderByDescending(x => x.Date).FirstOrDefault();
                if (companyShare != null)
                {
                    var capitalChangePercentage = 100 * ((companyShare.CurrentCapital - companyShare.PreviousCapital) / (double)companyShare.PreviousCapital);
                    var totalShare          = companyShare.PreviousShares * (1 + (capitalChangePercentage) / 100);
                    var ownershipPercentage = (woak.companiesShare2.companiesShare.Shares / totalShare) * 100;
                    var sh = new CompanyShareholderViewModel
                    {
                        CompanyId           = woak.companiesShare2.companiesShare.CompanyId,
                        CompanyTitle        = woak.companiesShare2.companies.Title,
                        ShareholderTitle    = woak.shares.Title,
                        IsMainShareholder   = woak.companiesShare2.companiesShare.IsMainShareholder,
                        ShareholderId       = woak.companiesShare2.companiesShare.ShareholderId,
                        OwnershipPercentage = ownershipPercentage,
                    };
                    fakes.Add(sh);
                }
            }

            var items = new List <KeyValueViewModel>();

            foreach (var fake in fakes)
            {
                items.Add(new KeyValueViewModel {
                    Key = fake.CompanyId + "-" + fake.ShareholderId, Value = fake.ShareholderTitle + " در " + fake.CompanyTitle + " (" + Math.Round((fake.OwnershipPercentage / 100), 2) + ")"
                });
            }

            model.ValidShareholderTreeNodes = items;
            return(model);
        }
Example #2
0
        public async Task <IActionResult> Edit(CompanyShareholderViewModel model)
        {
            model.SystemUserId = UserId;

            if (!HasAccess(model.CompanyId))
            {
                return(View("Error"));
            }


            if (ModelState.IsValid)
            {
                if (model.IsMainShareholder)
                {
                    //var dup = await _companyShareholderRepository.GetAsync(woak => woak.IsMainShareholder && woak.CompanyId == model.CompanyId && !woak.IsDeleted);
                    //if (dup.Any())
                    //    return View("Error", new ErrorViewModel { RequestId = "این شرکت سهامدار اصلی دارد" });

                    IEnumerable <CompanyShareholder> finds = await _companyShareholderRepository.GetAsync(woak => woak.CompanyId == model.CompanyId);

                    foreach (CompanyShareholder woak in finds)
                    {
                        if (woak.Id != model.Id)
                        {
                            woak.IsMainShareholder = false;
                        }
                    }
                }

                var item   = _mapper.Map <CompanyShareholderViewModel, CompanyShareholder>(model);
                var result = await _companyShareholderRepository.UpdateAsync(item);

                var archive_item = _mapper.Map <CompanyShareholderViewModel, CompanyShareholderArchive>(model);
                archive_item.Id = 0;
                await _companyShareholderArchiveRepository.InsertAsync(archive_item);

                ErrorMessage = Resources.Messages.ChangesSavedSuccessfully;


                //await ReCalculatePortfo();

                if (Request.Form.Keys.Contains("SaveAndReturn"))
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(RedirectToAction("Edit", new { id = item.Id }));
                }
            }
            return(View(model));
        }
Example #3
0
        public async Task <IActionResult> Create(CompanyShareholderViewModel model)
        {
            model.SystemUserId = UserId;

            if (!HasAccess(model.CompanyId))
            {
                return(View("Error"));
            }

            if (ModelState.IsValid)
            {
                if (model.IsMainShareholder)
                {
                    IEnumerable <CompanyShareholder> finds = await _companyShareholderRepository.GetAsync(woak => woak.CompanyId == model.CompanyId);

                    foreach (CompanyShareholder woak in finds)
                    {
                        woak.IsMainShareholder = false;
                    }
                }

                var item = _mapper.Map <CompanyShareholderViewModel, CompanyShareholder>(model);
                await _companyShareholderRepository.InsertAsync(item);

                var archive_item = _mapper.Map <CompanyShareholderViewModel, CompanyShareholderArchive>(model);

                await _companyShareholderArchiveRepository.InsertAsync(archive_item);

                ErrorMessage = Resources.Messages.ChangesSavedSuccessfully;
                if (Request.Form.Keys.Contains("SaveAndReturn"))
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(RedirectToAction("Edit", new { id = item.Id }));
                }
            }
            return(View(model));
        }
        private List <PortfoResult> getPortfo2(int id)
        {
            var             item  = _portfoRepository.GetAsQueryable(x => x.Id == id, includeProperties: "Company.CompanyType,PortfoShareholderPairs").FirstOrDefault();
            PortfoViewModel model = _mapper.Map <Portfo, PortfoViewModel>(item);

            model.CurrentCapital = _companyShareRepository.GetAsQueryable(x => x.CompanyId == item.CompanyId)
                                   .OrderByDescending(x => x.Id).Select(x => x.CurrentCapital).FirstOrDefault();
            model.CompanyTypeTitle = item.Company.CompanyType.Title;


            //var shareholderList = _companyShareholderRepository.GetAsQueryable(x => x.IncludeInFormula).GroupBy(woak => new { woak.CompanyId, woak.ShareholderId }).Select(g => g.OrderByDescending(c => c.Id).FirstOrDefault()).ToList();

            var shareholderList = _companyShareholderRepository.GetAsQueryableGroupBy(woak => new { woak.CompanyId, woak.ShareholderId }, x => x.IncludeInFormula).Select(g => g.OrderByDescending(c => c.Id).FirstOrDefault()).ToList();


            var companies = _companyRepository.GetAll();

            var shareholders = _shareholderRepository.GetAll();

            var portfoShareholders = _portfoShareholderPairRepository.GetAsQueryable(woak => woak.PortfoId == id).OrderBy(p => p.Row).ToList();


            var allPossibleShareholders = portfoShareholders.Join(shareholderList, a => new { a.CompanyId, a.ShareholderId }, b => new { b.CompanyId, b.ShareholderId }, (a, b) => new { portfo = a, companiesShare = b }).Join(companies, a => a.companiesShare.CompanyId, b => b.Id, (a, b) => new { companiesShare = a, companies = b }).Join(shareholders, a => a.companiesShare.companiesShare.ShareholderId, b => b.Id, (a, b) => new { companiesShare2 = a, shares = b }).ToList();



            List <CompanyShareholderViewModel> fakes = new List <CompanyShareholderViewModel>();

            foreach (var woak in allPossibleShareholders)
            {
                var companyShare = _companyShareRepository
                                   .GetAsQueryable(x => x.CompanyId == woak.companiesShare2.companiesShare.companiesShare.CompanyId && x.FormStatus == KavoshFrameWorkCore.FormStatus.Verified).OrderByDescending(x => x.Date).FirstOrDefault();
                if (companyShare != null)
                {
                    var capitalChangePercentage = 100 * ((companyShare.CurrentCapital - companyShare.PreviousCapital) / (double)companyShare.PreviousCapital);
                    var totalShare          = companyShare.PreviousShares * (1 + (capitalChangePercentage) / 100);
                    var ownershipPercentage = (woak.companiesShare2.companiesShare.companiesShare.Shares / totalShare);
                    var sh = new CompanyShareholderViewModel
                    {
                        Id                  = woak.companiesShare2.companiesShare.portfo.Id,
                        CompanyId           = woak.companiesShare2.companiesShare.companiesShare.CompanyId,
                        CompanyTitle        = woak.companiesShare2.companies.Title,
                        ShareholderTitle    = woak.shares.Title,
                        IsMainShareholder   = woak.companiesShare2.companiesShare.companiesShare.IsMainShareholder,
                        ShareholderId       = woak.companiesShare2.companiesShare.companiesShare.ShareholderId,
                        OwnershipPercentage = ownershipPercentage,
                        Row                 = woak.companiesShare2.companiesShare.portfo.Row
                    };
                    fakes.Add(sh);
                }
            }

            List <PortfoResult> bitPush = new List <PortfoResult>();

            double total = 0;
            IEnumerable <IGrouping <int, CompanyShareholderViewModel> > group = fakes.GroupBy(woak => woak.Row);

            foreach (IGrouping <int, CompanyShareholderViewModel> woak in group)
            {
                int    row   = woak.Key;
                string id1   = string.Empty;
                string title = string.Empty;
                string value = string.Empty;
                double sum   = 1;

                foreach (var put in woak)
                {
                    id1 += put.Id;
                    string strVal = String.Format("{0:.##}", (put.OwnershipPercentage * 100));
                    title += put.ShareholderTitle + " در " + put.CompanyTitle + "(" + strVal + ")>> " + "<a class='btn btn-sm btn-danger' onclick='deletePortfo2(" + put.Id + ")'>x</a>" + "  ";
                    value += Math.Round((put.OwnershipPercentage / 100), 2).ToString() + "-";
                    sum   *= (put.OwnershipPercentage);
                }
                id1 = id1.Remove(id1.Length - 1);
                //title = title.Remove(title.Length - 1);2345
                value = value.Remove(value.Length - 1);

                total += sum;
                string strSum   = String.Format("{0:.##}", sum * 100);
                string strTotla = String.Format("{0:.##}", total);
                bitPush.Add(new PortfoResult {
                    Id = id1, Title = title, Value = value, Sum = strSum == "" ? "0" : strSum, Total = strTotla, Row = row
                });
            }
            item.AggregatePercentage = total;
            int update = _portfoRepository.Update(item);

            return(bitPush);
        }