public HttpResponseMessage GetInvoice(SearchFilter searchFilter) { List<InvoiceFrontEndViewModel> resultList = new List<InvoiceFrontEndViewModel>(); using (DataClassesDataContext context = new DataClassesDataContext()) { try { var invoices = context.AccountSalesService(searchFilter.StartDate, searchFilter.EndDate); foreach (var invoice in invoices) { string customerName = ""; try { customerName = getCustomerName(invoice.ContactName); } catch (CustomerDoesNotExistException e) { ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message); return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary); } InvoiceFrontEndViewModel invoiceViewModel = new InvoiceFrontEndViewModel() { InvoiceNumber = invoice.InvoiceNumber, ContactName = getCustomerName(invoice.ContactName), Email = invoice.EmailAddress, Address1 = invoice.POAddressLine1, Address2 = invoice.POAddressLine2, Address3 = invoice.POAddressLine3, City = invoice.POCity, Region = invoice.PORegion, Country = invoice.POCountry, PostalCode = invoice.POPostalCode, InvoiceDate = invoice.InvoiceDate.GetValueOrDefault().ToString("dd-MM-yy"), DueDate = invoice.DueDate.GetValueOrDefault().ToString("dd-MM-yy"), InventoryItemCode = invoice.InventoryItemCode.ToString(), Description = invoice.Description, Quantity = invoice.Quantity.ToString(), UnitAmount = invoice.UnitAmount.ToString(), Discount = invoice.Discount.ToString(), AccountCode = invoice.AccountCode.ToString(), TaxType = invoice.TaxType, TrackingName = invoice.TrackingName1, TrackingOption = invoice.TrackingOption1, Currency = "NZD", BrandingTheme = invoice.BrandingTheme }; resultList.Add(invoiceViewModel); } } catch (System.Data.SqlClient.SqlException) { ModelStateDictionary dictionary = errorStringConvertor.Convert("To many invoices in daterange"); return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary); } return this.Request.CreateResponse(HttpStatusCode.OK, resultList); } }
public HttpResponseMessage PostInvoicesToXero(SearchFilter searchFilter) { using (DataClassesDataContext context = new DataClassesDataContext()) { try { var invoices = context.AccountSalesService(searchFilter.StartDate, searchFilter.EndDate); foreach (var invoice in invoices) { InvoiceToInvoiceViewModelConverter invoiceConverter = new InvoiceToInvoiceViewModelConverter(); try { InvoiceViewModel invoiceViewModel = invoiceConverter.Convert(invoice); invoicesToPostList.Add(invoiceViewModel); } catch (InvoiceValidationException e) { ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message); return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary); } catch (CustomerDoesNotExistException e) { ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message); return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary); } } } catch (System.Data.SqlClient.SqlException) { ModelStateDictionary dictionary = errorStringConvertor.Convert("To many invoices in daterange"); return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary); } } try { xeroLogic.ExportInvoices(invoicesToPostList); } catch (XeroApiLimitException e) { ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message); return this.Request.CreateErrorResponse(HttpStatusCode.BadGateway, dictionary); } catch (XeroException e) { ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message); return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary); } catch (XeroApiValidationException e) { ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message); return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary); } catch (Exception) { ModelStateDictionary dictionary = errorStringConvertor.Convert("Something went wrong. Please check the data and try again."); return this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, dictionary); } return this.Request.CreateResponse(HttpStatusCode.OK); }