OrangePaymentSlipData::getWithBankingCustomerId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * Swiss Payment Slip
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
6
 * @copyright 2012-2016 Some nice Swiss guys
7
 * @author Marc Würth [email protected]
8
 * @author Manuel Reinhard <[email protected]>
9
 * @author Peter Siska <[email protected]>
10
 * @link https://github.com/ravage84/SwissPaymentSlip/
11
 */
12
13
namespace SwissPaymentSlip\SwissPaymentSlip;
14
15
use SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException;
16
17
/**
18
 * Orange Swiss Payment Slip Data
19
 *
20
 * A data container class to encapsulate all the necessary data
21
 * for an orange Swiss payment slip with reference number.
22
 *
23
 * @see PaymentSlipData For more information about the various payment slips.
24
 * @link https://www.postfinance.ch/content/dam/pf/de/doc/consult/manual/dlserv/inpayslip_isr_man_de.pdf German manual
25
 * for ISRs.
26
 */
27
class OrangePaymentSlipData extends PaymentSlipData
28
{
29
    /**
30
     * Determines if the payment slip has a reference number. Can be disabled for pre-printed payment slips
31
     *
32
     * @var bool
33
     */
34
    protected $withReferenceNumber = true;
35
36
    /**
37
     * Determines if the payment slip's reference number should contain the banking customer ID.
38
     * Can be disabled for recipients who don't need this
39
     *
40
     * @var bool
41
     */
42
    protected $withBankingCustomerId = true;
43
44
    /**
45
     * The reference number, without banking customer ID and check digit
46
     *
47
     * @var string
48
     */
49
    protected $referenceNumber = '';
50
51
    /**
52
     * The banking customer ID, which will be prepended to the reference number
53
     *
54
     * @var string
55
     */
56
    protected $bankingCustomerId = '';
57
58
    /**
59
     * Set if payment slip has a reference number specified
60
     *
61
     * Resets the reference number when disabling.
62
     *
63
     * @param bool $withReferenceNumber True if yes, false if no.
64
     * @return $this The current instance for a fluent interface.
65
     */
66 2
    public function setWithReferenceNumber($withReferenceNumber = true)
67
    {
68 2
        $this->isBool($withReferenceNumber, 'withReferenceNumber');
69 1
        $this->withReferenceNumber = $withReferenceNumber;
70
71 1
        if ($withReferenceNumber === false) {
72 1
            $this->referenceNumber = '';
73
        }
74
75 1
        return $this;
76
    }
77
78
    /**
79
     * Get if payment slip has a reference number specified
80
     *
81
     * @return bool True if payment slip has a reference number specified, else false.
82
     */
83 1
    public function getWithReferenceNumber()
84
    {
85 1
        return $this->withReferenceNumber;
86
    }
87
88
    /**
89
     * Set if the payment slip's reference number should contain the banking customer ID
90
     *
91
     * Resets the banking customer ID when disabling.
92
     *
93
     * @param bool $withBankingCustomerId True if successful, else false.
94
     * @return $this The current instance for a fluent interface.
95
     */
96 2
    public function setWithBankingCustomerId($withBankingCustomerId = true)
97
    {
98 2
        $this->isBool($withBankingCustomerId, 'withBankingCustomerId');
99 1
        $this->withBankingCustomerId = $withBankingCustomerId;
100
101 1
        if ($withBankingCustomerId === false) {
102 1
            $this->bankingCustomerId = '';
103
        }
104
105 1
        return $this;
106
    }
107
108
    /**
109
     * Get if the payment slip's reference number should contain the banking customer ID.
110
     *
111
     * @return bool True if payment slip has the recipient specified, else false.
112
     */
113 1
    public function getWithBankingCustomerId()
114
    {
115 1
        return $this->withBankingCustomerId;
116
    }
117
118
    /**
119
     * Set the reference number
120
     *
121
     * @param string $referenceNumber The reference number.
122
     * @return $this The current instance for a fluent interface.
123
     * @throws DisabledDataException If the data is disabled.
124
     */
125 2
    public function setReferenceNumber($referenceNumber)
126
    {
127 2
        if (!$this->getWithReferenceNumber()) {
128 1
            throw new DisabledDataException('reference number');
129
        }
130
        // TODO validate reference number
131 1
        $this->referenceNumber = $referenceNumber;
132
133 1
        return $this;
134
    }
135
136
    /**
137
     * Get the reference number
138
     *
139
     * @return string The reference number, if withReferenceNumber is set to true.
140
     * @throws DisabledDataException If the data is disabled.
141
     */
142 3
    public function getReferenceNumber()
143
    {
144 3
        if (!$this->getWithReferenceNumber()) {
145 2
            throw new DisabledDataException('reference number');
146
        }
147 1
        return $this->referenceNumber;
148
    }
149
150
    /**
151
     * Set the banking customer ID
152
     *
153
     * @param string $bankingCustomerId The banking customer ID.
154
     * @return $this The current instance for a fluent interface.
155
     * @throws DisabledDataException If the data is disabled.
156
     */
157 2
    public function setBankingCustomerId($bankingCustomerId)
158
    {
159 2
        if (!$this->getWithBankingCustomerId()) {
160 1
            throw new DisabledDataException('banking customer ID');
161
        }
162
        // TODO check length (exactly 6)
163 1
        $this->bankingCustomerId = $bankingCustomerId;
164
165 1
        return $this;
166
    }
167
168
    /**
169
     * Get the banking customer ID
170
     *
171
     * @return string The  banking customer ID, if withBankingCustomerId is set to true.
172
     * @throws DisabledDataException If the data is disabled.
173
     */
174 2
    public function getBankingCustomerId()
175
    {
176 2
        if (!$this->getWithBankingCustomerId()) {
177 1
            throw new DisabledDataException('banking customer ID');
178
        }
179 1
        return $this->bankingCustomerId;
180
    }
181
182
    /**
183
     * Set payment slip for not to be used for payment
184
     *
185
     * XXXes out all fields to prevent people using the payment slip.
186
     *
187
     * @param boolean $notForPayment True if not for payment, else false.
188
     * @return $this The current instance for a fluent interface.
189
     */
190 2
    public function setNotForPayment($notForPayment = true)
191
    {
192 2
        parent::setNotForPayment($notForPayment);
193
194 2
        if ($notForPayment === true) {
195 2
            if ($this->getWithReferenceNumber() === true) {
196 1
                $this->setReferenceNumber('XXXXXXXXXXXXXXXXXXXX');
197
            }
198 2
            if ($this->getWithBankingCustomerId() === true) {
199 1
                $this->setBankingCustomerId('XXXXXX');
200
            }
201
        }
202
203 2
        return $this;
204
    }
205
206
    /**
207
     * Get complete reference number
208
     *
209
     * @param bool $formatted Should the returned reference be formatted in blocks of five (for better readability).
210
     * @param bool $fillZeros Fill up with leading zeros, only applies to the case where no banking customer ID is used.
211
     * @return string The complete (with/without bank customer ID), formatted reference number with check digit
212
     */
213 3
    public function getCompleteReferenceNumber($formatted = true, $fillZeros = true)
214
    {
215 3
        $referenceNumber = $this->getReferenceNumber();
216 2
        $notForPayment = $this->getNotForPayment();
217
218 2
        $completeReferenceNumber = $referenceNumber;
219 2
        if ($notForPayment) {
220 1
            $completeReferenceNumber = str_pad($referenceNumber, 26, 'X', STR_PAD_LEFT);
221 1
        } elseif ($this->getWithBankingCustomerId()) {
222
            // Get reference number and fill with zeros
223 1
            $referenceNumber = str_pad($referenceNumber, 20, '0', STR_PAD_LEFT);
224
            // Prepend banking customer identification code
225 1
            $completeReferenceNumber = $this->getBankingCustomerId() . $referenceNumber;
226 1
        } elseif ($fillZeros) {
227
            // Get reference number and fill with zeros
228 1
            $completeReferenceNumber = str_pad($referenceNumber, 26, '0', STR_PAD_LEFT);
229
        }
230
231
        // Add check digit
232 2
        $completeReferenceNumber = $this->appendCheckDigit($completeReferenceNumber, $notForPayment);
233
234 2
        if ($formatted) {
235 2
            $completeReferenceNumber = $this->breakStringIntoBlocks($completeReferenceNumber);
236
        }
237
238 2
        return $completeReferenceNumber;
239
    }
240
241
    /**
242
     * Append the check digit to the reference number
243
     *
244
     * Simply appends an 'X' if the slip is not meant for payment.
245
     *
246
     * @param string $referenceNumber The reference number to calculate the prefix with.
247
     * @param bool $notForPayment Whether the payment slip is not ment for payment.
248
     * @return string The reference number with the appended check digit.
249
     */
250 2
    protected function appendCheckDigit($referenceNumber, $notForPayment = false)
251
    {
252 2
        if ($notForPayment === true) {
253 1
            return $referenceNumber . 'X';
254
        }
255 1
        return $referenceNumber . $this->modulo10($referenceNumber);
256
    }
257
258
    /**
259
     * Get the full code line at the bottom of the ESR
260
     *
261
     * @param bool $fillZeros Whether to fill up the code line with leading zeros.
262
     * @return string The full code line.
263
     */
264 2
    public function getCodeLine($fillZeros = true)
265
    {
266 2
        $referenceNumber = $this->getCompleteReferenceNumber(false, $fillZeros);
267 2
        $accountNumber = $this->getAccountDigits();
268
269 2
        if ($this->getWithAmount()) {
270 2
            $francs = $this->getAmountFrancs();
271 2
            $cents = $this->getAmountCents();
272 2
            $francs = str_pad($francs, 8, '0', STR_PAD_LEFT);
273 2
            $cents = str_pad($cents, 2, '0', STR_PAD_RIGHT);
274 2
            $amountPrefix = '01';
275 2
            $amountPart = $francs . $cents;
276 2
            $amountCheck = $this->modulo10($amountPrefix . $amountPart);
277
        } else {
278 1
            $amountPrefix = '04';
279 1
            $amountPart = '';
280 1
            $amountCheck = '2';
281
        }
282 2
        if ($fillZeros) {
283 2
            $referenceNumberPart = str_pad($referenceNumber, 27, '0', STR_PAD_LEFT);
284
        } else {
285 1
            $referenceNumberPart = $referenceNumber;
286
        }
287 2
        $accountNumberPart = substr($accountNumber, 0, 2) .
288 2
            str_pad(substr($accountNumber, 2), 7, '0', STR_PAD_LEFT);
289
290 2
        if ($this->getNotForPayment()) {
291 1
            $amountPrefix = 'XX';
292 1
            $amountCheck = 'X';
293
        }
294
295 2
        $codeLine = sprintf(
296 2
            '%s%s%s>%s+ %s>',
297 2
            $amountPrefix,
298 2
            $amountPart,
299 2
            $amountCheck,
300 2
            $referenceNumberPart,
301 2
            $accountNumberPart
302
        );
303
304 2
        return $codeLine;
305
    }
306
}
307