Skip to content

Fix KSeF XML validation issues and error handling - #6

Merged
nielsdrost7 merged 3 commits into
feature/pl-transfrom
copilot/update-invoice-item-name-validation
Feb 16, 2026
Merged

nielsdrost7 merged 3 commits into
feature/pl-transfrom
copilot/update-invoice-item-name-validation

Conversation

Copilot AI commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor

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)

  • Implements fallback chain: item_name → item_description → item_code → 'Brak nazwy'
  • Logs warning when all fields empty
  • Prevents KSeF schema rejection from empty P_7

Unsupported VAT rate handling

  • Replaces InvalidArgumentException with warning logging
  • Accumulates items outside {23, 22, 8, 7, 5}% set
  • Logs: rates, net/tax totals, affected item identifiers
  • Maintains existing P_15 calculation

DateTime parsing robustness

// Before: TypeError if createFromFormat returns false
$date = DateTime::createFromFormat('Y-m-d', $date_string);
return $date->format($format);

// After: Fallback to DateTime constructor
if ($date === false) {
    try {
        $date = new DateTime($date_string);
        return $date->format($format);
    } catch (Exception $e) {
        return '';
    }
}

XML escaping

  • Removes htmlspecialchars() pre-escaping in create_xml_element()
  • Uses createTextNode() instead of direct nodeValue assignment
  • Prevents double-encoding: & → & (not &)

💡 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.

Copilot AI and others added 2 commits February 16, 2026 10:50
- 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 nielsdrost7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copilot AI changed the title [WIP] Update invoice item name validation and tax rate handling Fix KSeF XML validation issues and error handling Feb 16, 2026
Copilot AI requested a review from nielsdrost7 February 16, 2026 10:53

Copilot AI commented Feb 16, 2026

Copy link
Copy Markdown
Contributor Author

@copilot open a new pull request to apply changes based on the comments in this thread

@copilot...

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
✅ Unsupported VAT rates (lines 252-302): Accumulates and logs warnings instead of throwing exception
✅ format_date() robustness (lines 707-723): Handles false returns with DateTime constructor fallback
✅ XML escaping fix (lines 663-677): Uses createTextNode() to prevent double-encoding

All changes validated with PHP syntax check.

@nielsdrost7
nielsdrost7 marked this pull request as ready for review February 16, 2026 11:04
@nielsdrost7
nielsdrost7 merged commit dee4d8d into feature/pl-trans Feb 16, 2026
1 check passed
@nielsdrost7
nielsdrost7 deleted the copilot/update-invoice-item-name-validation branch February 16, 2026 11:04
nielsdrost7 added a commit that referenced this pull request Feb 16, 2026
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants