public async Task <IActionResult> OutstandingInvoices() { var token = await _tokenStore.GetAccessTokenAsync(User.XeroUserId()); var connections = await _xeroClient.GetConnectionsAsync(token); if (!connections.Any()) { return(RedirectToAction("NoTenants")); } var data = new Dictionary <string, int>(); foreach (var connection in connections) { var accessToken = token.AccessToken; var tenantId = connection.TenantId.ToString(); var organisations = await _accountingApi.GetOrganisationsAsync(accessToken, tenantId); var organisationName = organisations._Organisations[0].Name; var outstandingInvoices = await _accountingApi.GetInvoicesAsync(accessToken, tenantId, statuses : "AUTHORISED", where : "Type == \"ACCREC\""); data[organisationName] = outstandingInvoices._Invoices.Count; } var model = new OutstandingInvoicesViewModel { Name = $"{User.FindFirstValue(ClaimTypes.GivenName)} {User.FindFirstValue(ClaimTypes.Surname)}", Data = data }; return(View(model)); }
public async Task <IActionResult> Index() { var token = await _tokenService.GetAccessTokenAsync(User.XeroUserId()); var connections = await _xeroClient.GetConnectionsAsync(token); var tenantId = connections[0].TenantId.ToString(); var invoices = await _accountingApi.GetInvoicesAsync(token.AccessToken, tenantId); return(View(invoices._Invoices)); }
private async Task FetchInvoicesFromDb(string accessToken, string tenantId) { Console.WriteLine("Importing Invoices into Xero"); string connectionString = "Data Source=data.versentia.com;Initial Catalog=Versentia;User ID=VersentiaUser;Password=toomanyhands"; SqlConnection cnn = new SqlConnection(connectionString); cnn.Open(); //string sql = "SELECT TOP 500 BI.InvoiceID,BP.Amount,BI.ClientID, BI.ProducedDate,BI.OverdueDate, AC.Name, AC.AddressLine1, BIP.Amount, VBI.Amount, VBI.Name FROM [Versentia].[Billing].[Invoices] BI INNER JOIN [Versentia].[Billing].[InvoicePayments] BIP ON BI.InvoiceID = BIP.InvoiceID INNER JOIN [Versentia].[Billing].[Payments] BP ON BIP.PaymentID = BP.PaymentID INNER JOIN Versentia.Accounts.Clients AC ON BI.ClientID = AC.ClientID INNER JOIN [Versentia].[Billing].[InvoiceFees] VBI ON VBI.InvoiceID = BI.InvoiceID ORDER BY BI.InvoiceID"; string sql = "SELECT VBI.InvoiceID, VBI.InvoiceSerialNumber, VBI.ClientID, VBI.ProducedOn, VBI.TotalFee, VAC.AddressLine1, VAC.Name FROM [Versentia].[Billing].[InvoiceDetails] VBI INNER JOIN [Versentia].[Accounts].[Clients] VAC ON VBI.ClientID = VAC.ClientID Where VBI.IsPaid = 0 AND VBI.ProducedDate >= CONVERT(datetime,'2019-04-01') ORDER BY VBI.ProducedOn"; SqlCommand command = new SqlCommand(sql, cnn); SqlDataReader dataReader = command.ExecuteReader(); int invoiceID = 0; var lineitems = new List <LineItem>(); var invoice = new Invoice(); Invoices batchInvoices = new Invoices(); batchInvoices._Invoices = new List <Invoice>(); LineItem lineitem = new LineItem(); var invoicespresent = await _accountingApi.GetInvoicesAsync(accessToken, tenantId); while (dataReader.Read()) { invoice = new Invoice { Type = Invoice.TypeEnum.ACCREC, Contact = new Contact { Name = dataReader.GetString(6), ContactNumber = dataReader.GetInt32(2).ToString(), Addresses = new List <Address> { new Address { AddressLine1 = dataReader.GetString(5) } } }, Date = dataReader.GetDateTime(3), DueDate = dataReader.GetDateTime(3).AddDays(30), InvoiceNumber = "N" + dataReader.GetInt32(1).ToString(), ExpectedPaymentDate = dataReader.GetDateTime(3).AddDays(30), Status = Invoice.StatusEnum.DRAFT, LineItems = new List <LineItem>() { new LineItem { Description = "Inspection Services", Quantity = 1, UnitAmount = (double)dataReader.GetDecimal(4), AccountCode = "400", TaxType = "NONE" } } }; if (invoicespresent._Invoices.FindIndex(x => x.InvoiceNumber == "N" + dataReader.GetInt32(1).ToString()) > 0) { Console.WriteLine("Invoice with invoice number N" + dataReader.GetInt32(1).ToString() + " already present in Xero\n"); } else { Console.WriteLine("\nImporting invoice N" + dataReader.GetInt32(1).ToString()); batchInvoices._Invoices.Add(invoice); } } Invoices partialInvoices = new Invoices(); partialInvoices._Invoices = new List <Invoice>(); for (int i = 0; i < batchInvoices._Invoices.Count(); i++) { partialInvoices._Invoices.Add(batchInvoices._Invoices[i]); if (i % 200 == 0) { try { var createbatchinvoices = await _accountingApi.CreateInvoicesAsync(accessToken, tenantId, partialInvoices, false); } catch (Exception e) { Console.WriteLine(e.Message + "\n"); } partialInvoices._Invoices.Clear(); } } try { var createbatchinvoices = await _accountingApi.CreateInvoicesAsync(accessToken, tenantId, partialInvoices, false); } catch (Exception e) { Console.WriteLine(e.Message + "\n"); } dataReader.Close(); Console.WriteLine("Import invoice function completed"); }