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
@@ -216,21 +216,40 @@ class CustomerSpecialPricePackageController extends Controller
throw new UnprocessableEntityHttpException('حداقل یک محصول انتخاب کنید.');
}
$profitBuy = (int) preg_replace('/[^\d]/', '', (string) $request->input('profit_buy', 0));
$profitSell = (int) preg_replace('/[^\d]/', '', (string) $request->input('profit_sell', 0));
$profitBuy = $this->parseSignedRial($request->input('profit_buy', 0));
$profitSell = $this->parseSignedRial($request->input('profit_sell', 0));
return [
'title' => $title,
'customer_ids' => $customerIds,
'product_ids' => $productIds,
'category_names' => $categoryNames,
'profit_buy' => max(0, $profitBuy),
'profit_sell' => max(0, $profitSell),
'profit_buy' => $profitBuy,
'profit_sell' => $profitSell,
'show_product' => filter_var($request->input('show_product', 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
{
$package->customers()->sync($data['customer_ids']);
@@ -1461,7 +1461,7 @@ class SingleOrderController extends Controller
throw new UnprocessableEntityHttpException('این محصول برای شما در دسترس نیست.');
}
$specialAmt = $specialSvc->profitForDirection($specialRule, $direction);
if ($specialAmt > 0) {
if ($specialAmt !== 0) {
$amount += $specialAmt;
$keys[] = 'customer_special_pricing';
$lines[] = [
@@ -142,17 +142,17 @@ class CustomerSpecialPricingService
$buyExtra = (int) ($rule['profit_buy'] ?? 0);
$sellExtra = (int) ($rule['profit_sell'] ?? 0);
if (isset($payload['buy']) && (int) $payload['buy'] > 0 && $buyExtra > 0) {
$payload['buy'] = (int) $payload['buy'] + $buyExtra;
if (isset($payload['buy']) && (int) $payload['buy'] > 0 && $buyExtra !== 0) {
$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);
}
if (isset($payload['price_last']) && (int) $payload['price_last'] > 0 && $buyExtra > 0) {
$payload['price_last'] = (int) $payload['price_last'] + $buyExtra;
if (isset($payload['price_last']) && (int) $payload['price_last'] > 0 && $buyExtra !== 0) {
$payload['price_last'] = max(0, (int) $payload['price_last'] + $buyExtra);
}
if (isset($payload['priceLast']) && (int) $payload['priceLast'] > 0 && $buyExtra > 0) {
$payload['priceLast'] = (int) $payload['priceLast'] + $buyExtra;
if (isset($payload['priceLast']) && (int) $payload['priceLast'] > 0 && $buyExtra !== 0) {
$payload['priceLast'] = max(0, (int) $payload['priceLast'] + $buyExtra);
}
$payload['special_pricing'] = [