feat(server): store and apply negative special price profits

Keep signed buy/sell extras so discounts work on quotes and orders, and ignore the nested user clone in this repo.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-17 13:11:00 +03:30
co-authored by Cursor
parent bd8284e4f8
commit 7d405cb83b
6 changed files with 64 additions and 14 deletions
+3
View File
@@ -13,3 +13,6 @@ npm-debug.log
yarn-error.log yarn-error.log
/.idea /.idea
/.vscode /.vscode
khaneghi_user/
.DS_Store
@@ -216,21 +216,40 @@ class CustomerSpecialPricePackageController extends Controller
throw new UnprocessableEntityHttpException('حداقل یک محصول انتخاب کنید.'); throw new UnprocessableEntityHttpException('حداقل یک محصول انتخاب کنید.');
} }
$profitBuy = (int) preg_replace('/[^\d]/', '', (string) $request->input('profit_buy', 0)); $profitBuy = $this->parseSignedRial($request->input('profit_buy', 0));
$profitSell = (int) preg_replace('/[^\d]/', '', (string) $request->input('profit_sell', 0)); $profitSell = $this->parseSignedRial($request->input('profit_sell', 0));
return [ return [
'title' => $title, 'title' => $title,
'customer_ids' => $customerIds, 'customer_ids' => $customerIds,
'product_ids' => $productIds, 'product_ids' => $productIds,
'category_names' => $categoryNames, 'category_names' => $categoryNames,
'profit_buy' => max(0, $profitBuy), 'profit_buy' => $profitBuy,
'profit_sell' => max(0, $profitSell), 'profit_sell' => $profitSell,
'show_product' => filter_var($request->input('show_product', true), FILTER_VALIDATE_BOOLEAN), 'show_product' => filter_var($request->input('show_product', true), FILTER_VALIDATE_BOOLEAN),
'is_active' => filter_var($request->input('is_active', true), FILTER_VALIDATE_BOOLEAN), 'is_active' => filter_var($request->input('is_active', true), FILTER_VALIDATE_BOOLEAN),
]; ];
} }
private function parseSignedRial(mixed $value): int
{
$raw = trim((string) $value);
$raw = str_replace(["\u{066C}", ',', '٬', ' ', 'ریال'], '', $raw);
$raw = strtr($raw, [
'۰' => '0', '۱' => '1', '۲' => '2', '۳' => '3', '۴' => '4',
'۵' => '5', '۶' => '6', '۷' => '7', '۸' => '8', '۹' => '9',
'٠' => '0', '١' => '1', '٢' => '2', '٣' => '3', '٤' => '4',
'٥' => '5', '٦' => '6', '٧' => '7', '٨' => '8', '٩' => '9',
'−' => '-', '–' => '-', '—' => '-',
]);
$negative = str_starts_with($raw, '-');
$digits = preg_replace('/\D/', '', $raw) ?? '0';
$amount = (int) ($digits === '' ? '0' : $digits);
return $negative ? -$amount : $amount;
}
private function syncRelations(CustomerSpecialPricePackage $package, array $data): void private function syncRelations(CustomerSpecialPricePackage $package, array $data): void
{ {
$package->customers()->sync($data['customer_ids']); $package->customers()->sync($data['customer_ids']);
@@ -1461,7 +1461,7 @@ class SingleOrderController extends Controller
throw new UnprocessableEntityHttpException('این محصول برای شما در دسترس نیست.'); throw new UnprocessableEntityHttpException('این محصول برای شما در دسترس نیست.');
} }
$specialAmt = $specialSvc->profitForDirection($specialRule, $direction); $specialAmt = $specialSvc->profitForDirection($specialRule, $direction);
if ($specialAmt > 0) { if ($specialAmt !== 0) {
$amount += $specialAmt; $amount += $specialAmt;
$keys[] = 'customer_special_pricing'; $keys[] = 'customer_special_pricing';
$lines[] = [ $lines[] = [
@@ -142,17 +142,17 @@ class CustomerSpecialPricingService
$buyExtra = (int) ($rule['profit_buy'] ?? 0); $buyExtra = (int) ($rule['profit_buy'] ?? 0);
$sellExtra = (int) ($rule['profit_sell'] ?? 0); $sellExtra = (int) ($rule['profit_sell'] ?? 0);
if (isset($payload['buy']) && (int) $payload['buy'] > 0 && $buyExtra > 0) { if (isset($payload['buy']) && (int) $payload['buy'] > 0 && $buyExtra !== 0) {
$payload['buy'] = (int) $payload['buy'] + $buyExtra; $payload['buy'] = max(0, (int) $payload['buy'] + $buyExtra);
} }
if (isset($payload['sell']) && (int) $payload['sell'] > 0 && $sellExtra > 0) { if (isset($payload['sell']) && (int) $payload['sell'] > 0 && $sellExtra !== 0) {
$payload['sell'] = max(0, (int) $payload['sell'] - $sellExtra); $payload['sell'] = max(0, (int) $payload['sell'] - $sellExtra);
} }
if (isset($payload['price_last']) && (int) $payload['price_last'] > 0 && $buyExtra > 0) { if (isset($payload['price_last']) && (int) $payload['price_last'] > 0 && $buyExtra !== 0) {
$payload['price_last'] = (int) $payload['price_last'] + $buyExtra; $payload['price_last'] = max(0, (int) $payload['price_last'] + $buyExtra);
} }
if (isset($payload['priceLast']) && (int) $payload['priceLast'] > 0 && $buyExtra > 0) { if (isset($payload['priceLast']) && (int) $payload['priceLast'] > 0 && $buyExtra !== 0) {
$payload['priceLast'] = (int) $payload['priceLast'] + $buyExtra; $payload['priceLast'] = max(0, (int) $payload['priceLast'] + $buyExtra);
} }
$payload['special_pricing'] = [ $payload['special_pricing'] = [
@@ -12,8 +12,8 @@ return new class extends Migration
Schema::create('customer_special_price_packages', function (Blueprint $table) { Schema::create('customer_special_price_packages', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('title'); $table->string('title');
$table->unsignedBigInteger('profit_buy')->default(0); $table->bigInteger('profit_buy')->default(0);
$table->unsignedBigInteger('profit_sell')->default(0); $table->bigInteger('profit_sell')->default(0);
$table->boolean('show_product')->default(true); $table->boolean('show_product')->default(true);
$table->boolean('is_active')->default(true); $table->boolean('is_active')->default(true);
$table->timestamps(); $table->timestamps();
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('customer_special_price_packages')) {
return;
}
DB::statement('ALTER TABLE customer_special_price_packages MODIFY profit_buy BIGINT NOT NULL DEFAULT 0');
DB::statement('ALTER TABLE customer_special_price_packages MODIFY profit_sell BIGINT NOT NULL DEFAULT 0');
}
public function down(): void
{
if (! Schema::hasTable('customer_special_price_packages')) {
return;
}
DB::statement('ALTER TABLE customer_special_price_packages MODIFY profit_buy BIGINT UNSIGNED NOT NULL DEFAULT 0');
DB::statement('ALTER TABLE customer_special_price_packages MODIFY profit_sell BIGINT UNSIGNED NOT NULL DEFAULT 0');
}
};