fixed auto add customer into tahesab
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Http\Controllers\Site\ReportController;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
use App\Models\Tahesab;
|
use App\Models\Tahesab;
|
||||||
use App\Models\Customer;
|
use App\Models\Customer;
|
||||||
use App\Services\TahesabSyncService;
|
use App\Services\TahesabSyncService;
|
||||||
@@ -554,36 +555,58 @@ class TahesabController extends Controller
|
|||||||
$response = null;
|
$response = null;
|
||||||
|
|
||||||
if ($customer->moshtari_code == null) {
|
if ($customer->moshtari_code == null) {
|
||||||
try {
|
$lock = Cache::lock('tahesab:register-customer:'.$customer->id, 30);
|
||||||
$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)) {
|
if (! $lock->get()) {
|
||||||
$customer->moshtari_code = $response['OK'];
|
$customer->refresh();
|
||||||
$customer->save();
|
|
||||||
return $response['OK'];
|
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 = '';
|
$birth_date = '';
|
||||||
|
|||||||
@@ -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,6 +3,8 @@
|
|||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
use App\Console\MigrateCommand;
|
use App\Console\MigrateCommand;
|
||||||
|
use App\Models\Customer;
|
||||||
|
use App\Observers\CustomerObserver;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
|
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
|
||||||
use Illuminate\Database\MigrationServiceProvider;
|
use Illuminate\Database\MigrationServiceProvider;
|
||||||
@@ -40,6 +42,6 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
//
|
Customer::observe(CustomerObserver::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ namespace App\Services;
|
|||||||
use App\Http\Controllers\Admin\TahesabController;
|
use App\Http\Controllers\Admin\TahesabController;
|
||||||
use App\Models\Customer;
|
use App\Models\Customer;
|
||||||
use App\Models\Tahesab;
|
use App\Models\Tahesab;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
class TahesabSyncService
|
class TahesabSyncService
|
||||||
@@ -78,12 +80,54 @@ class TahesabSyncService
|
|||||||
return empty($tahesab->tahesab_id);
|
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
|
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);
|
$controller = app(TahesabController::class);
|
||||||
$result = $controller->createCustomerInTahesab($customer->id);
|
$result = $controller->createCustomerInTahesab($customer->id);
|
||||||
|
|
||||||
if (is_string($result) && str_contains($result, 'error')) {
|
if (is_string($result)) {
|
||||||
return ['ok' => false, 'message' => $result];
|
return ['ok' => false, 'message' => $result];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,6 +194,22 @@ class TahesabSyncService
|
|||||||
return 'goldReward';
|
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
|
private function hasSyncColumns(): bool
|
||||||
{
|
{
|
||||||
return Schema::hasTable('tahesab') && Schema::hasColumn('tahesab', 'sync_status');
|
return Schema::hasTable('tahesab') && Schema::hasColumn('tahesab', 'sync_status');
|
||||||
|
|||||||
Reference in New Issue
Block a user