work on moving customers from tahesab ۴
This commit is contained in:
@@ -215,15 +215,24 @@ class TahesabController extends Controller
|
|||||||
} while (Customer::where('referral_id', $referralId)->exists());
|
} while (Customer::where('referral_id', $referralId)->exists());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$customer = Customer::create([
|
$fullName = trim($item['name']);
|
||||||
'name' => trim($item['name']),
|
|
||||||
'fname' => trim($item['name']),
|
// توجه: مدل Customer یک mutator روی name دارد که name را از fname+lname میسازد
|
||||||
'mobile' => $mobile,
|
// و مقدار مستقیم name را نادیده میگیرد. پس باید fname را قبل از name ست کنیم.
|
||||||
'active' => true,
|
$customer = new Customer();
|
||||||
'moshtari_code' => $item['code'],
|
$customer->fname = $fullName;
|
||||||
'referral_id' => $referralId,
|
$customer->lname = '';
|
||||||
'type' => 2,
|
$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++;
|
$success++;
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$failed++;
|
$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)
|
public function retrySanad($id)
|
||||||
{
|
{
|
||||||
$tahesab = Tahesab::with('customer')->findOrFail($id);
|
$tahesab = Tahesab::with('customer')->findOrFail($id);
|
||||||
|
|||||||
@@ -156,6 +156,7 @@ Route::middleware('auth:user')->group(function (){
|
|||||||
Route::get('tahesab/customer-sync/status', [TahesabController::class, 'customerSyncStatus']);
|
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/panel-to-tahesab', [TahesabController::class, 'syncPanelToTahesab']);
|
||||||
Route::post('tahesab/customer-sync/import-from-tahesab', [TahesabController::class, 'importFromTahesab']);
|
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::get('tahesab/{id}', [TahesabController::class, 'show']);
|
||||||
Route::post('tahesab/{id}/retry', [TahesabController::class, 'retrySanad']);
|
Route::post('tahesab/{id}/retry', [TahesabController::class, 'retrySanad']);
|
||||||
Route::post('customer/{id}/tahesab/register', [TahesabController::class, 'registerCustomer']);
|
Route::post('customer/{id}/tahesab/register', [TahesabController::class, 'registerCustomer']);
|
||||||
|
|||||||
Reference in New Issue
Block a user