protected virtual void Branch_LedgerID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
        {
            Branch branch = e.Row as Branch;

            if (branch == null)
            {
                return;
            }

            Ledger ledger = GeneralLedgerMaint.FindLedgerByID(this, branch.LedgerID);

            BranchMaint.SetBranchParentID(branch, ledger?.DefBranchID);
        }
        protected virtual void OrganizationLedgerLink_RowInserting(PXCache cache, PXRowInsertingEventArgs e)
        {
            var link = e.Row as OrganizationLedgerLink;

            if (link == null)
            {
                return;
            }

            var ledger = GeneralLedgerMaint.FindLedgerByID(Base, link.LedgerID, isReadonly: false);

            if (ledger.BalanceType == LedgerBalanceType.Actual)
            {
                CheckActualLedgerCanBeAssigned(ledger, link.OrganizationID.SingleToArray());
            }
        }
        public virtual void CheckActualLedgerCanBeAssigned(Ledger ledger, int?[] organizationIDs)
        {
            IEnumerable <Organization> organizations = OrganizationMaint.FindOrganizationByIDs(Base, organizationIDs, isReadonly: false);

            foreach (Organization organization in organizations)
            {
                if (organization.ActualLedgerID != null && organization.ActualLedgerID != ledger.LedgerID)
                {
                    Ledger existingLedger = GeneralLedgerMaint.FindLedgerByID(Base, organization.ActualLedgerID, isReadonly: false);

                    throw new PXSetPropertyException(Messages.LedgerCannotBeAssociatedWithTheCompanyBecauseTheActualLedgerHasBeenAlreadyAssociated,
                                                     ledger.LedgerCD,
                                                     organization.OrganizationCD.Trim(),
                                                     existingLedger.LedgerCD);
                }
            }
        }
        protected virtual PXSetPropertyException CanBeLinkDeleted(OrganizationLedgerLink link)
        {
            Ledger ledger = GeneralLedgerMaint.FindLedgerByID(Base, link.LedgerID);

            if (ledger.BalanceType == LedgerBalanceType.Actual)
            {
                if (GLUtility.RelatedGLHistoryExists(Base, link.LedgerID, link.OrganizationID))
                {
                    Organization org = OrganizationMaint.FindOrganizationByID(Base, link.OrganizationID, true);
                    return(new PXSetPropertyException(Messages.TheRelationBetweenTheLedgerAndTheCompanyCannotBeRemovedBecauseAtLeastOneGeneralLedgerTransactionHasBeenPosted,
                                                      PXErrorLevel.RowError,
                                                      LinkCache.GetValueExt <OrganizationLedgerLink.ledgerID>(link),
                                                      org.OrganizationCD.Trim()
                                                      ));
                }
            }

            return(null);
        }
        public virtual IEnumerable deleteOrganizationLedgerLink(PXAdapter adapter)
        {
            var link = LinkCache.Current as OrganizationLedgerLink;

            if (link?.OrganizationID == null || link.LedgerID == null)
            {
                return(adapter.Get());
            }

            Ledger ledger = GeneralLedgerMaint.FindLedgerByID(Base, link.LedgerID);

            if (ledger.BalanceType == LedgerBalanceType.Actual)
            {
                LinkCache.Delete(link);
            }
            else
            {
                if (GLUtility.RelatedGLHistoryExists(Base, link.LedgerID, link.OrganizationID))
                {
                    Organization org = OrganizationMaint.FindOrganizationByID(Base, link.OrganizationID, true);

                    WebDialogResult dialogResult = OrganizationLedgerLinkSelect.Ask(PXMessages.LocalizeFormatNoPrefix(
                                                                                        Messages.AtLeastOneGeneralLedgerTransactionHasBeenPosted,
                                                                                        LinkCache.GetValueExt <OrganizationLedgerLink.ledgerID>(link).ToString().Trim(),
                                                                                        org.OrganizationCD.Trim()),
                                                                                    MessageButtons.YesNo);

                    if (dialogResult == WebDialogResult.Yes)
                    {
                        LinkCache.Delete(link);
                    }
                }
                else
                {
                    LinkCache.Delete(link);
                }
            }

            return(adapter.Get());
        }
        protected virtual void OrganizationLedgerLink_RowDeleted(PXCache cache, PXRowDeletedEventArgs e)
        {
            var link = e.Row as OrganizationLedgerLink;

            if (link == null)
            {
                return;
            }

            Ledger        ledger                  = GeneralLedgerMaint.FindLedgerByID(Base, link.LedgerID, isReadonly: false);
            Organization  organization            = GetUpdatingOrganization(link.OrganizationID);
            PXEntryStatus organizationEntryStatus = OrganizationViewBase.Cache.GetStatus(organization);

            if (ledger.BalanceType == LedgerBalanceType.Actual &&
                organizationEntryStatus != PXEntryStatus.Deleted &&
                organizationEntryStatus != PXEntryStatus.InsertedDeleted)
            {
                Organization organizationCopy = PXCache <Organization> .CreateCopy(organization);

                organizationCopy.ActualLedgerID = null;

                OrganizationViewBase.Update(organizationCopy);
            }
        }