public void LogInToAllXeroCompanies()
        {
            HttpContext AContext =  TestHelper.FakeHttpContext();
            AContext.Server.MapPath("~/Certificates/InterResolve Ltd/public_privatekey.pfx");

            List<InterResolveXeroService> AllTheInterResolveCompanies = new List<InterResolveXeroService>();

            InterResolveXeroService InterResolve_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Ltd);
            InterResolveXeroService MotorResolve_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.MotorResolve_Ltd);
            InterResolveXeroService InterMediation_Group_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterMediation_Group_Ltd);
            InterResolveXeroService InterResolve_Claims_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Claims_Ltd);
            InterResolveXeroService InterResolve_Holdings_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Holdings_Ltd);

            AllTheInterResolveCompanies.Add(InterResolve_LtdService);
            AllTheInterResolveCompanies.Add(MotorResolve_LtdService);
            AllTheInterResolveCompanies.Add(InterMediation_Group_LtdService);
            AllTheInterResolveCompanies.Add(InterResolve_Claims_LtdService);
            AllTheInterResolveCompanies.Add(InterResolve_Holdings_LtdService);

            foreach (var company in AllTheInterResolveCompanies)
            {
               Assert.IsTrue(company.LoginToXero());

            }
        }
        public void PushDummyInvoiceToAllXeroCompanies()
        {
            List<InterResolveXeroService> AllTheInterResolveCompanies = new List<InterResolveXeroService>();

            InterResolveXeroService InterResolve_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Ltd);
            InterResolveXeroService MotorResolve_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.MotorResolve_Ltd);
            InterResolveXeroService InterMediation_Group_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterMediation_Group_Ltd);
            InterResolveXeroService InterResolve_Claims_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Claims_Ltd);
            InterResolveXeroService InterResolve_Holdings_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Holdings_Ltd);

            AllTheInterResolveCompanies.Add(InterResolve_LtdService);
            AllTheInterResolveCompanies.Add(MotorResolve_LtdService);
            AllTheInterResolveCompanies.Add(InterMediation_Group_LtdService);
            AllTheInterResolveCompanies.Add(InterResolve_Claims_LtdService);
            AllTheInterResolveCompanies.Add(InterResolve_Holdings_LtdService);

            OutboundInvoiceLineItemList someInvoices = new OutboundInvoiceLineItemList();

            someInvoices.Get();

            XeroApi.Model.Invoice anInvoice = OutboundInvoiceLineItem.ConvertSingleLineItemOutboundInvoiceToXeroInvoice(someInvoices.OutboundInvoiceLineItems.FirstOrDefault());

            foreach (var company in AllTheInterResolveCompanies)
            {
              //     Assert.IsTrue(company.PostOutboundInvoiceToXero(anInvoice));
            }
        }
        public List<XeroApi.Model.Invoice> GetXeroInvoiceNumbersForLastThreeMonths(InterResolveXeroService aXeroCompanyToCheckAgainst)
        {
            List<XeroApi.Model.Invoice> invoices = aXeroCompanyToCheckAgainst.GetAllPurchaseInvoicesOfTypesThatNeedToBeInAquariumFromXero(DateTime.Now.AddMonths(-3), DateTime.Now);

            //do something with these invoices

            return invoices;
        }
        /// <summary>
        /// Takes a service (Organisation / Company) and polls company for all invoices within certain date range       
        /// 
        /// Multiple overloads depending on what you're looking for - invoice types, from a particular contact, etc etc etc 
        /// </summary>
        /// <param name="aService"></param>
        public List<XeroApi.Model.Invoice> FIlterXeroInvoiceTypesForAquariumInsertComparison(InterResolveXeroService aXeroCompanyToCheckAgainst)
        {
            //this is the list we will be passing back and removing items from should they exist in Aquarium
            List<XeroApi.Model.Invoice> theXeroInvoices = aXeroCompanyToCheckAgainst.GetAllPurchaseInvoicesOfTypesThatNeedToBeInAquariumFromXero(DateTime.Now.AddMonths(-3), DateTime.Now);

            //now filter these invoices to only the correct 'types' e.g. account codes

             List<XeroApi.Model.Invoice> theFilteredXeroInvoices = (from invoice in theXeroInvoices
                                                                    where invoice.LineItems.Any(
                                                                        x => x.AccountCode == "54070"
                                                                        )
                                                                      ||
                                                                       invoice.LineItems.Any(
                                                                        x => x.AccountCode == "54110"
                                                                        )
                                                                         ||
                                                                       invoice.LineItems.Any(
                                                                        x => x.AccountCode == "51070"
                                                                        )
                                                                         ||
                                                                       invoice.LineItems.Any(
                                                                        x => x.AccountCode == "54110"
                                                                        )

                                                                        //ds
                                                                 //   .Where(X => X.LineItems.Any(x => x.AccountCode == "54110")) //ds

                                                                //    .Where(X => X.LineItems.Any(x => x.AccountCode == "51110")) //tp trf
                                                                //    .Where(X => X.LineItems.Any(x => x.AccountCode == "51070")) //tp trf
                                                                    select invoice).ToList();

                                                                   //AccountCode == "42010 - Third Party Income - Scheme Fees (Claimant)")

            //for that company look for invoice numbers of a certain type
            //that aren't in the LeadID and Inbound Invoice / Medical Treatment Table (PULL DISTINCT LIST OF INVOICE NUMBERS FROM AQ AND COMPARE)

            //For each Lead, write in necessary table row to :
            //MR or Treatment tables (optional)
            //Inbound invoice table //necessary

             return theFilteredXeroInvoices;
        }
        public bool Log(XeroApi.Model.Invoice anInvoice, InterResolveXeroService theService, LogCode Code)
        {
            //compose a line and write to file

            StreamWriter sw = new StreamWriter();

                //success
                if(Code == LogCode.OK)
                {
                    sw = File.AppendText(SuccessLogFileName + LogFilePath);

                }

                //fail
                else if (Code == LogCode.FAIL)
                {
                    sw = File.AppendText(FailureLogFileName + LogFilePath);
                    sw.WriteLine("Type:" + anInvoice.Type + "Company:" + theService.CompanyName + "Invoice Number:" + anInvoice.InvoiceNumber + " : " + "Ref:" + anInvoice.Reference + "Date:" + DateTime.Now + "NoOfErrors" + anInvoice.ValidationErrors.Count);
                }

            return true;
        }
        public ActionResult ImportInvoice()
        {
            //create the correct company

            //get the Invoices to import from the session
            ObservableCollection<Datalayer.Xero.Interresolve.OutboundInvoiceLineItem> theAquariumInvoicesToImportFromSession = (ObservableCollection<Datalayer.Xero.Interresolve.OutboundInvoiceLineItem>)Session["InvoicesToImport"];

            OutboundInvoiceLineItemList theAquariumInvoicesToImport = new OutboundInvoiceLineItemList();
            theAquariumInvoicesToImport.OutboundInvoiceLineItems = theAquariumInvoicesToImportFromSession.ToList();

            //convert from Aquarium invoices to Xero Invoices
            XeroApi.Model.Invoices theXeroInvoicesToImport = new XeroApi.Model.Invoices(); //add each Xero invoice to this list

            //for each invoice to be imported, pass in the list of invoices to factor out the line items into the correct invoice
            foreach (var AquariumInvoiceLineItem in theAquariumInvoicesToImport.OutboundInvoiceLineItems)
            {
                //pass in the invoice, then find all matching line items
                if (AquariumInvoiceLineItem.Result != "true") //then we can try and import it - security check
                {
                    //check we havent' done it already
                    int HaveWeDoneThisInvoiceAlready = (from invoice in theXeroInvoicesToImport
                                                        .Where(x => x.InvoiceNumber == AquariumInvoiceLineItem.InvoiceNumber.ToString())
                                                        select invoice).ToList().Count();

                    //we haven't imported it already -- run the proceedure to convert to xero invoice
                    if (HaveWeDoneThisInvoiceAlready < 1)
                    {
                        //convert to Xero Invoice

                        //need to check what company it is, so we can pull the correct contact
                        Datalayer.Xero.Interresolve.InterResolveXeroService aServiceInstanceToCheckTheCompany = new Datalayer.Xero.Interresolve.InterResolveXeroService(Datalayer.Xero.Interresolve.InterResolveXeroService.GetXeroOrganisationFromIRCompanyName(theAquariumInvoicesToImport.OutboundInvoiceLineItems.ElementAt(0).SageCompanyName));

                        XeroApi.Model.Invoice invoiceToAdd = OutboundInvoiceLineItemList.ConvertMultipleOutboundInvoiceLineItemsToXeroInvoice(theAquariumInvoicesToImport, AquariumInvoiceLineItem, aServiceInstanceToCheckTheCompany);

                        //Add invocie to import list
                        theXeroInvoicesToImport.Add(invoiceToAdd);

                    }

                }

            }

            ///////////////////////////////////////////////////
            //now for each invoice in the list, push to Xero
            ///////////////////////////////////////////////////

            //check the invoice company, and create the correct Xero login to that company

            for (var i = 0; i < theXeroInvoicesToImport.Count; i++)
            {

                //get the company name from the original Invoice list where invoice number = xxxxxx
                string IRCompanyName = (from Invoice in theAquariumInvoicesToImport.OutboundInvoiceLineItems
                                            .Where(x => x.InvoiceNumber == Convert.ToInt64(theXeroInvoicesToImport.ElementAt(i).InvoiceNumber))
                                        select Invoice.SageCompanyName

                                            ).Distinct().FirstOrDefault();

                string IRCompanyToPostTo = IRCompanyName;

                InterResolveXeroService.InterResolveXeroOrganisation XeroCompanyToPostTo = InterResolveXeroService.GetXeroOrganisationFromIRCompanyName(IRCompanyToPostTo);
                //Find which organsation we want to post to, and create XeroLogin class accordingly
                InterResolveXeroService aXeroService = new InterResolveXeroService(XeroCompanyToPostTo);
                //post each invoice to xero and check the result was 'OK'
                aXeroService.LoginToXero();

                var InvoiceResponse = aXeroService.XeroRepository.Create<XeroApi.Model.Invoice>(theXeroInvoicesToImport.ElementAt(i));

                //check response - write back to Aquarium that invoice was successfully imported, or not

                if (InvoiceResponse.ValidationStatus == XeroApi.Model.ValidationStatus.OK)
                {
                    Int64 TableRowID = XeroHelper.GetTableRowIDfromXeroInvoice(theXeroInvoicesToImport.ElementAt(i), theAquariumInvoicesToImport);

                    if (TableRowID != 0)
                    {
                        //call function to write back to Aquarium
                        var writeToAq = XeroHelper.XeroImportSuccessful_UpdateAquariumTableRow(TableRowID);
                    }

                }

                //tidy up
                aXeroService = null;

                //end
            }

            return View();
        }
        public void TestPDFPull()
        {
            //get the invoice from xero
            InterResolveXeroService theService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Ltd);
            theService.LoginToXero();

            List<XeroApi.Model.Invoice> theInvoicesToTest = new List<XeroApi.Model.Invoice>();
            List<XeroApi.Model.Invoice> theInvoicesToACTUALLYEXPORTBecauseXeroAPIDontWorkPropertly = new List<XeroApi.Model.Invoice>();
            List<IQueryable<XeroApi.Model.Invoice>> theIqueryableInvoices = new List<IQueryable<XeroApi.Model.Invoice>>();

            //gets invoices without lineItems
            theInvoicesToTest = (from invoices in theService.XeroRepository.Invoices
                                 where invoices.Status != "DELETED"
                                 && invoices.Type == "ACCPAY"
                                 select invoices
                                        ).ToList();

               //get the line items for each invoice
            for (int i = 0; i < theInvoicesToTest.Count; i++ )
            {

                System.Guid theInvoiceIDGUID = theInvoicesToTest.ElementAt(i).InvoiceID;

                var anInvoice = (from invoices in theService.XeroRepository.Invoices
                                 where invoices.InvoiceID == theInvoiceIDGUID
                                 select invoices).ToList();

                theInvoicesToACTUALLYEXPORTBecauseXeroAPIDontWorkPropertly.Add(anInvoice.ElementAt(0));

                anInvoice = null;

            }

            //turn the invoices into Aquarium Invoices
            List<InboundInvoice> AquariumInboundInvoices = new List<InboundInvoice>();

            foreach (var invoice in theInvoicesToACTUALLYEXPORTBecauseXeroAPIDontWorkPropertly)
            {
                AquariumInboundInvoices.Add(new InboundInvoice(invoice));
            }

            //GET THAT INVOICE'S PDF AND ASSIGN

            //to actually pull the pdf from Xero.
            //to assign it to this invoice's PDF container

            foreach (var invoice in AquariumInboundInvoices)
            {
                invoice.InvoicePDF = theService.XeroRepository.FindById<XeroApi.Model.Invoice>(invoice.XeroInvoiceID_GUID.ToString(), XeroApi.MimeTypes.ApplicationPdf);
            }

            Datalayer.Xero.Interresolve.AquariumEventManagement.EventManagementSoapClient theSDk = new Datalayer.Xero.Interresolve.AquariumEventManagement.EventManagementSoapClient();

             AquariumLogin login = new AquariumLogin();
             login.LoginToAquarium();

             LoggedOnUserResult theUserResult = login.GetLoggedOnUserResult();

             Datalayer.Xero.Interresolve.AquariumEventManagement.SessionDetails sessionDetails = new Datalayer.Xero.Interresolve.AquariumEventManagement.SessionDetails(); //set this from the logon
                //set the USER sesson
             sessionDetails.SessionKey = theUserResult.SessionKey;
             sessionDetails.Username = theUserResult.Username;
             sessionDetails.ThirdPartySystemId = 29;

            InboundInvoice ARandomInvoice = AquariumInboundInvoices.ElementAt(1);

            Datalayer.Xero.Interresolve.AquariumEventManagement.FollowupOptions followUPOption = new Datalayer.Xero.Interresolve.AquariumEventManagement.FollowupOptions();

            //try and INSERT THE PDF INTO AQUARIUM
            Datalayer.Xero.Interresolve.AquariumEventManagement.EventResult didThePDFGoIn = theSDk.AddDocumentInEvent(sessionDetails, 66053, 1033432, 970452, ARandomInvoice.InvoicePDF, "A Title For The Docu", "Invoice.PDF", followUPOption);

            //FINALLY, PUT THE TABLE ROW INTO THE TABLE AND CONFIRM THAT IMPORT WAS SUCCESFUL
        }
        public void TestXeroLogin()
        {
            //  var test = InboundInvoice.GetAllAquariumPurchaseInvoiceNumbers();

            List<InterResolveXeroService> AllTheInterResolveCompanies = new List<InterResolveXeroService>();

            InterResolveXeroService InterResolve_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Ltd);
            InterResolveXeroService MotorResolve_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.MotorResolve_Ltd);
            InterResolveXeroService InterMediation_Group_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterMediation_Group_Ltd);
            InterResolveXeroService InterResolve_Claims_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Claims_Ltd);
            //    InterResolveXeroService InterResolve_Holdings_LtdService = new InterResolveXeroService(InterResolveXeroService.InterResolveXeroOrganisation.InterResolve_Holdings_Ltd);

            AllTheInterResolveCompanies.Add(InterResolve_LtdService);
            AllTheInterResolveCompanies.Add(MotorResolve_LtdService);
            AllTheInterResolveCompanies.Add(InterMediation_Group_LtdService);
            AllTheInterResolveCompanies.Add(InterResolve_Claims_LtdService);
              //      AllTheInterResolveCompanies.Add(InterResolve_Holdings_LtdService);

            OutboundInvoiceLineItemList someInvoices = new OutboundInvoiceLineItemList();

            someInvoices.Get();

            XeroApi.Model.Invoice anInvoice = OutboundInvoiceLineItem.ConvertSingleLineItemOutboundInvoiceToXeroInvoice(someInvoices.OutboundInvoiceLineItems.FirstOrDefault());

            foreach (var company in AllTheInterResolveCompanies)
            {
                company.LoginToXero();
                anInvoice.LineItems.ElementAt(0).LineAmount = 20;
                anInvoice.LineItems.ElementAt(0).UnitAmount = 20;

                XeroApi.Model.Contact theXeroContact = new XeroApi.Model.Contact();
                theXeroContact.Name = "Test Contact";

                anInvoice.Contact = theXeroContact;
                var InvoiceResponse = company.XeroRepository.Create<XeroApi.Model.Invoice>(anInvoice);
            }
        }