【问题标题】:How to properly add records to DAC through PXActions in an Extension如何通过扩展中的 PXActions 正确地将记录添加到 DAC
【发布时间】:2019-08-18 02:00:10
【问题描述】:

总结

我为APInvoiceEntry 图表(AP301000 屏幕)创建了一个图表扩展,名为APInvoiceEntry_Extension

我新建了一个名为APRegisterException的DB表,里面存储了SalesTax、Freight、Price和Qty错误的异常信息。APRegisterAPRegisterException之间存在一对多的关系,表示账单可以有许多不同类型的异常。对于这些异常中的每一个,我创建了一个按钮和一个操作,以在我的扩展图中将异常添加到 DAC。

问题

我只能将 1 条新的 APRegisterExcetion 记录添加到 DAC。 DAC 不会针对多次单击按钮进行更新。以下每个操作都应创建一个新的 APRegisterException 记录,并将它们添加到我的图表中的 Exceptions DAC。

public PXAction<APInvoice> ApplyPriceException;
public PXAction<APInvoice> ApplyQtyException;
public PXAction<PX.Objects.AP.APInvoice> ApplyFreightException;
public PXAction<PX.Objects.AP.APInvoice> ApplySalesTaxException;

注意:动作正在执行,只是 DAC 没有 更新。

代码

APREgisterException DAC

namespace BillsAndAdjustmentsExt
{
  [Serializable]
  public class APRegisterException : IBqlTable
  {
    #region APRegisterRefNbr
    [PXDBString(15, IsKey = true, IsUnicode = true, InputMask = "")]
    [PXUIField(DisplayName = "Ref Nbr")]
    public virtual string APRegisterRefNbr { get; set; }
    public abstract class aPRegisterRefNbr : IBqlField { }
    #endregion

    #region APTranLineNbr
    [PXDBInt()]
    [PXUIField(DisplayName = "Line Nbr")]
    public virtual int? APTranLineNbr { get; set; }
    public abstract class aPTranLineNbr : IBqlField { }
    #endregion

    #region ExceptionDesc
    [PXDBString(150, IsUnicode = true, InputMask = "")]
    [PXUIField(DisplayName = "Description")]
    public virtual string ExceptionDesc { get; set; }
    public abstract class exceptionDesc : IBqlField { }
    #endregion

    #region ExceptionType
    [PXDBString(3, IsUnicode = true, InputMask = "")]
    [PXUIField(DisplayName = "Exc. Type")]
    public virtual string ExceptionType { get; set; }
    public abstract class exceptionType : IBqlField { }
    #endregion

    #region ApprovedByID
    [PXDBString(15, IsUnicode = true, InputMask = "")]
    [PXUIField(DisplayName = "Approved By")]
    public virtual string ApprovedByID { get; set; }
    public abstract class approvedByID : IBqlField { }
    #endregion

    #region ApprovedDate
    [PXDBDate()]
    [PXUIField(DisplayName = "Approval Date")]
    public virtual DateTime? ApprovedDate { get; set; }
    public abstract class approvedDate : IBqlField { }
    #endregion
  }
}

扩展图:

namespace PX.Objects.AP
{
  public class APInvoiceEntry_Extension : PXGraphExtension<APInvoiceEntry>
  {
    #region properties
    public APRegister _currentDoc
    {
      get
      {
        return Base.Document.Current;
      }
    }

    #endregion
   // note

    #region selects

    public PXSelectJoin<
              APRegisterException,
                  LeftJoin<APInvoice,
                      On<APRegisterException.aPRegisterRefNbr, Equal<APInvoice.refNbr>>>,
              Where<APRegisterException.aPRegisterRefNbr, Equal<Current<APInvoice.refNbr>>>> Exceptions;

    #endregion


    #region Event Handlers
    #endregion

    #region Actions

    public PXAction<APRegisterException> AdjustSalesTax; 
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Adj. Sales Tax")]
    protected void adjustSalesTax()
    {
       // put code here to adjust sales tax

    }

    public PXAction<APInvoice> ApplyPriceException;
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Apply Price Exc.")]
    protected void applyPriceException()
    {
        APTran row = Base.Transactions.Current;
        if(row == null)
        {
          throw new PXException("No rows selected");
        }      

        APRegisterException rException = new APRegisterException();
        rException.APRegisterRefNbr = row.RefNbr;   
        rException.APTranLineNbr = row.LineNbr;
        rException.ExceptionDesc = row.TranDesc;
        rException.ExceptionType = "PRC";
        Exceptions.Insert(rException);          
    }

    public PXAction<APInvoice> ApplyQtyException;
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Apply Qty Exc.")]
    protected void applyQtyException()
    {
        APTran row = Base.Transactions.Current;
        if(row == null)
        {
          throw new PXException("No rows selected");
        }

        APRegisterException rException = new APRegisterException();
        rException.APRegisterRefNbr = row.RefNbr;   
        rException.APTranLineNbr = row.LineNbr;
        rException.ExceptionDesc = row.TranDesc;
        rException.ExceptionType = "QTY";      
        Exceptions.Insert(rException); 

    }  


    public PXAction<PX.Objects.AP.APInvoice> ApplyFreightException;
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Apply Freight Exc.")]
    protected void applyFreightException()
    {
        string exceptionMessage = string.Empty;

        // insert freight exception code here
        if(_currentDoc.DocType != "INV" ) { exceptionMessage += "Document type must be 'Bill' to apply a freight exception. \n"; }   
        if(!string.IsNullOrEmpty(exceptionMessage))
        {
          throw new PXException("One or more errors occured trying to save this record. \n" + exceptionMessage);  
        }
        // set the current document to hold
        _currentDoc.Hold = true;

        // create the exception record and store it in cache
        APRegisterException rException = new APRegisterException();
        rException.APRegisterRefNbr = _currentDoc.RefNbr;
        rException.ExceptionDesc = "FREIGHT";
        rException.ExceptionType = "FRT";        

        Exceptions.Insert(rException); 
       // Base.Actions.PressSave();
    } 

    public PXAction<PX.Objects.AP.APInvoice> ApplySalesTaxException;
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Apply Sales Tax Exc.")]
    protected void applySalesTaxException()
    {  
        string exceptionMessage = string.Empty;

        if(_currentDoc.RefNbr == "<NEW>") { exceptionMessage += "Please save the invoice before applying a sales tax exception. \n"; }
        if(_currentDoc.DocType != "INV" ) { exceptionMessage += "Document type must be 'Bill' to apply a sales tax exception. \n"; }      
        //if(((APInvoice)_currentDoc).CuryTaxTotal == 0) { exceptionMessage += "Tax total must be greate than $0.00 to apply a sales tax exception. \n"; }

        if(!string.IsNullOrEmpty(exceptionMessage))
        {
          throw new PXException("One or more errors occured trying to save this record. \n" + exceptionMessage);  
        }
        // set the current document to hold
        _currentDoc.Hold = true;

        // create the exception record and store it in cache
        APRegisterException rException = new APRegisterException();
        rException.APRegisterRefNbr = _currentDoc.RefNbr;
        rException.ExceptionDesc = "SALES TAX";
        rException.ExceptionType = "TAX";        

        Exceptions.Insert(rException);      
       // Base.Actions.PressSave();
    }

    #endregion

  }
}

【问题讨论】:

  • 文本和代码格式使关键点易于理解

标签: c# graph acumatica dac


【解决方案1】:

通过查看您的 DAC,您似乎只有一个关键字段,即 APRegisterRefNbr。该键已存在于缓存中,因此无法插入。如果您正在寻找一对多,我会考虑做一个自动编号的键,然后引用它正在影响的行。例如,您可以将数据库键设置为 bigint(如果此表将变得很大)和 SQL 中自动编号的标识,然后将其添加到您的 DAC:

[PXDBLongIdentity(IsKey = true)]
[PXUIField(DisplayName = "APRegisterExceptionID", Enabled = false)]
public virtual Int64? APRegisterExceptionID{ get; set; }
public abstract class aPRegisterExceptionID: IBqlField { }

然后您就有了唯一的 ID,并且可以使用 PXSelector 和 PXParent 链接回父表。

#region APRegisterRefNbr  
[PXDBString(15)]
[PXSelector(typeof(APRegister.refNbr))]
[PXForeignReference(typeof(Field<APRegisterException.aPRegisterRefNbr>.IsRelatedTo<APRegister.refNbr>))]
[PXUIField(DisplayName = "APRegisterRefNbr")]
public virtual String APRegisterRefNbr  { get; set; }
public abstract class aPRegisterRefNbr  : IBqlField { }
#endregion

我在许多组件的一对多关系表中使用了类似的方法,并且认为在这种情况下它可以为您工作。

【讨论】:

    【解决方案2】:
    1. 您似乎没有使用 APRegisterException 类上的 PXParent 和 PXDBDefault 属性为您的 DAC 定义父子关系,并且需要调整您的密钥来完成所描述的业务用例。
    2. 在声明的“异常”视图中包含对 DocType 的限制,否则不同的文档类型可能会拉取与其他文档关联的异常。
    3. 在 APRegisterException 中包含审计字段以及 tstamp 和 noteID。

              [Serializable]
              public class APRegisterException : IBqlTable
              {
                          #region APRegisterRefNbr
                          [PXDBString(15, IsKey = true, IsUnicode = true, InputMask = "")]
                          [PXUIField(DisplayName = "Ref Nbr")]
                          [PXParent(typeof(Select<APInvoice, Where<APInvoice.refNbr, Equal<Current<APRegisterException.refNbr>>, And<APInvoice.docType, Equal<Current<APRegisterException.docType>>>>>))]
                          [PXDBDefault(typeof(APInvoice.refNbr))]
                          public virtual string APRegisterRefNbr { get; set; }
                          public abstract class aPRegisterRefNbr : IBqlField { }
                          #endregion
      
                          #region APDocType
                          [PXDBString(3, IsKey = true, IsUnicode = true, InputMask = "")]
                          [PXUIField(DisplayName = "Doc Type")]
          [PXDBDefault(typeof(APInvoice.docType))]
          public virtual string APDocType { get; set; }
          public abstract class aPDocType: IBqlField { }
          #endregion
      
          #region ExceptionDesc
          [PXDBString(150, IsUnicode = true, InputMask = "")]
          [PXUIField(DisplayName = "Description")]
          public virtual string ExceptionDesc { get; set; }
          public abstract class exceptionDesc : IBqlField { }
          #endregion
      
          #region ExceptionType
          [PXDBString(3, IsUnicode = true, InputMask = "", IsKey = true)]
          [PXUIField(DisplayName = "Exc. Type")]
          public virtual string ExceptionType { get; set; }
          public abstract class exceptionType : IBqlField { }
          #endregion
      
          #region ApprovedByID
          [PXDBString(15, IsUnicode = true, InputMask = "")]
          [PXUIField(DisplayName = "Approved By")]
          public virtual string ApprovedByID { get; set; }
          public abstract class approvedByID : IBqlField { }
                          #endregion
      
                          #region ApprovedDate
                          [PXDBDate()]
                          [PXUIField(DisplayName = "Approval Date")]
                          public virtual DateTime? ApprovedDate { get; set; }
                          public abstract class approvedDate : IBqlField { }
                          #endregion
              }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多