whereDate('date_at', '<=', date("Y-m-d")) ->whereDate('date_at', '>', Carbon::now()->subDays(30)->format("Y-m-d")) ->get()->toArray(); $a = []; foreach ($candle as $c) { $create['date'] = $c["date_at"]; unset($c["date_at"]); $create['candle'] = array_values($c); $a[] = array_values($create); } return response()->json($a); } public function updatePrise() { // $difference = config('fee.single'); $difference = Setting::where('key', 'fee_single')->first()->value; $lastPrice = Price::latest()->first(); $PriceTriggers = PriceTrigger::where("is_active", true)->get(); foreach ($PriceTriggers as $PriceTrigger) { $priseGold = $PriceTrigger->dirction == 1 ? $lastPrice->price - $difference : ($PriceTrigger->dirction == 2 ? $lastPrice->price + $difference : $lastPrice->price); if (eval('return $priseGold ' . $PriceTrigger->price_condition . ' $PriceTrigger->target_price;')) { $customer = Customer::find($PriceTrigger->customer_id); $message = "قیمت طلا به " . number_format($priseGold, -1) . " در هر گرم رسید\n" .$this->Config('name_fa'); if ($PriceTrigger->action == 'email') { Mail::to("$customer->email")->send(new YourMailableClass($message, '', '', '', '')); } elseif ($PriceTrigger->action == 'sms') { $this->send($customer->mobile, $message); } $PriceTrigger->is_active = false; $PriceTrigger->triggered_at = Carbon::now(); $PriceTrigger->save(); } } return response()->json(); } public function upDownPrice() { return $this->buildUpDownPrices(); } protected function buildUpDownPrices(): array { $feeSettings = Setting::whereIn('key', ['fee_single', 'buy_fee', 'sell_fee']) ->pluck('value', 'key'); $difference = (float) ($feeSettings->get('fee_single') ?? 0); $diffBuy = (float) ($feeSettings->get('buy_fee') ?: $difference); $diffSell = (float) ($feeSettings->get('sell_fee') ?: $difference); $upSetting = Setting::where('key', 'up_price')->first(); $downSetting = Setting::where('key', 'down_price')->first(); if ($upSetting && $downSetting) { $down = (float) ($downSetting->value ?: 0); $up = (float) ($upSetting->value ?: 0); if ($down > 0 && $up > 0) { return [ 'sell' => $down - $diffSell, 'buy' => $up + $diffBuy, ]; } } $today = Carbon::now()->format("Y-m-d"); $prises = Price::where('date_at', $today)->get("price"); $minP = 0; $maxP = 0; foreach ($prises as $p) { if ($minP == 0 || $p->price < $minP) { $minP = $p->price; } if ($maxP == 0 || $p->price > $maxP) { $maxP = $p->price; } } if (count($prises) === 0) { $price = Price::orderBy('id', 'desc')->first(); $priceValue = $price->price; return ['sell' => $priceValue - $diffSell, 'buy' => $priceValue + $diffBuy]; } return ['sell' => $minP - $diffSell, 'buy' => $maxP + $diffBuy]; } protected function appendUpPrices(array $payload): array { $payload['up'] = $this->buildUpDownPrices(); return $payload; } public function lastPrice() { $config = app(ConfigService::class); $closed = false; $montzar_price = 0; $zaarbox_price = 0; $tgn = 0; $pr_price = 0; $open_buy = true; $open_sell = true; $montzar_open_buy = false; $montzar_open_sell = false; $priceSource = null; $priceValue = 0; $toBool = function ($value) { return filter_var($value, FILTER_VALIDATE_BOOLEAN); }; $zeroResponse = function () { return $this->appendUpPrices([ 'sell' => 0, 'buy' => 0, 'price_last' => 0, 'price' => 0, 'main_product' => 0, 'taban_gohar' => 0, 'montzar' => 0, 'zaarbox' => 0, 'closed' => true, 'time' => now(), ]); }; $priceProvider = $config->priceProvider(); /* |-------------------------------------------------------------------------- | بستن قیمت طبق منطق قبلی |-------------------------------------------------------------------------- */ if ($toBool($config->Config('close_price'))) { $startDay = $config->Config('startDay') ?: '00:00'; [$startHour, $startMinute] = explode(':', $startDay); $endDay = $config->Config('endDay') ?: '23:59'; [$endHour, $endMinute] = explode(':', $endDay); $current = Carbon::now(); $start = Carbon::today()->setTime((int) $startHour, (int) $startMinute); $end = Carbon::today()->setTime((int) $endHour, (int) $endMinute); if (!$current->between($start, $end)) { // عمومی: در Cnt بماند Cnt::where('key', 'app_closed')->update(['value' => 'true']); // مربوط به منتزر: settings $config->UpdateConfig('montzar_closed', 'true'); return $zeroResponse(); } Cnt::where('key', 'app_closed')->update(['value' => 'false']); } /* |-------------------------------------------------------------------------- | Settings |-------------------------------------------------------------------------- */ $settings = Setting::whereIn('key', [ 'fee_single', 'tabanGohar_price', 'main_product_price', 'buy_fee', 'sell_fee', 'show_last_price', ])->get(); $settingsMap = $settings->pluck('value', 'key'); $show_last_price = $toBool($settingsMap->get('show_last_price', false)); $taban_gohar_price = $settingsMap->get('tabanGohar_price'); $main_product_price = $settingsMap->get('main_product_price'); $difference = (float) $settingsMap->get('fee_single', 0); $diff_buy = (float) $settingsMap->get('buy_fee', $difference); $diff_sell = (float) $settingsMap->get('sell_fee', $difference); /* |-------------------------------------------------------------------------- | Montzar — فقط وقتی price_provider=montzar |-------------------------------------------------------------------------- */ if ($priceProvider === 'montzar' && !$closed) { try { $montzarBaseUrl = rtrim($config->Config('montzar_base_url') ?: 'https://montpar.ir', '/'); $montzarProductId = (int) ($config->Config('montzar_product_id') ?: 25); $montzar = app(MontzarService::class)->productList(); if (isset($montzar['message'])) { throw new \Exception('Provider message: ' . $montzar['message']); } $collection = collect($montzar); $montzarProduct = $collection->firstWhere('id', $montzarProductId); if (!$montzarProduct) { throw new \Exception("Provider product not found. product_id={$montzarProductId}"); } if (!isset($montzarProduct['sub_agent'])) { throw new \Exception("Provider product has no sub_agent. product_id={$montzarProductId}"); } $buy = (float) ($montzarProduct['sub_agent']['rate_buy_gram_price'] ?? 0); $sell = (float) ($montzarProduct['sub_agent']['rate_sell_gram_price'] ?? 0); if ($buy <= 0 || $sell <= 0) { throw new \Exception("Provider invalid gram price. buy={$buy}, sell={$sell}"); } $customPrice = []; $customPrice['price'] = ($buy + $sell) / 2; $customPrice['diff'] = ($buy - $sell) / 2; $config->UpdateConfig('montzar_live_price', (float) $customPrice['price']); $config->UpdateConfig('montzar_diff', (float) $customPrice['diff']); $config->UpdateConfig('montzar_mazaneh', ((float) $customPrice['price'] * 4.331802)); $config->UpdateConfig( 'montzar_truly_buy_mazaneh', $montzarProduct['sub_agent']['rate_buy_price'] ?? 0 ); $config->UpdateConfig( 'montzar_truly_sell_mazaneh', $montzarProduct['sub_agent']['rate_sell_price'] ?? 0 ); $config->UpdateConfig( 'montzar_last_product_log', json_encode($montzarProduct, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) ); $montzar_open_sell = $toBool($montzarProduct['open_sell'] ?? false); $montzar_open_buy = $toBool($montzarProduct['open_buy'] ?? false); $config->UpdateConfig('montzar_open_sell', $montzar_open_sell ? 'true' : 'false'); $config->UpdateConfig('montzar_open_buy', $montzar_open_buy ? 'true' : 'false'); if ($montzar_open_sell || $montzar_open_buy) { $montzar_price = (int) $customPrice['price']; } $config->UpdateConfig('montzar_closed', $montzar_price == 0 ? 'true' : 'false'); } catch (\Throwable $e) { $montzar_price = 0; \Log::error('MONZAR PRICE ERROR', [ 'message' => $e->getMessage(), 'montzar_product_id' => $montzarProductId ?? null, 'montzar_response' => $montzar ?? null, ]); $config->UpdateConfig('montzar_closed', 'true'); } } /* |-------------------------------------------------------------------------- | Zaarbox — price_provider=zaarbox + settings.zaarbox_price_key |-------------------------------------------------------------------------- */ if ($priceProvider === 'zaarbox' && !$closed) { $zaarbox = app(ZaarboxPriceService::class)->fetchPrice(); $zaarbox_price = (int) ($zaarbox['order_price'] ?? 0); } /* |-------------------------------------------------------------------------- | Main Product — settings.main_product سپس const |-------------------------------------------------------------------------- */ $pr_product = null; if ($priceProvider === 'main_product' || $toBool($main_product_price)) { $pr_product = $config->mainProduct(); } if ($pr_product && !$closed) { if ($priceProvider === 'main_product') { if (Product::hasStoredPriceColumns()) { app(EtehadiePriceService::class)->syncProductFromApi($pr_product->fresh()); $pr_product->refresh(); $pr_price = (float) $pr_product->price + (float) ($pr_product->price_profit ?? 0); } else { $apiUrl = $config->getString('zaarbox_api_url', '') ?: $config->getString('etehadie_api_url', 'https://price.zaarbox.ir/api/free'); $live = app(EtehadiePriceService::class)->fetchLiveOrderPrice( $pr_product->reading_method, $apiUrl ); $pr_price = (float) ($live['order_price'] ?? 0); } } else { $pr_price = Product::hasStoredPriceColumns() ? (float) $pr_product->price + (float) ($pr_product->price_profit ?? 0) : 0.0; } } /* |-------------------------------------------------------------------------- | Taban Gohar |-------------------------------------------------------------------------- */ if ($toBool($taban_gohar_price) && !$closed) { try { $taban_gohar = Http::withOptions([ 'curl' => [ CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, CURLOPT_TCP_FASTOPEN => 1, ], ]) ->withoutVerifying() ->timeout(2) ->get('https://tabangohar.com/GheymatKhan/prices_in_table.html') ->json(); $tgn = (int) $taban_gohar['c'] * 10; } catch (\Throwable $e) { $tgn = 0; } } /* |-------------------------------------------------------------------------- | انتخاب قیمت اصلی (همان price_provider) |-------------------------------------------------------------------------- */ if ($priceProvider === 'zaarbox' && $zaarbox_price > 0 && !$closed) { $priceValue = $zaarbox_price; $priceSource = 'zaarbox'; } elseif ($priceProvider === 'montzar' && ($montzar_price ?? 0) !== 0 && !$closed) { $priceValue = (int) $montzar_price; $priceSource = 'montzar'; } elseif ($priceProvider === 'taban_gohar' && $toBool($taban_gohar_price) && ($tgn ?? 0) !== 0 && !$closed) { $priceValue = $tgn; $priceSource = 'taban_gohar'; } elseif ($priceProvider === 'main_product' && ($pr_price ?? 0) !== 0 && !$closed) { $priceValue = $pr_price; $priceSource = 'main_product'; } else { $priceValue = 0; $priceSource = null; } if ($priceValue <= 0) { if (!$show_last_price) { return $this->appendUpPrices([ 'sell' => 0, 'buy' => 0, 'price_last' => 0, 'price' => 0, 'main_product' => $pr_price ?? 0, 'taban_gohar' => $tgn ?? 0, 'montzar' => $montzar_price ?? 0, 'zaarbox' => $zaarbox_price ?? 0, 'closed' => true, 'time' => now(), ]); } $lastPrice = Price::latest()->first(); if (!$lastPrice || (float) $lastPrice->price <= 0) { return $this->appendUpPrices([ 'sell' => 0, 'buy' => 0, 'price_last' => 0, 'price' => 0, 'main_product' => $pr_price ?? 0, 'taban_gohar' => $tgn ?? 0, 'montzar' => $montzar_price ?? 0, 'zaarbox' => $zaarbox_price ?? 0, 'closed' => true, 'time' => now(), ]); } $priceValue = (float) $lastPrice->price; $priceSource = 'database_last'; } /* |-------------------------------------------------------------------------- | ذخیره قیمت |-------------------------------------------------------------------------- */ $priceToStore = $priceValue; $lastPrice = Price::latest()->first(); if ( !$closed && (!$lastPrice || (float) $lastPrice->price != (float) $priceToStore) && $priceToStore > 30000000 ) { $price = new Price(); $price->price = $priceToStore; $price->time_at = Carbon::now()->toTimeString(); $price->date_at = Carbon::now()->toDateString(); $price->save(); $this->updatePrise(); $candle = PriceCandle::whereDate('date_at', date("Y-m-d"))->first(); if (!is_null($candle)) { $candle->end = $priceToStore; if ($priceToStore < $candle->low) { $candle->low = $priceToStore; } elseif ($priceToStore > $candle->high) { $candle->high = $priceToStore; } $candle->save(); } else { PriceCandle::create([ 'start' => $priceToStore, 'end' => $priceToStore, 'high' => $priceToStore, 'low' => $priceToStore, 'date_at' => date('Y-m-d'), ]); } $lastPrice = $price; } /* |-------------------------------------------------------------------------- | محاسبه sell / buy |-------------------------------------------------------------------------- */ if ($priceSource === 'montzar') { $montzar_profit = (float) $config->Config('montzar_diff'); $final_buy_diff = $diff_buy + $montzar_profit; $final_sell_diff = $diff_sell + $montzar_profit; $sell = $montzar_open_sell ? ceil($priceValue - $final_sell_diff) : 0; $buy = $montzar_open_buy ? ceil($priceValue + $final_buy_diff) : 0; $priceLast = ceil($priceValue + $final_buy_diff); } else { $sell = $open_sell ? ceil($priceValue - $difference) : 0; $buy = $open_buy ? ceil($priceValue + $difference) : 0; $priceLast = ceil($priceValue + $difference); } /* | main_product / zaarbox: سفارش از Price::latest() — نمایش buy/sell همان منبع. */ if (in_array($priceProvider, ['main_product', 'zaarbox'], true) && !$closed) { $storedPrice = $lastPrice ?? Price::latest()->first(); if ($storedPrice && (float) $storedPrice->price > 0) { $base = (float) $storedPrice->price; $sell = $open_sell ? (int) ceil($base - $difference) : 0; $buy = $open_buy ? (int) ceil($base + $difference) : 0; $priceLast = (int) ceil($base + $difference); } } if (!$closed && $priceValue > 0) { $config->trackDailyPriceExtreme((float) $priceValue); } return $this->appendUpPrices([ 'sell' => $sell, 'buy' => $buy, 'price_last' => $priceLast, 'price' => $priceValue, 'main_product' => $pr_price ?? 0, 'taban_gohar' => $tgn ?? 0, 'montzar' => $montzar_price ?? 0, 'zaarbox' => $zaarbox_price ?? 0, 'closed' => false, 'time' => now(), ]); } public function check() { $service = Http:: withHeaders([ 'accept' => 'application/json', 'Authorization' => 'Bearer ohxsrbjagqvbt0glaygt' ])->get('https://studio.persianapi.com/index.php/web-service/common/gold-currency-coin?format=json&limit=30&page=1') ->json(); $list = []; if (isset($service['result'])) { foreach ($service['result'] as $item) { if ($item['key'] == 137120 || $item['key'] == 137119) { $list[] = $item; if ($item['key'] == 137120) { (new PriceController())->store(new Request(["price" => $item['price']])); } } } Cnt::where('key', 'price')->update(['value_json' => $list]); } } public function test() { $a = '{{12, 23, {4, 0, {1}, {1}}}, 0, {1}}'; $b = str_split($a); $c = []; $str = ""; foreach ($b as $item) { if ($item != " ") { print_r($item); print_r('++'); if ($item == ',' || $item == '}') { if (!in_array($c[count($c) - 1], ['{', '}'])) { $c[count($c) - 1] += (int)$str; print_r((int)$str); print_r('^^'); } else { $c[] = $str; print_r($c); print_r('&&'); } $str = ""; } elseif (in_array($item, ['{', '}'])) { $c[] = $item; print_r($c); print_r('**'); } else { $str .= $item; print_r($str); print_r('@@'); } } } dd($c); $d = []; $e = []; foreach ($c as $item) { if ($item == '}') { print_r($d[count($d) - 1]); print_r('***'); $e[] = $d[count($d) - 1]; } else { $d[] = $item; print_r($d); print_r('-----'); } } dd($e); } }