I am in the search for a solution to allow the loading of an XML file where the value named as Total allows a difference of 1 cent above or below the actual value, for this solution I am getting the value of Total
from the XML file as follows.
$xml = new SimpleXMlElement( $_FILES['XmlToUpload']['tmp_name'], 0, true );
$total = (float)$xml['Total'];
To explain in detail what I want to achieve I will put the following example, when reading the Total
node of the XML file this gets as a value the following Total= "9840.00"
, what I want to allow when loading the XML file is that it allows that total to have a difference of more or less 1 cent, that is, that even if the Total of the XML file has a value of Total="9839.99"
or Total="9840.01"
it allows the file to be loaded.
The XML file is loaded as follows:
$fileXML = $_FILES['XmlToUpload']['name'];
$pathXML = "//LOCATION/XML/";
$filepathXML = $pathXML.$fileXML;
if(move_uploaded_file( $_FILES['XmlToUpload']['tmp_name'], $pathXML . $fileXML)){
echo 'Success Upload File';
}
I hope someone can give me some guidance on how to do this validation.
Update 1:
I add a short and representative sample of my XML file in which the Total
node that was desired to validate is located
<cfdi:Voucher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Certificated="m4gfzi9yNXuC0A="
Condition="002"
Date="2021-06-29T16:02:16"
Number="4938"
Payment="23"
NoCertificated="404627114"
Total="9840.00"
Version="3.3">
</cfdi:Voucher>
Update 2:
I tried to make a modification in my code based on one of the answers they give me, but I can't exist, it may be some additional error I have, what I tried was to add two new variables, one adding to the total an amount of "0.01" and the other variable subtract the amount of "0.01".
The variables are declared as follows:
$totalMgS = $total + "0.01";
$totalMgI = $total - "0.01";
It is important to make it clear that $total
I mentioned before at the beginning of the question, which I use to read the XML file.
Based on one of the responses I created the following validation in the loading section of my XML file:
if($total <= $totalMgS && $total >= $totalMgI){
if(move_uploaded_file( $_FILES['XmlToUpload']['tmp_name'], $pathXML . $fileXML)){
echo 'Success Upload File';
}
}else{
echo "It doesn't fit!";
When I test an XML file where $total
has a difference of 0.01, the file is not loaded and throws me the message of It doesn't fit!
.
Any changes I need to make to my validation?
Update 3:
Going back to the topic again and to validate it, I was analyzing all this time that I have to compare the total with another, and indeed now I visualize it like this, that other number with which I have to compare it is stored in a database table which I am displaying in an HTML tag as follows:
<div class="form-group row" >
<label for="lblName" class="col-sm-3 col-form-label">Total:</label>
<div class="col-sm-8">
<input type="text" name="txtTotal" class="form-control" id="txtTotal" disabled>
</div>
</div>
Then I declare in my Javascript the variable:
var TotalTable = $("#txtTotal").val();
Then I add it to my data to later be called from PHP.
datosForm.append("TotalTable", $("#txTotal").val());
And finally I call it from PHP.
$TotalReal = (isset($_POST["TotalTable"]));
I have called my variable in PHP as $TotalReal
To validate that the total is the same that I have in my HTML tag and that it is the same that I have stored in the database, I print the variable $TotalReal
but this only returns 1.
I am printing it as follows:
echo $TotalReal;
Is there something I need to change in how I am getting the variable from Javascript? Or do I have to do some conversion to data type float?