fixed auto add customer into tahesab

This commit is contained in:
2026-06-27 22:10:29 +03:30
parent 58b55b920a
commit e21e97988c
4 changed files with 127 additions and 28 deletions
@@ -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 = '';
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace App\Observers;
use App\Models\Customer;
use App\Services\TahesabSyncService;
class CustomerObserver
{
public function created(Customer $customer): void
{
app(TahesabSyncService::class)->registerOnCreate($customer);
}
}
+3 -1
View File
@@ -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);
}
}
+61 -1
View File
@@ -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');