首页 文章

如何在文档发布后创建额外的日记帐分录?

提问于
浏览
0

我想在一份文件之后创建第二个日记帐分录,比如Cash Sale,它已成功发布 . 我试图采用多种方式,但不断遇到不同的问题 .

在发布后创建原始日记帐分录后,我必须在何处放置代码来创建此日记帐分录?我在代码中需要的一些要求是原始批处理的屏幕以及原始文档的参考编号 . 如果我能够获得第一批及其GLTran记录,那对我来说就足以创造新记录 .

请注意,现金销售并不是唯一可用于此的文件,因此如果有办法集中执行此操作,则可以加分,但不是必需的 .

1 回答

  • 1

    我为CashSale制作了这个例子 . 我相信它足够通用,你也可以将它应用到其他场景中 .

    在ARCashSaleEntry上创建图表扩展并覆盖Release操作:
    enter image description here

    这是图扩展的代码,解释在代码注释中:

    using PX.Data;
    using PX.Objects.GL;
    using System.Collections;
    using ARCashSale = PX.Objects.AR.Standalone.ARCashSale;
    
    namespace PX.Objects.AR
    {
        public class ARCashSaleEntry_Extension : PXGraphExtension<ARCashSaleEntry>
        {
            #region Event Handlers
    
            public delegate IEnumerable ReleaseDelegate(PXAdapter adapter);
    
            [PXOverride]
            public IEnumerable Release(PXAdapter adapter, ReleaseDelegate baseMethod)
            {
                // Get reference to current cash sale if required
                ARCashSale cashSale = Base.Document.Current;
    
                // Declare event handler so we can remove the named delegate
                PXGraph.InstanceCreatedDelegate<JournalEntry> instanceCreatedHandler = null;
    
                // Handler definition used to intercept Journal Entry Graph
                instanceCreatedHandler = delegate (JournalEntry oldJournalEntry)
                {
                    // Remove event handler
                    PXGraph.InstanceCreated.RemoveHandler<JournalEntry>(instanceCreatedHandler);
    
                    // Add hook to intercept Batch persisted event
                    oldJournalEntry.RowPersisted.AddHandler<Batch>(delegate (PXCache sender, PXRowPersistedEventArgs e)
                    {
                        // Get reference to old batch 
                        Batch oldBatch = oldJournalEntry.BatchModule.Current;
    
                        // After oldBatch is inserted
                        if (oldBatch != null && e.Operation == PXDBOperation.Insert && e.TranStatus == PXTranStatus.Completed)
                        {
                            // Create new Journal Entry Graph
                            JournalEntry newJournalEntry = PXGraph.CreateInstance<JournalEntry>();
    
                            // Create new batch
                            Batch newBatch = new Batch();
    
                            // Set new batch properties here and insert it
                            newJournalEntry.BatchModule.Insert(newBatch);
    
                            // Iterate on old tran from old batch
                            foreach (GLTran oldTran in oldJournalEntry.GLTranModuleBatNbr.Select())
                            {
                                // Create new tran
                                GLTran newTran = new GLTran();
    
                                // Set new tran properties here and insert it
                                newJournalEntry.GLTranModuleBatNbr.Insert(newTran);
                            }
    
                            // Save new journal
                            newJournalEntry.Save.Press();
                        }
                    });
                };
    
                // Add hook to intercept Journal Entry Graph
                PXGraph.InstanceCreated.AddHandler<JournalEntry>(instanceCreatedHandler);
    
                // Call base method to release document
                // Your hook will be called after this action in the context of a PXLongOperation
                return baseMethod(adapter);
            }
    
            #endregion
        }
    }
    

相关问题