예제 #1
0
        public void Revalue(RevalueFilter filter, List <RevaluedAPHistory> list)
        {
            JournalEntry je        = PXGraph.CreateInstance <JournalEntry>();
            PostGraph    pg        = PXGraph.CreateInstance <PostGraph>();
            PXCache      cache     = je.Caches[typeof(CuryAPHist)];
            PXCache      basecache = je.Caches[typeof(APHist)];

            je.Views.Caches.Add(typeof(CuryAPHist));
            je.Views.Caches.Add(typeof(APHist));

            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 (RevaluedAPHistory hist in list)
                {
                    if (hist.FinPtdRevalued == 0m)
                    {
                        continue;
                    }

                    PXProcessing <RevaluedAPHistory> .SetCurrentItem(hist);

                    ProcessingResult result = CheckFinPeriod(filter.FinPeriodID, 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  = true;
                        cmbatch.Released     = true;
                        cmbatch.Hold         = false;
                        cmbatch.DateEntered  = filter.CuryEffDate;
                        cmbatch.FinPeriodID  = filter.FinPeriodID;
                        cmbatch.TranPeriodID = 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     = currency.APProvAcctID ?? hist.AccountID;
                        tran.SubID         = currency.APProvSubID ?? hist.SubID;
                        tran.CuryDebitAmt  = 0m;
                        tran.CuryCreditAmt = 0m;

                        tran.DebitAmt  = (hist.FinPtdRevalued < 0m) ? -1m * hist.FinPtdRevalued : 0m;
                        tran.CreditAmt = (hist.FinPtdRevalued < 0m) ? 0m : hist.FinPtdRevalued;

                        tran.TranType     = "REV";
                        tran.TranClass    = AccountType.Liability;
                        tran.RefNbr       = string.Empty;
                        tran.TranDesc     = filter.Description;
                        tran.TranPeriodID = filter.FinPeriodID;
                        tran.FinPeriodID  = filter.FinPeriodID;
                        tran.TranDate     = filter.CuryEffDate;
                        tran.CuryInfoID   = null;
                        tran.Released     = true;
                        tran.ReferenceID  = hist.VendorID;

                        je.GLTranModuleBatNbr.Insert(tran);
                    }

                    VendorClass vendclass = PXSelectReadonly <VendorClass, Where <VendorClass.vendorClassID, Equal <Required <VendorClass.vendorClassID> > > > .Select(je, hist.VendorClassID);

                    if (vendclass == null)
                    {
                        vendclass = new VendorClass();
                    }

                    if (vendclass.UnrealizedGainAcctID == null)
                    {
                        vendclass.UnrealizedGainSubID = null;
                    }

                    if (vendclass.UnrealizedLossAcctID == null)
                    {
                        vendclass.UnrealizedLossSubID = null;
                    }

                    {
                        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 = vendclass.UnrealizedGainAcctID ?? currency.UnrealizedGainAcctID;
                            tran.SubID     = vendclass.UnrealizedGainSubID ?? GainLossSubAccountMaskAttribute.GetSubID <Currency.unrealizedGainSubID>(je, hist.BranchID, currency);
                            tran.DebitAmt  = 0m;
                            tran.CreditAmt = (je.BatchModule.Current.DebitTotal - je.BatchModule.Current.CreditTotal);
                        }
                        else
                        {
                            tran.AccountID = vendclass.UnrealizedLossAcctID ?? currency.UnrealizedLossAcctID;
                            tran.SubID     = vendclass.UnrealizedLossSubID ?? GainLossSubAccountMaskAttribute.GetSubID <Currency.unrealizedLossSubID>(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);
                    }

                    {
                        CuryAPHist aphist = new CuryAPHist();
                        aphist.BranchID    = hist.BranchID;
                        aphist.AccountID   = hist.AccountID;
                        aphist.SubID       = hist.SubID;
                        aphist.FinPeriodID = filter.FinPeriodID;
                        aphist.VendorID    = hist.VendorID;
                        aphist.CuryID      = hist.CuryID;

                        aphist = (CuryAPHist)cache.Insert(aphist);
                        aphist.FinPtdRevalued += hist.FinPtdRevalued;
                    }

                    {
                        APHist aphist = new APHist();
                        aphist.BranchID    = hist.BranchID;
                        aphist.AccountID   = hist.AccountID;
                        aphist.SubID       = hist.SubID;
                        aphist.FinPeriodID = filter.FinPeriodID;
                        aphist.VendorID    = hist.VendorID;

                        aphist = (APHist)basecache.Insert(aphist);
                        aphist.FinPtdRevalued += hist.FinPtdRevalued;
                    }

                    PXProcessing <RevaluedAPHistory> .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 <RevaluedAPHistory> .SetCurrentItem(null);

                throw new PXException(ErrorMessages.SeveralItemsFailed);
            }

            if (created.Count > 0)
            {
                je.BatchModule.Current = created[created.Count - 1];
                throw new PXRedirectRequiredException(je, "Preview");
            }
        }
예제 #2
0
        public static void Revalue(RevalueFilter filter, List <RevaluedAPHistory> list)
        {
            JournalEntry je        = PXGraph.CreateInstance <JournalEntry>();
            PostGraph    pg        = PXGraph.CreateInstance <PostGraph>();
            PXCache      cache     = je.Caches[typeof(CuryAPHist)];
            PXCache      basecache = je.Caches[typeof(APHist)];

            je.Views.Caches.Add(typeof(CuryAPHist));
            je.Views.Caches.Add(typeof(APHist));

            string extRefNbrNumbering = je.CMSetup.Current.ExtRefNbrNumberingID;

            if (string.IsNullOrEmpty(extRefNbrNumbering) == false)
            {
                RevaluationRefNbrHelper helper = new RevaluationRefNbrHelper(extRefNbrNumbering);
                je.RowPersisting.AddHandler <GLTran>(helper.OnRowPersisting);
            }


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

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

            using (PXTransactionScope ts = new PXTransactionScope())
            {
                foreach (RevaluedAPHistory hist in list)
                {
                    if (hist.FinPtdRevalued == 0m)
                    {
                        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 = filter.CuryEffDate;
                        info             = je.currencyinfo.Insert(info) ?? info;

                        cmbatch              = new Batch();
                        cmbatch.BranchID     = hist.BranchID;
                        cmbatch.Module       = "CM";
                        cmbatch.Status       = "U";
                        cmbatch.AutoReverse  = true;
                        cmbatch.Released     = true;
                        cmbatch.Hold         = false;
                        cmbatch.DateEntered  = filter.CuryEffDate;
                        cmbatch.FinPeriodID  = filter.FinPeriodID;
                        cmbatch.TranPeriodID = 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 = filter.CuryEffDate;
                            b_info.CuryRate    = 1m;
                            b_info.CuryMultDiv = "M";
                            je.currencyinfo.Update(b_info);
                        }
                    }
                    else
                    {
                        je.BatchModule.Current = je.BatchModule.Search <Batch.batchNbr>(cmbatch.BatchNbr, cmbatch.Module);
                    }

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

                        tran.DebitAmt  = (hist.FinPtdRevalued < 0m) ? -1m * hist.FinPtdRevalued : 0m;
                        tran.CreditAmt = (hist.FinPtdRevalued < 0m) ? 0m : hist.FinPtdRevalued;

                        tran.TranType     = "REV";
                        tran.TranClass    = "L";
                        tran.RefNbr       = string.Empty;
                        tran.TranDesc     = string.Empty;
                        tran.TranPeriodID = filter.FinPeriodID;
                        tran.FinPeriodID  = filter.FinPeriodID;
                        tran.TranDate     = filter.CuryEffDate;
                        tran.CuryInfoID   = null;
                        tran.Released     = true;
                        tran.ReferenceID  = hist.VendorID;

                        je.GLTranModuleBatNbr.Insert(tran);
                    }


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

                    VendorClass vendclass = PXSelectReadonly <VendorClass, Where <VendorClass.vendorClassID, Equal <Required <VendorClass.vendorClassID> > > > .Select(je, hist.VendorClassID);

                    if (vendclass == null)
                    {
                        vendclass = new VendorClass();
                    }

                    if (vendclass.UnrealizedGainAcctID == null)
                    {
                        vendclass.UnrealizedGainSubID = null;
                    }

                    if (vendclass.UnrealizedLossAcctID == null)
                    {
                        vendclass.UnrealizedLossSubID = null;
                    }

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

                        if (je.BatchModule.Current.DebitTotal > je.BatchModule.Current.CreditTotal)
                        {
                            tran.AccountID = vendclass.UnrealizedGainAcctID ?? currency.UnrealizedGainAcctID;
                            tran.SubID     = vendclass.UnrealizedLossSubID ?? GainLossSubAccountMaskAttribute.GetSubID <Currency.unrealizedGainSubID>(je, hist.BranchID, currency);
                            tran.DebitAmt  = 0m;
                            tran.CreditAmt = (je.BatchModule.Current.DebitTotal - je.BatchModule.Current.CreditTotal);
                        }
                        else
                        {
                            tran.AccountID = vendclass.UnrealizedLossAcctID ?? currency.UnrealizedLossAcctID;
                            tran.SubID     = vendclass.UnrealizedLossSubID ?? GainLossSubAccountMaskAttribute.GetSubID <Currency.unrealizedLossSubID>(je, hist.BranchID, currency);
                            tran.DebitAmt  = (je.BatchModule.Current.CreditTotal - je.BatchModule.Current.DebitTotal);
                            tran.CreditAmt = 0m;
                        }

                        tran.TranType    = "REV";
                        tran.TranClass   = "G";
                        tran.RefNbr      = string.Empty;
                        tran.TranDesc    = string.Empty;
                        tran.Released    = true;
                        tran.ReferenceID = null;

                        je.GLTranModuleBatNbr.Insert(tran);
                    }

                    {
                        CuryAPHist aphist = new CuryAPHist();
                        aphist.BranchID    = hist.BranchID;
                        aphist.AccountID   = hist.AccountID;
                        aphist.SubID       = hist.SubID;
                        aphist.FinPeriodID = filter.FinPeriodID;
                        aphist.VendorID    = hist.VendorID;
                        aphist.CuryID      = hist.CuryID;

                        aphist = (CuryAPHist)cache.Insert(aphist);
                        aphist.FinPtdRevalued += hist.FinPtdRevalued;
                    }

                    {
                        APHist aphist = new APHist();
                        aphist.BranchID    = hist.BranchID;
                        aphist.AccountID   = hist.AccountID;
                        aphist.SubID       = hist.SubID;
                        aphist.FinPeriodID = filter.FinPeriodID;
                        aphist.VendorID    = hist.VendorID;

                        aphist = (APHist)basecache.Insert(aphist);
                        aphist.FinPtdRevalued += hist.FinPtdRevalued;
                    }
                }

                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 (created.Count > 0)
            {
                je.BatchModule.Current = created[created.Count - 1];
                throw new PXRedirectRequiredException(je, "Preview");
            }
        }