/// <summary> /// Gets the vat returns. /// </summary> /// <param name="userName">Name of the user.</param> /// <param name="password">The password.</param> /// <returns></returns> public async Task <HmrcVatReturnsInfo> GetVatReturns(string userName, string password) { InfoAccumulator info = ValidateUserNamePassword(userName, password); if (info.HasErrors) { return(ReturnError(info)); } info = await Login(userName, password); if (info.HasErrors) { return(ReturnError(info)); } //try to obtain user's VAT id var taxOfficeNumberAndVatId = await VatIdAndTaxOfficeNumberFetcher.GetUserVatIdAndTaxOfficeNumber(baseAddress, Browser); if (string.IsNullOrEmpty(taxOfficeNumberAndVatId.VatId)) { return(ReturnError("could not obtain customer's vat id")); } RtiTaxYearInfo yearInfo = null; if (string.IsNullOrEmpty(taxOfficeNumberAndVatId.TaxOfficeNumber)) { Log.Debug("Not fetching RTI Tax Years: Tax Office number is empty."); } else { yearInfo = await RtiTaxYearsFetcher.GetRtiTaxYears(taxOfficeNumberAndVatId.TaxOfficeNumber, baseAddress, Browser); } IEnumerable <VatReturnInfo> vatReturnInfos = await VatReturnsInfoFetcher.GetVatReturns(taxOfficeNumberAndVatId.VatId, baseAddress, Browser); return(new HmrcVatReturnsInfo { TaxOfficeNumber = taxOfficeNumberAndVatId.TaxOfficeNumber, VatReturnInfos = vatReturnInfos, RtiTaxYearInfo = yearInfo, Info = info }); }
/// <summary> /// Parses the specified document. /// </summary> /// <param name="document">The document.</param> /// <returns></returns> /// <exception cref="System.IO.InvalidDataException"> /// RTI tax years table head is empty. /// or /// Failed to fetch RTI tax years: no cells in header row /// or /// RTI tax years table body not found. /// or /// RTI tax years data not found. /// </exception> public RtiTaxYearInfo Parse(HtmlDocument document) { HtmlNode oTHead = document.DocumentNode.SelectSingleNode("//*[@id=\"top\"]/div[3]/div[2]/div/div[2]/table[1]/thead"); if (oTHead == null) { Log.Info("RTI tax years table head not found."); return(null); } // if HtmlNodeCollection oHeadRows = oTHead.SelectNodes("tr"); if ((oHeadRows == null) || (oHeadRows.Count != 1)) { throw new InvalidDataException("RTI tax years table head is empty."); } HtmlNodeCollection oHeadCells = oHeadRows[0].SelectNodes("th | td"); string[] aryExpectedColumnHeaders = { "Date", "Amount paid in period", "Amount due in period", }; if ((oHeadCells == null) || (oHeadCells.Count != aryExpectedColumnHeaders.Length)) { throw new InvalidDataException("Failed to fetch RTI tax years: no cells in header row"); } for (int i = 0; i < aryExpectedColumnHeaders.Length; i++) { if (!oHeadCells[i].InnerText.Trim() .StartsWith(aryExpectedColumnHeaders[i])) { Log.InfoFormat( "Not fetching RTI tax years: unexpected column {0} name: {1} (expected: {2})", i, oHeadCells[i].InnerText, aryExpectedColumnHeaders[i] ); return(null); } // if } // for HtmlNode oTBody = document.DocumentNode.SelectSingleNode("//*[@id=\"top\"]/div[3]/div[2]/div/div[2]/table[1]/tbody"); if (oTBody == null) { throw new InvalidDataException("RTI tax years table body not found."); } HtmlNodeCollection oRows = oTBody.SelectNodes("tr"); if ((oRows == null) || (oRows.Count < 1)) { throw new InvalidDataException("RTI tax years data not found."); } bool isFirst = true; int rowNum = -1; int firstYear = 0; int lastYear = 0; var data = new List <RtiTaxMonthSection>(); foreach (HtmlNode oTR in oRows) { rowNum++; HtmlNodeCollection cells = oTR.SelectNodes("th | td"); if ((cells == null) || (cells.Count < 1)) { throw new InvalidDataException(string.Format( "Failed to fetch RTI tax years: no cells in row {0}.", rowNum )); } // if if (isFirst) { isFirst = false; HtmlNode cell = cells[0]; if (!cell.Attributes.Contains("colspan") || (cell.Attributes["colspan"].Value != "3")) { throw new InvalidDataException(string.Format( "Failed to fetch RTI tax years: incorrect format in row {0}", rowNum )); } // if if (cell.InnerText.Trim() == "Previous tax years") { break; } MatchCollection match = Regex.Matches(cell.InnerText.Trim(), @"^Current tax year (\d\d)(\d\d)-(\d\d)$"); if (match.Count != 1) { throw new InvalidDataException(string.Format( "Failed to fetch RTI tax years: incorrect content in row {0}.", rowNum )); } // if GroupCollection grp = match[0].Groups; if (grp.Count != 4) { throw new InvalidDataException(string.Format( "Failed to fetch RTI tax years: unexpected content in row {0}.", rowNum )); } // if firstYear = Convert.ToInt32(grp[1].Value) * 100 + Convert.ToInt32(grp[2].Value); lastYear = Convert.ToInt32(grp[1].Value) * 100 + Convert.ToInt32(grp[3].Value); Log.InfoFormat("Current tax year: {0} - {1}", firstYear, lastYear); continue; } // if first row string sFirstCell = cells.Count > 0 ? cells[0].InnerText.Trim() : string.Empty; if (cells.Count != 3) { if ((cells.Count == 1) && (sFirstCell == "Previous tax years")) { break; } throw new InvalidDataException(string.Format( "Failed to fetch RTI tax years: unexpected number of cells in row {0}.", rowNum )); } // if if (sFirstCell == "Total") { break; } try { data.Add(CreateRtiTaxYearSection(sFirstCell, cells[1].InnerText.Trim(), cells[2].InnerText.Trim())); } catch (Exception e) { throw new InvalidDataException( string.Format( "Failed to fetch RTI tax years: unexpected format in row {0}.", rowNum ), e ); } // try } // for each row RtiTaxYearInfo yearInfo = new RtiTaxYearInfo { Months = CreateRttMonthInfos(data, firstYear, lastYear) }; Log.Debug("Fetching RTI Tax Years complete."); return(yearInfo); }