fixed auto add customer into tahesab
This commit is contained in:
@@ -5,6 +5,8 @@ namespace App\Services;
|
||||
use App\Http\Controllers\Admin\TahesabController;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Tahesab;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class TahesabSyncService
|
||||
@@ -78,12 +80,54 @@ class TahesabSyncService
|
||||
return empty($tahesab->tahesab_id);
|
||||
}
|
||||
|
||||
public function registerOnCreate(Customer $customer): void
|
||||
{
|
||||
if (! empty($customer->moshtari_code) || ! $this->isSyncEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$customerId = (int) $customer->id;
|
||||
|
||||
dispatch(function () use ($customerId) {
|
||||
$service = app(TahesabSyncService::class);
|
||||
$customer = Customer::find($customerId);
|
||||
if ($customer === null || ! empty($customer->moshtari_code) || ! $service->isSyncEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $service->registerCustomer($customer);
|
||||
if (! ($result['ok'] ?? false)) {
|
||||
Log::warning('Tahesab auto-register on customer create failed', [
|
||||
'customer_id' => $customerId,
|
||||
'message' => $result['message'] ?? null,
|
||||
]);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('Tahesab auto-register on customer create threw', [
|
||||
'customer_id' => $customerId,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
})->afterResponse();
|
||||
}
|
||||
|
||||
public function registerCustomer(Customer $customer): array
|
||||
{
|
||||
$customer->refresh();
|
||||
|
||||
if (! empty($customer->moshtari_code)) {
|
||||
return [
|
||||
'ok' => true,
|
||||
'message' => 'مشتری قبلاً در تهحساب ثبت شده است',
|
||||
'moshtari_code' => $customer->moshtari_code,
|
||||
];
|
||||
}
|
||||
|
||||
$controller = app(TahesabController::class);
|
||||
$result = $controller->createCustomerInTahesab($customer->id);
|
||||
|
||||
if (is_string($result) && str_contains($result, 'error')) {
|
||||
if (is_string($result)) {
|
||||
return ['ok' => false, 'message' => $result];
|
||||
}
|
||||
|
||||
@@ -150,6 +194,22 @@ class TahesabSyncService
|
||||
return 'goldReward';
|
||||
}
|
||||
|
||||
public function isSyncEnabled(): bool
|
||||
{
|
||||
$config = app(ConfigService::class);
|
||||
|
||||
if ($config->getString('send_to_tahesab', 'false') !== 'true') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$host = trim($config->getString('tahesab_host', ''));
|
||||
$port = trim($config->getString('tahesab_port', ''));
|
||||
$token = trim($config->getString('tahesab_token', ''));
|
||||
$db = trim($config->getString('tahesab_db', ''));
|
||||
|
||||
return $host !== '' && $port !== '' && $token !== '' && $db !== '';
|
||||
}
|
||||
|
||||
private function hasSyncColumns(): bool
|
||||
{
|
||||
return Schema::hasTable('tahesab') && Schema::hasColumn('tahesab', 'sync_status');
|
||||
|
||||
Reference in New Issue
Block a user