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) { if ($input instanceof \Illuminate\Http\Request) { $data = $input->all(); } elseif (is_array($input)) { $data = $input; } else { $data = request()->all(); } // return ($data); if (!isset($data['customer_id']) || !isset($data['product']['amount'])) { return request()->expectsJson() ? response()->json(['error' => 'Data missing: customer_id or value'], 422) : false; } // $customer = auth()->user(); // if (!$customer) { // throw new UnprocessableEntityHttpException("Auth needed"); // } $customer = Customer::where('id', $data['customer_id'])->first(); if ($customer && ($customer->moshtari_code === null)) { $this->store(); } $date = Verta::instance(Carbon::parse($data['pay_at']))->format('Y-m-d'); [$year, $month, $day] = explode('-', $date); $amount = $data['amount']; $tahesab = Tahesab::create([ "customer_id" => $customer->id, "desc" => $data['direction'] == 1 ? "خرید آبشده" : "فروش آبشده", "amount" => $amount, "date" => $data['pay_at'], "type" => $data['product']['metal_id'] == 1 ? "طلا" : "نقره", "created_at" => Carbon::now(), ]); 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'), [ "DoNewSanadBuySaleGOLD" => [ 1,//Sabte_Kol_Or_Movaghat_1_0 (int)$customer->moshtari_code, //Moshtari_Code -1, //Factor_Number 1, //Radif_Number (int)$year, //Shamsi_Year (int)$month, //Shamsi_Month (int)$day, //Shamsi_Day (float)$amount, //Vazn $data['product']['metal_id'] == 1 ? 750 : "", //Ayar 0, "", $data['direction'] == 1 ? 0 : 1, //BuyOrSale_0_1 (int)$data['price'], //Mazaneh 1, //MazanehIsMesghalOrGeram_0_1 1, //IsMotefaregheOrAbshode_0_1 (int)$data['total_price_original'], //MablaghKol $data['direction'] == 1 ? "خرید آبشده" : "فروش آبشده", "Code_Factor", "", //HavalehBe_Mcode "", //MultiRadif $data['product']['metal_id'] == 1 ? 0 : 2, //Jens_Felez (string)Verta::instance($data['pay_at']), ] ]); } catch (Exception $e) { return $e->getMessage(); } if ($response['OK']) { $tahesab->sh_factor = $response['Sh_factor']; $tahesab->tahesab_id = $response['OK']; $tahesab->factor_number = $response['Factor_Number']; $tahesab->tah_id = $response['ID']; $tahesab->res = $response; $tahesab->data = $data; $tahesab->save(); return $data; } return false; } // directon: 0 = daryaft az moshtari // directon: 1 = pardakht be moshtari public function wallet($input = null) { if ($input instanceof \Illuminate\Http\Request) { $data = $input->all(); } elseif (is_array($input)) { $data = $input; } else { $data = request()->all(); } if (!isset($data['customer_id']) || !isset($data['value'])) { return request()->expectsJson() ? response()->json(['error' => 'Data missing: customer_id or value'], 422) : false; } // $customer = auth()->user(); // if (!$customer) { // throw new UnprocessableEntityHttpException("Auth needed"); // } $customer = Customer::where('id', $data['customer_id'])->first(); if (!$customer || ($customer->moshtari_code === null)) { $this->store(); } $tahesab = Tahesab::create([ "customer_id" => $customer->id, "desc" => $data['direction'] == 0 ? "شارژ حساب" : "برداشت از حساب", "amount" => $data['value'], "date" => Carbon::now(), "created_at" => Carbon::now(), ]); 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'), [ "DoNewSanadVKHBank" => [ 1,//Sabte_Kol_Or_Movaghat_1_0 $customer->moshtari_code,//Moshtari_Code -1,//Factor_Number 1,//Radif_Number 1404,//Shamsi_Year 10,//Shamsi_Month 18,//Shamsi_Day $data['direction'],//IsDayaftAzMoshtariOrPardakhtBeMoshtari_0_1 (int)$data['value'],//Mablagh $data['direction'] == 0 ? "شارژ حساب" : "برداشت از حساب",//Sharh "test", "$tahesab->id",//Code_Rahgiri -1,//Factor_Code -1, //MultiRadif ] ]); } catch (\Exception $e) { return $e->getMessage(); } if ($response['OK']) { $tahesab->sh_factor = $response['Sh_factor']; $tahesab->tahesab_id = $response['OK']; $tahesab->save(); return $data; } return false; } }