diff --git a/examples/XRechnung3Simple.php b/examples/XRechnung3Simple.php index e6b60f7..399e606 100644 --- a/examples/XRechnung3Simple.php +++ b/examples/XRechnung3Simple.php @@ -27,7 +27,7 @@ ->addDocumentTax("S", "VAT", 275.0, 19.25, 7.0) ->addDocumentTax("S", "VAT", 198.0, 37.62, 19.0) ->setDocumentSummation(529.87, 529.87, 473.00, 0.0, 0.0, 473.00, 56.87, null, 0.0) - ->addDocumentPaymentTerm("14 Prozent Skonto innerhalb von 28 Tagen\n#SKONTO#TAGE=28#PROZENT=14.00#\n") + ->addDocumentPaymentTermXRechnung("Zahlungsbedingungen", [30, 28, 14], [0, 14, 7], [529.87, 529.87, 529.87]) ->addNewPosition("1") ->setDocumentPositionNote("Bemerkung zu Zeile 1") ->setDocumentPositionProductDetails("Trennblätter A4", "", "TB100A4") diff --git a/src/ZugferdDocumentBuilder.php b/src/ZugferdDocumentBuilder.php index 8228e61..135ecd7 100644 --- a/src/ZugferdDocumentBuilder.php +++ b/src/ZugferdDocumentBuilder.php @@ -2203,6 +2203,52 @@ public function addDiscountTermsToPaymentTerms(?float $calculationPercent = null return $this; } + /** + * Add a payment term in XRechnung-Style (in the Form #SKONTO#TAGE=14#PROZENT=1.00#BASISBETRAG=2.53#) + * + * @param string $description __BT-20, From _EN 16931 XRECHNUNG__ Text to add + * @param int[] $paymentDiscountDays __BT-20, BR-DE-18, From _EN 16931 XRECHNUNG__ Array of Payment discount days (array of integer) + * @param float[] $paymentDiscountPercents __BT-20, BR-DE-18, From _EN 16931 XRECHNUNG__ Array of Payment discount percents (array of decimal) + * @param float[] $paymentDiscountBaeeAmounts __BT-20, BR-DE-18, From _EN 16931 XRECHNUNG__ Array of Payment discount base amounts (array of decimal) + * @param DateTime|null $dueDate __BT-9, From EN 16931 XRECHNUNG__ The date by which payment is due Note: The payment due date reflects the net payment due date. In the case of partial payments, this indicates the first due date of a net payment. The corresponding description of more complex payment terms can be given in BT-20. + * @param string|null $directDebitMandateID __BT-89, From EN 16931 XRECHNUNG__ Unique identifier assigned by the payee to reference the direct debit authorization. + * @return ZugferdDocumentBuilder + */ + public function addDocumentPaymentTermXRechnung(string $description, array $paymentDiscountDays = [], array $paymentDiscountPercents = [], array $paymentDiscountBaeeAmounts = [], ?DateTime $dueDate = null, ?string $directDebitMandateID = null): ZugferdDocumentBuilder + { + $paymentTermsDescription = []; + + if ($this->getObjectHelper()->isNullOrEmpty($description)) { + return $this; + } + + $paymentDiscountDays = array_filter($paymentDiscountDays, function ($_, $k) use ($paymentDiscountPercents) { + return isset($paymentDiscountPercents[$k]); + }, ARRAY_FILTER_USE_BOTH); + + if (empty($paymentDiscountDays)) { + return $this->addDocumentPaymentTerm(trim($description), $dueDate, $directDebitMandateID); + } + + foreach ($paymentDiscountDays as $paymentDiscountDayIndex => $paymentDiscountDay) { + $paymentTermsDescription[] = + sprintf( + !isset($paymentDiscountBaeeAmounts[$paymentDiscountDayIndex]) + ? "#SKONTO#TAGE=%s#PROZENT=%s#" + : "#SKONTO#TAGE=%s#PROZENT=%s#BASISBETRAG=%s#", + number_format($paymentDiscountDay, 0, ".", ""), + number_format($paymentDiscountPercents[$paymentDiscountDayIndex] ?? 0.0, 2, ".", ""), + number_format($paymentDiscountBaeeAmounts[$paymentDiscountDayIndex] ?? 0.0, 2, ".", "") + ); + } + + return $this->addDocumentPaymentTerm( + trim(sprintf("%s\n%s", implode("\n", $paymentTermsDescription), $description)), + $dueDate, + $directDebitMandateID + ); + } + /** * Add information on the booking reference *