|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace InvoiceNinjaModule\Service; |
|
4
|
|
|
|
|
5
|
|
|
use InvoiceNinjaModule\Exception\InvalidResultException; |
|
6
|
|
|
use InvoiceNinjaModule\Exception\NotFoundException; |
|
7
|
|
|
use InvoiceNinjaModule\Model\Interfaces\InvoiceInterface; |
|
8
|
|
|
use InvoiceNinjaModule\Model\Invoice; |
|
9
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ClientManagerInterface; |
|
10
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class InvoiceManager |
|
14
|
|
|
* |
|
15
|
|
|
* @package InvoiceNinjaModule\Service |
|
16
|
|
|
*/ |
|
17
|
|
|
class InvoiceManager implements ClientManagerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var ObjectServiceInterface */ |
|
20
|
|
|
private $objectManager; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $reqRoute; |
|
23
|
|
|
/** @var Invoice */ |
|
24
|
|
|
private $objectType; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* InvoiceManager constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param ObjectServiceInterface $objectManager |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(ObjectServiceInterface $objectManager) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->objectManager = $objectManager; |
|
34
|
|
|
$this->reqRoute = '/invoices'; |
|
35
|
|
|
$this->objectType = new Invoice(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param InvoiceInterface $invoice |
|
40
|
|
|
* |
|
41
|
|
|
* @return InvoiceInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
public function createInvoice(InvoiceInterface $invoice) :InvoiceInterface |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->objectManager->createObject($invoice, $this->reqRoute); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function delete(InvoiceInterface $invoice) :InvoiceInterface |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->objectManager->deleteObject($invoice, $this->reqRoute); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function update(InvoiceInterface $invoice) :InvoiceInterface |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->objectManager->updateObject($invoice, $this->reqRoute); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function restore(InvoiceInterface $invoice) :InvoiceInterface |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->objectManager->restoreObject($invoice, $this->reqRoute); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function archive(InvoiceInterface $invoice) :InvoiceInterface |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->objectManager->archiveObject($invoice, $this->reqRoute); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getInvoiceById(int $id) :InvoiceInterface |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->objectManager->getObjectById($this->objectType, $id, $this->reqRoute); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getInvoiceByNumber(string $invoiceNumber) :InvoiceInterface |
|
74
|
|
|
{ |
|
75
|
|
|
$result = $this->objectManager->findObjectBy( |
|
76
|
|
|
$this->objectType, |
|
77
|
|
|
[Invoice::INVOICE_NR => $invoiceNumber], |
|
78
|
|
|
$this->reqRoute |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
if (\count($result) === 1) { |
|
82
|
|
|
return $result[0]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (empty($result)) { |
|
86
|
|
|
throw new NotFoundException(Invoice::INVOICE_NR.' '.$invoiceNumber); |
|
87
|
|
|
} |
|
88
|
|
|
throw new InvalidResultException(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getAllInvoices(int $page = 1, int $pageSize = 0) :array |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->objectManager->getAllObjects($this->objectType, $this->reqRoute, $page, $pageSize); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function downloadInvoice(int $invoiceId) :array |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->objectManager->downloadFile($invoiceId); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|