public virtual OrganizationFinPeriod GetOpenOrganizationFinPeriodInSubledger <TClosedInSubledgerField>(string orgFinPeriodID, int?branchID)
            where TClosedInSubledgerField : IBqlField
        {
            int?organizationID = PXAccess.GetParentOrganizationID(branchID);
            OrganizationFinPeriod orgFinPeriod = PXSelect <
                OrganizationFinPeriod,
                Where <OrganizationFinPeriod.finPeriodID, Equal <Required <OrganizationFinPeriod.finPeriodID> >,
                       And <OrganizationFinPeriod.organizationID, Equal <Required <OrganizationFinPeriod.organizationID> >,
                            And <TClosedInSubledgerField, NotEqual <True> > > > >
                                                 .SelectWindowed(Graph, 0, 1, orgFinPeriodID, organizationID);

            if (orgFinPeriod == null)
            {
                throw new PXException(
                          Messages.FiscalPeriodClosedInOrganization,
                          FinPeriodIDFormattingAttribute.FormatForError(orgFinPeriodID),
                          PXAccess.GetOrganizationCD(organizationID));
            }
            return(orgFinPeriod);
        }
        protected virtual OrganizationFinPeriod GetNearestOpenOrganizationFinPeriodInSubledgerByOrganization <TClosedInSubledgerField>(string orgFinPeriodID, int?organizationID, Func <bool> additionalCondition = null)
            where TClosedInSubledgerField : IBqlField
        {
            OrganizationFinPeriod orgFinPeriod = PXSelect <
                OrganizationFinPeriod,
                Where <OrganizationFinPeriod.finPeriodID, GreaterEqual <Required <OrganizationFinPeriod.finPeriodID> >,
                       And <OrganizationFinPeriod.organizationID, Equal <Required <OrganizationFinPeriod.organizationID> >,
                            And <TClosedInSubledgerField, NotEqual <True>,
                                 And <OrganizationFinPeriod.startDate, NotEqual <OrganizationFinPeriod.endDate> > > > >,
                OrderBy <Asc <OrganizationFinPeriod.finPeriodID> > >
                                                 .SelectWindowed(Graph, 0, 1, orgFinPeriodID, organizationID);

            if (orgFinPeriod == null && (additionalCondition == null || additionalCondition()))
            {
                throw new PXException(
                          Messages.NoOpenPeriodInOrganization,
                          PXAccess.GetOrganizationCD(organizationID));
            }
            return(orgFinPeriod);
        }
        public virtual OrganizationFinPeriod GetOpenOrganizationFinPeriodInFA(string orgFinPeriodID, int?assetID)
        {
            OrganizationFinPeriod orgFinPeriod = PXSelectJoin <
                OrganizationFinPeriod,
                InnerJoin <Branch,
                           On <Branch.organizationID, Equal <OrganizationFinPeriod.organizationID> >,
                           InnerJoin <FixedAsset,
                                      On <FixedAsset.assetID, Equal <Required <FixedAsset.assetID> >,
                                          And <FixedAsset.branchID, Equal <Branch.branchID> > > > >,
                Where <OrganizationFinPeriod.finPeriodID, Equal <Required <OrganizationFinPeriod.finPeriodID> >,
                       And <OrganizationFinPeriod.fAClosed, NotEqual <True> > > >
                                                 .SelectWindowed(Graph, 0, 1, assetID, orgFinPeriodID);

            if (orgFinPeriod == null)
            {
                throw new PXException(
                          Messages.FiscalPeriodClosed,
                          FinPeriodIDFormattingAttribute.FormatForError(orgFinPeriodID));
            }
            return(orgFinPeriod);
        }
        public virtual void VerifyAndSetFirstOpenedFinPeriod <TFinPeriodField, TBranchField>(PXCache rowCache, object row, PXSelectBase <OrganizationFinPeriod> finPeriodView, Type fieldModuleClosed = null)
            where TFinPeriodField : class, IBqlField
            where TBranchField : class, IBqlField
        {
            OrganizationFinPeriod finPeriod = finPeriodView.Current as OrganizationFinPeriod;

            if (finPeriod != null)
            {
                GLSetup glsetup = PXSetup <GLSetup> .Select(Graph);

                bool isClosed = finPeriod.Status == FinPeriods.TableDefinition.FinPeriod.status.Closed;

                if (fieldModuleClosed != null)
                {
                    isClosed |= (bool?)finPeriodView.Cache.GetValue(finPeriod, fieldModuleClosed.Name) == true;
                }

                if (finPeriod.Status == FinPeriod.status.Inactive ||
                    finPeriod.Status == FinPeriod.status.Locked ||
                    isClosed && glsetup != null && glsetup.RestrictAccessToClosedPeriods == true)
                {
                    string finPeriodID    = (string)rowCache.GetValue <TFinPeriodField>(row);
                    int?   organizationID = PXAccess.GetParentOrganizationID((int?)rowCache.GetValue <TBranchField>(row));

                    OrganizationFinPeriod firstopen = rowCache.Graph.GetService <IFinPeriodRepository>().FindFirstOpenFinPeriod(finPeriodID, organizationID, fieldModuleClosed);

                    if (firstopen == null)
                    {
                        string userPeriod = Mask.Format("##-####", finPeriodView.Cache.GetValueExt <OrganizationFinPeriod.finPeriodID>(finPeriodView.Current).ToString());
                        throw new PXSetPropertyException(GL.Messages.NoActivePeriodAfter, userPeriod);
                    }

                    rowCache.SetValue <TFinPeriodField>(row, firstopen.FinPeriodID);
                }
            }
        }