|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shoman4eg\Nalog\Api; |
|
5
|
|
|
|
|
6
|
|
|
use Brick\Math\BigDecimal; |
|
7
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
|
8
|
|
|
use Shoman4eg\Nalog\DTO; |
|
9
|
|
|
use Shoman4eg\Nalog\Enum; |
|
10
|
|
|
use Shoman4eg\Nalog\ErrorHandler; |
|
11
|
|
|
use Shoman4eg\Nalog\Exception; |
|
12
|
|
|
use Shoman4eg\Nalog\Model\Income\IncomeType; |
|
13
|
|
|
use Webmozart\Assert\Assert; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Artem Dubinin <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
final class Invoice extends BaseHttpApi |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param float|int $amount |
|
22
|
|
|
* @param float|int $quantity |
|
23
|
|
|
* |
|
24
|
|
|
* @throws \JsonException |
|
25
|
|
|
* @throws ClientExceptionInterface |
|
26
|
|
|
* @throws Exception\DomainException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function create( |
|
29
|
|
|
string $name, |
|
30
|
|
|
$amount, |
|
31
|
|
|
$quantity, |
|
32
|
|
|
?\DateTimeInterface $operationTime = null |
|
33
|
|
|
): IncomeType { |
|
34
|
|
|
Assert::notEmpty($name, 'Name cannot be empty'); |
|
35
|
|
|
Assert::numeric($amount, 'Amount must be int or float'); |
|
36
|
|
|
Assert::greaterThan($amount, 0, 'Amount must be greater than %2$s'); |
|
37
|
|
|
Assert::notEmpty($quantity, 'Quantity cannot be empty'); |
|
38
|
|
|
Assert::numeric($quantity, 'Quantity must be int or float'); |
|
39
|
|
|
Assert::greaterThan($quantity, 0, 'Quantity must be greater than %2$s'); |
|
40
|
|
|
|
|
41
|
|
|
$totalAmount = BigDecimal::of($amount)->multipliedBy($quantity); |
|
42
|
|
|
|
|
43
|
|
|
$response = $this->httpPost('/invoice', [ |
|
44
|
|
|
'paymentType' => Enum\PaymentType::ACCOUNT, |
|
45
|
|
|
'ignoreMaxTotalIncomeRestriction' => false, |
|
46
|
|
|
'client' => new DTO\IncomeClient(), |
|
47
|
|
|
'services' => [new DTO\InvoiceServiceItem($name, $amount, $quantity)], |
|
48
|
|
|
'requestTime' => new DTO\DateTime(new \DateTimeImmutable()), |
|
49
|
|
|
'operationTime' => new DTO\DateTime($operationTime ?: new \DateTimeImmutable()), |
|
50
|
|
|
'totalAmount' => (string)$totalAmount, |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
if ($response->getStatusCode() >= 400) { |
|
54
|
|
|
(new ErrorHandler())->handleResponse($response); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->hydrator->hydrate($response, IncomeType::class); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function cancel(int $invoiceId): void |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
throw new \BadMethodCallException('Not impemented'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function updatePaymentInfo(): void |
|
66
|
|
|
{ |
|
67
|
|
|
throw new \BadMethodCallException('Not impemented'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.