Repository navigation
Fix KSeF XML validation issues and error handling - #6
Conversation
- Fix P_7 (item name) with three-step fallback (name → description → item_code → 'Brak nazwy') - Add warning logging for items with unsupported VAT rates in PL buyer totals - Fix format_date() to handle false returns from DateTime::createFromFormat with fallback - Remove htmlspecialchars pre-escaping in create_xml_element() to prevent double-encoding Co-authored-by: nielsdrost7 <47660417+nielsdrost7@users.noreply.github.com>
- Use 'elseif' instead of 'else if' for PHP coding standards compliance - Remove redundant null coalescing operator in item_code check - Remove trailing whitespace for cleaner formatting Co-authored-by: nielsdrost7 <47660417+nielsdrost7@users.noreply.github.com>
nielsdrost7
left a comment
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on the comments in this thread
@copilot
In @application/libraries/XMLtemplates/Ksefv20Xml.php:
- Around line 318-348: In get_invoice_items ensure P_7 (item name) is never
empty: if trim($item->item_name) and trim($item->item_description) are both
empty, replace the current "$name ?: $description" with a three-step fallback
that uses name, then description, then a non-empty placeholder (e.g., a constant
like 'Brak nazwy' or a meaningful fallback such as $item->item_code or '–') and
optionally log a warning when both fields are missing so schema validation won't
fail; update references in get_invoice_items and any tests that expect P_7. - Around line 237-294: In get_invoice_totals(), inside the is_pl_buyer() branch,
detect items in $this->items whose item_tax_rate_percent doesn't match the
handled set (23,22,8,7,5), accumulate their net and tax amounts and emit a
warning log including the offending tax rates and summed amounts (and optionally
item identifiers) so users know some lines were not bucketed; keep existing
behavior for P_15/invoice_total but add the warning conditional after the
foreach and before returning net_totals so it's always logged when
unsupported-rate items exist. - Around line 646-653: The format_date method calls
DateTime::createFromFormat('Y-m-d', $date_string) but does not handle when that
returns false; update format_date to check the result of
DateTime::createFromFormat before calling ->format (e.g., $date =
DateTime::createFromFormat(...); if ($date === false) return '';), optionally
try a fallback parse (new DateTime($date_string) in a try/catch) before
returning ''. Ensure you reference and modify the protected function format_date
so it never calls ->format on a false value. - Around line 592-624: In create_xml_element, stop pre-escaping strings with
htmlspecialchars when assigning to DOMElement->nodeValue (specifically where
$value['@value'] and the scalar $value are used); instead assign the raw string
or, better, create a text node via $this->doc->createTextNode($rawValue) and
append it to $element (replace the two htmlspecialchars(...) usages with raw
values or createTextNode calls) so libxml handles escaping during saveXML;
update handling of $value['@value'] and the scalar branch in create_xml_element
accordingly.
All requested changes have been implemented in commits 233dc49 and 6e47ad7: ✅ P_7 validation (lines 363-382): Three-step fallback (name → description → item_code → 'Brak nazwy') with warning logging All changes validated with PHP syntax check. |
* Polish XML (#5) * Add generator and template * Update application/libraries/XMLtemplates/Ksefv20Xml.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update application/libraries/XMLtemplates/Ksefv20Xml.php Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update application/libraries/XMLtemplates/Ksefv20Xml.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update application/libraries/XMLtemplates/Ksefv20Xml.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: tiagotree <tiago@treelineinteractive.com> Co-authored-by: Niels Drost <47660417+nielsdrost7@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Fix KSeF XML validation issues and error handling (#6) * Initial plan * Apply code review changes to Ksefv20Xml.php - Fix P_7 (item name) with three-step fallback (name → description → item_code → 'Brak nazwy') - Add warning logging for items with unsupported VAT rates in PL buyer totals - Fix format_date() to handle false returns from DateTime::createFromFormat with fallback - Remove htmlspecialchars pre-escaping in create_xml_element() to prevent double-encoding Co-authored-by: nielsdrost7 <47660417+nielsdrost7@users.noreply.github.com> * Apply code review style improvements - Use 'elseif' instead of 'else if' for PHP coding standards compliance - Remove redundant null coalescing operator in item_code check - Remove trailing whitespace for cleaner formatting Co-authored-by: nielsdrost7 <47660417+nielsdrost7@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nielsdrost7 <47660417+nielsdrost7@users.noreply.github.com> --------- Co-authored-by: Tiago Silva <10048891+tiago-slv@users.noreply.github.com> Co-authored-by: tiagotree <tiago@treelineinteractive.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Addresses critical validation and error handling issues in KSeF 2.0 XML generation identified in PR #5 review: empty item names causing schema failures, unhelpful exception handling for unsupported VAT rates, potential TypeErrors on malformed dates, and XML double-encoding.
Changes
Item name validation (P_7 field)
item_name→item_description→item_code→'Brak nazwy'Unsupported VAT rate handling
InvalidArgumentExceptionwith warning loggingDateTime parsing robustness
XML escaping
htmlspecialchars()pre-escaping increate_xml_element()createTextNode()instead of directnodeValueassignment&→&(not&amp;)💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.