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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user