feat(convert): text the customer when a convert request is approved or rejected

The message goes out after the result is saved, and a send failure does not undo the approval or rejection.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-01 13:44:48 +03:30
co-authored by Cursor
parent 3f59af5a6d
commit c96e5820fe
2 changed files with 55 additions and 1 deletions
@@ -3,6 +3,7 @@
فرمت هر خط:
- YYYY-MM-DD | موضوع | مسیر فایل(ها) | نکتهٔ ۱ خطی
- 2026-11-01 | پیامک تبدیل | `CopperConvertRequestService::notifyCustomer` | بعد از تأیید یا رد، پیامک جدا به موبایل مشتری؛ خطا پیامک سفارش را برنمی‌گرداند
- 2026-11-01 | درخواست تبدیل مس | `CopperConvertRequestService` + `copper_convert_requests` + `POST customer/copper-convert-request` + `admin/copper-convert-request/{id}/approve` | تأیید = فروش دارایی و خرید حلقه با quote_unit_price؛ مهاجرت `2026_11_01_000001`
- 2026-10-28 | شایگان کالا/مشتری | `products.shayegan_item_number` + `ShayeganSyncService::registerCustomer` Account/Put + `ensureCustomerAccountNumber` | بانک 10001 و معین 12101 در settings؛ ItemNumber per-product
- 2026-10-17 | لاگ ترافیک شایگان | `ShayeganClient::request` | `Log::info` با کلید `shayegan.http_request` / `shayegan.http_response`; رمز `***`; خاموش: setting `shayegan_log_traffic=0`
+54 -1
View File
@@ -2,6 +2,7 @@
namespace App\Services;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Single\SingleOrderController;
use App\Models\CopperConvertRequest;
use App\Models\Customer;
@@ -83,7 +84,7 @@ class CopperConvertRequestService
public function approve(int $id, int $adminId): CopperConvertRequest
{
return DB::transaction(function () use ($id, $adminId) {
$row = DB::transaction(function () use ($id, $adminId) {
$row = CopperConvertRequest::query()->lockForUpdate()->findOrFail($id);
if ($row->status !== 'pending') {
throw new UnprocessableEntityHttpException('این درخواست قبلاً تعیین‌تکلیف شده است.');
@@ -113,6 +114,10 @@ class CopperConvertRequestService
return $row;
});
$this->notifyCustomer($row, true);
return $row;
}
public function reject(int $id, int $adminId): CopperConvertRequest
@@ -124,10 +129,58 @@ class CopperConvertRequestService
$row->status = 'rejected';
$row->admin_id = $adminId;
$row->save();
$this->notifyCustomer($row, false);
return $row;
}
private function notifyCustomer(CopperConvertRequest $row, bool $approved): void
{
try {
$customer = Customer::query()->find($row->customer_id);
$mobile = trim((string) ($customer?->mobile ?? ''));
if ($mobile === '') {
return;
}
$company = trim((string) Controller::Config('name_fa'));
$source = $this->sourceLabel((string) $row->source);
$lines = [$company !== '' ? $company : 'درخواست تبدیل'];
if ($approved) {
$lines[] = 'درخواست تبدیل #'.$row->id.' تأیید شد.';
$lines[] = 'دارایی '.$source.' به مقدار '.$row->consumed_amount.' '.$row->asset_unit.' فروخته شد.';
if ((int) $row->wire15_rings > 0) {
$lines[] = 'حلقه ۱.۵: '.$row->wire15_rings;
}
if ((int) $row->wire25_rings > 0) {
$lines[] = 'حلقه ۲.۵: '.$row->wire25_rings;
}
} else {
$lines[] = 'درخواست تبدیل #'.$row->id.' رد شد.';
$lines[] = 'دارایی '.$source.' شما بدون تغییر ماند.';
}
Controller::Send($mobile, implode("\n", $lines));
} catch (\Throwable $e) {
report($e);
}
}
private function sourceLabel(string $source): string
{
if ($source === 'katod') {
return 'کاتد';
}
if ($source === 'maftoul') {
return 'مفتول';
}
if ($source === 'shamsh') {
return 'شمش';
}
return $source;
}
private function placeLeg(Customer $customer, int $productId, int $direction, float $number, float $unitPrice, string $note): int
{
if ($productId < 1 || $number <= 0 || $unitPrice <= 0) {