/// <summary> /// Adds a new VAT Return Submission to the VAT100 database table following receipt of an Acknowledgement. /// </summary> /// <param name="companyCode"></param> /// <param name="documentType"></param> /// <param name="userName"></param> /// <param name="request"></param> /// <param name="acknowledgement"></param> /// <returns></returns> public bool Add(string companyCode, string documentType, string userName, VAT100_GovTalkMessage request, VAT100_Acknowledgement acknowledgement) { // Create a Document for use by the Polling functions. DocumentRecord newDocument = new DocumentRecord(); newDocument.theDocument = new VAT100Record(); newDocument.companyCode = companyCode; newDocument.companyPath = FDatabase.GetCompanyPath(companyCode); // Fill in the details newDocument.theDocument.correlationID = acknowledgement.Header.MessageDetails.CorrelationID; newDocument.theDocument.IRMark = request.Body.IRenvelope.IRheader.IRMark.Value; DateTime timeNow = DateTime.Now; newDocument.theDocument.dateSubmitted = timeNow.ToString("ddMMyyyy HHmmss"); newDocument.theDocument.documentType = documentType; newDocument.theDocument.VATPeriod = request.Body.IRenvelope.IRheader.PeriodID; newDocument.theDocument.username = userName; newDocument.theDocument.status = (short)SubmissionStatus.ssSubmitted; newDocument.theDocument.pollingInterval = acknowledgement.Header.MessageDetails.ResponseEndPoint.PollInterval; newDocument.theDocument.VATDueOnOutputs = request.Body.IRenvelope.VATDeclarationRequest.VATDueOnOutputs; newDocument.theDocument.VATDueOnECAcquisitions = request.Body.IRenvelope.VATDeclarationRequest.VATDueOnECAcquisitions; newDocument.theDocument.VATTotal = request.Body.IRenvelope.VATDeclarationRequest.TotalVAT; newDocument.theDocument.VATReclaimedOnInputs = request.Body.IRenvelope.VATDeclarationRequest.VATReclaimedOnInputs; newDocument.theDocument.VATNet = request.Body.IRenvelope.VATDeclarationRequest.NetVAT; newDocument.theDocument.netSalesAndOutputs = request.Body.IRenvelope.VATDeclarationRequest.NetSalesAndOutputs; newDocument.theDocument.netPurchasesAndInputs = request.Body.IRenvelope.VATDeclarationRequest.NetPurchasesAndInputs; newDocument.theDocument.netECSupplies = request.Body.IRenvelope.VATDeclarationRequest.NetECSupplies; newDocument.theDocument.netECAcquisitions = request.Body.IRenvelope.VATDeclarationRequest.NetECAcquisitions; // PKR. 16/09/2015. ABSEXCH-16865. Add a temporary narrative so that the Submission record isn't empty. newDocument.theDocument.hmrcNarrative = "Acknowledgement received from HMRC."; newDocument.theDocument.PollingURL = acknowledgement.Header.MessageDetails.ResponseEndPoint.EndPoint; // Save the pending document int Res = 0; // CJS 2016-06-06 - ABSEXCH-17494 - VAT submission returning HMRC message to wrong company. // If we are not polling, it probably means that we are searching for or processing // an existing document, in which case it is not safe to use the VAT100Database singleton, // because it is already being used and might be open in the wrong company. Instead use // a new instance. if (!pollingTimer.Enabled) { Res = VAT100Database.GetNewInstance().AddVAT100Entry(newDocument.theDocument, companyCode); } else { Res = VAT100Database.Instance.AddVAT100Entry(newDocument.theDocument, companyCode); } if (Res != 0) { Log.Add(string.Format("Failed to save pending document for {0}. Error code : {1}", companyCode, Res)); return(false); } else { return(true); } }
//--------------------------------------------------------------------------------------------- /// <summary> /// Submit an XML file to HMRC and adds it to the polling service to /// wait for a response. This method returns the immediate response, /// which will be an XML string containing either an acknowledgement /// that the submission was received by HMRC, or an error. /// </summary> /// <returns></returns> public string SubmitToHMRC(string companycode, string doctype, string xmldoc, string filename, string suburl, string username, string email) { string Result = string.Empty; Console.WriteLine(""); Console.WriteLine("SubmitToHMRC request"); Log.Add("SubmitToHMRC request received for company " + companycode); // Can only accept a submission if none are in progress for this company. if (FPollingService.Status(companycode) == ServiceStatus.ssIdle) { // Add the IRMark xmldoc = ApplyIRMark(xmldoc); // CJS 2015-09-24 - ABSEXCH-16922 - HMRC Filing VAT Submissions xml folder // Save a copy of the submitted file XMLWrite.ToSentFolder(dbHandler.tToolkit.Configuration.EnterpriseDirectory, filename, xmldoc); // Store the parameters locally in the pending document FDocType = doctype; FXMLDoc = xmldoc; //........................................................................................... // Prepare for the submission string hmrcURL = string.Empty; if (string.IsNullOrEmpty(username)) { username = "******"; } // Deserialize the XML to an hierarchical class structure so we can save the details VAT100_GovTalkMessage outMessage = DeserializeFromXmlString <VAT100_GovTalkMessage>(xmldoc); // Save the message class (HMRC-VAT-DEC or HMRC-VAT-DEC-TIL) FDocClass = outMessage.Header.MessageDetails.Class; // Determine where we want to send the data (Live or Dev) // The message class (HMRC-VAT-DEC or HMRC-VAT-DEC-TIL) will already be in the message sent by Exchequer. hmrcURL = suburl; //........................................................................................... // Submit the XML to HMRC and await the acknowledgement response // Create a request var request = (HttpWebRequest)WebRequest.Create(hmrcURL); var data = Encoding.ASCII.GetBytes(xmldoc); string responseXML = string.Empty; // LogText(PrettyPrinter.PrettyPrintXML(XMLDoc)); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; // Write the data to the request parameters using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } // Log.Add("Submitting request to " + hmrcURL); // Console.WriteLine("Submitting request to " + hmrcURL); // Send the request and get the response. var response = (HttpWebResponse)request.GetResponse(); // Get the response in a stream. var responseData = new StreamReader(response.GetResponseStream()).ReadToEnd(); // Convert the stream to a string responseXML = responseData.ToString(); // LogText(PrettyPrinter.PrettyPrintXML(responseXML)); // Clipboard.SetData(System.Windows.Forms.DataFormats.Text, responseXML); // Deserialise the response VAT100_Acknowledgement acknowledgementMsg; XmlSerializer serialiser = new XmlSerializer(typeof(VAT100_Acknowledgement)); using (StringReader reader = new StringReader(responseXML)) { acknowledgementMsg = (VAT100_Acknowledgement)(serialiser.Deserialize(reader)); } //........................................................................................... // Determine the response type. // For a successful submission, this will be "acknowledgement" // For a submission failure, this will be "error" string responseType = acknowledgementMsg.Header.MessageDetails.Qualifier; switch (responseType.ToLower()) { case "acknowledgement": Log.Add("Acknowledgement received"); FPollingService.Add(companycode, doctype, username, outMessage, acknowledgementMsg); break; case "error": Log.Add("Error response received from HMRC"); break; default: Log.Add("Unrecognised response from HMRC"); // Unrecognised response qualifier break; } Result = responseXML; } else { // Tried to submit a return while one is in progress. FErrorString = "A VAT 100 return submission is currently being processed"; Log.Add(FErrorString); Result = string.Empty; } return(Result); }