zarbox added 2

This commit is contained in:
2026-06-04 14:42:22 +03:30
parent 92d01f96da
commit f5d53b2747
4 changed files with 86 additions and 9 deletions
+17 -3
View File
@@ -330,10 +330,24 @@ class ReportController extends Controller
}
if ($pr_product && !$closed) {
if ($priceProvider === 'main_product') {
app(EtehadiePriceService::class)->syncProductFromApi($pr_product->fresh());
$pr_product->refresh();
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;
}
$pr_price = (float) $pr_product->price + (float) ($pr_product->price_profit ?? 0);
}
/*
+17
View File
@@ -16,6 +16,7 @@ class Product extends Model
use HasFactory,Selector,SoftDeletes;
private static ?bool $priceOffsetColumnExists = null;
private static ?bool $priceColumnExists = null;
protected static function booted(): void
{
@@ -44,6 +45,22 @@ class Product extends Model
return static::$priceOffsetColumnExists;
}
/** گلد علی: جدول products ممکن است ستون price نداشته باشد */
public static function hasStoredPriceColumns(): bool
{
if (static::$priceColumnExists !== null) {
return static::$priceColumnExists;
}
try {
static::$priceColumnExists = Schema::hasColumn((new static)->getTable(), 'price');
} catch (\Throwable) {
static::$priceColumnExists = false;
}
return static::$priceColumnExists;
}
public function getPriceOffsetPercentAttribute($value): float
{
if (!static::hasPriceOffsetColumn()) {
+40
View File
@@ -144,9 +144,49 @@ class EtehadiePriceService
return true;
}
/**
* قیمت زنده از API بدون نوشتن در products (برای DB بدون ستون price).
*
* @return array{order_price: int, raw: array<string, mixed>|null}
*/
public function fetchLiveOrderPrice(?string $readingMethod, ?string $apiUrl = null): array
{
$empty = ['order_price' => 0, 'raw' => null];
$readingMethod = trim((string) $readingMethod);
if ($readingMethod === '') {
return $empty;
}
try {
$url = $apiUrl ?: $this->apiUrl();
$prices = Http::withoutVerifying()->timeout(20)->get($url)->json();
if (!is_array($prices)) {
return $empty;
}
$row = collect($prices)->firstWhere('key', $readingMethod)
?? collect($prices)->firstWhere('id', $readingMethod);
if (empty($row)) {
return $empty;
}
$orderPrice = (int) round((float) ($row['price'] ?? 0) + (float) ($row['profit'] ?? 0));
return ['order_price' => $orderPrice, 'raw' => $row];
} catch (\Throwable $e) {
Log::warning('Live board price fetch failed', ['message' => $e->getMessage()]);
return $empty;
}
}
/** به‌روز قیمت یک محصول از API با reading_method */
public function syncProductFromApi(Product $product): bool
{
if (!Product::hasStoredPriceColumns()) {
return false;
}
if (!$product->reading_method) {
return false;
}
+12 -6
View File
@@ -28,18 +28,24 @@ class ProductPriceService
public static function syncFromReference(Product $product, int $basePrice, int $baseProfit = 0): void
{
$offsetPercent = self::offsetPercent($product);
if (!Product::hasStoredPriceColumns()) {
return;
}
Product::where('id', $product->id)->update([
'price' => self::adjustedTotal($basePrice, $baseProfit, $offsetPercent),
'price_profit' => 0,
]);
$offsetPercent = self::offsetPercent($product);
$data = ['price' => self::adjustedTotal($basePrice, $baseProfit, $offsetPercent)];
if (\Illuminate\Support\Facades\Schema::hasColumn($product->getTable(), 'price_profit')) {
$data['price_profit'] = 0;
}
Product::where('id', $product->id)->update($data);
}
/** وقتی فقط درصد در ادمین عوض می‌شود (بدون sync جدید) */
public static function reapplyOffsetPercent(Product $product, float $newOffsetPercent, ?float $oldOffsetPercent = null): void
{
if (!Product::hasPriceOffsetColumn()) {
if (!Product::hasPriceOffsetColumn() || !Product::hasStoredPriceColumns()) {
return;
}