如何使用C#在XERO API中通过InvoiceId更新AmountDue值

时间:2018-03-20 05:59:50

标签: c# xero-api

我按发票ID获取发票明细清单。

现在我想通过各自的发票ID更新AmountDue。 我试过下面的代码:

ByInvoiceId.AmountDue = Convert.ToDecimal(100.00); public_app_api.Invoices.Update(ByInvoiceId);

但是..错误为 "发生了验证异常"

背后的原因是什么以及如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

有很多方法可以更改发票上的到期金额,但是直接更改属性的值不是其中之一。

发票上的应付金额由发票上的项次总计减去付款总额和已分配的信用额度决定。更改应付金额的一种方法是更改​​订单项的值或添加/删除某些订单项。如果已支付/部分支付,您将无法更改发票。

您可以更改发票到期金额的另一种方法是在发票上添加付款,或者从信用票据,预付款或多付款到发票中分配贷款

答案 1 :(得分:2)

除了MJMortimer的回答。 您无法通过c#API更改AUTHORIZED发票上的行数。您必须取消发票并创建一个新发票。但是,您可以通过更新订单项来更新DRAFT和SUBMITTED。 编辑:这里有一些代码可以帮助您。这是创建发票代码,但修改代码基本相同。

    public XeroTransferResult CreateInvoices(IEnumerable<InvoiceModel> invoices, string user, string status)
    {
        _user = XeroApiHelper.User(user);

        var invoicestatus = InvoiceStatus.Draft;
        if (status == "SUBMITTED")
        {
            invoicestatus = InvoiceStatus.Submitted;
        }
        else if (status == "AUTHORISED")
        {
            invoicestatus = InvoiceStatus.Authorised;
        }
        var api = XeroApiHelper.CoreApi();
        var xinvs = new List<Invoice>();
        foreach (var inv in invoices)
        {
            var items = new List<LineItem>();
            foreach (var line in inv.Lines)
            {
                decimal discount = 0;
                if (line.PriceBeforeDiscount != line.Price)
                {
                    discount = (decimal)(1 - line.Price / line.PriceBeforeDiscount) * 100;
                }
                items.Add(new LineItem
                {
                    AccountCode = line.AccountCode,
                    Description = line.PublicationName != "N/A" ? line.PublicationName + " - " + line.Description : line.Description,
                    TaxAmount = (decimal)line.TaxAmount,
                    Quantity = 1,
                    UnitAmount = (decimal)line.PriceBeforeDiscount,
                    DiscountRate = discount,
                    TaxType = line.XeroCode,
                    ItemCode = line.ItemCode
                });
            }
            var person = inv.Company.People.FirstOrDefault(p => p.IsAccountContact);
            if (person == null)
            {
                person = inv.Company.People.FirstOrDefault(p => p.IsPrimaryContact);
            }
            var ninv = new Invoice
            {
                Number = inv.ClientInvoiceId,
                Type = InvoiceType.AccountsReceivable,
                Status = invoicestatus,
                Reference = inv.Reference,
                Contact = new Contact
                {
                    Name = inv.Company.OrganisationName,
                    //ContactNumber = "MM" + inv.Company.CompanyId.ToString(),
                    FirstName = person.FirstName,
                    LastName = person.LastName,
                    EmailAddress = person.Email,
                    Phones = new List<Phone>()
                    {
                        new Phone {PhoneNumber = person.Telephone, PhoneType = PhoneType.Default},
                        new Phone {PhoneNumber = person.Mobile, PhoneType = PhoneType.Mobile}
                    },
                    Addresses = new List<Address>
                    { new Address
                        {
                        //AttentionTo = inv.Company.People.FirstOrDefault(p => p.IsAccountContact) == null 
                        //? inv.Company.People.FirstOrDefault(p=> p.IsPrimaryContact).FullName
                        //: inv.Company.People.FirstOrDefault(p => p.IsAccountContact).FullName,
                            //AddressLine1 = inv.Company.OrganisationName,
                            AddressLine1 = inv.Company.Address.Address1,
                            AddressLine2 = inv.Company.Address.Address2 ?? "",
                            AddressLine3 = inv.Company.Address.Address3 ?? "",
                            Region = inv.Company.Address.CountyState,
                            City = inv.Company.Address.TownCity,
                            PostalCode = inv.Company.Address.PostCode,
                        }
                    }
                },
                AmountDue = (decimal)inv.TotalAmount,
                Date = inv.InvoiceDate,
                DueDate = inv.DueDate,
                LineItems = items,
                LineAmountTypes = LineAmountType.Exclusive
            };
            if (SessionContext.TransferContactDetailsToXero == false)
            {
                ninv.Contact = new Contact
                {
                    Id = inv.Company.XeroId ?? Guid.Empty,
                    Name = inv.Company.OrganisationName
                };
            }
            xinvs.Add(ninv);
        }
        var success = true;
        var xinvresult = new List<Invoice>();
        try
        {
            api.SummarizeErrors(false);
            xinvresult = api.Invoices.Create(xinvs).ToList();
        }
        catch (ValidationException ex)
        {
              // Something's gone wrong
        }
        foreach (var inv in xinvresult)
        {
            var mminvoice = invoices.FirstOrDefault(i => i.ClientInvoiceId == inv.Number);
            if (inv.Errors != null && inv.Errors.Count > 0)
            {
                success = false;
                if (mminvoice != null)
                {
                    var errors = new List<XeroError>();
                    foreach (var err in inv.Errors)
                    {
                        errors.Add(new XeroError { ErrorDescription = err.Message });
                    }
                    mminvoice.XeroErrors = errors;
                }
            }
            else
            {
                mminvoice.XeroTransferDate = DateTime.Now;
                mminvoice.XeroId = inv.Id;
                mminvoice.XeroErrors = new List<XeroError>();
            }
        }
        return new XeroTransferResult
        {
            Invoices = invoices,
            Success = success
        };
    }