public ActionResult Index(CreditModel postedModel)
        {
            CreditModel creditModel = new CreditModel();

            try
            {      
                if (Request.Params["PageSize"] != null)
                {
                    //SessionManager.Set<int>(PAGE_SIZE_SESSION_KEY, Convert.ToInt32(Request.Params["PageSize"]));
                    SessionManager.Set<int>(UserBL.SelectedPageSizeSessionKey, Convert.ToInt32(Request.Params["PageSize"]));
                }
                else
                {
                    //Posted values
                    SessionManager.Set<Guid>(SelectedFirmCreditParentIdSessionKey, postedModel.IdSelectedFirmInstitutionParentCritaria);
                    SessionManager.Set<Guid>(SelectedFirmCreditFilsIdSessionKey, postedModel.IdSelectedFirmInstitutionFilsCritaria);
                    SessionManager.Set<String>(SelectedCreditCodeSessionKey, postedModel.SelectedCreditCodeCritaria);
                    SessionManager.Set<DateTime?>(SelectedDateDuSessionKey, postedModel.DateDu);
                    SessionManager.Set<DateTime?>(SelectedDateAuSessionKey, postedModel.DateAu);
                }

                creditModel = GetCreditModel();
            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
            }
            return View(creditModel);
        }
 public ActionResult Index()
 {
     CreditModel creditModel = new CreditModel();
     try
     {
         SessionManager.Set<DateTime?>(SelectedDateAuSessionKey, DateTime.Now);
         creditModel = GetCreditModel();
     }
     catch (Exception ex)
     {
         Upsilab.Business.Log.Log.AppendException(ex);
     }
     return View(creditModel);
 }
        private CreditModel GetCreditModel()
        {
            var LanguageData = PageLanguageHelper.GetLanguageContent("User", "Credit");
            CreditModel creditModel = new CreditModel();

            //FirmInstitution parent
            List<FirmInstitution> lstFirmParents = FirmInstitutionBL.GetFirmInstitutionParents();
            lstFirmParents.Insert(0, new FirmInstitution() { idFirmInstitution = GuidHelper.EmptyGuid(), FirmInstitutionName = LanguageData.GetContent("user_all_firminstitutions") });
            creditModel.FirmInstitutionParentsCritaria = lstFirmParents;

            Guid idFirmInstitutionParent = Guid.Empty;
            SessionManager.Get<Guid>(SelectedFirmCreditParentIdSessionKey, out idFirmInstitutionParent);
            
            if (idFirmInstitutionParent == Guid.Empty)
            {
                idFirmInstitutionParent = creditModel.FirmInstitutionParentsCritaria.Where(f => f.idFirmInstitution != GuidHelper.EmptyGuid()).FirstOrDefault().idFirmInstitution;
            }
            creditModel.IdSelectedFirmInstitutionParentCritaria = idFirmInstitutionParent;

            //FirmInstitions fils
            creditModel.FirmInstitutionFilsCritaria = FirmInstitutionBL.GetFirmInstitutionByIdFirmInstitutionParent(creditModel.IdSelectedFirmInstitutionParentCritaria);
            creditModel.FirmInstitutionFilsCritaria.Insert(0, new Data.Model.FirmInstitution() { idFirmInstitution = GuidHelper.EmptyGuid(), FirmInstitutionName = LanguageData.GetContent("user_all_firminstitutions") });

            Guid idFirmInstitutionFils = Guid.Empty;
            SessionManager.Get<Guid>(SelectedFirmCreditFilsIdSessionKey, out idFirmInstitutionFils);
            if (idFirmInstitutionFils == Guid.Empty)
            {
                idFirmInstitutionFils = GuidHelper.EmptyGuid(); //:)
            }
            creditModel.IdSelectedFirmInstitutionFilsCritaria = idFirmInstitutionFils;

            List<Guid> idSelectedFirmInstitutions = new List<Guid>(); //Parent and child
            if (creditModel.IdSelectedFirmInstitutionFilsCritaria != GuidHelper.EmptyGuid())
            {
                idSelectedFirmInstitutions.Add(creditModel.IdSelectedFirmInstitutionFilsCritaria); //Add child Id
            }
            else
            {
                if (creditModel.IdSelectedFirmInstitutionParentCritaria != GuidHelper.EmptyGuid())
                {
                    //Add selected Parent Id
                    idSelectedFirmInstitutions.Add(creditModel.IdSelectedFirmInstitutionParentCritaria);
                }
                else
                { 
                    //Add all parents id
                    var tempIdParents = creditModel.FirmInstitutionParentsCritaria.Select(f => f.idFirmInstitution);
                    idSelectedFirmInstitutions = idSelectedFirmInstitutions.Concat(tempIdParents).ToList();
                }

                var tempIdChildren = creditModel.FirmInstitutionFilsCritaria.Select(f => f.idFirmInstitution);
                idSelectedFirmInstitutions = idSelectedFirmInstitutions.Concat(tempIdChildren).ToList(); //List of children
            }

            //acte (CreditOperationCost)
            List<CreditOperationCost> lstCredit =CreditOperationBL.GetAllCreditOperationCost();
            lstCredit.Insert(0, new CreditOperationCost() { idCreditOperation = 0, OperationName = LanguageData.GetContent("credit_all_operationcost"), CreditCost = 0, CreditCode = string.Empty, DateCreated = DateTime.Now });
            creditModel.CreditOperationCostCritaria = lstCredit;

            String CreditCode = String.Empty;
            SessionManager.Get<String>(SelectedCreditCodeSessionKey, out CreditCode);
            if (string.IsNullOrEmpty(CreditCode) || CreditCode == String.Empty)
            {
                CreditCode = creditModel.CreditOperationCostCritaria.FirstOrDefault().CreditCode;
            }
            creditModel.SelectedCreditCodeCritaria = CreditCode;
            //Date du
            DateTime? datedu = null;
            SessionManager.Get<DateTime?>(SelectedDateDuSessionKey, out datedu);
            if (datedu != null)
            {
                creditModel.DateDu = datedu.Value;
            }            
            //Date Au
            DateTime? dateau = null;
            SessionManager.Get<DateTime?>(SelectedDateAuSessionKey, out dateau);
            if (dateau != null)
            {
                creditModel.DateAu = dateau.Value;
            }


            List<CreditOperationLog> creditOperationLogs = CreditOperationBL.GetCreditOperationLogByCritaria(idSelectedFirmInstitutions, CreditCode, datedu, dateau);
            creditModel.CreditOperationLogResult = creditOperationLogs;

            //Sum credit
            creditModel.ConsumedCredit = CreditOperationBL.SumCredits(creditOperationLogs);

            creditModel.CreditCount = 0;
            if (idFirmInstitutionParent != null && idFirmInstitutionParent != Guid.Empty)
            {
                FirmInstitution f = FirmInstitutionBL.GetFirmInstitutionByIdFirmInstitution(idFirmInstitutionParent);
                if (f != null)
                {
                    creditModel.CreditCount = f.CreditCount.HasValue ? f.CreditCount.Value : 0;
                }

                creditModel.FirmInstitutionName = f.FirmInstitutionName;
            }
            
            //Page size
            creditModel.PageSize = GetPageSize();
            return creditModel;
        }