Ejemplo n.º 1
0
        protected virtual void RevalueFilter_TotalRevalued_FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
        {
            if (e.Row != null)
            {
                decimal val = 0m;
                foreach (RevaluedGLHistory res in GLAccountList.Cache.Updated)
                {
                    if ((bool)res.Selected)
                    {
                        decimal sign = AccountRules.IsDEALAccount(res.AccountType) ? 1.0m : -1.0m;
                        val += sign * (decimal)res.FinPtdRevalued;
                    }
                }

                if (val == 0)
                {
                    TimeSpan  timespan;
                    Exception ex;
                    if (PXLongOperation.GetStatus(UID, out timespan, out ex) == PXLongRunStatus.Completed)
                    {
                        Filter.Cache.RaiseExceptionHandling <RevalueFilter.totalRevalued>(e.Row, val,
                                                                                          new PXSetPropertyException(Messages.NoRevaluationEntryWasMade, PXErrorLevel.Warning));
                    }
                }
                else
                {
                    e.ReturnValue = val;
                    e.Cancel      = true;
                }
            }
        }
        /// <summary>
        /// Creates L record elements. Exports GL transactions and account balances.
        /// </summary>
        public IList <LedgerRecord> CreateLedgerRecords(GAFPeriod gafPeriod)
        {
            var taxPeriod = _gafRepository.GetTaxPeriodByKey(gafPeriod.BranchID, gafPeriod.TaxAgencyID, gafPeriod.TaxPeriodID);

            var finPeriods = _gafRepository.GetFinPeriodsInInterval(taxPeriod.StartDate, taxPeriod.EndDate)
                             .ToList();

            var finYears = finPeriods.Select(finPeriod => finPeriod.FinYear)
                           .Distinct();

            var lastYearPeriod = finYears.Select(finYear => _gafRepository.FindLastYearNotAdjustmentPeriod(finYear))
                                 .SingleOrDefault();

            if (lastYearPeriod != null && finPeriods.Select(finPeriod => finPeriod.FinPeriodID).Contains(lastYearPeriod.FinPeriodID))
            {
                finPeriods.AddRange(_gafRepository.GetAdjustmentFinPeriods(lastYearPeriod.FinYear));
            }

            var records = new List <LedgerRecord>();

            var branch = _gafRepository.GetBranchByID(gafPeriod.BranchID);

            var minFinPeriod = finPeriods.First();

            var accountsWithGLHistoryData = _gafRepository.GetAccountsWithDataToCalcBeginBalancesExcludingYTDNetIncAcc(branch.BranchID, branch.LedgerID, minFinPeriod.FinPeriodID);

            foreach (var accountWithGLHistoryData in accountsWithGLHistoryData)
            {
                var account = (Account)accountWithGLHistoryData;
                var glHistoryForLastActivityPeriod = (AH)accountWithGLHistoryData;
                var glHistoryForSelectedPeriod     = (GLHistory)accountWithGLHistoryData;

                decimal?beginBalanceForPeriod = 0m;

                if (glHistoryForLastActivityPeriod.FinPeriodID != null)
                {
                    var selectedPeriodPtdCreditTotal = glHistoryForSelectedPeriod.FinPtdCredit ?? 0m;
                    var selectedPeriodPtdDebitTotal  = glHistoryForSelectedPeriod.FinPtdDebit ?? 0m;
                    var selectedPeriodPtdSaldo       = AccountRules.CalcSaldo(account.Type, selectedPeriodPtdDebitTotal, selectedPeriodPtdCreditTotal);

                    beginBalanceForPeriod = GetBalanceSign(account.Type) * (glHistoryForLastActivityPeriod.FinYtdBalance - selectedPeriodPtdSaldo);
                }

                var balanceRecord = BuildLedgerRecordForBalance(account, minFinPeriod.StartDate, beginBalanceForPeriod.Value);
                records.Add(balanceRecord);

                foreach (var finPeriod in finPeriods)
                {
                    var glTrans = _gafRepository.GetPostedGLTrans(branch.BranchID, branch.LedgerID, account.AccountID, finPeriod.FinPeriodID);

                    foreach (var glTran in glTrans)
                    {
                        var record = BuildLedgerRecordForTran(account, glTran, records.LastOrDefault());

                        records.Add(record);
                    }

                    _gafRepository.ClearCaches();
                }
            }

            return(records);
        }
Ejemplo n.º 3
0
        public void Revalue(RevalueFilter filter, List <RevaluedGLHistory> list)
        {
            JournalEntry je        = PXGraph.CreateInstance <JournalEntry>();
            PostGraph    pg        = PXGraph.CreateInstance <PostGraph>();
            PXCache      basecache = je.Caches[typeof(AcctHist)];

            je.Views.Caches.Add(typeof(AcctHist));

            string extRefNbrNumbering = je.CMSetup.Current.ExtRefNbrNumberingID;

            if (string.IsNullOrEmpty(extRefNbrNumbering) == false)
            {
                RevaluationRefNbrHelper helper = new RevaluationRefNbrHelper(extRefNbrNumbering);
                helper.Subscribe(je);
            }

            DocumentList <Batch> created = new DocumentList <Batch>(je);

            Currency currency = PXSelect <Currency, Where <Currency.curyID, Equal <Required <Currency.curyID> > > > .Select(je, filter.CuryID);

            bool hasErrors = false;

            using (PXTransactionScope ts = new PXTransactionScope())
            {
                foreach (RevaluedGLHistory hist in list)
                {
                    PXProcessing <RevaluedGLHistory> .SetCurrentItem(hist);

                    if (hist.FinPtdRevalued == 0m)
                    {
                        PXProcessing <RevaluedGLHistory> .SetProcessed();

                        continue;
                    }

                    string FinPeriod =
                        FinPeriodRepository.GetFinPeriodByMasterPeriodID(PXAccess.GetParentOrganizationID(hist.BranchID), filter.FinPeriodID)
                        .Result
                        .FinPeriodID;

                    ProcessingResult result = CheckFinPeriod(FinPeriod, hist.BranchID);
                    if (!result.IsSuccess)
                    {
                        hasErrors = true;
                        continue;
                    }

                    if (je.GLTranModuleBatNbr.Cache.IsInsertedUpdatedDeleted)
                    {
                        je.Save.Press();

                        if (created.Find(je.BatchModule.Current) == null)
                        {
                            created.Add(je.BatchModule.Current);
                        }
                    }

                    Batch cmbatch = created.Find <Batch.branchID>(hist.BranchID) ?? new Batch();
                    if (cmbatch.BatchNbr == null)
                    {
                        je.Clear();

                        CurrencyInfo info = new CurrencyInfo();
                        info.CuryID      = hist.CuryID;
                        info.CuryEffDate = hist.CuryEffDate;
                        info.BaseCalc    = false;
                        info             = je.currencyinfo.Insert(info) ?? info;

                        cmbatch             = new Batch();
                        cmbatch.BranchID    = hist.BranchID;
                        cmbatch.Module      = "CM";
                        cmbatch.Status      = "U";
                        cmbatch.AutoReverse = false;
                        cmbatch.Released    = true;
                        cmbatch.Hold        = false;
                        cmbatch.DateEntered = filter.CuryEffDate;
                        FinPeriodIDAttribute.SetPeriodsByMaster <Batch.finPeriodID>(je.BatchModule.Cache, cmbatch, filter.FinPeriodID);

                        cmbatch.CuryID      = hist.CuryID;
                        cmbatch.CuryInfoID  = info.CuryInfoID;
                        cmbatch.DebitTotal  = 0m;
                        cmbatch.CreditTotal = 0m;
                        cmbatch.Description = filter.Description;
                        je.BatchModule.Insert(cmbatch);

                        CurrencyInfo b_info = je.currencyinfo.Select();
                        if (b_info != null)
                        {
                            b_info.CuryID         = hist.CuryID;
                            b_info.CuryEffDate    = hist.CuryEffDate;
                            b_info.CuryRateTypeID = hist.CuryRateTypeID;
                            b_info.CuryRate       = hist.CuryRate;
                            b_info.RecipRate      = hist.RateReciprocal;
                            b_info.CuryMultDiv    = hist.CuryMultDiv;
                            je.currencyinfo.Update(b_info);
                        }
                    }
                    else
                    {
                        if (!je.BatchModule.Cache.ObjectsEqual(je.BatchModule.Current, cmbatch))
                        {
                            je.Clear();
                        }

                        je.BatchModule.Current = je.BatchModule.Search <Batch.batchNbr>(cmbatch.BatchNbr, cmbatch.Module);
                    }

                    {
                        GLTran tran = new GLTran();
                        tran.SummPost      = false;
                        tran.AccountID     = hist.AccountID;
                        tran.SubID         = hist.SubID;
                        tran.CuryDebitAmt  = 0m;
                        tran.CuryCreditAmt = 0m;

                        if (hist.AccountType == AccountType.Asset || hist.AccountType == AccountType.Expense)
                        {
                            tran.DebitAmt  = (hist.FinPtdRevalued < 0m) ? 0m : hist.FinPtdRevalued;
                            tran.CreditAmt = (hist.FinPtdRevalued < 0m) ? -1m * hist.FinPtdRevalued : 0m;
                        }
                        else
                        {
                            tran.DebitAmt  = (hist.FinPtdRevalued < 0m) ? -1m * hist.FinPtdRevalued : 0m;
                            tran.CreditAmt = (hist.FinPtdRevalued < 0m) ? 0m : hist.FinPtdRevalued;
                        }

                        tran.TranType  = "REV";
                        tran.TranClass = hist.AccountType;
                        tran.RefNbr    = string.Empty;
                        tran.TranDesc  = filter.Description;
                        FinPeriodIDAttribute.SetPeriodsByMaster <GLTran.finPeriodID>(je.GLTranModuleBatNbr.Cache, tran, filter.FinPeriodID);
                        tran.TranDate    = filter.CuryEffDate;
                        tran.CuryInfoID  = null;
                        tran.Released    = true;
                        tran.ReferenceID = null;
                        tran.ProjectID   = PM.ProjectDefaultAttribute.NonProject();

                        je.GLTranModuleBatNbr.Insert(tran);
                    }

                    foreach (GLTran tran in je.GLTranModuleBatNbr.SearchAll <Asc <GLTran.tranClass> >(new object[] { "G" }))
                    {
                        je.GLTranModuleBatNbr.Delete(tran);
                    }

                    {
                        GLTran tran = new GLTran();
                        tran.SummPost      = true;
                        tran.ZeroPost      = false;
                        tran.CuryDebitAmt  = 0m;
                        tran.CuryCreditAmt = 0m;

                        if (je.BatchModule.Current.DebitTotal > je.BatchModule.Current.CreditTotal)
                        {
                            tran.AccountID = currency.RevalGainAcctID;
                            tran.SubID     = GainLossSubAccountMaskAttribute.GetSubID <Currency.revalGainSubID>(je, hist.BranchID, currency);
                            tran.DebitAmt  = 0m;
                            tran.CreditAmt = (je.BatchModule.Current.DebitTotal - je.BatchModule.Current.CreditTotal);
                        }
                        else
                        {
                            tran.AccountID = currency.RevalLossAcctID;
                            tran.SubID     = GainLossSubAccountMaskAttribute.GetSubID <Currency.revalLossSubID>(je, hist.BranchID, currency);
                            tran.DebitAmt  = (je.BatchModule.Current.CreditTotal - je.BatchModule.Current.DebitTotal);
                            tran.CreditAmt = 0m;
                        }

                        tran.TranType    = "REV";
                        tran.TranClass   = GLTran.tranClass.UnrealizedAndRevaluationGOL;
                        tran.RefNbr      = string.Empty;
                        tran.TranDesc    = filter.Description;
                        tran.Released    = true;
                        tran.ReferenceID = null;

                        je.GLTranModuleBatNbr.Insert(tran);
                    }

                    {
                        AcctHist accthist = new AcctHist();
                        accthist.BranchID    = hist.BranchID;
                        accthist.LedgerID    = hist.LedgerID;
                        accthist.AccountID   = hist.AccountID;
                        accthist.SubID       = hist.SubID;
                        accthist.FinPeriodID = filter.FinPeriodID;
                        accthist.CuryID      = hist.CuryID;
                        accthist.BalanceType = "A";

                        accthist = (AcctHist)basecache.Insert(accthist);
                        accthist.FinPtdRevalued += hist.FinPtdRevalued;
                    }

                    PXProcessing <RevaluedGLHistory> .SetProcessed();
                }

                if (je.GLTranModuleBatNbr.Cache.IsInsertedUpdatedDeleted)
                {
                    je.Save.Press();

                    if (created.Find(je.BatchModule.Current) == null)
                    {
                        created.Add(je.BatchModule.Current);
                    }
                }

                ts.Complete();
            }

            CMSetup cmsetup = PXSelect <CMSetup> .Select(je);

            for (int i = 0; i < created.Count; i++)
            {
                if (cmsetup.AutoPostOption == true)
                {
                    pg.Clear();
                    pg.PostBatchProc(created[i]);
                }
            }

            if (hasErrors)
            {
                //Clean current to prevent set exception to the last item
                PXProcessing <RevaluedGLHistory> .SetCurrentItem(null);

                throw new PXException(ErrorMessages.SeveralItemsFailed);
            }

            if (created.Count > 0)
            {
                je.BatchModule.Current = created[created.Count - 1];
                throw new PXRedirectRequiredException(je, "Preview");
            }

            decimal val = 0m;

            foreach (RevaluedGLHistory res in GLAccountList.Cache.Updated)
            {
                if ((bool)res.Selected)
                {
                    decimal sign = AccountRules.IsDEALAccount(res.AccountType) ? 1.0m : -1.0m;
                    val += sign * (decimal)res.FinPtdRevalued;
                }
            }

            if (val == 0)
            {
                throw new PXOperationCompletedWithWarningException(Messages.NoRevaluationEntryWasMade);
            }
        }
Ejemplo n.º 4
0
 public AccountsController(AccountRules accountRules)
 {
     _accountRules = accountRules;
 }