diff --git a/app/Http/Controllers/Admin/TahesabController.php b/app/Http/Controllers/Admin/TahesabController.php index 1b937cc..7ad9344 100644 --- a/app/Http/Controllers/Admin/TahesabController.php +++ b/app/Http/Controllers/Admin/TahesabController.php @@ -7,6 +7,7 @@ use App\Http\Controllers\Site\ReportController; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Cache; use App\Models\Tahesab; use App\Models\Customer; use App\Services\TahesabSyncService; @@ -554,36 +555,58 @@ class TahesabController extends Controller $response = null; if ($customer->moshtari_code == null) { - try { - $response = Http::withoutVerifying()->timeout(10)->withHeaders([ - 'DBName' => $this->Config('tahesab_db'), - 'Content-Type' => 'application/json', - 'Authorization' => "Bearer " . $this->Config('tahesab_token'), - ])->post($this->tahesabApiUrl(), [ - "DoNewMoshtari" => [ - (string)$customer->name, - $this->Config('name_fa'), - (string)$customer->mobile, - "", - "", - "", - "", - "", - -1, - 0 - ] - ])->json(); + $lock = Cache::lock('tahesab:register-customer:'.$customer->id, 30); - if ($this->sync()->responseOk($response)) { - $customer->moshtari_code = $response['OK']; - $customer->save(); - return $response['OK']; + if (! $lock->get()) { + $customer->refresh(); + + if ($customer->moshtari_code != null) { + return $customer->moshtari_code; } - } catch (\Exception $e) { - return $e->getMessage(); + + return 'ثبت مشتری در ته‌حساب در حال انجام است'; } - return 'tahesab api error!!!'; + try { + $customer->refresh(); + + if ($customer->moshtari_code != null) { + return $customer->moshtari_code; + } + + try { + $response = Http::withoutVerifying()->timeout(10)->withHeaders([ + 'DBName' => $this->Config('tahesab_db'), + 'Content-Type' => 'application/json', + 'Authorization' => "Bearer " . $this->Config('tahesab_token'), + ])->post($this->tahesabApiUrl(), [ + "DoNewMoshtari" => [ + (string)$customer->name, + $this->Config('name_fa'), + (string)$customer->mobile, + "", + "", + "", + "", + "", + -1, + 0 + ] + ])->json(); + + if ($this->sync()->responseOk($response)) { + $customer->moshtari_code = $response['OK']; + $customer->save(); + return $response['OK']; + } + } catch (\Exception $e) { + return $e->getMessage(); + } + + return 'tahesab api error!!!'; + } finally { + $lock->release(); + } } $birth_date = ''; diff --git a/app/Observers/CustomerObserver.php b/app/Observers/CustomerObserver.php new file mode 100644 index 0000000..b069533 --- /dev/null +++ b/app/Observers/CustomerObserver.php @@ -0,0 +1,14 @@ +registerOnCreate($customer); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index de092b6..7494738 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,6 +3,8 @@ namespace App\Providers; use App\Console\MigrateCommand; +use App\Models\Customer; +use App\Observers\CustomerObserver; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Migrations\MigrationRepositoryInterface; use Illuminate\Database\MigrationServiceProvider; @@ -40,6 +42,6 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - // + Customer::observe(CustomerObserver::class); } } diff --git a/app/Services/TahesabSyncService.php b/app/Services/TahesabSyncService.php index 269fc05..fcd6f9a 100644 --- a/app/Services/TahesabSyncService.php +++ b/app/Services/TahesabSyncService.php @@ -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');