Skip to content

Commit b650419

Browse files
committed
Allow to generate reference numbers including invoice and customer numbers
1 parent b4fa533 commit b650419

File tree

3 files changed

+93
-10
lines changed

3 files changed

+93
-10
lines changed

src/Entity/PaymentOrder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ class PaymentOrder implements DBElementInterface, TimestampedElementInterface, \
161161
private string $project_name = '';
162162

163163
#[ORM\Column(type: Types::STRING, nullable: true)]
164-
#[Assert\Length(max: 20)]
164+
#[Assert\Length(max: 23)]
165165
private ?string $invoice_number = null;
166166

167167
#[ORM\Column(type: Types::STRING, nullable: true)]
168-
#[Assert\Length(max: 20)]
168+
#[Assert\Length(max: 23)]
169169
private ?string $customer_number = null;
170170

171171
/*******************************************************************************************************************

src/Services/PaymentReferenceGenerator.php

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,47 @@ public function setPaymentReference(PaymentOrder $paymentOrder): void
4747
public function generatePaymentReference(PaymentOrder $paymentOrder): string
4848
{
4949
//Max 140 chars are allowed for a payment reference
50-
//Format: [ProjectName 70] [FSR Name 45] [?Funding ID 11] ZA[PaymentOrder ID]
50+
//Format: R.-Nr. [Invoice-Number 20] Kd.-Nr. [Customer-Number 20]
51+
//Format: [ProjectName 70] [FSR Name 35] [?Funding ID 20] ZA[PaymentOrder ID 9]
52+
53+
if (!empty($paymentOrder->getInvoiceNumber()) || !empty($paymentOrder->getCustomerNumber())) {
54+
$tmp = $this->paymentReferenceWithInvoiceAndCustomerNr($paymentOrder);
55+
} else {
56+
$tmp = $this->paymentReferenceWithoutInvoiceOrCustomerNr($paymentOrder);
57+
}
58+
59+
if (mb_strlen($tmp) > 140) {
60+
return new RuntimeException('Generated Payment reference exceeds 140 characters! This should not happen unless you have a very long ID...');
61+
}
62+
63+
return $tmp;
64+
}
65+
66+
private function paymentReferenceWithInvoiceAndCustomerNr(PaymentOrder $paymentOrder): string
67+
{
68+
//Max 140 chars are allowed for a payment reference
69+
//Format: R.-Nr. [Invoice-Number 23] Kd.-Nr. [Customer-Number 23] [ProjectName 45] [FSR Name 19] [?Funding ID 20] ZA[PaymentOrder ID 9] = 139
70+
71+
$tmp = '';
72+
if (!empty($paymentOrder->getInvoiceNumber())) {
73+
$tmp .= 'R.-Nr. ' . mb_strimwidth($paymentOrder->getInvoiceNumber(), 0, 23, '');
74+
$tmp .= ' ';
75+
}
76+
if (!empty($paymentOrder->getCustomerNumber())) {
77+
$tmp .= 'Kd.-Nr. ' . mb_strimwidth($paymentOrder->getCustomerNumber(), 0, 23, '');
78+
$tmp .= ' ';
79+
}
5180

5281
//Project name
53-
$tmp = mb_strimwidth($paymentOrder->getProjectName(), 0, 70, '');
82+
$tmp .= mb_strimwidth($paymentOrder->getProjectName(), 0, 45, '');
5483
$tmp .= ' ';
5584
//FSR Name
56-
$tmp .= mb_strimwidth((string) $paymentOrder->getDepartment()->getName(), 0, 45, '');
85+
$tmp .= mb_strimwidth((string) $paymentOrder->getDepartment()->getName(), 0, 15, '');
5786
$tmp .= ' ';
87+
5888
//Funding ID if existing
5989
if ($paymentOrder->getFundingId() !== '' && $paymentOrder->getFundingId() !== '0') {
60-
$tmp .= mb_strimwidth($paymentOrder->getFundingId(), 0, 11, '');
90+
$tmp .= mb_strimwidth($paymentOrder->getFundingId(), 0, 20, '');
6191
$tmp .= ' ';
6292
}
6393

@@ -67,9 +97,31 @@ public function generatePaymentReference(PaymentOrder $paymentOrder): string
6797
}
6898
$tmp .= $paymentOrder->getIDString();
6999

70-
if (mb_strlen($tmp) > 140) {
71-
return new RuntimeException('Generated Payment reference exceeds 140 characters! This should not happen unless you have a very long ID...');
100+
return $tmp;
101+
}
102+
103+
private function paymentReferenceWithoutInvoiceOrCustomerNr(PaymentOrder $paymentOrder): string
104+
{
105+
//Max 140 chars are allowed for a payment reference
106+
//Format: [ProjectName 70] [FSR Name 35] [?Funding ID 20] ZA[PaymentOrder ID 9] = 139
107+
108+
//Project name
109+
$tmp = mb_strimwidth($paymentOrder->getProjectName(), 0, 70, '');
110+
$tmp .= ' ';
111+
//FSR Name
112+
$tmp .= mb_strimwidth((string) $paymentOrder->getDepartment()->getName(), 0, 35, '');
113+
$tmp .= ' ';
114+
//Funding ID if existing
115+
if ($paymentOrder->getFundingId() !== '' && $paymentOrder->getFundingId() !== '0') {
116+
$tmp .= mb_strimwidth($paymentOrder->getFundingId(), 0, 20, '');
117+
$tmp .= ' ';
118+
}
119+
120+
//ZA + ID
121+
if (null === $paymentOrder->getId()) {
122+
throw new RuntimeException('ID is null. You have to persist the PaymentOrder before using this function!');
72123
}
124+
$tmp .= $paymentOrder->getIDString();
73125

74126
return $tmp;
75127
}

tests/Services/PaymentReferenceGeneratorTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,47 @@ public function generatePaymentReferenceDataProvider(): \Generator
7474
//Test project name padding
7575
yield ['veryveryveryveryverylonglonglonglonglonglonglonglonglonglong1234567890 Physik FA-123-2020 ZA0001', 'veryveryveryveryverylonglonglonglonglonglonglonglonglonglong1234567890123456', 'Physik', 'FA-123-2020', 1];
7676
//Test project FSR name padding
77-
yield ['Test PhysikLongLongLongLongLongLongLongLongLongLon FA-123-2020 ZA0001', 'Test', 'PhysikLongLongLongLongLongLongLongLongLongLong', 'FA-123-2020', 1];
77+
yield ['Test PhysikLongLongLongLongLongLongLongL FA-123-2020 ZA0001', 'Test', 'PhysikLongLongLongLongLongLongLongLongLongLong', 'FA-123-2020', 1];
7878
//Even long funding IDs should not be padded
7979
yield ['Test Physik FA-999-2020 ZA0001', 'Test', 'Physik', 'FA-999-2020', 1];
8080
yield ['Test Physik M-9999-2020 ZA0001', 'Test', 'Physik', 'M-9999-2020', 1];
8181
//Test everything very long
82-
yield ['veryveryveryveryverylonglonglonglonglonglonglonglonglonglong1234567890 PhysikLongLongLongLongLongLongLongLongLongLon FA-999-2020 ZA123456789',
82+
yield ['veryveryveryveryverylonglonglonglonglonglonglonglonglonglong1234567890 PhysikLongLongLongLongLongLongLongL FA-999-2020 ZA123456789',
8383
'veryveryveryveryverylonglonglonglonglonglonglonglonglonglong1234567890123456',
8484
'PhysikLongLongLongLongLongLongLongLongLongLong',
8585
'FA-999-2020',
8686
123456789,
8787
];
88+
//Test for new long funding IDs
89+
yield ['Test Queerparadies M-QUEER-1234-2024_25 ZA12345', 'Test', 'Queerparadies', 'M-QUEER-1234-2024_25', 12345];
90+
}
91+
92+
public function testGeneratePaymentReferenceWithInvoiceAndCustomerNr(): void
93+
{
94+
$payment_order = PaymentOrderTestingHelper::getDummyPaymentOrder(1);
95+
$department = new Department();
96+
$department->setName('Long Long Long Department Name');
97+
$payment_order->setProjectName('Test Very Very Long Project Name')
98+
->setDepartment($department)
99+
->setFundingId('M-QUEER-1234-2024_25')
100+
->setInvoiceNumber('12345678901234567890')
101+
->setCustomerNumber('LONG-CUSTOMER-NUMBER2345');
102+
103+
$tmp = $this->service->generatePaymentReference($payment_order);
104+
105+
static::assertSame('R.-Nr. 12345678901234567890 Kd.-Nr. LONG-CUSTOMER-NUMBER234 Test Very Very Long Project Name Long Long Long M-QUEER-1234-2024_25 ZA0001', $tmp);
106+
107+
//Test for invoice number missing
108+
$payment_order->setInvoiceNumber(null);
109+
110+
$tmp = $this->service->generatePaymentReference($payment_order);
111+
static::assertSame('Kd.-Nr. LONG-CUSTOMER-NUMBER234 Test Very Very Long Project Name Long Long Long M-QUEER-1234-2024_25 ZA0001', $tmp);
112+
113+
//Test for customer number missing
114+
$payment_order->setInvoiceNumber('12345678901234567890');
115+
$payment_order->setCustomerNumber(null);
116+
117+
$tmp = $this->service->generatePaymentReference($payment_order);
118+
static::assertSame('R.-Nr. 12345678901234567890 Test Very Very Long Project Name Long Long Long M-QUEER-1234-2024_25 ZA0001', $tmp);
88119
}
89120
}

0 commit comments

Comments
 (0)