feat: Implement soft deletes for customer, invoice, order, and transaction models, and add customer edit and trash management to the admin panel.

This commit is contained in:
pbao9
2026-02-16 17:04:27 +07:00
parent 4dae15ffca
commit 31cbdf323f
22 changed files with 560 additions and 34 deletions
+171 -16
View File
@@ -7,6 +7,9 @@ use App\Http\Facade\SignRSA;
use App\Http\Requests\AddPayRequest;
use App\Http\Requests\CreateCustomerRequest;
use App\Jobs\Mail\PasswordRecoverJob;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\Transaction;
use App\Repositories\Customer\CustomerRepositoryInterface;
use App\Repositories\Hosting\HostingRepositoryInterface;
use App\Repositories\Invoice\InvoiceRepositoryInterface;
@@ -32,7 +35,7 @@ use Yajra\DataTables\Facades\DataTables;
class CustomerController extends Controller
{
public function __construct(
public CustomerRepositoryInterface $customerRepository,
public InvoiceItemRepositoryInterface $invoiceItemRepository,
@@ -46,7 +49,8 @@ class CustomerController extends Controller
public InvoiceRepositoryInterface $invoiceRepository,
public TransactionRepositoryInterface $transactionRepository,
public MailRepositoryInterface $mailRepository
) {}
) {
}
public function index(Request $request)
{
@@ -222,7 +226,7 @@ class CustomerController extends Controller
return redirect()->route('customer.index')->withErrors('No data found for this user');
}
$customer = $this->customerRepository->find($userid);
if (! $customer) {
if (!$customer) {
return redirect()->route('customer.index')->withErrors('Invalidated Request');
}
@@ -231,15 +235,15 @@ class CustomerController extends Controller
['customer_id' => $userid],
['server']
);
$vpsServices = $this->vmwareRepository->filter(
['customer_id' => $userid],
['ipaddress.host.datacenter.vCenter', 'os.osGroup']
);
// Kết hợp và format dữ liệu
$allService = collect();
foreach ($hostingServices as $hosting) {
$allService->push((object) [
'product_id' => $hosting->id,
@@ -248,7 +252,7 @@ class CustomerController extends Controller
'description' => $hosting->name . ' (Hosting)'
]);
}
foreach ($vpsServices as $vps) {
$allService->push((object) [
'product_id' => $vps->id,
@@ -257,7 +261,7 @@ class CustomerController extends Controller
'description' => $vps->name . ' (VPS)'
]);
}
// Lấy invoice_item cụ thể cho sản phẩm
$invoice_item = $this->invoiceItemRepository->findBy(
$userid,
@@ -267,11 +271,11 @@ class CustomerController extends Controller
'value' => $userid
]
);
$type = null;
$product = null;
$productName = '';
if ($invoice_item) {
$product_id = $invoice_item->product_id ?? null;
$product_module_id = $request->get('product') ?? $invoice_item->product_id ?? null;
@@ -326,7 +330,7 @@ class CustomerController extends Controller
public function storeCustomerProduct(Request $request)
{
if (! $request->product_type) {
if (!$request->product_type) {
return back()->with($request->except(['_token', '_method']))->withErrors('Invalid Request');
}
@@ -335,7 +339,7 @@ class CustomerController extends Controller
$rules = [];
switch ($request->product_type) {
case 3:
$rules = [
$rules = [
'product_id' => 'required',
'next_due_date' => 'required',
'next_payment' => 'required',
@@ -360,7 +364,7 @@ class CustomerController extends Controller
if ($validator->fails()) {
return back()->withErrors($validator->errors()->first());
}
if (! $product) {
if (!$product) {
return back()->withErrors('Product not found');
}
try {
@@ -418,7 +422,7 @@ class CustomerController extends Controller
return redirect()->route('customer.index')->withErrors('No data found for this user');
}
$customer = $this->customerRepository->find($userid);
if (! $customer) {
if (!$customer) {
return redirect()->route('customer.index')->withErrors('Invalid Request');
}
$domains = $this->moduleDomainRepository->filter(
@@ -542,7 +546,7 @@ class CustomerController extends Controller
$customer = $this->customerRepository->find($userid);
if (! $customer) {
if (!$customer) {
return redirect()->route('customer.index')->withErrors('Invalid Request');
}
@@ -713,7 +717,7 @@ class CustomerController extends Controller
}
$customer = $this->customerRepository->find($request->get('userid'));
if (! $customer) {
if (!$customer) {
return back()->withErrors('Invalid request');
}
Auth::guard('users')->login($customer);
@@ -739,12 +743,163 @@ class CustomerController extends Controller
);
return DataTables::eloquent($query)
->addColumn('status', function ($d) {
return view('administrator.pages.customer.datatable.status', ['row' => $d])->render();
})
->addColumn(
'action',
function ($d) {
return view('administrator.pages.customer.datatable.action', ['row' => $d])->render();
}
)
->rawColumns(['status', 'action'])
->make(true);
}
public function edit(int $id = 0)
{
$customer = $this->customerRepository->find($id);
if (!$customer) {
return back()->withErrors('Invalid request.');
}
$title = 'Edit Customer';
return view('administrator.pages.customer.edit', compact('title', 'customer'));
}
public function update(int $id = 0, CreateCustomerRequest $request)
{
$customer = $this->customerRepository->find($id);
if (!$customer) {
return back()->withErrors('Invalid request.');
}
$request->validated();
try {
DB::beginTransaction();
$data = $request->except(['_token', 'password']);
if (!empty($request->password)) {
$data['password'] = password_hash($request->password, PASSWORD_BCRYPT);
}
$this->customerRepository->update($id, $data);
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('customer.index')->with('success', 'Update customer successfully!');
}
public function destroy(int $id = 0)
{
$customer = $this->customerRepository->find($id);
if (!$customer) {
return back()->withErrors('Invalid request.');
}
DB::beginTransaction();
try {
Invoice::where('customer_id', $id)->delete();
Order::where('customer_id', $id)->delete();
Transaction::where('customer_id', $id)->delete();
$customer->delete();
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('customer.index')->with('success', 'Deleted customer successfully!');
}
public function trash()
{
$title = 'Trash Customers';
return view('administrator.pages.customer.trash', compact('title'));
}
public function datatableTrash(Request $request)
{
$query = $this->customerRepository->onlyTrashed(
[],
['hosting', 'vps', 'domain']
);
return DataTables::eloquent($query)
->addColumn('status', function ($d) {
return view('administrator.pages.customer.datatable.status', ['row' => $d])->render();
})
->addColumn(
'action',
function ($d) {
return view('administrator.pages.customer.datatable.action-trash', ['row' => $d])->render();
}
)
->rawColumns(['status', 'action'])
->make(true);
}
public function restore(int $id = 0)
{
$customer = $this->customerRepository->find($id);
if (!$customer) {
$customer = $this->customerRepository->onlyTrashed()->find($id);
}
if (!$customer) {
return back()->withErrors('Invalid request.');
}
DB::beginTransaction();
try {
$customer->restore();
Invoice::withTrashed()->where('customer_id', $id)->restore();
Order::withTrashed()->where('customer_id', $id)->restore();
Transaction::withTrashed()->where('customer_id', $id)->restore();
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return back()->with('success', 'Restored customer successfully!');
}
public function forceDelete(int $id = 0)
{
$customer = $this->customerRepository->onlyTrashed()->find($id);
if (!$customer) {
return back()->withErrors('Invalid request.');
}
DB::beginTransaction();
try {
Invoice::withTrashed()->where('customer_id', $id)->forceDelete();
Order::withTrashed()->where('customer_id', $id)->forceDelete();
Transaction::withTrashed()->where('customer_id', $id)->forceDelete();
$customer->forceDelete();
DB::commit();
} catch (Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return back()->with('success', 'Permanently deleted customer!');
}
}