简体   繁体   English

通过代码创建发票

[英]Create Invoice By Code

I am using this code to create new Invoice. 我正在使用此代码创建新的发票。 I do customization in from SOInvoiceEntry. 我从SOInvoiceEntry中进行自定义。 I debugged and get an error Revision ID is not empty. 我调试并得到一个错误修订ID不为空。 I guest that this code could not get information of customer's financial information when I set value for customerID. 我认为,当我为customerID设置值时,此代码无法获取客户的财务信息。 Here is my code. 这是我的代码。 Thanks for your support. 谢谢你的支持。

public PXAction<ARInvoice> preparePayment;

    [PXUIField(DisplayName = "Pay Invoice", Enabled = true)]
    [PXButton()]
    public IEnumerable PreparePayment(PXAdapter adapter)
    {
        List<ARRegister> doclist = new List<ARRegister>();
        SOOrderShipment soOrderShipment =
                     PXSelect
                         <SOOrderShipment,
                             Where<SOOrderShipment.invoiceNbr, Equal<Required<SOOrderShipment.invoiceNbr>>>
                             >.Select(new PXGraph(), Base.Document.Current.RefNbr);

        if (soOrderShipment != null)
        {
            SOOrder soOrder = PXSelect<SOOrder, Where<SOOrder.orderNbr,
                Equal<Required<SOOrder.orderNbr>>,
                And<SOOrder.orderType, Equal<Required<SOOrder.orderType>>>>>.Select(
                    new PXGraph(), soOrderShipment.OrderNbr, soOrderShipment.OrderType);
            SOOrderExt soExt = PXCache<SOOrder>.GetExtension<SOOrderExt>(soOrder);
            if (soExt.CustomerID != soExt.UsrARCustomer)
            {
                ARInvoiceEntry arInvoiceGraph = PXGraph.CreateInstance<ARInvoiceEntry>();
                ARInvoice invoice = (ARInvoice)arInvoiceGraph.Caches[typeof(ARInvoice)].CreateInstance();
                invoice = (ARInvoice) arInvoiceGraph.Caches[typeof (ARInvoice)].Insert(invoice);
                arInvoiceGraph.Caches[typeof(ARInvoice)].SetValue<ARInvoice.customerID>(invoice,soExt.UsrARCustomer);

                invoice.DocType = ARInvoiceType.DebitMemo;
                invoice.CustomerID = soExt.UsrARCustomer;
                //invoice.CustomerID = Base.Document.Current.CustomerID;
                Location location =
                    PXSelect<Location, Where<Location.bAccountID, Equal<Required<Location.bAccountID>>>>.Select(
                        arInvoiceGraph, soExt.UsrARCustomer);
                if(location!=null)
                       arInvoiceGraph.Caches[typeof(ARInvoice)].SetValue<ARInvoice.customerLocationID>(invoice, location.LocationID);
                arInvoiceGraph.Caches[typeof(ARInvoice)].SetValueExt<ARInvoice.docDate>(invoice, DateTime.Now);
                arInvoiceGraph.Caches[typeof(ARInvoice)].SetValue<ARInvoice.projectID>(invoice,location.CDefProjectID);

                ARInvoice oldInvoice = (ARInvoice)arInvoiceGraph.Caches[typeof(ARInvoice)].CreateCopy(invoice);

                invoice.CuryOrigDocAmt = 0;
                arInvoiceGraph.Caches[typeof(ARInvoice)].RaiseRowUpdated(invoice, oldInvoice);
                arInvoiceGraph.Caches[typeof(ARInvoice)].SetValue<ARInvoice.curyOrigDocAmt>(invoice, invoice.CuryDocBal);
                invoice.RefNoteID = 1;

                arInvoiceGraph.Caches[typeof(ARInvoice)].SetValueExt<ARInvoice.hold>(invoice, false);

                doclist.Add((ARInvoice)arInvoiceGraph.Caches[typeof(ARInvoice)].Current);
                arInvoiceGraph.Save.Press();

            }


        }

} }

As far as I look inside protected virtual void ARInvoice_RowPersisting(PXCache sender, PXRowPersistingEventArgs e) in base graph, I see some error messages there, which says "may not be empty" . 据我在基础图中查看protected virtual void ARInvoice_RowPersisting(PXCache sender, PXRowPersistingEventArgs e) ,我看到了一些错误消息,上面写着"may not be empty" There is not any code which has relation to RevisionID. 没有任何代码与RevisionID相关。 But there is a code which has RevisionID in DAC class ARInvoice , fields BillAddressID, BillContactID . 但是在DAC类ARInvoice有一个代码具有RevisionID的字段BillAddressID, BillContactID From this facts I propose you to check do you put correct ID for ARAddress in ARInvoice and check whether in db table ARAddress has values in column RevisionID. 基于这个事实,我建议您检查是否在ARInvoice中ARInvoice正确的ARAddress ID,并检查了数据库表ARAddress是否在RevisionID列中包含值。 That is required due to fact that RevisionID in class ARAddress is declared in the following way: 由于以下事实需要声明类ARAddress中的RevisionID:

    [PXDBInt]
    [PXDefault]
    public virtual int? RevisionID
    {
      get
      {
        return this._RevisionID;
      }
      set
      {
        this._RevisionID = value;
      }
    }
 arInvoiceGraph.Caches[typeof(ARInvoice)].SetValue<ARInvoice.customerID>(invoice,soExt.UsrARCustomer);

Try this 尝试这个

invoice.Customer = soExt.UsrARCustomer;
invoice = arInvoiceGraph.Caches[typeof(ARInvoice)].Update(invoice);

I found solution for saving Document data by change code like this 我找到了通过这样的更改代码保存文档数据的解决方案

 public IEnumerable PreparePayment(PXAdapter adapter)
    {
        List<ARRegister> doclist = new List<ARRegister>();
        SOOrderShipment soOrderShipment =
                     PXSelect
                         <SOOrderShipment,
                             Where<SOOrderShipment.invoiceNbr, Equal<Required<SOOrderShipment.invoiceNbr>>>
                             >.Select(new PXGraph(), Base.Document.Current.RefNbr);

        if (soOrderShipment != null)
        {
            SOOrder soOrder = PXSelect<SOOrder, Where<SOOrder.orderNbr,
                Equal<Required<SOOrder.orderNbr>>,
                And<SOOrder.orderType, Equal<Required<SOOrder.orderType>>>>>.Select(
                    new PXGraph(), soOrderShipment.OrderNbr, soOrderShipment.OrderType);
            SOOrderExt soExt = PXCache<SOOrder>.GetExtension<SOOrderExt>(soOrder);
            if (soExt.CustomerID != soExt.UsrARCustomer)
            {
                BAccount bAccount = PXSelect<BAccount, Where<BAccount.bAccountID,Equal<Required<BAccount.bAccountID>>>>.Select(new PXGraph(),soExt.UsrARCustomer);
                ARInvoiceEntry arInvoiceGraph = PXGraph.CreateInstance<ARInvoiceEntry>();
                ARInvoice invoice = (ARInvoice)arInvoiceGraph.Caches[typeof(ARInvoice)].CreateInstance();
                invoice = (ARInvoice) arInvoiceGraph.Caches[typeof (ARInvoice)].Insert(invoice);
                // Using SetValueExt by Passing AcctCD
arInvoiceGraph.Caches[typeof(ARInvoice)].SetValueExt<ARInvoice.customerID>(invoice,bAccount.AcctCD);
                invoice.CustomerID = soExt.UsrARCustomer;
                arInvoiceGraph.Caches[typeof(ARInvoice)].SetValue<ARInvoice.docDesc>(invoice, "N/A");
                invoice.DocType = ARInvoiceType.DebitMemo;

                Location location =
                    PXSelect<Location, Where<Location.bAccountID, Equal<Required<Location.bAccountID>>>>.Select(
                        arInvoiceGraph, soExt.UsrARCustomer);
                if(location!=null)
                       arInvoiceGraph.Caches[typeof(ARInvoice)].SetValue<ARInvoice.customerLocationID>(invoice, location.LocationID);

                arInvoiceGraph.Caches[typeof(ARInvoice)].Update(invoice);
                ARInvoice oldInvoice = (ARInvoice)arInvoiceGraph.Caches[typeof(ARInvoice)].CreateCopy(invoice);

                invoice.CuryOrigDocAmt = Base.Document.Current.CuryOrigDocAmt;
                arInvoiceGraph.Caches[typeof(ARInvoice)].RaiseRowUpdated(invoice, oldInvoice);
                arInvoiceGraph.Caches[typeof(ARInvoice)].SetValue<ARInvoice.curyOrigDocAmt>(invoice, invoice.CuryDocBal);
                arInvoiceGraph.Caches[typeof(ARInvoice)].SetValueExt<ARInvoice.hold>(invoice, false);
                arInvoiceGraph.Caches[typeof(ARInvoice)].Update(invoice);
                doclist.Add((ARInvoice)arInvoiceGraph.Caches[typeof(ARInvoice)].Current);
       // Insert transaction data
                ARTran arTran = new ARTran();
                arTran.TranDesc = "Total Value";
                arTran.Qty = 1;
                arTran.UnitPrice = Base.Document.Current.CuryDocBal;
                arTran.CuryLineAmt = Base.Document.Current.CuryDocBal;
                arInvoiceGraph.Transactions.Insert(arTran);
                ARTran arTranUpdated = (ARTran) arInvoiceGraph.Transactions.Update(arTran);
                arInvoiceGraph.Transactions.Current = arTranUpdated;
                arInvoiceGraph.Transactions.Cache.Update(arInvoiceGraph.Transactions.Current);
                arInvoiceGraph.Save.Press();

            }


        }

Although Document data is saved, the transactions data is not. 尽管保存了凭证数据,但未保存交易数据。 I have tried this code to create it and I got and error. 我尝试使用此代码创建它,但是却出错了。 Do you have any suggestion for this? 您对此有什么建议吗? Thank you for your supporting 谢谢您的支持

 AR Error #113: Cannot save notes.

   at PX.Objects.AR.InvoiceNbrAttribute.RowPersisted(PXCache sender, PXRowPersistedEventArgs e)
   at PX.Objects.AR.ARInvoiceNbrAttribute.RowPersisted(PXCache sender, PXRowPersistedEventArgs e)
   at PX.Data.PXCache.OnRowPersisted(Object item, PXDBOperation operation, PXTranStatus tranStatus, Exception exception)
   at PX.Data.PXTableAttribute.PersistInserted(PXCache sender, Object row)
   at PX.Data.PXCache`1.PersistInserted(Object row)
   at PX.Data.PXCache`1.Persist(PXDBOperation operation)
   at PX.Data.PXGraph.Persist(Type cacheType, PXDBOperation operation)
   at PX.Data.PXGraph.Persist()
   at PX.Objects.AR.ARInvoiceEntry.Persist()
   at PX.Data.PXSave`1.d__0.MoveNext()
   at PX.Data.PXAction`1.d__c.MoveNext()
   at PX.Data.PXAction`1.d__c.MoveNext()
   at PX.Data.PXAction`1.Press() 

I'm suspicious about 我很怀疑

arInvoiceGraph.Transactions.Current = arTranUpdated;

According to T200 manual: You can set the Currentproperty in the following cases: • To be able to process multiple data records by a graph (see Example 9.2: Implementing the Receipt Release Operation). 根据T200手册:在以下情况下,您可以设置Currentproperty:•为了能够通过图形处理多个数据记录(请参见示例9.2:实现收货下达操作)。 • To be able to open a page displaying the specified data record when you redirect to the page from another one (see Example 3.4: Adding a Redirection Link to the Grid). •当您从另一个页面重定向到该页面时,能够打开一个显示指定数据记录的页面(请参见例3.4:向网格添加重定向链接)。

I not sure, that in your case you do point 1 or 2. 我不确定,在您的情况下,您确实要指出1或2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM