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']);