-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDolibarrInvoice.php
More file actions
183 lines (142 loc) · 5.38 KB
/
Copy pathDolibarrInvoice.php
File metadata and controls
183 lines (142 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
// Gestion de la récupération des données de facturation à partir de Dolibarr
// Enumération pour les certifications (attribut supplémentaire de proposition commerciale dans dolibarr)
enum DolibarrInvoiceType: string {
case BALANCE = '0';
case SITUATION = '1';
case ADVANCE = '3';
}
class DolibarrInvoice extends DolibarrObject {
protected static function getInvoiceClass(): string
{
return DolibarrInvoice::class;
}
public function getTotalHt() {
return $this->data->total_ht ?? null;
}
// Is this facture a balance or an advance ?
public function getType() {
return $this->data->type ?? null;
}
public function getDate() {
$dateNum = $this->data->date ?? null;
return $this->getFormattedDate($dateNum); // Appel de la fonction générique
}
public function isAdvance() : bool {
return $this->getType() === DolibarrInvoiceType::ADVANCE;
}
public function isBalance() : bool {
return $this->getType() === DolibarrInvoiceType::BALANCE;
}
public function getPdfDocumentURL(): ?string
{
$filenames = $this->getFilenames("invoice", $this->getRef());
// Recherche du premier fichier se terminant par '.pdf'
foreach ($filenames as $filename) {
if (isset($filename) && str_ends_with(strtolower($filename), '.pdf')) {
// Encodage du nom de fichier pour l'URL
$encodedFilename = rawurlencode($this->getRef() . "/" . $filename);
// Génération de l'URL sécurisée
return DOLIBARR_DOCUMENT_URL . "?modulepart=invoice&file=" . $encodedFilename . "&entity=1";
}
}
return null;
}
/**
* Récupère les IDs de propositions liées à cette facture.
*
* @return array<int, string>
*/
public function getProposalIds(): array
{
$proposalIds = $this->extractProposalIdsFromLinkedObjects();
if ($proposalIds !== []) {
return $proposalIds;
}
$invoiceId = $this->getId();
if ($invoiceId === null || $invoiceId === '') {
return [];
}
$fullInvoice = static::getInvoiceFromID($invoiceId);
if (!$fullInvoice) {
return [];
}
return $fullInvoice->extractProposalIdsFromLinkedObjects();
}
/**
* Extrait les IDs de propositions depuis linkedObjectsIds sans requête complémentaire.
*
* @return array<int, string>
*/
private function extractProposalIdsFromLinkedObjects(): array
{
$proposalIds = $this->getLinkedObjectIds('proposal');
if ($proposalIds !== []) {
return $proposalIds;
}
return $this->getLinkedObjectIds('propal');
}
/**
* Récupère la ref de la proposition liée à cette facture.
*
* @return string|null
*/
public function getProposalRef(): ?string
{
if (!class_exists('DolibarrProposal')) {
return null;
}
foreach ($this->getProposalIds() as $proposalId) {
$proposal = DolibarrProposal::getProposalFromID($proposalId);
if ($proposal) {
$proposalRef = trim((string) $proposal->getRef());
if ($proposalRef !== '') {
return $proposalRef;
}
}
}
return null;
}
// Ajoutez d'autres getters ici selon les champs disponibles dans votre réponse API
public static function getInvoice($invoiceRef, $retryCount = 3, $initialDelaySeconds = 10): ?static
{
$escapedRef = str_replace(['\\', "'"], ['\\\\', "\\'"], (string) $invoiceRef);
$sqlfilters = urlencode("(t.ref:like:'{$escapedRef}')");
$endpoint = "/invoices?contact_list=1&sqlfilters=" . $sqlfilters;
$data = parent::fetchFromDolibarr($endpoint, $retryCount, $initialDelaySeconds);
if (is_array($data)) {
foreach ($data as $invoice) {
if (is_object($invoice) && isset($invoice->ref) && $invoice->ref === $invoiceRef) {
$invoiceClass = static::getInvoiceClass();
return new $invoiceClass($invoice);
}
}
return null;
}
if (is_object($data) && isset($data->ref) && $data->ref === $invoiceRef) {
$invoiceClass = static::getInvoiceClass();
return new $invoiceClass($data);
}
return null;
}
// Retourne une facture à partir de son ID (l'id n'est pas la ref)
public static function getInvoiceFromID($invoiceId, $retryCount = 3, $initialDelaySeconds = 10): ?static
{
$endpoint = "/invoices/" . $invoiceId;
$data = parent::fetchFromDolibarr($endpoint, $retryCount, $initialDelaySeconds);
$invoiceClass = static::getInvoiceClass();
if (is_array($data)) {
foreach ($data as $invoice) {
if (!is_object($invoice)) {
continue;
}
$currentId = (string) ($invoice->id ?? $invoice->rowid ?? '');
if ($currentId !== '' && $currentId === (string) $invoiceId) {
return new $invoiceClass($invoice);
}
}
$firstInvoice = reset($data);
return is_object($firstInvoice) ? new $invoiceClass($firstInvoice) : null;
}
return $data ? new $invoiceClass($data) : null; }
}