Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Staff #30

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/Http/Controllers/Admin/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,19 @@ public function status(Request $request)
return redirect()->back()->withSuccess('Order Status Has Been Updated.');
}

public function staff(Request $request)
{
$request->validate([
'admin_id' => 'required',
'order_id' => 'required|array',
]);

$data['admin_id'] = $request->admin_id;
Order::whereIn('id', $request->order_id)->where('admin_id', '!=', $request->admin_id)->update($data);

return redirect()->back()->withSuccess('Order Staff Has Been Updated.');
}

public function updateQuantity(Request $request, Order $order)
{
$quantities = $request->quantity;
Expand Down
15 changes: 14 additions & 1 deletion app/Http/Controllers/Api/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function __invoke(Request $request)
$orders->latest('id');
});

$salesmans = Admin::where('role_id', Admin::SALESMAN)->get(['id', 'name'])->pluck('name', 'id');

return DataTables::of($orders)
->addIndexColumn()
->setRowAttr([
Expand Down Expand Up @@ -144,6 +146,17 @@ public function __invoke(Request $request)
$query->where('data->courier', 'like', '%' . $keyword . '%')
->orWhere('data->consignment_id', 'like', '%' . $keyword . '%');
})
->editColumn('staff', function ($row) use ($salesmans) {
$return = '<select data-id="' . $row->id . '" onchange="changeStaff" class="staff-column form-control-sm">';
if (!isset($salesmans[$row->admin_id])) {
$return .= '<option value="' . $row->admin_id . '" selected>' . $row->admin->name . '</option>';
}
foreach ($salesmans as $id => $name) {
$return .= '<option value="' . $id . '" ' . ($id == $row->admin_id ? 'selected' : '') . '>' . $name . '</option>';
}
$return .= '</select>';
return $return;
})
->filterColumn('created_at', function ($query, $keyword) {
if (str_contains($keyword, ' - ')) {
[$start, $end] = explode(' - ', $keyword);
Expand All @@ -153,7 +166,7 @@ public function __invoke(Request $request)
]);
}
})
->rawColumns(['checkbox', 'id', 'customer', 'products', 'status', 'courier', 'created_at', 'actions'])
->rawColumns(['checkbox', 'id', 'customer', 'products', 'status', 'courier', 'staff', 'created_at', 'actions'])
->make(true);
}
}
35 changes: 34 additions & 1 deletion resources/views/admin/orders/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ className: 'px-1 py-1 {{ request('status') == '' ? 'btn-secondary' : '' }}',
{ data: 'amount', name: 'amount', sortable: false },
{ data: 'status', name: 'status', sortable: false },
{ data: 'courier', name: 'courier', sortable: false },
{ data: 'admin.name', name: 'admin.name', sortable: false },
{ data: 'staff', name: 'admin.name', sortable: false },
{ data: 'created_at', name: 'created_at' },
],
initComplete: function (settings, json) {
Expand Down Expand Up @@ -351,6 +351,39 @@ function changeCourier() {
});
}

$(document).on('change', '.staff-column', changeStaff);

function changeStaff() {
$('[name="staff"]').prop('disabled', true);

var order_id = Array.from(checklist);
var staff = $('[name="staff"]').val();
if ($(this).data('id')) {
order_id = [$(this).data('id')];
staff = $(this).val();
}

$.post({
url: '{{ route('admin.orders.staff') }}',
data: {
_token: '{{ csrf_token() }}',
order_id: order_id,
admin_id: staff,
},
success: function (response) {
checklist.clear();
updateBulkMenu();
table.draw();

$.notify('Staff updated successfully', 'success');
},
complete: function () {
$('[name="staff"]').prop('disabled', false);
$('[name="staff"]').val('');
}
});
}

setInterval(function () {
$('.datatable').DataTable().ajax.reload(function (res) {
if (res.recordsTotal > window.ordersTotal) {
Expand Down
1 change: 1 addition & 0 deletions routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
Route::get('/orders/booking', 'OrderController@booking')->name('orders.booking');
Route::post('/orders/change-courier', 'OrderController@courier')->name('orders.courier');
Route::post('/orders/change-status', 'OrderController@status')->name('orders.status');
Route::post('/orders/change-staff', 'OrderController@staff')->name('orders.staff');
Route::patch('/orders/{order}/update-quantity', 'OrderController@updateQuantity')->name('orders.update-quantity');
Route::post('/logout-others/{admin}', 'ApiController@logoutOthers')->name('logout-others');
Route::get('/customers', 'CustomerController')->name('customers');
Expand Down
Loading