work on moving customers from tahesab ۴

This commit is contained in:
2026-06-29 01:05:47 +03:30
parent 5fffdf1ecd
commit 8ac18c6f03
2 changed files with 54 additions and 9 deletions
@@ -215,15 +215,24 @@ class TahesabController extends Controller
} while (Customer::where('referral_id', $referralId)->exists());
try {
$customer = Customer::create([
'name' => trim($item['name']),
'fname' => trim($item['name']),
'mobile' => $mobile,
'active' => true,
'moshtari_code' => $item['code'],
'referral_id' => $referralId,
'type' => 2,
]);
$fullName = trim($item['name']);
// توجه: مدل Customer یک mutator روی name دارد که name را از fname+lname می‌سازد
// و مقدار مستقیم name را نادیده می‌گیرد. پس باید fname را قبل از name ست کنیم.
$customer = new Customer();
$customer->fname = $fullName;
$customer->lname = '';
$customer->name = $fullName; // mutator: name = fname (+ lname)
$customer->mobile = $mobile;
$customer->active = true;
$customer->moshtari_code = $item['code'];
$customer->referral_id = $referralId;
$customer->type = 2;
// رمز عبور = شماره موبایل (فقط وقتی موبایل موجود است)
if ($mobile !== '') {
$customer->password = $mobile;
}
$customer->save();
$success++;
} catch (\Throwable $e) {
$failed++;
@@ -243,6 +252,41 @@ class TahesabController extends Controller
]);
}
/**
* حذف مشتریانِ خرابِ واردشده از ته‌حساب (نام خالی) برای پاک‌سازی import ناقص.
* با dry_run=1 فقط تعداد و نمونه را نشان می‌دهد و چیزی حذف نمی‌کند.
*/
public function rollbackImport(Request $request): \Illuminate\Http\JsonResponse
{
// مشتریانِ ساخته‌شده توسط import که name آن‌ها خالی مانده (به‌خاطر mutator)
$baseQuery = fn () => Customer::whereNotNull('moshtari_code')
->where(function ($q) {
$q->whereNull('name')->orWhere('name', '');
});
$count = $baseQuery()->count();
$sample = $baseQuery()
->select('id', 'fname', 'name', 'mobile', 'moshtari_code', 'created_at')
->orderByDesc('id')
->limit(20)
->get();
if ($request->boolean('dry_run')) {
return response()->json([
'dry_run' => true,
'count' => $count,
'sample' => $sample,
]);
}
$deleted = $baseQuery()->forceDelete();
return response()->json([
'ok' => true,
'deleted' => $deleted,
]);
}
public function retrySanad($id)
{
$tahesab = Tahesab::with('customer')->findOrFail($id);
+1
View File
@@ -156,6 +156,7 @@ Route::middleware('auth:user')->group(function (){
Route::get('tahesab/customer-sync/status', [TahesabController::class, 'customerSyncStatus']);
Route::post('tahesab/customer-sync/panel-to-tahesab', [TahesabController::class, 'syncPanelToTahesab']);
Route::post('tahesab/customer-sync/import-from-tahesab', [TahesabController::class, 'importFromTahesab']);
Route::post('tahesab/customer-sync/rollback-import', [TahesabController::class, 'rollbackImport']);
Route::get('tahesab/{id}', [TahesabController::class, 'show']);
Route::post('tahesab/{id}/retry', [TahesabController::class, 'retrySanad']);
Route::post('customer/{id}/tahesab/register', [TahesabController::class, 'registerCustomer']);