This commit is contained in:
Naz 2025-03-19 16:41:40 +08:00
parent bdb17f766c
commit c338ee7c6c
4 changed files with 119 additions and 83 deletions

View File

@ -19,23 +19,6 @@ namespace PSTW_CentralSystem.Areas.OTcalculate.Controllers
_context = context;
}
[HttpGet]
public async Task<IActionResult> GetRateList()
{
var users = await _context.Users
.Where(u => u.Id != 1 && u.Id != 2)
.Include(u => u.Department)
.Select(u => new RateModel
{
Id = u.Id.ToString(),
FullName = u.FullName,
departmentId = u.departmentId,
DepartmentName = u.Department != null ? u.Department.DepartmentName : "N/A"
})
.ToListAsync();
return Json(users);
}
public IActionResult Rate()
{
@ -56,5 +39,73 @@ namespace PSTW_CentralSystem.Areas.OTcalculate.Controllers
{
return View();
}
#region Rates
[HttpPost("UpdateRates")]
public async Task<IActionResult> UpdateRate([FromBody] List<RateModel> rates)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
foreach (var Rate in rates)
{
var existingRate = await _context.Rates.FindAsync(Rate.RateId);
if (existingRate != null)
{
existingRate.RateValue = Rate.RateValue;
_context.Rates.Update(existingRate);
}
}
await _context.SaveChangesAsync();
var updateRates = await _context.Rates
.Include(rates => rates.Users)
.Select(rates => new
{
rates.RateId,
rates.RateValue,
rates.UserId,
FullName = rates.Users.FullName,
DepartmentName = rates.Users.DepartmentName
})
.ToListAsync();
return Json(updateRates);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpPost("GetUserRates")]
public async Task<IActionResult> GetUserRates()
{
try
{
var userRates = await _context.Rates
.Include(rates => rates.Users)
.Select(rates => new
{
rates.RateId,
rates.RateValue,
rates.UserId,
FullName = rates.Users.FullName,
DepartmentName = rates.Users.DepartmentName
})
.ToListAsync();
return Json(userRates);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
#endregion
}
}

View File

@ -7,16 +7,15 @@ namespace PSTW_CentralSystem.Areas.OTcalculate.Models
public class RateModel
{
[Key]
public required string Id { get; set; }
public int RateId { get; set; }
public required string FullName { get; set; }
public decimal RateValue { get; set; }
public int? departmentId { get; set; }
public int UserId { get; set; }
[ForeignKey("departmentId")]
public virtual DepartmentModel? Department { get; set; }
[ForeignKey("UserId")]
public string? DepartmentName { get; set; }
public virtual UserModel? Users { get; set; }
}
}

View File

@ -46,38 +46,45 @@
<div class="col-md-12">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="row update-form card-body">
<div class="card-header" style="background-color: white;">
<h3 class="rate-heading text-center">UPDATE RATE</h3>
</div>
<div class="row update-form card-body">
<div class="col-md-6">
@* Enter Rate *@
<div class="form-group row">
<label for="rate" class="col-sm-3">Rate</label>
<div class="col-sm-9">
<input type="number" id="rate" class="form-control" v-model="rate" placeholder="Enter new rateeeee">
<input type="number" id="rate" class="form-control" v-model="rate" placeholder="Enter new rate">
</div>
</div>
</div>
</div>
</div>
@* User Table Rate *@
<div id="app">
<div class="row">
<div class="col-md-12 col-lg-12">
<div class="card">
<div class="card-body">
<div class="col-md-12 col-lg-12">
<div>
<table class="table table-bordered table-hover table-striped no-wrap align-middle" id="rateTable" style="width:100%;border-style: solid; border-width: 1px"></table>
</div>
</div>
</div>
</div>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Full Name</th>
<th>Department</th>
<th>Select Rate</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" :key="user.UserId">
<td>{{ user.FullName }}</td>
<td>{{ user.DepartmentName }}</td>
<td>
<input type="checkbox" v-model="selectedRates" :value="user.RateId">
</td>
</tr>
</tbody>
</table>
<button @click ="updateRates" class="btn btn-primary">Update Rates</button>
</div>
</div>
</div>
@ -93,54 +100,32 @@
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
<script>
$(function () {
app.mount('#app');
});
const app = Vue.createApp({
data() {
return {
rateList: [],
};
new Vue({
el: "#app",
data: {
users: [],
selectedRates: []
},
mounted() {
this.fetchRates();
},
methods: {
async fetchRates() {
try {
let response = await fetch('/OTcalculate/HrDashboard/GetRateList', { method: 'GET' });
let data = await response.json();
this.rateList = data.length ? data : [];
this.$nextTick(() => {
if (this.rateTable) {
this.rateTable.clear().destroy();
}
this.initiateTable();
const response = await fetch("/HrDashboard/GetUserRates", {
method: "POST",
headers: { "Content-Type": "application/json" }
});
} catch (error) {
console.error('Error fetching rates:', error);
}
this.users = await response.json();
},
async initiateTable() {
self = this;
this.rateTable = $('#rateTable').DataTable({
"data": self.rateList,
"columns": [
{
"title": "Full Name",
"data": "fullName",
},
{
"title": "Department",
"data": "departmentName",
}
],
responsive: true,
order: [[0, 'asc']],
async updateRates() {
const response = await fetch("/HrDashboard/UpdateRates", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(this.selectedRates.map(id => ({ RateId: id })))
});
const updatedUsers = await response.json();
this.users = updatedUsers;
}
}
});

View File

@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Newtonsoft.Json;
using PSTW_CentralSystem.Areas.Inventory.Models;
using PSTW_CentralSystem.Areas.OTcalculate.Models;
using PSTW_CentralSystem.Models;
using System.Text.Json;
@ -98,6 +99,6 @@ namespace PSTW_CentralSystem.DBContext
public DbSet<ItemMovementModel> ItemMovements { get; set; }
public DbSet<StationModel> Stations { get; set; }
public DbSet<StoreModel> Stores { get; set; }
public DbSet<RateModel> Rates { get; set; }
}
}