private async Task <XeroPurchaseOrder?> GenerateXeroPurchaseOrder(PurchaseOrder mexPo, CancellationToken cancellationToken) { var mexSupplier = mexPo.SupplierContact; var xeroContact = await this.AccountingApi.GetContacts(where : $"Name==\"{mexSupplier?.FirstName}\"") .FirstOrDefault(); if (xeroContact is null) { throw new InvalidOperationException($"Unable to find Xero Supplier with name {mexSupplier?.FirstName} for Purchase Order {mexPo.PurchaseOrderNumber}."); } var xeroPo = this.Mapper.Map <PurchaseOrder, XeroPurchaseOrder>(mexPo); var xeroAccounts = await this.AccountingApi.GetAccounts(); var xeroLines = new List <XeroLineItem>(); using var mexDbContext = this.MexDbContextFactory.Create(); if (mexDbContext is null) { return(null); } var poLines = this.GetPurchaseOrderLinesForPo(mexPo, mexDbContext); await foreach (var poLine in poLines.WithCancellation(cancellationToken)) { var accountCodeName = poLine.AccountCode?.AccountCodeName; var xeroLine = this.Mapper.Map <PurchaseOrderLine, XeroLineItem>(poLine); if (!poLine.Extension_PurchaseOrderLine.WorkOrderNumber.IsNullOrWhiteSpace()) { xeroLine.Description = xeroLine.Description.Join(" - ", $"WO#: {poLine.Extension_PurchaseOrderLine.WorkOrderNumber}"); } if (poLine.TaxPercentage != 0 && !accountCodeName.IsNullOrWhiteSpace()) { xeroLine.TaxType = xeroAccounts.FirstOrDefault(acc => acc.Code == accountCodeName)?.TaxType; } xeroLines.Add(xeroLine); } if (mexPo.FreightAmount > 0) { var freightAccount = xeroAccounts.FirstOrDefault(acc => acc.Name?.Contains("Freight") == true); var xeroLine = new XeroLineItem() { Quantity = 1, UnitAmount = mexPo.FreightAmount, Description = "Freight Line", AccountCode = freightAccount?.Code, TaxType = freightAccount?.TaxType, TaxAmount = mexPo.FreightAmount * mexPo.FreightTaxPercentage }; xeroLines.Add(xeroLine); } xeroPo.LineItems = xeroLines; return(xeroPo); }
private void button1_Click(object sender, EventArgs e) { LoadAPIConfig(); // Create a test invoice var xeroContact = new Xero.NetStandard.OAuth2.Model.Accounting.Contact { Name = "Client Name", FirstName = "Client", LastName = "Name", EmailAddress = "*****@*****.**", IsCustomer = true, AccountNumber = $"NEW-ACC", // Website = "http://google.com"; // Currently the Zero API has this read only!! Addresses = new List <Xero.NetStandard.OAuth2.Model.Accounting.Address>() }; var address = new Xero.NetStandard.OAuth2.Model.Accounting.Address { AddressType = Xero.NetStandard.OAuth2.Model.Accounting.Address.AddressTypeEnum.STREET, AddressLine1 = "Address1_Line1", AddressLine2 = "Address1_Line2", AddressLine3 = "Address1_Line3", City = "Address1_City", Region = "Address1_County", PostalCode = "Address1_PostalCode", Country = "Address1_Country" }; xeroContact.Addresses.Add(address); xeroContact.Phones = new List <Xero.NetStandard.OAuth2.Model.Accounting.Phone>(); var phone = new Xero.NetStandard.OAuth2.Model.Accounting.Phone(); phone.PhoneType = Xero.NetStandard.OAuth2.Model.Accounting.Phone.PhoneTypeEnum.DEFAULT; phone.PhoneNumber = "Telephone1"; xeroContact.Phones.Add(phone); var fax = new Xero.NetStandard.OAuth2.Model.Accounting.Phone(); fax.PhoneType = Xero.NetStandard.OAuth2.Model.Accounting.Phone.PhoneTypeEnum.FAX; fax.PhoneNumber = "Fax"; xeroContact.Phones.Add(fax); var mobile = new Xero.NetStandard.OAuth2.Model.Accounting.Phone(); mobile.PhoneType = Xero.NetStandard.OAuth2.Model.Accounting.Phone.PhoneTypeEnum.MOBILE; mobile.PhoneNumber = "MobilePhone"; xeroContact.Phones.Add(mobile); // Build the Invoice Body var invoiceRecord = new Xero.NetStandard.OAuth2.Model.Accounting.Invoice(); invoiceRecord.Contact = xeroContact; invoiceRecord.Date = DateTime.Now; invoiceRecord.DueDate = DateTime.Now.AddDays(30); invoiceRecord.Status = Xero.NetStandard.OAuth2.Model.Accounting.Invoice.StatusEnum.DRAFT; invoiceRecord.LineAmountTypes = Xero.NetStandard.OAuth2.Model.Accounting.LineAmountTypes.Exclusive; invoiceRecord.Type = Xero.NetStandard.OAuth2.Model.Accounting.Invoice.TypeEnum.ACCREC; invoiceRecord.Reference = $"oAuth2/Testing"; invoiceRecord.LineItems = new List <Xero.NetStandard.OAuth2.Model.Accounting.LineItem>(); // Line Item 1 // Create the Tracking Item var tracking = new List <Xero.NetStandard.OAuth2.Model.Accounting.LineItemTracking>(); tracking.Add(new Xero.NetStandard.OAuth2.Model.Accounting.LineItemTracking { Name = "Region", Option = "Eastside" }); Xero.NetStandard.OAuth2.Model.Accounting.LineItem lineItem = new Xero.NetStandard.OAuth2.Model.Accounting.LineItem { Description = $"Product Item 1{Environment.NewLine}Additional Description text", Quantity = 1, UnitAmount = 123m, LineAmount = 123m, TaxAmount = 123m * .20m, AccountCode = "200", Tracking = tracking }; invoiceRecord.LineItems.Add(lineItem); // Add the line item to the invoice object // Line Item 2 // Create the Tracking Item tracking = new List <Xero.NetStandard.OAuth2.Model.Accounting.LineItemTracking>(); tracking.Add(new Xero.NetStandard.OAuth2.Model.Accounting.LineItemTracking { Name = "Region", Option = "South" }); Xero.NetStandard.OAuth2.Model.Accounting.LineItem lineItem2 = new Xero.NetStandard.OAuth2.Model.Accounting.LineItem { Description = $"Product Item 2{Environment.NewLine}Additional Description text2", Quantity = 2, UnitAmount = 456m, LineAmount = 456m * 2, TaxAmount = (456m * 2) * .20m, AccountCode = "200", Tracking = tracking }; invoiceRecord.LineItems.Add(lineItem2); // Add the line item to the invoice object if (invoiceRecord.ValidationErrors == null || invoiceRecord.ValidationErrors.Count == 0) { var createdInv = xeroAPI.AccountingApi.CreateInvoice(invoiceRecord); if (createdInv.InvoiceID != Guid.Empty) { System.Diagnostics.Debug.WriteLine("Created New Invoice"); } } }