File: /www/wwwroot/erp.nhatnamsst.com/domains/Debt/DataTables/DebtDataTable.php
<?php
namespace Domains\Debt\DataTables;
use Domains\Core\DataTables\DataTables;
use Domains\Core\Enums\AdminRole;
use Domains\Core\Enums\DebtStatus;
use Domains\Debt\Repositories\Debt\DebtRepositoryInterface;
class DebtDataTable extends DataTables
{
public function __construct(
public DebtRepositoryInterface $repo
) {}
protected function setColumnHasSearch(): void
{
$this->columnHasSearch = ['invoice_number', 'invoice_date', 'debt_aging_date'];
}
protected function setRemoveColumns(): void
{
$currentAdmin = get_auth_admin();
$hasView = $currentAdmin && $currentAdmin->checkRouteNameAccessOrSuperAdmin('cms.debt.show');
$hasDelete = $currentAdmin && $currentAdmin->checkRouteNameAccessOrSuperAdmin('cms.debt.delete');
$hasAnyAction = $hasView || $hasDelete;
if (!$hasAnyAction) {
$this->removeColumns[] = 'action';
}
}
public function query()
{
$query = $this->repo->getQueryBuilder()->with(['sale', 'saleLeader', 'customer', 'details', 'payments']);
$currentAdmin = get_auth_admin();
if ($currentAdmin && !$currentAdmin->checkIsSuperAdmin()) {
// Sale chỉ thấy công nợ của mình
if ($currentAdmin->role === AdminRole::SALE) {
$query->where('sale_id', $currentAdmin->id);
}
// LeaderSale chỉ thấy công nợ của team mình
elseif ($currentAdmin->role === AdminRole::LEADER_SALE) {
$teamMemberIds = $currentAdmin->getTeamMemberIds();
$query->whereIn('sale_id', $teamMemberIds);
}
}
// Apply filters from request
if ($this->request) {
// Filter by invoice number
if ($this->request->has('filter_invoice_number') && $this->request->get('filter_invoice_number')) {
$invoiceNumber = $this->request->get('filter_invoice_number');
$query->where('invoice_number', 'like', '%' . $invoiceNumber . '%');
}
// Filter by customer
if ($this->request->has('filter_customer') && $this->request->get('filter_customer')) {
$customerId = $this->request->get('filter_customer');
$query->where('customer_id', $customerId);
}
// Filter by status
if ($this->request->has('filter_status') && $this->request->get('filter_status')) {
$status = $this->request->get('filter_status');
$query->where('status', $status);
}
}
return $query;
}
protected function setEditColumns(): void
{
$this->editColumns = [
'invoice_number' => 'cms.debts.datatable.invoice_number',
'invoice_date' => fn($row) => $row->invoice_date ? $row->invoice_date->format('d/m/Y') : '-',
'debt_aging_date' => fn($row) => $row->debt_aging_date ? $row->debt_aging_date->format('d/m/Y') : '-',
'sale_id' => fn($row) => $row->sale ? ($row->sale->code ?? '') . ($row->sale->code ? ' - ' : '') . $row->sale->fullname : '-',
'sale_leader_id' => fn($row) => $row->saleLeader ? ($row->saleLeader->code ?? '') . ($row->saleLeader->code ? ' - ' : '') . $row->saleLeader->fullname : '-',
'customer_id' => fn($row) => $row->customer ? $row->customer->name : '-',
'total_po_price' => function($row) {
return number_format($row->total_po_price, 0, ',', '.');
},
'total_payment_amount' => function($row) {
return number_format($row->total_payment_amount, 0, ',', '.');
},
'remaining_amount' => function($row) {
return number_format($row->remaining_amount, 0, ',', '.');
},
'status' => function($row) {
$status = $row->status instanceof DebtStatus ? $row->status : DebtStatus::from($row->status ?? 1);
return view('cms.debts.datatable.status', ['status' => $status])->render();
},
];
}
protected function setAddColumns(): void
{
$this->addColumns = [
'action' => 'cms.debts.datatable.action',
];
}
protected function setRawColumns(): void
{
$this->rawColumns = ['invoice_number', 'status', 'action'];
}
protected function setConfigColumns(): void
{
$this->configColumns = [
'invoice_number' => [
'title' => 'Số hóa đơn',
'orderable' => true,
],
'invoice_date' => [
'title' => 'Ngày xuất HĐ',
'orderable' => true,
],
'debt_aging_date' => [
'title' => 'Ngày công nợ',
'orderable' => true,
],
'sale_id' => [
'title' => 'Sale',
'orderable' => false,
],
'sale_leader_id' => [
'title' => 'Sale Leader',
'orderable' => false,
],
'customer_id' => [
'title' => 'Khách hàng',
'orderable' => false,
],
'total_po_price' => [
'title' => 'Tổng giá PO',
'orderable' => false,
'addClass' => 'text-right',
],
'total_payment_amount' => [
'title' => 'Tổng thanh toán',
'orderable' => false,
'addClass' => 'text-right',
],
'remaining_amount' => [
'title' => 'Còn lại',
'orderable' => false,
'addClass' => 'text-right',
],
'status' => [
'title' => 'Trạng thái',
'orderable' => true,
'width' => '120px',
'addClass' => 'text-center',
],
'action' => [
'title' => 'Action',
'orderable' => false,
'exportable' => false,
'printable' => false,
'addClass' => 'text-center',
'width' => '120px'
]
];
}
}