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; _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() public IActionResult Rate()
{ {
@ -56,5 +39,73 @@ namespace PSTW_CentralSystem.Areas.OTcalculate.Controllers
{ {
return View(); 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 public class RateModel
{ {
[Key] [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")] [ForeignKey("UserId")]
public virtual DepartmentModel? Department { get; set; }
public string? DepartmentName { get; set; } public virtual UserModel? Users { get; set; }
} }
} }

View File

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

View File

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