price_offset_percent ?? 0) : 0.0; } /** @return array|null */ public static function syncFromReference(Product $product, int $basePrice, int $baseProfit = 0, ?string $sourceName = null): ?array { if (!Product::hasStoredPriceColumns()) { return null; } $offsetPercent = self::offsetPercent($product); $adjustedPrice = self::adjustedTotal($basePrice, $baseProfit, $offsetPercent); $oldPrice = (int) ($product->price ?? 0); if ( $adjustedPrice <= 0 && Product::hasUseLastPriceIfZeroColumn() && ($product->use_last_price_if_zero ?? false) ) { return null; } $data = ['price' => $adjustedPrice]; if (\Illuminate\Support\Facades\Schema::hasColumn($product->getTable(), 'price_profit')) { $data['price_profit'] = 0; } Product::where('id', $product->id)->update($data); return [ 'product_id' => $product->id, 'product_title' => $product->title, 'reading_method' => $product->reading_method, 'source_name' => $sourceName ?? $product->reading_method, 'base_price' => $basePrice, 'base_profit' => $baseProfit, 'offset_percent' => $offsetPercent, 'old_price' => $oldPrice, 'new_price' => $adjustedPrice, 'changed' => $oldPrice !== $adjustedPrice, ]; } /** وقتی فقط درصد در ادمین عوض می‌شود (بدون sync جدید) */ public static function reapplyOffsetPercent(Product $product, float $newOffsetPercent, ?float $oldOffsetPercent = null): void { if (!Product::hasPriceOffsetColumn() || !Product::hasStoredPriceColumns()) { return; } $oldOffset = $oldOffsetPercent ?? self::offsetPercent($product); $currentTotal = (int) ($product->price + ($product->price_profit ?? 0)); $reference = $oldOffset != 0.0 ? (int) round($currentTotal / (1 + $oldOffset / 100)) : $currentTotal; Product::where('id', $product->id)->update([ 'price' => (int) round($reference * (1 + ($newOffsetPercent / 100))), 'price_profit' => 0, ]); } }