public HttpResponseMessage PostPurchasesToXero(SearchFilter searchFilter)
        {
            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                try
                {
                    var purchases = context.XEROPurchases(searchFilter.StartDate, searchFilter.EndDate);
                    foreach (var purchase in purchases)
                    {
                        PurchaseToInvoiceViewModelConverter purchaseConverter = new PurchaseToInvoiceViewModelConverter();
                        try
                        {
                            InvoiceViewModel invoiceViewModel = purchaseConverter.Convert(purchase);
                            purchasesToPostList.Add(invoiceViewModel);
                        }
                        catch (InvoiceValidationException e)
                        {
                            ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message);
                            return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
                        }
                        catch (SupplierDoesNotExistException e)
                        {
                            ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message);
                            return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
                        }
                    }
                }
                catch (System.Data.SqlClient.SqlException)
                {
                    ModelStateDictionary dictionary = errorStringConvertor.Convert("To many Purchases in daterange");
                    return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary);
                }
            }
            try
            {
                xeroLogic.ExportInvoices(purchasesToPostList);
            }
            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);
        }
        public HttpResponseMessage GetCustomers(SearchFilter searchFilter)
        {
            List<CustomerViewModel> resultList = new List<CustomerViewModel>();
            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                var customers = (from cu in context.CUSTOMERs.Where(cu => cu.DBTimeStamp >= searchFilter.StartDate
                    && cu.DBTimeStamp <= searchFilter.EndDate)
                                 select new CustomerViewModel()
                                     {
                                         customerId = cu.CUSTOMER_ID,
                                         firstname = cu.fname,
                                         lastname = cu.sname,
                                         email = cu.email,
                                         address1 = cu.address1,
                                         address2 = cu.address2,
                                         address3 = cu.address3,
                                         clubNumber = cu.club_no ?? 0,
                                         balance = cu.balance ?? 0
                                     }
                                 ).Take(1000);

                resultList = customers.ToList();
                return this.Request.CreateResponse(HttpStatusCode.OK, resultList);
            }
        }
        public HttpResponseMessage GetPayment(SearchFilter searchFilter)
        {
            List<PaymentViewModel> resultList = new List<PaymentViewModel>();
            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                try
                {
                    var payments = context.XEROPayments(searchFilter.StartDate, searchFilter.EndDate);

                    foreach (var payment in payments)
                    {
                        PaymentViewModel paymentViewModel = new PaymentViewModel()
                        {
                            InvoiceNumber = payment.InvoiceNumber,
                            ContactName = payment.ContactName,
                            Email = payment.EmailAddress,
                            Address1 = payment.POAddressLine1,
                            Address2 = payment.POAddressLine2,
                            Address3 = payment.POAddressLine3,
                            City = payment.POCity,
                            Region = payment.PORegion,
                            Country = payment.POCountry,
                            PostalCode = payment.POPostalCode,
                            InvoiceDate = payment.InvoiceDate.GetValueOrDefault().ToString("dd-MM-yy"),
                            DueDate = payment.DueDate.GetValueOrDefault().ToString("dd-MM-yy"),
                            InventoryItemCode = payment.InventoryItemCode.ToString(),
                            Description = payment.Description,
                            Quantity = payment.Quantity.ToString(),
                            UnitAmount = payment.UnitAmount.ToString(),
                            Discount = payment.Discount.ToString(),
                            AccountCode = payment.AccountCode.ToString(),
                            TaxType = payment.TaxType,
                            TrackingName = payment.TrackingName1,
                            TrackingOption = payment.TrackingOption1,
                            Reference = payment.Reference,
                            BrandingTheme = payment.BrandingTheme
                        };

                        resultList.Add(paymentViewModel);
                    }
                }
                catch (System.Data.SqlClient.SqlException)
                {
                    ModelStateDictionary dictionary = errorStringConvertor.Convert("To many payments in daterange");
                    return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary);
                }

                return this.Request.CreateResponse(HttpStatusCode.OK, resultList);
            }
        }
        public HttpResponseMessage GetSuppliers(SearchFilter searchFilter)
        {
            List<SupplierViewModel> resultList = new List<SupplierViewModel>();
            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                var suppliers = (from sp in context.SUPPLIERs.Where(sp => sp.DBTimeStamp >= searchFilter.StartDate
                    && sp.DBTimeStamp <= searchFilter.EndDate)
                                 select new SupplierViewModel()
                                 {
                                     supplierCode = sp.supplier1,
                                     name = sp.name,
                                     email = sp.email,
                                     phone = sp.phone,

                                 }
                                 );

                resultList = suppliers.ToList();
                return this.Request.CreateResponse(HttpStatusCode.OK, resultList);
            }
        }
        public void whenAXeroApiValidationExceptionIsThrownThenABadRequestIsReturned()
        {
            List<InvoiceViewModel> tesInvoiceViewModel = new List<InvoiceViewModel>();

            var IXEROLogicMock = new Mock<IExportingInvoicesToXEROLogic>();
            IXEROLogicMock.Setup(xl => xl.ExportInvoices(It.IsAny<List<InvoiceViewModel>>())).Throws(new XeroApiValidationException());

            xeroWebAPIController.XeroLogic = IXEROLogicMock.Object;

            xeroWebAPIController.Request = new HttpRequestMessage();
            xeroWebAPIController.Configuration = new HttpConfiguration();

            DateTime startDate = new DateTime(2010, 1, 1);
            DateTime endDate = new DateTime(2014, 09, 28);
            SearchFilter searchFilter = new SearchFilter();
            searchFilter.StartDate = startDate;
            searchFilter.EndDate = endDate;
            HttpResponseMessage response = xeroWebAPIController.PostPurchasesToXero(searchFilter);

            IXEROLogicMock.Verify(x => x.ExportInvoices(It.IsAny<List<InvoiceViewModel>>()), Times.Once());
            Assert.AreEqual(response.StatusCode, HttpStatusCode.BadRequest);
        }
        public HttpResponseMessage GetPurchases(SearchFilter searchFilter)
        {
            List<PurchaseViewModel> resultList = new List<PurchaseViewModel>();
            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                try
                {
                    var purchases = context.XEROPurchases(searchFilter.StartDate, searchFilter.EndDate);

                    foreach (var purchase in purchases)
                    {
                        PurchaseViewModel purchaseViewModel = new PurchaseViewModel()
                        {
                            InvoiceNumber = purchase.InvoiceNumber,
                            EmailAddress = purchase.EmailAddress,
                            Address1 = purchase.POAddressLine1,
                            Address2 = purchase.POAddressLine2,
                            Address3 = purchase.POAddressLine3,
                            Address4 = purchase.POAddressLine4,
                            City = purchase.POCity,
                            Region = purchase.PORegion,
                            Country = purchase.POCountry,
                            PostalCode = purchase.POPostalCode,
                            InvoiceDate = purchase.InvoiceDate.GetValueOrDefault().ToString("dd-MM-yy"),
                            DueDate = purchase.DueDate.GetValueOrDefault().ToString("dd-MM-yy"),
                            InventoryItemCode = purchase.InventoryItemCode.ToString(),
                            Description = purchase.Description,
                            Quantity = purchase.Quantity.ToString(),
                            UnitAmount = purchase.UnitAmount.ToString(),
                            AccountCode = purchase.AccountCode.ToString(),
                            TaxType = purchase.TaxType,
                            TrackingName = purchase.TrackingName1,
                            TrackingOption = purchase.TrackingOption1,
                            Currency = purchase.Currency,

                        };

                        try
                        {
                            purchaseViewModel.ContactName = getSupplierName(purchase.ContactName);
                        }
                        catch(PurchaseValidationException e)
                        {
                            ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message);
                            return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary);
                        }
                        catch (SupplierDoesNotExistException e )
                        {
                            ModelStateDictionary dictionary = errorStringConvertor.Convert(e.Message);
                            return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, dictionary);
                        }

                        resultList.Add(purchaseViewModel);
                    }
                }
                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 void whenPostPurchasesToXeroWithValidDatesIsCalledAThenHttpStatusCodeOkIsReturned()
        {
            List<InvoiceViewModel> tesInvoiceViewModel = new List<InvoiceViewModel>();

            var IXEROLogicMock = new Mock<IExportingInvoicesToXEROLogic>();
            IXEROLogicMock.Setup(xl => xl.ExportInvoices(It.IsAny<List<InvoiceViewModel>>())).Returns(tesInvoiceViewModel);

            xeroWebAPIController.XeroLogic = IXEROLogicMock.Object;

            xeroWebAPIController.Request = new HttpRequestMessage();
            xeroWebAPIController.Configuration = new HttpConfiguration();

            DateTime startDate = new DateTime(2015, 3, 12);
            DateTime endDate = new DateTime(2015, 09, 28);
            SearchFilter searchFilter = new SearchFilter();
            searchFilter.StartDate = startDate;
            searchFilter.EndDate = endDate;
            HttpResponseMessage response = xeroWebAPIController.PostPurchasesToXero(searchFilter);

            IXEROLogicMock.Verify(x => x.ExportInvoices(It.IsAny<List<InvoiceViewModel>>()), Times.Once());
            Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
        }
        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);
            }
        }