| 1 |
<?php |
| 2 |
|
| 3 |
namespace App\Billingo\WooCommerce\Controllers; |
| 4 |
|
| 5 |
use App\Billingo\Enums\Document\TypeEnum; |
| 6 |
use App\Billingo\Exceptions\BadContentException; |
| 7 |
use App\Billingo\Models\Document\Document; |
| 8 |
use App\Billingo\Models\Document\DocumentInsert; |
| 9 |
use App\Billingo\Service\BillingoClient; |
| 10 |
use App\Billingo\WooCommerce\Repositories\Billingo_Repositroy; |
| 11 |
use App\Billingo\WooCommerce\Service\Billingo_Logger; |
| 12 |
use Symfony\Component\HttpFoundation\Response; |
| 13 |
|
| 14 |
class Billingo_Controller |
| 15 |
{ |
| 16 |
|
| 17 |
private readonly BillingoClient $client; |
| 18 |
private readonly Billingo_Repositroy $repository; |
| 19 |
private bool $blockProforma; |
| 20 |
|
| 21 |
public function __construct(private readonly int $orderId) |
| 22 |
{ |
| 23 |
$this->client = new BillingoClient(get_option('wc_billingo_api_key', '')); |
| 24 |
$this->blockProforma = get_option('wc_billingo_disable_proforma_invoicing') === 'yes'; |
| 25 |
$this->repository = new Billingo_Repositroy(); |
| 26 |
} |
| 27 |
|
| 28 |
public function createDocument(DocumentInsert $document): ?Document |
| 29 |
{ |
| 30 |
|
| 31 |
Billingo_Logger::info('Document type of creating: ' . $document->type); |
| 32 |
$hasInvoice = $this->repository |
| 33 |
->where('type', TypeEnum::INVOICE->value) |
| 34 |
->where('order_id', $this->orderId) |
| 35 |
->where('canceled_by', null) |
| 36 |
->first(); |
| 37 |
|
| 38 |
if ($hasInvoice && $document->type === TypeEnum::INVOICE->value) { |
| 39 |
$errorMessage ='Már van érvényes számla a ' . $this->orderId . 'számú rendelésre, nem kell újat létrehozni, amennyiben mégis újat szeretne létrehozni, előbb sztornózza a már meglévő számlát! : Számla neve: ' . $hasInvoice['billingo_number']; |
| 40 |
Billingo_Logger::info($errorMessage); |
| 41 |
|
| 42 |
return null; |
| 43 |
}else { |
| 44 |
|
| 45 |
$hasProforma = $this->repository |
| 46 |
->where('type', TypeEnum::PROFORMA->value) |
| 47 |
->where('order_id', $this->orderId) |
| 48 |
->get(); |
| 49 |
|
| 50 |
$canUseProforma = !$this->blockProforma && |
| 51 |
$hasProforma !== null && |
| 52 |
isset($hasProforma['billingo_id']) && |
| 53 |
!empty($hasProforma['billingo_id']); |
| 54 |
|
| 55 |
$this->shouldDisableWcEmail($document->type); |
| 56 |
|
| 57 |
if ($canUseProforma) { |
| 58 |
return $this->createInvoiceFromProforma($hasProforma['billingo_id']); |
| 59 |
} |
| 60 |
else if ($document->type === TypeEnum::DRAFT->value) { |
| 61 |
Billingo_Logger::info('Piszkozat létrehozására beérkező igény'); |
| 62 |
return $this->createInvoice($document); |
| 63 |
} |
| 64 |
else { |
| 65 |
return $this->createInvoice($document); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
public function cancelDocument(int $billingoId, array $body = []): bool |
| 72 |
{ |
| 73 |
//handles the email sending for the storno because it is not a document genaration, just a cancellation request to the server |
| 74 |
//todo: refactor: Standard_Init.php ajax_stornoInvoice() also use the same logic, move to |
| 75 |
if(in_array(get_option('wc_billingo_storno_email'), ['both', 'billingo'])) { |
| 76 |
if (empty($body)) { |
| 77 |
$order = wc_get_order($this->orderId); |
| 78 |
if ($order && $order->get_billing_email()) { |
| 79 |
$body = [ |
| 80 |
'cancellation_recipients' => $order->get_billing_email() |
| 81 |
]; |
| 82 |
Billingo_Logger::info('Auto-setting cancellation email recipient for order ' . $this->orderId . ': ' . $order->get_billing_email()); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$response = $this->client->document()->cancelDocument($billingoId, $body)->getResponse(); |
| 87 |
}else{ |
| 88 |
$response = $this->client->document()->cancelDocument($billingoId)->getResponse(); |
| 89 |
} |
| 90 |
if ($response->getStatusCode() === Response::HTTP_OK) { |
| 91 |
|
| 92 |
$canceledDocument = $response->getData(); |
| 93 |
Billingo_Logger::info("Invoice cancel SUCCESSFUL ID: {$canceledDocument->id}"); |
| 94 |
$databaseRow = $this->repository->where('billingo_id', $billingoId)->first(); |
| 95 |
|
| 96 |
if (!is_null($databaseRow)) { |
| 97 |
$this->repository->update($databaseRow['id'], ['canceled_by' => $canceledDocument->id]); |
| 98 |
} |
| 99 |
|
| 100 |
$this->repository->createFromDocument($this->orderId, $canceledDocument); |
| 101 |
|
| 102 |
return true; |
| 103 |
} else { |
| 104 |
$errors = $response->getErrors(); |
| 105 |
Billingo_Logger::error('Invoice cancel FAILED: ' . json_encode($errors)); |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
public function getLink(int $id): ?string |
| 112 |
{ |
| 113 |
$response = $this->client |
| 114 |
->document() |
| 115 |
->getPublicUrlForDocument($id) |
| 116 |
->getResponse(); |
| 117 |
|
| 118 |
if ($response->getStatusCode() == Response::HTTP_OK) { |
| 119 |
|
| 120 |
return $response->getData()['public_url']; |
| 121 |
} |
| 122 |
Billingo_Logger::warning("Cant create a link for ID: {$id}"); |
| 123 |
|
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
private function createInvoice(DocumentInsert $document): ?Document |
| 128 |
{ |
| 129 |
try { |
| 130 |
$response = $this->client |
| 131 |
->document() |
| 132 |
->create($document) |
| 133 |
->getResponse(); |
| 134 |
} catch (BadContentException $exception) { |
| 135 |
|
| 136 |
Billingo_Logger::error('Billingo server creation FAILED: ' . $exception->getMessage()); |
| 137 |
|
| 138 |
return null; |
| 139 |
} |
| 140 |
|
| 141 |
if ($response->getStatusCode() === Response::HTTP_CREATED) { |
| 142 |
|
| 143 |
$created = $response->getData(); |
| 144 |
Billingo_Logger::info("Billingo server successfully created, ID: = {$created->id}"); |
| 145 |
|
| 146 |
$this->store($created); |
| 147 |
|
| 148 |
return $created; |
| 149 |
|
| 150 |
} else { |
| 151 |
|
| 152 |
Billingo_Logger::error('Billingo server creation FAILED: ' |
| 153 |
. json_encode($response->getErrors())); |
| 154 |
|
| 155 |
return null; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
private function createInvoiceFromProforma(int $id): ?Document |
| 160 |
{ |
| 161 |
$response = $this->client |
| 162 |
->document() |
| 163 |
->createFromProforma($id) |
| 164 |
->getResponse(); |
| 165 |
|
| 166 |
if ($response->getStatusCode() === Response::HTTP_CREATED) { |
| 167 |
|
| 168 |
$proforma = $response->getData(); |
| 169 |
|
| 170 |
Billingo_Logger::info("Billingo server successfully created from proforma, ID: = {$proforma->id}"); |
| 171 |
|
| 172 |
$this->store($proforma); |
| 173 |
|
| 174 |
return $response->getData(); |
| 175 |
} else { |
| 176 |
Billingo_Logger::error('Billingo server creation from proforma: FAILED ' |
| 177 |
. json_encode($response->getErrors())); |
| 178 |
|
| 179 |
return null; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private function store(Document $document): void |
| 184 |
{ |
| 185 |
$this->repository->createFromDocument($this->orderId, $document); |
| 186 |
} |
| 187 |
|
| 188 |
private function shouldDisableWcEmail(string $type): void |
| 189 |
{ |
| 190 |
$wcEmailId = match($type){ |
| 191 |
'invoice' => 'wc_billingo_email', |
| 192 |
'proforma' => 'wc_billingo_proforma_email', |
| 193 |
'cancellation' => 'wc_billingo_storno_email', |
| 194 |
default => null, |
| 195 |
}; |
| 196 |
|
| 197 |
$enabled = in_array(get_option($wcEmailId), ['attach', 'both']) |
| 198 |
? 'yes' |
| 199 |
: 'no'; |
| 200 |
|
| 201 |
if ($enabled === 'yes') { |
| 202 |
Billingo_Logger::info('Email send by WooCommerce'); |
| 203 |
$this->wcEmailToggle(true); |
| 204 |
} else { |
| 205 |
$this->wcEmailToggle(false); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
private function wcEmailToggle(bool $on): void |
| 210 |
{ |
| 211 |
$turnOffCallback = fn() => $on; |
| 212 |
|
| 213 |
add_filter('woocommerce_email_enabled_customer_completed_order', $turnOffCallback, 10, 2); |
| 214 |
add_filter('woocommerce_email_enabled_customer_refunded_order', $turnOffCallback, 10, 2); |
| 215 |
add_filter('woocommerce_email_enabled_customer_processing_order', $turnOffCallback, 10, 2); |
| 216 |
add_filter('woocommerce_email_enabled_customer_on_hold_order', $turnOffCallback, 10, 2); |
| 217 |
} |
| 218 |
} |
| 219 |
|