-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fatal error on non-numerical quantity #40
Comments
Related code: wp-pronamic-pay-gravityforms/src/Processor.php Lines 353 to 365 in b327317
At first i noticed the quantity field is of the type <input type="number" name="input_1.3" value="" id="input_1_1_1" class="ginput_quantity" size="10" min="0" aria-label="Quantity Product A" aria-describedby="ginput_product_price_1_1"> This field does not allows values like I know that in Moneybird you can also use other quantity values:
Perhaps we should process this in a similar way @rvdsteege? ChatGPT provided the following: function extractNumberFromString($input) {
// Regular expression om het eerste numerieke getal in de string te vinden
preg_match('/\d+/', $input, $matches);
// Als er een match is gevonden, retourneer het eerste getal, anders retourneer 0
if (!empty($matches)) {
return intval($matches[0]);
} else {
return 0;
}
}
// Testen van de functie met verschillende inputs
echo extractNumberFromString("1 x"); // Output: 1
echo extractNumberFromString("3 stuks"); // Output: 3
echo extractNumberFromString("5 dozen"); // Output: 5
echo extractNumberFromString("14 m^2"); // Output: 14 And with time function extractNumberFromString($input) {
// Replace spaces with empty strings to handle thousands separators
$input = str_replace(' ', '', $input);
// Regular expression to find the first numeric value in the string
preg_match('/\d+(:\d+)?/', $input, $matches);
// If a match is found
if (!empty($matches)) {
// Split the found number on ':' if it exists
$number_parts = explode(':', $matches[0]);
// If the number of parts is 1, simply return that number
if (count($number_parts) == 1) {
return intval($number_parts[0]);
}
// If the number of parts is 2, calculate the number as a decimal
elseif (count($number_parts) == 2) {
return intval($number_parts[0]) + intval($number_parts[1]) / 60;
}
}
// If no match is found, return 0
return 0;
}
// Testing the function with various inputs
echo extractNumberFromString("1 x"); // Output: 1
echo "\n";
echo extractNumberFromString("3 stuks"); // Output: 3
echo "\n";
echo extractNumberFromString("5 dozen"); // Output: 5
echo "\n";
echo extractNumberFromString("14 m^2"); // Output: 14
echo "\n";
echo extractNumberFromString("01:00"); // Output: 1
echo "\n";
echo extractNumberFromString("01:30"); // Output: 1.5
echo "\n";
echo extractNumberFromString("1 000"); // Output: 1000
echo "\n"; In Moneybird they even store these values separate in "details": [
{
"id": "416087652863837643",
"administration_id": 123,
"tax_rate_id": "416087461198825343",
"ledger_account_id": "416087461088724843",
"project_id": null,
"product_id": null,
"amount": "1 x",
"amount_decimal": "1.0",
"description": "Project X",
"price": "300.0",
"period": "20240301..20240331",
"row_order": 1,
"total_price_excl_tax_with_discount": "300.0",
"total_price_excl_tax_with_discount_base": "300.0",
"tax_report_reference": [
"NL/1a"
],
"mandatory_tax_text": null,
"created_at": "2024-03-21T14:42:09.629Z",
"updated_at": "2024-03-21T14:42:09.757Z"
}
], Source: https://developer.moneybird.com/api/sales_invoices/#get_sales_invoices_id |
I think there is little use for us in storing the non-numerical amount? But a 'parser' like suggested seems like a good solution. However, I know you're not a big fan of |
I'm not a fan of using complex regular expressions that are difficult to understand. Especially if it can also be solved with some string functions. In this case, a regular expression can be quite useful. However, I was still thinking about how we can properly handle decimal and thousand separators.
We need to know the decimal and thousand separators to be able to parse a text properly? Doesn't https://github.com/pronamic/wp-number/blob/main/src/Parser.php already take care of this for us? |
A fatal error occurs when a non-numerical value is provided as quantity.
Internal Help Scout ticket: https://secure.helpscout.net/conversation/2568111710/27116
The text was updated successfully, but these errors were encountered: