From 7de42d4edc7ea5f7bbd465cf33386562c0a78a08 Mon Sep 17 00:00:00 2001 From: Naser moghbel Date: Sat, 14 Mar 2026 09:57:15 +0330 Subject: [PATCH] NASER optimize tahesab controller --- .../Controllers/Admin/TahesabController.php | 153 +++++++++--------- 1 file changed, 79 insertions(+), 74 deletions(-) diff --git a/app/Http/Controllers/Admin/TahesabController.php b/app/Http/Controllers/Admin/TahesabController.php index b7bcc33..497f02b 100644 --- a/app/Http/Controllers/Admin/TahesabController.php +++ b/app/Http/Controllers/Admin/TahesabController.php @@ -15,77 +15,6 @@ use Hekmatinasser\Verta\Verta; class TahesabController extends Controller { - public function create_user($customer_id) - { - - - $customer = Customer::findOrFail($customer_id); - - if ($customer && ($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((string)env('TAHESAB'), [ - "DoNewMoshtari" => [ - (string)$customer->name, - $this->Config('name_fa'), - (string)$customer->mobile, - "", - "", - "", - "", - "", - -1, - 0 - ] -//{"DoNewMoshtari":["Name", "GroupName", "Tel", "Address", "CodeMeli", "Tarikh_Tavalod", "Moaref", "Code_Moaref", Moshtari_Code, Jens_Felez]} - ]); - if (!empty($response['OK'])) { - $customer->moshtari_code = $response['OK']; - $customer->save(); - } - } catch (\Exception $e) { - return $e->getMessage(); - } - }else { - if($customer && $customer->birth_date !== null){ - $birth_date = Verta::instance(Carbon::parse($customer->birth_date))->format('Y-m-d'); - } - try { - $response = Http::withoutVerifying()->timeout(10)->withHeaders([ - 'DBName' => $this->Config('tahesab_db'), - 'Content-Type' => 'application/json', - 'Authorization' => "Bearer " . $this->Config('tahesab_token'), - ])->post((string)env('TAHESAB'), [ - "DoEditMoshtari" => [ - $customer->name, - $this->Config('name_fa'), - $customer->mobile, - "", - $customer->n_code ?? "", - $birth_date ?? "", - "", - "", - (int)$customer->moshtari_code, - 0 // tala - ] -//{"DoEditMoshtari":["Name","GroupName", "Tel", "Address", "CodeMeli", "Tarikh_Tavalod", "Moaref", "Code_Moaref", Moshtari_Code]} - ]); - } catch (\Exception $e) { - return response($e->getMessage(), 500); - } - - } - if (empty($response['OK'])) { - return "tahesab api error!!!"; - } - - - return true; - - } public function storeTrade($input = null) { @@ -111,7 +40,8 @@ class TahesabController extends Controller $customer = Customer::where('id', $data['customer_id'])->first(); if ($customer && ($customer->moshtari_code === null)) { - $this->store(); + $this->create_user($customer->id); + $customer = Customer::where('id', $data['customer_id'])->first(); } $date = Verta::instance(Carbon::parse($data['pay_at']))->format('Y-m-d'); [$year, $month, $day] = explode('-', $date); @@ -200,8 +130,9 @@ class TahesabController extends Controller // } $customer = Customer::where('id', $data['customer_id'])->first(); - if (!$customer || ($customer->moshtari_code === null)) { - $this->store(); + if ($customer && ($customer->moshtari_code === null)) { + $this->create_user($customer->id); + $customer = Customer::where('id', $data['customer_id'])->first(); } $tahesab = Tahesab::create([ "customer_id" => $customer->id, @@ -245,4 +176,78 @@ class TahesabController extends Controller } return false; } + + private function create_user($customer_id) + { + + + $customer = Customer::findOrFail($customer_id); + + if ($customer && ($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((string)env('TAHESAB'), [ + "DoNewMoshtari" => [ + (string)$customer->name, + $this->Config('name_fa'), + (string)$customer->mobile, + "", + "", + "", + "", + "", + -1, + 0 + ] +//{"DoNewMoshtari":["Name", "GroupName", "Tel", "Address", "CodeMeli", "Tarikh_Tavalod", "Moaref", "Code_Moaref", Moshtari_Code, Jens_Felez]} + ]); + if (!empty($response['OK'])) { + $customer->moshtari_code = $response['OK']; + $customer->save(); + return $response['OK']; + } + } catch (\Exception $e) { + return $e->getMessage(); + } + }else { + if($customer && $customer->birth_date !== null){ + $birth_date = Verta::instance(Carbon::parse($customer->birth_date))->format('Y-m-d'); + } + try { + $response = Http::withoutVerifying()->timeout(10)->withHeaders([ + 'DBName' => $this->Config('tahesab_db'), + 'Content-Type' => 'application/json', + 'Authorization' => "Bearer " . $this->Config('tahesab_token'), + ])->post((string)env('TAHESAB'), [ + "DoEditMoshtari" => [ + $customer->name, + $this->Config('name_fa'), + $customer->mobile, + "", + $customer->n_code ?? "", + $birth_date ?? "", + "", + "", + (int)$customer->moshtari_code, + 0 // tala + ] +//{"DoEditMoshtari":["Name","GroupName", "Tel", "Address", "CodeMeli", "Tarikh_Tavalod", "Moaref", "Code_Moaref", Moshtari_Code]} + ]); + } catch (\Exception $e) { + return response($e->getMessage(), 500); + } + + } + if (empty($response['OK'])) { + return "tahesab api error!!!"; + } + + + return true; + + } + }