/// <summary>
        /// Creates a Xero line item for every Aquairum line item with a matching Invoice number,  and bundles them up into a Xero invoice
        /// </summary>
        /// <param name="theInvoiceLineItemToConvert"></param>
        /// <returns></returns>
        public static XeroApi.Model.Invoice ConvertMultipleOutboundInvoiceLineItemsToXeroInvoice(OutboundInvoiceLineItemList theInvoiceLineItemsToConvert, OutboundInvoiceLineItem theLineItemToMatchTo, Datalayer.Xero.Interresolve.InterResolveXeroService aService)
        {
            try
            {
                XeroApi.Model.Invoice theInvoiceToReturn = new XeroApi.Model.Invoice();
                theInvoiceToReturn.LineItems = new XeroApi.Model.LineItems();
                theInvoiceToReturn.Contact = new XeroApi.Model.Contact(); //NEED TO SPECIFY A CONTACT

                //assign the xero contact
                aService.LoginToXero();
                theInvoiceToReturn.Contact = aService.GetXeroContactFromAquariumInvoiceLineItemList(theLineItemToMatchTo);

                //assign top level invoice fields for the passed invoice to the Xero counterpart
                theInvoiceToReturn.InvoiceNumber = theLineItemToMatchTo.InvoiceNumber.ToString();
                theInvoiceToReturn.Status = "DRAFT";
                theInvoiceToReturn.Type = "ACCREC"; //Sales invoice
                theInvoiceToReturn.Reference = theLineItemToMatchTo.SageNarrative + " " + theLineItemToMatchTo.YourRef;
                theInvoiceToReturn.LineAmountTypes = XeroApi.Model.LineAmountType.Exclusive;
                theInvoiceToReturn.SubTotal = theLineItemToMatchTo.OriginalPurchaseInvoiceTotal;
                theInvoiceToReturn.DueDate = DateTime.Now.AddDays(14);
                theInvoiceToReturn.Date = theLineItemToMatchTo.InvoiceDate;
               // theInvoiceToReturn.TotalTax //this must be calculated AFTER by summing all the line item VATs
                theInvoiceToReturn.Total = theLineItemToMatchTo.OriginalPurchaseInvoiceTotal;

                //iterate through and if number matches specified invoice number, add it to the list
                decimal RunningVATTotalForInvoice = new decimal(0.00);

                for (int i = 0; i < theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.Count(); i++)
                {
                    if (theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).InvoiceNumber == theLineItemToMatchTo.InvoiceNumber)
                    {
                         //if the invoice numbers match, create a new line item and add this line item to the list
                        XeroApi.Model.LineItem aLineItem = new XeroApi.Model.LineItem();
                        aLineItem.Quantity = 1; //always one line item
                        aLineItem.Description = theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).InvoiceType; //
                        aLineItem.UnitAmount = theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).OriginalPurchaseInvoiceAmount;
                        aLineItem.TaxAmount = theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).OriginalPurchaseInvoiceVAT;
                        aLineItem.LineAmount = (aLineItem.Quantity * aLineItem.UnitAmount);
                        aLineItem.AccountCode = InterResolveXeroService.GetXeroAccountCodeForAquariumLineItem(theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i));

                        //add the VAT to the running total
                        RunningVATTotalForInvoice =+ theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).OriginalPurchaseInvoiceVAT;

                        //add it to the list
                        theInvoiceToReturn.LineItems.Add(aLineItem);
                     }

                 }

                //assign the overall invoice VAT
                theInvoiceToReturn.TotalTax = RunningVATTotalForInvoice;

                return theInvoiceToReturn;
                }

            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// converts a single IR line item invoice (e.g. one table Row) to a Xero Invoice
        /// </summary>
        /// <param name="theInvoiceLineItemToConvert"></param>
        /// <returns></returns>
        public static XeroApi.Model.Invoice ConvertSingleLineItemOutboundInvoiceToXeroInvoice(OutboundInvoiceLineItem theInvoiceLineItemToConvert)
        {
            try
            {
                XeroApi.Model.Invoice theInvoiceToReturn = new Invoice();
                XeroApi.Model.LineItem theInvoiceLineItem = new LineItem();

                //map the invoice and all fields
                theInvoiceToReturn.InvoiceNumber = theInvoiceLineItemToConvert.InvoiceNumber.ToString();
                theInvoiceToReturn.Type = "ACCREC"; //Sales invoice
                theInvoiceToReturn.Date = theInvoiceLineItemToConvert.InvoiceDate; //date of invoicve
                theInvoiceToReturn.Total = theInvoiceLineItemToConvert.NetInvoiceTotal; //NET amount = total
             //   theInvoiceToReturn.TotalTax = //THIS NEEDS TO BE A SUM OF ALL THE LINE ITEM VATS ------------------------<

                //map the line item (there is only one in this case)
                theInvoiceLineItem.Description = theInvoiceLineItemToConvert.InvoiceType; //Description of the invoice
                theInvoiceLineItem.Quantity = 1;
                theInvoiceLineItem.TaxAmount = theInvoiceLineItemToConvert.OriginalPurchaseInvoiceVAT;
                theInvoiceLineItem.AccountCode = theInvoiceLineItemToConvert.CompanyRef;

                theInvoiceToReturn.LineItems = new LineItems();
                theInvoiceToReturn.LineItems.Add(theInvoiceLineItem);

                return theInvoiceToReturn;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get the Xero Line Item Code from the Aquarium Line Item
        /// </summary>
        /// <param name="theLineItem"></param>
        /// <returns></returns>
        public static string GetXeroAccountCodeForAquariumLineItem(OutboundInvoiceLineItem theLineItem)
        {
            try
            {
                string codeToReturn = "";

                //is it sales or credit?
                if (theLineItem.NominalAccount == "40000")
                {
                    //ALL SALES TRANSACTIONS

                    //IF it's a third party case
                    switch (theLineItem.NominalCostCentre)
                    {
                        //IF it's a third party case
                        case "3PT":

                            //now test which department, and assign correct code;
                            switch (theLineItem.NominalDepartment)
                            {
                                case "CLA":
                                    codeToReturn = "41010";
                                    break;
                                case "COA":
                                    codeToReturn = "41020";
                                    break;
                                case "CRF":
                                    codeToReturn = "41030";
                                    break;
                                case "EFC":
                                    codeToReturn = "41040";
                                    break;
                                case "EFR":
                                    codeToReturn = "41050";
                                    break;
                                case "GPO":
                                    codeToReturn = "41060";
                                    break;
                                case "ILO":
                                    codeToReturn = "41070";
                                    break;
                                case "LOE":
                                    codeToReturn = "41080";
                                    break;
                                case "MAF":
                                    codeToReturn = "41090";
                                    break;
                                case "MRF":
                                    codeToReturn = "41100";
                                    break;
                                case "SFC":
                                    codeToReturn = "42010";
                                    break;
                                case "SFR":
                                    codeToReturn = "42020";
                                    break;
                                case "SLA":
                                    codeToReturn = "41110";
                                    break;
                                case "TRF":
                                    codeToReturn = "41120";
                                    break;

                            }

                            break;

                        //IF it's a direct case
                        case "DIR":

                            switch (theLineItem.NominalDepartment)
                            {
                                case "CLA":
                                    codeToReturn = "44010";
                                    break;
                                case "CRF":
                                    codeToReturn = "44030";
                                    break;
                                case "EFC":
                                    codeToReturn = "44040";
                                    break;
                                case "EFR":
                                    codeToReturn = "44050";
                                    break;
                                case "GPO":
                                    codeToReturn = "44060";
                                    break;
                                case "ILO":
                                    codeToReturn = "44130";
                                    break;
                                case "LOE":
                                    codeToReturn = "44080";
                                    break;
                                case "IPF":
                                    codeToReturn = "44139";
                                    break;
                                case "MRF":
                                    codeToReturn = "44100";
                                    break;
                                case "MAF":
                                    codeToReturn = "44090";
                                    break;
                                case "OSF":
                                    codeToReturn = "44160";
                                    break;
                                case "SLA":
                                    codeToReturn = "44110";
                                    break;
                                case "TRF":
                                    codeToReturn = "44120";
                                    break;
                                case "FPF":
                                    codeToReturn = "44140";
                                    break;

                            }

                            break;

                    } //END SALES

                    ///BEGIN CREDIT NOTES
                    if (theLineItem.NominalDepartment == "50000")
                    {
                        switch (theLineItem.NominalCostCentre)
                        {
                            //IF it's a third party case
                            case "3PT":

                                //now test which department, and assign correct code;
                                switch (theLineItem.NominalDepartment)
                                {
                                    case "CLA":
                                        codeToReturn = "51010";
                                        break;
                                    case "COA":
                                        codeToReturn = "51020";
                                        break;
                                    case "GPO":
                                        codeToReturn = "51030";
                                        break;
                                    case "ILO":
                                        codeToReturn = "51040";
                                        break;
                                    case "LOE":
                                        codeToReturn = "51050";
                                        break;
                                    case "MAF":
                                        codeToReturn = "51060";
                                        break;
                                    case "MRF":
                                        codeToReturn = "51070";
                                        break;
                                    case "SFC":
                                        codeToReturn = "51080";
                                        break;
                                    case "SFR":
                                        codeToReturn = "51090";
                                        break;
                                    case "SLA":
                                        codeToReturn = "51100";
                                        break;
                                    case "TRF":
                                        codeToReturn = "51110";
                                        break;

                                }

                                break;
                            //IF it's a direct case
                            case "DIR":

                                switch (theLineItem.NominalDepartment)
                                {
                                    case "CLA":
                                        codeToReturn = "54010";
                                        break;
                                    case "GPO":
                                        codeToReturn = "54030";
                                        break;
                                    case "ILO":
                                        codeToReturn = "54040";
                                        break;
                                    case "LOE":
                                        codeToReturn = "54050";
                                        break;
                                    case "MRF":
                                        codeToReturn = "54070";
                                        break;
                                    case "MAF":
                                        codeToReturn = "54060";
                                        break;
                                    case "SLA":
                                        codeToReturn = "54100";
                                        break;
                                    case "SFC":
                                        codeToReturn = "54080";
                                        break;
                                    case "SFR":
                                        codeToReturn = "54090";
                                        break;
                                    case "TRF":
                                        codeToReturn = "54110";
                                        break;

                                }

                                break;

                            // END CREDIT NOTES
                        }
                    }
                }

                return codeToReturn;
            }
            catch (Exception ex)
            { throw ex; }
        }
        /// <summary>
        /// this maps the correct company to it's corresponding Xero Company
        /// </summary>
        public XeroApi.Model.Contact GetXeroContactFromAquariumInvoiceLineItemList(OutboundInvoiceLineItem theAquariumLineItem)
        {
            try{

                IQueryable<XeroApi.Model.Contact> theXeroContact;

                //assign the correct company based on the CompanyRef
                switch(theAquariumLineItem.CompanyRef)
                {
                    case "ACR123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                          where contact.Name == "Acromas Insurance Company Ltd"
                                          select contact);
                        break;

                    case "ADM123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Admiral Insurance"
                                                                select contact);
                        break;

                    case "BRO123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Broker Direct"
                                                                 select contact);
                        break;

                    case "COV123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Covea Insurance plc"
                                                                 select contact);
                        break;

                    case "DOC123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Doctors Chambers (UK) Ltd"
                                                                 select contact);
                        break;

                    case "END123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Endsleigh Insurance Services Ltd"
                                                                 select contact);
                        break;

                    case "EQU123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Equity"
                                                                 select contact);
                        break;

                    case "LIV123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Liverpool Victoria Friendly Society"
                                                                 select contact);
                        break;

                    case "MARK123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Markerstudy"
                                                                 select contact);
                        break;

                    case "OCT123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Resolution Management Services Ltd"
                                                                 select contact);
                        break;

                    case "PRE123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Prettys Solicitors"
                                                                 select contact);
                        break;

                    case "PROV123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Covea Insurance plc"
                                                                 select contact);
                        break;

                    case "SER123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Service Underwriting Ltd"
                                                                 select contact);
                        break;

                    case "TEST123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Test"
                                                                 select contact);
                        break;

                    case "TST123":
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Test"
                                                                 select contact);
                        break;
                    default:
                        theXeroContact = (from contact in this.XeroRepository.Contacts
                                                                 where contact.Name == "Test"
                                                                 select contact);
                        break;

                }

                return theXeroContact.First();

            } catch (Exception ex)
            {
                throw ex;
            }
        }