如何通过扩展中的PXAction将记录正确添加到DAC

时间:2019-03-27 13:22:35

标签: c# graph acumatica dac

摘要

我为APInvoiceEntry图形(AP301000屏幕)创建了图形扩展,称为APInvoiceEntry_Extension

我创建了一个名为APRegisterException的新数据库表,该表存储了SalesTax,运费,价格和数量错误的异常信息。APRegisterAPRegisterException之间存在一对多关系,表示帐单可以有许多不同类型的例外。对于这些异常中的每一个,我创建了一个按钮和一个操作,以在扩展图中将异常添加到DAC。

问题

我只能向DAC添加1条新的APRegisterExcetion记录。对于多个按钮单击,DAC不会更新。以下每个操作都应创建一个新的APRegisterException记录,并将它们添加到图形中的Exception 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

  }
}

2 个答案:

答案 0 :(得分:0)

通过查看您的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

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

答案 1 :(得分:0)

  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
            }